File size: 1,411 Bytes
1187856
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import fs from "node:fs";
import path from "node:path";

const repository = process.env.HF_DATASET_REPO;
const fileName = process.env.HF_DATASET_FILE || "database.sqlite";
const revision = process.env.HF_REVISION || "main";
const token = process.env.HF_TOKEN;
const targetPath =
  process.env.TRIWORLDBENCH_DB_PATH ||
  path.join("/tmp", "triworldbench", fileName);

if (!repository) {
  console.error("A Hugging Face Dataset repository must be configured before startup.");
  process.exit(1);
}

const url = `https://huggingface.co/datasets/${repository}/resolve/${revision}/${fileName}`;
const headers = token ? { Authorization: `Bearer ${token}` } : {};
const response = await fetch(url, { headers });

if (!response.ok) {
  console.error(`Database download failed: ${response.status} ${response.statusText}`);
  process.exit(1);
}

const database = Buffer.from(await response.arrayBuffer());
if (database.length < 100 || database.subarray(0, 16).toString("binary") !== "SQLite format 3\u0000") {
  console.error("Database download returned a file that is not a valid SQLite database.");
  process.exit(1);
}
fs.mkdirSync(path.dirname(targetPath), { recursive: true });
const temporaryPath = `${targetPath}.download-${process.pid}`;
fs.writeFileSync(temporaryPath, database, { mode: 0o600 });
fs.renameSync(temporaryPath, targetPath);
console.log(`Downloaded the configured database revision ${revision}.`);