forked from angular/angular
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode.ts
More file actions
159 lines (146 loc) Β· 5.43 KB
/
Copy pathnode.ts
File metadata and controls
159 lines (146 loc) Β· 5.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import './node_util';
import './events';
import './fs';
import {findEventTasks} from '../common/events';
import {patchQueueMicrotask} from '../common/queue-microtask';
import {patchTimer} from '../common/timers';
import {ArraySlice, isMix, patchMacroTask, patchMicroTask} from '../common/utils';
const set = 'set';
const clear = 'clear';
Zone.__load_patch('node_timers', (global: any, Zone: ZoneType) => {
// Timers
let globalUseTimeoutFromTimer = false;
try {
const timers = require('timers');
let globalEqualTimersTimeout = global.setTimeout === timers.setTimeout;
if (!globalEqualTimersTimeout && !isMix) {
// 1. if isMix, then we are in mix environment such as Electron
// we should only patch timers.setTimeout because global.setTimeout
// have been patched
// 2. if global.setTimeout not equal timers.setTimeout, check
// whether global.setTimeout use timers.setTimeout or not
const originSetTimeout = timers.setTimeout;
timers.setTimeout = function() {
globalUseTimeoutFromTimer = true;
return originSetTimeout.apply(this, arguments);
};
const detectTimeout = global.setTimeout(() => {}, 100);
clearTimeout(detectTimeout);
timers.setTimeout = originSetTimeout;
}
patchTimer(timers, set, clear, 'Timeout');
patchTimer(timers, set, clear, 'Interval');
patchTimer(timers, set, clear, 'Immediate');
} catch (error) {
// timers module not exists, for example, when we using nativeScript
// timers is not available
}
if (isMix) {
// if we are in mix environment, such as Electron,
// the global.setTimeout has already been patched,
// so we just patch timers.setTimeout
return;
}
if (!globalUseTimeoutFromTimer) {
// 1. global setTimeout equals timers setTimeout
// 2. or global don't use timers setTimeout(maybe some other library patch setTimeout)
// 3. or load timers module error happens, we should patch global setTimeout
patchTimer(global, set, clear, 'Timeout');
patchTimer(global, set, clear, 'Interval');
patchTimer(global, set, clear, 'Immediate');
} else {
// global use timers setTimeout, but not equals
// this happens when use nodejs v0.10.x, global setTimeout will
// use a lazy load version of timers setTimeout
// we should not double patch timer's setTimeout
// so we only store the __symbol__ for consistency
global[Zone.__symbol__('setTimeout')] = global.setTimeout;
global[Zone.__symbol__('setInterval')] = global.setInterval;
global[Zone.__symbol__('setImmediate')] = global.setImmediate;
}
});
// patch process related methods
Zone.__load_patch('nextTick', () => {
// patch nextTick as microTask
patchMicroTask(process, 'nextTick', (self: any, args: any[]) => {
return {
name: 'process.nextTick',
args: args,
cbIdx: (args.length > 0 && typeof args[0] === 'function') ? 0 : -1,
target: process
};
});
});
Zone.__load_patch(
'handleUnhandledPromiseRejection', (global: any, Zone: ZoneType, api: _ZonePrivate) => {
(Zone as any)[api.symbol('unhandledPromiseRejectionHandler')] =
findProcessPromiseRejectionHandler('unhandledRejection');
(Zone as any)[api.symbol('rejectionHandledHandler')] =
findProcessPromiseRejectionHandler('rejectionHandled');
// handle unhandled promise rejection
function findProcessPromiseRejectionHandler(evtName: string) {
return function(e: any) {
const eventTasks = findEventTasks(process, evtName);
eventTasks.forEach(eventTask => {
// process has added unhandledrejection event listener
// trigger the event listener
if (evtName === 'unhandledRejection') {
eventTask.invoke(e.rejection, e.promise);
} else if (evtName === 'rejectionHandled') {
eventTask.invoke(e.promise);
}
});
};
}
});
// Crypto
Zone.__load_patch('crypto', () => {
let crypto: any;
try {
crypto = require('crypto');
} catch (err) {
}
// use the generic patchMacroTask to patch crypto
if (crypto) {
const methodNames = ['randomBytes', 'pbkdf2'];
methodNames.forEach(name => {
patchMacroTask(crypto, name, (self: any, args: any[]) => {
return {
name: 'crypto.' + name,
args: args,
cbIdx: (args.length > 0 && typeof args[args.length - 1] === 'function') ?
args.length - 1 :
-1,
target: crypto
};
});
});
}
});
Zone.__load_patch('console', (global: any, Zone: ZoneType) => {
const consoleMethods =
['dir', 'log', 'info', 'error', 'warn', 'assert', 'debug', 'timeEnd', 'trace'];
consoleMethods.forEach((m: string) => {
const originalMethod = (console as any)[Zone.__symbol__(m)] = (console as any)[m];
if (originalMethod) {
(console as any)[m] = function() {
const args = ArraySlice.call(arguments);
if (Zone.current === Zone.root) {
return originalMethod.apply(this, args);
} else {
return Zone.root.run(originalMethod, this, args);
}
};
}
});
});
Zone.__load_patch('queueMicrotask', (global: any, Zone: ZoneType, api: _ZonePrivate) => {
patchQueueMicrotask(global, api);
});