From 2a121faccc6760cc0ec2085bd51514520870a6df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Rebours?= Date: Fri, 2 Sep 2022 14:41:12 +0200 Subject: [PATCH 1/5] add testSerializationHooks option --- index.d.ts | 1 + lib/datastore.js | 5 ++++- lib/persistence.js | 13 ++++++++----- test/persistence.async.test.js | 14 ++++++++++++++ 4 files changed, 27 insertions(+), 6 deletions(-) diff --git a/index.d.ts b/index.d.ts index bbbd4af..b3059ab 100644 --- a/index.d.ts +++ b/index.d.ts @@ -114,6 +114,7 @@ declare namespace Nedb { corruptAlertThreshold?: number; compareStrings?(a: string, b: string): number; modes?: {fileMode: number, dirMode: number}; + testSerializationHooks?: boolean; } interface UpdateOptions { diff --git a/lib/datastore.js b/lib/datastore.js index 140cccf..1eabc88 100755 --- a/lib/datastore.js +++ b/lib/datastore.js @@ -193,6 +193,8 @@ class Datastore extends EventEmitter { * @param {compareStrings} [options.compareStrings] If specified, it overrides default string comparison which is not * well adapted to non-US characters in particular accented letters. Native `localCompare` will most of the time be * the right choice. + * @param {boolean} [options.testSerializationHooks=true] Whether to test the serialization hooks or not, + * might be CPU-intensive */ constructor (options) { super() @@ -262,7 +264,8 @@ class Datastore extends EventEmitter { afterSerialization: options.afterSerialization, beforeDeserialization: options.beforeDeserialization, corruptAlertThreshold: options.corruptAlertThreshold, - modes: options.modes + modes: options.modes, + testSerializationHooks: options.testSerializationHooks }) // This new executor is ready if we don't use persistence diff --git a/lib/persistence.js b/lib/persistence.js index 2d21aaa..5f96d0a 100755 --- a/lib/persistence.js +++ b/lib/persistence.js @@ -49,6 +49,7 @@ class Persistence { * @param {object} [options.modes] Modes to use for FS permissions. Will not work on Windows. * @param {number} [options.modes.fileMode=0o644] Mode to use for files. * @param {number} [options.modes.dirMode=0o755] Mode to use for directories. + * @param {boolean} [options.testSerializationHooks=true] Whether to test the serialization hooks or not, might be CPU-intensive */ constructor (options) { this.db = options.db @@ -77,11 +78,13 @@ class Persistence { this.afterSerialization = options.afterSerialization || (s => s) this.beforeDeserialization = options.beforeDeserialization || (s => s) - for (let i = 1; i < 30; i += 1) { - for (let j = 0; j < 10; j += 1) { - const randomString = customUtils.uid(i) - if (this.beforeDeserialization(this.afterSerialization(randomString)) !== randomString) { - throw new Error('beforeDeserialization is not the reverse of afterSerialization, cautiously refusing to start NeDB to prevent dataloss') + if (options.testSerializationHooks === undefined || options.testSerializationHooks) { + for (let i = 1; i < 30; i += 1) { + for (let j = 0; j < 10; j += 1) { + const randomString = customUtils.uid(i) + if (this.beforeDeserialization(this.afterSerialization(randomString)) !== randomString) { + throw new Error('beforeDeserialization is not the reverse of afterSerialization, cautiously refusing to start NeDB to prevent dataloss') + } } } } diff --git a/test/persistence.async.test.js b/test/persistence.async.test.js index 7473a7c..241147b 100755 --- a/test/persistence.async.test.js +++ b/test/persistence.async.test.js @@ -461,6 +461,20 @@ describe('Persistence async', function () { assert.equal(await fs.readFile(hookTestFilename, 'utf8'), 'Some content') }) + it('Declaring two hooks that are not reverse of one another will not cause exception if options.testSerializationHooks === false', async () => { + const hookTestFilename = 'workspace/hookTest.db' + await storage.ensureFileDoesntExistAsync(hookTestFilename) + await fs.writeFile(hookTestFilename, 'Some content', 'utf8') + const db = new Datastore({ + filename: hookTestFilename, + autoload: true, + afterSerialization: as, + beforeDeserialization: function (s) { return s }, + testSerializationHooks: false + }) + await assert.rejects(() => db.autoloadPromise) + }) + it('A serialization hook can be used to transform data before writing new state to disk', async () => { const hookTestFilename = 'workspace/hookTest.db' await storage.ensureFileDoesntExistAsync(hookTestFilename) From 314375f92e4a8abf3778c71ab498ea95613e0030 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Rebours?= Date: Fri, 2 Sep 2022 14:45:32 +0200 Subject: [PATCH 2/5] 3.0.1-0 --- package-lock.json | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index 46301a6..d9ddd3b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@seald-io/nedb", - "version": "3.0.0", + "version": "3.0.1-0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@seald-io/nedb", - "version": "3.0.0", + "version": "3.0.1-0", "license": "MIT", "dependencies": { "@seald-io/binary-search-tree": "^1.0.2", diff --git a/package.json b/package.json index e185c04..f682706 100755 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@seald-io/nedb", - "version": "3.0.0", + "version": "3.0.1-0", "files": [ "lib/**/*.js", "browser-version/**/*.js", From 62f3d4f977562bb189f634df45681de55a76e4c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Rebours?= Date: Fri, 2 Sep 2022 14:46:53 +0200 Subject: [PATCH 3/5] changelog --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1e46b52..b06c281 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [3.0.1] - 2022-09-02 +### Added +- Added a `testSerializationHooks` option which defaults to `true` to allow skipping the test of the hooks which may be slow + ## [3.0.0] - 2022-03-16 ### Added - Added a `Promise`-based interface. From 6cb1eada4497a4fc7a9c3918550f3a73f58989d4 Mon Sep 17 00:00:00 2001 From: tex0l Date: Fri, 2 Sep 2022 14:52:24 +0200 Subject: [PATCH 4/5] Update CHANGELOG.md Co-authored-by: arantes555 --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b06c281..9298818 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,7 +8,7 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [3.0.1] - 2022-09-02 ### Added -- Added a `testSerializationHooks` option which defaults to `true` to allow skipping the test of the hooks which may be slow +- Added a `testSerializationHooks` option which defaults to `true`. Setting to `false` allows to skip the test of the hooks, which may be slow. ## [3.0.0] - 2022-03-16 ### Added From 960235f8e654f6156c3af8b96d1ba96e00743c59 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Rebours?= Date: Fri, 2 Sep 2022 14:53:02 +0200 Subject: [PATCH 5/5] fix changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9298818..af5ece3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,7 +6,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). -## [3.0.1] - 2022-09-02 +## [3.1.0] - 2022-09-02 ### Added - Added a `testSerializationHooks` option which defaults to `true`. Setting to `false` allows to skip the test of the hooks, which may be slow.