fix types & add bun adapater (WIP)

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

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,
});
}
}