Repository navigation
Commit 641a3fe
protect: keep the runtime edge-safe (load Node fs lazily) (#117)
* protect: keep the runtime edge-safe (load Node fs lazily)
The protect runtime is what an EDGE guard imports — the Next edge-middleware
template, Cloudflare Workers, Deno, Bun, Supabase Functions. But runtime.js and
rules/store.js imported `node:fs`/`node:path` STATICALLY, and the bundler rewrites
those to bare `fs`/`path`, which don't resolve on an edge runtime at all: the built
dist/protect.js could fail the build or leave the guard unavailable on exactly the
platforms we advertise support for.
Both filesystem uses are Node-only conveniences and were already fail-open, so
load them with a dynamic `await import(…)` instead:
- rules/store.js: the disk cache tier (memoized loader; on a filesystem-less
runtime it reports "no cache" and the memory / pluggable `ruleCache` tiers carry
last-known-good).
- runtime.js: the `.patchstackrc.json` api-key fallback (resolveApiKey is now async;
createProtection already awaited).
Adds tests/protect/edge-safe.test.ts to pin the invariant — no static Node-builtin
import anywhere in the runtime graph, and none in the built bundle. The Node disk
cache / last-known-good tests still pass unchanged.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
* test: only assert on dist when the build is fresh
The dist assertion could fail spuriously against a STALE bundle left by a build on
another branch (CI tests before building, so it skips there and never ran). Gate it
on dist being newer than the newest source file; the source-graph assertion is the
real invariant and always runs.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>1 parent 81e590d commit 641a3fe
3 files changed
Lines changed: 97 additions & 17 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
8 | | - | |
9 | | - | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
10 | 31 | | |
11 | 32 | | |
12 | 33 | | |
| |||
54 | 75 | | |
55 | 76 | | |
56 | 77 | | |
57 | | - | |
58 | | - | |
59 | | - | |
60 | | - | |
61 | | - | |
| 78 | + | |
62 | 79 | | |
| 80 | + | |
| 81 | + | |
63 | 82 | | |
64 | | - | |
65 | | - | |
| 83 | + | |
| 84 | + | |
66 | 85 | | |
67 | 86 | | |
68 | 87 | | |
69 | 88 | | |
70 | 89 | | |
71 | | - | |
| 90 | + | |
72 | 91 | | |
| 92 | + | |
| 93 | + | |
73 | 94 | | |
74 | | - | |
| 95 | + | |
75 | 96 | | |
76 | 97 | | |
77 | 98 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
24 | 24 | | |
25 | 25 | | |
26 | 26 | | |
27 | | - | |
28 | | - | |
29 | 27 | | |
30 | 28 | | |
31 | 29 | | |
| |||
72 | 70 | | |
73 | 71 | | |
74 | 72 | | |
75 | | - | |
| 73 | + | |
76 | 74 | | |
77 | 75 | | |
78 | 76 | | |
| |||
580 | 578 | | |
581 | 579 | | |
582 | 580 | | |
583 | | - | |
584 | | - | |
| 581 | + | |
| 582 | + | |
| 583 | + | |
| 584 | + | |
| 585 | + | |
| 586 | + | |
| 587 | + | |
585 | 588 | | |
586 | 589 | | |
587 | 590 | | |
588 | 591 | | |
589 | 592 | | |
590 | 593 | | |
591 | 594 | | |
| 595 | + | |
592 | 596 | | |
593 | 597 | | |
594 | 598 | | |
595 | 599 | | |
596 | 600 | | |
597 | | - | |
| 601 | + | |
598 | 602 | | |
599 | 603 | | |
600 | 604 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
0 commit comments