local-modules.js (1637B)
1 import { BUNDLE_LIMIT, decodeBundle, sha256 } from './bundle.js'; 2 import { moduleHash, moduleIdentity } from './module-identity.js'; 3 export const LOCAL_MODULE_COUNT = 32, LOCAL_MODULE_BYTES = 64 * 1024 * 1024; 4 export async function validateLocalModule(input) { 5 if (!(input instanceof Uint8Array) || !input.byteLength || input.byteLength > BUNDLE_LIMIT) 6 throw new Error('Local module must be at most 16 MiB'); 7 const bytes = new Uint8Array(input), bundle = decodeBundle(bytes); 8 return { bytes, manifest: await moduleIdentity(bundle), hash: await sha256(bytes), names: bundle.exports.map(e => e.name) }; 9 } 10 // Copy at the async boundary; verify supplied identities against these bytes. 11 export function localModuleInputs(input = []) { 12 if (!Array.isArray(input) || input.length > LOCAL_MODULE_COUNT) 13 throw new Error('Local module count limit exceeded'); 14 let size = 0; 15 const seen = new Set(); 16 return input.map((entry) => { 17 if (!entry || typeof entry !== 'object') 18 throw new Error('Invalid local module'); 19 const { manifest, bytes } = entry; 20 moduleHash(manifest); 21 if (seen.has(manifest)) 22 throw new Error('Duplicate local module manifest'); 23 seen.add(manifest); 24 if (!(bytes instanceof Uint8Array) || !bytes.byteLength || bytes.byteLength > BUNDLE_LIMIT) 25 throw new Error('Local module byte limit exceeded'); 26 size += bytes.byteLength; 27 if (size > LOCAL_MODULE_BYTES) 28 throw new Error('Local module collection exceeds 64 MiB'); 29 return { manifest, bytes: new Uint8Array(bytes) }; 30 }); 31 }