Skip to content

Commit

Permalink
try using initialize always when available
Browse files Browse the repository at this point in the history
  • Loading branch information
bumblehead committed Sep 9, 2023
1 parent d2c3e02 commit e1fef53
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 deletions.
14 changes: 12 additions & 2 deletions src/esmock.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import module from 'node:module'
import threads from 'node:worker_threads'
import * as hooks from './esmockLoader.js'
import esmockRegister from './esmockRegister.js'
import esmockLoader from './esmockLoader.js'
import esmockModule from './esmockModule.js'
import esmockArgs from './esmockArgs.js'
import esmockErr from './esmockErr.js'

const esmockGo = opts => async (...args) => {
const [moduleId, parent, defs, gdefs, opt] = esmockArgs(args, opts)
if (!await esmockLoader())
if (!esmockRegister && !await esmockLoader())
throw esmockErr.errMissingLoader()

const fileURLKey = await esmockModule(moduleId, parent, defs, gdefs, opt)
Expand All @@ -31,4 +35,10 @@ const esmock = Object.assign(esmockGo(), {
purge, p: esmockGo({ purge: false }), strict, strictest })

export {esmock as default, strict, strictest}
export * from './esmockLoader.js'


const isMessageChannel = Boolean(module.register && threads.MessageChannel)
const hooksFinal = isMessageChannel ? {} : hooks
const { load, resolve, getSource, initialize, globalPreload } = hooksFinal
export { load, resolve, getSource, initialize, globalPreload }
// export * from './esmockLoader.js'
12 changes: 8 additions & 4 deletions src/esmockLoader.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import fs from 'node:fs/promises'
import module from 'node:module'
import threads from 'node:worker_threads'
import process from 'process'
import esmockErr from './esmockErr.js'

Expand Down Expand Up @@ -28,25 +30,27 @@ const hashbangRe = /^(#![^\n]*\n)/
const moduleIdReCreate = (moduleid, treeid) => new RegExp(
`.*(${moduleid}(\\?${treeid}(?:(?!#-#).)*)).*`)

const isMessageChannel = Boolean(module.register && threads.MessageChannel)

// node v12.0-v18.x, global
const mockKeys = global.mockKeys = (global.mockKeys || {})

// node v20.0-v20.6
const globalPreload = (({ port }) => (
const globalPreload = !isMessageChannel && (({ port }) => (
port.addEventListener('message', ev => (
mockKeys[ev.data.key] = ev.data.keylong)),
port.unref(),
'global.postMessageEsmk = d => port.postMessage(d)'
))

// node v20.6-current
const initialize = data => {
const initialize = isMessageChannel && (data => {
if (data && data.port) {
data.port.on('message', msg => {
mockKeys[msg.key] = msg.keylong
})
}
}
})

const parseImports = defstr => {
const [specifier, imports] = (defstr.match(esmkImportRe) || [])
Expand Down Expand Up @@ -220,7 +224,7 @@ export {
getSource,
// if getSource and initialize exported at the time,
// getSource is never called :(
// initialize,
initialize,
globalPreload,
loaderIsVerified as default
}
4 changes: 2 additions & 2 deletions src/esmockRegister.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ const register = (res => () => {
if (typeof res === 'boolean')
return res

if (!(res = global.shouldRegister === true))
return res
// if (!(res = global.shouldRegister === true))
// return res

if ((res = Boolean(channel && module.register))) {
module.register('./esmockLoader.js', {
Expand Down

0 comments on commit e1fef53

Please sign in to comment.