Skip to content

Commit

Permalink
fix: tag server constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
kitsonk committed Feb 13, 2024
1 parent 174ba2c commit 120a024
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 6 deletions.
25 changes: 24 additions & 1 deletion application.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import type {
ServerRequest,
ServeTlsOptions,
} from "./types.ts";
import { isNode } from "./util.ts";
import { createPromiseWithResolvers, isNode } from "./util.ts";

let optionsStack: Array<ListenOptions | ListenOptionsTls> = [];
let serverClosed = false;
Expand Down Expand Up @@ -1122,3 +1122,26 @@ Deno.test({
teardown();
},
});

Deno.test({
name: "Application load correct default server",
ignore: isNode(), // this just hangs on node, because we can't close down
sanitizeOps: false,
sanitizeResources: false,
fn() {
const app = new Application();
const { promise, resolve, reject } = createPromiseWithResolvers<void>();
app.addEventListener("listen", ({ serverType }) => {
try {
assertEquals(serverType, isNode() ? "node" : "native");
} catch (e) {
reject(e);
}
resolve();
});
app.use((_ctx) => {});
app.listen();
teardown();
return promise;
},
});
8 changes: 3 additions & 5 deletions application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ interface ApplicationListenEventInit extends EventInit {
listener: Listener;
port: number;
secure: boolean;
serverType: "native" | "custom";
serverType: "native" | "node" | "custom";
}

type ApplicationListenEventListenerOrEventListenerObject =
Expand Down Expand Up @@ -282,7 +282,7 @@ export class ApplicationListenEvent extends Event {
listener: Listener;
port: number;
secure: boolean;
serverType: "native" | "custom";
serverType: "native" | "node" | "custom";

constructor(eventInitDict: ApplicationListenEventInit) {
super("listen", eventInitDict);
Expand Down Expand Up @@ -653,9 +653,7 @@ export class Application<AS extends State = Record<string, any>>
}, { once: true });
}
const { secure = false } = options;
const serverType = DefaultServerCtor && server instanceof DefaultServerCtor
? "native"
: "custom";
const serverType = this.#serverConstructor.type ?? "custom";
const listener = await server.listen();
const { hostname, port } = listener.addr;
this.dispatchEvent(
Expand Down
2 changes: 2 additions & 0 deletions http_server_native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,6 @@ export class Server<AS extends State = Record<string, any>>
}
return this.#stream[Symbol.asyncIterator]();
}

static type: "native" = "native";
}
2 changes: 2 additions & 0 deletions http_server_node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,4 +187,6 @@ export class Server implements OakServer<NodeRequest> {
[Symbol.asyncIterator](): AsyncIterableIterator<NodeRequest> {
return this.#requestStream[Symbol.asyncIterator]();
}

static type: "node" = "node";
}
1 change: 1 addition & 0 deletions types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export interface ServerConstructor<T extends ServerRequest> {
options: Omit<ServeOptions | ServeTlsOptions, "signal">,
): OakServer<T>;
prototype: OakServer<T>;
type?: "native" | "node";
}

export type Data = string | number[] | ArrayBuffer | Uint8Array;
Expand Down

0 comments on commit 120a024

Please sign in to comment.