forked from codesandbox/codesandbox-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEmscripten.ts
More file actions
386 lines (360 loc) · 9.92 KB
/
Emscripten.ts
File metadata and controls
386 lines (360 loc) · 9.92 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
import {SynchronousFileSystem, BFSOneArgCallback, BFSCallback, BFSThreeArgCallback, FileSystemOptions} from '../core/file_system';
import {default as Stats, FileType} from '../core/node_fs_stats';
import {FileFlag} from '../core/file_flag';
import {BaseFile, File} from '../core/file';
import {uint8Array2Buffer, buffer2Uint8array} from '../core/util';
import {ApiError, ErrorCode, ErrorStrings} from '../core/api_error';
import {EmscriptenFSNode} from '../generic/emscripten_fs';
/**
* @hidden
*/
interface EmscriptenError {
node: EmscriptenFSNode;
errno: number;
}
/**
* @hidden
*/
function convertError(e: EmscriptenError, path: string = ''): ApiError {
const errno = e.errno;
let parent = e.node;
const paths: string[] = [];
while (parent) {
paths.unshift(parent.name);
if (parent === parent.parent) {
break;
}
parent = parent.parent;
}
return new ApiError(errno, ErrorStrings[errno], paths.length > 0 ? '/' + paths.join('/') : path);
}
export class EmscriptenFile extends BaseFile implements File {
constructor(
private _fs: EmscriptenFileSystem,
private _FS: any,
private _path: string,
private _stream: any) {
super();
}
public getPos(): number | undefined {
return undefined;
}
public close(cb: BFSOneArgCallback): void {
let err: ApiError | null = null;
try {
this.closeSync();
} catch (e) {
err = e;
} finally {
cb(err);
}
}
public closeSync(): void {
try {
this._FS.close(this._stream);
} catch (e) {
throw convertError(e, this._path);
}
}
public stat(cb: BFSCallback<Stats>): void {
try {
cb(null, this.statSync());
} catch (e) {
cb(e);
}
}
public statSync(): Stats {
try {
return this._fs.statSync(this._path, false);
} catch (e) {
throw convertError(e, this._path);
}
}
public truncate(len: number, cb: BFSOneArgCallback): void {
let err: ApiError | null = null;
try {
this.truncateSync(len);
} catch (e) {
err = e;
} finally {
cb(err);
}
}
public truncateSync(len: number): void {
try {
this._FS.ftruncate(this._stream.fd, len);
} catch (e) {
throw convertError(e, this._path);
}
}
public write(buffer: Buffer, offset: number, length: number, position: number, cb: BFSThreeArgCallback<number, Buffer>): void {
try {
cb(null, this.writeSync(buffer, offset, length, position), buffer);
} catch (e) {
cb(e);
}
}
public writeSync(buffer: Buffer, offset: number, length: number, position: number | null): number {
try {
const u8 = buffer2Uint8array(buffer);
// Emscripten is particular about what position is set to.
const emPosition = position === null ? undefined : position;
return this._FS.write(this._stream, u8, offset, length, emPosition);
} catch (e) {
throw convertError(e, this._path);
}
}
public read(buffer: Buffer, offset: number, length: number, position: number, cb: BFSThreeArgCallback<number, Buffer>): void {
try {
cb(null, this.readSync(buffer, offset, length, position), buffer);
} catch (e) {
cb(e);
}
}
public readSync(buffer: Buffer, offset: number, length: number, position: number | null): number {
try {
const u8 = buffer2Uint8array(buffer);
// Emscripten is particular about what position is set to.
const emPosition = position === null ? undefined : position;
return this._FS.read(this._stream, u8, offset, length, emPosition);
} catch (e) {
throw convertError(e, this._path);
}
}
public sync(cb: BFSOneArgCallback): void {
// NOP.
cb();
}
public syncSync(): void {
// NOP.
}
public chown(uid: number, gid: number, cb: BFSOneArgCallback): void {
let err: ApiError | null = null;
try {
this.chownSync(uid, gid);
} catch (e) {
err = e;
} finally {
cb(err);
}
}
public chownSync(uid: number, gid: number): void {
try {
this._FS.fchown(this._stream.fd, uid, gid);
} catch (e) {
throw convertError(e, this._path);
}
}
public chmod(mode: number, cb: BFSOneArgCallback): void {
let err: ApiError | null = null;
try {
this.chmodSync(mode);
} catch (e) {
err = e;
} finally {
cb(err);
}
}
public chmodSync(mode: number): void {
try {
this._FS.fchmod(this._stream.fd, mode);
} catch (e) {
throw convertError(e, this._path);
}
}
public utimes(atime: Date, mtime: Date, cb: BFSOneArgCallback): void {
let err: ApiError | null = null;
try {
this.utimesSync(atime, mtime);
} catch (e) {
err = e;
} finally {
cb(err);
}
}
public utimesSync(atime: Date, mtime: Date): void {
this._fs.utimesSync(this._path, atime, mtime);
}
}
/**
* Configuration options for Emscripten file systems.
*/
export interface EmscriptenFileSystemOptions {
// The Emscripten file system to use (`FS`)
FS: any;
}
/**
* Mounts an Emscripten file system into the BrowserFS file system.
*/
export default class EmscriptenFileSystem extends SynchronousFileSystem {
public static readonly Name = "EmscriptenFileSystem";
public static readonly Options: FileSystemOptions = {
FS: {
type: "object",
description: "The Emscripten file system to use (the `FS` variable)"
}
};
/**
* Create an EmscriptenFileSystem instance with the given options.
*/
public static Create(opts: EmscriptenFileSystemOptions, cb: BFSCallback<EmscriptenFileSystem>): void {
cb(null, new EmscriptenFileSystem(opts.FS));
}
public static isAvailable(): boolean { return true; }
private _FS: any;
private constructor(_FS: any) {
super();
this._FS = _FS;
}
public getName(): string { return this._FS.DB_NAME(); }
public isReadOnly(): boolean { return false; }
public supportsLinks(): boolean { return true; }
public supportsProps(): boolean { return true; }
public supportsSynch(): boolean { return true; }
public renameSync(oldPath: string, newPath: string): void {
try {
this._FS.rename(oldPath, newPath);
} catch (e) {
if (e.errno === ErrorCode.ENOENT) {
throw convertError(e, this.existsSync(oldPath) ? newPath : oldPath);
} else {
throw convertError(e);
}
}
}
public statSync(p: string, isLstat: boolean): Stats {
try {
const stats = isLstat ? this._FS.lstat(p) : this._FS.stat(p);
const itemType = this.modeToFileType(stats.mode);
return new Stats(
itemType,
stats.size,
stats.mode,
stats.atime.getTime(),
stats.mtime.getTime(),
stats.ctime.getTime()
);
} catch (e) {
throw convertError(e, p);
}
}
public openSync(p: string, flag: FileFlag, mode: number): EmscriptenFile {
try {
const stream = this._FS.open(p, flag.getFlagString(), mode);
if (this._FS.isDir(stream.node.mode)) {
this._FS.close(stream);
throw ApiError.EISDIR(p);
}
return new EmscriptenFile(this, this._FS, p, stream);
} catch (e) {
throw convertError(e, p);
}
}
public unlinkSync(p: string): void {
try {
this._FS.unlink(p);
} catch (e) {
throw convertError(e, p);
}
}
public rmdirSync(p: string): void {
try {
this._FS.rmdir(p);
} catch (e) {
throw convertError(e, p);
}
}
public mkdirSync(p: string, mode: number): void {
try {
this._FS.mkdir(p, mode);
} catch (e) {
throw convertError(e, p);
}
}
public readdirSync(p: string): string[] {
try {
// Emscripten returns items for '.' and '..'. Node does not.
return this._FS.readdir(p).filter((p: string) => p !== '.' && p !== '..');
} catch (e) {
throw convertError(e, p);
}
}
public truncateSync(p: string, len: number): void {
try {
this._FS.truncate(p, len);
} catch (e) {
throw convertError(e, p);
}
}
public readFileSync(p: string, encoding: string, flag: FileFlag): any {
try {
const data: Uint8Array = this._FS.readFile(p, { flags: flag.getFlagString() });
const buff = uint8Array2Buffer(data);
if (encoding) {
return buff.toString(encoding);
} else {
return buff;
}
} catch (e) {
throw convertError(e, p);
}
}
public writeFileSync(p: string, data: any, encoding: string, flag: FileFlag, mode: number): void {
try {
if (encoding) {
data = Buffer.from(data, encoding);
}
const u8 = buffer2Uint8array(data);
this._FS.writeFile(p, u8, { flags: flag.getFlagString(), encoding: 'binary' });
this._FS.chmod(p, mode);
} catch (e) {
throw convertError(e, p);
}
}
public chmodSync(p: string, isLchmod: boolean, mode: number) {
try {
isLchmod ? this._FS.lchmod(p, mode) : this._FS.chmod(p, mode);
} catch (e) {
throw convertError(e, p);
}
}
public chownSync(p: string, isLchown: boolean, uid: number, gid: number): void {
try {
isLchown ? this._FS.lchown(p, uid, gid) : this._FS.chown(p, uid, gid);
} catch (e) {
throw convertError(e, p);
}
}
public symlinkSync(srcpath: string, dstpath: string, type: string): void {
try {
this._FS.symlink(srcpath, dstpath);
} catch (e) {
throw convertError(e);
}
}
public readlinkSync(p: string): string {
try {
return this._FS.readlink(p);
} catch (e) {
throw convertError(e, p);
}
}
public utimesSync(p: string, atime: Date, mtime: Date): void {
try {
this._FS.utime(p, atime.getTime(), mtime.getTime());
} catch (e) {
throw convertError(e, p);
}
}
private modeToFileType(mode: number): FileType {
if (this._FS.isDir(mode)) {
return FileType.DIRECTORY;
} else if (this._FS.isFile(mode)) {
return FileType.FILE;
} else if (this._FS.isLink(mode)) {
return FileType.SYMLINK;
} else {
throw ApiError.EPERM(`Invalid mode: ${mode}`);
}
}
}