two-stroke 1.1.6 → 2.0.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
@@ -51,7 +51,7 @@
51
51
  "name": "two-stroke",
52
52
  "packageManager": "yarn@4.3.0",
53
53
  "type": "module",
54
- "version": "1.1.6",
54
+ "version": "2.0.0",
55
55
  "devDependencies": {
56
56
  "@types/eslint": "^9.6.1",
57
57
  "@types/node": "^22.7.5",
package/src/index.ts CHANGED
@@ -1,9 +1,9 @@
1
- import { Toucan } from "toucan-js";
2
- import { ZodObject, ZodSchema, z } from "zod";
3
- import { verify as pbkdfVerify } from "pbkdf-subtle";
4
1
  import { verify as jwkVerify } from "jwk-subtle";
5
- import { Env, Handler, Route } from "./types";
2
+ import { verify as pbkdfVerify } from "pbkdf-subtle";
3
+ import { Toucan } from "toucan-js";
4
+ import { SafeParseError, ZodObject, ZodSchema, z } from "zod";
6
5
  import { openAPI } from "./open-api";
6
+ import { Env, Handler, Route } from "./types";
7
7
 
8
8
  // eslint-disable-next-line @typescript-eslint/require-await
9
9
  const noAuth = async () => null;
@@ -288,22 +288,56 @@ export function twoStroke<T extends Env>(title: string, release: string) {
288
288
  }
289
289
  throw Error("Invalid");
290
290
  },
291
- queueHandler<I extends ZodSchema>(
291
+ queueHandler<I extends ZodSchema, Parsed = z.infer<I>>(
292
292
  input: I,
293
293
  handler: (c: {
294
294
  env: T;
295
- batch: MessageBatch<z.infer<I>>;
295
+ batch: MessageBatch<Parsed>;
296
+ sentry: Toucan;
297
+ }) => Promise<void>,
298
+ parseFailedHandler?: (c: {
299
+ env: T;
300
+ batch: MessageBatch & {
301
+ messages: (Message<unknown> & {
302
+ error: SafeParseError<z.input<I>>;
303
+ })[];
304
+ };
296
305
  sentry: Toucan;
297
306
  }) => Promise<void>,
298
307
  ) {
299
308
  _queue = async ({ batch, env, sentry }) => {
300
- batch.messages.map((message): void => {
301
- const body = input.safeParse(message.body);
302
- if (!body.success) {
303
- console.error(body.error, message);
304
- }
305
- });
306
- await handler({ batch, env, sentry });
309
+ const { messages, failed } = batch.messages.reduce<{
310
+ messages: Message<Parsed>[];
311
+ failed: (Message<unknown> & { error: SafeParseError<z.input<I>> })[];
312
+ }>(
313
+ (acc, message) => {
314
+ const body: z.SafeParseReturnType<
315
+ z.input<I>,
316
+ Parsed
317
+ > = input.safeParse(message.body);
318
+ if (!body.success) {
319
+ console.error(body.error, message);
320
+ return {
321
+ messages: acc.messages,
322
+ failed: [...acc.failed, { ...message, error: body }],
323
+ };
324
+ }
325
+
326
+ return {
327
+ messages: [...acc.messages, { ...message, body: body.data }],
328
+ failed: acc.failed,
329
+ };
330
+ },
331
+ { messages: [], failed: [] },
332
+ );
333
+
334
+ await handler({ batch: { ...batch, messages }, env, sentry });
335
+ if (failed.length > 0)
336
+ await parseFailedHandler?.({
337
+ batch: { ...batch, messages: failed },
338
+ env,
339
+ sentry,
340
+ });
307
341
  console.log("Queue batch finished");
308
342
  };
309
343
  },
package/src/test.ts CHANGED
@@ -15,6 +15,8 @@ export const setupTests = async <Paths extends {}>(bindings: Env) => {
15
15
  fetchMock.disableNetConnect();
16
16
 
17
17
  const config = toml.parse(fs.readFileSync("wrangler.toml", "utf8")) as {
18
+ compatibility_date: string;
19
+ compatibility_flags: string[];
18
20
  queues?: {
19
21
  consumers?: { queue: string }[];
20
22
  producers?: { queue: string; binding: string }[];
@@ -23,11 +25,14 @@ export const setupTests = async <Paths extends {}>(bindings: Env) => {
23
25
  kv_namespaces?: { binding: string }[];
24
26
  d1_databases?: { binding: string }[];
25
27
  services?: { binding: string; service: string }[];
28
+ durable_objects?: { bindings?: { name: string; class_name: string }[] };
26
29
  };
27
30
 
28
31
  const miniflare = new Miniflare({
29
32
  modules: true,
30
33
  scriptPath: "dist/index.js",
34
+ compatibilityDate: config.compatibility_date,
35
+ compatibilityFlags: config.compatibility_flags,
31
36
  bindings: {
32
37
  TOKEN_HASH:
33
38
  "djAxlhzT1IU9QIP3UKdipECQPAGGoPQK86/GnTBcbHLtPC3ni6JkTQ/iIeF0KG0y1CZ+J+9W",
@@ -47,6 +52,12 @@ export const setupTests = async <Paths extends {}>(bindings: Env) => {
47
52
  (config.services ?? []).map(({ binding, service }) => [binding, service]),
48
53
  ),
49
54
  fetchMock,
55
+ durableObjects: Object.fromEntries(
56
+ (config.durable_objects?.bindings ?? []).map(({ name, class_name }) => [
57
+ name,
58
+ class_name,
59
+ ]),
60
+ ),
50
61
  });
51
62
 
52
63
  const url = await miniflare.ready;
package/src/types.ts CHANGED
@@ -8,7 +8,8 @@ export type Env = {
8
8
  | R2Bucket
9
9
  | D1Database
10
10
  | Fetcher
11
- | Hyperdrive;
11
+ | Hyperdrive
12
+ | DurableObjectNamespace;
12
13
  };
13
14
  export type Route<T extends Env, A> =
14
15
  | {