realm
Version:
Realm by MongoDB is an offline-first mobile database: an alternative to SQLite and key-value stores
115 lines • 3.88 kB
JavaScript
;
////////////////////////////////////////////////////////////////////////////
//
// Copyright 2024 Realm Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
////////////////////////////////////////////////////////////////////////////
Object.defineProperty(exports, "__esModule", { value: true });
exports.applyPatch = void 0;
const fetch_1 = require("@realm/fetch");
/**
* Applies SDK level patches to the binding.
* This should only be called after the binding has been injected.
* @internal
*/
function applyPatch(binding) {
binding.IndexSet.prototype.asIndexes = function* () {
for (const [from, to] of this) {
let i = from;
while (i < to) {
yield i;
i++;
}
}
};
binding.Timestamp.fromDate = (d) => binding.Timestamp.make(binding.Int64.numToInt(Math.floor(d.valueOf() / 1000)), (d.valueOf() % 1000) * 1000000);
binding.Timestamp.prototype.toDate = function () {
return new Date(Number(this.seconds) * 1000 + this.nanoseconds / 1000000);
};
binding.SyncSession.prototype.weaken = function () {
try {
return binding.WeakSyncSession.weakCopyOf(this);
}
finally {
this.$resetSharedPtr();
}
};
binding.WeakSyncSession.prototype.withDeref = function (callback) {
const shared = this.rawDereference();
try {
return callback(shared);
}
finally {
shared?.$resetSharedPtr();
}
};
binding.InvalidObjKey = class InvalidObjKey extends TypeError {
constructor(input) {
super(`Cannot convert '${input}' to an ObjKey`);
}
};
binding.stringToObjKey = (input) => {
try {
return binding.Int64.strToInt(input);
}
catch {
throw new binding.InvalidObjKey(input);
}
};
binding.isEmptyObjKey = (objKey) => {
// This relies on the JS representation of an ObjKey being a bigint
return binding.Int64.equals(objKey, -1);
};
function fromBindingFetchBody(body) {
if (body.length === 0) {
return undefined;
}
else {
return body;
}
}
const HTTP_METHOD = {
[0 /* binding.HttpMethod.Get */]: "GET",
[1 /* binding.HttpMethod.Post */]: "POST",
[3 /* binding.HttpMethod.Put */]: "PUT",
[2 /* binding.HttpMethod.Patch */]: "PATCH",
[4 /* binding.HttpMethod.Del */]: "DELETE",
};
function fromBindingFetchMethod(method) {
if (method in HTTP_METHOD) {
return HTTP_METHOD[method];
}
else {
throw new Error(`Unexpected method ${method}`);
}
}
function fromBindingTimeoutSignal(timeoutMs) {
const timeout = Number(timeoutMs);
return timeout > 0 ? fetch_1.AbortSignal.timeout(timeout) : undefined;
}
binding.toFetchArgs = ({ url, method, timeoutMs, body, headers }) => {
return [
url,
{
body: fromBindingFetchBody(body),
method: fromBindingFetchMethod(method),
signal: fromBindingTimeoutSignal(timeoutMs),
headers,
},
];
};
}
exports.applyPatch = applyPatch;
//# sourceMappingURL=binding-patch.js.map