workers-start 0.1.1 → 0.2.1
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/CHANGELOG.md +21 -1
- package/README.md +28 -15
- package/biome.preset.json +31 -13
- package/dist/accept.d.ts +18 -0
- package/dist/accept.d.ts.map +1 -0
- package/dist/accept.js +54 -0
- package/dist/accept.js.map +1 -0
- package/dist/check-bundle-cli.d.ts +1 -1
- package/dist/check-bundle-cli.js +50 -25
- package/dist/check-bundle-cli.js.map +1 -1
- package/dist/check-bundle.d.ts +36 -8
- package/dist/check-bundle.d.ts.map +1 -1
- package/dist/check-bundle.js +122 -31
- package/dist/check-bundle.js.map +1 -1
- package/dist/entry.d.ts +40 -18
- package/dist/entry.d.ts.map +1 -1
- package/dist/entry.js +99 -35
- package/dist/entry.js.map +1 -1
- package/dist/head.d.ts +34 -10
- package/dist/head.d.ts.map +1 -1
- package/dist/head.js +46 -26
- package/dist/head.js.map +1 -1
- package/dist/index.d.ts +5 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -2
- package/dist/index.js.map +1 -1
- package/dist/scripts.d.ts +34 -6
- package/dist/scripts.d.ts.map +1 -1
- package/dist/scripts.js +36 -11
- package/dist/scripts.js.map +1 -1
- package/dist/workers.d.ts +6 -4
- package/dist/workers.d.ts.map +1 -1
- package/dist/workers.js +17 -13
- package/dist/workers.js.map +1 -1
- package/package.json +21 -9
- package/src/accept.ts +63 -0
- package/src/check-bundle-cli.ts +57 -23
- package/src/check-bundle.ts +133 -34
- package/src/entry.ts +117 -44
- package/src/head.ts +75 -40
- package/src/index.ts +14 -3
- package/src/scripts.ts +62 -14
- package/src/workers.ts +19 -15
package/CHANGELOG.md
CHANGED
|
@@ -1,8 +1,28 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.2.1 (2026-11-17)
|
|
4
|
+
|
|
5
|
+
- Secret check: `--public-prefix <PREFIX>` marks a family of keys as public, such as `NEXT_PUBLIC_` in an app that kept its Next.js names.
|
|
6
|
+
|
|
7
|
+
## 0.2.0 (2026-11-17)
|
|
8
|
+
|
|
9
|
+
Fixes from a review of 0.1. Several change responses, so compare an app against production after upgrading.
|
|
10
|
+
|
|
11
|
+
- Entry: Accept is parsed with q-values and case-insensitive types. `text/markdown;q=0` and `text/html, text/markdown;q=0.1` now get HTML.
|
|
12
|
+
- Entry: paths are decoded and normalized before every rule, and prefixes match by segment ignoring case. `POST /API/x` and `POST /%61pi/x` were reaching the handler; they now get 405. The default prefix is `/api`, which also covers `/api` itself.
|
|
13
|
+
- Entry: OPTIONS under a read-only prefix gets 204 instead of 405.
|
|
14
|
+
- Entry: with `markdown` set, `Vary: Accept` goes on every page response (redirects included) and is merged, not duplicated.
|
|
15
|
+
- Entry: dotted slugs like `/blog/node.js-tips` are pages; only known file extensions are not. `/@handle` paths are pages.
|
|
16
|
+
- Entry: a renderer that throws falls back to HTML; 204 and 304 results have no body. `markdown()` receives the decoded path.
|
|
17
|
+
- Head: `absoluteUrl` resolves relative paths, protocol-relative and upper-case URLs correctly. New: `image` as an object with type, size and alt; `authors`, `tags`, `keywords`, `robots`, `canonical` (override or `false`), `meta`; `twitterSite`; `twitter:image:alt`.
|
|
18
|
+
- Scripts: values are escaped so none can close the script tag; hosts compare without case. New: `target: { class: true }` and `followSystem` for the theme, `skipPaths` and `consentCookie` for analytics.
|
|
19
|
+
- Workers: `cloudflare:workers` loads under `@cloudflare/vitest-pool-workers`; `afterResponse` logs a failure outside workerd instead of throwing; `isKvNamespace` no longer accepts an R2 bucket.
|
|
20
|
+
- Secret check: dotenv parsing (inline comments, multi-line and escaped values), encoded copies, `.gz`/`.br` files, symlinks skipped, `--skip` by path inside the build, repeatable `--dist`, `--allow`, `VITE_*` ignored, `.tpl` files and `op://` references ignored.
|
|
21
|
+
- Package: `default` export condition and `./package.json`; Node 22 or later; the Biome preset drops a routes override that did nothing.
|
|
22
|
+
|
|
3
23
|
## 0.1.1 (2026-11-17)
|
|
4
24
|
|
|
5
|
-
|
|
25
|
+
The same code as 0.1.0, published from GitHub Actions through trusted publishing with a provenance statement. 0.1.0 was published by hand.
|
|
6
26
|
|
|
7
27
|
## 0.1.0 (2026-11-17)
|
|
8
28
|
|
package/README.md
CHANGED
|
@@ -38,15 +38,22 @@ Point `main` in `wrangler.jsonc` at this file (`"main": "./src/server.ts"`). Poi
|
|
|
38
38
|
|
|
39
39
|
What it does to each request:
|
|
40
40
|
|
|
41
|
-
| Request
|
|
42
|
-
|
|
|
43
|
-
| Page,
|
|
44
|
-
| Page, Accept
|
|
45
|
-
| Any
|
|
46
|
-
|
|
|
47
|
-
|
|
|
48
|
-
|
|
49
|
-
|
|
41
|
+
| Request | Response |
|
|
42
|
+
| ------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
43
|
+
| Page, Accept prefers `text/markdown` | Your `markdown(path)` result as `text/markdown` with `X-Robots-Tag: noindex`. Return a string, `{ body, status }`, or `null` to serve HTML. If it throws, the error is logged and HTML is served. |
|
|
44
|
+
| Page, Accept rules out HTML | HTML, not 406 |
|
|
45
|
+
| Any page, with `markdown` set | `Vary: Accept`, merged into any `Vary` already there, on HTML, redirects and Markdown alike |
|
|
46
|
+
| OPTIONS under `/api` | 204 with `Allow: GET, HEAD, OPTIONS` |
|
|
47
|
+
| Anything but GET, HEAD or OPTIONS under `/api` | 405 with the same `Allow` |
|
|
48
|
+
| Other API requests, files with an extension, `/_…`, Vite dev paths | Passed through untouched |
|
|
49
|
+
|
|
50
|
+
Details that matter when you compare against production:
|
|
51
|
+
|
|
52
|
+
- Accept is parsed with q-values. Markdown is served only when the client names `text/markdown` with q > 0 and rates it at least as high as HTML, so `text/markdown;q=0` and `*/*` both get HTML.
|
|
53
|
+
- Paths are decoded, repeated slashes collapsed and trailing slashes dropped before any test, and prefixes match whole segments ignoring case. `/API/x`, `/%61pi/x` and `//api//x` all count as `/api`; `/apiary` does not. `markdown()` receives that normalized path, so `/learn/caf%C3%A9/` arrives as `/learn/café`.
|
|
54
|
+
- A page is any path whose last segment has no file extension like `.js`, `.xml` or `.txt`. A dotted slug such as `/blog/node.js-tips` is still a page.
|
|
55
|
+
- `readOnlyPrefixes` (default `["/api"]`, `[]` to turn off) and `isPage` change those rules. An API that accepts writes, or that needs CORS headers on preflight, should pass `readOnlyPrefixes: []` and answer those methods itself.
|
|
56
|
+
- Static assets are served before the Worker runs. On a prerendered site, or a static site on Pages, none of this applies to the prerendered pages unless `assets.run_worker_first` sends them through the Worker.
|
|
50
57
|
|
|
51
58
|
## Head tags
|
|
52
59
|
|
|
@@ -70,7 +77,9 @@ export const Route = createFileRoute("/about")({
|
|
|
70
77
|
});
|
|
71
78
|
```
|
|
72
79
|
|
|
73
|
-
It returns the title, description, canonical link, Open Graph tags and Twitter tags. `type: "article"` with `published` and `
|
|
80
|
+
It returns the title, description, canonical link, Open Graph tags and Twitter tags. Optional fields add the rest: `image` as a path or `{ url, width, height, alt, type }`; `type: "article"` with `published`, `modified`, `authors` and `tags`; `keywords`; `robots` (or `noindex: true`); `canonical` to override it or `false` to drop it; and `meta` for anything else, such as an App Store banner. The site config takes `twitterSite` and `twitterCreator`.
|
|
81
|
+
|
|
82
|
+
Call `pageHead` from leaf routes only. The router keeps one meta per name with the deepest route winning, so a tag a leaf leaves out falls back to its parent's, and it concatenates links, so a canonical from a layout and a leaf would appear twice.
|
|
74
83
|
|
|
75
84
|
Two tags with the same name, like the light and dark `theme-color`, can't go through `head()`, because the router keeps only one. Write them straight into the root document.
|
|
76
85
|
|
|
@@ -86,14 +95,16 @@ const key = await getEnvString("API_KEY"); // Worker env, then process.env
|
|
|
86
95
|
await afterResponse(refreshCache()); // waitUntil in workerd, awaited elsewhere
|
|
87
96
|
```
|
|
88
97
|
|
|
89
|
-
In tests, `setWorkersModule({ env, waitUntil })` stands in for the runtime.
|
|
98
|
+
In tests, `setWorkersModule({ env, waitUntil })` stands in for the runtime. Under `@cloudflare/vitest-pool-workers` the real module loads. Outside workerd, a failure in `afterResponse` work is logged rather than thrown, as `waitUntil` would.
|
|
90
99
|
|
|
91
100
|
## Inline scripts
|
|
92
101
|
|
|
93
102
|
`workers-start/scripts` returns strings for `dangerouslySetInnerHTML` in the root document.
|
|
94
103
|
|
|
95
|
-
- `themeScript()` applies a stored `light` or `dark` choice before first paint
|
|
96
|
-
- `analyticsScript({ measurementId, hosts })` loads GA4 only on the listed hostnames, so local dev and preview deploys never report.
|
|
104
|
+
- `themeScript()` applies a stored `light` or `dark` choice before first paint, as a `data-theme` attribute by default or as a class with `target: { class: true }`. With nothing stored it does nothing, and `color-scheme: light dark` follows the OS; `followSystem: true` applies the OS choice instead, for CSS keyed off the class.
|
|
105
|
+
- `analyticsScript({ measurementId, hosts })` loads GA4 only on the listed hostnames, so local dev and preview deploys never report. `skipPaths` keeps it off paths like `/admin`, and `consentCookie` waits for a consent cookie.
|
|
106
|
+
|
|
107
|
+
Every value is embedded with `inlineJson`, which escapes `<`, so no option can close the script tag.
|
|
97
108
|
|
|
98
109
|
## Secret check
|
|
99
110
|
|
|
@@ -103,7 +114,9 @@ Run it between the build and the deploy:
|
|
|
103
114
|
"deploy": "vite build && workers-start-check-bundle && wrangler deploy"
|
|
104
115
|
```
|
|
105
116
|
|
|
106
|
-
It reads every value in your local `.env*` and `.dev.vars*` files and fails if any of them appears in a built file. It
|
|
117
|
+
It reads every value in your local `.env*` and `.dev.vars*` files (not `.example` or `.tpl` templates, and not `op://` references) and fails if any of them appears in a built file. It parses dotenv the way dotenv does, and searches for each value raw, URL-encoded, JSON-escaped and base64-encoded, inside `.gz` and `.br` files too. Symlinks are skipped. `VITE_*` keys are public by design and ignored.
|
|
118
|
+
|
|
119
|
+
It skips `server/.dev.vars` inside the build directory, which wrangler never uploads. Flags: `--dist <dir>` (repeatable; default `dist`, `out` for a static site), `--root <dir>`, `--skip <path inside the build>`, `--allow <KEY>` for another public value, `--public-prefix <PREFIX>` for a whole family of them (for example `NEXT_PUBLIC_` in an app that kept its Next.js variable names).
|
|
107
120
|
|
|
108
121
|
## Biome preset
|
|
109
122
|
|
|
@@ -112,7 +125,7 @@ It reads every value in your local `.env*` and `.dev.vars*` files and fails if a
|
|
|
112
125
|
{ "extends": ["workers-start/biome"] }
|
|
113
126
|
```
|
|
114
127
|
|
|
115
|
-
Biome's recommended rules
|
|
128
|
+
Biome's recommended rules plus `useTopLevelRegex`, Tailwind directives in CSS, and sorted imports and JSX attributes. It turns `noDangerouslySetInnerHtml` off everywhere, because the inline scripts above need it. Biome 2.6 or later.
|
|
116
129
|
|
|
117
130
|
## License
|
|
118
131
|
|
package/biome.preset.json
CHANGED
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"$schema": "https://biomejs.dev/schemas/2.6.8/schema.json",
|
|
3
|
-
"formatter": {
|
|
3
|
+
"formatter": {
|
|
4
|
+
"indentStyle": "space",
|
|
5
|
+
"indentWidth": 2,
|
|
6
|
+
"lineEnding": "lf",
|
|
7
|
+
"lineWidth": 80
|
|
8
|
+
},
|
|
4
9
|
"javascript": {
|
|
5
10
|
"formatter": {
|
|
6
11
|
"arrowParentheses": "always",
|
|
@@ -10,20 +15,33 @@
|
|
|
10
15
|
"trailingCommas": "es5"
|
|
11
16
|
}
|
|
12
17
|
},
|
|
13
|
-
"json": {
|
|
14
|
-
|
|
15
|
-
|
|
18
|
+
"json": {
|
|
19
|
+
"formatter": {
|
|
20
|
+
"trailingCommas": "none"
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
"css": {
|
|
24
|
+
"parser": {
|
|
25
|
+
"tailwindDirectives": true
|
|
26
|
+
}
|
|
27
|
+
},
|
|
28
|
+
"assist": {
|
|
29
|
+
"actions": {
|
|
30
|
+
"source": {
|
|
31
|
+
"organizeImports": "on",
|
|
32
|
+
"useSortedAttributes": "on"
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
},
|
|
16
36
|
"linter": {
|
|
17
37
|
"rules": {
|
|
18
38
|
"preset": "recommended",
|
|
19
|
-
"security": {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
"includes": ["**/src/routes/**"],
|
|
26
|
-
"linter": { "rules": { "style": { "useFilenamingConvention": "off" } } }
|
|
39
|
+
"security": {
|
|
40
|
+
"noDangerouslySetInnerHtml": "off"
|
|
41
|
+
},
|
|
42
|
+
"performance": {
|
|
43
|
+
"useTopLevelRegex": "error"
|
|
44
|
+
}
|
|
27
45
|
}
|
|
28
|
-
|
|
46
|
+
}
|
|
29
47
|
}
|
package/dist/accept.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Accept header parsing with q-values, per RFC 9110 section 12.5.1.
|
|
3
|
+
*/
|
|
4
|
+
export interface MediaRange {
|
|
5
|
+
type: string;
|
|
6
|
+
subtype: string;
|
|
7
|
+
q: number;
|
|
8
|
+
}
|
|
9
|
+
/** Media ranges from an Accept header, lowercased. An empty or missing header means `*\/*`. */
|
|
10
|
+
export declare function parseAccept(header: string | null | undefined): MediaRange[];
|
|
11
|
+
/**
|
|
12
|
+
* The quality a client gives `mediaType`, from the most specific range that
|
|
13
|
+
* matches it. 0 when nothing matches or the match says q=0.
|
|
14
|
+
*/
|
|
15
|
+
export declare function quality(ranges: readonly MediaRange[], mediaType: string, options?: {
|
|
16
|
+
exactOnly?: boolean;
|
|
17
|
+
}): number;
|
|
18
|
+
//# sourceMappingURL=accept.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"accept.d.ts","sourceRoot":"","sources":["../src/accept.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,MAAM,WAAW,UAAU;IACzB,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,MAAM,CAAC;IAChB,CAAC,EAAE,MAAM,CAAC;CACX;AAED,+FAA+F;AAC/F,wBAAgB,WAAW,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,GAAG,UAAU,EAAE,CAsB3E;AAED;;;GAGG;AACH,wBAAgB,OAAO,CACrB,MAAM,EAAE,SAAS,UAAU,EAAE,EAC7B,SAAS,EAAE,MAAM,EACjB,OAAO,GAAE;IAAE,SAAS,CAAC,EAAE,OAAO,CAAA;CAAO,GACpC,MAAM,CAmBR"}
|
package/dist/accept.js
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Accept header parsing with q-values, per RFC 9110 section 12.5.1.
|
|
3
|
+
*/
|
|
4
|
+
/** Media ranges from an Accept header, lowercased. An empty or missing header means `*\/*`. */
|
|
5
|
+
export function parseAccept(header) {
|
|
6
|
+
if (!header?.trim()) {
|
|
7
|
+
return [{ type: "*", subtype: "*", q: 1 }];
|
|
8
|
+
}
|
|
9
|
+
const ranges = [];
|
|
10
|
+
for (const part of header.split(",")) {
|
|
11
|
+
const [range = "", ...params] = part.split(";").map((s) => s.trim());
|
|
12
|
+
const [type, subtype] = range.toLowerCase().split("/");
|
|
13
|
+
if (!(type && subtype)) {
|
|
14
|
+
continue;
|
|
15
|
+
}
|
|
16
|
+
let q = 1;
|
|
17
|
+
for (const param of params) {
|
|
18
|
+
const [key, value] = param.split("=").map((s) => s.trim());
|
|
19
|
+
if (key?.toLowerCase() === "q" && value !== undefined) {
|
|
20
|
+
const parsed = Number(value);
|
|
21
|
+
q = Number.isFinite(parsed) ? Math.min(Math.max(parsed, 0), 1) : 0;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
ranges.push({ type, subtype, q });
|
|
25
|
+
}
|
|
26
|
+
return ranges;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* The quality a client gives `mediaType`, from the most specific range that
|
|
30
|
+
* matches it. 0 when nothing matches or the match says q=0.
|
|
31
|
+
*/
|
|
32
|
+
export function quality(ranges, mediaType, options = {}) {
|
|
33
|
+
const [type, subtype] = mediaType.toLowerCase().split("/");
|
|
34
|
+
let best = -1;
|
|
35
|
+
let q = 0;
|
|
36
|
+
for (const range of ranges) {
|
|
37
|
+
let specificity = -1;
|
|
38
|
+
if (range.type === type && range.subtype === subtype) {
|
|
39
|
+
specificity = 2;
|
|
40
|
+
}
|
|
41
|
+
else if (!options.exactOnly && range.type === type && range.subtype === "*") {
|
|
42
|
+
specificity = 1;
|
|
43
|
+
}
|
|
44
|
+
else if (!options.exactOnly && range.type === "*" && range.subtype === "*") {
|
|
45
|
+
specificity = 0;
|
|
46
|
+
}
|
|
47
|
+
if (specificity > best) {
|
|
48
|
+
best = specificity;
|
|
49
|
+
q = range.q;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
return q;
|
|
53
|
+
}
|
|
54
|
+
//# sourceMappingURL=accept.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"accept.js","sourceRoot":"","sources":["../src/accept.ts"],"names":[],"mappings":"AAAA;;GAEG;AAQH,+FAA+F;AAC/F,MAAM,UAAU,WAAW,CAAC,MAAiC;IAC3D,IAAI,CAAC,MAAM,EAAE,IAAI,EAAE,EAAE,CAAC;QACpB,OAAO,CAAC,EAAE,IAAI,EAAE,GAAG,EAAE,OAAO,EAAE,GAAG,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC;IAC7C,CAAC;IACD,MAAM,MAAM,GAAiB,EAAE,CAAC;IAChC,KAAK,MAAM,IAAI,IAAI,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,EAAE,CAAC;QACrC,MAAM,CAAC,KAAK,GAAG,EAAE,EAAE,GAAG,MAAM,CAAC,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC,CAAC;QACrE,MAAM,CAAC,IAAI,EAAE,OAAO,CAAC,GAAG,KAAK,CAAC,WAAW,EAAE,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;QACvD,IAAI,CAAC,C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AAO,KAAK,OAAO,EAAE,CAAC;YACrD,WAAW,GAAG,CAAC,CAAC;QAClB,CAAC;aAAM,IAAI,CAAC,OAAO,CAAC,SAAS,IAAI,KAAK,CAAC,IAAI,KAAK,IAAI,IAAI,KAAK,CAAC,OAAO,KAAK,GAAG,EAAE,CAAC;YAC9E,WAAW,GAAG,CAAC,CAAC;QAClB,CAAC;aAAM,IAAI,CAAC,OAAO,CAAC,SAAS,IAAI,KAAK,CAAC,IAAI,KAAK,GAAG,IAAI,KAAK,CAAC,OAAO,KAAK,GAAG,EAAE,CAAC;YAC7E,WAAW,GAAG,CAAC,CAAC;QAClB,CAAC;QACD,IAAI,WAAW,GAAG,IAAI,EAAE,CAAC;YACvB,IAAI,GAAG,WAAW,CAAC;YACnB,CAAC,GAAG,KAAK,CAAC,CAAC,CAAC;QACd,CAAC;IACH,CAAC;IACD,OAAO,CAAC,CAAC;AACX,CAAC"}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
/**
|
|
3
|
-
* workers-start-check-bundle [--dist
|
|
3
|
+
* workers-start-check-bundle [--dist dir ...] [--root dir] [--skip path ...] [--allow KEY ...] [--public-prefix PREFIX ...]
|
|
4
4
|
*
|
|
5
5
|
* Exits 1 if any local env value appears in a built file. Run it between the
|
|
6
6
|
* build and the deploy.
|
package/dist/check-bundle-cli.js
CHANGED
|
@@ -1,45 +1,69 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
/**
|
|
3
|
-
* workers-start-check-bundle [--dist
|
|
3
|
+
* workers-start-check-bundle [--dist dir ...] [--root dir] [--skip path ...] [--allow KEY ...] [--public-prefix PREFIX ...]
|
|
4
4
|
*
|
|
5
5
|
* Exits 1 if any local env value appears in a built file. Run it between the
|
|
6
6
|
* build and the deploy.
|
|
7
7
|
*/
|
|
8
8
|
import { relative, resolve } from "node:path";
|
|
9
|
-
import { checkBundle, DEFAULT_SKIP } from "./check-bundle.js";
|
|
9
|
+
import { checkBundle, DEFAULT_PUBLIC_PREFIXES, DEFAULT_SKIP } from "./check-bundle.js";
|
|
10
|
+
const USAGE = `Usage: workers-start-check-bundle [options]
|
|
11
|
+
|
|
12
|
+
--dist <dir> Build directory to scan, relative to --root. Repeatable. Default: dist
|
|
13
|
+
--root <dir> Directory holding the .env and .dev.vars files. Default: .
|
|
14
|
+
--skip <path> Path inside a build directory that never ships. Repeatable.
|
|
15
|
+
Always skipped: ${DEFAULT_SKIP.join(", ")}
|
|
16
|
+
--allow <KEY> Env key whose value is public. Repeatable.
|
|
17
|
+
--public-prefix <PREFIX>
|
|
18
|
+
Key prefix whose values are public, such as NEXT_PUBLIC_. Repeatable.
|
|
19
|
+
Always public: ${DEFAULT_PUBLIC_PREFIXES.join(", ")}`;
|
|
10
20
|
function parseArgs(argv) {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
21
|
+
const args = {
|
|
22
|
+
dists: [],
|
|
23
|
+
root: ".",
|
|
24
|
+
skip: [...DEFAULT_SKIP],
|
|
25
|
+
allow: [],
|
|
26
|
+
publicPrefixes: [...DEFAULT_PUBLIC_PREFIXES],
|
|
27
|
+
};
|
|
14
28
|
for (let i = 0; i < argv.length; i++) {
|
|
15
29
|
const flag = argv[i];
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
}
|
|
20
|
-
if (flag === "--dist") {
|
|
21
|
-
dist = value;
|
|
22
|
-
i++;
|
|
23
|
-
}
|
|
24
|
-
else if (flag === "--root") {
|
|
25
|
-
root = value;
|
|
26
|
-
i++;
|
|
30
|
+
if (flag === "--help" || flag === "-h") {
|
|
31
|
+
console.log(USAGE);
|
|
32
|
+
process.exit(0);
|
|
27
33
|
}
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
34
|
+
const value = argv[i + 1];
|
|
35
|
+
if (!(flag === "--dist" || flag === "--root" || flag === "--skip" || flag === "--allow" || flag === "--public-prefix")) {
|
|
36
|
+
throw new Error(`Unknown argument: ${flag}\n\n${USAGE}`);
|
|
31
37
|
}
|
|
32
|
-
|
|
33
|
-
throw new Error(
|
|
38
|
+
if (value === undefined || value.startsWith("--")) {
|
|
39
|
+
throw new Error(`${flag} needs a value`);
|
|
34
40
|
}
|
|
41
|
+
i++;
|
|
42
|
+
if (flag === "--dist")
|
|
43
|
+
args.dists.push(value);
|
|
44
|
+
else if (flag === "--root")
|
|
45
|
+
args.root = value;
|
|
46
|
+
else if (flag === "--skip")
|
|
47
|
+
args.skip.push(value.replace(/^\.?\//, ""));
|
|
48
|
+
else if (flag === "--allow")
|
|
49
|
+
args.allow.push(value);
|
|
50
|
+
else
|
|
51
|
+
args.publicPrefixes.push(value);
|
|
52
|
+
}
|
|
53
|
+
if (args.dists.length === 0) {
|
|
54
|
+
args.dists.push("dist");
|
|
35
55
|
}
|
|
36
|
-
return
|
|
56
|
+
return args;
|
|
37
57
|
}
|
|
38
58
|
function main() {
|
|
39
59
|
const args = parseArgs(process.argv.slice(2));
|
|
40
60
|
const root = resolve(args.root);
|
|
41
|
-
const
|
|
42
|
-
const result = checkBundle(root,
|
|
61
|
+
const dists = args.dists.map((dist) => resolve(root, dist));
|
|
62
|
+
const result = checkBundle(root, dists, {
|
|
63
|
+
skip: args.skip,
|
|
64
|
+
allow: args.allow,
|
|
65
|
+
publicPrefixes: args.publicPrefixes,
|
|
66
|
+
});
|
|
43
67
|
if (result.leaked.length === 0) {
|
|
44
68
|
console.log(` ok none of ${result.envValues} local env value(s) appear in ${result.files} built file(s)`);
|
|
45
69
|
return;
|
|
@@ -48,7 +72,8 @@ function main() {
|
|
|
48
72
|
for (const key of result.leaked) {
|
|
49
73
|
console.error(` ${key}`);
|
|
50
74
|
}
|
|
51
|
-
|
|
75
|
+
const where = dists.map((dist) => `${relative(process.cwd(), dist) || "."}/`).join(", ");
|
|
76
|
+
console.error(`\n Find where it is read at build time (import.meta.env, define, a module-scope\n process.env read) under ${where} and fix that. If the value is meant to be\n public, pass --allow <KEY> or --public-prefix <PREFIX>.\n`);
|
|
52
77
|
process.exit(1);
|
|
53
78
|
}
|
|
54
79
|
try {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"check-bundle-cli.js","sourceRoot":"","sources":["../src/check-bundle-cli.ts"],"names":[],"mappings":";AACA;;;;;GAKG;AAEH,OAAO,EAAE,QAAQ,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAC9C,OAAO,EAAE,WAAW,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;
|
|
1
|
+
{"version":3,"file":"check-bundle-cli.js","sourceRoot":"","sources":["../src/check-bundle-cli.ts"],"names":[],"mappings":";AACA;;;;;GAKG;AAEH,OAAO,EAAE,QAAQ,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAC9C,OAAO,EAAE,WAAW,EAAE,uBAAuB,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AAEvF,MAAM,KAAK,GAAG;;;;;mCAKqB,YAAY,CAAC,IAAI,CAAC,IAAI,CAAC;;;;kCAIxB,uBAAuB,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC;AAUvE,SAAS,SAAS,CAAC,IAAc;IAC/B,MAAM,IAAI,GAAS;QACjB,KAAK,EAAE,EAAE;QACT,IAAI,EAAE,GAAG;QACT,IAAI,EAAE,CAAC,GAAG,YAAY,CAAC;QACvB,KAAK,EAAE,EAAE;QACT,cAAc,EAAE,CAAC,GAAG,uBAAuB,CAAC;KAC7C,CAAC;IACF,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,IAAI,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;QACrC,MAAM,IAAI,GAAG,IAAI,CAAC,CAAC,CAAC,CAAC;QACrB,IAAI,IAAI,KAAK,QAAQ,IAAI,IAAI,KAAK,IAAI,EAAE,CAAC;YACvC,OAAO,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;YACnB,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;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AAC;IACf,OAAO,CAAC,KAAK,CAAC,OAAO,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;IACjF,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;AAClB,CAAC"}
|
package/dist/check-bundle.d.ts
CHANGED
|
@@ -1,26 +1,54 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Finds local secrets compiled into a build.
|
|
3
3
|
*
|
|
4
|
-
* Reads every value in the local `.env*` and `.dev.vars*` files (examples
|
|
5
|
-
* excluded) and searches the built files for each one
|
|
4
|
+
* Reads every value in the local `.env*` and `.dev.vars*` files (examples and
|
|
5
|
+
* `.tpl` templates excluded) and searches the built files for each one, raw
|
|
6
|
+
* and in the encodings a bundler or a URL would give it. A match means a key
|
|
6
7
|
* reached something that ships, whichever tool put it there.
|
|
7
8
|
*/
|
|
8
9
|
/** Values shorter than this are flags or placeholders, not secrets. */
|
|
9
10
|
export declare const MIN_SECRET_LENGTH = 12;
|
|
10
|
-
/**
|
|
11
|
+
/**
|
|
12
|
+
* Paths, relative to a build directory, that are never uploaded: the copy of
|
|
13
|
+
* the local env the Cloudflare Vite plugin writes for `vite preview`.
|
|
14
|
+
*/
|
|
11
15
|
export declare const DEFAULT_SKIP: string[];
|
|
12
|
-
/**
|
|
16
|
+
/** Keys Vite inlines on purpose; their values are public. */
|
|
17
|
+
export declare const DEFAULT_PUBLIC_PREFIXES: string[];
|
|
18
|
+
/**
|
|
19
|
+
* KEY=value pairs from a dotenv file, parsed the way dotenv does: `#`
|
|
20
|
+
* comments, inline ` #` comments on unquoted values, single quotes taken
|
|
21
|
+
* literally, double quotes with `\n` escapes and multi-line values.
|
|
22
|
+
*/
|
|
13
23
|
export declare function parseEnvFile(source: string): [string, string][];
|
|
14
|
-
/** The
|
|
15
|
-
export declare function
|
|
24
|
+
/** The forms a value can take once it is in a bundle or a URL. */
|
|
25
|
+
export declare function encodings(value: string): string[];
|
|
26
|
+
export interface LeakOptions {
|
|
27
|
+
/** Keys whose values are public. Matched exactly. */
|
|
28
|
+
allow?: readonly string[];
|
|
29
|
+
/** Key prefixes whose values are public. Default `["VITE_"]`. */
|
|
30
|
+
publicPrefixes?: readonly string[];
|
|
31
|
+
}
|
|
32
|
+
/** The env keys whose values appear, in any encoding, in any of the given file contents. */
|
|
33
|
+
export declare function leakedKeys(pairs: [string, string][], contents: string[], options?: LeakOptions): string[];
|
|
16
34
|
/** Every KEY=value pair in the local env files of `dir`. */
|
|
17
35
|
export declare function localEnvPairs(dir: string): [string, string][];
|
|
18
|
-
/**
|
|
36
|
+
/**
|
|
37
|
+
* Every regular file under `dir`, recursively. Symlinks are skipped, so the
|
|
38
|
+
* walk never loops or leaves the directory. `skip` holds paths relative to
|
|
39
|
+
* `dir`, with forward slashes.
|
|
40
|
+
*/
|
|
19
41
|
export declare function listFiles(dir: string, skip?: readonly string[]): string[];
|
|
42
|
+
/** File contents as text, decompressing `.gz` and `.br` files. */
|
|
43
|
+
export declare function readBuilt(path: string): string;
|
|
20
44
|
export interface CheckResult {
|
|
21
45
|
envValues: number;
|
|
22
46
|
files: number;
|
|
23
47
|
leaked: string[];
|
|
24
48
|
}
|
|
25
|
-
export
|
|
49
|
+
export interface CheckOptions extends LeakOptions {
|
|
50
|
+
skip?: readonly string[];
|
|
51
|
+
}
|
|
52
|
+
/** Scan one or more build directories against the local env files in `root`. */
|
|
53
|
+
export declare function checkBundle(root: string, dists: string | readonly string[], options?: CheckOptions): CheckResult;
|
|
26
54
|
//# sourceMappingURL=check-bundle.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"check-bundle.d.ts","sourceRoot":"","sources":["../src/check-bundle.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"check-bundle.d.ts","sourceRoot":"","sources":["../src/check-bundle.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAMH,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