2017-10-22 09:10:33 +08:00
|
|
|
"use strict";
|
|
|
|
|
2017-10-29 07:55:55 +08:00
|
|
|
const crypto = require('crypto');
|
2017-12-10 09:44:06 +08:00
|
|
|
const randtoken = require('rand-token').generator({source: 'crypto'});
|
2017-10-29 07:55:55 +08:00
|
|
|
|
2017-10-15 11:31:44 +08:00
|
|
|
function newNoteId() {
|
2017-12-08 11:57:39 +08:00
|
|
|
return randomString(12);
|
2017-10-15 11:31:44 +08:00
|
|
|
}
|
|
|
|
|
2017-11-19 21:47:22 +08:00
|
|
|
function newNoteTreeId() {
|
2017-11-20 01:06:48 +08:00
|
|
|
return randomString(12);
|
2017-11-19 21:47:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function newNoteHistoryId() {
|
|
|
|
return randomString(12);
|
|
|
|
}
|
|
|
|
|
2018-01-07 10:49:02 +08:00
|
|
|
function newImageId() {
|
|
|
|
return randomString(12);
|
|
|
|
}
|
|
|
|
|
|
|
|
function newNoteImageId() {
|
|
|
|
return randomString(12);
|
|
|
|
}
|
|
|
|
|
2018-01-11 13:01:16 +08:00
|
|
|
function newAttributeId() {
|
|
|
|
return randomString(12);
|
|
|
|
}
|
|
|
|
|
2017-10-29 01:19:12 +08:00
|
|
|
function randomString(length) {
|
2017-12-10 09:44:06 +08:00
|
|
|
return randtoken.generate(length);
|
2017-10-15 11:31:44 +08:00
|
|
|
}
|
|
|
|
|
2017-10-29 07:55:55 +08:00
|
|
|
function randomSecureToken(bytes = 32) {
|
2017-10-29 23:22:41 +08:00
|
|
|
return crypto.randomBytes(bytes).toString('base64');
|
2017-10-29 07:55:55 +08:00
|
|
|
}
|
|
|
|
|
2017-12-11 01:56:59 +08:00
|
|
|
function nowDate() {
|
|
|
|
return dateStr(new Date());
|
|
|
|
}
|
|
|
|
|
|
|
|
function dateStr(date) {
|
2017-12-11 04:31:43 +08:00
|
|
|
return date.toISOString();
|
2017-12-11 01:56:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-12-11 04:31:43 +08:00
|
|
|
* @param str - needs to be in the ISO 8601 format "YYYY-MM-DDTHH:MM:SS.sssZ" format as outputted by dateStr().
|
|
|
|
* also is assumed to be GMT time (as indicated by the "Z" at the end), *not* local time
|
2017-12-11 01:56:59 +08:00
|
|
|
*/
|
|
|
|
function parseDate(str) {
|
|
|
|
try {
|
2017-12-11 04:31:43 +08:00
|
|
|
return new Date(Date.parse(str));
|
2017-12-11 01:56:59 +08:00
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
throw new Error("Can't parse date from " + str + ": " + e.stack);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-15 11:31:44 +08:00
|
|
|
function toBase64(plainText) {
|
|
|
|
return Buffer.from(plainText).toString('base64');
|
|
|
|
}
|
|
|
|
|
|
|
|
function fromBase64(encodedText) {
|
|
|
|
return Buffer.from(encodedText, 'base64');
|
|
|
|
}
|
|
|
|
|
2017-10-30 02:55:48 +08:00
|
|
|
function hmac(secret, value) {
|
|
|
|
const hmac = crypto.createHmac('sha256', Buffer.from(secret.toString(), 'ASCII'));
|
|
|
|
hmac.update(value.toString());
|
|
|
|
return hmac.digest('base64');
|
|
|
|
}
|
|
|
|
|
2017-11-06 06:58:55 +08:00
|
|
|
function isElectron() {
|
|
|
|
return !!process.versions['electron'];
|
|
|
|
}
|
|
|
|
|
2017-11-22 11:11:27 +08:00
|
|
|
function hash(text) {
|
|
|
|
return crypto.createHash('sha1').update(text).digest('base64');
|
|
|
|
}
|
|
|
|
|
2017-11-27 12:10:23 +08:00
|
|
|
function isEmptyOrWhitespace(str) {
|
|
|
|
return str === null || str.match(/^ *$/) !== null;
|
|
|
|
}
|
|
|
|
|
2017-12-16 13:05:37 +08:00
|
|
|
function getDateTimeForFile() {
|
|
|
|
return new Date().toISOString().substr(0, 19).replace(/:/g, '');
|
|
|
|
}
|
2017-11-22 11:11:27 +08:00
|
|
|
|
2017-12-23 22:35:00 +08:00
|
|
|
function sanitizeSql(str) {
|
|
|
|
// should be improved or usage eliminated
|
|
|
|
return str.replace(/'/g, "\\'");
|
|
|
|
}
|
|
|
|
|
2018-01-07 04:56:00 +08:00
|
|
|
function assertArguments() {
|
|
|
|
for (const i in arguments) {
|
|
|
|
if (!arguments[i]) {
|
|
|
|
throw new Error(`Argument idx#${i} should not be falsy: ${arguments[i]}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-14 04:25:09 +08:00
|
|
|
async function stopWatch(what, func) {
|
|
|
|
const start = new Date();
|
|
|
|
|
2018-01-23 12:18:08 +08:00
|
|
|
const ret = await func();
|
2018-01-14 04:25:09 +08:00
|
|
|
|
|
|
|
const tookMs = new Date().getTime() - start.getTime();
|
|
|
|
|
|
|
|
console.log(`${what} took ${tookMs}ms`);
|
2018-01-23 12:18:08 +08:00
|
|
|
|
|
|
|
return ret;
|
2018-01-14 04:25:09 +08:00
|
|
|
}
|
|
|
|
|
2017-10-15 11:31:44 +08:00
|
|
|
module.exports = {
|
2017-10-29 07:55:55 +08:00
|
|
|
randomSecureToken,
|
2017-10-29 00:12:20 +08:00
|
|
|
randomString,
|
2017-12-11 01:56:59 +08:00
|
|
|
nowDate,
|
|
|
|
dateStr,
|
|
|
|
parseDate,
|
2017-10-15 11:31:44 +08:00
|
|
|
newNoteId,
|
2017-11-19 21:47:22 +08:00
|
|
|
newNoteTreeId,
|
|
|
|
newNoteHistoryId,
|
2018-01-07 10:49:02 +08:00
|
|
|
newImageId,
|
|
|
|
newNoteImageId,
|
2018-01-11 13:01:16 +08:00
|
|
|
newAttributeId,
|
2017-10-15 11:31:44 +08:00
|
|
|
toBase64,
|
2017-10-30 02:55:48 +08:00
|
|
|
fromBase64,
|
2017-11-05 23:41:54 +08:00
|
|
|
hmac,
|
2017-11-06 10:56:42 +08:00
|
|
|
isElectron,
|
2017-11-27 12:10:23 +08:00
|
|
|
hash,
|
2017-12-16 13:05:37 +08:00
|
|
|
isEmptyOrWhitespace,
|
2017-12-23 22:35:00 +08:00
|
|
|
getDateTimeForFile,
|
2018-01-07 04:56:00 +08:00
|
|
|
sanitizeSql,
|
2018-01-14 04:25:09 +08:00
|
|
|
assertArguments,
|
|
|
|
stopWatch
|
2017-10-15 11:31:44 +08:00
|
|
|
};
|