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