Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

address PR feedback #9

Merged
merged 5 commits into from
May 3, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@

import * as types from '../../types';
import * as path from 'path';
import * as util from 'util';
import { types as utilTypes } from 'util';
import { satisfies } from 'semver';
import * as shimmer from 'shimmer';
import { wrap, unwrap, massWrap, massUnwrap } from 'shimmer';
import { InstrumentationAbstract } from '../../instrumentation';
import {
RequireInTheMiddleSingleton,
Hooked,
} from './RequireInTheMiddleSingleton';
import { HookFn } from 'import-in-the-middle';
import type { HookFn } from 'import-in-the-middle';
import * as ImportInTheMiddle from 'import-in-the-middle';
import { InstrumentationModuleDefinition } from './types';
import { diag } from '@opentelemetry/api';
Expand Down Expand Up @@ -72,44 +72,29 @@ export abstract class InstrumentationBase<T = any>
}
}

protected override _wrap: typeof shimmer.wrap = (
moduleExports,
name,
wrapper
) => {
if (!util.types.isProxy(moduleExports)) {
return shimmer.wrap(moduleExports, name, wrapper);
protected override _wrap: typeof wrap = (moduleExports, name, wrapper) => {
if (!utilTypes.isProxy(moduleExports)) {
return wrap(moduleExports, name, wrapper);
} else {
return this._wrapEsm(moduleExports, name, wrapper);
const wrapped = wrap(Object.assign({}, moduleExports), name, wrapper);

return Object.defineProperty(moduleExports, name, {
value: wrapped,
});
}
};

protected override _unwrap: typeof shimmer.unwrap = (moduleExports, name) => {
if (!util.types.isProxy(moduleExports)) {
return shimmer.unwrap(moduleExports, name);
protected override _unwrap: typeof unwrap = (moduleExports, name) => {
if (!utilTypes.isProxy(moduleExports)) {
return unwrap(moduleExports, name);
} else {
return this._unwrapEsm(moduleExports, name);
return Object.defineProperty(moduleExports, name, {
value: moduleExports[name],
});
}
};

private _wrapEsm: typeof shimmer.wrap = (moduleExports, name, wrapper) => {
const wrapped = shimmer.wrap(
Object.assign({}, moduleExports),
name,
wrapper
);
Object.defineProperty(moduleExports, name, {
value: wrapped,
});
};

private _unwrapEsm: typeof shimmer.unwrap = (moduleExports, name) => {
Object.defineProperty(moduleExports, name, {
value: moduleExports[name],
});
};

protected override _massWrap: typeof shimmer.massWrap = (
protected override _massWrap: typeof massWrap = (
moduleExportsArray,
names,
wrapper
Expand All @@ -133,7 +118,7 @@ export abstract class InstrumentationBase<T = any>
});
};

protected override _massUnwrap: typeof shimmer.massUnwrap = (
protected override _massUnwrap: typeof massUnwrap = (
moduleExportsArray,
names
) => {
Expand Down Expand Up @@ -256,7 +241,7 @@ export abstract class InstrumentationBase<T = any>

this._warnOnPreloadedModules();
for (const module of this._modules) {
const hookFn: ImportInTheMiddle.HookFn = (exports, name, baseDir) => {
const hookFn: HookFn = (exports, name, baseDir) => {
return this._onRequire<typeof exports>(
module as unknown as InstrumentationModuleDefinition<typeof exports>,
exports,
Expand Down