@acala-network/chopsticks 0.9.1-2 → 0.9.1-3
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/cjs/cli-options.js +30 -15
- package/dist/cjs/cli.js +69 -80
- package/dist/cjs/context.js +93 -64
- package/dist/cjs/index.js +32 -20
- package/dist/cjs/logger.js +18 -5
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/plugins/decode-key/index.js +23 -20
- package/dist/cjs/plugins/dry-run/cli.js +46 -41
- package/dist/cjs/plugins/dry-run/dry-run-extrinsic.js +37 -21
- package/dist/cjs/plugins/dry-run/dry-run-preimage.js +101 -47
- package/dist/cjs/plugins/dry-run/index.js +18 -17
- package/dist/cjs/plugins/dry-run/rpc.js +48 -74
- package/dist/cjs/plugins/follow-chain/index.js +78 -46
- package/dist/cjs/plugins/index.js +76 -40
- package/dist/cjs/plugins/new-block/index.js +21 -53
- package/dist/cjs/plugins/run-block/index.js +145 -104
- package/dist/cjs/plugins/set-block-build-mode/index.js +17 -25
- package/dist/cjs/plugins/set-head/index.js +13 -23
- package/dist/cjs/plugins/set-runtime-log-level/index.js +16 -23
- package/dist/cjs/plugins/set-storage/index.js +16 -35
- package/dist/cjs/plugins/time-travel/index.js +13 -23
- package/dist/cjs/plugins/try-runtime/index.js +51 -42
- package/dist/cjs/plugins/types.js +43 -18
- package/dist/cjs/rpc/index.js +30 -21
- package/dist/cjs/schema/index.js +77 -48
- package/dist/cjs/server.js +89 -65
- package/dist/cjs/setup-with-server.js +19 -13
- package/dist/cjs/types.js +30 -18
- package/dist/cjs/utils/decoder.js +32 -16
- package/dist/cjs/utils/generate-html-diff.js +38 -19
- package/dist/cjs/utils/index.js +20 -19
- package/dist/cjs/utils/open-html.js +12 -6
- package/dist/cjs/utils/override.js +40 -27
- package/dist/cjs/utils/tunnel.js +11 -14
- package/dist/esm/cli-options.js +11 -11
- package/dist/esm/cli.js +45 -60
- package/dist/esm/context.js +20 -24
- package/dist/esm/plugins/decode-key/index.js +9 -12
- package/dist/esm/plugins/dry-run/cli.js +32 -33
- package/dist/esm/plugins/dry-run/dry-run-extrinsic.js +18 -8
- package/dist/esm/plugins/dry-run/dry-run-preimage.js +72 -24
- package/dist/esm/plugins/dry-run/rpc.js +14 -24
- package/dist/esm/plugins/follow-chain/index.js +52 -28
- package/dist/esm/plugins/index.js +8 -6
- package/dist/esm/plugins/new-block/index.js +8 -9
- package/dist/esm/plugins/run-block/index.js +97 -65
- package/dist/esm/plugins/set-block-build-mode/index.js +4 -3
- package/dist/esm/plugins/set-head/index.js +2 -4
- package/dist/esm/plugins/set-runtime-log-level/index.js +4 -3
- package/dist/esm/plugins/set-storage/index.js +3 -4
- package/dist/esm/plugins/time-travel/index.js +2 -4
- package/dist/esm/plugins/try-runtime/index.js +33 -30
- package/dist/esm/rpc/index.js +18 -15
- package/dist/esm/schema/index.js +31 -18
- package/dist/esm/server.js +70 -52
- package/dist/esm/setup-with-server.js +3 -3
- package/dist/esm/types.js +12 -1
- package/dist/esm/utils/decoder.js +12 -4
- package/dist/esm/utils/generate-html-diff.js +10 -6
- package/dist/esm/utils/open-html.js +1 -1
- package/dist/esm/utils/override.js +11 -13
- package/dist/esm/utils/tunnel.js +1 -8
- package/package.json +13 -17
- package/dist/esm/package.json +0 -1
- /package/{chopsticks.js → chopsticks.cjs} +0 -0
|
@@ -1,58 +1,67 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
Object.defineProperty(exports, "cli", {
|
|
6
|
+
enumerable: true,
|
|
7
|
+
get: function() {
|
|
8
|
+
return cli;
|
|
9
|
+
}
|
|
10
|
+
});
|
|
11
|
+
const _nodefs = require("node:fs");
|
|
12
|
+
const _clioptions = require("../../cli-options");
|
|
13
|
+
const _generatehtmldiff = require("../../utils/generate-html-diff");
|
|
14
|
+
const _openhtml = require("../../utils/open-html");
|
|
15
|
+
const _context = require("../../context");
|
|
16
|
+
const cli = (y)=>{
|
|
17
|
+
y.command('try-runtime', 'Runs runtime upgrade', (yargs)=>yargs.options({
|
|
18
|
+
..._clioptions.defaultOptions,
|
|
19
|
+
'wasm-override': {
|
|
20
|
+
desc: 'Path to WASM built with feature `try-runtime` enabled',
|
|
21
|
+
string: true,
|
|
22
|
+
required: true
|
|
23
|
+
},
|
|
24
|
+
'output-path': {
|
|
25
|
+
desc: 'File path to print output',
|
|
26
|
+
string: true
|
|
27
|
+
},
|
|
28
|
+
html: {
|
|
29
|
+
desc: 'Generate html with storage diff',
|
|
30
|
+
boolean: true
|
|
31
|
+
},
|
|
32
|
+
open: {
|
|
33
|
+
desc: 'Open generated html',
|
|
34
|
+
boolean: true
|
|
35
|
+
}
|
|
36
|
+
}), async (argv)=>{
|
|
37
|
+
const context = await (0, _context.setupContext)(argv);
|
|
31
38
|
const block = context.chain.head;
|
|
32
39
|
const registry = await block.registry;
|
|
33
40
|
registry.register({
|
|
34
41
|
UpgradeCheckSelect: {
|
|
35
42
|
_enum: {
|
|
36
|
-
None: null
|
|
37
|
-
}
|
|
38
|
-
}
|
|
43
|
+
None: null
|
|
44
|
+
}
|
|
45
|
+
}
|
|
39
46
|
});
|
|
40
47
|
const select_none = registry.createType('UpgradeCheckSelect', 'None');
|
|
41
|
-
const result = await block.call('TryRuntime_on_runtime_upgrade', [
|
|
48
|
+
const result = await block.call('TryRuntime_on_runtime_upgrade', [
|
|
49
|
+
select_none.toHex()
|
|
50
|
+
]);
|
|
42
51
|
if (argv.html) {
|
|
43
|
-
const filePath = await (0,
|
|
52
|
+
const filePath = await (0, _generatehtmldiff.generateHtmlDiffPreviewFile)(block, result.storageDiff, block.hash);
|
|
44
53
|
console.log(`Generated preview ${filePath}`);
|
|
45
54
|
if (argv.open) {
|
|
46
|
-
(0,
|
|
55
|
+
(0, _openhtml.openHtml)(filePath);
|
|
47
56
|
}
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
57
|
+
} else if (argv.outputPath) {
|
|
58
|
+
(0, _nodefs.writeFileSync)(argv.outputPath, JSON.stringify(result, null, 2));
|
|
59
|
+
} else {
|
|
60
|
+
console.dir(result, {
|
|
61
|
+
depth: null,
|
|
62
|
+
colors: false
|
|
63
|
+
});
|
|
54
64
|
}
|
|
55
65
|
process.exit(0);
|
|
56
66
|
});
|
|
57
67
|
};
|
|
58
|
-
exports.cli = cli;
|
|
@@ -1,19 +1,44 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
var
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
function _export(target, all) {
|
|
6
|
+
for(var name in all)Object.defineProperty(target, name, {
|
|
7
|
+
enumerable: true,
|
|
8
|
+
get: all[name]
|
|
9
|
+
});
|
|
10
|
+
}
|
|
11
|
+
_export(exports, {
|
|
12
|
+
dryRun: function() {
|
|
13
|
+
return _dryrun.rpc;
|
|
14
|
+
},
|
|
15
|
+
newBlock: function() {
|
|
16
|
+
return _newblock.rpc;
|
|
17
|
+
},
|
|
18
|
+
runBlock: function() {
|
|
19
|
+
return _runblock.rpc;
|
|
20
|
+
},
|
|
21
|
+
setBlockBuildMode: function() {
|
|
22
|
+
return _setblockbuildmode.rpc;
|
|
23
|
+
},
|
|
24
|
+
setHead: function() {
|
|
25
|
+
return _sethead.rpc;
|
|
26
|
+
},
|
|
27
|
+
setRuntimeLogLevel: function() {
|
|
28
|
+
return _setruntimeloglevel.rpc;
|
|
29
|
+
},
|
|
30
|
+
setStorage: function() {
|
|
31
|
+
return _setstorage.rpc;
|
|
32
|
+
},
|
|
33
|
+
timeTravel: function() {
|
|
34
|
+
return _timetravel.rpc;
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
const _newblock = require("./new-block");
|
|
38
|
+
const _dryrun = require("./dry-run");
|
|
39
|
+
const _setblockbuildmode = require("./set-block-build-mode");
|
|
40
|
+
const _sethead = require("./set-head");
|
|
41
|
+
const _setruntimeloglevel = require("./set-runtime-log-level");
|
|
42
|
+
const _setstorage = require("./set-storage");
|
|
43
|
+
const _timetravel = require("./time-travel");
|
|
44
|
+
const _runblock = require("./run-block");
|
package/dist/cjs/rpc/index.js
CHANGED
|
@@ -1,30 +1,39 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
Object.defineProperty(exports, "handler", {
|
|
6
|
+
enumerable: true,
|
|
7
|
+
get: function() {
|
|
8
|
+
return handler;
|
|
9
|
+
}
|
|
10
|
+
});
|
|
11
|
+
const _chopstickscore = require("@acala-network/chopsticks-core");
|
|
12
|
+
const _plugins = require("../plugins");
|
|
6
13
|
const allHandlers = {
|
|
7
|
-
...
|
|
8
|
-
rpc_methods: async ()
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
14
|
+
..._chopstickscore.substrate,
|
|
15
|
+
rpc_methods: async ()=>Promise.resolve({
|
|
16
|
+
version: 1,
|
|
17
|
+
methods: [
|
|
18
|
+
...Object.keys(allHandlers),
|
|
19
|
+
...Object.keys(_plugins.pluginHandlers)
|
|
20
|
+
]
|
|
21
|
+
})
|
|
12
22
|
};
|
|
13
|
-
const getHandler = (method)
|
|
23
|
+
const getHandler = (method)=>{
|
|
14
24
|
const handler = allHandlers[method];
|
|
15
25
|
if (!handler) {
|
|
16
26
|
// no handler for this method, check if it's a plugin
|
|
17
|
-
return
|
|
27
|
+
return _plugins.pluginHandlers[method];
|
|
18
28
|
}
|
|
19
29
|
return handler;
|
|
20
30
|
};
|
|
21
|
-
const handler = (context)
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
};
|
|
30
|
-
exports.handler = handler;
|
|
31
|
+
const handler = (context)=>({ method, params }, subscriptionManager)=>{
|
|
32
|
+
_chopstickscore.logger.trace('Handling %s', method);
|
|
33
|
+
const handler = getHandler(method);
|
|
34
|
+
if (!handler) {
|
|
35
|
+
_chopstickscore.logger.warn('Method not found %s', method);
|
|
36
|
+
throw new _chopstickscore.ResponseError(-32601, `Method not found: ${method}`);
|
|
37
|
+
}
|
|
38
|
+
return handler(context, params, subscriptionManager);
|
|
39
|
+
};
|
package/dist/cjs/schema/index.js
CHANGED
|
@@ -1,60 +1,89 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
};
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
.
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
function _export(target, all) {
|
|
6
|
+
for(var name in all)Object.defineProperty(target, name, {
|
|
7
|
+
enumerable: true,
|
|
8
|
+
get: all[name]
|
|
9
|
+
});
|
|
10
|
+
}
|
|
11
|
+
_export(exports, {
|
|
12
|
+
configSchema: function() {
|
|
13
|
+
return configSchema;
|
|
14
|
+
},
|
|
15
|
+
fetchConfig: function() {
|
|
16
|
+
return fetchConfig;
|
|
17
|
+
}
|
|
18
|
+
});
|
|
19
|
+
const _chopstickscore = require("@acala-network/chopsticks-core");
|
|
20
|
+
const _nodepath = require("node:path");
|
|
21
|
+
const _nodefs = require("node:fs");
|
|
22
|
+
const _zod = require("zod");
|
|
23
|
+
const _lodash = /*#__PURE__*/ _interop_require_default(require("lodash"));
|
|
24
|
+
const _axios = /*#__PURE__*/ _interop_require_default(require("axios"));
|
|
25
|
+
const _jsyaml = /*#__PURE__*/ _interop_require_default(require("js-yaml"));
|
|
26
|
+
function _interop_require_default(obj) {
|
|
27
|
+
return obj && obj.__esModule ? obj : {
|
|
28
|
+
default: obj
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
const configSchema = _zod.z.object({
|
|
32
|
+
port: _zod.z.number().optional(),
|
|
33
|
+
endpoint: _zod.z.string().optional(),
|
|
34
|
+
block: _zod.z.union([
|
|
35
|
+
_zod.z.string().length(66).startsWith('0x'),
|
|
36
|
+
_zod.z.number(),
|
|
37
|
+
_zod.z.null()
|
|
38
|
+
]).optional(),
|
|
39
|
+
'build-block-mode': _zod.z.nativeEnum(_chopstickscore.BuildBlockMode).optional(),
|
|
40
|
+
'import-storage': _zod.z.any().optional(),
|
|
41
|
+
'mock-signature-host': _zod.z.boolean().optional(),
|
|
42
|
+
'max-memory-block-count': _zod.z.number().optional(),
|
|
43
|
+
db: _zod.z.string().optional(),
|
|
44
|
+
'wasm-override': _zod.z.string().optional(),
|
|
45
|
+
genesis: _zod.z.union([
|
|
46
|
+
_zod.z.string(),
|
|
47
|
+
_chopstickscore.genesisSchema
|
|
48
|
+
]).optional(),
|
|
49
|
+
timestamp: _zod.z.number().optional(),
|
|
50
|
+
'registered-types': _zod.z.any().optional(),
|
|
51
|
+
'runtime-log-level': _zod.z.number().min(0).max(5).optional(),
|
|
52
|
+
'offchain-worker': _zod.z.boolean().optional(),
|
|
53
|
+
resume: _zod.z.union([
|
|
54
|
+
_zod.z.string().length(66).startsWith('0x'),
|
|
55
|
+
_zod.z.number(),
|
|
56
|
+
_zod.z.boolean()
|
|
57
|
+
]).optional()
|
|
58
|
+
}).strict();
|
|
33
59
|
const CONFIGS_BASE_URL = 'https://raw.githubusercontent.com/AcalaNetwork/chopsticks/master/configs/';
|
|
34
|
-
const fetchConfig = async (path)
|
|
60
|
+
const fetchConfig = async (path)=>{
|
|
35
61
|
let file;
|
|
36
|
-
if ((0,
|
|
37
|
-
file = await
|
|
38
|
-
}
|
|
39
|
-
else {
|
|
62
|
+
if ((0, _chopstickscore.isUrl)(path)) {
|
|
63
|
+
file = await _axios.default.get(path).then((x)=>x.data);
|
|
64
|
+
} else {
|
|
40
65
|
try {
|
|
41
|
-
file = (0,
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
66
|
+
file = (0, _nodefs.readFileSync)(path, 'utf8');
|
|
67
|
+
} catch (err) {
|
|
68
|
+
if ((0, _nodepath.basename)(path) === path && [
|
|
69
|
+
'',
|
|
70
|
+
'.yml',
|
|
71
|
+
'.yaml',
|
|
72
|
+
'.json'
|
|
73
|
+
].includes((0, _nodepath.extname)(path))) {
|
|
74
|
+
if ((0, _nodepath.extname)(path) === '') {
|
|
46
75
|
path += '.yml';
|
|
47
76
|
}
|
|
48
77
|
const url = CONFIGS_BASE_URL + path;
|
|
49
|
-
|
|
50
|
-
file = await
|
|
51
|
-
}
|
|
52
|
-
else {
|
|
78
|
+
_chopstickscore.defaultLogger.info(`Loading config file ${url}`);
|
|
79
|
+
file = await _axios.default.get(url).then((x)=>x.data);
|
|
80
|
+
} else {
|
|
53
81
|
throw err;
|
|
54
82
|
}
|
|
55
83
|
}
|
|
56
84
|
}
|
|
57
|
-
const config =
|
|
58
|
-
|
|
85
|
+
const config = _jsyaml.default.load(_lodash.default.template(file, {
|
|
86
|
+
variable: 'env'
|
|
87
|
+
})(process.env));
|
|
88
|
+
return configSchema.parse(config);
|
|
59
89
|
};
|
|
60
|
-
exports.fetchConfig = fetchConfig;
|
package/dist/cjs/server.js
CHANGED
|
@@ -1,43 +1,60 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", {
|
|
3
|
-
|
|
4
|
-
const ws_1 = require("ws");
|
|
5
|
-
const chopsticks_core_1 = require("@acala-network/chopsticks-core");
|
|
6
|
-
const zod_1 = require("zod");
|
|
7
|
-
const logger_1 = require("./logger");
|
|
8
|
-
const logger = logger_1.defaultLogger.child({ name: 'ws' });
|
|
9
|
-
const singleRequest = zod_1.z.object({
|
|
10
|
-
id: zod_1.z.number(),
|
|
11
|
-
jsonrpc: zod_1.z.literal('2.0'),
|
|
12
|
-
method: zod_1.z.string(),
|
|
13
|
-
params: zod_1.z.array(zod_1.z.any()).default([]),
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
14
4
|
});
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
5
|
+
Object.defineProperty(exports, "createServer", {
|
|
6
|
+
enumerable: true,
|
|
7
|
+
get: function() {
|
|
8
|
+
return createServer;
|
|
9
|
+
}
|
|
10
|
+
});
|
|
11
|
+
const _ws = require("ws");
|
|
12
|
+
const _chopstickscore = require("@acala-network/chopsticks-core");
|
|
13
|
+
const _zod = require("zod");
|
|
14
|
+
const _logger = require("./logger");
|
|
15
|
+
const logger = _logger.defaultLogger.child({
|
|
16
|
+
name: 'ws'
|
|
17
|
+
});
|
|
18
|
+
const singleRequest = _zod.z.object({
|
|
19
|
+
id: _zod.z.number(),
|
|
20
|
+
jsonrpc: _zod.z.literal('2.0'),
|
|
21
|
+
method: _zod.z.string(),
|
|
22
|
+
params: _zod.z.array(_zod.z.any()).default([])
|
|
23
|
+
});
|
|
24
|
+
const batchRequest = _zod.z.array(singleRequest);
|
|
25
|
+
const requestSchema = _zod.z.union([
|
|
26
|
+
singleRequest,
|
|
27
|
+
batchRequest
|
|
28
|
+
]);
|
|
29
|
+
const parseRequest = (request)=>{
|
|
18
30
|
try {
|
|
19
31
|
return JSON.parse(request);
|
|
20
|
-
}
|
|
21
|
-
catch (e) {
|
|
32
|
+
} catch (e) {
|
|
22
33
|
return undefined;
|
|
23
34
|
}
|
|
24
35
|
};
|
|
25
|
-
const createWS = async (port)
|
|
26
|
-
const wss = new
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
36
|
+
const createWS = async (port)=>{
|
|
37
|
+
const wss = new _ws.WebSocketServer({
|
|
38
|
+
port,
|
|
39
|
+
maxPayload: 1024 * 1024 * 100
|
|
40
|
+
});
|
|
41
|
+
const promise = new Promise((resolve)=>{
|
|
42
|
+
wss.on('listening', ()=>{
|
|
43
|
+
resolve([
|
|
44
|
+
wss,
|
|
45
|
+
wss.address().port
|
|
46
|
+
]);
|
|
30
47
|
});
|
|
31
|
-
wss.on('error', (_)
|
|
48
|
+
wss.on('error', (_)=>{
|
|
32
49
|
resolve([]);
|
|
33
50
|
});
|
|
34
51
|
});
|
|
35
52
|
return promise;
|
|
36
53
|
};
|
|
37
|
-
const createServer = async (handler, port)
|
|
54
|
+
const createServer = async (handler, port)=>{
|
|
38
55
|
let wss;
|
|
39
56
|
let listenPort;
|
|
40
|
-
for
|
|
57
|
+
for(let i = 0; i < 10; i++){
|
|
41
58
|
const preferPort = (port ?? 0) > 0 ? (port ?? 0) + i : 0;
|
|
42
59
|
logger.debug('Try starting on port %d', preferPort);
|
|
43
60
|
const [maybeWss, maybeListenPort] = await createWS(preferPort);
|
|
@@ -50,80 +67,86 @@ const createServer = async (handler, port) => {
|
|
|
50
67
|
if (!wss || !listenPort) {
|
|
51
68
|
throw new Error(`Failed to create WebsocketServer at port ${port}`);
|
|
52
69
|
}
|
|
53
|
-
wss.on('connection', (ws)
|
|
70
|
+
wss.on('connection', (ws)=>{
|
|
54
71
|
logger.debug('New connection');
|
|
55
|
-
const send = (data)
|
|
56
|
-
if (ws.readyState ===
|
|
72
|
+
const send = (data)=>{
|
|
73
|
+
if (ws.readyState === _ws.WebSocket.OPEN) {
|
|
57
74
|
ws.send(JSON.stringify(data));
|
|
58
75
|
}
|
|
59
76
|
};
|
|
60
77
|
const subscriptions = {};
|
|
61
78
|
const subscriptionManager = {
|
|
62
|
-
subscribe: (method, subid, onCancel = ()
|
|
79
|
+
subscribe: (method, subid, onCancel = ()=>{})=>{
|
|
63
80
|
subscriptions[subid] = onCancel;
|
|
64
|
-
return (data)
|
|
81
|
+
return (data)=>{
|
|
65
82
|
if (subscriptions[subid]) {
|
|
66
|
-
logger.trace({
|
|
83
|
+
logger.trace({
|
|
84
|
+
method,
|
|
85
|
+
subid,
|
|
86
|
+
data: (0, _logger.truncate)(data)
|
|
87
|
+
}, 'Subscription notification');
|
|
67
88
|
send({
|
|
68
89
|
jsonrpc: '2.0',
|
|
69
90
|
method,
|
|
70
91
|
params: {
|
|
71
92
|
result: data,
|
|
72
|
-
subscription: subid
|
|
73
|
-
}
|
|
93
|
+
subscription: subid
|
|
94
|
+
}
|
|
74
95
|
});
|
|
75
96
|
}
|
|
76
97
|
};
|
|
77
98
|
},
|
|
78
|
-
unsubscribe: (subid)
|
|
99
|
+
unsubscribe: (subid)=>{
|
|
79
100
|
if (subscriptions[subid]) {
|
|
80
101
|
subscriptions[subid](subid);
|
|
81
102
|
delete subscriptions[subid];
|
|
82
103
|
}
|
|
83
|
-
}
|
|
104
|
+
}
|
|
84
105
|
};
|
|
85
|
-
const processRequest = async (req)
|
|
106
|
+
const processRequest = async (req)=>{
|
|
86
107
|
logger.trace({
|
|
87
108
|
id: req.id,
|
|
88
|
-
method: req.method
|
|
109
|
+
method: req.method
|
|
89
110
|
}, 'Received message');
|
|
90
111
|
try {
|
|
91
112
|
const resp = await handler(req, subscriptionManager);
|
|
92
113
|
logger.trace({
|
|
93
114
|
id: req.id,
|
|
94
115
|
method: req.method,
|
|
95
|
-
result: (0,
|
|
116
|
+
result: (0, _logger.truncate)(resp)
|
|
96
117
|
}, 'Response for request');
|
|
97
118
|
return {
|
|
98
119
|
id: req.id,
|
|
99
120
|
jsonrpc: '2.0',
|
|
100
|
-
result: resp ?? null
|
|
121
|
+
result: resp ?? null
|
|
101
122
|
};
|
|
102
|
-
}
|
|
103
|
-
catch (e) {
|
|
123
|
+
} catch (e) {
|
|
104
124
|
logger.info('Error handling request: %s %o', e, e.stack);
|
|
105
125
|
return {
|
|
106
126
|
id: req.id,
|
|
107
127
|
jsonrpc: '2.0',
|
|
108
|
-
error: e instanceof
|
|
128
|
+
error: e instanceof _chopstickscore.ResponseError ? e : {
|
|
129
|
+
code: -32603,
|
|
130
|
+
message: `Internal ${e}`
|
|
131
|
+
}
|
|
109
132
|
};
|
|
110
133
|
}
|
|
111
134
|
};
|
|
112
|
-
ws.on('close', ()
|
|
135
|
+
ws.on('close', ()=>{
|
|
113
136
|
logger.debug('Connection closed');
|
|
114
|
-
for (const [subid, onCancel] of Object.entries(subscriptions))
|
|
137
|
+
for (const [subid, onCancel] of Object.entries(subscriptions)){
|
|
115
138
|
onCancel(subid);
|
|
116
139
|
}
|
|
117
140
|
ws.removeAllListeners();
|
|
118
141
|
});
|
|
119
|
-
ws.on('error', ()
|
|
142
|
+
ws.on('error', ()=>{
|
|
120
143
|
logger.debug('Connection error');
|
|
121
|
-
for (const [subid, onCancel] of Object.entries(subscriptions))
|
|
144
|
+
for (const [subid, onCancel] of Object.entries(subscriptions)){
|
|
122
145
|
onCancel(subid);
|
|
123
146
|
}
|
|
124
147
|
ws.removeAllListeners();
|
|
125
148
|
});
|
|
126
|
-
ws.on('message', async (message)
|
|
149
|
+
ws.on('message', async (message)=>{
|
|
127
150
|
const parsed = await requestSchema.safeParseAsync(parseRequest(message.toString()));
|
|
128
151
|
if (!parsed.success) {
|
|
129
152
|
logger.info('Invalid request: %s', message);
|
|
@@ -132,19 +155,22 @@ const createServer = async (handler, port) => {
|
|
|
132
155
|
jsonrpc: '2.0',
|
|
133
156
|
error: {
|
|
134
157
|
code: -32600,
|
|
135
|
-
message: 'Invalid JSON Request'
|
|
136
|
-
}
|
|
158
|
+
message: 'Invalid JSON Request'
|
|
159
|
+
}
|
|
137
160
|
});
|
|
138
161
|
return;
|
|
139
162
|
}
|
|
140
163
|
const { data: req } = parsed;
|
|
141
164
|
if (Array.isArray(req)) {
|
|
142
|
-
logger.trace({
|
|
165
|
+
logger.trace({
|
|
166
|
+
req
|
|
167
|
+
}, 'Received batch request');
|
|
143
168
|
const resp = await Promise.all(req.map(processRequest));
|
|
144
169
|
send(resp);
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
|
|
170
|
+
} else {
|
|
171
|
+
logger.trace({
|
|
172
|
+
req
|
|
173
|
+
}, 'Received single request');
|
|
148
174
|
const resp = await processRequest(req);
|
|
149
175
|
send(resp);
|
|
150
176
|
}
|
|
@@ -152,17 +178,15 @@ const createServer = async (handler, port) => {
|
|
|
152
178
|
});
|
|
153
179
|
return {
|
|
154
180
|
port: listenPort,
|
|
155
|
-
close: ()
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
}
|
|
164
|
-
})
|
|
165
|
-
}),
|
|
181
|
+
close: ()=>new Promise((resolve, reject)=>{
|
|
182
|
+
wss?.clients.forEach((socket)=>socket.close());
|
|
183
|
+
wss?.close((err)=>{
|
|
184
|
+
if (err) {
|
|
185
|
+
reject(err);
|
|
186
|
+
} else {
|
|
187
|
+
resolve();
|
|
188
|
+
}
|
|
189
|
+
});
|
|
190
|
+
})
|
|
166
191
|
};
|
|
167
192
|
};
|
|
168
|
-
exports.createServer = createServer;
|