fix types & add bun adapater (WIP)

This commit is contained in:
186526 2022-07-26 08:32:33 +00:00
parent f87ab5366e
commit b1558bd9fa
6 changed files with 66 additions and 11 deletions

View File

@ -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",

View File

@ -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;

20
src/platform/bun.ts Normal file
View File

@ -0,0 +1,20 @@
import { SWPlatformAdapter } from "./serviceworker";
import { platformAdapater } from "./index";
export class BunPlatformAdapter<T = any, K = any>
extends SWPlatformAdapter<T, K>
implements platformAdapater<T, K>
{
async listen(port: number): Promise<void> {
Bun.serve({
fetch: async (request: Request): Promise<Response> => {
return await this.handleResponse(
await this.handleRequest(request).then((request) =>
this.router.respond(request)
)
);
},
port,
});
}
}

View File

@ -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
}

View File

@ -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;

View File

@ -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"