Files
FrenoCorp/node_modules/metro-file-map/src/worker.js
Michael Freno 7c684a42cc FRE-600: Fix code review blockers
- Consolidated duplicate UndoManagers to single instance
- Fixed connection promise to only resolve on 'connected' status
- Fixed WebSocketProvider import (WebsocketProvider)
- Added proper doc.destroy() cleanup
- Renamed isPresenceInitialized property to avoid conflict

Co-Authored-By: Paperclip <noreply@paperclip.ing>
2026-04-25 00:08:01 -04:00

67 lines
1.5 KiB
JavaScript

"use strict";
const { createHash } = require("crypto");
const fs = require("graceful-fs");
function sha1hex(content) {
return createHash("sha1").update(content).digest("hex");
}
class Worker {
#plugins;
constructor({ plugins = [] }) {
this.#plugins = plugins.map(({ modulePath, setupArgs }) => {
const PluginWorker = require(modulePath);
return new PluginWorker(setupArgs);
});
}
processFile(data) {
let content;
let sha1;
const { computeSha1, filePath, pluginsToRun } = data;
const getContent = () => {
if (content == null) {
content = fs.readFileSync(filePath);
}
return content;
};
const workerUtils = {
getContent,
};
const pluginData = pluginsToRun.map((pluginIdx) =>
this.#plugins[pluginIdx].processFile(data, workerUtils),
);
if (computeSha1) {
sha1 = sha1hex(getContent());
}
return content && data.maybeReturnContent
? {
content,
pluginData,
sha1,
}
: {
pluginData,
sha1,
};
}
}
let singletonWorker;
function setup(args) {
if (singletonWorker) {
throw new Error("metro-file-map: setup() should only be called once");
}
singletonWorker = new Worker(args);
}
function processFile(data) {
if (!singletonWorker) {
throw new Error(
"metro-file-map: setup() must be called before processFile()",
);
}
return singletonWorker.processFile(data);
}
module.exports = {
setup,
processFile,
Worker,
};