import-in-the-middle 1.11.3 → 1.12.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,3 +1,3 @@
1
1
  {
2
- ".": "1.11.3"
2
+ ".": "1.12.0"
3
3
  }
package/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  # Changelog
2
2
 
3
+ ## [1.12.0](https://github.com/nodejs/import-in-the-middle/compare/import-in-the-middle-v1.11.3...import-in-the-middle-v1.12.0) (2024-12-13)
4
+
5
+
6
+ ### Features
7
+
8
+ * Support absolute paths for `include` ([#168](https://github.com/nodejs/import-in-the-middle/issues/168)) ([d0d9bc3](https://github.com/nodejs/import-in-the-middle/commit/d0d9bc3d1e0bcef1094af58c15cf997507777067))
9
+ * Warn on multiple hook initialization ([#165](https://github.com/nodejs/import-in-the-middle/issues/165)) ([9bd539e](https://github.com/nodejs/import-in-the-middle/commit/9bd539ea6ff1684c8807bc30c8b68882cc9e057f))
10
+
3
11
  ## [1.11.3](https://github.com/nodejs/import-in-the-middle/compare/import-in-the-middle-v1.11.2...import-in-the-middle-v1.11.3) (2024-12-04)
4
12
 
5
13
 
package/hook.js CHANGED
@@ -2,7 +2,7 @@
2
2
  //
3
3
  // This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2021 Datadog, Inc.
4
4
 
5
- const { URL } = require('url')
5
+ const { URL, fileURLToPath } = require('url')
6
6
  const { inspect } = require('util')
7
7
  const { builtinModules } = require('module')
8
8
  const specifiers = new Map()
@@ -279,6 +279,12 @@ function createHook (meta) {
279
279
  let includeModules, excludeModules
280
280
 
281
281
  async function initialize (data) {
282
+ if (global.__import_in_the_middle_initialized__) {
283
+ process.emitWarning("The 'import-in-the-middle' hook has already been initialized")
284
+ }
285
+
286
+ global.__import_in_the_middle_initialized__ = true
287
+
282
288
  if (data) {
283
289
  includeModules = ensureArrayWithBareSpecifiersFileUrlsAndRegex(data.include, 'include')
284
290
  excludeModules = ensureArrayWithBareSpecifiersFileUrlsAndRegex(data.exclude, 'exclude')
@@ -335,7 +341,7 @@ function createHook (meta) {
335
341
  return each.test(result.url)
336
342
  }
337
343
 
338
- return each === specifier || each === result.url
344
+ return each === specifier || each === result.url || (result.url.startsWith('file:') && each === fileURLToPath(result.url))
339
345
  }
340
346
 
341
347
  if (includeModules && !includeModules.some(match)) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "import-in-the-middle",
3
- "version": "1.11.3",
3
+ "version": "1.12.0",
4
4
  "description": "Intercept imports in Node.js",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -0,0 +1,10 @@
1
+ import { strictEqual } from 'assert'
2
+ import * as foo from './foo.mjs'
3
+ import { dirname, join } from 'path'
4
+ import { fileURLToPath } from 'url'
5
+
6
+ const fooPath = join(dirname(fileURLToPath(import.meta.url)), 'foo.mjs')
7
+
8
+ strictEqual(typeof foo.foo, 'function')
9
+ strictEqual(global.hooked.length, 1)
10
+ strictEqual(global.hooked[0], fooPath)
@@ -0,0 +1,18 @@
1
+ import { register } from 'module'
2
+ import { Hook, createAddHookMessageChannel } from '../../index.js'
3
+ import { dirname, join } from 'path'
4
+ import { fileURLToPath } from 'url'
5
+
6
+ const fooPath = join(dirname(fileURLToPath(import.meta.url)), 'foo.mjs')
7
+
8
+ const { registerOptions, waitForAllMessagesAcknowledged } = createAddHookMessageChannel()
9
+
10
+ register('../../hook.mjs', import.meta.url, registerOptions)
11
+
12
+ global.hooked = []
13
+
14
+ Hook([fooPath], (_, name) => {
15
+ global.hooked.push(name)
16
+ })
17
+
18
+ await waitForAllMessagesAcknowledged()
@@ -0,0 +1,2 @@
1
+ console.log(`skipping ${process.env.IITM_TEST_FILE} as no native module is available for ${process.platform}-${process.arch}`)
2
+ process.exit(0)
@@ -0,0 +1,2 @@
1
+ console.log(`skipping ${process.env.IITM_TEST_FILE} as no native module is available for ${process.platform}-${process.arch}`)
2
+ process.exit(0)
@@ -0,0 +1,2 @@
1
+ console.log(`skipping ${process.env.IITM_TEST_FILE} as no native module is available for ${process.platform}-${process.arch}`)
2
+ process.exit(0)
@@ -0,0 +1,14 @@
1
+ import { spawnSync } from 'child_process'
2
+
3
+ const out = spawnSync(process.execPath,
4
+ ['--import', './test/fixtures/import-absolute.mjs', './test/fixtures/import-absolute-after.mjs'],
5
+ { stdio: 'inherit', env: {} }
6
+ )
7
+
8
+ if (out.error) {
9
+ console.error(out.error)
10
+ }
11
+ if (out.status !== 0) {
12
+ console.error(`Expected exit code 0, got ${out.status}`)
13
+ }
14
+ process.exit(out.status)