Skip to content

Commit

Permalink
fix: listen() no longer accept string (#92)
Browse files Browse the repository at this point in the history
  • Loading branch information
keroxp committed Apr 2, 2020
1 parent 74cbd7e commit 0391ed3
Show file tree
Hide file tree
Showing 17 changed files with 25 additions and 36 deletions.
13 changes: 7 additions & 6 deletions app.ts
Expand Up @@ -7,8 +7,6 @@ import {
ServerRequest
} from "./server.ts";
import { createLogger, Logger, Loglevel, namedLogger } from "./logger.ts";
import ListenOptions = Deno.ListenOptions;
import ListenTLSOptions = Deno.ListenTLSOptions;
import {
createRouter,
Router
Expand All @@ -18,10 +16,13 @@ import { kHttpStatusMessages } from "./serveio.ts";

export interface App extends Router {
/** Start listening with given addr */
listen(addr: string | ListenOptions, opts?: ServeOptions): ServeListener;
listen(addr: Deno.ListenOptions, opts?: ServeOptions): ServeListener;

/** Start listening for HTTPS server */
listenTLS(tlsOptions: ListenTLSOptions, opts?: ServeOptions): ServeListener;
listenTLS(
tlsOptions: Deno.ListenTLSOptions,
opts?: ServeOptions,
): ServeListener;
}

export type AppOptions = {
Expand Down Expand Up @@ -76,15 +77,15 @@ export function createApp(
}
};
function listen(
addr: string | ListenOptions,
addr: Deno.ListenOptions,
opts?: ServeOptions,
): ServeListener {
const listener = listenAndServe(addr, (req) => handleRoute("", req), opts);
info(`listening on ${addr}`);
return listener;
}
function listenTLS(
listenOptions: ListenTLSOptions,
listenOptions: Deno.ListenTLSOptions,
opts?: ServeOptions,
): ServeListener {
const listener = listenAndServeTLS(
Expand Down
2 changes: 1 addition & 1 deletion cookie_test.ts
Expand Up @@ -97,7 +97,7 @@ it("cookie integration", (t) => {
const deno = req.cookies.get("deno");
return req.respond({ status: 200, body: deno || "" });
});
const lis = router.listen(":9983");
const lis = router.listen({ port: 9983 });
return () => lis.close();
});
t.run("basic", async () => {
Expand Down
2 changes: 1 addition & 1 deletion serve_static_test.ts
Expand Up @@ -38,7 +38,7 @@ it("serveStatic integration", (t) => {
t.beforeAfterAll(() => {
const router = createApp();
router.use(serveStatic("./fixtures/public"));
const l = router.listen(":9988");
const l = router.listen({ port: 9988 });
return () => l.close();
});
t.run("basic", async () => {
Expand Down
18 changes: 3 additions & 15 deletions server.ts
Expand Up @@ -118,20 +118,8 @@ export type ServeListener = Deno.Closer;
export type ServeHandler = (req: ServerRequest) => void | Promise<void>;

export type HostPort = { hostname?: string; port: number };
function createListener(listenOptions: string | HostPort): Listener {
if (typeof listenOptions === "string") {
const [h, p] = listenOptions.split(":");
if (!p) {
throw new Error("server: port must be specified");
}
const opts: HostPort = { port: parseInt(p) };
if (h) {
opts.hostname = h;
}
return Deno.listen({ ...opts, transport: "tcp" });
} else {
return Deno.listen({ ...listenOptions, transport: "tcp" });
}
function createListener(opts: HostPort): Listener {
return Deno.listen({ ...opts, transport: "tcp" });
}

export function listenAndServeTLS(
Expand All @@ -144,7 +132,7 @@ export function listenAndServeTLS(
}

export function listenAndServe(
listenOptions: string | ListenOptions,
listenOptions: ListenOptions,
handler: ServeHandler,
opts: ServeOptions = {},
): ServeListener {
Expand Down
2 changes: 1 addition & 1 deletion site/public/example/app_server.ts
Expand Up @@ -26,4 +26,4 @@ app.get(new RegExp("^/foo/(.+)"), async (req, { match }) => {
});
});
// Start listening on port 8899
app.listen(":8899");
app.listen({ port: 8899 });
2 changes: 1 addition & 1 deletion site/public/example/basic_auth.ts
Expand Up @@ -18,4 +18,4 @@ app.get("/", async (req) => {
body: "Hello, Servest!",
});
});
app.listen(":8899");
app.listen({ port: 8899 });
2 changes: 1 addition & 1 deletion site/public/example/get_started.ts
Expand Up @@ -10,4 +10,4 @@ app.handle("/", async (req) => {
body: "Hello, Servest!",
});
});
app.listen(":8899");
app.listen({ port: 8899 });
2 changes: 1 addition & 1 deletion site/public/example/handle_errors.ts
Expand Up @@ -33,4 +33,4 @@ app.catch(async (e, req) => {
});
}
});
app.listen(":8899");
app.listen({ port: 8899 });
2 changes: 1 addition & 1 deletion site/public/example/handle_ws.ts
Expand Up @@ -13,4 +13,4 @@ function handleHandshake(sock: WebSocket) {
}
const app = createApp();
app.ws("/ws", handleHandshake);
app.listen(":8899");
app.listen({ port: 8899 });
2 changes: 1 addition & 1 deletion site/public/example/manage_session.ts
Expand Up @@ -102,4 +102,4 @@ app.get("/logout", async (req) => {
req.clearCookie("sid");
return req.redirect("/login");
});
app.listen(":8899");
app.listen({ port: 8899 });
2 changes: 1 addition & 1 deletion site/public/example/reading_body.ts
Expand Up @@ -40,4 +40,4 @@ app.post("/raw", async (req) => {
// ...respond
});
// Start listening on port 8899
app.listen(":8899");
app.listen({ port: 8899 });
2 changes: 1 addition & 1 deletion site/public/example/simple_server.ts
@@ -1,6 +1,6 @@
// Copyright 2019 Yusuke Sakurai. All rights reserved. MIT license.
import { listenAndServe } from "../../../server.ts";
const listener = listenAndServe(":8899", async (req) => {
const listener = listenAndServe({ port: 8899 }, async (req) => {
await req.respond({
status: 200,
headers: new Headers({
Expand Down
2 changes: 1 addition & 1 deletion site/public/example/use_jsx.tsx
Expand Up @@ -22,4 +22,4 @@ app.handle("/", async (req) => {
),
});
});
app.listen(":8899");
app.listen({ port: 8899 });
2 changes: 1 addition & 1 deletion site/public/example/use_middleware.ts
Expand Up @@ -11,4 +11,4 @@ app.use(async (req) => {
}
// Go through the next middleware
});
app.listen(":8899");
app.listen({ port: 8899 });
2 changes: 1 addition & 1 deletion site/public/example/use_router.ts
Expand Up @@ -40,4 +40,4 @@ function UserRoutes() {

app.route("/", IndexRoutes());
app.route("/users", UserRoutes());
app.listen(":8899");
app.listen({ port: 8899 });
2 changes: 1 addition & 1 deletion site/public/example/use_serve_jsx.ts
Expand Up @@ -5,4 +5,4 @@ const app = createApp();
// .jsx/.tsx files in ./pages directory will be dynamically imported
// and rendered component served as html
app.use(serveJsx("./pages", (f) => import(f)));
app.listen(":8899");
app.listen({ port: 8899 });
2 changes: 1 addition & 1 deletion site/public/example/use_serve_static.ts
Expand Up @@ -6,4 +6,4 @@ const app = createApp();
// are served automatically
// Otherwise, request will be passed to next handler
app.use(serveStatic("./public"));
app.listen(":8899");
app.listen({ port: 8899 });

0 comments on commit 0391ed3

Please sign in to comment.