Skip to content
New issue

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

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

Already on GitHub? # to your account

fix: listen() no longer accept string #92

Merged
merged 2 commits into from
Apr 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions app.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,4 @@ app.get("/logout", async (req) => {
req.clearCookie("sid");
return req.redirect("/#");
});
app.listen(":8899");
app.listen({ port: 8899 });
2 changes: 1 addition & 1 deletion site/public/example/reading_body.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 });