From f000cc748d7dda5b57b6744f41be1dd128dbeb22 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Rebours?= Date: Thu, 28 Oct 2021 15:35:35 +0200 Subject: [PATCH] unify browser & rn versions for most files --- .../lib/{storage.js => storage.browser.js} | 1 + .../lib/storage.react-native.js | 11 ++-- package.json | 10 +-- react-native-version/lib/byline.js | 1 - react-native-version/lib/customUtils.js | 66 ------------------- webpack.config.js | 4 +- 6 files changed, 14 insertions(+), 79 deletions(-) rename browser-version/lib/{storage.js => storage.browser.js} (97%) rename react-native-version/lib/storage.js => browser-version/lib/storage.react-native.js (90%) delete mode 100644 react-native-version/lib/byline.js delete mode 100755 react-native-version/lib/customUtils.js diff --git a/browser-version/lib/storage.js b/browser-version/lib/storage.browser.js similarity index 97% rename from browser-version/lib/storage.js rename to browser-version/lib/storage.browser.js index 7a5fdfc..885557d 100755 --- a/browser-version/lib/storage.js +++ b/browser-version/lib/storage.browser.js @@ -2,6 +2,7 @@ * Way data is stored for this database * For a Node.js/Node Webkit database it's the file system * For a browser-side database it's localforage, which uses the best backend available (IndexedDB then WebSQL then localStorage) + * For a react-native database, we use @react-native-async-storage/async-storage * * This version is the browser version */ diff --git a/react-native-version/lib/storage.js b/browser-version/lib/storage.react-native.js similarity index 90% rename from react-native-version/lib/storage.js rename to browser-version/lib/storage.react-native.js index 81040b3..977d232 100755 --- a/react-native-version/lib/storage.js +++ b/browser-version/lib/storage.react-native.js @@ -3,6 +3,7 @@ * For a Node.js/Node Webkit database it's the file system * For a browser-side database it's localforage, which uses the best backend available (IndexedDB then WebSQL then localStorage) * For a react-native database, we use @react-native-async-storage/async-storage + * * This version is the react-native version */ const AsyncStorage = require('@react-native-async-storage/async-storage') @@ -31,13 +32,13 @@ const rename = (filename, newFilename, callback) => { } const writeFile = (filename, contents, options, callback) => { - // Options do not matter in browser setup + // Options do not matter in a react-native setup if (typeof options === 'function') { callback = options } AsyncStorage.setItem(filename, contents, callback) } const appendFile = (filename, toAppend, options, callback) => { - // Options do not matter in browser setup + // Options do not matter in a react-native setup if (typeof options === 'function') { callback = options } // eslint-disable-next-line node/handle-callback-err @@ -49,7 +50,7 @@ const appendFile = (filename, toAppend, options, callback) => { } const readFile = (filename, options, callback) => { - // Options do not matter in browser setup + // Options do not matter in a react-native setup if (typeof options === 'function') { callback = options } // eslint-disable-next-line node/handle-callback-err AsyncStorage.getItem(filename, (err, contents) => { @@ -61,10 +62,10 @@ const unlink = (filename, callback) => { AsyncStorage.removeItem(filename, callback) } -// Nothing to do, no directories will be used on the browser +// Nothing to do, no directories will be used on react-native const mkdir = (dir, options, callback) => callback() -// Nothing to do, no data corruption possible in the browser +// Nothing to do, no data corruption possible on react-native const ensureDatafileIntegrity = (filename, callback) => callback(null) const crashSafeWriteFileLines = (filename, lines, callback) => { diff --git a/package.json b/package.json index bd1af69..796f0c9 100755 --- a/package.json +++ b/package.json @@ -3,7 +3,7 @@ "version": "2.1.0", "files": [ "lib/**/*.js", - "browser-version/**/*.js", + "platform-specific/**/*.js", "index.js", "index.d.ts" ], @@ -89,13 +89,13 @@ "main": "index.js", "browser": { "./lib/customUtils.js": "./browser-version/lib/customUtils.js", - "./lib/storage.js": "./browser-version/lib/storage.js", + "./lib/storage.js": "./browser-version/lib/storage.browser.js", "./lib/byline.js": "./browser-version/lib/byline.js" }, "react-native": { - "./lib/customUtils.js": "./react-native-version/lib/customUtils.js", - "./lib/storage.js": "./react-native-version/lib/storage.js", - "./lib/byline.js": "./react-native-version/lib/byline.js" + "./lib/customUtils.js": "./browser-version/lib/customUtils.js", + "./lib/storage.js": "./browser-version/lib/storage.react-native.js", + "./lib/byline.js": "./browser-version/lib/byline.js" }, "license": "MIT", "publishConfig": { diff --git a/react-native-version/lib/byline.js b/react-native-version/lib/byline.js deleted file mode 100644 index 4ba52ba..0000000 --- a/react-native-version/lib/byline.js +++ /dev/null @@ -1 +0,0 @@ -module.exports = {} diff --git a/react-native-version/lib/customUtils.js b/react-native-version/lib/customUtils.js deleted file mode 100755 index 13021df..0000000 --- a/react-native-version/lib/customUtils.js +++ /dev/null @@ -1,66 +0,0 @@ -/** - * Specific customUtils for the browser, where we don't have access to the Crypto and Buffer modules - */ - -/** - * Taken from the crypto-browserify module - * https://github.com/dominictarr/crypto-browserify - * NOTE: Math.random() does not guarantee "cryptographic quality" but we actually don't need it - */ -const randomBytes = size => { - const bytes = new Array(size) - - for (let i = 0, r; i < size; i++) { - if ((i & 0x03) === 0) r = Math.random() * 0x100000000 - bytes[i] = r >>> ((i & 0x03) << 3) & 0xff - } - - return bytes -} - -/** - * Taken from the base64-js module - * https://github.com/beatgammit/base64-js/ - */ -const byteArrayToBase64 = uint8 => { - const lookup = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/' - const extraBytes = uint8.length % 3 // if we have 1 byte left, pad 2 bytes - let output = '' - let temp - - const tripletToBase64 = num => lookup[num >> 18 & 0x3F] + lookup[num >> 12 & 0x3F] + lookup[num >> 6 & 0x3F] + lookup[num & 0x3F] - - // go through the array every three bytes, we'll deal with trailing stuff later - for (let i = 0, length = uint8.length - extraBytes; i < length; i += 3) { - temp = (uint8[i] << 16) + (uint8[i + 1] << 8) + (uint8[i + 2]) - output += tripletToBase64(temp) - } - - // pad the end with zeros, but make sure to not forget the extra bytes - if (extraBytes === 1) { - temp = uint8[uint8.length - 1] - output += lookup[temp >> 2] - output += lookup[(temp << 4) & 0x3F] - output += '==' - } else if (extraBytes === 2) { - temp = (uint8[uint8.length - 2] << 8) + (uint8[uint8.length - 1]) - output += lookup[temp >> 10] - output += lookup[(temp >> 4) & 0x3F] - output += lookup[(temp << 2) & 0x3F] - output += '=' - } - - return output -} - -/** - * Return a random alphanumerical string of length len - * There is a very small probability (less than 1/1,000,000) for the length to be less than len - * (il the base64 conversion yields too many pluses and slashes) but - * that's not an issue here - * The probability of a collision is extremely small (need 3*10^12 documents to have one chance in a million of a collision) - * See http://en.wikipedia.org/wiki/Birthday_problem - */ -const uid = len => byteArrayToBase64(randomBytes(Math.ceil(Math.max(8, len * 2)))).replace(/[+/]/g, '').slice(0, len) - -module.exports.uid = uid diff --git a/webpack.config.js b/webpack.config.js index 94e507f..890af13 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -25,7 +25,7 @@ module.exports = (env, argv) => { } }, plugins: [ - new webpack.NormalModuleReplacementPlugin(new RegExp(path.resolve(__dirname, 'lib/storage.js')), path.resolve(__dirname, 'browser-version/lib/storage.js')), + new webpack.NormalModuleReplacementPlugin(new RegExp(path.resolve(__dirname, 'lib/storage.js')), path.resolve(__dirname, 'browser-version/lib/storage.browser.js')), new webpack.NormalModuleReplacementPlugin(new RegExp(path.resolve(__dirname, 'lib/customUtils.js')), path.resolve(__dirname, 'browser-version/lib/customUtils.js')), new webpack.NormalModuleReplacementPlugin(/byline/, path.resolve(__dirname, 'browser-version/lib/byline.js')), new webpack.ProvidePlugin({ @@ -39,7 +39,7 @@ module.exports = (env, argv) => { Nedb: path.join(__dirname, 'lib', 'datastore.js') }, output: { - path: path.join(__dirname, 'browser-version/out'), + path: path.join(__dirname, 'platform-specific/out'), filename: minimize ? 'nedb.min.js' : 'nedb.js', libraryTarget: 'window', library: '[name]'