Bugfix Websocket hostname. (#21)

* Add yarn-error.log to gitignore.

* Add ts-node to devDependencies.
Add script to only build tsc.
Add websocket to keywords.

* Update yarn.lock libs.

* FIX websocket endpoint hostname.
FIX corrent import for all examples.
FIX websocket instrucions on readme.
This commit is contained in:
Miguel Medeiros 2021-05-20 12:03:40 -03:00 committed by GitHub
parent 0aa3757217
commit e3068c2d8d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
28 changed files with 236 additions and 169 deletions

2
.gitignore vendored
View File

@ -2,3 +2,5 @@
/lib
/dist/*
!.gitkeep
yarn-error.log

View File

@ -630,20 +630,22 @@ Default push: `{ action: 'want', data: ['blocks', ...] }` to express what you wa
Push transactions related to address: `{ 'track-address': '3PbJ...bF9B' }` to receive all new transactions containing that address as input or output. Returns an array of transactions. address-transactions for new mempool transactions, and block-transactions for new block confirmed transactions.
[ [NodeJS Example](examples/nodejs/bitcoin/addresses.ts) ] [ [HTML Example](examples/html/bitcoin/addresses.html) ] [ [Top](#features) ]
[ [NodeJS Example](examples/nodejs/bitcoin/websocket.ts) ] [ [HTML Example](examples/html/bitcoin/websocket.html) ] [ [Top](#features) ]
#### **Websocket Server**
Only use on server side apps.
```js
const {
bitcoin: { websocket },
} = mempoolJS();
const { bitcoin: { websocket } } = mempoolJS();
const init = async () => {
const ws = websocket.initServer({
options: ['blocks', 'stats', 'mempool-blocks', 'live-2h-chart'],
options: ["blocks", "stats", "mempool-blocks", "live-2h-chart"],
});
ws.on('message', function incoming(data) {
ws.on("message", function incoming(data) {
const res = JSON.parse(data.toString());
if (res.blocks) {
console.log(res.blocks);
@ -658,11 +660,16 @@ ws.on('message', function incoming(data) {
console.log(res.mempoolBlocks);
}
});
}
init();
```
#### **Websocket Client**
Only use on browser apps.
```js
const init = async () => {
const {
bitcoin: { websocket },
} = mempoolJS();
@ -671,7 +678,7 @@ const ws = websocket.initClient({
options: ['blocks', 'stats', 'mempool-blocks', 'live-2h-chart'],
});
ws.on('message', function incoming(data) {
ws.addEventListener('message', function incoming({data}) {
const res = JSON.parse(data.toString());
if (res.blocks) {
console.log(res.blocks);
@ -686,4 +693,6 @@ ws.on('message', function incoming(data) {
console.log(res.mempoolBlocks);
}
});
};
init();
```

View File

@ -698,20 +698,22 @@ Default push: `{ action: 'want', data: ['blocks', ...] }` to express what you wa
Push transactions related to address: `{ 'track-address': '3PbJ...bF9B' }` to receive all new transactions containing that address as input or output. Returns an array of transactions. address-transactions for new mempool transactions, and block-transactions for new block confirmed transactions.
[ [NodeJS Example](examples/nodejs/liquid/addresses.ts) ] [ [HTML Example](examples/html/liquid/addresses.html) ] [ [Top](#features) ]
[ [NodeJS Example](examples/nodejs/liquid/websocket.ts) ] [ [HTML Example](examples/html/liquid/websocket.html) ] [ [Top](#features) ]
#### **Websocket Server**
Only use on server side apps.
```js
const {
liquid: { websocket },
} = mempoolJS();
const { liquid: { websocket } } = mempoolJS();
const init = async () => {
const ws = websocket.initServer({
options: ['blocks', 'stats', 'mempool-blocks', 'live-2h-chart'],
options: ["blocks", "stats", "mempool-blocks", "live-2h-chart"],
});
ws.on('message', function incoming(data) {
ws.on("message", function incoming(data) {
const res = JSON.parse(data.toString());
if (res.blocks) {
console.log(res.blocks);
@ -726,11 +728,16 @@ ws.on('message', function incoming(data) {
console.log(res.mempoolBlocks);
}
});
}
init();
```
#### **Websocket Client**
Only use on browser apps.
```js
const init = async () => {
const {
liquid: { websocket },
} = mempoolJS();
@ -739,7 +746,7 @@ const ws = websocket.initClient({
options: ['blocks', 'stats', 'mempool-blocks', 'live-2h-chart'],
});
ws.on('message', function incoming(data) {
ws.addEventListener('message', function incoming({data}) {
const res = JSON.parse(data.toString());
if (res.blocks) {
console.log(res.blocks);
@ -754,4 +761,6 @@ ws.on('message', function incoming(data) {
console.log(res.mempoolBlocks);
}
});
};
init();
```

View File

@ -8,12 +8,12 @@
const {
bitcoin: { websocket },
} = mempoolJS();
// console.log(mempoolJS());
const ws = websocket.initClient({
options: ['blocks', 'stats', 'mempool-blocks', 'live-2h-chart'],
});
ws.on('message', function incoming(data) {
ws.addEventListener('message', function incoming({data}) {
const res = JSON.parse(data.toString());
if (res.blocks) {
console.log(res.blocks);

View File

@ -9,11 +9,11 @@
liquid: { websocket },
} = mempoolJS();
const ws = websocket.initServer({
const ws = websocket.initClient({
options: ['blocks', 'stats', 'mempool-blocks', 'live-2h-chart'],
});
ws.on('message', function incoming(data) {
ws.addEventListener('message', function incoming({data}) {
const res = JSON.parse(data.toString());
if (res.blocks) {
console.log(res.blocks);

View File

@ -1,4 +1,4 @@
import mempoolJS from '../../../src/index';
import mempoolJS from "@mempool/mempool.js";
const init = async () => {
const {

View File

@ -1,4 +1,4 @@
import mempoolJS from '../../../src/index';
import mempoolJS from "@mempool/mempool.js";
const init = async () => {
const {

View File

@ -1,4 +1,4 @@
import mempoolJS from '../../../src/index';
import mempoolJS from "@mempool/mempool.js";
const init = async () => {
const {

View File

@ -1,4 +1,4 @@
import mempoolJS from '../../../src/index';
import mempoolJS from "@mempool/mempool.js";
const init = async () => {
const {

View File

@ -1,4 +1,4 @@
import mempoolJS from '../../../src/index';
import mempoolJS from "@mempool/mempool.js";
const init = async () => {
const {

View File

@ -1,4 +1,4 @@
import mempoolJS from '../../../src/index';
import mempoolJS from "@mempool/mempool.js";
const init = async () => {
const {

View File

@ -1,4 +1,4 @@
import mempoolJS from '../../../src/index';
import mempoolJS from "@mempool/mempool.js";
const init = async () => {
const {

View File

@ -1,4 +1,4 @@
import mempoolJS from '../../../src/index';
import mempoolJS from "@mempool/mempool.js";
const init = async () => {
const {

View File

@ -1,4 +1,4 @@
import mempoolJS from '../../../src/index';
import mempoolJS from "@mempool/mempool.js";
const init = async () => {
const {

View File

@ -1,15 +1,14 @@
import mempoolJS from '../../../src/index';
import mempoolJS from "@mempool/mempool.js";
const { bitcoin: { websocket } } = mempoolJS();
const init = async () => {
const {
bitcoin: { websocket },
} = mempoolJS();
const ws = websocket.initServer({
options: ['blocks', 'stats', 'mempool-blocks', 'live-2h-chart'],
options: ["blocks", "stats", "mempool-blocks", "live-2h-chart"],
});
ws.on('message', function incoming(data) {
ws.on("message", function incoming(data) {
const res = JSON.parse(data.toString());
if (res.blocks) {
console.log(res.blocks);
@ -24,5 +23,5 @@ const init = async () => {
console.log(res.mempoolBlocks);
}
});
};
}
init();

View File

@ -1,4 +1,4 @@
import mempoolJS from '../../../src/index';
import mempoolJS from "@mempool/mempool.js";
const init = async () => {
const {

View File

@ -1,4 +1,4 @@
import mempoolJS from '../../../src/index';
import mempoolJS from "@mempool/mempool.js";
const init = async () => {
const {

View File

@ -1,4 +1,4 @@
import mempoolJS from '../../../src/index';
import mempoolJS from "@mempool/mempool.js";
const init = async () => {
const {

View File

@ -1,4 +1,4 @@
import mempoolJS from '../../../src/index';
import mempoolJS from "@mempool/mempool.js";
const init = async () => {
const {

View File

@ -1,4 +1,4 @@
import mempoolJS from '../../../src/index';
import mempoolJS from "@mempool/mempool.js";
const init = async () => {
const {

View File

@ -1,4 +1,4 @@
import mempoolJS from '../../../src/index';
import mempoolJS from "@mempool/mempool.js";
const init = async () => {
const {

View File

@ -1,15 +1,14 @@
import mempoolJS from '../../../src/index';
import mempoolJS from "@mempool/mempool.js";
const { liquid: { websocket } } = mempoolJS();
const init = async () => {
const {
liquid: { websocket },
} = mempoolJS();
const ws = websocket.initServer({
options: ['blocks', 'stats', 'mempool-blocks', 'live-2h-chart'],
options: ["blocks", "stats", "mempool-blocks", "live-2h-chart"],
});
ws.on('message', function incoming(data) {
ws.on("message", function incoming(data) {
const res = JSON.parse(data.toString());
if (res.blocks) {
console.log(res.blocks);
@ -24,5 +23,5 @@ const init = async () => {
console.log(res.mempoolBlocks);
}
});
};
}
init();

View File

@ -16,6 +16,7 @@
"mempool.space",
"mempool.js",
"mempool",
"websocket",
"nodejs",
"typescript"
],
@ -31,6 +32,7 @@
"start": "ts-node src/index.ts",
"dev": "nodemon src/index.ts",
"build": "tsc | browserify lib/index.js --standalone mempoolJS > dist/mempool.js | browserify -p tinyify lib/index.js --standalone mempoolJS > dist/mempool.min.js",
"build-tsc": "tsc",
"prepare": "npm run build",
"postversion": "git push && git push --tags"
},
@ -51,6 +53,7 @@
"eslint": "^7.19.0",
"nodemon": "^2.0.7",
"tinyify": "^3.0.0",
"ts-node": "^9.1.1",
"typescript": "^4.1.3"
},
"license": "MIT"

View File

@ -2,13 +2,13 @@ import { WsInterface, WsInstance } from '../../interfaces/bitcoin/websockets';
import wsClient from '../../services/ws/client';
import wsServer from '../../services/ws/server';
const defaultWs = 'wss://mempool.space/api/v1/ws';
export const useWebsocket = (hostname: string): WsInstance => {
export const useWebsocket = (hostname?: string): WsInstance => {
const wsEndpoint = `wss://${hostname}/api/v1/ws`;
return {
initClient: ({ options }: WsInterface) =>
wsClient(options, defaultWs, hostname),
wsClient(options, wsEndpoint),
initServer: ({ options }: WsInterface) =>
wsServer(options, defaultWs, hostname),
wsServer(options, wsEndpoint),
};
};

View File

@ -2,13 +2,13 @@ import { WsInterface, WsInstance } from '../../interfaces/bitcoin/websockets';
import wsClient from '../../services/ws/client';
import wsServer from '../../services/ws/server';
const defaultWs = 'wss://mempool.space/liquid/api/v1/ws';
export const useWebsocket = (hostname: string): WsInstance => {
export const useWebsocket = (hostname?: string): WsInstance => {
const wsEndpoint = `wss://${hostname}/liquid/api/v1/ws`;
return {
initClient: ({ options }: WsInterface) =>
wsClient(options, defaultWs, hostname),
wsClient(options, wsEndpoint),
initServer: ({ options }: WsInterface) =>
wsServer(options, defaultWs, hostname),
wsServer(options, wsEndpoint),
};
};

View File

@ -1,23 +1,25 @@
const browserWS = (
const serverWS = (
options: string[],
defaultWs: string,
websocketEndpoint?: string
endpoint: string,
): WebSocket => {
const ws = new WebSocket(websocketEndpoint || defaultWs);
ws.addEventListener('open', function open() {
handleMessage(ws, options);
const ws = new WebSocket(endpoint);
ws.addEventListener("open", function open() {
ws.send(JSON.stringify({ action: "want", data: options }));
});
ws.addEventListener("close", async function close() {
await sleep(60000);
serverWS(options, endpoint);
});
return ws;
}
const sleep = (ms: number) => {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
};
const handleMessage = (ws: WebSocket, options: string[]) => {
ws.send(JSON.stringify({ action: 'init' }));
ws.send(
JSON.stringify({
action: 'want',
data: options,
})
);
};
export default serverWS;
export default browserWS;

View File

@ -2,23 +2,30 @@ import WebSocket from 'ws';
const serverWS = (
options: string[],
defaultWs: string,
websocketEndpoint?: string
endpoint: string,
): WebSocket => {
const ws = new WebSocket(websocketEndpoint || defaultWs);
ws.on('open', function open() {
handleMessage(ws, options);
const ws = new WebSocket(endpoint);
const interval = setInterval(function ping() {
ws.ping();
}, 30000);
ws.on("open", function open() {
ws.send(JSON.stringify({ action: "want", data: options }));
});
ws.on("close", async function close() {
clearInterval(interval);
ws.terminate();
await sleep(60000);
serverWS(options, endpoint);
});
return ws;
}
const sleep = (ms: number) => {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
};
const handleMessage = (ws: WebSocket, options: string[]) => {
ws.send(JSON.stringify({ action: 'init' }));
ws.send(
JSON.stringify({
action: 'want',
data: options,
})
);
};
export default serverWS;

View File

@ -321,6 +321,11 @@ anymatch@~3.1.1:
normalize-path "^3.0.0"
picomatch "^2.0.4"
arg@^4.1.0:
version "4.1.3"
resolved "https://registry.yarnpkg.com/arg/-/arg-4.1.3.tgz#269fc7ad5b8e42cb63c896d5666017261c144089"
integrity sha512-58S9QDqG0Xx27YwPSt9fJxivjYl432YCwfDMfZ+71RAqUrZef7LrKQZ3LHLOwCS4FLNBplP533Zx895SeOCHvA==
argparse@^1.0.7:
version "1.0.10"
resolved "https://registry.yarnpkg.com/argparse/-/argparse-1.0.10.tgz#bcd6791ea5ae09725e17e5ad988134cd40b3d911"
@ -914,6 +919,11 @@ create-hmac@^1.1.0, create-hmac@^1.1.4, create-hmac@^1.1.7:
safe-buffer "^5.0.1"
sha.js "^2.4.8"
create-require@^1.1.0:
version "1.1.1"
resolved "https://registry.yarnpkg.com/create-require/-/create-require-1.1.1.tgz#c1d7e8f1e5f6cfc9ff65f9cd352d37348756c333"
integrity sha512-dcKFX3jn0MpIaXjisoRvexIJVEKzaq7z2rZKxf+MSr9TkdmHmsU4m2lcLojrj/FHl8mk5VxMmYA+ftRkP/3oKQ==
cross-spawn@^7.0.2:
version "7.0.3"
resolved "https://registry.yarnpkg.com/cross-spawn/-/cross-spawn-7.0.3.tgz#f73a85b9d5d41d045551c177e2882d4ac85728a6"
@ -1062,6 +1072,11 @@ detective@^5.2.0:
defined "^1.0.0"
minimist "^1.1.1"
diff@^4.0.1:
version "4.0.2"
resolved "https://registry.yarnpkg.com/diff/-/diff-4.0.2.tgz#60f3aecb89d5fae520c11aa19efc2bb982aade7d"
integrity sha512-58lmxKSA4BNyLz+HHMUzlOEpg09FV+ev6ZMe3vJihgdxzgcwZ8VoEEPmALCZG9LmqfVoNMMKpttIYTVG6uDY7A==
diffie-hellman@^5.0.0:
version "5.0.3"
resolved "https://registry.yarnpkg.com/diffie-hellman/-/diffie-hellman-5.0.3.tgz#40e8ee98f55a2149607146921c63e1ae5f3d2875"
@ -2140,6 +2155,11 @@ make-dir@^3.0.0:
dependencies:
semver "^6.0.0"
make-error@^1.1.1:
version "1.3.6"
resolved "https://registry.yarnpkg.com/make-error/-/make-error-1.3.6.tgz#2eb2e37ea9b67c4891f684a1394799af484cf7a2"
integrity sha512-s8UhlNe7vPKomQhC1qFelMokr/Sc3AgNbso3n74mVPA5LTZwkB9NlXf4XPamLxJE8h0gh73rM94xvwRT2CVInw==
md5.js@^1.3.4:
version "1.3.5"
resolved "https://registry.yarnpkg.com/md5.js/-/md5.js-1.3.5.tgz#b5d07b8e3216e3e27cd728d72f70d1e6a342005f"
@ -2825,7 +2845,7 @@ slice-ansi@^4.0.0:
astral-regex "^2.0.0"
is-fullwidth-code-point "^3.0.0"
source-map-support@~0.5.10, source-map-support@~0.5.12:
source-map-support@^0.5.17, source-map-support@~0.5.10, source-map-support@~0.5.12:
version "0.5.19"
resolved "https://registry.yarnpkg.com/source-map-support/-/source-map-support-0.5.19.tgz#a98b62f86dcaf4f67399648c085291ab9e8fed61"
integrity sha512-Wonm7zOCIJzBGQdB+thsPar0kYuCIzYvxZwlBa87yi/Mdjv7Tip2cyVbLj5o0cFPN4EVkuTwb3GDDyUx2DGnGw==
@ -3134,6 +3154,18 @@ transform-ast@^2.4.2, transform-ast@^2.4.3:
merge-source-map "1.0.4"
nanobench "^2.1.1"
ts-node@^9.1.1:
version "9.1.1"
resolved "https://registry.yarnpkg.com/ts-node/-/ts-node-9.1.1.tgz#51a9a450a3e959401bda5f004a72d54b936d376d"
integrity sha512-hPlt7ZACERQGf03M253ytLY3dHbGNGrAq9qIHWUY9XHYl1z7wYngSr3OQ5xmui8o2AaxsONxIzjafLUiWBo1Fg==
dependencies:
arg "^4.1.0"
create-require "^1.1.0"
diff "^4.0.1"
make-error "^1.1.1"
source-map-support "^0.5.17"
yn "3.1.1"
tslib@^1.8.1:
version "1.14.1"
resolved "https://registry.yarnpkg.com/tslib/-/tslib-1.14.1.tgz#cf2d38bdc34a134bcaf1091c41f6619e2f672d00"
@ -3430,3 +3462,8 @@ yallist@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/yallist/-/yallist-4.0.0.tgz#9bb92790d9c0effec63be73519e11a35019a3a72"
integrity sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A==
yn@3.1.1:
version "3.1.1"
resolved "https://registry.yarnpkg.com/yn/-/yn-3.1.1.tgz#1e87401a09d767c1d5eab26a6e4c185182d2eb50"
integrity sha512-Ux4ygGWsu2c7isFWe8Yu1YluJmqVhxqK2cLXNQA5AcC3QfbGNpM7fu0Y8b/z16pXLnFxZYvWhd3fhBY9DLmC6Q==