fix(zone.js): don't rely on global node typings outside of node/ directory (#31783)

PR Close #31783
This commit is contained in:
Alex Eagle 2019-07-22 10:59:42 -07:00 committed by Andrew Kushnir
parent 3479fddf68
commit 5c9a8961da
3 changed files with 23 additions and 14 deletions

View File

@ -331,8 +331,9 @@ Zone.__load_patch('Error', (global: any, Zone: ZoneType, api: _ZonePrivate) => {
// we need to detect all zone related frames, it will // we need to detect all zone related frames, it will
// exceed default stackTraceLimit, so we set it to // exceed default stackTraceLimit, so we set it to
// larger number here, and restore it after detect finish. // larger number here, and restore it after detect finish.
const originalStackTraceLimit = Error.stackTraceLimit; // We cast through any so we don't need to depend on nodejs typings.
Error.stackTraceLimit = 100; const originalStackTraceLimit = (Error as any).stackTraceLimit;
(Error as any).stackTraceLimit = 100;
// we schedule event/micro/macro task, and invoke them // we schedule event/micro/macro task, and invoke them
// when onSchedule, so we can get all stack traces for // when onSchedule, so we can get all stack traces for
// all kinds of tasks with one error thrown. // all kinds of tasks with one error thrown.
@ -374,5 +375,5 @@ Zone.__load_patch('Error', (global: any, Zone: ZoneType, api: _ZonePrivate) => {
}); });
}); });
Error.stackTraceLimit = originalStackTraceLimit; (Error as any).stackTraceLimit = originalStackTraceLimit;
}); });

View File

@ -78,6 +78,16 @@ function renderLongStackTrace(frames: LongStackTrace[], stack?: string): string
return longTrace.join(NEWLINE); return longTrace.join(NEWLINE);
} }
// if Error.stackTraceLimit is 0, means stack trace
// is disabled, so we don't need to generate long stack trace
// this will improve performance in some test(some test will
// set stackTraceLimit to 0, https://github.com/angular/zone.js/issues/698
function stackTracesEnabled(): boolean {
// Cast through any since this property only exists on Error in the nodejs
// typings.
return (Error as any).stackTraceLimit > 0;
}
type LongStackTraceZoneSpec = ZoneSpec & {longStackTraceLimit: number}; type LongStackTraceZoneSpec = ZoneSpec & {longStackTraceLimit: number};
(Zone as any)['longStackTraceZoneSpec'] = <LongStackTraceZoneSpec>{ (Zone as any)['longStackTraceZoneSpec'] = <LongStackTraceZoneSpec>{
@ -99,11 +109,7 @@ type LongStackTraceZoneSpec = ZoneSpec & {longStackTraceLimit: number};
onScheduleTask: function( onScheduleTask: function(
parentZoneDelegate: ZoneDelegate, currentZone: Zone, targetZone: Zone, task: Task): any { parentZoneDelegate: ZoneDelegate, currentZone: Zone, targetZone: Zone, task: Task): any {
if (Error.stackTraceLimit > 0) { if (stackTracesEnabled()) {
// if Error.stackTraceLimit is 0, means stack trace
// is disabled, so we don't need to generate long stack trace
// this will improve performance in some test(some test will
// set stackTraceLimit to 0, https://github.com/angular/zone.js/issues/698
const currentTask = Zone.currentTask; const currentTask = Zone.currentTask;
let trace = currentTask && currentTask.data && (currentTask.data as any)[creationTrace] || []; let trace = currentTask && currentTask.data && (currentTask.data as any)[creationTrace] || [];
trace = [new LongStackTrace()].concat(trace); trace = [new LongStackTrace()].concat(trace);
@ -127,11 +133,7 @@ type LongStackTraceZoneSpec = ZoneSpec & {longStackTraceLimit: number};
onHandleError: function( onHandleError: function(
parentZoneDelegate: ZoneDelegate, currentZone: Zone, targetZone: Zone, error: any): boolean { parentZoneDelegate: ZoneDelegate, currentZone: Zone, targetZone: Zone, error: any): boolean {
if (Error.stackTraceLimit > 0) { if (stackTracesEnabled()) {
// if Error.stackTraceLimit is 0, means stack trace
// is disabled, so we don't need to generate long stack trace
// this will improve performance in some test(some test will
// set stackTraceLimit to 0, https://github.com/angular/zone.js/issues/698
const parentTask = Zone.currentTask || error.task; const parentTask = Zone.currentTask || error.task;
if (error instanceof Error && parentTask) { if (error instanceof Error && parentTask) {
const longStack = const longStack =
@ -154,7 +156,7 @@ function captureStackTraces(stackTraces: string[][], count: number): void {
} }
function computeIgnoreFrames() { function computeIgnoreFrames() {
if (Error.stackTraceLimit <= 0) { if (!stackTracesEnabled()) {
return; return;
} }
const frames: string[][] = []; const frames: string[][] = [];

View File

@ -675,6 +675,12 @@ type AmbientZone = Zone;
/** @internal */ /** @internal */
type AmbientZoneDelegate = ZoneDelegate; type AmbientZoneDelegate = ZoneDelegate;
// CommonJS / Node have global context exposed as "global" variable.
// This code should run in a Browser, so we don't want to include the whole node.d.ts
// typings for this compilation unit.
// We'll just fake the global "global" var for now.
declare var global: NodeJS.Global;
const Zone: ZoneType = (function(global: any) { const Zone: ZoneType = (function(global: any) {
const performance: {mark(name: string): void; measure(name: string, label: string): void;} = const performance: {mark(name: string): void; measure(name: string, label: string): void;} =
global['performance']; global['performance'];