UNPKG

4.7 kBJavaScriptView Raw
1"use strict";
2
3var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
5Object.defineProperty(exports, "__esModule", {
6 value: true
7});
8exports.Projector = void 0;
9
10var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
11
12var _archiveFiles = require("@shockpkg/archive-files");
13
14var _fsExtra = _interopRequireDefault(require("fs-extra"));
15
16/**
17 * Projector constructor.
18 *
19 * @param path Output path.
20 */
21class Projector extends Object {
22 /**
23 * Path to hdiutil binary.
24 */
25
26 /**
27 * Output path.
28 */
29 constructor(path) {
30 super();
31 (0, _defineProperty2.default)(this, "hdiutil", null);
32 (0, _defineProperty2.default)(this, "path", void 0);
33 this.path = path;
34 }
35 /**
36 * The movie appended marker.
37 *
38 * @returns Hex string.
39 */
40
41
42 get movieAppendMarker() {
43 return '563412FA';
44 }
45 /**
46 * Get the player file or directory as an Archive instance.
47 *
48 * @param path File path.
49 * @returns Archive instance.
50 */
51
52
53 async openAsArchive(path) {
54 const stat = await _fsExtra.default.stat(path);
55
56 if (stat.isDirectory()) {
57 return new _archiveFiles.ArchiveDir(path);
58 }
59
60 if (!stat.isFile()) {
61 throw new Error(`Archive path not file or directory: ${path}`);
62 }
63
64 const r = (0, _archiveFiles.createArchiveByFileExtension)(path);
65
66 if (!r) {
67 throw new Error(`Archive file type unknown: ${path}`);
68 }
69
70 if (r instanceof _archiveFiles.ArchiveHdi) {
71 const {
72 hdiutil
73 } = this;
74
75 if (hdiutil) {
76 r.mounterMac.hdiutil = hdiutil;
77 }
78
79 r.nobrowse = true;
80 }
81
82 return r;
83 }
84 /**
85 * Write out projector with player and file.
86 *
87 * @param player Player path.
88 * @param movieFile Movie file.
89 */
90
91
92 async withFile(player, movieFile) {
93 const movieData = movieFile ? await _fsExtra.default.readFile(movieFile) : null;
94 await this.withData(player, movieData);
95 }
96 /**
97 * Write out projector with player and data.
98 *
99 * @param player Player path.
100 * @param movieData Movie data.
101 */
102
103
104 async withData(player, movieData) {
105 await this._checkOutput();
106 await this._writePlayer(player);
107 await this._modifyPlayer();
108 await this._writeMovie(movieData);
109 }
110 /**
111 * Check that output path is valid, else throws.
112 */
113
114
115 async _checkOutput() {
116 if (await _fsExtra.default.pathExists(this.path)) {
117 throw new Error(`Output path already exists: ${this.path}`);
118 }
119 }
120 /**
121 * Append movie data to a file.
122 * Format string characters:
123 * - d: Movie data.
124 * - m: Marker bytes.
125 * - s: Size, 32LE.
126 * - S: Size, 32BE.
127 * - l: Size, 64LE.
128 * - L: Size, 64BE.
129 *
130 * @param file File to append to.
131 * @param data Movie data.
132 * @param format Format string.
133 */
134
135
136 async _appendMovieData(file, data, format) {
137 const buffers = [];
138
139 for (const c of format) {
140 switch (c) {
141 case 'd':
142 {
143 buffers.push(data);
144 break;
145 }
146
147 case 'm':
148 {
149 buffers.push(Buffer.from(this.movieAppendMarker, 'hex'));
150 break;
151 }
152
153 case 's':
154 {
155 const b = Buffer.alloc(4);
156 b.writeUInt32LE(data.length, 0);
157 buffers.push(b);
158 break;
159 }
160
161 case 'S':
162 {
163 const b = Buffer.alloc(4);
164 b.writeUInt32BE(data.length, 0);
165 buffers.push(b);
166 break;
167 }
168
169 case 'l':
170 {
171 // 64-bit, just write 32-bit, SWF cannot be larger.
172 const b = Buffer.alloc(8, 0);
173 b.writeUInt32LE(data.length, 0);
174 buffers.push(b);
175 break;
176 }
177
178 case 'L':
179 {
180 // 64-bit, just write 32-bit, SWF cannot be larger.
181 const b = Buffer.alloc(8, 0);
182 b.writeUInt32BE(data.length, 4);
183 buffers.push(b);
184 break;
185 }
186
187 default:
188 {
189 throw new Error(`Unknown format string character: ${c}`);
190 }
191 }
192 }
193
194 const stat = await _fsExtra.default.stat(file);
195
196 if (!stat.isFile()) {
197 throw new Error(`Path not a file: ${file}`);
198 }
199
200 const fd = await _fsExtra.default.open(file, 'a');
201
202 try {
203 for (const b of buffers) {
204 // eslint-disable-next-line no-await-in-loop
205 await _fsExtra.default.appendFile(fd, b);
206 }
207 } finally {
208 await _fsExtra.default.close(fd);
209 }
210 }
211 /**
212 * Projector file extension.
213 *
214 * @returns File extension.
215 */
216
217
218}
219
220exports.Projector = Projector;
221//# sourceMappingURL=projector.js.map