@ereo/runtime-bun 0.1.6

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 ADDED
@@ -0,0 +1,137 @@
1
+ # @ereo/runtime-bun
2
+
3
+ Bun runtime adapter for the EreoJS framework. This is the default runtime, optimized for Bun's exceptional performance and built-in features.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ bun add @ereo/runtime-bun
9
+ ```
10
+
11
+ ## Quick Start
12
+
13
+ ```typescript
14
+ import { createBunRuntime, serve } from '@ereo/runtime-bun';
15
+
16
+ // Quick start with defaults
17
+ const runtime = await serve({
18
+ server: { port: 3000 },
19
+ });
20
+
21
+ // Or with more control
22
+ const runtime = createBunRuntime({
23
+ server: {
24
+ port: 3000,
25
+ hostname: 'localhost',
26
+ },
27
+ config: {
28
+ // Framework configuration
29
+ },
30
+ });
31
+
32
+ runtime.use(myPlugin);
33
+ await runtime.start();
34
+ ```
35
+
36
+ ## API
37
+
38
+ ### `createBunRuntime(options?)`
39
+
40
+ Create a new Bun runtime instance.
41
+
42
+ ```typescript
43
+ const runtime = createBunRuntime({
44
+ server: { port: 3000 },
45
+ config: { /* EreoJS config */ },
46
+ });
47
+ ```
48
+
49
+ ### `serve(options?)`
50
+
51
+ Quick start helper that creates and starts the runtime.
52
+
53
+ ```typescript
54
+ const runtime = await serve({ server: { port: 3000 } });
55
+ ```
56
+
57
+ ### Runtime Methods
58
+
59
+ ```typescript
60
+ const runtime = createBunRuntime({ server: { port: 3000 } });
61
+
62
+ // Register plugins
63
+ runtime.use(myPlugin);
64
+
65
+ // Start the server
66
+ const server = await runtime.start();
67
+
68
+ // Handle requests directly (useful for testing)
69
+ const response = await runtime.handle(new Request('http://localhost/api/health'));
70
+
71
+ // Stop the server
72
+ runtime.stop();
73
+ ```
74
+
75
+ ## Bun-Specific Utilities
76
+
77
+ The package includes optimized utilities that leverage Bun's built-in features:
78
+
79
+ ```typescript
80
+ import {
81
+ readFile,
82
+ writeFile,
83
+ readJSON,
84
+ gzip,
85
+ gunzip,
86
+ hashPassword,
87
+ verifyPassword,
88
+ randomUUID,
89
+ sleep,
90
+ spawn,
91
+ env,
92
+ requireEnv,
93
+ getDatabase,
94
+ } from '@ereo/runtime-bun';
95
+
96
+ // File operations
97
+ const content = await readFile('./data.txt');
98
+ await writeFile('./output.txt', content);
99
+ const config = await readJSON('./config.json');
100
+
101
+ // Compression
102
+ const compressed = gzip('Hello World');
103
+ const decompressed = gunzip(compressed);
104
+
105
+ // Password hashing
106
+ const hash = await hashPassword('secret');
107
+ const valid = await verifyPassword('secret', hash);
108
+
109
+ // Environment variables
110
+ const apiKey = env('API_KEY', 'default');
111
+ const dbUrl = requireEnv('DATABASE_URL'); // throws if missing
112
+
113
+ // SQLite database
114
+ const db = await getDatabase('./app.db');
115
+ ```
116
+
117
+ ## Key Features
118
+
119
+ - Native Bun server integration
120
+ - Optimized file I/O with Bun.file
121
+ - Built-in gzip compression
122
+ - Secure password hashing with Bun.password
123
+ - SQLite database support via bun:sqlite
124
+ - Environment variable helpers
125
+ - Process spawning utilities
126
+
127
+ ## Documentation
128
+
129
+ For full documentation, visit [https://ereo.dev/docs/runtime-bun](https://ereo.dev/docs/runtime-bun)
130
+
131
+ ## Part of EreoJS
132
+
133
+ This package is part of the [EreoJS monorepo](https://github.com/anthropics/ereo-js).
134
+
135
+ ## License
136
+
137
+ MIT
@@ -0,0 +1,126 @@
1
+ /**
2
+ * @ereo/runtime-bun
3
+ *
4
+ * Bun runtime adapter for the EreoJS framework.
5
+ * This is the default runtime, optimized for Bun's performance.
6
+ */
7
+ import type { Server } from 'bun';
8
+ import type { FrameworkConfig, Plugin } from '@ereo/core';
9
+ import { createApp, EreoApp } from '@ereo/core';
10
+ import { createServer, type ServerOptions } from '@ereo/server';
11
+ /**
12
+ * Bun runtime options.
13
+ */
14
+ export interface BunRuntimeOptions {
15
+ /** Server configuration */
16
+ server?: ServerOptions;
17
+ /** Framework configuration */
18
+ config?: FrameworkConfig;
19
+ }
20
+ /**
21
+ * Bun runtime adapter.
22
+ */
23
+ export declare class BunRuntime {
24
+ private app;
25
+ private server;
26
+ private bunServer;
27
+ private options;
28
+ constructor(options?: BunRuntimeOptions);
29
+ /**
30
+ * Get the EreoJS app instance.
31
+ */
32
+ getApp(): EreoApp;
33
+ /**
34
+ * Register a plugin.
35
+ */
36
+ use(plugin: Plugin): this;
37
+ /**
38
+ * Start the server.
39
+ */
40
+ start(): Promise<Server<unknown>>;
41
+ /**
42
+ * Stop the server.
43
+ */
44
+ stop(): void;
45
+ /**
46
+ * Handle a request directly (for testing or custom integrations).
47
+ */
48
+ handle(request: Request): Promise<Response>;
49
+ }
50
+ /**
51
+ * Create a Bun runtime.
52
+ */
53
+ export declare function createBunRuntime(options?: BunRuntimeOptions): BunRuntime;
54
+ /**
55
+ * Quick start helper.
56
+ */
57
+ export declare function serve(options?: BunRuntimeOptions): Promise<BunRuntime>;
58
+ /**
59
+ * Get Bun's SQLite database.
60
+ * Uses dynamic import to avoid bundling issues in non-Bun environments.
61
+ */
62
+ export declare function getDatabase(path: string): Promise<import("bun:sqlite").Database>;
63
+ /**
64
+ * Check if running in Bun.
65
+ */
66
+ export declare function isBun(): boolean;
67
+ /**
68
+ * Get Bun version.
69
+ */
70
+ export declare function getBunVersion(): string;
71
+ /**
72
+ * Bun-optimized file reading.
73
+ */
74
+ export declare function readFile(path: string): Promise<string>;
75
+ /**
76
+ * Bun-optimized file writing.
77
+ */
78
+ export declare function writeFile(path: string, content: string): Promise<void>;
79
+ /**
80
+ * Bun-optimized JSON reading.
81
+ */
82
+ export declare function readJSON<T = unknown>(path: string): Promise<T>;
83
+ /**
84
+ * Bun-optimized gzip compression.
85
+ */
86
+ export declare function gzip(data: string | ArrayBuffer): Uint8Array;
87
+ /**
88
+ * Bun-optimized gunzip decompression.
89
+ */
90
+ export declare function gunzip(data: ArrayBuffer): Uint8Array;
91
+ /**
92
+ * Bun password hashing.
93
+ */
94
+ export declare function hashPassword(password: string): Promise<string>;
95
+ /**
96
+ * Bun password verification.
97
+ */
98
+ export declare function verifyPassword(password: string, hash: string): Promise<boolean>;
99
+ /**
100
+ * Generate crypto-random UUID.
101
+ */
102
+ export declare function randomUUID(): string;
103
+ /**
104
+ * Sleep utility using Bun.sleep.
105
+ */
106
+ export declare function sleep(ms: number): Promise<void>;
107
+ /**
108
+ * Spawn a shell command.
109
+ */
110
+ export declare function spawn(command: string[], options?: {
111
+ cwd?: string;
112
+ env?: Record<string, string>;
113
+ }): Bun.Subprocess<"ignore", "pipe", "inherit">;
114
+ /**
115
+ * Get environment variable with type safety.
116
+ * Uses Bun.env for better Bun compatibility.
117
+ */
118
+ export declare function env<T extends string = string>(key: string, defaultValue?: T): T;
119
+ /**
120
+ * Get required environment variable (throws if missing).
121
+ * Uses Bun.env for better Bun compatibility.
122
+ */
123
+ export declare function requireEnv(key: string): string;
124
+ export { createApp, createServer };
125
+ export type { FrameworkConfig, Plugin, ServerOptions };
126
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EAAE,MAAM,EAAmB,MAAM,KAAK,CAAC;AACnD,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,EAAE,MAAM,YAAY,CAAC;AAC1D,OAAO,EAAE,SAAS,EAAE,OAAO,EAAE,MAAM,YAAY,CAAC;AAChD,OAAO,EAAE,YAAY,EAAE,KAAK,aAAa,EAAE,MAAM,cAAc,CAAC;AAEhE;;GAEG;AACH,MAAM,WAAW,iBAAiB;IAChC,2BAA2B;IAC3B,MAAM,CAAC,EAAE,aAAa,CAAC;IACvB,8BAA8B;IAC9B,MAAM,CAAC,EAAE,eAAe,CAAC;CAC1B;AAED;;GAEG;AACH,qBAAa,UAAU;IACrB,OAAO,CAAC,GAAG,CAAU;IACrB,OAAO,CAAC,MAAM,CAAgD;IAC9D,O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G;AACH,wBAAsB,YAAY,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAEpE;AAED;;GAEG;AACH,wBAAsB,cAAc,CAClC,QAAQ,EAAE,MAAM,EAChB,IAAI,EAAE,MAAM,GACX,OAAO,CAAC,OAAO,CAAC,CAElB;AAED;;GAEG;AACH,wBAAgB,UAAU,IAAI,MAAM,CAEnC;AAED;;GAEG;AACH,wBAAsB,KAAK,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAErD;AAED;;GAEG;AACH,wBAAgB,KAAK,CACnB,OAAO,EAAE,MAAM,EAAE,EACjB,OAAO,CAAC,EAAE;IAAE,GAAG,CAAC,EAAE,MAAM,CAAC;IAAC,GAAG,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;CAAE,+CAGzD;AAED;;;GAGG;AACH,wBAAgB,GAAG,CAAC,CAAC,SAAS,MAAM,GAAG,MAAM,EAAE,GAAG,EAAE,MAAM,EAAE,YAAY,CAAC,EAAE,CAAC,GAAG,CAAC,CAE/E;AAED;;;GAGG;AACH,wBAAgB,UAAU,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAM9C;AAGD,OAAO,EAAE,SAAS,EAAE,YAAY,EAAE,CAAC;AACnC,YAAY,EAAE,eAAe,EAAE,MAAM,EAAE,aAAa,EAAE,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,119 @@
1
+ // @bun
2
+ var __require = import.meta.require;
3
+
4
+ // src/index.ts
5
+ import { createApp } from "@ereo/core";
6
+ import { createServer } from "@ereo/server";
7
+
8
+ class BunRuntime {
9
+ app;
10
+ server = null;
11
+ bunServer = null;
12
+ options;
13
+ constructor(options = {}) {
14
+ this.options = options;
15
+ this.app = createApp({ config: options.config });
16
+ }
17
+ getApp() {
18
+ return this.app;
19
+ }
20
+ use(plugin) {
21
+ this.app.use(plugin);
22
+ return this;
23
+ }
24
+ async start() {
25
+ this.server = createServer(this.options.server);
26
+ this.server.setApp(this.app);
27
+ const server = await this.server.start();
28
+ this.bunServer = server;
29
+ return server;
30
+ }
31
+ stop() {
32
+ if (this.server) {
33
+ this.server.stop();
34
+ }
35
+ }
36
+ async handle(request) {
37
+ return this.app.handle(request);
38
+ }
39
+ }
40
+ function createBunRuntime(options) {
41
+ return new BunRuntime(options);
42
+ }
43
+ async function serve(options) {
44
+ const runtime = createBunRuntime(options);
45
+ await runtime.start();
46
+ return runtime;
47
+ }
48
+ async function getDatabase(path) {
49
+ const { Database } = await import("bun:sqlite");
50
+ return new Database(path);
51
+ }
52
+ function isBun() {
53
+ return typeof Bun !== "undefined";
54
+ }
55
+ function getBunVersion() {
56
+ return Bun.version;
57
+ }
58
+ async function readFile(path) {
59
+ return Bun.file(path).text();
60
+ }
61
+ async function writeFile(path, content) {
62
+ await Bun.write(path, content);
63
+ }
64
+ async function readJSON(path) {
65
+ return Bun.file(path).json();
66
+ }
67
+ function gzip(data) {
68
+ return Bun.gzipSync(data);
69
+ }
70
+ function gunzip(data) {
71
+ return Bun.gunzipSync(data);
72
+ }
73
+ async function hashPassword(password) {
74
+ return Bun.password.hash(password);
75
+ }
76
+ async function verifyPassword(password, hash) {
77
+ return Bun.password.verify(password, hash);
78
+ }
79
+ function randomUUID() {
80
+ return crypto.randomUUID();
81
+ }
82
+ async function sleep(ms) {
83
+ return Bun.sleep(ms);
84
+ }
85
+ function spawn(command, options) {
86
+ return Bun.spawn(command, options);
87
+ }
88
+ function env(key, defaultValue) {
89
+ return Bun.env[key] ?? defaultValue;
90
+ }
91
+ function requireEnv(key) {
92
+ const value = Bun.env[key];
93
+ if (!value) {
94
+ throw new Error(`Missing required environment variable: ${key}`);
95
+ }
96
+ return value;
97
+ }
98
+ export {
99
+ writeFile,
100
+ verifyPassword,
101
+ spawn,
102
+ sleep,
103
+ serve,
104
+ requireEnv,
105
+ readJSON,
106
+ readFile,
107
+ randomUUID,
108
+ isBun,
109
+ hashPassword,
110
+ gzip,
111
+ gunzip,
112
+ getDatabase,
113
+ getBunVersion,
114
+ env,
115
+ createServer,
116
+ createBunRuntime,
117
+ createApp,
118
+ BunRuntime
119
+ };
package/package.json ADDED
@@ -0,0 +1,42 @@
1
+ {
2
+ "name": "@ereo/runtime-bun",
3
+ "version": "0.1.6",
4
+ "license": "MIT",
5
+ "author": "Ereo Team",
6
+ "homepage": "https://ereo.dev",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "https://github.com/ereojs/ereo.git",
10
+ "directory": "packages/runtime-bun"
11
+ },
12
+ "bugs": {
13
+ "url": "https://github.com/ereojs/ereo/issues"
14
+ },
15
+ "type": "module",
16
+ "main": "./dist/index.js",
17
+ "types": "./dist/index.d.ts",
18
+ "exports": {
19
+ ".": {
20
+ "types": "./dist/index.d.ts",
21
+ "import": "./dist/index.js"
22
+ }
23
+ },
24
+ "files": [
25
+ "dist"
26
+ ],
27
+ "scripts": {
28
+ "build": "bun build ./src/index.ts --outdir ./dist --target bun --external @ereo/core --external @ereo/server && bun run build:types",
29
+ "build:types": "tsc --emitDeclarationOnly --outDir dist",
30
+ "dev": "bun build ./src/index.ts --outdir ./dist --target bun --watch",
31
+ "test": "bun test",
32
+ "typecheck": "tsc --noEmit"
33
+ },
34
+ "dependencies": {
35
+ "@ereo/core": "workspace:*",
36
+ "@ereo/server": "workspace:*"
37
+ },
38
+ "devDependencies": {
39
+ "@types/bun": "^1.1.0",
40
+ "typescript": "^5.4.0"
41
+ }
42
+ }