import-in-the-middle 1.3.0 → 1.3.3

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.
package/hook.mjs CHANGED
@@ -3,12 +3,15 @@
3
3
  // This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2021 Datadog, Inc.
4
4
 
5
5
  const specifiers = new Map()
6
+ const isWin = process.platform === "win32"
7
+
6
8
 
7
9
  // FIXME: Typescript extensions are added temporarily until we find a better
8
10
  // way of supporting arbitrary extensions
9
11
  const EXTENSION_RE = /\.(js|mjs|cjs|ts|mts|cts)$/
10
-
11
- const NODE_MAJOR = Number(process.versions.node.split('.')[0])
12
+ const NODE_VERSION = process.versions.node.split('.')
13
+ const NODE_MAJOR = Number(NODE_VERSION[0])
14
+ const NODE_MINOR = Number(NODE_VERSION[1])
12
15
 
13
16
  let entrypoint
14
17
 
@@ -42,17 +45,42 @@ function deleteIitm (url) {
42
45
  return resultUrl
43
46
  }
44
47
 
48
+ function isNode16AndBiggerOrEqualsThan16_17_0() {
49
+ return NODE_MAJOR === 16 && NODE_MINOR >= 17
50
+ }
51
+
52
+ function isFileProtocol (urlObj) {
53
+ return urlObj.protocol === 'file:'
54
+ }
55
+
56
+ function isNodeProtocol (urlObj) {
57
+ return urlObj.protocol === 'node:'
58
+ }
59
+
60
+ function needsToAddFileProtocol(urlObj) {
61
+ if (NODE_MAJOR === 17) {
62
+ return !isFileProtocol(urlObj)
63
+ }
64
+ if (isNode16AndBiggerOrEqualsThan16_17_0()) {
65
+ return !isFileProtocol(urlObj) && !isNodeProtocol(urlObj)
66
+ }
67
+ return !isFileProtocol(urlObj) && NODE_MAJOR < 18
68
+ }
69
+
70
+
45
71
  function addIitm (url) {
46
72
  const urlObj = new URL(url)
47
73
  urlObj.searchParams.set('iitm', 'true')
48
- return urlObj.protocol !== 'file:' && NODE_MAJOR < 18 ? 'file:' + urlObj.href : urlObj.href
74
+ return needsToAddFileProtocol(urlObj) ? 'file:' + urlObj.href : urlObj.href
49
75
  }
50
76
 
51
77
  export async function resolve (specifier, context, parentResolve) {
52
78
  const { parentURL = '' } = context
53
-
54
- const url = await parentResolve(deleteIitm(specifier), context, parentResolve)
55
-
79
+ const newSpecifier = deleteIitm(specifier)
80
+ if (isWin && parentURL.indexOf('file:node') === 0) {
81
+ context.parentURL = ''
82
+ }
83
+ const url = await parentResolve(newSpecifier, context, parentResolve)
56
84
  if (parentURL === '' && !EXTENSION_RE.test(url.url)) {
57
85
  entrypoint = url.url
58
86
  return { url: url.url, format: 'commonjs' }
package/index.js CHANGED
@@ -51,7 +51,11 @@ function Hook(modules, options, hookFn) {
51
51
  if (isBuiltin) {
52
52
  name = name.replace(/^node:/, '')
53
53
  } else {
54
- name = name.replace(/^file:\/\//, '')
54
+ if (name.startsWith('file://')) {
55
+ try {
56
+ name = fileURLToPath(name)
57
+ } catch (e) {}
58
+ }
55
59
  const details = parse(name)
56
60
  if (details) {
57
61
  name = details.name
package/package.json CHANGED
@@ -1,11 +1,13 @@
1
1
  {
2
2
  "name": "import-in-the-middle",
3
- "version": "1.3.0",
3
+ "version": "1.3.3",
4
4
  "description": "Intercept imports in Node.js",
5
5
  "main": "index.js",
6
6
  "scripts": {
7
- "test": "c8 --check-coverage --lines 90 imhotap --runner test/runtest --files test/{hook,low-level,other}/*",
7
+ "test": "c8 --check-coverage --lines 88 imhotap --runner test/runtest --files test/{hook,low-level,other}/*",
8
+ "test-win": "c8 --check-coverage --lines 88 imhotap --runner test\\runtest.bat --files test/{hook,low-level,other}/*",
8
9
  "test:ts": "c8 imhotap --runner test/runtest --files test/typescript/*.test.mts",
10
+ "test-win:ts": "c8 imhotap --runner test\\runtest.bat --files test/typescript/*.test.mts",
9
11
  "coverage": "c8 --reporter html imhotap --runner test/runtest --files test/{hook,low-level,other}/* && echo '\nNow open coverage/index.html\n'"
10
12
  },
11
13
  "repository": {
@@ -7,7 +7,7 @@ import { strictEqual } from 'assert'
7
7
  const c8Dir = path.join(path.dirname(fileURLToPath(import.meta.url)), '..', '..', 'node_modules', 'c8')
8
8
 
9
9
  Hook(['c8'], { internals: true }, (exports, name, baseDir) => {
10
- strictEqual(name, 'c8/index.js')
10
+ strictEqual(name, path.join('c8','index.js'))
11
11
  strictEqual(baseDir, c8Dir)
12
12
  exports.Report = () => 42
13
13
  })
package/test/runtest CHANGED
@@ -6,6 +6,7 @@
6
6
 
7
7
  const { spawn } = require('child_process')
8
8
  const path = require('path')
9
+ const url = require('url')
9
10
 
10
11
  process.env.NODE_NO_WARNINGS = 1
11
12
 
@@ -20,7 +21,7 @@ if (!filename.includes('disabled')) {
20
21
  ? path.join(__dirname, 'typescript', 'iitm-ts-node-loader.mjs')
21
22
  : path.join(__dirname, '..', 'hook.mjs')
22
23
 
23
- args.unshift(`--experimental-loader=${loaderPath}`)
24
+ args.unshift(`--experimental-loader=${url.pathToFileURL(loaderPath)}`)
24
25
  }
25
26
 
26
27
  spawn('node', args, { stdio: 'inherit' }).on('close', code => {
@@ -0,0 +1 @@
1
+ node test/runtest %*