Skip to content

Commit b64522f

Browse files
mverzilliclaude
andauthored
feat: package sqlite kv-store backend for stricter browser envs (#23089)
While the current sqlite backend for kv-store works fine in vanilla browser environments, browser policies can be stricter for extensions. For example, Chromium's MV3 doesn't accept extensions using `eval` or freely instantiating `Function`'s. This PR deals with said issues so that eventual wallet implementers don't have to. --------- Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent d6ca8c5 commit b64522f

9 files changed

Lines changed: 553 additions & 8 deletions

File tree

yarn-project/kv-store/browser-stubs/buffer.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,21 @@ BufferPolyfill.from = function (value, encodingOrOffset, length) {
240240
);
241241
};
242242

243+
BufferPolyfill.compare = function (a, b) {
244+
if (!ArrayBuffer.isView(a) || !ArrayBuffer.isView(b)) {
245+
throw new TypeError('Arguments must be Buffers or Uint8Arrays');
246+
}
247+
const x = new Uint8Array(a.buffer, a.byteOffset, a.byteLength);
248+
const y = new Uint8Array(b.buffer, b.byteOffset, b.byteLength);
249+
const len = Math.min(x.length, y.length);
250+
for (let i = 0; i < len; i++) {
251+
if (x[i] !== y[i]) {
252+
return x[i] < y[i] ? -1 : 1;
253+
}
254+
}
255+
return x.length < y.length ? -1 : x.length > y.length ? 1 : 0;
256+
};
257+
243258
BufferPolyfill.alloc = function (size, fill, encoding) {
244259
const buf = new BufferPolyfill(size);
245260
if (fill !== undefined) {

yarn-project/kv-store/package.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,16 @@
1111
"./sqlite-opfs": "./dest/sqlite-opfs/index.js",
1212
"./stores": "./dest/stores/index.js"
1313
},
14+
"imports": {
15+
"#msgpackr": {
16+
"browser": "msgpackr/index-no-eval",
17+
"default": "msgpackr"
18+
},
19+
"#ordered-binary": {
20+
"browser": "./dest/sqlite-opfs/internal/ordered-binary-browser.js",
21+
"default": "ordered-binary"
22+
}
23+
},
1424
"scripts": {
1525
"build": "yarn clean && ../scripts/tsc.sh",
1626
"build:dev": "../scripts/tsc.sh --watch",

yarn-project/kv-store/src/sqlite-opfs/array.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { Encoder } from 'msgpackr';
1+
import { Encoder } from '#msgpackr';
2+
import { toBufferKey } from '#ordered-binary';
23
import { hash } from 'ohash';
3-
import { toBufferKey } from 'ordered-binary';
44

55
import type { AztecAsyncArray } from '../interfaces/array.js';
66
import type { Value } from '../interfaces/common.js';

0 commit comments

Comments
 (0)