forked from AssemblyScript/assemblyscript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweb.js
33 lines (30 loc) · 796 Bytes
/
web.js
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
/**
* @fileoverview Web polyfills.
* @license Apache-2.0
*/
import { fs } from "./node.js";
var _fetch = typeof fetch === "function" ? fetch :
url => new Promise((resolve, reject) => { // eslint-disable-line no-global-assign
fs.readFile(url, (err, data) => {
if (err) reject(err);
resolve({
arrayBuffer() {
let offset = data.byteOffset;
return Promise.resolve(data.buffer.slice(offset, offset + data.byteLength));
},
text() {
return Promise.resolve(data.toString());
},
json() {
try {
return Promise.resolve(JSON.parse(data.toString()));
} catch (err) {
return Promise.reject(err);
}
}
});
});
});
export {
_fetch as fetch
};