Commit e3580b2
committed
fix(stack): address review — native FFI leak, Result plumbing, batch bugs
Review found 15 issues. This commit takes the source and test ones.
CRITICAL (1/15) — the entry pulled in the NATIVE protect-ffi.
`@/encryption/helpers/error-code` value-imports `ProtectError` for an
`instanceof` narrow, and protect-ffi is not in tsup `noExternal`, so
`dist/wasm-inline.js` carried a bare `@cipherstash/protect-ffi` import —
the NAPI entry, in the one bundle that exists to avoid it. On Workers /
Edge the non-`node` condition resolves it to a module exporting no
`ProtectError`; under Deno it resolves to the NAPI loader.
I had previously dismissed this as pre-existing. That was wrong: I ran
`git stash -u` on a clean tree, so it stashed nothing and I rebuilt my
own branch and compared it against itself. Re-verified properly by
swapping in main's `wasm-inline.ts` and rebuilding: main emits only the
`/wasm-inline` specifier, mine emitted both. Replaced with a structural
`readErrorCode` — which is the only thing that could ever work here
anyway, since the WASM build ships no error class for `instanceof` to
match. GH #744 is filed on my faulty premise and needs correcting.
New `wasm-inline-bundle-isolation.test.ts` asserts the built bundle's
external imports, so this class of regression fails CI. Verified it
catches the exact regression by reintroducing it (2 of 3 assertions
fail), rather than assuming.
3/15 — `failure.code` could never be populated: `withResult` runs
`onException` first, so the mapper only ever saw a fresh Error. `toError`
now carries a structural `code` onto the synthesized Error, and
`bulkDecrypt` puts each item's FFI code in its per-index message (a batch
has no single code, but dropping it lost the only machine-readable part).
9/15 — `toError`'s `String(ex)` fallback could itself throw on a
null-prototype object, escaping the Result contract entirely and
rejecting the call. Guarded with `safeString`.
10/15 — sparse inputs. `items.map(() => null)` SKIPS holes, so a hole
came back `undefined` rather than the documented `null` while lengths
still matched, which the length guard cannot see. Now `Array.from`.
15/15 — the positional-batch scaffolding was hand-rolled three times, so
a fourth batch method could omit `assertBatchLength` with no build
failure. One `runBatch` helper owns compaction, short-circuit, length
assert and scatter; the sparse fix lives there once.
5/15, 6/15, 7/15 — docs and surface: removed a duplicate stale `@returns`,
migrated the module-header and all five `encryptQuery` examples off the
pre-Result contract (they interpolated the envelope straight into SQL),
corrected the pending `.changeset/wasm-encrypt-query.md` sentence that
still said errors throw, and exported `EncryptionError` /
`EncryptionErrorTypes` so an edge consumer can discriminate
`failure.type` from the same import.
`WasmResult<T>` is now declared locally rather than re-exporting
`@byteslice/result`'s. Re-exporting put that specifier in the emitted
`.d.ts`, which Deno cannot resolve — the same reason `WasmPlaintext`
re-declares protect-ffi's `JsPlaintext`.
14/15 — test gaps: falsy-but-live values (`0` / `''` / `false`) per batch
method, the sparse-hole case, `encryptQueryBulk`'s previously untested
length guard, and the two stale "throws" titles.
840 tests pass. Zero new tsc errors, diffed against main's error set.
0 biome errors. Bundle verified free of the native FFI.1 parent 93a83a3 commit e3580b2
8 files changed
Lines changed: 577 additions & 173 deletions
File tree
- .changeset
- e2e/wasm
- packages/stack
- __tests__
- helpers
- integration/wasm
- src
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
14 | 14 | | |
15 | 15 | | |
16 | 16 | | |
17 | | - | |
18 | | - | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
72 | 72 | | |
73 | 73 | | |
74 | 74 | | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
75 | 95 | | |
76 | 96 | | |
77 | 97 | | |
| |||
119 | 139 | | |
120 | 140 | | |
121 | 141 | | |
122 | | - | |
123 | | - | |
124 | | - | |
125 | | - | |
126 | | - | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
127 | 150 | | |
128 | 151 | | |
129 | 152 | | |
130 | 153 | | |
131 | 154 | | |
132 | | - | |
133 | | - | |
134 | | - | |
135 | | - | |
136 | | - | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
137 | 163 | | |
138 | 164 | | |
139 | 165 | | |
140 | 166 | | |
141 | 167 | | |
142 | | - | |
143 | | - | |
144 | | - | |
145 | | - | |
146 | | - | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
147 | 176 | | |
148 | 177 | | |
149 | 178 | | |
150 | 179 | | |
151 | 180 | | |
152 | 181 | | |
153 | | - | |
154 | | - | |
155 | | - | |
156 | | - | |
157 | | - | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
158 | 190 | | |
159 | 191 | | |
160 | 192 | | |
| |||
171 | 203 | | |
172 | 204 | | |
173 | 205 | | |
174 | | - | |
175 | | - | |
176 | | - | |
177 | | - | |
178 | | - | |
179 | | - | |
180 | | - | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
181 | 216 | | |
182 | 217 | | |
183 | 218 | | |
184 | 219 | | |
185 | 220 | | |
186 | 221 | | |
187 | 222 | | |
188 | | - | |
189 | | - | |
190 | | - | |
191 | | - | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
192 | 230 | | |
193 | 231 | | |
194 | 232 | | |
195 | 233 | | |
196 | 234 | | |
197 | | - | |
| 235 | + | |
198 | 236 | | |
199 | 237 | | |
200 | 238 | | |
| |||
225 | 263 | | |
226 | 264 | | |
227 | 265 | | |
| 266 | + | |
228 | 267 | | |
229 | 268 | | |
230 | 269 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
102 | 102 | | |
103 | 103 | | |
104 | 104 | | |
105 | | - | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
106 | 109 | | |
107 | 110 | | |
108 | 111 | | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
109 | 118 | | |
110 | 119 | | |
111 | 120 | | |
| |||
138 | 147 | | |
139 | 148 | | |
140 | 149 | | |
141 | | - | |
142 | | - | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
143 | 157 | | |
144 | 158 | | |
145 | 159 | | |
146 | 160 | | |
147 | | - | |
| 161 | + | |
148 | 162 | | |
149 | 163 | | |
150 | 164 | | |
151 | | - | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
152 | 172 | | |
153 | 173 | | |
154 | 174 | | |
| |||
158 | 178 | | |
159 | 179 | | |
160 | 180 | | |
161 | | - | |
| 181 | + | |
162 | 182 | | |
163 | 183 | | |
164 | 184 | | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
165 | 191 | | |
166 | 192 | | |
167 | 193 | | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
168 | 224 | | |
169 | 225 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
2 | | - | |
| 1 | + | |
3 | 2 | | |
4 | 3 | | |
5 | | - | |
| 4 | + | |
6 | 5 | | |
7 | | - | |
8 | | - | |
9 | | - | |
10 | | - | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
11 | 9 | | |
12 | | - | |
13 | | - | |
14 | | - | |
15 | | - | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
16 | 13 | | |
17 | 14 | | |
18 | 15 | | |
19 | 16 | | |
20 | 17 | | |
21 | | - | |
| 18 | + | |
22 | 19 | | |
23 | 20 | | |
24 | 21 | | |
25 | 22 | | |
26 | 23 | | |
27 | | - | |
| 24 | + | |
28 | 25 | | |
0 commit comments