orez 0.2.27 → 0.2.29

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 (150) hide show
  1. package/package.json +3 -4
  2. package/src/admin/admin-data.test.ts +0 -348
  3. package/src/admin/http-proxy.ts +0 -252
  4. package/src/admin/log-store.ts +0 -192
  5. package/src/admin/server.ts +0 -471
  6. package/src/admin/ui.ts +0 -1322
  7. package/src/bench/proxy-throughput.bench.ts +0 -343
  8. package/src/bench/serial-mutations.bench.ts +0 -270
  9. package/src/browser.ts +0 -203
  10. package/src/cf-do/.wrangler/cache/cf.json +0 -1
  11. package/src/cf-do/.wrangler/state/v3/cache/miniflare-CacheObject/metadata.sqlite +0 -0
  12. package/src/cf-do/.wrangler/state/v3/cache/miniflare-CacheObject/metadata.sqlite-shm +0 -0
  13. package/src/cf-do/.wrangler/state/v3/cache/miniflare-CacheObject/metadata.sqlite-wal +0 -0
  14. package/src/cf-do/.wrangler/state/v3/do/zero-do-ZeroDO/0ffaabee41a60e04dd0eb7db3073f0a40139e6a97ccd26823967acb652b89a7b.sqlite +0 -0
  15. package/src/cf-do/.wrangler/state/v3/do/zero-do-ZeroDO/metadata.sqlite +0 -0
  16. package/src/cf-do/.wrangler/state/v3/do/zero-do-ZeroDO/metadata.sqlite-shm +0 -0
  17. package/src/cf-do/.wrangler/state/v3/do/zero-do-ZeroDO/metadata.sqlite-wal +0 -0
  18. package/src/cf-do/.wrangler/tmp/bundle-0z4CpE/middleware-insertion-facade.js +0 -11
  19. package/src/cf-do/.wrangler/tmp/bundle-0z4CpE/middleware-loader.entry.ts +0 -134
  20. package/src/cf-do/.wrangler/tmp/bundle-vYmw0E/middleware-insertion-facade.js +0 -11
  21. package/src/cf-do/.wrangler/tmp/bundle-vYmw0E/middleware-loader.entry.ts +0 -134
  22. package/src/cf-do/.wrangler/tmp/dev-cbILNo/worker.js +0 -1059
  23. package/src/cf-do/.wrangler/tmp/dev-cbILNo/worker.js.map +0 -8
  24. package/src/cf-do/.wrangler/tmp/dev-qbho19/worker.js +0 -1059
  25. package/src/cf-do/.wrangler/tmp/dev-qbho19/worker.js.map +0 -8
  26. package/src/cf-do/ARCHITECTURE.md +0 -93
  27. package/src/cf-do/CHAT_E2E.md +0 -213
  28. package/src/cf-do/watermark.test.ts +0 -103
  29. package/src/cf-do/watermark.ts +0 -118
  30. package/src/cf-do/worker.ts +0 -1041
  31. package/src/cf-do/wrangler.toml +0 -11
  32. package/src/cf-pglite/README.md +0 -19
  33. package/src/change-tracking.ts +0 -25
  34. package/src/child-process.test.ts +0 -147
  35. package/src/child-process.ts +0 -90
  36. package/src/cli-entry.ts +0 -72
  37. package/src/cli.test.ts +0 -40
  38. package/src/cli.ts +0 -1214
  39. package/src/config.ts +0 -150
  40. package/src/do-sql-tracking.test.ts +0 -19
  41. package/src/do-sql-tracking.ts +0 -19
  42. package/src/index.ts +0 -1215
  43. package/src/integration/integration.test.ts +0 -517
  44. package/src/integration/native-binary.guard.test.ts +0 -13
  45. package/src/integration/native-startup.test.ts +0 -44
  46. package/src/integration/replication-latency.test.ts +0 -428
  47. package/src/integration/restore-live-stress.test.ts +0 -433
  48. package/src/integration/restore-reset.test.ts +0 -400
  49. package/src/integration/restore.test.ts +0 -274
  50. package/src/integration/test-permissions.ts +0 -147
  51. package/src/load-config.ts +0 -46
  52. package/src/log.ts +0 -96
  53. package/src/mutex.ts +0 -47
  54. package/src/pg-proxy-browser.singledb.test.ts +0 -233
  55. package/src/pg-proxy-browser.ts +0 -2022
  56. package/src/pg-proxy-do-backend.test.ts +0 -3890
  57. package/src/pg-proxy-do-backend.ts +0 -7191
  58. package/src/pg-proxy.ts +0 -1087
  59. package/src/pg-sqlite-compiler/README.md +0 -53
  60. package/src/pg-sqlite-compiler/catalog/seed.ts +0 -524
  61. package/src/pg-sqlite-compiler/fixtures/pgsqlite/arithmetic.json +0 -307
  62. package/src/pg-sqlite-compiler/fixtures/pgsqlite/array.json +0 -377
  63. package/src/pg-sqlite-compiler/fixtures/pgsqlite/cast.json +0 -12
  64. package/src/pg-sqlite-compiler/fixtures/pgsqlite/catalog.json +0 -447
  65. package/src/pg-sqlite-compiler/fixtures/pgsqlite/create-table.json +0 -32
  66. package/src/pg-sqlite-compiler/fixtures/pgsqlite/datetime.json +0 -397
  67. package/src/pg-sqlite-compiler/fixtures/pgsqlite/enum.json +0 -337
  68. package/src/pg-sqlite-compiler/fixtures/pgsqlite/insert.json +0 -337
  69. package/src/pg-sqlite-compiler/fixtures/pgsqlite/json.json +0 -537
  70. package/src/pg-sqlite-compiler/fixtures/pgsqlite/misc.json +0 -1837
  71. package/src/pg-sqlite-compiler/index.ts +0 -73
  72. package/src/pg-sqlite-compiler/integration.test.ts +0 -136
  73. package/src/pg-sqlite-compiler/passes/ast-utils.ts +0 -113
  74. package/src/pg-sqlite-compiler/passes/catalog.ts +0 -65
  75. package/src/pg-sqlite-compiler/passes/datetime.ts +0 -74
  76. package/src/pg-sqlite-compiler/passes/index.ts +0 -49
  77. package/src/pg-sqlite-compiler/passes/types.ts +0 -156
  78. package/src/pg-sqlite-compiler/smoke.test.ts +0 -69
  79. package/src/pg-sqlite-compiler/test/catalog.test.ts +0 -171
  80. package/src/pg-sqlite-compiler/test/corpus.test.ts +0 -161
  81. package/src/pg-sqlite-compiler/test/datetime.oracle.test.ts +0 -102
  82. package/src/pg-sqlite-compiler/test/oracle.ts +0 -237
  83. package/src/pg-sqlite-compiler/test/types.test.ts +0 -109
  84. package/src/pg-sqlite-compiler/types.ts +0 -63
  85. package/src/pglite-ipc.test.ts +0 -116
  86. package/src/pglite-ipc.ts +0 -266
  87. package/src/pglite-manager.ts +0 -557
  88. package/src/pglite-web-proxy.test.ts +0 -57
  89. package/src/pglite-web-proxy.ts +0 -221
  90. package/src/pglite-web-worker.ts +0 -152
  91. package/src/pglite-worker-thread.ts +0 -253
  92. package/src/port.ts +0 -25
  93. package/src/process-title.ts +0 -9
  94. package/src/recovery.ts +0 -155
  95. package/src/replication/change-tracker.test.ts +0 -357
  96. package/src/replication/change-tracker.ts +0 -279
  97. package/src/replication/handler.test.ts +0 -511
  98. package/src/replication/handler.ts +0 -1190
  99. package/src/replication/pgoutput-encoder.test.ts +0 -697
  100. package/src/replication/pgoutput-encoder.ts +0 -373
  101. package/src/replication/tcp-replication.test.ts +0 -876
  102. package/src/replication/zero-compat.test.ts +0 -1150
  103. package/src/restore-stress.test.ts +0 -188
  104. package/src/s3-local.ts +0 -203
  105. package/src/shim/hooks.mjs +0 -120
  106. package/src/shim/register.mjs +0 -4
  107. package/src/sqlite-mode/apply-mode.ts +0 -224
  108. package/src/sqlite-mode/index.ts +0 -15
  109. package/src/sqlite-mode/native-binary.ts +0 -89
  110. package/src/sqlite-mode/package-resolve.ts +0 -17
  111. package/src/sqlite-mode/resolve-mode.ts +0 -80
  112. package/src/sqlite-mode/shim-template.ts +0 -159
  113. package/src/sqlite-mode/sqlite-mode.test.ts +0 -427
  114. package/src/sqlite-mode/types.ts +0 -30
  115. package/src/vite-plugin.ts +0 -67
  116. package/src/wasm-sqlite.test.ts +0 -537
  117. package/src/worker/browser-admin.ts +0 -52
  118. package/src/worker/browser-build-config.test.ts +0 -71
  119. package/src/worker/browser-build-config.ts +0 -109
  120. package/src/worker/browser-embed-admin.test.ts +0 -75
  121. package/src/worker/browser-embed.ts +0 -345
  122. package/src/worker/cf-patches.ts +0 -384
  123. package/src/worker/embed-integration.test.ts +0 -321
  124. package/src/worker/index.ts +0 -138
  125. package/src/worker/shims/fastify.test.ts +0 -255
  126. package/src/worker/shims/fastify.ts +0 -306
  127. package/src/worker/shims/http-service.test.ts +0 -355
  128. package/src/worker/shims/http-service.ts +0 -293
  129. package/src/worker/shims/node-stub.ts +0 -290
  130. package/src/worker/shims/oxfmt.ts +0 -3
  131. package/src/worker/shims/postgres-browser.ts +0 -59
  132. package/src/worker/shims/postgres-socket.test.ts +0 -576
  133. package/src/worker/shims/postgres-socket.ts +0 -310
  134. package/src/worker/shims/postgres.test.ts +0 -364
  135. package/src/worker/shims/postgres.ts +0 -1454
  136. package/src/worker/shims/sqlite-browser.test.ts +0 -233
  137. package/src/worker/shims/sqlite-browser.ts +0 -175
  138. package/src/worker/shims/sqlite.test.ts +0 -786
  139. package/src/worker/shims/sqlite.ts +0 -978
  140. package/src/worker/shims/stream-browser.ts +0 -15
  141. package/src/worker/shims/ws-browser.test.ts +0 -205
  142. package/src/worker/shims/ws-browser.ts +0 -248
  143. package/src/worker/shims/ws.test.ts +0 -288
  144. package/src/worker/shims/ws.ts +0 -467
  145. package/src/worker/shims/zero-process-env.ts +0 -11
  146. package/src/worker/types.ts +0 -75
  147. package/src/worker/worker-integration.test.ts +0 -223
  148. package/src/worker/worker.test.ts +0 -136
  149. package/src/worker/zero-cache-embed-cf.ts +0 -463
  150. 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
- }