allserver 2.3.0 → 2.4.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/README.md CHANGED
@@ -653,13 +653,11 @@ import { Lambda } from "@aws-sdk/client-lambda";
653
653
  const invocationResponse = await new Lambda().invoke({
654
654
  FunctionName: "my-lambda-name",
655
655
  Payload: JSON.stringify({
656
+ _: { procedureName: "updateUser" },
656
657
  id: "123412341234123412341234",
657
658
  firstName: "Fred",
658
659
  lastName: "Flinstone",
659
660
  }),
660
- ClientContext: JSON.stringify({
661
- procedureName: "updateUser",
662
- }),
663
661
  });
664
662
  const { success, code, message, user } = JSON.parse(invocationResponse.Payload);
665
663
  ```
@@ -667,7 +665,7 @@ const { success, code, message, user } = JSON.parse(invocationResponse.Payload);
667
665
  Alternatively, you can call the same procedure using the `aws` CLI:
668
666
 
669
667
  ```shell
670
- aws lambda invoke --function-name my-lambda-name --client-context '{"procedureName":"updateUser"}' --payload '{"id":"123412341234123412341234","firstName":"Fred","lastName":"Flinstone"}'
668
+ aws lambda invoke --function-name my-lambda-name --payload '{"_":{"procedureName":"updateUser"},"id":"123412341234123412341234","firstName":"Fred","lastName":"Flinstone"}}'
671
669
  ```
672
670
 
673
671
  ## `AllserverClient` options
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "allserver",
3
- "version": "2.3.0",
3
+ "version": "2.4.0",
4
4
  "description": "Multi-protocol simple RPC server and [optional] client. Boilerplate-less. Opinionated. Minimalistic. DX-first.",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
@@ -213,40 +213,55 @@ module.exports = require("stampit")({
213
213
  const defaultCtx = { client: this, isIntrospection: true };
214
214
  const ctx = transport.createCallContext(defaultCtx);
215
215
 
216
- await this._callMiddlewares(ctx, "before");
217
-
218
- if (!ctx.result) {
219
- try {
220
- // This is supposed to be executed only once (per uri) unless it throws.
221
- // There are only 3 situations when this throws:
222
- // * the "introspect" method not found on server,
223
- // * the network request is malformed,
224
- // * couldn't connect to the remote host.
225
- ctx.result = await transport.introspect(ctx);
226
- } catch (err) {
227
- ctx.result = {
228
- success: false,
229
- code: "ALLSERVER_CLIENT_INTROSPECTION_FAILED",
230
- message: `Couldn't introspect ${transport.uri} due to: ${err.message}`,
231
- noNetToServer: Boolean(err.noNetToServer),
232
- error: err,
233
- };
216
+ await this._callMiddlewares(ctx, "before", async () => {
217
+ if (!ctx.result) {
218
+ try {
219
+ // This is supposed to be executed only once (per uri) unless it throws.
220
+ // There are only 3 situations when this throws:
221
+ // * the "introspect" method not found on server,
222
+ // * the network request is malformed,
223
+ // * couldn't connect to the remote host.
224
+ ctx.result = await transport.introspect(ctx);
225
+ } catch (err) {
226
+ ctx.result = {
227
+ success: false,
228
+ code: "ALLSERVER_CLIENT_INTROSPECTION_FAILED",
229
+ message: `Couldn't introspect ${transport.uri} due to: ${err.message}`,
230
+ noNetToServer: Boolean(err.noNetToServer),
231
+ error: err,
232
+ };
233
+ }
234
234
  }
235
- }
236
235
 
237
- await this._callMiddlewares(ctx, "after");
236
+ await this._callMiddlewares(ctx, "after");
237
+ });
238
238
 
239
239
  return ctx.result;
240
240
  },
241
241
 
242
- async _callMiddlewares(ctx, middlewareType) {
243
- const middlewares = [].concat(this[p][middlewareType]).filter(isFunction);
244
- for (const middleware of middlewares) {
245
- try {
246
- const result = await middleware.call(this, ctx);
242
+ async _callMiddlewares(ctx, middlewareType, next) {
243
+ const runMiddlewares = async (middlewares) => {
244
+ if (!middlewares?.length) {
245
+ // no middlewares to run
246
+ if (next) return await next();
247
+ return;
248
+ }
249
+ const middleware = middlewares[0];
250
+ async function handleMiddlewareResult(result) {
247
251
  if (result !== undefined) {
248
252
  ctx.result = result;
249
- break;
253
+ // Do not call any more middlewares
254
+ } else {
255
+ await runMiddlewares(middlewares.slice(1));
256
+ }
257
+ }
258
+ try {
259
+ if (middleware.length > 1) {
260
+ // This middleware accepts more than one argument
261
+ await middleware.call(this, ctx, handleMiddlewareResult);
262
+ } else {
263
+ const result = await middleware.call(this, ctx);
264
+ await handleMiddlewareResult(result);
250
265
  }
251
266
  } catch (err) {
252
267
  if (!this[p].neverThrow) throw err;
@@ -257,9 +272,13 @@ module.exports = require("stampit")({
257
272
  message = `The '${middlewareType}' middleware error while calling '${ctx.procedureName}' procedure: ${err.message}`;
258
273
  }
259
274
  ctx.result = { success: false, code, message, error: err };
260
- return;
275
+ // Do not call any more middlewares
276
+ if (next) return await next();
261
277
  }
262
- }
278
+ };
279
+
280
+ const middlewares = [].concat(this[p][middlewareType]).filter(isFunction);
281
+ return await runMiddlewares(middlewares);
263
282
  },
264
283
 
265
284
  async call(procedureName, arg) {
@@ -271,24 +290,24 @@ module.exports = require("stampit")({
271
290
  const defaultCtx = { procedureName, arg, client: this };
272
291
  const ctx = transport.createCallContext(defaultCtx);
273
292
 
274
- await this._callMiddlewares(ctx, "before");
275
-
276
- if (!ctx.result) {
277
- try {
278
- ctx.result = await transport.call(ctx);
279
- } catch (err) {
280
- if (!this[p].neverThrow) throw err;
281
-
282
- let { code, message } = err;
283
- if (!err.code || err.noNetToServer) {
284
- code = "ALLSERVER_CLIENT_PROCEDURE_UNREACHABLE";
285
- message = `Couldn't reach remote procedure ${ctx.procedureName} due to: ${err.message}`;
293
+ await this._callMiddlewares(ctx, "before", async () => {
294
+ if (!ctx.result) {
295
+ try {
296
+ ctx.result = await transport.call(ctx);
297
+ } catch (err) {
298
+ if (!this[p].neverThrow) throw err;
299
+
300
+ let { code, message } = err;
301
+ if (!err.code || err.noNetToServer) {
302
+ code = "ALLSERVER_CLIENT_PROCEDURE_UNREACHABLE";
303
+ message = `Couldn't reach remote procedure ${ctx.procedureName} due to: ${err.message}`;
304
+ }
305
+ ctx.result = { success: false, code, message, error: err };
286
306
  }
287
- ctx.result = { success: false, code, message, error: err };
288
307
  }
289
- }
290
308
 
291
- await this._callMiddlewares(ctx, "after");
309
+ await this._callMiddlewares(ctx, "after");
310
+ });
292
311
 
293
312
  return ctx.result;
294
313
  },