two-stroke 5.6.1 → 5.7.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/bin/test.mjs CHANGED
@@ -35,7 +35,7 @@ if (fs.existsSync("wrangler.toml")) {
35
35
  .join("\n\n");
36
36
  fs.writeFileSync(
37
37
  "test/api.d.ts",
38
- await prettier.format(result, { parser: "typescript" }),
38
+ await prettier.format(result, { parser: "typescript", printWidth: 100 }),
39
39
  );
40
40
  }
41
41
  }
package/package.json CHANGED
@@ -10,32 +10,32 @@
10
10
  "type-check": "./bin/type-check.mjs"
11
11
  },
12
12
  "dependencies": {
13
- "@cloudflare/vitest-pool-workers": "^0.8.57",
14
- "@cloudflare/workers-types": "^4.20250726.0",
15
- "@sentry/cli": "^2.50.2",
16
- "@types/node": "^24.1.0",
17
- "@typescript-eslint/eslint-plugin": "^8.38.0",
18
- "@typescript-eslint/parser": "^8.38.0",
13
+ "@cloudflare/vitest-pool-workers": "^0.8.65",
14
+ "@cloudflare/workers-types": "^4.20250816.0",
15
+ "@sentry/cli": "^2.52.0",
16
+ "@types/node": "^24.3.0",
17
+ "@typescript-eslint/eslint-plugin": "^8.40.0",
18
+ "@typescript-eslint/parser": "^8.40.0",
19
19
  "@vitest/coverage-istanbul": "^3.2.4",
20
20
  "eslint-config-prettier": "^10.1.8",
21
- "eslint-config-two-stroke": "^1.2.3",
21
+ "eslint-config-two-stroke": "^1.2.6",
22
22
  "eslint-config-typescript": "^3.0.0",
23
23
  "jose": "^6.0.12",
24
24
  "jwk-subtle": "^1.0.7",
25
25
  "mime": "^4.0.7",
26
- "miniflare": "^4.20250712.2",
26
+ "miniflare": "^4.20250816.0",
27
27
  "openapi-fetch": "^0.14.0",
28
- "openapi-typescript": "^7.8.0",
28
+ "openapi-typescript": "^7.9.1",
29
29
  "pbkdf-subtle": "^1.0.0",
30
30
  "toucan-js": "^4.1.1",
31
- "zod": "^4.0.10"
31
+ "zod": "^4.0.17"
32
32
  },
33
33
  "peerDependencies": {
34
34
  "@sentry/cli": ">=2",
35
35
  "eslint": ">=9",
36
36
  "prettier": ">=3",
37
37
  "typescript": ">=5",
38
- "vitest": "^2.1.8",
38
+ "vitest": ">=3",
39
39
  "wrangler": ">=3"
40
40
  },
41
41
  "description": "Simple Cloudflare Worker framework.",
@@ -52,12 +52,12 @@
52
52
  "name": "two-stroke",
53
53
  "packageManager": "yarn@4.6.0",
54
54
  "type": "module",
55
- "version": "5.6.1",
55
+ "version": "5.7.0",
56
56
  "devDependencies": {
57
57
  "@types/eslint": "^9.6.1",
58
- "eslint": "^9.32.0",
58
+ "eslint": "^9.33.0",
59
59
  "prettier": "^3.6.2",
60
- "typescript": "^5.8.3",
60
+ "typescript": "^5.9.2",
61
61
  "vitest": "^3.2.4"
62
62
  }
63
63
  }
package/src/index.ts CHANGED
@@ -11,7 +11,11 @@ const noAuth = async () => null;
11
11
  const escapeRegex = (str: string) =>
12
12
  str.replace(/([.*+?^=!:$()|[\]\\])/g, "\\$&");
13
13
 
14
- export function twoStroke<T extends Env>(title: string, release: string) {
14
+ export function twoStroke<T extends Env>(
15
+ title: string,
16
+ release: string,
17
+ origin?: (o: string | null) => string,
18
+ ) {
15
19
  let _queue: (c: {
16
20
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
17
21
  batch: MessageBatch<any>;
@@ -45,6 +49,11 @@ export function twoStroke<T extends Env>(title: string, release: string) {
45
49
  },
46
50
  context: ExecutionContext,
47
51
  ): Promise<Response> {
52
+ const defaultHeaders = {
53
+ "Access-Control-Allow-Origin": origin
54
+ ? origin(req.headers.get("Origin"))
55
+ : "*",
56
+ };
48
57
  const sentry = new Toucan({
49
58
  dsn: env.SENTRY_DSN,
50
59
  context,
@@ -60,9 +69,9 @@ export function twoStroke<T extends Env>(title: string, release: string) {
60
69
  return new Response(null, {
61
70
  status: 204,
62
71
  headers: {
63
- "Access-Control-Allow-Origin": "*",
72
+ ...defaultHeaders,
64
73
  "Access-Control-Max-Age": "86400",
65
- "Access-Control-Allow-Methods": "GET,PUT,POST,DELETE",
74
+ "Access-Control-Allow-Methods": "GET,HEAD,PUT,POST,DELETE",
66
75
  "Access-Control-Allow-Headers": "Authorization,Content-Type",
67
76
  },
68
77
  });
@@ -86,8 +95,8 @@ export function twoStroke<T extends Env>(title: string, release: string) {
86
95
  status: 401,
87
96
  statusText: "Invalid Authorization",
88
97
  headers: {
98
+ ...defaultHeaders,
89
99
  "WWW-Authenticate": "Bearer",
90
- "Access-Control-Allow-Origin": "*",
91
100
  },
92
101
  });
93
102
  }
@@ -112,9 +121,7 @@ export function twoStroke<T extends Env>(title: string, release: string) {
112
121
  }),
113
122
  {
114
123
  status: 400,
115
- headers: {
116
- "Access-Control-Allow-Origin": "*",
117
- },
124
+ headers: defaultHeaders,
118
125
  },
119
126
  );
120
127
  }
@@ -150,9 +157,7 @@ export function twoStroke<T extends Env>(title: string, release: string) {
150
157
  }),
151
158
  {
152
159
  status: 400,
153
- headers: {
154
- "Access-Control-Allow-Origin": "*",
155
- },
160
+ headers: defaultHeaders,
156
161
  },
157
162
  );
158
163
  }
@@ -185,16 +190,13 @@ export function twoStroke<T extends Env>(title: string, release: string) {
185
190
  ...response,
186
191
  headers: new Headers(response.headers ?? {}),
187
192
  };
188
- if (!responseWithHeaders.headers.has("Content-Type"))
189
- responseWithHeaders.headers.set(
190
- "Content-Type",
191
- "application/json",
192
- );
193
- if (!responseWithHeaders.headers.has("Access-Control-Allow-Origin"))
194
- responseWithHeaders.headers.set(
195
- "Access-Control-Allow-Origin",
196
- "*",
197
- );
193
+ Object.entries({
194
+ ...defaultHeaders,
195
+ "Content-Type": "application/json",
196
+ }).forEach(([k, v]) => {
197
+ if (!responseWithHeaders.headers.has(k))
198
+ responseWithHeaders.headers.set(k, v);
199
+ });
198
200
 
199
201
  return new Response(
200
202
  // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
@@ -208,7 +210,7 @@ export function twoStroke<T extends Env>(title: string, release: string) {
208
210
  }
209
211
  return new Response("", {
210
212
  status: 404,
211
- headers: { "Access-Control-Allow-Origin": "*" },
213
+ headers: defaultHeaders,
212
214
  });
213
215
  } catch (err) {
214
216
  console.warn(err);
@@ -216,7 +218,7 @@ export function twoStroke<T extends Env>(title: string, release: string) {
216
218
  return new Response("", {
217
219
  status: 500,
218
220
  statusText: "Internal Server Error",
219
- headers: { "Access-Control-Allow-Origin": "*" },
221
+ headers: defaultHeaders,
220
222
  });
221
223
  }
222
224
  },
package/src/types.ts CHANGED
@@ -64,6 +64,16 @@ export type Handler<
64
64
  status?: 200;
65
65
  headers?: Record<string, string>;
66
66
  }
67
+ | {
68
+ body: z.infer<O>;
69
+ status: 301;
70
+ headers?: Record<string, string>;
71
+ }
72
+ | {
73
+ body: z.infer<O>;
74
+ status: 302;
75
+ headers?: Record<string, string>;
76
+ }
67
77
  | {
68
78
  body?: { error: string };
69
79
  status: number;