@remit/backend 0.0.38 → 0.0.39

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/index.ts +31 -25
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@remit/backend",
3
- "version": "0.0.38",
3
+ "version": "0.0.39",
4
4
  "description": "Remit Mail Inspector API backend",
5
5
  "license": "MIT",
6
6
  "author": "",
package/src/index.ts CHANGED
@@ -2,7 +2,7 @@ import { existsSync, readFileSync } from "node:fs";
2
2
  import { dirname, join } from "node:path";
3
3
  import { fileURLToPath } from "node:url";
4
4
  import { inspect } from "node:util";
5
- import { logger, withTelemetry } from "@remit/logger-lambda";
5
+ import { logger, withLogContext, withTelemetry } from "@remit/logger-lambda";
6
6
  import type { APIGatewayProxyEvent, Context } from "aws-lambda";
7
7
  import {
8
8
  type Document,
@@ -141,30 +141,36 @@ const readOriginHeader = (
141
141
  return undefined;
142
142
  };
143
143
 
144
- const rawHandler = async (event: APIGatewayProxyEvent, context: Context) => {
145
- logger.setBindings({
146
- requestId: context.awsRequestId,
147
- path: event.path,
148
- method: event.httpMethod,
149
- });
150
-
151
- logger.debug(
152
- { method: event.httpMethod, path: event.path },
153
- "Request received",
154
- );
155
-
156
- const origin = readOriginHeader(event.headers);
157
-
158
- if (usesBetterAuthJwt()) {
159
- const denied = await authenticatePostgresRequest(event);
160
- if (denied) return denied;
161
- }
162
-
163
- return runWithRequestContext({ origin }, () =>
164
- api
165
- .handleRequest(normalizeRequest(event), event, context)
166
- .catch(handleError),
144
+ // A scope, not `logger.setBindings`: this process serves requests concurrently,
145
+ // and bindings on the shared logger belong to whichever request wrote them last,
146
+ // so a line gets attributed to the wrong request. The scope follows the request
147
+ // through its own async continuations and nothing else.
148
+ const rawHandler = async (event: APIGatewayProxyEvent, context: Context) =>
149
+ withLogContext(
150
+ {
151
+ requestId: context.awsRequestId,
152
+ path: event.path,
153
+ method: event.httpMethod,
154
+ },
155
+ async () => {
156
+ logger.debug(
157
+ { method: event.httpMethod, path: event.path },
158
+ "Request received",
159
+ );
160
+
161
+ const origin = readOriginHeader(event.headers);
162
+
163
+ if (usesBetterAuthJwt()) {
164
+ const denied = await authenticatePostgresRequest(event);
165
+ if (denied) return denied;
166
+ }
167
+
168
+ return runWithRequestContext({ origin }, () =>
169
+ api
170
+ .handleRequest(normalizeRequest(event), event, context)
171
+ .catch(handleError),
172
+ );
173
+ },
167
174
  );
168
- };
169
175
 
170
176
  export const handler = withTelemetry(rawHandler);