@acala-network/chopsticks 0.8.4 → 0.8.5-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.
Files changed (2) hide show
  1. package/lib/server.js +38 -24
  2. package/package.json +2 -2
package/lib/server.js CHANGED
@@ -29,12 +29,14 @@ const zod_1 = require("zod");
29
29
  const ws_1 = __importStar(require("ws"));
30
30
  const logger_1 = require("./logger");
31
31
  const logger = logger_1.defaultLogger.child({ name: 'ws' });
32
- const requestSchema = zod_1.z.object({
32
+ const singleRequest = zod_1.z.object({
33
33
  id: zod_1.z.number(),
34
34
  jsonrpc: zod_1.z.literal('2.0'),
35
35
  method: zod_1.z.string(),
36
36
  params: zod_1.z.array(zod_1.z.any()).default([]),
37
37
  });
38
+ const batchRequest = zod_1.z.array(singleRequest);
39
+ const requestSchema = zod_1.z.union([singleRequest, batchRequest]);
38
40
  const parseRequest = (request) => {
39
41
  try {
40
42
  return JSON.parse(request);
@@ -103,6 +105,33 @@ const createServer = async (handler, port) => {
103
105
  }
104
106
  },
105
107
  };
108
+ const processRequest = async (req) => {
109
+ logger.trace({
110
+ id: req.id,
111
+ method: req.method,
112
+ }, 'Received message');
113
+ try {
114
+ const resp = await handler(req, subscriptionManager);
115
+ logger.trace({
116
+ id: req.id,
117
+ method: req.method,
118
+ result: (0, logger_1.truncate)(resp),
119
+ }, 'Response for request');
120
+ return {
121
+ id: req.id,
122
+ jsonrpc: '2.0',
123
+ result: resp ?? null,
124
+ };
125
+ }
126
+ catch (e) {
127
+ logger.info('Error handling request: %s %o', e, e.stack);
128
+ return {
129
+ id: req.id,
130
+ jsonrpc: '2.0',
131
+ error: e instanceof chopsticks_core_1.ResponseError ? e : { code: -32603, message: `Internal ${e}` },
132
+ };
133
+ }
134
+ };
106
135
  ws.on('close', () => {
107
136
  logger.debug('Connection closed');
108
137
  for (const [subid, onCancel] of Object.entries(subscriptions)) {
@@ -132,30 +161,15 @@ const createServer = async (handler, port) => {
132
161
  return;
133
162
  }
134
163
  const { data: req } = parsed;
135
- logger.trace({
136
- id: req.id,
137
- method: req.method,
138
- }, 'Received message');
139
- try {
140
- const resp = await handler(req, subscriptionManager);
141
- logger.trace({
142
- id: req.id,
143
- method: req.method,
144
- result: (0, logger_1.truncate)(resp),
145
- }, 'Sending response for request');
146
- send({
147
- id: req.id,
148
- jsonrpc: '2.0',
149
- result: resp ?? null,
150
- });
164
+ if (Array.isArray(req)) {
165
+ logger.trace({ req }, 'Received batch request');
166
+ const resp = await Promise.all(req.map(processRequest));
167
+ send(resp);
151
168
  }
152
- catch (e) {
153
- logger.info('Error handling request: %s %o', e, e.stack);
154
- send({
155
- id: req.id,
156
- jsonrpc: '2.0',
157
- error: e instanceof chopsticks_core_1.ResponseError ? e : { code: -32603, message: `Internal ${e}` },
158
- });
169
+ else {
170
+ logger.trace({ req }, 'Received single request');
171
+ const resp = await processRequest(req);
172
+ send(resp);
159
173
  }
160
174
  });
161
175
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@acala-network/chopsticks",
3
- "version": "0.8.4",
3
+ "version": "0.8.5-0",
4
4
  "author": "Acala Developers <hello@acala.network>",
5
5
  "license": "Apache-2.0",
6
6
  "bin": "./chopsticks.js",
@@ -17,7 +17,7 @@
17
17
  "docs:prep": "typedoc"
18
18
  },
19
19
  "dependencies": {
20
- "@acala-network/chopsticks-core": "0.8.4",
20
+ "@acala-network/chopsticks-core": "0.8.5-0",
21
21
  "@pnpm/npm-conf": "^2.2.2",
22
22
  "@polkadot/api": "^10.9.1",
23
23
  "axios": "^1.5.1",