From b1558bd9fade791eb697b243f1f511cab8fa2812 Mon Sep 17 00:00:00 2001 From: 186526 Date: Tue, 26 Jul 2022 08:32:33 +0000 Subject: [PATCH] fix types & add bun adapater (WIP) --- package.json | 9 ++++++--- src/lib.ts | 5 ++++- src/platform/bun.ts | 20 ++++++++++++++++++++ tsconfig.json | 30 +++++++++++++++++++++++------- webpack.config.js | 8 ++++++++ yarn.lock | 5 +++++ 6 files changed, 66 insertions(+), 11 deletions(-) create mode 100644 src/platform/bun.ts diff --git a/package.json b/package.json index 0627157..7c8895c 100644 --- a/package.json +++ b/package.json @@ -1,11 +1,12 @@ { "name": "handlers.js", "description": "Handlers.js is a unified and lightweight web application framework for multiple platforms.", - "version": "0.1.0", - "main": "./dist/main.node.js", + "version": "0.1.1", + "main": "./dist/index.js", "webpack": "./dist/index.js", "browser": "./dist/main.serviceworker.js", "module": "./dist/index.js", + "types": "dist/index.d.ts", "files": [ "dist", "LICENSE" @@ -32,6 +33,7 @@ "@webpack-cli/generators": "^2.5.0", "axios": "^0.27.2", "bluebird": "^3.7.2", + "bun-types": "^0.1.4", "jest": "^28.1.2", "prettier": "^2.7.1", "ts-jest": "^28.0.5", @@ -43,12 +45,13 @@ }, "sideEffects": false, "scripts": { - "build": "yarn clean && yarn build:node && yarn build:serviceworker && yarn build:cfworker && yarn build:deno && yarn build:txiki", + "build": "yarn clean && yarn build:node && yarn build:serviceworker && yarn build:cfworker && yarn build:deno && yarn build:txiki && yarn build:bun", "build:node": "BUILD_TARGET=node webpack", "build:serviceworker": "BUILD_TARGET=serviceworker webpack", "build:cfworker": "BUILD_TARGET=cfworker webpack", "build:deno": "BUILD_TARGET=deno webpack", "build:txiki": "BUILD_TARGET=txiki webpack", + "build:bun": "BUILD_TARGET=bun webpack", "watch": "webpack --watch", "clean": "rm -rf ./dist", "demo": "env NODE_ENV=development yarn build:node && node ./dist/main.node.js", diff --git a/src/lib.ts b/src/lib.ts index cb0604c..1b0c048 100644 --- a/src/lib.ts +++ b/src/lib.ts @@ -7,6 +7,9 @@ export const platform = (() => { if (typeof Deno != "undefined") { return "Deno"; } + if (typeof Bun != "undefined") { + return "Bun"; + } if (typeof tjs != "undefined") { return "txiki.js"; } @@ -17,7 +20,7 @@ export const platform = (() => { })(); export const version = (() => { switch (platform) { - case "Node.js": + case "Node.js" || "Bun": return process.version; case "Deno": return Deno.version.deno; diff --git a/src/platform/bun.ts b/src/platform/bun.ts new file mode 100644 index 0000000..c287c4f --- /dev/null +++ b/src/platform/bun.ts @@ -0,0 +1,20 @@ +import { SWPlatformAdapter } from "./serviceworker"; +import { platformAdapater } from "./index"; + +export class BunPlatformAdapter + extends SWPlatformAdapter + implements platformAdapater +{ + async listen(port: number): Promise { + Bun.serve({ + fetch: async (request: Request): Promise => { + return await this.handleResponse( + await this.handleRequest(request).then((request) => + this.router.respond(request) + ) + ); + }, + port, + }); + } +} diff --git a/tsconfig.json b/tsconfig.json index adc8cc8..c57e66e 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -2,8 +2,15 @@ "compilerOptions": { "target": "esnext", "module": "esnext", - "lib": ["ESNext"], - "types": ["@cloudflare/workers-types", "@types/node", "@types/jest"], + "lib": [ + "ESNext" + ], + "types": [ + "@cloudflare/workers-types", + "@types/node", + "@types/jest", + "bun-types" + ], "skipLibCheck": true, "sourceMap": true, "outDir": "./dist", @@ -23,12 +30,21 @@ "experimentalDecorators": true, "resolveJsonModule": true, "baseUrl": ".", - "plugins": [{ - "transform": "@zerollup/ts-transform-paths" - }], + "plugins": [ + { + "transform": "@zerollup/ts-transform-paths" + } + ], "declaration": true }, - "exclude": ["node_modules"], - "include": ["index.ts", "src/**/*.ts", "demo/**/*.ts", "types/*.d.ts"], + "exclude": [ + "node_modules" + ], + "include": [ + "index.ts", + "src/**/*.ts", + "demo/**/*.ts", + "types/*.d.ts" + ], // "esm": true } \ No newline at end of file diff --git a/webpack.config.js b/webpack.config.js index d1acd6c..50a7d0e 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -81,12 +81,20 @@ export default () => { 'Promise': 'bluebird' }) ) + break; + case "bun": + config.mode = "production"; + config.target = "node12"; + config.output.filename = "main.bun.js"; + break; case "deno:test": config.target = "webworker"; config.output.filename = "test.deno.js"; config.entry = "./test/test-server.deno.ts"; + break; default: config.target = "es6"; + break; } return config; diff --git a/yarn.lock b/yarn.lock index a7057d8..d1ebcb5 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1565,6 +1565,11 @@ builtins@^1.0.3: resolved "https://registry.yarnpkg.com/builtins/-/builtins-1.0.3.tgz#cb94faeb61c8696451db36534e1422f94f0aee88" integrity sha512-uYBjakWipfaO/bXI7E8rq6kpwHRZK5cNYrUv2OzZSI/FvmdMyXJ2tG9dKcjEC5YHmHpUAwsargWIZNWdxb/bnQ== +bun-types@^0.1.4: + version "0.1.4" + resolved "https://registry.yarnpkg.com/bun-types/-/bun-types-0.1.4.tgz#b4ab4ae10828bea0eacf1f221b197b37abd88cda" + integrity sha512-8nyx22INhlPT+R/1MebbAK7TX48aLQdrLTEmce3oTlne5GunoxMEtCiHOku90XKeHbxxbMkkPI2TX1m0pvlLaw== + cacache@^15.0.3, cacache@^15.0.5, cacache@^15.2.0: version "15.3.0" resolved "https://registry.yarnpkg.com/cacache/-/cacache-15.3.0.tgz#dc85380fb2f556fe3dda4c719bfa0ec875a7f1eb"