@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 06:00 UTC

main
ts 100 lines 4.16 KB
Raw
1 import * as Effect from "effect/Effect";
2 import * as FileSystem from "effect/FileSystem";
3 import * as Layer from "effect/Layer";
4 import * as Path from "effect/Path";
5 import { toOutputFile } from "../core/BuildOutput.ts";
6 import { DeployTargetError } from "../core/DeployTarget.ts";
7 import type { FrameworkBuildOptions } from "../core/Framework.ts";
8 import { finishNeonOutput, makeNeonServeEntrySource, makeNeonTarget } from "../core/NeonServe.ts";
9 import { pinNodeServeModule } from "../core/NodeServe.ts";
10 import {
11 make as makeNode,
12 makeNodeTarget,
13 NEXT_PRODUCTION_APP_SOURCE,
14 type NextjsNodeOptions,
15 } from "./node.ts";
16
17 /** Next.js production Node handler adapted to Neon's Fetch interface. */
18 export const target = (config?: Parameters<typeof makeNodeTarget>[0]) => {
19 const node = makeNodeTarget(config);
20 return {
21 ...makeNeonTarget(node),
22 build: (context: Parameters<NonNullable<typeof node.build>>[0]) =>
23 Effect.gen(function* () {
24 const fs = yield* FileSystem.FileSystem;
25 const path = yield* Path.Path;
26 const output = yield* node.build!(context).pipe(Effect.flatMap(finishNeonOutput));
27 const nodeServe = {
28 ...output.nodeServe,
29 handler: {
30 kind: "fetch" as const,
31 imports: [
32 'import requestMeta from "next/dist/server/request-meta.js";',
33 'import { toFetchHandler } from "./neon-node-adapter.mjs";',
34 NEXT_PRODUCTION_APP_SOURCE,
35 "const nextHandler = app.getRequestHandler();",
36 "const fetchNext = request => {",
37 " const url = new URL(request.url);",
38 " return toFetchHandler((req, res) => {",
39 " const protocol = url.protocol.slice(0, -1);",
40 " req.headers.host = url.host;",
41 ' req.headers["x-forwarded-host"] = url.host;',
42 ' req.headers["x-forwarded-proto"] = protocol;',
43 ' req.headers["x-forwarded-port"] = url.port || (protocol === "https" ? "443" : "80");',
44 " const meta = requestMeta.getRequestMeta(req);",
45 " // Fetch supplies the initial URL; Next's router and renderer must not replace it with a listening address.",
46 " Object.defineProperties(meta, {",
47 " initURL: { enumerable: true, get: () => request.url, set() {} },",
48 " initProtocol: { enumerable: true, get: () => protocol, set() {} },",
49 " });",
50 " requestMeta.setRequestMeta(req, meta);",
51 " Promise.resolve(nextHandler(req, res)).catch(error => res.destroy(error));",
52 " })(request);",
53 "};",
54 ].join("\n"),
55 expr: "fetchNext",
56 },
57 };
58 const name = output.serverModules![0]!.name;
59 const source = makeNeonServeEntrySource(nodeServe);
60 yield* fs.writeFileString(path.join(output.distDirectory!, name), source);
61 return pinNodeServeModule({ ...output, nodeServe }, yield* toOutputFile(name, source));
62 }).pipe(
63 Effect.mapError((cause) =>
64 cause instanceof DeployTargetError
65 ? cause
66 : new DeployTargetError({
67 platform: "neon",
68 message: "Failed to emit the Next.js Fetch entry.",
69 cause,
70 }),
71 ),
72 ),
73 };
74 };
75 export default target;
76
77 /** Native Next.js development with Neon-compatible production output. */
78 export const make = Effect.fn(function* (options: NextjsNodeOptions = {}) {
79 const fs = yield* FileSystem.FileSystem;
80 const path = yield* Path.Path;
81 const node = yield* makeNode(options);
82 const services = Layer.mergeAll(
83 Layer.succeed(FileSystem.FileSystem)(fs),
84 Layer.succeed(Path.Path)(path),
85 );
86 return {
87 dev: node.dev,
88 build: (buildOptions?: FrameworkBuildOptions) =>
89 Effect.gen(function* () {
90 const root = yield* Effect.sync(() =>
91 path.resolve(buildOptions?.root ?? options.root ?? process.cwd()),
92 );
93 return yield* target({ root }).build!({
94 root,
95 framework: "nextjs",
96 env: buildOptions?.env,
97 });
98 }).pipe(Effect.provide(services)),
99 };
100 });