import-in-the-middle 1.13.0 → 1.13.2

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.13.0"
2
+ ".": "1.13.2"
3
3
  }
package/CHANGELOG.md CHANGED
@@ -1,5 +1,21 @@
1
1
  # Changelog
2
2
 
3
+ ## [1.13.2](https://github.com/nodejs/import-in-the-middle/compare/import-in-the-middle-v1.13.1...import-in-the-middle-v1.13.2) (2025-05-12)
4
+
5
+
6
+ ### Bug Fixes
7
+
8
+ * Don't attempt to wrap TypeScript modules ([#191](https://github.com/nodejs/import-in-the-middle/issues/191)) ([6deb87e](https://github.com/nodejs/import-in-the-middle/commit/6deb87ea069ec2ee749ce2297ea47ce071d18cf9))
9
+
10
+ ## [1.13.1](https://github.com/nodejs/import-in-the-middle/compare/import-in-the-middle-v1.13.0...import-in-the-middle-v1.13.1) (2025-02-28)
11
+
12
+
13
+ ### Bug Fixes
14
+
15
+ * handling of circular dependencies ([#181](https://github.com/nodejs/import-in-the-middle/issues/181)) ([b58092e](https://github.com/nodejs/import-in-the-middle/commit/b58092ec9becf4a14f541da4cf5bfb190f2a9a9b))
16
+ * importing JSON files ([#182](https://github.com/nodejs/import-in-the-middle/issues/182)) ([8c52014](https://github.com/nodejs/import-in-the-middle/commit/8c52014658fcf698cc340d032b441d9e7a65be36))
17
+ * warning from use of context.importAssertions ([#179](https://github.com/nodejs/import-in-the-middle/issues/179)) ([8e56cf1](https://github.com/nodejs/import-in-the-middle/commit/8e56cf1e89752e6c8768d648c10c12fb3178e2ae))
18
+
3
19
  ## [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
20
 
5
21
 
package/hook.js CHANGED
@@ -14,6 +14,7 @@ const EXTENSION_RE = /\.(js|mjs|cjs|ts|mts|cts)$/
14
14
  const NODE_VERSION = process.versions.node.split('.')
15
15
  const NODE_MAJOR = Number(NODE_VERSION[0])
16
16
  const NODE_MINOR = Number(NODE_VERSION[1])
17
+ const HANDLED_FORMATS = new Set(['builtin', 'module', 'commonjs'])
17
18
 
18
19
  let entrypoint
19
20
 
@@ -253,7 +254,12 @@ async function processModule ({ srcUrl, context, parentGetSource, parentResolve,
253
254
  }
254
255
  } else {
255
256
  addSetter(n, `
256
- let $${n} = _.${n}
257
+ let $${n}
258
+ try {
259
+ $${n} = _.${n} = namespace.${n}
260
+ } catch (err) {
261
+ if (!(err instanceof ReferenceError)) throw err
262
+ }
257
263
  export { $${n} as ${n} }
258
264
  set.${n} = (v) => {
259
265
  $${n} = v
@@ -344,6 +350,10 @@ function createHook (meta) {
344
350
  return each === specifier || each === result.url || (result.url.startsWith('file:') && each === fileURLToPath(result.url))
345
351
  }
346
352
 
353
+ if (result.format && !HANDLED_FORMATS.has(result.format)) {
354
+ return result
355
+ }
356
+
347
357
  if (includeModules && !includeModules.some(match)) {
348
358
  return result
349
359
  }
@@ -362,10 +372,8 @@ function createHook (meta) {
362
372
  }
363
373
 
364
374
  // Node.js v21 renames importAssertions to importAttributes
365
- if (
366
- (context.importAssertions && context.importAssertions.type === 'json') ||
367
- (context.importAttributes && context.importAttributes.type === 'json')
368
- ) {
375
+ const importAttributes = context.importAttributes || context.importAssertions
376
+ if (importAttributes && importAttributes.type === 'json') {
369
377
  return result
370
378
  }
371
379
 
@@ -404,10 +412,7 @@ import { register } from '${iitmURL}'
404
412
  import * as namespace from ${JSON.stringify(realUrl)}
405
413
 
406
414
  // Mimic a Module object (https://tc39.es/ecma262/#sec-module-namespace-objects).
407
- const _ = Object.assign(
408
- Object.create(null, { [Symbol.toStringTag]: { value: 'Module' } }),
409
- namespace
410
- )
415
+ const _ = Object.create(null, { [Symbol.toStringTag]: { value: 'Module' } })
411
416
  const set = {}
412
417
  const get = {}
413
418
 
@@ -49,7 +49,7 @@ async function getCjsExports (url, context, parentLoad, source) {
49
49
  // Resolve the re-exported module relative to the current module.
50
50
  const newUrl = pathToFileURL(require.resolve(re, { paths: [dirname(fileURLToPath(url))] })).href
51
51
 
52
- if (newUrl.endsWith('.node')) {
52
+ if (newUrl.endsWith('.node') || newUrl.endsWith('.json')) {
53
53
  return
54
54
  }
55
55
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "import-in-the-middle",
3
- "version": "1.13.0",
3
+ "version": "1.13.2",
4
4
  "description": "Intercept imports in Node.js",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -50,7 +50,7 @@
50
50
  "eslint-plugin-promise": "^6.1.1",
51
51
  "got": "^14.3.0",
52
52
  "imhotap": "^2.1.0",
53
- "openai": "^4.47.2",
53
+ "openai": "4.47.2",
54
54
  "ts-node": "^10.9.1",
55
55
  "typescript": "^4.7.4",
56
56
  "vue": "^3.4.31"
@@ -0,0 +1,5 @@
1
+ import { bar } from './circular-b.mjs'
2
+
3
+ export const foo = 1
4
+
5
+ export { bar }
@@ -0,0 +1,3 @@
1
+ import { foo } from './circular-a.mjs'
2
+
3
+ export const bar = foo + 1
@@ -0,0 +1 @@
1
+ module.exports = require('./something.json')
@@ -0,0 +1,7 @@
1
+ export type { Debugger } from 'node:inspector'
2
+
3
+ export const foo: number = 42
4
+
5
+ export function bar(): number {
6
+ return foo
7
+ }
@@ -1,7 +1,8 @@
1
1
  import Hook from '../../index.js'
2
2
  import foo from '../fixtures/re-export-cjs-built-in.js'
3
3
  import foo2 from '../fixtures/re-export-cjs.js'
4
- import { strictEqual } from 'assert'
4
+ import foo3 from '../fixtures/re-export-cjs-json.js'
5
+ import { deepStrictEqual, strictEqual } from 'assert'
5
6
 
6
7
  Hook((exports, name) => {
7
8
  if (name.endsWith('fixtures/re-export-cjs-built-in.js')) {
@@ -13,7 +14,13 @@ Hook((exports, name) => {
13
14
  strictEqual(exports.default, 'bar')
14
15
  exports.default = '2'
15
16
  }
17
+
18
+ if (name.endsWith('fixtures/re-export-cjs-json.js')) {
19
+ deepStrictEqual(exports.default, { data: 'dog' })
20
+ exports.default = '3'
21
+ }
16
22
  })
17
23
 
18
24
  strictEqual(foo, '1')
19
25
  strictEqual(foo2, '2')
26
+ strictEqual(foo3, '3')
@@ -0,0 +1,8 @@
1
+ import { strictEqual } from 'assert'
2
+
3
+ const mod = await import('../fixtures/something.mts')
4
+
5
+ strictEqual(mod.foo, 42)
6
+ strictEqual(typeof mod.bar, 'function')
7
+ strictEqual(mod.bar(), 42)
8
+ strictEqual(Object.keys(mod).length, 2)
@@ -0,0 +1,17 @@
1
+ import { register } from 'module'
2
+ import Hook from '../../index.js'
3
+ import { strictEqual } from 'assert'
4
+
5
+ register('../../hook.mjs', import.meta.url)
6
+
7
+ let bar
8
+
9
+ Hook((exports, name) => {
10
+ if (name.match(/circular-b.mjs/)) {
11
+ bar = exports.bar
12
+ }
13
+ })
14
+
15
+ await import('../fixtures/circular-b.mjs')
16
+
17
+ strictEqual(bar, 2)