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,427 +0,0 @@
1
- /**
2
- * sqlite-mode tests - mode resolution, shim generation, and mode transitions
3
- */
4
-
5
- import { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs'
6
- import { tmpdir } from 'node:os'
7
- import { join } from 'node:path'
8
-
9
- import { afterEach, beforeEach, describe, expect, it } from 'vitest'
10
-
11
- import {
12
- applySqliteMode,
13
- backupOriginal,
14
- cleanupShim,
15
- getShimMode,
16
- hasBackup,
17
- restoreOriginal,
18
- } from './apply-mode.js'
19
- import { resolveSqliteMode, resolveSqliteModeConfig } from './resolve-mode.js'
20
- import { generateCjsShim, generateEsmShim } from './shim-template.js'
21
- import { BACKUP_MARKER, JOURNAL_MODE } from './types.js'
22
-
23
- describe('sqlite mode types', () => {
24
- it('journal_mode is wal2 for both modes (required by zero-cache)', () => {
25
- expect(JOURNAL_MODE.native).toBe('wal2')
26
- expect(JOURNAL_MODE.wasm).toBe('wal2')
27
- })
28
- })
29
-
30
- describe('sqlite mode resolution', () => {
31
- it('resolves native mode when disableWasmSqlite is true', () => {
32
- expect(resolveSqliteMode(true, false)).toBe('native')
33
- })
34
-
35
- it('resolves wasm mode when forceWasmSqlite is true', () => {
36
- expect(resolveSqliteMode(false, true)).toBe('wasm')
37
- })
38
-
39
- it('auto-detects mode based on native binary availability', () => {
40
- // when neither flag is set, mode depends on whether native binary exists
41
- const mode = resolveSqliteMode(false, false)
42
- expect(['wasm', 'native']).toContain(mode)
43
- })
44
-
45
- it('returns config with mode for native', () => {
46
- const config = resolveSqliteModeConfig(true, false)
47
- expect(config).not.toBeNull()
48
- expect(config?.mode).toBe('native')
49
- })
50
- })
51
-
52
- describe('shim template generation', () => {
53
- it('generates cjs shim with correct journal_mode for wasm', () => {
54
- const shim = generateCjsShim({
55
- mode: 'wasm',
56
- bedrockPath: '/path/to/bedrock',
57
- })
58
- expect(shim).toContain("db.pragma('journal_mode = wal2')")
59
- expect(shim).toContain('// mode: wasm')
60
- expect(shim).toContain('orez sqlite shim')
61
- })
62
-
63
- it('generates cjs shim with correct journal_mode for native', () => {
64
- const shim = generateCjsShim({
65
- mode: 'native',
66
- bedrockPath: '/path/to/bedrock',
67
- })
68
- expect(shim).toContain("db.pragma('journal_mode = wal2')")
69
- expect(shim).toContain('// mode: native')
70
- })
71
-
72
- it('generates esm shim with correct journal_mode for wasm', () => {
73
- const shim = generateEsmShim({
74
- mode: 'wasm',
75
- bedrockPath: '/path/to/bedrock',
76
- })
77
- expect(shim).toContain("db.pragma('journal_mode = wal2')")
78
- expect(shim).toContain('// mode: wasm')
79
- })
80
-
81
- it('includes common pragmas in both shim types', () => {
82
- const cjs = generateCjsShim({ mode: 'wasm', bedrockPath: '/path' })
83
- const esm = generateEsmShim({ mode: 'wasm', bedrockPath: '/path' })
84
-
85
- for (const shim of [cjs, esm]) {
86
- expect(shim).toContain("db.pragma('busy_timeout = 30000')")
87
- expect(shim).toContain("db.pragma('synchronous = off')")
88
- }
89
- })
90
-
91
- it('includes api polyfills in generated shims', () => {
92
- const shim = generateCjsShim({ mode: 'wasm', bedrockPath: '/path' })
93
-
94
- expect(shim).toContain('Database.prototype.unsafeMode')
95
- expect(shim).toContain('Database.prototype.defaultSafeIntegers')
96
- expect(shim).toContain('Database.prototype.serialize')
97
- expect(shim).toContain('Database.prototype.backup')
98
- expect(shim).toContain('SP.scanStatus')
99
- expect(shim).toContain('SP.scanStatusV2')
100
- expect(shim).toContain('SQLITE_SCANSTAT_NLOOP')
101
- })
102
-
103
- it('includes pragma wrapper to skip optimize', () => {
104
- const shim = generateCjsShim({ mode: 'wasm', bedrockPath: '/path' })
105
- expect(shim).toContain("str.trim().toLowerCase().startsWith('optimize')")
106
- })
107
-
108
- it('includes tracing when enabled', () => {
109
- const withTracing = generateCjsShim({
110
- mode: 'wasm',
111
- bedrockPath: '/path',
112
- includeTracing: true,
113
- })
114
- const withoutTracing = generateCjsShim({
115
- mode: 'wasm',
116
- bedrockPath: '/path',
117
- includeTracing: false,
118
- })
119
-
120
- expect(withTracing).toContain('_zero.changeLog')
121
- expect(withoutTracing).not.toContain('_zero.changeLog')
122
- })
123
- })
124
-
125
- describe('shim backup/restore lifecycle', () => {
126
- let testDir: string
127
- let mockPackageDir: string
128
- let mockIndexPath: string
129
-
130
- const originalContent = `'use strict';
131
- // original @rocicorp/zero-sqlite3 content
132
- module.exports = require('better-sqlite3');
133
- `
134
-
135
- beforeEach(() => {
136
- testDir = join(
137
- tmpdir(),
138
- `orez-test-${Date.now()}-${Math.random().toString(36).slice(2)}`
139
- )
140
- mockPackageDir = join(testDir, 'node_modules', '@rocicorp', 'zero-sqlite3', 'lib')
141
- mkdirSync(mockPackageDir, { recursive: true })
142
- mockIndexPath = join(mockPackageDir, 'index.js')
143
-
144
- // create mock package.json
145
- writeFileSync(
146
- join(testDir, 'node_modules', '@rocicorp', 'zero-sqlite3', 'package.json'),
147
- JSON.stringify({ name: '@rocicorp/zero-sqlite3', main: 'lib/index.js' })
148
- )
149
-
150
- // create original index.js
151
- writeFileSync(mockIndexPath, originalContent)
152
- })
153
-
154
- afterEach(() => {
155
- rmSync(testDir, { recursive: true, force: true })
156
- })
157
-
158
- it('backups original file before shimming', () => {
159
- expect(hasBackup(mockIndexPath)).toBe(false)
160
- expect(backupOriginal(mockIndexPath)).toBe(true)
161
- expect(hasBackup(mockIndexPath)).toBe(true)
162
-
163
- const backupContent = readFileSync(mockIndexPath + BACKUP_MARKER, 'utf-8')
164
- expect(backupContent).toBe(originalContent)
165
- })
166
-
167
- it('does not re-backup if backup already exists', () => {
168
- backupOriginal(mockIndexPath)
169
- const backupPath = mockIndexPath + BACKUP_MARKER
170
-
171
- // modify original (simulate shim)
172
- writeFileSync(mockIndexPath, 'shimmed content')
173
-
174
- // backup should not overwrite existing backup
175
- expect(backupOriginal(mockIndexPath)).toBe(true)
176
- expect(readFileSync(backupPath, 'utf-8')).toBe(originalContent)
177
- })
178
-
179
- it('restores original from backup', () => {
180
- backupOriginal(mockIndexPath)
181
- writeFileSync(mockIndexPath, 'shimmed content')
182
-
183
- expect(restoreOriginal(mockIndexPath)).toBe(true)
184
- expect(readFileSync(mockIndexPath, 'utf-8')).toBe(originalContent)
185
- expect(hasBackup(mockIndexPath)).toBe(false)
186
- })
187
-
188
- it('returns false when no backup to restore', () => {
189
- expect(restoreOriginal(mockIndexPath)).toBe(false)
190
- })
191
-
192
- it('getShimMode returns null for unshimmed file', () => {
193
- expect(getShimMode(mockIndexPath)).toBeNull()
194
- })
195
-
196
- it('getShimMode returns mode for shimmed file', () => {
197
- const shimContent = generateCjsShim({
198
- mode: 'wasm',
199
- bedrockPath: '/path/to/bedrock',
200
- })
201
- writeFileSync(mockIndexPath, shimContent)
202
-
203
- expect(getShimMode(mockIndexPath)).toBe('wasm')
204
- })
205
-
206
- it('cleanupShim restores original if backup exists', () => {
207
- backupOriginal(mockIndexPath)
208
- writeFileSync(mockIndexPath, 'shimmed')
209
-
210
- cleanupShim(
211
- join(testDir, 'node_modules', '@rocicorp', 'zero-sqlite3', 'lib', 'index.js')
212
- )
213
-
214
- expect(readFileSync(mockIndexPath, 'utf-8')).toBe(originalContent)
215
- })
216
- })
217
-
218
- describe('mode transitions', () => {
219
- let testDir: string
220
- let mockPackageDir: string
221
- let mockIndexPath: string
222
-
223
- const originalContent = `'use strict';
224
- // original @rocicorp/zero-sqlite3
225
- module.exports = require('better-sqlite3');
226
- `
227
-
228
- beforeEach(() => {
229
- testDir = join(
230
- tmpdir(),
231
- `orez-test-${Date.now()}-${Math.random().toString(36).slice(2)}`
232
- )
233
- mockPackageDir = join(testDir, 'node_modules', '@rocicorp', 'zero-sqlite3', 'lib')
234
- mkdirSync(mockPackageDir, { recursive: true })
235
- mockIndexPath = join(mockPackageDir, 'index.js')
236
-
237
- writeFileSync(
238
- join(testDir, 'node_modules', '@rocicorp', 'zero-sqlite3', 'package.json'),
239
- JSON.stringify({ name: '@rocicorp/zero-sqlite3', main: 'lib/index.js' })
240
- )
241
- writeFileSync(mockIndexPath, originalContent)
242
- })
243
-
244
- afterEach(() => {
245
- rmSync(testDir, { recursive: true, force: true })
246
- })
247
-
248
- it('native -> wasm -> native preserves original', () => {
249
- // start in native mode (no shim)
250
- expect(getShimMode(mockIndexPath)).toBeNull()
251
-
252
- // switch to wasm mode
253
- const wasmResult = applySqliteMode({
254
- mode: 'wasm',
255
- bedrockPath: '/path/to/bedrock',
256
- zeroSqlitePath: mockIndexPath,
257
- })
258
- expect(wasmResult.success).toBe(true)
259
- expect(getShimMode(mockIndexPath)).toBe('wasm')
260
- expect(hasBackup(mockIndexPath)).toBe(true)
261
-
262
- // switch back to native mode
263
- const nativeResult = applySqliteMode({
264
- mode: 'native',
265
- zeroSqlitePath: mockIndexPath,
266
- })
267
- expect(nativeResult.success).toBe(true)
268
- expect(getShimMode(mockIndexPath)).toBeNull()
269
- expect(readFileSync(mockIndexPath, 'utf-8')).toBe(originalContent)
270
- expect(hasBackup(mockIndexPath)).toBe(false)
271
- })
272
-
273
- it('wasm -> native -> wasm works correctly', () => {
274
- // start in wasm mode
275
- applySqliteMode({
276
- mode: 'wasm',
277
- bedrockPath: '/path/to/bedrock',
278
- zeroSqlitePath: mockIndexPath,
279
- })
280
- expect(getShimMode(mockIndexPath)).toBe('wasm')
281
- const wasmContent = readFileSync(mockIndexPath, 'utf-8')
282
-
283
- // switch to native
284
- applySqliteMode({
285
- mode: 'native',
286
- zeroSqlitePath: mockIndexPath,
287
- })
288
- expect(getShimMode(mockIndexPath)).toBeNull()
289
-
290
- // switch back to wasm
291
- applySqliteMode({
292
- mode: 'wasm',
293
- bedrockPath: '/path/to/bedrock',
294
- zeroSqlitePath: mockIndexPath,
295
- })
296
- expect(getShimMode(mockIndexPath)).toBe('wasm')
297
-
298
- // shim should have same journal_mode
299
- const newWasmContent = readFileSync(mockIndexPath, 'utf-8')
300
- expect(newWasmContent).toContain('journal_mode = wal2')
301
- })
302
-
303
- it('multiple wasm applies are idempotent', () => {
304
- applySqliteMode({
305
- mode: 'wasm',
306
- bedrockPath: '/path/to/bedrock',
307
- zeroSqlitePath: mockIndexPath,
308
- })
309
- const firstShim = readFileSync(mockIndexPath, 'utf-8')
310
-
311
- // apply again
312
- applySqliteMode({
313
- mode: 'wasm',
314
- bedrockPath: '/path/to/bedrock',
315
- zeroSqlitePath: mockIndexPath,
316
- })
317
- const secondShim = readFileSync(mockIndexPath, 'utf-8')
318
-
319
- expect(firstShim).toBe(secondShim)
320
- expect(hasBackup(mockIndexPath)).toBe(true)
321
- })
322
-
323
- it('does not backup shimmed content', () => {
324
- // apply wasm shim
325
- applySqliteMode({
326
- mode: 'wasm',
327
- bedrockPath: '/path/to/bedrock',
328
- zeroSqlitePath: mockIndexPath,
329
- })
330
-
331
- // manually delete backup to simulate corruption
332
- const backupPath = mockIndexPath + BACKUP_MARKER
333
- rmSync(backupPath)
334
-
335
- // try to backup again - should fail because file is shimmed
336
- expect(backupOriginal(mockIndexPath)).toBe(false)
337
- })
338
-
339
- it('wasm re-apply is idempotent even without backup', () => {
340
- // apply wasm shim first
341
- applySqliteMode({
342
- mode: 'wasm',
343
- bedrockPath: '/path/to/bedrock',
344
- zeroSqlitePath: mockIndexPath,
345
- })
346
-
347
- // delete backup to simulate corruption
348
- const backupPath = mockIndexPath + BACKUP_MARKER
349
- rmSync(backupPath)
350
-
351
- // re-applying same wasm mode should succeed (idempotent)
352
- const result = applySqliteMode({
353
- mode: 'wasm',
354
- bedrockPath: '/path/to/bedrock',
355
- zeroSqlitePath: mockIndexPath,
356
- })
357
- expect(result.success).toBe(true)
358
- })
359
-
360
- it('wasm shim fails on unshimmed file if backup cannot be created', () => {
361
- // manually write a shimmed file without backup (simulates external corruption)
362
- const shimContent = generateCjsShim({ mode: 'native', bedrockPath: '/path' })
363
- writeFileSync(mockIndexPath, shimContent)
364
-
365
- // try to apply wasm shim - should fail because file is shimmed but no backup
366
- const result = applySqliteMode({
367
- mode: 'wasm',
368
- bedrockPath: '/path/to/bedrock',
369
- zeroSqlitePath: mockIndexPath,
370
- })
371
- expect(result.success).toBe(false)
372
- expect(result.error).toContain('no backup')
373
- })
374
-
375
- it('native mode fails if shimmed with no backup', () => {
376
- // apply wasm shim first
377
- applySqliteMode({
378
- mode: 'wasm',
379
- bedrockPath: '/path/to/bedrock',
380
- zeroSqlitePath: mockIndexPath,
381
- })
382
-
383
- // delete backup to simulate corruption
384
- const backupPath = mockIndexPath + BACKUP_MARKER
385
- rmSync(backupPath)
386
-
387
- // try to restore native - should fail
388
- const result = applySqliteMode({
389
- mode: 'native',
390
- zeroSqlitePath: mockIndexPath,
391
- })
392
- expect(result.success).toBe(false)
393
- expect(result.error).toContain('no backup')
394
- })
395
- })
396
-
397
- describe('shim contract tests', () => {
398
- it('wasm shim sets journal_mode = wal2', () => {
399
- const shim = generateCjsShim({ mode: 'wasm', bedrockPath: '/path' })
400
- expect(shim).toContain("db.pragma('journal_mode = wal2')")
401
- })
402
-
403
- it('native shim sets journal_mode = wal2', () => {
404
- const shim = generateCjsShim({ mode: 'native', bedrockPath: '/path' })
405
- expect(shim).toContain("db.pragma('journal_mode = wal2')")
406
- })
407
-
408
- it('both modes set busy_timeout and synchronous', () => {
409
- for (const mode of ['wasm', 'native'] as const) {
410
- const shim = generateCjsShim({ mode, bedrockPath: '/path' })
411
- expect(shim).toContain("db.pragma('busy_timeout = 30000')")
412
- expect(shim).toContain("db.pragma('synchronous = off')")
413
- }
414
- })
415
-
416
- it('shim exports Database and SqliteError', () => {
417
- const shim = generateCjsShim({ mode: 'wasm', bedrockPath: '/path' })
418
- expect(shim).toContain('module.exports = Database')
419
- expect(shim).toContain('module.exports.SqliteError = SqliteError')
420
- })
421
-
422
- it('esm shim exports default and named SqliteError', () => {
423
- const shim = generateEsmShim({ mode: 'wasm', bedrockPath: '/path' })
424
- expect(shim).toContain('export default Database')
425
- expect(shim).toContain('export { SqliteError }')
426
- })
427
- })
@@ -1,30 +0,0 @@
1
- /**
2
- * sqlite mode types and constants
3
- */
4
-
5
- export type SqliteMode = 'native' | 'wasm'
6
-
7
- export interface SqliteModeConfig {
8
- mode: SqliteMode
9
- // path to bedrock-sqlite for wasm mode
10
- bedrockPath?: string
11
- // path to @rocicorp/zero-sqlite3 package
12
- zeroSqlitePath?: string
13
- }
14
-
15
- // journal mode - zero-cache requires wal2 for replica sync (BEGIN CONCURRENT)
16
- // both modes use wal2 now - bedrock-sqlite wasm should support it
17
- export const JOURNAL_MODE: Record<SqliteMode, string> = {
18
- native: 'wal2',
19
- wasm: 'wal2',
20
- }
21
-
22
- // common pragmas shared by both modes
23
- // synchronous=off skips fsync — safe for a dev tool, big perf win
24
- export const COMMON_PRAGMAS = {
25
- busy_timeout: '30000',
26
- synchronous: 'off',
27
- }
28
-
29
- // backup file marker for identifying orez-shimmed packages
30
- export const BACKUP_MARKER = '.orez-backup'
@@ -1,67 +0,0 @@
1
- import { startZeroLite } from './index.js'
2
-
3
- import type { Hook, ZeroLiteConfig } from './config.js'
4
- import type { Server } from 'node:http'
5
- import type { Plugin } from 'vite'
6
-
7
- export interface OrezPluginOptions extends Partial<
8
- Omit<ZeroLiteConfig, 'onDbReady' | 'onHealthy'>
9
- > {
10
- s3?: boolean
11
- s3Port?: number
12
- // lifecycle hooks - callback functions (preferred for vite) or shell commands
13
- onDbReady?: Hook
14
- onHealthy?: Hook
15
- }
16
-
17
- export function orezPlugin(options?: OrezPluginOptions): Plugin {
18
- let stop: (() => Promise<void>) | null = null
19
- let s3Server: Server | null = null
20
- let adminServer: Server | null = null
21
-
22
- return {
23
- name: 'orez',
24
-
25
- async configureServer(server) {
26
- const result = await startZeroLite(options)
27
- stop = result.stop
28
-
29
- // start admin dashboard if adminPort is configured
30
- if (result.config.adminPort > 0 && result.logStore) {
31
- const { startAdminServer } = await import('./admin/server.js')
32
- adminServer = await startAdminServer({
33
- port: result.config.adminPort,
34
- logStore: result.logStore,
35
- httpLog: result.httpLog,
36
- config: result.config,
37
- zeroEnv: result.zeroEnv || {},
38
- actions: {
39
- restartZero: result.restartZero,
40
- stopZero: result.stopZero,
41
- resetZero: result.resetZero,
42
- resetZeroFull: result.resetZeroFull,
43
- },
44
- startTime: Date.now(),
45
- db: result.instances,
46
- })
47
- }
48
-
49
- if (options?.s3) {
50
- const { startS3Local } = await import('./s3-local.js')
51
- s3Server = await startS3Local({
52
- port: options.s3Port || 9200,
53
- dataDir: result.config.dataDir,
54
- })
55
- }
56
-
57
- server.httpServer?.on('close', async () => {
58
- adminServer?.close()
59
- s3Server?.close()
60
- if (stop) {
61
- await stop()
62
- stop = null
63
- }
64
- })
65
- },
66
- }
67
- }