|
| 1 | +import {receiveMessageOnPort} from 'worker_threads'; |
| 2 | +const mockedModuleExports = new Map(); |
| 3 | +let currentMockVersion = 0; |
| 4 | + |
| 5 | +/** |
| 6 | + * FIXME: this is a hack to workaround loaders being |
| 7 | + * single threaded for now |
| 8 | + */ |
| 9 | +function doDrainPort() { |
| 10 | + let msg; |
| 11 | + while (msg = receiveMessageOnPort(preloadPort)) { |
| 12 | + onPreloadPortMessage(msg.message); |
| 13 | + } |
| 14 | +} |
| 15 | +function onPreloadPortMessage({ |
| 16 | + mockVersion, resolved, exports |
| 17 | +}) { |
| 18 | + currentMockVersion = mockVersion; |
| 19 | + mockedModuleExports.set(resolved, exports); |
| 20 | +} |
| 21 | +let preloadPort; |
| 22 | +export function globalPreload({port}) { |
| 23 | + preloadPort = port; |
| 24 | + port.on('message', onPreloadPortMessage); |
| 25 | + port.unref(); |
| 26 | + return `(${()=>{ |
| 27 | + let mockedModules = new Map(); |
| 28 | + let mockVersion = 0; |
| 29 | + const doMock = (resolved, replacementProperties) => { |
| 30 | + let exports = Object.keys(replacementProperties); |
| 31 | + let namespace = Object.create(null); |
| 32 | + let listeners = []; |
| 33 | + for (const name of exports) { |
| 34 | + let currentValue = replacementProperties[name]; |
| 35 | + Object.defineProperty(namespace, name, { |
| 36 | + enumerable: true, |
| 37 | + get() { |
| 38 | + return currentValue; |
| 39 | + }, |
| 40 | + set(v) { |
| 41 | + currentValue = v; |
| 42 | + for (let fn of listeners) { |
| 43 | + try { |
| 44 | + fn(name); |
| 45 | + } catch { |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + }); |
| 50 | + } |
| 51 | + mockedModules.set(resolved, { |
| 52 | + namespace, |
| 53 | + listeners |
| 54 | + }); |
| 55 | + mockVersion++; |
| 56 | + port.postMessage({mockVersion, resolved, exports }); |
| 57 | + return namespace; |
| 58 | + } |
| 59 | + setImportMetaCallback((meta, context, parent) => { |
| 60 | + if (context.url === 'node:mock') { |
| 61 | + meta.doMock = doMock; |
| 62 | + return; |
| 63 | + } |
| 64 | + if (context.url.startsWith('mock:')) { |
| 65 | + let [proto, version, encodedTargetURL] = context.url.split(':'); |
| 66 | + let decodedTargetURL = decodeURIComponent(encodedTargetURL); |
| 67 | + if (mockedModules.has(decodedTargetURL)) { |
| 68 | + meta.mock = mockedModules.get(decodedTargetURL); |
| 69 | + return; |
| 70 | + } |
| 71 | + } |
| 72 | + parent(meta, context); |
| 73 | + }); |
| 74 | + }})()`; |
| 75 | +} |
| 76 | + |
| 77 | + |
| 78 | +// rewrites node: loading to mock: so that it can be intercepted |
| 79 | +export function resolve(specifier, context, defaultResolve) { |
| 80 | + if (specifier === 'node:mock') { |
| 81 | + return { |
| 82 | + url: specifier |
| 83 | + }; |
| 84 | + } |
| 85 | + doDrainPort(); |
| 86 | + const def = defaultResolve(specifier, context); |
| 87 | + if (context.parentURL?.startsWith('mock:')) { |
| 88 | + // do nothing, let it get the "real" module |
| 89 | + } else if (mockedModuleExports.has(def.url)) { |
| 90 | + return { |
| 91 | + url: `mock:${currentMockVersion}:${encodeURIComponent(def.url)}` |
| 92 | + }; |
| 93 | + }; |
| 94 | + return { |
| 95 | + url: `${def.url}` |
| 96 | + }; |
| 97 | +} |
| 98 | + |
| 99 | +export function load(url, context, defaultLoad) { |
| 100 | + doDrainPort(); |
| 101 | + if (url === 'node:mock') { |
| 102 | + return { |
| 103 | + source: 'export default import.meta.doMock', |
| 104 | + format: 'module' |
| 105 | + }; |
| 106 | + } |
| 107 | + if (url.startsWith('mock:')) { |
| 108 | + let [proto, version, encodedTargetURL] = url.split(':'); |
| 109 | + let ret = generateModule(mockedModuleExports.get( |
| 110 | + decodeURIComponent(encodedTargetURL) |
| 111 | + )); |
| 112 | + return { |
| 113 | + source: ret, |
| 114 | + format: 'module' |
| 115 | + }; |
| 116 | + } |
| 117 | + return defaultLoad(url, context); |
| 118 | +} |
| 119 | + |
| 120 | +function generateModule(exports) { |
| 121 | + let body = 'export {};let mapping = {__proto__: null};' |
| 122 | + for (const [i, name] of Object.entries(exports)) { |
| 123 | + let key = JSON.stringify(name); |
| 124 | + body += `var _${i} = import.meta.mock.namespace[${key}];` |
| 125 | + body += `Object.defineProperty(mapping, ${key}, {enumerable: true,set(v) {_${i} = v;}});` |
| 126 | + body += `export {_${i} as ${name}};`; |
| 127 | + } |
| 128 | + body += `import.meta.mock.listeners.push(${ |
| 129 | + () => { |
| 130 | + for (var k in mapping) { |
| 131 | + mapping[k] = import.meta.mock.namespace[k]; |
| 132 | + } |
| 133 | + } |
| 134 | + });` |
| 135 | + return body; |
| 136 | +} |
0 commit comments