From de162b0b169fd2f7ce9d7b5920f2b1fac59e28f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Rebours?= Date: Mon, 7 Jun 2021 11:55:03 +0200 Subject: [PATCH 1/2] stop using util --- CHANGELOG.md | 5 ++ lib/datastore.js | 6 +- lib/indexes.js | 5 +- lib/model.js | 25 +++---- lib/utils.js | 10 +++ package-lock.json | 187 ---------------------------------------------- package.json | 1 - webpack.config.js | 1 - 8 files changed, 32 insertions(+), 208 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4c8f38a..a5e125e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,11 @@ 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). +## [2.0.3] - 2021-06-07 +### Fixed +- no longer use `util` module for type verification as it needed to be + polyfilled in the browser. + ## [2.0.2] - 2021-05-26 ### Fixed - the `browser` field of the `package.json` no longer points to the bundled diff --git a/lib/datastore.js b/lib/datastore.js index 48296e9..f301026 100755 --- a/lib/datastore.js +++ b/lib/datastore.js @@ -1,5 +1,4 @@ const { EventEmitter } = require('events') -const util = require('util') const async = require('async') const Cursor = require('./cursor.js') const customUtils = require('./customUtils.js') @@ -7,6 +6,7 @@ const Executor = require('./executor.js') const Index = require('./indexes.js') const model = require('./model.js') const Persistence = require('./persistence.js') +const { isDate } = require('./utils.js') class Datastore extends EventEmitter { /** @@ -250,7 +250,7 @@ class Datastore extends EventEmitter { // For a basic match usableQueryKeys = [] Object.keys(query).forEach(k => { - if (typeof query[k] === 'string' || typeof query[k] === 'number' || typeof query[k] === 'boolean' || util.types.isDate(query[k]) || query[k] === null) { + if (typeof query[k] === 'string' || typeof query[k] === 'number' || typeof query[k] === 'boolean' || isDate(query[k]) || query[k] === null) { usableQueryKeys.push(k) } }) @@ -297,7 +297,7 @@ class Datastore extends EventEmitter { docs.forEach(doc => { let valid = true ttlIndexesFieldNames.forEach(i => { - if (doc[i] !== undefined && util.types.isDate(doc[i]) && Date.now() > doc[i].getTime() + this.ttlIndexes[i] * 1000) { + if (doc[i] !== undefined && isDate(doc[i]) && Date.now() > doc[i].getTime() + this.ttlIndexes[i] * 1000) { valid = false } }) diff --git a/lib/indexes.js b/lib/indexes.js index 4d25b1a..eebffc6 100755 --- a/lib/indexes.js +++ b/lib/indexes.js @@ -1,7 +1,6 @@ -const util = require('util') const BinarySearchTree = require('@seald-io/binary-search-tree').BinarySearchTree const model = require('./model.js') -const { uniq } = require('./utils.js') +const { uniq, isDate } = require('./utils.js') /** * Two indexed pointers are equal iif they point to the same place @@ -16,7 +15,7 @@ function projectForUnique (elt) { if (typeof elt === 'string') return '$string' + elt if (typeof elt === 'boolean') return '$boolean' + elt if (typeof elt === 'number') return '$number' + elt - if (util.types.isDate(elt)) return '$date' + elt.getTime() + if (isDate(elt)) return '$date' + elt.getTime() return elt // Arrays and objects, will check for pointer equality } diff --git a/lib/model.js b/lib/model.js index c28128c..3d24b96 100755 --- a/lib/model.js +++ b/lib/model.js @@ -4,8 +4,7 @@ * Copying * Querying, update */ -const util = require('util') -const { uniq } = require('./utils.js') +const { uniq, isDate, isRegExp } = require('./utils.js') const modifierFunctions = {} const lastStepModifierFunctions = {} const comparisonFunctions = {} @@ -106,7 +105,7 @@ function deepCopy (obj, strictKeys) { typeof obj === 'number' || typeof obj === 'string' || obj === null || - (util.types.isDate(obj)) + (isDate(obj)) ) return obj if (Array.isArray(obj)) return obj.map(o => deepCopy(o, strictKeys)) @@ -136,7 +135,7 @@ const isPrimitiveType = obj => ( typeof obj === 'number' || typeof obj === 'string' || obj === null || - util.types.isDate(obj) || + isDate(obj) || Array.isArray(obj) ) @@ -197,8 +196,8 @@ const compareThings = (a, b, _compareStrings) => { if (typeof b === 'boolean') return typeof a === 'boolean' ? compareNSB(a, b) : 1 // Dates - if (util.types.isDate(a)) return util.types.isDate(b) ? compareNSB(a.getTime(), b.getTime()) : -1 - if (util.types.isDate(b)) return util.types.isDate(a) ? compareNSB(a.getTime(), b.getTime()) : 1 + if (isDate(a)) return isDate(b) ? compareNSB(a.getTime(), b.getTime()) : -1 + if (isDate(b)) return isDate(a) ? compareNSB(a.getTime(), b.getTime()) : 1 // Arrays (first element is most significant and so on) if (Array.isArray(a)) return Array.isArray(b) ? compareArrays(a, b) : -1 @@ -484,7 +483,7 @@ const areThingsEqual = (a, b) => { ) return a === b // Dates - if (util.types.isDate(a) || util.types.isDate(b)) return util.types.isDate(a) && util.types.isDate(b) && a.getTime() === b.getTime() + if (isDate(a) || isDate(b)) return isDate(a) && isDate(b) && a.getTime() === b.getTime() // Arrays (no match since arrays are used as a $in) // undefined (no match since they mean field doesn't exist and can't be serialized) @@ -519,10 +518,10 @@ const areComparable = (a, b) => { if ( typeof a !== 'string' && typeof a !== 'number' && - !util.types.isDate(a) && + !isDate(a) && typeof b !== 'string' && typeof b !== 'number' && - !util.types.isDate(b) + !isDate(b) ) return false if (typeof a !== typeof b) return false @@ -562,7 +561,7 @@ comparisonFunctions.$nin = (a, b) => { } comparisonFunctions.$regex = (a, b) => { - if (!util.types.isRegExp(b)) throw new Error('$regex operator called with non regular expression') + if (!isRegExp(b)) throw new Error('$regex operator called with non regular expression') if (typeof a !== 'string') return false else return b.test(a) @@ -683,7 +682,7 @@ function matchQueryPart (obj, queryKey, queryValue, treatObjAsValue) { if (Array.isArray(queryValue)) return matchQueryPart(obj, queryKey, queryValue, true) // Check if we are using an array-specific comparison function - if (queryValue !== null && typeof queryValue === 'object' && !util.types.isRegExp(queryValue)) { + if (queryValue !== null && typeof queryValue === 'object' && !isRegExp(queryValue)) { for (const key in queryValue) { if (Object.prototype.hasOwnProperty.call(queryValue, key) && arrayComparisonFunctions[key]) { return matchQueryPart(obj, queryKey, queryValue, true) } } @@ -698,7 +697,7 @@ function matchQueryPart (obj, queryKey, queryValue, treatObjAsValue) { // queryValue is an actual object. Determine whether it contains comparison operators // or only normal fields. Mixed objects are not allowed - if (queryValue !== null && typeof queryValue === 'object' && !util.types.isRegExp(queryValue) && !Array.isArray(queryValue)) { + if (queryValue !== null && typeof queryValue === 'object' && !isRegExp(queryValue) && !Array.isArray(queryValue)) { const keys = Object.keys(queryValue) const firstChars = keys.map(item => item[0]) const dollarFirstChars = firstChars.filter(c => c === '$') @@ -717,7 +716,7 @@ function matchQueryPart (obj, queryKey, queryValue, treatObjAsValue) { } // Using regular expressions with basic querying - if (util.types.isRegExp(queryValue)) return comparisonFunctions.$regex(objValue, queryValue) + if (isRegExp(queryValue)) return comparisonFunctions.$regex(objValue, queryValue) // queryValue is either a native value or a normal object // Basic matching is possible diff --git a/lib/utils.js b/lib/utils.js index 9f18e65..d3d1735 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -3,4 +3,14 @@ const uniq = (array, iterator) => { else return [...new Set(array)] } +const objectToString = o => Object.prototype.toString.call(o) + +const isObject = arg => typeof arg === 'object' && arg !== null + +const isDate = d => isObject(d) && objectToString(d) === '[object Date]' + +const isRegExp = re => isObject(re) && objectToString(re) === '[object RegExp]' + module.exports.uniq = uniq +module.exports.isDate = isDate +module.exports.isRegExp = isRegExp diff --git a/package-lock.json b/package-lock.json index 11eee68..7567004 100644 --- a/package-lock.json +++ b/package-lock.json @@ -33,7 +33,6 @@ "standard": "^16.0.3", "terser-webpack-plugin": "^5.1.2", "timers-browserify": "^2.0.12", - "util": "^0.12.3", "webpack": "^5.37.0", "webpack-cli": "^4.7.0", "xvfb-maybe": "^0.2.1" @@ -572,12 +571,6 @@ "integrity": "sha512-8+9WqebbFzpX9OR+Wa6O29asIogeRMzcGtAINdpMHHyAg10f05aSFVBbcEqGf/PXw1EjAZ+q2/bEBg3DvurK3Q==", "dev": true }, - "node_modules/array-filter": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/array-filter/-/array-filter-1.0.0.tgz", - "integrity": "sha1-uveeYubvTCpMC4MSMtr/7CUfnYM=", - "dev": true - }, "node_modules/array-includes": { "version": "3.1.3", "resolved": "https://registry.npmjs.org/array-includes/-/array-includes-3.1.3.tgz", @@ -655,21 +648,6 @@ "resolved": "https://registry.npmjs.org/async/-/async-0.2.10.tgz", "integrity": "sha1-trvgsGdLnXGXCMo43owjfLUmw9E=" }, - "node_modules/available-typed-arrays": { - "version": "1.0.2", - "resolved": "https://registry.npmjs.org/available-typed-arrays/-/available-typed-arrays-1.0.2.tgz", - "integrity": "sha512-XWX3OX8Onv97LMk/ftVyBibpGwY5a8SmuxZPzeOxqmuEqUCOM9ZE+uIaD1VNJ5QnvU2UQusvmKbuM1FR8QWGfQ==", - "dev": true, - "dependencies": { - "array-filter": "^1.0.0" - }, - "engines": { - "node": ">= 0.4" - }, - "funding": { - "url": "https://github.com/sponsors/ljharb" - } - }, "node_modules/balanced-match": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.2.tgz", @@ -2135,12 +2113,6 @@ } } }, - "node_modules/foreach": { - "version": "2.0.5", - "resolved": "https://registry.npmjs.org/foreach/-/foreach-2.0.5.tgz", - "integrity": "sha1-C+4AUBiusmDQo6865ljdATbsG5k=", - "dev": true - }, "node_modules/fs-extra": { "version": "8.1.0", "resolved": "https://registry.npmjs.org/fs-extra/-/fs-extra-8.1.0.tgz", @@ -2583,21 +2555,6 @@ "node": ">= 0.10" } }, - "node_modules/is-arguments": { - "version": "1.1.0", - "resolved": "https://registry.npmjs.org/is-arguments/-/is-arguments-1.1.0.tgz", - "integrity": "sha512-1Ij4lOMPl/xB5kBDn7I+b2ttPMKa8szhEIrXDuXQD/oe3HJLTLhqhgGspwgyGd6MOywBUqVvYicF72lkgDnIHg==", - "dev": true, - "dependencies": { - "call-bind": "^1.0.0" - }, - "engines": { - "node": ">= 0.4" - }, - "funding": { - "url": "https://github.com/sponsors/ljharb" - } - }, "node_modules/is-arrayish": { "version": "0.2.1", "resolved": "https://registry.npmjs.org/is-arrayish/-/is-arrayish-0.2.1.tgz", @@ -2700,18 +2657,6 @@ "node": ">=4" } }, - "node_modules/is-generator-function": { - "version": "1.0.9", - "resolved": "https://registry.npmjs.org/is-generator-function/-/is-generator-function-1.0.9.tgz", - "integrity": "sha512-ZJ34p1uvIfptHCN7sFTjGibB9/oBg17sHqzDLfuwhvmN/qLVvIQXRQ8licZQ35WJ8KuEQt/etnnzQFI9C9Ue/A==", - "dev": true, - "engines": { - "node": ">= 0.4" - }, - "funding": { - "url": "https://github.com/sponsors/ljharb" - } - }, "node_modules/is-glob": { "version": "4.0.1", "resolved": "https://registry.npmjs.org/is-glob/-/is-glob-4.0.1.tgz", @@ -2830,25 +2775,6 @@ "url": "https://github.com/sponsors/ljharb" } }, - "node_modules/is-typed-array": { - "version": "1.1.5", - "resolved": "https://registry.npmjs.org/is-typed-array/-/is-typed-array-1.1.5.tgz", - "integrity": "sha512-S+GRDgJlR3PyEbsX/Fobd9cqpZBuvUS+8asRqYDMLCb2qMzt1oz5m5oxQCxOgUDxiWsOVNi4yaF+/uvdlHlYug==", - "dev": true, - "dependencies": { - "available-typed-arrays": "^1.0.2", - "call-bind": "^1.0.2", - "es-abstract": "^1.18.0-next.2", - "foreach": "^2.0.5", - "has-symbols": "^1.0.1" - }, - "engines": { - "node": ">= 0.4" - }, - "funding": { - "url": "https://github.com/sponsors/ljharb" - } - }, "node_modules/isarray": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/isarray/-/isarray-1.0.0.tgz", @@ -5159,20 +5085,6 @@ "punycode": "^2.1.0" } }, - "node_modules/util": { - "version": "0.12.3", - "resolved": "https://registry.npmjs.org/util/-/util-0.12.3.tgz", - "integrity": "sha512-I8XkoQwE+fPQEhy9v012V+TSdH2kp9ts29i20TaaDUXsg7x/onePbhFJUExBfv/2ay1ZOp/Vsm3nDlmnFGSAog==", - "dev": true, - "dependencies": { - "inherits": "^2.0.3", - "is-arguments": "^1.0.4", - "is-generator-function": "^1.0.7", - "is-typed-array": "^1.1.3", - "safe-buffer": "^5.1.2", - "which-typed-array": "^1.1.2" - } - }, "node_modules/utils-merge": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/utils-merge/-/utils-merge-1.0.1.tgz", @@ -5388,27 +5300,6 @@ "url": "https://github.com/sponsors/ljharb" } }, - "node_modules/which-typed-array": { - "version": "1.1.4", - "resolved": "https://registry.npmjs.org/which-typed-array/-/which-typed-array-1.1.4.tgz", - "integrity": "sha512-49E0SpUe90cjpoc7BOJwyPHRqSAd12c10Qm2amdEZrJPCY2NDxaW01zHITrem+rnETY3dwrbH3UUrUwagfCYDA==", - "dev": true, - "dependencies": { - "available-typed-arrays": "^1.0.2", - "call-bind": "^1.0.0", - "es-abstract": "^1.18.0-next.1", - "foreach": "^2.0.5", - "function-bind": "^1.1.1", - "has-symbols": "^1.0.1", - "is-typed-array": "^1.1.3" - }, - "engines": { - "node": ">= 0.4" - }, - "funding": { - "url": "https://github.com/sponsors/ljharb" - } - }, "node_modules/wide-align": { "version": "1.1.3", "resolved": "https://registry.npmjs.org/wide-align/-/wide-align-1.1.3.tgz", @@ -6190,12 +6081,6 @@ "integrity": "sha512-8+9WqebbFzpX9OR+Wa6O29asIogeRMzcGtAINdpMHHyAg10f05aSFVBbcEqGf/PXw1EjAZ+q2/bEBg3DvurK3Q==", "dev": true }, - "array-filter": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/array-filter/-/array-filter-1.0.0.tgz", - "integrity": "sha1-uveeYubvTCpMC4MSMtr/7CUfnYM=", - "dev": true - }, "array-includes": { "version": "3.1.3", "resolved": "https://registry.npmjs.org/array-includes/-/array-includes-3.1.3.tgz", @@ -6249,15 +6134,6 @@ "resolved": "https://registry.npmjs.org/async/-/async-0.2.10.tgz", "integrity": "sha1-trvgsGdLnXGXCMo43owjfLUmw9E=" }, - "available-typed-arrays": { - "version": "1.0.2", - "resolved": "https://registry.npmjs.org/available-typed-arrays/-/available-typed-arrays-1.0.2.tgz", - "integrity": "sha512-XWX3OX8Onv97LMk/ftVyBibpGwY5a8SmuxZPzeOxqmuEqUCOM9ZE+uIaD1VNJ5QnvU2UQusvmKbuM1FR8QWGfQ==", - "dev": true, - "requires": { - "array-filter": "^1.0.0" - } - }, "balanced-match": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.2.tgz", @@ -7386,12 +7262,6 @@ "integrity": "sha512-HWqDgT7ZEkqRzBvc2s64vSZ/hfOceEol3ac/7tKwzuvEyWx3/4UegXh5oBOIotkGsObyk3xznnSRVADBgWSQVg==", "dev": true }, - "foreach": { - "version": "2.0.5", - "resolved": "https://registry.npmjs.org/foreach/-/foreach-2.0.5.tgz", - "integrity": "sha1-C+4AUBiusmDQo6865ljdATbsG5k=", - "dev": true - }, "fs-extra": { "version": "8.1.0", "resolved": "https://registry.npmjs.org/fs-extra/-/fs-extra-8.1.0.tgz", @@ -7717,15 +7587,6 @@ "integrity": "sha512-Ju0Bz/cEia55xDwUWEa8+olFpCiQoypjnQySseKtmjNrnps3P+xfpUmGr90T7yjlVJmOtybRvPXhKMbHr+fWnw==", "dev": true }, - "is-arguments": { - "version": "1.1.0", - "resolved": "https://registry.npmjs.org/is-arguments/-/is-arguments-1.1.0.tgz", - "integrity": "sha512-1Ij4lOMPl/xB5kBDn7I+b2ttPMKa8szhEIrXDuXQD/oe3HJLTLhqhgGspwgyGd6MOywBUqVvYicF72lkgDnIHg==", - "dev": true, - "requires": { - "call-bind": "^1.0.0" - } - }, "is-arrayish": { "version": "0.2.1", "resolved": "https://registry.npmjs.org/is-arrayish/-/is-arrayish-0.2.1.tgz", @@ -7795,12 +7656,6 @@ "integrity": "sha1-o7MKXE8ZkYMWeqq5O+764937ZU8=", "dev": true }, - "is-generator-function": { - "version": "1.0.9", - "resolved": "https://registry.npmjs.org/is-generator-function/-/is-generator-function-1.0.9.tgz", - "integrity": "sha512-ZJ34p1uvIfptHCN7sFTjGibB9/oBg17sHqzDLfuwhvmN/qLVvIQXRQ8licZQ35WJ8KuEQt/etnnzQFI9C9Ue/A==", - "dev": true - }, "is-glob": { "version": "4.0.1", "resolved": "https://registry.npmjs.org/is-glob/-/is-glob-4.0.1.tgz", @@ -7874,19 +7729,6 @@ "has-symbols": "^1.0.2" } }, - "is-typed-array": { - "version": "1.1.5", - "resolved": "https://registry.npmjs.org/is-typed-array/-/is-typed-array-1.1.5.tgz", - "integrity": "sha512-S+GRDgJlR3PyEbsX/Fobd9cqpZBuvUS+8asRqYDMLCb2qMzt1oz5m5oxQCxOgUDxiWsOVNi4yaF+/uvdlHlYug==", - "dev": true, - "requires": { - "available-typed-arrays": "^1.0.2", - "call-bind": "^1.0.2", - "es-abstract": "^1.18.0-next.2", - "foreach": "^2.0.5", - "has-symbols": "^1.0.1" - } - }, "isarray": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/isarray/-/isarray-1.0.0.tgz", @@ -9627,20 +9469,6 @@ "punycode": "^2.1.0" } }, - "util": { - "version": "0.12.3", - "resolved": "https://registry.npmjs.org/util/-/util-0.12.3.tgz", - "integrity": "sha512-I8XkoQwE+fPQEhy9v012V+TSdH2kp9ts29i20TaaDUXsg7x/onePbhFJUExBfv/2ay1ZOp/Vsm3nDlmnFGSAog==", - "dev": true, - "requires": { - "inherits": "^2.0.3", - "is-arguments": "^1.0.4", - "is-generator-function": "^1.0.7", - "is-typed-array": "^1.1.3", - "safe-buffer": "^5.1.2", - "which-typed-array": "^1.1.2" - } - }, "utils-merge": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/utils-merge/-/utils-merge-1.0.1.tgz", @@ -9787,21 +9615,6 @@ "is-symbol": "^1.0.3" } }, - "which-typed-array": { - "version": "1.1.4", - "resolved": "https://registry.npmjs.org/which-typed-array/-/which-typed-array-1.1.4.tgz", - "integrity": "sha512-49E0SpUe90cjpoc7BOJwyPHRqSAd12c10Qm2amdEZrJPCY2NDxaW01zHITrem+rnETY3dwrbH3UUrUwagfCYDA==", - "dev": true, - "requires": { - "available-typed-arrays": "^1.0.2", - "call-bind": "^1.0.0", - "es-abstract": "^1.18.0-next.1", - "foreach": "^2.0.5", - "function-bind": "^1.1.1", - "has-symbols": "^1.0.1", - "is-typed-array": "^1.1.3" - } - }, "wide-align": { "version": "1.1.3", "resolved": "https://registry.npmjs.org/wide-align/-/wide-align-1.1.3.tgz", diff --git a/package.json b/package.json index 380def4..d3d4324 100755 --- a/package.json +++ b/package.json @@ -58,7 +58,6 @@ "standard": "^16.0.3", "terser-webpack-plugin": "^5.1.2", "timers-browserify": "^2.0.12", - "util": "^0.12.3", "webpack": "^5.37.0", "webpack-cli": "^4.7.0", "xvfb-maybe": "^0.2.1" diff --git a/webpack.config.js b/webpack.config.js index 60b8b30..847b6f6 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -20,7 +20,6 @@ module.exports = (env, argv) => { fallback: { fs: false, path: require.resolve('path-browserify'), - util: require.resolve('util/'), events: require.resolve('events/'), crypto: false } From 5ae801572f76f254276ee22f759e5111282ba976 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Rebours?= Date: Mon, 7 Jun 2021 11:55:10 +0200 Subject: [PATCH 2/2] 2.0.3-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 7567004..b8eceb7 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@seald-io/nedb", - "version": "2.0.2", + "version": "2.0.3-0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@seald-io/nedb", - "version": "2.0.2", + "version": "2.0.3-0", "license": "MIT", "dependencies": { "@seald-io/binary-search-tree": "^1.0.2", diff --git a/package.json b/package.json index d3d4324..3fb9790 100755 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@seald-io/nedb", - "version": "2.0.2", + "version": "2.0.3-0", "files": [ "lib/**/*.js", "browser-version/**/*.js",