@jimhoyd/urlcode 0.4.0-alpha.1 → 0.4.0-alpha.2

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 (191) hide show
  1. package/.claude/skills/urlcode-authoring/SKILL.md +24 -8
  2. package/.claude/skills/urlcode-operations/SKILL.md +10 -16
  3. package/.claude-plugin/marketplace.json +1 -1
  4. package/README.md +75 -62
  5. package/ROADMAP.md +61 -16
  6. package/SECURITY.md +23 -8
  7. package/dist/BUILD-MANIFEST.json +38 -43
  8. package/dist/agents-guide.js +18 -18
  9. package/dist/build-static.js +134 -0
  10. package/dist/capabilities.js +85 -31
  11. package/dist/capability-query.js +0 -1
  12. package/dist/cli.js +23 -31
  13. package/dist/compliance-rules/baseline.js +2 -10
  14. package/dist/compliance-rules/privacy.js +5 -16
  15. package/dist/compliance-rules/shared.js +0 -2
  16. package/dist/compliance.js +6 -8
  17. package/dist/config.js +15 -8
  18. package/dist/context.js +9 -10
  19. package/dist/examples.js +2 -2
  20. package/dist/explain-cli.js +3 -3
  21. package/dist/explain.js +5 -6
  22. package/dist/extensions.js +61 -2
  23. package/dist/function-sources.js +34 -2
  24. package/dist/function-worker.js +3 -1
  25. package/dist/functions.js +84 -13
  26. package/dist/guest-api.js +29 -3
  27. package/dist/index.js +3 -6
  28. package/dist/manifest.js +5 -7
  29. package/dist/mcp.js +1 -1
  30. package/dist/observability.js +1 -21
  31. package/dist/policies/cache.js +8 -3
  32. package/dist/policy.js +28 -9
  33. package/dist/project-tests.js +3 -3
  34. package/dist/readiness.js +32 -9
  35. package/dist/router.js +5 -7
  36. package/dist/runtime.js +77 -58
  37. package/dist/sandbox.js +48 -0
  38. package/dist/scaffold.js +0 -0
  39. package/dist/scripts/operational-drills.js +12 -54
  40. package/dist/server.js +3 -29
  41. package/dist/tooling.js +1 -1
  42. package/dist/trusted-functions.js +210 -0
  43. package/dist/types/build-static.d.ts +43 -0
  44. package/dist/types/capabilities.d.ts +14 -5
  45. package/dist/types/compliance-rules/shared.d.ts +0 -2
  46. package/dist/types/compliance.d.ts +0 -3
  47. package/dist/types/config.d.ts +2 -1
  48. package/dist/types/context.d.ts +2 -1
  49. package/dist/types/examples.d.ts +1 -1
  50. package/dist/types/extensions.d.ts +55 -0
  51. package/dist/types/function-sources.d.ts +4 -0
  52. package/dist/types/functions.d.ts +48 -5
  53. package/dist/types/guest-api.d.ts +1 -0
  54. package/dist/types/index.d.ts +3 -6
  55. package/dist/types/manifest.d.ts +1 -3
  56. package/dist/types/observability.d.ts +1 -14
  57. package/dist/types/project-tests.d.ts +1 -2
  58. package/dist/types/readiness.d.ts +10 -3
  59. package/dist/types/router.d.ts +2 -1
  60. package/dist/types/runtime.d.ts +0 -27
  61. package/dist/types/sandbox.d.ts +12 -0
  62. package/dist/types/scaffold.d.ts +0 -2
  63. package/dist/types/server.d.ts +1 -4
  64. package/dist/types/tooling.d.ts +3 -3
  65. package/dist/types/trusted-functions.d.ts +29 -0
  66. package/dist/types/types.d.ts +14 -7
  67. package/dist/types/verify-deployment.d.ts +2 -2
  68. package/dist/types.js +17 -4
  69. package/dist/typescript-authoring.js +55 -17
  70. package/dist/verify-deployment.js +3 -3
  71. package/docs/AI-AUTHORING.md +104 -9
  72. package/docs/AWS.md +4 -4
  73. package/docs/BEST-PRACTICES.md +3 -2
  74. package/docs/CAPABILITIES.md +29 -16
  75. package/docs/CAPACITY.md +128 -31
  76. package/docs/CLOUDFLARE.md +1 -2
  77. package/docs/COMPLIANCE.md +6 -9
  78. package/docs/DEPLOYMENT-CHECKS.md +1 -1
  79. package/docs/EXTENSIONS.md +183 -11
  80. package/docs/FRAMEWORK.md +35 -19
  81. package/docs/FUNCTION-SECURITY.md +129 -32
  82. package/docs/INSTALL.md +0 -5
  83. package/docs/LOAD-TESTING.md +4 -4
  84. package/docs/MIDDLEWARE.md +27 -16
  85. package/docs/MONITORING.md +2 -19
  86. package/docs/NEXT-PHASE-PLAN.md +12 -4
  87. package/docs/NEXT-STEPS.md +259 -40
  88. package/docs/OBSERVABILITY.md +7 -16
  89. package/docs/OPEN-DECISIONS.md +212 -0
  90. package/docs/OPERATIONAL-PROOF.md +26 -30
  91. package/docs/OPERATIONS.md +23 -32
  92. package/docs/POLICIES.md +15 -4
  93. package/docs/PRERENDER.md +2 -2
  94. package/docs/PROJECT-DIRECTION.md +34 -9
  95. package/docs/README.md +13 -11
  96. package/docs/RELEASE-READINESS.md +49 -29
  97. package/docs/RELEASE-SECURITY.md +89 -3
  98. package/docs/RESILIENCE.md +16 -15
  99. package/docs/ROUTING.md +8 -10
  100. package/docs/SANDBOX-REVIEW.md +19 -6
  101. package/docs/SCAFFOLDING.md +0 -2
  102. package/docs/SECURITY-AUDIT.md +40 -0
  103. package/docs/SPECIFICATION.md +60 -26
  104. package/docs/SPIKE-AI-FRAMEWORK-BENCHMARK.md +287 -0
  105. package/docs/SPIKE-BUSINESS-SUITE.md +8 -0
  106. package/docs/SPIKE-CORE-LAYERING.md +337 -0
  107. package/docs/SPIKE-DEFAULT-TRUST-MODEL.md +209 -0
  108. package/docs/SPIKE-EXTENSION-MODEL.md +2 -2
  109. package/docs/SPIKE-EXTENSIONS.md +5 -0
  110. package/docs/SPIKE-LAMBDA-COMPILE.md +4 -2
  111. package/docs/SPIKE-MONOREPO.md +322 -0
  112. package/docs/STARTERS.md +1 -1
  113. package/docs/STATIC.md +94 -0
  114. package/docs/TOOLING.md +13 -9
  115. package/docs/TUNNELS.md +0 -3
  116. package/docs/TYPESCRIPT-AUTHORING.md +27 -12
  117. package/docs/TYPESCRIPT.md +25 -4
  118. package/docs/USABILITY-REVIEW.md +7 -1
  119. package/docs/VERCEL.md +4 -5
  120. package/docs/VERSION-ALIGNMENT.md +205 -0
  121. package/docs/YAML-GUIDE.md +0 -3
  122. package/docs/YAML-REFERENCE.md +16 -6
  123. package/docs/policies/cache.md +13 -0
  124. package/examples/assets/example.yaml +1 -1
  125. package/examples/aws/example.yaml +1 -1
  126. package/examples/cloudflare/example.yaml +1 -1
  127. package/examples/conditions/example.yaml +1 -1
  128. package/examples/cookbook/example.yaml +1 -1
  129. package/examples/cookbook/middleware/bucket.mjs +12 -2
  130. package/examples/cookbook/middleware/locale.mjs +7 -3
  131. package/examples/egress/example.yaml +1 -1
  132. package/examples/extensions/example.yaml +1 -1
  133. package/examples/prerender/README.md +2 -2
  134. package/examples/prerender/example.yaml +1 -1
  135. package/examples/provider-conformance/example.yaml +1 -1
  136. package/examples/vercel/example.yaml +1 -1
  137. package/llms-full.txt +594 -219
  138. package/llms.txt +23 -12
  139. package/package.json +10 -3
  140. package/packaging/claude-plugin/.claude-plugin/plugin.json +1 -1
  141. package/packaging/claude-plugin/skills/urlcode-authoring/SKILL.md +24 -8
  142. package/packaging/claude-plugin/skills/urlcode-operations/SKILL.md +10 -16
  143. package/recipes/authenticated-json-api/recipe.yaml +1 -1
  144. package/recipes/contact-form/recipe.yaml +1 -1
  145. package/recipes/cors-api/recipe.yaml +1 -1
  146. package/recipes/health-page/recipe.yaml +1 -1
  147. package/recipes/json-api/recipe.yaml +1 -1
  148. package/recipes/middleware/middleware/bucket.mjs +12 -2
  149. package/recipes/middleware/middleware/locale.mjs +7 -3
  150. package/recipes/middleware/recipe.yaml +1 -1
  151. package/recipes/protected-download/recipe.yaml +1 -1
  152. package/recipes/redirect/recipe.yaml +1 -1
  153. package/recipes/static-plus-api/recipe.yaml +1 -1
  154. package/recipes/typescript/README.md +2 -1
  155. package/recipes/typescript/recipe.yaml +1 -1
  156. package/recipes/webhook-receiver/README.md +5 -1
  157. package/recipes/webhook-receiver/recipe.yaml +2 -1
  158. package/recipes/webhook-receiver/urlcode.yaml +7 -0
  159. package/schemas/recipe.schema.json +4 -3
  160. package/schemas/urlcode.schema.json +14 -41
  161. package/skills/urlcode/SKILL.md +29 -8
  162. package/starters/default/AGENTS.md +18 -18
  163. package/starters/default/urlcode.yaml +0 -1
  164. package/dist/link-api.js +0 -136
  165. package/dist/link-cli.js +0 -141
  166. package/dist/link-events.js +0 -76
  167. package/dist/link-records.js +0 -31
  168. package/dist/link-store-worker.js +0 -150
  169. package/dist/link-store.js +0 -250
  170. package/dist/management-policy.js +0 -40
  171. package/dist/sqlite-version.js +0 -6
  172. package/dist/types/link-api.d.ts +0 -30
  173. package/dist/types/link-cli.d.ts +0 -37
  174. package/dist/types/link-events.d.ts +0 -27
  175. package/dist/types/link-records.d.ts +0 -11
  176. package/dist/types/link-store-worker.d.ts +0 -1
  177. package/dist/types/link-store.d.ts +0 -130
  178. package/dist/types/management-policy.d.ts +0 -8
  179. package/dist/types/sqlite-version.d.ts +0 -1
  180. package/docs/DYNAMIC-LINKS.md +0 -61
  181. package/docs/MANAGEMENT-SECURITY.md +0 -82
  182. package/docs/links/cli.md +0 -110
  183. package/docs/links/limits.md +0 -175
  184. package/docs/links/management-api.md +0 -80
  185. package/docs/links/pools.md +0 -75
  186. package/docs/links/setup.md +0 -135
  187. package/docs/yaml/links.md +0 -30
  188. package/examples/live-links/README.md +0 -11
  189. package/examples/live-links/example.yaml +0 -21
  190. package/examples/live-links/tests/requests.json +0 -6
  191. package/examples/live-links/urlcode.yaml +0 -16
@@ -1,6 +1,6 @@
1
1
  ---
2
2
  name: urlcode
3
- description: Work on a URLCode project, recognized by a urlcode.yaml file with version "1" and served by the @jimhoyd/urlcode runtime. Use this skill whenever a task touches urlcode.yaml, its included route files, functions or middleware under a URLCode project, or asks to add, change, test or deploy URL behavior (redirects, responses, pages, files, functions, live links, policies) in such a project. It teaches the authoring loop and how to retrieve the minimum reference from the installed runtime instead of guessing fields.
3
+ description: Work on a URLCode project, recognized by a urlcode.yaml file with version "1" and served by the @jimhoyd/urlcode runtime. Use this skill whenever a task touches urlcode.yaml, its included route files, functions or middleware under a URLCode project, or asks to add, change, test or deploy URL behavior (redirects, responses, pages, files, functions, policies) in such a project. It teaches the authoring loop and how to retrieve the minimum reference from the installed runtime instead of guessing fields.
4
4
  ---
5
5
 
6
6
  # URLCode authoring loop
@@ -10,6 +10,15 @@ it. Your job is to change the declaration and the minimal guest code it names,
10
10
  then prove the change with the runtime's own checks. Never reimplement what the
11
11
  runtime provides, and never invent fields.
12
12
 
13
+ ## Declarative-first default
14
+
15
+ > Use URLCode's highest-level declarative features whenever possible. Generate custom code only when the framework cannot express the requirement.
16
+
17
+ Check the installed version's primitives, YAML configuration, policies, supported
18
+ extensions and recipes/templates before writing a custom function or middleware.
19
+ Keep necessary custom code focused and report the capability gap; never invent
20
+ fields or bypass target limits or operator grants. See `docs/PROJECT-DIRECTION.md` in the installed runtime.
21
+
13
22
  ## 1. Recognize the project
14
23
 
15
24
  - The root has `urlcode.yaml` with `version: "1"`. Included route files are
@@ -52,18 +61,30 @@ installed package also ships `docs/YAML-REFERENCE.md` (generated field
52
61
  inventory) and `schemas/urlcode.schema.json`; search them for the one key you
53
62
  need rather than reading them whole.
54
63
 
55
- ## 3. Prefer YAML, then a recipe, then a function
64
+ ## 3. Choose the highest-level supported abstraction
56
65
 
57
66
  1. If a native handler expresses the behavior (`redirect`, `respond`, `page`,
58
- `static`, `download`, `link`, `proxy`, `conditional`), write YAML only.
59
- 2. If a recipe from `recipes list` is close, `urlcode recipes add NAME --out DIR`
67
+ `static`, `download`, `proxy`, `conditional`), write YAML only.
68
+ 2. Check supported extensions and their configuration before custom code. If a
69
+ recipe from `recipes list` is close, `urlcode recipes add NAME --out DIR`
60
70
  and adapt the copy into the project's layout.
61
71
  3. Only then write a function or middleware: one exported handler, relative
62
72
  ES-module imports inside the project only, inputs from validated `args`,
63
- output as a `Response`. The sandbox has no `fetch`, Node, filesystem or
64
- timers; a need for them is a `proxy` route, a binding, or a report.
65
- 4. Never build routing, validation, middleware chains, policies, static
66
- serving, caching, throttling or authentication in code. Declare them.
73
+ output as a `Response`. `function`/`middleware` routes run trusted and
74
+ unsandboxed by default: full Node, npm, filesystem and `fetch` access, like
75
+ any other project code. Add `sandbox: true` only when that route's own code
76
+ warrants isolation (untrusted input, an unreviewed contribution, a
77
+ particularly sensitive secret) — a `sandbox: true` route then has no
78
+ `fetch`, Node, npm, filesystem, WebSocket, streaming, crypto API or timers;
79
+ a need for those in a sandboxed route is a `proxy` route, a binding, or a
80
+ report.
81
+ 4. Declare routing, validation, middleware chains, policies, static serving,
82
+ caching, throttling and authentication wherever the runtime or a supported
83
+ extension provides them. Use custom code only for the unmet requirement.
84
+ Where a short form exists, it is the highest-level form: `auth: true` or
85
+ `auth: { role: admin }` on a route whose project declares an `auth`
86
+ extension, and `cache: { … }` for `policies.cache`. Each expands to the long
87
+ form; declaring both is refused.
67
88
 
68
89
  Keep every route you were not asked to change. Match the file organization the
69
90
  project already uses.
@@ -16,11 +16,11 @@ authentication; the runtime provides them. Read this file before changing anythi
16
16
  3. Run `urlcode recipes list` and `urlcode recipes show NAME` before writing a
17
17
  route from scratch. If a recipe covers the need, add it with
18
18
  `urlcode recipes add NAME --out DIR` and adapt the copy.
19
- 4. Prefer YAML over code. Prefer native handlers over functions.
19
+ 4. Use URLCode's highest-level declarative features whenever possible. Generate custom code only when the framework cannot express the requirement. Check supported extensions and recipes first; explain any capability gap.
20
20
 
21
21
  ## Ask the runtime through MCP first
22
22
 
23
- `.mcp.json` registers the read-only `urlcode mcp` server. When it is
23
+ When present, `.mcp.json` registers the read-only `urlcode mcp` server. When it is
24
24
  available, prefer its tools over reading documents: `get_context`,
25
25
  `get_capability`, `get_schema`, `search_recipes`, `explain`, `get_manifest`.
26
26
  The CLI equivalents are the fallback: `urlcode context`, `urlcode capabilities NAME`,
@@ -29,24 +29,25 @@ The CLI equivalents are the fallback: `urlcode context`, `urlcode capabilities N
29
29
 
30
30
  ## What the runtime provides (this version)
31
31
 
32
- - Handlers, exactly one per route: `redirect`, `respond`, `page`, `static`, `download`, `function`, `link`, `proxy`, `conditional`, `extension`.
33
- - Ordered `middleware` around any handler, declared in YAML, run in the sandbox.
32
+ - Handlers, exactly one per route: `redirect`, `respond`, `page`, `static`, `download`, `function`, `proxy`, `conditional`.
33
+ - Ordered `middleware` around any handler, declared in YAML, trusted by default.
34
34
  - Validated inputs: `parameters`, `request.body` and `methods` on the route;
35
35
  functions receive validated `args`, never raw user input.
36
36
  - Policies, host-enforced and off by default: `agents`, `throttle`, `cache`, `security`, `compression`.
37
37
  - Site conventions under `site`, each generating one native route: `robots` (/robots.txt), `sitemap` (/sitemap.xml), `favicon` (/favicon.ico), `securityTxt` (/.well-known/security.txt), `llms` (/llms.txt).
38
38
  - Bindings: named `env` and `secrets` references resolved by the operator, never values in YAML.
39
39
 
40
- Never recreate any of these in a function. If a requirement seems to need one
41
- of them and it is missing, that is a report, not an invitation to reimplement.
40
+ Never recreate any of these in a function; a missing one is a report, not an
41
+ invitation to reimplement it.
42
42
 
43
- ## Functions and middleware are sandboxed
43
+ ## Functions and middleware are trusted by default; sandbox is opt-in
44
44
 
45
- Guest code runs in an isolated JavaScript engine with a fresh heap per call.
46
- It sees a text/JSON `Request`/`Response` subset, validated `args` and granted
47
- `env`. There is no `fetch`, no Node API, no filesystem, no timers and no
48
- imports outside the project. Do not write code that needs them; declare a
49
- `proxy` route or a binding instead and say why.
45
+ A route's `function`/`middleware` code runs trusted, in-process, with full
46
+ Node/filesystem/`fetch` access, receiving only the declared/granted `args`
47
+ and `env`/`secrets`. Add `sandbox: true` when code warrants isolation
48
+ (untrusted input, an unreviewed contribution, an especially sensitive
49
+ secret): that route then gets a text/JSON subset only, no Node/filesystem/
50
+ outside imports — use `proxy`/a binding instead, and say why in `description`.
50
51
 
51
52
  ## Checks that count as evidence
52
53
 
@@ -56,10 +57,9 @@ urlcode test
56
57
  urlcode audit --expect-routes 2
57
58
  ```
58
59
 
59
- Run all three after every change. Update the expected route count deliberately
60
- when you add or remove a route, and add fixtures to `tests/requests.json` for
61
- every new route (positive and negative cases, every active method, HEAD).
62
- Without a global install, invoke `node /path/to/urlcode/src/cli.ts` instead of `urlcode`.
60
+ Run all three after every change, updating the route count deliberately and
61
+ adding `tests/requests.json` fixtures for every new route (positive/negative,
62
+ every active method, HEAD). No global install: use `node /path/to/urlcode/src/cli.ts`.
63
63
 
64
64
  ## Rules
65
65
 
@@ -69,8 +69,8 @@ Without a global install, invoke `node /path/to/urlcode/src/cli.ts` instead of `
69
69
  stop; the operator grants it outside this project, pinned to the revision.
70
70
  - Secrets stay out of the project: no keys, tokens or credentials in YAML,
71
71
  functions, fixtures, `.env` files that are not ignored, or commit messages.
72
- - Authentication is host processing: declare `auth` on the route, never build
73
- login forms, sessions or password checks in functions.
72
+ - Protect a route with `auth: true`/`auth: { role: admin }` where an `auth`
73
+ extension is declared; `cache` likewise expands to `policies.cache`.
74
74
  - Validation, tests and the audit are the evidence. Local checks are not a
75
75
  deployment, a soak test or a security review; do not claim otherwise.
76
76
 
@@ -1,7 +1,6 @@
1
1
  # yaml-language-server: $schema=https://raw.githubusercontent.com/jimhoyd-com/urlcode/669632fb4f188f293a0f622847f87ee019c0627f/schemas/urlcode.schema.json
2
2
  # Organize files however you like; list their paths from this project root.
3
3
  version: "1"
4
- dynamicLinks: false
5
4
  includes:
6
5
  - routes/functions.yaml
7
6
  - routes/marketing/links.yaml
package/dist/link-api.js DELETED
@@ -1,136 +0,0 @@
1
- import http from 'node:http';
2
- import {createHash,timingSafeEqual,randomUUID} from 'node:crypto';
3
- import {readFile,lstat} from 'node:fs/promises';
4
-
5
- import {outsideProject} from './link-store.js';
6
-
7
-
8
- import {linkCollection,linkCode} from './link-records.js';
9
- import {assert,HttpError} from './errors.js';
10
- import {createJsonLogger} from './logging.js';
11
-
12
- /** The store surface management needs: the pooled link store, or anything with the same contract. */
13
-
14
-
15
-
16
-
17
-
18
-
19
-
20
-
21
-
22
-
23
-
24
-
25
-
26
- export async function loadLinkToken(file ,project ) {
27
- const path=await outsideProject(file,project);const info=await lstat(path);
28
- assert(info.isFile()&&!info.isSymbolicLink()&&info.nlink===1&&info.size<=1024,'Invalid management token file');
29
- assert(process.platform==='win32'||(info.mode&0o077)===0,'Management token file must be private (mode 600)');
30
- const token=(await readFile(path,'utf8')).trim();
31
- assert(/^[A-Za-z0-9_-]{43,256}$/.test(token),'Management token must contain at least 43 base64url characters');return token;
32
- }
33
- function body(req ) {
34
- return new Promise((resolve,reject)=>{
35
- let bytes=0;const chunks =[];
36
- const cleanup=()=>{req.off('data',data);req.off('end',end);req.off('error',failed);req.off('aborted',failed);};
37
- const failed=()=>{cleanup();reject(new HttpError(400,'Incomplete management body'));};
38
- const data=(chunk )=>{
39
- bytes+=chunk.length;
40
- if(bytes>16384){cleanup();req.resume();reject(new HttpError(413,'Management body too large'));return;}
41
- chunks.push(chunk);
42
- };
43
- const end=()=>{
44
- cleanup();
45
- if(!bytes){reject(new HttpError(400,'JSON body required'));return;}
46
- try{resolve(JSON.parse(new TextDecoder('utf-8',{fatal:true}).decode(Buffer.concat(chunks))));}
47
- catch{reject(new HttpError(400,'Invalid JSON body'));}
48
- };
49
- req.on('data',data);req.once('end',end);req.once('error',failed);req.once('aborted',failed);
50
- });
51
- }
52
- const isRecord=(value ) =>value!==null && typeof value==='object' && !Array.isArray(value);
53
- const mutations ={POST:'create',PUT:'update',DELETE:'delete'};
54
- export async function startLinkApi({store,collection,token,authorize,host='127.0.0.1',port=3001,maxInFlightRequests=32,socketTimeoutMs=10000,log=createJsonLogger()} ) {
55
- linkCollection(collection);assert(['127.0.0.1','::1'].includes(host),'Management must bind a literal loopback address; use a private authenticated tunnel');
56
- assert(!authorize || store.atomicAudit===true,'Scoped management requires a store with atomic audit support');
57
- assert(authorize===undefined || typeof authorize==='function','Invalid management authorizer');
58
- assert(authorize || typeof token==='string'&&/^[A-Za-z0-9_-]{43,256}$/.test(token),'Invalid management token');
59
- assert(Number.isInteger(maxInFlightRequests)&&maxInFlightRequests>=1&&maxInFlightRequests<=64,'Management admission must be 1–64');
60
- assert(Number.isInteger(socketTimeoutMs)&&socketTimeoutMs>=100&&socketTimeoutMs<=60000,'Management socket timeout must be 100–60000 ms');
61
- let inFlight=0,shuttingDown=false,closing ;
62
- const digest=(value )=>createHash('sha256').update(value).digest();const expected=digest('Bearer '+token);
63
- const server=http.createServer({maxHeaderSize:8192,headersTimeout:5000,requestTimeout:10000,keepAliveTimeout:5000},async(req,res)=>{
64
- req.on('error',()=>{});res.on('error',()=>{});
65
- const requestId=randomUUID(),started=performance.now();let principal ;let authenticated=false,action='request',admitted=false,reported=false;
66
- const target=req.url??'',method=req.method??'';
67
- const report=()=>{
68
- if(reported)return;reported=true;if(admitted)inFlight--;
69
- try{log({event:'management_request',timestamp:new Date().toISOString(),requestId,collection,action,authenticated,principal:principal?.id,status:res.headersSent?res.statusCode:0,outcome:res.writableFinished?'finished':'aborted',durationMs:Math.round((performance.now()-started)*100)/100});}catch{/* Logging must not fail requests. */}
70
- };
71
- res.once('finish',report);res.once('close',report);
72
- const send=(status ,value ,headers ={})=>{const payload=value===undefined?undefined:JSON.stringify(value);res.writeHead(status,{'content-type':'application/json','cache-control':'no-store','x-content-type-options':'nosniff','x-request-id':requestId,...headers});res.end(payload);};
73
- try {
74
- if(shuttingDown || inFlight>=maxInFlightRequests)throw new HttpError(503,'Management capacity unavailable');
75
- inFlight++;admitted=true;
76
- let authCount=0;for(let i=0;i<req.rawHeaders.length;i+=2)if(req.rawHeaders[i]?.toLowerCase()==='authorization')authCount++;
77
- if(authCount===1){
78
- if(authorize){
79
- const bearer=/^Bearer ([A-Za-z0-9_-]{43,256})$/.exec(req.headers.authorization||'');
80
- if(bearer?.[1]!==undefined)principal=await authorize(bearer[1]);
81
- }else if(timingSafeEqual(expected,digest(req.headers.authorization||'')))principal={id:'legacy-shared',collections:[collection],actions:['get','list','create','update','delete']};
82
- }
83
- if(!principal) {req.resume();send(401,{error:'Unauthorized'},{'www-authenticate':'Bearer',connection:'close'});return;}
84
- authenticated=true;
85
- if(req.headers.origin!==undefined)throw new HttpError(403,'Browser-origin management requests are unsupported');
86
- if(!target.startsWith('/')||target.startsWith('//')||target.length>2048)throw new HttpError(400,'Invalid management target');
87
- const url=new URL(target,'http://localhost');
88
- if(target.split('?')[0]!==url.pathname)throw new HttpError(400,'Noncanonical management path');
89
- const match=/^\/v1\/links(?:\/([A-Za-z0-9_-]{1,128}))?$/.exec(url.pathname);
90
- if(!match)throw new HttpError(404,'Not found');
91
- const code=match[1];if(code)linkCode(code);
92
- if([...url.searchParams.keys()].some(k=>!['limit','after'].includes(k)) || (code && url.search))throw new HttpError(400,'Unsupported query');
93
- const allowed=code?['GET','PUT','DELETE']:['GET','POST'];
94
- if(!allowed.includes(method)){req.resume();send(405,{error:'Method not allowed'},{allow:allowed.join(', '),connection:'close'});return;}
95
- action=method==='GET'?(code?'get':'list'):mutations[method]??'request';
96
- if(!principal.collections.includes(collection) || !principal.actions.includes(action))throw new HttpError(403,'Management permission denied');
97
- const audit ={actor:principal.id,requestId};
98
- if(method==='GET'){
99
- req.resume();
100
- if(code){const value=await store.get(collection,code);if(!value)throw new HttpError(404,'Link not found');send(200,value,{etag:`"${value.version}"`});}
101
- else {
102
- if([...url.searchParams.keys()].some(k=>url.searchParams.getAll(k).length!==1))throw new HttpError(400,'Duplicate query');
103
- const raw=url.searchParams.get('limit')??'100';if(!/^\d{1,3}$/.test(raw))throw new HttpError(400,'Invalid limit');
104
- const items=await store.list(collection,{limit:Number(raw),after:url.searchParams.get('after')||''});
105
- send(200,{items,nextAfter:items.length===Number(raw)?items.at(-1)?.code??null:null});
106
- }
107
- return;
108
- }
109
- if(url.search)throw new HttpError(400,'Query unsupported for mutations');
110
- let expectedVersion ;
111
- if(method!=='POST'){
112
- const etag=req.headers['if-match'];if(!etag)throw new HttpError(428,'If-Match is required');
113
- if(!/^"[1-9]\d{0,15}"$/.test(etag))throw new HttpError(400,'Invalid If-Match');expectedVersion=Number(etag.slice(1,-1));
114
- }
115
- if(method==='DELETE'){req.resume();await store.delete(collection,code??'',expectedVersion,audit);send(204);return;}
116
- if(req.headers['content-encoding'] && req.headers['content-encoding']!=='identity')throw new HttpError(415,'Encoding unsupported');
117
- if(req.headers['content-type']?.split(';')[0]?.trim().toLowerCase()!=='application/json')throw new HttpError(415,'Expected application/json');
118
- const data=await body(req);
119
- if(!isRecord(data))throw new HttpError(400,'Invalid record');
120
- let value ;
121
- if(method==='POST'){const {code:assigned,...record}=data;value=await store.create(collection,record,assigned,audit);}
122
- else value=await store.update(collection,code??'',data,expectedVersion,audit);
123
- send(method==='POST'?201:200,value,{etag:`"${value.version}"`});
124
- }catch(e){req.resume();if(res.headersSent){res.destroy();return;}if(!res.destroyed){const status=e instanceof HttpError?e.status:503;send(status,{error:e instanceof HttpError?e.message:'Management service unavailable'},{connection:'close'});}}
125
- });
126
- server.setTimeout(socketTimeoutMs,socket=>socket.destroy());
127
- server.maxConnections=64;server.maxRequestsPerSocket=100;
128
- server.on('clientError',(_error,socket)=>{if(socket.writable)socket.end('HTTP/1.1 400 Bad Request\r\nConnection: close\r\nContent-Length: 0\r\n\r\n');});
129
- await new Promise ((resolve,reject)=>{server.once('error',reject);server.listen(port,host,()=>{server.off('error',reject);resolve();});});
130
- const address=server.address();
131
- assert(address!==null && typeof address==='object','Management server has no address');
132
- return {address,close(){
133
- if(closing)return closing;shuttingDown=true;
134
- closing=(async()=>{const timeout=setTimeout(()=>server.closeAllConnections(),10000);timeout.unref();try{await new Promise (resolve=>server.close(()=>resolve()));}finally{clearTimeout(timeout);}})();return closing;
135
- }};
136
- }
package/dist/link-cli.js DELETED
@@ -1,141 +0,0 @@
1
- import {createHash} from 'node:crypto';
2
- import {createReadStream} from 'node:fs';
3
- import {createInterface} from 'node:readline';
4
- import {isAbsolute} from 'node:path';
5
- import {managementPolicy} from './management-policy.js';
6
- import {openLinkStore} from './link-store.js';
7
-
8
- import {startLinkApi,loadLinkToken} from './link-api.js';
9
- import {linkCollection,linkCode,linkData} from './link-records.js';
10
- import {assert} from './errors.js';
11
- /** The parsed command-line values the links commands read; every flag is a string or absent. */
12
-
13
-
14
-
15
-
16
-
17
-
18
-
19
-
20
-
21
-
22
- const poolFlags=[['link-readers','readers',8],['link-read-limit','maxReads',32],['link-write-limit','maxWrites',32]] ;
23
- export function linkPoolOptions(values ) {
24
- const result ={};
25
- for(const [flag,key,max] of poolFlags){
26
- const value=values[flag];
27
- if(value===undefined)continue;
28
- assert(/^\d+$/.test(value)&&Number(value)>=1&&Number(value)<=max,`Invalid --${flag}`);result[key]=Number(value);
29
- }
30
- return result;
31
- }
32
- export function parseLinkBinding(value ,options ={}) {
33
- if(value===undefined)return undefined;
34
- const index=value.indexOf('=');assert(index>0,'Use --link-store collection=/absolute/path.sqlite');
35
- return {collection:linkCollection(value.slice(0,index)),file:value.slice(index+1),...options};
36
- }
37
- const isRecord=(value ) =>value!==null && typeof value==='object' && !Array.isArray(value);
38
- export async function runLinkCommand(action ,values ,print ) {
39
- assert(action!==undefined && ['init','create','get','list','update','delete','export','import','api'].includes(action),'Use links init/create/get/list/update/delete/export/import/api');
40
- assert(values.store,'Links commands require --store with an absolute database path');
41
- const collection=linkCollection(values.collection||'links');
42
- const poolOptions=linkPoolOptions(values);
43
- // Authentication material is checked before creating or opening a writable store.
44
- if(values['auth-file'] && values['token-file'])throw new Error('Use either --auth-file or --token-file');
45
- const project=values.project??'.';
46
- const authorize=action==='api' && values['auth-file']?await managementPolicy(values['auth-file'],project):undefined;
47
- if(authorize)await authorize('');
48
- const token=action==='api' && !authorize?await loadLinkToken(values['token-file'],project):undefined;
49
- const storeOptions ={file:values.store,project:values.project,readOnly:['get','list','export'].includes(action),...poolOptions};
50
- const store=await openLinkStore(storeOptions);
51
- try {
52
- if(action==='api'){
53
- const port=Number(values.port);assert(values.port!==undefined&&/^\d+$/.test(values.port)&&port>=0&&port<=65535,'Invalid port');
54
- const api=await startLinkApi({store,collection,token,authorize,host:values.host,port});
55
- print({event:'link-management-listening',address:api.address.address,port:api.address.port,collection});
56
- await new Promise (resolve=>{
57
- const stop=()=>{process.off('SIGINT',stop);process.off('SIGTERM',stop);resolve();};
58
- process.once('SIGINT',stop);process.once('SIGTERM',stop);
59
- });
60
- await api.close();return;
61
- }
62
- let value ;
63
- if(action==='init')value={event:'link-store-initialized'};
64
- if(action==='get'){value=await store.get(collection,values.code??'');assert(value,'Link not found');}
65
- if(action==='list')value=await store.list(collection,{limit:values.limit===undefined?100:Number(values.limit),after:values.after||''});
66
- if(action==='create'||action==='update'){
67
- assert(values.destination,'Use --destination with an HTTP(S) URL');
68
- assert(values.enabled===undefined||['true','false'].includes(values.enabled),'Enabled must be true or false');
69
- const data={url:values.destination,status:values.status===undefined?302:Number(values.status),enabled:values.enabled!=='false',expires:values.expires||null};
70
- value=action==='create'?await store.create(collection,data,values.code):await store.update(collection,values.code??'',data,Number(values['if-version']));
71
- }
72
- if(action==='delete')value={deleted:await store.delete(collection,values.code??'',Number(values['if-version']))};
73
- // Export and import stream their own NDJSON; they never buffer the store.
74
- if(action==='export'){await exportLinks(store,values,print);return;}
75
- if(action==='import'){print(await importLinks(store,values));return;}
76
- print(value);
77
- }finally{await store.close();}
78
- }
79
-
80
- // A restorable point-in-time snapshot: a header line carrying format, schema and
81
- // snapshot identity, one line per record, and a completion line with the record
82
- // count and a digest over every preceding line. Operator data goes to stdout
83
- // deliberately; redirect it to a file only operators can read.
84
- async function exportLinks(store ,values ,print ) {
85
- const pageSize=values['page-size']===undefined?100:Number(values['page-size']);
86
- assert(Number.isInteger(pageSize)&&pageSize>=1&&pageSize<=100,'Use --page-size 1–100');
87
- const hash=createHash('sha256');
88
- const line=(value )=>{hash.update(JSON.stringify(value)+'\n');print(value);};
89
- const summary=await store.exportSnapshot(
90
- {collection:values.collection===undefined?undefined:linkCollection(values.collection),pageSize},
91
- {
92
- onHeader:header=>line({event:'link-export-begin',format:header.format,schemaVersion:header.schemaVersion,
93
- applicationId:header.applicationId,collection:header.collection,revision:header.revision,
94
- records:header.records,generatedAt:header.generatedAt}),
95
- onRecords:records=>{for(const record of records)line({record});},
96
- });
97
- print({event:'link-export-complete',exported:summary.exported,sha256:hash.digest('hex')});
98
- }
99
- async function importLinks(store ,values ) {
100
- assert(typeof values.input==='string' && isAbsolute(values.input),'Use --input with an absolute export file path');
101
- const hash=createHash('sha256');
102
- const counts=new Map ();let imported=0,header ,complete ;
103
- const stream=createReadStream(values.input,{encoding:'utf8'});
104
- try {
105
- for await (const text of createInterface({input:stream,crlfDelay:Infinity})) {
106
- if(text==='')continue;
107
- assert(Buffer.byteLength(text)<=65536,'Export line exceeds 65536 bytes');
108
- assert(!complete,'Export file has content after its completion line');
109
- let parsed ;try{parsed=JSON.parse(text);}catch{throw new Error('Export file is not valid NDJSON');}
110
- const line =isRecord(parsed)?parsed:{};
111
- if(line.event==='link-export-complete'){
112
- assert(header,'Export file has no header line');
113
- assert(hash.copy().digest('hex')===line.sha256,'Export digest does not match its content');
114
- assert(line.exported===imported,'Export record count does not match its record lines');
115
- complete=line;continue;
116
- }
117
- hash.update(text+'\n');
118
- if(line.event==='link-export-begin'){
119
- assert(!header && !imported,'Export file has more than one header line');
120
- assert(line.format==='urlcode.links.v1' && line.schemaVersion===1 && line.applicationId===1431456835,
121
- 'Unsupported export format, schema version or store identity');
122
- header=line;continue;
123
- }
124
- assert(header,'Export records precede the header line');
125
- assert(isRecord(line.record),'Unsupported export line');
126
- const collection=linkCollection(line.record.collection),code=linkCode(line.record.code);
127
- // Restore into empty collections only: this never overwrites live records.
128
- if(!counts.has(collection)){
129
- assert((await store.list(collection,{limit:1})).length===0,'Target collection already contains records');
130
- counts.set(collection,0);
131
- }
132
- await store.create(collection,linkData({url:line.record.url,status:line.record.status,enabled:line.record.enabled,expires:line.record.expires}),code);
133
- counts.set(collection,(counts.get(collection)??0)+1);imported++;
134
- }
135
- } finally { stream.destroy(); }
136
- assert(complete,'Export file has no completion line; it is truncated');
137
- // Versions and audit revisions are reassigned by this store, so management
138
- // ETags taken against the exported database do not survive a restore.
139
- return {event:'link-import-complete',imported,collections:Object.fromEntries(counts),
140
- source:{revision:header?.revision,generatedAt:header?.generatedAt,sha256:complete.sha256},versionsReassigned:true};
141
- }
@@ -1,76 +0,0 @@
1
- import {assert} from './errors.js';
2
-
3
-
4
-
5
-
6
-
7
-
8
-
9
- // Trusted-operator observation of finished link requests. The observer is
10
- // supplied by the embedding operator process, never by route YAML or guest
11
- // code, and it runs after the response is over: it can never change, delay or
12
- // fail a redirect. Work is bounded by an explicit queue; overload drops events
13
- // and counts the drops rather than growing memory behind the operator's back.
14
- const outcomes=new Set(['completed','aborted','missing','disabled','expired','invalid_code','invalid_record','unavailable']);
15
- export function createLinkObserver(options , log = () => {}) {
16
- if(options===undefined)return undefined;
17
- assert(options && typeof options==='object' && !Array.isArray(options),'Link events must be an options object');
18
- const {observe,includeCode=false,maxQueue=256,timeoutMs=1000}=options ;
19
- assert(Object.keys(options).every(key=>['observe','includeCode','maxQueue','timeoutMs'].includes(key)),'Unsupported link event option');
20
- assert(typeof observe==='function','Link events require an observe(event) function');
21
- assert(typeof includeCode==='boolean','Link event code disclosure must be a boolean');
22
- assert(Number.isInteger(maxQueue)&&maxQueue>=1&&maxQueue<=4096,'Link event queue must be 1–4096');
23
- assert(Number.isInteger(timeoutMs)&&timeoutMs>=1&&timeoutMs<=10000,'Link event timeout must be 1–10000 ms');
24
- const queue =[];
25
- let delivered=0,dropped=0,failed=0,timedOut=0,closed=false,draining ;
26
- const report =event=>{try{log(event);}catch{/* Logging cannot fail the observer. */}};
27
- async function deliver(event ) {
28
- let timer ;
29
- try {
30
- // A slow collector must not pin the queue: the budget is per event and the
31
- // observer keeps running afterwards, whatever the abandoned call does.
32
- await Promise.race([
33
- Promise.resolve(observe (event)),
34
- new Promise((_resolve,reject)=>{timer=setTimeout(()=>reject(new Error('timeout')),timeoutMs);timer.unref?.();}),
35
- ]);
36
- delivered++;
37
- } catch(error) {
38
- failed++;
39
- const timeout=(error )?.message==='timeout';
40
- if(timeout)timedOut++;
41
- report({event:'link_observer',status:'failed',reason:timeout?'timeout':'error'});
42
- } finally { clearTimeout(timer); }
43
- }
44
- async function pump() {
45
- if(draining)return;
46
- draining=(async()=>{while(queue.length)await deliver(queue.shift() );})();
47
- try{await draining;}finally{draining=undefined;}
48
- }
49
- return {
50
- // Called from the request path. It must stay synchronous and total.
51
- emit(event ) {
52
- try {
53
- if(closed||!outcomes.has(event.outcome)){dropped++;return;}
54
- if(queue.length>=maxQueue){
55
- dropped++;
56
- if(dropped===1||dropped%maxQueue===0)report({event:'link_observer',status:'dropped',dropped});
57
- return;
58
- }
59
- const {code:_code,...redacted}=event;
60
- queue.push(includeCode?{...event}:redacted);
61
- void pump();
62
- } catch { dropped++; }
63
- },
64
- stats:()=>({queued:queue.length,delivered,dropped,failed,timedOut,closed}),
65
- async close() {
66
- closed=true;
67
- // Drain what was already accepted, then stop; a stuck collector cannot
68
- // hold shutdown open past its own per-event budget plus this deadline.
69
- void pump();
70
- const deadline=new Promise (resolve=>{const timer=setTimeout(()=>resolve(false),timeoutMs*2+1000);timer.unref?.();});
71
- while(draining)if(!await Promise.race([draining.then(()=>true,()=>true),deadline]))break;
72
- dropped+=queue.length;queue.length=0;
73
- return this.stats();
74
- },
75
- };
76
- }
@@ -1,31 +0,0 @@
1
- import {randomBytes} from 'node:crypto';
2
- import {HttpError} from './errors.js';
3
-
4
- const check = (value,message) => { if(!value)throw new HttpError(400,message); };
5
- export function linkCollection(value ) {
6
- check(typeof value==='string' && /^[A-Za-z][A-Za-z0-9_-]{0,63}$/.test(value),'Invalid link collection');return value;
7
- }
8
- export function linkCode(value ) {
9
- check(typeof value==='string' && /^[A-Za-z0-9_-]{1,128}$/.test(value),'Invalid short code');return value;
10
- }
11
- export function linkVersion(value ) {
12
- check(typeof value==='number' && Number.isSafeInteger(value) && value>0,'A positive expected version is required');return value;
13
- }
14
- export function linkData(value ) {
15
- check(value && typeof value==='object' && !Array.isArray(value),'Invalid link record');
16
- check(Object.keys(value).every(k=>['url','status','enabled','expires'].includes(k)),'Unsupported link field');
17
- const record=value ;
18
- check(typeof record.url==='string' && record.url.length<=8192 && !/[\s\u0000-\u001f\u007f\\]/u.test(record.url),'Invalid destination');
19
- let url ;try{url=new URL(record.url);}catch{throw new HttpError(400,'Invalid destination');}
20
- check(['http:','https:'].includes(url.protocol) && !url.username && !url.password,'Destination must be HTTP(S) without credentials');
21
- check(Buffer.byteLength(url.href)<=8192,'Destination exceeds 8192 bytes');
22
- const status=record.status??302,enabled=record.enabled??true,expires=record.expires??null;
23
- check(typeof status==='number' && [301,302,303,307,308].includes(status),'Invalid redirect status');
24
- check(typeof enabled==='boolean','Invalid enabled flag');
25
- if(expires!==null){
26
- check(typeof expires==='string' && /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d{3})?Z$/.test(expires) && Number.isFinite(Date.parse(expires)),'Invalid expiry');
27
- check(new Date(expires).toISOString().replace('.000Z','Z')===expires.replace('.000Z','Z'),'Invalid expiry');
28
- }
29
- return {url:url.href,status,enabled,expires};
30
- }
31
- export const randomLinkCode=() =>randomBytes(12).toString('base64url');