@zeltjs/adapter-bun 0.4.0 → 0.6.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 +35 -0
- package/dist/index.d.ts +2 -5
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +17 -31
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
package/README.md
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# @zeltjs/adapter-bun
|
|
2
|
+
|
|
3
|
+
[](https://zeltjs.com)
|
|
4
|
+
|
|
5
|
+
Bun runtime adapter for Zelt applications.
|
|
6
|
+
|
|
7
|
+
**[Read the Documentation](https://zeltjs.com)**
|
|
8
|
+
|
|
9
|
+
## Installation
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
bun add @zeltjs/adapter-bun @zeltjs/core
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Usage
|
|
16
|
+
|
|
17
|
+
```typescript
|
|
18
|
+
import { createApp, Controller, Get } from '@zeltjs/core';
|
|
19
|
+
import { onBun } from '@zeltjs/adapter-bun';
|
|
20
|
+
|
|
21
|
+
@Controller('/hello')
|
|
22
|
+
class HelloController {
|
|
23
|
+
@Get('/')
|
|
24
|
+
greet() {
|
|
25
|
+
return { message: 'Hello from Bun!' };
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const app = createApp({
|
|
30
|
+
http: { controllers: [HelloController] },
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
const bun = await onBun(app);
|
|
34
|
+
bun.listen(3000);
|
|
35
|
+
```
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { CommandApp, EnvConfig, HttpApp, ReadyResult } from "@zeltjs/core";
|
|
1
|
+
import { CommandApp, EnvConfig, ExecResult, ExecResult as ExecResult$1, HttpApp, ReadyResult } from "@zeltjs/core";
|
|
2
2
|
|
|
3
3
|
//#region src/bun-env.config.d.ts
|
|
4
4
|
declare class BunEnvConfig extends EnvConfig {
|
|
@@ -20,9 +20,6 @@ type ServerHandle = {
|
|
|
20
20
|
type BunAppOptions = {
|
|
21
21
|
readonly warmup?: boolean;
|
|
22
22
|
};
|
|
23
|
-
type ExecResult = {
|
|
24
|
-
readonly exitCode: 0 | 1;
|
|
25
|
-
};
|
|
26
23
|
type BunAppBase = {
|
|
27
24
|
readonly args: readonly string[];
|
|
28
25
|
};
|
|
@@ -31,7 +28,7 @@ type HttpBunApp = ReadyResult & BunAppBase & {
|
|
|
31
28
|
readonly shutdown: () => Promise<void>;
|
|
32
29
|
};
|
|
33
30
|
type CommandBunApp = ReadyResult & BunAppBase & {
|
|
34
|
-
readonly execCommand: (argv: string[]) => Promise<ExecResult>;
|
|
31
|
+
readonly execCommand: (argv: readonly string[]) => Promise<ExecResult$1>;
|
|
35
32
|
readonly shutdown: () => Promise<void>;
|
|
36
33
|
};
|
|
37
34
|
type FullBunApp = HttpBunApp & CommandBunApp;
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/bun-env.config.ts","../src/on-bun.ts"],"mappings":";;;cAGa,YAAA,SAAqB,SAAA;EACvB,GAAA,CAAI,GAAA;AAAA;;;KCAV,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,
|
|
1
|
+
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/bun-env.config.ts","../src/on-bun.ts"],"mappings":";;;cAGa,YAAA,SAAqB,SAAA;EACvB,GAAA,CAAI,GAAA;AAAA;;;KCAV,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;AAvInF;AAAA,iBAyIgB,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,14 +1,19 @@
|
|
|
1
1
|
import { Config, EnvConfig } from "@zeltjs/core";
|
|
2
2
|
//#region src/bun-env.config.ts
|
|
3
|
-
|
|
3
|
+
function _ts_decorate(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 BunEnvConfig = class extends EnvConfig {
|
|
4
10
|
get(key) {
|
|
5
11
|
return Bun.env[key];
|
|
6
12
|
}
|
|
7
13
|
};
|
|
14
|
+
BunEnvConfig = _ts_decorate([Config], BunEnvConfig);
|
|
8
15
|
//#endregion
|
|
9
16
|
//#region src/on-bun.ts
|
|
10
|
-
const successResult = { exitCode: 0 };
|
|
11
|
-
const failureResult = { exitCode: 1 };
|
|
12
17
|
const createServeForHttp = (appFetch, appShutdown) => {
|
|
13
18
|
return (options) => {
|
|
14
19
|
const port = options?.port ?? 3e3;
|
|
@@ -31,28 +36,6 @@ const createServeForHttp = (appFetch, appShutdown) => {
|
|
|
31
36
|
};
|
|
32
37
|
};
|
|
33
38
|
};
|
|
34
|
-
/** @throws {ZeltLifecycleStateError} */
|
|
35
|
-
const runCommand = (CommandClass, get) => {
|
|
36
|
-
const instance = get(CommandClass);
|
|
37
|
-
return Promise.resolve().then(() => instance.run()).then(() => successResult).catch(() => failureResult);
|
|
38
|
-
};
|
|
39
|
-
/** @throws {ZeltLifecycleStateError} */
|
|
40
|
-
const createExecForCommands = (hasCommand, getCommands, get, stderr) => {
|
|
41
|
-
return async (argv) => {
|
|
42
|
-
const commandName = argv[0];
|
|
43
|
-
if (!commandName) {
|
|
44
|
-
stderr.write("No command specified\n");
|
|
45
|
-
return failureResult;
|
|
46
|
-
}
|
|
47
|
-
if (!hasCommand(commandName)) {
|
|
48
|
-
stderr.write(`Command not found: ${commandName}\n`);
|
|
49
|
-
return failureResult;
|
|
50
|
-
}
|
|
51
|
-
const CommandClass = getCommands().get(commandName);
|
|
52
|
-
if (!CommandClass) return failureResult;
|
|
53
|
-
return runCommand(CommandClass, get);
|
|
54
|
-
};
|
|
55
|
-
};
|
|
56
39
|
const getStderr = () => globalThis.process.stderr;
|
|
57
40
|
const getArgs = () => Bun.argv.slice(2);
|
|
58
41
|
const extractCapabilities = (app) => app;
|
|
@@ -65,17 +48,21 @@ const buildHttpBunApp = (caps, resolver, shutdown, args) => {
|
|
|
65
48
|
shutdown
|
|
66
49
|
};
|
|
67
50
|
};
|
|
68
|
-
/** @throws {ZeltLifecycleStateError} */
|
|
69
51
|
const buildCommandBunApp = (caps, resolver, shutdown, stderr, args) => {
|
|
70
|
-
if (typeof caps.
|
|
52
|
+
if (typeof caps.execCommand !== "function") return void 0;
|
|
53
|
+
const coreExecCommand = caps.execCommand;
|
|
54
|
+
const execCommand = async (argv) => {
|
|
55
|
+
const result = await coreExecCommand(argv);
|
|
56
|
+
if (result.exitCode === 1) stderr.write(`${result.reason.message}\n`);
|
|
57
|
+
return result;
|
|
58
|
+
};
|
|
71
59
|
return {
|
|
72
60
|
...resolver,
|
|
73
61
|
args,
|
|
74
|
-
execCommand
|
|
62
|
+
execCommand,
|
|
75
63
|
shutdown
|
|
76
64
|
};
|
|
77
65
|
};
|
|
78
|
-
/** @throws {ZeltLifecycleStateError} */
|
|
79
66
|
const buildBunApps = (caps, resolver, shutdown, stderr, args) => ({
|
|
80
67
|
httpResult: buildHttpBunApp(caps, resolver, shutdown, args),
|
|
81
68
|
commandResult: buildCommandBunApp(caps, resolver, shutdown, stderr, args)
|
|
@@ -101,8 +88,7 @@ const mergeBunApps = (result, resolver, shutdown, args) => {
|
|
|
101
88
|
})
|
|
102
89
|
};
|
|
103
90
|
};
|
|
104
|
-
/** @throws {ZeltLifecycleStateError} */
|
|
105
|
-
async function onBun(app, options = {}) {
|
|
91
|
+
/** @throws {ZeltLifecycleStateError} */ async function onBun(app, options = {}) {
|
|
106
92
|
app.addFallbackConfig(BunEnvConfig);
|
|
107
93
|
const readyOptions = { warmup: options.warmup ?? true };
|
|
108
94
|
const resolver = await app.ready(readyOptions);
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":[],"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,
|
|
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;EAE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C,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"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zeltjs/adapter-bun",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.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.6.0"
|
|
25
25
|
},
|
|
26
26
|
"devDependencies": {
|
|
27
27
|
"@types/bun": "1.2.17"
|