@polkadot-api/ws-middleware 1.0.0-canary.3789023
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 +12 -0
- package/dist/esm/index.mjs.map +1 -0
- package/dist/esm/legacy/downstream/archive.mjs +96 -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-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 +19 -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 +11 -0
- package/dist/esm/utils/utils.mjs.map +1 -0
- package/dist/index.d.ts +27 -0
- package/dist/index.js +1706 -0
- package/dist/index.js.map +1 -0
- package/package.json +57 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 Josep M Sobrepere
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# @polkadot-api/ws-middleware
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export { middleware } from './middleware.mjs';
|
|
2
|
+
export { modern } from './modern/modern.mjs';
|
|
3
|
+
export { followEnhancer } from './modern/fix-follow.mjs';
|
|
4
|
+
export { fixPrematureBlocks } from './modern/fix-premature-blocks.mjs';
|
|
5
|
+
export { fixUnorderedBlocks } from './modern/fix-unordered-blocks.mjs';
|
|
6
|
+
export { fixUnorderedEvents } from './modern/fix-unordered-events.mjs';
|
|
7
|
+
export { patchChainHeadEvents } from './modern/patch-chainhead-events.mjs';
|
|
8
|
+
export { unpinHash } from './modern/unpin-hash.mjs';
|
|
9
|
+
export { withLegacy } from './legacy/downstream/downstream.mjs';
|
|
10
|
+
export { methodsRouter } from './methods-router.mjs';
|
|
11
|
+
export { apply } from './utils/utils.mjs';
|
|
12
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;"}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { map, take, finalize } from 'rxjs';
|
|
2
|
+
import { createOpaqueToken } from '../utils/create-opaque-token.mjs';
|
|
3
|
+
import { areItemsValid, getStg$ } from './storage.mjs';
|
|
4
|
+
import { getMsgFromErr } from '../utils/message-from-error.mjs';
|
|
5
|
+
import { archive } from '../../methods.mjs';
|
|
6
|
+
|
|
7
|
+
const {
|
|
8
|
+
body,
|
|
9
|
+
call,
|
|
10
|
+
finalizedHeight,
|
|
11
|
+
genesisHash,
|
|
12
|
+
hashByHeight,
|
|
13
|
+
header,
|
|
14
|
+
stopStorage,
|
|
15
|
+
storage
|
|
16
|
+
} = archive;
|
|
17
|
+
const createArchive = (upstream, reply, err, notification) => {
|
|
18
|
+
const stgSubscriptions = /* @__PURE__ */ new Map();
|
|
19
|
+
const stg = (reply2, at, items) => {
|
|
20
|
+
const subId = createOpaqueToken();
|
|
21
|
+
reply2(subId);
|
|
22
|
+
const innerNotifiaction = (result2) => {
|
|
23
|
+
notification("archive_v1_storageEvent", subId, result2);
|
|
24
|
+
};
|
|
25
|
+
const subscription = getStg$(upstream, at, items).pipe(
|
|
26
|
+
finalize(() => {
|
|
27
|
+
stgSubscriptions.delete(subId);
|
|
28
|
+
})
|
|
29
|
+
).subscribe(
|
|
30
|
+
(items2) => {
|
|
31
|
+
items2.forEach(
|
|
32
|
+
(item) => innerNotifiaction({ event: "storage", ...item })
|
|
33
|
+
);
|
|
34
|
+
},
|
|
35
|
+
(e) => {
|
|
36
|
+
innerNotifiaction({ event: "storageError", error: getMsgFromErr(e) });
|
|
37
|
+
},
|
|
38
|
+
() => {
|
|
39
|
+
innerNotifiaction({ event: "storageDone" });
|
|
40
|
+
}
|
|
41
|
+
);
|
|
42
|
+
if (!subscription.closed) stgSubscriptions.set(subId, subscription);
|
|
43
|
+
};
|
|
44
|
+
const result = (rId, name, params) => {
|
|
45
|
+
const innerReply = (value) => {
|
|
46
|
+
reply(rId, value);
|
|
47
|
+
};
|
|
48
|
+
const obsReply = (input) => {
|
|
49
|
+
input.subscribe({
|
|
50
|
+
next: innerReply,
|
|
51
|
+
error: (e) => {
|
|
52
|
+
err(rId, e.code ?? -1, getMsgFromErr(e));
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
};
|
|
56
|
+
const [firstArg, secondArg, thirdArg] = params;
|
|
57
|
+
switch (name) {
|
|
58
|
+
case body:
|
|
59
|
+
return obsReply(upstream.getBody(firstArg));
|
|
60
|
+
case call:
|
|
61
|
+
return obsReply(
|
|
62
|
+
upstream.runtimeCall(firstArg, secondArg, thirdArg).pipe(map((value) => ({ success: true, value })))
|
|
63
|
+
);
|
|
64
|
+
case finalizedHeight:
|
|
65
|
+
return obsReply(
|
|
66
|
+
upstream.finalized$.pipe(
|
|
67
|
+
map((x) => x.number),
|
|
68
|
+
take(1)
|
|
69
|
+
)
|
|
70
|
+
);
|
|
71
|
+
case genesisHash:
|
|
72
|
+
return obsReply(upstream.genesisHash);
|
|
73
|
+
case hashByHeight:
|
|
74
|
+
return obsReply(upstream.getBlockHash$(firstArg));
|
|
75
|
+
case header:
|
|
76
|
+
return obsReply(
|
|
77
|
+
upstream.getHeader$(firstArg).pipe(map((h) => h.header))
|
|
78
|
+
);
|
|
79
|
+
case stopStorage: {
|
|
80
|
+
const sub = stgSubscriptions.get(firstArg);
|
|
81
|
+
return sub ? sub.unsubscribe() : err(rId, -32602, "Invalid args");
|
|
82
|
+
}
|
|
83
|
+
case storage:
|
|
84
|
+
return areItemsValid(secondArg) ? stg(innerReply, firstArg, secondArg) : err(rId, -32602, "Invalid args");
|
|
85
|
+
}
|
|
86
|
+
throw null;
|
|
87
|
+
};
|
|
88
|
+
result.stop = () => {
|
|
89
|
+
[...stgSubscriptions].forEach(([, s]) => s.unsubscribe());
|
|
90
|
+
stgSubscriptions.clear();
|
|
91
|
+
};
|
|
92
|
+
return result;
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
export { createArchive };
|
|
96
|
+
//# sourceMappingURL=archive.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"archive.mjs","sources":["../../../../src/legacy/downstream/archive.ts"],"sourcesContent":["import { finalize, map, Observable, Subscription, take } from \"rxjs\"\nimport { createUpstream } from \"../upstream/upstream\"\nimport { createOpaqueToken } from \"../utils/create-opaque-token\"\nimport { areItemsValid, getStg$ } from \"./storage\"\nimport { getMsgFromErr } from \"../utils/message-from-error\"\nimport { archive } from \"../../methods\"\n\nconst {\n body,\n call,\n finalizedHeight,\n genesisHash,\n hashByHeight,\n header,\n stopStorage,\n storage,\n} = archive\n\nexport const createArchive = (\n upstream: ReturnType<typeof createUpstream>,\n reply: (id: string, result: any) => void,\n err: (id: string, code: number, msg: string) => void,\n notification: (method: string, subscription: string, result: any) => void,\n) => {\n const stgSubscriptions = new Map<string, Subscription>()\n\n const stg = (\n reply: (x: string) => void,\n at: string,\n items: Array<{\n key: string\n type:\n | \"value\"\n | \"hash\"\n | \"descendantsValues\"\n | \"descendantsHashes\"\n | \"closestDescendantMerkleValue\"\n }>,\n ) => {\n const subId = createOpaqueToken()\n reply(subId)\n const innerNotifiaction = (result: any) => {\n notification(\"archive_v1_storageEvent\", subId, result)\n }\n\n const subscription = getStg$(upstream, at, items)\n .pipe(\n finalize(() => {\n stgSubscriptions.delete(subId)\n }),\n )\n .subscribe(\n (items) => {\n items.forEach((item) =>\n innerNotifiaction({ event: \"storage\", ...item }),\n )\n },\n (e) => {\n innerNotifiaction({ event: \"storageError\", error: getMsgFromErr(e) })\n },\n () => {\n innerNotifiaction({ event: \"storageDone\" })\n },\n )\n\n if (!subscription.closed) stgSubscriptions.set(subId, subscription)\n }\n\n const result = (rId: string, name: string, params: Array<any>) => {\n const innerReply = (value: any) => {\n reply(rId, value)\n }\n\n const obsReply = (input: Observable<any>) => {\n // There is no need to keep these subscriptions around\n // b/c killing upstream will also kill this subscription\n input.subscribe({\n next: innerReply,\n error: (e) => {\n err(rId, e.code ?? -1, getMsgFromErr(e))\n },\n })\n }\n\n const [firstArg, secondArg, thirdArg] = params\n switch (name) {\n case body:\n return obsReply(upstream.getBody(firstArg))\n case call:\n return obsReply(\n upstream\n .runtimeCall(firstArg, secondArg, thirdArg)\n .pipe(map((value) => ({ success: true, value }))),\n )\n case finalizedHeight:\n return obsReply(\n upstream.finalized$.pipe(\n map((x) => x.number),\n take(1),\n ),\n )\n case genesisHash:\n return obsReply(upstream.genesisHash)\n case hashByHeight:\n return obsReply(upstream.getBlockHash$(firstArg))\n case header:\n return obsReply(\n upstream.getHeader$(firstArg).pipe(map((h) => h.header)),\n )\n case stopStorage: {\n const sub = stgSubscriptions.get(firstArg)\n return sub ? sub.unsubscribe() : err(rId, -32602, \"Invalid args\")\n }\n case storage:\n return areItemsValid(secondArg)\n ? stg(innerReply, firstArg, secondArg)\n : err(rId, -32602, \"Invalid args\")\n }\n throw null\n }\n\n result.stop = () => {\n ;[...stgSubscriptions].forEach(([, s]) => s.unsubscribe())\n stgSubscriptions.clear()\n }\n\n return result\n}\n"],"names":["reply","result","items"],"mappings":";;;;;;AAOA,MAAM;AAAA,EACJ,IAAA;AAAA,EACA,IAAA;AAAA,EACA,eAAA;AAAA,EACA,WAAA;AAAA,EACA,YAAA;AAAA,EACA,MAAA;AAAA,EACA,WAAA;AAAA,EACA;AACF,CAAA,GAAI,OAAA;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AAK,IAAI,CAAC,CAAA,KAAM,CAAA,CAAE,MAAM,CAAC;AAAA,SACzD;AAAA,MACF,KAAK,WAAA,EAAa;AAChB,QAAA,MAAM,GAAA,GAAM,gBAAA,CAAiB,GAAA,CAAI,QAAQ,CAAA;AACzC,QAAA,OAAO,MAAM,GAAA,CAAI,WAAA,KAAgB,GAAA,CAAI,GAAA,EAAK,QAAQ,cAAc,CAAA;AAAA,MAClE;AAAA,MACA,KAAK,OAAA;AACH,QAAA,OAAO,aAAA,CAAc,SAAS,CAAA,GAC1B,GAAA,CAAI,UAAA,EAAY,QAAA,EAAU,SAAS,CAAA,GACnC,GAAA,CAAI,GAAA,EAAK,MAAA,EAAQ,cAAc,CAAA;AAAA;AAEvC,IAAA,MAAM,IAAA;AAAA,EACR,CAAA;AAEA,EAAA,MAAA,CAAO,OAAO,MAAM;AACjB,IAAA,CAAC,GAAG,gBAAgB,CAAA,CAAE,OAAA,CAAQ,CAAC,GAAG,CAAC,CAAA,KAAM,CAAA,CAAE,WAAA,EAAa,CAAA;AACzD,IAAA,gBAAA,CAAiB,KAAA,EAAM;AAAA,EACzB,CAAA;AAEA,EAAA,OAAO,MAAA;AACT;;;;"}
|
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
import { noop } from '@polkadot-api/utils';
|
|
2
|
+
import { finalize } from 'rxjs';
|
|
3
|
+
import { createOpaqueToken } from '../utils/create-opaque-token.mjs';
|
|
4
|
+
import { areItemsValid, getStg$ } from './storage.mjs';
|
|
5
|
+
import { getMsgFromErr } from '../utils/message-from-error.mjs';
|
|
6
|
+
import { chainHead } from '../../methods.mjs';
|
|
7
|
+
|
|
8
|
+
const { follow, header, storage, body, call, unfollow, stopOperation, unpin } = chainHead;
|
|
9
|
+
const createChainHead = (upstream, reply, err, notification) => {
|
|
10
|
+
const subscriptions = /* @__PURE__ */ new Map();
|
|
11
|
+
const _follow = (rId) => {
|
|
12
|
+
if (subscriptions.size === 2) {
|
|
13
|
+
return err(rId, -32800, "Limit reached");
|
|
14
|
+
}
|
|
15
|
+
const token = createOpaqueToken();
|
|
16
|
+
const fNotification = (result2) => notification("chainHead_v1_followEvent", token, result2);
|
|
17
|
+
const up = upstream.getBlocks(token);
|
|
18
|
+
const operations = /* @__PURE__ */ new Map();
|
|
19
|
+
subscriptions.set(token, {
|
|
20
|
+
id: token,
|
|
21
|
+
up,
|
|
22
|
+
operations,
|
|
23
|
+
cleanUp: () => {
|
|
24
|
+
cleanUp();
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
reply(rId, token);
|
|
28
|
+
let subscription = null;
|
|
29
|
+
let cleanUp = () => {
|
|
30
|
+
cleanUp = noop;
|
|
31
|
+
subscription?.unsubscribe();
|
|
32
|
+
subscription = null;
|
|
33
|
+
operations.forEach((cb) => {
|
|
34
|
+
cb();
|
|
35
|
+
});
|
|
36
|
+
operations.clear();
|
|
37
|
+
subscriptions.delete(token);
|
|
38
|
+
};
|
|
39
|
+
subscription = up.blocks$.subscribe({
|
|
40
|
+
next(v) {
|
|
41
|
+
fNotification(v);
|
|
42
|
+
},
|
|
43
|
+
error() {
|
|
44
|
+
cleanUp();
|
|
45
|
+
fNotification({ event: "stop" });
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
if (subscription.closed) subscription = null;
|
|
49
|
+
};
|
|
50
|
+
const _unfollow = (rId, followId) => {
|
|
51
|
+
subscriptions.get(followId)?.cleanUp();
|
|
52
|
+
reply(rId, "null");
|
|
53
|
+
};
|
|
54
|
+
const _stopOperation = (rId, followId, operationId) => {
|
|
55
|
+
const cb = subscriptions.get(followId)?.operations.get(operationId);
|
|
56
|
+
if (cb) cb();
|
|
57
|
+
reply(rId, "null");
|
|
58
|
+
};
|
|
59
|
+
const _header = ({ up: { getHeader } }, reply2, at) => reply2(getHeader(at));
|
|
60
|
+
const _unpin = ({ up: { unpin: innerUnpin } }, reply2, hashOrHashes) => {
|
|
61
|
+
const hashes = typeof hashOrHashes === "string" ? [hashOrHashes] : hashOrHashes;
|
|
62
|
+
hashes.forEach(innerUnpin);
|
|
63
|
+
reply2(null);
|
|
64
|
+
};
|
|
65
|
+
const _call = ({ operations, id: followId }, reply2, at, method, args) => {
|
|
66
|
+
const operationId = createOpaqueToken();
|
|
67
|
+
reply2({ result: "started", operationId });
|
|
68
|
+
const cNotification = (output, isErr = false) => notification(
|
|
69
|
+
call,
|
|
70
|
+
followId,
|
|
71
|
+
isErr ? {
|
|
72
|
+
operationId,
|
|
73
|
+
event: "operationError",
|
|
74
|
+
error: output
|
|
75
|
+
} : {
|
|
76
|
+
operationId,
|
|
77
|
+
event: "operationCallDone",
|
|
78
|
+
output
|
|
79
|
+
}
|
|
80
|
+
);
|
|
81
|
+
const subscription = upstream.runtimeCall(at, method, args).subscribe(
|
|
82
|
+
(output) => {
|
|
83
|
+
operations.delete(operationId);
|
|
84
|
+
cNotification(output);
|
|
85
|
+
},
|
|
86
|
+
(e) => {
|
|
87
|
+
operations.delete(operationId);
|
|
88
|
+
cNotification(getMsgFromErr(e), true);
|
|
89
|
+
}
|
|
90
|
+
);
|
|
91
|
+
if (!subscription.closed)
|
|
92
|
+
operations.set(operationId, () => {
|
|
93
|
+
subscription.unsubscribe();
|
|
94
|
+
operations.delete(operationId);
|
|
95
|
+
});
|
|
96
|
+
};
|
|
97
|
+
const _body = ({ operations, id: followId }, reply2, at) => {
|
|
98
|
+
const operationId = createOpaqueToken();
|
|
99
|
+
reply2({ result: "started", operationId });
|
|
100
|
+
const subscription = upstream.getBody(at).subscribe(
|
|
101
|
+
({ block: { extrinsics: value } }) => {
|
|
102
|
+
operations.delete(operationId);
|
|
103
|
+
notification(body, followId, {
|
|
104
|
+
event: "operationBodyDone",
|
|
105
|
+
operationId,
|
|
106
|
+
value
|
|
107
|
+
});
|
|
108
|
+
},
|
|
109
|
+
(e) => {
|
|
110
|
+
operations.delete(operationId);
|
|
111
|
+
notification(body, followId, {
|
|
112
|
+
event: "operationError",
|
|
113
|
+
operationId,
|
|
114
|
+
error: getMsgFromErr(e)
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
);
|
|
118
|
+
if (!subscription.closed)
|
|
119
|
+
operations.set(operationId, () => {
|
|
120
|
+
subscription.unsubscribe();
|
|
121
|
+
operations.delete(operationId);
|
|
122
|
+
});
|
|
123
|
+
};
|
|
124
|
+
const _stg = ({ operations, id: followId }, reply2, at, items) => {
|
|
125
|
+
const operationId = createOpaqueToken();
|
|
126
|
+
reply2({ result: "started", operationId });
|
|
127
|
+
const innerNotifiaction = (msg) => {
|
|
128
|
+
notification(storage, followId, msg);
|
|
129
|
+
};
|
|
130
|
+
const subscription = getStg$(upstream, at, items).pipe(
|
|
131
|
+
finalize(() => {
|
|
132
|
+
operations.delete(operationId);
|
|
133
|
+
})
|
|
134
|
+
).subscribe(
|
|
135
|
+
(items2) => {
|
|
136
|
+
innerNotifiaction({
|
|
137
|
+
event: "operationStorageItems",
|
|
138
|
+
operationId,
|
|
139
|
+
items: items2
|
|
140
|
+
});
|
|
141
|
+
},
|
|
142
|
+
(e) => {
|
|
143
|
+
innerNotifiaction({
|
|
144
|
+
event: "operationError",
|
|
145
|
+
operationId,
|
|
146
|
+
error: getMsgFromErr(e)
|
|
147
|
+
});
|
|
148
|
+
},
|
|
149
|
+
() => {
|
|
150
|
+
innerNotifiaction({
|
|
151
|
+
event: "operationStorageDone",
|
|
152
|
+
operationId
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
);
|
|
156
|
+
if (!subscription.closed)
|
|
157
|
+
operations.set(operationId, () => {
|
|
158
|
+
subscription.unsubscribe();
|
|
159
|
+
});
|
|
160
|
+
};
|
|
161
|
+
const result = (rId, method, params) => {
|
|
162
|
+
if (method === follow) return _follow(rId);
|
|
163
|
+
const [followId, ...rest] = params;
|
|
164
|
+
const ctx = subscriptions.get(followId);
|
|
165
|
+
if (!ctx) return err(rId, -32602, "Ivalid followSubscription");
|
|
166
|
+
const innerReply = (value) => {
|
|
167
|
+
reply(rId, value);
|
|
168
|
+
};
|
|
169
|
+
switch (method) {
|
|
170
|
+
case unfollow:
|
|
171
|
+
return _unfollow(rId, followId);
|
|
172
|
+
case stopOperation:
|
|
173
|
+
return _stopOperation(rId, followId, rest[0]);
|
|
174
|
+
case unpin: {
|
|
175
|
+
const [hashOrHashes] = rest;
|
|
176
|
+
if ((Array.isArray(hashOrHashes) ? hashOrHashes : [hashOrHashes]).some(
|
|
177
|
+
(hash) => typeof hash !== "string"
|
|
178
|
+
))
|
|
179
|
+
return err(rId, -32602, "Invalid args");
|
|
180
|
+
return _unpin(ctx, innerReply, hashOrHashes);
|
|
181
|
+
}
|
|
182
|
+
default: {
|
|
183
|
+
const [at, ...other] = rest;
|
|
184
|
+
if (!ctx.up.isPinned(at)) return err(rId, -32801, "Block not pinned");
|
|
185
|
+
switch (method) {
|
|
186
|
+
case header:
|
|
187
|
+
return _header(ctx, innerReply, at);
|
|
188
|
+
case body:
|
|
189
|
+
return _body(ctx, innerReply, at);
|
|
190
|
+
case call: {
|
|
191
|
+
const [method2, data] = other;
|
|
192
|
+
if (typeof method2 !== "string" || typeof data !== "string")
|
|
193
|
+
return err(rId, -32602, "Invalid args");
|
|
194
|
+
return _call(ctx, innerReply, at, method2, data);
|
|
195
|
+
}
|
|
196
|
+
case storage: {
|
|
197
|
+
const [items] = other;
|
|
198
|
+
return areItemsValid(items) ? _stg(ctx, innerReply, at, items) : err(rId, -32602, "Invalid args");
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
throw null;
|
|
204
|
+
};
|
|
205
|
+
result.stop = () => {
|
|
206
|
+
subscriptions.forEach((x) => {
|
|
207
|
+
x.cleanUp();
|
|
208
|
+
});
|
|
209
|
+
};
|
|
210
|
+
return result;
|
|
211
|
+
};
|
|
212
|
+
|
|
213
|
+
export { createChainHead };
|
|
214
|
+
//# sourceMappingURL=chain-head.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"chain-head.mjs","sources":["../../../../src/legacy/downstream/chain-head.ts"],"sourcesContent":["import { noop } from \"@polkadot-api/utils\"\nimport { finalize, Subscription } from \"rxjs\"\nimport { createUpstream } from \"../upstream/upstream\"\nimport { createOpaqueToken } from \"../utils/create-opaque-token\"\nimport { areItemsValid, getStg$ } from \"./storage\"\nimport { getMsgFromErr } from \"../utils/message-from-error\"\nimport { chainHead } from \"../../methods\"\n\nconst { follow, header, storage, body, call, unfollow, stopOperation, unpin } =\n chainHead\n\nexport const createChainHead = (\n upstream: ReturnType<typeof createUpstream>,\n reply: (id: string, result: any) => void,\n err: (id: string, code: number, msg: string) => void,\n notification: (method: string, subscription: string, result: any) => void,\n) => {\n type SubCtx = {\n id: string\n up: ReturnType<typeof upstream.getBlocks>\n operations: Map<string, () => void>\n cleanUp: () => void\n }\n const subscriptions = new Map<string, SubCtx>()\n\n const _follow = (rId: string) => {\n if (subscriptions.size === 2) {\n return err(rId, -32800, \"Limit reached\")\n }\n const token = createOpaqueToken()\n const fNotification = (result: any) =>\n notification(\"chainHead_v1_followEvent\", token, result)\n const up = upstream.getBlocks(token)\n const operations = new Map<string, () => void>()\n subscriptions.set(token, {\n id: token,\n up,\n operations,\n cleanUp: () => {\n cleanUp()\n },\n })\n\n reply(rId, token)\n\n let subscription: Subscription | null = null\n let cleanUp = () => {\n cleanUp = noop\n subscription?.unsubscribe()\n subscription = null as any\n operations.forEach((cb) => {\n cb()\n })\n operations.clear()\n subscriptions.delete(token)\n }\n\n subscription = up.blocks$.subscribe({\n next(v) {\n fNotification(v)\n },\n error() {\n cleanUp()\n fNotification({ event: \"stop\" })\n },\n })\n if (subscription.closed) subscription = null\n }\n\n const _unfollow = (rId: string, followId: string) => {\n subscriptions.get(followId)?.cleanUp()\n reply(rId, \"null\")\n }\n\n const _stopOperation = (\n rId: string,\n followId: string,\n operationId: string,\n ) => {\n const cb = subscriptions.get(followId)?.operations.get(operationId)\n if (cb) cb()\n reply(rId, \"null\")\n }\n\n const _header = (\n { up: { getHeader } }: SubCtx,\n reply: (x: any) => void,\n at: string,\n ) => reply(getHeader(at))\n\n const _unpin = (\n { up: { unpin: innerUnpin } }: SubCtx,\n reply: (x: any) => void,\n hashOrHashes: string | string[],\n ) => {\n const hashes =\n typeof hashOrHashes === \"string\" ? [hashOrHashes] : hashOrHashes\n hashes.forEach(innerUnpin)\n reply(null)\n }\n\n const _call = (\n { operations, id: followId }: SubCtx,\n reply: (x: any) => void,\n at: string,\n method: string,\n args: string,\n ) => {\n const operationId = createOpaqueToken()\n reply({ result: \"started\", operationId })\n const cNotification = (output: any, isErr = false) =>\n notification(\n call,\n followId,\n isErr\n ? {\n operationId,\n event: \"operationError\",\n error: output,\n }\n : {\n operationId,\n event: \"operationCallDone\",\n output,\n },\n )\n const subscription = upstream.runtimeCall(at, method, args).subscribe(\n (output) => {\n operations.delete(operationId)\n cNotification(output)\n },\n (e) => {\n operations.delete(operationId)\n cNotification(getMsgFromErr(e), true)\n },\n )\n if (!subscription.closed)\n operations.set(operationId, () => {\n subscription.unsubscribe()\n operations.delete(operationId)\n })\n }\n\n const _body = (\n { operations, id: followId }: SubCtx,\n reply: (x: any) => void,\n at: string,\n ) => {\n const operationId = createOpaqueToken()\n reply({ result: \"started\", operationId })\n const subscription = upstream.getBody(at).subscribe(\n ({ block: { extrinsics: value } }) => {\n operations.delete(operationId)\n notification(body, followId, {\n event: \"operationBodyDone\",\n operationId,\n value,\n })\n },\n (e) => {\n operations.delete(operationId)\n notification(body, followId, {\n event: \"operationError\",\n operationId,\n error: getMsgFromErr(e),\n })\n },\n )\n\n if (!subscription.closed)\n operations.set(operationId, () => {\n subscription.unsubscribe()\n operations.delete(operationId)\n })\n }\n\n const _stg = (\n { operations, id: followId }: SubCtx,\n reply: (x: any) => void,\n at: string,\n items: Array<{\n key: string\n type:\n | \"value\"\n | \"hash\"\n | \"descendantsValues\"\n | \"descendantsHashes\"\n | \"closestDescendantMerkleValue\"\n }>,\n ) => {\n const operationId = createOpaqueToken()\n reply({ result: \"started\", operationId })\n const innerNotifiaction = (msg: any) => {\n notification(storage, followId, msg)\n }\n const subscription = getStg$(upstream, at, items)\n .pipe(\n finalize(() => {\n operations.delete(operationId)\n }),\n )\n .subscribe(\n (items) => {\n innerNotifiaction({\n event: \"operationStorageItems\",\n operationId,\n items,\n })\n },\n (e) => {\n innerNotifiaction({\n event: \"operationError\",\n operationId,\n error: getMsgFromErr(e),\n })\n },\n () => {\n innerNotifiaction({\n event: \"operationStorageDone\",\n operationId,\n })\n },\n )\n\n if (!subscription.closed)\n operations.set(operationId, () => {\n subscription.unsubscribe()\n })\n }\n\n const result = (rId: string, method: string, params: Array<any>) => {\n if (method === follow) return _follow(rId)\n const [followId, ...rest] = params as [string, ...any[]]\n const ctx = subscriptions.get(followId)\n if (!ctx) return err(rId, -32602, \"Ivalid followSubscription\")\n\n const innerReply = (value: any) => {\n reply(rId, value)\n }\n\n switch (method) {\n case unfollow:\n return _unfollow(rId, followId)\n case stopOperation:\n return _stopOperation(rId, followId, rest[0])\n case unpin: {\n const [hashOrHashes] = rest\n if (\n (Array.isArray(hashOrHashes) ? hashOrHashes : [hashOrHashes]).some(\n (hash) => typeof hash !== \"string\",\n )\n )\n return err(rId, -32602, \"Invalid args\")\n return _unpin(ctx, innerReply, hashOrHashes)\n }\n default: {\n const [at, ...other] = rest as [string, ...any[]]\n if (!ctx.up.isPinned(at)) return err(rId, -32801, \"Block not pinned\")\n\n switch (method) {\n case header:\n return _header(ctx, innerReply, at)\n case body:\n return _body(ctx, innerReply, at)\n case call: {\n const [method, data] = other\n if (typeof method !== \"string\" || typeof data !== \"string\")\n return err(rId, -32602, \"Invalid args\")\n return _call(ctx, innerReply, at, method, data)\n }\n case storage: {\n const [items] = other\n return areItemsValid(items)\n ? _stg(ctx, innerReply, at, items)\n : err(rId, -32602, \"Invalid args\")\n }\n }\n }\n }\n throw null\n }\n\n result.stop = () => {\n subscriptions.forEach((x) => {\n x.cleanUp()\n })\n }\n return result\n}\n"],"names":["result","reply","items","method"],"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IAAY,OAAO,IAAA,KAAS,QAAA;AAChD,cAAA,OAAO,GAAA,CAAI,GAAA,EAAK,MAAA,EAAQ,cAAc,CAAA;AACxC,YAAA,OAAO,KAAA,CAAM,GAAA,EAAK,UAAA,EAAY,EAAA,EAAIA,SAAQ,IAAI,CAAA;AAAA,UAChD;AAAA,UACA,KAAK,OAAA,EAAS;AACZ,YAAA,MAAM,CAAC,KAAK,CAAA,GAAI,KAAA;AAChB,YAAA,OAAO,aAAA,CAAc,KAAK,CAAA,GACtB,IAAA,CAAK,GAAA,EAAK,UAAA,EAAY,EAAA,EAAI,KAAK,CAAA,GAC/B,GAAA,CAAI,GAAA,EAAK,MAAA,EAAQ,cAAc,CAAA;AAAA,UACrC;AAAA;AACF,MACF;AAAA;AAEF,IAAA,MAAM,IAAA;AAAA,EACR,CAAA;AAEA,EAAA,MAAA,CAAO,OAAO,MAAM;AAClB,IAAA,aAAA,CAAc,OAAA,CAAQ,CAAC,CAAA,KAAM;AAC3B,MAAA,CAAA,CAAE,OAAA,EAAQ;AAAA,IACZ,CAAC,CAAA;AAAA,EACH,CAAA;AACA,EAAA,OAAO,MAAA;AACT;;;;"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
const createChainSpec = (upstream, reply, err) => {
|
|
2
|
+
return (rId, method) => {
|
|
3
|
+
const [, , name] = method.split("_");
|
|
4
|
+
const observable = upstream[name];
|
|
5
|
+
if (!observable) throw null;
|
|
6
|
+
observable.subscribe(
|
|
7
|
+
(result) => {
|
|
8
|
+
reply(rId, result);
|
|
9
|
+
},
|
|
10
|
+
() => {
|
|
11
|
+
err(rId, -32602, "Invalid");
|
|
12
|
+
}
|
|
13
|
+
);
|
|
14
|
+
};
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export { createChainSpec };
|
|
18
|
+
//# sourceMappingURL=chainspec.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"chainspec.mjs","sources":["../../../../src/legacy/downstream/chainspec.ts"],"sourcesContent":["import { createUpstream } from \"../upstream/upstream\"\n\nexport const createChainSpec = (\n upstream: ReturnType<typeof createUpstream>,\n reply: (id: string, result: any) => void,\n err: (id: string, code: number, msg: string) => void,\n) => {\n return (rId: string, method: string) => {\n const [, , name] = method.split(\"_\")\n const observable =\n upstream[name as \"chainName\" | \"genesisHash\" | \"properties\"]\n if (!observable) throw null\n\n observable.subscribe(\n (result) => {\n reply(rId, result)\n },\n () => {\n err(rId, -32602, \"Invalid\")\n },\n )\n }\n}\n"],"names":[],"mappings":"AAEO,MAAM,eAAA,GAAkB,CAC7B,QAAA,EACA,KAAA,EACA,GAAA,KACG;AACH,EAAA,OAAO,CAAC,KAAa,MAAA,KAAmB;AACtC,IAAA,MAAM,KAAK,IAAI,CAAA,GAAI,MAAA,CAAO,MAAM,GAAG,CAAA;AACnC,IAAA,MAAM,UAAA,GACJ,SAAS,IAAkD,CAAA;AAC7D,IAAA,IAAI,CAAC,YAAY,MAAM,IAAA;AAEvB,IAAA,UAAA,CAAW,SAAA;AAAA,MACT,CAAC,MAAA,KAAW;AACV,QAAA,KAAA,CAAM,KAAK,MAAM,CAAA;AAAA,MACnB,CAAA;AAAA,MACA,MAAM;AACJ,QAAA,GAAA,CAAI,GAAA,EAAK,QAAQ,SAAS,CAAA;AAAA,MAC5B;AAAA,KACF;AAAA,EACF,CAAA;AACF;;;;"}
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { noop } from '@polkadot-api/utils';
|
|
2
|
+
import { createUpstream } from '../upstream/upstream.mjs';
|
|
3
|
+
import { createChainSpec } from './chainspec.mjs';
|
|
4
|
+
import { createChainHead } from './chain-head.mjs';
|
|
5
|
+
import { createTransactionFns } from './transaction.mjs';
|
|
6
|
+
import { createArchive } from './archive.mjs';
|
|
7
|
+
import { createClient } from '@polkadot-api/raw-client';
|
|
8
|
+
import { chainHead, chainSpec, archive, transaction } from '../../methods.mjs';
|
|
9
|
+
|
|
10
|
+
const supportedMethods = new Set(
|
|
11
|
+
[chainHead, chainSpec, archive, transaction].map((methods) => Object.values(methods)).flat()
|
|
12
|
+
);
|
|
13
|
+
const withLegacy = (base) => (onMessage, onHalt) => {
|
|
14
|
+
let clean = noop;
|
|
15
|
+
let onMsg = noop;
|
|
16
|
+
const innerConnection = base(
|
|
17
|
+
(msg) => {
|
|
18
|
+
onMsg(msg);
|
|
19
|
+
},
|
|
20
|
+
(e) => {
|
|
21
|
+
clean();
|
|
22
|
+
onHalt(e);
|
|
23
|
+
}
|
|
24
|
+
);
|
|
25
|
+
const { disconnect, request } = createClient((_onMsg) => {
|
|
26
|
+
onMsg = _onMsg;
|
|
27
|
+
return innerConnection;
|
|
28
|
+
});
|
|
29
|
+
const upstream = createUpstream(request);
|
|
30
|
+
const jsonRpc = (input) => onMessage({
|
|
31
|
+
jsonrpc: "2.0",
|
|
32
|
+
...input
|
|
33
|
+
});
|
|
34
|
+
const reply = (id, result) => {
|
|
35
|
+
jsonRpc({ id, result });
|
|
36
|
+
};
|
|
37
|
+
const err = (id, code, message) => {
|
|
38
|
+
jsonRpc({ id, error: { code, message } });
|
|
39
|
+
};
|
|
40
|
+
const notification = (method, subscription, result) => {
|
|
41
|
+
jsonRpc({ method, params: { subscription, result } });
|
|
42
|
+
};
|
|
43
|
+
const groups = {
|
|
44
|
+
chainSpec: createChainSpec(upstream, reply, err),
|
|
45
|
+
chainHead: createChainHead(upstream, reply, err, notification),
|
|
46
|
+
archive: createArchive(upstream, reply, err, notification),
|
|
47
|
+
transaction: createTransactionFns(upstream, reply)
|
|
48
|
+
};
|
|
49
|
+
clean = () => {
|
|
50
|
+
groups.chainHead.stop();
|
|
51
|
+
groups.archive.stop();
|
|
52
|
+
groups.transaction.stop();
|
|
53
|
+
upstream.clean();
|
|
54
|
+
};
|
|
55
|
+
return {
|
|
56
|
+
send: (parsedMsg) => {
|
|
57
|
+
if (!parsedMsg) return;
|
|
58
|
+
const { id, method, params } = parsedMsg;
|
|
59
|
+
if (id !== null && typeof id !== "string" && typeof id !== "number" || typeof method !== "string") {
|
|
60
|
+
console.warn(`Invalid message:
|
|
61
|
+
${JSON.stringify(parsedMsg)}`);
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
if (method === "rpc_methods") {
|
|
65
|
+
return upstream.methods.subscribe(
|
|
66
|
+
({ methods }) => {
|
|
67
|
+
reply(id, {
|
|
68
|
+
methods: [
|
|
69
|
+
...supportedMethods,
|
|
70
|
+
...methods.filter((method2) => !supportedMethods.has(method2))
|
|
71
|
+
]
|
|
72
|
+
});
|
|
73
|
+
},
|
|
74
|
+
(e) => {
|
|
75
|
+
console.error(e);
|
|
76
|
+
err(id, -32602, "Invalid");
|
|
77
|
+
}
|
|
78
|
+
);
|
|
79
|
+
}
|
|
80
|
+
const [groupName] = method.split("_");
|
|
81
|
+
if (groupName in groups) {
|
|
82
|
+
try {
|
|
83
|
+
return groups[groupName](id, method, params);
|
|
84
|
+
} catch (e) {
|
|
85
|
+
if (e !== null) throw e;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
upstream.request(
|
|
89
|
+
method,
|
|
90
|
+
params,
|
|
91
|
+
(value) => {
|
|
92
|
+
reply(id, value);
|
|
93
|
+
},
|
|
94
|
+
(e) => {
|
|
95
|
+
err(id, e?.code || -1, e?.message || "");
|
|
96
|
+
}
|
|
97
|
+
);
|
|
98
|
+
},
|
|
99
|
+
disconnect: () => {
|
|
100
|
+
clean();
|
|
101
|
+
disconnect();
|
|
102
|
+
}
|
|
103
|
+
};
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
export { withLegacy };
|
|
107
|
+
//# sourceMappingURL=downstream.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"downstream.mjs","sources":["../../../../src/legacy/downstream/downstream.ts"],"sourcesContent":["import { noop } from \"@polkadot-api/utils\"\nimport { createUpstream } from \"../upstream\"\nimport { createChainSpec } from \"./chainspec\"\nimport { createChainHead } from \"./chain-head\"\nimport { createTransactionFns } from \"./transaction\"\nimport { createArchive } from \"./archive\"\nimport { Middleware } from \"../../types\"\nimport { createClient } from \"@polkadot-api/raw-client\"\nimport { JsonRpcMessage } from \"@polkadot-api/json-rpc-provider\"\nimport { archive, chainHead, chainSpec, transaction } from \"../../methods\"\n\nconst supportedMethods = new Set(\n [chainHead, chainSpec, archive, transaction]\n .map((methods) => Object.values(methods) as string[])\n .flat(),\n)\n\nexport const withLegacy: Middleware = (base) => (onMessage, onHalt) => {\n let clean = noop\n let onMsg: (msg: JsonRpcMessage) => void = noop\n const innerConnection = base(\n (msg) => {\n onMsg(msg)\n },\n (e) => {\n clean()\n onHalt(e)\n },\n )\n const { disconnect, request } = createClient((_onMsg) => {\n onMsg = _onMsg\n return innerConnection\n })\n const upstream = createUpstream(request)\n\n const jsonRpc = (\n input:\n | ({ id: string } & (\n | { result: any }\n | { error: { code: number; message: string } }\n ))\n | {\n method: string\n params: { subscription: string; result: any }\n },\n ) =>\n onMessage({\n jsonrpc: \"2.0\",\n ...input,\n })\n\n const reply = (id: string, result: any) => {\n jsonRpc({ id, result })\n }\n const err = (id: string, code: number, message: string) => {\n jsonRpc({ id, error: { code, message } })\n }\n const notification = (method: string, subscription: string, result: any) => {\n jsonRpc({ method, params: { subscription, result } })\n }\n\n const groups = {\n chainSpec: createChainSpec(upstream, reply, err),\n chainHead: createChainHead(upstream, reply, err, notification),\n archive: createArchive(upstream, reply, err, notification),\n transaction: createTransactionFns(upstream, reply),\n }\n\n clean = () => {\n groups.chainHead.stop()\n groups.archive.stop()\n groups.transaction.stop()\n upstream.clean()\n }\n\n return {\n send: (parsedMsg: any) => {\n if (!parsedMsg) return\n const { id, method, params } = parsedMsg\n if (\n (id !== null && typeof id !== \"string\" && typeof id !== \"number\") ||\n typeof method !== \"string\"\n ) {\n console.warn(`Invalid message:\\n${JSON.stringify(parsedMsg)}`)\n return\n }\n if (method === \"rpc_methods\") {\n return upstream.methods.subscribe(\n ({ methods }) => {\n reply(id, {\n methods: [\n ...supportedMethods,\n ...methods.filter((method) => !supportedMethods.has(method)),\n ],\n })\n },\n (e: any) => {\n console.error(e)\n err(id, -32602, \"Invalid\")\n },\n )\n }\n\n const [groupName] = method.split(\"_\")\n if (groupName in groups) {\n try {\n return groups[groupName as keyof typeof groups](id, method, params)\n } catch (e) {\n if (e !== null) throw e\n }\n }\n\n upstream.request(\n method,\n params,\n (value) => {\n reply(id, value)\n },\n (e) => {\n err(id, e?.code || -1, e?.message || \"\")\n },\n )\n },\n disconnect: () => {\n clean()\n disconnect()\n },\n }\n}\n"],"names":["method"],"mappings":";;;;;;;;;AAWA,MAAM,mBAAmB,IAAI,GAAA;AAAA,EAC3B,CAAC,SAAA,EAAW,SAAA,EAAW,OAAA,EAAS,WAAW,CAAA,CACxC,GAAA,CAAI,CAAC,OAAA,KAAY,MAAA,CAAO,MAAA,CAAO,OAAO,CAAa,EACnD,IAAA;AACL,CAAA;AAEO,MAAM,UAAA,GAAyB,CAAC,IAAA,KAAS,CAAC,WAAW,MAAA,KAAW;AACrE,EAAA,IAAI,KAAA,GAAQ,IAAA;AACZ,EAAA,IAAI,KAAA,GAAuC,IAAA;AAC3C,EAAA,MAAM,eAAA,GAAkB,IAAA;AAAA,IACtB,CAAC,GAAA,KAAQ;AACP,MAAA,KAAA,CAAM,GAAG,CAAA;AAAA,IACX,CAAA;AAAA,IACA,CAAC,CAAA,KAAM;AACL,MAAA,KAAA,EAAM;AACN,MAAA,MAAA,CAAO,CAAC,CAAA;AAAA,IACV;AAAA,GACF;AACA,EAAA,MAAM,EAAE,UAAA,EAAY,OAAA,EAAQ,GAAI,YAAA,CAAa,CAAC,MAAA,KAAW;AACvD,IAAA,KAAA,GAAQ,MAAA;AACR,IAAA,OAAO,eAAA;AAAA,EACT,CAAC,CAAA;AACD,EAAA,MAAM,QAAA,GAAW,eAAe,OAAO,CAAA;AAEvC,EAAA,MAAM,OAAA,GAAU,CACd,KAAA,KAUA,SAAA,CAAU;AAAA,IACR,OAAA,EAAS,KAAA;AAAA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A;AAAA,QACjB,CAAA;AAAA,QACA,CAAC,CAAA,KAAM;AACL,UAAA,GAAA,CAAI,IAAI,CAAA,EAAG,IAAA,IAAQ,EAAA,EAAI,CAAA,EAAG,WAAW,EAAE,CAAA;AAAA,QACzC;AAAA,OACF;AAAA,IACF,CAAA;AAAA,IACA,YAAY,MAAM;AAChB,MAAA,KAAA,EAAM;AACN,MAAA,UAAA,EAAW;AAAA,IACb;AAAA,GACF;AACF;;;;"}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { merge, filter, map } from 'rxjs';
|
|
2
|
+
|
|
3
|
+
const validStorageTypes = /* @__PURE__ */ new Set([
|
|
4
|
+
"value",
|
|
5
|
+
"hash",
|
|
6
|
+
"closestDescendantMerkleValue",
|
|
7
|
+
"descendantsValues",
|
|
8
|
+
"descendantsHashes"
|
|
9
|
+
]);
|
|
10
|
+
const areItemsValid = (items) => Array.isArray(items) && items.every(
|
|
11
|
+
(x) => typeof x === "object" && typeof x.key === "string" && validStorageTypes.has(x.type)
|
|
12
|
+
);
|
|
13
|
+
const getStg$ = (upstream, at, items) => merge(
|
|
14
|
+
...items.map(({ key, type }) => {
|
|
15
|
+
switch (type) {
|
|
16
|
+
case "value":
|
|
17
|
+
return upstream.stgValue(at, key).pipe(
|
|
18
|
+
filter(Boolean),
|
|
19
|
+
map((value) => [
|
|
20
|
+
{
|
|
21
|
+
key,
|
|
22
|
+
value
|
|
23
|
+
}
|
|
24
|
+
])
|
|
25
|
+
);
|
|
26
|
+
case "hash":
|
|
27
|
+
return upstream.stgHash(at, key).pipe(
|
|
28
|
+
filter(Boolean),
|
|
29
|
+
map((hash) => [
|
|
30
|
+
{
|
|
31
|
+
key,
|
|
32
|
+
hash
|
|
33
|
+
}
|
|
34
|
+
])
|
|
35
|
+
);
|
|
36
|
+
case "descendantsValues":
|
|
37
|
+
return upstream.stgDescendantValues(at, key).pipe(
|
|
38
|
+
map((values) => values.map(([key2, value]) => ({ key: key2, value })))
|
|
39
|
+
);
|
|
40
|
+
case "descendantsHashes":
|
|
41
|
+
return upstream.stgDescendantHashes(at, key).pipe(map((values) => values.map(([key2, hash]) => ({ key: key2, hash }))));
|
|
42
|
+
case "closestDescendantMerkleValue":
|
|
43
|
+
return upstream.stgClosestDescendant(at, key).pipe(
|
|
44
|
+
filter(Boolean),
|
|
45
|
+
map((closestDescendantMerkleValue) => [
|
|
46
|
+
{
|
|
47
|
+
key,
|
|
48
|
+
closestDescendantMerkleValue
|
|
49
|
+
}
|
|
50
|
+
])
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
})
|
|
54
|
+
);
|
|
55
|
+
|
|
56
|
+
export { areItemsValid, getStg$ };
|
|
57
|
+
//# sourceMappingURL=storage.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"storage.mjs","sources":["../../../../src/legacy/downstream/storage.ts"],"sourcesContent":["import { filter, map, merge } from \"rxjs\"\nimport { createUpstream } from \"../upstream/upstream\"\n\nconst validStorageTypes = new Set([\n \"value\",\n \"hash\",\n \"closestDescendantMerkleValue\",\n \"descendantsValues\",\n \"descendantsHashes\",\n])\n\nexport type Items = Array<{\n key: string\n type:\n | \"value\"\n | \"hash\"\n | \"closestDescendantMerkleValue\"\n | \"descendantsValues\"\n | \"descendantsHashes\"\n}>\n\nexport const areItemsValid = (items: any): items is Items =>\n Array.isArray(items) &&\n items.every(\n (x) =>\n typeof x === \"object\" &&\n typeof x.key === \"string\" &&\n validStorageTypes.has(x.type),\n )\n\nexport const getStg$ = (\n upstream: ReturnType<typeof createUpstream>,\n at: string,\n items: Array<{\n key: string\n type:\n | \"value\"\n | \"hash\"\n | \"descendantsValues\"\n | \"descendantsHashes\"\n | \"closestDescendantMerkleValue\"\n }>,\n) =>\n merge(\n ...items.map(({ key, type }) => {\n switch (type) {\n case \"value\":\n return upstream.stgValue(at, key).pipe(\n filter(Boolean),\n map((value) => [\n {\n key,\n value,\n },\n ]),\n )\n case \"hash\":\n return upstream.stgHash(at, key).pipe(\n filter(Boolean),\n map((hash) => [\n {\n key,\n hash,\n },\n ]),\n )\n case \"descendantsValues\":\n return upstream\n .stgDescendantValues(at, key)\n .pipe(\n map((values) => values.map(([key, value]) => ({ key, value }))),\n )\n\n case \"descendantsHashes\":\n return upstream\n .stgDescendantHashes(at, key)\n .pipe(map((values) => values.map(([key, hash]) => ({ key, hash }))))\n\n case \"closestDescendantMerkleValue\":\n return upstream.stgClosestDescendant(at, key).pipe(\n filter(Boolean),\n map((closestDescendantMerkleValue) => [\n {\n key,\n closestDescendantMerkleValue,\n },\n ]),\n )\n }\n }),\n )\n"],"names":["key"],"mappings":";;AAGA,MAAM,iBAAA,uBAAwB,GAAA,CAAI;AAAA,EAChC,OAAA;AAAA,EACA,MAAA;AAAA,EACA,8BAAA;AAAA,EACA,mBAAA;AAAA,EACA;AACF,CAAC,CAAA;AAYM,MAAM,gBAAgB,CAAC,KAAA,KAC5B,MAAM,OAAA,CAAQ,KAAK,KACnB,KAAA,CAAM,KAAA;AAAA,EACJ,CAAC,CAAA,KACC,OAAO,CAAA,KAAM,QAAA,IACb,OAAO,CAAA,CAAE,GAAA,KAAQ,QAAA,IACjB,iBAAA,CAAkB,GAAA,CAAI,CAAA,CAAE,IAAI;AAChC;AAEK,MAAM,OAAA,GAAU,CACrB,QAAA,EACA,EAAA,EACA,KAAA,KAUA,KAAA;AAAA,EACE,GAAG,KAAA,CAAM,GAAA,CAAI,CAAC,EAAE,GAAA,EAAK,MAAK,KAAM;AAC9B,IAAA,QAAQ,IAAA;AAAM,MACZ,KAAK,OAAA;AACH,QAAA,OAAO,QAAA,CAAS,QAAA,CAAS,EAAA,EAAI,GAAG,CAAA,CAAE,IAAA;AAAA,UAChC,OAAO,OAAO,CAAA;AAAA,UACd,GAAA,CAAI,CAAC,KAAA,KAAU;AAAA,YACb;AAAA,cACE,GAAA;AAAA,cACA;AAAA;AACF,WACD;AAAA,SACH;AAAA,MACF,KAAK,MAAA;AACH,QAAA,OAAO,QAA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