Skip to content

Commit

Permalink
fix: more global namespace fixes for node
Browse files Browse the repository at this point in the history
  • Loading branch information
kitsonk committed Feb 13, 2024
1 parent 7c4808c commit f1b7d46
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions http_server_native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ import type {
} from "./types.ts";
import { createPromiseWithResolvers } from "./util.ts";

const serve: (
options: ServeInit & (ServeOptions | ServeTlsOptions),
) => HttpServer = "serve" in Deno
// deno-lint-ignore no-explicit-any
? (Deno as any).serve.bind(Deno)
: undefined;
const serve:
| ((
options: ServeInit & (ServeOptions | ServeTlsOptions),
) => HttpServer)
| undefined = "Deno" in globalThis && "serve" in globalThis.Deno
? globalThis.Deno.serve.bind(globalThis.Deno)
: undefined;

/** The oak abstraction of the Deno native HTTP server which is used internally
* for handling native HTTP requests. Generally users of oak do not need to
Expand All @@ -35,7 +36,7 @@ export class Server<AS extends State = Record<string, any>>
app: Application<AS>,
options: Omit<ServeOptions | ServeTlsOptions, "signal">,
) {
if (!("serve" in Deno)) {
if (!serve) {
throw new Error(
"The native bindings for serving HTTP are not available.",
);
Expand Down Expand Up @@ -75,7 +76,7 @@ export class Server<AS extends State = Record<string, any>>
const { promise, resolve } = createPromiseWithResolvers<Listener>();
this.#stream = new ReadableStream<NativeRequest>({
start: (controller) => {
this.#httpServer = serve({
this.#httpServer = serve?.({
handler: (req, info) => {
const nativeRequest = new NativeRequest(req, info);
controller.enqueue(nativeRequest);
Expand Down

0 comments on commit f1b7d46

Please sign in to comment.