@zeltjs/adapter-bun 0.6.1 → 0.8.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 +14 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +57 -7
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,17 @@
|
|
|
1
|
-
import { CommandApp,
|
|
1
|
+
import { CliConfig, CommandApp, EnvAdaptor, ExecResult, ExecResult as ExecResult$1, HttpApp, ReadyResult, Signal, SignalHandler } from "@zeltjs/core";
|
|
2
2
|
|
|
3
|
-
//#region src/bun-
|
|
4
|
-
declare class
|
|
3
|
+
//#region src/bun-cli.config.d.ts
|
|
4
|
+
declare class BunCliConfig extends CliConfig {
|
|
5
|
+
cwd(): string;
|
|
6
|
+
argv(): readonly string[];
|
|
7
|
+
exit(code: number): never;
|
|
8
|
+
setExitCode(code: number): void;
|
|
9
|
+
onSignal(signal: Signal, handler: SignalHandler): void;
|
|
10
|
+
offSignal(signal: Signal, handler: SignalHandler): void;
|
|
11
|
+
}
|
|
12
|
+
//#endregion
|
|
13
|
+
//#region src/bun-env.adaptor.d.ts
|
|
14
|
+
declare class BunEnvAdaptor extends EnvAdaptor {
|
|
5
15
|
get(key: string): string | undefined;
|
|
6
16
|
}
|
|
7
17
|
//#endregion
|
|
@@ -40,5 +50,5 @@ declare function onBun(app: HttpApp, options?: BunAppOptions): Promise<HttpBunAp
|
|
|
40
50
|
/** @throws {ZeltLifecycleStateError} */
|
|
41
51
|
declare function onBun(app: CommandApp, options?: BunAppOptions): Promise<CommandBunApp>;
|
|
42
52
|
//#endregion
|
|
43
|
-
export { type BunApp, type BunAppOptions,
|
|
53
|
+
export { type BunApp, type BunAppOptions, BunCliConfig, BunEnvAdaptor, type CommandBunApp, type ExecResult, type FullBunApp, type HttpBunApp, type ServerHandle, onBun };
|
|
44
54
|
//# 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-
|
|
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;AAAA;;;cCtBjC,aAAA,SAAsB,UAAA;EACxB,GAAA,CAAI,GAAA;AAAA;;;KCCV,YAAA;EAAA,SACM,IAAA;EAAA,SACA,QAAA;AAAA;AAAA,KAGC,YAAA;EAAA,SACD,OAAA;IAAW,IAAA;IAAc,QAAA;EAAA;EAAA,SACzB,QAAA,QAAgB,OAAA;AAAA;AAAA,KAGf,aAAA;EAAA,SACD,MAAA;AAAA;AAAA,KAKN,UAAA;EAAA,SACM,IAAA;AAAA;AAAA,KAGC,UAAA,GAAa,WAAA,GACvB,UAAA;EAAA,SACW,KAAA,GAAQ,OAAA,GAAU,YAAA,KAAiB,YAAA;EAAA,SACnC,QAAA,QAAgB,OAAA;AAAA;AAAA,KAGjB,aAAA,GAAgB,WAAA,GAC1B,UAAA;EAAA,SACW,WAAA,GAAc,IAAA,wBAA4B,OAAA,CAAQ,YAAA;EAAA,SAClD,QAAA,QAAgB,OAAA;AAAA;AAAA,KAGjB,UAAA,GAAa,UAAA,GAAa,aAAA;AAAA,KAE1B,MAAA,GAAS,UAAA,GAAa,aAAA,GAAgB,UAAA;;iBA+GlC,KAAA,CAAM,GAAA,EAAK,OAAA,GAAU,UAAA,EAAY,OAAA,GAAU,aAAA,GAAgB,OAAA,CAAQ,UAAA;;iBAEnE,KAAA,CAAM,GAAA,EAAK,OAAA,EAAS,OAAA,GAAU,aAAA,GAAgB,OAAA,CAAQ,UAAA;;iBAEtD,KAAA,CAAM,GAAA,EAAK,UAAA,EAAY,OAAA,GAAU,aAAA,GAAgB,OAAA,CAAQ,aAAA"}
|
package/dist/index.js
CHANGED
|
@@ -1,17 +1,46 @@
|
|
|
1
|
-
import { Config,
|
|
2
|
-
//#region src/bun-
|
|
1
|
+
import { CliConfig, Config, EnvAdaptor } from "@zeltjs/core";
|
|
2
|
+
//#region src/bun-cli.config.ts
|
|
3
|
+
function _ts_decorate$1(decorators, target, key, desc) {
|
|
4
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
5
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
6
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
7
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
8
|
+
}
|
|
9
|
+
var BunCliConfig = class extends CliConfig {
|
|
10
|
+
cwd() {
|
|
11
|
+
return process.cwd();
|
|
12
|
+
}
|
|
13
|
+
argv() {
|
|
14
|
+
return Bun.argv;
|
|
15
|
+
}
|
|
16
|
+
exit(code) {
|
|
17
|
+
process.exit(code);
|
|
18
|
+
}
|
|
19
|
+
setExitCode(code) {
|
|
20
|
+
process.exitCode = code;
|
|
21
|
+
}
|
|
22
|
+
onSignal(signal, handler) {
|
|
23
|
+
process.on(signal, handler);
|
|
24
|
+
}
|
|
25
|
+
offSignal(signal, handler) {
|
|
26
|
+
process.off(signal, handler);
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
BunCliConfig = _ts_decorate$1([Config], BunCliConfig);
|
|
30
|
+
//#endregion
|
|
31
|
+
//#region src/bun-env.adaptor.ts
|
|
3
32
|
function _ts_decorate(decorators, target, key, desc) {
|
|
4
33
|
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
5
34
|
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
6
35
|
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
7
36
|
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
8
37
|
}
|
|
9
|
-
var
|
|
38
|
+
var BunEnvAdaptor = class extends EnvAdaptor {
|
|
10
39
|
get(key) {
|
|
11
40
|
return Bun.env[key];
|
|
12
41
|
}
|
|
13
42
|
};
|
|
14
|
-
|
|
43
|
+
BunEnvAdaptor = _ts_decorate([Config], BunEnvAdaptor);
|
|
15
44
|
//#endregion
|
|
16
45
|
//#region src/on-bun.ts
|
|
17
46
|
const createServeForHttp = (appFetch, appShutdown) => {
|
|
@@ -89,15 +118,36 @@ const mergeBunApps = (result, resolver, shutdown, args) => {
|
|
|
89
118
|
};
|
|
90
119
|
};
|
|
91
120
|
/** @throws {ZeltLifecycleStateError} */ async function onBun(app, options = {}) {
|
|
92
|
-
app.addFallbackConfig(
|
|
121
|
+
app.addFallbackConfig(BunCliConfig);
|
|
122
|
+
app.addFallbackConfig(BunEnvAdaptor);
|
|
93
123
|
const readyOptions = { warmup: options.warmup ?? true };
|
|
94
124
|
const resolver = await app.ready(readyOptions);
|
|
125
|
+
const cliConfig = resolver.get(BunCliConfig);
|
|
126
|
+
let shuttingDown = false;
|
|
127
|
+
const detachSignals = () => {
|
|
128
|
+
cliConfig.offSignal("SIGINT", gracefulShutdown);
|
|
129
|
+
cliConfig.offSignal("SIGTERM", gracefulShutdown);
|
|
130
|
+
};
|
|
131
|
+
const shutdown = async () => {
|
|
132
|
+
if (shuttingDown) return;
|
|
133
|
+
shuttingDown = true;
|
|
134
|
+
try {
|
|
135
|
+
await app.shutdown();
|
|
136
|
+
} finally {
|
|
137
|
+
detachSignals();
|
|
138
|
+
}
|
|
139
|
+
};
|
|
140
|
+
const gracefulShutdown = () => {
|
|
141
|
+
shutdown().catch(() => {});
|
|
142
|
+
};
|
|
143
|
+
cliConfig.onSignal("SIGINT", gracefulShutdown);
|
|
144
|
+
cliConfig.onSignal("SIGTERM", gracefulShutdown);
|
|
95
145
|
const caps = extractCapabilities(app);
|
|
96
146
|
const stderr = getStderr();
|
|
97
147
|
const args = getArgs();
|
|
98
|
-
return mergeBunApps(buildBunApps(caps, resolver,
|
|
148
|
+
return mergeBunApps(buildBunApps(caps, resolver, shutdown, stderr, args), resolver, shutdown, args);
|
|
99
149
|
}
|
|
100
150
|
//#endregion
|
|
101
|
-
export {
|
|
151
|
+
export { BunCliConfig, BunEnvAdaptor, onBun };
|
|
102
152
|
|
|
103
153
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":["Config","EnvConfig","BunEnvConfig","get","key","Bun","env","BunEnvConfig","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"],"sources":["../src/bun-env.config.ts","../src/on-bun.ts"],"sourcesContent":["import { Config, EnvConfig } from '@zeltjs/core';\n\n@Config\nexport class BunEnvConfig extends EnvConfig {\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 { BunEnvConfig } from './bun-env.config';\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(BunEnvConfig);\n\n const readyOptions: ReadyOptions = { warmup: options.warmup ?? true };\n const resolver = await app.ready(readyOptions);\n\n const caps = extractCapabilities(app);\n const stderr = getStderr();\n const args = getArgs();\n const result = buildBunApps(caps, resolver, app.shutdown, stderr, args);\n\n return mergeBunApps(result, resolver, app.shutdown, args);\n}\n"],"mappings":";;;;;;;;AAGA,IAAaE,eAAb,cAAkCD,UAAAA;CACvBE,IAAIC,KAAiC;AAC5C,SAAOC,IAAIC,IAAIF;;;;;;ACqCnB,MAAMI,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;EAAE,GAAGF;EAAUC;EAAMjB,OAAOR,mBAAmBuB,KAAKd,OAAOC,SAAAA;EAAWA;EAAS;;AAGxF,MAAMiB,sBACJJ,MACAC,UACAd,UACAM,QACAS,SAAAA;AAEA,KAAI,OAAOF,KAAKK,gBAAgB,WAAY,QAAOF,KAAAA;CAEnD,MAAMG,kBAAkBN,KAAKK;CAC7B,MAAMA,cAAc,OAAOV,SAAAA;EACzB,MAAMY,SAAS,MAAMD,gBAAgBX,KAAAA;AACrC,MAAIY,OAAOC,aAAa,EACtBf,QAAOgB,MAAM,GAAGF,OAAOG,OAAOC,QAAQ,IAAG;AAE3C,SAAOJ;;AAGT,QAAO;EAAE,GAAGN;EAAUC;EAAMG;EAAalB;EAAS;;AAGpD,MAAMyB,gBACJZ,MACAC,UACAd,UACAM,QACAS,UACiB;CACjBW,YAAYd,gBAAgBC,MAAMC,UAAUd,UAAUe,KAAAA;CACtDY,eAAeV,mBAAmBJ,MAAMC,UAAUd,UAAUM,QAAQS,KAAAA;CACtE;AAEA,MAAMa,gBACJR,QACAN,UACAd,UACAe,SAAAA;CAEA,MAAM,EAAEW,YAAYC,kBAAkBP;AACtC,KAAIM,cAAcC,cAEhB,QAAOE;EADuB,GAAGH;EAAYR,aAAaS,cAAcT;EACjEW;AAET,KAAIH,WAAY,QAAOA;AACvB,KAAIC,cAAe,QAAOA;AAC1B,QAAO;EACL,GAAGb;EACHC;EACAf;EACAF,cAAc;GAAEI,SAAS;IAAER,MAAM;IAAGC,UAAU;IAAG;GAAGK;GAAS;EAC/D;;yCAUF,eAAsB8B,MACpBnB,KACAlB,UAAyB,EAAE,EAAA;AAE3BkB,KAAIoB,kBAAkB1C,aAAAA;CAEtB,MAAM2C,eAA6B,EAAEC,QAAQxC,QAAQwC,UAAU,MAAK;CACpE,MAAMnB,WAAW,MAAMH,IAAIuB,MAAMF,aAAAA;CAEjC,MAAMnB,OAAOH,oBAAoBC,IAAAA;CACjC,MAAML,SAASH,WAAAA;CACf,MAAMY,OAAOR,SAAAA;AAGb,QAAOqB,aAFQH,aAAaZ,MAAMC,UAAUH,IAAIX,UAAUM,QAAQS,KAE9CK,EAAQN,UAAUH,IAAIX,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","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":"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"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zeltjs/adapter-bun",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -21,7 +21,7 @@
|
|
|
21
21
|
"dist"
|
|
22
22
|
],
|
|
23
23
|
"dependencies": {
|
|
24
|
-
"@zeltjs/core": "0.
|
|
24
|
+
"@zeltjs/core": "0.8.0"
|
|
25
25
|
},
|
|
26
26
|
"devDependencies": {
|
|
27
27
|
"@types/bun": "1.2.17"
|