import-in-the-middle 1.1.1 → 1.2.1

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
@@ -4,39 +4,53 @@
4
4
 
5
5
  const specifiers = new Map()
6
6
 
7
+ const EXTENSION_RE = /\.(js|mjs|cjs)$/
8
+
9
+ let entrypoint
10
+
7
11
  export async function resolve (specifier, context, parentResolve) {
8
12
  const { parentURL = '' } = context
9
13
 
10
- if (specifier.startsWith('iitm:')) {
11
- specifier = specifier.replace('iitm:', '')
14
+ if (specifier.startsWith('file:iitm:')) {
15
+ specifier = specifier.replace('file:iitm:', '')
12
16
  }
13
17
  const url = await parentResolve(specifier, context, parentResolve)
14
18
 
15
- if (parentURL === import.meta.url || parentURL.startsWith('iitm:')) {
19
+ if (parentURL === '' && !EXTENSION_RE.test(url.url)) {
20
+ entrypoint = url.url
21
+ return { url: url.url, format: 'commonjs' }
22
+ }
23
+
24
+ if (parentURL === import.meta.url || parentURL.startsWith('file:iitm:')) {
16
25
  return url
17
26
  }
18
27
 
19
28
  specifiers.set(url.url, specifier)
20
29
 
21
30
  return {
22
- url: `iitm:${url.url}`
31
+ url: `file:iitm:${url.url}`
23
32
  }
24
33
  }
25
34
 
26
35
  export function getFormat (url, context, parentGetFormat) {
27
- if (url.startsWith('iitm:')) {
36
+ if (url.startsWith('file:iitm:')) {
28
37
  return {
29
38
  format: 'module'
30
39
  }
31
40
  }
41
+ if (url === entrypoint) {
42
+ return {
43
+ format: 'commonjs'
44
+ }
45
+ }
32
46
 
33
47
  return parentGetFormat(url, context, parentGetFormat)
34
48
  }
35
49
 
36
50
  const iitmURL = new URL('lib/register.js', import.meta.url).toString()
37
51
  export async function getSource (url, context, parentGetSource) {
38
- if (url.startsWith('iitm:')) {
39
- const realUrl = url.replace('iitm:', '')
52
+ if (url.startsWith('file:iitm:')) {
53
+ const realUrl = url.replace('file:iitm:', '')
40
54
  const realModule = await import(realUrl)
41
55
  const exportNames = Object.keys(realModule)
42
56
  return {
@@ -59,3 +73,17 @@ register('${realUrl}', namespace, set, '${specifiers.get(realUrl)}')
59
73
 
60
74
  return parentGetSource(url, context, parentGetSource)
61
75
  }
76
+
77
+ // For Node.js 16.12.0 and higher.
78
+ export async function load (url, context, parentLoad) {
79
+ if (url.startsWith('file:iitm:')) {
80
+ const { source } = await getSource(url, context)
81
+ return {
82
+ source,
83
+ format: 'module'
84
+ }
85
+ }
86
+
87
+ return parentLoad(url, context, parentLoad)
88
+ }
89
+
package/package.json CHANGED
@@ -1,11 +1,11 @@
1
1
  {
2
2
  "name": "import-in-the-middle",
3
- "version": "1.1.1",
3
+ "version": "1.2.1",
4
4
  "description": "Intercept imports in Node.js",
5
5
  "main": "index.js",
6
6
  "scripts": {
7
- "test": "c8 --check-coverage --lines 100 imhotap --runner test/runtest --files test/{hook,low-level}/*.*js",
8
- "coverage": "c8 --reporter html imhotap --runner test/runtest --files test/{hook,low-level}/*.*js && echo '\nNow open coverage/index.html\n'"
7
+ "test": "c8 --check-coverage --lines 90 imhotap --runner test/runtest --files test/{hook,low-level,other}/*",
8
+ "coverage": "c8 --reporter html imhotap --runner test/runtest --files test/{hook,low-level,other}/* && echo '\nNow open coverage/index.html\n'"
9
9
  },
10
10
  "repository": {
11
11
  "type": "git",
@@ -27,7 +27,7 @@
27
27
  "homepage": "https://github.com/DataDog/import-in-the-middle#readme",
28
28
  "devDependencies": {
29
29
  "c8": "^7.8.0",
30
- "imhotap": "^1.1.0"
30
+ "imhotap": "^2.0.0"
31
31
  },
32
32
  "dependencies": {
33
33
  "module-details-from-path": "^1.0.3"
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env node
2
+ // Unless explicitly stated otherwise all files in this repository are licensed under the Apache 2.0 License.
3
+ //
4
+ // This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2021 Datadog, Inc.
5
+
6
+ // This script doesn't actually have to do anything.
@@ -0,0 +1,11 @@
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 { rejects } from 'assert'
6
+ (async () => {
7
+ await rejects(() => import('./executable'), {
8
+ name: 'TypeError',
9
+ code: 'ERR_UNKNOWN_FILE_EXTENSION'
10
+ })
11
+ })()