Skip to content

Commit 9ab996f

Browse files
refactor: use sets for features & detected exports
1 parent d0bfeed commit 9ab996f

1 file changed

Lines changed: 13 additions & 13 deletions

File tree

src/componentize.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -123,27 +123,27 @@ export async function componentize(
123123
// Determine the default features that should be included
124124
const features = DEFAULT_FEATURES.reduce((acc, f) => {
125125
if (!disableFeatures.includes(f)) {
126-
acc.push(f);
126+
acc.add(f);
127127
}
128128
return acc;
129-
}, []);
129+
}, new Set());
130130

131131
if (!jsSource && sourcePath) {
132132
jsSource = await readFile(sourcePath, 'utf8');
133133
}
134-
const detectedExports = await detectKnownSourceExportNames(sourceName, jsSource);
134+
const detectedExports = await detectKnownSourceExportNames(
135+
sourceName,
136+
jsSource,
137+
);
135138

136139
// If there is an export of incomingHandler, there is likely to be a
137140
// manual implementation of wasi:http/incoming-handler, so we should
138141
// disable fetch-event
139-
if (
140-
features.includes('http') &&
141-
detectedExports.includes('incomingHandler')
142-
) {
142+
if (features.has('http') && detectedExports.has('incomingHandler')) {
143143
console.error(
144144
'Detected `incomingHandler` export, disabling fetch-event...',
145145
);
146-
disableFeatures.push('fetch-event');
146+
features.delete('fetch-event');
147147
}
148148

149149
// Splice the bindigns for the given WIT world into the engine WASM
@@ -220,7 +220,7 @@ export async function componentize(
220220
DEBUG: enableWizerLogging ? '1' : '',
221221
SOURCE_NAME: sourceName,
222222
EXPORT_CNT: exports.length.toString(),
223-
FEATURE_CLOCKS: features.includes('clocks') ? '1' : '',
223+
FEATURE_CLOCKS: features.has('clocks') ? '1' : '',
224224
};
225225

226226
for (const [idx, [export_name, expt]] of exports.entries()) {
@@ -610,15 +610,15 @@ async function handleCheckInitOutput(
610610
* @returns {Promise<string[]>} A Promise that resolves to a list of string that represent unversioned interfaces
611611
*/
612612
async function detectKnownSourceExportNames(filename, code) {
613-
const names = [];
614-
615-
// TODO: determine whether the javascript source code has a wasi:http/incoming-handler export
616613
if (!filename) {
617614
throw new Error('missing filename');
618615
}
619616
if (!code) {
620617
throw new Error('missing JS code');
621618
}
619+
620+
const names = new Set();
621+
622622
const results = await oxc.parseAsync(filename, code);
623623
if (results.errors.length > 0) {
624624
throw new Error(
@@ -628,7 +628,7 @@ async function detectKnownSourceExportNames(filename, code) {
628628

629629
for (const staticExport of results.module.staticExports) {
630630
for (const entry of staticExport.entries) {
631-
names.push(entry);
631+
names.add(entry.exportName.name);
632632
}
633633
}
634634

0 commit comments

Comments
 (0)