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

View File

@ -43,7 +43,7 @@ export function validateEvent<T>(event: T): event is T & UnsignedEvent {
let tag = event.tags[i] let tag = event.tags[i]
if (!Array.isArray(tag)) return false if (!Array.isArray(tag)) return false
for (let j = 0; j < tag.length; j++) { 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 const utf8Encoder: TextEncoder = new TextEncoder()
export function normalizeURL(url: string): string { export function normalizeURL(url: string): string {
if (url.indexOf('://') === -1) url = 'wss://' + url try {
let p = new URL(url) if (url.indexOf('://') === -1) url = 'wss://' + url
p.pathname = p.pathname.replace(/\/+/g, '/') let p = new URL(url)
if (p.pathname.endsWith('/')) p.pathname = p.pathname.slice(0, -1) p.pathname = p.pathname.replace(/\/+/g, '/')
if ((p.port === '80' && p.protocol === 'ws:') || (p.port === '443' && p.protocol === 'wss:')) p.port = '' if (p.pathname.endsWith('/')) p.pathname = p.pathname.slice(0, -1)
p.searchParams.sort() if ((p.port === '80' && p.protocol === 'ws:') || (p.port === '443' && p.protocol === 'wss:')) p.port = ''
p.hash = '' p.searchParams.sort()
return p.toString() p.hash = ''
return p.toString()
} catch (e) {
throw new Error(`Invalid URL: ${url}`)
}
} }
export function insertEventIntoDescendingList(sortedArray: Event[], event: Event): Event[] { export function insertEventIntoDescendingList(sortedArray: Event[], event: Event): Event[] {
@ -111,6 +115,9 @@ export class Queue<V> {
const target = this.first const target = this.first
this.first = target.next this.first = target.next
if (this.first) {
this.first.prev = null // fix: clean up prev pointer
}
return target.value return target.value
} }