Skip to content

Commit

Permalink
Merge branch 'master' into more-careful-rev-checks-3
Browse files Browse the repository at this point in the history
  • Loading branch information
alxndrsn authored Apr 15, 2024
2 parents bfc7060 + 20a5ccc commit 6fbdcff
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 20 deletions.
10 changes: 7 additions & 3 deletions packages/node_modules/pouchdb-core/src/adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,10 @@ function attachmentNameError(name) {
return false;
}

function isNotSingleDoc(doc) {
return doc === null || typeof doc !== 'object' || Array.isArray(doc);
}

function isValidRev(rev) {
return typeof rev === 'string' && /^\d+-/.test(rev);
}
Expand All @@ -201,7 +205,7 @@ class AbstractPouchDB extends EventEmitter {
callback = opts;
opts = {};
}
if (typeof doc !== 'object' || Array.isArray(doc)) {
if (isNotSingleDoc(doc)) {
return callback(createError(NOT_AN_OBJECT));
}
this.bulkDocs({docs: [doc]}, opts, yankError(callback, doc._id));
Expand All @@ -212,7 +216,7 @@ class AbstractPouchDB extends EventEmitter {
cb = opts;
opts = {};
}
if (typeof doc !== 'object' || Array.isArray(doc)) {
if (isNotSingleDoc(doc)) {
return cb(createError(NOT_AN_OBJECT));
}
invalidIdError(doc._id);
Expand Down Expand Up @@ -786,7 +790,7 @@ class AbstractPouchDB extends EventEmitter {

for (var i = 0; i < req.docs.length; ++i) {
const doc = req.docs[i];
if (typeof doc !== 'object' || Array.isArray(doc)) {
if (isNotSingleDoc(doc)) {
return callback(createError(NOT_AN_OBJECT));
}
if ('_rev' in doc && !isValidRev(doc._rev)) {
Expand Down
50 changes: 33 additions & 17 deletions tests/integration/test.basics.js
Original file line number Diff line number Diff line change
Expand Up @@ -859,23 +859,39 @@ adapters.forEach(function (adapter) {
});
});

it('Error when document is not an object', function (done) {
var db = new PouchDB(dbs.name);
var doc1 = [{ _id: 'foo' }, { _id: 'bar' }];
var doc2 = 'this is not an object';
var count = 5;
var callback = function (err) {
should.exist(err);
count--;
if (count === 0) {
done();
}
};
db.post(doc1, callback);
db.post(doc2, callback);
db.put(doc1, callback);
db.put(doc2, callback);
db.bulkDocs({docs: [doc1, doc2]}, callback);
[
undefined,
null,
[],
[{ _id: 'foo' }, { _id: 'bar' }],
'this is not an object',
String('this is not an object'),
//new String('this is not an object'), actually, this _is_ an object
].forEach((badDoc, idx) => {
describe(`Should error when document is not an object #${idx}`, () => {
let db;

const expectNotAnObject = fn => async () => {
let threw;
try {
await fn();
} catch (err) {
threw = true;
err.message.should.equal('Document must be a JSON object');
}
if (!threw) {
throw new Error('should have thrown');
}
};

beforeEach(() => {
db = new PouchDB(dbs.name);
});

it('should error for .post()', expectNotAnObject(() => db.post(badDoc)));
it('should error for .put()', expectNotAnObject(() => db.put(badDoc)));
it('should error for .bulkDocs()', expectNotAnObject(() => db.bulkDocs({docs: [badDoc]})));
});
});

it('Test instance update_seq updates correctly', function (done) {
Expand Down

0 comments on commit 6fbdcff

Please sign in to comment.