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