Skip to content

Commit

Permalink
Fix autoEnsure, fix blatant SQLi stupidity because I'm a failable cun…
Browse files Browse the repository at this point in the history
…t, sometimes
  • Loading branch information
eslachance committed Aug 28, 2020
1 parent 14f0913 commit 198d768
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 18 deletions.
3 changes: 0 additions & 3 deletions index.js

This file was deleted.

5 changes: 3 additions & 2 deletions package.json
Expand Up @@ -2,7 +2,7 @@
"name": "enmap",
"version": "5.7.0",
"description": "A simple database wrapper to make sqlite database interactions much easier for beginners, with additional array helper methods.",
"types": "index.d.ts",
"main": "src/index.js",
"scripts": {
"test": "jest",
"docs": "node ./docs/build.js"
Expand Down Expand Up @@ -47,5 +47,6 @@
"jest": "^26.1.0",
"jsdoc-to-markdown": "^6.0.1",
"limax": "^2.1.0"
}
},
"types": "./typings/index.d.ts"
}
20 changes: 7 additions & 13 deletions src/index.js
Expand Up @@ -193,7 +193,7 @@ class Enmap extends Map {
*/
set(key, val, path = null) {
if (isNil(key) || key.constructor.name !== 'String') {
throw new Err('Enmap require keys to be strings.', 'EnmapKeyTypeError');
throw new Err(`Enmap requires keys to be a string. Provided: ${isNil(key) ? 'nil' : key.constructor.name}`, 'EnmapKeyTypeError');
}
key = key.toString();
let data = this.get(key);
Expand Down Expand Up @@ -273,7 +273,10 @@ class Enmap extends Map {
if (isNil(key)) return null;
this[_fetchCheck](key);
key = key.toString();
const data = this.autoEnsure !== this.off ? this.ensure(key, this.autoEnsure) : super.get(key);
if(this.autoEnsure !== this.off && !this.has(key)) {
this.set(key, cloneDeep(this.autoEnsure));
}
const data = super.get(key);
if (!isNil(path)) {
this[_check](key, ['Object', 'Array']);
return _get(data, path);
Expand Down Expand Up @@ -316,15 +319,6 @@ class Enmap extends Map {
return rows.map(row => row.key);
}

/**
* Migrates an Enmap from version 3 or lower to a Version 4 enmap, which is locked to sqlite backend only.
* This migration MUST be executed in version 3.1.4 of Enmap, along with appropriate providers.
* See https://enmap.evie.codes/install/upgrade for more details.
*/
static async migrate() {
throw new Err('PLEASE DOWNGRADE TO ENMAP@3.1.4 TO USE THE MIGRATE TOOL', 'EnmapMigrationError');
}

/**
* Fetches every key from the persistent enmap and loads them into the current enmap value.
* @return {Enmap} The enmap containing all values.
Expand Down Expand Up @@ -387,7 +381,7 @@ class Enmap extends Map {
let { lastnum } = this.db.prepare("SELECT lastnum FROM 'internal::autonum' WHERE enmap = ?").get(this.name);
lastnum++;
this.db.prepare("INSERT OR REPLACE INTO 'internal::autonum' (enmap, lastnum) VALUES (?, ?)").run(this.name, lastnum);
return lastnum;
return lastnum.toString();
}

/**
Expand Down Expand Up @@ -656,7 +650,7 @@ class Enmap extends Map {
if (this.polling) {
this.db.prepare(`INSERT INTO 'internal::changes::${this.name}' (type, key, timestamp, pid) VALUES (?, ?, ?, ?);`).run('delete', key.toString(), Date.now(), process.pid);
}
this.db.prepare(`DELETE FROM ${this.name} WHERE key = '${key}'`).run();
this.db.prepare(`DELETE FROM ${this.name} WHERE key = ?`).run(key);
return this;
}
if (typeof this.changedCB === 'function') {
Expand Down
0 index.d.ts → typings/index.d.ts 100755 → 100644
File renamed without changes.

0 comments on commit 198d768

Please sign in to comment.