allserver 2.2.0-0 → 2.2.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "allserver",
3
- "version": "2.2.0-0",
3
+ "version": "2.2.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": {
@@ -31,14 +31,10 @@ module.exports = require("./ClientTransport").compose({
31
31
  async call({ procedureName, lambda }) {
32
32
  let promise = this.awsSdkLambdaClient.invoke({
33
33
  FunctionName: this.uri.substring("lambda://".length),
34
- // InvocationType: "RequestResponse",
35
- Payload: lambda.payload && JSON.stringify(lambda.payload),
36
- ClientContext: Buffer.from(
37
- JSON.stringify({
38
- ...lambda.clientContext,
39
- procedureName,
40
- })
41
- ).toString("base64"),
34
+ Payload: JSON.stringify({
35
+ callContext: { ...lambda.callContext, procedureName },
36
+ callArg: lambda.callArg,
37
+ }),
42
38
  });
43
39
  if (typeof promise.promise === "function") promise = promise.promise(); // AWS SDK v2 adoption
44
40
  const invocationResponse = await promise;
@@ -49,8 +45,8 @@ module.exports = require("./ClientTransport").compose({
49
45
  return {
50
46
  ...defaultCtx,
51
47
  lambda: {
52
- clientContext: {},
53
- payload: defaultCtx.arg,
48
+ callContext: {},
49
+ callArg: defaultCtx.arg,
54
50
  },
55
51
  };
56
52
  },
@@ -4,17 +4,24 @@ module.exports = require("./Transport").compose({
4
4
  methods: {
5
5
  async deserializeEvent(ctx) {
6
6
  if (ctx.lambda.isHttp) {
7
- const body = ctx.lambda.event.body;
8
- let query = ctx.lambda.query;
7
+ const { body, query } = ctx.lambda.http;
9
8
  try {
10
9
  // If there is no body we will use request query (aka search params)
11
- ctx.arg = body ? JSON.parse(body) : query;
10
+ if (!body) {
11
+ ctx.arg = query || {};
12
+ } else {
13
+ if ((typeof body === "string" && body[0] === "{") || Buffer.isBuffer(body)) {
14
+ ctx.arg = JSON.parse(body);
15
+ } else {
16
+ return false;
17
+ }
18
+ }
12
19
  return true;
13
20
  } catch (err) {
14
21
  return false;
15
22
  }
16
23
  } else {
17
- ctx.arg = ctx.lambda.event || {};
24
+ ctx.arg = ctx.lambda.invoke.callArg || {};
18
25
  return true;
19
26
  }
20
27
  },
@@ -34,19 +41,30 @@ module.exports = require("./Transport").compose({
34
41
  startServer(defaultCtx) {
35
42
  return async (event, context) => {
36
43
  return new Promise((resolve) => {
44
+ const lambda = { event, context, resolve };
45
+
37
46
  const path = (event && (event.path || event.requestContext?.http?.path)) || undefined;
38
- const isHttp = Boolean(path);
39
- const procedureName = isHttp ? path.substr(1) : context.clientContext?.procedureName;
40
- const query = { ...(event?.queryStringParameters || {}) };
41
- const lambda = { resolve, isHttp, event, context, path, query, procedureName };
47
+ lambda.isHttp = Boolean(path);
48
+
49
+ if (lambda.isHttp) {
50
+ lambda.http = {
51
+ path,
52
+ query: event?.queryStringParameters,
53
+ body: event?.body,
54
+ headers: event?.headers,
55
+ };
56
+ } else {
57
+ lambda.invoke = { callContext: event?.callContext, callArg: event?.callArg };
58
+ }
42
59
 
43
60
  this._handleRequest({ ...defaultCtx, lambda });
44
61
  });
45
62
  };
46
63
  },
47
64
 
48
- getProcedureName({ lambda: { procedureName } }) {
49
- return procedureName;
65
+ getProcedureName(ctx) {
66
+ const { isHttp, http, invoke } = ctx.lambda;
67
+ return isHttp ? http.path.substr(1) : invoke.callContext?.procedureName;
50
68
  },
51
69
 
52
70
  isIntrospection(ctx) {