Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

fix: do not automatically delete empty notes

thecodrrfda5e0c84bed
diff --git a/packages/core/__tests__/notes.test.js b/packages/core/__tests__/notes.test.js
index c19e1f918..eac9d967e 100644
--- a/packages/core/__tests__/notes.test.js
+++ b/packages/core/__tests__/notes.test.js
@@ -63,7 +63,7 @@ test("get all notes", () =>
test("note without a title should get title from content", () =>
noteTest().then(async ({ db, id }) => {
let note = db.notes.note(id);
- expect(note.title).toBe("Hello This is colorful");
+ expect(note.title).toBe("HelloThis is colorful");
}));
test("note should get headline from content", () =>
@@ -87,7 +87,7 @@ test("note should get headline from content containing only lists", () =>
},
}).then(async ({ db, id }) => {
let note = db.notes.note(id);
- expect(note.headline).toBe("Hello I won't be a headline :(Me too.Gold.");
+ expect(note.headline).toBe("Hello I won't be a headline :(Me too.");
}));
test("note title should allow trailing space", () =>
@@ -127,21 +127,6 @@ test("update note", () =>
expect(note.data.favorite).toBe(true);
}));
-test("updating empty note should delete it", () =>
- noteTest().then(async ({ db, id }) => {
- id = await db.notes.add({
- id,
- title: "\n",
- content: {
- type: TEST_NOTE.content.type,
- data: "<p><br></p>",
- },
- });
- expect(id).toBeUndefined();
- let note = db.notes.note(id);
- expect(note).toBeUndefined();
- }));
-
test("get favorite notes", () =>
noteTest({
...TEST_NOTE,
diff --git a/packages/core/collections/notes.js b/packages/core/collections/notes.js
index 1cbb476ea..0d7ebb44b 100644
--- a/packages/core/collections/notes.js
+++ b/packages/core/collections/notes.js
@@ -75,14 +75,6 @@ export default class Notes extends Collection {
note.title = getNoteTitle(note, content);
note.headline = getNoteHeadline(note, content);
- if (isNoteEmpty(note, content)) {
- if (oldNote) {
- EV.publish(EVENTS.noteRemoved, id);
- await this.remove(id);
- }
- return;
- }
-
note.contentId = await this._db.content.add({
noteId: id,
id: note.contentId,
@@ -277,12 +269,6 @@ export default class Notes extends Collection {
}
}
-function isNoteEmpty(note, content) {
- const { title, locked } = note;
- const isTitleEmpty = !title || !title.trim().length;
- return !locked && isTitleEmpty && content.isEmpty();
-}
-
function getNoteHeadline(note, content) {
if (note.locked) return "";
return content.toHeadline();
diff --git a/packages/core/content-types/tiny.js b/packages/core/content-types/tiny.js
index 44472654f..265d0bae3 100644
--- a/packages/core/content-types/tiny.js
+++ b/packages/core/content-types/tiny.js
@@ -1,17 +1,16 @@
-import showdown from "showdown";
-import { decode, DecodingMode, EntityLevel } from "entities";
+import showdown from "showdown/dist/showdown";
import dataurl from "../utils/dataurl";
-import { parseHTML } from "../utils/html-parser";
+import { getDummyDocument, parseHTML } from "../utils/html-parser";
var converter = new showdown.Converter();
converter.setFlavor("original");
const splitter = /\W+/gm;
-
class Tiny {
constructor(data) {
this.data = data;
this.text;
+ this.document = parseHTML(data);
}
toHTML() {
@@ -19,14 +18,13 @@ class Tiny {
}
toTXT() {
- return decode(
- this.data.replace(/<br[^>]*>/gi, "\n").replace(/<[^>]+>/g, ""),
- { level: EntityLevel.HTML, mode: DecodingMode.Strict }
- ).trim();
+ return this.document.body
+ ? this.document.body.innerText || this.document.body.textContent
+ : this.document.textContent;
}
toMD() {
- return converter.makeMarkdown(this.data);
+ return converter.makeMarkdown(this.data, getDummyDocument());
}
toTitle() {
@@ -57,8 +55,7 @@ class Tiny {
}
async insertMedia(getData) {
- let document = parseHTML(this.data);
- const attachmentElements = document.querySelectorAll("img");
+ const attachmentElements = this.document.querySelectorAll("img");
for (var i = 0; i < attachmentElements.length; ++i) {
const attachment = attachmentElements[i];
switch (attachment.tagName) {
@@ -76,14 +73,12 @@ class Tiny {
}
}
}
- return document.outerHTML || document.body.innerHTML;
+ return this.document.outerHTML || this.document.body.innerHTML;
}
async extractAttachments(store) {
const attachments = [];
- let document = parseHTML(this.data);
-
- const attachmentElements = document.querySelectorAll("img,span");
+ const attachmentElements = this.document.querySelectorAll("img,span");
for (var i = 0; i < attachmentElements.length; ++i) {
const attachment = attachmentElements[i];
@@ -134,7 +129,10 @@ class Tiny {
throw e;
}
}
- return { data: document.outerHTML || document.body.innerHTML, attachments };
+ return {
+ data: this.document.outerHTML || this.document.body.innerHTML,
+ attachments,
+ };
}
}
export default Tiny;
@@ -142,13 +140,7 @@ export default Tiny;
function getHeadlineFromText(text) {
for (var i = 0; i < text.length; ++i) {
const char = text[i];
- const nextChar = text[i + 1];
- if (
- char === "\n" ||
- char === "\t" ||
- char === "\r" ||
- (char === "." && nextChar === " ")
- ) {
+ if (char === "\n" || char === "\t" || char === "\r" || char === ".") {
if (char === ".") ++i;
return text.substring(0, i);
}
diff --git a/packages/core/jest.config.js b/packages/core/jest.config.js
index 185f7d30c..1662e128a 100644
--- a/packages/core/jest.config.js
+++ b/packages/core/jest.config.js
@@ -130,7 +130,7 @@ module.exports = {
// snapshotSerializers: [],
// The test environment that will be used for testing
- // testEnvironment: "jest-environment-jsdom",
+ testEnvironment: "node",
// Options that will be passed to the testEnvironment
// testEnvironmentOptions: {},
diff --git a/packages/core/jest.setup.js b/packages/core/jest.setup.js
index 8f81eded3..c3e695364 100644
--- a/packages/core/jest.setup.js
+++ b/packages/core/jest.setup.js
@@ -1,15 +1,6 @@
var nodeCrypto = require("crypto");
-var jsdom = require("jsdom");
var dotenv = require("dotenv");
dotenv.config();
-const dom = new jsdom.JSDOM("", {});
-global.window = dom.window;
-global.document = dom.window.document;
global.crypto = nodeCrypto;
-
-global.HTMLParser = new dom.window.DOMParser().parseFromString(
- "<body></body>",
- "text/html"
-);
diff --git a/packages/core/package-lock.json b/packages/core/package-lock.json
index f4aab41af..27fd3ef77 100644
--- a/packages/core/package-lock.json
+++ b/packages/core/package-lock.json
@@ -1,22 +1,20 @@
{
"name": "notes-core",
- "version": "6.11.0",
+ "version": "6.13.1",
"lockfileVersion": 2,
"requires": true,
"packages": {
"": {
"name": "notes-core",
- "version": "6.11.0",
+ "version": "6.13.1",
"dependencies": {
"@stablelib/blake2s": "^1.0.1",
"async-mutex": "^0.3.2",
"dayjs": "^1.10.6",
- "entities": "^3.0.1",
"fast-sort": "^2.0.1",
- "no-internet": "^1.5.2",
- "node-html-parser": "^5.1.0",
+ "node-html-parser": "github:thecodrr/node-html-parser",
"qclone": "^1.0.4",
- "showdown": "https://github.com/thecodrr/showdown",
+ "showdown": "github:thecodrr/showdown",
"spark-md5": "^3.0.1"
},
"devDependencies": {
@@ -36,6 +34,75 @@
"jsdom": "^16.5.3"
}
},
+ "../node-html-parser": {
+ "version": "5.1.0",
+ "extraneous": true,
+ "license": "MIT",
+ "dependencies": {
+ "css-select": "^4.1.3",
+ "entities": "^3.0.1"
+ },
+ "devDependencies": {
+ "@types/entities": "latest",
+ "@types/he": "latest",
+ "@types/node": "latest",
+ "@typescript-eslint/eslint-plugin": "latest",
+ "@typescript-eslint/eslint-plugin-tslint": "latest",
+ "@typescript-eslint/parser": "latest",
+ "blanket": "latest",
+ "cheerio": "^1.0.0-rc.5",
+ "cross-env": "^7.0.3",
+ "eslint": "^7.32.0",
+ "eslint-config-prettier": "latest",
+ "eslint-plugin-import": "latest",
+ "high5": "^1.0.0",
+ "htmlparser": "^1.7.7",
+ "htmlparser-benchmark": "^1.1.3",
+ "htmlparser2": "^6.0.0",
+ "mocha": "latest",
+ "mocha-each": "^2.0.1",
+ "np": "latest",
+ "parse5": "^6.0.1",
+ "rimraf": "^3.0.2",
+ "should": "latest",
+ "spec": "latest",
+ "standard-version": "^9.3.1",
+ "travis-cov": "latest",
+ "ts-node": "^10.2.1",
+ "typescript": "latest"
+ }
+ },
+ "../showdown": {
+ "version": "2.0.0-alpha1",
+ "extraneous": true,
+ "license": "MIT",
+ "dependencies": {
+ "yargs": "14.2.0"
+ },
+ "bin": {
+ "showdown": "bin/showdown.js"
+ },
+ "devDependencies": {
+ "chai": "^4.3.4",
+ "grunt": "^1.4.1",
+ "grunt-contrib-clean": "^2.0.0",
+ "grunt-contrib-concat": "^2.0.0",
+ "grunt-contrib-jshint": "^3.1.0",
+ "grunt-contrib-uglify": "^5.0.1",
+ "grunt-conventional-changelog": "^6.1.0",
+ "grunt-conventional-github-releaser": "^1.0.0",
+ "grunt-endline": "^0.7.0",
+ "grunt-eslint": "^24.0.0",
+ "grunt-simple-mocha": "^0.4.0",
+ "load-grunt-tasks": "^5.1.0",
+ "performance-now": "^2.1.0",
+ "quiet-grunt": "^0.2.0",
+ "semver": "^7.3.0",
+ "semver-sort": "^0.0.4",
+ "sinon": "^12.0.1",
+ "source-map-support": "^0.5.20"
+ }
+ },
"node_modules/@babel/code-frame": {
"version": "7.8.3",
"resolved": "https://registry.npmjs.org/@babel/code-frame/-/code-frame-7.8.3.tgz",
@@ -3678,9 +3745,9 @@
}
},
"node_modules/domhandler": {
- "version": "4.2.2",
- "resolved": "https://registry.npmjs.org/domhandler/-/domhandler-4.2.2.tgz",
- "integrity": "sha512-PzE9aBMsdZO8TK4BnuJwH0QT41wgMbRzuZrHUcpYncEjmQazq8QEaBWgLG7ZyC/DAZKEgglpIA6j4Qn/HmxS3w==",
+ "version": "4.3.0",
+ "resolved": "https://registry.npmjs.org/domhandler/-/domhandler-4.3.0.tgz",
+ "integrity": "sha512-fC0aXNQXqKSFTr2wDNZDhsEYjCiYsDWl3D01kwt25hm1YIPyDGHvvi3rw+PLqHAl/m71MaiF7d5zvBr0p5UB2g==",
"dependencies": {
"domelementtype": "^2.2.0"
},
@@ -4302,14 +4369,6 @@
"node": ">=0.10.0"
}
},
- "node_modules/he": {
- "version": "1.2.0",
- "resolved": "https://registry.npmjs.org/he/-/he-1.2.0.tgz",
- "integrity": "sha512-F/1DnUGPopORZi0ni+CvrCgHQ5FyEAHRLSApuYWMmrbSwoN2Mn/7k+Gl38gJnR7yyDZk6WLXwiGod1JOWNDKGw==",
- "bin": {
- "he": "bin/he"
- }
- },
"node_modules/home-or-tmp": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/home-or-tmp/-/home-or-tmp-2.0.0.tgz",
@@ -6102,14 +6161,6 @@
"integrity": "sha512-1nh45deeb5olNY7eX82BkPO7SSxR5SSYJiPTrTdFUVYwAl8CKMA5N9PjTYkHiRjisVcxcQ1HXdLhx2qxxJzLNQ==",
"dev": true
},
- "node_modules/no-internet": {
- "version": "1.5.2",
- "resolved": "https://registry.npmjs.org/no-internet/-/no-internet-1.5.2.tgz",
- "integrity": "sha512-4GnJQsc/BCwcOG+8acwAsm9bYVdgh0V5ORQTAnB+7u9hoKdR4WqSqw6Bhjo3DO/tvhad4wbrw4l07/CG6C+wDg==",
- "dependencies": {
- "set-interval": "^2.0.1"
- }
- },
"node_modules/node-fetch": {
"version": "2.6.0",
"resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.0.tgz",
@@ -6121,11 +6172,11 @@
},
"node_modules/node-html-parser": {
"version": "5.1.0",
- "resolved": "https://registry.npmjs.org/node-html-parser/-/node-html-parser-5.1.0.tgz",
- "integrity": "sha512-l6C1Gf1o7YuxeMGa17PypEez/rj+ii3q4/NZG37nRmWSLDjHyB0WNrlE4h2UW92D0JSfUSfu+lOvxThttVe7Jw==",
+ "resolved": "git+ssh://[email protected]/thecodrr/node-html-parser.git#35d8d7b2965943b11e669e3e53438a681bbbdee2",
+ "license": "MIT",
"dependencies": {
"css-select": "^4.1.3",
- "he": "1.2.0"
+ "entities": "^3.0.1"
}
},
"node_modules/node-int64": {
@@ -7142,11 +7193,6 @@
"resolved": "https://registry.npmjs.org/set-blocking/-/set-blocking-2.0.0.tgz",
"integrity": "sha1-BF+XgtARrppoA93TgrJDkrPYkPc="
},
- "node_modules/set-interval": {
- "version": "2.0.4",
- "resolved": "https://registry.npmjs.org/set-interval/-/set-interval-2.0.4.tgz",
- "integrity": "sha512-Srlnjq2O84WQxv2DkTyf7S0nRebiNE+uD5nd2Iy4ISOtiZbxglSfup88vegIAYRpCn/ITS1rYmjCPuAhn/hdmg=="
- },
"node_modules/set-value": {
"version": "2.0.1",
"resolved": "https://registry.npmjs.org/set-value/-/set-value-2.0.1.tgz",
@@ -7191,8 +7237,8 @@
},
"node_modules/showdown": {
"version": "2.0.0-alpha1",
- "resolved": "git+ssh://[email protected]/thecodrr/showdown.git#f34864e9bea3d3ca597c5b034aceb0573a89c3b0",
- "license": "BSD-3-Clause",
+ "resolved": "git+ssh://[email protected]/thecodrr/showdown.git#82545b8ff5f318b91a16f08f8a368f9d92eeafee",
+ "license": "MIT",
"dependencies": {
"yargs": "14.2.0"
},
@@ -7219,9 +7265,9 @@
}
},
"node_modules/showdown/node_modules/yargs-parser": {
- "version": "15.0.1",
- "resolved": "https://registry.npmjs.org/yargs-parser/-/yargs-parser-15.0.1.tgz",
- "integrity": "sha512-0OAMV2mAZQrs3FkNpDQcBk1x5HXb8X4twADss4S0Iuk+2dGnLOE/fRHrsYm542GduMveyA77OF4wrNJuanRCWw==",
+ "version": "15.0.3",
+ "resolved": "https://registry.npmjs.org/yargs-parser/-/yargs-parser-15.0.3.tgz",
+ "integrity": "sha512-/MVEVjTXy/cGAjdtQf8dW3V9b97bPN7rNn8ETj6BmAQL7ibC7O1Q9SPJbGjgh3SlwoBNXMzj/ZGIj8mBgl12YA==",
"dependencies": {
"camelcase": "^5.0.0",
"decamelize": "^1.2.0"
@@ -11405,9 +11451,9 @@
}
},
"domhandler": {
- "version": "4.2.2",
- "resolved": "https://registry.npmjs.org/domhandler/-/domhandler-4.2.2.tgz",
- "integrity": "sha512-PzE9aBMsdZO8TK4BnuJwH0QT41wgMbRzuZrHUcpYncEjmQazq8QEaBWgLG7ZyC/DAZKEgglpIA6j4Qn/HmxS3w==",
+ "version": "4.3.0",
+ "resolved": "https://registry.npmjs.org/domhandler/-/domhandler-4.3.0.tgz",
+ "integrity": "sha512-fC0aXNQXqKSFTr2wDNZDhsEYjCiYsDWl3D01kwt25hm1YIPyDGHvvi3rw+PLqHAl/m71MaiF7d5zvBr0p5UB2g==",
"requires": {
"domelementtype": "^2.2.0"
}
@@ -11884,11 +11930,6 @@
}
}
},
- "he": {
- "version": "1.2.0",
- "resolved": "https://registry.npmjs.org/he/-/he-1.2.0.tgz",
- "integrity": "sha512-F/1DnUGPopORZi0ni+CvrCgHQ5FyEAHRLSApuYWMmrbSwoN2Mn/7k+Gl38gJnR7yyDZk6WLXwiGod1JOWNDKGw=="
- },
"home-or-tmp": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/home-or-tmp/-/home-or-tmp-2.0.0.tgz",
@@ -13333,14 +13374,6 @@
"integrity": "sha512-1nh45deeb5olNY7eX82BkPO7SSxR5SSYJiPTrTdFUVYwAl8CKMA5N9PjTYkHiRjisVcxcQ1HXdLhx2qxxJzLNQ==",
"dev": true
},
- "no-internet": {
- "version": "1.5.2",
- "resolved": "https://registry.npmjs.org/no-internet/-/no-internet-1.5.2.tgz",
- "integrity": "sha512-4GnJQsc/BCwcOG+8acwAsm9bYVdgh0V5ORQTAnB+7u9hoKdR4WqSqw6Bhjo3DO/tvhad4wbrw4l07/CG6C+wDg==",
- "requires": {
- "set-interval": "^2.0.1"
- }
- },
"node-fetch": {
"version": "2.6.0",
"resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.0.tgz",
@@ -13348,12 +13381,11 @@
"dev": true
},
"node-html-parser": {
- "version": "5.1.0",
- "resolved": "https://registry.npmjs.org/node-html-parser/-/node-html-parser-5.1.0.tgz",
- "integrity": "sha512-l6C1Gf1o7YuxeMGa17PypEez/rj+ii3q4/NZG37nRmWSLDjHyB0WNrlE4h2UW92D0JSfUSfu+lOvxThttVe7Jw==",
+ "version": "git+ssh://[email protected]/thecodrr/node-html-parser.git#35d8d7b2965943b11e669e3e53438a681bbbdee2",
+ "from": "node-html-parser@github:thecodrr/node-html-parser",
"requires": {
"css-select": "^4.1.3",
- "he": "1.2.0"
+ "entities": "^3.0.1"
}
},
"node-int64": {
@@ -14141,11 +14173,6 @@
"resolved": "https://registry.npmjs.org/set-blocking/-/set-blocking-2.0.0.tgz",
"integrity": "sha1-BF+XgtARrppoA93TgrJDkrPYkPc="
},
- "set-interval": {
- "version": "2.0.4",
- "resolved": "https://registry.npmjs.org/set-interval/-/set-interval-2.0.4.tgz",
- "integrity": "sha512-Srlnjq2O84WQxv2DkTyf7S0nRebiNE+uD5nd2Iy4ISOtiZbxglSfup88vegIAYRpCn/ITS1rYmjCPuAhn/hdmg=="
- },
"set-value": {
"version": "2.0.1",
"resolved": "https://registry.npmjs.org/set-value/-/set-value-2.0.1.tgz",
@@ -14180,8 +14207,8 @@
"dev": true
},
"showdown": {
- "version": "git+ssh://[email protected]/thecodrr/showdown.git#f34864e9bea3d3ca597c5b034aceb0573a89c3b0",
- "from": "showdown@https://github.com/thecodrr/showdown",
+ "version": "git+ssh://[email protected]/thecodrr/showdown.git#82545b8ff5f318b91a16f08f8a368f9d92eeafee",
+ "from": "showdown@github:thecodrr/showdown",
"requires": {
"yargs": "14.2.0"
},
@@ -14205,9 +14232,9 @@
}
},
"yargs-parser": {
- "version": "15.0.1",
- "resolved": "https://registry.npmjs.org/yargs-parser/-/yargs-parser-15.0.1.tgz",
- "integrity": "sha512-0OAMV2mAZQrs3FkNpDQcBk1x5HXb8X4twADss4S0Iuk+2dGnLOE/fRHrsYm542GduMveyA77OF4wrNJuanRCWw==",
+ "version": "15.0.3",
+ "resolved": "https://registry.npmjs.org/yargs-parser/-/yargs-parser-15.0.3.tgz",
+ "integrity": "sha512-/MVEVjTXy/cGAjdtQf8dW3V9b97bPN7rNn8ETj6BmAQL7ibC7O1Q9SPJbGjgh3SlwoBNXMzj/ZGIj8mBgl12YA==",
"requires": {
"camelcase": "^5.0.0",
"decamelize": "^1.2.0"
diff --git a/packages/core/package.json b/packages/core/package.json
index 865792a73..95247d9ec 100644
--- a/packages/core/package.json
+++ b/packages/core/package.json
@@ -26,12 +26,10 @@
"@stablelib/blake2s": "^1.0.1",
"async-mutex": "^0.3.2",
"dayjs": "^1.10.6",
- "entities": "^3.0.1",
"fast-sort": "^2.0.1",
- "no-internet": "^1.5.2",
- "node-html-parser": "^5.1.0",
+ "node-html-parser": "github:thecodrr/node-html-parser",
"qclone": "^1.0.4",
- "showdown": "https://github.com/thecodrr/showdown",
+ "showdown": "github:thecodrr/showdown",
"spark-md5": "^3.0.1"
}
}
diff --git a/packages/core/utils/html-parser.js b/packages/core/utils/html-parser.js
index 8b32a57de..4f05cf8a3 100644
--- a/packages/core/utils/html-parser.js
+++ b/packages/core/utils/html-parser.js
@@ -4,3 +4,8 @@ export const parseHTML =
typeof DOMParser === "undefined"
? (input) => parse(input)
: (input) => new DOMParser().parseFromString(input, "text/html");
+
+export function getDummyDocument() {
+ const doc = parseHTML("<div></div>");
+ return typeof DOMParser === "undefined" ? doc : doc.body.firstElementChild;
+}
diff --git a/packages/core/utils/id.js b/packages/core/utils/id.js
index 15b80edef..fd7aaabf3 100644
--- a/packages/core/utils/id.js
+++ b/packages/core/utils/id.js
@@ -1,7 +1,6 @@
-import { hash, SALT_LENGTH, PERSONALIZATION_LENGTH } from "@stablelib/blake2s";
+import { hash, SALT_LENGTH } from "@stablelib/blake2s";
import SparkMD5 from "spark-md5";
import { USER_PERSONALIZATION_HASH } from "../common";
-import ObjectID from "./object-id";
import { randomBytes } from "./random";
export default function () {
diff --git a/packages/core/utils/object-id.js b/packages/core/utils/object-id.js
deleted file mode 100644
index 53b3e89f7..000000000
--- a/packages/core/utils/object-id.js
+++ /dev/null
@@ -1,343 +0,0 @@
-// Copied from https://github.com/williamkapke/bson-objectid
-
-const { randomInt } = require("./random");
-
-var MACHINE_ID = randomInt();
-var index = (ObjectID.index = randomInt());
-var pid =
- (typeof process === "undefined" || typeof process.pid !== "number"
- ? randomInt()
- : process.pid) % 0xffff;
-
-/**
- * Determine if an object is Buffer
- *
- * Author: Feross Aboukhadijeh <[email protected]> <http://feross.org>
- * License: MIT
- *
- */
-var isBuffer = function (obj) {
- return !!(
- obj != null &&
- obj.constructor &&
- typeof obj.constructor.isBuffer === "function" &&
- obj.constructor.isBuffer(obj)
- );
-};
-
-// Precomputed hex table enables speedy hex string conversion
-var hexTable = [];
-for (var i = 0; i < 256; i++) {
- hexTable[i] = (i <= 15 ? "0" : "") + i.toString(16);
-}
-
-// Regular expression that checks for hex value
-var checkForHexRegExp = new RegExp("^[0-9a-fA-F]{24}$");
-
-// Lookup tables
-var decodeLookup = [];
-i = 0;
-while (i < 10) decodeLookup[0x30 + i] = i++;
-while (i < 16) decodeLookup[0x41 - 10 + i] = decodeLookup[0x61 - 10 + i] = i++;
-
-/**
- * Create a new immutable ObjectID instance
- *
- * @class Represents the BSON ObjectID type
- * @param {String|Number} id Can be a 24 byte hex string, 12 byte binary string or a Number.
- * @return {Object} instance of ObjectID.
- */
-function ObjectID(id) {
- if (!(this instanceof ObjectID)) return new ObjectID(id);
- if (id && (id instanceof ObjectID || id._bsontype === "ObjectID")) return id;
-
- this._bsontype = "ObjectID";
-
- // The most common usecase (blank id, new objectId instance)
- if (id == null || typeof id === "number") {
- // Generate a new id
- this.id = this.generate(id);
- // Return the object
- return;
- }
-
- // Check if the passed in id is valid
- var valid = ObjectID.isValid(id);
-
- // Throw an error if it's not a valid setup
- if (!valid && id != null) {
- throw new Error(
- "Argument passed in must be a single String of 12 bytes or a string of 24 hex characters"
- );
- } else if (valid && typeof id === "string" && id.length === 24) {
- return ObjectID.createFromHexString(id);
- } else if (id != null && id.length === 12) {
- // assume 12 byte string
- this.id = id;
- } else if (id != null && typeof id.toHexString === "function") {
- // Duck-typing to support ObjectId from different npm packages
- return id;
- } else {
- throw new Error(
- "Argument passed in must be a single String of 12 bytes or a string of 24 hex characters"
- );
- }
-}
-module.exports = ObjectID;
-ObjectID.default = ObjectID;
-
-/**
- * Creates an ObjectID from a second based number, with the rest of the ObjectID zeroed out. Used for comparisons or sorting the ObjectID.
- *
- * @param {Number} time an integer number representing a number of seconds.
- * @return {ObjectID} return the created ObjectID
- * @api public
- */
-ObjectID.createFromTime = function (time) {
- time = parseInt(time, 10) % 0xffffffff;
- return new ObjectID(hex(8, time) + "0000000000000000");
-};
-
-/**
- * Creates an ObjectID from a hex string representation of an ObjectID.
- *
- * @param {String} hexString create a ObjectID from a passed in 24 byte hexstring.
- * @return {ObjectID} return the created ObjectID
- * @api public
- */
-ObjectID.createFromHexString = function (hexString) {
- // Throw an error if it's not a valid setup
- if (
- typeof hexString === "undefined" ||
- (hexString != null && hexString.length !== 24)
- ) {
- throw new Error(
- "Argument passed in must be a single String of 12 bytes or a string of 24 hex characters"
- );
- }
-
- // Calculate lengths
- var data = "";
- var i = 0;
-
- while (i < 24) {
- data += String.fromCharCode(
- (decodeLookup[hexString.charCodeAt(i++)] << 4) |
- decodeLookup[hexString.charCodeAt(i++)]
- );
- }
-
- return new ObjectID(data);
-};
-
-/**
- * Checks if a value is a valid bson ObjectId
- *
- * @param {String} objectid Can be a 24 byte hex string or an instance of ObjectID.
- * @return {Boolean} return true if the value is a valid bson ObjectID, return false otherwise.
- * @api public
- *
- * THE NATIVE DOCUMENTATION ISN'T CLEAR ON THIS GUY!
- * http://mongodb.github.io/node-mongodb-native/api-bson-generated/objectid.html#objectid-isvalid
- */
-ObjectID.isValid = function (id) {
- if (id == null) return false;
-
- if (typeof id === "number") {
- return true;
- }
-
- if (typeof id === "string") {
- return id.length === 12 || (id.length === 24 && checkForHexRegExp.test(id));
- }
-
- if (id instanceof ObjectID) {
- return true;
- }
-
- if (isBuffer(id)) {
- return true;
- }
-
- // Duck-Typing detection of ObjectId like objects
- if (
- typeof id.toHexString === "function" &&
- (id.id instanceof _Buffer || typeof id.id === "string")
- ) {
- return (
- id.id.length === 12 ||
- (id.id.length === 24 && checkForHexRegExp.test(id.id))
- );
- }
-
- return false;
-};
-
-ObjectID.prototype = {
- constructor: ObjectID,
-
- /**
- * Return the ObjectID id as a 24 byte hex string representation
- *
- * @return {String} return the 24 byte hex string representation.
- * @api public
- */
- toHexString: function () {
- if (!this.id || !this.id.length) {
- throw new Error(
- "invalid ObjectId, ObjectId.id must be either a string or a Buffer, but is [" +
- JSON.stringify(this.id) +
- "]"
- );
- }
-
- if (this.id.length === 24) {
- return this.id;
- }
-
- if (isBuffer(this.id)) {
- return this.id.toString("hex");
- }
-
- var hexString = "";
- for (var i = 0; i < this.id.length; i++) {
- hexString += hexTable[this.id.charCodeAt(i)];
- }
-
- return hexString;
- },
-
- /**
- * Compares the equality of this ObjectID with `otherID`.
- *
- * @param {Object} otherId ObjectID instance to compare against.
- * @return {Boolean} the result of comparing two ObjectID's
- * @api public
- */
- equals: function (otherId) {
- if (otherId instanceof ObjectID) {
- return this.toString() === otherId.toString();
- } else if (
- typeof otherId === "string" &&
- ObjectID.isValid(otherId) &&
- otherId.length === 12 &&
- isBuffer(this.id)
- ) {
- return otherId === this.id.toString("binary");
- } else if (
- typeof otherId === "string" &&
- ObjectID.isValid(otherId) &&
- otherId.length === 24
- ) {
- return otherId.toLowerCase() === this.toHexString();
- } else if (
- typeof otherId === "string" &&
- ObjectID.isValid(otherId) &&
- otherId.length === 12
- ) {
- return otherId === this.id;
- } else if (
- otherId != null &&
- (otherId instanceof ObjectID || otherId.toHexString)
- ) {
- return otherId.toHexString() === this.toHexString();
- } else {
- return false;
- }
- },
-
- /**
- * Returns the generation date (accurate up to the second) that this ID was generated.
- *
- * @return {Date} the generation date
- * @api public
- */
- getTimestamp: function () {
- var timestamp = new Date();
- var time;
- if (isBuffer(this.id)) {
- time =
- this.id[3] |
- (this.id[2] << 8) |
- (this.id[1] << 16) |
- (this.id[0] << 24);
- } else {
- time =
- this.id.charCodeAt(3) |
- (this.id.charCodeAt(2) << 8) |
- (this.id.charCodeAt(1) << 16) |
- (this.id.charCodeAt(0) << 24);
- }
- timestamp.setTime(Math.floor(time) * 1000);
- return timestamp;
- },
-
- /**
- * Generate a 12 byte id buffer used in ObjectID's
- *
- * @method
- * @param {number} [time] optional parameter allowing to pass in a second based timestamp.
- * @return {string} return the 12 byte id buffer string.
- */
- generate: function (time) {
- if ("number" !== typeof time) {
- time = ~~(Date.now() / 1000);
- }
-
- //keep it in the ring!
- time = parseInt(time, 10) % 0xffffffff;
-
- var inc = next();
-
- return String.fromCharCode(
- (time >> 24) & 0xff,
- (time >> 16) & 0xff,
- (time >> 8) & 0xff,
- time & 0xff,
- (MACHINE_ID >> 16) & 0xff,
- (MACHINE_ID >> 8) & 0xff,
- MACHINE_ID & 0xff,
- (pid >> 8) & 0xff,
- pid & 0xff,
- (inc >> 16) & 0xff,
- (inc >> 8) & 0xff,
- inc & 0xff
- );
- },
-};
-
-function next() {
- return (index = (index + 1) % 0xffffff);
-}
-
-function hex(length, n) {
- n = n.toString(16);
- return n.length === length ? n : "00000000".substring(n.length, length) + n;
-}
-
-function buffer(str) {
- var i = 0,
- out = [];
-
- if (str.length === 24)
- for (; i < 24; out.push(parseInt(str[i] + str[i + 1], 16)), i += 2);
- else if (str.length === 12) for (; i < 12; out.push(str.charCodeAt(i)), i++);
-
- return out;
-}
-
-var inspect =
- (Symbol && Symbol.for && Symbol.for("nodejs.util.inspect.custom")) ||
- "inspect";
-
-/**
- * Converts to a string representation of this Id.
- *
- * @return {String} return the 24 byte hex string representation.
- * @api private
- */
-ObjectID.prototype[inspect] = function () {
- return "ObjectID(" + this + ")";
-};
-ObjectID.prototype.toJSON = ObjectID.prototype.toHexString;
-ObjectID.prototype.toString = ObjectID.prototype.toHexString;