-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathio-worker.js
More file actions
130 lines (118 loc) · 4.63 KB
/
io-worker.js
File metadata and controls
130 lines (118 loc) · 4.63 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
'use strict';
const fs = require('fs');
const nodePath = require('path');
// ── File I/O operations (offloaded from main thread) ──────
process.on('message', (msg) => {
switch (msg.type) {
// ── Write JSON (fire-and-forget) ────────────────────────
case 'write-json': {
try {
const data = typeof msg.data === 'string' ? msg.data : JSON.stringify(msg.data, null, 2);
fs.writeFileSync(msg.path, data, 'utf-8');
if (msg.requestId) {
process.send({ type: 'write-json', requestId: msg.requestId, ok: true });
}
} catch (e) {
if (msg.requestId) {
process.send({ type: 'write-json', requestId: msg.requestId, ok: false, error: e.message });
}
}
break;
}
// ── Write text (fire-and-forget) ────────────────────────
case 'write-text': {
try {
fs.writeFileSync(msg.path, msg.text, 'utf-8');
if (msg.requestId) {
process.send({ type: 'write-text', requestId: msg.requestId, ok: true });
}
} catch (e) {
if (msg.requestId) {
process.send({ type: 'write-text', requestId: msg.requestId, ok: false, error: e.message });
}
}
break;
}
// ── Read JSON (async with response) ─────────────────────
case 'read-json': {
try {
if (!fs.existsSync(msg.path)) {
process.send({ type: 'read-json', requestId: msg.requestId, data: null, exists: false });
} else {
const raw = fs.readFileSync(msg.path, 'utf-8');
const data = JSON.parse(raw);
process.send({ type: 'read-json', requestId: msg.requestId, data, exists: true });
}
} catch (e) {
process.send({ type: 'read-json', requestId: msg.requestId, data: null, exists: false, error: e.message });
}
break;
}
// ── Batch exists check ──────────────────────────────────
case 'exists-batch': {
const results = {};
for (const p of msg.paths) {
try { results[p] = fs.existsSync(p); }
catch (_) { results[p] = false; }
}
process.send({ type: 'exists-batch', requestId: msg.requestId, results });
break;
}
// ── Read-modify-write JSON (atomic merge for tags/bookmarks/history) ──
case 'merge-write-json': {
try {
let all = {};
if (fs.existsSync(msg.path)) {
try { all = JSON.parse(fs.readFileSync(msg.path, 'utf-8')); } catch (_) {}
}
all[msg.key] = msg.value;
fs.writeFileSync(msg.path, JSON.stringify(all, null, 2), 'utf-8');
if (msg.requestId) {
process.send({ type: 'merge-write-json', requestId: msg.requestId, ok: true });
}
} catch (e) {
if (msg.requestId) {
process.send({ type: 'merge-write-json', requestId: msg.requestId, ok: false, error: e.message });
}
}
break;
}
// ── Serialize entries + write JSON (heavy stringify offloaded) ──
case 'serialize-write-json': {
try {
const blob = JSON.stringify(msg.data, null, 2);
fs.writeFileSync(msg.path, blob + '\n', 'utf-8');
process.send({ type: 'serialize-write-json', requestId: msg.requestId, ok: true });
} catch (e) {
process.send({ type: 'serialize-write-json', requestId: msg.requestId, ok: false, error: e.message });
}
break;
}
// ── Batch write text files (multiple fs.writeFileSync in worker) ──
case 'batch-write-text': {
let ok = 0;
const errs = [];
for (const item of msg.files) {
try {
const dir = nodePath.dirname(item.path);
if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true });
fs.writeFileSync(item.path, item.text, 'utf-8');
ok++;
} catch (e) { errs.push(`${nodePath.basename(item.path)}: ${e.message}`); }
}
process.send({ type: 'batch-write-text', requestId: msg.requestId, ok, total: msg.files.length, errs });
break;
}
// ── Write recovery snapshot (heavy JSON.stringify offloaded) ──
case 'write-recovery': {
try {
const json = JSON.stringify(msg.snapshot);
fs.writeFileSync(msg.path, json, 'utf-8');
process.send({ type: 'write-recovery', requestId: msg.requestId, ok: true });
} catch (e) {
process.send({ type: 'write-recovery', requestId: msg.requestId, ok: false, error: e.message });
}
break;
}
}
});