summaryrefslogtreecommitdiff
path: root/ext/node
diff options
context:
space:
mode:
authorBartek IwaƄczuk <biwanczuk@gmail.com>2024-04-03 20:37:10 +0100
committerGitHub <noreply@github.com>2024-04-03 21:37:10 +0200
commit778b0b8eb59c26b2868c5f820e84988b8039597b (patch)
tree675e550e917e350701a181a59461c6c67af1b814 /ext/node
parent86bc7a43810846fc66bf06b7577490f01ead1918 (diff)
fix(ext/node): polyfill node:domain module (#23088)
Closes https://github.com/denoland/deno/issues/16852 --------- Co-authored-by: Nathan Whitaker <nathan@deno.com>
Diffstat (limited to 'ext/node')
-rw-r--r--ext/node/polyfills/domain.ts81
1 files changed, 77 insertions, 4 deletions
diff --git a/ext/node/polyfills/domain.ts b/ext/node/polyfills/domain.ts
index 16fec26a0..f9c99f725 100644
--- a/ext/node/polyfills/domain.ts
+++ b/ext/node/polyfills/domain.ts
@@ -1,14 +1,87 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
// Copyright Joyent and Node contributors. All rights reserved. MIT license.
+// This code has been inspired by https://github.com/bevry/domain-browser/commit/8bce7f4a093966ca850da75b024239ad5d0b33c6
-import { notImplemented } from "ext:deno_node/_utils.ts";
+import { primordials } from "ext:core/mod.js";
+const {
+ ArrayPrototypeSlice,
+ FunctionPrototypeBind,
+ FunctionPrototypeCall,
+ FunctionPrototypeApply,
+} = primordials;
+import { EventEmitter } from "node:events";
+
+function emitError(e) {
+ this.emit("error", e);
+}
export function create() {
- notImplemented("domain.create");
+ return new Domain();
}
-export class Domain {
+export class Domain extends EventEmitter {
+ #handler;
+
constructor() {
- notImplemented("domain.Domain.prototype.constructor");
+ super();
+ this.#handler = FunctionPrototypeBind(emitError, this);
+ }
+
+ add(emitter) {
+ emitter.on("error", this.#handler);
+ }
+
+ remove(emitter) {
+ emitter.off("error", this.#handler);
+ }
+
+ bind(fn) {
+ // deno-lint-ignore no-this-alias
+ const self = this;
+ return function () {
+ try {
+ FunctionPrototypeApply(fn, null, ArrayPrototypeSlice(arguments));
+ } catch (e) {
+ FunctionPrototypeCall(emitError, self, e);
+ }
+ };
+ }
+
+ intercept(fn) {
+ // deno-lint-ignore no-this-alias
+ const self = this;
+ return function (e) {
+ if (e) {
+ FunctionPrototypeCall(emitError, self, e);
+ } else {
+ try {
+ FunctionPrototypeApply(fn, null, ArrayPrototypeSlice(arguments, 1));
+ } catch (e) {
+ FunctionPrototypeCall(emitError, self, e);
+ }
+ }
+ };
+ }
+
+ run(fn) {
+ try {
+ fn();
+ } catch (e) {
+ FunctionPrototypeCall(emitError, this, e);
+ }
+ return this;
+ }
+
+ dispose() {
+ this.removeAllListeners();
+ return this;
+ }
+
+ enter() {
+ return this;
+ }
+
+ exit() {
+ return this;
}
}
export default {