@rexezuge/tooling 1.9.1 → 1.9.3

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 (42) hide show
  1. package/dist/eslint.d.ts.map +1 -1
  2. package/dist/eslint.js +1 -1
  3. package/dist/eslint.js.map +2 -2
  4. package/dist/scripts/check-god-files.js +2 -2
  5. package/dist/scripts/check-god-files.js.map +3 -3
  6. package/dist/scripts/lib/entrypoint.d.ts +27 -0
  7. package/dist/scripts/lib/entrypoint.d.ts.map +1 -0
  8. package/dist/scripts/lib/entrypoint.js +1 -0
  9. package/dist/scripts/lib/entrypoint.js.map +7 -0
  10. package/dist/scripts/spa-shell.js +2 -2
  11. package/dist/scripts/spa-shell.js.map +3 -3
  12. package/dist/scripts/validate-locales.js +5 -5
  13. package/dist/scripts/validate-locales.js.map +3 -3
  14. package/dist/scripts/verify-migrations.js +2 -2
  15. package/dist/scripts/verify-migrations.js.map +3 -3
  16. package/dist/scripts/wrangler-config/patches.d.ts.map +1 -1
  17. package/dist/scripts/wrangler-config/patches.js +3 -3
  18. package/dist/scripts/wrangler-config/patches.js.map +3 -3
  19. package/dist/scripts/wrangler-config/resources.d.ts +17 -7
  20. package/dist/scripts/wrangler-config/resources.d.ts.map +1 -1
  21. package/dist/scripts/wrangler-config/resources.js +1 -1
  22. package/dist/scripts/wrangler-config/resources.js.map +3 -3
  23. package/dist/scripts/wrangler-config/types.d.ts +3 -2
  24. package/dist/scripts/wrangler-config/types.d.ts.map +1 -1
  25. package/dist/scripts/wrangler-config/types.js.map +2 -2
  26. package/dist/vite.d.ts +21 -0
  27. package/dist/vite.d.ts.map +1 -1
  28. package/dist/vite.js +3 -3
  29. package/dist/vite.js.map +2 -2
  30. package/package.json +3 -3
  31. package/src/eslint.ts +9 -1
  32. package/src/scripts/check-god-files.ts +2 -2
  33. package/src/scripts/lib/entrypoint.test.ts +60 -0
  34. package/src/scripts/lib/entrypoint.ts +39 -0
  35. package/src/scripts/spa-shell.ts +2 -2
  36. package/src/scripts/validate-locales.ts +2 -2
  37. package/src/scripts/verify-migrations.ts +2 -2
  38. package/src/scripts/wrangler-config/patches.ts +11 -2
  39. package/src/scripts/wrangler-config/resources.test.ts +49 -0
  40. package/src/scripts/wrangler-config/resources.ts +38 -16
  41. package/src/scripts/wrangler-config/types.ts +3 -2
  42. package/src/vite.ts +31 -1
@@ -0,0 +1,49 @@
1
+ import { describe, expect, it } from 'vitest';
2
+ import { ensureRequiredKvBindings, getKVNamespaceName, getRequiredKvBindings } from './resources.ts';
3
+ import type { WranglerConfig } from './types.ts';
4
+
5
+ describe('getRequiredKvBindings', () => {
6
+ it('defaults to the CACHE binding every deployment needs', () => {
7
+ expect(getRequiredKvBindings()).toEqual(['CACHE']);
8
+ });
9
+
10
+ it('takes the bindings from the map it is given', () => {
11
+ expect(getRequiredKvBindings({ RECORD_CACHE: 'record-provider-record-cache' })).toEqual(['RECORD_CACHE']);
12
+ });
13
+
14
+ it('treats an explicitly empty map as needing no bindings', () => {
15
+ expect(getRequiredKvBindings({})).toEqual([]);
16
+ });
17
+ });
18
+
19
+ describe('getKVNamespaceName', () => {
20
+ it('derives a name from the worker and binding when the map does not list it', () => {
21
+ expect(getKVNamespaceName({ name: 'records-provider' }, 'RECORD_CACHE')).toBe('records-provider-record_cache');
22
+ });
23
+
24
+ it('uses the name the map gives', () => {
25
+ const namespaceNames = { RECORD_CACHE: 'record-provider-record-cache' };
26
+ expect(getKVNamespaceName({ name: 'records-provider' }, 'RECORD_CACHE', namespaceNames)).toBe('record-provider-record-cache');
27
+ });
28
+ });
29
+
30
+ describe('ensureRequiredKvBindings', () => {
31
+ it('adds the bindings from the caller’s map rather than the default CACHE binding', () => {
32
+ // The regression: a repo that binds RECORD_CACHE to an existing namespace
33
+ // must not have a stray CACHE added, whose provisioning would create an
34
+ // empty namespace and leave the record data unread.
35
+ const patched = ensureRequiredKvBindings(
36
+ '{\n "kv_namespaces": []\n}',
37
+ { kv_namespaces: [] },
38
+ { RECORD_CACHE: 'record-provider-record-cache' },
39
+ );
40
+ const bindings = (JSON.parse(patched) as WranglerConfig).kv_namespaces?.map((namespace) => namespace.binding);
41
+ expect(bindings).toEqual(['RECORD_CACHE']);
42
+ });
43
+
44
+ it('adds the default binding when no map is given', () => {
45
+ const patched = ensureRequiredKvBindings('{\n "kv_namespaces": []\n}', { kv_namespaces: [] });
46
+ const bindings = (JSON.parse(patched) as WranglerConfig).kv_namespaces?.map((namespace) => namespace.binding);
47
+ expect(bindings).toEqual(['CACHE']);
48
+ });
49
+ });
@@ -77,14 +77,27 @@ export function listKVNamespaces(): KVNamespace[] {
77
77
  }
78
78
 
79
79
  /**
80
- The namespace name a binding's namespace is created under.
81
- */
82
- export function getKVNamespaceName(config: WranglerConfig, binding: string): string {
83
- return DEFAULT_KV_NAMESPACE_NAMES[binding] ?? `${config.name ?? DEFAULT_WORKER_NAME}-${binding.toLowerCase()}`;
80
+ * The namespace name a binding's namespace is created under.
81
+ *
82
+ * `namespaceNames` is the binding → namespace map, defaulting to the kit's
83
+ * `DEFAULT_KV_NAMESPACE_NAMES`. A consumer whose namespaces predate that convention
84
+ * passes its own, so provisioning finds the namespace that already holds the data
85
+ * instead of creating an empty one beside it and binding the worker to the new one.
86
+ */
87
+ export function getKVNamespaceName(
88
+ config: WranglerConfig,
89
+ binding: string,
90
+ namespaceNames: Record<string, string> = DEFAULT_KV_NAMESPACE_NAMES,
91
+ ): string {
92
+ return namespaceNames[binding] ?? `${config.name ?? DEFAULT_WORKER_NAME}-${binding.toLowerCase()}`;
84
93
  }
85
94
 
86
- export function ensureKVNamespace(config: WranglerConfig, binding: string): Provisioned {
87
- const namespaceName = getKVNamespaceName(config, binding);
95
+ export function ensureKVNamespace(
96
+ config: WranglerConfig,
97
+ binding: string,
98
+ namespaceNames: Record<string, string> = DEFAULT_KV_NAMESPACE_NAMES,
99
+ ): Provisioned {
100
+ const namespaceName = getKVNamespaceName(config, binding, namespaceNames);
88
101
  const candidateNames = new Set([namespaceName, `${config.name ?? DEFAULT_WORKER_NAME}-${binding}`, binding]);
89
102
  let namespace = listKVNamespaces().find((candidate) => {
90
103
  const candidateName = candidate.title ?? candidate.name;
@@ -106,8 +119,8 @@ export function ensureKVNamespace(config: WranglerConfig, binding: string): Prov
106
119
  /**
107
120
  The bindings a deployment always needs, whether or not a custom config lists them.
108
121
  */
109
- export function getRequiredKvBindings(): string[] {
110
- return Object.keys(DEFAULT_KV_NAMESPACE_NAMES);
122
+ export function getRequiredKvBindings(namespaceNames: Record<string, string> = DEFAULT_KV_NAMESPACE_NAMES): string[] {
123
+ return Object.keys(namespaceNames);
111
124
  }
112
125
 
113
126
  /**
@@ -116,9 +129,13 @@ export function getRequiredKvBindings(): string[] {
116
129
  * A config supplied through `WRANGLER_JSONC` is edited by hand and drifts; without
117
130
  * this the worker starts with an unbound `CACHE` and fails on the first read.
118
131
  */
119
- export function ensureRequiredKvBindings(content: string, config: WranglerConfig): string {
132
+ export function ensureRequiredKvBindings(
133
+ content: string,
134
+ config: WranglerConfig,
135
+ namespaceNames: Record<string, string> = DEFAULT_KV_NAMESPACE_NAMES,
136
+ ): string {
120
137
  const existing = new Set((config.kv_namespaces ?? []).map((namespace) => namespace.binding).filter((binding) => binding !== undefined));
121
- const missing = getRequiredKvBindings().filter((binding) => !existing.has(binding));
138
+ const missing = getRequiredKvBindings(namespaceNames).filter((binding) => !existing.has(binding));
122
139
  if (missing.length === 0) {
123
140
  return content;
124
141
  }
@@ -256,15 +273,15 @@ function provisionD1Databases(content: string, config: WranglerConfig, created:
256
273
  /**
257
274
  Replace each placeholder KV namespace id with a real one.
258
275
  */
259
- function provisionKvNamespaces(content: string, config: WranglerConfig, created: string[]): string {
276
+ function provisionKvNamespaces(content: string, config: WranglerConfig, created: string[], namespaceNames: Record<string, string>): string {
260
277
  let patched = content;
261
278
  for (const [index, namespace] of config.kv_namespaces?.entries() ?? []) {
262
279
  if (namespace.id !== DEFAULT_HEX_ID) continue;
263
280
  if (namespace.binding === undefined || namespace.binding === '') {
264
281
  throw new Error(`KV namespace at index ${index} has a placeholder id but no binding.`);
265
282
  }
266
- const resolved = ensureKVNamespace(config, namespace.binding);
267
- const namespaceName = getKVNamespaceName(config, namespace.binding);
283
+ const resolved = ensureKVNamespace(config, namespace.binding, namespaceNames);
284
+ const namespaceName = getKVNamespaceName(config, namespace.binding, namespaceNames);
268
285
  if (resolved.created) {
269
286
  created.push(`kv:${namespaceName}`);
270
287
  }
@@ -302,16 +319,21 @@ function provisionSecretStore(content: string, config: WranglerConfig, created:
302
319
  * Create every resource the config names, and write the real ids over the
303
320
  * placeholders.
304
321
  *
322
+ * `namespaceNames` is the binding → namespace map KV provisioning reconciles
323
+ * against, defaulting to the kit's `DEFAULT_KV_NAMESPACE_NAMES`; a consumer whose
324
+ * namespaces were named before that convention passes its own so the existing
325
+ * namespace is found instead of a second empty one being created.
326
+ *
305
327
  * Returns what it had to create, as `<kind>:<name>` — returned rather than logged,
306
328
  * because a caller acts on it: the backup workflow refuses to export a D1 database
307
329
  * that appears here, since one created moments ago holds no user data and an empty
308
330
  * dump uploaded on schedule is a false sense of safety.
309
331
  */
310
- export function provisionWranglerResources(): string[] {
332
+ export function provisionWranglerResources(namespaceNames: Record<string, string> = DEFAULT_KV_NAMESPACE_NAMES): string[] {
311
333
  const created: string[] = [];
312
334
  let { content, config } = readConfig();
313
335
 
314
- content = ensureRequiredKvBindings(content, config);
336
+ content = ensureRequiredKvBindings(content, config, namespaceNames);
315
337
  config = parse(content) as WranglerConfig;
316
338
 
317
339
  // Each of the three below re-parses before it patches, because each writes into
@@ -322,7 +344,7 @@ export function provisionWranglerResources(): string[] {
322
344
  content = provisionD1Databases(content, config, created);
323
345
  config = parse(content) as WranglerConfig;
324
346
 
325
- content = provisionKvNamespaces(content, config, created);
347
+ content = provisionKvNamespaces(content, config, created, namespaceNames);
326
348
  config = parse(content) as WranglerConfig;
327
349
 
328
350
  content = provisionSecretStore(content, config, created);
@@ -54,8 +54,9 @@ export const DEFAULT_WORKER_NAME = process.env['WRANGLER_WORKER_NAME'] ?? 'my-wo
54
54
  /**
55
55
  * KV bindings a deployment always needs, and the namespace each maps to.
56
56
  *
57
- * A consumer whose worker has no cache binding passes an empty map; a consumer
58
- * with one names it here or leaves the derived `<worker>-<BINDING>` convention.
57
+ * The default for `provisionWranglerResources`'s `namespaceNames` argument: a
58
+ * consumer whose worker has no cache binding passes an empty map, and one whose
59
+ * namespaces predate this convention passes its own map.
59
60
  */
60
61
  export const DEFAULT_KV_NAMESPACE_NAMES: Record<string, string> = {
61
62
  CACHE: `${DEFAULT_WORKER_NAME}-cache`,
package/src/vite.ts CHANGED
@@ -41,6 +41,16 @@ export interface ProxyEntry {
41
41
  readonly target: string;
42
42
  readonly changeOrigin?: boolean;
43
43
  readonly bypass?: (request: { headers: Record<string, string | string[] | undefined> }) => string | undefined;
44
+ /**
45
+ * Proxy WebSocket upgrades on this path.
46
+ *
47
+ * Vite's `server.proxy` needs `ws: true` for an entry that carries a
48
+ * WebSocket, and the entry has no other way to say so: without it the
49
+ * handshake is proxied as an ordinary request and the socket never opens.
50
+ * A consumer with a live-update or realtime endpoint therefore could not
51
+ * express its dev proxy through this table at all.
52
+ */
53
+ readonly ws?: boolean;
44
54
  }
45
55
 
46
56
  /**
@@ -70,6 +80,17 @@ export interface SpaShellTarget {
70
80
  *
71
81
  * Defaults to `dist/index.html` under the same root. Vite's `closeBundle` runs
72
82
  * after the bundle is written, so this is the file the build just produced.
83
+ *
84
+ * A **relative** path resolves against {@link SpaShellTarget.root}, exactly as
85
+ * {@link SpaShellTarget.path} does. It used to resolve against the process's
86
+ * working directory, which under `pnpm --filter … build` is the *package*
87
+ * directory rather than the repository root — so a consumer naming the file
88
+ * relative to the root it had just been given wrote nowhere, and the plugin
89
+ * reported "dist/index.html not found, skipping". That message is also what a
90
+ * genuinely fresh checkout prints on its first build, so the two are
91
+ * indistinguishable in the log, and the worker keeps serving the *previous*
92
+ * shell: the SPA ships yesterday's JavaScript with nothing in the build log
93
+ * to explain it.
73
94
  */
74
95
  readonly distIndex?: string | URL;
75
96
  }
@@ -172,8 +193,17 @@ export function spaShellEmbedPlugin(target: SpaShellTarget): Plugin & { closeBun
172
193
  // `pnpm --filter @scope/web build`, which is how every consumer runs it.
173
194
  const root = toDirUrl(target.root ?? process.cwd());
174
195
  const outputPath = fileURLToPath(new URL(target.path, root));
196
+ // A relative `distIndex` resolves against the root, like `path` does —
197
+ // not against the process's working directory, which under
198
+ // `pnpm --filter … build` is the package directory.
175
199
  const indexPath =
176
- target.distIndex === undefined ? fileURLToPath(new URL('dist/index.html', root)) : fileURLToPath(toUrl(target.distIndex));
200
+ target.distIndex === undefined
201
+ ? fileURLToPath(new URL('dist/index.html', root))
202
+ : fileURLToPath(
203
+ typeof target.distIndex === 'string' && !/^[a-z][a-z\d+\-.]*:/i.test(target.distIndex)
204
+ ? new URL(target.distIndex, root)
205
+ : toUrl(target.distIndex),
206
+ );
177
207
 
178
208
  if (!existsSync(indexPath)) {
179
209
  console.warn('spa-shell-embed: dist/index.html not found, skipping');