From c5b1819ef97222d801d88133496663d5b39406a7 Mon Sep 17 00:00:00 2001 From: Kitson Kelly Date: Thu, 6 Jun 2024 11:24:19 +1000 Subject: [PATCH] feat: add ErrorEvent polyfill for Node.js --- deno.json | 2 +- polyfills.ts | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++ router.ts | 2 ++ 3 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 polyfills.ts diff --git a/deno.json b/deno.json index c89599b..2f65243 100644 --- a/deno.json +++ b/deno.json @@ -1,6 +1,6 @@ { "name": "@oak/acorn", - "version": "0.6.0-alpha.1", + "version": "0.6.0-alpha.2", "exports": { ".": "./mod.ts", "./context": "./context.ts", diff --git a/polyfills.ts b/polyfills.ts new file mode 100644 index 0000000..34c017c --- /dev/null +++ b/polyfills.ts @@ -0,0 +1,54 @@ +// Copyright 2022-2024 the oak authors. All rights reserved. + +/** + * Node.js does not have the web standard {@linkcode ErrorEvent} and acorn + * extends it for router error events, so we need to polyfill it. + * + * @module + */ + +if (!("ErrorEvent" in globalThis)) { + class ErrorEvent extends Event { + #message: string; + #filename: string; + #lineno: number; + #colno: number; + // deno-lint-ignore no-explicit-any + #error: any; + + get message(): string { + return this.#message; + } + get filename(): string { + return this.#filename; + } + get lineno(): number { + return this.#lineno; + } + get colno(): number { + return this.#colno; + } + // deno-lint-ignore no-explicit-any + get error(): any { + return this.#error; + } + + constructor(type: string, eventInitDict: ErrorEventInit = {}) { + super(type, eventInitDict); + const { message = "error", filename = "", lineno = 0, colno = 0, error } = + eventInitDict; + this.#message = message; + this.#filename = filename; + this.#lineno = lineno; + this.#colno = colno; + this.#error = error; + } + } + + Object.defineProperty(globalThis, "ErrorEvent", { + value: ErrorEvent, + writable: true, + enumerable: false, + configurable: true, + }); +} diff --git a/router.ts b/router.ts index 91fd66c..ec82867 100644 --- a/router.ts +++ b/router.ts @@ -60,6 +60,8 @@ import { responseFromHttpError, } from "./util.ts"; +import "./polyfills.ts"; + if (!("URLPattern" in globalThis)) { await import("npm:urlpattern-polyfill@10.0.0"); }