two-stroke 5.6.3 → 5.8.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
@@ -10,25 +10,25 @@
10
10
  "type-check": "./bin/type-check.mjs"
11
11
  },
12
12
  "dependencies": {
13
- "@cloudflare/vitest-pool-workers": "^0.8.60",
14
- "@cloudflare/workers-types": "^4.20250805.0",
15
- "@sentry/cli": "^2.50.2",
16
- "@types/node": "^24.2.0",
17
- "@typescript-eslint/eslint-plugin": "^8.39.0",
18
- "@typescript-eslint/parser": "^8.39.0",
13
+ "@cloudflare/vitest-pool-workers": "^0.8.65",
14
+ "@cloudflare/workers-types": "^4.20250819.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.4",
21
+ "eslint-config-two-stroke": "^1.2.7",
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.20250803.0",
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.14"
31
+ "zod": "^4.0.17"
32
32
  },
33
33
  "peerDependencies": {
34
34
  "@sentry/cli": ">=2",
@@ -52,10 +52,10 @@
52
52
  "name": "two-stroke",
53
53
  "packageManager": "yarn@4.6.0",
54
54
  "type": "module",
55
- "version": "5.6.3",
55
+ "version": "5.8.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
60
  "typescript": "^5.9.2",
61
61
  "vitest": "^3.2.4"
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,14 @@ 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
+ ? env.SENTRY_ENVIRONMENT === "staging" &&
55
+ req.headers.get("Origin")?.split(":")[1]?.endsWith("localhost")
56
+ ? (req.headers.get("Origin") ?? "")
57
+ : origin(req.headers.get("Origin"))
58
+ : "*",
59
+ };
48
60
  const sentry = new Toucan({
49
61
  dsn: env.SENTRY_DSN,
50
62
  context,
@@ -60,9 +72,9 @@ export function twoStroke<T extends Env>(title: string, release: string) {
60
72
  return new Response(null, {
61
73
  status: 204,
62
74
  headers: {
63
- "Access-Control-Allow-Origin": "*",
75
+ ...defaultHeaders,
64
76
  "Access-Control-Max-Age": "86400",
65
- "Access-Control-Allow-Methods": "GET,PUT,POST,DELETE",
77
+ "Access-Control-Allow-Methods": "GET,HEAD,PUT,POST,DELETE",
66
78
  "Access-Control-Allow-Headers": "Authorization,Content-Type",
67
79
  },
68
80
  });
@@ -86,8 +98,8 @@ export function twoStroke<T extends Env>(title: string, release: string) {
86
98
  status: 401,
87
99
  statusText: "Invalid Authorization",
88
100
  headers: {
101
+ ...defaultHeaders,
89
102
  "WWW-Authenticate": "Bearer",
90
- "Access-Control-Allow-Origin": "*",
91
103
  },
92
104
  });
93
105
  }
@@ -112,9 +124,7 @@ export function twoStroke<T extends Env>(title: string, release: string) {
112
124
  }),
113
125
  {
114
126
  status: 400,
115
- headers: {
116
- "Access-Control-Allow-Origin": "*",
117
- },
127
+ headers: defaultHeaders,
118
128
  },
119
129
  );
120
130
  }
@@ -150,9 +160,7 @@ export function twoStroke<T extends Env>(title: string, release: string) {
150
160
  }),
151
161
  {
152
162
  status: 400,
153
- headers: {
154
- "Access-Control-Allow-Origin": "*",
155
- },
163
+ headers: defaultHeaders,
156
164
  },
157
165
  );
158
166
  }
@@ -185,16 +193,13 @@ export function twoStroke<T extends Env>(title: string, release: string) {
185
193
  ...response,
186
194
  headers: new Headers(response.headers ?? {}),
187
195
  };
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
- );
196
+ Object.entries({
197
+ ...defaultHeaders,
198
+ "Content-Type": "application/json",
199
+ }).forEach(([k, v]) => {
200
+ if (!responseWithHeaders.headers.has(k))
201
+ responseWithHeaders.headers.set(k, v);
202
+ });
198
203
 
199
204
  return new Response(
200
205
  // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
@@ -208,7 +213,7 @@ export function twoStroke<T extends Env>(title: string, release: string) {
208
213
  }
209
214
  return new Response("", {
210
215
  status: 404,
211
- headers: { "Access-Control-Allow-Origin": "*" },
216
+ headers: defaultHeaders,
212
217
  });
213
218
  } catch (err) {
214
219
  console.warn(err);
@@ -216,7 +221,7 @@ export function twoStroke<T extends Env>(title: string, release: string) {
216
221
  return new Response("", {
217
222
  status: 500,
218
223
  statusText: "Internal Server Error",
219
- headers: { "Access-Control-Allow-Origin": "*" },
224
+ headers: defaultHeaders,
220
225
  });
221
226
  }
222
227
  },
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;
package/tsconfig.json CHANGED
@@ -1,5 +1,6 @@
1
1
  {
2
2
  "compilerOptions": {
3
+ "erasableSyntaxOnly": true,
3
4
  "esModuleInterop": true,
4
5
  "isolatedModules": true,
5
6
  "lib": ["ESNext"],
@@ -14,7 +15,8 @@
14
15
  "@cloudflare/workers-types",
15
16
  "vitest/globals",
16
17
  "@cloudflare/vitest-pool-workers"
17
- ]
18
+ ],
19
+ "verbatimModuleSyntax": true
18
20
  },
19
21
  "exclude": ["../../dist"]
20
22
  }