@lowerdeck/rpc-server 1.0.1 → 1.0.2
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/CHANGELOG.md +6 -0
- package/package.json +1 -1
- package/src/rpcMux.ts +48 -26
package/CHANGELOG.md
CHANGED
package/package.json
CHANGED
package/src/rpcMux.ts
CHANGED
|
@@ -207,19 +207,6 @@ export let rpcMux = (
|
|
|
207
207
|
contentType: 'application/json'
|
|
208
208
|
});
|
|
209
209
|
|
|
210
|
-
let valRes = validation.validate(body);
|
|
211
|
-
if (!valRes.success) {
|
|
212
|
-
return new Response(
|
|
213
|
-
JSON.stringify(
|
|
214
|
-
validationError({
|
|
215
|
-
errors: valRes.errors,
|
|
216
|
-
entity: 'request_data'
|
|
217
|
-
}).toResponse()
|
|
218
|
-
),
|
|
219
|
-
{ status: 406, headers }
|
|
220
|
-
);
|
|
221
|
-
}
|
|
222
|
-
|
|
223
210
|
return provideExecutionContext(
|
|
224
211
|
createExecutionContext({
|
|
225
212
|
type: 'request',
|
|
@@ -233,8 +220,20 @@ export let rpcMux = (
|
|
|
233
220
|
{ id: string; name: string; payload: any }[]
|
|
234
221
|
>();
|
|
235
222
|
|
|
236
|
-
|
|
237
|
-
|
|
223
|
+
let resRef = {
|
|
224
|
+
body: {
|
|
225
|
+
__typename: 'rpc.response',
|
|
226
|
+
calls: [] as any[]
|
|
227
|
+
},
|
|
228
|
+
status: 200
|
|
229
|
+
};
|
|
230
|
+
|
|
231
|
+
let pathParts = url.pathname.split('/').filter(Boolean);
|
|
232
|
+
let lastPart = pathParts[pathParts.length - 1];
|
|
233
|
+
|
|
234
|
+
if (lastPart[0] == '$') {
|
|
235
|
+
let id = lastPart.slice(1);
|
|
236
|
+
let rpcIndex = handlerNameToRpcMap.get(id);
|
|
238
237
|
if (rpcIndex == undefined) {
|
|
239
238
|
return new Response(
|
|
240
239
|
JSON.stringify(
|
|
@@ -245,19 +244,42 @@ export let rpcMux = (
|
|
|
245
244
|
}
|
|
246
245
|
|
|
247
246
|
let calls = callsByRpc.get(rpcIndex) ?? [];
|
|
248
|
-
calls.push(
|
|
247
|
+
calls.push({
|
|
248
|
+
id: generateCustomId('call_'),
|
|
249
|
+
name: id,
|
|
250
|
+
payload: body
|
|
251
|
+
});
|
|
249
252
|
callsByRpc.set(rpcIndex, calls);
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
|
|
253
|
+
} else {
|
|
254
|
+
let valRes = validation.validate(body);
|
|
255
|
+
if (!valRes.success) {
|
|
256
|
+
return new Response(
|
|
257
|
+
JSON.stringify(
|
|
258
|
+
validationError({
|
|
259
|
+
errors: valRes.errors,
|
|
260
|
+
entity: 'request_data'
|
|
261
|
+
}).toResponse()
|
|
262
|
+
),
|
|
263
|
+
{ status: 406, headers }
|
|
264
|
+
);
|
|
265
|
+
}
|
|
253
266
|
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
267
|
+
for (let call of valRes.value.calls) {
|
|
268
|
+
let rpcIndex = handlerNameToRpcMap.get(call.name);
|
|
269
|
+
if (rpcIndex == undefined) {
|
|
270
|
+
return new Response(
|
|
271
|
+
JSON.stringify(
|
|
272
|
+
notFoundError({ entity: 'handler' }).toResponse()
|
|
273
|
+
),
|
|
274
|
+
{ status: 404, headers }
|
|
275
|
+
);
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
let calls = callsByRpc.get(rpcIndex) ?? [];
|
|
279
|
+
calls.push(call as any);
|
|
280
|
+
callsByRpc.set(rpcIndex, calls);
|
|
281
|
+
}
|
|
282
|
+
}
|
|
261
283
|
|
|
262
284
|
await Promise.all(
|
|
263
285
|
Array.from(callsByRpc.entries()).map(async ([rpcIndex, calls]) => {
|