allserver 2.3.0 → 2.4.1
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 --
|
|
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
|
@@ -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
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
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
|
-
|
|
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
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
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
|
-
|
|
309
|
+
await this._callMiddlewares(ctx, "after");
|
|
310
|
+
});
|
|
292
311
|
|
|
293
312
|
return ctx.result;
|
|
294
313
|
},
|
|
@@ -41,7 +41,7 @@ module.exports = require("./ClientTransport").compose({
|
|
|
41
41
|
});
|
|
42
42
|
if (typeof promise.promise === "function") promise = promise.promise(); // AWS SDK v2 adoption
|
|
43
43
|
const invocationResponse = await promise;
|
|
44
|
-
return JSON.parse(invocationResponse.Payload);
|
|
44
|
+
return JSON.parse(Buffer.from(invocationResponse.Payload));
|
|
45
45
|
},
|
|
46
46
|
|
|
47
47
|
createCallContext(defaultCtx) {
|