1 | 'use strict';
|
---|
2 | /**
|
---|
3 | * @license Angular v12.0.0-next.0
|
---|
4 | * (c) 2010-2020 Google LLC. https://angular.io/
|
---|
5 | * License: MIT
|
---|
6 | */
|
---|
7 | /**
|
---|
8 | * @license
|
---|
9 | * Copyright Google LLC All Rights Reserved.
|
---|
10 | *
|
---|
11 | * Use of this source code is governed by an MIT-style license that can be
|
---|
12 | * found in the LICENSE file at https://angular.io/license
|
---|
13 | */
|
---|
14 | /**
|
---|
15 | * @fileoverview
|
---|
16 | * @suppress {globalThis}
|
---|
17 | */
|
---|
18 | const NEWLINE = '\n';
|
---|
19 | const IGNORE_FRAMES = {};
|
---|
20 | const creationTrace = '__creationTrace__';
|
---|
21 | const ERROR_TAG = 'STACKTRACE TRACKING';
|
---|
22 | const SEP_TAG = '__SEP_TAG__';
|
---|
23 | let sepTemplate = SEP_TAG + '@[native]';
|
---|
24 | class LongStackTrace {
|
---|
25 | constructor() {
|
---|
26 | this.error = getStacktrace();
|
---|
27 | this.timestamp = new Date();
|
---|
28 | }
|
---|
29 | }
|
---|
30 | function getStacktraceWithUncaughtError() {
|
---|
31 | return new Error(ERROR_TAG);
|
---|
32 | }
|
---|
33 | function getStacktraceWithCaughtError() {
|
---|
34 | try {
|
---|
35 | throw getStacktraceWithUncaughtError();
|
---|
36 | }
|
---|
37 | catch (err) {
|
---|
38 | return err;
|
---|
39 | }
|
---|
40 | }
|
---|
41 | // Some implementations of exception handling don't create a stack trace if the exception
|
---|
42 | // isn't thrown, however it's faster not to actually throw the exception.
|
---|
43 | const error = getStacktraceWithUncaughtError();
|
---|
44 | const caughtError = getStacktraceWithCaughtError();
|
---|
45 | const getStacktrace = error.stack ?
|
---|
46 | getStacktraceWithUncaughtError :
|
---|
47 | (caughtError.stack ? getStacktraceWithCaughtError : getStacktraceWithUncaughtError);
|
---|
48 | function getFrames(error) {
|
---|
49 | return error.stack ? error.stack.split(NEWLINE) : [];
|
---|
50 | }
|
---|
51 | function addErrorStack(lines, error) {
|
---|
52 | let trace = getFrames(error);
|
---|
53 | for (let i = 0; i < trace.length; i++) {
|
---|
54 | const frame = trace[i];
|
---|
55 | // Filter out the Frames which are part of stack capturing.
|
---|
56 | if (!IGNORE_FRAMES.hasOwnProperty(frame)) {
|
---|
57 | lines.push(trace[i]);
|
---|
58 | }
|
---|
59 | }
|
---|
60 | }
|
---|
61 | function renderLongStackTrace(frames, stack) {
|
---|
62 | const longTrace = [stack ? stack.trim() : ''];
|
---|
63 | if (frames) {
|
---|
64 | let timestamp = new Date().getTime();
|
---|
65 | for (let i = 0; i < frames.length; i++) {
|
---|
66 | const traceFrames = frames[i];
|
---|
67 | const lastTime = traceFrames.timestamp;
|
---|
68 | let separator = `____________________Elapsed ${timestamp - lastTime.getTime()} ms; At: ${lastTime}`;
|
---|
69 | separator = separator.replace(/[^\w\d]/g, '_');
|
---|
70 | longTrace.push(sepTemplate.replace(SEP_TAG, separator));
|
---|
71 | addErrorStack(longTrace, traceFrames.error);
|
---|
72 | timestamp = lastTime.getTime();
|
---|
73 | }
|
---|
74 | }
|
---|
75 | return longTrace.join(NEWLINE);
|
---|
76 | }
|
---|
77 | // if Error.stackTraceLimit is 0, means stack trace
|
---|
78 | // is disabled, so we don't need to generate long stack trace
|
---|
79 | // this will improve performance in some test(some test will
|
---|
80 | // set stackTraceLimit to 0, https://github.com/angular/zone.js/issues/698
|
---|
81 | function stackTracesEnabled() {
|
---|
82 | // Cast through any since this property only exists on Error in the nodejs
|
---|
83 | // typings.
|
---|
84 | return Error.stackTraceLimit > 0;
|
---|
85 | }
|
---|
86 | Zone['longStackTraceZoneSpec'] = {
|
---|
87 | name: 'long-stack-trace',
|
---|
88 | longStackTraceLimit: 10,
|
---|
89 | // add a getLongStackTrace method in spec to
|
---|
90 | // handle handled reject promise error.
|
---|
91 | getLongStackTrace: function (error) {
|
---|
92 | if (!error) {
|
---|
93 | return undefined;
|
---|
94 | }
|
---|
95 | const trace = error[Zone.__symbol__('currentTaskTrace')];
|
---|
96 | if (!trace) {
|
---|
97 | return error.stack;
|
---|
98 | }
|
---|
99 | return renderLongStackTrace(trace, error.stack);
|
---|
100 | },
|
---|
101 | onScheduleTask: function (parentZoneDelegate, currentZone, targetZone, task) {
|
---|
102 | if (stackTracesEnabled()) {
|
---|
103 | const currentTask = Zone.currentTask;
|
---|
104 | let trace = currentTask && currentTask.data && currentTask.data[creationTrace] || [];
|
---|
105 | trace = [new LongStackTrace()].concat(trace);
|
---|
106 | if (trace.length > this.longStackTraceLimit) {
|
---|
107 | trace.length = this.longStackTraceLimit;
|
---|
108 | }
|
---|
109 | if (!task.data)
|
---|
110 | task.data = {};
|
---|
111 | if (task.type === 'eventTask') {
|
---|
112 | // Fix issue https://github.com/angular/zone.js/issues/1195,
|
---|
113 | // For event task of browser, by default, all task will share a
|
---|
114 | // singleton instance of data object, we should create a new one here
|
---|
115 | // The cast to `any` is required to workaround a closure bug which wrongly applies
|
---|
116 | // URL sanitization rules to .data access.
|
---|
117 | task.data = Object.assign({}, task.data);
|
---|
118 | }
|
---|
119 | task.data[creationTrace] = trace;
|
---|
120 | }
|
---|
121 | return parentZoneDelegate.scheduleTask(targetZone, task);
|
---|
122 | },
|
---|
123 | onHandleError: function (parentZoneDelegate, currentZone, targetZone, error) {
|
---|
124 | if (stackTracesEnabled()) {
|
---|
125 | const parentTask = Zone.currentTask || error.task;
|
---|
126 | if (error instanceof Error && parentTask) {
|
---|
127 | const longStack = renderLongStackTrace(parentTask.data && parentTask.data[creationTrace], error.stack);
|
---|
128 | try {
|
---|
129 | error.stack = error.longStack = longStack;
|
---|
130 | }
|
---|
131 | catch (err) {
|
---|
132 | }
|
---|
133 | }
|
---|
134 | }
|
---|
135 | return parentZoneDelegate.handleError(targetZone, error);
|
---|
136 | }
|
---|
137 | };
|
---|
138 | function captureStackTraces(stackTraces, count) {
|
---|
139 | if (count > 0) {
|
---|
140 | stackTraces.push(getFrames((new LongStackTrace()).error));
|
---|
141 | captureStackTraces(stackTraces, count - 1);
|
---|
142 | }
|
---|
143 | }
|
---|
144 | function computeIgnoreFrames() {
|
---|
145 | if (!stackTracesEnabled()) {
|
---|
146 | return;
|
---|
147 | }
|
---|
148 | const frames = [];
|
---|
149 | captureStackTraces(frames, 2);
|
---|
150 | const frames1 = frames[0];
|
---|
151 | const frames2 = frames[1];
|
---|
152 | for (let i = 0; i < frames1.length; i++) {
|
---|
153 | const frame1 = frames1[i];
|
---|
154 | if (frame1.indexOf(ERROR_TAG) == -1) {
|
---|
155 | let match = frame1.match(/^\s*at\s+/);
|
---|
156 | if (match) {
|
---|
157 | sepTemplate = match[0] + SEP_TAG + ' (http://localhost)';
|
---|
158 | break;
|
---|
159 | }
|
---|
160 | }
|
---|
161 | }
|
---|
162 | for (let i = 0; i < frames1.length; i++) {
|
---|
163 | const frame1 = frames1[i];
|
---|
164 | const frame2 = frames2[i];
|
---|
165 | if (frame1 === frame2) {
|
---|
166 | IGNORE_FRAMES[frame1] = true;
|
---|
167 | }
|
---|
168 | else {
|
---|
169 | break;
|
---|
170 | }
|
---|
171 | }
|
---|
172 | }
|
---|
173 | computeIgnoreFrames();
|
---|