|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * `TursoDriverConfig.timeout` beside a `wss://` / `ws://` url is refused at |
| 5 | + * construction — the ADR-0049 enforce-or-remove answer to the one remote scheme |
| 6 | + * on which the key reaches nothing. |
| 7 | + * |
| 8 | + * # What was measured (the reading this refusal stands on) |
| 9 | + * |
| 10 | + * `@libsql/client@0.17.4` routes on scheme: `wss` / `ws` go to its WebSocket |
| 11 | + * client (`lib-esm/ws.js`), which opens `hrana.openWs(url, authToken)` and reads |
| 12 | + * neither `Config.fetch` — the seam the HTTP arm's window rides — nor any |
| 13 | + * timeout option: over `@libsql/hrana-client@0.10.0`'s `lib-esm/ws/*.js` and |
| 14 | + * `lib-esm/index.js` a `timeout` grep returns zero, while a `fetch` grep over |
| 15 | + * `lib-esm/http/` finds the call sites (the control that makes the zero a |
| 16 | + * reading). So once the HTTP and replica arms were bounded, a `wss://` url with |
| 17 | + * `timeout: 30000` still constructed, connected and ran unbounded, silently. |
| 18 | + * |
| 19 | + * # What this file pins |
| 20 | + * |
| 21 | + * The refusal itself, on both schemes, as the ADR-0112 envelope (`code` + |
| 22 | + * `status`) with a message naming the key, the scheme it met and the two ways |
| 23 | + * out — never a bare `toThrow()`, which any unrelated constructor failure |
| 24 | + * would satisfy. And the refusal's WIDTH, by controls that must stay accepted: |
| 25 | + * the same url without `timeout`; with `timeout: 0` (the documented "no |
| 26 | + * bound"); every HTTP-side scheme WITH a window (`libsql://`, `https://`, |
| 27 | + * `http://`); and the replica arm, where `sync()` is bounded whatever the url's |
| 28 | + * scheme. A refusal that took any of those would be wider than the gap. |
| 29 | + * |
| 30 | + * # Reverse verification — direction predicted before it was run |
| 31 | + * |
| 32 | + * Restore `turso-driver.ts` to its pre-refusal state and the refusal cases go |
| 33 | + * RED (the constructor returns a driver, `transportMode: 'remote'`, and there |
| 34 | + * is no envelope to read); every control stays GREEN, because the controls |
| 35 | + * describe what was accepted before and after alike. Measured — see the PR. |
| 36 | + */ |
| 37 | + |
| 38 | +import { describe, expect, it } from 'vitest'; |
| 39 | +import { createTursoDriver } from './index.js'; |
| 40 | +import { TursoDriver } from './turso-driver.js'; |
| 41 | + |
| 42 | +type Refusal = Error & { code?: string; status?: number }; |
| 43 | + |
| 44 | +/** The error `build` threw, or `null` when it returned. */ |
| 45 | +function refusalOf(build: () => unknown): Refusal | null { |
| 46 | + try { |
| 47 | + build(); |
| 48 | + return null; |
| 49 | + } catch (error) { |
| 50 | + return error as Refusal; |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +const WINDOW_MS = 30_000; |
| 55 | +const WSS_URL = 'wss://db.example.turso.io'; |
| 56 | +const WS_URL = 'ws://127.0.0.1:8080'; |
| 57 | +const PRIMARY_URL = 'libsql://primary.example.turso.io'; |
| 58 | + |
| 59 | +describe('TursoDriverConfig.timeout beside a WebSocket url — refused at construction', () => { |
| 60 | + it.each([ |
| 61 | + ['wss://', WSS_URL], |
| 62 | + ['ws://', WS_URL], |
| 63 | + ])('%s + timeout is refused as VALIDATION_ERROR / 400, naming the key, the scheme and the way out', (scheme, url) => { |
| 64 | + const refusal = refusalOf(() => new TursoDriver({ url, authToken: 'token', timeout: WINDOW_MS })); |
| 65 | + |
| 66 | + expect(refusal).not.toBeNull(); |
| 67 | + expect(refusal!.code).toBe('VALIDATION_ERROR'); |
| 68 | + expect(refusal!.status).toBe(400); |
| 69 | + expect(refusal!.message).toContain('TursoDriverConfig.timeout'); |
| 70 | + expect(refusal!.message).toContain(`${WINDOW_MS} ms`); |
| 71 | + expect(refusal!.message).toContain(`\`${scheme}\``); |
| 72 | + // Both ways out are in the text: the bounded spellings, and the option of |
| 73 | + // dropping the key. |
| 74 | + expect(refusal!.message).toContain('libsql://'); |
| 75 | + expect(refusal!.message).toContain('https://'); |
| 76 | + expect(refusal!.message).toContain('omit `timeout`'); |
| 77 | + }); |
| 78 | + |
| 79 | + it('a forced `mode: "remote"` meets the same refusal — the override does not route around it', () => { |
| 80 | + const refusal = refusalOf(() => new TursoDriver({ url: WSS_URL, mode: 'remote', timeout: WINDOW_MS })); |
| 81 | + |
| 82 | + expect(refusal?.code).toBe('VALIDATION_ERROR'); |
| 83 | + expect(refusal?.status).toBe(400); |
| 84 | + }); |
| 85 | + |
| 86 | + it('createTursoDriver() is the same constructor, and refuses the same pair', () => { |
| 87 | + const refusal = refusalOf(() => createTursoDriver({ url: WSS_URL, timeout: WINDOW_MS })); |
| 88 | + |
| 89 | + expect(refusal?.code).toBe('VALIDATION_ERROR'); |
| 90 | + expect(refusal?.status).toBe(400); |
| 91 | + }); |
| 92 | +}); |
| 93 | + |
| 94 | +describe('CONTROLS — what the refusal must leave accepted', () => { |
| 95 | + it.each([WSS_URL, WS_URL])('%s with no timeout constructs as remote, exactly as before', (url) => { |
| 96 | + const driver = new TursoDriver({ url, authToken: 'token' }); |
| 97 | + |
| 98 | + expect(driver.transportMode).toBe('remote'); |
| 99 | + expect(driver.getTursoConfig().timeout).toBeUndefined(); |
| 100 | + }); |
| 101 | + |
| 102 | + it('`timeout: 0` is the documented "no bound", asks for nothing, and is not refused', () => { |
| 103 | + const driver = new TursoDriver({ url: WSS_URL, authToken: 'token', timeout: 0 }); |
| 104 | + |
| 105 | + expect(driver.transportMode).toBe('remote'); |
| 106 | + expect(driver.getTursoConfig().timeout).toBe(0); |
| 107 | + }); |
| 108 | + |
| 109 | + it.each(['libsql://db.example.turso.io', 'https://db.example.turso.io', 'http://127.0.0.1:8080'])( |
| 110 | + '%s + timeout stays accepted — the HTTP arm IS bounded, so the refusal is no wider than the gap', |
| 111 | + (url) => { |
| 112 | + const driver = new TursoDriver({ url, authToken: 'token', timeout: WINDOW_MS }); |
| 113 | + |
| 114 | + expect(driver.transportMode).toBe('remote'); |
| 115 | + expect(driver.getTursoConfig().timeout).toBe(WINDOW_MS); |
| 116 | + }, |
| 117 | + ); |
| 118 | + |
| 119 | + it('the replica arm keeps timeout whatever the url scheme — sync() is bounded there', () => { |
| 120 | + const fileReplica = new TursoDriver({ |
| 121 | + url: ':memory:', |
| 122 | + syncUrl: PRIMARY_URL, |
| 123 | + timeout: WINDOW_MS, |
| 124 | + sync: { onConnect: false }, |
| 125 | + }); |
| 126 | + expect(fileReplica.transportMode).toBe('replica'); |
| 127 | + expect(fileReplica.getTursoConfig().timeout).toBe(WINDOW_MS); |
| 128 | + |
| 129 | + const wsReplica = new TursoDriver({ |
| 130 | + url: WSS_URL, |
| 131 | + syncUrl: PRIMARY_URL, |
| 132 | + timeout: WINDOW_MS, |
| 133 | + sync: { onConnect: false }, |
| 134 | + }); |
| 135 | + expect(wsReplica.transportMode).toBe('replica'); |
| 136 | + expect(wsReplica.getTursoConfig().timeout).toBe(WINDOW_MS); |
| 137 | + }); |
| 138 | +}); |
0 commit comments