Skip to content

Commit

Permalink
feat: add ErrorEvent polyfill for Node.js
Browse files Browse the repository at this point in the history
  • Loading branch information
kitsonk committed Jun 6, 2024
1 parent 3088469 commit c5b1819
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 1 deletion.
2 changes: 1 addition & 1 deletion deno.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
54 changes: 54 additions & 0 deletions polyfills.ts
Original file line number Diff line number Diff line change
@@ -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,
});
}
2 changes: 2 additions & 0 deletions router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ import {
responseFromHttpError,
} from "./util.ts";

import "./polyfills.ts";

if (!("URLPattern" in globalThis)) {
await import("npm:urlpattern-polyfill@10.0.0");
}
Expand Down

0 comments on commit c5b1819

Please sign in to comment.