2024-08-02 19:44:20 +00:00
|
|
|
'use strict';
|
|
|
|
|
2024-08-09 19:44:42 +00:00
|
|
|
const {
|
|
|
|
ObjectSetPrototypeOf,
|
|
|
|
} = primordials;
|
|
|
|
|
2024-08-02 19:44:20 +00:00
|
|
|
const {
|
|
|
|
getContinuationPreservedEmbedderData,
|
|
|
|
setContinuationPreservedEmbedderData,
|
|
|
|
} = internalBinding('async_context_frame');
|
|
|
|
|
|
|
|
let enabled_;
|
|
|
|
|
2024-08-30 20:27:28 +00:00
|
|
|
class ActiveAsyncContextFrame extends Map {
|
2024-08-02 19:44:20 +00:00
|
|
|
static get enabled() {
|
2024-08-09 19:44:42 +00:00
|
|
|
return true;
|
2024-08-02 19:44:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static current() {
|
2024-08-09 19:44:42 +00:00
|
|
|
return getContinuationPreservedEmbedderData();
|
2024-08-02 19:44:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static set(frame) {
|
2024-08-09 19:44:42 +00:00
|
|
|
setContinuationPreservedEmbedderData(frame);
|
2024-08-02 19:44:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static exchange(frame) {
|
|
|
|
const prior = this.current();
|
|
|
|
this.set(frame);
|
|
|
|
return prior;
|
|
|
|
}
|
|
|
|
|
|
|
|
static disable(store) {
|
|
|
|
const frame = this.current();
|
|
|
|
frame?.disable(store);
|
|
|
|
}
|
2024-08-09 19:44:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function checkEnabled() {
|
|
|
|
const enabled = require('internal/options')
|
2024-11-02 15:14:28 +00:00
|
|
|
.getOptionValue('--async-context-frame');
|
2024-08-09 19:44:42 +00:00
|
|
|
|
|
|
|
// If enabled, swap to active prototype so we don't need to check status
|
|
|
|
// on every interaction with the async context frame.
|
|
|
|
if (enabled) {
|
|
|
|
// eslint-disable-next-line no-use-before-define
|
|
|
|
ObjectSetPrototypeOf(AsyncContextFrame, ActiveAsyncContextFrame);
|
|
|
|
}
|
|
|
|
|
|
|
|
return enabled;
|
|
|
|
}
|
|
|
|
|
2024-08-30 20:27:28 +00:00
|
|
|
class InactiveAsyncContextFrame extends Map {
|
2024-08-09 19:44:42 +00:00
|
|
|
static get enabled() {
|
|
|
|
enabled_ ??= checkEnabled();
|
|
|
|
return enabled_;
|
|
|
|
}
|
|
|
|
|
|
|
|
static current() {}
|
|
|
|
static set(frame) {}
|
|
|
|
static exchange(frame) {}
|
|
|
|
static disable(store) {}
|
2024-08-30 20:27:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class AsyncContextFrame extends InactiveAsyncContextFrame {
|
|
|
|
constructor(store, data) {
|
|
|
|
super(AsyncContextFrame.current());
|
|
|
|
this.set(store, data);
|
|
|
|
}
|
2024-08-02 19:44:20 +00:00
|
|
|
|
|
|
|
disable(store) {
|
|
|
|
this.delete(store);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = AsyncContextFrame;
|