146 lines
3.9 KiB
JavaScript
146 lines
3.9 KiB
JavaScript
"use strict";
|
|
var __defProp = Object.defineProperty;
|
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
var __export = (target, all) => {
|
|
for (var name in all)
|
|
__defProp(target, name, { get: all[name], enumerable: true });
|
|
};
|
|
var __copyProps = (to, from, except, desc) => {
|
|
if (from && typeof from === "object" || typeof from === "function") {
|
|
for (let key of __getOwnPropNames(from))
|
|
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
}
|
|
return to;
|
|
};
|
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
|
|
// utils.ts
|
|
var utils_exports = {};
|
|
__export(utils_exports, {
|
|
Queue: () => Queue,
|
|
QueueNode: () => QueueNode,
|
|
binarySearch: () => binarySearch,
|
|
bytesToHex: () => import_utils.bytesToHex,
|
|
hexToBytes: () => import_utils.hexToBytes,
|
|
insertEventIntoAscendingList: () => insertEventIntoAscendingList,
|
|
insertEventIntoDescendingList: () => insertEventIntoDescendingList,
|
|
normalizeURL: () => normalizeURL,
|
|
utf8Decoder: () => utf8Decoder,
|
|
utf8Encoder: () => utf8Encoder
|
|
});
|
|
module.exports = __toCommonJS(utils_exports);
|
|
var import_utils = require("@noble/hashes/utils");
|
|
var utf8Decoder = new TextDecoder("utf-8");
|
|
var utf8Encoder = new TextEncoder();
|
|
function normalizeURL(url) {
|
|
try {
|
|
if (url.indexOf("://") === -1)
|
|
url = "wss://" + url;
|
|
let p = new URL(url);
|
|
p.pathname = p.pathname.replace(/\/+/g, "/");
|
|
if (p.pathname.endsWith("/"))
|
|
p.pathname = p.pathname.slice(0, -1);
|
|
if (p.port === "80" && p.protocol === "ws:" || p.port === "443" && p.protocol === "wss:")
|
|
p.port = "";
|
|
p.searchParams.sort();
|
|
p.hash = "";
|
|
return p.toString();
|
|
} catch (e) {
|
|
throw new Error(`Invalid URL: ${url}`);
|
|
}
|
|
}
|
|
function insertEventIntoDescendingList(sortedArray, event) {
|
|
const [idx, found] = binarySearch(sortedArray, (b) => {
|
|
if (event.id === b.id)
|
|
return 0;
|
|
if (event.created_at === b.created_at)
|
|
return -1;
|
|
return b.created_at - event.created_at;
|
|
});
|
|
if (!found) {
|
|
sortedArray.splice(idx, 0, event);
|
|
}
|
|
return sortedArray;
|
|
}
|
|
function insertEventIntoAscendingList(sortedArray, event) {
|
|
const [idx, found] = binarySearch(sortedArray, (b) => {
|
|
if (event.id === b.id)
|
|
return 0;
|
|
if (event.created_at === b.created_at)
|
|
return -1;
|
|
return event.created_at - b.created_at;
|
|
});
|
|
if (!found) {
|
|
sortedArray.splice(idx, 0, event);
|
|
}
|
|
return sortedArray;
|
|
}
|
|
function binarySearch(arr, compare) {
|
|
let start = 0;
|
|
let end = arr.length - 1;
|
|
while (start <= end) {
|
|
const mid = Math.floor((start + end) / 2);
|
|
const cmp = compare(arr[mid]);
|
|
if (cmp === 0) {
|
|
return [mid, true];
|
|
}
|
|
if (cmp < 0) {
|
|
end = mid - 1;
|
|
} else {
|
|
start = mid + 1;
|
|
}
|
|
}
|
|
return [start, false];
|
|
}
|
|
var QueueNode = class {
|
|
value;
|
|
next = null;
|
|
prev = null;
|
|
constructor(message) {
|
|
this.value = message;
|
|
}
|
|
};
|
|
var Queue = class {
|
|
first;
|
|
last;
|
|
constructor() {
|
|
this.first = null;
|
|
this.last = null;
|
|
}
|
|
enqueue(value) {
|
|
const newNode = new QueueNode(value);
|
|
if (!this.last) {
|
|
this.first = newNode;
|
|
this.last = newNode;
|
|
} else if (this.last === this.first) {
|
|
this.last = newNode;
|
|
this.last.prev = this.first;
|
|
this.first.next = newNode;
|
|
} else {
|
|
newNode.prev = this.last;
|
|
this.last.next = newNode;
|
|
this.last = newNode;
|
|
}
|
|
return true;
|
|
}
|
|
dequeue() {
|
|
if (!this.first)
|
|
return null;
|
|
if (this.first === this.last) {
|
|
const target2 = this.first;
|
|
this.first = null;
|
|
this.last = null;
|
|
return target2.value;
|
|
}
|
|
const target = this.first;
|
|
this.first = target.next;
|
|
if (this.first) {
|
|
this.first.prev = null;
|
|
}
|
|
return target.value;
|
|
}
|
|
};
|