From 32ae641ad13be8786cce9ffc3942efa385c0ab33 Mon Sep 17 00:00:00 2001 From: Trent Mick Date: Tue, 2 May 2023 00:43:13 -0700 Subject: [PATCH] fix(deps): update dependency require-in-the-middle to v7.1.0 for types and named export (#3727) Co-authored-by: Daniel Dyla --- experimental/CHANGELOG.md | 1 + .../package.json | 2 +- .../node/RequireInTheMiddleSingleton.ts | 14 ++++---- .../src/platform/node/instrumentation.ts | 16 ++++----- .../platform/node/require-in-the-middle.d.ts | 36 ------------------- .../node/RequireInTheMiddleSingleton.test.ts | 4 +-- 6 files changed, 17 insertions(+), 56 deletions(-) delete mode 100644 experimental/packages/opentelemetry-instrumentation/src/platform/node/require-in-the-middle.d.ts diff --git a/experimental/CHANGELOG.md b/experimental/CHANGELOG.md index 34d7727835..02d6ab567a 100644 --- a/experimental/CHANGELOG.md +++ b/experimental/CHANGELOG.md @@ -16,6 +16,7 @@ All notable changes to experimental packages in this project will be documented ### :bug: (Bug Fix) +* fix(instrumentation): update `require-in-the-middle` to v7.1.0 [#3727](https://github.com/open-telemetry/opentelemetry-js/pull/3727) @trentm * fix(instrumentation): update `require-in-the-middle` to v7.0.1 [#3743](https://github.com/open-telemetry/opentelemetry-js/pull/3743) @trentm ### :books: (Refine Doc) diff --git a/experimental/packages/opentelemetry-instrumentation/package.json b/experimental/packages/opentelemetry-instrumentation/package.json index 04d913ae86..266198f5c5 100644 --- a/experimental/packages/opentelemetry-instrumentation/package.json +++ b/experimental/packages/opentelemetry-instrumentation/package.json @@ -68,7 +68,7 @@ "url": "https://github.com/open-telemetry/opentelemetry-js/issues" }, "dependencies": { - "require-in-the-middle": "^7.0.1", + "require-in-the-middle": "^7.1.0", "semver": "^7.3.2", "shimmer": "^1.2.1" }, diff --git a/experimental/packages/opentelemetry-instrumentation/src/platform/node/RequireInTheMiddleSingleton.ts b/experimental/packages/opentelemetry-instrumentation/src/platform/node/RequireInTheMiddleSingleton.ts index 0bcea53ea8..ca74699e90 100644 --- a/experimental/packages/opentelemetry-instrumentation/src/platform/node/RequireInTheMiddleSingleton.ts +++ b/experimental/packages/opentelemetry-instrumentation/src/platform/node/RequireInTheMiddleSingleton.ts @@ -14,13 +14,14 @@ * limitations under the License. */ -import * as RequireInTheMiddle from 'require-in-the-middle'; +import type { OnRequireFn } from 'require-in-the-middle'; +import { Hook } from 'require-in-the-middle'; import * as path from 'path'; import { ModuleNameTrie, ModuleNameSeparator } from './ModuleNameTrie'; export type Hooked = { moduleName: string; - onRequire: RequireInTheMiddle.OnRequireFn; + onRequire: OnRequireFn; }; /** @@ -59,7 +60,7 @@ export class RequireInTheMiddleSingleton { } private _initialize() { - RequireInTheMiddle( + new Hook( // Intercept all `require` calls; we will filter the matching ones below null, { internals: true }, @@ -88,13 +89,10 @@ export class RequireInTheMiddleSingleton { * Register a hook with `require-in-the-middle` * * @param {string} moduleName Module name - * @param {RequireInTheMiddle.OnRequireFn} onRequire Hook function + * @param {OnRequireFn} onRequire Hook function * @returns {Hooked} Registered hook */ - register( - moduleName: string, - onRequire: RequireInTheMiddle.OnRequireFn - ): Hooked { + register(moduleName: string, onRequire: OnRequireFn): Hooked { const hooked = { moduleName, onRequire }; this._moduleNameTrie.insert(hooked); return hooked; diff --git a/experimental/packages/opentelemetry-instrumentation/src/platform/node/instrumentation.ts b/experimental/packages/opentelemetry-instrumentation/src/platform/node/instrumentation.ts index 93fbb4c176..5790c9564e 100644 --- a/experimental/packages/opentelemetry-instrumentation/src/platform/node/instrumentation.ts +++ b/experimental/packages/opentelemetry-instrumentation/src/platform/node/instrumentation.ts @@ -24,7 +24,8 @@ import { } from './RequireInTheMiddleSingleton'; import { InstrumentationModuleDefinition } from './types'; import { diag } from '@opentelemetry/api'; -import * as RequireInTheMiddle from 'require-in-the-middle'; +import type { OnRequireFn } from 'require-in-the-middle'; +import { Hook } from 'require-in-the-middle'; /** * Base abstract class for instrumenting node plugins @@ -34,7 +35,7 @@ export abstract class InstrumentationBase implements types.Instrumentation { private _modules: InstrumentationModuleDefinition[]; - private _hooks: (Hooked | RequireInTheMiddle.Hooked)[] = []; + private _hooks: (Hooked | Hook)[] = []; private _requireInTheMiddleSingleton: RequireInTheMiddleSingleton = RequireInTheMiddleSingleton.getInstance(); private _enabled = false; @@ -167,11 +168,7 @@ export abstract class InstrumentationBase this._warnOnPreloadedModules(); for (const module of this._modules) { - const onRequire: RequireInTheMiddle.OnRequireFn = ( - exports, - name, - baseDir - ) => { + const onRequire: OnRequireFn = (exports, name, baseDir) => { return this._onRequire( module as unknown as InstrumentationModuleDefinition, exports, @@ -181,9 +178,10 @@ export abstract class InstrumentationBase }; // `RequireInTheMiddleSingleton` does not support absolute paths. - // For an absolute paths, we must create a separate instance of `RequireInTheMiddle`. + // For an absolute paths, we must create a separate instance of the + // require-in-the-middle `Hook`. const hook = path.isAbsolute(module.name) - ? RequireInTheMiddle([module.name], { internals: true }, onRequire) + ? new Hook([module.name], { internals: true }, onRequire) : this._requireInTheMiddleSingleton.register(module.name, onRequire); this._hooks.push(hook); diff --git a/experimental/packages/opentelemetry-instrumentation/src/platform/node/require-in-the-middle.d.ts b/experimental/packages/opentelemetry-instrumentation/src/platform/node/require-in-the-middle.d.ts deleted file mode 100644 index 93a766b5fb..0000000000 --- a/experimental/packages/opentelemetry-instrumentation/src/platform/node/require-in-the-middle.d.ts +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright The OpenTelemetry Authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -declare module 'require-in-the-middle' { - namespace hook { - type Options = { - internals?: boolean; - }; - type OnRequireFn = (exports: T, name: string, basedir?: string) => T; - type Hooked = { unhook(): void }; - } - function hook( - modules: string[] | null, - options: hook.Options | null, - onRequire: hook.OnRequireFn - ): hook.Hooked; - function hook( - modules: string[] | null, - onRequire: hook.OnRequireFn - ): hook.Hooked; - function hook(onRequire: hook.OnRequireFn): hook.Hooked; - export = hook; -} diff --git a/experimental/packages/opentelemetry-instrumentation/test/node/RequireInTheMiddleSingleton.test.ts b/experimental/packages/opentelemetry-instrumentation/test/node/RequireInTheMiddleSingleton.test.ts index df7059987c..d6401d1faa 100644 --- a/experimental/packages/opentelemetry-instrumentation/test/node/RequireInTheMiddleSingleton.test.ts +++ b/experimental/packages/opentelemetry-instrumentation/test/node/RequireInTheMiddleSingleton.test.ts @@ -17,7 +17,7 @@ import * as assert from 'assert'; import * as sinon from 'sinon'; import * as path from 'path'; -import * as RequireInTheMiddle from 'require-in-the-middle'; +import type { OnRequireFn } from 'require-in-the-middle'; import { RequireInTheMiddleSingleton } from '../../src/platform/node/RequireInTheMiddleSingleton'; const requireInTheMiddleSingleton = RequireInTheMiddleSingleton.getInstance(); @@ -31,7 +31,7 @@ const makeOnRequiresStub = (label: string): sinon.SinonStub => exports.__ritmOnRequires ??= []; exports.__ritmOnRequires.push(label); return exports; - }) as RequireInTheMiddle.OnRequireFn); + }) as OnRequireFn); describe('RequireInTheMiddleSingleton', () => { describe('register', () => {