Skip to content

Commit 314b865

Browse files
fix(p2-shim): missing browser shim bits
This commit fixes an issue that was exposed when all imports are checked for existence during import. The browser shims at present not only are incomplete, but they're also missing key functions and classes that should be present. This commit unfortunately doesn't actually bring the browser implementation up to snuff -- rather we simply add the placeholders that should be there (and will fail if use is attempted at runtime). Up until now imports were not checked for existence, but the usefulness of explicit upfront errors (instead of less scruable ones later) is a good tradeoff. In the future an option to disable import checks may be added.
1 parent b3bc626 commit 314b865

File tree

3 files changed

+108
-0
lines changed

3 files changed

+108
-0
lines changed

packages/preview2-shim/lib/browser/filesystem.js

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,103 @@ export const preopens = {
325325
export const types = {
326326
Descriptor,
327327
DirectoryEntryStream,
328+
filesystemErrorCode(err) {
329+
return convertFsError(err.payload);
330+
},
328331
};
329332

333+
function convertFsError(e) {
334+
switch (e.code) {
335+
case 'EACCES':
336+
return 'access';
337+
case 'EAGAIN':
338+
case 'EWOULDBLOCK':
339+
return 'would-block';
340+
case 'EALREADY':
341+
return 'already';
342+
case 'EBADF':
343+
return 'bad-descriptor';
344+
case 'EBUSY':
345+
return 'busy';
346+
case 'EDEADLK':
347+
return 'deadlock';
348+
case 'EDQUOT':
349+
return 'quota';
350+
case 'EEXIST':
351+
return 'exist';
352+
case 'EFBIG':
353+
return 'file-too-large';
354+
case 'EILSEQ':
355+
return 'illegal-byte-sequence';
356+
case 'EINPROGRESS':
357+
return 'in-progress';
358+
case 'EINTR':
359+
return 'interrupted';
360+
case 'EINVAL':
361+
return 'invalid';
362+
case 'EIO':
363+
return 'io';
364+
case 'EISDIR':
365+
return 'is-directory';
366+
case 'ELOOP':
367+
return 'loop';
368+
case 'EMLINK':
369+
return 'too-many-links';
370+
case 'EMSGSIZE':
371+
return 'message-size';
372+
case 'ENAMETOOLONG':
373+
return 'name-too-long';
374+
case 'ENODEV':
375+
return 'no-device';
376+
case 'ENOENT':
377+
return 'no-entry';
378+
case 'ENOLCK':
379+
return 'no-lock';
380+
case 'ENOMEM':
381+
return 'insufficient-memory';
382+
case 'ENOSPC':
383+
return 'insufficient-space';
384+
case 'ENOTDIR':
385+
case 'ERR_FS_EISDIR':
386+
return 'not-directory';
387+
case 'ENOTEMPTY':
388+
return 'not-empty';
389+
case 'ENOTRECOVERABLE':
390+
return 'not-recoverable';
391+
case 'ENOTSUP':
392+
return 'unsupported';
393+
case 'ENOTTY':
394+
return 'no-tty';
395+
// windows gives this error for badly structured `//` reads
396+
// this seems like a slightly better error than unknown given
397+
// that it's a common footgun
398+
case -4094:
399+
case 'ENXIO':
400+
return 'no-such-device';
401+
case 'EOVERFLOW':
402+
return 'overflow';
403+
case 'EPERM':
404+
return 'not-permitted';
405+
case 'EPIPE':
406+
return 'pipe';
407+
case 'EROFS':
408+
return 'read-only';
409+
case 'ESPIPE':
410+
return 'invalid-seek';
411+
case 'ETXTBSY':
412+
return 'text-file-busy';
413+
case 'EXDEV':
414+
return 'cross-device';
415+
case 'UNKNOWN':
416+
switch (e.errno) {
417+
case -4094:
418+
return 'no-such-device';
419+
default:
420+
throw e;
421+
}
422+
default:
423+
throw e;
424+
}
425+
}
426+
330427
export { types as filesystemTypes };

packages/preview2-shim/lib/browser/http.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,4 +142,14 @@ export const types = {
142142
listenToFutureIncomingResponse(_f) {
143143
console.log('[types] Listen to future incoming response');
144144
},
145+
Fields: class Fields {},
146+
FutureIncomingResponse: new class FutureIncomingResponse {},
147+
IncomingBody: new class IncomingBody {},
148+
IncomingRequest: new class IncomingRequest {},
149+
IncomingResponse: new class IncomingResponse {},
150+
OutgoingBody: new class OutgoingBody {},
151+
OutgoingRequest: new class OutgoingRequest {},
152+
OutgoingResponse: new class OutgoingResponse {},
153+
RequestOptions: new class RequestOptions {},
154+
ResponseOutparam: new class ResponseOutparam {},
145155
};

packages/preview2-shim/lib/browser/io.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,4 +190,5 @@ export const poll = {
190190
Pollable,
191191
pollList,
192192
pollOne,
193+
poll: pollOne,
193194
};

0 commit comments

Comments
 (0)