mirror of
https://github.com/nbd-wtf/nostr-tools.git
synced 2025-12-08 16:28:49 +00:00
Compare commits
2 Commits
34a1d8db47
...
e959409c14
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e959409c14 | ||
|
|
8a76c4e329 |
2
jsr.json
2
jsr.json
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@nostr/tools",
|
"name": "@nostr/tools",
|
||||||
"version": "2.18.1",
|
"version": "2.18.2",
|
||||||
"exports": {
|
"exports": {
|
||||||
".": "./index.ts",
|
".": "./index.ts",
|
||||||
"./core": "./core.ts",
|
"./core": "./core.ts",
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ test('kind classification', () => {
|
|||||||
expect(classifyKind(30000)).toBe('parameterized')
|
expect(classifyKind(30000)).toBe('parameterized')
|
||||||
expect(classifyKind(39999)).toBe('parameterized')
|
expect(classifyKind(39999)).toBe('parameterized')
|
||||||
expect(classifyKind(40000)).toBe('unknown')
|
expect(classifyKind(40000)).toBe('unknown')
|
||||||
expect(classifyKind(255)).toBe('unknown')
|
expect(classifyKind(255)).toBe('regular')
|
||||||
})
|
})
|
||||||
|
|
||||||
test('kind type guard', () => {
|
test('kind type guard', () => {
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"name": "nostr-tools",
|
"name": "nostr-tools",
|
||||||
"version": "2.18.1",
|
"version": "2.18.2",
|
||||||
"description": "Tools for making a Nostr client.",
|
"description": "Tools for making a Nostr client.",
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
|
|||||||
@@ -1,6 +1,12 @@
|
|||||||
import { describe, test, expect } from 'bun:test'
|
import { describe, test, expect } from 'bun:test'
|
||||||
import { buildEvent } from './test-helpers.ts'
|
import { buildEvent } from './test-helpers.ts'
|
||||||
import { Queue, insertEventIntoAscendingList, insertEventIntoDescendingList, binarySearch } from './utils.ts'
|
import {
|
||||||
|
Queue,
|
||||||
|
insertEventIntoAscendingList,
|
||||||
|
insertEventIntoDescendingList,
|
||||||
|
binarySearch,
|
||||||
|
normalizeURL,
|
||||||
|
} from './utils.ts'
|
||||||
|
|
||||||
import type { Event } from './core.ts'
|
import type { Event } from './core.ts'
|
||||||
|
|
||||||
@@ -263,3 +269,43 @@ test('binary search', () => {
|
|||||||
expect(binarySearch(['a', 'b', 'd', 'e'], b => ('a' < b ? -1 : 'a' === b ? 0 : 1))).toEqual([0, true])
|
expect(binarySearch(['a', 'b', 'd', 'e'], b => ('a' < b ? -1 : 'a' === b ? 0 : 1))).toEqual([0, true])
|
||||||
expect(binarySearch(['a', 'b', 'd', 'e'], b => ('[' < b ? -1 : '[' === b ? 0 : 1))).toEqual([0, false])
|
expect(binarySearch(['a', 'b', 'd', 'e'], b => ('[' < b ? -1 : '[' === b ? 0 : 1))).toEqual([0, false])
|
||||||
})
|
})
|
||||||
|
|
||||||
|
describe('normalizeURL', () => {
|
||||||
|
test('normalizes wss:// URLs', () => {
|
||||||
|
expect(normalizeURL('wss://example.com')).toBe('wss://example.com/')
|
||||||
|
expect(normalizeURL('wss://example.com/')).toBe('wss://example.com/')
|
||||||
|
expect(normalizeURL('wss://example.com//path')).toBe('wss://example.com/path')
|
||||||
|
expect(normalizeURL('wss://example.com:443')).toBe('wss://example.com/')
|
||||||
|
})
|
||||||
|
|
||||||
|
test('normalizes https:// URLs', () => {
|
||||||
|
expect(normalizeURL('https://example.com')).toBe('wss://example.com/')
|
||||||
|
expect(normalizeURL('https://example.com/')).toBe('wss://example.com/')
|
||||||
|
expect(normalizeURL('http://example.com//path')).toBe('ws://example.com/path')
|
||||||
|
})
|
||||||
|
|
||||||
|
test('normalizes ws:// URLs', () => {
|
||||||
|
expect(normalizeURL('ws://example.com')).toBe('ws://example.com/')
|
||||||
|
expect(normalizeURL('ws://example.com/')).toBe('ws://example.com/')
|
||||||
|
expect(normalizeURL('ws://example.com//path')).toBe('ws://example.com/path')
|
||||||
|
expect(normalizeURL('ws://example.com:80')).toBe('ws://example.com/')
|
||||||
|
})
|
||||||
|
|
||||||
|
test('adds wss:// to URLs without scheme', () => {
|
||||||
|
expect(normalizeURL('example.com')).toBe('wss://example.com/')
|
||||||
|
expect(normalizeURL('example.com/')).toBe('wss://example.com/')
|
||||||
|
expect(normalizeURL('example.com//path')).toBe('wss://example.com/path')
|
||||||
|
})
|
||||||
|
|
||||||
|
test('handles query parameters', () => {
|
||||||
|
expect(normalizeURL('wss://example.com?z=1&a=2')).toBe('wss://example.com/?a=2&z=1')
|
||||||
|
})
|
||||||
|
|
||||||
|
test('removes hash', () => {
|
||||||
|
expect(normalizeURL('wss://example.com#hash')).toBe('wss://example.com/')
|
||||||
|
})
|
||||||
|
|
||||||
|
test('throws on invalid URL', () => {
|
||||||
|
expect(() => normalizeURL('http://')).toThrow('Invalid URL: http://')
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|||||||
2
utils.ts
2
utils.ts
@@ -9,6 +9,8 @@ export function normalizeURL(url: string): string {
|
|||||||
try {
|
try {
|
||||||
if (url.indexOf('://') === -1) url = 'wss://' + url
|
if (url.indexOf('://') === -1) url = 'wss://' + url
|
||||||
let p = new URL(url)
|
let p = new URL(url)
|
||||||
|
if (p.protocol === 'http:') p.protocol = 'ws:'
|
||||||
|
else if (p.protocol === 'https:') p.protocol = 'wss:'
|
||||||
p.pathname = p.pathname.replace(/\/+/g, '/')
|
p.pathname = p.pathname.replace(/\/+/g, '/')
|
||||||
if (p.pathname.endsWith('/')) p.pathname = p.pathname.slice(0, -1)
|
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 = ''
|
if ((p.port === '80' && p.protocol === 'ws:') || (p.port === '443' && p.protocol === 'wss:')) p.port = ''
|
||||||
|
|||||||
Reference in New Issue
Block a user