Skip to content

Commit

Permalink
Update Deno std used by dev server (#102)
Browse files Browse the repository at this point in the history
  • Loading branch information
kidonng authored Feb 18, 2023
1 parent 6b02e11 commit 7965b2c
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions src/runtime/dev-server.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Response, serve } from 'https://deno.land/std@0.106.0/http/server.ts';
import { writeAllSync } from 'https://deno.land/std@0.130.0/streams/conversion.ts';

function isNetAddr(v: any): v is Deno.NetAddr {
return v && typeof v.port === 'number';
Expand All @@ -15,16 +15,16 @@ if (typeof handler !== 'function') {
}

// Spawn HTTP server on ephemeral port
const s = serve({ hostname: '127.0.0.1', port: 0 });
const conn = await Deno.listen({ port: 0 });

if (isNetAddr(s.listener.addr)) {
const { port } = s.listener.addr;
if (isNetAddr(conn.addr)) {
const { port } = conn.addr;
const portBytes = new TextEncoder().encode(String(port));

try {
// Write the port number to FD 3
const portFd = Deno.openSync('/dev/fd/3', { read: false, write: true });
Deno.writeAllSync(portFd, portBytes);
writeAllSync(portFd, portBytes);
Deno.close(portFd.rid);
} catch (err) {
// This fallback is necessary for Windows
Expand All @@ -38,11 +38,12 @@ if (isNetAddr(s.listener.addr)) {
}
}

const s = Deno.serveHttp(await conn.accept());
// Serve HTTP requests to handler function
for await (const req of s) {
Promise.resolve(handler(req)).then((res: Response | void) => {
if (res) {
return req.respond(res);
return req.respondWith(res);
}
});
}

1 comment on commit 7965b2c

@vercel
Copy link

@vercel vercel bot commented on 7965b2c Feb 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.