some small bugs codebuff found.

This commit is contained in:
fiatjaf 2025-04-03 23:31:34 -03:00
parent ba26b92973
commit dd603e47d8
3 changed files with 18 additions and 9 deletions

View File

@ -90,6 +90,7 @@ export class AbstractRelay {
try {
this.ws = new this._WebSocket(this.url)
} catch (err) {
clearTimeout(this.connectionTimeoutHandle)
reject(err)
return
}
@ -101,6 +102,7 @@ export class AbstractRelay {
}
this.ws.onerror = ev => {
clearTimeout(this.connectionTimeoutHandle)
reject((ev as any).message || 'websocket error')
if (this._connected) {
this._connected = false

View File

@ -43,7 +43,7 @@ export function validateEvent<T>(event: T): event is T & UnsignedEvent {
let tag = event.tags[i]
if (!Array.isArray(tag)) return false
for (let j = 0; j < tag.length; j++) {
if (typeof tag[j] === 'object') return false
if (typeof tag[j] !== 'string') return false
}
}

View File

@ -4,14 +4,18 @@ export const utf8Decoder: TextDecoder = new TextDecoder('utf-8')
export const utf8Encoder: TextEncoder = new TextEncoder()
export function normalizeURL(url: string): string {
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()
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}`)
}
}
export function insertEventIntoDescendingList(sortedArray: Event[], event: Event): Event[] {
@ -111,6 +115,9 @@ export class Queue<V> {
const target = this.first
this.first = target.next
if (this.first) {
this.first.prev = null // fix: clean up prev pointer
}
return target.value
}