import-in-the-middle 1.11.3 → 1.13.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.13.0"
3
3
  }
package/CHANGELOG.md CHANGED
@@ -1,5 +1,20 @@
1
1
  # Changelog
2
2
 
3
+ ## [1.13.0](https://github.com/nodejs/import-in-the-middle/compare/import-in-the-middle-v1.12.0...import-in-the-middle-v1.13.0) (2025-02-06)
4
+
5
+
6
+ ### Features
7
+
8
+ * Support import attributes ([#176](https://github.com/nodejs/import-in-the-middle/issues/176)) ([916af26](https://github.com/nodejs/import-in-the-middle/commit/916af2627e0e8cb6d50a3b54c1a280dc16e20925))
9
+
10
+ ## [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)
11
+
12
+
13
+ ### Features
14
+
15
+ * 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))
16
+ * 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))
17
+
3
18
  ## [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
19
 
5
20
 
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)) {
@@ -1,14 +1,14 @@
1
1
  'use strict'
2
2
 
3
3
  const { Parser } = require('acorn')
4
- const { importAssertions } = require('acorn-import-attributes')
4
+ const { importAttributesOrAssertions } = require('acorn-import-attributes')
5
5
 
6
6
  const acornOpts = {
7
7
  ecmaVersion: 'latest',
8
8
  sourceType: 'module'
9
9
  }
10
10
 
11
- const parser = Parser.extend(importAssertions)
11
+ const parser = Parser.extend(importAttributesOrAssertions)
12
12
 
13
13
  function warn (txt) {
14
14
  process.emitWarning(txt, 'get-esm-exports')
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.13.0",
4
4
  "description": "Intercept imports in Node.js",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -56,7 +56,7 @@
56
56
  "vue": "^3.4.31"
57
57
  },
58
58
  "dependencies": {
59
- "acorn": "^8.8.2",
59
+ "acorn": "^8.14.0",
60
60
  "acorn-import-attributes": "^1.9.5",
61
61
  "cjs-module-lexer": "^1.2.2",
62
62
  "module-details-from-path": "^1.0.3"
@@ -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)
@@ -2,16 +2,14 @@
2
2
  //
3
3
  // This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2021 Datadog, Inc.
4
4
 
5
+ import Hook from '../../index.js'
5
6
  import jsonMjs from '../fixtures/json-attributes.mjs'
6
7
  import { strictEqual } from 'assert'
7
8
 
8
- // Acorn does not support import attributes so an error is logged but the import
9
- // still works!
10
- //
11
- // Hook((exports, name) => {
12
- // if (name.match(/json\.mjs/)) {
13
- // exports.default.data += '-dawg'
14
- // }
15
- // })
9
+ Hook((exports, name) => {
10
+ if (name.match(/json-attributes\.mjs/)) {
11
+ exports.default.data += '-dawg'
12
+ }
13
+ })
16
14
 
17
- strictEqual(jsonMjs.data, 'dog')
15
+ strictEqual(jsonMjs.data, 'dog-dawg')
@@ -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)