@zeltjs/adapter-bun 0.12.0-canary.3 → 0.12.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/README.md CHANGED
@@ -15,7 +15,7 @@ bun add @zeltjs/adapter-bun @zeltjs/core
15
15
  ## Usage
16
16
 
17
17
  ```typescript
18
- import { createApp, http, Controller, Get } from '@zeltjs/core';
18
+ import { createApp, http, Controller, Get, httpServer } from '@zeltjs/core';
19
19
  import { onBun } from '@zeltjs/adapter-bun';
20
20
 
21
21
  @Controller('/hello')
@@ -26,8 +26,10 @@ class HelloController {
26
26
  }
27
27
  }
28
28
 
29
- const app = createApp([http({ controllers: [HelloController] })]);
29
+ const app = createApp([httpServer(), http({ controllers: [HelloController] })]);
30
30
 
31
31
  const bunApp = await onBun(app);
32
- bunApp.http.serve({ port: 3000 });
32
+ await bunApp.server.listen(bunApp.http, { port: 3000 });
33
33
  ```
34
+
35
+ Server handles provide `address`, `closed`, and `shutdown()`. Shutdown follows Bun's native graceful stop: it refuses new TCP connections, while already accepted keep-alive connections may remain usable until the client closes them.
package/dist/index.cjs CHANGED
@@ -2,7 +2,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
2
  let _zeltjs_core = require("@zeltjs/core");
3
3
  let _zeltjs_core_async_context = require("@zeltjs/core/async-context");
4
4
  //#region src/bun-cli.config.ts
5
- function _ts_decorate$1(decorators, target, key, desc) {
5
+ function _ts_decorate$2(decorators, target, key, desc) {
6
6
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
7
7
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
8
8
  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;
@@ -28,10 +28,10 @@ var BunCliConfig = class extends _zeltjs_core.CliConfig {
28
28
  process.off(signal, handler);
29
29
  }
30
30
  };
31
- BunCliConfig = _ts_decorate$1([_zeltjs_core.Config], BunCliConfig);
31
+ BunCliConfig = _ts_decorate$2([_zeltjs_core.Config], BunCliConfig);
32
32
  //#endregion
33
33
  //#region src/bun-env.adaptor.ts
34
- function _ts_decorate(decorators, target, key, desc) {
34
+ function _ts_decorate$1(decorators, target, key, desc) {
35
35
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
36
36
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
37
37
  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;
@@ -42,77 +42,87 @@ var BunEnvAdaptor = class extends _zeltjs_core.EnvAdaptor {
42
42
  return Bun.env[key];
43
43
  }
44
44
  };
45
- BunEnvAdaptor = _ts_decorate([_zeltjs_core.Config], BunEnvAdaptor);
45
+ BunEnvAdaptor = _ts_decorate$1([_zeltjs_core.Config], BunEnvAdaptor);
46
46
  //#endregion
47
- //#region src/on-bun.ts
48
- const createServeForHttp = (appFetch, registerShutdown) => {
49
- return (options) => {
50
- const port = options?.port ?? 3e3;
51
- const hostname = options?.hostname ?? "0.0.0.0";
52
- const server = Bun.serve({
53
- fetch: appFetch,
54
- port,
55
- hostname
47
+ //#region src/bun-http-server.adaptor.ts
48
+ function _ts_decorate(decorators, target, key, desc) {
49
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
50
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
51
+ 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;
52
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
53
+ }
54
+ var BunHttpServerAdaptor = class extends _zeltjs_core.HttpServerAdaptor {
55
+ /** @throws {Error} */ async listen(fetch, options, registerShutdown) {
56
+ let close;
57
+ const closed = new Promise((resolve) => {
58
+ close = resolve;
56
59
  });
60
+ let server;
57
61
  const shutdown = registerShutdown(async () => {
58
- await server.stop();
62
+ if (server !== void 0) await server.stop();
63
+ close();
59
64
  });
65
+ try {
66
+ server = Bun.serve({
67
+ fetch,
68
+ port: options.port ?? 3e3,
69
+ hostname: options.hostname ?? "0.0.0.0"
70
+ });
71
+ } catch (error) {
72
+ await shutdown();
73
+ throw error;
74
+ }
75
+ const port = server.port;
76
+ const address = server.hostname;
77
+ if (port === void 0 || address === void 0) {
78
+ await shutdown();
79
+ throw new Error("Bun did not return a TCP listening address");
80
+ }
60
81
  return {
61
82
  address: {
62
- port: server.port ?? port,
63
- hostname: server.hostname ?? hostname
83
+ port,
84
+ address
64
85
  },
86
+ closed,
65
87
  shutdown
66
88
  };
67
- };
89
+ }
68
90
  };
91
+ BunHttpServerAdaptor = _ts_decorate([_zeltjs_core.Config], BunHttpServerAdaptor);
92
+ //#endregion
93
+ //#region src/on-bun.ts
69
94
  const getArgs = () => Bun.argv.slice(2);
70
- const createBunApp = (readyApp, shutdown, args) => {
71
- const bunApp = {
72
- ...readyApp,
73
- args,
74
- shutdown
75
- };
76
- for (const entry of readyApp.getFeatureEntries(_zeltjs_core.HttpFeature)) Object.defineProperty(bunApp, entry.key, {
77
- value: {
78
- ...entry.capabilities,
79
- serve: createServeForHttp(entry.capabilities.fetch, (callback) => readyApp.registerShutdown(callback))
80
- },
81
- configurable: true,
82
- enumerable: true
83
- });
84
- return bunApp;
85
- };
86
95
  /** @throws {ZeltLifecycleStateError | TypeError} */ async function onBun(app, options = {}) {
87
96
  (0, _zeltjs_core_async_context.enableAsyncContext)();
88
97
  const readyApp = await app.createRuntime({
89
98
  ...options.contextMode === void 0 ? {} : { contextMode: options.contextMode },
90
99
  ...options.configs === void 0 ? {} : { configs: options.configs },
91
100
  ...options.prebuilt === void 0 ? {} : { prebuilt: options.prebuilt },
92
- fallbackConfigs: [BunCliConfig, BunEnvAdaptor],
101
+ fallbackConfigs: [
102
+ BunCliConfig,
103
+ BunEnvAdaptor,
104
+ BunHttpServerAdaptor
105
+ ],
93
106
  warmup: options.warmup ?? true
94
107
  });
95
108
  const cliConfig = await readyApp.get(BunCliConfig);
96
- let shuttingDown = false;
97
- const detachSignals = () => {
98
- cliConfig.offSignal("SIGINT", gracefulShutdown);
99
- cliConfig.offSignal("SIGTERM", gracefulShutdown);
100
- };
101
- const shutdown = async () => {
102
- if (shuttingDown) return;
103
- shuttingDown = true;
104
- try {
105
- await readyApp.shutdown();
106
- } finally {
107
- detachSignals();
108
- }
109
- };
109
+ const logger = await readyApp.get(_zeltjs_core.LoggerService);
110
110
  const gracefulShutdown = () => {
111
- shutdown().catch(() => {});
111
+ readyApp.shutdown().catch((error) => {
112
+ logger.error("Bun application shutdown failed", { error: Bun.inspect(error, { depth: Infinity }) });
113
+ });
112
114
  };
115
+ readyApp.registerShutdown(() => {
116
+ cliConfig.offSignal("SIGINT", gracefulShutdown);
117
+ cliConfig.offSignal("SIGTERM", gracefulShutdown);
118
+ }, { phase: "after" });
113
119
  cliConfig.onSignal("SIGINT", gracefulShutdown);
114
120
  cliConfig.onSignal("SIGTERM", gracefulShutdown);
115
- return createBunApp(readyApp, shutdown, getArgs());
121
+ const args = getArgs();
122
+ return {
123
+ ...readyApp,
124
+ args
125
+ };
116
126
  }
117
127
  //#endregion
118
128
  Object.defineProperty(exports, "BunCliConfig", {
package/dist/index.d.cts CHANGED
@@ -1,4 +1,4 @@
1
- import { CliConfig, CommandCapabilities, ConfigClass, ConfiguredFeature, ContextMode, EnvAdaptor, ExecResult, FeatureApp, FeatureReadyCapabilities, HttpFeature, RuntimeApp, Signal, SignalHandler, ZeltPrebuilt } from "@zeltjs/core";
1
+ import { CliConfig, CommandFeature, ConfigClass, ConfiguredFeature, ContextMode, EnvAdaptor, ExecResult, FeatureApp, HttpFeature, HttpServerHandle as ServerHandle, ReservedAppProperties, RuntimeApp, Signal, SignalHandler, ZeltPrebuilt } from "@zeltjs/core";
2
2
 
3
3
  //#region src/bun-cli.config.d.ts
4
4
  declare class BunCliConfig extends CliConfig {
@@ -16,46 +16,21 @@ declare class BunEnvAdaptor extends EnvAdaptor {
16
16
  }
17
17
  //#endregion
18
18
  //#region src/on-bun.d.ts
19
- type ServeOptions = {
20
- readonly port?: number;
21
- readonly hostname?: string;
22
- };
23
- type ServerHandle = {
24
- readonly address: {
25
- port: number;
26
- hostname: string;
27
- };
28
- readonly shutdown: () => Promise<void>;
29
- };
30
19
  type BunAppOptions = {
31
20
  readonly contextMode?: ContextMode;
32
21
  readonly configs?: readonly ConfigClass<object>[];
33
22
  readonly warmup?: boolean;
34
23
  readonly prebuilt?: ZeltPrebuilt;
35
24
  };
36
- type BunAppBase = {
37
- readonly get: <T extends object>(cls: new (...args: never[]) => T) => Promise<T>;
38
- readonly args: readonly string[];
39
- readonly shutdown: () => Promise<void>;
40
- };
41
- type EnvironmentBunAppPart = {
25
+ type EnvironmentBunAppPart = ReservedAppProperties<{
42
26
  readonly args: readonly string[];
43
- readonly shutdown: () => Promise<void>;
44
- };
45
- type HttpBunAppPart = {
46
- readonly serve: (options?: ServeOptions) => ServerHandle;
47
- };
48
- type BunFeatureCapabilities<TFeature extends ConfiguredFeature> = FeatureReadyCapabilities<TFeature> & (TFeature extends HttpFeature<string> ? HttpBunAppPart : unknown);
49
- type BunNamespacedCapabilities<F extends readonly ConfiguredFeature[]> = { readonly [TFeature in F[number] as TFeature['key']]: BunFeatureCapabilities<TFeature> };
50
- type HttpBunApp = BunAppBase & RuntimeApp<readonly [HttpFeature]> & {
51
- readonly http: RuntimeApp<readonly [HttpFeature]>['http'] & HttpBunAppPart;
52
- };
53
- type CommandBunApp = BunAppBase & {
54
- readonly commands: CommandCapabilities;
55
- };
56
- type FullBunApp = HttpBunApp & CommandBunApp;
27
+ }>;
28
+ type HttpBunApp = RuntimeApp<readonly [HttpFeature<'http'>]> & EnvironmentBunAppPart;
29
+ type CommandBunApp = RuntimeApp<readonly [CommandFeature]> & EnvironmentBunAppPart;
30
+ type FullBunApp = RuntimeApp<readonly [HttpFeature<'http'>, CommandFeature]> & EnvironmentBunAppPart;
57
31
  type BunApp = RuntimeApp<readonly ConfiguredFeature[]> & EnvironmentBunAppPart;
58
- type BunAppForFeatures<F extends readonly ConfiguredFeature[]> = RuntimeApp<F> & EnvironmentBunAppPart & BunNamespacedCapabilities<F>;
32
+ type BunAppForFeatures<F extends readonly ConfiguredFeature[]> = RuntimeApp<F> & EnvironmentBunAppPart;
33
+ /** @throws {ZeltLifecycleStateError | TypeError} */
59
34
  declare function onBun<const F extends readonly ConfiguredFeature[]>(app: FeatureApp<F>, options?: BunAppOptions): Promise<BunAppForFeatures<F>>;
60
35
  //#endregion
61
36
  export { type BunApp, type BunAppOptions, BunCliConfig, BunEnvAdaptor, type CommandBunApp, type ExecResult, type FullBunApp, type HttpBunApp, type ServerHandle, onBun };
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.cts","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;;;KCYV,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,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":";;;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;;;KCgBH,aAAA;EAAA,SACD,WAAA,GAAc,WAAA;EAAA,SACd,OAAA,YAAmB,WAAA;EAAA,SACnB,MAAA;EAAA,SACA,QAAA,GAAW,YAAA;AAAA;AAAA,KAKjB,qBAAA,GAAwB,qBAAA;EAAA,SAClB,IAAA;AAAA;AAAA,KAGC,UAAA,GAAa,UAAA,WAAqB,WAAA,aAAwB,qBAAA;AAAA,KAC1D,aAAA,GAAgB,UAAA,WAAqB,cAAA,KAAmB,qBAAA;AAAA,KACxD,UAAA,GAAa,UAAA,WAAqB,WAAA,UAAqB,cAAA,KACjE,qBAAA;AAAA,KAEU,MAAA,GAAS,UAAA,UAAoB,iBAAA,MAAuB,qBAAA;AAAA,KAE3D,iBAAA,oBAAqC,iBAAA,MAAuB,UAAA,CAAW,CAAA,IAC1E,qBAAA;;iBAKoB,KAAA,0BAA+B,iBAAA,GAAA,CACnD,GAAA,EAAK,UAAA,CAAW,CAAA,GAChB,OAAA,GAAS,aAAA,GACR,OAAA,CAAQ,iBAAA,CAAkB,CAAA"}
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { CliConfig, CommandCapabilities, ConfigClass, ConfiguredFeature, ContextMode, EnvAdaptor, ExecResult, FeatureApp, FeatureReadyCapabilities, HttpFeature, RuntimeApp, Signal, SignalHandler, ZeltPrebuilt } from "@zeltjs/core";
1
+ import { CliConfig, CommandFeature, ConfigClass, ConfiguredFeature, ContextMode, EnvAdaptor, ExecResult, FeatureApp, HttpFeature, HttpServerHandle as ServerHandle, ReservedAppProperties, RuntimeApp, Signal, SignalHandler, ZeltPrebuilt } from "@zeltjs/core";
2
2
 
3
3
  //#region src/bun-cli.config.d.ts
4
4
  declare class BunCliConfig extends CliConfig {
@@ -16,46 +16,21 @@ declare class BunEnvAdaptor extends EnvAdaptor {
16
16
  }
17
17
  //#endregion
18
18
  //#region src/on-bun.d.ts
19
- type ServeOptions = {
20
- readonly port?: number;
21
- readonly hostname?: string;
22
- };
23
- type ServerHandle = {
24
- readonly address: {
25
- port: number;
26
- hostname: string;
27
- };
28
- readonly shutdown: () => Promise<void>;
29
- };
30
19
  type BunAppOptions = {
31
20
  readonly contextMode?: ContextMode;
32
21
  readonly configs?: readonly ConfigClass<object>[];
33
22
  readonly warmup?: boolean;
34
23
  readonly prebuilt?: ZeltPrebuilt;
35
24
  };
36
- type BunAppBase = {
37
- readonly get: <T extends object>(cls: new (...args: never[]) => T) => Promise<T>;
38
- readonly args: readonly string[];
39
- readonly shutdown: () => Promise<void>;
40
- };
41
- type EnvironmentBunAppPart = {
25
+ type EnvironmentBunAppPart = ReservedAppProperties<{
42
26
  readonly args: readonly string[];
43
- readonly shutdown: () => Promise<void>;
44
- };
45
- type HttpBunAppPart = {
46
- readonly serve: (options?: ServeOptions) => ServerHandle;
47
- };
48
- type BunFeatureCapabilities<TFeature1 extends ConfiguredFeature> = FeatureReadyCapabilities<TFeature1> & (TFeature1 extends HttpFeature<string> ? HttpBunAppPart : unknown);
49
- type BunNamespacedCapabilities<F extends readonly ConfiguredFeature[]> = { readonly [TFeature in F[number] as TFeature['key']]: BunFeatureCapabilities<TFeature> };
50
- type HttpBunApp = BunAppBase & RuntimeApp<readonly [HttpFeature]> & {
51
- readonly http: RuntimeApp<readonly [HttpFeature]>['http'] & HttpBunAppPart;
52
- };
53
- type CommandBunApp = BunAppBase & {
54
- readonly commands: CommandCapabilities;
55
- };
56
- type FullBunApp = HttpBunApp & CommandBunApp;
27
+ }>;
28
+ type HttpBunApp = RuntimeApp<readonly [HttpFeature<'http'>]> & EnvironmentBunAppPart;
29
+ type CommandBunApp = RuntimeApp<readonly [CommandFeature]> & EnvironmentBunAppPart;
30
+ type FullBunApp = RuntimeApp<readonly [HttpFeature<'http'>, CommandFeature]> & EnvironmentBunAppPart;
57
31
  type BunApp = RuntimeApp<readonly ConfiguredFeature[]> & EnvironmentBunAppPart;
58
- type BunAppForFeatures<F extends readonly ConfiguredFeature[]> = RuntimeApp<F> & EnvironmentBunAppPart & BunNamespacedCapabilities<F>;
32
+ type BunAppForFeatures<F extends readonly ConfiguredFeature[]> = RuntimeApp<F> & EnvironmentBunAppPart;
33
+ /** @throws {ZeltLifecycleStateError | TypeError} */
59
34
  declare function onBun<const F extends readonly ConfiguredFeature[]>(app: FeatureApp<F>, options?: BunAppOptions): Promise<BunAppForFeatures<F>>;
60
35
  //#endregion
61
36
  export { type BunApp, type BunAppOptions, BunCliConfig, BunEnvAdaptor, type CommandBunApp, type ExecResult, type FullBunApp, type HttpBunApp, type ServerHandle, onBun };
@@ -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;AAAA;;;cCtBjC,aAAA,SAAsB,UAAA;EACxB,GAAA,CAAI,GAAA;AAAA;;;KCYV,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,SAC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appings":";;;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;;;KCgBH,aAAA;EAAA,SACD,WAAA,GAAc,WAAA;EAAA,SACd,OAAA,YAAmB,WAAA;EAAA,SACnB,MAAA;EAAA,SACA,QAAA,GAAW,YAAA;AAAA;AAAA,KAKjB,qBAAA,GAAwB,qBAAA;EAAA,SAClB,IAAA;AAAA;AAAA,KAGC,UAAA,GAAa,UAAA,WAAqB,WAAA,aAAwB,qBAAA;AAAA,KAC1D,aAAA,GAAgB,UAAA,WAAqB,cAAA,KAAmB,qBAAA;AAAA,KACxD,UAAA,GAAa,UAAA,WAAqB,WAAA,UAAqB,cAAA,KACjE,qBAAA;AAAA,KAEU,MAAA,GAAS,UAAA,UAAoB,iBAAA,MAAuB,qBAAA;AAAA,KAE3D,iBAAA,oBAAqC,iBAAA,MAAuB,UAAA,CAAW,CAAA,IAC1E,qBAAA;;iBAKoB,KAAA,0BAA+B,iBAAA,GAAA,CACnD,GAAA,EAAK,UAAA,CAAW,CAAA,GAChB,OAAA,GAAS,aAAA,GACR,OAAA,CAAQ,iBAAA,CAAkB,CAAA"}
package/dist/index.js CHANGED
@@ -1,7 +1,7 @@
1
- import { CliConfig, Config, EnvAdaptor, HttpFeature } from "@zeltjs/core";
1
+ import { CliConfig, Config, EnvAdaptor, HttpServerAdaptor, LoggerService } from "@zeltjs/core";
2
2
  import { enableAsyncContext } from "@zeltjs/core/async-context";
3
3
  //#region src/bun-cli.config.ts
4
- function _ts_decorate$1(decorators, target, key, desc) {
4
+ function _ts_decorate$2(decorators, target, key, desc) {
5
5
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
6
6
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
7
7
  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;
@@ -27,10 +27,10 @@ var BunCliConfig = class extends CliConfig {
27
27
  process.off(signal, handler);
28
28
  }
29
29
  };
30
- BunCliConfig = _ts_decorate$1([Config], BunCliConfig);
30
+ BunCliConfig = _ts_decorate$2([Config], BunCliConfig);
31
31
  //#endregion
32
32
  //#region src/bun-env.adaptor.ts
33
- function _ts_decorate(decorators, target, key, desc) {
33
+ function _ts_decorate$1(decorators, target, key, desc) {
34
34
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
35
35
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
36
36
  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;
@@ -41,77 +41,87 @@ var BunEnvAdaptor = class extends EnvAdaptor {
41
41
  return Bun.env[key];
42
42
  }
43
43
  };
44
- BunEnvAdaptor = _ts_decorate([Config], BunEnvAdaptor);
44
+ BunEnvAdaptor = _ts_decorate$1([Config], BunEnvAdaptor);
45
45
  //#endregion
46
- //#region src/on-bun.ts
47
- const createServeForHttp = (appFetch, registerShutdown) => {
48
- return (options) => {
49
- const port = options?.port ?? 3e3;
50
- const hostname = options?.hostname ?? "0.0.0.0";
51
- const server = Bun.serve({
52
- fetch: appFetch,
53
- port,
54
- hostname
46
+ //#region src/bun-http-server.adaptor.ts
47
+ function _ts_decorate(decorators, target, key, desc) {
48
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
49
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
50
+ 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;
51
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
52
+ }
53
+ var BunHttpServerAdaptor = class extends HttpServerAdaptor {
54
+ /** @throws {Error} */ async listen(fetch, options, registerShutdown) {
55
+ let close;
56
+ const closed = new Promise((resolve) => {
57
+ close = resolve;
55
58
  });
59
+ let server;
56
60
  const shutdown = registerShutdown(async () => {
57
- await server.stop();
61
+ if (server !== void 0) await server.stop();
62
+ close();
58
63
  });
64
+ try {
65
+ server = Bun.serve({
66
+ fetch,
67
+ port: options.port ?? 3e3,
68
+ hostname: options.hostname ?? "0.0.0.0"
69
+ });
70
+ } catch (error) {
71
+ await shutdown();
72
+ throw error;
73
+ }
74
+ const port = server.port;
75
+ const address = server.hostname;
76
+ if (port === void 0 || address === void 0) {
77
+ await shutdown();
78
+ throw new Error("Bun did not return a TCP listening address");
79
+ }
59
80
  return {
60
81
  address: {
61
- port: server.port ?? port,
62
- hostname: server.hostname ?? hostname
82
+ port,
83
+ address
63
84
  },
85
+ closed,
64
86
  shutdown
65
87
  };
66
- };
88
+ }
67
89
  };
90
+ BunHttpServerAdaptor = _ts_decorate([Config], BunHttpServerAdaptor);
91
+ //#endregion
92
+ //#region src/on-bun.ts
68
93
  const getArgs = () => Bun.argv.slice(2);
69
- const createBunApp = (readyApp, shutdown, args) => {
70
- const bunApp = {
71
- ...readyApp,
72
- args,
73
- shutdown
74
- };
75
- for (const entry of readyApp.getFeatureEntries(HttpFeature)) Object.defineProperty(bunApp, entry.key, {
76
- value: {
77
- ...entry.capabilities,
78
- serve: createServeForHttp(entry.capabilities.fetch, (callback) => readyApp.registerShutdown(callback))
79
- },
80
- configurable: true,
81
- enumerable: true
82
- });
83
- return bunApp;
84
- };
85
94
  /** @throws {ZeltLifecycleStateError | TypeError} */ async function onBun(app, options = {}) {
86
95
  enableAsyncContext();
87
96
  const readyApp = await app.createRuntime({
88
97
  ...options.contextMode === void 0 ? {} : { contextMode: options.contextMode },
89
98
  ...options.configs === void 0 ? {} : { configs: options.configs },
90
99
  ...options.prebuilt === void 0 ? {} : { prebuilt: options.prebuilt },
91
- fallbackConfigs: [BunCliConfig, BunEnvAdaptor],
100
+ fallbackConfigs: [
101
+ BunCliConfig,
102
+ BunEnvAdaptor,
103
+ BunHttpServerAdaptor
104
+ ],
92
105
  warmup: options.warmup ?? true
93
106
  });
94
107
  const cliConfig = await readyApp.get(BunCliConfig);
95
- let shuttingDown = false;
96
- const detachSignals = () => {
97
- cliConfig.offSignal("SIGINT", gracefulShutdown);
98
- cliConfig.offSignal("SIGTERM", gracefulShutdown);
99
- };
100
- const shutdown = async () => {
101
- if (shuttingDown) return;
102
- shuttingDown = true;
103
- try {
104
- await readyApp.shutdown();
105
- } finally {
106
- detachSignals();
107
- }
108
- };
108
+ const logger = await readyApp.get(LoggerService);
109
109
  const gracefulShutdown = () => {
110
- shutdown().catch(() => {});
110
+ readyApp.shutdown().catch((error) => {
111
+ logger.error("Bun application shutdown failed", { error: Bun.inspect(error, { depth: Infinity }) });
112
+ });
111
113
  };
114
+ readyApp.registerShutdown(() => {
115
+ cliConfig.offSignal("SIGINT", gracefulShutdown);
116
+ cliConfig.offSignal("SIGTERM", gracefulShutdown);
117
+ }, { phase: "after" });
112
118
  cliConfig.onSignal("SIGINT", gracefulShutdown);
113
119
  cliConfig.onSignal("SIGTERM", gracefulShutdown);
114
- return createBunApp(readyApp, shutdown, getArgs());
120
+ const args = getArgs();
121
+ return {
122
+ ...readyApp,
123
+ args
124
+ };
115
125
  }
116
126
  //#endregion
117
127
  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","HttpFeature","enableAsyncContext","BunCliConfig","BunEnvAdaptor","createServeForHttp","appFetch","registerShutdown","options","port","hostname","server","Bun","serve","fetch","shutdown","stop","address","getArgs","argv","slice","createBunApp","readyApp","args","base","bunApp","entry","getFeatureEntries","Object","defineProperty","key","value","capabilities","callback","configurable","enumerable","onBun","app","createRuntime","contextMode","undefined","configs","prebuilt","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 ConfigClass,\n ConfiguredFeature,\n ContextMode,\n FeatureApp,\n FeatureReadyCapabilities,\n RuntimeApp,\n ZeltPrebuilt,\n} from '@zeltjs/core';\nimport { HttpFeature } from '@zeltjs/core';\nimport { enableAsyncContext } from '@zeltjs/core/async-context';\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 contextMode?: ContextMode;\n readonly configs?: readonly ConfigClass<object>[];\n readonly warmup?: boolean;\n readonly prebuilt?: ZeltPrebuilt;\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\ntype BunFeatureCapabilities<TFeature extends ConfiguredFeature> =\n FeatureReadyCapabilities<TFeature> &\n (TFeature extends HttpFeature<string> ? HttpBunAppPart : unknown);\n\ntype BunNamespacedCapabilities<F extends readonly ConfiguredFeature[]> = {\n readonly [TFeature in F[number] as TFeature['key']]: BunFeatureCapabilities<TFeature>;\n};\n\nexport type HttpBunApp = BunAppBase &\n RuntimeApp<readonly [HttpFeature]> & {\n readonly http: RuntimeApp<readonly [HttpFeature]>['http'] & HttpBunAppPart;\n };\n\nexport type CommandBunApp = BunAppBase & { readonly commands: CommandCapabilities };\n\nexport type FullBunApp = HttpBunApp & CommandBunApp;\n\nexport type BunApp = RuntimeApp<readonly ConfiguredFeature[]> & EnvironmentBunAppPart;\n\ntype BunAppForFeatures<F extends readonly ConfiguredFeature[]> = RuntimeApp<F> &\n EnvironmentBunAppPart &\n BunNamespacedCapabilities<F>;\n\nconst createServeForHttp = (\n appFetch: (request: Request) => Promise<Response>,\n registerShutdown: (callback: () => Promise<void>) => () => 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 const shutdown = registerShutdown(async () => {\n await server.stop();\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 base: RuntimeApp<readonly ConfiguredFeature[]> & EnvironmentBunAppPart = {\n ...readyApp,\n args,\n shutdown,\n };\n\n const bunApp: BunApp = { ...base };\n\n for (const entry of readyApp.getFeatureEntries(HttpFeature)) {\n Object.defineProperty(bunApp, entry.key, {\n value: {\n ...entry.capabilities,\n serve: createServeForHttp(entry.capabilities.fetch, (callback) =>\n readyApp.registerShutdown(callback),\n ),\n },\n configurable: true,\n enumerable: true,\n });\n }\n\n return bunApp;\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 | TypeError} */\nexport async function onBun<const F extends readonly ConfiguredFeature[]>(\n app: FeatureApp<F>,\n options: BunAppOptions = {},\n): Promise<BunApp> {\n enableAsyncContext();\n const readyApp = await app.createRuntime({\n ...(options.contextMode === undefined ? {} : { contextMode: options.contextMode }),\n ...(options.configs === undefined ? {} : { configs: options.configs }),\n ...(options.prebuilt === undefined ? {} : { prebuilt: options.prebuilt }),\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;;;;;;ACoEnB,MAAMO,sBACJC,UACAC,qBAAAA;AAEA,SAAQC,YAAAA;EACN,MAAMC,OAAOD,SAASC,QAAQ;EAC9B,MAAMC,WAAWF,SAASE,YAAY;EAEtC,MAAMC,SAASC,IAAIC,MAAM;GACvBC,OAAOR;GACPG;GACAC;GACF,CAAA;EACA,MAAMK,WAAWR,iBAAiB,YAAA;AAChC,SAAMI,OAAOK,MAAI;IACnB;AAEA,SAAO;GACLC,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;CAQA,MAAME,SAAiB;EALrB,GAAGH;EACHC;EACAR;EAG+B;AAEjC,MAAK,MAAMW,SAASJ,SAASK,kBAAkB1B,YAAAA,CAC7C2B,QAAOC,eAAeJ,QAAQC,MAAMI,KAAK;EACvCC,OAAO;GACL,GAAGL,MAAMM;GACTnB,OAAOR,mBAAmBqB,MAAMM,aAAalB,QAAQmB,aACnDX,SAASf,iBAAiB0B,SAAAA,CAAAA;GAE9B;EACAC,cAAc;EACdC,YAAY;EACd,CAAA;AAGF,QAAOV;;qDAST,eAAsBW,MACpBC,KACA7B,UAAyB,EAAE,EAAA;AAE3BN,qBAAAA;CACA,MAAMoB,WAAW,MAAMe,IAAIC,cAAc;EACvC,GAAI9B,QAAQ+B,gBAAgBC,KAAAA,IAAY,EAAC,GAAI,EAAED,aAAa/B,QAAQ+B,aAAa;EACjF,GAAI/B,QAAQiC,YAAYD,KAAAA,IAAY,EAAC,GAAI,EAAEC,SAASjC,QAAQiC,SAAS;EACrE,GAAIjC,QAAQkC,aAAaF,KAAAA,IAAY,EAAC,GAAI,EAAEE,UAAUlC,QAAQkC,UAAU;EACxEC,iBAAiB,CAACxC,cAAcC,cAAc;EAC9CwC,QAAQpC,QAAQoC,UAAU;EAC5B,CAAA;CAEA,MAAMC,YAAY,MAAMvB,SAASwB,IAAI3C,aAAAA;CAErC,IAAI4C,eAAe;CACnB,MAAMC,sBAAgB;AACpBH,YAAUI,UAAU,UAAUC,iBAAAA;AAC9BL,YAAUI,UAAU,WAAWC,iBAAAA;;CAGjC,MAAMnC,WAAW,YAAA;AACf,MAAIgC,aAAc;AAClBA,iBAAe;AACf,MAAI;AACF,SAAMzB,SAASP,UAAQ;YACf;AACRiC,kBAAAA;;;CAIJ,MAAME,yBAAmB;AAClBnC,YAAAA,CAAWoC,YAAM,GAAO;;AAG/BN,WAAUO,SAAS,UAAUF,iBAAAA;AAC7BL,WAAUO,SAAS,WAAWF,iBAAAA;AAI9B,QAAO7B,aAAaC,UAAUP,UAFjBG,SAE2BK,CAAAA"}
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","Config","HttpServerAdaptor","BunHttpServerAdaptor","listen","fetch","options","registerShutdown","close","closed","Promise","resolve","server","shutdown","undefined","stop","Bun","serve","port","hostname","error","address","Error","LoggerService","enableAsyncContext","BunCliConfig","BunEnvAdaptor","BunHttpServerAdaptor","getArgs","Bun","argv","slice","onBun","app","options","readyApp","createRuntime","contextMode","undefined","configs","prebuilt","fallbackConfigs","warmup","cliConfig","get","logger","gracefulShutdown","shutdown","catch","error","inspect","depth","Infinity","registerShutdown","offSignal","phase","onSignal","args"],"sources":["../src/bun-cli.config.ts","../src/bun-env.adaptor.ts","../src/bun-http-server.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 HttpListenOptions,\n HttpServerHandle,\n HttpServerShutdownRegistration,\n} from '@zeltjs/core';\nimport { Config, HttpServerAdaptor } from '@zeltjs/core';\n\n@Config\nexport class BunHttpServerAdaptor extends HttpServerAdaptor {\n /** @throws {Error} */\n override async listen(\n fetch: (request: Request) => Promise<Response>,\n options: HttpListenOptions,\n registerShutdown: HttpServerShutdownRegistration,\n ): Promise<HttpServerHandle> {\n let close!: () => void;\n const closed = new Promise<void>((resolve) => {\n close = resolve;\n });\n let server: ReturnType<typeof Bun.serve> | undefined;\n const shutdown = registerShutdown(async () => {\n if (server !== undefined) await server.stop();\n close();\n });\n try {\n server = Bun.serve({\n fetch,\n port: options.port ?? 3000,\n hostname: options.hostname ?? '0.0.0.0',\n });\n } catch (error) {\n await shutdown();\n throw error;\n }\n const port = server.port;\n const address = server.hostname;\n if (port === undefined || address === undefined) {\n await shutdown();\n throw new Error('Bun did not return a TCP listening address');\n }\n return {\n address: { port, address },\n closed,\n shutdown,\n };\n }\n}\n","import type {\n CommandFeature,\n ConfigClass,\n ConfiguredFeature,\n ContextMode,\n FeatureApp,\n HttpFeature,\n ReservedAppProperties,\n RuntimeApp,\n ZeltPrebuilt,\n} from '@zeltjs/core';\nimport { LoggerService } from '@zeltjs/core';\nimport { enableAsyncContext } from '@zeltjs/core/async-context';\n\nimport { BunCliConfig } from './bun-cli.config';\nimport { BunEnvAdaptor } from './bun-env.adaptor';\nimport { BunHttpServerAdaptor } from './bun-http-server.adaptor';\n\nexport type { HttpServerHandle as ServerHandle } from '@zeltjs/core';\n\nexport type BunAppOptions = {\n readonly contextMode?: ContextMode;\n readonly configs?: readonly ConfigClass<object>[];\n readonly warmup?: boolean;\n readonly prebuilt?: ZeltPrebuilt;\n};\n\nexport type { ExecResult } from '@zeltjs/core';\n\ntype EnvironmentBunAppPart = ReservedAppProperties<{\n readonly args: readonly string[];\n}>;\n\nexport type HttpBunApp = RuntimeApp<readonly [HttpFeature<'http'>]> & EnvironmentBunAppPart;\nexport type CommandBunApp = RuntimeApp<readonly [CommandFeature]> & EnvironmentBunAppPart;\nexport type FullBunApp = RuntimeApp<readonly [HttpFeature<'http'>, CommandFeature]> &\n EnvironmentBunAppPart;\n\nexport type BunApp = RuntimeApp<readonly ConfiguredFeature[]> & EnvironmentBunAppPart;\n\ntype BunAppForFeatures<F extends readonly ConfiguredFeature[]> = RuntimeApp<F> &\n EnvironmentBunAppPart;\n\nconst getArgs = (): readonly string[] => Bun.argv.slice(2);\n\n/** @throws {ZeltLifecycleStateError | TypeError} */\nexport async function onBun<const F extends readonly ConfiguredFeature[]>(\n app: FeatureApp<F>,\n options: BunAppOptions = {},\n): Promise<BunAppForFeatures<F>> {\n enableAsyncContext();\n const readyApp = await app.createRuntime({\n ...(options.contextMode === undefined ? {} : { contextMode: options.contextMode }),\n ...(options.configs === undefined ? {} : { configs: options.configs }),\n ...(options.prebuilt === undefined ? {} : { prebuilt: options.prebuilt }),\n fallbackConfigs: [BunCliConfig, BunEnvAdaptor, BunHttpServerAdaptor],\n warmup: options.warmup ?? true,\n });\n\n const cliConfig = await readyApp.get(BunCliConfig);\n const logger = await readyApp.get(LoggerService);\n\n const gracefulShutdown = (): void => {\n void readyApp.shutdown().catch((error: unknown) => {\n logger.error('Bun application shutdown failed', {\n error: Bun.inspect(error, { depth: Infinity }),\n });\n });\n };\n readyApp.registerShutdown(\n () => {\n cliConfig.offSignal('SIGINT', gracefulShutdown);\n cliConfig.offSignal('SIGTERM', gracefulShutdown);\n },\n { phase: 'after' },\n );\n\n cliConfig.onSignal('SIGINT', gracefulShutdown);\n cliConfig.onSignal('SIGTERM', gracefulShutdown);\n\n const args = getArgs();\n\n return { ...readyApp, 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;;;;;;;;;;;;ACGnB,IAAaK,uBAAb,cAA0CD,kBAAAA;wBAExC,MAAeE,OACbC,OACAC,SACAC,kBAC2B;EAC3B,IAAIC;EACJ,MAAMC,SAAS,IAAIC,SAAeC,YAAAA;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QAAQD,OAAO,EAAEE,OAAOC,UAAS,CAAA,EAC9C,CAAA;IACF;;AAEFjB,UAASkB,uBACP;AACEV,YAAUW,UAAU,UAAUR,iBAAAA;AAC9BH,YAAUW,UAAU,WAAWR,iBAAAA;IAEjC,EAAES,OAAO,SAAQ,CAAA;AAGnBZ,WAAUa,SAAS,UAAUV,iBAAAA;AAC7BH,WAAUa,SAAS,WAAWV,iBAAAA;CAE9B,MAAMW,OAAO7B,SAAAA;AAEb,QAAO;EAAE,GAAGO;EAAUsB;EAAK"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zeltjs/adapter-bun",
3
- "version": "0.12.0-canary.3",
3
+ "version": "0.12.0",
4
4
  "description": "Bun adapter for the Zelt framework",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -28,11 +28,11 @@
28
28
  "dist"
29
29
  ],
30
30
  "peerDependencies": {
31
- "@zeltjs/core": "0.12.0-canary.3"
31
+ "@zeltjs/core": "0.12.0"
32
32
  },
33
33
  "devDependencies": {
34
34
  "@types/bun": "1.2.17",
35
- "@zeltjs/core": "0.12.0-canary.3"
35
+ "@zeltjs/core": "0.12.0"
36
36
  },
37
37
  "volta": {
38
38
  "extends": "../../package.json"
@@ -41,6 +41,8 @@
41
41
  "scripts": {
42
42
  "build": "tsdown",
43
43
  "test": "pnpm -w run test:prepare && vitest run",
44
- "typecheck": "tsc -b"
44
+ "typecheck": "node ../../scripts/typecheck-project.mjs",
45
+ "lint": "node --max-old-space-size=3072 ../../node_modules/eslint/bin/eslint.js . && oxlint .",
46
+ "distribution": "node ../../scripts/pack-distribution.mjs"
45
47
  }
46
48
  }