|
// A tiny fetch polyfill for node that allows loading external resources in isomorphic libraries. |
|
// Run node code with `-r ./node-fetch` to enable polyfill without needing to change source code. |
|
|
|
let { readFile } = require('fs') |
|
|
|
global.fetch = fetch |
|
|
|
let P = (fn, ...args) => new Promise((resolve, reject) => |
|
fn(...args, (err, val) => err ? reject(err) : resolve(val))) |
|
|
|
async function fetch (path) { |
|
let data = await P(readFile, path) |
|
|
|
return { |
|
code: 200, |
|
arrayBuffer |
|
} |
|
|
|
async function arrayBuffer () { |
|
return new Uint8Array(data) |
|
} |
|
} |