Skip to content
This repository was archived by the owner on Feb 26, 2024. It is now read-only.

Commit fe07a8f

Browse files
committed
fix(patch): fix #744, add namespace to load patch name
1 parent 1d422f2 commit fe07a8f

File tree

3 files changed

+76
-72
lines changed

3 files changed

+76
-72
lines changed

lib/node/events.ts

Lines changed: 44 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -8,52 +8,54 @@
88

99
import {makeZoneAwareAddListener, makeZoneAwareListeners, makeZoneAwareRemoveAllListeners, makeZoneAwareRemoveListener, patchMethod} from '../common/utils';
1010

11-
const callAndReturnFirstParam = (fn: (self: any, args: any[]) => any) => {
12-
return (self: any, args: any[]) => {
13-
fn(self, args);
14-
return self;
11+
Zone.__load_patch('node_events', (global: any, Zone: ZoneType, api: _ZonePrivate) => {
12+
const callAndReturnFirstParam = (fn: (self: any, args: any[]) => any) => {
13+
return (self: any, args: any[]) => {
14+
fn(self, args);
15+
return self;
16+
};
1517
};
16-
};
1718

18-
// For EventEmitter
19-
const EE_ADD_LISTENER = 'addListener';
20-
const EE_PREPEND_LISTENER = 'prependListener';
21-
const EE_REMOVE_LISTENER = 'removeListener';
22-
const EE_REMOVE_ALL_LISTENER = 'removeAllListeners';
23-
const EE_LISTENERS = 'listeners';
24-
const EE_ON = 'on';
19+
// For EventEmitter
20+
const EE_ADD_LISTENER = 'addListener';
21+
const EE_PREPEND_LISTENER = 'prependListener';
22+
const EE_REMOVE_LISTENER = 'removeListener';
23+
const EE_REMOVE_ALL_LISTENER = 'removeAllListeners';
24+
const EE_LISTENERS = 'listeners';
25+
const EE_ON = 'on';
2526

26-
const zoneAwareAddListener = callAndReturnFirstParam(
27-
makeZoneAwareAddListener(EE_ADD_LISTENER, EE_REMOVE_LISTENER, false, true, false));
28-
const zoneAwarePrependListener = callAndReturnFirstParam(
29-
makeZoneAwareAddListener(EE_PREPEND_LISTENER, EE_REMOVE_LISTENER, false, true, true));
30-
const zoneAwareRemoveListener =
31-
callAndReturnFirstParam(makeZoneAwareRemoveListener(EE_REMOVE_LISTENER, false));
32-
const zoneAwareRemoveAllListeners =
33-
callAndReturnFirstParam(makeZoneAwareRemoveAllListeners(EE_REMOVE_ALL_LISTENER));
34-
const zoneAwareListeners = makeZoneAwareListeners(EE_LISTENERS);
27+
const zoneAwareAddListener = callAndReturnFirstParam(
28+
makeZoneAwareAddListener(EE_ADD_LISTENER, EE_REMOVE_LISTENER, false, true, false));
29+
const zoneAwarePrependListener = callAndReturnFirstParam(
30+
makeZoneAwareAddListener(EE_PREPEND_LISTENER, EE_REMOVE_LISTENER, false, true, true));
31+
const zoneAwareRemoveListener =
32+
callAndReturnFirstParam(makeZoneAwareRemoveListener(EE_REMOVE_LISTENER, false));
33+
const zoneAwareRemoveAllListeners =
34+
callAndReturnFirstParam(makeZoneAwareRemoveAllListeners(EE_REMOVE_ALL_LISTENER));
35+
const zoneAwareListeners = makeZoneAwareListeners(EE_LISTENERS);
3536

36-
export function patchEventEmitterMethods(obj: any): boolean {
37-
if (obj && obj.addListener) {
38-
patchMethod(obj, EE_ADD_LISTENER, () => zoneAwareAddListener);
39-
patchMethod(obj, EE_PREPEND_LISTENER, () => zoneAwarePrependListener);
40-
patchMethod(obj, EE_REMOVE_LISTENER, () => zoneAwareRemoveListener);
41-
patchMethod(obj, EE_REMOVE_ALL_LISTENER, () => zoneAwareRemoveAllListeners);
42-
patchMethod(obj, EE_LISTENERS, () => zoneAwareListeners);
43-
obj[EE_ON] = obj[EE_ADD_LISTENER];
44-
return true;
45-
} else {
46-
return false;
37+
function patchEventEmitterMethods(obj: any): boolean {
38+
if (obj && obj.addListener) {
39+
patchMethod(obj, EE_ADD_LISTENER, () => zoneAwareAddListener);
40+
patchMethod(obj, EE_PREPEND_LISTENER, () => zoneAwarePrependListener);
41+
patchMethod(obj, EE_REMOVE_LISTENER, () => zoneAwareRemoveListener);
42+
patchMethod(obj, EE_REMOVE_ALL_LISTENER, () => zoneAwareRemoveAllListeners);
43+
patchMethod(obj, EE_LISTENERS, () => zoneAwareListeners);
44+
obj[EE_ON] = obj[EE_ADD_LISTENER];
45+
return true;
46+
} else {
47+
return false;
48+
}
4749
}
48-
}
4950

50-
// EventEmitter
51-
let events;
52-
try {
53-
events = require('events');
54-
} catch (err) {
55-
}
51+
// EventEmitter
52+
let events;
53+
try {
54+
events = require('events');
55+
} catch (err) {
56+
}
5657

57-
if (events && events.EventEmitter) {
58-
patchEventEmitterMethods(events.EventEmitter.prototype);
59-
}
58+
if (events && events.EventEmitter) {
59+
patchEventEmitterMethods(events.EventEmitter.prototype);
60+
}
61+
});

lib/node/fs.ts

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,32 +8,34 @@
88

99
import {patchMacroTask} from '../common/utils';
1010

11-
let fs: any;
12-
try {
13-
fs = require('fs');
14-
} catch (err) {
15-
}
11+
Zone.__load_patch('node_fs', (global: any, Zone: ZoneType, api: _ZonePrivate) => {
12+
let fs: any;
13+
try {
14+
fs = require('fs');
15+
} catch (err) {
16+
}
1617

17-
// watch, watchFile, unwatchFile has been patched
18-
// because EventEmitter has been patched
19-
const TO_PATCH_MACROTASK_METHODS = [
20-
'access', 'appendFile', 'chmod', 'chown', 'close', 'exists', 'fchmod',
21-
'fchown', 'fdatasync', 'fstat', 'fsync', 'ftruncate', 'futimes', 'lchmod',
22-
'lchown', 'link', 'lstat', 'mkdir', 'mkdtemp', 'open', 'read',
23-
'readdir', 'readFile', 'readlink', 'realpath', 'rename', 'rmdir', 'stat',
24-
'symlink', 'truncate', 'unlink', 'utimes', 'write', 'writeFile',
25-
];
18+
// watch, watchFile, unwatchFile has been patched
19+
// because EventEmitter has been patched
20+
const TO_PATCH_MACROTASK_METHODS = [
21+
'access', 'appendFile', 'chmod', 'chown', 'close', 'exists', 'fchmod',
22+
'fchown', 'fdatasync', 'fstat', 'fsync', 'ftruncate', 'futimes', 'lchmod',
23+
'lchown', 'link', 'lstat', 'mkdir', 'mkdtemp', 'open', 'read',
24+
'readdir', 'readFile', 'readlink', 'realpath', 'rename', 'rmdir', 'stat',
25+
'symlink', 'truncate', 'unlink', 'utimes', 'write', 'writeFile',
26+
];
2627

27-
if (fs) {
28-
TO_PATCH_MACROTASK_METHODS.filter(name => !!fs[name] && typeof fs[name] === 'function')
29-
.forEach(name => {
30-
patchMacroTask(fs, name, (self: any, args: any[]) => {
31-
return {
32-
name: 'fs.' + name,
33-
args: args,
34-
callbackIndex: args.length > 0 ? args.length - 1 : -1,
35-
target: self
36-
};
28+
if (fs) {
29+
TO_PATCH_MACROTASK_METHODS.filter(name => !!fs[name] && typeof fs[name] === 'function')
30+
.forEach(name => {
31+
patchMacroTask(fs, name, (self: any, args: any[]) => {
32+
return {
33+
name: 'fs.' + name,
34+
args: args,
35+
callbackIndex: args.length > 0 ? args.length - 1 : -1,
36+
target: self
37+
};
38+
});
3739
});
38-
});
39-
}
40+
}
41+
});

lib/node/node.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import {findEventTask, patchMacroTask, patchMicroTask} from '../common/utils';
1818
const set = 'set';
1919
const clear = 'clear';
2020

21-
Zone.__load_patch('timers', (global: any, Zone: ZoneType, api: _ZonePrivate) => {
21+
Zone.__load_patch('node_timers', (global: any, Zone: ZoneType, api: _ZonePrivate) => {
2222
// Timers
2323
let globalUseTimeoutFromTimer = false;
2424
try {
@@ -63,7 +63,7 @@ Zone.__load_patch('timers', (global: any, Zone: ZoneType, api: _ZonePrivate) =>
6363
});
6464

6565
// patch process related methods
66-
Zone.__load_patch('nextTick', (global: any, Zone: ZoneType, api: _ZonePrivate) => {
66+
Zone.__load_patch('node_nextTick', (global: any, Zone: ZoneType, api: _ZonePrivate) => {
6767
// patch nextTick as microTask
6868
patchMicroTask(process, 'nextTick', (self: any, args: any[]) => {
6969
return {
@@ -76,7 +76,7 @@ Zone.__load_patch('nextTick', (global: any, Zone: ZoneType, api: _ZonePrivate) =
7676
});
7777

7878
Zone.__load_patch(
79-
'handleUnhandledPromiseRejection', (global: any, Zone: ZoneType, api: _ZonePrivate) => {
79+
'node_handleUnhandledPromiseRejection', (global: any, Zone: ZoneType, api: _ZonePrivate) => {
8080
(Zone as any)[api.symbol('unhandledPromiseRejectionHandler')] =
8181
findProcessPromiseRejectionHandler('unhandledRejection');
8282

@@ -103,7 +103,7 @@ Zone.__load_patch(
103103

104104

105105
// Crypto
106-
Zone.__load_patch('crypto', (global: any, Zone: ZoneType, api: _ZonePrivate) => {
106+
Zone.__load_patch('node_crypto', (global: any, Zone: ZoneType, api: _ZonePrivate) => {
107107
let crypto: any;
108108
try {
109109
crypto = require('crypto');

0 commit comments

Comments
 (0)