@remit/backend 0.0.38 → 0.0.40

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": "@remit/backend",
3
- "version": "0.0.38",
3
+ "version": "0.0.40",
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);
@@ -316,6 +316,24 @@ export const createRemitClient = (deps: RemitClientDeps): RemitClient => {
316
316
  }
317
317
  : undefined;
318
318
 
319
+ const flagPushService = new FlagPushService({
320
+ markerService: repositories.flagPush,
321
+ sqsQueueUrl,
322
+ logger,
323
+ });
324
+
325
+ // Shared with the returned `flagQueue` below (issue #302): body sync
326
+ // auto-marks-read a message from an unsubscribed sender through the exact
327
+ // same `FlagQueueService.markAsRead` a manual mark-as-read goes through, not
328
+ // a second instance or a second primitive.
329
+ const flagQueueService = new FlagQueueService({
330
+ messageFlagService: repositories.messageFlag,
331
+ messageService: repositories.message,
332
+ threadMessageService: repositories.threadMessage,
333
+ flagPushService,
334
+ logger,
335
+ });
336
+
319
337
  const bodySync = new BodySyncService(
320
338
  repositories.message,
321
339
  storage,
@@ -325,14 +343,10 @@ export const createRemitClient = (deps: RemitClientDeps): RemitClient => {
325
343
  logger,
326
344
  undefined,
327
345
  filterConfig,
346
+ undefined,
347
+ { flagQueueService },
328
348
  );
329
349
 
330
- const flagPushService = new FlagPushService({
331
- markerService: repositories.flagPush,
332
- sqsQueueUrl,
333
- logger,
334
- });
335
-
336
350
  return {
337
351
  accountConfig: repositories.accountConfig,
338
352
  account: repositories.account,
@@ -364,13 +378,7 @@ export const createRemitClient = (deps: RemitClientDeps): RemitClient => {
364
378
  placementMove: repositories.placementMove,
365
379
  flagPush: repositories.flagPush,
366
380
 
367
- flagQueue: new FlagQueueService({
368
- messageFlagService: repositories.messageFlag,
369
- messageService: repositories.message,
370
- threadMessageService: repositories.threadMessage,
371
- flagPushService,
372
- logger,
373
- }),
381
+ flagQueue: flagQueueService,
374
382
  mailboxQueue: new MailboxQueueService({
375
383
  mailboxService: repositories.mailbox,
376
384
  sqsQueueUrl,