This reverts commit 55d3a42079.
This commit is contained in:
@@ -1,10 +1,5 @@
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import {
|
||||
encodePairingOffer,
|
||||
decodePairingOffer,
|
||||
parsePairingCode,
|
||||
type PairingOffer
|
||||
} from './pairing'
|
||||
import { encodePairingOffer, decodePairingOffer, type PairingOffer } from './pairing'
|
||||
|
||||
describe('pairing offer', () => {
|
||||
const offer: PairingOffer = {
|
||||
@@ -54,43 +49,3 @@ describe('pairing offer', () => {
|
||||
expect(() => decodePairingOffer(`orca://pair#${base64}`)).toThrow()
|
||||
})
|
||||
})
|
||||
|
||||
describe('parsePairingCode', () => {
|
||||
const offer: PairingOffer = {
|
||||
v: 2,
|
||||
endpoint: 'ws://192.168.1.10:6768',
|
||||
deviceToken: 'token-abc',
|
||||
publicKeyB64: 'pubkey-xyz'
|
||||
}
|
||||
|
||||
it('parses a full orca://pair# URL', () => {
|
||||
const url = encodePairingOffer(offer)
|
||||
expect(parsePairingCode(url)).toEqual(offer)
|
||||
})
|
||||
|
||||
it('parses a bare base64url payload (without scheme prefix)', () => {
|
||||
const url = encodePairingOffer(offer)
|
||||
const base64url = url.split('#')[1]!
|
||||
expect(parsePairingCode(base64url)).toEqual(offer)
|
||||
})
|
||||
|
||||
it('tolerates surrounding whitespace from clipboard', () => {
|
||||
const url = encodePairingOffer(offer)
|
||||
expect(parsePairingCode(` ${url}\n`)).toEqual(offer)
|
||||
})
|
||||
|
||||
it('returns null for empty input', () => {
|
||||
expect(parsePairingCode('')).toBeNull()
|
||||
expect(parsePairingCode(' ')).toBeNull()
|
||||
})
|
||||
|
||||
it('returns null for garbage input', () => {
|
||||
expect(parsePairingCode('not a pairing code')).toBeNull()
|
||||
expect(parsePairingCode('https://example.com')).toBeNull()
|
||||
})
|
||||
|
||||
it('returns null for valid base64 of unrelated JSON', () => {
|
||||
const bogus = Buffer.from(JSON.stringify({ hello: 'world' })).toString('base64')
|
||||
expect(parsePairingCode(bogus)).toBeNull()
|
||||
})
|
||||
})
|
||||
|
||||
+1
-22
@@ -28,28 +28,7 @@ export function decodePairingOffer(url: string): PairingOffer {
|
||||
if (!url.startsWith('orca://pair') || hashIndex === -1) {
|
||||
throw new Error('Invalid pairing URL: must start with orca://pair#')
|
||||
}
|
||||
return decodePairingBase64(url.slice(hashIndex + 1))
|
||||
}
|
||||
|
||||
// Why: accept either an `orca://pair#<base64>` URL or the bare base64
|
||||
// string so the mobile paste-pair flow can take whichever the user
|
||||
// actually copied from desktop.
|
||||
export function parsePairingCode(input: string): PairingOffer | null {
|
||||
const trimmed = input.trim()
|
||||
if (!trimmed) {
|
||||
return null
|
||||
}
|
||||
try {
|
||||
if (trimmed.startsWith('orca://pair')) {
|
||||
return decodePairingOffer(trimmed)
|
||||
}
|
||||
return decodePairingBase64(trimmed)
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
function decodePairingBase64(base64url: string): PairingOffer {
|
||||
const base64url = url.slice(hashIndex + 1)
|
||||
const base64 = base64url.replace(/-/g, '+').replace(/_/g, '/')
|
||||
const json = Buffer.from(base64, 'base64').toString('utf-8')
|
||||
return PairingOfferSchema.parse(JSON.parse(json))
|
||||
|
||||
Reference in New Issue
Block a user