@anderspitman/fetch-handler 0.1.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/bun.lock ADDED
@@ -0,0 +1,16 @@
1
+ {
2
+ "lockfileVersion": 1,
3
+ "workspaces": {
4
+ "": {
5
+ "dependencies": {
6
+ "@hono/node-server": "^1.17.1",
7
+ "hono": "^4.8.10",
8
+ },
9
+ },
10
+ },
11
+ "packages": {
12
+ "@hono/node-server": ["@hono/node-server@1.17.1", "", { "peerDependencies": { "hono": "^4" } }, "sha512-SY79W/C+2b1MyAzmIcV32Q47vO1b5XwLRwj8S9N6Jr5n1QCkIfAIH6umOSgqWZ4/v67hg6qq8Ha5vZonVidGsg=="],
13
+
14
+ "hono": ["hono@4.8.10", "", {}, "sha512-DRMYbR3aFk6YET1FCSAFbgF2cWYTz5j0YAFYPECx9fmrbKBDAYnWU+YCgRTpOaatxMYN6e68U/2IG39zRP4W/A=="],
15
+ }
16
+ }
@@ -0,0 +1,7 @@
1
+ import { serve } from '../../fetch_handler.js';
2
+
3
+ const handler = (req) => {
4
+ return new Response("Hi there");
5
+ }
6
+
7
+ serve({ handler, port: 3000 });
@@ -0,0 +1,55 @@
1
+ async function serve(opt) {
2
+
3
+ switch (detectRuntime()) {
4
+ case 'deno': {
5
+ Deno.serve({ handler: opt.handler, port: opt?.port });
6
+ break;
7
+ }
8
+ case 'bun': {
9
+ Bun.serve({ fetch: opt.handler, port: opt?.port })
10
+ break;
11
+ }
12
+ default: {
13
+ // Default to assuming node/hono
14
+
15
+ const { serve } = await import('@hono/node-server');
16
+ const { Hono } = await import('hono');
17
+
18
+ const app = new Hono();
19
+ app.use('*', (c) => {
20
+ return opt.handler(c.req.raw);
21
+ });
22
+
23
+ await serve({
24
+ fetch: app.fetch,
25
+ port: opt?.port,
26
+ });
27
+
28
+ break;
29
+ }
30
+ }
31
+ }
32
+
33
+ function detectRuntime() {
34
+ if (typeof Deno !== "undefined" && typeof Deno.version !== "undefined") {
35
+ return "deno";
36
+ }
37
+
38
+ if (typeof Bun !== "undefined" && typeof Bun.version !== "undefined") {
39
+ return "bun";
40
+ }
41
+
42
+ if (typeof process !== "undefined" && process.versions != null && process.versions.node != null) {
43
+ return "node";
44
+ }
45
+
46
+ if (typeof window !== "undefined" && typeof document !== "undefined") {
47
+ return "browser";
48
+ }
49
+
50
+ return "unknown";
51
+ }
52
+
53
+ export {
54
+ serve,
55
+ };
package/package.json ADDED
@@ -0,0 +1,20 @@
1
+ {
2
+ "type": "module",
3
+ "dependencies": {
4
+ "@hono/node-server": "^1.17.1",
5
+ "hono": "^4.8.10"
6
+ },
7
+ "name": "@anderspitman/fetch-handler",
8
+ "publishConfig": {
9
+ "access": "public"
10
+ },
11
+ "version": "0.1.0",
12
+ "main": "fetch_handler.js",
13
+ "devDependencies": {},
14
+ "scripts": {
15
+ "test": "echo \"Error: no test specified\" && exit 1"
16
+ },
17
+ "author": "",
18
+ "license": "MIT",
19
+ "description": ""
20
+ }