two-stroke 1.0.12 → 1.0.14

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 +17 -17
  2. package/src/test.ts +54 -19
package/package.json CHANGED
@@ -8,22 +8,22 @@
8
8
  "type-check": "./bin/type-check.mjs"
9
9
  },
10
10
  "dependencies": {
11
- "@anatine/zod-openapi": "^2.2.3",
12
- "@asteasolutions/zod-to-openapi": "^6.4.0",
13
- "@cloudflare/workers-types": "^4.20240222.0",
14
- "@sentry/cli": "^2.30.0",
15
- "@typescript-eslint/eslint-plugin": "^7.1.1",
16
- "@typescript-eslint/parser": "^7.1.1",
17
- "@vitest/coverage-v8": "^1.3.1",
11
+ "@anatine/zod-openapi": "^2.2.5",
12
+ "@asteasolutions/zod-to-openapi": "^7.0.0",
13
+ "@cloudflare/workers-types": "^4.20240405.0",
14
+ "@sentry/cli": "^2.31.0",
15
+ "@typescript-eslint/eslint-plugin": "^7.7.0",
16
+ "@typescript-eslint/parser": "^7.7.0",
17
+ "@vitest/coverage-v8": "^1.5.0",
18
18
  "eslint-config-prettier": "^9.1.0",
19
- "eslint-config-two-stroke": "^1.0.0",
19
+ "eslint-config-two-stroke": "^1.0.1",
20
20
  "eslint-config-typescript": "^3.0.0",
21
- "jose": "^5.2.3",
21
+ "jose": "^5.2.4",
22
22
  "jwk-subtle": "^1.0.7",
23
- "miniflare": "^3.20240223.1",
23
+ "miniflare": "^3.20240405.1",
24
24
  "openapi-fetch": "^0.9.3",
25
- "openapi-typescript": "^6.7.4",
26
- "openapi3-ts": "^4.2.2",
25
+ "openapi-typescript": "^6.7.5",
26
+ "openapi3-ts": "^4.3.1",
27
27
  "pbkdf-subtle": "^1.0.0",
28
28
  "toml": "^3.0.0",
29
29
  "toucan-js": "^3.3.1",
@@ -51,13 +51,13 @@
51
51
  "name": "two-stroke",
52
52
  "packageManager": "yarn@4.1.0",
53
53
  "type": "module",
54
- "version": "1.0.12",
54
+ "version": "1.0.14",
55
55
  "devDependencies": {
56
- "@types/eslint": "^8.56.5",
57
- "@types/node": "^20.11.25",
56
+ "@types/eslint": "^8.56.9",
57
+ "@types/node": "^20.12.7",
58
58
  "eslint": "^8.57.0",
59
59
  "prettier": "^3.2.5",
60
- "typescript": "^5.4.2",
61
- "vitest": "^1.3.1"
60
+ "typescript": "^5.4.5",
61
+ "vitest": "^1.5.0"
62
62
  }
63
63
  }
package/src/test.ts CHANGED
@@ -1,11 +1,13 @@
1
- import { Miniflare, createFetchMock } from "miniflare";
2
- import toml from "toml";
3
1
  import fs from "fs";
4
- import { Env } from "./types";
2
+ import { JWTPayload, SignJWT, exportJWK, generateKeyPair } from "jose";
3
+ import { Miniflare, createFetchMock } from "miniflare";
4
+ import nodefs from "node:fs/promises";
5
+ import path from "node:path";
6
+ import createClient from "openapi-fetch";
5
7
  import consumers from "stream/consumers";
8
+ import toml from "toml";
6
9
  import { URLSearchParams } from "url";
7
- import createClient from "openapi-fetch";
8
- import { generateKeyPair, SignJWT, JWTPayload, exportJWK } from "jose";
10
+ import { Env } from "./types";
9
11
 
10
12
  // eslint-disable-next-line @typescript-eslint/ban-types
11
13
  export const setupTests = async <Paths extends {}>(bindings: Env) => {
@@ -23,38 +25,71 @@ export const setupTests = async <Paths extends {}>(bindings: Env) => {
23
25
  ...bindings,
24
26
  },
25
27
  queueConsumers: (config.queues?.consumers ?? []).map(
26
- ({ queue }: { queue: string }) => queue,
28
+ ({ queue }: { queue: string }) => queue
27
29
  ),
28
30
  queueProducers: Object.fromEntries(
29
31
  (config.queues?.producers ?? []).map(
30
32
  ({ binding, queue }: { queue: string; binding: string }) => [
31
33
  binding,
32
34
  queue,
33
- ],
34
- ),
35
+ ]
36
+ )
35
37
  ),
36
38
  r2Buckets: (config.r2_buckets ?? []).map(
37
- ({ binding }: { binding: string }) => binding,
39
+ ({ binding }: { binding: string }) => binding
38
40
  ),
39
41
  kvNamespaces: (config.kv_namespaces ?? []).map(
40
- ({ binding }: { binding: string }) => binding,
42
+ ({ binding }: { binding: string }) => binding
41
43
  ),
42
44
  d1Databases: (config.d1_databases ?? []).map(
43
- ({ binding }: { binding: string }) => binding,
45
+ ({ binding }: { binding: string }) => binding
44
46
  ),
45
47
  serviceBindings: Object.fromEntries(
46
48
  (config.services ?? []).map(
47
49
  ({ binding, service }: { binding: string; service: string }) => [
48
50
  binding,
49
51
  service,
50
- ],
51
- ),
52
+ ]
53
+ )
52
54
  ),
53
55
  fetchMock,
54
56
  });
55
57
 
56
58
  const url = await miniflare.ready;
57
59
 
60
+ // Hack until miniflare supports auto running D1 migrations.
61
+ // Note: Each migration file can contain only a single statement.
62
+ await Promise.all(
63
+ (config.d1_databases ?? []).map(
64
+ async ({ binding }: { binding: string }) => {
65
+ const d1 = await miniflare.getD1Database(binding);
66
+
67
+ const migrationsDir = "./migrations";
68
+ for (const migrationName of (await nodefs.readdir(migrationsDir)).sort(
69
+ (a, b) => {
70
+ const migrationNumberA = parseInt(a.split("_")[0]!);
71
+ const migrationNumberB = parseInt(b.split("_")[0]!);
72
+ if (migrationNumberA < migrationNumberB) {
73
+ return -1;
74
+ }
75
+ if (migrationNumberA > migrationNumberB) {
76
+ return 1;
77
+ }
78
+
79
+ // numbers must be equal
80
+ return 0;
81
+ }
82
+ )) {
83
+ const migrationPath = path.join(migrationsDir, migrationName);
84
+ let migration = await nodefs.readFile(migrationPath, "utf8");
85
+ // Remove migration comment
86
+ migration = migration.split("\n").slice(1).join(" ");
87
+ await d1.exec(migration);
88
+ }
89
+ }
90
+ )
91
+ );
92
+
58
93
  const client = createClient<Paths>({ baseUrl: url.toString() });
59
94
 
60
95
  return {
@@ -91,7 +126,7 @@ export const setupTests = async <Paths extends {}>(bindings: Env) => {
91
126
  headers: {
92
127
  "Content-Type": "application/json",
93
128
  },
94
- },
129
+ }
95
130
  )
96
131
  .persist();
97
132
  fetchMock
@@ -106,7 +141,7 @@ export const setupTests = async <Paths extends {}>(bindings: Env) => {
106
141
  headers: {
107
142
  "Content-Type": "application/json",
108
143
  },
109
- },
144
+ }
110
145
  )
111
146
  .persist();
112
147
  return await new SignJWT(claims)
@@ -138,7 +173,7 @@ export function recordRequest(
138
173
  data: string | object | Buffer | undefined,
139
174
  responseOptions?: {
140
175
  headers: Record<string, string | string[] | undefined>;
141
- },
176
+ }
142
177
  ) {
143
178
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
144
179
  return ({ body }: any) => {
@@ -154,7 +189,7 @@ export function recordFormRequest(
154
189
  data: string | object | Buffer | undefined,
155
190
  responseOptions?: {
156
191
  headers: Record<string, string | string[] | undefined>;
157
- },
192
+ }
158
193
  ) {
159
194
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
160
195
  return ({ body }: any) => {
@@ -162,7 +197,7 @@ export function recordFormRequest(
162
197
  .text(body)
163
198
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
164
199
  .then((data: any) =>
165
- cb(Object.fromEntries(new URLSearchParams(data).entries())),
200
+ cb(Object.fromEntries(new URLSearchParams(data).entries()))
166
201
  );
167
202
  return { statusCode, data, responseOptions };
168
203
  };
@@ -175,7 +210,7 @@ export function recordFirehoseRequest(
175
210
  data: string | object | Buffer | undefined,
176
211
  responseOptions?: {
177
212
  headers: Record<string, string | string[] | undefined>;
178
- },
213
+ }
179
214
  ) {
180
215
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
181
216
  return ({ body }: any) => {