2020-08-21 23:25:39 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const {
|
2023-03-31 17:40:30 +00:00
|
|
|
ArrayPrototypeAt,
|
2020-08-21 23:25:39 +00:00
|
|
|
ArrayPrototypeIndexOf,
|
|
|
|
ArrayPrototypePush,
|
2024-10-14 10:55:39 +00:00
|
|
|
ArrayPrototypePushApply,
|
|
|
|
ArrayPrototypeSlice,
|
2020-08-21 23:25:39 +00:00
|
|
|
ArrayPrototypeSplice,
|
2024-03-07 03:03:32 +00:00
|
|
|
ObjectDefineProperty,
|
2020-08-21 23:25:39 +00:00
|
|
|
ObjectGetPrototypeOf,
|
|
|
|
ObjectSetPrototypeOf,
|
2023-03-31 17:40:30 +00:00
|
|
|
Promise,
|
|
|
|
PromisePrototypeThen,
|
|
|
|
PromiseReject,
|
2024-04-21 16:53:08 +00:00
|
|
|
PromiseResolve,
|
2023-03-31 17:40:30 +00:00
|
|
|
ReflectApply,
|
2024-04-21 16:53:08 +00:00
|
|
|
SafeFinalizationRegistry,
|
2023-03-31 17:40:30 +00:00
|
|
|
SafeMap,
|
2020-08-21 23:25:39 +00:00
|
|
|
SymbolHasInstance,
|
|
|
|
} = primordials;
|
|
|
|
|
|
|
|
const {
|
|
|
|
codes: {
|
|
|
|
ERR_INVALID_ARG_TYPE,
|
2023-02-12 18:26:21 +00:00
|
|
|
},
|
2020-08-21 23:25:39 +00:00
|
|
|
} = require('internal/errors');
|
2021-01-24 07:46:24 +00:00
|
|
|
const {
|
|
|
|
validateFunction,
|
|
|
|
} = require('internal/validators');
|
2020-08-21 23:25:39 +00:00
|
|
|
|
|
|
|
const { triggerUncaughtException } = internalBinding('errors');
|
|
|
|
|
2023-08-07 14:28:56 +00:00
|
|
|
const { WeakReference } = require('internal/util');
|
2020-08-21 23:25:39 +00:00
|
|
|
|
2023-04-13 13:40:50 +00:00
|
|
|
// Can't delete when weakref count reaches 0 as it could increment again.
|
|
|
|
// Only GC can be used as a valid time to clean up the channels map.
|
|
|
|
class WeakRefMap extends SafeMap {
|
|
|
|
#finalizers = new SafeFinalizationRegistry((key) => {
|
|
|
|
this.delete(key);
|
|
|
|
});
|
|
|
|
|
|
|
|
set(key, value) {
|
|
|
|
this.#finalizers.register(value, key);
|
|
|
|
return super.set(key, new WeakReference(value));
|
2023-03-31 17:40:30 +00:00
|
|
|
}
|
|
|
|
|
2023-04-13 13:40:50 +00:00
|
|
|
get(key) {
|
|
|
|
return super.get(key)?.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
incRef(key) {
|
|
|
|
return super.get(key)?.incRef();
|
|
|
|
}
|
|
|
|
|
|
|
|
decRef(key) {
|
|
|
|
return super.get(key)?.decRef();
|
|
|
|
}
|
2023-03-31 17:40:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function markActive(channel) {
|
|
|
|
// eslint-disable-next-line no-use-before-define
|
|
|
|
ObjectSetPrototypeOf(channel, ActiveChannel.prototype);
|
|
|
|
channel._subscribers = [];
|
|
|
|
channel._stores = new SafeMap();
|
|
|
|
}
|
|
|
|
|
|
|
|
function maybeMarkInactive(channel) {
|
|
|
|
// When there are no more active subscribers or bound, restore to fast prototype.
|
|
|
|
if (!channel._subscribers.length && !channel._stores.size) {
|
|
|
|
// eslint-disable-next-line no-use-before-define
|
|
|
|
ObjectSetPrototypeOf(channel, Channel.prototype);
|
|
|
|
channel._subscribers = undefined;
|
|
|
|
channel._stores = undefined;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function defaultTransform(data) {
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
function wrapStoreRun(store, data, next, transform = defaultTransform) {
|
|
|
|
return () => {
|
|
|
|
let context;
|
|
|
|
try {
|
|
|
|
context = transform(data);
|
|
|
|
} catch (err) {
|
|
|
|
process.nextTick(() => {
|
|
|
|
triggerUncaughtException(err, false);
|
|
|
|
});
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
|
|
|
return store.run(context, next);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-08-21 23:25:39 +00:00
|
|
|
// TODO(qard): should there be a C++ channel interface?
|
|
|
|
class ActiveChannel {
|
|
|
|
subscribe(subscription) {
|
2021-01-24 07:46:24 +00:00
|
|
|
validateFunction(subscription, 'subscription');
|
2024-10-14 10:55:39 +00:00
|
|
|
this._subscribers = ArrayPrototypeSlice(this._subscribers);
|
2020-08-21 23:25:39 +00:00
|
|
|
ArrayPrototypePush(this._subscribers, subscription);
|
2023-04-13 13:40:50 +00:00
|
|
|
channels.incRef(this.name);
|
2020-08-21 23:25:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
unsubscribe(subscription) {
|
|
|
|
const index = ArrayPrototypeIndexOf(this._subscribers, subscription);
|
2021-10-19 09:56:42 +00:00
|
|
|
if (index === -1) return false;
|
2020-08-21 23:25:39 +00:00
|
|
|
|
2024-10-14 10:55:39 +00:00
|
|
|
const before = ArrayPrototypeSlice(this._subscribers, 0, index);
|
|
|
|
const after = ArrayPrototypeSlice(this._subscribers, index + 1);
|
|
|
|
this._subscribers = before;
|
|
|
|
ArrayPrototypePushApply(this._subscribers, after);
|
2021-10-19 09:56:42 +00:00
|
|
|
|
2023-04-13 13:40:50 +00:00
|
|
|
channels.decRef(this.name);
|
2023-03-31 17:40:30 +00:00
|
|
|
maybeMarkInactive(this);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bindStore(store, transform) {
|
|
|
|
const replacing = this._stores.has(store);
|
2023-04-13 13:40:50 +00:00
|
|
|
if (!replacing) channels.incRef(this.name);
|
2023-03-31 17:40:30 +00:00
|
|
|
this._stores.set(store, transform);
|
|
|
|
}
|
|
|
|
|
|
|
|
unbindStore(store) {
|
|
|
|
if (!this._stores.has(store)) {
|
|
|
|
return false;
|
2020-08-21 23:25:39 +00:00
|
|
|
}
|
2021-10-19 09:56:42 +00:00
|
|
|
|
2023-03-31 17:40:30 +00:00
|
|
|
this._stores.delete(store);
|
|
|
|
|
2023-04-13 13:40:50 +00:00
|
|
|
channels.decRef(this.name);
|
2023-03-31 17:40:30 +00:00
|
|
|
maybeMarkInactive(this);
|
|
|
|
|
2021-10-19 09:56:42 +00:00
|
|
|
return true;
|
2020-08-21 23:25:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
get hasSubscribers() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
publish(data) {
|
2024-10-14 10:55:39 +00:00
|
|
|
const subscribers = this._subscribers;
|
|
|
|
for (let i = 0; i < (subscribers?.length || 0); i++) {
|
2020-08-21 23:25:39 +00:00
|
|
|
try {
|
2024-10-14 10:55:39 +00:00
|
|
|
const onMessage = subscribers[i];
|
2020-08-21 23:25:39 +00:00
|
|
|
onMessage(data, this.name);
|
|
|
|
} catch (err) {
|
|
|
|
process.nextTick(() => {
|
|
|
|
triggerUncaughtException(err, false);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-03-31 17:40:30 +00:00
|
|
|
|
|
|
|
runStores(data, fn, thisArg, ...args) {
|
|
|
|
let run = () => {
|
|
|
|
this.publish(data);
|
|
|
|
return ReflectApply(fn, thisArg, args);
|
|
|
|
};
|
|
|
|
|
|
|
|
for (const entry of this._stores.entries()) {
|
|
|
|
const store = entry[0];
|
|
|
|
const transform = entry[1];
|
|
|
|
run = wrapStoreRun(store, data, run, transform);
|
|
|
|
}
|
|
|
|
|
|
|
|
return run();
|
|
|
|
}
|
2020-08-21 23:25:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class Channel {
|
|
|
|
constructor(name) {
|
|
|
|
this._subscribers = undefined;
|
2023-03-31 17:40:30 +00:00
|
|
|
this._stores = undefined;
|
2020-08-21 23:25:39 +00:00
|
|
|
this.name = name;
|
2023-03-31 17:40:30 +00:00
|
|
|
|
2023-04-13 13:40:50 +00:00
|
|
|
channels.set(name, this);
|
2020-08-21 23:25:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static [SymbolHasInstance](instance) {
|
|
|
|
const prototype = ObjectGetPrototypeOf(instance);
|
|
|
|
return prototype === Channel.prototype ||
|
|
|
|
prototype === ActiveChannel.prototype;
|
|
|
|
}
|
|
|
|
|
|
|
|
subscribe(subscription) {
|
2023-03-31 17:40:30 +00:00
|
|
|
markActive(this);
|
2020-08-21 23:25:39 +00:00
|
|
|
this.subscribe(subscription);
|
|
|
|
}
|
|
|
|
|
2021-10-19 10:01:41 +00:00
|
|
|
unsubscribe() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-03-31 17:40:30 +00:00
|
|
|
bindStore(store, transform) {
|
|
|
|
markActive(this);
|
|
|
|
this.bindStore(store, transform);
|
|
|
|
}
|
|
|
|
|
|
|
|
unbindStore() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-08-21 23:25:39 +00:00
|
|
|
get hasSubscribers() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
publish() {}
|
2023-03-31 17:40:30 +00:00
|
|
|
|
|
|
|
runStores(data, fn, thisArg, ...args) {
|
|
|
|
return ReflectApply(fn, thisArg, args);
|
|
|
|
}
|
2020-08-21 23:25:39 +00:00
|
|
|
}
|
|
|
|
|
2023-04-13 13:40:50 +00:00
|
|
|
const channels = new WeakRefMap();
|
2020-08-21 23:25:39 +00:00
|
|
|
|
|
|
|
function channel(name) {
|
2023-04-13 13:40:50 +00:00
|
|
|
const channel = channels.get(name);
|
2020-08-21 23:25:39 +00:00
|
|
|
if (channel) return channel;
|
|
|
|
|
|
|
|
if (typeof name !== 'string' && typeof name !== 'symbol') {
|
|
|
|
throw new ERR_INVALID_ARG_TYPE('channel', ['string', 'symbol'], name);
|
|
|
|
}
|
|
|
|
|
2023-03-31 17:40:30 +00:00
|
|
|
return new Channel(name);
|
2020-08-21 23:25:39 +00:00
|
|
|
}
|
|
|
|
|
2022-04-12 22:54:03 +00:00
|
|
|
function subscribe(name, subscription) {
|
2023-03-31 17:40:30 +00:00
|
|
|
return channel(name).subscribe(subscription);
|
2022-04-12 22:54:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function unsubscribe(name, subscription) {
|
2023-03-31 17:40:30 +00:00
|
|
|
return channel(name).unsubscribe(subscription);
|
2022-04-12 22:54:03 +00:00
|
|
|
}
|
|
|
|
|
2020-08-21 23:25:39 +00:00
|
|
|
function hasSubscribers(name) {
|
2023-04-13 13:40:50 +00:00
|
|
|
const channel = channels.get(name);
|
|
|
|
if (!channel) return false;
|
2020-08-21 23:25:39 +00:00
|
|
|
|
|
|
|
return channel.hasSubscribers;
|
|
|
|
}
|
|
|
|
|
2023-03-31 17:40:30 +00:00
|
|
|
const traceEvents = [
|
|
|
|
'start',
|
|
|
|
'end',
|
|
|
|
'asyncStart',
|
|
|
|
'asyncEnd',
|
|
|
|
'error',
|
|
|
|
];
|
|
|
|
|
|
|
|
function assertChannel(value, name) {
|
|
|
|
if (!(value instanceof Channel)) {
|
|
|
|
throw new ERR_INVALID_ARG_TYPE(name, ['Channel'], value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-07 03:03:32 +00:00
|
|
|
function tracingChannelFrom(nameOrChannels, name) {
|
|
|
|
if (typeof nameOrChannels === 'string') {
|
|
|
|
return channel(`tracing:${nameOrChannels}:${name}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof nameOrChannels === 'object' && nameOrChannels !== null) {
|
|
|
|
const channel = nameOrChannels[name];
|
|
|
|
assertChannel(channel, `nameOrChannels.${name}`);
|
|
|
|
return channel;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new ERR_INVALID_ARG_TYPE('nameOrChannels',
|
|
|
|
['string', 'object', 'TracingChannel'],
|
|
|
|
nameOrChannels);
|
|
|
|
}
|
|
|
|
|
2023-03-31 17:40:30 +00:00
|
|
|
class TracingChannel {
|
|
|
|
constructor(nameOrChannels) {
|
2024-06-19 14:12:59 +00:00
|
|
|
for (let i = 0; i < traceEvents.length; ++i) {
|
|
|
|
const eventName = traceEvents[i];
|
2024-03-07 03:03:32 +00:00
|
|
|
ObjectDefineProperty(this, eventName, {
|
|
|
|
__proto__: null,
|
|
|
|
value: tracingChannelFrom(nameOrChannels, eventName),
|
|
|
|
});
|
2023-03-31 17:40:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-07 03:03:32 +00:00
|
|
|
get hasSubscribers() {
|
2024-06-19 14:12:59 +00:00
|
|
|
return this.start?.hasSubscribers ||
|
|
|
|
this.end?.hasSubscribers ||
|
|
|
|
this.asyncStart?.hasSubscribers ||
|
|
|
|
this.asyncEnd?.hasSubscribers ||
|
|
|
|
this.error?.hasSubscribers;
|
2024-03-07 03:03:32 +00:00
|
|
|
}
|
|
|
|
|
2023-03-31 17:40:30 +00:00
|
|
|
subscribe(handlers) {
|
2024-06-19 14:12:59 +00:00
|
|
|
for (let i = 0; i < traceEvents.length; ++i) {
|
|
|
|
const name = traceEvents[i];
|
2023-03-31 17:40:30 +00:00
|
|
|
if (!handlers[name]) continue;
|
|
|
|
|
|
|
|
this[name]?.subscribe(handlers[name]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unsubscribe(handlers) {
|
|
|
|
let done = true;
|
|
|
|
|
2024-06-19 14:12:59 +00:00
|
|
|
for (let i = 0; i < traceEvents.length; ++i) {
|
|
|
|
const name = traceEvents[i];
|
2023-03-31 17:40:30 +00:00
|
|
|
if (!handlers[name]) continue;
|
|
|
|
|
|
|
|
if (!this[name]?.unsubscribe(handlers[name])) {
|
|
|
|
done = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return done;
|
|
|
|
}
|
|
|
|
|
|
|
|
traceSync(fn, context = {}, thisArg, ...args) {
|
2024-03-07 03:03:32 +00:00
|
|
|
if (!this.hasSubscribers) {
|
|
|
|
return ReflectApply(fn, thisArg, args);
|
|
|
|
}
|
|
|
|
|
2023-03-31 17:40:30 +00:00
|
|
|
const { start, end, error } = this;
|
|
|
|
|
|
|
|
return start.runStores(context, () => {
|
|
|
|
try {
|
|
|
|
const result = ReflectApply(fn, thisArg, args);
|
|
|
|
context.result = result;
|
|
|
|
return result;
|
|
|
|
} catch (err) {
|
|
|
|
context.error = err;
|
|
|
|
error.publish(context);
|
|
|
|
throw err;
|
|
|
|
} finally {
|
|
|
|
end.publish(context);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
tracePromise(fn, context = {}, thisArg, ...args) {
|
2024-03-07 03:03:32 +00:00
|
|
|
if (!this.hasSubscribers) {
|
|
|
|
return ReflectApply(fn, thisArg, args);
|
|
|
|
}
|
|
|
|
|
2023-03-31 17:40:30 +00:00
|
|
|
const { start, end, asyncStart, asyncEnd, error } = this;
|
|
|
|
|
|
|
|
function reject(err) {
|
|
|
|
context.error = err;
|
|
|
|
error.publish(context);
|
|
|
|
asyncStart.publish(context);
|
|
|
|
// TODO: Is there a way to have asyncEnd _after_ the continuation?
|
|
|
|
asyncEnd.publish(context);
|
|
|
|
return PromiseReject(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
function resolve(result) {
|
|
|
|
context.result = result;
|
|
|
|
asyncStart.publish(context);
|
|
|
|
// TODO: Is there a way to have asyncEnd _after_ the continuation?
|
|
|
|
asyncEnd.publish(context);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
return start.runStores(context, () => {
|
|
|
|
try {
|
|
|
|
let promise = ReflectApply(fn, thisArg, args);
|
|
|
|
// Convert thenables to native promises
|
|
|
|
if (!(promise instanceof Promise)) {
|
|
|
|
promise = PromiseResolve(promise);
|
|
|
|
}
|
|
|
|
return PromisePrototypeThen(promise, resolve, reject);
|
|
|
|
} catch (err) {
|
|
|
|
context.error = err;
|
|
|
|
error.publish(context);
|
|
|
|
throw err;
|
|
|
|
} finally {
|
|
|
|
end.publish(context);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
traceCallback(fn, position = -1, context = {}, thisArg, ...args) {
|
2024-03-07 03:03:32 +00:00
|
|
|
if (!this.hasSubscribers) {
|
|
|
|
return ReflectApply(fn, thisArg, args);
|
|
|
|
}
|
|
|
|
|
2023-03-31 17:40:30 +00:00
|
|
|
const { start, end, asyncStart, asyncEnd, error } = this;
|
|
|
|
|
|
|
|
function wrappedCallback(err, res) {
|
|
|
|
if (err) {
|
|
|
|
context.error = err;
|
|
|
|
error.publish(context);
|
|
|
|
} else {
|
|
|
|
context.result = res;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Using runStores here enables manual context failure recovery
|
|
|
|
asyncStart.runStores(context, () => {
|
|
|
|
try {
|
2023-12-08 21:01:23 +00:00
|
|
|
return ReflectApply(callback, this, arguments);
|
2023-03-31 17:40:30 +00:00
|
|
|
} finally {
|
|
|
|
asyncEnd.publish(context);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const callback = ArrayPrototypeAt(args, position);
|
2023-11-30 13:55:06 +00:00
|
|
|
validateFunction(callback, 'callback');
|
2023-03-31 17:40:30 +00:00
|
|
|
ArrayPrototypeSplice(args, position, 1, wrappedCallback);
|
|
|
|
|
|
|
|
return start.runStores(context, () => {
|
|
|
|
try {
|
|
|
|
return ReflectApply(fn, thisArg, args);
|
|
|
|
} catch (err) {
|
|
|
|
context.error = err;
|
|
|
|
error.publish(context);
|
|
|
|
throw err;
|
|
|
|
} finally {
|
|
|
|
end.publish(context);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function tracingChannel(nameOrChannels) {
|
|
|
|
return new TracingChannel(nameOrChannels);
|
|
|
|
}
|
|
|
|
|
2020-08-21 23:25:39 +00:00
|
|
|
module.exports = {
|
|
|
|
channel,
|
|
|
|
hasSubscribers,
|
2022-04-12 22:54:03 +00:00
|
|
|
subscribe,
|
2023-03-31 17:40:30 +00:00
|
|
|
tracingChannel,
|
2022-04-12 22:54:03 +00:00
|
|
|
unsubscribe,
|
2023-02-12 18:26:21 +00:00
|
|
|
Channel,
|
2020-08-21 23:25:39 +00:00
|
|
|
};
|