two-stroke 1.0.13 → 1.0.15

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 +1 -1
  2. package/src/test.ts +62 -19
package/package.json CHANGED
@@ -51,7 +51,7 @@
51
51
  "name": "two-stroke",
52
52
  "packageManager": "yarn@4.1.0",
53
53
  "type": "module",
54
- "version": "1.0.13",
54
+ "version": "1.0.15",
55
55
  "devDependencies": {
56
56
  "@types/eslint": "^8.56.9",
57
57
  "@types/node": "^20.12.7",
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,79 @@ 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
+ let fileNames: string[] = [];
69
+ try {
70
+ fileNames = await nodefs.readdir(migrationsDir);
71
+ } catch (error) {
72
+ console.log("Error loading migrations", error);
73
+ }
74
+
75
+ if (fileNames.length === 0)
76
+ console.log("No migrations found in ./migrations");
77
+
78
+ for (const migrationName of fileNames.sort((a, b) => {
79
+ const migrationNumberA = parseInt(a.split("_")[0]!);
80
+ const migrationNumberB = parseInt(b.split("_")[0]!);
81
+ if (migrationNumberA < migrationNumberB) {
82
+ return -1;
83
+ }
84
+ if (migrationNumberA > migrationNumberB) {
85
+ return 1;
86
+ }
87
+
88
+ // numbers must be equal
89
+ return 0;
90
+ })) {
91
+ const migrationPath = path.join(migrationsDir, migrationName);
92
+ let migration = await nodefs.readFile(migrationPath, "utf8");
93
+ // Remove migration comment
94
+ migration = migration.split("\n").slice(1).join(" ");
95
+ await d1.exec(migration);
96
+ }
97
+ }
98
+ )
99
+ );
100
+
58
101
  const client = createClient<Paths>({ baseUrl: url.toString() });
59
102
 
60
103
  return {
@@ -91,7 +134,7 @@ export const setupTests = async <Paths extends {}>(bindings: Env) => {
91
134
  headers: {
92
135
  "Content-Type": "application/json",
93
136
  },
94
- },
137
+ }
95
138
  )
96
139
  .persist();
97
140
  fetchMock
@@ -106,7 +149,7 @@ export const setupTests = async <Paths extends {}>(bindings: Env) => {
106
149
  headers: {
107
150
  "Content-Type": "application/json",
108
151
  },
109
- },
152
+ }
110
153
  )
111
154
  .persist();
112
155
  return await new SignJWT(claims)
@@ -138,7 +181,7 @@ export function recordRequest(
138
181
  data: string | object | Buffer | undefined,
139
182
  responseOptions?: {
140
183
  headers: Record<string, string | string[] | undefined>;
141
- },
184
+ }
142
185
  ) {
143
186
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
144
187
  return ({ body }: any) => {
@@ -154,7 +197,7 @@ export function recordFormRequest(
154
197
  data: string | object | Buffer | undefined,
155
198
  responseOptions?: {
156
199
  headers: Record<string, string | string[] | undefined>;
157
- },
200
+ }
158
201
  ) {
159
202
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
160
203
  return ({ body }: any) => {
@@ -162,7 +205,7 @@ export function recordFormRequest(
162
205
  .text(body)
163
206
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
164
207
  .then((data: any) =>
165
- cb(Object.fromEntries(new URLSearchParams(data).entries())),
208
+ cb(Object.fromEntries(new URLSearchParams(data).entries()))
166
209
  );
167
210
  return { statusCode, data, responseOptions };
168
211
  };
@@ -175,7 +218,7 @@ export function recordFirehoseRequest(
175
218
  data: string | object | Buffer | undefined,
176
219
  responseOptions?: {
177
220
  headers: Record<string, string | string[] | undefined>;
178
- },
221
+ }
179
222
  ) {
180
223
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
181
224
  return ({ body }: any) => {