rebilder 0.2.1 → 0.4.0
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 +40 -0
- package/README.md +59 -72
- package/dist/bin.js +436 -146
- package/dist/bin.js.map +1 -1
- package/dist/index.d.ts +23 -26
- package/dist/index.js +436 -146
- package/dist/index.js.map +1 -1
- package/package.json +18 -8
- package/dist/index.cjs +0 -1668
- package/dist/index.cjs.map +0 -1
- package/dist/index.d.cts +0 -503
package/dist/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { parseArgs } from 'util';
|
|
2
|
-
import { score } from '@rebilder/agent-readability';
|
|
2
|
+
import { DEFAULT_RULESET, score } from '@rebilder/agent-readability';
|
|
3
3
|
import { readFile, writeFile } from 'fs/promises';
|
|
4
4
|
import { resolve } from 'path';
|
|
5
5
|
import { createLimiter, strictPolicy, probeStrict } from '@rebilder/agent-readability/probe';
|
|
@@ -335,68 +335,113 @@ function approxTokensOf(capture) {
|
|
|
335
335
|
const characters = capture.body?.length ?? capture.bytes;
|
|
336
336
|
return Math.round(characters / 4);
|
|
337
337
|
}
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
{ file: "wrangler.toml", framework: "edge", reason: "wrangler.toml in the working directory" },
|
|
355
|
-
{ file: "wrangler.jsonc", framework: "edge", reason: "wrangler.jsonc in the working directory" }
|
|
338
|
+
var FRAMEWORKS = [
|
|
339
|
+
"next",
|
|
340
|
+
"node",
|
|
341
|
+
"edge",
|
|
342
|
+
"shopify",
|
|
343
|
+
"fetch",
|
|
344
|
+
"sveltekit",
|
|
345
|
+
"nuxt",
|
|
346
|
+
"astro",
|
|
347
|
+
"react-router",
|
|
348
|
+
"remix",
|
|
349
|
+
"hono",
|
|
350
|
+
"bun",
|
|
351
|
+
"deno",
|
|
352
|
+
"netlify",
|
|
353
|
+
"vercel"
|
|
356
354
|
];
|
|
357
|
-
var
|
|
355
|
+
var FRAMEWORK_FILES = [
|
|
356
|
+
{ file: "shopify.app.toml", framework: "shopify" },
|
|
357
|
+
{ file: "next.config.ts", framework: "next" },
|
|
358
|
+
{ file: "next.config.mjs", framework: "next" },
|
|
359
|
+
{ file: "next.config.js", framework: "next" },
|
|
360
|
+
{ file: "svelte.config.js", framework: "sveltekit" },
|
|
361
|
+
{ file: "svelte.config.ts", framework: "sveltekit" },
|
|
362
|
+
{ file: "nuxt.config.ts", framework: "nuxt" },
|
|
363
|
+
{ file: "nuxt.config.js", framework: "nuxt" },
|
|
364
|
+
{ file: "nuxt.config.mjs", framework: "nuxt" },
|
|
365
|
+
{ file: "astro.config.mjs", framework: "astro" },
|
|
366
|
+
{ file: "astro.config.ts", framework: "astro" },
|
|
367
|
+
{ file: "astro.config.js", framework: "astro" },
|
|
368
|
+
{ file: "react-router.config.ts", framework: "react-router" },
|
|
369
|
+
{ file: "react-router.config.js", framework: "react-router" },
|
|
370
|
+
{ file: "remix.config.js", framework: "remix" }
|
|
371
|
+
];
|
|
372
|
+
var FRAMEWORK_DEPENDENCIES = [
|
|
358
373
|
{ dependency: "@shopify/shopify-app-remix", framework: "shopify" },
|
|
374
|
+
{ dependency: "@shopify/shopify-app-react-router", framework: "shopify" },
|
|
359
375
|
{ dependency: "@shopify/shopify-api", framework: "shopify" },
|
|
360
376
|
{ dependency: "next", framework: "next" },
|
|
361
|
-
{ dependency: "@
|
|
362
|
-
{ dependency: "
|
|
363
|
-
{ dependency: "
|
|
377
|
+
{ dependency: "@sveltejs/kit", framework: "sveltekit" },
|
|
378
|
+
{ dependency: "nuxt", framework: "nuxt" },
|
|
379
|
+
{ dependency: "astro", framework: "astro" },
|
|
380
|
+
{ dependency: "@react-router/dev", framework: "react-router" },
|
|
381
|
+
{ dependency: "@react-router/node", framework: "react-router" },
|
|
382
|
+
{ dependency: "@remix-run/node", framework: "remix" },
|
|
383
|
+
{ dependency: "@remix-run/express", framework: "remix" },
|
|
384
|
+
{ dependency: "hono", framework: "hono" },
|
|
364
385
|
{ dependency: "express", framework: "node" },
|
|
365
386
|
{ dependency: "fastify", framework: "node" }
|
|
366
387
|
];
|
|
367
|
-
|
|
368
|
-
|
|
388
|
+
var RUNTIME_FILES = [
|
|
389
|
+
{ file: "wrangler.toml", framework: "edge" },
|
|
390
|
+
{ file: "wrangler.jsonc", framework: "edge" },
|
|
391
|
+
{ file: "wrangler.json", framework: "edge" },
|
|
392
|
+
{ file: "netlify.toml", framework: "netlify" },
|
|
393
|
+
{ file: "vercel.json", framework: "vercel" },
|
|
394
|
+
{ file: "deno.json", framework: "deno" },
|
|
395
|
+
{ file: "deno.jsonc", framework: "deno" },
|
|
396
|
+
{ file: "bunfig.toml", framework: "bun" },
|
|
397
|
+
{ file: "bun.lock", framework: "bun" },
|
|
398
|
+
{ file: "bun.lockb", framework: "bun" }
|
|
399
|
+
];
|
|
400
|
+
var RUNTIME_DEPENDENCIES = [
|
|
401
|
+
{ dependency: "wrangler", framework: "edge" },
|
|
402
|
+
{ dependency: "@cloudflare/workers-types", framework: "edge" },
|
|
403
|
+
{ dependency: "@netlify/edge-functions", framework: "netlify" },
|
|
404
|
+
{ dependency: "@vercel/functions", framework: "vercel" }
|
|
405
|
+
];
|
|
406
|
+
async function firstFile(runtime, markers) {
|
|
407
|
+
for (const marker of markers) {
|
|
369
408
|
if (await runtime.readFile(marker.file) !== null) {
|
|
370
|
-
return {
|
|
409
|
+
return {
|
|
410
|
+
framework: marker.framework,
|
|
411
|
+
reason: `detected from ${marker.file} in the working directory`
|
|
412
|
+
};
|
|
371
413
|
}
|
|
372
414
|
}
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
...asRecord(record["devDependencies"])
|
|
383
|
-
};
|
|
384
|
-
}
|
|
385
|
-
} catch {
|
|
386
|
-
dependencies = {};
|
|
387
|
-
}
|
|
388
|
-
for (const marker of DEPENDENCY_MARKERS) {
|
|
389
|
-
if (marker.dependency in dependencies) {
|
|
390
|
-
return {
|
|
391
|
-
framework: marker.framework,
|
|
392
|
-
reason: `detected from "${marker.dependency}" in package.json`
|
|
393
|
-
};
|
|
394
|
-
}
|
|
415
|
+
return null;
|
|
416
|
+
}
|
|
417
|
+
function firstDependency(dependencies, markers) {
|
|
418
|
+
for (const marker of markers) {
|
|
419
|
+
if (marker.dependency in dependencies) {
|
|
420
|
+
return {
|
|
421
|
+
framework: marker.framework,
|
|
422
|
+
reason: `detected from "${marker.dependency}" in package.json`
|
|
423
|
+
};
|
|
395
424
|
}
|
|
396
425
|
}
|
|
397
|
-
return
|
|
398
|
-
|
|
399
|
-
|
|
426
|
+
return null;
|
|
427
|
+
}
|
|
428
|
+
async function readDependencies(runtime) {
|
|
429
|
+
const manifest = await runtime.readFile("package.json");
|
|
430
|
+
if (manifest === null) return {};
|
|
431
|
+
try {
|
|
432
|
+
const parsed = JSON.parse(manifest);
|
|
433
|
+
if (typeof parsed !== "object" || parsed === null) return {};
|
|
434
|
+
const record = parsed;
|
|
435
|
+
return { ...asRecord(record["dependencies"]), ...asRecord(record["devDependencies"]) };
|
|
436
|
+
} catch {
|
|
437
|
+
return {};
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
async function detectFramework(runtime) {
|
|
441
|
+
const dependencies = await readDependencies(runtime);
|
|
442
|
+
return await firstFile(runtime, FRAMEWORK_FILES) ?? firstDependency(dependencies, FRAMEWORK_DEPENDENCIES) ?? await firstFile(runtime, RUNTIME_FILES) ?? firstDependency(dependencies, RUNTIME_DEPENDENCIES) ?? {
|
|
443
|
+
framework: "fetch",
|
|
444
|
+
reason: `no framework marker found \u2014 scaffolding the framework-neutral fetch middleware. Pass --framework ${FRAMEWORKS.join("|")} to choose.`
|
|
400
445
|
};
|
|
401
446
|
}
|
|
402
447
|
function asRecord(value) {
|
|
@@ -423,8 +468,8 @@ async function runInit(command, runtime) {
|
|
|
423
468
|
}
|
|
424
469
|
var SHARED_NOTES = [
|
|
425
470
|
"The resolvers return your source of truth. Every substantive value \u2014 price, stock, hours, policy text \u2014 comes from these fields; the gateway never invents one.",
|
|
426
|
-
"The
|
|
427
|
-
|
|
471
|
+
"The Apache-2.0 SDK serves without a Rebilder account or subscription. Configure reporting separately.",
|
|
472
|
+
`Run \`rebilder check <url>\` before and after to see what changed. Sending a Markdown copy from the page address is D2.1 (${DEFAULT_RULESET.weights["machine-representation.negotiated-response"] ?? 0} points) and unlocks D2.3 and D2.4.`
|
|
428
473
|
];
|
|
429
474
|
var CONFIG_BODY = `import type { GatewayConfig } from '@rebilder/gateway'
|
|
430
475
|
|
|
@@ -449,81 +494,287 @@ export const gatewayConfig: GatewayConfig = {
|
|
|
449
494
|
// onEvent: (event) => { void event },
|
|
450
495
|
}
|
|
451
496
|
`;
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
497
|
+
function scaffold(suggestedPath, mount, notes) {
|
|
498
|
+
const [header = "", ...body] = mount.trim().split("\n");
|
|
499
|
+
const commented = body.map((line) => line === "" ? "//" : `// ${line}`);
|
|
500
|
+
return {
|
|
501
|
+
suggestedPath,
|
|
455
502
|
language: "ts",
|
|
456
|
-
notes: [
|
|
457
|
-
"Then add the proxy: `const gateway = createGatewayProxy(gatewayConfig)` in proxy.ts (Next 16) or middleware.ts (Next 15 and earlier), returning `(await gateway(req)) ?? NextResponse.next()`.",
|
|
458
|
-
...SHARED_NOTES
|
|
459
|
-
],
|
|
503
|
+
notes: [...notes, ...SHARED_NOTES],
|
|
460
504
|
contents: `${CONFIG_BODY}
|
|
461
|
-
|
|
462
|
-
//
|
|
463
|
-
// import { NextResponse } from 'next/server'
|
|
464
|
-
// import { createGatewayProxy } from '@rebilder/gateway/next'
|
|
465
|
-
// import { gatewayConfig } from './lib/gateway-config'
|
|
505
|
+
${header}
|
|
466
506
|
//
|
|
467
|
-
|
|
468
|
-
//
|
|
469
|
-
// export default async function proxy(req: Request) {
|
|
470
|
-
// return (await gateway(req)) ?? NextResponse.next()
|
|
471
|
-
// }
|
|
507
|
+
${commented.join("\n")}
|
|
472
508
|
`
|
|
473
|
-
}
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
509
|
+
};
|
|
510
|
+
}
|
|
511
|
+
var DOCS = "https://rebilder.com/docs/adapters";
|
|
512
|
+
var SCAFFOLDS = {
|
|
513
|
+
next: scaffold(
|
|
514
|
+
"lib/gateway-config.ts",
|
|
515
|
+
`
|
|
516
|
+
/* \u2500\u2500 proxy.ts (Next 16) or middleware.ts (Next \u226415) \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500 */
|
|
517
|
+
import { NextResponse } from 'next/server'
|
|
518
|
+
import { createGatewayProxy } from '@rebilder/gateway/next'
|
|
519
|
+
import { gatewayConfig } from './lib/gateway-config'
|
|
520
|
+
|
|
521
|
+
// Passing the fallthrough adds Vary: Accept to your HTML responses too.
|
|
522
|
+
export default createGatewayProxy(gatewayConfig, () => NextResponse.next())
|
|
523
|
+
`,
|
|
524
|
+
[
|
|
525
|
+
"Then add the proxy: `createGatewayProxy(gatewayConfig, () => NextResponse.next())` in proxy.ts (Next 16) or middleware.ts (Next 15 and earlier).",
|
|
526
|
+
`Full guide: ${DOCS}/nextjs`
|
|
527
|
+
]
|
|
528
|
+
),
|
|
529
|
+
node: scaffold(
|
|
530
|
+
"gateway-config.ts",
|
|
531
|
+
`
|
|
482
532
|
/* \u2500\u2500 Express \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500 */
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
//
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
533
|
+
import express from 'express'
|
|
534
|
+
import { createGatewayMiddleware } from '@rebilder/gateway/node'
|
|
535
|
+
import { gatewayConfig } from './gateway-config'
|
|
536
|
+
|
|
537
|
+
const app = express()
|
|
538
|
+
app.use(createGatewayMiddleware(gatewayConfig)) // before your routes
|
|
539
|
+
`,
|
|
540
|
+
[
|
|
541
|
+
"Then mount it before your routes: `app.use(createGatewayMiddleware(gatewayConfig))`.",
|
|
542
|
+
`Fastify and the full guide: ${DOCS}/node`
|
|
543
|
+
]
|
|
544
|
+
),
|
|
545
|
+
edge: scaffold(
|
|
546
|
+
"gateway-config.ts",
|
|
547
|
+
`
|
|
548
|
+
/* \u2500\u2500 worker.ts (Cloudflare Workers) \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500 */
|
|
549
|
+
import { createGatewayFetchHandler } from '@rebilder/gateway/edge'
|
|
550
|
+
import { gatewayConfig } from './gateway-config'
|
|
551
|
+
|
|
552
|
+
export default { fetch: createGatewayFetchHandler(gatewayConfig) }
|
|
553
|
+
`,
|
|
554
|
+
[
|
|
496
555
|
"Then export the fetch handler: `export default { fetch: createGatewayFetchHandler(gatewayConfig) }`.",
|
|
497
556
|
"On a route-mounted Worker, non-markdown requests pass through to your origin unchanged, so browsers and crawlers keep receiving the canonical HTML.",
|
|
498
|
-
|
|
499
|
-
]
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
// import { gatewayConfig } from './gateway-config'
|
|
505
|
-
//
|
|
506
|
-
// export default { fetch: createGatewayFetchHandler(gatewayConfig) }
|
|
507
|
-
`
|
|
508
|
-
},
|
|
509
|
-
shopify: {
|
|
510
|
-
suggestedPath: "app/gateway-config.ts",
|
|
511
|
-
language: "ts",
|
|
512
|
-
notes: [
|
|
513
|
-
"Then mount the app-proxy handler and give it your app\u2019s shared secret; requests are signature-verified before anything is rendered.",
|
|
514
|
-
...SHARED_NOTES
|
|
515
|
-
],
|
|
516
|
-
contents: `${CONFIG_BODY}
|
|
557
|
+
`Full guide: ${DOCS}/cloudflare`
|
|
558
|
+
]
|
|
559
|
+
),
|
|
560
|
+
shopify: scaffold(
|
|
561
|
+
"app/gateway-config.ts",
|
|
562
|
+
`
|
|
517
563
|
/* \u2500\u2500 Shopify app proxy \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500 */
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
564
|
+
import { createShopifyAppProxyHandler } from '@rebilder/gateway/shopify'
|
|
565
|
+
import { gatewayConfig } from './gateway-config'
|
|
566
|
+
|
|
567
|
+
export const loader = createShopifyAppProxyHandler(gatewayConfig, {
|
|
568
|
+
sharedSecret: process.env.SHOPIFY_API_SECRET ?? '',
|
|
569
|
+
})
|
|
570
|
+
`,
|
|
571
|
+
[
|
|
572
|
+
"Then mount the app-proxy handler and give it your app\u2019s shared secret; requests are signature-verified before anything is rendered.",
|
|
573
|
+
`Full guide: ${DOCS}/shopify`
|
|
574
|
+
]
|
|
575
|
+
),
|
|
576
|
+
fetch: scaffold(
|
|
577
|
+
"gateway-config.ts",
|
|
578
|
+
`
|
|
579
|
+
/* \u2500\u2500 Any host whose middleware receives a Request and a next() \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500 */
|
|
580
|
+
import { createFetchMiddleware } from '@rebilder/gateway/fetch'
|
|
581
|
+
import { gatewayConfig } from './gateway-config'
|
|
582
|
+
|
|
583
|
+
const gateway = createFetchMiddleware(gatewayConfig)
|
|
584
|
+
|
|
585
|
+
// In your middleware: return gateway(request, () => next())
|
|
586
|
+
`,
|
|
587
|
+
[
|
|
588
|
+
"Mount `createFetchMiddleware(gatewayConfig)` wherever your framework hands middleware a Request and a next().",
|
|
589
|
+
`Recipes for SvelteKit, Nuxt, Astro, React Router, Remix and Hono: ${DOCS}/frameworks`
|
|
590
|
+
]
|
|
591
|
+
),
|
|
592
|
+
sveltekit: scaffold(
|
|
593
|
+
"src/lib/gateway-config.ts",
|
|
594
|
+
`
|
|
595
|
+
/* \u2500\u2500 src/hooks.server.ts \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500 */
|
|
596
|
+
import type { Handle } from '@sveltejs/kit'
|
|
597
|
+
import { createFetchMiddleware } from '@rebilder/gateway/fetch'
|
|
598
|
+
import { gatewayConfig } from '$lib/gateway-config'
|
|
599
|
+
|
|
600
|
+
const gateway = createFetchMiddleware(gatewayConfig)
|
|
601
|
+
|
|
602
|
+
export const handle: Handle = ({ event, resolve }) => gateway(event.request, () => resolve(event))
|
|
603
|
+
`,
|
|
604
|
+
[
|
|
605
|
+
"Then add the `handle` hook in src/hooks.server.ts. If you already have one, combine them with `sequence` from @sveltejs/kit/hooks.",
|
|
606
|
+
`Full guide: ${DOCS}/frameworks#sveltekit`
|
|
607
|
+
]
|
|
608
|
+
),
|
|
609
|
+
nuxt: scaffold(
|
|
610
|
+
"server/utils/gateway-config.ts",
|
|
611
|
+
`
|
|
612
|
+
/* \u2500\u2500 server/middleware/rebilder.ts (Nuxt and Nitro; h3 helpers are auto-imported) \u2500\u2500 */
|
|
613
|
+
import { handleRequest, negotiationHeaders } from '@rebilder/gateway/fetch'
|
|
614
|
+
import { gatewayConfig } from '../utils/gateway-config'
|
|
615
|
+
|
|
616
|
+
export default defineEventHandler(async (event) => {
|
|
617
|
+
if (event.method !== 'GET' && event.method !== 'HEAD') return
|
|
618
|
+
const served = await handleRequest(toWebRequest(event), gatewayConfig)
|
|
619
|
+
if (served) return served
|
|
620
|
+
for (const [name, value] of negotiationHeaders(gatewayConfig, getRequestURL(event))) {
|
|
621
|
+
appendResponseHeader(event, name, value)
|
|
526
622
|
}
|
|
623
|
+
})
|
|
624
|
+
`,
|
|
625
|
+
[
|
|
626
|
+
"Then add the server middleware. It returns markdown to agents and otherwise returns nothing, so Nuxt renders the page as usual.",
|
|
627
|
+
`Full guide: ${DOCS}/frameworks#nuxt`
|
|
628
|
+
]
|
|
629
|
+
),
|
|
630
|
+
astro: scaffold(
|
|
631
|
+
"src/gateway-config.ts",
|
|
632
|
+
`
|
|
633
|
+
/* \u2500\u2500 src/middleware.ts \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500 */
|
|
634
|
+
import { defineMiddleware } from 'astro:middleware'
|
|
635
|
+
import { createFetchMiddleware } from '@rebilder/gateway/fetch'
|
|
636
|
+
import { gatewayConfig } from './gateway-config'
|
|
637
|
+
|
|
638
|
+
const gateway = createFetchMiddleware(gatewayConfig)
|
|
639
|
+
|
|
640
|
+
export const onRequest = defineMiddleware((context, next) => gateway(context.request, () => next()))
|
|
641
|
+
`,
|
|
642
|
+
[
|
|
643
|
+
'Then add src/middleware.ts. Astro runs middleware per request only on on-demand rendered pages (an adapter plus `output: "server"` or `export const prerender = false`).',
|
|
644
|
+
`Full guide: ${DOCS}/frameworks#astro`
|
|
645
|
+
]
|
|
646
|
+
),
|
|
647
|
+
"react-router": scaffold(
|
|
648
|
+
"app/gateway-config.server.ts",
|
|
649
|
+
`
|
|
650
|
+
/* \u2500\u2500 app/root.tsx (React Router 8, or 7.9+ with future.v8_middleware) \u2500\u2500\u2500\u2500\u2500\u2500\u2500 */
|
|
651
|
+
import type { Route } from './+types/root'
|
|
652
|
+
import { createFetchMiddleware } from '@rebilder/gateway/fetch'
|
|
653
|
+
import { gatewayConfig } from './gateway-config.server'
|
|
654
|
+
|
|
655
|
+
const gateway = createFetchMiddleware(gatewayConfig)
|
|
656
|
+
|
|
657
|
+
export const middleware: Route.MiddlewareFunction[] = [({ request }, next) => gateway(request, next)]
|
|
658
|
+
`,
|
|
659
|
+
[
|
|
660
|
+
"Then export `middleware` from app/root.tsx. On React Router 7, enable `future: { v8_middleware: true }` in react-router.config.ts first.",
|
|
661
|
+
`Full guide: ${DOCS}/frameworks#react-router`
|
|
662
|
+
]
|
|
663
|
+
),
|
|
664
|
+
remix: scaffold(
|
|
665
|
+
"app/gateway-config.server.ts",
|
|
666
|
+
`
|
|
667
|
+
/* \u2500\u2500 server.ts (Remix v2 with a custom Express server) \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500 */
|
|
668
|
+
import express from 'express'
|
|
669
|
+
import { createRequestHandler } from '@remix-run/express'
|
|
670
|
+
import { createGatewayMiddleware } from '@rebilder/gateway/node'
|
|
671
|
+
import { gatewayConfig } from './app/gateway-config.server'
|
|
672
|
+
|
|
673
|
+
const app = express()
|
|
674
|
+
app.use(createGatewayMiddleware(gatewayConfig)) // before Remix
|
|
675
|
+
app.all('*', createRequestHandler({ build: await import('./build/server/index.js') }))
|
|
676
|
+
`,
|
|
677
|
+
[
|
|
678
|
+
"Remix v2 has no middleware, so the gateway runs as Express middleware in front of `createRequestHandler`.",
|
|
679
|
+
`Full guide: ${DOCS}/frameworks#remix`
|
|
680
|
+
]
|
|
681
|
+
),
|
|
682
|
+
hono: scaffold(
|
|
683
|
+
"src/gateway-config.ts",
|
|
684
|
+
`
|
|
685
|
+
/* \u2500\u2500 src/index.ts (Hono) \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500 */
|
|
686
|
+
import { Hono } from 'hono'
|
|
687
|
+
import { createFetchMiddleware } from '@rebilder/gateway/fetch'
|
|
688
|
+
import { gatewayConfig } from './gateway-config'
|
|
689
|
+
|
|
690
|
+
const gateway = createFetchMiddleware(gatewayConfig)
|
|
691
|
+
const app = new Hono()
|
|
692
|
+
|
|
693
|
+
app.use(async (c, next) => {
|
|
694
|
+
let passed = false
|
|
695
|
+
const res = await gateway(c.req.raw, async () => {
|
|
696
|
+
passed = true
|
|
697
|
+
await next()
|
|
698
|
+
return c.res
|
|
699
|
+
})
|
|
700
|
+
if (!passed) return res // markdown, answered before any route ran
|
|
701
|
+
c.res = undefined // clear first, so Hono does not copy the old headers over ours
|
|
702
|
+
c.res = res
|
|
703
|
+
})
|
|
704
|
+
`,
|
|
705
|
+
[
|
|
706
|
+
"Register the middleware before your routes. It works the same on Workers, Bun, Deno and Node.",
|
|
707
|
+
`Full guide: ${DOCS}/frameworks#hono`
|
|
708
|
+
]
|
|
709
|
+
),
|
|
710
|
+
bun: scaffold(
|
|
711
|
+
"gateway-config.ts",
|
|
712
|
+
`
|
|
713
|
+
/* \u2500\u2500 server.ts (Bun) \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500 */
|
|
714
|
+
import { createFetchMiddleware } from '@rebilder/gateway/fetch'
|
|
715
|
+
import { gatewayConfig } from './gateway-config'
|
|
716
|
+
import { app } from './app' // TODO: your existing (request) => Response handler
|
|
717
|
+
|
|
718
|
+
const gateway = createFetchMiddleware(gatewayConfig)
|
|
719
|
+
|
|
720
|
+
Bun.serve({ fetch: (request) => gateway(request, () => app(request)) })
|
|
721
|
+
`,
|
|
722
|
+
[
|
|
723
|
+
"Wrap your existing fetch handler. Requests served by Bun.serve `routes` skip `fetch`, so wrap those handlers too.",
|
|
724
|
+
`Full guide: ${DOCS}/runtimes#bun`
|
|
725
|
+
]
|
|
726
|
+
),
|
|
727
|
+
deno: scaffold(
|
|
728
|
+
"gateway-config.ts",
|
|
729
|
+
`
|
|
730
|
+
/* \u2500\u2500 main.ts (Deno) \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500 */
|
|
731
|
+
import { createFetchMiddleware } from '@rebilder/gateway/fetch'
|
|
732
|
+
import { gatewayConfig } from './gateway-config.ts'
|
|
733
|
+
import { app } from './app.ts' // TODO: your existing (request) => Response handler
|
|
734
|
+
|
|
735
|
+
const gateway = createFetchMiddleware(gatewayConfig)
|
|
736
|
+
|
|
737
|
+
Deno.serve((request) => gateway(request, () => app(request)))
|
|
738
|
+
`,
|
|
739
|
+
["Wrap your existing handler in Deno.serve.", `Full guide: ${DOCS}/runtimes#deno`]
|
|
740
|
+
),
|
|
741
|
+
netlify: scaffold(
|
|
742
|
+
"gateway-config.ts",
|
|
743
|
+
`
|
|
744
|
+
/* \u2500\u2500 netlify/edge-functions/rebilder.ts \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500 */
|
|
745
|
+
import type { Config, Context } from '@netlify/edge-functions'
|
|
746
|
+
import { createFetchMiddleware } from '@rebilder/gateway/fetch'
|
|
747
|
+
import { gatewayConfig } from '../../gateway-config.ts'
|
|
748
|
+
|
|
749
|
+
const gateway = createFetchMiddleware(gatewayConfig)
|
|
750
|
+
|
|
751
|
+
export default (request: Request, context: Context) => gateway(request, () => context.next())
|
|
752
|
+
|
|
753
|
+
export const config: Config = { path: '/*', excludedPath: ['/*.css', '/*.js'] }
|
|
754
|
+
`,
|
|
755
|
+
[
|
|
756
|
+
"Then add the edge function. It answers agents at the edge and passes everything else to your site with `context.next()`.",
|
|
757
|
+
`Full guide: ${DOCS}/runtimes#netlify`
|
|
758
|
+
]
|
|
759
|
+
),
|
|
760
|
+
vercel: scaffold(
|
|
761
|
+
"gateway-config.ts",
|
|
762
|
+
`
|
|
763
|
+
/* \u2500\u2500 middleware.ts (Vercel Routing Middleware, outside Next.js) \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500 */
|
|
764
|
+
import { next } from '@vercel/functions'
|
|
765
|
+
import { handleRequest, negotiationHeaders } from '@rebilder/gateway/fetch'
|
|
766
|
+
import { gatewayConfig } from './gateway-config'
|
|
767
|
+
|
|
768
|
+
export default async function middleware(request: Request) {
|
|
769
|
+
const served = await handleRequest(request, gatewayConfig)
|
|
770
|
+
return served ?? next({ headers: Object.fromEntries(negotiationHeaders(gatewayConfig, new URL(request.url))) })
|
|
771
|
+
}
|
|
772
|
+
`,
|
|
773
|
+
[
|
|
774
|
+
"Then add middleware.ts at the project root. In a Next.js project, run `rebilder init --framework next` instead.",
|
|
775
|
+
`Full guide: ${DOCS}/runtimes#vercel`
|
|
776
|
+
]
|
|
777
|
+
)
|
|
527
778
|
};
|
|
528
779
|
|
|
529
780
|
// src/disclosure.ts
|
|
@@ -596,6 +847,20 @@ function statusOf(report) {
|
|
|
596
847
|
return report.kind === "rejected" ? "rejected" : report.result.outcome.kind;
|
|
597
848
|
}
|
|
598
849
|
|
|
850
|
+
// src/markdown-copy.ts
|
|
851
|
+
var NEGOTIATED_RESPONSE = "machine-representation.negotiated-response";
|
|
852
|
+
function markdownCopyOf(result) {
|
|
853
|
+
if (result.cost.negotiatedBytes !== null) return "page-address";
|
|
854
|
+
for (const dimension of result.dimensions) {
|
|
855
|
+
for (const check of dimension.checks) {
|
|
856
|
+
if (check.id === NEGOTIATED_RESPONSE) return check.earned > 0 ? "linked" : "none";
|
|
857
|
+
}
|
|
858
|
+
}
|
|
859
|
+
return "none";
|
|
860
|
+
}
|
|
861
|
+
var LINKED_COPY_NOTE = "Your linked Markdown copy works. Send it from the page address too for full credit.";
|
|
862
|
+
var LINKED_PARITY_NOTE = "This page does not send a Markdown copy itself. These facts come from the Markdown copy it links to.";
|
|
863
|
+
|
|
599
864
|
// src/format/json.ts
|
|
600
865
|
function renderJson(payload, ctx) {
|
|
601
866
|
return JSON.stringify(jsonValue(payload, ctx), null, 2) + "\n";
|
|
@@ -630,6 +895,10 @@ function jsonValue(payload, ctx) {
|
|
|
630
895
|
agent: payload.diff.agent,
|
|
631
896
|
browser: payload.diff.browser,
|
|
632
897
|
substanceParity: payload.diff.parity,
|
|
898
|
+
// Where the Markdown copy came from, so a consumer knows what the
|
|
899
|
+
// parity check compared: `page-address` (negotiation), `linked` (the
|
|
900
|
+
// copy the page links to, ARS 0.3), or `none`.
|
|
901
|
+
markdownCopy: payload.diff.result === null ? null : markdownCopyOf(payload.diff.result),
|
|
633
902
|
result: payload.diff.result
|
|
634
903
|
};
|
|
635
904
|
case "init":
|
|
@@ -814,15 +1083,26 @@ function nextSteps(result) {
|
|
|
814
1083
|
const missing = result.factProfile.core.filter((kind) => !found.has(kind));
|
|
815
1084
|
const steps = [];
|
|
816
1085
|
if (missing.length > 0) {
|
|
817
|
-
steps.push(
|
|
1086
|
+
steps.push(
|
|
1087
|
+
`Check these business details: ${missing.join(", ")}. They were not detected in this capture (heuristic); verify the page before adding or changing content.`
|
|
1088
|
+
);
|
|
818
1089
|
} else {
|
|
819
|
-
steps.push(
|
|
1090
|
+
steps.push(
|
|
1091
|
+
"The expected core business details were detected (heuristic). Review their accuracy against your current products, services and policies."
|
|
1092
|
+
);
|
|
820
1093
|
}
|
|
821
1094
|
const first = result.recommendations[0];
|
|
822
1095
|
if (first !== void 0) steps.push(`Start with: ${first.title}. ${first.detail}`);
|
|
823
|
-
|
|
824
|
-
steps.push(
|
|
825
|
-
|
|
1096
|
+
const copy = markdownCopyOf(result);
|
|
1097
|
+
steps.push(
|
|
1098
|
+
copy === "page-address" ? "Compare the two responses with rebilder diff <url> and confirm the same business facts reach customers and assistants." : copy === "linked" ? `${LINKED_COPY_NOTE} Run rebilder init in your project to set that up, then connect your real data before deployment.` : "To connect your existing information, run rebilder init in your project. Review the generated configuration and connect your real data before deployment."
|
|
1099
|
+
);
|
|
1100
|
+
steps.push(
|
|
1101
|
+
"After updating your site, run rebilder check <url> again. Export --format markdown --out review.md to share the findings with your team."
|
|
1102
|
+
);
|
|
1103
|
+
steps.push(
|
|
1104
|
+
"This scan measures page readability; it does not measure customer demand, AI recommendations or sales."
|
|
1105
|
+
);
|
|
826
1106
|
return steps;
|
|
827
1107
|
}
|
|
828
1108
|
|
|
@@ -941,6 +1221,9 @@ function diffSection(diff) {
|
|
|
941
1221
|
"Compared on price, currency, availability and title only \u2014 never description or free text.",
|
|
942
1222
|
""
|
|
943
1223
|
);
|
|
1224
|
+
if (diff.result !== null && markdownCopyOf(diff.result) === "linked") {
|
|
1225
|
+
out.push(LINKED_PARITY_NOTE, "");
|
|
1226
|
+
}
|
|
944
1227
|
for (const evidence of diff.parity.evidence) {
|
|
945
1228
|
out.push(
|
|
946
1229
|
`- ${escapeCell(evidence.label)}: ${escapeCell(evidence.value)} (${basisLabel(evidence.basis)})`
|
|
@@ -951,20 +1234,20 @@ function diffSection(diff) {
|
|
|
951
1234
|
return out.join("\n");
|
|
952
1235
|
}
|
|
953
1236
|
function initSection(payload, ctx) {
|
|
954
|
-
const { scaffold } = payload;
|
|
1237
|
+
const { scaffold: scaffold2 } = payload;
|
|
955
1238
|
const out = [
|
|
956
|
-
`## Rebilder gateway scaffold \u2014 ${
|
|
1239
|
+
`## Rebilder gateway scaffold \u2014 ${scaffold2.framework}`,
|
|
957
1240
|
"",
|
|
958
|
-
|
|
1241
|
+
scaffold2.detection,
|
|
959
1242
|
"",
|
|
960
|
-
`Suggested path: \`${
|
|
1243
|
+
`Suggested path: \`${scaffold2.suggestedPath}\``,
|
|
961
1244
|
"",
|
|
962
|
-
"```" +
|
|
963
|
-
|
|
1245
|
+
"```" + scaffold2.language,
|
|
1246
|
+
scaffold2.contents.trimEnd(),
|
|
964
1247
|
"```",
|
|
965
1248
|
""
|
|
966
1249
|
];
|
|
967
|
-
for (const note of
|
|
1250
|
+
for (const note of scaffold2.notes) out.push(`- ${note}`);
|
|
968
1251
|
out.push("", `<!-- generated by rebilder ${ctx.cliVersion} -->`, "");
|
|
969
1252
|
return out.join("\n");
|
|
970
1253
|
}
|
|
@@ -1329,6 +1612,9 @@ function diffBlock(diff, pretty) {
|
|
|
1329
1612
|
"gray"
|
|
1330
1613
|
)
|
|
1331
1614
|
);
|
|
1615
|
+
if (diff.result !== null && markdownCopyOf(diff.result) === "linked") {
|
|
1616
|
+
lines.push(paint(` ${LINKED_PARITY_NOTE}`, "gray"));
|
|
1617
|
+
}
|
|
1332
1618
|
for (const evidence of diff.parity.evidence) {
|
|
1333
1619
|
lines.push(
|
|
1334
1620
|
` ${glyphs.bullet} ${evidence.label}: ${evidence.value} [${basisLabel(evidence.basis)}]`
|
|
@@ -1346,19 +1632,19 @@ function diffBlock(diff, pretty) {
|
|
|
1346
1632
|
function text(value, unicode) {
|
|
1347
1633
|
return value === void 0 || value === null ? absent(unicode) : String(value);
|
|
1348
1634
|
}
|
|
1349
|
-
function initBlock(
|
|
1635
|
+
function initBlock(scaffold2, pretty) {
|
|
1350
1636
|
const { paint } = pretty;
|
|
1351
1637
|
const lines = [
|
|
1352
|
-
paint(`Detected framework: ${
|
|
1353
|
-
paint(` ${
|
|
1638
|
+
paint(`Detected framework: ${scaffold2.framework}`, "bold"),
|
|
1639
|
+
paint(` ${scaffold2.detection}`, "gray"),
|
|
1354
1640
|
"",
|
|
1355
|
-
paint(`Suggested path: ${
|
|
1641
|
+
paint(`Suggested path: ${scaffold2.suggestedPath}`, "gray"),
|
|
1356
1642
|
"",
|
|
1357
|
-
|
|
1643
|
+
scaffold2.contents.trimEnd()
|
|
1358
1644
|
];
|
|
1359
|
-
if (
|
|
1645
|
+
if (scaffold2.notes.length > 0) {
|
|
1360
1646
|
lines.push("");
|
|
1361
|
-
for (const note of
|
|
1647
|
+
for (const note of scaffold2.notes) lines.push(paint(` ${note}`, "gray"));
|
|
1362
1648
|
}
|
|
1363
1649
|
return lines;
|
|
1364
1650
|
}
|
|
@@ -1444,8 +1730,9 @@ var CHECK = `rebilder check <url...>
|
|
|
1444
1730
|
|
|
1445
1731
|
Probes each URL twice \u2014 an agent request and a browser control that differ in
|
|
1446
1732
|
the Accept header and in nothing else \u2014 plus /robots.txt, /llms.txt and
|
|
1447
|
-
/.well-known/ucp per origin.
|
|
1448
|
-
|
|
1733
|
+
/.well-known/ucp per origin. When the page does not send a Markdown copy but
|
|
1734
|
+
links one on the same site, that one copy is fetched too. Nothing else is
|
|
1735
|
+
requested. robots.txt is fetched first and obeyed.
|
|
1449
1736
|
|
|
1450
1737
|
OPTIONS
|
|
1451
1738
|
--format ${FORMATS.join("|")} default pretty
|
|
@@ -1471,7 +1758,8 @@ var DIFF = `rebilder diff <url>
|
|
|
1471
1758
|
|
|
1472
1759
|
Shows what the agent request received next to what the browser request
|
|
1473
1760
|
received: status, content type, decoded bytes, an estimated token count, and
|
|
1474
|
-
the D2.4 substance-parity comparison verbatim.
|
|
1761
|
+
the D2.4 substance-parity comparison verbatim. When the page links a Markdown
|
|
1762
|
+
copy instead of sending one, that comparison uses the linked copy.
|
|
1475
1763
|
|
|
1476
1764
|
It shows structure and numbers, never an excerpt of the page. --fail-on is not
|
|
1477
1765
|
supported: diff reports, it does not grade.
|
|
@@ -1488,7 +1776,9 @@ var INIT = `rebilder init
|
|
|
1488
1776
|
reads nothing outside the working directory.
|
|
1489
1777
|
|
|
1490
1778
|
OPTIONS
|
|
1491
|
-
--framework next
|
|
1779
|
+
--framework <name> override detection: next, node, edge, shopify,
|
|
1780
|
+
fetch, sveltekit, nuxt, astro, react-router,
|
|
1781
|
+
remix, hono, bun, deno, netlify, vercel
|
|
1492
1782
|
--out <file> write the scaffold to a file
|
|
1493
1783
|
--format ${FORMATS.join("|")} junit is refused: init makes no observations
|
|
1494
1784
|
|
|
@@ -1522,7 +1812,7 @@ function helpText(topic) {
|
|
|
1522
1812
|
}
|
|
1523
1813
|
|
|
1524
1814
|
// src/cli.ts
|
|
1525
|
-
var CLI_VERSION = "0.
|
|
1815
|
+
var CLI_VERSION = "0.4.0";
|
|
1526
1816
|
async function runCli(argv, runtime) {
|
|
1527
1817
|
const caps = detectCapabilities({
|
|
1528
1818
|
env: runtime.env,
|
|
@@ -1582,8 +1872,8 @@ async function execute(command, runtime) {
|
|
|
1582
1872
|
return { payload: { command: "diff", diff }, exitCode: exitCodeFor(diff.report, null) };
|
|
1583
1873
|
}
|
|
1584
1874
|
case "init": {
|
|
1585
|
-
const
|
|
1586
|
-
return { payload: { command: "init", scaffold }, exitCode: EXIT.OK };
|
|
1875
|
+
const scaffold2 = await runInit(command, runtime);
|
|
1876
|
+
return { payload: { command: "init", scaffold: scaffold2 }, exitCode: EXIT.OK };
|
|
1587
1877
|
}
|
|
1588
1878
|
case "badge": {
|
|
1589
1879
|
return { payload: { command: "badge", badge: runBadge(command) }, exitCode: EXIT.OK };
|
|
@@ -1598,7 +1888,7 @@ function buildProbe(runtime, urlCount, flags) {
|
|
|
1598
1888
|
});
|
|
1599
1889
|
}
|
|
1600
1890
|
function budgetFor(urlCount) {
|
|
1601
|
-
return Math.max(20, urlCount *
|
|
1891
|
+
return Math.max(20, urlCount * 7 + 4);
|
|
1602
1892
|
}
|
|
1603
1893
|
function policyFor(setup, base) {
|
|
1604
1894
|
return { ...base, allowHttp: setup.allowHttp || base.allowHttp };
|