@polkadot-api/ws-middleware 1.0.0-canary.22b2fd2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +1 -0
- package/dist/esm/index.mjs +13 -0
- package/dist/esm/index.mjs.map +1 -0
- package/dist/esm/legacy/downstream/archive.mjs +98 -0
- package/dist/esm/legacy/downstream/archive.mjs.map +1 -0
- package/dist/esm/legacy/downstream/chain-head.mjs +214 -0
- package/dist/esm/legacy/downstream/chain-head.mjs.map +1 -0
- package/dist/esm/legacy/downstream/chainspec.mjs +18 -0
- package/dist/esm/legacy/downstream/chainspec.mjs.map +1 -0
- package/dist/esm/legacy/downstream/downstream.mjs +107 -0
- package/dist/esm/legacy/downstream/downstream.mjs.map +1 -0
- package/dist/esm/legacy/downstream/storage.mjs +57 -0
- package/dist/esm/legacy/downstream/storage.mjs.map +1 -0
- package/dist/esm/legacy/downstream/transaction.mjs +48 -0
- package/dist/esm/legacy/downstream/transaction.mjs.map +1 -0
- package/dist/esm/legacy/upstream/blocks/blocks.mjs +309 -0
- package/dist/esm/legacy/upstream/blocks/blocks.mjs.map +1 -0
- package/dist/esm/legacy/upstream/blocks/index.mjs +7 -0
- package/dist/esm/legacy/upstream/blocks/index.mjs.map +1 -0
- package/dist/esm/legacy/upstream/blocks/upstream-events.mjs +92 -0
- package/dist/esm/legacy/upstream/blocks/upstream-events.mjs.map +1 -0
- package/dist/esm/legacy/upstream/proofs.mjs +41 -0
- package/dist/esm/legacy/upstream/proofs.mjs.map +1 -0
- package/dist/esm/legacy/upstream/upstream.mjs +108 -0
- package/dist/esm/legacy/upstream/upstream.mjs.map +1 -0
- package/dist/esm/legacy/utils/create-opaque-token.mjs +7 -0
- package/dist/esm/legacy/utils/create-opaque-token.mjs.map +1 -0
- package/dist/esm/legacy/utils/fromShittyHeader.mjs +31 -0
- package/dist/esm/legacy/utils/fromShittyHeader.mjs.map +1 -0
- package/dist/esm/legacy/utils/get-hasher-from-block.mjs +12 -0
- package/dist/esm/legacy/utils/get-hasher-from-block.mjs.map +1 -0
- package/dist/esm/legacy/utils/message-from-error.mjs +8 -0
- package/dist/esm/legacy/utils/message-from-error.mjs.map +1 -0
- package/dist/esm/legacy/utils/share-latest.mjs +9 -0
- package/dist/esm/legacy/utils/share-latest.mjs.map +1 -0
- package/dist/esm/legacy/utils/with-latest-from-bp.mjs +40 -0
- package/dist/esm/legacy/utils/with-latest-from-bp.mjs.map +1 -0
- package/dist/esm/methods-router.mjs +41 -0
- package/dist/esm/methods-router.mjs.map +1 -0
- package/dist/esm/methods.mjs +40 -0
- package/dist/esm/methods.mjs.map +1 -0
- package/dist/esm/middleware.mjs +22 -0
- package/dist/esm/middleware.mjs.map +1 -0
- package/dist/esm/modern/fix-follow.mjs +79 -0
- package/dist/esm/modern/fix-follow.mjs.map +1 -0
- package/dist/esm/modern/fix-missing-initial-best.mjs +66 -0
- package/dist/esm/modern/fix-missing-initial-best.mjs.map +1 -0
- package/dist/esm/modern/fix-premature-blocks.mjs +94 -0
- package/dist/esm/modern/fix-premature-blocks.mjs.map +1 -0
- package/dist/esm/modern/fix-unordered-blocks.mjs +100 -0
- package/dist/esm/modern/fix-unordered-blocks.mjs.map +1 -0
- package/dist/esm/modern/fix-unordered-events.mjs +101 -0
- package/dist/esm/modern/fix-unordered-events.mjs.map +1 -0
- package/dist/esm/modern/modern.mjs +21 -0
- package/dist/esm/modern/modern.mjs.map +1 -0
- package/dist/esm/modern/patch-chainhead-events.mjs +16 -0
- package/dist/esm/modern/patch-chainhead-events.mjs.map +1 -0
- package/dist/esm/modern/unpin-hash.mjs +22 -0
- package/dist/esm/modern/unpin-hash.mjs.map +1 -0
- package/dist/esm/numeric-ids.mjs +37 -0
- package/dist/esm/numeric-ids.mjs.map +1 -0
- package/dist/esm/utils/get-async-middleware.mjs +60 -0
- package/dist/esm/utils/get-async-middleware.mjs.map +1 -0
- package/dist/esm/utils/utils.mjs +22 -0
- package/dist/esm/utils/utils.mjs.map +1 -0
- package/dist/index.d.ts +29 -0
- package/dist/index.js +1782 -0
- package/dist/index.js.map +1 -0
- package/package.json +57 -0
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { chainHead } from '../methods.mjs';
|
|
2
|
+
|
|
3
|
+
const { follow: follow$2, unpin: unpin$1, unfollow: unfollow$2 } = chainHead;
|
|
4
|
+
const fixPrematureBlocks = (base) => (onMsg, onHalt) => {
|
|
5
|
+
const pendingChainHeadSubs = /* @__PURE__ */ new Set();
|
|
6
|
+
const pinnedBlocksInSub = /* @__PURE__ */ new Map();
|
|
7
|
+
const prematureBlocks = /* @__PURE__ */ new Map();
|
|
8
|
+
const withClear = (fn) => (...args) => {
|
|
9
|
+
[pendingChainHeadSubs, pinnedBlocksInSub, prematureBlocks].forEach(
|
|
10
|
+
(x) => {
|
|
11
|
+
x.clear();
|
|
12
|
+
}
|
|
13
|
+
);
|
|
14
|
+
fn(...args);
|
|
15
|
+
};
|
|
16
|
+
const { send: originalSend, disconnect } = base((message) => {
|
|
17
|
+
if ("id" in message) {
|
|
18
|
+
onMsg(message);
|
|
19
|
+
const { id, result } = message;
|
|
20
|
+
if (pendingChainHeadSubs.has(id)) {
|
|
21
|
+
pendingChainHeadSubs.delete(id);
|
|
22
|
+
pinnedBlocksInSub.set(result, /* @__PURE__ */ new Set());
|
|
23
|
+
prematureBlocks.set(result, /* @__PURE__ */ new Map());
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
} else {
|
|
27
|
+
const { subscription } = message.params;
|
|
28
|
+
const pinnedBlocks = pinnedBlocksInSub.get(subscription);
|
|
29
|
+
const prematureSub = prematureBlocks.get(subscription);
|
|
30
|
+
if (pinnedBlocks) {
|
|
31
|
+
const result = message.params.result;
|
|
32
|
+
const { event } = result;
|
|
33
|
+
if (event === "initialized") {
|
|
34
|
+
result.finalizedBlockHashes.forEach((hash) => {
|
|
35
|
+
pinnedBlocks.add(hash);
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
if (event === "newBlock") {
|
|
39
|
+
const { parentBlockHash } = result;
|
|
40
|
+
if (!pinnedBlocks.has(parentBlockHash)) {
|
|
41
|
+
const list = prematureSub.get(parentBlockHash) ?? [];
|
|
42
|
+
list.push(message);
|
|
43
|
+
prematureSub.set(parentBlockHash, list);
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
const hash = result.blockHash;
|
|
47
|
+
pinnedBlocks.add(result.blockHash);
|
|
48
|
+
onMsg(message);
|
|
49
|
+
const prematureMessages = prematureSub.get(hash);
|
|
50
|
+
if (prematureMessages) {
|
|
51
|
+
prematureSub.delete(hash);
|
|
52
|
+
prematureMessages.forEach((msg) => {
|
|
53
|
+
pinnedBlocks.add(msg.params.result.blockHash);
|
|
54
|
+
onMsg(msg);
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
if (event === "stop") {
|
|
60
|
+
pinnedBlocks.delete(subscription);
|
|
61
|
+
prematureBlocks.delete(subscription);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
onMsg(message);
|
|
65
|
+
}
|
|
66
|
+
}, withClear(onHalt));
|
|
67
|
+
const send = (msg) => {
|
|
68
|
+
const subId = msg.params[0];
|
|
69
|
+
switch (msg.method) {
|
|
70
|
+
case follow$2:
|
|
71
|
+
pendingChainHeadSubs.add(msg.id);
|
|
72
|
+
break;
|
|
73
|
+
case unpin$1:
|
|
74
|
+
const [subscription, blocks] = msg.params;
|
|
75
|
+
blocks.forEach((block) => {
|
|
76
|
+
pinnedBlocksInSub.get(subscription)?.delete(block);
|
|
77
|
+
prematureBlocks.get(subscription)?.delete(block);
|
|
78
|
+
});
|
|
79
|
+
break;
|
|
80
|
+
case unfollow$2:
|
|
81
|
+
pinnedBlocksInSub.delete(subId);
|
|
82
|
+
prematureBlocks.delete(subId);
|
|
83
|
+
break;
|
|
84
|
+
}
|
|
85
|
+
originalSend(msg);
|
|
86
|
+
};
|
|
87
|
+
return {
|
|
88
|
+
send,
|
|
89
|
+
disconnect: withClear(disconnect)
|
|
90
|
+
};
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
export { fixPrematureBlocks };
|
|
94
|
+
//# sourceMappingURL=fix-premature-blocks.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fix-premature-blocks.mjs","sources":["../../../src/modern/fix-premature-blocks.ts"],"sourcesContent":["import { chainHead } from \"../methods\"\nimport type { Middleware } from \"../types\"\nimport { JsonRpcMessage } from \"@polkadot-api/json-rpc-provider\"\n\ninterface InitializedRpc {\n event: \"initialized\"\n finalizedBlockHashes: string[]\n}\n\ninterface NewBlockRpc {\n event: \"newBlock\"\n blockHash: string\n parentBlockHash: string\n}\n\ninterface BestBlockChangedRpc {\n event: \"bestBlockChanged\"\n bestBlockHash: string\n}\n\ninterface FinalizedRpc {\n event: \"finalized\"\n finalizedBlockHashes: Array<string>\n prunedBlockHashes: Array<string>\n}\n\nexport interface StopRpc {\n event: \"stop\"\n}\n\ntype FollowEvent =\n | InitializedRpc\n | NewBlockRpc\n | BestBlockChangedRpc\n | FinalizedRpc\n | StopRpc\n\nconst { follow, unpin, unfollow } = chainHead\nexport const fixPrematureBlocks: Middleware = (base) => (onMsg, onHalt) => {\n const pendingChainHeadSubs = new Set<string>()\n const pinnedBlocksInSub = new Map<string, Set<string>>()\n const prematureBlocks = new Map<string, Map<string, Array<JsonRpcMessage>>>()\n const withClear =\n <Args extends Array<any>>(\n fn: (...args: Args) => void,\n ): ((...args: Args) => void) =>\n (...args) => {\n ;[pendingChainHeadSubs, pinnedBlocksInSub, prematureBlocks].forEach(\n (x) => {\n x.clear()\n },\n )\n fn(...args)\n }\n\n const { send: originalSend, disconnect } = base((message) => {\n // it's a response\n if (\"id\" in message) {\n onMsg(message)\n const { id, result } = message as unknown as {\n id: string\n result: string\n }\n\n if (pendingChainHeadSubs.has(id)) {\n pendingChainHeadSubs.delete(id)\n pinnedBlocksInSub.set(result, new Set())\n prematureBlocks.set(result, new Map())\n return\n }\n } else {\n // it's a notification\n const { subscription } = (message as any).params\n const pinnedBlocks = pinnedBlocksInSub.get(subscription)\n const prematureSub = prematureBlocks.get(subscription)!\n if (pinnedBlocks) {\n const result = (message as any).params.result as FollowEvent\n const { event } = result\n if (event === \"initialized\") {\n result.finalizedBlockHashes.forEach((hash) => {\n pinnedBlocks.add(hash)\n })\n }\n\n if (event === \"newBlock\") {\n const { parentBlockHash } = result\n if (!pinnedBlocks.has(parentBlockHash)) {\n const list = prematureSub.get(parentBlockHash) ?? []\n list.push(message)\n prematureSub.set(parentBlockHash, list)\n return\n }\n\n const hash = result.blockHash\n pinnedBlocks.add(result.blockHash)\n onMsg(message)\n\n const prematureMessages = prematureSub.get(hash)\n if (prematureMessages) {\n prematureSub.delete(hash)\n prematureMessages.forEach((msg) => {\n pinnedBlocks.add((msg as any).params.result.blockHash)\n onMsg(msg)\n })\n }\n return\n }\n\n if (event === \"stop\") {\n pinnedBlocks.delete(subscription)\n prematureBlocks.delete(subscription)\n }\n }\n onMsg(message)\n }\n }, withClear(onHalt))\n\n const send = (msg: any) => {\n const subId = msg.params[0]\n switch (msg.method) {\n case follow:\n pendingChainHeadSubs.add(msg.id)\n break\n\n case unpin:\n const [subscription, blocks] = msg.params as [string, string[]]\n blocks.forEach((block) => {\n pinnedBlocksInSub.get(subscription)?.delete(block)\n prematureBlocks.get(subscription)?.delete(block)\n })\n break\n\n case unfollow:\n pinnedBlocksInSub.delete(subId)\n prematureBlocks.delete(subId)\n break\n }\n originalSend(msg)\n }\n\n return {\n send,\n disconnect: withClear(disconnect),\n }\n}\n"],"names":["follow","unpin","unfollow"],"mappings":";;AAqCA,MAAM,UAAEA,QAAA,SAAQC,OAAA,YAAOC,UAAA,EAAS,GAAI,SAAA;AAC7B,MAAM,kBAAA,GAAiC,CAAC,IAAA,KAAS,CAAC,OAAO,MAAA,KAAW;AACzE,EAAA,MAAM,oBAAA,uBAA2B,GA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AAA;AAAA,IACf;AAAA,EACF,CAAA,EAAG,SAAA,CAAU,MAAM,CAAC,CAAA;AAEpB,EAAA,MAAM,IAAA,GAAO,CAAC,GAAA,KAAa;AACzB,IAAA,MAAM,KAAA,GAAQ,GAAA,CAAI,MAAA,CAAO,CAAC,CAAA;AAC1B,IAAA,QAAQ,IAAI,MAAA;AAAQ,MAClB,KAAKF,QAAA;AACH,QAAA,oBAAA,CAAqB,GAAA,CAAI,IAAI,EAAE,CAAA;AAC/B,QAAA;AAAA,MAEF,KAAKC,OAAA;AACH,QAAA,MAAM,CAAC,YAAA,EAAc,MAAM,CAAA,GAAI,GAAA,CAAI,MAAA;AACnC,QAAA,MAAA,CAAO,OAAA,CAAQ,CAAC,KAAA,KAAU;AACxB,UAAA,iBAAA,CAAkB,GAAA,CAAI,YAAY,CAAA,EAAG,MAAA,CAAO,KAAK,CAAA;AACjD,UAAA,eAAA,CAAgB,GAAA,CAAI,YAAY,CAAA,EAAG,MAAA,CAAO,KAAK,CAAA;AAAA,QACjD,CAAC,CAAA;AACD,QAAA;AAAA,MAEF,KAAKC,UAAA;AACH,QAAA,iBAAA,CAAkB,OAAO,KAAK,CAAA;AAC9B,QAAA,eAAA,CAAgB,OAAO,KAAK,CAAA;AAC5B,QAAA;AAAA;AAEJ,IAAA,YAAA,CAAa,GAAG,CAAA;AAAA,EAClB,CAAA;AAEA,EAAA,OAAO;AAAA,IACL,IAAA;AAAA,IACA,UAAA,EAAY,UAAU,UAAU;AAAA,GAClC;AACF;;;;"}
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { chainHead } from '../methods.mjs';
|
|
2
|
+
|
|
3
|
+
const { follow: follow$1, unpin, unfollow: unfollow$1 } = chainHead;
|
|
4
|
+
const fixUnorderedBlocks = (base) => (onMsg, onHalt) => {
|
|
5
|
+
const pendingChainHeadSubs = /* @__PURE__ */ new Set();
|
|
6
|
+
const pinnedBlocksInSub = /* @__PURE__ */ new Map();
|
|
7
|
+
const uknownBlocksNotifications = /* @__PURE__ */ new Map();
|
|
8
|
+
const withClear = (fn) => (...args) => {
|
|
9
|
+
[
|
|
10
|
+
pendingChainHeadSubs,
|
|
11
|
+
pinnedBlocksInSub,
|
|
12
|
+
uknownBlocksNotifications
|
|
13
|
+
].forEach((x) => {
|
|
14
|
+
x.clear();
|
|
15
|
+
});
|
|
16
|
+
fn(...args);
|
|
17
|
+
};
|
|
18
|
+
const { send: originalSend, disconnect } = base((message) => {
|
|
19
|
+
if ("id" in message) {
|
|
20
|
+
onMsg(message);
|
|
21
|
+
const { id, result } = message;
|
|
22
|
+
if (pendingChainHeadSubs.has(id)) {
|
|
23
|
+
pendingChainHeadSubs.delete(id);
|
|
24
|
+
pinnedBlocksInSub.set(result, /* @__PURE__ */ new Set());
|
|
25
|
+
uknownBlocksNotifications.set(result, /* @__PURE__ */ new Map());
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
} else {
|
|
29
|
+
const { subscription } = message.params;
|
|
30
|
+
const pinnedBlocks = pinnedBlocksInSub.get(subscription);
|
|
31
|
+
const premature = uknownBlocksNotifications.get(subscription);
|
|
32
|
+
if (pinnedBlocks) {
|
|
33
|
+
const result = message.params.result;
|
|
34
|
+
const { event } = result;
|
|
35
|
+
if (event === "initialized") {
|
|
36
|
+
result.finalizedBlockHashes.forEach((hash) => {
|
|
37
|
+
pinnedBlocks.add(hash);
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
if (event === "finalized") {
|
|
41
|
+
result.prunedBlockHashes = result.prunedBlockHashes.filter(
|
|
42
|
+
(x) => pinnedBlocks.has(x)
|
|
43
|
+
);
|
|
44
|
+
}
|
|
45
|
+
if (event === "newBlock") {
|
|
46
|
+
pinnedBlocks.add(result.blockHash);
|
|
47
|
+
const hash = result.blockHash;
|
|
48
|
+
const missing = premature.get(hash);
|
|
49
|
+
if (missing) {
|
|
50
|
+
premature.delete(hash);
|
|
51
|
+
onMsg(message);
|
|
52
|
+
Promise.resolve().then(() => {
|
|
53
|
+
onMsg(missing);
|
|
54
|
+
});
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
if (event === "bestBlockChanged") {
|
|
59
|
+
const hash = result.bestBlockHash;
|
|
60
|
+
if (!pinnedBlocks.has(hash)) {
|
|
61
|
+
uknownBlocksNotifications.get(subscription).set(hash, message);
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
if (event === "stop") {
|
|
66
|
+
pinnedBlocks.delete(subscription);
|
|
67
|
+
uknownBlocksNotifications.delete(subscription);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
onMsg(message);
|
|
71
|
+
}
|
|
72
|
+
}, withClear(onHalt));
|
|
73
|
+
const send = (msg) => {
|
|
74
|
+
const subId = msg.params[0];
|
|
75
|
+
switch (msg.method) {
|
|
76
|
+
case follow$1:
|
|
77
|
+
pendingChainHeadSubs.add(msg.id);
|
|
78
|
+
break;
|
|
79
|
+
case unpin:
|
|
80
|
+
const [subscription, blocks] = msg.params;
|
|
81
|
+
blocks.forEach((block) => {
|
|
82
|
+
pinnedBlocksInSub.get(subscription)?.delete(block);
|
|
83
|
+
uknownBlocksNotifications.get(subscription)?.delete(block);
|
|
84
|
+
});
|
|
85
|
+
break;
|
|
86
|
+
case unfollow$1:
|
|
87
|
+
pinnedBlocksInSub.delete(subId);
|
|
88
|
+
uknownBlocksNotifications.delete(subId);
|
|
89
|
+
break;
|
|
90
|
+
}
|
|
91
|
+
originalSend(msg);
|
|
92
|
+
};
|
|
93
|
+
return {
|
|
94
|
+
send,
|
|
95
|
+
disconnect: withClear(disconnect)
|
|
96
|
+
};
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
export { fixUnorderedBlocks };
|
|
100
|
+
//# sourceMappingURL=fix-unordered-blocks.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fix-unordered-blocks.mjs","sources":["../../../src/modern/fix-unordered-blocks.ts"],"sourcesContent":["import type { Middleware } from \"../types\"\nimport { chainHead } from \"../methods\"\n\ninterface InitializedRpc {\n event: \"initialized\"\n finalizedBlockHashes: string[]\n}\n\ninterface NewBlockRpc {\n event: \"newBlock\"\n blockHash: string\n parentBlockHash: string\n}\n\ninterface BestBlockChangedRpc {\n event: \"bestBlockChanged\"\n bestBlockHash: string\n}\n\ninterface FinalizedRpc {\n event: \"finalized\"\n finalizedBlockHashes: Array<string>\n prunedBlockHashes: Array<string>\n}\n\nexport interface StopRpc {\n event: \"stop\"\n}\n\ntype FollowEvent =\n | InitializedRpc\n | NewBlockRpc\n | BestBlockChangedRpc\n | FinalizedRpc\n | StopRpc\n\nconst { follow, unpin, unfollow } = chainHead\nexport const fixUnorderedBlocks: Middleware = (base) => (onMsg, onHalt) => {\n const pendingChainHeadSubs = new Set<string>()\n const pinnedBlocksInSub = new Map<string, Set<string>>()\n const uknownBlocksNotifications = new Map<string, Map<string, any>>()\n const withClear =\n <Args extends Array<any>>(\n fn: (...args: Args) => void,\n ): ((...args: Args) => void) =>\n (...args) => {\n ;[\n pendingChainHeadSubs,\n pinnedBlocksInSub,\n uknownBlocksNotifications,\n ].forEach((x) => {\n x.clear()\n })\n fn(...args)\n }\n\n const { send: originalSend, disconnect } = base((message) => {\n // it's a response\n if (\"id\" in message) {\n onMsg(message)\n const { id, result } = message as unknown as {\n id: string\n result: string\n }\n\n if (pendingChainHeadSubs.has(id)) {\n pendingChainHeadSubs.delete(id)\n pinnedBlocksInSub.set(result, new Set())\n uknownBlocksNotifications.set(result, new Map())\n return\n }\n } else {\n // it's a notification\n const { subscription } = (message as any).params\n const pinnedBlocks = pinnedBlocksInSub.get(subscription)\n const premature = uknownBlocksNotifications.get(subscription)!\n if (pinnedBlocks) {\n const result = (message as any).params.result as FollowEvent\n const { event } = result\n if (event === \"initialized\") {\n result.finalizedBlockHashes.forEach((hash) => {\n pinnedBlocks.add(hash)\n })\n }\n\n if (event === \"finalized\") {\n result.prunedBlockHashes = result.prunedBlockHashes.filter((x) =>\n pinnedBlocks.has(x),\n )\n }\n\n if (event === \"newBlock\") {\n pinnedBlocks.add(result.blockHash)\n const hash = result.blockHash\n const missing = premature.get(hash)\n if (missing) {\n premature.delete(hash)\n onMsg(message)\n Promise.resolve().then(() => {\n onMsg(missing)\n })\n return\n }\n }\n\n if (event === \"bestBlockChanged\") {\n const hash = result.bestBlockHash\n if (!pinnedBlocks.has(hash)) {\n uknownBlocksNotifications.get(subscription)!.set(hash, message)\n return\n }\n }\n\n if (event === \"stop\") {\n pinnedBlocks.delete(subscription)\n uknownBlocksNotifications.delete(subscription)\n }\n }\n onMsg(message)\n }\n }, withClear(onHalt))\n\n const send = (msg: any) => {\n const subId = msg.params[0]\n switch (msg.method) {\n case follow:\n pendingChainHeadSubs.add(msg.id)\n break\n\n case unpin:\n const [subscription, blocks] = msg.params as [string, string[]]\n blocks.forEach((block) => {\n pinnedBlocksInSub.get(subscription)?.delete(block)\n uknownBlocksNotifications.get(subscription)?.delete(block)\n })\n break\n\n case unfollow:\n pinnedBlocksInSub.delete(subId)\n uknownBlocksNotifications.delete(subId)\n break\n }\n originalSend(msg)\n }\n\n return {\n send,\n disconnect: withClear(disconnect),\n }\n}\n"],"names":["follow","unfollow"],"mappings":";;AAoCA,MAAM,UAAEA,QAAA,EAAQ,KAAA,YAAOC,UAAA,EAAS,GAAI,SAAA;AAC7B,MAAM,kBAAA,GAAiC,CAAC,IAAA,KAAS,CAAC,OAAO,MAAA,KAAW;AACzE,EAAA,MAAM,oBAAA,uBAA2B,GAAA,EAAY;AAC7C,EAAA,MAAM,iBAAA,uBAAwB,GAAA,EAAyB;AACvD,EAAA,MAAM,yBAAA,uBAAgC,GAAA,EAA8B;AACpE,EAAA,MAAM,SAAA,GACJ,CACE,EAAA,KAEF,CAAA,GAAI,IAAA,KAAS;AACV,IAAA;AAAA,MACC,oBAAA;AAAA,MACA,iBAAA;AAAA,MACA;AAAA,KACF,CAAE,OAAA,CAAQ,CAAC,CAAA,KAAM;AAC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I,MAAA;AAAQ,MAClB,KAAKD,QAAA;AACH,QAAA,oBAAA,CAAqB,GAAA,CAAI,IAAI,EAAE,CAAA;AAC/B,QAAA;AAAA,MAEF,KAAK,KAAA;AACH,QAAA,MAAM,CAAC,YAAA,EAAc,MAAM,CAAA,GAAI,GAAA,CAAI,MAAA;AACnC,QAAA,MAAA,CAAO,OAAA,CAAQ,CAAC,KAAA,KAAU;AACxB,UAAA,iBAAA,CAAkB,GAAA,CAAI,YAAY,CAAA,EAAG,MAAA,CAAO,KAAK,CAAA;AACjD,UAAA,yBAAA,CAA0B,GAAA,CAAI,YAAY,CAAA,EAAG,MAAA,CAAO,KAAK,CAAA;AAAA,QAC3D,CAAC,CAAA;AACD,QAAA;AAAA,MAEF,KAAKC,UAAA;AACH,QAAA,iBAAA,CAAkB,OAAO,KAAK,CAAA;AAC9B,QAAA,yBAAA,CAA0B,OAAO,KAAK,CAAA;AACtC,QAAA;AAAA;AAEJ,IAAA,YAAA,CAAa,GAAG,CAAA;AAAA,EAClB,CAAA;AAEA,EAAA,OAAO;AAAA,IACL,IAAA;AAAA,IACA,UAAA,EAAY,UAAU,UAAU;AAAA,GAClC;AACF;;;;"}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { chainHead } from '../methods.mjs';
|
|
2
|
+
|
|
3
|
+
const { follow, body, call, storage, unfollow, stopOperation } = chainHead;
|
|
4
|
+
const terminalOperationEvents = new Set(
|
|
5
|
+
["BodyDone", "CallDone", "StorageDone", "Inaccessible", "Error"].map(
|
|
6
|
+
(x) => "operation" + x
|
|
7
|
+
)
|
|
8
|
+
);
|
|
9
|
+
const isTerminalNotification = (msg) => terminalOperationEvents.has(msg.params?.result?.event);
|
|
10
|
+
const fixUnorderedEvents = (base) => (onMsg, onHalt) => {
|
|
11
|
+
const pendingChainHeadSubs = /* @__PURE__ */ new Set();
|
|
12
|
+
const pendingOperationIds = /* @__PURE__ */ new Map();
|
|
13
|
+
const activeOperationIds = /* @__PURE__ */ new Map();
|
|
14
|
+
const uknownOperationNotifications = /* @__PURE__ */ new Map();
|
|
15
|
+
const withClear = (fn) => (...args) => {
|
|
16
|
+
[
|
|
17
|
+
pendingChainHeadSubs,
|
|
18
|
+
pendingOperationIds,
|
|
19
|
+
activeOperationIds,
|
|
20
|
+
uknownOperationNotifications
|
|
21
|
+
].forEach((x) => {
|
|
22
|
+
x.clear();
|
|
23
|
+
});
|
|
24
|
+
fn(...args);
|
|
25
|
+
};
|
|
26
|
+
const { send: originalSend, disconnect } = base((message) => {
|
|
27
|
+
if ("id" in message) {
|
|
28
|
+
onMsg(message);
|
|
29
|
+
const { id, result } = message;
|
|
30
|
+
if (pendingChainHeadSubs.has(id)) {
|
|
31
|
+
pendingChainHeadSubs.delete(id);
|
|
32
|
+
activeOperationIds.set(result, /* @__PURE__ */ new Set());
|
|
33
|
+
uknownOperationNotifications.set(result, /* @__PURE__ */ new Map());
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
const subId = pendingOperationIds.get(id);
|
|
37
|
+
if (subId !== void 0) {
|
|
38
|
+
pendingOperationIds.delete(id);
|
|
39
|
+
const opId = message.result?.operationId;
|
|
40
|
+
if (opId !== void 0 && activeOperationIds.has(subId)) {
|
|
41
|
+
const subOperations = activeOperationIds.get(subId);
|
|
42
|
+
subOperations.add(opId);
|
|
43
|
+
const pendingNotifications = uknownOperationNotifications.get(subId)?.get(opId);
|
|
44
|
+
if (pendingNotifications) {
|
|
45
|
+
pendingNotifications.forEach(onMsg);
|
|
46
|
+
uknownOperationNotifications.get(subId).delete(opId);
|
|
47
|
+
if (isTerminalNotification(pendingNotifications.at(-1)))
|
|
48
|
+
subOperations.delete(opId);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
} else {
|
|
53
|
+
const { subscription, result } = message.params;
|
|
54
|
+
const operationIds = activeOperationIds.get(subscription);
|
|
55
|
+
if (operationIds) {
|
|
56
|
+
const { operationId } = message.params.result;
|
|
57
|
+
if (operationId !== void 0) {
|
|
58
|
+
if (!operationIds.has(operationId)) {
|
|
59
|
+
const subscriptionPending = uknownOperationNotifications.get(subscription);
|
|
60
|
+
const pendingMessages = subscriptionPending.get(operationId) ?? [];
|
|
61
|
+
pendingMessages.push(message);
|
|
62
|
+
subscriptionPending.set(operationId, pendingMessages);
|
|
63
|
+
return;
|
|
64
|
+
} else if (isTerminalNotification(message))
|
|
65
|
+
operationIds.delete(operationId);
|
|
66
|
+
} else if (result?.event === "stop") {
|
|
67
|
+
activeOperationIds.delete(subscription);
|
|
68
|
+
uknownOperationNotifications.delete(subscription);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
onMsg(message);
|
|
72
|
+
}
|
|
73
|
+
}, withClear(onHalt));
|
|
74
|
+
const send = (msg) => {
|
|
75
|
+
const subId = msg.params[0];
|
|
76
|
+
switch (msg.method) {
|
|
77
|
+
case follow:
|
|
78
|
+
pendingChainHeadSubs.add(msg.id);
|
|
79
|
+
break;
|
|
80
|
+
case body:
|
|
81
|
+
case call:
|
|
82
|
+
case storage:
|
|
83
|
+
pendingOperationIds.set(msg.id, subId);
|
|
84
|
+
break;
|
|
85
|
+
case unfollow:
|
|
86
|
+
activeOperationIds.delete(subId);
|
|
87
|
+
uknownOperationNotifications.delete(subId);
|
|
88
|
+
break;
|
|
89
|
+
case stopOperation:
|
|
90
|
+
activeOperationIds.get(subId)?.delete(msg.params[1]);
|
|
91
|
+
}
|
|
92
|
+
originalSend(msg);
|
|
93
|
+
};
|
|
94
|
+
return {
|
|
95
|
+
send,
|
|
96
|
+
disconnect: withClear(disconnect)
|
|
97
|
+
};
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
export { fixUnorderedEvents };
|
|
101
|
+
//# sourceMappingURL=fix-unordered-events.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fix-unordered-events.mjs","sources":["../../../src/modern/fix-unordered-events.ts"],"sourcesContent":["import type { Middleware } from \"../types\"\nimport { chainHead } from \"../methods\"\n\nconst { follow, body, call, storage, unfollow, stopOperation } = chainHead\nconst terminalOperationEvents = new Set(\n [\"BodyDone\", \"CallDone\", \"StorageDone\", \"Inaccessible\", \"Error\"].map(\n (x) => \"operation\" + x,\n ),\n)\nconst isTerminalNotification = (msg: any): boolean =>\n terminalOperationEvents.has(msg.params?.result?.event)\n\nexport const fixUnorderedEvents: Middleware = (base) => (onMsg, onHalt) => {\n const pendingChainHeadSubs = new Set<string>()\n const pendingOperationIds: Map<string, string> = new Map()\n\n const activeOperationIds = new Map<string, Set<string>>()\n const uknownOperationNotifications = new Map<\n string,\n Map<string, Array<any>>\n >()\n\n const withClear =\n <Args extends Array<any>>(\n fn: (...args: Args) => void,\n ): ((...args: Args) => void) =>\n (...args) => {\n ;[\n pendingChainHeadSubs,\n pendingOperationIds,\n activeOperationIds,\n uknownOperationNotifications,\n ].forEach((x) => {\n x.clear()\n })\n fn(...args)\n }\n\n const { send: originalSend, disconnect } = base((message) => {\n // it's a response\n if (\"id\" in message) {\n onMsg(message)\n const { id, result } = message as unknown as {\n id: string\n result: string\n }\n if (pendingChainHeadSubs.has(id)) {\n pendingChainHeadSubs.delete(id)\n activeOperationIds.set(result, new Set())\n uknownOperationNotifications.set(result, new Map())\n return\n }\n\n const subId = pendingOperationIds.get(id)\n if (subId !== undefined) {\n pendingOperationIds.delete(id)\n const opId = (message as any).result?.operationId\n // it's possible that the limit has been reached... so we need to check\n // it's also possible that the response came after an unfollow\n if (opId !== undefined && activeOperationIds.has(subId)) {\n const subOperations = activeOperationIds.get(subId)!\n subOperations.add(opId)\n const pendingNotifications = uknownOperationNotifications\n .get(subId)\n ?.get(opId)\n\n if (pendingNotifications) {\n pendingNotifications.forEach(onMsg)\n uknownOperationNotifications.get(subId)!.delete(opId)\n if (isTerminalNotification(pendingNotifications.at(-1)))\n subOperations.delete(opId)\n }\n }\n }\n } else {\n // it's a notification\n const { subscription, result } = (message as any).params\n const operationIds = activeOperationIds.get(subscription)\n if (operationIds) {\n const { operationId } = (message as any).params.result\n if (operationId !== undefined) {\n if (!operationIds.has(operationId)) {\n // The operationId hasn't arrived yet\n const subscriptionPending =\n uknownOperationNotifications.get(subscription)!\n const pendingMessages = subscriptionPending.get(operationId) ?? []\n pendingMessages.push(message)\n subscriptionPending.set(operationId, pendingMessages)\n return\n } else if (isTerminalNotification(message))\n operationIds.delete(operationId)\n } else if (result?.event === \"stop\") {\n activeOperationIds.delete(subscription)\n uknownOperationNotifications.delete(subscription)\n }\n }\n onMsg(message)\n }\n }, withClear(onHalt))\n\n const send = (msg: any) => {\n const subId = msg.params[0]\n switch (msg.method) {\n case follow:\n pendingChainHeadSubs.add(msg.id)\n break\n\n case body:\n case call:\n case storage:\n pendingOperationIds.set(msg.id, subId)\n break\n\n case unfollow:\n activeOperationIds.delete(subId)\n uknownOperationNotifications.delete(subId)\n break\n\n case stopOperation:\n activeOperationIds.get(subId)?.delete(msg.params[1])\n }\n originalSend(msg)\n }\n\n return {\n send,\n disconnect: withClear(disconnect),\n }\n}\n"],"names":[],"mappings":";;AAGA,MAAM,EAAE,MAAA,EAAQ,IAAA,EAAM,MAAM,OAAA,EAAS,QAAA,EAAU,eAAc,GAAI,SAAA;AACjE,MAAM,0BAA0B,IAAI,GAAA;AAAA,EAClC,CAAC,UAAA,EAAY,UAAA,EAAY,aAAA,EAAe,cAAA,EAAgB,OAAO,CAAA,CAAE,GAAA;AAAA,IAC/D,CAAC,MAAM,WAAA,GAAc;AAAA;AAEzB,CAAA;AACA,MAAM,sBAAA,GAAyB,CAAC,GAAA,KAC9B,uBAAA,CAAwB,IAAI,GAAA,CAAI,MAAA,EAAQ,QAAQ,KAAK,CAAA;AAEhD,MAAM,kBAAA,GAAiC,CAAC,IAAA,KAAS,CAAC,OAAO,MAAA,KAAW;AACzE,EAAA,MAAM,oBAAA,uBAA2B,GAAA,EAAY;AAC7C,EAAA,MAAM,mBAAA,uBAA+C,GAAA,EAAI;AAEzD,EAAA,MAAM,kBAAA,uBAAyB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AAA,KAAU,MAAA,EAAQ;AACnC,UAAA,kBAAA,CAAmB,OAAO,YAAY,CAAA;AACtC,UAAA,4BAAA,CAA6B,OAAO,YAAY,CAAA;AAAA,QAClD;AAAA,MACF;AACA,MAAA,KAAA,CAAM,OAAO,CAAA;AAAA,IACf;AAAA,EACF,CAAA,EAAG,SAAA,CAAU,MAAM,CAAC,CAAA;AAEpB,EAAA,MAAM,IAAA,GAAO,CAAC,GAAA,KAAa;AACzB,IAAA,MAAM,KAAA,GAAQ,GAAA,CAAI,MAAA,CAAO,CAAC,CAAA;AAC1B,IAAA,QAAQ,IAAI,MAAA;AAAQ,MAClB,KAAK,MAAA;AACH,QAAA,oBAAA,CAAqB,GAAA,CAAI,IAAI,EAAE,CAAA;AAC/B,QAAA;AAAA,MAEF,KAAK,IAAA;AAAA,MACL,KAAK,IAAA;AAAA,MACL,KAAK,OAAA;AACH,QAAA,mBAAA,CAAoB,GAAA,CAAI,GAAA,CAAI,EAAA,EAAI,KAAK,CAAA;AACrC,QAAA;AAAA,MAEF,KAAK,QAAA;AACH,QAAA,kBAAA,CAAmB,OAAO,KAAK,CAAA;AAC/B,QAAA,4BAAA,CAA6B,OAAO,KAAK,CAAA;AACzC,QAAA;AAAA,MAEF,KAAK,aAAA;AACH,QAAA,kBAAA,CAAmB,IAAI,KAAK,CAAA,EAAG,OAAO,GAAA,CAAI,MAAA,CAAO,CAAC,CAAC,CAAA;AAAA;AAEvD,IAAA,YAAA,CAAa,GAAG,CAAA;AAAA,EAClB,CAAA;AAEA,EAAA,OAAO;AAAA,IACL,IAAA;AAAA,IACA,UAAA,EAAY,UAAU,UAAU;AAAA,GAClC;AACF;;;;"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { followEnhancer } from './fix-follow.mjs';
|
|
2
|
+
import { fixPrematureBlocks } from './fix-premature-blocks.mjs';
|
|
3
|
+
import { fixUnorderedBlocks } from './fix-unordered-blocks.mjs';
|
|
4
|
+
import { fixUnorderedEvents } from './fix-unordered-events.mjs';
|
|
5
|
+
import { patchChainHeadEvents } from './patch-chainhead-events.mjs';
|
|
6
|
+
import { unpinHash } from './unpin-hash.mjs';
|
|
7
|
+
import { apply } from '../utils/utils.mjs';
|
|
8
|
+
import { fixMissingInitialBest } from './fix-missing-initial-best.mjs';
|
|
9
|
+
|
|
10
|
+
const modern = apply(
|
|
11
|
+
fixUnorderedEvents,
|
|
12
|
+
unpinHash,
|
|
13
|
+
patchChainHeadEvents,
|
|
14
|
+
fixPrematureBlocks,
|
|
15
|
+
fixUnorderedBlocks,
|
|
16
|
+
fixMissingInitialBest,
|
|
17
|
+
followEnhancer
|
|
18
|
+
);
|
|
19
|
+
|
|
20
|
+
export { modern };
|
|
21
|
+
//# sourceMappingURL=modern.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"modern.mjs","sources":["../../../src/modern/modern.ts"],"sourcesContent":["import { followEnhancer } from \"./fix-follow\"\nimport { fixPrematureBlocks } from \"./fix-premature-blocks\"\nimport { fixUnorderedBlocks } from \"./fix-unordered-blocks\"\nimport { fixUnorderedEvents } from \"./fix-unordered-events\"\nimport { patchChainHeadEvents } from \"./patch-chainhead-events\"\nimport { unpinHash } from \"./unpin-hash\"\nimport { apply } from \"../utils\"\nimport { fixMissingInitialBest } from \"./fix-missing-initial-best\"\n\nexport const modern = apply(\n fixUnorderedEvents,\n unpinHash,\n patchChainHeadEvents,\n fixPrematureBlocks,\n fixUnorderedBlocks,\n fixMissingInitialBest,\n followEnhancer,\n)\n"],"names":[],"mappings":";;;;;;;;;AASO,MAAM,MAAA,GAAS,KAAA;AAAA,EACpB,kBAAA;AAAA,EACA,SAAA;AAAA,EACA,oBAAA;AAAA,EACA,kBAAA;AAAA,EACA,kBAAA;AAAA,EACA,qBAAA;AAAA,EACA;AACF;;;;"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
const patchChainHeadEvents = (base) => (onMsg, onHalt) => base((message) => {
|
|
2
|
+
const result = message.params?.result;
|
|
3
|
+
if (!("id" in message) && result) {
|
|
4
|
+
const { prunedBlockHashes, finalizedBlockHash, event } = result;
|
|
5
|
+
if (event === "finalized" && Array.isArray(prunedBlockHashes))
|
|
6
|
+
result.prunedBlockHashes = [...new Set(result.prunedBlockHashes)];
|
|
7
|
+
else if (event === "initialized" && finalizedBlockHash) {
|
|
8
|
+
result.finalizedBlockHashes = [result.finalizedBlockHash];
|
|
9
|
+
delete result.finalizedBlockHash;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
onMsg(message);
|
|
13
|
+
}, onHalt);
|
|
14
|
+
|
|
15
|
+
export { patchChainHeadEvents };
|
|
16
|
+
//# sourceMappingURL=patch-chainhead-events.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"patch-chainhead-events.mjs","sources":["../../../src/modern/patch-chainhead-events.ts"],"sourcesContent":["import type { Middleware } from \"../types\"\n\nexport const patchChainHeadEvents: Middleware = (base) => (onMsg, onHalt) =>\n base((message) => {\n const result = (message as any).params?.result\n if (!(\"id\" in message) && result) {\n const { prunedBlockHashes, finalizedBlockHash, event } = result\n if (event === \"finalized\" && Array.isArray(prunedBlockHashes))\n result.prunedBlockHashes = [...new Set(result.prunedBlockHashes)]\n else if (event === \"initialized\" && finalizedBlockHash) {\n result.finalizedBlockHashes = [result.finalizedBlockHash]\n delete result.finalizedBlockHash\n }\n }\n onMsg(message)\n }, onHalt)\n"],"names":[],"mappings":"AAEO,MAAM,oBAAA,GAAmC,CAAC,IAAA,KAAS,CAAC,OAAO,MAAA,KAChE,IAAA,CAAK,CAAC,OAAA,KAAY;AAChB,EAAA,MAAM,MAAA,GAAU,QAAgB,MAAA,EAAQ,MAAA;AACxC,EAAA,IAAI,EAAE,IAAA,IAAQ,OAAA,CAAA,IAAY,MAAA,EAAQ;AAChC,IAAA,MAAM,EAAE,iBAAA,EAAmB,kBAAA,EAAoB,KAAA,EAAM,GAAI,MAAA;AACzD,IAAA,IAAI,KAAA,KAAU,WAAA,IAAe,KAAA,CAAM,OAAA,CAAQ,iBAAiB,CAAA;AAC1D,MAAA,MAAA,CAAO,oBAAoB,CAAC,GAAG,IAAI,GAAA,CAAI,MAAA,CAAO,iBAAiB,CAAC,CAAA;AAAA,SAAA,IACzD,KAAA,KAAU,iBAAiB,kBAAA,EAAoB;AACtD,MAAA,MAAA,CAAO,oBAAA,GAAuB,CAAC,MAAA,CAAO,kBAAkB,CAAA;AACxD,MAAA,OAAO,MAAA,CAAO,kBAAA;AAAA,IAChB;AAAA,EACF;AACA,EAAA,KAAA,CAAM,OAAO,CAAA;AACf,CAAA,EAAG,MAAM;;;;"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { chainHead } from '../methods.mjs';
|
|
2
|
+
|
|
3
|
+
const unpinHash = (base) => (...args) => {
|
|
4
|
+
const { send: _send, disconnect } = base(...args);
|
|
5
|
+
const send = (msg) => {
|
|
6
|
+
const { method, params, id, ...rest } = msg;
|
|
7
|
+
if (method == chainHead.unpin && params && Array.isArray(params[1])) {
|
|
8
|
+
params[1].forEach((hash, idx) => {
|
|
9
|
+
_send({
|
|
10
|
+
...rest,
|
|
11
|
+
id: idx === 0 ? id : `${id}-patched-${idx}`,
|
|
12
|
+
method,
|
|
13
|
+
params: [params[0], hash]
|
|
14
|
+
});
|
|
15
|
+
});
|
|
16
|
+
} else _send(msg);
|
|
17
|
+
};
|
|
18
|
+
return { send, disconnect };
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export { unpinHash };
|
|
22
|
+
//# sourceMappingURL=unpin-hash.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"unpin-hash.mjs","sources":["../../../src/modern/unpin-hash.ts"],"sourcesContent":["import { chainHead } from \"../methods\"\nimport type { Middleware } from \"../types\"\nimport { JsonRpcRequest } from \"@polkadot-api/json-rpc-provider\"\n\nexport const unpinHash: Middleware =\n (base) =>\n (...args) => {\n const { send: _send, disconnect } = base(...args)\n\n const send = (msg: JsonRpcRequest) => {\n const { method, params, id, ...rest } = msg\n if (method == chainHead.unpin && params && Array.isArray(params[1])) {\n params[1].forEach((hash, idx) => {\n _send({\n ...rest,\n id: idx === 0 ? id : `${id}-patched-${idx}`,\n method,\n params: [params[0], hash],\n })\n })\n } else _send(msg)\n }\n\n return { send, disconnect }\n }\n"],"names":[],"mappings":";;AAIO,MAAM,SAAA,GACX,CAAC,IAAA,KACD,CAAA,GAAI,IAAA,KAAS;AACX,EAAA,MAAM,EAAE,IAAA,EAAM,KAAA,EAAO,YAAW,GAAI,IAAA,CAAK,GAAG,IAAI,CAAA;AAEhD,EAAA,MAAM,IAAA,GAAO,CAAC,GAAA,KAAwB;AACpC,IAAA,MAAM,EAAE,MAAA,EAAQ,MAAA,EAAQ,EAAA,EAAI,GAAG,MAAK,GAAI,GAAA;AACxC,IAAA,IAAI,MAAA,IAAU,UAAU,KAAA,IAAS,MAAA,IAAU,MAAM,OAAA,CAAQ,MAAA,CAAO,CAAC,CAAC,CAAA,EAAG;AACnE,MAAA,MAAA,CAAO,CAAC,CAAA,CAAE,OAAA,CAAQ,CAAC,MAAM,GAAA,KAAQ;AAC/B,QAAA,KAAA,CAAM;AAAA,UACJ,GAAG,IAAA;AAAA,UACH,IAAI,GAAA,KAAQ,CAAA,GAAI,KAAK,CAAA,EAAG,EAAE,YAAY,GAAG,CAAA,CAAA;AAAA,UACzC,MAAA;AAAA,UACA,MAAA,EAAQ,CAAC,MAAA,CAAO,CAAC,GAAG,IAAI;AAAA,SACzB,CAAA;AAAA,MACH,CAAC,CAAA;AAAA,IACH,CAAA,YAAa,GAAG,CAAA;AAAA,EAClB,CAAA;AAEA,EAAA,OAAO,EAAE,MAAM,UAAA,EAAW;AAC5B;;;;"}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
const withNumericIds = (base) => (onMsg, onHalt) => {
|
|
2
|
+
let nextId = 0;
|
|
3
|
+
const numberToOriginal = /* @__PURE__ */ new Map();
|
|
4
|
+
const clear = () => {
|
|
5
|
+
numberToOriginal.clear();
|
|
6
|
+
};
|
|
7
|
+
const { send: originalSend, disconnect } = base(
|
|
8
|
+
(message) => {
|
|
9
|
+
const { id } = message;
|
|
10
|
+
if (numberToOriginal.has(id)) {
|
|
11
|
+
message.id = numberToOriginal.get(id);
|
|
12
|
+
numberToOriginal.delete(id);
|
|
13
|
+
}
|
|
14
|
+
onMsg(message);
|
|
15
|
+
},
|
|
16
|
+
(e) => {
|
|
17
|
+
clear();
|
|
18
|
+
onHalt(e);
|
|
19
|
+
}
|
|
20
|
+
);
|
|
21
|
+
return {
|
|
22
|
+
send: (msg) => {
|
|
23
|
+
if (msg.id !== void 0) {
|
|
24
|
+
numberToOriginal.set(nextId, msg.id);
|
|
25
|
+
msg.id = nextId++;
|
|
26
|
+
}
|
|
27
|
+
originalSend(msg);
|
|
28
|
+
},
|
|
29
|
+
disconnect() {
|
|
30
|
+
clear();
|
|
31
|
+
disconnect();
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
export { withNumericIds };
|
|
37
|
+
//# sourceMappingURL=numeric-ids.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"numeric-ids.mjs","sources":["../../src/numeric-ids.ts"],"sourcesContent":["import { JsonRpcId, JsonRpcRequest } from \"@polkadot-api/json-rpc-provider\"\nimport { Middleware } from \"./types\"\n\nexport const withNumericIds: Middleware = (base) => (onMsg, onHalt) => {\n let nextId = 0\n const numberToOriginal = new Map<number, JsonRpcId>()\n const clear = () => {\n numberToOriginal.clear()\n }\n\n const { send: originalSend, disconnect } = base(\n (message: any) => {\n const { id } = message\n if (numberToOriginal.has(id)) {\n message.id = numberToOriginal.get(id)\n numberToOriginal.delete(id)\n }\n onMsg(message)\n },\n (e) => {\n clear()\n onHalt(e)\n },\n )\n\n return {\n send: (msg: JsonRpcRequest) => {\n if (msg.id !== undefined) {\n numberToOriginal.set(nextId, msg.id)\n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,0 +1,60 @@
|
|
|
1
|
+
import { createClient } from '@polkadot-api/raw-client';
|
|
2
|
+
import { noop } from '@polkadot-api/utils';
|
|
3
|
+
|
|
4
|
+
const getAsyncMiddleware = (input) => (base) => (onMessageOut, onHaltOut) => {
|
|
5
|
+
let interceptedMessage = noop;
|
|
6
|
+
let pending = [];
|
|
7
|
+
let send = (msg) => {
|
|
8
|
+
pending.push(msg);
|
|
9
|
+
};
|
|
10
|
+
let stopRequest = noop;
|
|
11
|
+
let isOn = true;
|
|
12
|
+
let done = () => {
|
|
13
|
+
isOn = false;
|
|
14
|
+
pending = [];
|
|
15
|
+
stopRequest();
|
|
16
|
+
done = interceptedMessage = interceptedHalt = stopRequest = noop;
|
|
17
|
+
};
|
|
18
|
+
let interceptedHalt = (e) => {
|
|
19
|
+
done();
|
|
20
|
+
onHaltOut(e);
|
|
21
|
+
};
|
|
22
|
+
const innerCon = base(
|
|
23
|
+
(msg) => interceptedMessage(msg),
|
|
24
|
+
(e) => interceptedHalt(e)
|
|
25
|
+
);
|
|
26
|
+
let { disconnect } = innerCon;
|
|
27
|
+
const { request } = createClient((innerClientOnMsg) => {
|
|
28
|
+
interceptedMessage = innerClientOnMsg;
|
|
29
|
+
return { ...innerCon, disconnect: noop };
|
|
30
|
+
});
|
|
31
|
+
if (isOn)
|
|
32
|
+
stopRequest = input((cb) => {
|
|
33
|
+
stopRequest = noop;
|
|
34
|
+
if (!cb) interceptedHalt();
|
|
35
|
+
else {
|
|
36
|
+
({ send, disconnect } = cb((onMiddleMsg, onMiddleHalt) => {
|
|
37
|
+
interceptedHalt = (e) => {
|
|
38
|
+
done();
|
|
39
|
+
onMiddleHalt(e);
|
|
40
|
+
};
|
|
41
|
+
interceptedMessage = onMiddleMsg;
|
|
42
|
+
return innerCon;
|
|
43
|
+
})(onMessageOut, onHaltOut));
|
|
44
|
+
pending.forEach(send);
|
|
45
|
+
pending = [];
|
|
46
|
+
}
|
|
47
|
+
}, request);
|
|
48
|
+
return {
|
|
49
|
+
send: (msg) => {
|
|
50
|
+
send(msg);
|
|
51
|
+
},
|
|
52
|
+
disconnect: () => {
|
|
53
|
+
done();
|
|
54
|
+
disconnect();
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
export { getAsyncMiddleware };
|
|
60
|
+
//# sourceMappingURL=get-async-middleware.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"get-async-middleware.mjs","sources":["../../../src/utils/get-async-middleware.ts"],"sourcesContent":["import { JsonRpcMessage, JsonRpcRequest } from \"@polkadot-api/json-rpc-provider\"\nimport { createClient } from \"@polkadot-api/raw-client\"\nimport { noop } from \"@polkadot-api/utils\"\nimport { Middleware } from \"../types\"\n\nexport const getAsyncMiddleware =\n (\n input: (\n onResult: (x: Middleware | null) => void,\n request: ReturnType<typeof createClient>[\"request\"],\n ) => () => void,\n ): Middleware =>\n (base) =>\n (onMessageOut, onHaltOut) => {\n // null -> loading\n // undefined -> destroyed\n //\n let interceptedMessage: (msg: JsonRpcMessage) => void = noop\n let pending: Array<JsonRpcRequest> = []\n let send = (msg: JsonRpcRequest) => {\n pending.push(msg)\n }\n let stopRequest = noop\n let isOn = true\n let done = () => {\n isOn = false\n pending = []\n stopRequest()\n done = interceptedMessage = interceptedHalt = stopRequest = noop\n }\n\n let interceptedHalt = (e?: any) => {\n done()\n onHaltOut(e)\n }\n const innerCon = base(\n (msg) => interceptedMessage(msg),\n (e) => interceptedHalt(e),\n )\n let { disconnect } = innerCon\n const { request } = createClient((innerClientOnMsg) => {\n interceptedMessage = innerClientOnMsg\n return { ...innerCon, disconnect: noop }\n })\n\n if (isOn)\n stopRequest = input((cb) => {\n stopRequest = noop\n if (!cb) interceptedHalt()\n else {\n ;({ send, disconnect } = cb((onMiddleMsg, onMiddleHalt) => {\n interceptedHalt = (e?: any) => {\n done()\n onMiddleHalt(e)\n }\n interceptedMessage = onMiddleMsg\n return innerCon\n })(onMessageOut, onHaltOut))\n pending.forEach(send)\n pending = []\n }\n }, request)\n\n return {\n send: (msg) => {\n send(msg)\n },\n disconnect: () => {\n done()\n disconnect()\n },\n }\n }\n"],"names":[],"mappings":";;;AAKO,MAAM,qBACX,CACE,KAAA,KAKF,CAAC,IAAA,KACD,CAAC,cAAc,SAAA,KAAc;AAI3B,EAAA,IAAI,kBAAA,GAAoD,IAAA;AACxD,EAAA,IAAI,UAAiC,EAAC;AACtC,EAAA,IAAI,IAAA,GAAO,CAAC,GAAA,KAAwB;AAClC,IAAA,OAAA,CAAQ,KAAK,GAAG,CAAA;AAAA,EAClB,CAAA;AACA,EAAA,IAAI,WAAA,GAAc,IAAA;AAClB,EAAA,IAAI,IAAA,GAAO,IAAA;AACX,EAAA,IAAI,OAAO,MAAM;AACf,IAAA,IAAA,GAAO,KAAA;AACP,IAAA,OAAA,GAAU,EAAC;AACX,IAAA,WAAA,EAAY;AACZ,IAAA,IAAA,GAAO,kBAAA,GAAqB,kBAAkB,WAAA,GAAc,IAAA;AAAA,EAC9D,CAAA;AAEA,EAAA,IAAI,eAAA,GAAkB,CAAC,CAAA,KAAY;AACjC,IAAA,IAAA,EAAK;AACL,IAAA,SAAA,CAAU,CAAC,CAAA;AAAA,EACb,CAAA;AACA,EAAA,MAAM,QAAA,GAAW,IAAA;AAAA,IACf,CAAC,GAAA,KAAQ,kBAAA,CAAmB,GAAG,CAAA;AAAA,IAC/B,CAAC,CAAA,KAAM,eAAA,CAAgB,CAAC;AAAA,GAC1B;AACA,EAAA,IAAI,EAAE,YAAW,GAAI,QAAA;AACrB,EAAA,MAAM,EAAE,OAAA,EAAQ,GAAI,YAAA,CAAa,CAAC,gBAAA,KAAqB;AACrD,IAAA,kBAAA,GAAqB,gBAAA;AACrB,IAAA,OAAO,EAAE,GAAG,QAAA,EAAU,UAAA,EAAY,IAAA,EAAK;AAAA,EACzC,CAAC,CAAA;AAED,EAAA,IAAI,IAAA;AACF,IAAA,WAAA,GAAc,KAAA,CAAM,CAAC,EAAA,KAAO;AAC1B,MAAA,WAAA,GAAc,IAAA;AACd,MAAA,IAAI,CAAC,IAAI,eAAA,EAAgB;AAAA,WACpB;AACF,QAAA,CAAC,EAAE,IAAA,EAAM,UAAA,KAAe,EAAA,CAAG,CAAC,aAAa,YAAA,KAAiB;AACzD,UAAA,eAAA,GAAkB,CAAC,CAAA,KAAY;AAC7B,YAAA,IAAA,EAAK;AACL,YAAA,YAAA,CAAa,CAAC,CAAA;AAAA,UAChB,CAAA;AACA,UAAA,kBAAA,GAAqB,WAAA;AACrB,UAAA,OAAO,QAAA;AAAA,QACT,CAAC,CAAA,CAAE,YAAA,EAAc,SAAS,CAAA;AAC1B,QAAA,OAAA,CAAQ,QAAQ,IAAI,CAAA;AACpB,QAAA,OAAA,GAAU,EAAC;AAAA,MACb;AAAA,IACF,GAAG,OAAO,CAAA;AAEZ,EAAA,OAAO;AAAA,IACL,IAAA,EAAM,CAAC,GAAA,KAAQ;AACb,MAAA,IAAA,CAAK,GAAG,CAAA;AAAA,IACV,CAAA;AAAA,IACA,YAAY,MAAM;AAChB,MAAA,IAAA,EAAK;AACL,MAAA,UAAA,EAAW;AAAA,IACb;AAAA,GACF;AACF;;;;"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import '@polkadot-api/raw-client';
|
|
2
|
+
import '@polkadot-api/utils';
|
|
3
|
+
|
|
4
|
+
const apply = (...middlewares) => (base) => middlewares.reduce((a, b) => b(a), base);
|
|
5
|
+
const jsonObj = (input) => ({
|
|
6
|
+
jsonrpc: "2.0",
|
|
7
|
+
...input
|
|
8
|
+
});
|
|
9
|
+
const operationNotification = (subscription, event, operationId, innerResult = {}) => jsonObj({
|
|
10
|
+
method: "chainHead_v1_followEvent",
|
|
11
|
+
params: {
|
|
12
|
+
subscription,
|
|
13
|
+
result: {
|
|
14
|
+
event,
|
|
15
|
+
operationId,
|
|
16
|
+
...innerResult
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
export { apply, jsonObj, operationNotification };
|
|
22
|
+
//# sourceMappingURL=utils.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.mjs","sources":["../../../src/utils/utils.ts"],"sourcesContent":["import { JsonRpcMessage, JsonRpcRequest } from \"@polkadot-api/json-rpc-provider\"\nimport { InnerJsonRpcProvider } from \"@polkadot-api/json-rpc-provider-proxy\"\nimport { createClient } from \"@polkadot-api/raw-client\"\nimport { noop } from \"@polkadot-api/utils\"\nimport { Middleware } from \"../types\"\n\nexport const apply =\n (...middlewares: Middleware[]): Middleware =>\n (base) =>\n middlewares.reduce((a, b) => b(a), base)\n\nexport const jsonObj = <T extends {}>(input: T) => ({\n jsonrpc: \"2.0\" as const,\n ...input,\n})\n\nexport const operationNotification = <T extends {}>(\n subscription: string,\n event: string,\n operationId?: string,\n innerResult: T = {} as T,\n) =>\n jsonObj({\n method: \"chainHead_v1_followEvent\",\n params: {\n subscription,\n result: {\n event,\n operationId,\n ...innerResult,\n },\n },\n })\n\nexport const getRequest = (base: InnerJsonRpcProvider) => {\n let clientSend: (msg: JsonRpcRequest) => void = noop\n let clientReceive: (msg: JsonRpcMessage) => void = noop\n\n const cleanup = () => {\n clientSend = noop\n clientReceive = noop\n }\n\n const { request } = createClient((clientMsg) => {\n clientReceive = clientMsg\n return {\n disconnect: noop,\n send(x) {\n clientSend(x)\n },\n }\n })\n\n const simpleRequest = <Args extends Array<any>, Payload>(\n method: string,\n params: Args,\n onSuccess: (value: Payload) => void,\n onError: (e: any) => void,\n ): (() => void) => request(method, params, { onSuccess, onError })\n\n const provider: InnerJsonRpcProvider = (onMsg, onHalt) => {\n const { send, disconnect } = base(\n (msg) => {\n clientReceive(msg)\n onMsg(msg)\n },\n (e) => {\n cleanup()\n onHalt(e)\n },\n )\n clientSend = send\n return {\n send,\n disconnect() {\n cleanup()\n disconnect()\n },\n }\n }\n return { provider, request, simpleRequest }\n}\n"],"names":[],"mappings":";;;AAMO,MAAM,KAAA,GACX,CAAA,GAAI,WAAA,KACJ,CAAC,IAAA,KACC,WAAA,CAAY,MAAA,CAAO,CAAC,CAAA,EAAG,CAAA,KAAM,CAAA,CAAE,CAAC,GAAG,IAAI;AAEpC,MAAM,OAAA,GAAU,CAAe,KAAA,MAAc;AAAA,EAClD,OAAA,EAAS,KAAA;AAAA,EACT,GAAG;AACL,CAAA;AAEO,MAAM,qBAAA,GAAwB,CACnC,YAAA,EACA,KAAA,EACA,aACA,WAAA,GAAiB,OAEjB,OAAA,CAAQ;AAAA,EACN,MAAA,EAAQ,0BAAA;AAAA,EACR,MAAA,EAAQ;AAAA,IACN,YAAA;AAAA,IACA,MAAA,EAAQ;AAAA,MACN,KAAA;AAAA,MACA,WAAA;AAAA,MACA,GAAG;AAAA;AACL;AAEJ,CAAC;;;;"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { InnerJsonRpcProvider } from '@polkadot-api/json-rpc-provider-proxy';
|
|
2
|
+
|
|
3
|
+
type Middleware = (base: InnerJsonRpcProvider) => InnerJsonRpcProvider;
|
|
4
|
+
|
|
5
|
+
declare const middleware: Middleware;
|
|
6
|
+
|
|
7
|
+
declare const modern: Middleware;
|
|
8
|
+
|
|
9
|
+
declare const followEnhancer: Middleware;
|
|
10
|
+
|
|
11
|
+
declare const fixPrematureBlocks: Middleware;
|
|
12
|
+
|
|
13
|
+
declare const fixUnorderedBlocks: Middleware;
|
|
14
|
+
|
|
15
|
+
declare const fixUnorderedEvents: Middleware;
|
|
16
|
+
|
|
17
|
+
declare const fixMissingInitialBest: Middleware;
|
|
18
|
+
|
|
19
|
+
declare const patchChainHeadEvents: Middleware;
|
|
20
|
+
|
|
21
|
+
declare const unpinHash: Middleware;
|
|
22
|
+
|
|
23
|
+
declare const withLegacy: Middleware;
|
|
24
|
+
|
|
25
|
+
declare const methodsRouter: (getMiddleware: (methods: string[]) => Middleware) => Middleware;
|
|
26
|
+
|
|
27
|
+
declare const apply: (...middlewares: Middleware[]) => Middleware;
|
|
28
|
+
|
|
29
|
+
export { apply, fixMissingInitialBest, fixPrematureBlocks, fixUnorderedBlocks, fixUnorderedEvents, followEnhancer, methodsRouter, middleware, modern, patchChainHeadEvents, unpinHash, withLegacy };
|