import-in-the-middle 1.3.2 → 1.3.4
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 +39 -10
- package/package.json +3 -3
- package/test/fixtures/json.mjs +9 -0
- package/test/fixtures/something.json +3 -0
- package/test/hook/v18-static-import-assert.mjs +15 -0
- package/test/runtest +12 -0
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(
|
|
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,21 +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
|
-
|
|
49
|
-
return urlObj.protocol !== 'file:' ? 'file:' + urlObj.href : urlObj.href
|
|
50
|
-
}
|
|
51
|
-
return urlObj.protocol !== 'file:' && urlObj.protocol !== 'node:' && NODE_MAJOR < 18
|
|
52
|
-
? 'file:' + urlObj.href
|
|
53
|
-
: urlObj.href
|
|
74
|
+
return needsToAddFileProtocol(urlObj) ? 'file:' + urlObj.href : urlObj.href
|
|
54
75
|
}
|
|
55
76
|
|
|
56
77
|
export async function resolve (specifier, context, parentResolve) {
|
|
57
78
|
const { parentURL = '' } = context
|
|
58
|
-
const
|
|
59
|
-
|
|
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)
|
|
60
84
|
if (parentURL === '' && !EXTENSION_RE.test(url.url)) {
|
|
61
85
|
entrypoint = url.url
|
|
62
86
|
return { url: url.url, format: 'commonjs' }
|
|
@@ -66,6 +90,11 @@ export async function resolve (specifier, context, parentResolve) {
|
|
|
66
90
|
return url
|
|
67
91
|
}
|
|
68
92
|
|
|
93
|
+
if (context.importAssertions && context.importAssertions.type === 'json') {
|
|
94
|
+
return url
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
|
|
69
98
|
specifiers.set(url.url, specifier)
|
|
70
99
|
|
|
71
100
|
return {
|
package/package.json
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "import-in-the-middle",
|
|
3
|
-
"version": "1.3.
|
|
3
|
+
"version": "1.3.4",
|
|
4
4
|
"description": "Intercept imports in Node.js",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
7
|
-
"test": "c8 --check-coverage --lines
|
|
8
|
-
"test-win": "c8 --check-coverage --lines
|
|
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}/*",
|
|
9
9
|
"test:ts": "c8 imhotap --runner test/runtest --files test/typescript/*.test.mts",
|
|
10
10
|
"test-win:ts": "c8 imhotap --runner test\\runtest.bat --files test/typescript/*.test.mts",
|
|
11
11
|
"coverage": "c8 --reporter html imhotap --runner test/runtest --files test/{hook,low-level,other}/* && echo '\nNow open coverage/index.html\n'"
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
// Unless explicitly stated otherwise all files in this repository are licensed under the Apache 2.0 License.
|
|
2
|
+
//
|
|
3
|
+
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2021 Datadog, Inc.
|
|
4
|
+
|
|
5
|
+
import coolFile from './something.json' assert { type: 'json' };
|
|
6
|
+
|
|
7
|
+
export default {
|
|
8
|
+
data: coolFile.data
|
|
9
|
+
};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
// Unless explicitly stated otherwise all files in this repository are licensed under the Apache 2.0 License.
|
|
2
|
+
//
|
|
3
|
+
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2021 Datadog, Inc.
|
|
4
|
+
|
|
5
|
+
import Hook from '../../index.js'
|
|
6
|
+
import jsonMjs from '../fixtures/json.mjs'
|
|
7
|
+
import { strictEqual } from 'assert'
|
|
8
|
+
|
|
9
|
+
Hook((exports, name) => {
|
|
10
|
+
if (name.match(/json\.mjs/)) {
|
|
11
|
+
exports.default.data += '-dawg'
|
|
12
|
+
}
|
|
13
|
+
})
|
|
14
|
+
|
|
15
|
+
strictEqual(jsonMjs.data, 'dog-dawg')
|
package/test/runtest
CHANGED
|
@@ -15,6 +15,18 @@ const args = [
|
|
|
15
15
|
'--unhandled-rejections=strict',
|
|
16
16
|
...process.argv.slice(2)
|
|
17
17
|
]
|
|
18
|
+
|
|
19
|
+
const [processMajor] = process.versions.node.split('.').map(Number)
|
|
20
|
+
|
|
21
|
+
const match = filename.match(/v([0-9]+)/)
|
|
22
|
+
|
|
23
|
+
const versionRequirement = match ? match[1] : 0;
|
|
24
|
+
|
|
25
|
+
if (processMajor < versionRequirement) {
|
|
26
|
+
console.log(`skipping ${filename} as this is Node.js v${processMajor} and test wants v${versionRequirement}`);
|
|
27
|
+
process.exit(0);
|
|
28
|
+
}
|
|
29
|
+
|
|
18
30
|
if (!filename.includes('disabled')) {
|
|
19
31
|
const isTypescript = path.extname(filename).slice(-2) === 'ts'
|
|
20
32
|
const loaderPath = isTypescript
|