@polkadot-api/smoldot 0.2.7 → 0.3.0
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/dist/from-node-worker/esm/from-node-worker.mjs +66 -0
- package/dist/from-node-worker/esm/from-node-worker.mjs.map +1 -0
- package/dist/from-node-worker/from-node-worker.d.ts +8 -0
- package/dist/from-node-worker/from-node-worker.js +68 -0
- package/dist/from-node-worker/from-node-worker.js.map +1 -0
- package/dist/node-worker/esm/node-worker.mjs +73 -0
- package/dist/node-worker/esm/node-worker.mjs.map +1 -0
- package/dist/node-worker/node-worker.d.ts +33 -0
- package/dist/node-worker/node-worker.js +75 -0
- package/dist/node-worker/node-worker.js.map +1 -0
- package/from-worker/package.json +1 -0
- package/package.json +26 -6
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
const startFromWorker = (worker, options = {}) => {
|
|
2
|
+
sendToWorker(worker, {
|
|
3
|
+
type: "start",
|
|
4
|
+
value: options
|
|
5
|
+
});
|
|
6
|
+
return {
|
|
7
|
+
async addChain(options2) {
|
|
8
|
+
const id = await sendToWorker(worker, {
|
|
9
|
+
type: "add-chain",
|
|
10
|
+
value: options2
|
|
11
|
+
});
|
|
12
|
+
return {
|
|
13
|
+
nextJsonRpcResponse() {
|
|
14
|
+
return sendToWorker(worker, {
|
|
15
|
+
type: "chain",
|
|
16
|
+
value: {
|
|
17
|
+
id,
|
|
18
|
+
type: "receive"
|
|
19
|
+
}
|
|
20
|
+
});
|
|
21
|
+
},
|
|
22
|
+
remove() {
|
|
23
|
+
return sendToWorker(worker, {
|
|
24
|
+
type: "chain",
|
|
25
|
+
value: {
|
|
26
|
+
id,
|
|
27
|
+
type: "remove"
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
},
|
|
31
|
+
sendJsonRpc(value) {
|
|
32
|
+
return sendToWorker(worker, {
|
|
33
|
+
type: "chain",
|
|
34
|
+
value: {
|
|
35
|
+
id,
|
|
36
|
+
type: "send",
|
|
37
|
+
value
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
},
|
|
43
|
+
async terminate() {
|
|
44
|
+
await sendToWorker(worker, {
|
|
45
|
+
type: "terminate"
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
};
|
|
50
|
+
let msgId = 0;
|
|
51
|
+
function sendToWorker(worker, msg) {
|
|
52
|
+
const id = msgId++;
|
|
53
|
+
worker.postMessage({ ...msg, id });
|
|
54
|
+
return new Promise((resolve) => {
|
|
55
|
+
const msgHandler = (response) => {
|
|
56
|
+
if (response.id === id) {
|
|
57
|
+
resolve(response.value);
|
|
58
|
+
worker.off("message", msgHandler);
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
worker.on("message", msgHandler);
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export { startFromWorker };
|
|
66
|
+
//# sourceMappingURL=from-node-worker.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"from-node-worker.mjs","sources":["../../../src/from-node-worker.ts"],"sourcesContent":["import { Worker } from \"node:worker_threads\"\nimport type { Client } from \"smoldot\"\nimport type { RequestMessage, SmoldotOptions } from \"./node-worker\"\n\nexport const startFromWorker = (\n worker: Worker,\n options: SmoldotOptions = {},\n): Client => {\n sendToWorker(worker, {\n type: \"start\",\n value: options,\n })\n\n return {\n async addChain(options) {\n const id = await sendToWorker(worker, {\n type: \"add-chain\",\n value: options,\n })\n return {\n nextJsonRpcResponse() {\n return sendToWorker(worker, {\n type: \"chain\",\n value: {\n id,\n type: \"receive\",\n },\n })\n },\n remove() {\n return sendToWorker(worker, {\n type: \"chain\",\n value: {\n id,\n type: \"remove\",\n },\n })\n },\n sendJsonRpc(value) {\n return sendToWorker(worker, {\n type: \"chain\",\n value: {\n id,\n type: \"send\",\n value,\n },\n })\n },\n }\n },\n async terminate() {\n await sendToWorker(worker, {\n type: \"terminate\",\n })\n },\n }\n}\n\nlet msgId = 0\nfunction sendToWorker(worker: Worker, msg: RequestMessage): Promise<any> {\n const id = msgId++\n worker.postMessage({ ...msg, id })\n return new Promise((resolve) => {\n const msgHandler = (response: any) => {\n if (response.id === id) {\n resolve(response.value)\n worker.off(\"message\", msgHandler)\n }\n }\n worker.on(\"message\", msgHandler)\n })\n}\n"],"names":["options"],"mappings":"AAIO,MAAM,eAAkB,GAAA,CAC7B,MACA,EAAA,OAAA,GAA0B,EACf,KAAA;AACX,EAAA,YAAA,CAAa,MAAQ,EAAA;AAAA,IACnB,IAAM,EAAA,OAAA;AAAA,IACN,KAAO,EAAA,OAAA;AAAA,GACR,CAAA,CAAA;AAED,EAAO,OAAA;AAAA,IACL,MAAM,SAASA,QAAS,EAAA;AACtB,MAAM,MAAA,EAAA,GAAK,MAAM,YAAA,CAAa,MAAQ,EAAA;AAAA,QACpC,IAAM,EAAA,WAAA;AAAA,QACN,KAAOA,EAAAA,QAAAA;AAAA,OACR,CAAA,CAAA;AACD,MAAO,OAAA;AAAA,QACL,mBAAsB,GAAA;AACpB,UAAA,OAAO,aAAa,MAAQ,EAAA;AAAA,YAC1B,IAAM,EAAA,OAAA;AAAA,YACN,KAAO,EAAA;AAAA,cACL,EAAA;AAAA,cACA,IAAM,EAAA,SAAA;AAAA,aACR;AAAA,WACD,CAAA,CAAA;AAAA,SACH;AAAA,QACA,MAAS,GAAA;AACP,UAAA,OAAO,aAAa,MAAQ,EAAA;AAAA,YAC1B,IAAM,EAAA,OAAA;AAAA,YACN,KAAO,EAAA;AAAA,cACL,EAAA;AAAA,cACA,IAAM,EAAA,QAAA;AAAA,aACR;AAAA,WACD,CAAA,CAAA;AAAA,SACH;AAAA,QACA,YAAY,KAAO,EAAA;AACjB,UAAA,OAAO,aAAa,MAAQ,EAAA;AAAA,YAC1B,IAAM,EAAA,OAAA;AAAA,YACN,KAAO,EAAA;AAAA,cACL,EAAA;AAAA,cACA,IAAM,EAAA,MAAA;AAAA,cACN,KAAA;AAAA,aACF;AAAA,WACD,CAAA,CAAA;AAAA,SACH;AAAA,OACF,CAAA;AAAA,KACF;AAAA,IACA,MAAM,SAAY,GAAA;AAChB,MAAA,MAAM,aAAa,MAAQ,EAAA;AAAA,QACzB,IAAM,EAAA,WAAA;AAAA,OACP,CAAA,CAAA;AAAA,KACH;AAAA,GACF,CAAA;AACF,EAAA;AAEA,IAAI,KAAQ,GAAA,CAAA,CAAA;AACZ,SAAS,YAAA,CAAa,QAAgB,GAAmC,EAAA;AACvE,EAAA,MAAM,EAAK,GAAA,KAAA,EAAA,CAAA;AACX,EAAA,MAAA,CAAO,WAAY,CAAA,EAAE,GAAG,GAAA,EAAK,IAAI,CAAA,CAAA;AACjC,EAAO,OAAA,IAAI,OAAQ,CAAA,CAAC,OAAY,KAAA;AAC9B,IAAM,MAAA,UAAA,GAAa,CAAC,QAAkB,KAAA;AACpC,MAAI,IAAA,QAAA,CAAS,OAAO,EAAI,EAAA;AACtB,QAAA,OAAA,CAAQ,SAAS,KAAK,CAAA,CAAA;AACtB,QAAO,MAAA,CAAA,GAAA,CAAI,WAAW,UAAU,CAAA,CAAA;AAAA,OAClC;AAAA,KACF,CAAA;AACA,IAAO,MAAA,CAAA,EAAA,CAAG,WAAW,UAAU,CAAA,CAAA;AAAA,GAChC,CAAA,CAAA;AACH;;;;"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { Worker } from 'node:worker_threads';
|
|
2
|
+
import { ClientOptions, Client } from 'smoldot';
|
|
3
|
+
|
|
4
|
+
type SmoldotOptions = Omit<ClientOptions, "portToWorker">;
|
|
5
|
+
|
|
6
|
+
declare const startFromWorker: (worker: Worker, options?: SmoldotOptions) => Client;
|
|
7
|
+
|
|
8
|
+
export { startFromWorker };
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const startFromWorker = (worker, options = {}) => {
|
|
4
|
+
sendToWorker(worker, {
|
|
5
|
+
type: "start",
|
|
6
|
+
value: options
|
|
7
|
+
});
|
|
8
|
+
return {
|
|
9
|
+
async addChain(options2) {
|
|
10
|
+
const id = await sendToWorker(worker, {
|
|
11
|
+
type: "add-chain",
|
|
12
|
+
value: options2
|
|
13
|
+
});
|
|
14
|
+
return {
|
|
15
|
+
nextJsonRpcResponse() {
|
|
16
|
+
return sendToWorker(worker, {
|
|
17
|
+
type: "chain",
|
|
18
|
+
value: {
|
|
19
|
+
id,
|
|
20
|
+
type: "receive"
|
|
21
|
+
}
|
|
22
|
+
});
|
|
23
|
+
},
|
|
24
|
+
remove() {
|
|
25
|
+
return sendToWorker(worker, {
|
|
26
|
+
type: "chain",
|
|
27
|
+
value: {
|
|
28
|
+
id,
|
|
29
|
+
type: "remove"
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
},
|
|
33
|
+
sendJsonRpc(value) {
|
|
34
|
+
return sendToWorker(worker, {
|
|
35
|
+
type: "chain",
|
|
36
|
+
value: {
|
|
37
|
+
id,
|
|
38
|
+
type: "send",
|
|
39
|
+
value
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
},
|
|
45
|
+
async terminate() {
|
|
46
|
+
await sendToWorker(worker, {
|
|
47
|
+
type: "terminate"
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
};
|
|
52
|
+
let msgId = 0;
|
|
53
|
+
function sendToWorker(worker, msg) {
|
|
54
|
+
const id = msgId++;
|
|
55
|
+
worker.postMessage({ ...msg, id });
|
|
56
|
+
return new Promise((resolve) => {
|
|
57
|
+
const msgHandler = (response) => {
|
|
58
|
+
if (response.id === id) {
|
|
59
|
+
resolve(response.value);
|
|
60
|
+
worker.off("message", msgHandler);
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
worker.on("message", msgHandler);
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
exports.startFromWorker = startFromWorker;
|
|
68
|
+
//# sourceMappingURL=from-node-worker.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"from-node-worker.js","sources":["../../src/from-node-worker.ts"],"sourcesContent":["import { Worker } from \"node:worker_threads\"\nimport type { Client } from \"smoldot\"\nimport type { RequestMessage, SmoldotOptions } from \"./node-worker\"\n\nexport const startFromWorker = (\n worker: Worker,\n options: SmoldotOptions = {},\n): Client => {\n sendToWorker(worker, {\n type: \"start\",\n value: options,\n })\n\n return {\n async addChain(options) {\n const id = await sendToWorker(worker, {\n type: \"add-chain\",\n value: options,\n })\n return {\n nextJsonRpcResponse() {\n return sendToWorker(worker, {\n type: \"chain\",\n value: {\n id,\n type: \"receive\",\n },\n })\n },\n remove() {\n return sendToWorker(worker, {\n type: \"chain\",\n value: {\n id,\n type: \"remove\",\n },\n })\n },\n sendJsonRpc(value) {\n return sendToWorker(worker, {\n type: \"chain\",\n value: {\n id,\n type: \"send\",\n value,\n },\n })\n },\n }\n },\n async terminate() {\n await sendToWorker(worker, {\n type: \"terminate\",\n })\n },\n }\n}\n\nlet msgId = 0\nfunction sendToWorker(worker: Worker, msg: RequestMessage): Promise<any> {\n const id = msgId++\n worker.postMessage({ ...msg, id })\n return new Promise((resolve) => {\n const msgHandler = (response: any) => {\n if (response.id === id) {\n resolve(response.value)\n worker.off(\"message\", msgHandler)\n }\n }\n worker.on(\"message\", msgHandler)\n })\n}\n"],"names":["options"],"mappings":";;AAIO,MAAM,eAAkB,GAAA,CAC7B,MACA,EAAA,OAAA,GAA0B,EACf,KAAA;AACX,EAAA,YAAA,CAAa,MAAQ,EAAA;AAAA,IACnB,IAAM,EAAA,OAAA;AAAA,IACN,KAAO,EAAA,OAAA;AAAA,GACR,CAAA,CAAA;AAED,EAAO,OAAA;AAAA,IACL,MAAM,SAASA,QAAS,EAAA;AACtB,MAAM,MAAA,EAAA,GAAK,MAAM,YAAA,CAAa,MAAQ,EAAA;AAAA,QACpC,IAAM,EAAA,WAAA;AAA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S,OAAO,EAAI,EAAA;AACtB,QAAA,OAAA,CAAQ,SAAS,KAAK,CAAA,CAAA;AACtB,QAAO,MAAA,CAAA,GAAA,CAAI,WAAW,UAAU,CAAA,CAAA;AAAA,OAClC;AAAA,KACF,CAAA;AACA,IAAO,MAAA,CAAA,EAAA,CAAG,WAAW,UAAU,CAAA,CAAA;AAAA,GAChC,CAAA,CAAA;AACH;;;;"}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { parentPort } from 'node:worker_threads';
|
|
2
|
+
import { start } from 'smoldot';
|
|
3
|
+
|
|
4
|
+
let smoldot = null;
|
|
5
|
+
let chainId = 0;
|
|
6
|
+
const chains = /* @__PURE__ */ new Map();
|
|
7
|
+
parentPort.on("message", async (msg) => {
|
|
8
|
+
if (msg.type === "start") {
|
|
9
|
+
if (smoldot !== null) {
|
|
10
|
+
throw new Error("Can't call start on a client already started");
|
|
11
|
+
}
|
|
12
|
+
smoldot = start(msg.value);
|
|
13
|
+
return parentPort?.postMessage({
|
|
14
|
+
id: msg.id
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
if (smoldot === null) {
|
|
18
|
+
throw new Error("Smoldot not started");
|
|
19
|
+
}
|
|
20
|
+
switch (msg.type) {
|
|
21
|
+
case "add-chain": {
|
|
22
|
+
const potentialRelayChains = msg.value.potentialRelayChainIds?.map(
|
|
23
|
+
(id2) => {
|
|
24
|
+
const chain2 = chains.get(id2);
|
|
25
|
+
if (!chain2) throw new Error("Can't reference removed chain");
|
|
26
|
+
return chain2;
|
|
27
|
+
}
|
|
28
|
+
);
|
|
29
|
+
const chain = await smoldot.addChain({
|
|
30
|
+
...msg.value,
|
|
31
|
+
potentialRelayChains
|
|
32
|
+
});
|
|
33
|
+
const id = chainId++;
|
|
34
|
+
chains.set(id, chain);
|
|
35
|
+
parentPort?.postMessage({
|
|
36
|
+
id: msg.id,
|
|
37
|
+
value: id
|
|
38
|
+
});
|
|
39
|
+
break;
|
|
40
|
+
}
|
|
41
|
+
case "terminate":
|
|
42
|
+
await smoldot.terminate();
|
|
43
|
+
parentPort?.postMessage({
|
|
44
|
+
id: msg.id
|
|
45
|
+
});
|
|
46
|
+
smoldot = null;
|
|
47
|
+
chains.clear();
|
|
48
|
+
break;
|
|
49
|
+
case "chain":
|
|
50
|
+
handleChainMessage(msg, msg.id);
|
|
51
|
+
break;
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
async function handleChainMessage(msg, id) {
|
|
55
|
+
const chain = chains.get(msg.value.id);
|
|
56
|
+
if (!chain) throw new Error("Can't reference removed chain");
|
|
57
|
+
switch (msg.value.type) {
|
|
58
|
+
case "receive":
|
|
59
|
+
parentPort?.postMessage({
|
|
60
|
+
id,
|
|
61
|
+
value: await chain.nextJsonRpcResponse()
|
|
62
|
+
});
|
|
63
|
+
break;
|
|
64
|
+
case "send":
|
|
65
|
+
chain.sendJsonRpc(msg.value.value);
|
|
66
|
+
break;
|
|
67
|
+
case "remove":
|
|
68
|
+
chain.remove();
|
|
69
|
+
chains.delete(msg.value.id);
|
|
70
|
+
break;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
//# sourceMappingURL=node-worker.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"node-worker.mjs","sources":["../../../src/node-worker.ts"],"sourcesContent":["import { parentPort } from \"node:worker_threads\"\nimport { AddChainOptions, Chain, Client, ClientOptions, start } from \"smoldot\"\n\nexport type SmoldotOptions = Omit<ClientOptions, \"portToWorker\">\nexport interface StartMsg {\n type: \"start\"\n value: SmoldotOptions\n}\nexport interface AddChainMsg {\n type: \"add-chain\"\n value: Omit<AddChainOptions, \"potentialRelayChains\"> & {\n potentialRelayChainIds?: number[]\n }\n}\nexport interface TerminateMsg {\n type: \"terminate\"\n}\nexport interface ChainMsg {\n type: \"chain\"\n value: {\n id: number\n } & (\n | {\n type: \"remove\" | \"receive\"\n }\n | {\n type: \"send\"\n value: string\n }\n )\n}\nexport type RequestMessage = StartMsg | AddChainMsg | TerminateMsg | ChainMsg\nexport type CorrelatedRequestMessage = { id: number } & RequestMessage\n\nlet smoldot: Client | null = null\n\nlet chainId = 0\nconst chains = new Map<number, Chain>()\n\nparentPort!.on(\"message\", async (msg: CorrelatedRequestMessage) => {\n if (msg.type === \"start\") {\n if (smoldot !== null) {\n throw new Error(\"Can't call start on a client already started\")\n }\n smoldot = start(msg.value)\n return parentPort?.postMessage({\n id: msg.id,\n })\n }\n\n if (smoldot === null) {\n throw new Error(\"Smoldot not started\")\n }\n\n switch (msg.type) {\n case \"add-chain\": {\n const potentialRelayChains = msg.value.potentialRelayChainIds?.map(\n (id) => {\n const chain = chains.get(id)\n if (!chain) throw new Error(\"Can't reference removed chain\")\n return chain\n },\n )\n const chain = await smoldot.addChain({\n ...msg.value,\n potentialRelayChains,\n })\n const id = chainId++\n chains.set(id, chain)\n parentPort?.postMessage({\n id: msg.id,\n value: id,\n })\n break\n }\n case \"terminate\":\n await smoldot.terminate()\n parentPort?.postMessage({\n id: msg.id,\n })\n smoldot = null\n chains.clear()\n break\n case \"chain\":\n handleChainMessage(msg, msg.id)\n break\n }\n})\n\nasync function handleChainMessage(msg: ChainMsg, id: number) {\n const chain = chains.get(msg.value.id)\n if (!chain) throw new Error(\"Can't reference removed chain\")\n\n switch (msg.value.type) {\n case \"receive\":\n parentPort?.postMessage({\n id,\n value: await chain.nextJsonRpcResponse(),\n })\n break\n case \"send\":\n chain.sendJsonRpc(msg.value.value)\n break\n case \"remove\":\n chain.remove()\n chains.delete(msg.value.id)\n break\n }\n}\n"],"names":["id","chain"],"mappings":";;;AAkCA,IAAI,OAAyB,GAAA,IAAA,CAAA;AAE7B,IAAI,OAAU,GAAA,CAAA,CAAA;AACd,MAAM,MAAA,uBAAa,GAAmB,EAAA,CAAA;AAEtC,UAAY,CAAA,EAAA,CAAG,SAAW,EAAA,OAAO,GAAkC,KAAA;AACjE,EAAI,IAAA,GAAA,CAAI,SAAS,OAAS,EAAA;AACxB,IAAA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AA;AAAA,QACtB,IAAI,GAAI,CAAA,EAAA;AAAA,OACT,CAAA,CAAA;AACD,MAAU,OAAA,GAAA,IAAA,CAAA;AACV,MAAA,MAAA,CAAO,KAAM,EAAA,CAAA;AACb,MAAA,MAAA;AAAA,IACF,KAAK,OAAA;AACH,MAAmB,kBAAA,CAAA,GAAA,EAAK,IAAI,EAAE,CAAA,CAAA;AAC9B,MAAA,MAAA;AAAA,GACJ;AACF,CAAC,CAAA,CAAA;AAED,eAAe,kBAAA,CAAmB,KAAe,EAAY,EAAA;AAC3D,EAAA,MAAM,KAAQ,GAAA,MAAA,CAAO,GAAI,CAAA,GAAA,CAAI,MAAM,EAAE,CAAA,CAAA;AACrC,EAAA,IAAI,CAAC,KAAA,EAAa,MAAA,IAAI,MAAM,+BAA+B,CAAA,CAAA;AAE3D,EAAQ,QAAA,GAAA,CAAI,MAAM,IAAM;AAAA,IACtB,KAAK,SAAA;AACH,MAAA,UAAA,EAAY,WAAY,CAAA;AAAA,QACtB,EAAA;AAAA,QACA,KAAA,EAAO,MAAM,KAAA,CAAM,mBAAoB,EAAA;AAAA,OACxC,CAAA,CAAA;AACD,MAAA,MAAA;AAAA,IACF,KAAK,MAAA;AACH,MAAM,KAAA,CAAA,WAAA,CAAY,GAAI,CAAA,KAAA,CAAM,KAAK,CAAA,CAAA;AACjC,MAAA,MAAA;AAAA,IACF,KAAK,QAAA;AACH,MAAA,KAAA,CAAM,MAAO,EAAA,CAAA;AACb,MAAO,MAAA,CAAA,MAAA,CAAO,GAAI,CAAA,KAAA,CAAM,EAAE,CAAA,CAAA;AAC1B,MAAA,MAAA;AAAA,GACJ;AACF"}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { ClientOptions, AddChainOptions } from 'smoldot';
|
|
2
|
+
|
|
3
|
+
type SmoldotOptions = Omit<ClientOptions, "portToWorker">;
|
|
4
|
+
interface StartMsg {
|
|
5
|
+
type: "start";
|
|
6
|
+
value: SmoldotOptions;
|
|
7
|
+
}
|
|
8
|
+
interface AddChainMsg {
|
|
9
|
+
type: "add-chain";
|
|
10
|
+
value: Omit<AddChainOptions, "potentialRelayChains"> & {
|
|
11
|
+
potentialRelayChainIds?: number[];
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
interface TerminateMsg {
|
|
15
|
+
type: "terminate";
|
|
16
|
+
}
|
|
17
|
+
interface ChainMsg {
|
|
18
|
+
type: "chain";
|
|
19
|
+
value: {
|
|
20
|
+
id: number;
|
|
21
|
+
} & ({
|
|
22
|
+
type: "remove" | "receive";
|
|
23
|
+
} | {
|
|
24
|
+
type: "send";
|
|
25
|
+
value: string;
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
type RequestMessage = StartMsg | AddChainMsg | TerminateMsg | ChainMsg;
|
|
29
|
+
type CorrelatedRequestMessage = {
|
|
30
|
+
id: number;
|
|
31
|
+
} & RequestMessage;
|
|
32
|
+
|
|
33
|
+
export type { AddChainMsg, ChainMsg, CorrelatedRequestMessage, RequestMessage, SmoldotOptions, StartMsg, TerminateMsg };
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var node_worker_threads = require('node:worker_threads');
|
|
4
|
+
var smoldot$1 = require('smoldot');
|
|
5
|
+
|
|
6
|
+
let smoldot = null;
|
|
7
|
+
let chainId = 0;
|
|
8
|
+
const chains = /* @__PURE__ */ new Map();
|
|
9
|
+
node_worker_threads.parentPort.on("message", async (msg) => {
|
|
10
|
+
if (msg.type === "start") {
|
|
11
|
+
if (smoldot !== null) {
|
|
12
|
+
throw new Error("Can't call start on a client already started");
|
|
13
|
+
}
|
|
14
|
+
smoldot = smoldot$1.start(msg.value);
|
|
15
|
+
return node_worker_threads.parentPort?.postMessage({
|
|
16
|
+
id: msg.id
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
if (smoldot === null) {
|
|
20
|
+
throw new Error("Smoldot not started");
|
|
21
|
+
}
|
|
22
|
+
switch (msg.type) {
|
|
23
|
+
case "add-chain": {
|
|
24
|
+
const potentialRelayChains = msg.value.potentialRelayChainIds?.map(
|
|
25
|
+
(id2) => {
|
|
26
|
+
const chain2 = chains.get(id2);
|
|
27
|
+
if (!chain2) throw new Error("Can't reference removed chain");
|
|
28
|
+
return chain2;
|
|
29
|
+
}
|
|
30
|
+
);
|
|
31
|
+
const chain = await smoldot.addChain({
|
|
32
|
+
...msg.value,
|
|
33
|
+
potentialRelayChains
|
|
34
|
+
});
|
|
35
|
+
const id = chainId++;
|
|
36
|
+
chains.set(id, chain);
|
|
37
|
+
node_worker_threads.parentPort?.postMessage({
|
|
38
|
+
id: msg.id,
|
|
39
|
+
value: id
|
|
40
|
+
});
|
|
41
|
+
break;
|
|
42
|
+
}
|
|
43
|
+
case "terminate":
|
|
44
|
+
await smoldot.terminate();
|
|
45
|
+
node_worker_threads.parentPort?.postMessage({
|
|
46
|
+
id: msg.id
|
|
47
|
+
});
|
|
48
|
+
smoldot = null;
|
|
49
|
+
chains.clear();
|
|
50
|
+
break;
|
|
51
|
+
case "chain":
|
|
52
|
+
handleChainMessage(msg, msg.id);
|
|
53
|
+
break;
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
async function handleChainMessage(msg, id) {
|
|
57
|
+
const chain = chains.get(msg.value.id);
|
|
58
|
+
if (!chain) throw new Error("Can't reference removed chain");
|
|
59
|
+
switch (msg.value.type) {
|
|
60
|
+
case "receive":
|
|
61
|
+
node_worker_threads.parentPort?.postMessage({
|
|
62
|
+
id,
|
|
63
|
+
value: await chain.nextJsonRpcResponse()
|
|
64
|
+
});
|
|
65
|
+
break;
|
|
66
|
+
case "send":
|
|
67
|
+
chain.sendJsonRpc(msg.value.value);
|
|
68
|
+
break;
|
|
69
|
+
case "remove":
|
|
70
|
+
chain.remove();
|
|
71
|
+
chains.delete(msg.value.id);
|
|
72
|
+
break;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
//# sourceMappingURL=node-worker.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"node-worker.js","sources":["../../src/node-worker.ts"],"sourcesContent":["import { parentPort } from \"node:worker_threads\"\nimport { AddChainOptions, Chain, Client, ClientOptions, start } from \"smoldot\"\n\nexport type SmoldotOptions = Omit<ClientOptions, \"portToWorker\">\nexport interface StartMsg {\n type: \"start\"\n value: SmoldotOptions\n}\nexport interface AddChainMsg {\n type: \"add-chain\"\n value: Omit<AddChainOptions, \"potentialRelayChains\"> & {\n potentialRelayChainIds?: number[]\n }\n}\nexport interface TerminateMsg {\n type: \"terminate\"\n}\nexport interface ChainMsg {\n type: \"chain\"\n value: {\n id: number\n } & (\n | {\n type: \"remove\" | \"receive\"\n }\n | {\n type: \"send\"\n value: string\n }\n )\n}\nexport type RequestMessage = StartMsg | AddChainMsg | TerminateMsg | ChainMsg\nexport type CorrelatedRequestMessage = { id: number } & RequestMessage\n\nlet smoldot: Client | null = null\n\nlet chainId = 0\nconst chains = new Map<number, Chain>()\n\nparentPort!.on(\"message\", async (msg: CorrelatedRequestMessage) => {\n if (msg.type === \"start\") {\n if (smoldot !== null) {\n throw new Error(\"Can't call start on a client already started\")\n }\n smoldot = start(msg.value)\n return parentPort?.postMessage({\n id: msg.id,\n })\n }\n\n if (smoldot === null) {\n throw new Error(\"Smoldot not started\")\n }\n\n switch (msg.type) {\n case \"add-chain\": {\n const potentialRelayChains = msg.value.potentialRelayChainIds?.map(\n (id) => {\n const chain = chains.get(id)\n if (!chain) throw new Error(\"Can't reference removed chain\")\n return chain\n },\n )\n const chain = await smoldot.addChain({\n ...msg.value,\n potentialRelayChains,\n })\n const id = chainId++\n chains.set(id, chain)\n parentPort?.postMessage({\n id: msg.id,\n value: id,\n })\n break\n }\n case \"terminate\":\n await smoldot.terminate()\n parentPort?.postMessage({\n id: msg.id,\n })\n smoldot = null\n chains.clear()\n break\n case \"chain\":\n handleChainMessage(msg, msg.id)\n break\n }\n})\n\nasync function handleChainMessage(msg: ChainMsg, id: number) {\n const chain = chains.get(msg.value.id)\n if (!chain) throw new Error(\"Can't reference removed chain\")\n\n switch (msg.value.type) {\n case \"receive\":\n parentPort?.postMessage({\n id,\n value: await chain.nextJsonRpcResponse(),\n })\n break\n case \"send\":\n chain.sendJsonRpc(msg.value.value)\n break\n case \"remove\":\n chain.remove()\n chains.delete(msg.value.id)\n break\n }\n}\n"],"names":["parentPort","start","id","chain"],"mappings":";;;;;AAkCA,IAAI,OAAyB,GAAA,IAAA,CAAA;AAE7B,IAAI,OAAU,GAAA,CAAA,CAAA;AACd,MAAM,MAAA,uBAAa,GAAmB,EAAA,CAAA;AAEtCA,8BAAY,CAAA,EAAA,CAAG,SAAW,EAAA,OAAO,GAAkC,KAAA;AACjE,EAAI,IAAA,GAAA,CAAI,SAAS,OAAS,EAAA;AACxB,IAAA,IAAI,YAAY,IAAM,EAAA;AACpB,MAAM,MAAA,IAAI,MAAM,8CAA8C,CAAA,CAAA;AAAA,KAChE;AACA,IAAU,OAAA,GAAAC,eAAA,CAAM,IAAI,KAAK,CAAA,CAAA;AACzB,IAAA,OAAOD,gCAAY,WAAY,CAAA;AAAA,MAC7B,IAAI,GAAI,CAAA,EAAA;AAAA,KACT,CAAA,CAAA;AAAA,GACH;AAEA,EAAA,IAAI,YAAY,IAAM,EAAA;AACpB,IAAM,MAAA,IAAI,MAAM,qBAAqB,CAAA,CAAA;AAAA,GACvC;AAEA,EAAA,QAAQ,IAAI,IAAM;AAAA,IAChB,KAAK,WAAa,EAAA;AAChB,MAAM,MAAA,oBAAA,GAAuB,GAAI,CAAA,KAAA,CAAM,sBAAwB,EAAA,GAAA;AAAA,QAC7D,CAACE,GAAO,KAAA;AACN,UAAMC,MAAAA,MAAAA,GAAQ,MAAO,CAAA,GAAA,CAAID,GAAE,CAAA,CAAA;AAC3B,UAAA,IAAI,CAACC,MAAAA,EAAa,MAAA,IAAI,MAAM,+BAA+B,CAAA,CAAA;AAC3D,UAAOA,OAAAA,MAAAA,CAAAA;AAAA,SACT;AAAA,O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AAAA,IACF,KAAK,MAAA;AACH,MAAM,KAAA,CAAA,WAAA,CAAY,GAAI,CAAA,KAAA,CAAM,KAAK,CAAA,CAAA;AACjC,MAAA,MAAA;AAAA,IACF,KAAK,QAAA;AACH,MAAA,KAAA,CAAM,MAAO,EAAA,CAAA;AACb,MAAO,MAAA,CAAA,MAAA,CAAO,GAAI,CAAA,KAAA,CAAM,EAAE,CAAA,CAAA;AAC1B,MAAA,MAAA;AAAA,GACJ;AACF;;"}
|
package/from-worker/package.json
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
"types": "../dist/from-worker/from-worker.d.ts",
|
|
4
4
|
"module": "../dist/from-worker/esm/from-worker.mjs",
|
|
5
5
|
"import": "../dist/from-worker/esm/from-worker.mjs",
|
|
6
|
+
"browser": "../dist/node-worker/esm/node-worker.mjs",
|
|
6
7
|
"require": "../dist/from-worker/from-worker.js",
|
|
7
8
|
"default": "../dist/from-worker/from-worker.js"
|
|
8
9
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@polkadot-api/smoldot",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"author": "Josep M Sobrepere (https://github.com/josepot)",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -19,6 +19,17 @@
|
|
|
19
19
|
"require": "./dist/index.js",
|
|
20
20
|
"default": "./dist/index.js"
|
|
21
21
|
},
|
|
22
|
+
"./from-node-worker": {
|
|
23
|
+
"node": {
|
|
24
|
+
"import": "./dist/from-node-worker/esm/from-node-worker.mjs",
|
|
25
|
+
"require": "./dist/from-node-worker/from-node-worker.js",
|
|
26
|
+
"default": "./dist/from-node-worker/from-node-worker.js"
|
|
27
|
+
},
|
|
28
|
+
"module": "./dist/from-node-worker/esm/from-node-worker.mjs",
|
|
29
|
+
"import": "./dist/from-node-worker/esm/from-node-worker.mjs",
|
|
30
|
+
"require": "./dist/from-node-worker/from-node-worker.js",
|
|
31
|
+
"default": "./dist/from-node-worker/from-node-worker.js"
|
|
32
|
+
},
|
|
22
33
|
"./from-worker": {
|
|
23
34
|
"node": {
|
|
24
35
|
"import": "./dist/from-worker/esm/from-worker.mjs",
|
|
@@ -30,6 +41,17 @@
|
|
|
30
41
|
"require": "./dist/from-worker/from-worker.js",
|
|
31
42
|
"default": "./dist/from-worker/from-worker.js"
|
|
32
43
|
},
|
|
44
|
+
"./node-worker": {
|
|
45
|
+
"node": {
|
|
46
|
+
"import": "./dist/node-worker/esm/node-worker.mjs",
|
|
47
|
+
"require": "./dist/node-worker/node-worker.js",
|
|
48
|
+
"default": "./dist/node-worker/node-worker.js"
|
|
49
|
+
},
|
|
50
|
+
"module": "./dist/node-worker/esm/node-worker.mjs",
|
|
51
|
+
"import": "./dist/node-worker/esm/node-worker.mjs",
|
|
52
|
+
"require": "./dist/node-worker/node-worker.js",
|
|
53
|
+
"default": "./dist/node-worker/node-worker.js"
|
|
54
|
+
},
|
|
33
55
|
"./worker": {
|
|
34
56
|
"node": {
|
|
35
57
|
"import": "./dist/worker/esm/worker.mjs",
|
|
@@ -44,8 +66,8 @@
|
|
|
44
66
|
"./package.json": "./package.json"
|
|
45
67
|
},
|
|
46
68
|
"main": "./dist/index.js",
|
|
47
|
-
"module": "./dist/index.mjs",
|
|
48
|
-
"browser": "./dist/index.mjs",
|
|
69
|
+
"module": "./dist/esm/index.mjs",
|
|
70
|
+
"browser": "./dist/esm/index.mjs",
|
|
49
71
|
"types": "./dist/index.d.ts",
|
|
50
72
|
"files": [
|
|
51
73
|
"dist",
|
|
@@ -53,11 +75,9 @@
|
|
|
53
75
|
"worker"
|
|
54
76
|
],
|
|
55
77
|
"dependencies": {
|
|
78
|
+
"@types/node": "^22.2.0",
|
|
56
79
|
"smoldot": "2.0.29"
|
|
57
80
|
},
|
|
58
|
-
"devDependencies": {
|
|
59
|
-
"@types/node": "^20.14.12"
|
|
60
|
-
},
|
|
61
81
|
"scripts": {
|
|
62
82
|
"build-core": "tsc --noEmit && rollup -c",
|
|
63
83
|
"build": "pnpm build-core",
|