orez 0.2.27 → 0.2.30

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (157) hide show
  1. package/dist/cf-do/worker.d.ts +3 -0
  2. package/dist/cf-do/worker.d.ts.map +1 -1
  3. package/dist/cf-do/worker.js +37 -15
  4. package/dist/cf-do/worker.js.map +1 -1
  5. package/dist/index.d.ts.map +1 -1
  6. package/dist/index.js +8 -0
  7. package/dist/index.js.map +1 -1
  8. package/package.json +3 -4
  9. package/src/admin/admin-data.test.ts +0 -348
  10. package/src/admin/http-proxy.ts +0 -252
  11. package/src/admin/log-store.ts +0 -192
  12. package/src/admin/server.ts +0 -471
  13. package/src/admin/ui.ts +0 -1322
  14. package/src/bench/proxy-throughput.bench.ts +0 -343
  15. package/src/bench/serial-mutations.bench.ts +0 -270
  16. package/src/browser.ts +0 -203
  17. package/src/cf-do/.wrangler/cache/cf.json +0 -1
  18. package/src/cf-do/.wrangler/state/v3/cache/miniflare-CacheObject/metadata.sqlite +0 -0
  19. package/src/cf-do/.wrangler/state/v3/cache/miniflare-CacheObject/metadata.sqlite-shm +0 -0
  20. package/src/cf-do/.wrangler/state/v3/cache/miniflare-CacheObject/metadata.sqlite-wal +0 -0
  21. package/src/cf-do/.wrangler/state/v3/do/zero-do-ZeroDO/0ffaabee41a60e04dd0eb7db3073f0a40139e6a97ccd26823967acb652b89a7b.sqlite +0 -0
  22. package/src/cf-do/.wrangler/state/v3/do/zero-do-ZeroDO/metadata.sqlite +0 -0
  23. package/src/cf-do/.wrangler/state/v3/do/zero-do-ZeroDO/metadata.sqlite-shm +0 -0
  24. package/src/cf-do/.wrangler/state/v3/do/zero-do-ZeroDO/metadata.sqlite-wal +0 -0
  25. package/src/cf-do/.wrangler/tmp/bundle-0z4CpE/middleware-insertion-facade.js +0 -11
  26. package/src/cf-do/.wrangler/tmp/bundle-0z4CpE/middleware-loader.entry.ts +0 -134
  27. package/src/cf-do/.wrangler/tmp/bundle-vYmw0E/middleware-insertion-facade.js +0 -11
  28. package/src/cf-do/.wrangler/tmp/bundle-vYmw0E/middleware-loader.entry.ts +0 -134
  29. package/src/cf-do/.wrangler/tmp/dev-cbILNo/worker.js +0 -1059
  30. package/src/cf-do/.wrangler/tmp/dev-cbILNo/worker.js.map +0 -8
  31. package/src/cf-do/.wrangler/tmp/dev-qbho19/worker.js +0 -1059
  32. package/src/cf-do/.wrangler/tmp/dev-qbho19/worker.js.map +0 -8
  33. package/src/cf-do/ARCHITECTURE.md +0 -93
  34. package/src/cf-do/CHAT_E2E.md +0 -213
  35. package/src/cf-do/watermark.test.ts +0 -103
  36. package/src/cf-do/watermark.ts +0 -118
  37. package/src/cf-do/worker.ts +0 -1041
  38. package/src/cf-do/wrangler.toml +0 -11
  39. package/src/cf-pglite/README.md +0 -19
  40. package/src/change-tracking.ts +0 -25
  41. package/src/child-process.test.ts +0 -147
  42. package/src/child-process.ts +0 -90
  43. package/src/cli-entry.ts +0 -72
  44. package/src/cli.test.ts +0 -40
  45. package/src/cli.ts +0 -1214
  46. package/src/config.ts +0 -150
  47. package/src/do-sql-tracking.test.ts +0 -19
  48. package/src/do-sql-tracking.ts +0 -19
  49. package/src/index.ts +0 -1215
  50. package/src/integration/integration.test.ts +0 -517
  51. package/src/integration/native-binary.guard.test.ts +0 -13
  52. package/src/integration/native-startup.test.ts +0 -44
  53. package/src/integration/replication-latency.test.ts +0 -428
  54. package/src/integration/restore-live-stress.test.ts +0 -433
  55. package/src/integration/restore-reset.test.ts +0 -400
  56. package/src/integration/restore.test.ts +0 -274
  57. package/src/integration/test-permissions.ts +0 -147
  58. package/src/load-config.ts +0 -46
  59. package/src/log.ts +0 -96
  60. package/src/mutex.ts +0 -47
  61. package/src/pg-proxy-browser.singledb.test.ts +0 -233
  62. package/src/pg-proxy-browser.ts +0 -2022
  63. package/src/pg-proxy-do-backend.test.ts +0 -3890
  64. package/src/pg-proxy-do-backend.ts +0 -7191
  65. package/src/pg-proxy.ts +0 -1087
  66. package/src/pg-sqlite-compiler/README.md +0 -53
  67. package/src/pg-sqlite-compiler/catalog/seed.ts +0 -524
  68. package/src/pg-sqlite-compiler/fixtures/pgsqlite/arithmetic.json +0 -307
  69. package/src/pg-sqlite-compiler/fixtures/pgsqlite/array.json +0 -377
  70. package/src/pg-sqlite-compiler/fixtures/pgsqlite/cast.json +0 -12
  71. package/src/pg-sqlite-compiler/fixtures/pgsqlite/catalog.json +0 -447
  72. package/src/pg-sqlite-compiler/fixtures/pgsqlite/create-table.json +0 -32
  73. package/src/pg-sqlite-compiler/fixtures/pgsqlite/datetime.json +0 -397
  74. package/src/pg-sqlite-compiler/fixtures/pgsqlite/enum.json +0 -337
  75. package/src/pg-sqlite-compiler/fixtures/pgsqlite/insert.json +0 -337
  76. package/src/pg-sqlite-compiler/fixtures/pgsqlite/json.json +0 -537
  77. package/src/pg-sqlite-compiler/fixtures/pgsqlite/misc.json +0 -1837
  78. package/src/pg-sqlite-compiler/index.ts +0 -73
  79. package/src/pg-sqlite-compiler/integration.test.ts +0 -136
  80. package/src/pg-sqlite-compiler/passes/ast-utils.ts +0 -113
  81. package/src/pg-sqlite-compiler/passes/catalog.ts +0 -65
  82. package/src/pg-sqlite-compiler/passes/datetime.ts +0 -74
  83. package/src/pg-sqlite-compiler/passes/index.ts +0 -49
  84. package/src/pg-sqlite-compiler/passes/types.ts +0 -156
  85. package/src/pg-sqlite-compiler/smoke.test.ts +0 -69
  86. package/src/pg-sqlite-compiler/test/catalog.test.ts +0 -171
  87. package/src/pg-sqlite-compiler/test/corpus.test.ts +0 -161
  88. package/src/pg-sqlite-compiler/test/datetime.oracle.test.ts +0 -102
  89. package/src/pg-sqlite-compiler/test/oracle.ts +0 -237
  90. package/src/pg-sqlite-compiler/test/types.test.ts +0 -109
  91. package/src/pg-sqlite-compiler/types.ts +0 -63
  92. package/src/pglite-ipc.test.ts +0 -116
  93. package/src/pglite-ipc.ts +0 -266
  94. package/src/pglite-manager.ts +0 -557
  95. package/src/pglite-web-proxy.test.ts +0 -57
  96. package/src/pglite-web-proxy.ts +0 -221
  97. package/src/pglite-web-worker.ts +0 -152
  98. package/src/pglite-worker-thread.ts +0 -253
  99. package/src/port.ts +0 -25
  100. package/src/process-title.ts +0 -9
  101. package/src/recovery.ts +0 -155
  102. package/src/replication/change-tracker.test.ts +0 -357
  103. package/src/replication/change-tracker.ts +0 -279
  104. package/src/replication/handler.test.ts +0 -511
  105. package/src/replication/handler.ts +0 -1190
  106. package/src/replication/pgoutput-encoder.test.ts +0 -697
  107. package/src/replication/pgoutput-encoder.ts +0 -373
  108. package/src/replication/tcp-replication.test.ts +0 -876
  109. package/src/replication/zero-compat.test.ts +0 -1150
  110. package/src/restore-stress.test.ts +0 -188
  111. package/src/s3-local.ts +0 -203
  112. package/src/shim/hooks.mjs +0 -120
  113. package/src/shim/register.mjs +0 -4
  114. package/src/sqlite-mode/apply-mode.ts +0 -224
  115. package/src/sqlite-mode/index.ts +0 -15
  116. package/src/sqlite-mode/native-binary.ts +0 -89
  117. package/src/sqlite-mode/package-resolve.ts +0 -17
  118. package/src/sqlite-mode/resolve-mode.ts +0 -80
  119. package/src/sqlite-mode/shim-template.ts +0 -159
  120. package/src/sqlite-mode/sqlite-mode.test.ts +0 -427
  121. package/src/sqlite-mode/types.ts +0 -30
  122. package/src/vite-plugin.ts +0 -67
  123. package/src/wasm-sqlite.test.ts +0 -537
  124. package/src/worker/browser-admin.ts +0 -52
  125. package/src/worker/browser-build-config.test.ts +0 -71
  126. package/src/worker/browser-build-config.ts +0 -109
  127. package/src/worker/browser-embed-admin.test.ts +0 -75
  128. package/src/worker/browser-embed.ts +0 -345
  129. package/src/worker/cf-patches.ts +0 -384
  130. package/src/worker/embed-integration.test.ts +0 -321
  131. package/src/worker/index.ts +0 -138
  132. package/src/worker/shims/fastify.test.ts +0 -255
  133. package/src/worker/shims/fastify.ts +0 -306
  134. package/src/worker/shims/http-service.test.ts +0 -355
  135. package/src/worker/shims/http-service.ts +0 -293
  136. package/src/worker/shims/node-stub.ts +0 -290
  137. package/src/worker/shims/oxfmt.ts +0 -3
  138. package/src/worker/shims/postgres-browser.ts +0 -59
  139. package/src/worker/shims/postgres-socket.test.ts +0 -576
  140. package/src/worker/shims/postgres-socket.ts +0 -310
  141. package/src/worker/shims/postgres.test.ts +0 -364
  142. package/src/worker/shims/postgres.ts +0 -1454
  143. package/src/worker/shims/sqlite-browser.test.ts +0 -233
  144. package/src/worker/shims/sqlite-browser.ts +0 -175
  145. package/src/worker/shims/sqlite.test.ts +0 -786
  146. package/src/worker/shims/sqlite.ts +0 -978
  147. package/src/worker/shims/stream-browser.ts +0 -15
  148. package/src/worker/shims/ws-browser.test.ts +0 -205
  149. package/src/worker/shims/ws-browser.ts +0 -248
  150. package/src/worker/shims/ws.test.ts +0 -288
  151. package/src/worker/shims/ws.ts +0 -467
  152. package/src/worker/shims/zero-process-env.ts +0 -11
  153. package/src/worker/types.ts +0 -75
  154. package/src/worker/worker-integration.test.ts +0 -223
  155. package/src/worker/worker.test.ts +0 -136
  156. package/src/worker/zero-cache-embed-cf.ts +0 -463
  157. package/src/worker/zero-cache-embed.ts +0 -277
@@ -1,355 +0,0 @@
1
- import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'
2
-
3
- import {
4
- HttpServiceAdapter,
5
- createHttpServiceAdapter,
6
- type InjectableFastify,
7
- type WebSocketHandler,
8
- type WebSocketUpgradeResult,
9
- } from './http-service.js'
10
-
11
- /** minimal mock fastify that records inject() calls and returns canned responses */
12
- function createMockFastify(
13
- responses: Record<
14
- string,
15
- { status: number; body: string; headers?: Record<string, string> }
16
- >
17
- ): InjectableFastify {
18
- return {
19
- async ready() {},
20
- async inject(opts) {
21
- const key = `${opts.method} ${opts.url}`
22
- const resp = responses[key]
23
- if (!resp) {
24
- return { statusCode: 404, headers: {}, body: 'not found' }
25
- }
26
- return {
27
- statusCode: resp.status,
28
- headers: resp.headers ?? { 'content-type': 'text/plain' },
29
- body: resp.body,
30
- }
31
- },
32
- }
33
- }
34
-
35
- /** helper to create a Request (works in node/bun/vitest with fetch globals) */
36
- function makeRequest(
37
- url: string,
38
- opts?: { method?: string; headers?: Record<string, string>; body?: string }
39
- ): Request {
40
- return new Request(url, {
41
- method: opts?.method ?? 'GET',
42
- headers: opts?.headers,
43
- body: opts?.body,
44
- })
45
- }
46
-
47
- describe('HttpServiceAdapter', () => {
48
- let adapter: HttpServiceAdapter
49
-
50
- beforeEach(() => {
51
- adapter = new HttpServiceAdapter()
52
- })
53
-
54
- describe('initialization', () => {
55
- it('is not ready before initialize', () => {
56
- expect(adapter.isReady).toBe(false)
57
- })
58
-
59
- it('is ready after initialize', async () => {
60
- await adapter.initialize(createMockFastify({}))
61
- expect(adapter.isReady).toBe(true)
62
- })
63
-
64
- it('returns 503 when not initialized', async () => {
65
- const resp = await adapter.handleRequest(makeRequest('http://localhost/'))
66
- expect(resp.status).toBe(503)
67
- })
68
- })
69
-
70
- describe('HTTP routing via inject()', () => {
71
- beforeEach(async () => {
72
- await adapter.initialize(
73
- createMockFastify({
74
- 'GET /': { status: 200, body: 'ok' },
75
- 'GET /keepalive': { status: 200, body: 'alive' },
76
- 'GET /statz': {
77
- status: 200,
78
- body: '{"uptime":123}',
79
- headers: { 'content-type': 'application/json' },
80
- },
81
- 'POST /data': { status: 201, body: 'created' },
82
- })
83
- )
84
- })
85
-
86
- it('routes GET / to fastify', async () => {
87
- const resp = await adapter.handleRequest(makeRequest('http://localhost/'))
88
- expect(resp.status).toBe(200)
89
- expect(await resp.text()).toBe('ok')
90
- })
91
-
92
- it('routes GET /keepalive to fastify', async () => {
93
- const resp = await adapter.handleRequest(makeRequest('http://localhost/keepalive'))
94
- expect(resp.status).toBe(200)
95
- expect(await resp.text()).toBe('alive')
96
- })
97
-
98
- it('routes GET /statz with correct content-type', async () => {
99
- const resp = await adapter.handleRequest(makeRequest('http://localhost/statz'))
100
- expect(resp.status).toBe(200)
101
- expect(resp.headers.get('content-type')).toBe('application/json')
102
- expect(await resp.text()).toBe('{"uptime":123}')
103
- })
104
-
105
- it('handles POST with body', async () => {
106
- const resp = await adapter.handleRequest(
107
- makeRequest('http://localhost/data', {
108
- method: 'POST',
109
- body: '{"key":"value"}',
110
- headers: { 'content-type': 'application/json' },
111
- })
112
- )
113
- expect(resp.status).toBe(201)
114
- expect(await resp.text()).toBe('created')
115
- })
116
-
117
- it('returns 404 for unknown routes', async () => {
118
- const resp = await adapter.handleRequest(makeRequest('http://localhost/nope'))
119
- expect(resp.status).toBe(404)
120
- })
121
-
122
- it('preserves query string', async () => {
123
- const fastify = createMockFastify({
124
- 'GET /search?q=hello': { status: 200, body: 'found' },
125
- })
126
- await adapter.initialize(fastify)
127
- const resp = await adapter.handleRequest(
128
- makeRequest('http://localhost/search?q=hello')
129
- )
130
- expect(resp.status).toBe(200)
131
- expect(await resp.text()).toBe('found')
132
- })
133
- })
134
-
135
- describe('WebSocket route matching', () => {
136
- const noopHandler: WebSocketHandler = () => {}
137
-
138
- it('matches exact WebSocket routes', () => {
139
- adapter.addWsRoute('/replication/v1/changes', noopHandler)
140
- const match = adapter.matchWsRoute('/replication/v1/changes')
141
- expect(match).not.toBeNull()
142
- expect(match!.pattern).toBe('/replication/v1/changes')
143
- })
144
-
145
- it('returns null for unmatched paths', () => {
146
- adapter.addWsRoute('/replication/v1/changes', noopHandler)
147
- expect(adapter.matchWsRoute('/other/path')).toBeNull()
148
- })
149
-
150
- it('matches wildcard patterns', () => {
151
- adapter.addWsRoute('/replication/v*/changes', noopHandler)
152
-
153
- expect(adapter.matchWsRoute('/replication/v1/changes')).not.toBeNull()
154
- expect(adapter.matchWsRoute('/replication/v2/changes')).not.toBeNull()
155
- expect(adapter.matchWsRoute('/replication/v99/changes')).not.toBeNull()
156
-
157
- // should not match different structure
158
- expect(adapter.matchWsRoute('/replication/v1/snapshot')).toBeNull()
159
- })
160
-
161
- it('matches multiple wildcard patterns', () => {
162
- const changesHandler: WebSocketHandler = () => {}
163
- const snapshotHandler: WebSocketHandler = () => {}
164
-
165
- adapter.addWsRoute('/replication/v*/changes', changesHandler)
166
- adapter.addWsRoute('/replication/v*/snapshot', snapshotHandler)
167
-
168
- const changesMatch = adapter.matchWsRoute('/replication/v1/changes')
169
- expect(changesMatch).not.toBeNull()
170
- expect(changesMatch!.handler).toBe(changesHandler)
171
-
172
- const snapshotMatch = adapter.matchWsRoute('/replication/v2/snapshot')
173
- expect(snapshotMatch).not.toBeNull()
174
- expect(snapshotMatch!.handler).toBe(snapshotHandler)
175
- })
176
-
177
- it('prefers exact match over pattern', () => {
178
- const exactHandler: WebSocketHandler = () => {}
179
- const patternHandler: WebSocketHandler = () => {}
180
-
181
- adapter.addWsRoute('/replication/v1/changes', exactHandler)
182
- adapter.addWsRoute('/replication/v*/changes', patternHandler)
183
-
184
- const match = adapter.matchWsRoute('/replication/v1/changes')
185
- expect(match).not.toBeNull()
186
- expect(match!.handler).toBe(exactHandler)
187
- })
188
-
189
- it('wildcard does not match across slashes', () => {
190
- adapter.addWsRoute('/api/v*/data', noopHandler)
191
- expect(adapter.matchWsRoute('/api/v1/extra/data')).toBeNull()
192
- })
193
- })
194
-
195
- describe('WebSocket upgrade detection', () => {
196
- beforeEach(async () => {
197
- await adapter.initialize(createMockFastify({}))
198
- })
199
-
200
- it('detects upgrade header', async () => {
201
- adapter.addWsRoute('/ws', () => {})
202
-
203
- // WebSocketPair is a CF global — not available in vitest.
204
- // we verify the adapter detects the upgrade and tries to use it.
205
- const resp = await adapter.handleRequest(
206
- makeRequest('http://localhost/ws', {
207
- headers: { upgrade: 'websocket' },
208
- })
209
- )
210
-
211
- // without WebSocketPair in the runtime, we get a 500
212
- // this confirms the adapter correctly detected the upgrade
213
- // and attempted ws handling (rather than routing to fastify)
214
- expect(resp.status).toBe(500)
215
- expect(await resp.text()).toBe('WebSocketPair not available in this runtime')
216
- })
217
-
218
- it('returns 404 for ws upgrade with no matching route', async () => {
219
- const resp = await adapter.handleRequest(
220
- makeRequest('http://localhost/unknown', {
221
- headers: { upgrade: 'websocket' },
222
- })
223
- )
224
- expect(resp.status).toBe(404)
225
- })
226
-
227
- it('routes non-upgrade requests to fastify even if ws route exists', async () => {
228
- adapter.addWsRoute('/dual', () => {})
229
-
230
- // regular GET (no upgrade header) should go to fastify inject
231
- const resp = await adapter.handleRequest(makeRequest('http://localhost/dual'))
232
- // fastify returns 404 since we didn't register an HTTP route for /dual
233
- expect(resp.status).toBe(404)
234
- })
235
- })
236
-
237
- describe('WebSocket upgrade preparation with mock WebSocketPair', () => {
238
- // tests use prepareWebSocketUpgrade() directly because the CF Workers
239
- // Response constructor (status 101 + webSocket property) is not available
240
- // in Node.js. this tests the full setup logic without the CF-specific part.
241
-
242
- let originalWebSocketPair: unknown
243
-
244
- beforeEach(async () => {
245
- originalWebSocketPair = (globalThis as any).WebSocketPair
246
- await adapter.initialize(createMockFastify({}))
247
- })
248
-
249
- afterEach(() => {
250
- if (originalWebSocketPair !== undefined) {
251
- ;(globalThis as any).WebSocketPair = originalWebSocketPair
252
- } else {
253
- delete (globalThis as any).WebSocketPair
254
- }
255
- })
256
-
257
- it('creates WebSocket pair and returns upgrade result', () => {
258
- const mockServer = {
259
- accept: vi.fn(),
260
- close: vi.fn(),
261
- send: vi.fn(),
262
- addEventListener: vi.fn(),
263
- }
264
- const mockClient = { close: vi.fn() }
265
-
266
- ;(globalThis as any).WebSocketPair = class {
267
- 0 = mockClient
268
- 1 = mockServer
269
- }
270
-
271
- const handler = vi.fn()
272
- adapter.addWsRoute('/ws/test', handler)
273
-
274
- const req = makeRequest('http://localhost/ws/test', {
275
- headers: { upgrade: 'websocket' },
276
- })
277
- const url = new URL(req.url)
278
- const result = adapter.prepareWebSocketUpgrade(req, url)
279
-
280
- // should be an upgrade result, not a Response or null
281
- expect(result).not.toBeNull()
282
- expect(result).not.toBeInstanceOf(Response)
283
-
284
- const upgrade = result as WebSocketUpgradeResult
285
- expect(upgrade.client).toBe(mockClient)
286
- expect(upgrade.server).toBe(mockServer)
287
- expect(upgrade.handler).toBe(handler)
288
- expect(upgrade.url.pathname).toBe('/ws/test')
289
- expect(mockServer.accept).toHaveBeenCalled()
290
- })
291
-
292
- it('returns null when no route matches', () => {
293
- const req = makeRequest('http://localhost/nope')
294
- const url = new URL(req.url)
295
- expect(adapter.prepareWebSocketUpgrade(req, url)).toBeNull()
296
- })
297
-
298
- it('returns Response(500) when WebSocketPair is unavailable', () => {
299
- delete (globalThis as any).WebSocketPair
300
-
301
- adapter.addWsRoute('/ws/test', () => {})
302
-
303
- const req = makeRequest('http://localhost/ws/test')
304
- const url = new URL(req.url)
305
- const result = adapter.prepareWebSocketUpgrade(req, url)
306
-
307
- expect(result).toBeInstanceOf(Response)
308
- expect((result as Response).status).toBe(500)
309
- })
310
-
311
- it('handler invocation catches errors and closes socket', async () => {
312
- const mockServer = {
313
- accept: vi.fn(),
314
- close: vi.fn(),
315
- send: vi.fn(),
316
- addEventListener: vi.fn(),
317
- }
318
- const mockClient = { close: vi.fn() }
319
-
320
- ;(globalThis as any).WebSocketPair = class {
321
- 0 = mockClient
322
- 1 = mockServer
323
- }
324
-
325
- const handler = vi.fn().mockRejectedValue(new Error('handler boom'))
326
- adapter.addWsRoute('/ws/fail', handler)
327
-
328
- const req = makeRequest('http://localhost/ws/fail')
329
- const url = new URL(req.url)
330
- const result = adapter.prepareWebSocketUpgrade(req, url) as WebSocketUpgradeResult
331
-
332
- // simulate what handleWebSocket does: invoke handler and catch errors
333
- await Promise.resolve(
334
- result.handler(result.server as any, result.request, result.url)
335
- ).catch((err) => {
336
- try {
337
- ;(result.server as any).close(1011, String(err))
338
- } catch {
339
- // socket may already be closed
340
- }
341
- })
342
-
343
- expect(handler).toHaveBeenCalled()
344
- expect(mockServer.close).toHaveBeenCalledWith(1011, 'Error: handler boom')
345
- })
346
- })
347
-
348
- describe('createHttpServiceAdapter factory', () => {
349
- it('returns a valid adapter', () => {
350
- const adapter = createHttpServiceAdapter()
351
- expect(adapter).toBeInstanceOf(HttpServiceAdapter)
352
- expect(adapter.isReady).toBe(false)
353
- })
354
- })
355
- })
@@ -1,293 +0,0 @@
1
- // NOTE THIS IS NOT OREZ NODE THIS IS NOT A GOOD REFERENCE BECAUSE ITS OUR EARLY GUESS AT WHAT COULD WORK
2
- // DO NOT STUDY THIS, THE OTHER STUFF IN SRC IS WHERE YOU EANT TO LOOK
3
-
4
- /**
5
- * http service adapter for CF Workers / Durable Objects.
6
- *
7
- * bridges between a Durable Object's fetch() handler and zero-cache's
8
- * Fastify-based HTTP service. two routing modes:
9
- *
10
- * 1. HTTP routes — delegated to Fastify via inject() (health, keepalive, statz, etc.)
11
- * 2. WebSocket routes — handled via DO native WebSocket pairs, bypassing Fastify
12
- * entirely since inject() cannot do upgrades.
13
- *
14
- * usage:
15
- * const adapter = createHttpServiceAdapter()
16
- * adapter.addWsRoute('/replication/v1/changes', handler)
17
- * await adapter.initialize(fastifyInstance)
18
- * // in DO fetch():
19
- * return adapter.handleRequest(request)
20
- */
21
-
22
- // CF Workers globals — not in Node.js types
23
- declare const WebSocketPair: (new () => { 0: WebSocket; 1: WebSocket }) | undefined
24
-
25
- // -- types for the adapter, kept minimal so we don't import fastify at module level --
26
-
27
- /** handler for a WebSocket connection routed from a DO */
28
- export type WebSocketHandler = (
29
- server: WebSocket,
30
- request: Request,
31
- url: URL
32
- ) => void | Promise<void>
33
-
34
- /** minimal interface for what we need from fastify (avoids hard dep) */
35
- export interface InjectableFastify {
36
- inject(opts: {
37
- method: string
38
- url: string
39
- headers?: Record<string, string>
40
- payload?: string | null
41
- }): Promise<{
42
- statusCode: number
43
- headers: Record<string, string>
44
- body: string
45
- }>
46
- ready(): Promise<void>
47
- }
48
-
49
- /** route match result */
50
- interface RouteMatch {
51
- handler: WebSocketHandler
52
- pattern: string
53
- }
54
-
55
- /** result of preparing a WebSocket upgrade (before CF Response creation) */
56
- export interface WebSocketUpgradeResult {
57
- client: WebSocket
58
- server: WebSocket
59
- handler: WebSocketHandler
60
- request: Request
61
- url: URL
62
- }
63
-
64
- /**
65
- * the http service adapter. manages routing between Fastify inject()
66
- * for regular HTTP and DO-native WebSocket pairs for upgrade requests.
67
- */
68
- export class HttpServiceAdapter {
69
- private fastify: InjectableFastify | null = null
70
- private wsRoutes: Map<string, WebSocketHandler> = new Map()
71
- private wsPatterns: Array<{
72
- regex: RegExp
73
- pattern: string
74
- handler: WebSocketHandler
75
- }> = []
76
- private initialized = false
77
-
78
- /**
79
- * register a WebSocket route handler.
80
- * supports exact paths and simple patterns with wildcard segments.
81
- *
82
- * call before initialize() so routes are ready when requests arrive.
83
- *
84
- * examples:
85
- * addWsRoute('/replication/v1/changes', handler) -- exact
86
- * addWsRoute('/replication/v[star]/changes', handler) -- wildcard (use *)
87
- */
88
- addWsRoute(path: string, handler: WebSocketHandler): void {
89
- if (path.includes('*')) {
90
- // convert glob-style pattern to regex
91
- const escaped = path.replace(/[.+?^${}()|[\]\\]/g, '\\$&')
92
- const regex = new RegExp('^' + escaped.replace(/\*/g, '[^/]+') + '$')
93
- this.wsPatterns.push({ regex, pattern: path, handler })
94
- } else {
95
- this.wsRoutes.set(path, handler)
96
- }
97
- }
98
-
99
- /**
100
- * initialize with a fastify instance. call after route registration
101
- * (e.g., after zero-cache's init callback has run) but before handling requests.
102
- * calls fastify.ready() to finalize route compilation.
103
- */
104
- async initialize(fastify: InjectableFastify): Promise<void> {
105
- this.fastify = fastify
106
- await fastify.ready()
107
- this.initialized = true
108
- }
109
-
110
- /** whether the adapter is ready to handle requests */
111
- get isReady(): boolean {
112
- return this.initialized
113
- }
114
-
115
- /**
116
- * handle an incoming request from a DO's fetch() method.
117
- * routes WebSocket upgrades to DO-native WebSocket handlers,
118
- * everything else to Fastify inject().
119
- */
120
- async handleRequest(request: Request): Promise<Response> {
121
- if (!this.initialized) {
122
- return new Response('service not ready', { status: 503 })
123
- }
124
-
125
- const url = new URL(request.url)
126
-
127
- // websocket upgrade — route to DO native handler
128
- if (request.headers.get('upgrade')?.toLowerCase() === 'websocket') {
129
- return this.handleWebSocket(request, url)
130
- }
131
-
132
- // regular HTTP — fastify inject
133
- return this.handleHttp(request, url)
134
- }
135
-
136
- /**
137
- * route a regular HTTP request through Fastify's inject() mechanism.
138
- * this works for all non-WebSocket routes (health, keepalive, statz, etc.).
139
- */
140
- private async handleHttp(request: Request, url: URL): Promise<Response> {
141
- if (!this.fastify) {
142
- return new Response('fastify not initialized', { status: 503 })
143
- }
144
-
145
- // build headers object from request
146
- const headers: Record<string, string> = {}
147
- request.headers.forEach((value, key) => {
148
- headers[key] = value
149
- })
150
-
151
- // read body for non-GET/HEAD requests
152
- let payload: string | null = null
153
- if (request.method !== 'GET' && request.method !== 'HEAD' && request.body) {
154
- payload = await request.text()
155
- }
156
-
157
- const result = await this.fastify.inject({
158
- method: request.method,
159
- url: url.pathname + url.search,
160
- headers,
161
- payload,
162
- })
163
-
164
- return new Response(result.body, {
165
- status: result.statusCode,
166
- headers: result.headers,
167
- })
168
- }
169
-
170
- /**
171
- * handle a WebSocket upgrade request using DO-native WebSocket pairs.
172
- *
173
- * creates a WebSocketPair, accepts the server side, and passes it
174
- * to the matched route handler. returns the 101 response with the
175
- * client side for the DO runtime to manage.
176
- *
177
- * note: WebSocketPair is a CF Workers global. in non-CF environments
178
- * this will throw — callers should polyfill or avoid ws routes.
179
- */
180
- private handleWebSocket(request: Request, url: URL): Response {
181
- const result = this.prepareWebSocketUpgrade(request, url)
182
-
183
- if (!result) {
184
- return new Response('no websocket handler for path', { status: 404 })
185
- }
186
-
187
- if (result instanceof Response) {
188
- return result
189
- }
190
-
191
- // invoke handler (fire-and-forget; handler manages the connection lifecycle)
192
- // errors in the handler should close the socket, not crash the DO
193
- Promise.resolve(result.handler(result.server, result.request, result.url)).catch(
194
- (err) => {
195
- try {
196
- result.server.close(1011, String(err))
197
- } catch {
198
- // socket may already be closed
199
- }
200
- }
201
- )
202
-
203
- // return 101 with client socket for DO runtime.
204
- // CF Workers Response constructor accepts status 101 + webSocket property.
205
- // standard fetch spec rejects status 101, so we try CF-style first and
206
- // fall back to a plain 101-like response for non-CF environments.
207
- try {
208
- return new Response(null, {
209
- status: 101,
210
- // @ts-expect-error — webSocket is a CF Workers Response extension
211
- webSocket: result.client,
212
- })
213
- } catch {
214
- // non-CF runtime (Node.js, vitest) — status 101 not allowed.
215
- // return a marker response that callers can detect.
216
- const resp = new Response(null, { status: 200 })
217
- ;(resp as any).__orez_websocket = result.client
218
- ;(resp as any).__orez_ws_upgrade = true
219
- return resp
220
- }
221
- }
222
-
223
- /**
224
- * prepare a WebSocket upgrade: match route, create pair, accept server side.
225
- * separated from handleWebSocket so tests can verify the setup logic without
226
- * needing the CF runtime's special Response(101) support.
227
- *
228
- * returns null if no route matches, a Response if WebSocketPair unavailable,
229
- * or a WebSocketUpgradeResult with the pair + handler ready to invoke.
230
- */
231
- prepareWebSocketUpgrade(
232
- request: Request,
233
- url: URL
234
- ): WebSocketUpgradeResult | Response | null {
235
- const match = this.matchWsRoute(url.pathname)
236
- if (!match) {
237
- return null
238
- }
239
-
240
- // WebSocketPair is a CF Workers runtime global
241
- const WsPair = (globalThis as any).WebSocketPair
242
- if (!WsPair) {
243
- return new Response('WebSocketPair not available in this runtime', { status: 500 })
244
- }
245
-
246
- const pair = new WsPair()
247
- const [client, server] = Object.values(pair) as [WebSocket, WebSocket]
248
-
249
- // accept the server side so we can send/receive (CF Workers WebSocket extension)
250
- ;(server as any).accept()
251
-
252
- return { client, server, handler: match.handler, request, url }
253
- }
254
-
255
- /**
256
- * match a pathname against registered WebSocket routes.
257
- * checks exact matches first, then pattern matches.
258
- */
259
- matchWsRoute(pathname: string): RouteMatch | null {
260
- // exact match first
261
- const exact = this.wsRoutes.get(pathname)
262
- if (exact) {
263
- return { handler: exact, pattern: pathname }
264
- }
265
-
266
- // pattern match
267
- for (const { regex, pattern, handler } of this.wsPatterns) {
268
- if (regex.test(pathname)) {
269
- return { handler, pattern }
270
- }
271
- }
272
-
273
- return null
274
- }
275
- }
276
-
277
- /**
278
- * factory function — creates a pre-configured adapter with the standard
279
- * zero-cache WebSocket routes registered.
280
- *
281
- * handlers are placeholders that will be replaced during Phase 3 integration
282
- * when we wire up the actual zero-cache WebSocket protocol handlers.
283
- */
284
- export function createHttpServiceAdapter(): HttpServiceAdapter {
285
- const adapter = new HttpServiceAdapter()
286
-
287
- // register known zero-cache websocket route patterns.
288
- // actual handlers will be set during integration (Phase 3).
289
- // these patterns match zero-cache's replication endpoints.
290
- // for now, register the patterns so path matching works.
291
-
292
- return adapter
293
- }