-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathquibble.mjs
184 lines (160 loc) · 5.25 KB
/
quibble.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import quibble from './quibble.js'
import { thisWillRunInUserThread } from './thisWillRunInUserThread.js'
export default quibble
export const reset = quibble.reset
export const ignoreCallsFromThisFile = quibble.ignoreCallsFromThisFile
export const config = quibble.config
export const isLoaderLoaded = quibble.isLoaderLoaded
/** @typedef {{hasDefaultExportStub: boolean, namedExports: [string]}} ModuleLoaderMockInfo */
/**
* @type {{
* quibbledModules: Map<string, ModuleLoaderMockInfo>,
* stubModuleGeneration: number
* }}
*
*/
const quibbleLoaderState = {
quibbledModules: new Map(),
stubModuleGeneration: 0
}
export async function resolve (specifier, context, nextResolve) {
const resolve = () =>
nextResolve(
specifier.includes('__quibble')
? specifier
.replace(/[?&]__quibbleresolveurl/, '')
.replace(/[?&]__quibbleoriginal/, '')
: specifier,
context
)
if (specifier.includes('__quibbleresolveurl')) {
const resolvedUrl = (await resolve()).url
const error = new Error()
error.code = 'QUIBBLE_RESOLVED_URL'
error.resolvedUrl = resolvedUrl
throw error
}
if (!quibbleLoaderState.quibbledModules) {
return resolve()
}
if (specifier === 'quibble') {
return resolve()
}
if (specifier.includes('__quibbleoriginal')) {
return resolve()
}
const stubModuleGeneration = quibbleLoaderState.stubModuleGeneration
const { parentURL } = context
try {
const { url, ...ctx } = await resolve()
const quibbledUrl = addQueryToUrl(url, '__quibble', stubModuleGeneration)
if (url.startsWith('node:') && !getStubsInfo(quibbledUrl)) {
return { ...ctx, url } // It's allowed to change ctx for a builtin (but unlikely)
}
return { ...ctx, url: quibbledUrl }
} catch (error) {
if (error.code === 'ERR_MODULE_NOT_FOUND') {
return {
url: parentURL
? addQueryToUrl(
new URL(specifier, parentURL).href
, '__quibble', stubModuleGeneration)
: new URL(specifier).href
}
} else {
throw error
}
}
}
/**
* @param {string} moduleUrl
*
* @returns {[string, ModuleLoaderMockInfo] | undefined}
* */
function getStubsInfo (moduleUrl) {
if (!quibbleLoaderState.quibbledModules) return undefined
if (!moduleUrl.includes('__quibble=')) return undefined
const moduleKey = moduleUrl.replace(/\?.*/, '').replace(/#.*/, '')
const moduleMockingInfo = quibbleLoaderState.quibbledModules.get(moduleKey)
return moduleMockingInfo ? [moduleKey, moduleMockingInfo] : undefined
}
/**
*
* @param {[string, ModuleLoaderMockInfo]} options
* @returns
*/
function transformModuleSource ([moduleKey, mockingInfo]) {
return `
${mockingInfo.namedExports
.map(
(name) =>
`export let ${name} = globalThis[Symbol.for('__quibbleUserState')].quibbledModules.get(${JSON.stringify(
moduleKey
)}).namedExportStubs["${name}"]`
)
.join(';\n')};
${
mockingInfo.hasDefaultExport
? `export default globalThis[Symbol.for('__quibbleUserState')].quibbledModules.get(${JSON.stringify(
moduleKey
)}).defaultExportStub;`
: ''
}
`
}
/**
* @param {string} url
* @param {{
* format: string,
* }} context
* @param {Function} nextLoad
* @returns {Promise<{ source: !(string | SharedArrayBuffer | Uint8Array), format: string}>}
*/
export async function load (url, context, nextLoad) {
const mockingInfo = getStubsInfo(url)
return mockingInfo
? {
source: transformModuleSource(mockingInfo),
format: 'module',
shortCircuit: true
}
: await nextLoad(url.replace(/\?.*/, '').replace(/#.*/, ''), context)
}
export const globalPreload = ({ port }) => {
globalThis[Symbol.for('__quibbleLoaderState')] = quibbleLoaderState
port.addEventListener('message', ({ data }) => {
if (data.type === 'reset') {
quibbleLoaderState.quibbledModules = new Map()
quibbleLoaderState.stubModuleGeneration++
Atomics.store(data.hasResetHappened, 0, 1)
Atomics.notify(data.hasResetHappened, 0)
} else if (data.type === 'addMockedModule') {
quibbleLoaderState.quibbledModules.set(data.moduleUrl, {
namedExports: data.namedExports,
hasDefaultExport: data.hasDefaultExport
})
++quibbleLoaderState.stubModuleGeneration
Atomics.store(data.hasAddMockedHappened, 0, 1)
Atomics.notify(data.hasAddMockedHappened, 0)
} else if (data.type === 'listMockedModules') {
const mockedModules = Array.from(quibbleLoaderState.quibbledModules.keys())
const serializedMockedModules = mockedModules.join(' ')
const encodedMockedModules = new TextEncoder().encode(serializedMockedModules)
data.mockedModulesListLength[0] = encodedMockedModules.length
if (encodedMockedModules.length <= data.mockedModulesList.length) {
for (let i = 0; i < encodedMockedModules.length; ++i) {
data.mockedModulesList[i] = encodedMockedModules[i]
}
}
Atomics.store(data.hasListMockedModulesHappened, 0, 1)
Atomics.notify(data.hasListMockedModulesHappened, 0)
}
})
port.unref()
return `(${thisWillRunInUserThread})(globalThis, port)`
}
function addQueryToUrl (url, query, value) {
const urlObject = new URL(url)
urlObject.searchParams.set(query, value)
return urlObject.href
}