Skip to content

Commit

Permalink
chore: small fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Sep 16, 2024
1 parent a06c056 commit 592b97c
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 43 deletions.
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.github
pnpm-lock.yaml
.docs
4 changes: 1 addition & 3 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import unjs from "eslint-config-unjs";

export default unjs({
ignores: [
// ignore paths
],
ignores: ["**/.docs"],
rules: {
"unicorn/no-null": "off",
},
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
".": {
"types": "./dist/types.d.mts",
"bun": "./dist/bun.mjs",
"deno": "./dist/deno.mjs",
"node": "./dist/node.mjs"
},
"./node-utils": {
Expand All @@ -26,13 +27,13 @@
"dist"
],
"scripts": {
"bench:node": "node test/bench-node/_run.mjs",
"build": "unbuild",
"dev": "vitest dev",
"lint": "eslint . && prettier -c .",
"lint:fix": "automd && eslint . --fix && prettier -w .",
"prepack": "pnpm build",
"play:bun": "bun run ./playground/index.ts",
"bench:node": "node test/bench-node/_run.mjs",
"play:deno": "deno run --unstable-byonm -A ./playground/index.ts",
"play:node": "node --import jiti/register ./playground/index.ts",
"release": "pnpm test && changelogen --release && npm publish && git push --follow-tags",
Expand All @@ -57,9 +58,9 @@
"get-port-please": "^3.1.2",
"jiti": "^2.0.0-beta.3",
"prettier": "^3.3.3",
"srvx": "link:.",
"typescript": "^5.5.4",
"unbuild": "^3.0.0-rc.7",
"srvx": "link:.",
"vitest": "^2.0.5"
},
"packageManager": "[email protected]"
Expand Down
12 changes: 8 additions & 4 deletions playground/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import { serve } from "srvx";
// prettier-ignore
const runtime = (globalThis as any).Deno ? "deno" : (globalThis.Bun ? "bun" : "node");
const { serve } = (await import(
`../src/${runtime}.ts`
)) as typeof import("../src/types.ts");

const server = serve({
remoteAddress: true,
export const server = serve({
xRemoteAddress: true,
fetch(request) {
return new Response(
/* html */ `
<h1>👋 Hello there</h1>
<p>You are visiting <code>${request.url}</code> from <code>${request.remoteAddress}</code></p>
<p>You are visiting <code>${request.url}</code> from <code>${request.xRemoteAddress}</code></p>
<hr>
Runtime: ${server.runtime}
`,
Expand Down
12 changes: 12 additions & 0 deletions src/_common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export function resolvePort(
portConfig: string | number | undefined,
portEnv: string | undefined,
): number {
const portInput = portConfig ?? portEnv;
if (portInput === undefined) {
return 3000;
}
return typeof portInput === "number"
? portInput
: Number.parseInt(portInput, 10);
}
10 changes: 2 additions & 8 deletions src/bun.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { ServerOptions } from "./types";
import { Server } from "./server.ts";
import { resolvePort } from "./_common.ts";

export function serve(options: ServerOptions): Server {
return new BunServer(options);
Expand Down Expand Up @@ -28,7 +29,7 @@ class BunServer extends Server {
}

this.bunServer = Bun.serve({
port: resolvePort(options.port),
port: resolvePort(options.port, globalThis.process?.env.PORT),
hostname: this.options.hostname,
reusePort: this.options.reusePort,
...this.options.bun,
Expand All @@ -48,10 +49,3 @@ class BunServer extends Server {
this.bunServer.stop(closeAll);
}
}

function resolvePort(port: string | number | undefined): number {
return (
Number.parseInt((port as string) ?? globalThis.process?.env?.PORT, 10) ??
3000
);
}
19 changes: 5 additions & 14 deletions src/deno.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { ServerOptions } from "./types";
import type DenoTypes from "@deno/types";
import { Server } from "./server.ts";
import { resolvePort } from "./_common.ts";

export function serve(options: ServerOptions): Server {
return new DenoServer(options);
Expand Down Expand Up @@ -37,7 +38,10 @@ class DenoServer extends Server {

this.denoServer = Deno.serve(
{
port: resolvePort(options.port),
port: resolvePort(
options.port,
(globalThis as any).Deno?.env.get("PORT"),
),
hostname: this.options.hostname,
reusePort: this.options.reusePort,
...this.options.deno,
Expand Down Expand Up @@ -65,16 +69,3 @@ class DenoServer extends Server {
this.denoServer.shutdown();
}
}

function resolvePort(port: string | number | undefined): number {
return (
Number.parseInt(
(port as string) ??
((globalThis as any).Deno as typeof DenoTypes.Deno)?.env?.get?.(
"PORT",
) ??
globalThis.process?.env?.PORT,
10,
) ?? 3000
);
}
10 changes: 2 additions & 8 deletions src/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { ServerOptions, xRequest } from "./types";
import NodeHttp from "node:http";
import { Server } from "./server";
import { sendNodeResponse, NodeRequestProxy } from "./node-utils/index.ts";
import { resolvePort } from "./_common.ts";

export function serve(options: ServerOptions): Server {
return new NodeServer(options);
Expand Down Expand Up @@ -34,7 +35,7 @@ class NodeServer extends Server {
this._listening = new Promise<void>((resolve) => {
nodeServer.listen(
{
port: resolvePort(options.port),
port: resolvePort(options.port, globalThis.process?.env.PORT),
host: options.hostname,
exclusive: !options.reusePort,
...options.node,
Expand Down Expand Up @@ -78,10 +79,3 @@ class NodeServer extends Server {
});
}
}

function resolvePort(port: string | number | undefined): number {
return (
Number.parseInt((port as string) ?? globalThis.process?.env?.PORT, 10) ??
3000
);
}
13 changes: 10 additions & 3 deletions test/_fixture.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
import { serve } from "srvx";
// prettier-ignore
const runtime = (globalThis as any).Deno ? "deno" : (globalThis.Bun ? "bun" : "node");
const { serve } = (await import(
`../src/${runtime}.ts`
)) as typeof import("../src/types.ts");

export const server = serve({
port: globalThis.process?.env.PORT || globalThis.Deno?.env.get("PORT") || 0,
port:
globalThis.process?.env.PORT ||
(globalThis as any).Deno?.env.get("PORT") ||
0,
hostname: "localhost",
xRemoteAddress: true,
fetch(_request) {
fetch(_req) {
return new Response("ok");
},
});
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"esModuleInterop": true,
"strict": true,
"skipLibCheck": true,
"noEmit": true,
"allowImportingTsExtensions": true
},
"include": ["src"]
"include": ["src", "test", "playground"]
}

0 comments on commit 592b97c

Please sign in to comment.