1 | import _defineProperty from "@babel/runtime/helpers/defineProperty";
|
2 | import { Readable, pipeline } from 'stream';
|
3 | import { join as pathJoin, dirname, basename, resolve } from 'path';
|
4 | import { promisify } from 'util';
|
5 | import fse from 'fs-extra';
|
6 | import { fsLchmodSupported, fsLchmod, fsLutimesSupported, fsLutimes, fsWalk, fsLstatExists } from '@shockpkg/archive-files';
|
7 | import { once } from "./util.mjs";
|
8 | import { Queue } from "./queue.mjs";
|
9 | const pipelineP = promisify(pipeline);
|
10 | const userExec = 0b001000000;
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 | export class Bundle extends Object {
|
21 | |
22 |
|
23 |
|
24 |
|
25 | |
26 |
|
27 |
|
28 |
|
29 | |
30 |
|
31 |
|
32 |
|
33 | |
34 |
|
35 |
|
36 |
|
37 | |
38 |
|
39 |
|
40 | constructor(path) {
|
41 | super();
|
42 |
|
43 | _defineProperty(this, "excludes", [/^\./, /^ehthumbs\.db$/, /^Thumbs\.db$/]);
|
44 |
|
45 | _defineProperty(this, "path", void 0);
|
46 |
|
47 | _defineProperty(this, "projector", void 0);
|
48 |
|
49 | _defineProperty(this, "_isOpen", false);
|
50 |
|
51 | _defineProperty(this, "_closeQueue", new Queue());
|
52 |
|
53 | this.path = path;
|
54 | }
|
55 | |
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 | get isOpen() {
|
63 | return this._isOpen;
|
64 | }
|
65 | |
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 | isExcludedFile(name) {
|
74 | for (const exclude of this.excludes) {
|
75 | if (exclude.test(name)) {
|
76 | return true;
|
77 | }
|
78 | }
|
79 |
|
80 | return false;
|
81 | }
|
82 | |
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 | async openFile(player, movieFile) {
|
91 | const movieData = movieFile ? await fse.readFile(movieFile) : null;
|
92 | await this.openData(player, movieData);
|
93 | }
|
94 | |
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 | async openData(player, movieData) {
|
103 | if (this._isOpen) {
|
104 | throw new Error('Already open');
|
105 | }
|
106 |
|
107 | await this._checkOutput();
|
108 |
|
109 | this._closeQueue.clear();
|
110 |
|
111 | await this._openData(player, movieData);
|
112 | this._isOpen = true;
|
113 | }
|
114 | |
115 |
|
116 |
|
117 |
|
118 |
|
119 | async close() {
|
120 | this._assertIsOpen();
|
121 |
|
122 | try {
|
123 | await this._close();
|
124 | } finally {
|
125 | this._closeQueue.clear();
|
126 | }
|
127 |
|
128 | this._isOpen = false;
|
129 | }
|
130 | |
131 |
|
132 |
|
133 |
|
134 |
|
135 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 |
|
141 | async withFile(player, movieFile, func = null) {
|
142 | const movieData = movieFile ? await fse.readFile(movieFile) : null;
|
143 | return this.withData(player, movieData, func);
|
144 | }
|
145 | |
146 |
|
147 |
|
148 |
|
149 |
|
150 |
|
151 |
|
152 |
|
153 |
|
154 |
|
155 |
|
156 | async withData(player, movieData, func = null) {
|
157 | await this.openData(player, movieData);
|
158 |
|
159 | try {
|
160 | return func ? await func.call(this, this) : null;
|
161 | } finally {
|
162 | await this.close();
|
163 | }
|
164 | }
|
165 | |
166 |
|
167 |
|
168 |
|
169 |
|
170 |
|
171 |
|
172 |
|
173 | resourcePath(destination) {
|
174 | return pathJoin(dirname(this.projector.path), destination);
|
175 | }
|
176 | |
177 |
|
178 |
|
179 |
|
180 |
|
181 |
|
182 |
|
183 |
|
184 | async resourceExists(destination) {
|
185 | return !!fsLstatExists(this.resourcePath(destination));
|
186 | }
|
187 | |
188 |
|
189 |
|
190 |
|
191 |
|
192 |
|
193 |
|
194 |
|
195 |
|
196 | async copyResource(destination, source, options = null) {
|
197 | this._assertIsOpen();
|
198 |
|
199 | const stat = await fse.lstat(source);
|
200 |
|
201 | switch (true) {
|
202 | case stat.isSymbolicLink():
|
203 | {
|
204 | await this.copyResourceSymlink(destination, source, options);
|
205 | break;
|
206 | }
|
207 |
|
208 | case stat.isFile():
|
209 | {
|
210 | await this.copyResourceFile(destination, source, options);
|
211 | break;
|
212 | }
|
213 |
|
214 | case stat.isDirectory():
|
215 | {
|
216 | await this.copyResourceDirectory(destination, source, options);
|
217 | break;
|
218 | }
|
219 |
|
220 | default:
|
221 | {
|
222 | throw new Error(`Unsupported resource type: ${source}`);
|
223 | }
|
224 | }
|
225 | }
|
226 | |
227 |
|
228 |
|
229 |
|
230 |
|
231 |
|
232 |
|
233 |
|
234 |
|
235 | async copyResourceDirectory(destination, source, options = null) {
|
236 | this._assertIsOpen();
|
237 |
|
238 |
|
239 | await this.createResourceDirectory(destination, options ? await this._expandResourceOptionsCopy(options, async () => fse.stat(source)) : options);
|
240 |
|
241 | if (options && options.noRecurse) {
|
242 | return;
|
243 | }
|
244 |
|
245 |
|
246 | const opts = { ...(options || {}),
|
247 | noRecurse: true
|
248 | };
|
249 | await fsWalk(source, async (path, stat) => {
|
250 |
|
251 | if (this.isExcludedFile(basename(path))) {
|
252 | return false;
|
253 | }
|
254 |
|
255 | await this.copyResource(pathJoin(destination, path), pathJoin(source, path), opts);
|
256 | return true;
|
257 | });
|
258 | }
|
259 | |
260 |
|
261 |
|
262 |
|
263 |
|
264 |
|
265 |
|
266 |
|
267 |
|
268 | async copyResourceFile(destination, source, options = null) {
|
269 | this._assertIsOpen();
|
270 |
|
271 | await this.streamResourceFile(destination, fse.createReadStream(source), options ? await this._expandResourceOptionsCopy(options, async () => fse.stat(source)) : options);
|
272 | }
|
273 | |
274 |
|
275 |
|
276 |
|
277 |
|
278 |
|
279 |
|
280 |
|
281 |
|
282 | async copyResourceSymlink(destination, source, options = null) {
|
283 | this._assertIsOpen();
|
284 |
|
285 | await this.createResourceSymlink(destination, await fse.readlink(source), options ? await this._expandResourceOptionsCopy(options, async () => fse.lstat(source)) : options);
|
286 | }
|
287 | |
288 |
|
289 |
|
290 |
|
291 |
|
292 |
|
293 |
|
294 |
|
295 | async createResourceDirectory(destination, options = null) {
|
296 | this._assertIsOpen();
|
297 |
|
298 | const dest = await this._assertNotResourceExists(destination, !!(options && options.merge));
|
299 | await fse.ensureDir(dest);
|
300 |
|
301 | if (options && (options.atime || options.mtime)) {
|
302 |
|
303 |
|
304 | const abs = resolve(dest);
|
305 |
|
306 | this._closeQueue.push(this._setResourceAttributes.bind(this, abs, { ...options
|
307 | }), abs.length);
|
308 | }
|
309 | }
|
310 | |
311 |
|
312 |
|
313 |
|
314 |
|
315 |
|
316 |
|
317 |
|
318 |
|
319 | async createResourceFile(destination, data, options = null) {
|
320 | this._assertIsOpen();
|
321 |
|
322 | await this.streamResourceFile(destination, new Readable({
|
323 | read() {
|
324 | this.push(data);
|
325 | this.push(null);
|
326 | }
|
327 |
|
328 | }), options);
|
329 | }
|
330 | |
331 |
|
332 |
|
333 |
|
334 |
|
335 |
|
336 |
|
337 |
|
338 |
|
339 | async createResourceSymlink(destination, target, options = null) {
|
340 | this._assertIsOpen();
|
341 |
|
342 | const dest = await this._assertNotResourceExists(destination);
|
343 | await fse.ensureDir(dirname(dest));
|
344 | await fse.symlink(target, dest);
|
345 |
|
346 | if (options) {
|
347 | await this._setResourceAttributes(dest, options);
|
348 | }
|
349 | }
|
350 | |
351 |
|
352 |
|
353 |
|
354 |
|
355 |
|
356 |
|
357 |
|
358 |
|
359 | async streamResourceFile(destination, data, options = null) {
|
360 | this._assertIsOpen();
|
361 |
|
362 | const dest = await this._assertNotResourceExists(destination);
|
363 | await fse.ensureDir(dirname(dest));
|
364 | await pipelineP(data, fse.createWriteStream(dest));
|
365 |
|
366 | if (options) {
|
367 | await this._setResourceAttributes(dest, options);
|
368 | }
|
369 | }
|
370 | |
371 |
|
372 |
|
373 |
|
374 |
|
375 | async _checkOutput() {
|
376 | for (const p of [this.path, this.resourcePath('')]) {
|
377 |
|
378 | if (await fse.pathExists(p)) {
|
379 | throw new Error(`Output path already exists: ${p}`);
|
380 | }
|
381 | }
|
382 | }
|
383 | |
384 |
|
385 |
|
386 |
|
387 |
|
388 |
|
389 |
|
390 |
|
391 |
|
392 | async _expandResourceOptionsCopy(options, stat) {
|
393 | const r = { ...options
|
394 | };
|
395 | const st = once(stat);
|
396 |
|
397 | if (!r.atime && r.atimeCopy) {
|
398 | r.atime = (await st()).atime;
|
399 | }
|
400 |
|
401 | if (!r.mtime && r.mtimeCopy) {
|
402 | r.mtime = (await st()).mtime;
|
403 | }
|
404 |
|
405 | if (typeof r.executable !== 'boolean' && r.executableCopy) {
|
406 | r.executable = this._getResourceModeExecutable((await st()).mode);
|
407 | }
|
408 |
|
409 | return r;
|
410 | }
|
411 | |
412 |
|
413 |
|
414 |
|
415 |
|
416 |
|
417 |
|
418 |
|
419 | async _setResourceAttributes(path, options) {
|
420 | const {
|
421 | atime,
|
422 | mtime,
|
423 | executable
|
424 | } = options;
|
425 | const st = await fse.lstat(path);
|
426 |
|
427 | if (typeof executable === 'boolean' && !st.isDirectory()) {
|
428 | if (!st.isSymbolicLink()) {
|
429 | await fse.chmod(path, this._setResourceModeExecutable(st.mode, executable));
|
430 | } else if (fsLchmodSupported) {
|
431 | await fsLchmod(path, this._setResourceModeExecutable(
|
432 |
|
433 | st.mode & 0b111111111, executable));
|
434 | }
|
435 | }
|
436 |
|
437 |
|
438 | if (atime || mtime) {
|
439 | if (!st.isSymbolicLink()) {
|
440 | await fse.utimes(path, atime || st.atime, mtime || st.mtime);
|
441 | } else if (fsLutimesSupported) {
|
442 | await fsLutimes(path, atime || st.atime, mtime || st.mtime);
|
443 | }
|
444 | }
|
445 | }
|
446 | |
447 |
|
448 |
|
449 |
|
450 |
|
451 |
|
452 |
|
453 |
|
454 | _getResourceModeExecutable(mode) {
|
455 |
|
456 | return !!(mode & userExec);
|
457 | }
|
458 | |
459 |
|
460 |
|
461 |
|
462 |
|
463 |
|
464 |
|
465 |
|
466 |
|
467 | _setResourceModeExecutable(mode, executable) {
|
468 |
|
469 | return (executable ? mode | userExec : mode & ~userExec) >>> 0;
|
470 | }
|
471 | |
472 |
|
473 |
|
474 |
|
475 |
|
476 |
|
477 |
|
478 |
|
479 | async _openData(player, movieData) {
|
480 | await this.projector.withData(player, movieData);
|
481 | }
|
482 | |
483 |
|
484 |
|
485 |
|
486 |
|
487 | async _close() {
|
488 | await this._writeLauncher();
|
489 | await this._closeQueue.run();
|
490 | }
|
491 | |
492 |
|
493 |
|
494 |
|
495 |
|
496 | _assertIsOpen() {
|
497 | if (!this._isOpen) {
|
498 | throw new Error('Not open');
|
499 | }
|
500 | }
|
501 | |
502 |
|
503 |
|
504 |
|
505 |
|
506 |
|
507 |
|
508 |
|
509 |
|
510 | async _assertNotResourceExists(destination, ignoreDirectory = false) {
|
511 | const dest = this.resourcePath(destination);
|
512 | const st = await fsLstatExists(dest);
|
513 |
|
514 | if (st && (!ignoreDirectory || !st.isDirectory())) {
|
515 | throw new Error(`Resource path exists: ${dest}`);
|
516 | }
|
517 |
|
518 | return dest;
|
519 | }
|
520 | |
521 |
|
522 |
|
523 |
|
524 |
|
525 |
|
526 |
|
527 | }
|
528 |
|