File size: 3,240 Bytes
26fa2b2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// Source-only repository: prepare verified runtime files during build/dev.
// URLs are version-pinned; the content must match the audited SHA-256 hashes.
import { createHash, randomUUID } from 'node:crypto';
import { mkdir, readFile, rename, rm, writeFile } from 'node:fs/promises';
import { dirname, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';

const root = resolve(dirname(fileURLToPath(import.meta.url)), '..');
const runtimes = [
  {path:'public/vendor/basis/basis_transcoder.js',source:'node_modules/three/examples/jsm/libs/basis/basis_transcoder.js',bytes:57529,sha256:'8478b5b6d6b74e7d3082b89f6417321d8d1dc0307f2b30d4484bb11b441696a1'},
  {
    path: 'public/vendor/basis/basis_transcoder.wasm',
    source: 'node_modules/three/examples/jsm/libs/basis/basis_transcoder.wasm',
    sha256: '6cf17dc889352c42e9acf8897107978d127005fe3386c36a0e3845e27967630a',
    bytes: 527333,
  },
  {
    path: 'public/vendor/draco/draco_decoder.wasm',
    source: 'node_modules/three/examples/jsm/libs/draco/gltf/draco_decoder.wasm',
    sha256: 'a680d927bed9cb864ddbd63521868891af2bfbe755092761b4837487618df8ac',
    bytes: 192420,
  },
  {
    path: 'public/vendor/draco/draco_wasm_wrapper.js',
    source: 'node_modules/three/examples/jsm/libs/draco/gltf/draco_wasm_wrapper.js',
    sha256: '8bb2952d2ba7d67e1414f8df819410cb0434a666be53f671fff75f68843d76f6',
    bytes: 58456,
  },
];

function matches(data, runtime) {
  return data.length === runtime.bytes
    && (!runtime.path.endsWith('.wasm') || data.subarray(0, 4).equals(Buffer.from([0, 97, 115, 109])))
    && createHash('sha256').update(data).digest('hex') === runtime.sha256;
}

async function prepare(runtime) {
  const destination = resolve(root, runtime.path);
  try {
    if (matches(await readFile(destination), runtime)) {
      console.log(`Verified ${runtime.path}`);
      return;
    }
  } catch (error) {
    if (error.code !== 'ENOENT') throw error;
  }

  const chunks = [];
  let length = 0;
  if (runtime.source) {
    chunks.push(await readFile(resolve(root, runtime.source)));
  } else {
    console.log(`Downloading ${runtime.path}`);
    const response = await fetch(runtime.url, { signal: AbortSignal.timeout(60000) });
    if (!response.ok) throw new Error(`HTTP ${response.status}: ${runtime.url}`);
    for await (const chunk of response.body) {
      length += chunk.length;
      if (length > runtime.bytes) throw new Error(`Unexpected file size: ${runtime.path}`);
      chunks.push(chunk);
    }
  }
  const data = Buffer.concat(chunks);
  if (!matches(data, runtime)) throw new Error(`SHA-256 or size mismatch: ${runtime.path}`);

  await mkdir(dirname(destination), { recursive: true });
  const temporary = `${destination}.${randomUUID()}.tmp`;
  try {
    await writeFile(temporary, data, { flag: 'wx' });
    await rename(temporary, destination);
  } finally {
    await rm(temporary, { force: true });
  }
  console.log(`Downloaded and verified ${runtime.path}`);
}

try {
  for (const runtime of runtimes) await prepare(runtime);
} catch (error) {
  console.error(`Runtime preparation failed: ${error.message}`);
  console.error('Build stopped. Check access to the pinned public URLs and retry.');
  process.exitCode = 1;
}