mirror of https://github.com/seald/nedb
commit
c10487fb8a
@ -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; |
@ -0,0 +1,40 @@ |
|||||||
|
/** |
||||||
|
* Handle every persistence-related task |
||||||
|
* The interface Datastore expects to be implemented is |
||||||
|
* * Persistence.loadDatabase(callback) and callback has signature err |
||||||
|
* * Persistence.persistNewState(newDocs, callback) where newDocs is an array of documents and callback has signature err |
||||||
|
* |
||||||
|
* Shim for the browser |
||||||
|
*/ |
||||||
|
|
||||||
|
/** |
||||||
|
* Create a new Persistence object for database options.db |
||||||
|
* For now, no browser persistence supported, in-memory only mode forced |
||||||
|
* @param {Datastore} options.db |
||||||
|
*/ |
||||||
|
function Persistence (options) { |
||||||
|
this.db = options.db; |
||||||
|
this.db.inMemoryOnly = true; |
||||||
|
this.db.filename = null; |
||||||
|
this.inMemoryOnly = true; |
||||||
|
}; |
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* No persistence in the browser (for now) |
||||||
|
*/ |
||||||
|
Persistence.prototype.persistNewState = function (newDocs, cb) { |
||||||
|
if (cb) { return cb(); } |
||||||
|
}; |
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* No persistence in the browser (for now) |
||||||
|
*/ |
||||||
|
Persistence.prototype.loadDatabase = function (cb) { |
||||||
|
if (cb) { return cb(); } |
||||||
|
}; |
||||||
|
|
||||||
|
|
||||||
|
// Interface
|
||||||
|
module.exports = Persistence; |
@ -0,0 +1,95 @@ |
|||||||
|
/** |
||||||
|
* Build the browser version of nedb |
||||||
|
*/ |
||||||
|
|
||||||
|
var fs = require('fs') |
||||||
|
, path = require('path') |
||||||
|
, child_process = require('child_process') |
||||||
|
, toCopy = ['lib', 'node_modules'] |
||||||
|
, async, browserify, uglify |
||||||
|
; |
||||||
|
|
||||||
|
// Ensuring the node_modules, src and out directories exist
|
||||||
|
function ensureDirExists (name) { |
||||||
|
try { |
||||||
|
fs.mkdirSync(path.join(__dirname, name)); |
||||||
|
} catch (e) { |
||||||
|
if (e.code !== 'EEXIST') { |
||||||
|
console.log("Error ensuring that node_modules exists"); |
||||||
|
process.exit(1); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
ensureDirExists('node_modules'); |
||||||
|
ensureDirExists('out'); |
||||||
|
ensureDirExists('src'); |
||||||
|
|
||||||
|
|
||||||
|
// Installing build dependencies and require them
|
||||||
|
console.log("Installing build dependencies"); |
||||||
|
child_process.exec('npm install fs-extra async uglify-js browserify', function (err, stdout, stderr) { |
||||||
|
if (err) { console.log("Error reinstalling dependencies"); process.exit(1); } |
||||||
|
|
||||||
|
fs = require('fs-extra'); |
||||||
|
async = require('async'); |
||||||
|
browserify = require('browserify'); |
||||||
|
uglify = require('uglify-js'); |
||||||
|
|
||||||
|
async.waterfall([ |
||||||
|
function (cb) { |
||||||
|
console.log("Removing contents of the src directory"); |
||||||
|
|
||||||
|
async.eachSeries(fs.readdirSync(path.join(__dirname, 'src')), function (item, _cb) { |
||||||
|
fs.remove(path.join(__dirname, 'src', item), _cb); |
||||||
|
}, cb); |
||||||
|
} |
||||||
|
, function (cb) { |
||||||
|
console.log("Copying source files"); |
||||||
|
|
||||||
|
async.eachSeries(toCopy, function (item, _cb) { |
||||||
|
fs.copy(path.join(__dirname, '..', item), path.join(__dirname, 'src', item), _cb); |
||||||
|
}, cb); |
||||||
|
} |
||||||
|
, function (cb) { |
||||||
|
console.log("Copying browser specific files to replace their server-specific counterparts"); |
||||||
|
|
||||||
|
async.eachSeries(fs.readdirSync(path.join(__dirname, 'browser-specific')), function (item, _cb) { |
||||||
|
fs.copy(path.join(__dirname, 'browser-specific', item), path.join(__dirname, 'src', item), _cb); |
||||||
|
}, cb); |
||||||
|
} |
||||||
|
, function (cb) { |
||||||
|
console.log("Browserifying the code"); |
||||||
|
|
||||||
|
var b = browserify() |
||||||
|
, srcPath = path.join(__dirname, 'src/lib/datastore.js'); |
||||||
|
|
||||||
|
b.add(srcPath); |
||||||
|
b.bundle({ standalone: 'Nedb' }, function (err, out) { |
||||||
|
if (err) { return cb(err); } |
||||||
|
fs.writeFile(path.join(__dirname, 'out/nedb.js'), out, 'utf8', function (err) { |
||||||
|
if (err) { |
||||||
|
return cb(err); |
||||||
|
} else { |
||||||
|
return cb(null, out); |
||||||
|
} |
||||||
|
}); |
||||||
|
}); |
||||||
|
} |
||||||
|
, function (out, cb) { |
||||||
|
console.log("Creating the minified version"); |
||||||
|
|
||||||
|
var compressedCode = uglify.minify(out, { fromString: true }); |
||||||
|
fs.writeFile(path.join(__dirname, 'out/nedb.min.js'), compressedCode.code, 'utf8', cb); |
||||||
|
} |
||||||
|
], function (err) { |
||||||
|
if (err) { |
||||||
|
console.log("Error during build"); |
||||||
|
console.log(err); |
||||||
|
} else { |
||||||
|
console.log("Build finished with success"); |
||||||
|
} |
||||||
|
}); |
||||||
|
}); |
||||||
|
|
||||||
|
|
||||||
|
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because one or more lines are too long
Loading…
Reference in new issue