@zeltjs/adapter-node 0.8.1 → 0.8.2-canary.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/dist/index.d.ts +8 -25
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +15 -66
- package/dist/index.js.map +1 -1
- package/package.json +3 -2
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { ServerType, createAdaptorServer } from "@hono/node-server";
|
|
2
|
-
import { CliConfig,
|
|
2
|
+
import { CliConfig, ConfiguredFeature, EnvAdaptor, FeatureApp, RuntimeApp, Signal, SignalHandler } from "@zeltjs/core";
|
|
3
3
|
|
|
4
4
|
//#region src/node-cli.config.d.ts
|
|
5
5
|
declare class NodeCliConfig extends CliConfig {
|
|
@@ -26,34 +26,17 @@ type ServerHandle = {
|
|
|
26
26
|
type NodeAppOptions = {
|
|
27
27
|
readonly warmup?: boolean;
|
|
28
28
|
};
|
|
29
|
-
type
|
|
29
|
+
type EnvironmentNodeAppPart = {
|
|
30
30
|
readonly args: readonly string[];
|
|
31
|
-
};
|
|
32
|
-
type HttpNodeApp = ReadyResult & NodeAppBase & {
|
|
33
|
-
readonly listen: (portOrOptions?: number | ListenOptions) => Promise<ServerHandle>;
|
|
34
31
|
readonly shutdown: () => Promise<void>;
|
|
35
32
|
};
|
|
36
|
-
type
|
|
37
|
-
readonly
|
|
38
|
-
readonly shutdown: () => Promise<void>;
|
|
39
|
-
};
|
|
40
|
-
type SchedulerNodeAppPart = {
|
|
41
|
-
readonly startScheduler: () => Promise<void>;
|
|
42
|
-
readonly stopScheduler: () => Promise<void>;
|
|
33
|
+
type HttpNodeAppPart = {
|
|
34
|
+
readonly listen: (portOrOptions?: number | ListenOptions) => Promise<ServerHandle>;
|
|
43
35
|
};
|
|
44
|
-
type
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
declare function onNode(app: HttpApp & SchedulerApp, options?: NodeAppOptions): Promise<HttpNodeApp & SchedulerNodeAppPart>;
|
|
49
|
-
/** @throws {ZeltLifecycleStateError} */
|
|
50
|
-
declare function onNode(app: CommandApp & SchedulerApp, options?: NodeAppOptions): Promise<CommandNodeApp & SchedulerNodeAppPart>;
|
|
51
|
-
/** @throws {ZeltLifecycleStateError} */
|
|
52
|
-
declare function onNode(app: HttpApp & CommandApp, options?: NodeAppOptions): Promise<FullNodeApp>;
|
|
53
|
-
/** @throws {ZeltLifecycleStateError} */
|
|
54
|
-
declare function onNode(app: HttpApp, options?: NodeAppOptions): Promise<HttpNodeApp>;
|
|
55
|
-
/** @throws {ZeltLifecycleStateError} */
|
|
56
|
-
declare function onNode(app: CommandApp, options?: NodeAppOptions): Promise<CommandNodeApp>;
|
|
36
|
+
type FeatureKeys<F extends readonly ConfiguredFeature[]> = F[number]['key'];
|
|
37
|
+
type WithFeature<F extends readonly ConfiguredFeature[], TKey extends string, TPart extends object> = TKey extends FeatureKeys<F> ? TPart : unknown;
|
|
38
|
+
type NodeAppForFeatures<F extends readonly ConfiguredFeature[]> = RuntimeApp<F> & EnvironmentNodeAppPart & (string extends FeatureKeys<F> ? Partial<HttpNodeAppPart> : WithFeature<F, 'http', HttpNodeAppPart>);
|
|
39
|
+
declare function onNode<const F extends readonly ConfiguredFeature[]>(app: FeatureApp<F>, options?: NodeAppOptions): Promise<NodeAppForFeatures<F>>;
|
|
57
40
|
//#endregion
|
|
58
41
|
//#region src/process-env.adaptor.d.ts
|
|
59
42
|
declare class ProcessEnvAdaptor extends EnvAdaptor {
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/node-cli.config.ts","../src/on-node.ts","../src/process-env.adaptor.ts","../src/serve.lib.ts"],"mappings":";;;;cAIa,aAAA,SAAsB,SAAA;EACxB,GAAA,CAAA;EAIA,IAAA,CAAA;EAIA,IAAA,CAAK,IAAA;EAIL,WAAA,CAAY,IAAA;E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,GAChB,OAAA,GAAU,cAAA,GACT,OAAA,CAAQ,kBAAA,CAAmB,CAAA;;;cCnGjB,iBAAA,SAA0B,UAAA;EAC5B,GAAA,CAAI,GAAA;AAAA;;;KCDH,YAAA;EACV,IAAA;EACA,QAAA;AAAA;AAAA,KAGU,WAAA;EACV,IAAA;EACA,OAAA;AAAA;AAAA,KAGG,OAAA;EACH,KAAA,GAAQ,OAAA,EAAS,OAAA,KAAY,OAAA,CAAQ,QAAA;AAAA;AAAA,cAa1B,QAAA,GACX,GAAA,EAAK,OAAA,EACL,iBAAA,GAAoB,YAAA,KAAiB,IAAA,EAAM,WAAA,YAC3C,aAAA,IAAiB,IAAA,EAAM,WAAA,cACtB,UAAA"}
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { createAdaptorServer, serve } from "@hono/node-server";
|
|
2
2
|
import { CliConfig, Config, EnvAdaptor } from "@zeltjs/core";
|
|
3
|
+
import { unsafeGetNamespacedCallable } from "@zeltjs/unsafe-type-lib";
|
|
3
4
|
//#region src/node-cli.config.ts
|
|
4
5
|
function _ts_decorate$1(decorators, target, key, desc) {
|
|
5
6
|
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
@@ -72,75 +73,26 @@ const createListenForHttp = (appFetch, appShutdown) => {
|
|
|
72
73
|
};
|
|
73
74
|
};
|
|
74
75
|
};
|
|
75
|
-
const getStderr = () => globalThis.process.stderr;
|
|
76
76
|
const getArgs = () => globalThis.process.argv.slice(2);
|
|
77
|
-
const
|
|
78
|
-
const
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
...resolver,
|
|
82
|
-
args,
|
|
83
|
-
listen: createListenForHttp(caps.fetch, shutdown),
|
|
84
|
-
shutdown
|
|
85
|
-
};
|
|
86
|
-
};
|
|
87
|
-
const buildCommandNodeApp = (caps, resolver, shutdown, stderr, args) => {
|
|
88
|
-
if (typeof caps.execCommand !== "function") return void 0;
|
|
89
|
-
const coreExecCommand = caps.execCommand;
|
|
90
|
-
const execCommand = async (argv) => {
|
|
91
|
-
const result = await coreExecCommand(argv);
|
|
92
|
-
if (result.exitCode === 1) stderr.write(`${result.reason.message}\n`);
|
|
93
|
-
return result;
|
|
94
|
-
};
|
|
95
|
-
return {
|
|
96
|
-
...resolver,
|
|
77
|
+
const createNodeApp = (readyApp, shutdown, args) => {
|
|
78
|
+
const fetch = unsafeGetNamespacedCallable(readyApp, "http", "fetch");
|
|
79
|
+
const base = {
|
|
80
|
+
...readyApp,
|
|
97
81
|
args,
|
|
98
|
-
execCommand,
|
|
99
82
|
shutdown
|
|
100
83
|
};
|
|
101
|
-
|
|
102
|
-
const buildSchedulerPart = (caps) => {
|
|
103
|
-
if (typeof caps.startScheduler !== "function" || typeof caps.stopScheduler !== "function") return void 0;
|
|
84
|
+
if (typeof fetch !== "function") return base;
|
|
104
85
|
return {
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
};
|
|
108
|
-
};
|
|
109
|
-
const buildNodeApps = (caps, resolver, shutdown, stderr, args) => ({
|
|
110
|
-
httpResult: buildHttpNodeApp(caps, resolver, shutdown, args),
|
|
111
|
-
commandResult: buildCommandNodeApp(caps, resolver, shutdown, stderr, args),
|
|
112
|
-
schedulerPart: buildSchedulerPart(caps)
|
|
113
|
-
});
|
|
114
|
-
const mergeNodeApps = (result, resolver, shutdown, args) => {
|
|
115
|
-
const { httpResult, commandResult, schedulerPart } = result;
|
|
116
|
-
const schedulerMethods = schedulerPart ?? {};
|
|
117
|
-
if (httpResult && commandResult) return {
|
|
118
|
-
...httpResult,
|
|
119
|
-
execCommand: commandResult.execCommand,
|
|
120
|
-
...schedulerMethods
|
|
121
|
-
};
|
|
122
|
-
if (httpResult) return {
|
|
123
|
-
...httpResult,
|
|
124
|
-
...schedulerMethods
|
|
125
|
-
};
|
|
126
|
-
if (commandResult) return {
|
|
127
|
-
...commandResult,
|
|
128
|
-
...schedulerMethods
|
|
129
|
-
};
|
|
130
|
-
return {
|
|
131
|
-
...resolver,
|
|
132
|
-
args,
|
|
133
|
-
shutdown,
|
|
134
|
-
listen: () => new Promise(() => {}),
|
|
135
|
-
...schedulerMethods
|
|
86
|
+
...base,
|
|
87
|
+
listen: createListenForHttp(fetch, shutdown)
|
|
136
88
|
};
|
|
137
89
|
};
|
|
138
90
|
/** @throws {ZeltLifecycleStateError} */ async function onNode(app, options = {}) {
|
|
139
|
-
app.
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
const cliConfig =
|
|
91
|
+
const readyApp = await app.createRuntime({
|
|
92
|
+
fallbackConfigs: [NodeCliConfig, ProcessEnvAdaptor],
|
|
93
|
+
warmup: options.warmup ?? true
|
|
94
|
+
});
|
|
95
|
+
const cliConfig = await readyApp.get(NodeCliConfig);
|
|
144
96
|
let shuttingDown = false;
|
|
145
97
|
const detachSignals = () => {
|
|
146
98
|
cliConfig.offSignal("SIGINT", gracefulShutdown);
|
|
@@ -150,7 +102,7 @@ const mergeNodeApps = (result, resolver, shutdown, args) => {
|
|
|
150
102
|
if (shuttingDown) return;
|
|
151
103
|
shuttingDown = true;
|
|
152
104
|
try {
|
|
153
|
-
await
|
|
105
|
+
await readyApp.shutdown();
|
|
154
106
|
} finally {
|
|
155
107
|
detachSignals();
|
|
156
108
|
}
|
|
@@ -160,10 +112,7 @@ const mergeNodeApps = (result, resolver, shutdown, args) => {
|
|
|
160
112
|
};
|
|
161
113
|
cliConfig.onSignal("SIGINT", gracefulShutdown);
|
|
162
114
|
cliConfig.onSignal("SIGTERM", gracefulShutdown);
|
|
163
|
-
|
|
164
|
-
const stderr = getStderr();
|
|
165
|
-
const args = getArgs();
|
|
166
|
-
return mergeNodeApps(buildNodeApps(caps, resolver, shutdown, stderr, args), resolver, shutdown, args);
|
|
115
|
+
return createNodeApp(readyApp, shutdown, getArgs());
|
|
167
116
|
}
|
|
168
117
|
//#endregion
|
|
169
118
|
//#region src/serve.lib.ts
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":["CliConfig","Config","NodeCliConfig","cwd","process","argv","exit","code","setExitCode","exitCode","onSignal","signal","handler","on","offSignal","off","Config","EnvAdaptor","ProcessEnvAdaptor","get","key","process","env","serve","NodeCliConfig","ProcessEnvAdaptor","createListenForHttp","appFetch","appShutdown","portOrOptions","listenOptions","port","hostname","server","serverReady","Promise","resolve","fetch","info","address","shutdown","reject","close","err","getStderr","globalThis","process","stderr","getArgs","argv","slice","extractCapabilities","app","buildHttpNodeApp","caps","resolver","args","undefined","listen","buildCommandNodeApp","execCommand","coreExecCommand","result","exitCode","write","reason","message","buildSchedulerPart","startScheduler","stopScheduler","buildNodeApps","httpResult","commandResult","schedulerPart","mergeNodeApps","schedulerMethods","fullApp","onNode","options","addFallbackConfig","readyOptions","warmup","ready","cliConfig","get","shuttingDown","detachSignals","offSignal","gracefulShutdown","catch","onSignal","serve","buildServeOptions","optionsOrCallback","extractCallback","maybeCallback","serveApp","app","options","callback","fetch","port","hostname"],"sources":["../src/node-cli.config.ts","../src/process-env.adaptor.ts","../src/on-node.ts","../src/serve.lib.ts"],"sourcesContent":["import type { Signal, SignalHandler } from '@zeltjs/core';\nimport { CliConfig, Config } from '@zeltjs/core';\n\n@Config\nexport class NodeCliConfig extends CliConfig {\n override cwd(): string {\n return process.cwd();\n }\n\n override argv(): readonly string[] {\n return process.argv;\n }\n\n override exit(code: number): never {\n process.exit(code);\n }\n\n override setExitCode(code: number): void {\n process.exitCode = code;\n }\n\n override onSignal(signal: Signal, handler: SignalHandler): void {\n process.on(signal, handler);\n }\n\n override offSignal(signal: Signal, handler: SignalHandler): void {\n process.off(signal, handler);\n }\n}\n","import { Config, EnvAdaptor } from '@zeltjs/core';\n\n@Config\nexport class ProcessEnvAdaptor extends EnvAdaptor {\n override get(key: string): string | undefined {\n return process.env[key];\n }\n}\n","import type { ServerType } from '@hono/node-server';\nimport { serve } from '@hono/node-server';\nimport type {\n CommandApp,\n ExecResult,\n HttpApp,\n ReadyOptions,\n ReadyResult,\n SchedulerApp,\n} from '@zeltjs/core';\n\nimport { NodeCliConfig } from './node-cli.config';\nimport { ProcessEnvAdaptor } from './process-env.adaptor';\n\ntype ListenOptions = {\n readonly port?: number;\n readonly hostname?: string;\n};\n\nexport type ServerHandle = {\n readonly address: { port: number; address: string };\n readonly shutdown: () => Promise<void>;\n};\n\nexport type NodeAppOptions = {\n readonly warmup?: boolean;\n};\n\nexport type { ExecResult } from '@zeltjs/core';\n\ntype NodeAppBase = {\n readonly args: readonly string[];\n};\n\nexport type HttpNodeApp = ReadyResult &\n NodeAppBase & {\n readonly listen: (portOrOptions?: number | ListenOptions) => Promise<ServerHandle>;\n readonly shutdown: () => Promise<void>;\n };\n\nexport type CommandNodeApp = ReadyResult &\n NodeAppBase & {\n readonly execCommand: (argv: readonly string[]) => Promise<ExecResult>;\n readonly shutdown: () => Promise<void>;\n };\n\nexport type SchedulerNodeAppPart = {\n readonly startScheduler: () => Promise<void>;\n readonly stopScheduler: () => Promise<void>;\n};\n\nexport type FullNodeApp = HttpNodeApp & CommandNodeApp;\n\nexport type NodeApp = HttpNodeApp | CommandNodeApp | FullNodeApp;\n\ntype Stderr = { write: (s: string) => void };\n\nconst createListenForHttp = (\n appFetch: (request: Request) => Promise<Response>,\n appShutdown: () => Promise<void>,\n): ((portOrOptions?: number | ListenOptions) => Promise<ServerHandle>) => {\n return async (portOrOptions?: number | ListenOptions): Promise<ServerHandle> => {\n const listenOptions: ListenOptions =\n typeof portOrOptions === 'number' ? { port: portOrOptions } : (portOrOptions ?? {});\n\n const port = listenOptions.port ?? 3000;\n const hostname = listenOptions.hostname ?? '0.0.0.0';\n\n let server!: ServerType;\n const serverReady = new Promise<{ port: number; address: string }>((resolve) => {\n server = serve({ fetch: appFetch, port, hostname }, (info) =>\n resolve({ port: info.port, address: info.address }),\n );\n });\n\n const address = await serverReady;\n\n const shutdown = async (): Promise<void> => {\n await new Promise<void>((resolve, reject) => {\n server.close((err) => (err ? reject(err) : resolve()));\n });\n await appShutdown();\n };\n\n return { address, shutdown };\n };\n};\n\nconst getStderr = (): Stderr => globalThis.process.stderr;\n\nconst getArgs = (): readonly string[] => globalThis.process.argv.slice(2);\n\ntype AppCapabilities = {\n fetch?: (request: Request) => Promise<Response>;\n execCommand?: (argv: readonly string[]) => Promise<ExecResult>;\n startScheduler?: () => Promise<void>;\n stopScheduler?: () => Promise<void>;\n};\n\ntype AnyApp = HttpApp | CommandApp | SchedulerApp | (HttpApp & CommandApp & SchedulerApp);\n\nconst extractCapabilities = (app: AnyApp): AppCapabilities => app;\n\ntype BuildResult = {\n httpResult: HttpNodeApp | undefined;\n commandResult: CommandNodeApp | undefined;\n schedulerPart: SchedulerNodeAppPart | undefined;\n};\n\nconst buildHttpNodeApp = (\n caps: AppCapabilities,\n resolver: ReadyResult,\n shutdown: () => Promise<void>,\n args: readonly string[],\n): HttpNodeApp | undefined => {\n if (typeof caps.fetch !== 'function') return undefined;\n return { ...resolver, args, listen: createListenForHttp(caps.fetch, shutdown), shutdown };\n};\n\nconst buildCommandNodeApp = (\n caps: AppCapabilities,\n resolver: ReadyResult,\n shutdown: () => Promise<void>,\n stderr: Stderr,\n args: readonly string[],\n): CommandNodeApp | undefined => {\n if (typeof caps.execCommand !== 'function') return undefined;\n\n const coreExecCommand = caps.execCommand;\n const execCommand = async (argv: readonly string[]): Promise<ExecResult> => {\n const result = await coreExecCommand(argv);\n if (result.exitCode === 1) {\n stderr.write(`${result.reason.message}\\n`);\n }\n return result;\n };\n\n return { ...resolver, args, execCommand, shutdown };\n};\n\nconst buildSchedulerPart = (caps: AppCapabilities): SchedulerNodeAppPart | undefined => {\n if (typeof caps.startScheduler !== 'function' || typeof caps.stopScheduler !== 'function')\n return undefined;\n return {\n startScheduler: caps.startScheduler,\n stopScheduler: caps.stopScheduler,\n };\n};\n\nconst buildNodeApps = (\n caps: AppCapabilities,\n resolver: ReadyResult,\n shutdown: () => Promise<void>,\n stderr: Stderr,\n args: readonly string[],\n): BuildResult => ({\n httpResult: buildHttpNodeApp(caps, resolver, shutdown, args),\n commandResult: buildCommandNodeApp(caps, resolver, shutdown, stderr, args),\n schedulerPart: buildSchedulerPart(caps),\n});\n\nconst mergeNodeApps = (\n result: BuildResult,\n resolver: ReadyResult,\n shutdown: () => Promise<void>,\n args: readonly string[],\n): NodeApp => {\n const { httpResult, commandResult, schedulerPart } = result;\n const schedulerMethods = schedulerPart ?? {};\n\n if (httpResult && commandResult) {\n const fullApp: FullNodeApp = {\n ...httpResult,\n execCommand: commandResult.execCommand,\n ...schedulerMethods,\n };\n return fullApp;\n }\n if (httpResult) return { ...httpResult, ...schedulerMethods };\n if (commandResult) return { ...commandResult, ...schedulerMethods };\n return { ...resolver, args, shutdown, listen: () => new Promise(() => {}), ...schedulerMethods };\n};\n\n/** @throws {ZeltLifecycleStateError} */\nexport function onNode(\n app: HttpApp & CommandApp & SchedulerApp,\n options?: NodeAppOptions,\n): Promise<FullNodeApp & SchedulerNodeAppPart>;\n/** @throws {ZeltLifecycleStateError} */\nexport function onNode(\n app: HttpApp & SchedulerApp,\n options?: NodeAppOptions,\n): Promise<HttpNodeApp & SchedulerNodeAppPart>;\n/** @throws {ZeltLifecycleStateError} */\nexport function onNode(\n app: CommandApp & SchedulerApp,\n options?: NodeAppOptions,\n): Promise<CommandNodeApp & SchedulerNodeAppPart>;\n/** @throws {ZeltLifecycleStateError} */\nexport function onNode(app: HttpApp & CommandApp, options?: NodeAppOptions): Promise<FullNodeApp>;\n/** @throws {ZeltLifecycleStateError} */\nexport function onNode(app: HttpApp, options?: NodeAppOptions): Promise<HttpNodeApp>;\n/** @throws {ZeltLifecycleStateError} */\nexport function onNode(app: CommandApp, options?: NodeAppOptions): Promise<CommandNodeApp>;\n/** @throws {ZeltLifecycleStateError} */\nexport async function onNode(app: AnyApp, options: NodeAppOptions = {}): Promise<NodeApp> {\n app.addFallbackConfig(NodeCliConfig);\n app.addFallbackConfig(ProcessEnvAdaptor);\n\n const readyOptions: ReadyOptions = { warmup: options.warmup ?? true };\n const resolver = await app.ready(readyOptions);\n\n const cliConfig = resolver.get(NodeCliConfig);\n\n let shuttingDown = false;\n const detachSignals = (): void => {\n cliConfig.offSignal('SIGINT', gracefulShutdown);\n cliConfig.offSignal('SIGTERM', gracefulShutdown);\n };\n\n const shutdown = async (): Promise<void> => {\n if (shuttingDown) return;\n shuttingDown = true;\n try {\n await app.shutdown();\n } finally {\n detachSignals();\n }\n };\n\n const gracefulShutdown = (): void => {\n void shutdown().catch(() => {});\n };\n\n cliConfig.onSignal('SIGINT', gracefulShutdown);\n cliConfig.onSignal('SIGTERM', gracefulShutdown);\n\n const caps = extractCapabilities(app);\n const stderr = getStderr();\n const args = getArgs();\n const result = buildNodeApps(caps, resolver, shutdown, stderr, args);\n\n return mergeNodeApps(result, resolver, shutdown, args);\n}\n","import type { ServerType } from '@hono/node-server';\nimport { serve } from '@hono/node-server';\n\nexport type ServeOptions = {\n port?: number;\n hostname?: string;\n};\n\nexport type AddressInfo = {\n port: number;\n address: string;\n};\n\ntype AppLike = {\n fetch: (request: Request) => Promise<Response>;\n};\n\nconst buildServeOptions = (\n optionsOrCallback: ServeOptions | ((info: AddressInfo) => void) | undefined,\n): ServeOptions => (typeof optionsOrCallback === 'function' ? {} : (optionsOrCallback ?? {}));\n\nconst extractCallback = (\n optionsOrCallback: ServeOptions | ((info: AddressInfo) => void) | undefined,\n maybeCallback: ((info: AddressInfo) => void) | undefined,\n): ((info: AddressInfo) => void) | undefined =>\n typeof optionsOrCallback === 'function' ? optionsOrCallback : maybeCallback;\n\nexport const serveApp = (\n app: AppLike,\n optionsOrCallback?: ServeOptions | ((info: AddressInfo) => void),\n maybeCallback?: (info: AddressInfo) => void,\n): ServerType => {\n const options = buildServeOptions(optionsOrCallback);\n const callback = extractCallback(optionsOrCallback, maybeCallback);\n\n return serve(\n {\n fetch: app.fetch,\n port: options.port ?? 3000,\n hostname: options.hostname ?? '0.0.0.0',\n },\n callback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,UAAUN,kBAAkBC,kBAAAA;CAClC,MAAMM,WAAWL,gBAAgBD,mBAAmBE,cAAAA;AAEpD,QAAOJ,MACL;EACES,OAAOH,IAAIG;EACXC,MAAMH,QAAQG,QAAQ;EACtBC,UAAUJ,QAAQI,YAAY;EAChC,EACAH,SAAAA"}
|
|
1
|
+
{"version":3,"file":"index.js","names":["CliConfig","Config","NodeCliConfig","cwd","process","argv","exit","code","setExitCode","exitCode","onSignal","signal","handler","on","offSignal","off","Config","EnvAdaptor","ProcessEnvAdaptor","get","key","process","env","serve","unsafeGetNamespacedCallable","NodeCliConfig","ProcessEnvAdaptor","createListenForHttp","appFetch","appShutdown","portOrOptions","listenOptions","port","hostname","server","serverReady","Promise","resolve","fetch","info","address","shutdown","reject","close","err","getArgs","globalThis","process","argv","slice","createNodeApp","readyApp","args","base","listen","onNode","app","options","createRuntime","fallbackConfigs","warmup","cliConfig","get","shuttingDown","detachSignals","offSignal","gracefulShutdown","catch","onSignal","serve","buildServeOptions","optionsOrCallback","extractCallback","maybeCallback","serveApp","app","options","callback","fetch","port","hostname"],"sources":["../src/node-cli.config.ts","../src/process-env.adaptor.ts","../src/on-node.ts","../src/serve.lib.ts"],"sourcesContent":["import type { Signal, SignalHandler } from '@zeltjs/core';\nimport { CliConfig, Config } from '@zeltjs/core';\n\n@Config\nexport class NodeCliConfig extends CliConfig {\n override cwd(): string {\n return process.cwd();\n }\n\n override argv(): readonly string[] {\n return process.argv;\n }\n\n override exit(code: number): never {\n process.exit(code);\n }\n\n override setExitCode(code: number): void {\n process.exitCode = code;\n }\n\n override onSignal(signal: Signal, handler: SignalHandler): void {\n process.on(signal, handler);\n }\n\n override offSignal(signal: Signal, handler: SignalHandler): void {\n process.off(signal, handler);\n }\n}\n","import { Config, EnvAdaptor } from '@zeltjs/core';\n\n@Config\nexport class ProcessEnvAdaptor extends EnvAdaptor {\n override get(key: string): string | undefined {\n return process.env[key];\n }\n}\n","import type { ServerType } from '@hono/node-server';\nimport { serve } from '@hono/node-server';\nimport type { ConfiguredFeature, FeatureApp, HttpCapabilities, RuntimeApp } from '@zeltjs/core';\nimport { unsafeGetNamespacedCallable } from '@zeltjs/unsafe-type-lib';\n\nimport { NodeCliConfig } from './node-cli.config';\nimport { ProcessEnvAdaptor } from './process-env.adaptor';\n\ntype ListenOptions = {\n readonly port?: number;\n readonly hostname?: string;\n};\n\nexport type ServerHandle = {\n readonly address: { port: number; address: string };\n readonly shutdown: () => Promise<void>;\n};\n\nexport type NodeAppOptions = {\n readonly warmup?: boolean;\n};\n\nexport type { ExecResult } from '@zeltjs/core';\n\ntype EnvironmentNodeAppPart = {\n readonly args: readonly string[];\n readonly shutdown: () => Promise<void>;\n};\n\ntype HttpNodeAppPart = {\n readonly listen: (portOrOptions?: number | ListenOptions) => Promise<ServerHandle>;\n};\n\nexport type NodeApp = (RuntimeApp<readonly ConfiguredFeature[]> & EnvironmentNodeAppPart) &\n Partial<HttpNodeAppPart>;\n\ntype FeatureKeys<F extends readonly ConfiguredFeature[]> = F[number]['key'];\n\ntype WithFeature<\n F extends readonly ConfiguredFeature[],\n TKey extends string,\n TPart extends object,\n> = TKey extends FeatureKeys<F> ? TPart : unknown;\n\ntype NodeAppForFeatures<F extends readonly ConfiguredFeature[]> = RuntimeApp<F> &\n EnvironmentNodeAppPart &\n (string extends FeatureKeys<F>\n ? Partial<HttpNodeAppPart>\n : WithFeature<F, 'http', HttpNodeAppPart>);\n\nconst createListenForHttp = (\n appFetch: (request: Request) => Promise<Response>,\n appShutdown: () => Promise<void>,\n): ((portOrOptions?: number | ListenOptions) => Promise<ServerHandle>) => {\n return async (portOrOptions?: number | ListenOptions): Promise<ServerHandle> => {\n const listenOptions: ListenOptions =\n typeof portOrOptions === 'number' ? { port: portOrOptions } : (portOrOptions ?? {});\n\n const port = listenOptions.port ?? 3000;\n const hostname = listenOptions.hostname ?? '0.0.0.0';\n\n let server!: ServerType;\n const serverReady = new Promise<{ port: number; address: string }>((resolve) => {\n server = serve({ fetch: appFetch, port, hostname }, (info) =>\n resolve({ port: info.port, address: info.address }),\n );\n });\n\n const address = await serverReady;\n\n const shutdown = async (): Promise<void> => {\n await new Promise<void>((resolve, reject) => {\n server.close((err) => (err ? reject(err) : resolve()));\n });\n await appShutdown();\n };\n\n return { address, shutdown };\n };\n};\n\nconst getArgs = (): readonly string[] => globalThis.process.argv.slice(2);\n\nconst createNodeApp = (\n readyApp: RuntimeApp<readonly ConfiguredFeature[]>,\n shutdown: () => Promise<void>,\n args: readonly string[],\n): NodeApp => {\n const fetch = unsafeGetNamespacedCallable<HttpCapabilities['fetch']>(readyApp, 'http', 'fetch');\n const base: RuntimeApp<readonly ConfiguredFeature[]> & EnvironmentNodeAppPart = {\n ...readyApp,\n args,\n shutdown,\n };\n\n if (typeof fetch !== 'function') return base;\n return { ...base, listen: createListenForHttp(fetch, shutdown) };\n};\n\nexport function onNode<const F extends readonly ConfiguredFeature[]>(\n app: FeatureApp<F>,\n options?: NodeAppOptions,\n): Promise<NodeAppForFeatures<F>>;\n\n/** @throws {ZeltLifecycleStateError} */\nexport async function onNode(\n app: FeatureApp<readonly ConfiguredFeature[]>,\n options: NodeAppOptions = {},\n): Promise<NodeApp> {\n const readyApp = await app.createRuntime({\n fallbackConfigs: [NodeCliConfig, ProcessEnvAdaptor],\n warmup: options.warmup ?? true,\n });\n\n const cliConfig = await readyApp.get(NodeCliConfig);\n\n let shuttingDown = false;\n const detachSignals = (): void => {\n cliConfig.offSignal('SIGINT', gracefulShutdown);\n cliConfig.offSignal('SIGTERM', gracefulShutdown);\n };\n\n const shutdown = async (): Promise<void> => {\n if (shuttingDown) return;\n shuttingDown = true;\n try {\n await readyApp.shutdown();\n } finally {\n detachSignals();\n }\n };\n\n const gracefulShutdown = (): void => {\n void shutdown().catch(() => {});\n };\n\n cliConfig.onSignal('SIGINT', gracefulShutdown);\n cliConfig.onSignal('SIGTERM', gracefulShutdown);\n\n const args = getArgs();\n\n return createNodeApp(readyApp, shutdown, args);\n}\n","import type { ServerType } from '@hono/node-server';\nimport { serve } from '@hono/node-server';\n\nexport type ServeOptions = {\n port?: number;\n hostname?: string;\n};\n\nexport type AddressInfo = {\n port: number;\n address: string;\n};\n\ntype AppLike = {\n fetch: (request: Request) => Promise<Response>;\n};\n\nconst buildServeOptions = (\n optionsOrCallback: ServeOptions | ((info: AddressInfo) => void) | undefined,\n): ServeOptions => (typeof optionsOrCallback === 'function' ? {} : (optionsOrCallback ?? {}));\n\nconst extractCallback = (\n optionsOrCallback: ServeOptions | ((info: AddressInfo) => void) | undefined,\n maybeCallback: ((info: AddressInfo) => void) | undefined,\n): ((info: AddressInfo) => void) | undefined =>\n typeof optionsOrCallback === 'function' ? optionsOrCallback : maybeCallback;\n\nexport const serveApp = (\n app: AppLike,\n optionsOrCallback?: ServeOptions | ((info: AddressInfo) => void),\n maybeCallback?: (info: AddressInfo) => void,\n): ServerType => {\n const options = buildServeOptions(optionsOrCallback);\n const callback = extractCallback(optionsOrCallback, maybeCallback);\n\n return serve(\n {\n fetch: app.fetch,\n port: options.port ?? 3000,\n hostname: options.hostname ?? '0.0.0.0',\n },\n callback,\n );\n};\n"],"mappings":";;;;;;;;;;AAIA,IAAaE,gBAAb,cAAmCF,UAAAA;CACxBG,MAAc;AACrB,SAAOC,QAAQD,KAAG;;CAGXE,OAA0B;AACjC,SAAOD,QAAQC;;CAGRC,KAAKC,MAAqB;AACjCH,UAAQE,KAAKC,KAAAA;;CAGNC,YAAYD,MAAoB;AACvCH,UAAQK,WAAWF;;CAGZG,SAASC,QAAgBC,SAA8B;AAC9DR,UAAQS,GAAGF,QAAQC,QAAAA;;CAGZE,UAAUH,QAAgBC,SAA8B;AAC/DR,UAAQW,IAAIJ,QAAQC,QAAAA;;;;;;;;;;;;ACvBxB,IAAaM,oBAAb,cAAuCD,WAAAA;CAC5BE,IAAIC,KAAiC;AAC5C,SAAOC,QAAQC,IAAIF;;;;;;AC6CvB,MAAMO,uBACJC,UACAC,gBAAAA;AAEA,QAAO,OAAOC,kBAAA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AA;AACf,MAAIsB,aAAc;AAClBA,iBAAe;AACf,MAAI;AACF,SAAMZ,SAASV,UAAQ;YACf;AACRuB,kBAAAA;;;CAIJ,MAAME,yBAAmB;AAClBzB,YAAAA,CAAW0B,YAAM,GAAO;;AAG/BN,WAAUO,SAAS,UAAUF,iBAAAA;AAC7BL,WAAUO,SAAS,WAAWF,iBAAAA;AAI9B,QAAOhB,cAAcC,UAAUV,UAFlBI,SAE4BO,CAAAA;;;;AC5H3C,MAAMkB,qBACJC,sBACkB,OAAOA,sBAAsB,aAAa,EAAC,GAAKA,qBAAqB,EAAC;AAE1F,MAAMC,mBACJD,mBACAE,kBAEA,OAAOF,sBAAsB,aAAaA,oBAAoBE;AAEhE,MAAaC,YACXC,KACAJ,mBACAE,kBAAAA;CAEA,MAAMG,UAAUN,kBAAkBC,kBAAAA;CAClC,MAAMM,WAAWL,gBAAgBD,mBAAmBE,cAAAA;AAEpD,QAAOJ,MACL;EACES,OAAOH,IAAIG;EACXC,MAAMH,QAAQG,QAAQ;EACtBC,UAAUJ,QAAQI,YAAY;EAChC,EACAH,SAAAA"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zeltjs/adapter-node",
|
|
3
|
-
"version": "0.8.
|
|
3
|
+
"version": "0.8.2-canary.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -22,7 +22,8 @@
|
|
|
22
22
|
],
|
|
23
23
|
"dependencies": {
|
|
24
24
|
"@hono/node-server": "2.0.1",
|
|
25
|
-
"@zeltjs/core": "0.8.
|
|
25
|
+
"@zeltjs/core": "0.8.2-canary.0",
|
|
26
|
+
"@zeltjs/unsafe-type-lib": "0.8.2-canary.0"
|
|
26
27
|
},
|
|
27
28
|
"devDependencies": {
|
|
28
29
|
"@types/node": "22.19.17"
|