Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
DB.idIsValid = function (id) {
var binary;
var base64Str;
if (!id) {
throw new exception_1.InvalidSyncIdException();
}
try {
binary = new mongodb.Binary(Buffer.from(id, 'hex'), 4);
base64Str = binary.buffer.toString('base64');
}
catch (err) {
throw new exception_1.InvalidSyncIdException();
}
if (!binary || !base64Str) {
throw new exception_1.InvalidSyncIdException();
}
};
// Closes the database connection
appRouter.param('document', function (req, res, next, id) {
if (id === 'undefined' || id === undefined) {
req.session.error = 'Document lacks an _id!';
return res.redirect(res.locals.baseHref + 'db/' + req.dbName + '/' + req.collectionName);
}
id = JSON.parse(decodeURIComponent(id));
let _id;
try {
// Case 1 : Object ID
_id = new mongodb.ObjectID.createFromHexString(id);
} catch (err) {
// Case 2 : BinaryID (only subtype 3 and 4)
if (('subtype' in req.query) && [3, 4].indexOf(req.query.subtype)) {
_id = new mongodb.Binary(Buffer.from(id, 'base64'), req.query.subtype);
} else {
// Case 3 : Try as raw ID
_id = id;
}
}
let findStraighId = function (id) {
// No document found with obj_id, try again with straight id
req.collection.findOne({ _id: id }, function (err, doc) {
if (err) {
req.session.error = 'Error: ' + err;
return res.redirect(res.locals.baseHref + 'db/' + req.dbName + '/' + req.collectionName);
}
if (doc === null) {
req.session.error = 'Document not found!';
it('returns a buffer for the bson Binary data', () => {
var mapping = new BufferMapping();
var testValue = "test";
var result = mapping.read(new ReadContext(null), new Binary(new Buffer(testValue))).toString("utf8");
assert.equal(result, testValue);
});
});
module.exports = function (obj) {
var key, val
for (key in obj) {
if (!obj.hasOwnProperty(key)) continue
val = obj[key]
switch (key) {
case '$binary':
case '$type':
return new mongo.Binary(obj.$binary, obj.$type)
case '$date':
return new Date(val)
case '$decimal128':
return new mongo.Decimal128(Buffer.from(val))
case '$timestamp':
return new mongo.Timestamp(val.t, val.i)
case '$regex':
case '$options':
return new RegExp(obj.$regex, obj.$options)
case '$oid':
return new mongo.ObjectID(val)
case '$ref':
case '$id':
case '$db':
var id = obj.$id._bsontype ? obj.$id : mongo.ObjectID(obj.$id.$oid)
return new mongo.DBRef(obj.$ref, id, obj.$db)
from(uuid) {
if (typeof uuid === 'string') {
return generateUUID(uuid);
} else if (
(uuid instanceof Binary || uuid._bsontype === 'Binary') &&
uuid.sub_type === Binary.SUBTYPE_UUID
) {
if (uuid.length() !== 16) throw Err.InvalidUUID;
return apply(
new Binary(uuid.read(0, uuid.length()), Binary.SUBTYPE_UUID)
);
} else {
throw Err.UnexpectedUUIDType;
}
},
};
function generateUUID(uuid, v = 1) {
if (v <= 0 || v > 4 || v === 2 || v === 3) throw Err.UnsupportedUUIDVersion;
let uuidv = uuidv1;
if (v === 4) uuidv = uuidv4;
if (uuid) {
if (!validateUuid(uuid)) throw Err.InvalidUUID;
const normalized = normalize(uuid);
if (!normalized) throw Err.InvalidHexString;
return apply(
new Binary(Buffer.from(normalized, 'hex'), Binary.SUBTYPE_UUID)
);
} else {
const uuid = uuidv(null, Buffer.alloc(16));
return apply(Binary(uuid, Binary.SUBTYPE_UUID));
}
}
var createProduct = function(i, callback) {
new Product(cols
, i
, f('product_%s', i)
, {bin: new Binary(new Buffer(sizeOfProductsInBytes))}).create(function(err, p) {
if(err) return callback(err);
new Inventory(cols, i, quantityPrItem).create(function(err, i) {
if(err) return callback(err);
callback();
});
});
}
JSONToDatabase(json) {
return new mongodb.Binary(new Buffer(json.base64, 'base64'));
},
var createProduct = function(i, callback) {
new Product(cols
, i
, f('product_%s', i)
, {bin: new Binary(new Buffer(sizeOfProductsInBytes))}).create(function(err, p) {
if(err) return callback(err);
new Inventory(cols, i, quantityPrItem).create(function(err, i) {
if(err) return callback(err);
callback();
});
});
}
.png()
.toBuffer();
} else {
img = await sharp({
create: {
width: 600,
height: 600,
channels: 3,
background: { r, g, b }
}
})
.jpeg()
.toBuffer();
}
return Binary(img);
}