-
Notifications
You must be signed in to change notification settings - Fork 68
/
node-async_hooks.js
235 lines (215 loc) · 7.18 KB
/
node-async_hooks.js
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import { createHook, AsyncResource } from 'async_hooks';
const asyncHooksSymbols = {
async_id_symbol: undefined,
trigger_async_id_symbol: undefined,
destroyed: undefined,
};
const promiseAsyncHookFallbackStates = new WeakMap();
const setAsyncSymbol = (description, symbol) => {
if (!(description in asyncHooksSymbols)) {
throw new Error('Unknown symbol');
} else if (!asyncHooksSymbols[description]) {
if (symbol.description !== description) {
// Throw an error since the whitelist mechanism relies on the description
throw new Error(
`Mismatched symbol found for ${description}: ${String(symbol)}`,
);
}
asyncHooksSymbols[description] = symbol;
return true;
} else if (asyncHooksSymbols[description] !== symbol) {
// process._rawDebug(
// `Found duplicate ${description}:`,
// symbol,
// asyncHooksSymbols[description],
// );
return false;
} else {
return true;
}
};
// We can get the `async_id_symbol` and `trigger_async_id_symbol` through a
// simple instantiation of async_hook.AsyncResource, which causes little side
// effects. These are the 2 symbols that may be late bound, aka after the promise
// is returned to the program and would normally be frozen.
const findAsyncSymbolsFromAsyncResource = () => {
let found = 0;
Object.getOwnPropertySymbols(new AsyncResource('Bootstrap')).forEach(sym => {
const { description } = sym;
if (description in asyncHooksSymbols) {
if (setAsyncSymbol(description, sym)) {
found += 1;
}
}
});
return found;
};
// To get the `destroyed` symbol installed on promises by async_hooks,
// the only option is to create and enable an AsyncHook.
// Different versions of Node handle this in various ways.
const getPromiseFromCreateHook = () => {
const bootstrapHookData = [];
const bootstrapHook = createHook({
init(asyncId, type, triggerAsyncId, resource) {
if (type !== 'PROMISE') return;
// process._rawDebug('Bootstrap', asyncId, triggerAsyncId, resource);
bootstrapHookData.push({ asyncId, triggerAsyncId, resource });
},
destroy(_asyncId) {
// Needs to be present to trigger the addition of the destroyed symbol
},
});
bootstrapHook.enable();
// Use a never resolving promise to avoid triggering settlement hooks
const trigger = new Promise(() => {});
bootstrapHook.disable();
// In some versions of Node, async_hooks don't give access to the resource
// itself, but to a "wrapper" which is basically hooks metadata for the promise
const promisesData = bootstrapHookData.filter(
({ resource }) => Promise.resolve(resource) === resource,
);
const { length } = promisesData;
if (length > 1) {
// process._rawDebug('Found multiple potential candidates');
}
const promiseData = promisesData.find(({ resource }) => resource === trigger);
if (promiseData) {
// Normally all promise hooks are disabled in a subsequent microtask
// That means Node versions that modify promises at init will still
// trigger our proto hooks for promises created in this turn
// The following trick will disable the internal promise init hook
// However, only do this for destroy modifying versions, since some versions
// only modify promises if no destroy hook is requested, and do not correctly
// reset the internal init promise hook in those case. (e.g. v14.16.2)
const resetHook = createHook({});
resetHook.enable();
resetHook.disable();
} else if (length) {
// process._rawDebug('No candidates matched');
}
return promiseData;
};
const findAsyncSymbolsFromPromiseCreateHook = () => {
const bootstrapPromise = getPromiseFromCreateHook();
if (bootstrapPromise) {
const { asyncId, triggerAsyncId, resource } = bootstrapPromise;
const symbols = Object.getOwnPropertySymbols(resource);
// const { length } = symbols;
let found = 0;
// if (length !== 3) {
// process._rawDebug(`Found ${length} symbols on promise:`, ...symbols);
// }
symbols.forEach(symbol => {
const value = resource[symbol];
let type;
if (value === asyncId) {
type = 'async_id_symbol';
} else if (value === triggerAsyncId) {
type = 'trigger_async_id_symbol';
} else if (typeof value === 'object' && 'destroyed' in value) {
type = 'destroyed';
} else {
// process._rawDebug(`Unexpected symbol`, symbol);
return;
}
if (setAsyncSymbol(type, symbol)) {
found += 1;
}
});
return found;
} else {
// This node version is not mutating promises
return -2;
}
};
const getAsyncHookFallbackState = (promise, { create = false } = {}) => {
let state = promiseAsyncHookFallbackStates.get(promise);
if (!state && create) {
state = {
[asyncHooksSymbols.async_id_symbol]: undefined,
[asyncHooksSymbols.trigger_async_id_symbol]: undefined,
};
if (asyncHooksSymbols.destroyed) {
state[asyncHooksSymbols.destroyed] = undefined;
}
promiseAsyncHookFallbackStates.set(promise, state);
}
return state;
};
const setAsyncIdFallback = (promise, symbol, value) => {
const state = getAsyncHookFallbackState(promise, { create: true });
if (state[symbol]) {
if (state[symbol] !== value) {
// This can happen if a frozen promise created before hooks were enabled
// is used multiple times as a parent promise
// It's safe to ignore subsequent values
}
} else {
state[symbol] = value;
}
};
const getAsyncHookSymbolPromiseProtoDesc = (
symbol,
{ disallowGet = false } = {},
) => ({
set(value) {
if (Object.isExtensible(this)) {
Object.defineProperty(this, symbol, {
value,
writable: false,
configurable: false,
enumerable: false,
});
} else {
// process._rawDebug('fallback set of async id', symbol, value, new Error().stack);
setAsyncIdFallback(this, symbol, value);
}
},
get() {
if (disallowGet) {
return undefined;
}
const state = getAsyncHookFallbackState(this, { create: false });
return state && state[symbol];
},
enumerable: false,
configurable: true,
});
export const setup = ({ withDestroy = true } = {}) => {
if (withDestroy) {
findAsyncSymbolsFromPromiseCreateHook();
} else {
findAsyncSymbolsFromAsyncResource();
}
if (
!asyncHooksSymbols.async_id_symbol ||
!asyncHooksSymbols.trigger_async_id_symbol
) {
// process._rawDebug(`Async symbols not found, moving on`);
return;
}
const PromiseProto = Promise.prototype;
Object.defineProperty(
PromiseProto,
asyncHooksSymbols.async_id_symbol,
getAsyncHookSymbolPromiseProtoDesc(asyncHooksSymbols.async_id_symbol),
);
Object.defineProperty(
PromiseProto,
asyncHooksSymbols.trigger_async_id_symbol,
getAsyncHookSymbolPromiseProtoDesc(
asyncHooksSymbols.trigger_async_id_symbol,
),
);
if (asyncHooksSymbols.destroyed) {
Object.defineProperty(
PromiseProto,
asyncHooksSymbols.destroyed,
getAsyncHookSymbolPromiseProtoDesc(asyncHooksSymbols.destroyed, {
disallowGet: true,
}),
);
} else if (withDestroy) {
// process._rawDebug(`Couldn't find destroyed symbol to setup trap`);
}
};