Spaces:
Running
Running
File size: 3,814 Bytes
c1b5e50 | 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 | /* Copyright 2026 Renchao Wu. Apache-2.0. */
const PINS = Object.freeze({
"router-good": {
"bytes": 98720,
"sha256": "eaf600b428ca7ce94e2fa420cf8e313fc702d7962a47ec819979b32174e8bc8d"
},
"router-payload-bitflip": {
"bytes": 98720,
"sha256": "d54a8ef765aba2914096aa5b71618b372e0687bacae6063b35fca16c57acf1a1"
},
"router-truncated": {
"bytes": 98719,
"sha256": "6306267ce34448ea737ca4e60f1e4e897ff36be841b8867b42db1a1936ef4bc3"
},
"overlap": {
"bytes": 117,
"sha256": "99976e5621ef397de83bfdd544bb9282afe564f544b625b214e600e54b919efd"
},
"unknown-dtype": {
"bytes": 74,
"sha256": "70cf013aea86a5fd5069db2ace084afc89d2706e7191a62375f36b6d99c33965"
},
"invalid-json": {
"bytes": 9,
"sha256": "adcc128f22190c20ed729c3761590562a2087891889854b9d7c7fd61dd2bc769"
}
});
export const MAX_CARRIER_BYTES = 140000;
async function readBounded(response) {
const declared = response.headers.get('content-length');
if (declared !== null && (!/^\d+$/.test(declared) || Number(declared) > MAX_CARRIER_BYTES))
throw new Error('Example carrier exceeds the byte limit');
if (!response.body?.getReader) throw new Error('Example response has no readable body');
const reader = response.body.getReader(), chunks = []; let size = 0;
try {
for (;;) {
const {done, value} = await reader.read();
if (done) break;
size += value.byteLength;
if (size > MAX_CARRIER_BYTES) throw new Error('Example carrier exceeds the byte limit');
chunks.push(value);
}
} catch (error) {
try { await reader.cancel(); } catch {}
throw error;
} finally { reader.releaseLock(); }
const bytes = new Uint8Array(size); let offset = 0;
for (const chunk of chunks) { bytes.set(chunk, offset); offset += chunk.length; }
return new TextDecoder('utf-8', {fatal:true}).decode(bytes);
}
export async function loadExample(name) {
if (!Object.hasOwn(PINS, name)) throw new Error('Unknown included example');
const response = await fetch(`fixtures-web/${name}.json`, {
mode:'same-origin', credentials:'omit', redirect:'error', signal:AbortSignal.timeout(15000)
});
try {
if (!response.ok) throw new Error(`Example read failed (${response.status})`);
if (response.redirected) throw new Error('Example carrier redirected');
if (!/^application\/json(?:\s*;|$)/i.test(response.headers.get('content-type') ?? ''))
throw new Error('Example carrier is not JSON');
const entry = JSON.parse(await readBounded(response)), pin = PINS[name];
if (!entry || typeof entry !== 'object' || Array.isArray(entry) || Object.keys(entry).length !== 6 ||
entry.schema !== 'tensor-header-example-bytes/v1' || entry.encoding !== 'base64' ||
entry.source_file !== `fixtures/${name}.safetensors` || entry.byte_length !== pin.bytes ||
entry.sha256 !== pin.sha256 || typeof entry.data_base64 !== 'string')
throw new Error('Example carrier metadata does not match the saved fixture');
const data = entry.data_base64;
if (data.length !== 4 * Math.ceil(pin.bytes / 3) ||
!/^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/.test(data))
throw new Error('Example carrier has invalid base64');
const binary = atob(data);
if (binary.length !== pin.bytes || btoa(binary) !== data) throw new Error('Example byte length or encoding mismatch');
const bytes = Uint8Array.from(binary, c => c.charCodeAt(0));
const sha = [...new Uint8Array(await crypto.subtle.digest('SHA-256', bytes))]
.map(x => x.toString(16).padStart(2, '0')).join('');
if (sha !== pin.sha256) throw new Error('Example bytes do not match the saved fixture');
return new Blob([bytes], {type:'application/octet-stream'});
} catch (error) {
try { await response.body?.cancel(); } catch {}
throw error;
}
}
|