Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Redesign of http server module #188

Merged
merged 12 commits into from
Feb 15, 2019
27 changes: 27 additions & 0 deletions async/deferred.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
Copy link
Member

Choose a reason for hiding this comment

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

I don't know about /async ... seems a bit too specific.

I feel like /util would be a useful junk drawer for one-off things like this.
@hayd what do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I also considered to let it promise FYI. I don't have any special reason to async

Copy link
Member

Choose a reason for hiding this comment

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

Can you rename this to //util/deferred.ts ?


export type Deferred<T = any, R = Error> = {
promise: Promise<T>;
resolve: (t?: T) => void;
reject: (r?: R) => void;
};

/** Create deferred promise that can be resolved and rejected by outside */
export function defer<T>(): Deferred<T> {
let resolve = () => {};
let reject = () => {};
const promise = new Promise<T>((res, rej) => {
resolve = res;
reject = rej;
});
return { promise, resolve, reject };
}

export function isDeferred(x): x is Deferred {
return (
typeof x === "object" &&
x.promise instanceof Promise &&
typeof x["resolve"] === "function" &&
typeof x["reject"] === "function"
);
}
Copy link

Choose a reason for hiding this comment

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

Isn't this testing the same thing as the Deferred type restriction itself? And it's not even used anywhere outside of the test file. 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's custom type guard for Deferred object. Mostly used to resolve it from union type.

16 changes: 16 additions & 0 deletions async/deferred_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.

import { assert, test } from "../testing/mod.ts";
import { defer, isDeferred } from "./deferred.ts";

test(async function asyncIsDeferred() {
const d = defer();
assert.assert(isDeferred(d));
assert.assert(
isDeferred({
promise: null,
resolve: () => {},
reject: () => {}
}) === false
);
});
8 changes: 4 additions & 4 deletions http/file_server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
listenAndServe,
ServerRequest,
setContentLength,
Response
ServerResponse
} from "./server.ts";
import { cwd, DenoError, ErrorKind, args, stat, readDir, open } from "deno";
import { extname } from "../fs/path.ts";
Expand Down Expand Up @@ -195,14 +195,14 @@ async function serveFallback(req: ServerRequest, e: Error) {
}
}

function serverLog(req: ServerRequest, res: Response) {
function serverLog(req: ServerRequest, res: ServerResponse) {
const d = new Date().toISOString();
const dateFmt = `[${d.slice(0, 10)} ${d.slice(11, 19)}]`;
const s = `${dateFmt} "${req.method} ${req.url} ${req.proto}" ${res.status}`;
console.log(s);
}

function setCORS(res: Response) {
function setCORS(res: ServerResponse) {
if (!res.headers) {
res.headers = new Headers();
}
Expand All @@ -217,7 +217,7 @@ listenAndServe(addr, async req => {
const fileName = req.url.replace(/\/$/, "");
const filePath = currentDir + fileName;

let response: Response;
let response: ServerResponse;

try {
const fileInfo = await stat(filePath);
Expand Down
Loading