export function server(port = 4221) {
const listener = Deno.listen({ port });
(async () => {
for await (const conn of listener) {
conn.readable.pipeTo(conn.writable);
}
})();
return listener;
}
mod.test.ts
import { server } from "./mod.ts";
import { assertEquals } from "https://deno.land/std@0.204.0/assert/mod.ts";
const E = new TextEncoder();
const D = new TextDecoder();
Deno.test("smoke", async () => {
const listner = server(4221);
const con = await Deno.connect({ port: 4221 });
await con.write(E.encode("hello"));
const ans = new Uint8Array(5);
await con.read(ans);
assertEquals(D.decode(ans), "hello");
listner.close();
con.close();
// give time for the listner loop to end
await new Promise((r) => setTimeout(r, 100));
});
build.ts
// ex. scripts/build_npm.ts
import { build, emptyDir } from "https://deno.land/x/dnt@0.38.1/mod.ts";
await emptyDir("./npm");
await build({
entryPoints: ["./mod.ts"],
outDir: "./npm",
shims: {
// see JS docs for overview and more options
deno: true,
},
package: {
// package.json properties
name: "http-server",
version: Deno.args[0],
description: "Http Server",
license: "MIT",
},
postBuild() {
// steps to run after building and before running the tests
// Deno.copyFileSync("LICENSE", "npm/LICENSE");
// Deno.copyFileSync("README.md", "npm/README.md");
},
});
deno run -A build.ts
error
[dnt] Transforming...
[dnt] Running npm install...
added 7 packages, and audited 8 packages in 6s
found 0 vulnerabilities
[dnt] Building project...
[dnt] Type checking ESM...
[dnt] Emitting ESM package...
[dnt] Emitting script package...
[dnt] Running post build action...
[dnt] Running tests...
> test
> node test_runner.js
Running tests in ./script/mod.test.js...
test smoke ... fail
FAILURES
smoke
TypeError: Cannot read properties of null (reading 'fd')
at Object.listen (/home/mrcool/dev/codecraft/codecrafters-http-server-javascript/deno/npm/node_modules/@deno/shim-deno/dist/deno/stable/functions/listen.js:44:64)
at server (/home/mrcool/dev/codecraft/codecrafters-http-server-javascript/deno/npm/script/mod.js:29:35)
at Object.fn (/home/mrcool/dev/codecraft/codecrafters-http-server-javascript/deno/npm/script/mod.test.js:32:41)
at runTestDefinitions (/home/mrcool/dev/codecraft/codecrafters-http-server-javascript/deno/npm/test_runner.js:54:30)
at main (/home/mrcool/dev/codecraft/codecrafters-http-server-javascript/deno/npm/test_runner.js:31:15)
at Object.<anonymous> (/home/mrcool/dev/codecraft/codecrafters-http-server-javascript/deno/npm/test_runner.js:198:1)
at Module._compile (node:internal/modules/cjs/loader:1256:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1310:10)
at Module.load (node:internal/modules/cjs/loader:1119:32)
at Module._load (node:internal/modules/cjs/loader:960:12)error: Uncaught (in promise) Error: npm run test failed with exit code 1
throw new Error(
^
at runCommand (https://deno.land/x/dnt@0.38.1/lib/utils.ts:56:13)
at eventLoopTick (ext:core/01_core.js:183:11)
at async build (https://deno.land/x/dnt@0.38.1/mod.ts:407:5)
at async file:///home/mrcool/dev/codecraft/codecrafters-http-server-javascript/deno/scripts/build.ts:6:1
mod.ts
mod.test.ts
build.ts
deno run -A build.ts
error