Repository navigation
Expand file tree
/
Copy pathsourcecode.js
More file actions
79 lines (73 loc) · 2.36 KB
/
Copy pathsourcecode.js
File metadata and controls
79 lines (73 loc) · 2.36 KB
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
const TREE_URL = 'https://api.github.com/repos/BasedHardware/omi/git/trees/main?recursive=1';
const RAW_ROOT = 'https://raw.githubusercontent.com/BasedHardware/omi/main/';
const PAGE_ROOT = 'https://github.com/BasedHardware/omi/blob/main/';
const MAX_SOURCE_FILE_BYTES = 250_000;
function usefulSourceEntry(entry) {
const path = String(entry?.path || '');
const size = Number(entry?.size || 0);
return (
entry?.type === 'blob' &&
size > 0 &&
size <= MAX_SOURCE_FILE_BYTES &&
/^app\/lib\/(?:providers|services)\/.+\.dart$/i.test(path) &&
!/\.(?:g|freezed)\.dart$/i.test(path)
);
}
function encodePath(path) {
return String(path || '')
.split('/')
.map((part) => encodeURIComponent(part))
.join('/');
}
async function mapPool(items, limit, fn) {
const out = [];
let next = 0;
async function worker() {
while (next < items.length) {
const index = next;
next += 1;
out[index] = await fn(items[index]);
}
}
await Promise.all(Array.from({ length: Math.min(limit, items.length) }, () => worker()));
return out;
}
async function loadOfficialSource(fetchImpl, store, { concurrency = 6, maxFiles = 250 } = {}) {
if (!store?.saveDocPage) return 0;
const fetchFn = fetchImpl || fetch;
const treeRes = await fetchFn(TREE_URL, {
headers: { Accept: 'application/vnd.github+json', 'User-Agent': 'omi-vector-bot' },
});
if (!treeRes.ok) return 0;
const data = await treeRes.json();
const entries = (Array.isArray(data?.tree) ? data.tree : [])
.filter(usefulSourceEntry)
.slice(0, Math.max(0, maxFiles));
const saved = await mapPool(entries, Math.max(1, concurrency), async (entry) => {
try {
const encoded = encodePath(entry.path);
const res = await fetchFn(`${RAW_ROOT}${encoded}`, {
headers: { Accept: 'text/plain', 'User-Agent': 'omi-vector-bot' },
});
if (!res.ok) return 0;
const body = await res.text();
if (!String(body || '').trim()) return 0;
await store.saveDocPage({
url: `${PAGE_ROOT}${encoded}`,
title: entry.path,
body,
});
return 1;
} catch (err) {
console.error(`[DB] official source failed (${entry.path}):`, err.message);
return 0;
}
});
return saved.reduce((sum, count) => sum + count, 0);
}
module.exports = {
PAGE_ROOT,
TREE_URL,
loadOfficialSource,
usefulSourceEntry,
};