rebilder 0.2.1 → 0.3.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 +32 -0
- package/README.md +44 -60
- package/dist/bin.js +388 -133
- package/dist/bin.js.map +1 -1
- package/dist/index.d.ts +16 -21
- package/dist/index.js +388 -133
- package/dist/index.js.map +1 -1
- package/package.json +12 -7
- 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
|
@@ -337,66 +337,113 @@ function approxTokensOf(capture) {
|
|
|
337
337
|
}
|
|
338
338
|
|
|
339
339
|
// src/commands/init.ts
|
|
340
|
-
var FRAMEWORKS = [
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
340
|
+
var FRAMEWORKS = [
|
|
341
|
+
"next",
|
|
342
|
+
"node",
|
|
343
|
+
"edge",
|
|
344
|
+
"shopify",
|
|
345
|
+
"fetch",
|
|
346
|
+
"sveltekit",
|
|
347
|
+
"nuxt",
|
|
348
|
+
"astro",
|
|
349
|
+
"react-router",
|
|
350
|
+
"remix",
|
|
351
|
+
"hono",
|
|
352
|
+
"bun",
|
|
353
|
+
"deno",
|
|
354
|
+
"netlify",
|
|
355
|
+
"vercel"
|
|
356
356
|
];
|
|
357
|
-
var
|
|
357
|
+
var FRAMEWORK_FILES = [
|
|
358
|
+
{ file: "shopify.app.toml", framework: "shopify" },
|
|
359
|
+
{ file: "next.config.ts", framework: "next" },
|
|
360
|
+
{ file: "next.config.mjs", framework: "next" },
|
|
361
|
+
{ file: "next.config.js", framework: "next" },
|
|
362
|
+
{ file: "svelte.config.js", framework: "sveltekit" },
|
|
363
|
+
{ file: "svelte.config.ts", framework: "sveltekit" },
|
|
364
|
+
{ file: "nuxt.config.ts", framework: "nuxt" },
|
|
365
|
+
{ file: "nuxt.config.js", framework: "nuxt" },
|
|
366
|
+
{ file: "nuxt.config.mjs", framework: "nuxt" },
|
|
367
|
+
{ file: "astro.config.mjs", framework: "astro" },
|
|
368
|
+
{ file: "astro.config.ts", framework: "astro" },
|
|
369
|
+
{ file: "astro.config.js", framework: "astro" },
|
|
370
|
+
{ file: "react-router.config.ts", framework: "react-router" },
|
|
371
|
+
{ file: "react-router.config.js", framework: "react-router" },
|
|
372
|
+
{ file: "remix.config.js", framework: "remix" }
|
|
373
|
+
];
|
|
374
|
+
var FRAMEWORK_DEPENDENCIES = [
|
|
358
375
|
{ dependency: "@shopify/shopify-app-remix", framework: "shopify" },
|
|
376
|
+
{ dependency: "@shopify/shopify-app-react-router", framework: "shopify" },
|
|
359
377
|
{ dependency: "@shopify/shopify-api", framework: "shopify" },
|
|
360
378
|
{ dependency: "next", framework: "next" },
|
|
361
|
-
{ dependency: "@
|
|
362
|
-
{ dependency: "
|
|
363
|
-
{ dependency: "
|
|
379
|
+
{ dependency: "@sveltejs/kit", framework: "sveltekit" },
|
|
380
|
+
{ dependency: "nuxt", framework: "nuxt" },
|
|
381
|
+
{ dependency: "astro", framework: "astro" },
|
|
382
|
+
{ dependency: "@react-router/dev", framework: "react-router" },
|
|
383
|
+
{ dependency: "@react-router/node", framework: "react-router" },
|
|
384
|
+
{ dependency: "@remix-run/node", framework: "remix" },
|
|
385
|
+
{ dependency: "@remix-run/express", framework: "remix" },
|
|
386
|
+
{ dependency: "hono", framework: "hono" },
|
|
364
387
|
{ dependency: "express", framework: "node" },
|
|
365
388
|
{ dependency: "fastify", framework: "node" }
|
|
366
389
|
];
|
|
367
|
-
|
|
368
|
-
|
|
390
|
+
var RUNTIME_FILES = [
|
|
391
|
+
{ file: "wrangler.toml", framework: "edge" },
|
|
392
|
+
{ file: "wrangler.jsonc", framework: "edge" },
|
|
393
|
+
{ file: "wrangler.json", framework: "edge" },
|
|
394
|
+
{ file: "netlify.toml", framework: "netlify" },
|
|
395
|
+
{ file: "vercel.json", framework: "vercel" },
|
|
396
|
+
{ file: "deno.json", framework: "deno" },
|
|
397
|
+
{ file: "deno.jsonc", framework: "deno" },
|
|
398
|
+
{ file: "bunfig.toml", framework: "bun" },
|
|
399
|
+
{ file: "bun.lock", framework: "bun" },
|
|
400
|
+
{ file: "bun.lockb", framework: "bun" }
|
|
401
|
+
];
|
|
402
|
+
var RUNTIME_DEPENDENCIES = [
|
|
403
|
+
{ dependency: "wrangler", framework: "edge" },
|
|
404
|
+
{ dependency: "@cloudflare/workers-types", framework: "edge" },
|
|
405
|
+
{ dependency: "@netlify/edge-functions", framework: "netlify" },
|
|
406
|
+
{ dependency: "@vercel/functions", framework: "vercel" }
|
|
407
|
+
];
|
|
408
|
+
async function firstFile(runtime, markers) {
|
|
409
|
+
for (const marker of markers) {
|
|
369
410
|
if (await runtime.readFile(marker.file) !== null) {
|
|
370
|
-
return {
|
|
411
|
+
return {
|
|
412
|
+
framework: marker.framework,
|
|
413
|
+
reason: `detected from ${marker.file} in the working directory`
|
|
414
|
+
};
|
|
371
415
|
}
|
|
372
416
|
}
|
|
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
|
-
}
|
|
417
|
+
return null;
|
|
418
|
+
}
|
|
419
|
+
function firstDependency(dependencies, markers) {
|
|
420
|
+
for (const marker of markers) {
|
|
421
|
+
if (marker.dependency in dependencies) {
|
|
422
|
+
return {
|
|
423
|
+
framework: marker.framework,
|
|
424
|
+
reason: `detected from "${marker.dependency}" in package.json`
|
|
425
|
+
};
|
|
395
426
|
}
|
|
396
427
|
}
|
|
397
|
-
return
|
|
398
|
-
|
|
399
|
-
|
|
428
|
+
return null;
|
|
429
|
+
}
|
|
430
|
+
async function readDependencies(runtime) {
|
|
431
|
+
const manifest = await runtime.readFile("package.json");
|
|
432
|
+
if (manifest === null) return {};
|
|
433
|
+
try {
|
|
434
|
+
const parsed = JSON.parse(manifest);
|
|
435
|
+
if (typeof parsed !== "object" || parsed === null) return {};
|
|
436
|
+
const record = parsed;
|
|
437
|
+
return { ...asRecord(record["dependencies"]), ...asRecord(record["devDependencies"]) };
|
|
438
|
+
} catch {
|
|
439
|
+
return {};
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
async function detectFramework(runtime) {
|
|
443
|
+
const dependencies = await readDependencies(runtime);
|
|
444
|
+
return await firstFile(runtime, FRAMEWORK_FILES) ?? firstDependency(dependencies, FRAMEWORK_DEPENDENCIES) ?? await firstFile(runtime, RUNTIME_FILES) ?? firstDependency(dependencies, RUNTIME_DEPENDENCIES) ?? {
|
|
445
|
+
framework: "fetch",
|
|
446
|
+
reason: `no framework marker found \u2014 scaffolding the framework-neutral fetch middleware. Pass --framework ${FRAMEWORKS.join("|")} to choose.`
|
|
400
447
|
};
|
|
401
448
|
}
|
|
402
449
|
function asRecord(value) {
|
|
@@ -423,7 +470,7 @@ async function runInit(command, runtime) {
|
|
|
423
470
|
}
|
|
424
471
|
var SHARED_NOTES = [
|
|
425
472
|
"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
|
|
473
|
+
"The Apache-2.0 SDK serves without a Rebilder account or subscription. Configure reporting separately.",
|
|
427
474
|
"Run `rebilder check <url>` before and after to see what changed. Content negotiation is D2.1 (10 points) and unlocks D2.3 and D2.4."
|
|
428
475
|
];
|
|
429
476
|
var CONFIG_BODY = `import type { GatewayConfig } from '@rebilder/gateway'
|
|
@@ -449,81 +496,287 @@ export const gatewayConfig: GatewayConfig = {
|
|
|
449
496
|
// onEvent: (event) => { void event },
|
|
450
497
|
}
|
|
451
498
|
`;
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
499
|
+
function scaffold(suggestedPath, mount, notes) {
|
|
500
|
+
const [header = "", ...body] = mount.trim().split("\n");
|
|
501
|
+
const commented = body.map((line) => line === "" ? "//" : `// ${line}`);
|
|
502
|
+
return {
|
|
503
|
+
suggestedPath,
|
|
455
504
|
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
|
-
],
|
|
505
|
+
notes: [...notes, ...SHARED_NOTES],
|
|
460
506
|
contents: `${CONFIG_BODY}
|
|
461
|
-
|
|
507
|
+
${header}
|
|
462
508
|
//
|
|
463
|
-
|
|
464
|
-
// import { createGatewayProxy } from '@rebilder/gateway/next'
|
|
465
|
-
// import { gatewayConfig } from './lib/gateway-config'
|
|
466
|
-
//
|
|
467
|
-
// const gateway = createGatewayProxy(gatewayConfig)
|
|
468
|
-
//
|
|
469
|
-
// export default async function proxy(req: Request) {
|
|
470
|
-
// return (await gateway(req)) ?? NextResponse.next()
|
|
471
|
-
// }
|
|
509
|
+
${commented.join("\n")}
|
|
472
510
|
`
|
|
473
|
-
}
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
511
|
+
};
|
|
512
|
+
}
|
|
513
|
+
var DOCS = "https://rebilder.com/docs/adapters";
|
|
514
|
+
var SCAFFOLDS = {
|
|
515
|
+
next: scaffold(
|
|
516
|
+
"lib/gateway-config.ts",
|
|
517
|
+
`
|
|
518
|
+
/* \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 */
|
|
519
|
+
import { NextResponse } from 'next/server'
|
|
520
|
+
import { createGatewayProxy } from '@rebilder/gateway/next'
|
|
521
|
+
import { gatewayConfig } from './lib/gateway-config'
|
|
522
|
+
|
|
523
|
+
// Passing the fallthrough adds Vary: Accept to your HTML responses too.
|
|
524
|
+
export default createGatewayProxy(gatewayConfig, () => NextResponse.next())
|
|
525
|
+
`,
|
|
526
|
+
[
|
|
527
|
+
"Then add the proxy: `createGatewayProxy(gatewayConfig, () => NextResponse.next())` in proxy.ts (Next 16) or middleware.ts (Next 15 and earlier).",
|
|
528
|
+
`Full guide: ${DOCS}/nextjs`
|
|
529
|
+
]
|
|
530
|
+
),
|
|
531
|
+
node: scaffold(
|
|
532
|
+
"gateway-config.ts",
|
|
533
|
+
`
|
|
482
534
|
/* \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
|
-
|
|
535
|
+
import express from 'express'
|
|
536
|
+
import { createGatewayMiddleware } from '@rebilder/gateway/node'
|
|
537
|
+
import { gatewayConfig } from './gateway-config'
|
|
538
|
+
|
|
539
|
+
const app = express()
|
|
540
|
+
app.use(createGatewayMiddleware(gatewayConfig)) // before your routes
|
|
541
|
+
`,
|
|
542
|
+
[
|
|
543
|
+
"Then mount it before your routes: `app.use(createGatewayMiddleware(gatewayConfig))`.",
|
|
544
|
+
`Fastify and the full guide: ${DOCS}/node`
|
|
545
|
+
]
|
|
546
|
+
),
|
|
547
|
+
edge: scaffold(
|
|
548
|
+
"gateway-config.ts",
|
|
549
|
+
`
|
|
550
|
+
/* \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 */
|
|
551
|
+
import { createGatewayFetchHandler } from '@rebilder/gateway/edge'
|
|
552
|
+
import { gatewayConfig } from './gateway-config'
|
|
553
|
+
|
|
554
|
+
export default { fetch: createGatewayFetchHandler(gatewayConfig) }
|
|
555
|
+
`,
|
|
556
|
+
[
|
|
496
557
|
"Then export the fetch handler: `export default { fetch: createGatewayFetchHandler(gatewayConfig) }`.",
|
|
497
558
|
"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}
|
|
559
|
+
`Full guide: ${DOCS}/cloudflare`
|
|
560
|
+
]
|
|
561
|
+
),
|
|
562
|
+
shopify: scaffold(
|
|
563
|
+
"app/gateway-config.ts",
|
|
564
|
+
`
|
|
517
565
|
/* \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
|
-
|
|
566
|
+
import { createShopifyAppProxyHandler } from '@rebilder/gateway/shopify'
|
|
567
|
+
import { gatewayConfig } from './gateway-config'
|
|
568
|
+
|
|
569
|
+
export const loader = createShopifyAppProxyHandler(gatewayConfig, {
|
|
570
|
+
sharedSecret: process.env.SHOPIFY_API_SECRET ?? '',
|
|
571
|
+
})
|
|
572
|
+
`,
|
|
573
|
+
[
|
|
574
|
+
"Then mount the app-proxy handler and give it your app\u2019s shared secret; requests are signature-verified before anything is rendered.",
|
|
575
|
+
`Full guide: ${DOCS}/shopify`
|
|
576
|
+
]
|
|
577
|
+
),
|
|
578
|
+
fetch: scaffold(
|
|
579
|
+
"gateway-config.ts",
|
|
580
|
+
`
|
|
581
|
+
/* \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 */
|
|
582
|
+
import { createFetchMiddleware } from '@rebilder/gateway/fetch'
|
|
583
|
+
import { gatewayConfig } from './gateway-config'
|
|
584
|
+
|
|
585
|
+
const gateway = createFetchMiddleware(gatewayConfig)
|
|
586
|
+
|
|
587
|
+
// In your middleware: return gateway(request, () => next())
|
|
588
|
+
`,
|
|
589
|
+
[
|
|
590
|
+
"Mount `createFetchMiddleware(gatewayConfig)` wherever your framework hands middleware a Request and a next().",
|
|
591
|
+
`Recipes for SvelteKit, Nuxt, Astro, React Router, Remix and Hono: ${DOCS}/frameworks`
|
|
592
|
+
]
|
|
593
|
+
),
|
|
594
|
+
sveltekit: scaffold(
|
|
595
|
+
"src/lib/gateway-config.ts",
|
|
596
|
+
`
|
|
597
|
+
/* \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 */
|
|
598
|
+
import type { Handle } from '@sveltejs/kit'
|
|
599
|
+
import { createFetchMiddleware } from '@rebilder/gateway/fetch'
|
|
600
|
+
import { gatewayConfig } from '$lib/gateway-config'
|
|
601
|
+
|
|
602
|
+
const gateway = createFetchMiddleware(gatewayConfig)
|
|
603
|
+
|
|
604
|
+
export const handle: Handle = ({ event, resolve }) => gateway(event.request, () => resolve(event))
|
|
605
|
+
`,
|
|
606
|
+
[
|
|
607
|
+
"Then add the `handle` hook in src/hooks.server.ts. If you already have one, combine them with `sequence` from @sveltejs/kit/hooks.",
|
|
608
|
+
`Full guide: ${DOCS}/frameworks#sveltekit`
|
|
609
|
+
]
|
|
610
|
+
),
|
|
611
|
+
nuxt: scaffold(
|
|
612
|
+
"server/utils/gateway-config.ts",
|
|
613
|
+
`
|
|
614
|
+
/* \u2500\u2500 server/middleware/rebilder.ts (Nuxt and Nitro; h3 helpers are auto-imported) \u2500\u2500 */
|
|
615
|
+
import { handleRequest, negotiationHeaders } from '@rebilder/gateway/fetch'
|
|
616
|
+
import { gatewayConfig } from '../utils/gateway-config'
|
|
617
|
+
|
|
618
|
+
export default defineEventHandler(async (event) => {
|
|
619
|
+
if (event.method !== 'GET' && event.method !== 'HEAD') return
|
|
620
|
+
const served = await handleRequest(toWebRequest(event), gatewayConfig)
|
|
621
|
+
if (served) return served
|
|
622
|
+
for (const [name, value] of negotiationHeaders(gatewayConfig, getRequestURL(event))) {
|
|
623
|
+
appendResponseHeader(event, name, value)
|
|
526
624
|
}
|
|
625
|
+
})
|
|
626
|
+
`,
|
|
627
|
+
[
|
|
628
|
+
"Then add the server middleware. It returns markdown to agents and otherwise returns nothing, so Nuxt renders the page as usual.",
|
|
629
|
+
`Full guide: ${DOCS}/frameworks#nuxt`
|
|
630
|
+
]
|
|
631
|
+
),
|
|
632
|
+
astro: scaffold(
|
|
633
|
+
"src/gateway-config.ts",
|
|
634
|
+
`
|
|
635
|
+
/* \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 */
|
|
636
|
+
import { defineMiddleware } from 'astro:middleware'
|
|
637
|
+
import { createFetchMiddleware } from '@rebilder/gateway/fetch'
|
|
638
|
+
import { gatewayConfig } from './gateway-config'
|
|
639
|
+
|
|
640
|
+
const gateway = createFetchMiddleware(gatewayConfig)
|
|
641
|
+
|
|
642
|
+
export const onRequest = defineMiddleware((context, next) => gateway(context.request, () => next()))
|
|
643
|
+
`,
|
|
644
|
+
[
|
|
645
|
+
'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`).',
|
|
646
|
+
`Full guide: ${DOCS}/frameworks#astro`
|
|
647
|
+
]
|
|
648
|
+
),
|
|
649
|
+
"react-router": scaffold(
|
|
650
|
+
"app/gateway-config.server.ts",
|
|
651
|
+
`
|
|
652
|
+
/* \u2500\u2500 app/root.tsx (React Router 8, or 7.9+ with future.v8_middleware) \u2500\u2500\u2500\u2500\u2500\u2500\u2500 */
|
|
653
|
+
import type { Route } from './+types/root'
|
|
654
|
+
import { createFetchMiddleware } from '@rebilder/gateway/fetch'
|
|
655
|
+
import { gatewayConfig } from './gateway-config.server'
|
|
656
|
+
|
|
657
|
+
const gateway = createFetchMiddleware(gatewayConfig)
|
|
658
|
+
|
|
659
|
+
export const middleware: Route.MiddlewareFunction[] = [({ request }, next) => gateway(request, next)]
|
|
660
|
+
`,
|
|
661
|
+
[
|
|
662
|
+
"Then export `middleware` from app/root.tsx. On React Router 7, enable `future: { v8_middleware: true }` in react-router.config.ts first.",
|
|
663
|
+
`Full guide: ${DOCS}/frameworks#react-router`
|
|
664
|
+
]
|
|
665
|
+
),
|
|
666
|
+
remix: scaffold(
|
|
667
|
+
"app/gateway-config.server.ts",
|
|
668
|
+
`
|
|
669
|
+
/* \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 */
|
|
670
|
+
import express from 'express'
|
|
671
|
+
import { createRequestHandler } from '@remix-run/express'
|
|
672
|
+
import { createGatewayMiddleware } from '@rebilder/gateway/node'
|
|
673
|
+
import { gatewayConfig } from './app/gateway-config.server'
|
|
674
|
+
|
|
675
|
+
const app = express()
|
|
676
|
+
app.use(createGatewayMiddleware(gatewayConfig)) // before Remix
|
|
677
|
+
app.all('*', createRequestHandler({ build: await import('./build/server/index.js') }))
|
|
678
|
+
`,
|
|
679
|
+
[
|
|
680
|
+
"Remix v2 has no middleware, so the gateway runs as Express middleware in front of `createRequestHandler`.",
|
|
681
|
+
`Full guide: ${DOCS}/frameworks#remix`
|
|
682
|
+
]
|
|
683
|
+
),
|
|
684
|
+
hono: scaffold(
|
|
685
|
+
"src/gateway-config.ts",
|
|
686
|
+
`
|
|
687
|
+
/* \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 */
|
|
688
|
+
import { Hono } from 'hono'
|
|
689
|
+
import { createFetchMiddleware } from '@rebilder/gateway/fetch'
|
|
690
|
+
import { gatewayConfig } from './gateway-config'
|
|
691
|
+
|
|
692
|
+
const gateway = createFetchMiddleware(gatewayConfig)
|
|
693
|
+
const app = new Hono()
|
|
694
|
+
|
|
695
|
+
app.use(async (c, next) => {
|
|
696
|
+
let passed = false
|
|
697
|
+
const res = await gateway(c.req.raw, async () => {
|
|
698
|
+
passed = true
|
|
699
|
+
await next()
|
|
700
|
+
return c.res
|
|
701
|
+
})
|
|
702
|
+
if (!passed) return res // markdown, answered before any route ran
|
|
703
|
+
c.res = undefined // clear first, so Hono does not copy the old headers over ours
|
|
704
|
+
c.res = res
|
|
705
|
+
})
|
|
706
|
+
`,
|
|
707
|
+
[
|
|
708
|
+
"Register the middleware before your routes. It works the same on Workers, Bun, Deno and Node.",
|
|
709
|
+
`Full guide: ${DOCS}/frameworks#hono`
|
|
710
|
+
]
|
|
711
|
+
),
|
|
712
|
+
bun: scaffold(
|
|
713
|
+
"gateway-config.ts",
|
|
714
|
+
`
|
|
715
|
+
/* \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 */
|
|
716
|
+
import { createFetchMiddleware } from '@rebilder/gateway/fetch'
|
|
717
|
+
import { gatewayConfig } from './gateway-config'
|
|
718
|
+
import { app } from './app' // TODO: your existing (request) => Response handler
|
|
719
|
+
|
|
720
|
+
const gateway = createFetchMiddleware(gatewayConfig)
|
|
721
|
+
|
|
722
|
+
Bun.serve({ fetch: (request) => gateway(request, () => app(request)) })
|
|
723
|
+
`,
|
|
724
|
+
[
|
|
725
|
+
"Wrap your existing fetch handler. Requests served by Bun.serve `routes` skip `fetch`, so wrap those handlers too.",
|
|
726
|
+
`Full guide: ${DOCS}/runtimes#bun`
|
|
727
|
+
]
|
|
728
|
+
),
|
|
729
|
+
deno: scaffold(
|
|
730
|
+
"gateway-config.ts",
|
|
731
|
+
`
|
|
732
|
+
/* \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 */
|
|
733
|
+
import { createFetchMiddleware } from '@rebilder/gateway/fetch'
|
|
734
|
+
import { gatewayConfig } from './gateway-config.ts'
|
|
735
|
+
import { app } from './app.ts' // TODO: your existing (request) => Response handler
|
|
736
|
+
|
|
737
|
+
const gateway = createFetchMiddleware(gatewayConfig)
|
|
738
|
+
|
|
739
|
+
Deno.serve((request) => gateway(request, () => app(request)))
|
|
740
|
+
`,
|
|
741
|
+
["Wrap your existing handler in Deno.serve.", `Full guide: ${DOCS}/runtimes#deno`]
|
|
742
|
+
),
|
|
743
|
+
netlify: scaffold(
|
|
744
|
+
"gateway-config.ts",
|
|
745
|
+
`
|
|
746
|
+
/* \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 */
|
|
747
|
+
import type { Config, Context } from '@netlify/edge-functions'
|
|
748
|
+
import { createFetchMiddleware } from '@rebilder/gateway/fetch'
|
|
749
|
+
import { gatewayConfig } from '../../gateway-config.ts'
|
|
750
|
+
|
|
751
|
+
const gateway = createFetchMiddleware(gatewayConfig)
|
|
752
|
+
|
|
753
|
+
export default (request: Request, context: Context) => gateway(request, () => context.next())
|
|
754
|
+
|
|
755
|
+
export const config: Config = { path: '/*', excludedPath: ['/*.css', '/*.js'] }
|
|
756
|
+
`,
|
|
757
|
+
[
|
|
758
|
+
"Then add the edge function. It answers agents at the edge and passes everything else to your site with `context.next()`.",
|
|
759
|
+
`Full guide: ${DOCS}/runtimes#netlify`
|
|
760
|
+
]
|
|
761
|
+
),
|
|
762
|
+
vercel: scaffold(
|
|
763
|
+
"gateway-config.ts",
|
|
764
|
+
`
|
|
765
|
+
/* \u2500\u2500 middleware.ts (Vercel Routing Middleware, outside Next.js) \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500 */
|
|
766
|
+
import { next } from '@vercel/functions'
|
|
767
|
+
import { handleRequest, negotiationHeaders } from '@rebilder/gateway/fetch'
|
|
768
|
+
import { gatewayConfig } from './gateway-config'
|
|
769
|
+
|
|
770
|
+
export default async function middleware(request: Request) {
|
|
771
|
+
const served = await handleRequest(request, gatewayConfig)
|
|
772
|
+
return served ?? next({ headers: Object.fromEntries(negotiationHeaders(gatewayConfig, new URL(request.url))) })
|
|
773
|
+
}
|
|
774
|
+
`,
|
|
775
|
+
[
|
|
776
|
+
"Then add middleware.ts at the project root. In a Next.js project, run `rebilder init --framework next` instead.",
|
|
777
|
+
`Full guide: ${DOCS}/runtimes#vercel`
|
|
778
|
+
]
|
|
779
|
+
)
|
|
527
780
|
};
|
|
528
781
|
|
|
529
782
|
// src/disclosure.ts
|
|
@@ -951,20 +1204,20 @@ function diffSection(diff) {
|
|
|
951
1204
|
return out.join("\n");
|
|
952
1205
|
}
|
|
953
1206
|
function initSection(payload, ctx) {
|
|
954
|
-
const { scaffold } = payload;
|
|
1207
|
+
const { scaffold: scaffold2 } = payload;
|
|
955
1208
|
const out = [
|
|
956
|
-
`## Rebilder gateway scaffold \u2014 ${
|
|
1209
|
+
`## Rebilder gateway scaffold \u2014 ${scaffold2.framework}`,
|
|
957
1210
|
"",
|
|
958
|
-
|
|
1211
|
+
scaffold2.detection,
|
|
959
1212
|
"",
|
|
960
|
-
`Suggested path: \`${
|
|
1213
|
+
`Suggested path: \`${scaffold2.suggestedPath}\``,
|
|
961
1214
|
"",
|
|
962
|
-
"```" +
|
|
963
|
-
|
|
1215
|
+
"```" + scaffold2.language,
|
|
1216
|
+
scaffold2.contents.trimEnd(),
|
|
964
1217
|
"```",
|
|
965
1218
|
""
|
|
966
1219
|
];
|
|
967
|
-
for (const note of
|
|
1220
|
+
for (const note of scaffold2.notes) out.push(`- ${note}`);
|
|
968
1221
|
out.push("", `<!-- generated by rebilder ${ctx.cliVersion} -->`, "");
|
|
969
1222
|
return out.join("\n");
|
|
970
1223
|
}
|
|
@@ -1346,19 +1599,19 @@ function diffBlock(diff, pretty) {
|
|
|
1346
1599
|
function text(value, unicode) {
|
|
1347
1600
|
return value === void 0 || value === null ? absent(unicode) : String(value);
|
|
1348
1601
|
}
|
|
1349
|
-
function initBlock(
|
|
1602
|
+
function initBlock(scaffold2, pretty) {
|
|
1350
1603
|
const { paint } = pretty;
|
|
1351
1604
|
const lines = [
|
|
1352
|
-
paint(`Detected framework: ${
|
|
1353
|
-
paint(` ${
|
|
1605
|
+
paint(`Detected framework: ${scaffold2.framework}`, "bold"),
|
|
1606
|
+
paint(` ${scaffold2.detection}`, "gray"),
|
|
1354
1607
|
"",
|
|
1355
|
-
paint(`Suggested path: ${
|
|
1608
|
+
paint(`Suggested path: ${scaffold2.suggestedPath}`, "gray"),
|
|
1356
1609
|
"",
|
|
1357
|
-
|
|
1610
|
+
scaffold2.contents.trimEnd()
|
|
1358
1611
|
];
|
|
1359
|
-
if (
|
|
1612
|
+
if (scaffold2.notes.length > 0) {
|
|
1360
1613
|
lines.push("");
|
|
1361
|
-
for (const note of
|
|
1614
|
+
for (const note of scaffold2.notes) lines.push(paint(` ${note}`, "gray"));
|
|
1362
1615
|
}
|
|
1363
1616
|
return lines;
|
|
1364
1617
|
}
|
|
@@ -1488,7 +1741,9 @@ var INIT = `rebilder init
|
|
|
1488
1741
|
reads nothing outside the working directory.
|
|
1489
1742
|
|
|
1490
1743
|
OPTIONS
|
|
1491
|
-
--framework next
|
|
1744
|
+
--framework <name> override detection: next, node, edge, shopify,
|
|
1745
|
+
fetch, sveltekit, nuxt, astro, react-router,
|
|
1746
|
+
remix, hono, bun, deno, netlify, vercel
|
|
1492
1747
|
--out <file> write the scaffold to a file
|
|
1493
1748
|
--format ${FORMATS.join("|")} junit is refused: init makes no observations
|
|
1494
1749
|
|
|
@@ -1522,7 +1777,7 @@ function helpText(topic) {
|
|
|
1522
1777
|
}
|
|
1523
1778
|
|
|
1524
1779
|
// src/cli.ts
|
|
1525
|
-
var CLI_VERSION = "0.
|
|
1780
|
+
var CLI_VERSION = "0.3.0";
|
|
1526
1781
|
async function runCli(argv, runtime) {
|
|
1527
1782
|
const caps = detectCapabilities({
|
|
1528
1783
|
env: runtime.env,
|
|
@@ -1582,8 +1837,8 @@ async function execute(command, runtime) {
|
|
|
1582
1837
|
return { payload: { command: "diff", diff }, exitCode: exitCodeFor(diff.report, null) };
|
|
1583
1838
|
}
|
|
1584
1839
|
case "init": {
|
|
1585
|
-
const
|
|
1586
|
-
return { payload: { command: "init", scaffold }, exitCode: EXIT.OK };
|
|
1840
|
+
const scaffold2 = await runInit(command, runtime);
|
|
1841
|
+
return { payload: { command: "init", scaffold: scaffold2 }, exitCode: EXIT.OK };
|
|
1587
1842
|
}
|
|
1588
1843
|
case "badge": {
|
|
1589
1844
|
return { payload: { command: "badge", badge: runBadge(command) }, exitCode: EXIT.OK };
|