MCPcopy Create free account
hub / github.com/GCWing/BitFun / dispatch

Function dispatch

src/apps/desktop/resources/worker_host.js:72–178  ·  view source on GitHub ↗
(method, params)

Source from the content-addressed store, hash-verified

70}
71
72async function dispatch(method, params) {
73 if (userHandlerLoadError) {
74 throw new Error('Failed to load source/worker.js: ' + (userHandlerLoadError.message || String(userHandlerLoadError)));
75 }
76 if (userHandlers[method] && typeof userHandlers[method] === 'function') {
77 return await userHandlers[method](params || {});
78 }
79
80 const [ns, name] = method.split('.');
81 if (ns === 'fs') {
82 const p = params.path || params.p;
83 if (p !== undefined && !isPathAllowed(p, 'read') && name !== 'access') {
84 if (name === 'writeFile' || name === 'mkdir' || name === 'rm' || name === 'appendFile' || name === 'rename' || name === 'copyFile') {
85 if (!isPathAllowed(p, 'write')) throw new Error('Path not allowed');
86 } else if (!isPathAllowed(p, 'read')) throw new Error('Path not allowed');
87 }
88 switch (name) {
89 case 'readFile': {
90 const enc = params.encoding || 'utf8';
91 const data = fs.readFileSync(p, enc === 'base64' ? undefined : enc);
92 return enc === 'base64' ? data.toString('base64') : data;
93 }
94 case 'writeFile':
95 fs.writeFileSync(p, params.data, params.encoding || 'utf8');
96 return null;
97 case 'readdir': {
98 const entries = fs.readdirSync(p, { withFileTypes: true });
99 return entries.map((e) => ({ name: e.name, path: path.join(p, e.name), isDirectory: e.isDirectory() }));
100 }
101 case 'stat': {
102 const s = fs.statSync(p);
103 return { size: s.size, isDirectory: s.isDirectory(), isFile: s.isFile() };
104 }
105 case 'mkdir':
106 fs.mkdirSync(p, { recursive: !!params.recursive });
107 return null;
108 case 'rm':
109 fs.rmSync(p, { recursive: !!params.recursive, force: !!params.force });
110 return null;
111 case 'copyFile':
112 fs.copyFileSync(params.src, params.dst);
113 return null;
114 case 'rename':
115 fs.renameSync(params.oldPath, params.newPath);
116 return null;
117 case 'appendFile':
118 fs.appendFileSync(p, params.data);
119 return null;
120 case 'access':
121 fs.accessSync(p);
122 return null;
123 default:
124 throw new Error('Unknown fs method: ' + method);
125 }
126 }
127
128 if (ns === 'shell') {
129 if (name === 'exec') {

Callers 1

worker_host.jsFile · 0.70

Calls 10

isPathAllowedFunction · 0.85
toStringMethod · 0.80
trimMethod · 0.80
basenameMethod · 0.80
extnameMethod · 0.80
includesMethod · 0.80
textMethod · 0.80
loadStorageFunction · 0.70
saveStorageFunction · 0.70
joinMethod · 0.45

Tested by

no test coverage detected