-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresources.js
More file actions
85 lines (76 loc) · 2.38 KB
/
Copy pathresources.js
File metadata and controls
85 lines (76 loc) · 2.38 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
80
81
82
83
84
85
import { Readable } from 'node:stream';
import { threadId } from 'node:worker_threads';
const originUrl = process.env.HARPER_TEST_ORIGIN_URL;
// Keep payloads above core's 8 KiB inline-storage threshold.
const payloadSize = 16 * 1024;
function payloadFor(token) {
const bytes = Buffer.alloc(payloadSize, 0x2e);
bytes.write(token);
return createBlob(Readable.from(bytes));
}
tables.PairRecord.sourcedFrom({
async get(id) {
const response = await fetch(`${originUrl}/resolve`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ id, node: server.hostname, threadId }),
});
if (!response.ok) throw new Error(`Pairing origin returned ${response.status}: ${await response.text()}`);
const resolved = await response.json();
return {
id,
token: resolved.token,
sourceNode: server.hostname,
sourceThread: threadId,
payload: payloadFor(resolved.token),
};
},
});
function describeRecord(record) {
if (!record?.payload) return null;
return record.payload.bytes().then((bytes) => ({
id: record.id,
token: record.token,
sourceNode: record.sourceNode,
sourceThread: record.sourceThread,
payloadToken: bytes.subarray(0, Buffer.byteLength(record.token)).toString(),
}));
}
export class PairPointProbe extends tables.PairRecord {
static async get(target) {
const record = await super.get(target);
let raw;
for (const entry of tables.PairRecord.primaryStore.getRange({ start: null, versions: true, snapshot: false })) {
if (entry.key === target.id) {
raw = { version: entry.version, nodeId: entry.nodeId, record: await describeRecord(entry.value) };
break;
}
}
return { node: server.hostname, threadId, record: await describeRecord(record), raw };
}
}
export class PairScanProbe extends Resource {
static loadAsInstance = false;
async get(target) {
target.checkPermission = false;
for (const entry of tables.PairRecord.primaryStore.getRange({ start: null, versions: true, snapshot: false })) {
if (entry.key === target.id) {
return {
node: server.hostname,
threadId,
version: entry.version,
nodeId: entry.nodeId,
record: await describeRecord(entry.value),
};
}
}
return { node: server.hostname, threadId, record: null };
}
}
export class PairWorker extends Resource {
static loadAsInstance = false;
get(target) {
target.checkPermission = false;
return { threadId };
}
}