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