@aaa_iii/fzv 0.1.1 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.ja.md +178 -0
- package/README.md +131 -72
- package/THIRD_PARTY_NOTICES.md +4 -1
- package/dist/client/assets/{c-pw5cNQ6f.js → c-Bt2S8zZg.js} +1 -1
- package/dist/client/assets/{cpp-BzP2DU1a.js → cpp-DPK4tyRH.js} +1 -1
- package/dist/client/assets/{highlight-m6b6lqez.js → highlight-Cjbz6wKa.js} +4 -4
- package/dist/client/assets/index-CDvmZwbk.js +69 -0
- package/dist/client/assets/{index-CmRL0QNk.css → index-CUTCg_dk.css} +1 -1
- package/dist/client/assets/{java-D112xljv.js → java-J15MpnFT.js} +1 -1
- package/dist/client/assets/{jsx-Czjwr3X8.js → jsx-DUOQ5e6U.js} +1 -1
- package/dist/client/assets/{latex-DyPjLw3u.js → latex-CJrCGu_N.js} +1 -1
- package/dist/client/assets/{lua-C7IYe9JZ.js → lua-CKx32ZrF.js} +1 -1
- package/dist/client/assets/{pdf-APdaxgPI.js → pdf-27wT5C7q.js} +2 -2
- package/dist/client/assets/{r-9GJvLLqN.js → r-C56hVSf1.js} +1 -1
- package/dist/client/assets/{ruby-BcWFKqsj.js → ruby-CmgALhjT.js} +1 -1
- package/dist/client/assets/{tsx-COvetw7Z.js → tsx-clVZ5Fr8.js} +1 -1
- package/dist/client/assets/{typescript-D48Br3Og.js → typescript-D4OylR-N.js} +1 -1
- package/dist/client/assets/worker-12brr2uW.js +4 -0
- package/dist/client/assets/{xml-_pY6RLHw.js → xml-DDzn0dx3.js} +1 -1
- package/dist/client/index.html +105 -2
- package/dist/navigation.js +2 -2
- package/dist/server/catalog.js +6 -0
- package/dist/server/cli.js +36 -9
- package/dist/server/git.js +131 -0
- package/dist/server/network.js +4 -0
- package/dist/server/office.js +244 -0
- package/dist/server/server.js +66 -4
- package/dist/shared.js +1 -1
- package/package.json +1 -1
- package/dist/client/assets/index-DZRy_kMM.js +0 -69
- package/dist/server/interactive.js +0 -32
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
import { spawn } from 'node:child_process';
|
|
2
|
+
import { createHash } from 'node:crypto';
|
|
3
|
+
import { constants } from 'node:fs';
|
|
4
|
+
import { access, mkdir, mkdtemp, rename, rm, stat, writeFile, } from 'node:fs/promises';
|
|
5
|
+
import { tmpdir } from 'node:os';
|
|
6
|
+
import path from 'node:path';
|
|
7
|
+
import { pathToFileURL } from 'node:url';
|
|
8
|
+
export const OFFICE_CONVERTER_NAMES = {
|
|
9
|
+
ibisoffice: 'ibisOffice',
|
|
10
|
+
libreoffice: 'LibreOffice',
|
|
11
|
+
};
|
|
12
|
+
const SEARCH_NAMES = ['ibisOffice', 'ibisoffice', 'soffice', 'libreoffice'];
|
|
13
|
+
const TIMEOUT = 60_000;
|
|
14
|
+
const CACHE_LIMIT = 32;
|
|
15
|
+
export function converterKind(command) {
|
|
16
|
+
return /^(?:soffice|libreoffice)/i.test(path.basename(command))
|
|
17
|
+
? 'libreoffice'
|
|
18
|
+
: 'ibisoffice';
|
|
19
|
+
}
|
|
20
|
+
async function executable(filename) {
|
|
21
|
+
try {
|
|
22
|
+
await access(filename, constants.X_OK);
|
|
23
|
+
return (await stat(filename)).isFile();
|
|
24
|
+
}
|
|
25
|
+
catch {
|
|
26
|
+
return false;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
export async function which(name, env) {
|
|
30
|
+
const extensions = process.platform === 'win32'
|
|
31
|
+
? (env.PATHEXT ?? '.EXE').split(';').filter(Boolean)
|
|
32
|
+
: [''];
|
|
33
|
+
for (const directory of (env.PATH ?? '').split(path.delimiter)) {
|
|
34
|
+
if (!directory)
|
|
35
|
+
continue;
|
|
36
|
+
for (const extension of extensions) {
|
|
37
|
+
const candidate = path.join(directory, name + extension);
|
|
38
|
+
if (await executable(candidate))
|
|
39
|
+
return candidate;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return undefined;
|
|
43
|
+
}
|
|
44
|
+
/** Resolves an explicit converter, or prefers ibisOffice over LibreOffice on PATH. */
|
|
45
|
+
export async function findOfficeConverter(explicit, env = process.env) {
|
|
46
|
+
if (explicit) {
|
|
47
|
+
const command = /[\\/]/.test(explicit)
|
|
48
|
+
? (await executable(path.resolve(explicit)))
|
|
49
|
+
? path.resolve(explicit)
|
|
50
|
+
: undefined
|
|
51
|
+
: await which(explicit, env);
|
|
52
|
+
if (!command)
|
|
53
|
+
throw new Error(`--office-converter: ${explicit} is not an executable file.`);
|
|
54
|
+
return { kind: converterKind(command), command };
|
|
55
|
+
}
|
|
56
|
+
const candidates = [];
|
|
57
|
+
for (const name of SEARCH_NAMES) {
|
|
58
|
+
const command = await which(name, env);
|
|
59
|
+
if (command)
|
|
60
|
+
candidates.push(command);
|
|
61
|
+
}
|
|
62
|
+
if (process.platform === 'darwin')
|
|
63
|
+
candidates.push('/Applications/ibisOffice.app/Contents/MacOS/ibisOffice', '/Applications/LibreOffice.app/Contents/MacOS/soffice');
|
|
64
|
+
if (process.platform === 'win32' && env.ProgramFiles)
|
|
65
|
+
candidates.push(path.join(env.ProgramFiles, 'LibreOffice', 'program', 'soffice.exe'));
|
|
66
|
+
for (const command of candidates)
|
|
67
|
+
if (await executable(command))
|
|
68
|
+
return { kind: converterKind(command), command };
|
|
69
|
+
return null;
|
|
70
|
+
}
|
|
71
|
+
export class OfficeConversionError extends Error {
|
|
72
|
+
status;
|
|
73
|
+
constructor(message, status) {
|
|
74
|
+
super(message);
|
|
75
|
+
this.status = status;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Converts Office documents to PDF in a private temporary directory.
|
|
80
|
+
* Inputs are copies of validated file contents, so the converter never reads project paths.
|
|
81
|
+
*/
|
|
82
|
+
export class OfficeRenderer {
|
|
83
|
+
converter;
|
|
84
|
+
directory;
|
|
85
|
+
// Keyed by content, so watch-mode edits convert again and reverted files reuse the cache.
|
|
86
|
+
cache = new Map();
|
|
87
|
+
children = new Set();
|
|
88
|
+
queue = [];
|
|
89
|
+
limit;
|
|
90
|
+
running = 0;
|
|
91
|
+
sequence = 0;
|
|
92
|
+
closed = false;
|
|
93
|
+
constructor(converter) {
|
|
94
|
+
this.converter = converter;
|
|
95
|
+
// LibreOffice instances sharing a profile cannot run concurrently.
|
|
96
|
+
this.limit = converter.kind === 'libreoffice' ? 1 : 2;
|
|
97
|
+
}
|
|
98
|
+
get name() {
|
|
99
|
+
return OFFICE_CONVERTER_NAMES[this.converter.kind];
|
|
100
|
+
}
|
|
101
|
+
pdf(bytes, extension) {
|
|
102
|
+
const key = createHash('sha256')
|
|
103
|
+
.update(extension)
|
|
104
|
+
.update('\0')
|
|
105
|
+
.update(bytes)
|
|
106
|
+
.digest('hex');
|
|
107
|
+
const cached = this.cache.get(key);
|
|
108
|
+
if (cached) {
|
|
109
|
+
this.cache.delete(key);
|
|
110
|
+
this.cache.set(key, cached);
|
|
111
|
+
return cached;
|
|
112
|
+
}
|
|
113
|
+
const result = this.convert(key, bytes, extension);
|
|
114
|
+
this.cache.set(key, result);
|
|
115
|
+
result.catch(() => {
|
|
116
|
+
if (this.cache.get(key) === result)
|
|
117
|
+
this.cache.delete(key);
|
|
118
|
+
});
|
|
119
|
+
while (this.cache.size > CACHE_LIMIT) {
|
|
120
|
+
const [oldest, pending] = this.cache.entries().next().value;
|
|
121
|
+
this.cache.delete(oldest);
|
|
122
|
+
void pending.then((filename) => rm(filename, { force: true }), () => { });
|
|
123
|
+
}
|
|
124
|
+
return result;
|
|
125
|
+
}
|
|
126
|
+
async close() {
|
|
127
|
+
this.closed = true;
|
|
128
|
+
for (const child of this.children)
|
|
129
|
+
child.kill('SIGKILL');
|
|
130
|
+
for (const next of this.queue.splice(0))
|
|
131
|
+
next();
|
|
132
|
+
const directory = await this.directory?.catch(() => undefined);
|
|
133
|
+
if (directory)
|
|
134
|
+
await rm(directory, { recursive: true, force: true });
|
|
135
|
+
}
|
|
136
|
+
root() {
|
|
137
|
+
this.directory ??= mkdtemp(path.join(tmpdir(), 'fzv-office-'));
|
|
138
|
+
return this.directory;
|
|
139
|
+
}
|
|
140
|
+
acquire() {
|
|
141
|
+
if (this.running < this.limit) {
|
|
142
|
+
this.running++;
|
|
143
|
+
return Promise.resolve();
|
|
144
|
+
}
|
|
145
|
+
return new Promise((resolve) => this.queue.push(resolve));
|
|
146
|
+
}
|
|
147
|
+
release() {
|
|
148
|
+
const next = this.queue.shift();
|
|
149
|
+
if (next)
|
|
150
|
+
next();
|
|
151
|
+
else
|
|
152
|
+
this.running--;
|
|
153
|
+
}
|
|
154
|
+
async convert(key, bytes, extension) {
|
|
155
|
+
await this.acquire();
|
|
156
|
+
try {
|
|
157
|
+
if (this.closed)
|
|
158
|
+
throw new OfficeConversionError('The viewer is shutting down.', 503);
|
|
159
|
+
const root = await this.root();
|
|
160
|
+
const work = path.join(root, `work-${++this.sequence}`);
|
|
161
|
+
await mkdir(work);
|
|
162
|
+
try {
|
|
163
|
+
const input = path.join(work, `document${extension}`);
|
|
164
|
+
await writeFile(input, bytes, { flag: 'wx', mode: 0o600 });
|
|
165
|
+
const stderr = await this.run(root, work, input);
|
|
166
|
+
const target = path.join(root, `${key}.pdf`);
|
|
167
|
+
try {
|
|
168
|
+
await rename(path.join(work, 'document.pdf'), target);
|
|
169
|
+
}
|
|
170
|
+
catch {
|
|
171
|
+
throw new OfficeConversionError(stderr || `${this.name} did not produce a PDF.`, 422);
|
|
172
|
+
}
|
|
173
|
+
return target;
|
|
174
|
+
}
|
|
175
|
+
finally {
|
|
176
|
+
await rm(work, { recursive: true, force: true });
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
finally {
|
|
180
|
+
this.release();
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
/** Resolves with the last diagnostic line when the converter exits successfully. */
|
|
184
|
+
run(root, work, input) {
|
|
185
|
+
const args = this.converter.kind === 'libreoffice'
|
|
186
|
+
? [
|
|
187
|
+
'--headless',
|
|
188
|
+
'--norestore',
|
|
189
|
+
'--nolockcheck',
|
|
190
|
+
`-env:UserInstallation=${pathToFileURL(path.join(root, 'libreoffice-profile')).href}`,
|
|
191
|
+
'--convert-to',
|
|
192
|
+
'pdf',
|
|
193
|
+
'--outdir',
|
|
194
|
+
work,
|
|
195
|
+
input,
|
|
196
|
+
]
|
|
197
|
+
: ['--export', 'pdf', '--out-dir', work, input];
|
|
198
|
+
return new Promise((resolve, reject) => {
|
|
199
|
+
const child = spawn(this.converter.command, args, {
|
|
200
|
+
cwd: work,
|
|
201
|
+
stdio: ['ignore', 'ignore', 'pipe'],
|
|
202
|
+
windowsHide: true,
|
|
203
|
+
});
|
|
204
|
+
this.children.add(child);
|
|
205
|
+
let stderr = '';
|
|
206
|
+
let timedOut = false;
|
|
207
|
+
child.stderr.setEncoding('utf8');
|
|
208
|
+
child.stderr.on('data', (chunk) => {
|
|
209
|
+
if (stderr.length < 8192)
|
|
210
|
+
stderr += chunk;
|
|
211
|
+
});
|
|
212
|
+
const timer = setTimeout(() => {
|
|
213
|
+
timedOut = true;
|
|
214
|
+
child.kill('SIGKILL');
|
|
215
|
+
}, TIMEOUT);
|
|
216
|
+
const finish = () => {
|
|
217
|
+
clearTimeout(timer);
|
|
218
|
+
this.children.delete(child);
|
|
219
|
+
};
|
|
220
|
+
// Diagnostics name the temporary copy; show only its file name.
|
|
221
|
+
const diagnostic = () => stderr
|
|
222
|
+
.replaceAll(work + path.sep, '')
|
|
223
|
+
.split(/\r?\n/)
|
|
224
|
+
.map((line) => line.trim())
|
|
225
|
+
.filter(Boolean)
|
|
226
|
+
.at(-1)
|
|
227
|
+
?.slice(0, 500) ?? '';
|
|
228
|
+
child.once('error', (error) => {
|
|
229
|
+
finish();
|
|
230
|
+
reject(new OfficeConversionError(`Could not start ${this.name}: ${error.message}`, 503));
|
|
231
|
+
});
|
|
232
|
+
child.once('close', (code, signal) => {
|
|
233
|
+
finish();
|
|
234
|
+
if (code === 0)
|
|
235
|
+
resolve(diagnostic());
|
|
236
|
+
else
|
|
237
|
+
reject(new OfficeConversionError(timedOut
|
|
238
|
+
? `${this.name} did not finish within ${TIMEOUT / 1000} seconds.`
|
|
239
|
+
: diagnostic() ||
|
|
240
|
+
`${this.name} exited with ${signal ?? `code ${code}`}.`, 422));
|
|
241
|
+
});
|
|
242
|
+
});
|
|
243
|
+
}
|
|
244
|
+
}
|
package/dist/server/server.js
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
import { createServer } from 'node:http';
|
|
2
2
|
import { networkInterfaces } from 'node:os';
|
|
3
|
-
import { watch as watchFiles } from 'node:fs';
|
|
4
|
-
import { readFile, realpath, stat } from 'node:fs/promises';
|
|
3
|
+
import { constants, watch as watchFiles } from 'node:fs';
|
|
4
|
+
import { open, readFile, realpath, stat } from 'node:fs/promises';
|
|
5
5
|
import path from 'node:path';
|
|
6
6
|
import { fileURLToPath } from 'node:url';
|
|
7
7
|
import { pipeline } from 'node:stream/promises';
|
|
8
8
|
import { Catalog, contentType, FileUnavailable, isVisibleRelative, } from './catalog.js';
|
|
9
|
+
import { GitRepository } from './git.js';
|
|
10
|
+
import { OfficeConversionError, OfficeRenderer } from './office.js';
|
|
9
11
|
import { installNavigation } from '../navigation.js';
|
|
10
12
|
const UI_ROOT = fileURLToPath(new URL('../client/', import.meta.url));
|
|
11
13
|
const UI_CSP = "default-src 'self'; script-src 'self' 'wasm-unsafe-eval'; worker-src 'self'; style-src 'self'; img-src 'self' data: blob:; font-src 'self'; frame-src 'self'; object-src 'none'; base-uri 'none'; frame-ancestors 'none'; form-action 'none'";
|
|
@@ -119,6 +121,7 @@ function allowedHosts(host) {
|
|
|
119
121
|
}
|
|
120
122
|
return values;
|
|
121
123
|
}
|
|
124
|
+
export const OFFICE_UNAVAILABLE = 'Office previews need ibisOffice or LibreOffice. Install one and restart fzv, or pass --office-converter PATH.';
|
|
122
125
|
export async function createViewer(options) {
|
|
123
126
|
const catalog = await Catalog.create(options.root);
|
|
124
127
|
const hosts = allowedHosts(options.host ?? '127.0.0.1');
|
|
@@ -126,6 +129,10 @@ export async function createViewer(options) {
|
|
|
126
129
|
const allowHtmlNetwork = options.allowHtmlNetwork ?? false;
|
|
127
130
|
const watchEnabled = options.watch ?? false;
|
|
128
131
|
const previewCsp = allowHtmlNetwork ? NETWORK_PREVIEW_CSP : PREVIEW_CSP;
|
|
132
|
+
const office = options.officeConverter
|
|
133
|
+
? new OfficeRenderer(options.officeConverter)
|
|
134
|
+
: undefined;
|
|
135
|
+
const git = options.git ? new GitRepository(options.git, catalog.root) : null;
|
|
129
136
|
const eventClients = new Set();
|
|
130
137
|
let reloadTimer;
|
|
131
138
|
// Unknown or excessive changes are sent as null so clients refresh every tab.
|
|
@@ -242,7 +249,11 @@ export async function createViewer(options) {
|
|
|
242
249
|
reply(req, res, 400, 'text/plain', 'Too many file paths\n');
|
|
243
250
|
return;
|
|
244
251
|
}
|
|
245
|
-
const project = {
|
|
252
|
+
const project = {
|
|
253
|
+
...catalog.project,
|
|
254
|
+
watch: watchEnabled,
|
|
255
|
+
git: git !== null,
|
|
256
|
+
};
|
|
246
257
|
const body = url.pathname === '/api/project'
|
|
247
258
|
? project
|
|
248
259
|
: url.pathname === '/api/session-files'
|
|
@@ -251,6 +262,24 @@ export async function createViewer(options) {
|
|
|
251
262
|
reply(req, res, 200, 'application/json; charset=utf-8', JSON.stringify(body));
|
|
252
263
|
return;
|
|
253
264
|
}
|
|
265
|
+
if (url.pathname === '/api/git/status' ||
|
|
266
|
+
url.pathname === '/api/git/file') {
|
|
267
|
+
if (req.headers.origin && req.headers.origin !== origin.origin) {
|
|
268
|
+
reply(req, res, 403, 'text/plain', 'Origin not allowed\n');
|
|
269
|
+
return;
|
|
270
|
+
}
|
|
271
|
+
if (!git)
|
|
272
|
+
throw new FileUnavailable();
|
|
273
|
+
if (url.pathname === '/api/git/status') {
|
|
274
|
+
reply(req, res, 200, 'application/json; charset=utf-8', JSON.stringify(await git.status()));
|
|
275
|
+
return;
|
|
276
|
+
}
|
|
277
|
+
const revision = url.searchParams.get('revision');
|
|
278
|
+
if (revision !== 'HEAD' && revision !== 'index')
|
|
279
|
+
throw new FileUnavailable();
|
|
280
|
+
reply(req, res, 200, 'text/plain; charset=utf-8', await git.read(url.searchParams.get('path') ?? '', revision));
|
|
281
|
+
return;
|
|
282
|
+
}
|
|
254
283
|
if (url.pathname === '/preview-html') {
|
|
255
284
|
const relative = url.searchParams.get('path') ?? '';
|
|
256
285
|
const file = await catalog.openFile(relative);
|
|
@@ -266,6 +295,34 @@ export async function createViewer(options) {
|
|
|
266
295
|
}
|
|
267
296
|
return;
|
|
268
297
|
}
|
|
298
|
+
if (url.pathname === '/office-pdf') {
|
|
299
|
+
const relative = url.searchParams.get('path') ?? '';
|
|
300
|
+
const file = await catalog.openFile(relative);
|
|
301
|
+
let bytes;
|
|
302
|
+
try {
|
|
303
|
+
if (file.kind !== 'office')
|
|
304
|
+
throw new FileUnavailable();
|
|
305
|
+
if (!office)
|
|
306
|
+
throw new OfficeConversionError(OFFICE_UNAVAILABLE, 503);
|
|
307
|
+
// Convert the validated descriptor's contents, never a path the converter reopens.
|
|
308
|
+
bytes = await file.handle.readFile();
|
|
309
|
+
}
|
|
310
|
+
finally {
|
|
311
|
+
await file.handle.close();
|
|
312
|
+
}
|
|
313
|
+
const filename = await office.pdf(bytes, path.extname(file.filename).toLowerCase());
|
|
314
|
+
const handle = await open(filename, constants.O_RDONLY);
|
|
315
|
+
let size;
|
|
316
|
+
try {
|
|
317
|
+
size = (await handle.stat()).size;
|
|
318
|
+
}
|
|
319
|
+
catch (error) {
|
|
320
|
+
await handle.close();
|
|
321
|
+
throw error;
|
|
322
|
+
}
|
|
323
|
+
await sendProjectFile(req, res, { handle, size, filename, kind: 'pdf' }, relative, false, previewCsp, allowHtmlNetwork);
|
|
324
|
+
return;
|
|
325
|
+
}
|
|
269
326
|
if (url.pathname === '/file' || url.pathname.startsWith('/asset/')) {
|
|
270
327
|
const asset = url.pathname.startsWith('/asset/');
|
|
271
328
|
const relative = asset
|
|
@@ -306,6 +363,10 @@ export async function createViewer(options) {
|
|
|
306
363
|
res.destroy();
|
|
307
364
|
return;
|
|
308
365
|
}
|
|
366
|
+
if (error instanceof OfficeConversionError) {
|
|
367
|
+
reply(req, res, error.status, 'text/plain; charset=utf-8', error.message + '\n');
|
|
368
|
+
return;
|
|
369
|
+
}
|
|
309
370
|
const status = error instanceof URIError
|
|
310
371
|
? 400
|
|
311
372
|
: error instanceof FileUnavailable ||
|
|
@@ -325,6 +386,7 @@ export async function createViewer(options) {
|
|
|
325
386
|
server.headersTimeout = 10_000;
|
|
326
387
|
server.on('close', () => {
|
|
327
388
|
watcher?.close();
|
|
389
|
+
void office?.close();
|
|
328
390
|
clearTimeout(reloadTimer);
|
|
329
391
|
for (const client of eventClients)
|
|
330
392
|
client.end();
|
|
@@ -334,5 +396,5 @@ export async function createViewer(options) {
|
|
|
334
396
|
watcher.close();
|
|
335
397
|
server.emit('watchError', error);
|
|
336
398
|
});
|
|
337
|
-
return { server, catalog };
|
|
399
|
+
return { server, catalog, office };
|
|
338
400
|
}
|
package/dist/shared.js
CHANGED