Skip to content

Commit 5c9a896

Browse files
alexeagleAndrewKushnir
authored andcommitted
fix(zone.js): don't rely on global node typings outside of node/ directory (#31783)
PR Close #31783
1 parent 3479fdd commit 5c9a896

3 files changed

Lines changed: 23 additions & 14 deletions

File tree

packages/zone.js/lib/common/error-rewrite.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -331,8 +331,9 @@ Zone.__load_patch('Error', (global: any, Zone: ZoneType, api: _ZonePrivate) => {
331331
// we need to detect all zone related frames, it will
332332
// exceed default stackTraceLimit, so we set it to
333333
// larger number here, and restore it after detect finish.
334-
const originalStackTraceLimit = Error.stackTraceLimit;
335-
Error.stackTraceLimit = 100;
334+
// We cast through any so we don't need to depend on nodejs typings.
335+
const originalStackTraceLimit = (Error as any).stackTraceLimit;
336+
(Error as any).stackTraceLimit = 100;
336337
// we schedule event/micro/macro task, and invoke them
337338
// when onSchedule, so we can get all stack traces for
338339
// all kinds of tasks with one error thrown.
@@ -374,5 +375,5 @@ Zone.__load_patch('Error', (global: any, Zone: ZoneType, api: _ZonePrivate) => {
374375
});
375376
});
376377

377-
Error.stackTraceLimit = originalStackTraceLimit;
378+
(Error as any).stackTraceLimit = originalStackTraceLimit;
378379
});

packages/zone.js/lib/zone-spec/long-stack-trace.ts

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,16 @@ function renderLongStackTrace(frames: LongStackTrace[], stack?: string): string
7878
return longTrace.join(NEWLINE);
7979
}
8080

81+
// if Error.stackTraceLimit is 0, means stack trace
82+
// is disabled, so we don't need to generate long stack trace
83+
// this will improve performance in some test(some test will
84+
// set stackTraceLimit to 0, https://github.com/angular/zone.js/issues/698
85+
function stackTracesEnabled(): boolean {
86+
// Cast through any since this property only exists on Error in the nodejs
87+
// typings.
88+
return (Error as any).stackTraceLimit > 0;
89+
}
90+
8191
type LongStackTraceZoneSpec = ZoneSpec & {longStackTraceLimit: number};
8292

8393
(Zone as any)['longStackTraceZoneSpec'] = <LongStackTraceZoneSpec>{
@@ -99,11 +109,7 @@ type LongStackTraceZoneSpec = ZoneSpec & {longStackTraceLimit: number};
99109

100110
onScheduleTask: function(
101111
parentZoneDelegate: ZoneDelegate, currentZone: Zone, targetZone: Zone, task: Task): any {
102-
if (Error.stackTraceLimit > 0) {
103-
// if Error.stackTraceLimit is 0, means stack trace
104-
// is disabled, so we don't need to generate long stack trace
105-
// this will improve performance in some test(some test will
106-
// set stackTraceLimit to 0, https://github.com/angular/zone.js/issues/698
112+
if (stackTracesEnabled()) {
107113
const currentTask = Zone.currentTask;
108114
let trace = currentTask && currentTask.data && (currentTask.data as any)[creationTrace] || [];
109115
trace = [new LongStackTrace()].concat(trace);
@@ -127,11 +133,7 @@ type LongStackTraceZoneSpec = ZoneSpec & {longStackTraceLimit: number};
127133

128134
onHandleError: function(
129135
parentZoneDelegate: ZoneDelegate, currentZone: Zone, targetZone: Zone, error: any): boolean {
130-
if (Error.stackTraceLimit > 0) {
131-
// if Error.stackTraceLimit is 0, means stack trace
132-
// is disabled, so we don't need to generate long stack trace
133-
// this will improve performance in some test(some test will
134-
// set stackTraceLimit to 0, https://github.com/angular/zone.js/issues/698
136+
if (stackTracesEnabled()) {
135137
const parentTask = Zone.currentTask || error.task;
136138
if (error instanceof Error && parentTask) {
137139
const longStack =
@@ -154,7 +156,7 @@ function captureStackTraces(stackTraces: string[][], count: number): void {
154156
}
155157

156158
function computeIgnoreFrames() {
157-
if (Error.stackTraceLimit <= 0) {
159+
if (!stackTracesEnabled()) {
158160
return;
159161
}
160162
const frames: string[][] = [];

packages/zone.js/lib/zone.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -675,6 +675,12 @@ type AmbientZone = Zone;
675675
/** @internal */
676676
type AmbientZoneDelegate = ZoneDelegate;
677677

678+
// CommonJS / Node have global context exposed as "global" variable.
679+
// This code should run in a Browser, so we don't want to include the whole node.d.ts
680+
// typings for this compilation unit.
681+
// We'll just fake the global "global" var for now.
682+
declare var global: NodeJS.Global;
683+
678684
const Zone: ZoneType = (function(global: any) {
679685
const performance: {mark(name: string): void; measure(name: string, label: string): void;} =
680686
global['performance'];

0 commit comments

Comments
 (0)