forked from codesandbox/codesandbox-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHTTPRequest.ts
More file actions
445 lines (411 loc) · 14 KB
/
HTTPRequest.ts
File metadata and controls
445 lines (411 loc) · 14 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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
import {BaseFileSystem, FileSystem, BFSCallback, FileSystemOptions} from '../core/file_system';
import {ApiError, ErrorCode} from '../core/api_error';
import {FileFlag, ActionType} from '../core/file_flag';
import {copyingSlice} from '../core/util';
import {File} from '../core/file';
import Stats from '../core/node_fs_stats';
import {NoSyncFile} from '../generic/preload_file';
import {xhrIsAvailable, asyncDownloadFile, syncDownloadFile, getFileSizeAsync, getFileSizeSync} from '../generic/xhr';
import {fetchIsAvailable, fetchFileAsync, fetchFileSizeAsync} from '../generic/fetch';
import {FileIndex, isFileInode, isDirInode} from '../generic/file_index';
/**
* Try to convert the given buffer into a string, and pass it to the callback.
* Optimization that removes the needed try/catch into a helper function, as
* this is an uncommon case.
* @hidden
*/
function tryToString(buff: Buffer, encoding: string, cb: BFSCallback<string>) {
try {
cb(null, buff.toString(encoding));
} catch (e) {
cb(e);
}
}
/**
* Configuration options for a HTTPRequest file system.
*/
export interface HTTPRequestOptions {
// URL to a file index as a JSON file or the file index object itself, generated with the make_http_index script.
// Defaults to `index.json`.
index?: string | object;
// Used as the URL prefix for fetched files.
// Default: Fetch files relative to the index.
baseUrl?: string;
// Whether to prefer XmlHttpRequest or fetch for async operations if both are available.
// Default: false
preferXHR?: boolean;
}
interface AsyncDownloadFileMethod {
(p: string, type: 'buffer', cb: BFSCallback<Buffer>): void;
(p: string, type: 'json', cb: BFSCallback<any>): void;
(p: string, type: string, cb: BFSCallback<any>): void;
}
interface SyncDownloadFileMethod {
(p: string, type: 'buffer'): Buffer;
(p: string, type: 'json'): any;
(p: string, type: string): any;
}
function syncNotAvailableError(): never {
throw new ApiError(ErrorCode.ENOTSUP, `Synchronous HTTP download methods are not available in this environment.`);
}
/**
* A simple filesystem backed by HTTP downloads. You must create a directory listing using the
* `make_http_index` tool provided by BrowserFS.
*
* If you install BrowserFS globally with `npm i -g browserfs`, you can generate a listing by
* running `make_http_index` in your terminal in the directory you would like to index:
*
* ```
* make_http_index > index.json
* ```
*
* Listings objects look like the following:
*
* ```json
* {
* "home": {
* "jvilk": {
* "someFile.txt": null,
* "someDir": {
* // Empty directory
* }
* }
* }
* }
* ```
*
* *This example has the folder `/home/jvilk` with subfile `someFile.txt` and subfolder `someDir`.*
*/
export default class HTTPRequest extends BaseFileSystem implements FileSystem {
public static readonly Name = "HTTPRequest";
public static readonly Options: FileSystemOptions = {
index: {
type: ["string", "object"],
optional: true,
description: "URL to a file index as a JSON file or the file index object itself, generated with the make_http_index script. Defaults to `index.json`."
},
baseUrl: {
type: "string",
optional: true,
description: "Used as the URL prefix for fetched files. Default: Fetch files relative to the index."
},
preferXHR: {
type: "boolean",
optional: true,
description: "Whether to prefer XmlHttpRequest or fetch for async operations if both are available. Default: false"
}
};
/**
* Construct an HTTPRequest file system backend with the given options.
*/
public static Create(opts: HTTPRequestOptions, cb: BFSCallback<HTTPRequest>): void {
if (opts.index === undefined) {
opts.index = `index.json`;
}
if (typeof(opts.index) === "string") {
asyncDownloadFile(opts.index, "json", (e, data?) => {
if (e) {
cb(e);
} else {
cb(null, new HTTPRequest(data, opts.baseUrl));
}
});
} else {
cb(null, new HTTPRequest(opts.index, opts.baseUrl));
}
}
public static isAvailable(): boolean {
return xhrIsAvailable || fetchIsAvailable;
}
public readonly prefixUrl: string;
private _index: FileIndex<{}>;
private _requestFileAsyncInternal: AsyncDownloadFileMethod;
private _requestFileSizeAsyncInternal: (p: string, cb: BFSCallback<number>) => void;
private _requestFileSyncInternal: SyncDownloadFileMethod;
private _requestFileSizeSyncInternal: (p: string) => number;
private constructor(index: object, prefixUrl: string = '', preferXHR: boolean = false) {
super();
// prefix_url must end in a directory separator.
if (prefixUrl.length > 0 && prefixUrl.charAt(prefixUrl.length - 1) !== '/') {
prefixUrl = prefixUrl + '/';
}
this.prefixUrl = prefixUrl;
this._index = FileIndex.fromListing(index);
if (fetchIsAvailable && (!preferXHR || !xhrIsAvailable)) {
this._requestFileAsyncInternal = fetchFileAsync;
this._requestFileSizeAsyncInternal = fetchFileSizeAsync;
} else {
this._requestFileAsyncInternal = asyncDownloadFile;
this._requestFileSizeAsyncInternal = getFileSizeAsync;
}
if (xhrIsAvailable) {
this._requestFileSyncInternal = syncDownloadFile;
this._requestFileSizeSyncInternal = getFileSizeSync;
} else {
this._requestFileSyncInternal = syncNotAvailableError;
this._requestFileSizeSyncInternal = syncNotAvailableError;
}
}
public empty(): void {
this._index.fileIterator(function(file: Stats) {
file.fileData = null;
});
}
public getName(): string {
return HTTPRequest.Name;
}
public diskSpace(path: string, cb: (total: number, free: number) => void): void {
// Read-only file system. We could calculate the total space, but that's not
// important right now.
cb(0, 0);
}
public isReadOnly(): boolean {
return true;
}
public supportsLinks(): boolean {
return false;
}
public supportsProps(): boolean {
return false;
}
public supportsSynch(): boolean {
// Synchronous operations are only available via the XHR interface for now.
return xhrIsAvailable;
}
/**
* Special HTTPFS function: Preload the given file into the index.
* @param [String] path
* @param [BrowserFS.Buffer] buffer
*/
public preloadFile(path: string, buffer: Buffer): void {
const inode = this._index.getInode(path);
if (isFileInode<Stats>(inode)) {
if (inode === null) {
throw ApiError.ENOENT(path);
}
const stats = inode.getData();
stats.size = buffer.length;
stats.fileData = buffer;
} else {
throw ApiError.EISDIR(path);
}
}
public stat(path: string, isLstat: boolean, cb: BFSCallback<Stats>): void {
const inode = this._index.getInode(path);
if (inode === null) {
return cb(ApiError.ENOENT(path));
}
let stats: Stats;
if (isFileInode<Stats>(inode)) {
stats = inode.getData();
// At this point, a non-opened file will still have default stats from the listing.
if (stats.size < 0) {
this._requestFileSizeAsync(path, function(e: ApiError, size?: number) {
if (e) {
return cb(e);
}
stats.size = size!;
cb(null, Stats.clone(stats));
});
} else {
cb(null, Stats.clone(stats));
}
} else if (isDirInode(inode)) {
stats = inode.getStats();
cb(null, stats);
} else {
cb(ApiError.FileError(ErrorCode.EINVAL, path));
}
}
public statSync(path: string, isLstat: boolean): Stats {
const inode = this._index.getInode(path);
if (inode === null) {
throw ApiError.ENOENT(path);
}
let stats: Stats;
if (isFileInode<Stats>(inode)) {
stats = inode.getData();
// At this point, a non-opened file will still have default stats from the listing.
if (stats.size < 0) {
stats.size = this._requestFileSizeSync(path);
}
} else if (isDirInode(inode)) {
stats = inode.getStats();
} else {
throw ApiError.FileError(ErrorCode.EINVAL, path);
}
return stats;
}
public open(path: string, flags: FileFlag, mode: number, cb: BFSCallback<File>): void {
// INVARIANT: You can't write to files on this file system.
if (flags.isWriteable()) {
return cb(new ApiError(ErrorCode.EPERM, path));
}
const self = this;
// Check if the path exists, and is a file.
const inode = this._index.getInode(path);
if (inode === null) {
return cb(ApiError.ENOENT(path));
}
if (isFileInode<Stats>(inode)) {
const stats = inode.getData();
switch (flags.pathExistsAction()) {
case ActionType.THROW_EXCEPTION:
case ActionType.TRUNCATE_FILE:
return cb(ApiError.EEXIST(path));
case ActionType.NOP:
// Use existing file contents.
// XXX: Uh, this maintains the previously-used flag.
if (stats.fileData) {
return cb(null, new NoSyncFile(self, path, flags, Stats.clone(stats), stats.fileData));
}
// @todo be lazier about actually requesting the file
this._requestFileAsync(path, 'buffer', function(err: ApiError, buffer?: Buffer) {
if (err) {
return cb(err);
}
// we don't initially have file sizes
stats.size = buffer!.length;
stats.fileData = buffer!;
return cb(null, new NoSyncFile(self, path, flags, Stats.clone(stats), buffer));
});
break;
default:
return cb(new ApiError(ErrorCode.EINVAL, 'Invalid FileMode object.'));
}
} else {
return cb(ApiError.EISDIR(path));
}
}
public openSync(path: string, flags: FileFlag, mode: number): File {
// INVARIANT: You can't write to files on this file system.
if (flags.isWriteable()) {
throw new ApiError(ErrorCode.EPERM, path);
}
// Check if the path exists, and is a file.
const inode = this._index.getInode(path);
if (inode === null) {
throw ApiError.ENOENT(path);
}
if (isFileInode<Stats>(inode)) {
const stats = inode.getData();
switch (flags.pathExistsAction()) {
case ActionType.THROW_EXCEPTION:
case ActionType.TRUNCATE_FILE:
throw ApiError.EEXIST(path);
case ActionType.NOP:
// Use existing file contents.
// XXX: Uh, this maintains the previously-used flag.
if (stats.fileData) {
return new NoSyncFile(this, path, flags, Stats.clone(stats), stats.fileData);
}
// @todo be lazier about actually requesting the file
const buffer = this._requestFileSync(path, 'buffer');
// we don't initially have file sizes
stats.size = buffer.length;
stats.fileData = buffer;
return new NoSyncFile(this, path, flags, Stats.clone(stats), buffer);
default:
throw new ApiError(ErrorCode.EINVAL, 'Invalid FileMode object.');
}
} else {
throw ApiError.EISDIR(path);
}
}
public readdir(path: string, cb: BFSCallback<string[]>): void {
try {
cb(null, this.readdirSync(path));
} catch (e) {
cb(e);
}
}
public readdirSync(path: string): string[] {
// Check if it exists.
const inode = this._index.getInode(path);
if (inode === null) {
throw ApiError.ENOENT(path);
} else if (isDirInode(inode)) {
return inode.getListing();
} else {
throw ApiError.ENOTDIR(path);
}
}
/**
* We have the entire file as a buffer; optimize readFile.
*/
public readFile(fname: string, encoding: string, flag: FileFlag, cb: BFSCallback<string | Buffer>): void {
// Wrap cb in file closing code.
const oldCb = cb;
// Get file.
this.open(fname, flag, 0x1a4, function(err: ApiError, fd?: File) {
if (err) {
return cb(err);
}
cb = function(err: ApiError, arg?: Buffer) {
fd!.close(function(err2: any) {
if (!err) {
err = err2;
}
return oldCb(err, arg);
});
};
const fdCast = <NoSyncFile<HTTPRequest>> fd;
const fdBuff = <Buffer> fdCast.getBuffer();
if (encoding === null) {
cb(err, copyingSlice(fdBuff));
} else {
tryToString(fdBuff, encoding, cb);
}
});
}
/**
* Specially-optimized readfile.
*/
public readFileSync(fname: string, encoding: string, flag: FileFlag): any {
// Get file.
const fd = this.openSync(fname, flag, 0x1a4);
try {
const fdCast = <NoSyncFile<HTTPRequest>> fd;
const fdBuff = <Buffer> fdCast.getBuffer();
if (encoding === null) {
return copyingSlice(fdBuff);
}
return fdBuff.toString(encoding);
} finally {
fd.closeSync();
}
}
private _getHTTPPath(filePath: string): string {
if (filePath.charAt(0) === '/') {
filePath = filePath.slice(1);
}
return this.prefixUrl + filePath;
}
/**
* Asynchronously download the given file.
*/
private _requestFileAsync(p: string, type: 'buffer', cb: BFSCallback<Buffer>): void;
private _requestFileAsync(p: string, type: 'json', cb: BFSCallback<any>): void;
private _requestFileAsync(p: string, type: string, cb: BFSCallback<any>): void;
private _requestFileAsync(p: string, type: string, cb: BFSCallback<any>): void {
this._requestFileAsyncInternal(this._getHTTPPath(p), type, cb);
}
/**
* Synchronously download the given file.
*/
private _requestFileSync(p: string, type: 'buffer'): Buffer;
private _requestFileSync(p: string, type: 'json'): any;
private _requestFileSync(p: string, type: string): any;
private _requestFileSync(p: string, type: string): any {
return this._requestFileSyncInternal(this._getHTTPPath(p), type);
}
/**
* Only requests the HEAD content, for the file size.
*/
private _requestFileSizeAsync(path: string, cb: BFSCallback<number>): void {
this._requestFileSizeAsyncInternal(this._getHTTPPath(path), cb);
}
private _requestFileSizeSync(path: string): number {
return this._requestFileSizeSyncInternal(this._getHTTPPath(path));
}
}