Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 43 additions & 19 deletions yarn-project/kv-store/browser-stubs/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class BufferPolyfill extends Uint8Array {
toJSON() {
return {
type: 'Buffer',
data: Array.from(this)
data: Array.from(this),
};
}

Expand Down Expand Up @@ -147,7 +147,7 @@ class BufferPolyfill extends Uint8Array {
}
}

return x.length < y.length ? -1 : (x.length > y.length ? 1 : 0);
return x.length < y.length ? -1 : x.length > y.length ? 1 : 0;
}

fill(val, start, end, encoding) {
Expand All @@ -168,8 +168,7 @@ class BufferPolyfill extends Uint8Array {
}
if (val.length === 1) {
const code = val.charCodeAt(0);
if ((encoding === 'utf8' && code < 128) ||
encoding === 'latin1') {
if ((encoding === 'utf8' && code < 128) || encoding === 'latin1') {
val = code;
}
}
Expand All @@ -195,9 +194,7 @@ class BufferPolyfill extends Uint8Array {
this[i] = val;
}
} else {
const bytes = BufferPolyfill.isBuffer(val)
? val
: BufferPolyfill.from(val, encoding);
const bytes = BufferPolyfill.isBuffer(val) ? val : BufferPolyfill.from(val, encoding);
const len = bytes.length;
for (let i = 0; i < end - start; ++i) {
this[i + start] = bytes[i % len];
Expand All @@ -221,7 +218,9 @@ BufferPolyfill.from = function (value, encodingOrOffset, length) {
}

if (value == null) {
throw new TypeError('The first argument must be one of type string, Buffer, ArrayBuffer, Array, or Array-like Object');
throw new TypeError(
'The first argument must be one of type string, Buffer, ArrayBuffer, Array, or Array-like Object',
);
}

if (value instanceof ArrayBuffer) {
Expand All @@ -236,7 +235,9 @@ BufferPolyfill.from = function (value, encodingOrOffset, length) {
return copy;
}

throw new TypeError('The first argument must be one of type string, Buffer, ArrayBuffer, Array, or Array-like Object');
throw new TypeError(
'The first argument must be one of type string, Buffer, ArrayBuffer, Array, or Array-like Object',
);
};

BufferPolyfill.alloc = function (size, fill, encoding) {
Expand Down Expand Up @@ -345,11 +346,7 @@ BufferPolyfill.prototype.copy = function (target, targetStart, start, end) {
if (this === target && typeof Uint8Array.prototype.copyWithin === 'function') {
this.copyWithin(targetStart, start, end);
} else {
Uint8Array.prototype.set.call(
target,
this.subarray(start, end),
targetStart
);
Uint8Array.prototype.set.call(target, this.subarray(start, end), targetStart);
}

return len;
Expand Down Expand Up @@ -460,7 +457,12 @@ function utf8Write(buf, string, offset, length) {
}

function latin1Write(buf, string, offset, length) {
return blitBuffer(string.split('').map(c => c.charCodeAt(0)), buf, offset, length);
return blitBuffer(
string.split('').map(c => c.charCodeAt(0)),
buf,
offset,
length,
);
}

function base64Write(buf, string, offset, length) {
Expand Down Expand Up @@ -488,9 +490,28 @@ function base64ToBytes(str) {
}

const hexCharValueTable = {
48: 0, 49: 1, 50: 2, 51: 3, 52: 4, 53: 5, 54: 6, 55: 7, 56: 8, 57: 9,
65: 10, 66: 11, 67: 12, 68: 13, 69: 14, 70: 15,
97: 10, 98: 11, 99: 12, 100: 13, 101: 14, 102: 15
48: 0,
49: 1,
50: 2,
51: 3,
52: 4,
53: 5,
54: 6,
55: 7,
56: 8,
57: 9,
65: 10,
66: 11,
67: 12,
68: 13,
69: 14,
70: 15,
97: 10,
98: 11,
99: 12,
100: 13,
101: 14,
102: 15,
};

// Constants
Expand All @@ -499,9 +520,12 @@ BufferPolyfill.kMaxLength = K_MAX_LENGTH;
BufferPolyfill.kStringMaxLength = K_STRING_MAX_LENGTH;
BufferPolyfill.constants = {
MAX_LENGTH: K_MAX_LENGTH,
MAX_STRING_LENGTH: K_STRING_MAX_LENGTH
MAX_STRING_LENGTH: K_STRING_MAX_LENGTH,
};

// Also register as global for code that accesses Buffer without importing
globalThis.Buffer = BufferPolyfill;

// Export
export const Buffer = BufferPolyfill;
export const INSPECT_MAX_BYTES = BufferPolyfill.INSPECT_MAX_BYTES;
Expand Down
12 changes: 7 additions & 5 deletions yarn-project/kv-store/browser-stubs/eth-address.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// Browser stub for @aztec/foundation/eth-address
// Provides a simple EthAddress implementation without crypto dependencies

// Only used during vitest browser tests to avoid loading Barretenberg WASM.
import { z } from 'zod';

export class EthAddress {
Expand Down Expand Up @@ -37,9 +36,12 @@ export class EthAddress {
static ZERO = new EthAddress(new Uint8Array(20));

toString() {
return '0x' + Array.from(this.buffer)
.map(b => b.toString(16).padStart(2, '0'))
.join('');
return (
'0x' +
Array.from(this.buffer)
.map(b => b.toString(16).padStart(2, '0'))
.join('')
);
}

toBuffer() {
Expand Down
3 changes: 1 addition & 2 deletions yarn-project/kv-store/browser-stubs/foundation-log.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// Browser stub for @aztec/foundation/log
// Provides a logger implementation that outputs to console

// Only used during vitest browser tests to avoid loading Barretenberg WASM.
// Create a logger that returns itself for chaining
function createLogger(name, bindings) {
const prefix = name ? `[${name}]` : '';
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/kv-store/browser-stubs/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

// Custom inspect symbol
export const inspect = {
custom: Symbol.for('nodejs.util.inspect.custom')
custom: Symbol.for('nodejs.util.inspect.custom'),
};

// Other util functions as simple pass-throughs
Expand Down
12 changes: 6 additions & 6 deletions yarn-project/kv-store/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"build:dev": "../scripts/tsc.sh --watch",
"clean": "rm -rf ./dest .tsbuildinfo",
"test:node": "NODE_NO_WARNINGS=1 mocha --config ./.mocharc.json",
"test:browser": "wtr --config ./web-test-runner.config.mjs",
"test:browser": "vitest run --config ./vitest.config.ts",
"test": "yarn test:node && yarn test:browser",
"test:jest": "NODE_NO_WARNINGS=1 node --experimental-vm-modules ../node_modules/.bin/jest --passWithNoTests --maxWorkers=${JEST_MAX_WORKERS:-8}"
},
Expand Down Expand Up @@ -46,19 +46,19 @@
"@types/node": "^22.15.17",
"@types/sinon": "^17.0.3",
"@typescript/native-preview": "7.0.0-dev.20260113.1",
"@web/dev-server-esbuild": "^1.0.3",
"@web/dev-server-import-maps": "^0.2.1",
"@web/test-runner": "^0.19.0",
"@web/test-runner-playwright": "^0.11.0",
"@vitest/browser-playwright": "^4.0.0",
"buffer": "^6.0.3",
"chai": "^5.1.2",
"chai-as-promised": "^8.0.1",
"jest": "^30.0.0",
"mocha": "^10.8.2",
"mocha-each": "^2.0.1",
"playwright": "1.49.0",
"sinon": "^19.0.2",
"ts-node": "^10.9.1",
"typescript": "^5.3.3",
"util": "^0.12.5"
"util": "^0.12.5",
"vitest": "^4.0.0"
},
"files": [
"dest",
Expand Down
10 changes: 6 additions & 4 deletions yarn-project/kv-store/src/indexeddb/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,18 +192,20 @@ export class AztecIndexedDBStore implements AztecAsyncKVStore {
}

/** Deletes this store and removes the database */
delete() {
async delete() {
this.#containers.clear();
await this.#txQueue.end();
this.#rootDB.close();
return deleteDB(this.#name);
await deleteDB(this.#name);
}

estimateSize(): Promise<StoreSize> {
return Promise.resolve({ mappingSize: 0, physicalFileSize: 0, actualSize: 0, numItems: 0 });
}

close(): Promise<void> {
return Promise.resolve();
async close(): Promise<void> {
await this.#txQueue.end();
this.#rootDB.close();
}

backupTo(_dstPath: string, _compact?: boolean): Promise<void> {
Expand Down
4 changes: 0 additions & 4 deletions yarn-project/kv-store/src/stores/l2_tips_store.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ import { L2TipsKVStore } from './l2_tips_store.js';
describe('L2TipsStore', () => {
let kvStore: AztecAsyncKVStore;

beforeEach(async () => {
kvStore = await openTmpStore('test', true);
});

afterEach(async () => {
await kvStore.delete();
});
Expand Down
73 changes: 73 additions & 0 deletions yarn-project/kv-store/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { playwright } from '@vitest/browser-playwright';
import { existsSync } from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
import { defineConfig } from 'vitest/config';

const __dirname = path.dirname(fileURLToPath(import.meta.url));

// Use pre-installed playwright browsers if available (e.g., in CI)
const systemPlaywrightPath = '/opt/ms-playwright';
if (!process.env.PLAYWRIGHT_BROWSERS_PATH && existsSync(systemPlaywrightPath)) {
process.env.PLAYWRIGHT_BROWSERS_PATH = systemPlaywrightPath;
}

export default defineConfig({
define: {
'process.env': {},
},
resolve: {
alias: {
// Browser stubs for modules that pull in Barretenberg WASM.
'@aztec/foundation/eth-address': path.resolve(__dirname, 'browser-stubs/eth-address.js'),
'@aztec/foundation/log': path.resolve(__dirname, 'browser-stubs/foundation-log.js'),
buffer: path.resolve(__dirname, 'browser-stubs/buffer.js'),
util: path.resolve(__dirname, 'browser-stubs/util.js'),
},
},
optimizeDeps: {
include: [
'chai',
'zod',
'idb',
'sha3',
'viem',
'ohash',
'hash.js',
'@noble/curves/secp256k1',
'colorette',
'detect-node',
'pino',
'msgpackr',
'pako',
'idb-keyval',
'comlink',
],
},
test: {
globals: true,
reporters: ['verbose'],
include: ['./src/indexeddb/**/*.test.ts'],
// Run test files sequentially to avoid race conditions in browser module loading
fileParallelism: false,
browser: {
enabled: true,
provider: playwright(),
headless: true,
instances: [
{
browser: 'chromium',
launch: {
args: ['--no-sandbox', '--disable-setuid-sandbox', '--disable-dev-shm-usage'],
},
context: {
actionTimeout: 10_000,
},
},
],
},
testTimeout: 30000,
teardownTimeout: 10000,
globalSetup: './vitest.global-setup.ts',
},
});
14 changes: 14 additions & 0 deletions yarn-project/kv-store/vitest.global-setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Global setup for vitest - runs before browser is launched
export async function setup() {
console.log('[global-setup] Starting global setup...');
console.log('[global-setup] Node version:', process.version);
console.log('[global-setup] Platform:', process.platform);
console.log('[global-setup] CI:', process.env.CI);
console.log('[global-setup] PLAYWRIGHT_BROWSERS_PATH:', process.env.PLAYWRIGHT_BROWSERS_PATH);
console.log('[global-setup] Global setup complete, browser should launch next...');
}

export async function teardown() {
console.log('[global-teardown] Global teardown starting...');
console.log('[global-teardown] Global teardown complete');
}
Loading
Loading