| 
									
										
										
										
											2017-10-21 21:10:33 -04:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-28 19:55:55 -04:00
										 |  |  | const crypto = require('crypto'); | 
					
						
							| 
									
										
										
										
											2017-12-09 20:44:06 -05:00
										 |  |  | const randtoken = require('rand-token').generator({source: 'crypto'}); | 
					
						
							| 
									
										
										
										
											2018-02-26 20:47:34 -05:00
										 |  |  | const unescape = require('unescape'); | 
					
						
							| 
									
										
										
										
											2017-10-28 19:55:55 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | function newNoteId() { | 
					
						
							| 
									
										
										
										
											2017-12-07 22:57:39 -05:00
										 |  |  |     return randomString(12); | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-24 21:39:15 -04:00
										 |  |  | function newBranchId() { | 
					
						
							| 
									
										
										
										
											2017-11-19 12:06:48 -05:00
										 |  |  |     return randomString(12); | 
					
						
							| 
									
										
										
										
											2017-11-19 08:47:22 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-11 00:18:59 -05:00
										 |  |  | function newNoteRevisionId() { | 
					
						
							| 
									
										
										
										
											2017-11-19 08:47:22 -05:00
										 |  |  |     return randomString(12); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-06 21:49:02 -05:00
										 |  |  | function newImageId() { | 
					
						
							|  |  |  |     return randomString(12); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function newNoteImageId() { | 
					
						
							|  |  |  |     return randomString(12); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-11 00:01:16 -05:00
										 |  |  | function newAttributeId() { | 
					
						
							|  |  |  |     return randomString(12); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-11 00:18:59 -05:00
										 |  |  | function newApiTokenId() { | 
					
						
							|  |  |  |     return randomString(12); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-28 13:19:12 -04:00
										 |  |  | function randomString(length) { | 
					
						
							| 
									
										
										
										
											2017-12-09 20:44:06 -05:00
										 |  |  |     return randtoken.generate(length); | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-28 19:55:55 -04:00
										 |  |  | function randomSecureToken(bytes = 32) { | 
					
						
							| 
									
										
										
										
											2017-10-29 11:22:41 -04:00
										 |  |  |     return crypto.randomBytes(bytes).toString('base64'); | 
					
						
							| 
									
										
										
										
											2017-10-28 19:55:55 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-10 12:56:59 -05:00
										 |  |  | function nowDate() { | 
					
						
							|  |  |  |     return dateStr(new Date()); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-12 23:53:00 -05:00
										 |  |  | function localDate() { | 
					
						
							|  |  |  |     const date = new Date(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return date.getFullYear() + "-" | 
					
						
							|  |  |  |         + (date.getMonth() < 9 ? "0" : "") + (date.getMonth() + 1) + "-" | 
					
						
							|  |  |  |         + (date.getDate() < 10 ? "0" : "") + date.getDate(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-10 12:56:59 -05:00
										 |  |  | function dateStr(date) { | 
					
						
							| 
									
										
										
										
											2017-12-10 15:31:43 -05:00
										 |  |  |     return date.toISOString(); | 
					
						
							| 
									
										
										
										
											2017-12-10 12:56:59 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							| 
									
										
										
										
											2017-12-10 15:31:43 -05: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-10 12:56:59 -05:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2018-02-10 13:53:35 -05:00
										 |  |  | function parseDateTime(str) { | 
					
						
							| 
									
										
										
										
											2017-12-10 12:56:59 -05:00
										 |  |  |     try { | 
					
						
							| 
									
										
										
										
											2017-12-10 15:31:43 -05:00
										 |  |  |         return new Date(Date.parse(str)); | 
					
						
							| 
									
										
										
										
											2017-12-10 12:56:59 -05:00
										 |  |  |     } | 
					
						
							|  |  |  |     catch (e) { | 
					
						
							|  |  |  |         throw new Error("Can't parse date from " + str + ": " + e.stack); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-10 13:53:35 -05:00
										 |  |  | function parseDate(str) { | 
					
						
							|  |  |  |     const datePart = str.substr(0, 10); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return parseDateTime(datePart + "T12:00:00.000Z"); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | function toBase64(plainText) { | 
					
						
							|  |  |  |     return Buffer.from(plainText).toString('base64'); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function fromBase64(encodedText) { | 
					
						
							|  |  |  |     return Buffer.from(encodedText, 'base64'); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-29 14:55:48 -04: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-05 17:58:55 -05:00
										 |  |  | function isElectron() { | 
					
						
							|  |  |  |     return !!process.versions['electron']; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-21 22:11:27 -05:00
										 |  |  | function hash(text) { | 
					
						
							|  |  |  |     return crypto.createHash('sha1').update(text).digest('base64'); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-26 23:10:23 -05:00
										 |  |  | function isEmptyOrWhitespace(str) { | 
					
						
							|  |  |  |     return str === null || str.match(/^ *$/) !== null; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-16 00:05:37 -05:00
										 |  |  | function getDateTimeForFile() { | 
					
						
							|  |  |  |     return new Date().toISOString().substr(0, 19).replace(/:/g, ''); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2017-11-21 22:11:27 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-23 09:35:00 -05:00
										 |  |  | function sanitizeSql(str) { | 
					
						
							|  |  |  |     // should be improved or usage eliminated
 | 
					
						
							|  |  |  |     return str.replace(/'/g, "\\'"); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-06 15:56:00 -05: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-13 15:25:09 -05:00
										 |  |  | async function stopWatch(what, func) { | 
					
						
							|  |  |  |     const start = new Date(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-22 23:18:08 -05:00
										 |  |  |     const ret = await func(); | 
					
						
							| 
									
										
										
										
											2018-01-13 15:25:09 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     const tookMs = new Date().getTime() - start.getTime(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     console.log(`${what} took ${tookMs}ms`); | 
					
						
							| 
									
										
										
										
											2018-01-22 23:18:08 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return ret; | 
					
						
							| 
									
										
										
										
											2018-01-13 15:25:09 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-26 20:47:34 -05:00
										 |  |  | function unescapeHtml(str) { | 
					
						
							|  |  |  |     return unescape(str); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-04 12:06:35 -05:00
										 |  |  | function toObject(array, fn) { | 
					
						
							|  |  |  |     const obj = {}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for (const item of array) { | 
					
						
							|  |  |  |         const ret = fn(item); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         obj[ret[0]] = ret[1]; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return obj; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | module.exports = { | 
					
						
							| 
									
										
										
										
											2017-10-28 19:55:55 -04:00
										 |  |  |     randomSecureToken, | 
					
						
							| 
									
										
										
										
											2017-10-28 12:12:20 -04:00
										 |  |  |     randomString, | 
					
						
							| 
									
										
										
										
											2017-12-10 12:56:59 -05:00
										 |  |  |     nowDate, | 
					
						
							| 
									
										
										
										
											2018-02-12 23:53:00 -05:00
										 |  |  |     localDate, | 
					
						
							| 
									
										
										
										
											2017-12-10 12:56:59 -05:00
										 |  |  |     dateStr, | 
					
						
							|  |  |  |     parseDate, | 
					
						
							| 
									
										
										
										
											2018-02-10 13:53:35 -05:00
										 |  |  |     parseDateTime, | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  |     newNoteId, | 
					
						
							| 
									
										
										
										
											2018-03-24 21:39:15 -04:00
										 |  |  |     newBranchId, | 
					
						
							| 
									
										
										
										
											2018-02-11 00:18:59 -05:00
										 |  |  |     newNoteRevisionId, | 
					
						
							| 
									
										
										
										
											2018-01-06 21:49:02 -05:00
										 |  |  |     newImageId, | 
					
						
							|  |  |  |     newNoteImageId, | 
					
						
							| 
									
										
										
										
											2018-01-11 00:01:16 -05:00
										 |  |  |     newAttributeId, | 
					
						
							| 
									
										
										
										
											2018-02-11 00:18:59 -05:00
										 |  |  |     newApiTokenId, | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  |     toBase64, | 
					
						
							| 
									
										
										
										
											2017-10-29 14:55:48 -04:00
										 |  |  |     fromBase64, | 
					
						
							| 
									
										
										
										
											2017-11-05 10:41:54 -05:00
										 |  |  |     hmac, | 
					
						
							| 
									
										
										
										
											2017-11-05 21:56:42 -05:00
										 |  |  |     isElectron, | 
					
						
							| 
									
										
										
										
											2017-11-26 23:10:23 -05:00
										 |  |  |     hash, | 
					
						
							| 
									
										
										
										
											2017-12-16 00:05:37 -05:00
										 |  |  |     isEmptyOrWhitespace, | 
					
						
							| 
									
										
										
										
											2017-12-23 09:35:00 -05:00
										 |  |  |     getDateTimeForFile, | 
					
						
							| 
									
										
										
										
											2018-01-06 15:56:00 -05:00
										 |  |  |     sanitizeSql, | 
					
						
							| 
									
										
										
										
											2018-01-13 15:25:09 -05:00
										 |  |  |     assertArguments, | 
					
						
							| 
									
										
										
										
											2018-02-26 20:47:34 -05:00
										 |  |  |     stopWatch, | 
					
						
							| 
									
										
										
										
											2018-03-04 12:06:35 -05:00
										 |  |  |     unescapeHtml, | 
					
						
							|  |  |  |     toObject | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | }; |