Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
private incrementIncomingBytes_(args: unknown) {
// TODO: This is an annoying perf hit just to track the number of incoming bytes. Maybe it should be opt-in.
const bytesReceived = stringify(args).length;
this.bytesReceived += bytesReceived;
this.stats_.incrementCounter('bytes_received', bytesReceived);
}
}
private onDataMessage_(message: { [k: string]: any }) {
if ('r' in message) {
// this is a response
this.log_('from server: ' + stringify(message));
const reqNum = message['r'];
const onResponse = this.requestCBHash_[reqNum];
if (onResponse) {
delete this.requestCBHash_[reqNum];
onResponse(message[/*body*/ 'b']);
}
} else if ('error' in message) {
throw 'A server-side error has occurred: ' + message['error'];
} else if ('a' in message) {
// a and b are action and body, respectively
this.onDataPush_(message['a'], message['b']);
}
}
authTokenProvider
);
// Minor hack: Fire onConnect immediately, since there's no actual connection.
setTimeout(this.onConnectStatus_.bind(this, true), 0);
} else {
const authOverride = app.options['databaseAuthVariableOverride'];
// Validate authOverride
if (typeof authOverride !== 'undefined' && authOverride !== null) {
if (typeof authOverride !== 'object') {
throw new Error(
'Only objects are supported for option databaseAuthVariableOverride'
);
}
try {
stringify(authOverride);
} catch (e) {
throw new Error('Invalid authOverride provided: ' + e);
}
}
this.persistentConnection_ = new PersistentConnection(
this.repoInfo_,
this.onDataUpdate_.bind(this),
this.onConnectStatus_.bind(this),
this.onServerInfoUpdate_.bind(this),
authTokenProvider,
authOverride
);
this.server_ = this.persistentConnection_;
}
export const ObjectToUniqueKey = function(obj: unknown): string {
if (typeof obj !== 'object' || obj === null) {
return stringify(obj);
}
const keys = [];
// eslint-disable-next-line guard-for-in
for (const k in obj) {
keys.push(k);
}
// Export as json, but with the keys sorted.
keys.sort();
let key = '{';
for (let i = 0; i < keys.length; i++) {
if (i !== 0) {
key += ',';
}
key += stringify(keys[i]);
protected sendRequest(
action: string,
body: any,
onResponse?: (a: any) => void
) {
const curReqNum = ++this.requestNumber_;
const msg = { r: curReqNum, a: action, b: body };
this.log_(stringify(msg));
assert(
this.connected_,
"sendRequest call when we're not connected not allowed."
);
this.realtime_.sendRequest(msg);
if (onResponse) {
this.requestCBHash_[curReqNum] = onResponse;
}
}
orderBy = REST_CONSTANTS.KEY_INDEX;
} else {
assert(this.index_ instanceof PathIndex, 'Unrecognized index type!');
orderBy = this.index_.toString();
}
qs[REST_CONSTANTS.ORDER_BY] = stringify(orderBy);
if (this.startSet_) {
qs[REST_CONSTANTS.START_AT] = stringify(this.indexStartValue_);
if (this.startNameSet_) {
qs[REST_CONSTANTS.START_AT] += ',' + stringify(this.indexStartName_);
}
}
if (this.endSet_) {
qs[REST_CONSTANTS.END_AT] = stringify(this.indexEndValue_);
if (this.endNameSet_) {
qs[REST_CONSTANTS.END_AT] += ',' + stringify(this.indexEndName_);
}
}
if (this.limitSet_) {
if (this.isViewFromLeft()) {
qs[REST_CONSTANTS.LIMIT_TO_FIRST] = this.limit_;
} else {
qs[REST_CONSTANTS.LIMIT_TO_LAST] = this.limit_;
}
}
return qs;
}
}
toString(): string {
return (
this.getPath().toString() +
':' +
this.eventType +
':' +
stringify(this.snapshot.exportVal())
);
}
}
set(key: string, value: unknown | null) {
if (value == null) {
this.domStorage_.removeItem(this.prefixedName_(key));
} else {
this.domStorage_.setItem(this.prefixedName_(key), stringify(value));
}
}
protected sendRequest(
action: string,
body: unknown,
onResponse?: (a: unknown) => void
) {
const curReqNum = ++this.requestNumber_;
const msg = { r: curReqNum, a: action, b: body };
this.log_(stringify(msg));
assert(
this.connected_,
"sendRequest call when we're not connected not allowed."
);
this.realtime_.sendRequest(msg);
if (onResponse) {
this.requestCBHash_[curReqNum] = onResponse;
}
}