Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function testEJSON(t, patch, expected, recursive = true) {
const string_djson = DJSON.stringify(patch)
const string_ejson = EJSON.stringify(patch)
const parsed_json_djson = JSON.parse(string_djson)
const parsed_json_ejson = JSON.parse(string_ejson)
t.deepEqual(parsed_json_djson, expected)
t.deepEqual(parsed_json_djson, parsed_json_ejson)
if (recursive) {
testEJSON(t, DJSON.parse(string_djson), expected, false)
// testEJSON(t, EJSON.parse(string_ejson), expected, false)
}
}
this.connection.onmessage = (e) => {
try {
// console.log('received', e.data, e.target.readyState);
const data = EJSON.parse(e.data);
this.emit(data.msg, data);
return data.collection && this.emit(data.collection, data);
} catch (err) {
log('EJSON parse', err);
}
};
});
var stringForErrorMessage = function (value, options) {
options = options || {};
if ( value === null ) return "null";
if ( options.onlyShowType ) {
return typeof value;
}
// Your average non-object things. Saves from doing the try/catch below for.
if ( typeof value !== "object" ) {
return EJSON.stringify(value)
}
try {
// Find objects with circular references since EJSON doesn't support them yet (Issue #4778 + Unaccepted PR)
// If the native stringify is going to choke, EJSON.stringify is going to choke too.
JSON.stringify(value);
} catch (stringifyError) {
if ( stringifyError.name === "TypeError" ) {
return typeof value;
}
}
return EJSON.stringify(value);
};
Device.prototype.parseMessage = function(message) {
// Attempt to parse the message.
try { message = EJSON.parse(message.toString()); }
catch(e) { return null; }
// The message has to be something.
if(!message) {
return null;
}
return message;
};
if (!existing.ready) existing.readyCallback = callbacks.onReady;
}
if (callbacks.onStop) {
existing.stopCallback = callbacks.onStop;
}
} else {
// New sub! Generate an id, save it locally, and send message.
id = Random.id();
const subIdRemember = Data.ddp.sub(name, params);
Data.subscriptions[id] = {
id: id,
subIdRemember: subIdRemember,
name: name,
params: EJSON.clone(params),
inactive: false,
ready: false,
readyDeps: new Trackr.Dependency(),
readyCallback: callbacks.onReady,
stopCallback: callbacks.onStop,
stop: function() {
Data.ddp.unsub(this.subIdRemember);
delete Data.subscriptions[this.id];
this.ready && this.readyDeps.changed();
if (callbacks.onStop) {
callbacks.onStop();
}
},
};
}
// collection has no _id.
throw new Error('can only transform documents with _id');
}
var id = doc._id;
// XXX consider making tracker a weak dependency and checking Package.tracker here
var transformed = Tracker.nonreactive(function() {
return transform(doc);
});
if (!isPlainObject(transformed)) {
throw new Error('transform must return object');
}
if (_.has(transformed, '_id')) {
if (!EJSON.equals(transformed._id, id)) {
throw new Error("transformed document can't have different _id");
}
} else {
transformed._id = id;
}
return transformed;
};
wrapped.__wrappedTransform__ = true;
// Meteor.subscribe("foo", Session.get("foo"));
// Meteor.subscribe("bar", Session.get("bar"));
// });
//
// If "foo" has changed but "bar" has not, we will match the "bar"
// subcribe to an existing inactive subscription in order to not
// unsub and resub the subscription unnecessarily.
//
// We only look for one such sub; if there are N apparently-identical subs
// being invalidated, we will require N matching subscribe calls to keep
// them all active.
let existing = false;
for (var i in Data.subscriptions) {
const sub = Data.subscriptions[i];
if (sub.inactive && sub.name === name && EJSON.equals(sub.params, params))
existing = sub;
}
let id;
if (existing) {
id = existing.id;
existing.inactive = false;
if (callbacks.onReady) {
// If the sub is not already ready, replace any ready callback with the
// one provided now. (It's not really clear what users would expect for
// an onReady callback inside an autorun; the semantics we provide is
// that at the time the sub first becomes ready, we call the last
// onReady callback provided, if any.)
if (!existing.ready) existing.readyCallback = callbacks.onReady;
}
const handleMessageReceived = protectedFunction((ddpMessage) => {
const message = buildMessage(EJSON.fromJSONValue(ddpMessage.fields.args[0]));
if (rid !== message.rid) {
return;
}
requestAnimationFrame(() => {
try {
database.write(() => {
database.create('messages', message, true);
// if it's a thread "header"
if (message.tlm) {
database.create('threads', message, true);
} else if (message.tmid) {
message.rid = message.tmid;
database.create('threadMessages', message, true);
}
});
MongoID.ObjectID.prototype.typeName = function() {
return "oid";
};
MongoID.ObjectID.prototype.getTimestamp = function() {
var self = this;
return parseInt(self._str.substr(0, 8), 16);
};
MongoID.ObjectID.prototype.valueOf =
MongoID.ObjectID.prototype.toJSONValue =
MongoID.ObjectID.prototype.toHexString =
function () { return this._str; };
EJSON.addType("oid", function (str) {
return new MongoID.ObjectID(str);
});
MongoID.idStringify = function (id) {
if (id instanceof MongoID.ObjectID) {
return id.valueOf();
} else if (typeof id === 'string') {
if (id === "") {
return id;
} else if (id.substr(0, 1) === "-" || // escape previously dashed strings
id.substr(0, 1) === "~" || // escape escaped numbers, true, false
MongoID._looksLikeObjectID(id) || // escape object-id-form strings
id.substr(0, 1) === '{') { // escape object-form strings, for maybe implementing later
return "-" + id;
} else {
return id; // other strings go through unchanged.
};
MongoID.ObjectID.prototype.typeName = function() {
return 'oid';
};
MongoID.ObjectID.prototype.getTimestamp = function() {
var self = this;
return parseInt(self._str.substr(0, 8), 16);
};
MongoID.ObjectID.prototype.valueOf = MongoID.ObjectID.prototype.toJSONValue = MongoID.ObjectID.prototype.toHexString = function() {
return this._str;
};
EJSON.addType('oid', function(str) {
return new MongoID.ObjectID(str);
});
MongoID.idStringify = function(id) {
if (id instanceof MongoID.ObjectID) {
return id.valueOf();
} else if (typeof id === 'string') {
if (id === '') {
return id;
} else if (
id.substr(0, 1) === '-' || // escape previously dashed strings
id.substr(0, 1) === '~' || // escape escaped numbers, true, false
MongoID._looksLikeObjectID(id) || // escape object-id-form strings
id.substr(0, 1) === '{'
) {
// escape object-form strings, for maybe implementing later