@zeltjs/adapter-bun 0.8.0 → 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 +18 -13
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +15 -55
- package/dist/index.js.map +1 -1
- package/package.json +3 -2
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { CliConfig,
|
|
1
|
+
import { CliConfig, CommandCapabilities, ConfiguredFeature, EnvAdaptor, ExecResult, FeatureApp, HttpCapabilities, RuntimeApp, Signal, SignalHandler } from "@zeltjs/core";
|
|
2
2
|
|
|
3
3
|
//#region src/bun-cli.config.d.ts
|
|
4
4
|
declare class BunCliConfig extends CliConfig {
|
|
@@ -31,24 +31,29 @@ type BunAppOptions = {
|
|
|
31
31
|
readonly warmup?: boolean;
|
|
32
32
|
};
|
|
33
33
|
type BunAppBase = {
|
|
34
|
+
readonly get: <T extends object>(cls: new (...args: never[]) => T) => Promise<T>;
|
|
34
35
|
readonly args: readonly string[];
|
|
35
|
-
};
|
|
36
|
-
type HttpBunApp = ReadyResult & BunAppBase & {
|
|
37
|
-
readonly serve: (options?: ServeOptions) => ServerHandle;
|
|
38
36
|
readonly shutdown: () => Promise<void>;
|
|
39
37
|
};
|
|
40
|
-
type
|
|
41
|
-
readonly
|
|
38
|
+
type EnvironmentBunAppPart = {
|
|
39
|
+
readonly args: readonly string[];
|
|
42
40
|
readonly shutdown: () => Promise<void>;
|
|
43
41
|
};
|
|
42
|
+
type HttpBunAppPart = {
|
|
43
|
+
readonly serve: (options?: ServeOptions) => ServerHandle;
|
|
44
|
+
};
|
|
45
|
+
type HttpBunApp = BunAppBase & {
|
|
46
|
+
readonly http: HttpCapabilities;
|
|
47
|
+
} & HttpBunAppPart;
|
|
48
|
+
type CommandBunApp = BunAppBase & {
|
|
49
|
+
readonly commands: CommandCapabilities;
|
|
50
|
+
};
|
|
44
51
|
type FullBunApp = HttpBunApp & CommandBunApp;
|
|
45
|
-
type BunApp =
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
declare function onBun(app:
|
|
50
|
-
/** @throws {ZeltLifecycleStateError} */
|
|
51
|
-
declare function onBun(app: CommandApp, options?: BunAppOptions): Promise<CommandBunApp>;
|
|
52
|
+
type BunApp = (RuntimeApp<readonly ConfiguredFeature[]> & EnvironmentBunAppPart) & Partial<HttpBunAppPart>;
|
|
53
|
+
type FeatureKeys<F extends readonly ConfiguredFeature[]> = F[number]['key'];
|
|
54
|
+
type WithFeature<F extends readonly ConfiguredFeature[], TKey extends string, TPart extends object> = TKey extends FeatureKeys<F> ? TPart : unknown;
|
|
55
|
+
type BunAppForFeatures<F extends readonly ConfiguredFeature[]> = RuntimeApp<F> & EnvironmentBunAppPart & (string extends FeatureKeys<F> ? Partial<HttpBunAppPart> : WithFeature<F, 'http', HttpBunAppPart>);
|
|
56
|
+
declare function onBun<const F extends readonly ConfiguredFeature[]>(app: FeatureApp<F>, options?: BunAppOptions): Promise<BunAppForFeatures<F>>;
|
|
52
57
|
//#endregion
|
|
53
58
|
export { type BunApp, type BunAppOptions, BunCliConfig, BunEnvAdaptor, type CommandBunApp, type ExecResult, type FullBunApp, type HttpBunApp, type ServerHandle, onBun };
|
|
54
59
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/bun-cli.config.ts","../src/bun-env.adaptor.ts","../src/on-bun.ts"],"mappings":";;;cAIa,YAAA,SAAqB,SAAA;EACvB,GAAA,CAAA;EAIA,IAAA,CAAA;EAIA,IAAA,CAAK,IAAA;EAIL,WAAA,CAAY,IAAA;EAIZ,QAAA,CAAS,MAAA,EAAQ,MAAA,EAAQ,OAAA,EAAS,aAAA;EAIlC,SAAA,CAAU,MAAA,EAAQ,MAAA,EAAQ,OAAA,EAAS,aAAA;AA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AA,IAC/D,OAAA,CAAQ,cAAA;AAAA,KAEL,WAAA,oBAA+B,iBAAA,MAAuB,CAAA;AAAA,KAEtD,WAAA,oBACgB,iBAAA,iDAGjB,IAAA,SAAa,WAAA,CAAY,CAAA,IAAK,KAAA;AAAA,KAE7B,iBAAA,oBAAqC,iBAAA,MAAuB,UAAA,CAAW,CAAA,IAC1E,qBAAA,mBACgB,WAAA,CAAY,CAAA,IACxB,OAAA,CAAQ,cAAA,IACR,WAAA,CAAY,CAAA,UAAW,cAAA;AAAA,iBA8Cb,KAAA,0BAA+B,iBAAA,GAAA,CAC7C,GAAA,EAAK,UAAA,CAAW,CAAA,GAChB,OAAA,GAAU,aAAA,GACT,OAAA,CAAQ,iBAAA,CAAkB,CAAA"}
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { CliConfig, Config, EnvAdaptor } from "@zeltjs/core";
|
|
2
|
+
import { unsafeGetNamespacedCallable } from "@zeltjs/unsafe-type-lib";
|
|
2
3
|
//#region src/bun-cli.config.ts
|
|
3
4
|
function _ts_decorate$1(decorators, target, key, desc) {
|
|
4
5
|
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
@@ -65,64 +66,26 @@ const createServeForHttp = (appFetch, appShutdown) => {
|
|
|
65
66
|
};
|
|
66
67
|
};
|
|
67
68
|
};
|
|
68
|
-
const getStderr = () => globalThis.process.stderr;
|
|
69
69
|
const getArgs = () => Bun.argv.slice(2);
|
|
70
|
-
const
|
|
71
|
-
const
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
...resolver,
|
|
70
|
+
const createBunApp = (readyApp, shutdown, args) => {
|
|
71
|
+
const fetch = unsafeGetNamespacedCallable(readyApp, "http", "fetch");
|
|
72
|
+
const base = {
|
|
73
|
+
...readyApp,
|
|
75
74
|
args,
|
|
76
|
-
serve: createServeForHttp(caps.fetch, shutdown),
|
|
77
75
|
shutdown
|
|
78
76
|
};
|
|
79
|
-
|
|
80
|
-
const buildCommandBunApp = (caps, resolver, shutdown, stderr, args) => {
|
|
81
|
-
if (typeof caps.execCommand !== "function") return void 0;
|
|
82
|
-
const coreExecCommand = caps.execCommand;
|
|
83
|
-
const execCommand = async (argv) => {
|
|
84
|
-
const result = await coreExecCommand(argv);
|
|
85
|
-
if (result.exitCode === 1) stderr.write(`${result.reason.message}\n`);
|
|
86
|
-
return result;
|
|
87
|
-
};
|
|
77
|
+
if (typeof fetch !== "function") return base;
|
|
88
78
|
return {
|
|
89
|
-
...
|
|
90
|
-
|
|
91
|
-
execCommand,
|
|
92
|
-
shutdown
|
|
93
|
-
};
|
|
94
|
-
};
|
|
95
|
-
const buildBunApps = (caps, resolver, shutdown, stderr, args) => ({
|
|
96
|
-
httpResult: buildHttpBunApp(caps, resolver, shutdown, args),
|
|
97
|
-
commandResult: buildCommandBunApp(caps, resolver, shutdown, stderr, args)
|
|
98
|
-
});
|
|
99
|
-
const mergeBunApps = (result, resolver, shutdown, args) => {
|
|
100
|
-
const { httpResult, commandResult } = result;
|
|
101
|
-
if (httpResult && commandResult) return {
|
|
102
|
-
...httpResult,
|
|
103
|
-
execCommand: commandResult.execCommand
|
|
104
|
-
};
|
|
105
|
-
if (httpResult) return httpResult;
|
|
106
|
-
if (commandResult) return commandResult;
|
|
107
|
-
return {
|
|
108
|
-
...resolver,
|
|
109
|
-
args,
|
|
110
|
-
shutdown,
|
|
111
|
-
serve: () => ({
|
|
112
|
-
address: {
|
|
113
|
-
port: 0,
|
|
114
|
-
hostname: ""
|
|
115
|
-
},
|
|
116
|
-
shutdown
|
|
117
|
-
})
|
|
79
|
+
...base,
|
|
80
|
+
serve: createServeForHttp(fetch, shutdown)
|
|
118
81
|
};
|
|
119
82
|
};
|
|
120
83
|
/** @throws {ZeltLifecycleStateError} */ async function onBun(app, options = {}) {
|
|
121
|
-
app.
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
const cliConfig =
|
|
84
|
+
const readyApp = await app.createRuntime({
|
|
85
|
+
fallbackConfigs: [BunCliConfig, BunEnvAdaptor],
|
|
86
|
+
warmup: options.warmup ?? true
|
|
87
|
+
});
|
|
88
|
+
const cliConfig = await readyApp.get(BunCliConfig);
|
|
126
89
|
let shuttingDown = false;
|
|
127
90
|
const detachSignals = () => {
|
|
128
91
|
cliConfig.offSignal("SIGINT", gracefulShutdown);
|
|
@@ -132,7 +95,7 @@ const mergeBunApps = (result, resolver, shutdown, args) => {
|
|
|
132
95
|
if (shuttingDown) return;
|
|
133
96
|
shuttingDown = true;
|
|
134
97
|
try {
|
|
135
|
-
await
|
|
98
|
+
await readyApp.shutdown();
|
|
136
99
|
} finally {
|
|
137
100
|
detachSignals();
|
|
138
101
|
}
|
|
@@ -142,10 +105,7 @@ const mergeBunApps = (result, resolver, shutdown, args) => {
|
|
|
142
105
|
};
|
|
143
106
|
cliConfig.onSignal("SIGINT", gracefulShutdown);
|
|
144
107
|
cliConfig.onSignal("SIGTERM", gracefulShutdown);
|
|
145
|
-
|
|
146
|
-
const stderr = getStderr();
|
|
147
|
-
const args = getArgs();
|
|
148
|
-
return mergeBunApps(buildBunApps(caps, resolver, shutdown, stderr, args), resolver, shutdown, args);
|
|
108
|
+
return createBunApp(readyApp, shutdown, getArgs());
|
|
149
109
|
}
|
|
150
110
|
//#endregion
|
|
151
111
|
export { BunCliConfig, BunEnvAdaptor, onBun };
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":["CliConfig","Config","BunCliConfig","cwd","process","argv","Bun","exit","code","setExitCode","exitCode","onSignal","signal","handler","on","offSignal","off","Config","EnvAdaptor","BunEnvAdaptor","get","key","Bun","env","BunCliConfig","BunEnvAdaptor","createServeForHttp","appFetch","appShutdown","options","port","hostname","server","Bun","serve","fetch","shutdown","stop","address","getStderr","globalThis","process","stderr","getArgs","argv","slice","extractCapabilities","app","buildHttpBunApp","caps","resolver","args","undefined","buildCommandBunApp","execCommand","coreExecCommand","result","exitCode","write","reason","message","buildBunApps","httpResult","commandResult","mergeBunApps","fullApp","onBun","addFallbackConfig","readyOptions","warmup","ready","cliConfig","get","shuttingDown","detachSignals","offSignal","gracefulShutdown","catch","onSignal"],"sources":["../src/bun-cli.config.ts","../src/bun-env.adaptor.ts","../src/on-bun.ts"],"sourcesContent":["import type { Signal, SignalHandler } from '@zeltjs/core';\nimport { CliConfig, Config } from '@zeltjs/core';\n\n@Config\nexport class BunCliConfig extends CliConfig {\n override cwd(): string {\n return process.cwd();\n }\n\n override argv(): readonly string[] {\n return Bun.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 BunEnvAdaptor extends EnvAdaptor {\n override get(key: string): string | undefined {\n return Bun.env[key];\n }\n}\n","import type { CommandApp, ExecResult, HttpApp, ReadyOptions, ReadyResult } from '@zeltjs/core';\n\nimport { BunCliConfig } from './bun-cli.config';\nimport { BunEnvAdaptor } from './bun-env.adaptor';\n\ntype ServeOptions = {\n readonly port?: number;\n readonly hostname?: string;\n};\n\nexport type ServerHandle = {\n readonly address: { port: number; hostname: string };\n readonly shutdown: () => Promise<void>;\n};\n\nexport type BunAppOptions = {\n readonly warmup?: boolean;\n};\n\nexport type { ExecResult } from '@zeltjs/core';\n\ntype BunAppBase = {\n readonly args: readonly string[];\n};\n\nexport type HttpBunApp = ReadyResult &\n BunAppBase & {\n readonly serve: (options?: ServeOptions) => ServerHandle;\n readonly shutdown: () => Promise<void>;\n };\n\nexport type CommandBunApp = ReadyResult &\n BunAppBase & {\n readonly execCommand: (argv: readonly string[]) => Promise<ExecResult>;\n readonly shutdown: () => Promise<void>;\n };\n\nexport type FullBunApp = HttpBunApp & CommandBunApp;\n\nexport type BunApp = HttpBunApp | CommandBunApp | FullBunApp;\n\ntype Stderr = { write: (s: string) => void };\n\nconst createServeForHttp = (\n appFetch: (request: Request) => Promise<Response>,\n appShutdown: () => Promise<void>,\n): ((options?: ServeOptions) => ServerHandle) => {\n return (options?: ServeOptions): ServerHandle => {\n const port = options?.port ?? 3000;\n const hostname = options?.hostname ?? '0.0.0.0';\n\n const server = Bun.serve({\n fetch: appFetch,\n port,\n hostname,\n });\n\n const shutdown = async (): Promise<void> => {\n await server.stop();\n await appShutdown();\n };\n\n return {\n address: { port: server.port ?? port, hostname: server.hostname ?? hostname },\n shutdown,\n };\n };\n};\n\nconst getStderr = (): Stderr => globalThis.process.stderr;\n\nconst getArgs = (): readonly string[] => Bun.argv.slice(2);\n\ntype AppCapabilities = {\n fetch?: (request: Request) => Promise<Response>;\n execCommand?: (argv: readonly string[]) => Promise<ExecResult>;\n};\n\nconst extractCapabilities = (app: HttpApp | CommandApp | (HttpApp & CommandApp)): AppCapabilities =>\n app;\n\ntype BuildResult = {\n httpResult: HttpBunApp | undefined;\n commandResult: CommandBunApp | undefined;\n};\n\nconst buildHttpBunApp = (\n caps: AppCapabilities,\n resolver: ReadyResult,\n shutdown: () => Promise<void>,\n args: readonly string[],\n): HttpBunApp | undefined => {\n if (typeof caps.fetch !== 'function') return undefined;\n return { ...resolver, args, serve: createServeForHttp(caps.fetch, shutdown), shutdown };\n};\n\nconst buildCommandBunApp = (\n caps: AppCapabilities,\n resolver: ReadyResult,\n shutdown: () => Promise<void>,\n stderr: Stderr,\n args: readonly string[],\n): CommandBunApp | 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 buildBunApps = (\n caps: AppCapabilities,\n resolver: ReadyResult,\n shutdown: () => Promise<void>,\n stderr: Stderr,\n args: readonly string[],\n): BuildResult => ({\n httpResult: buildHttpBunApp(caps, resolver, shutdown, args),\n commandResult: buildCommandBunApp(caps, resolver, shutdown, stderr, args),\n});\n\nconst mergeBunApps = (\n result: BuildResult,\n resolver: ReadyResult,\n shutdown: () => Promise<void>,\n args: readonly string[],\n): BunApp => {\n const { httpResult, commandResult } = result;\n if (httpResult && commandResult) {\n const fullApp: FullBunApp = { ...httpResult, execCommand: commandResult.execCommand };\n return fullApp;\n }\n if (httpResult) return httpResult;\n if (commandResult) return commandResult;\n return {\n ...resolver,\n args,\n shutdown,\n serve: () => ({ address: { port: 0, hostname: '' }, shutdown }),\n };\n};\n\n/** @throws {ZeltLifecycleStateError} */\nexport function onBun(app: HttpApp & CommandApp, options?: BunAppOptions): Promise<FullBunApp>;\n/** @throws {ZeltLifecycleStateError} */\nexport function onBun(app: HttpApp, options?: BunAppOptions): Promise<HttpBunApp>;\n/** @throws {ZeltLifecycleStateError} */\nexport function onBun(app: CommandApp, options?: BunAppOptions): Promise<CommandBunApp>;\n/** @throws {ZeltLifecycleStateError} */\nexport async function onBun(\n app: HttpApp | CommandApp | (HttpApp & CommandApp),\n options: BunAppOptions = {},\n): Promise<BunApp> {\n app.addFallbackConfig(BunCliConfig);\n app.addFallbackConfig(BunEnvAdaptor);\n\n const readyOptions: ReadyOptions = { warmup: options.warmup ?? true };\n const resolver = await app.ready(readyOptions);\n\n const cliConfig = resolver.get(BunCliConfig);\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 = buildBunApps(caps, resolver, shutdown, stderr, args);\n\n return mergeBunApps(result, resolver, shutdown, args);\n}\n"],"mappings":";;;;;;;;AAIA,IAAaE,eAAb,cAAkCF,UAAAA;CACvBG,MAAc;AACrB,SAAOC,QAAQD,KAAG;;CAGXE,OAA0B;AACjC,SAAOC,IAAID;;CAGJE,KAAKC,MAAqB;AACjCJ,UAAQG,KAAKC,KAAAA;;CAGNC,YAAYD,MAAoB;AACvCJ,UAAQM,WAAWF;;CAGZG,SAASC,QAAgBC,SAA8B;AAC9DT,UAAQU,GAAGF,QAAQC,QAAAA;;CAGZE,UAAUH,QAAgBC,SAA8B;AAC/DT,UAAQY,IAAIJ,QAAQC,QAAAA;;;;;;;;;;;;ACvBxB,IAAaM,gBAAb,cAAmCD,WAAAA;CACxBE,IAAIC,KAAiC;AAC5C,SAAOC,IAAIC,IAAIF;;;;;;ACsCnB,MAAMK,sBACJC,UACAC,gBAAAA;AAEA,SAAQC,YAAAA;EACN,MAAMC,OAAOD,SAASC,QAAQ;EAC9B,MAAMC,WAAWF,SAASE,YAAY;EAEtC,MAAMC,SAASC,IAAIC,MAAM;GACvBC,OAAOR;GACPG;GACAC;GACF,CAAA;EAEA,MAAMK,WAAW,YAAA;AACf,SAAMJ,OAAOK,MAAI;AACjB,SAAMT,aAAAA;;AAGR,SAAO;GACLU,SAAS;IAAER,MAAME,OAAOF,QAAQA;IAAMC,UAAUC,OAAOD,YAAYA;IAAS;GAC5EK;GACF;;;AAIJ,MAAMG,kBAA0BC,WAAWC,QAAQC;AAEnD,MAAMC,gBAAmCV,IAAIW,KAAKC,MAAM,EAAA;AAOxD,MAAMC,uBAAuBC,QAC3BA;AAOF,MAAMC,mBACJC,MACAC,UACAd,UACAe,SAAAA;AAEA,KAAI,OAAOF,KAAKd,UAAU,WAAY,QAAOiB,KAAAA;AAC7C,QAAO;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AA;CAE/B,IAAIiD,eAAe;CACnB,MAAMC,sBAAgB;AACpBH,YAAUI,UAAU,UAAUC,iBAAAA;AAC9BL,YAAUI,UAAU,WAAWC,iBAAAA;;CAGjC,MAAMxC,WAAW,YAAA;AACf,MAAIqC,aAAc;AAClBA,iBAAe;AACf,MAAI;AACF,SAAM1B,IAAIX,UAAQ;YACV;AACRsC,kBAAAA;;;CAIJ,MAAME,yBAAmB;AAClBxC,YAAAA,CAAWyC,YAAM,GAAO;;AAG/BN,WAAUO,SAAS,UAAUF,iBAAAA;AAC7BL,WAAUO,SAAS,WAAWF,iBAAAA;CAE9B,MAAM3B,OAAOH,oBAAoBC,IAAAA;CACjC,MAAML,SAASH,WAAAA;CACf,MAAMY,OAAOR,SAAAA;AAGb,QAAOqB,aAFQH,aAAaZ,MAAMC,UAAUd,UAAUM,QAAQS,KAE1CK,EAAQN,UAAUd,UAAUe,KAAAA"}
|
|
1
|
+
{"version":3,"file":"index.js","names":["CliConfig","Config","BunCliConfig","cwd","process","argv","Bun","exit","code","setExitCode","exitCode","onSignal","signal","handler","on","offSignal","off","Config","EnvAdaptor","BunEnvAdaptor","get","key","Bun","env","unsafeGetNamespacedCallable","BunCliConfig","BunEnvAdaptor","createServeForHttp","appFetch","appShutdown","options","port","hostname","server","Bun","serve","fetch","shutdown","stop","address","getArgs","argv","slice","createBunApp","readyApp","args","base","onBun","app","createRuntime","fallbackConfigs","warmup","cliConfig","get","shuttingDown","detachSignals","offSignal","gracefulShutdown","catch","onSignal"],"sources":["../src/bun-cli.config.ts","../src/bun-env.adaptor.ts","../src/on-bun.ts"],"sourcesContent":["import type { Signal, SignalHandler } from '@zeltjs/core';\nimport { CliConfig, Config } from '@zeltjs/core';\n\n@Config\nexport class BunCliConfig extends CliConfig {\n override cwd(): string {\n return process.cwd();\n }\n\n override argv(): readonly string[] {\n return Bun.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 BunEnvAdaptor extends EnvAdaptor {\n override get(key: string): string | undefined {\n return Bun.env[key];\n }\n}\n","import type {\n CommandCapabilities,\n ConfiguredFeature,\n FeatureApp,\n HttpCapabilities,\n RuntimeApp,\n} from '@zeltjs/core';\nimport { unsafeGetNamespacedCallable } from '@zeltjs/unsafe-type-lib';\n\nimport { BunCliConfig } from './bun-cli.config';\nimport { BunEnvAdaptor } from './bun-env.adaptor';\n\ntype ServeOptions = {\n readonly port?: number;\n readonly hostname?: string;\n};\n\nexport type ServerHandle = {\n readonly address: { port: number; hostname: string };\n readonly shutdown: () => Promise<void>;\n};\n\nexport type BunAppOptions = {\n readonly warmup?: boolean;\n};\n\nexport type { ExecResult } from '@zeltjs/core';\n\ntype BunAppBase = {\n readonly get: <T extends object>(cls: new (...args: never[]) => T) => Promise<T>;\n readonly args: readonly string[];\n readonly shutdown: () => Promise<void>;\n};\n\ntype EnvironmentBunAppPart = {\n readonly args: readonly string[];\n readonly shutdown: () => Promise<void>;\n};\n\ntype HttpBunAppPart = {\n readonly serve: (options?: ServeOptions) => ServerHandle;\n};\n\nexport type HttpBunApp = BunAppBase & { readonly http: HttpCapabilities } & HttpBunAppPart;\n\nexport type CommandBunApp = BunAppBase & { readonly commands: CommandCapabilities };\n\nexport type FullBunApp = HttpBunApp & CommandBunApp;\n\nexport type BunApp = (RuntimeApp<readonly ConfiguredFeature[]> & EnvironmentBunAppPart) &\n Partial<HttpBunAppPart>;\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 BunAppForFeatures<F extends readonly ConfiguredFeature[]> = RuntimeApp<F> &\n EnvironmentBunAppPart &\n (string extends FeatureKeys<F>\n ? Partial<HttpBunAppPart>\n : WithFeature<F, 'http', HttpBunAppPart>);\n\nconst createServeForHttp = (\n appFetch: (request: Request) => Promise<Response>,\n appShutdown: () => Promise<void>,\n): ((options?: ServeOptions) => ServerHandle) => {\n return (options?: ServeOptions): ServerHandle => {\n const port = options?.port ?? 3000;\n const hostname = options?.hostname ?? '0.0.0.0';\n\n const server = Bun.serve({\n fetch: appFetch,\n port,\n hostname,\n });\n\n const shutdown = async (): Promise<void> => {\n await server.stop();\n await appShutdown();\n };\n\n return {\n address: { port: server.port ?? port, hostname: server.hostname ?? hostname },\n shutdown,\n };\n };\n};\n\nconst getArgs = (): readonly string[] => Bun.argv.slice(2);\n\nconst createBunApp = (\n readyApp: RuntimeApp<readonly ConfiguredFeature[]>,\n shutdown: () => Promise<void>,\n args: readonly string[],\n): BunApp => {\n const fetch = unsafeGetNamespacedCallable<HttpCapabilities['fetch']>(readyApp, 'http', 'fetch');\n const base: RuntimeApp<readonly ConfiguredFeature[]> & EnvironmentBunAppPart = {\n ...readyApp,\n args,\n shutdown,\n };\n\n if (typeof fetch !== 'function') return base;\n return { ...base, serve: createServeForHttp(fetch, shutdown) };\n};\n\nexport function onBun<const F extends readonly ConfiguredFeature[]>(\n app: FeatureApp<F>,\n options?: BunAppOptions,\n): Promise<BunAppForFeatures<F>>;\n\n/** @throws {ZeltLifecycleStateError} */\nexport async function onBun(\n app: FeatureApp<readonly ConfiguredFeature[]>,\n options: BunAppOptions = {},\n): Promise<BunApp> {\n const readyApp = await app.createRuntime({\n fallbackConfigs: [BunCliConfig, BunEnvAdaptor],\n warmup: options.warmup ?? true,\n });\n\n const cliConfig = await readyApp.get(BunCliConfig);\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 createBunApp(readyApp, shutdown, args);\n}\n"],"mappings":";;;;;;;;;AAIA,IAAaE,eAAb,cAAkCF,UAAAA;CACvBG,MAAc;AACrB,SAAOC,QAAQD,KAAG;;CAGXE,OAA0B;AACjC,SAAOC,IAAID;;CAGJE,KAAKC,MAAqB;AACjCJ,UAAQG,KAAKC,KAAAA;;CAGNC,YAAYD,MAAoB;AACvCJ,UAAQM,WAAWF;;CAGZG,SAASC,QAAgBC,SAA8B;AAC9DT,UAAQU,GAAGF,QAAQC,QAAAA;;CAGZE,UAAUH,QAAgBC,SAA8B;AAC/DT,UAAQY,IAAIJ,QAAQC,QAAAA;;;;;;;;;;;;ACvBxB,IAAaM,gBAAb,cAAmCD,WAAAA;CACxBE,IAAIC,KAAiC;AAC5C,SAAOC,IAAIC,IAAIF;;;;;;AC6DnB,MAAMM,sBACJC,UACAC,gBAAAA;AAEA,SAAQC,YAAAA;EACN,MAAMC,OAAOD,SAASC,QAAQ;EAC9B,MAAMC,WAAWF,SAASE,YAAY;EAEtC,MAAMC,SAASC,IAAIC,MAAM;GACvBC,OAAOR;GACPG;GACAC;GACF,CAAA;EAEA,MAAMK,WAAW,YAAA;AACf,SAAMJ,OAAOK,MAAI;AACjB,SAAMT,aAAAA;;AAGR,SAAO;GACLU,SAAS;IAAER,MAAME,OAAOF,QAAQA;IAAMC,UAAUC,OAAOD,YAAYA;IAAS;GAC5EK;GACF;;;AAIJ,MAAMG,gBAAmCN,IAAIO,KAAKC,MAAM,EAAA;AAExD,MAAMC,gBACJC,UACAP,UACAQ,SAAAA;CAEA,MAAMT,QAAQZ,4BAAuDoB,UAAU,QAAQ,QAAA;CACvF,MAAME,OAAyE;EAC7E,GAAGF;EACHC;EACAR;EACF;AAEA,KAAI,OAAOD,UAAU,WAAY,QAAOU;AACxC,QAAO;EAAE,GAAGA;EAAMX,OAAOR,mBAAmBS,OAAOC,SAAAA;EAAU;;yCAS/D,eAAsBU,MACpBC,KACAlB,UAAyB,EAAE,EAAA;CAE3B,MAAMc,WAAW,MAAMI,IAAIC,cAAc;EACvCC,iBAAiB,CAACzB,cAAcC,cAAc;EAC9CyB,QAAQrB,QAAQqB,UAAU;EAC5B,CAAA;CAEA,MAAMC,YAAY,MAAMR,SAASS,IAAI5B,aAAAA;CAErC,IAAI6B,eAAe;CACnB,MAAMC,sBAAgB;AACpBH,YAAUI,UAAU,UAAUC,iBAAAA;AAC9BL,YAAUI,UAAU,WAAWC,iBAAAA;;CAGjC,MAAMpB,WAAW,YAAA;AACf,MAAIiB,aAAc;AAClBA,iBAAe;AACf,MAAI;AACF,SAAMV,SAASP,UAAQ;YACf;AACRkB,kBAAAA;;;CAIJ,MAAME,yBAAmB;AAClBpB,YAAAA,CAAWqB,YAAM,GAAO;;AAG/BN,WAAUO,SAAS,UAAUF,iBAAAA;AAC7BL,WAAUO,SAAS,WAAWF,iBAAAA;AAI9B,QAAOd,aAAaC,UAAUP,UAFjBG,SAE2BK,CAAAA"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zeltjs/adapter-bun",
|
|
3
|
-
"version": "0.8.0",
|
|
3
|
+
"version": "0.8.2-canary.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -21,7 +21,8 @@
|
|
|
21
21
|
"dist"
|
|
22
22
|
],
|
|
23
23
|
"dependencies": {
|
|
24
|
-
"@zeltjs/core": "0.8.0"
|
|
24
|
+
"@zeltjs/core": "0.8.2-canary.0",
|
|
25
|
+
"@zeltjs/unsafe-type-lib": "0.8.2-canary.0"
|
|
25
26
|
},
|
|
26
27
|
"devDependencies": {
|
|
27
28
|
"@types/bun": "1.2.17"
|