import-in-the-middle 1.11.0 → 1.11.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.
- package/.eslintrc.yaml +3 -0
- package/.release-please-manifest.json +1 -1
- package/CHANGELOG.md +14 -0
- package/hook.js +3 -1
- package/lib/register.js +15 -1
- package/package.json +1 -1
- package/test/hook/v14-double-hook.mjs +33 -0
package/.eslintrc.yaml
CHANGED
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,19 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## [1.11.2](https://github.com/nodejs/import-in-the-middle/compare/import-in-the-middle-v1.11.1...import-in-the-middle-v1.11.2) (2024-09-30)
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
### Bug Fixes
|
|
7
|
+
|
|
8
|
+
* do nothing if target does not exist in getters map ([#155](https://github.com/nodejs/import-in-the-middle/issues/155)) ([5f6be49](https://github.com/nodejs/import-in-the-middle/commit/5f6be494fc11caf8dcf900807c5b6b646fcd8d74))
|
|
9
|
+
|
|
10
|
+
## [1.11.1](https://github.com/nodejs/import-in-the-middle/compare/import-in-the-middle-v1.11.0...import-in-the-middle-v1.11.1) (2024-09-26)
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
### Bug Fixes
|
|
14
|
+
|
|
15
|
+
* Support Hooking multiple times ([#153](https://github.com/nodejs/import-in-the-middle/issues/153)) ([e0d8080](https://github.com/nodejs/import-in-the-middle/commit/e0d808041eff228f4b4519454f7eea8f0930238a))
|
|
16
|
+
|
|
3
17
|
## [1.11.0](https://github.com/nodejs/import-in-the-middle/compare/import-in-the-middle-v1.10.0...import-in-the-middle-v1.11.0) (2024-07-29)
|
|
4
18
|
|
|
5
19
|
|
package/hook.js
CHANGED
|
@@ -259,6 +259,7 @@ async function processModule ({ srcUrl, context, parentGetSource, parentResolve,
|
|
|
259
259
|
$${n} = v
|
|
260
260
|
return true
|
|
261
261
|
}
|
|
262
|
+
get.${n} = () => $${n}
|
|
262
263
|
`)
|
|
263
264
|
}
|
|
264
265
|
}
|
|
@@ -402,10 +403,11 @@ const _ = Object.assign(
|
|
|
402
403
|
namespace
|
|
403
404
|
)
|
|
404
405
|
const set = {}
|
|
406
|
+
const get = {}
|
|
405
407
|
|
|
406
408
|
${Array.from(setters.values()).join('\n')}
|
|
407
409
|
|
|
408
|
-
register(${JSON.stringify(realUrl)}, _, set, ${JSON.stringify(specifiers.get(realUrl))})
|
|
410
|
+
register(${JSON.stringify(realUrl)}, _, set, get, ${JSON.stringify(specifiers.get(realUrl))})
|
|
409
411
|
`
|
|
410
412
|
}
|
|
411
413
|
} catch (cause) {
|
package/lib/register.js
CHANGED
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
|
|
5
5
|
const importHooks = [] // TODO should this be a Set?
|
|
6
6
|
const setters = new WeakMap()
|
|
7
|
+
const getters = new WeakMap()
|
|
7
8
|
const specifiers = new Map()
|
|
8
9
|
const toHook = []
|
|
9
10
|
|
|
@@ -12,6 +13,18 @@ const proxyHandler = {
|
|
|
12
13
|
return setters.get(target)[name](value)
|
|
13
14
|
},
|
|
14
15
|
|
|
16
|
+
get (target, name) {
|
|
17
|
+
if (name === Symbol.toStringTag) {
|
|
18
|
+
return 'Module'
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const getter = getters.get(target)[name]
|
|
22
|
+
|
|
23
|
+
if (typeof getter === 'function') {
|
|
24
|
+
return getter()
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
|
|
15
28
|
defineProperty (target, property, descriptor) {
|
|
16
29
|
if ((!('value' in descriptor))) {
|
|
17
30
|
throw new Error('Getters/setters are not supported for exports property descriptors.')
|
|
@@ -21,9 +34,10 @@ const proxyHandler = {
|
|
|
21
34
|
}
|
|
22
35
|
}
|
|
23
36
|
|
|
24
|
-
function register (name, namespace, set, specifier) {
|
|
37
|
+
function register (name, namespace, set, get, specifier) {
|
|
25
38
|
specifiers.set(name, specifier)
|
|
26
39
|
setters.set(namespace, set)
|
|
40
|
+
getters.set(namespace, get)
|
|
27
41
|
const proxy = new Proxy(namespace, proxyHandler)
|
|
28
42
|
importHooks.forEach(hook => hook(name, proxy))
|
|
29
43
|
toHook.push([name, proxy])
|
package/package.json
CHANGED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { fileURLToPath } from 'url'
|
|
2
|
+
import { join } from 'path'
|
|
3
|
+
import { strictEqual } from 'assert'
|
|
4
|
+
import Hook from '../../index.js'
|
|
5
|
+
|
|
6
|
+
const toWrap = join(fileURLToPath(import.meta.url), '..', '..', 'fixtures', 'foo.mjs')
|
|
7
|
+
|
|
8
|
+
Hook([toWrap], (exports) => {
|
|
9
|
+
const original = exports.foo
|
|
10
|
+
exports.foo = function foo () {
|
|
11
|
+
return original() + '-first'
|
|
12
|
+
}
|
|
13
|
+
})
|
|
14
|
+
|
|
15
|
+
Hook([toWrap], (exports) => {
|
|
16
|
+
const original = exports.foo
|
|
17
|
+
exports.foo = function foo () {
|
|
18
|
+
return original() + '-second'
|
|
19
|
+
}
|
|
20
|
+
})
|
|
21
|
+
|
|
22
|
+
Hook([toWrap], (exports) => {
|
|
23
|
+
const shouldNotExist = exports.default
|
|
24
|
+
exports = function () {
|
|
25
|
+
return shouldNotExist()
|
|
26
|
+
}
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
const imp = await import('../fixtures/foo.mjs')
|
|
30
|
+
|
|
31
|
+
strictEqual(imp.foo(), 'foo-first-second')
|
|
32
|
+
// This should not throw!
|
|
33
|
+
strictEqual(imp.default, undefined)
|