blob: 2a7fce592b9aa3a04023964f8bc761b3f0c033ca [file] [log] [blame]
/**
* @license
* Copyright 2019 The Emscripten Authors
* SPDX-License-Identifier: MIT
*/
// These modules will usually be used on Node.js. Load them eagerly to avoid
// the complexity of lazy-loading. However, for now we must guard on require()
// actually existing: if the JS is put in a .mjs file (ES6 module) and run on
// node, then we'll detect node as the environment and get here, but require()
// does not exist (since ES6 modules should use |import|). If the code actually
// uses the node filesystem then it will crash, of course, but in the case of
// code that never uses it we don't want to crash here, so the guarding if lets
// such code work properly. See discussion in
// https://github.com/emscripten-core/emscripten/pull/17851
var fs, nodePath;
if (typeof require === 'function') {
fs = require('fs');
nodePath = require('path');
}
read_ = (filename, binary) => {
#if SUPPORT_BASE64_EMBEDDING
var ret = tryParseAsDataURI(filename);
if (ret) {
return binary ? ret : ret.toString();
}
#endif
filename = nodePath['normalize'](filename);
return fs.readFileSync(filename, binary ? undefined : 'utf8');
};
readBinary = (filename) => {
var ret = read_(filename, true);
if (!ret.buffer) {
ret = new Uint8Array(ret);
}
#if ASSERTIONS
assert(ret.buffer);
#endif
return ret;
};
readAsync = (filename, onload, onerror) => {
#if SUPPORT_BASE64_EMBEDDING
var ret = tryParseAsDataURI(filename);
if (ret) {
onload(ret);
}
#endif
filename = nodePath['normalize'](filename);
fs.readFile(filename, function(err, data) {
if (err) onerror(err);
else onload(data.buffer);
});
};