first commit

This commit is contained in:
Stefan Hacker
2026-04-03 09:38:48 +02:00
commit 37ad745546
47450 changed files with 3120798 additions and 0 deletions
+26
View File
@@ -0,0 +1,26 @@
import { IModulePatcher } from "./patchRequire";
export { PatchFunction, IModulePatcher, makePatchingRequire } from "./patchRequire";
export interface ISpanContext {
traceId: string;
spanId: string;
traceFlags?: string;
tracestate?: string;
}
declare type ScopeManager = any;
export interface IStandardEvent<T> {
timestamp: number;
data: T;
}
export declare type ISubscriber<T> = (event: IStandardEvent<T>) => void;
export declare type IFilter = (publishing: boolean) => boolean;
export interface IChannel {
shouldPublish(name: string): boolean;
publish<T>(name: string, event: T): void;
subscribe<T>(name: string, listener: ISubscriber<T>, filter?: IFilter): void;
unsubscribe<T>(name: string, listener: ISubscriber<T>, filter?: IFilter): void;
bindToContext<T extends Function>(cb: T): T;
addContextPreservation<T extends Function>(preserver: (cb: T) => T): void;
registerMonkeyPatch(packageName: string, patcher: IModulePatcher): void;
spanContextPropagator: ScopeManager;
}
export declare const channel: IChannel;
+109
View File
@@ -0,0 +1,109 @@
"use strict";
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for details.
Object.defineProperty(exports, "__esModule", { value: true });
var patchRequire_1 = require("./patchRequire");
var patchRequire_2 = require("./patchRequire");
exports.makePatchingRequire = patchRequire_2.makePatchingRequire;
var trueFilter = function (publishing) { return true; };
var ContextPreservingEventEmitter = /** @class */ (function () {
function ContextPreservingEventEmitter() {
this.version = require("./../../package.json").version; // Allow for future versions to replace things?
this.subscribers = {};
this.contextPreservationFunction = function (cb) { return cb; };
this.knownPatches = {};
this.currentlyPublishing = false;
}
ContextPreservingEventEmitter.prototype.shouldPublish = function (name) {
var listeners = this.subscribers[name];
if (listeners) {
return listeners.some(function (_a) {
var filter = _a.filter;
return !filter || filter(false);
});
}
return false;
};
ContextPreservingEventEmitter.prototype.publish = function (name, event) {
if (this.currentlyPublishing) {
return; // Avoid reentrancy
}
var listeners = this.subscribers[name];
// Note: Listeners called synchronously to preserve context
if (listeners) {
var standardEvent_1 = {
timestamp: Date.now(),
data: event,
};
this.currentlyPublishing = true;
listeners.forEach(function (_a) {
var listener = _a.listener, filter = _a.filter;
try {
if (filter && filter(true)) {
listener(standardEvent_1);
}
}
catch (e) {
// Subscriber threw an error
}
});
this.currentlyPublishing = false;
}
};
ContextPreservingEventEmitter.prototype.subscribe = function (name, listener, filter) {
if (filter === void 0) { filter = trueFilter; }
if (!this.subscribers[name]) {
this.subscribers[name] = [];
}
this.subscribers[name].push({ listener: listener, filter: filter });
};
ContextPreservingEventEmitter.prototype.unsubscribe = function (name, listener, filter) {
if (filter === void 0) { filter = trueFilter; }
var listeners = this.subscribers[name];
if (listeners) {
for (var index = 0; index < listeners.length; ++index) {
if (listeners[index].listener === listener && listeners[index].filter === filter) {
listeners.splice(index, 1);
return true;
}
}
}
return false;
};
// Used for tests
ContextPreservingEventEmitter.prototype.reset = function () {
var _this = this;
this.subscribers = {};
this.contextPreservationFunction = function (cb) { return cb; };
// Modify the knownPatches object rather than replace, since a reference will be used in the require patcher
Object.getOwnPropertyNames(this.knownPatches).forEach(function (prop) { return delete _this.knownPatches[prop]; });
};
ContextPreservingEventEmitter.prototype.bindToContext = function (cb) {
return this.contextPreservationFunction(cb);
};
ContextPreservingEventEmitter.prototype.addContextPreservation = function (preserver) {
var previousPreservationStack = this.contextPreservationFunction;
this.contextPreservationFunction = (function (cb) { return preserver(previousPreservationStack(cb)); });
};
ContextPreservingEventEmitter.prototype.registerMonkeyPatch = function (packageName, patcher) {
if (!this.knownPatches[packageName]) {
this.knownPatches[packageName] = [];
}
this.knownPatches[packageName].push(patcher);
};
ContextPreservingEventEmitter.prototype.getPatchesObject = function () {
return this.knownPatches;
};
return ContextPreservingEventEmitter;
}());
if (!global.diagnosticsSource) {
global.diagnosticsSource = new ContextPreservingEventEmitter();
// TODO: should this only patch require after at least one monkey patch is registered?
/* tslint:disable-next-line:no-var-requires */
var moduleModule = require("module");
// Note: We pass in the object now before any patches are registered, but the object is passed by reference
// so any updates made to the object will be visible in the patcher.
moduleModule.prototype.require = patchRequire_1.makePatchingRequire(global.diagnosticsSource.getPatchesObject());
}
exports.channel = global.diagnosticsSource;
//# sourceMappingURL=channel.js.map
+9
View File
@@ -0,0 +1,9 @@
export declare type PatchFunction = (module: any, path: string) => any;
export interface IModulePatcher {
versionSpecifier: string;
patch: PatchFunction;
}
export interface IModulePatchMap {
[key: string]: IModulePatcher[];
}
export declare function makePatchingRequire(knownPatches: IModulePatchMap): (moduleId: string) => any;
+58
View File
@@ -0,0 +1,58 @@
"use strict";
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for details.
Object.defineProperty(exports, "__esModule", { value: true });
var path = require("path");
var semver = require("semver");
/* tslint:disable-next-line:no-var-requires */
var moduleModule = require("module");
var nativeModules = Object.keys(process.binding("natives"));
var originalRequire = moduleModule.prototype.require;
function makePatchingRequire(knownPatches) {
var patchedModules = {};
return function patchedRequire(moduleId) {
var originalModule = originalRequire.apply(this, arguments);
if (knownPatches[moduleId]) {
// Fetch the specific path of the module
var modulePath = moduleModule._resolveFilename(moduleId, this);
if (patchedModules.hasOwnProperty(modulePath)) {
// This module has already been patched, no need to reapply
return patchedModules[modulePath];
}
var moduleVersion = void 0;
if (nativeModules.indexOf(moduleId) < 0) {
try {
moduleVersion = originalRequire.call(this, path.join(moduleId, "package.json")).version;
}
catch (e) {
// This should only happen if moduleId is actually a path rather than a module
// This is not a supported scenario
return originalModule;
}
}
else {
// This module is implemented natively so we cannot find a package.json
// Instead, take the version of node itself
moduleVersion = process.version.substring(1);
}
var prereleaseTagIndex = moduleVersion.indexOf("-");
if (prereleaseTagIndex >= 0) {
// We ignore prerelease tags to avoid impossible to fix gaps in support
// e.g. supporting console in >= 4.0.0 would otherwise not include
// 8.0.0-pre
moduleVersion = moduleVersion.substring(0, prereleaseTagIndex);
}
var modifiedModule = originalModule;
for (var _i = 0, _a = knownPatches[moduleId]; _i < _a.length; _i++) {
var modulePatcher = _a[_i];
if (semver.satisfies(moduleVersion, modulePatcher.versionSpecifier)) {
modifiedModule = modulePatcher.patch(modifiedModule, modulePath);
}
}
return patchedModules[modulePath] = modifiedModule;
}
return originalModule;
};
}
exports.makePatchingRequire = makePatchingRequire;
//# sourceMappingURL=patchRequire.js.map