(function () {
'use strict';
var local;
(function () {
local = {};
local.modeJs = (function () {
try {
return typeof navigator.userAgent === 'string' &&
typeof document.querySelector('body') === 'object' &&
typeof XMLHttpRequest.prototype.open === 'function' &&
'browser';
} catch (errorCaughtBrowser) {
return module.exports &&
typeof process.versions.node === 'string' &&
typeof require('http').createServer === 'function' &&
'node';
}
}());
local = local.modeJs === 'browser'
? window.Nedb.local
: module.isRollup
? module
: require('nedb-lite').local;
local.global = local.modeJs === 'browser'
? window
: global;
local.global.local = local;
}());
switch (local.modeJs) {
case 'browser':
['error', 'log'].forEach(function (key) {
console['_' + key] = console[key];
console[key] = function () {
console['_' + key].apply(console, arguments);
document.querySelector('#outputTextarea1').value +=
Array.prototype.slice.call(arguments).map(function (arg) {
return typeof arg === 'string'
? arg
: local.Nedb.jsonStringifyOrdered(arg, null, 4);
}).join(' ') + '\n';
};
});
local.testRun = function (event) {
var reader, tmp;
switch (event && event.currentTarget.id) {
case 'nedbExportButton1':
tmp = window.URL.createObjectURL(new window.Blob([local.Nedb.dbExport()]));
document.querySelector('#nedbExportA1').href = tmp;
document.querySelector('#nedbExportA1').click();
setTimeout(function () {
window.URL.revokeObjectURL(tmp);
}, 30000);
break;
case 'nedbImportButton1':
document.querySelector('#nedbImportInput1').click();
break;
case 'nedbImportInput1':
document.querySelector('#outputTextarea1').value = '';
console.log('importing nedb-database ...');
reader = new window.FileReader();
tmp = document.querySelector('#nedbImportInput1').files[0];
if (!tmp) {
return;
}
reader.addEventListener('load', function () {
local.Nedb.dbImport(reader.result, function () {
console.log('... imported nedb-database');
});
});
reader.readAsText(tmp);
break;
case 'nedbResetButton1':
document.querySelector('#outputTextarea1').value = '';
console.log('resetting nedb-database ...');
local.Nedb.dbReset(function () {
console.log('... resetted nedb-database');
});
break;
case 'testRunButton1':
local.modeTest = true;
local.utility2.testRun(local);
break;
default:
document.querySelector('#outputTextarea1').value = '';
try {
eval(document.querySelector('#inputTextarea1').value);
} catch (errorCaught) {
document.querySelector('#outputTextarea1').value = errorCaught.stack;
}
}
};
['change', 'click', 'keyup'].forEach(function (event) {
Array.prototype.slice.call(
document.querySelectorAll('.on' + event)
).forEach(function (element) {
element.addEventListener(event, local.testRun);
});
});
local.testRun();
break;
case 'node':
module.exports = local;
local.fs = require('fs');
local.http = require('http');
local.path = require('path');
local.url = require('url');
local.templateIndexHtml = '\
<!doctype html>\n\
<html lang="en">\n\
<head>\n\
<meta charset="UTF-8">\n\
<meta name="viewport" content="width=device-width, initial-scale=1">\n\
<title>\n\
{{envDict.npm_package_name}} v{{envDict.npm_package_version}}\n\
</title>\n\
<style>\n\
/*csslint\n\
box-sizing: false,\n\
ids: false,\n\
universal-selector: false\n\
*/\n\
* {\n\
box-sizing: border-box;\n\
}\n\
body {\n\
background-color: #fff;\n\
font-family: Arial, Helvetica, sans-serif;\n\
}\n\
body > * {\n\
margin-bottom: 1rem;\n\
}\n\
body > button {\n\
width: 15rem;\n\
}\n\
textarea {\n\
font-family: monospace;\n\
height: 16rem;\n\
width: 100%;\n\
}\n\
textarea[readonly] {\n\
background-color: #ddd;\n\
}\n\
.zeroPixel {\n\
border: 0;\n\
height: 0;\n\
margin: 0;\n\
padding: 0;\n\
width: 0;\n\
}\n\
</style>\n\
</head>\n\
<body>\n\
<h1>\n\
<!-- utility2-comment\n\
<a\n\
{{#if envDict.npm_package_homepage}}\n\
href="{{envDict.npm_package_homepage}}"\n\
{{/if envDict.npm_package_homepage}}\n\
target="_blank"\n\
>\n\
utility2-comment -->\n\
{{envDict.npm_package_name}} v{{envDict.npm_package_version}}\n\
<!-- utility2-comment\n\
</a>\n\
utility2-comment -->\n\
<!-- utility2-comment\n\
{{#if envDict.NODE_ENV}}\n\
(NODE_ENV={{envDict.NODE_ENV}})\n\
{{/if envDict.NODE_ENV}}\n\
utility2-comment -->\n\
</h1>\n\
<h3>{{envDict.npm_package_description}}</h3>\n\
<!-- utility2-comment\n\
<h4><a download href="assets.app.js">download standalone app</a></h4>\n\
<button class="onclick" id="testRunButton1">run internal test</button><br>\n\
<div class="testReportDiv" style="display: none;"></div>\n\
utility2-comment -->\n\
\n\
<button class="onclick" id="nedbResetButton1">reset nedb-database</button><br>\n\
<button class="onclick" id="nedbExportButton1">save nedb-database to file</button><br>\n\
<a download="nedb.persistence.json" href="" id="nedbExportA1"></a>\n\
<button class="onclick" id="nedbImportButton1">load nedb-database from file</button><br>\n\
<input class="onchange zeroPixel" type="file" id="nedbImportInput1">\n\
<label>edit or paste script below to\n\
<a\n\
href="https://kaizhu256.github.io/node-nedb-lite/build/doc.api.html"\n\
target="_blank"\n\
>eval</a>\n\
</label>\n\
<textarea class="onkeyup" id="inputTextarea1">\n\
window.table1 = window.Nedb.dbTableCreate({ name: "table1" });\n\
table1.insert({ field1: "hello", field2: "world"}, function () {\n\
console.log();\n\
console.log(table1.export());\n\
});\n\
\n\
window.table2 = window.Nedb.dbTableCreate({ name: "table2" });\n\
table2.insert({ field1: "hello", field2: "world"}, function () {\n\
console.log();\n\
console.log(table2.export());\n\
});\n\
</textarea>\n\
<label>stderr and stdout</label>\n\
<textarea id="outputTextarea1" readonly></textarea>\n\
<!-- utility2-comment\n\
{{#if isRollup}}\n\
<script src="assets.app.min.js"></script>\n\
{{#unless isRollup}}\n\
utility2-comment -->\n\
<script src="assets.utility2.rollup.js"></script>\n\
<script src="jsonp.utility2.stateInit?callback=window.utility2.stateInit"></script>\n\
<script src="assets.nedb-lite.js"></script>\n\
<script src="assets.example.js"></script>\n\
<script src="assets.test.js"></script>\n\
<!-- utility2-comment\n\
{{/if isRollup}}\n\
utility2-comment -->\n\
</body>\n\
</html>\n\
';
local['/'] = local.templateIndexHtml
.replace((/\{\{envDict\.(\w+?)\}\}/g), function (match0, match1) {
String(match0);
switch (match1) {
case 'npm_package_description':
return 'example module';
case 'npm_package_name':
return 'example';
case 'npm_package_version':
return '0.0.1';
}
});
if (module.isRollup) {
break;
}
try {
local['/assets.example.js'] = local.fs.readFileSync(__filename, 'utf8');
} catch (ignore) {
}
local['/assets.nedb-lite.js'] = local.fs.readFileSync(
local.Nedb.__dirname + '/index.js',
'utf8'
);
if (module !== require.main) {
break;
}
console.log('server starting on port ' + process.env.PORT);
local.http.createServer(function (request, response) {
switch (local.url.parse(request.url).pathname) {
case '/':
case '/assets.example.js':
case '/assets.nedb-lite.js':
case '/assets.test.js':
response.end(local[local.url.parse(request.url).pathname]);
break;
default:
response.end();
}
}).listen(process.env.PORT);
if (Number(process.env.npm_config_timeout_exit)) {
setTimeout(process.exit, Number(process.env.npm_config_timeout_exit));
}
break;
}
}());