UNPKG

react-native-ota-hot-update

Version:
178 lines (169 loc) 5 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.writeFile = exports.unlink = exports.symlink = exports.stat = exports.rmdir = exports.readlink = exports.readdir = exports.readFile = exports.mkdir = exports.lstat = exports.chmod = void 0; var _buffer = require("buffer"); let RNFS = { unlink: console.log, readdir: console.log, mkdir: console.log, readFile: console.log, writeFile: console.log, stat: console.log }; try { RNFS = require('react-native-fs'); } catch {} // Try to load native OtaHotUpdate module for better performance let OtaHotUpdateNative = null; try { const { NativeModules } = require('react-native'); OtaHotUpdateNative = NativeModules.OtaHotUpdate; } catch (e) { // Native module not available, will fallback to RNFS } function Err(name) { return class extends Error { code = name; constructor(...args) { super(...args); if (this.message) { this.message = name + ': ' + this.message; } else { this.message = name; } } }; } // const EEXIST = Err('EEXIST'); // <-- Unused because RNFS's mkdir never throws const ENOENT = Err('ENOENT'); const ENOTDIR = Err('ENOTDIR'); // const ENOTEMPTY = Err('ENOTEMPTY'); // <-- Unused because RNFS's unlink is recursive by default const readdir = async path => { try { return await RNFS.readdir(path); } catch (err) { switch (err.message) { case 'Attempt to get length of null array': { throw new ENOTDIR(path); } case 'Folder does not exist': { throw new ENOENT(path); } default: throw err; } } }; exports.readdir = readdir; const mkdir = async path => { return RNFS.mkdir(path); }; exports.mkdir = mkdir; const readFile = async (path, opts) => { let encoding; if (typeof opts === 'string') { encoding = opts; } else if (typeof opts === 'object') { encoding = opts.encoding; } // @ts-ignore let result = await RNFS.readFile(path, encoding || 'base64'); if (!encoding) { // @ts-ignore result = _buffer.Buffer.from(result, 'base64'); } return result; }; exports.readFile = readFile; const writeFile = async (path, content, opts) => { let encoding; if (typeof opts === 'string') { encoding = opts; } else if (typeof opts === 'object') { encoding = opts.encoding; } if (typeof content === 'string') { // Text file - use RNFS (fast enough for text) encoding = encoding || 'utf8'; await RNFS.writeFile(path, content, encoding); } else { // Binary file - try native module first for better performance encoding = 'base64'; const base64Content = _buffer.Buffer.from(content).toString('base64'); if (OtaHotUpdateNative && OtaHotUpdateNative.writeFile) { try { // Use native write (runs on native thread, doesn't block JS thread) await OtaHotUpdateNative.writeFile(path, base64Content, encoding); } catch (e) { // Fallback to RNFS if native fails console.warn('OtaHotUpdate.writeFile failed, falling back to RNFS:', e); await RNFS.writeFile(path, base64Content, encoding); } } else { // No native module available - use RNFS await RNFS.writeFile(path, base64Content, encoding); } } }; exports.writeFile = writeFile; const stat = async path => { try { const r = await RNFS.stat(path); // we monkeypatch the result with a `isSymbolicLink` method because isomorphic-git needs it. // Since RNFS doesn't appear to support symlinks at all, we'll just always return false. // @ts-ignore r.isSymbolicLink = () => false; return r; } catch (err) { switch (err.message) { case 'File does not exist': { throw new ENOENT(path); } default: throw err; } } }; // Since there are no symbolic links, lstat and stat are equivalent exports.stat = stat; const lstat = exports.lstat = stat; const unlink = async path => { try { await RNFS.unlink(path); } catch (err) { switch (err.message) { case 'File does not exist': { throw new ENOENT(path); } default: throw err; } } }; // RNFS doesn't have a separate rmdir method, so we can use unlink for deleting directories too exports.unlink = unlink; const rmdir = exports.rmdir = unlink; // These are optional, which is good because there is no equivalent in RNFS const readlink = async () => { throw new Error('not implemented'); }; exports.readlink = readlink; const symlink = async () => { throw new Error('not implemented'); }; // Technically we could pull this off by using `readFile` + `writeFile` with the `mode` option // However, it's optional, because isomorphic-git will do exactly that (a readFile and a writeFile with the new mode) exports.symlink = symlink; const chmod = async () => { throw new Error('not implemented'); }; exports.chmod = chmod; //# sourceMappingURL=fs.js.map