Skip to content
File

Blob: src/node/domain.ts

typescript93 lines
1// Copyright (c) 2017-2022 Cloudflare, Inc.
2// Licensed under the Apache 2.0 license found in the LICENSE file or at:
3// https://opensource.org/licenses/Apache-2.0
4// Copyright Joyent, Inc. and other Node contributors.
5//
6// Permission is hereby granted, free of charge, to any person obtaining a
7// copy of this software and associated documentation files (the
8// "Software"), to deal in the Software without restriction, including
9// without limitation the rights to use, copy, modify, merge, publish,
10// distribute, sublicense, and/or sell copies of the Software, and to permit
11// persons to whom the Software is furnished to do so, subject to the
12// following conditions:
13//
14// The above copyright notice and this permission notice shall be included
15// in all copies or substantial portions of the Software.
16//
17// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
20// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
21// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
22// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
23// USE OR OTHER DEALINGS IN THE SOFTWARE.
24 
25// Of all the deprecated Node.js modules that we need to have available
26// but don't want to actually implement, domain is the probably the
27// most least likely to ever be implemented.
28 
29import { EventEmitter } from 'node-internal:events';
30EventEmitter.usingDomains = false;
31 
32export class Domain extends EventEmitter {
33 members: unknown[] | undefined = undefined;
34 
35 _errorHandler(_er: unknown): void {
36 // Should never be called since we override everything that would call it.
37 // But if it is, just throw the error that is passed in.
38 throw _er;
39 }
40 
41 enter(): void {
42 // Instead of throwing, we just don't do anything here
43 }
44 
45 exit(): void {
46 // Instead of throwing, we just don't do anything here.
47 }
48 
49 add(_ee: unknown): void {
50 // Instead of throwing, we just don't do anything here.
51 }
52 
53 remove(_ee: unknown): void {
54 // Instead of throwing, we just don't do anything here.
55 }
56 
57 // eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
58 run(fn: Function, ...args: unknown[]): unknown {
59 // This is non-operational. We end up just calling the function directly.
60 this.enter();
61 const ret: unknown = Reflect.apply(fn, this, args);
62 this.exit();
63 return ret;
64 }
65 
66 // eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
67 intercept(cb: Function): Function {
68 // This is non-operational. We end up just returning the callback directly.
69 return cb;
70 }
71 
72 // eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
73 bind(cb: Function): Function {
74 // This is non-operational. We end up just returning the callback directly.
75 return cb;
76 }
77}
78 
79export function createDomain(): Domain {
80 return new Domain();
81}
82export const create = createDomain();
83 
84// The active domain is always the one that we're currently in.
85export const active: Domain | null = null;
86 
87export default {
88 Domain,
89 active,
90 createDomain,
91 create,
92};