@acala-network/chopsticks 0.13.3 → 0.14.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.
@@ -25,7 +25,11 @@ const wsLogger = _logger.defaultLogger.child({
25
25
  name: 'ws'
26
26
  });
27
27
  const singleRequest = _zod.z.object({
28
- id: _zod.z.number(),
28
+ id: _zod.z.optional(_zod.z.union([
29
+ _zod.z.number().int(),
30
+ _zod.z.string(),
31
+ _zod.z.null()
32
+ ])),
29
33
  jsonrpc: _zod.z.literal('2.0'),
30
34
  method: _zod.z.string(),
31
35
  params: _zod.z.array(_zod.z.any()).default([])
@@ -93,6 +97,24 @@ const createServer = async (handler, port)=>{
93
97
  throw new Error('Subscription is not supported');
94
98
  }
95
99
  };
100
+ const safeHandleRequest = async (request)=>{
101
+ try {
102
+ const result = await handler(request, emptySubscriptionManager);
103
+ return request.id === undefined ? undefined : {
104
+ id: request.id,
105
+ jsonrpc: '2.0',
106
+ result
107
+ };
108
+ } catch (err) {
109
+ return {
110
+ jsonrpc: '2.0',
111
+ id: request.id,
112
+ error: {
113
+ message: err.message
114
+ }
115
+ };
116
+ }
117
+ };
96
118
  const server = _nodehttp.default.createServer(async (req, res)=>{
97
119
  if (req.method === 'OPTIONS') {
98
120
  return respond(res);
@@ -112,27 +134,18 @@ const createServer = async (handler, port)=>{
112
134
  }, 'Received request');
113
135
  let response;
114
136
  if (Array.isArray(parsed.data)) {
115
- response = await Promise.all(parsed.data.map((req)=>{
116
- const result = handler(req, emptySubscriptionManager);
117
- return {
118
- id: req.id,
119
- jsonrpc: '2.0',
120
- result
121
- };
122
- }));
137
+ response = await Promise.all(parsed.data.map(safeHandleRequest));
138
+ response = response.filter((r)=>r !== undefined);
123
139
  } else {
124
- const result = await handler(parsed.data, emptySubscriptionManager);
125
- response = {
126
- id: parsed.data.id,
127
- jsonrpc: '2.0',
128
- result
129
- };
140
+ response = await safeHandleRequest(parsed.data);
141
+ }
142
+ if (response !== undefined) {
143
+ respond(res, JSON.stringify(response));
130
144
  }
131
- respond(res, JSON.stringify(response));
132
145
  } catch (err) {
133
146
  respond(res, JSON.stringify({
134
147
  jsonrpc: '2.0',
135
- id: 1,
148
+ id: null,
136
149
  error: {
137
150
  message: err.message
138
151
  }
@@ -10,7 +10,11 @@ const wsLogger = defaultLogger.child({
10
10
  name: 'ws'
11
11
  });
12
12
  const singleRequest = z.object({
13
- id: z.number(),
13
+ id: z.optional(z.union([
14
+ z.number().int(),
15
+ z.string(),
16
+ z.null()
17
+ ])),
14
18
  jsonrpc: z.literal('2.0'),
15
19
  method: z.string(),
16
20
  params: z.array(z.any()).default([])
@@ -78,6 +82,24 @@ export const createServer = async (handler, port)=>{
78
82
  throw new Error('Subscription is not supported');
79
83
  }
80
84
  };
85
+ const safeHandleRequest = async (request)=>{
86
+ try {
87
+ const result = await handler(request, emptySubscriptionManager);
88
+ return request.id === undefined ? undefined : {
89
+ id: request.id,
90
+ jsonrpc: '2.0',
91
+ result
92
+ };
93
+ } catch (err) {
94
+ return {
95
+ jsonrpc: '2.0',
96
+ id: request.id,
97
+ error: {
98
+ message: err.message
99
+ }
100
+ };
101
+ }
102
+ };
81
103
  const server = http.createServer(async (req, res)=>{
82
104
  if (req.method === 'OPTIONS') {
83
105
  return respond(res);
@@ -97,27 +119,18 @@ export const createServer = async (handler, port)=>{
97
119
  }, 'Received request');
98
120
  let response;
99
121
  if (Array.isArray(parsed.data)) {
100
- response = await Promise.all(parsed.data.map((req)=>{
101
- const result = handler(req, emptySubscriptionManager);
102
- return {
103
- id: req.id,
104
- jsonrpc: '2.0',
105
- result
106
- };
107
- }));
122
+ response = await Promise.all(parsed.data.map(safeHandleRequest));
123
+ response = response.filter((r)=>r !== undefined);
108
124
  } else {
109
- const result = await handler(parsed.data, emptySubscriptionManager);
110
- response = {
111
- id: parsed.data.id,
112
- jsonrpc: '2.0',
113
- result
114
- };
125
+ response = await safeHandleRequest(parsed.data);
126
+ }
127
+ if (response !== undefined) {
128
+ respond(res, JSON.stringify(response));
115
129
  }
116
- respond(res, JSON.stringify(response));
117
130
  } catch (err) {
118
131
  respond(res, JSON.stringify({
119
132
  jsonrpc: '2.0',
120
- id: 1,
133
+ id: null,
121
134
  error: {
122
135
  message: err.message
123
136
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@acala-network/chopsticks",
3
- "version": "0.13.3",
3
+ "version": "0.14.0",
4
4
  "author": "Acala Developers <hello@acala.network>",
5
5
  "license": "Apache-2.0",
6
6
  "bin": "./chopsticks.cjs",
@@ -12,8 +12,8 @@
12
12
  "depcheck": "npx depcheck --ignore-patterns='*.test.ts'"
13
13
  },
14
14
  "dependencies": {
15
- "@acala-network/chopsticks-core": "0.13.3",
16
- "@acala-network/chopsticks-db": "0.13.3",
15
+ "@acala-network/chopsticks-core": "0.14.0",
16
+ "@acala-network/chopsticks-db": "0.14.0",
17
17
  "@pnpm/npm-conf": "^2.2.2",
18
18
  "@polkadot/api": "^12.3.1",
19
19
  "@polkadot/api-augment": "^12.3.1",