stuff
This commit is contained in:
78
buildfiles/app/node_modules/nedb/browser-version/browser-specific/lib/customUtils.js
generated
vendored
Executable file
78
buildfiles/app/node_modules/nedb/browser-version/browser-specific/lib/customUtils.js
generated
vendored
Executable file
@ -0,0 +1,78 @@
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
function randomBytes (size) {
|
||||
var bytes = new Array(size);
|
||||
var r;
|
||||
|
||||
for (var 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/
|
||||
*/
|
||||
function byteArrayToBase64 (uint8) {
|
||||
var lookup = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
|
||||
, extraBytes = uint8.length % 3 // if we have 1 byte left, pad 2 bytes
|
||||
, output = ""
|
||||
, temp, length, i;
|
||||
|
||||
function tripletToBase64 (num) {
|
||||
return 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 (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
|
||||
switch (extraBytes) {
|
||||
case 1:
|
||||
temp = uint8[uint8.length - 1];
|
||||
output += lookup[temp >> 2];
|
||||
output += lookup[(temp << 4) & 0x3F];
|
||||
output += '==';
|
||||
break;
|
||||
case 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 += '=';
|
||||
break;
|
||||
}
|
||||
|
||||
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
|
||||
*/
|
||||
function uid (len) {
|
||||
return byteArrayToBase64(randomBytes(Math.ceil(Math.max(8, len * 2)))).replace(/[+\/]/g, '').slice(0, len);
|
||||
}
|
||||
|
||||
|
||||
|
||||
module.exports.uid = uid;
|
95
buildfiles/app/node_modules/nedb/browser-version/browser-specific/lib/storage.js
generated
vendored
Executable file
95
buildfiles/app/node_modules/nedb/browser-version/browser-specific/lib/storage.js
generated
vendored
Executable file
@ -0,0 +1,95 @@
|
||||
/**
|
||||
* 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)
|
||||
*
|
||||
* This version is the browser version
|
||||
*/
|
||||
|
||||
var localforage = require('localforage')
|
||||
|
||||
// Configure localforage to display NeDB name for now. Would be a good idea to let user use his own app name
|
||||
localforage.config({
|
||||
name: 'NeDB'
|
||||
, storeName: 'nedbdata'
|
||||
});
|
||||
|
||||
|
||||
function exists (filename, callback) {
|
||||
localforage.getItem(filename, function (err, value) {
|
||||
if (value !== null) { // Even if value is undefined, localforage returns null
|
||||
return callback(true);
|
||||
} else {
|
||||
return callback(false);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
function rename (filename, newFilename, callback) {
|
||||
localforage.getItem(filename, function (err, value) {
|
||||
if (value === null) {
|
||||
localforage.removeItem(newFilename, function () { return callback(); });
|
||||
} else {
|
||||
localforage.setItem(newFilename, value, function () {
|
||||
localforage.removeItem(filename, function () { return callback(); });
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
function writeFile (filename, contents, options, callback) {
|
||||
// Options do not matter in browser setup
|
||||
if (typeof options === 'function') { callback = options; }
|
||||
localforage.setItem(filename, contents, function () { return callback(); });
|
||||
}
|
||||
|
||||
|
||||
function appendFile (filename, toAppend, options, callback) {
|
||||
// Options do not matter in browser setup
|
||||
if (typeof options === 'function') { callback = options; }
|
||||
|
||||
localforage.getItem(filename, function (err, contents) {
|
||||
contents = contents || '';
|
||||
contents += toAppend;
|
||||
localforage.setItem(filename, contents, function () { return callback(); });
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
function readFile (filename, options, callback) {
|
||||
// Options do not matter in browser setup
|
||||
if (typeof options === 'function') { callback = options; }
|
||||
localforage.getItem(filename, function (err, contents) { return callback(null, contents || ''); });
|
||||
}
|
||||
|
||||
|
||||
function unlink (filename, callback) {
|
||||
localforage.removeItem(filename, function () { return callback(); });
|
||||
}
|
||||
|
||||
|
||||
// Nothing to do, no directories will be used on the browser
|
||||
function mkdirp (dir, callback) {
|
||||
return callback();
|
||||
}
|
||||
|
||||
|
||||
// Nothing to do, no data corruption possible in the brower
|
||||
function ensureDatafileIntegrity (filename, callback) {
|
||||
return callback(null);
|
||||
}
|
||||
|
||||
|
||||
// Interface
|
||||
module.exports.exists = exists;
|
||||
module.exports.rename = rename;
|
||||
module.exports.writeFile = writeFile;
|
||||
module.exports.crashSafeWriteFile = writeFile; // No need for a crash safe function in the browser
|
||||
module.exports.appendFile = appendFile;
|
||||
module.exports.readFile = readFile;
|
||||
module.exports.unlink = unlink;
|
||||
module.exports.mkdirp = mkdirp;
|
||||
module.exports.ensureDatafileIntegrity = ensureDatafileIntegrity;
|
||||
|
Reference in New Issue
Block a user