提交 3ec69f74 编写于 作者: R rsercano

trying to fix #523

上级 8e9f400f
const { Binary, Long, MaxKey, MinKey, BSONRegExp, ObjectId, Timestamp, Code, Decimal128 } = require('bson');
const ExtendedJSON = function () {
};
const convertBsonDocumentToJson = function (doc) {
if (doc instanceof Binary || doc._bsontype === 'Binary') return { $binary: doc.buffer.toString('base64'), $type: Buffer.from([doc.sub_type]).toString('hex') };
if (doc instanceof Code || doc._bsontype === 'Code') {
const res = { $code: doc.code };
if (doc.scope) res.$scope = doc.scope;
return res;
} if (doc instanceof Date) return { $date: doc.toISOString() };
if (doc instanceof Long || doc._bsontype === 'Long') return { $numberLong: doc.toString() };
if (doc instanceof MaxKey || doc._bsontype === 'MaxKey') return { $maxKey: true };
if (doc instanceof MinKey || doc._bsontype === 'MinKey') return { $minKey: true };
if (doc instanceof ObjectId || doc._bsontype === 'ObjectID') return { $oid: doc.toString() };
if (doc instanceof BSONRegExp) return { $regex: doc.pattern, $options: doc.options };
if (doc instanceof Timestamp || doc._bsontype === 'Timestamp') return { $timestamp: { t: doc.high_, i: doc.low_ } };
if (doc instanceof Decimal128 || doc._bsontype === 'Decimal128') return { $numberDecimal: doc.toString() };
if (doc === undefined) return { $undefined: true };
};
const convertJsonDocumentToBson = function (doc) {
if (doc.$binary) return new Binary(Buffer.from(doc.$binary, 'base64'), Buffer.from(doc.$type, 'hex')[0]);
if (doc.$code) return new Code(doc.$code, doc.$scope);
if (doc.$date) {
if (typeof doc.$date === 'string') return new Date(doc.$date);
if (typeof doc.$date === 'object' && doc.$date.$numberLong) {
const date = new Date();
date.setTime(parseInt(doc.$date.$numberLong, 10));
return date;
}
} else if (doc.$numberLong) return Long.fromString(doc.$numberLong);
else if (doc.$maxKey) return new MaxKey();
else if (doc.$minKey) return new MinKey();
else if (doc.$oid) return new ObjectId(Buffer.from(doc.$oid, 'hex'));
else if (doc.$regex) return new BSONRegExp(doc.$regex, doc.$options || '');
else if (doc.$timestamp) return new Timestamp(doc.$timestamp.i, doc.$timestamp.t);
else if (doc.$numberDecimal) return Decimal128.fromString(doc.$numberDecimal);
else if (doc.$undefined) return undefined;
};
const isBsonDocConvertible = function (param) {
return param !== null && (typeof param === 'object')
&& Object.prototype.toString.call(param) !== '[object Array]' && convertBsonDocumentToJson(param);
};
const isJsonDocConvertible = function (param) {
return param !== null && Object.prototype.toString.call(param) === '[object Object]' && convertJsonDocumentToBson(param);
};
ExtendedJSON.prototype = {
convertBSONtoJSON(obj) {
if (!obj) return obj;
// there are some other objects such as Math, Date etc..
if (obj && (typeof obj === 'object') && Object.prototype.toString.call(obj) !== '[object Array]' && convertBsonDocumentToJson(obj)) {
return convertBsonDocumentToJson(obj);
}
Object.keys(obj).forEach((property) => {
if (obj[property]) {
if ((typeof obj[property] === 'object') && Object.prototype.toString.call(obj[property]) !== '[object Array]') {
if (convertBsonDocumentToJson(obj[property])) obj[property] = convertBsonDocumentToJson(obj[property]);
else obj[property] = this.convertBSONtoJSON(obj[property]);
} else if (Object.prototype.toString.call(obj[property]) === '[object Array]') {
for (let i = 0; i < obj[property].length; i += 1) {
if (isBsonDocConvertible(obj[property][i])) obj[property][i] = convertBsonDocumentToJson(obj[property][i]);
else obj[property][i] = this.convertBSONtoJSON(obj[property][i]);
}
}
}
});
return obj;
},
convertJSONtoBSON(obj) {
if (!obj) return obj;
if (obj && Object.prototype.toString.call(obj) === '[object Object]' && convertJsonDocumentToBson(obj)) {
return convertJsonDocumentToBson(obj);
}
Object.keys(obj).forEach((property) => {
if (obj[property]) {
if (Object.prototype.toString.call(obj[property]) === '[object Object]') {
if (convertJsonDocumentToBson(obj[property])) obj[property] = convertJsonDocumentToBson(obj[property]);
else obj[property] = this.convertJSONtoBSON(obj[property]);
} else if (Object.prototype.toString.call(obj[property]) === '[object Array]') {
for (let i = 0; i < obj[property].length; i += 1) {
if (isJsonDocConvertible(obj[property][i])) obj[property][i] = convertJsonDocumentToBson(obj[property][i]);
else obj[property][i] = this.convertJSONtoBSON(obj[property][i]);
}
}
}
});
return obj;
}
};
export default new ExtendedJSON();
/* global Async */
import { Logger, Error } from '/server/imports/modules';
import MongoDB from './index';
import ExtendedJSON from './extended_json';
const mongodbApi = require('mongodb');
const { EJSON } = require('bson');
const MongoDBGridFS = function () {
};
......@@ -38,7 +38,7 @@ MongoDBGridFS.prototype = {
const metadataToLog = { bucketName, selector, sessionId };
Logger.info({ message: 'delete-files', metadataToLog });
selector = ExtendedJSON.convertJSONtoBSON(selector);
selector = EJSON.deserialize(selector);
const result = Async.runSync((done) => {
try {
......@@ -76,7 +76,7 @@ MongoDBGridFS.prototype = {
}
});
return ExtendedJSON.convertBSONtoJSON(result);
return EJSON.serialize(result);
},
deleteFile({ bucketName, fileId, sessionId }) {
......@@ -96,7 +96,7 @@ MongoDBGridFS.prototype = {
}
});
return ExtendedJSON.convertBSONtoJSON(result);
return EJSON.serialize(result);
},
getFilesInfo({ bucketName, selector, limit, sessionId }) {
......@@ -104,7 +104,7 @@ MongoDBGridFS.prototype = {
selector = selector || {};
const metadataToLog = { bucketName, selector, limit, sessionId };
selector = ExtendedJSON.convertJSONtoBSON(selector);
selector = EJSON.deserialize(selector);
Logger.info({ message: 'get-files-info', metadataToLog });
......@@ -121,13 +121,13 @@ MongoDBGridFS.prototype = {
}
});
return ExtendedJSON.convertBSONtoJSON(result);
return EJSON.serialize(result);
},
uploadFile({ bucketName, blob, fileName, contentType, metaData, aliases, sessionId }) {
const metadataToLog = { bucketName, fileName, contentType, metaData, aliases, sessionId, blobLength: blob.length };
if (metaData) metaData = ExtendedJSON.convertJSONtoBSON(metaData);
if (metaData) metaData = EJSON.deserialize(metaData);
blob = Buffer.from(blob);
......@@ -167,7 +167,7 @@ MongoDBGridFS.prototype = {
}
});
return ExtendedJSON.convertBSONtoJSON(result);
return EJSON.serialize(result);
},
download({ req, res }) {
......
/* global Async */
import { Meteor } from 'meteor/meteor';
import { Logger, Database, Error } from '/server/imports/modules';
import ExtendedJSON from './extended_json';
const { EJSON } = require('bson');
const os = require('os');
const fs = require('fs');
......@@ -65,7 +65,7 @@ MongoDBHelper.prototype = {
},
proceedMapReduceExecution({ execution, map, reduce, options, metadataToLog }) {
options = ExtendedJSON.convertJSONtoBSON(options);
options = EJSON.deserialize(options);
const result = Async.runSync((done) => {
try {
......@@ -89,7 +89,7 @@ MongoDBHelper.prototype = {
}
});
return ExtendedJSON.convertBSONtoJSON(result);
return EJSON.serialize(result);
},
proceedExecutingQuery({ methodArray, execution, removeCollectionTopology, metadataToLog }) {
......@@ -98,7 +98,7 @@ MongoDBHelper.prototype = {
try {
for (let i = 0; i < methodArray.length; i += 1) {
const last = (i === (methodArray.length - 1));
const entry = ExtendedJSON.convertJSONtoBSON(methodArray[i]);
const entry = EJSON.deserialize(methodArray[i]);
execution = this.proceedExecutionStepByStep(entry, last, done, execution, metadataToLog);
}
......@@ -109,7 +109,7 @@ MongoDBHelper.prototype = {
if (removeCollectionTopology) this.removeCollectionTopologyFromResult(result);
this.removeConnectionTopologyFromResult(result);
result = ExtendedJSON.convertBSONtoJSON(result);
result = EJSON.serialize(result);
result.executionTime = new Date() - start;
return result;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册