Compare commits

..

No commits in common. "5ab3070b3a63c2e8fed0e413dea06cf03fb48f1e" and "70fca92e781696a057089bc8bc48adebdf6e017e" have entirely different histories.

91 changed files with 1679 additions and 5866 deletions

View File

@ -18,26 +18,22 @@
"exports": {
".": {
"default": "./lib/index.js"
},
"./node-sqlite3-bindings": {
"default": "./lib/node-sqlite3-impl.js"
}
},
"devDependencies": {
"@types/better-sqlite3": "^7.6.4",
"@types/node": "^20.4.1",
"ava": "^5.3.1",
"@types/node": "^18.11.17",
"ava": "^4.3.3",
"esm": "^3.2.25",
"prettier": "^2.8.8",
"rimraf": "^5.0.1",
"typescript": "^5.1.6"
"rimraf": "^3.0.2",
"typescript": "^5.1.3"
},
"dependencies": {
"tslib": "^2.6.0"
"tslib": "^2.5.3"
},
"ava": {
"failFast": true
},
"optionalDependencies": {
"better-sqlite3": "^8.4.0"
"require": [
"esm"
]
}
}

View File

@ -15,9 +15,334 @@
*/
import test from "ava";
import {
BridgeIDBCursorWithValue,
BridgeIDBDatabase,
BridgeIDBFactory,
BridgeIDBKeyRange,
BridgeIDBRequest,
BridgeIDBTransaction,
} from "./bridge-idb.js";
import {
IDBCursorDirection,
IDBCursorWithValue,
IDBDatabase,
IDBKeyRange,
IDBValidKey,
} from "./idbtypes.js";
import { MemoryBackend } from "./MemoryBackend.js";
import { BridgeIDBDatabase, BridgeIDBFactory } from "./bridge-idb.js";
import { promiseFromRequest, promiseFromTransaction } from "./idbpromutil.js";
function promiseFromRequest(request: BridgeIDBRequest): Promise<any> {
return new Promise((resolve, reject) => {
request.onsuccess = () => {
resolve(request.result);
};
request.onerror = () => {
reject(request.error);
};
});
}
function promiseFromTransaction(
transaction: BridgeIDBTransaction,
): Promise<void> {
return new Promise<void>((resolve, reject) => {
transaction.oncomplete = () => {
resolve();
};
transaction.onerror = () => {
reject();
};
});
}
test("Spec: Example 1 Part 1", async (t) => {
const backend = new MemoryBackend();
const idb = new BridgeIDBFactory(backend);
const request = idb.open("library");
request.onupgradeneeded = () => {
const db = request.result;
const store = db.createObjectStore("books", { keyPath: "isbn" });
const titleIndex = store.createIndex("by_title", "title", { unique: true });
const authorIndex = store.createIndex("by_author", "author");
// Populate with initial data.
store.put({ title: "Quarry Memories", author: "Fred", isbn: 123456 });
store.put({ title: "Water Buffaloes", author: "Fred", isbn: 234567 });
store.put({ title: "Bedrock Nights", author: "Barney", isbn: 345678 });
};
await promiseFromRequest(request);
t.pass();
});
test("Spec: Example 1 Part 2", async (t) => {
const backend = new MemoryBackend();
const idb = new BridgeIDBFactory(backend);
const request = idb.open("library");
request.onupgradeneeded = () => {
const db = request.result;
const store = db.createObjectStore("books", { keyPath: "isbn" });
const titleIndex = store.createIndex("by_title", "title", { unique: true });
const authorIndex = store.createIndex("by_author", "author");
};
const db: BridgeIDBDatabase = await promiseFromRequest(request);
t.is(db.name, "library");
const tx = db.transaction("books", "readwrite");
tx.oncomplete = () => {
console.log("oncomplete called");
};
const store = tx.objectStore("books");
store.put({ title: "Quarry Memories", author: "Fred", isbn: 123456 });
store.put({ title: "Water Buffaloes", author: "Fred", isbn: 234567 });
store.put({ title: "Bedrock Nights", author: "Barney", isbn: 345678 });
await promiseFromTransaction(tx);
t.pass();
});
test("Spec: Example 1 Part 3", async (t) => {
const backend = new MemoryBackend();
backend.enableTracing = true;
const idb = new BridgeIDBFactory(backend);
const request = idb.open("library");
request.onupgradeneeded = () => {
const db = request.result;
const store = db.createObjectStore("books", { keyPath: "isbn" });
const titleIndex = store.createIndex("by_title", "title", { unique: true });
const authorIndex = store.createIndex("by_author", "author");
};
const db: BridgeIDBDatabase = await promiseFromRequest(request);
t.is(db.name, "library");
const tx = db.transaction("books", "readwrite");
const store = tx.objectStore("books");
store.put({ title: "Bedrock Nights", author: "Barney", isbn: 345678 });
store.put({ title: "Quarry Memories", author: "Fred", isbn: 123456 });
store.put({ title: "Water Buffaloes", author: "Fred", isbn: 234567 });
await promiseFromTransaction(tx);
const tx2 = db.transaction("books", "readonly");
const store2 = tx2.objectStore("books");
var index2 = store2.index("by_title");
const request2 = index2.get("Bedrock Nights");
const result2: any = await promiseFromRequest(request2);
t.is(result2.author, "Barney");
const tx3 = db.transaction(["books"], "readonly");
const store3 = tx3.objectStore("books");
const index3 = store3.index("by_author");
const request3 = index3.openCursor(BridgeIDBKeyRange.only("Fred"));
await promiseFromRequest(request3);
let cursor: BridgeIDBCursorWithValue | null;
cursor = request3.result as BridgeIDBCursorWithValue;
t.is(cursor.value.author, "Fred");
t.is(cursor.value.isbn, 123456);
cursor.continue();
await promiseFromRequest(request3);
cursor = request3.result as BridgeIDBCursorWithValue;
t.is(cursor.value.author, "Fred");
t.is(cursor.value.isbn, 234567);
await promiseFromTransaction(tx3);
const tx4 = db.transaction("books", "readonly");
const store4 = tx4.objectStore("books");
const request4 = store4.openCursor();
await promiseFromRequest(request4);
cursor = request4.result;
if (!cursor) {
throw new Error();
}
t.is(cursor.value.isbn, 123456);
cursor.continue();
await promiseFromRequest(request4);
cursor = request4.result;
if (!cursor) {
throw new Error();
}
t.is(cursor.value.isbn, 234567);
cursor.continue();
await promiseFromRequest(request4);
cursor = request4.result;
if (!cursor) {
throw new Error();
}
t.is(cursor.value.isbn, 345678);
cursor.continue();
await promiseFromRequest(request4);
cursor = request4.result;
t.is(cursor, null);
const tx5 = db.transaction("books", "readonly");
const store5 = tx5.objectStore("books");
const index5 = store5.index("by_author");
const request5 = index5.openCursor(null, "next");
await promiseFromRequest(request5);
cursor = request5.result;
if (!cursor) {
throw new Error();
}
t.is(cursor.value.author, "Barney");
cursor.continue();
await promiseFromRequest(request5);
cursor = request5.result;
if (!cursor) {
throw new Error();
}
t.is(cursor.value.author, "Fred");
cursor.continue();
await promiseFromRequest(request5);
cursor = request5.result;
if (!cursor) {
throw new Error();
}
t.is(cursor.value.author, "Fred");
cursor.continue();
await promiseFromRequest(request5);
cursor = request5.result;
t.is(cursor, null);
const request6 = index5.openCursor(null, "nextunique");
await promiseFromRequest(request6);
cursor = request6.result;
if (!cursor) {
throw new Error();
}
t.is(cursor.value.author, "Barney");
cursor.continue();
await promiseFromRequest(request6);
cursor = request6.result;
if (!cursor) {
throw new Error();
}
t.is(cursor.value.author, "Fred");
t.is(cursor.value.isbn, 123456);
cursor.continue();
await promiseFromRequest(request6);
cursor = request6.result;
t.is(cursor, null);
const request7 = index5.openCursor(null, "prevunique");
await promiseFromRequest(request7);
cursor = request7.result;
if (!cursor) {
throw new Error();
}
t.is(cursor.value.author, "Fred");
t.is(cursor.value.isbn, 123456);
cursor.continue();
await promiseFromRequest(request7);
cursor = request7.result;
if (!cursor) {
throw new Error();
}
t.is(cursor.value.author, "Barney");
cursor.continue();
await promiseFromRequest(request7);
cursor = request7.result;
t.is(cursor, null);
db.close();
t.pass();
});
test("simple deletion", async (t) => {
const backend = new MemoryBackend();
const idb = new BridgeIDBFactory(backend);
const request = idb.open("library");
request.onupgradeneeded = () => {
const db = request.result;
const store = db.createObjectStore("books", { keyPath: "isbn" });
const titleIndex = store.createIndex("by_title", "title", { unique: true });
const authorIndex = store.createIndex("by_author", "author");
};
const db: BridgeIDBDatabase = await promiseFromRequest(request);
t.is(db.name, "library");
const tx = db.transaction("books", "readwrite");
tx.oncomplete = () => {
console.log("oncomplete called");
};
const store = tx.objectStore("books");
store.put({ title: "Quarry Memories", author: "Fred", isbn: 123456 });
store.put({ title: "Water Buffaloes", author: "Fred", isbn: 234567 });
store.put({ title: "Bedrock Nights", author: "Barney", isbn: 345678 });
await promiseFromTransaction(tx);
const tx2 = db.transaction("books", "readwrite");
const store2 = tx2.objectStore("books");
const req1 = store2.get(234567);
await promiseFromRequest(req1);
t.is(req1.readyState, "done");
t.is(req1.result.author, "Fred");
store2.delete(123456);
const req2 = store2.get(123456);
await promiseFromRequest(req2);
t.is(req2.readyState, "done");
t.is(req2.result, undefined);
const req3 = store2.get(234567);
await promiseFromRequest(req3);
t.is(req3.readyState, "done");
t.is(req3.result.author, "Fred");
await promiseFromTransaction(tx2);
t.pass();
});
test("export", async (t) => {
const backend = new MemoryBackend();
@ -61,3 +386,276 @@ test("export", async (t) => {
t.is(exportedData2.databases["library"].schema.databaseVersion, 42);
t.pass();
});
test("update with non-existent index values", async (t) => {
const backend = new MemoryBackend();
backend.enableTracing = true;
const idb = new BridgeIDBFactory(backend);
const request = idb.open("mydb");
request.onupgradeneeded = () => {
const db = request.result;
const store = db.createObjectStore("bla", { keyPath: "x" });
store.createIndex("by_y", "y");
store.createIndex("by_z", "z");
};
const db: BridgeIDBDatabase = await promiseFromRequest(request);
t.is(db.name, "mydb");
{
const tx = db.transaction("bla", "readwrite");
const store = tx.objectStore("bla");
store.put({ x: 0, y: "a", z: 42 });
const index = store.index("by_z");
const indRes = await promiseFromRequest(index.get(42));
t.is(indRes.x, 0);
const res = await promiseFromRequest(store.get(0));
t.is(res.z, 42);
await promiseFromTransaction(tx);
}
{
const tx = db.transaction("bla", "readwrite");
const store = tx.objectStore("bla");
store.put({ x: 0, y: "a" });
const res = await promiseFromRequest(store.get(0));
t.is(res.z, undefined);
await promiseFromTransaction(tx);
}
{
const tx = db.transaction("bla", "readwrite");
const store = tx.objectStore("bla");
const index = store.index("by_z");
{
const indRes = await promiseFromRequest(index.get(42));
t.is(indRes, undefined);
}
const res = await promiseFromRequest(store.get(0));
t.is(res.z, undefined);
await promiseFromTransaction(tx);
}
t.pass();
});
test("delete from unique index", async (t) => {
const backend = new MemoryBackend();
backend.enableTracing = true;
const idb = new BridgeIDBFactory(backend);
const request = idb.open("mydb");
request.onupgradeneeded = () => {
const db = request.result as IDBDatabase;
const store = db.createObjectStore("bla", { keyPath: "x" });
store.createIndex("by_yz", ["y", "z"], {
unique: true,
});
};
const db: BridgeIDBDatabase = await promiseFromRequest(request);
t.is(db.name, "mydb");
{
const tx = db.transaction("bla", "readwrite");
const store = tx.objectStore("bla");
store.put({ x: 0, y: "a", z: 42 });
const index = store.index("by_yz");
const indRes = await promiseFromRequest(index.get(["a", 42]));
t.is(indRes.x, 0);
const res = await promiseFromRequest(store.get(0));
t.is(res.z, 42);
await promiseFromTransaction(tx);
}
{
const tx = db.transaction("bla", "readwrite");
const store = tx.objectStore("bla");
store.put({ x: 0, y: "a", z: 42, extra: 123 });
await promiseFromTransaction(tx);
}
t.pass();
});
test("range queries", async (t) => {
const backend = new MemoryBackend();
backend.enableTracing = true;
const idb = new BridgeIDBFactory(backend);
const request = idb.open("mydb");
request.onupgradeneeded = () => {
const db = request.result;
const store = db.createObjectStore("bla", { keyPath: "x" });
store.createIndex("by_y", "y");
store.createIndex("by_z", "z");
};
const db: BridgeIDBDatabase = await promiseFromRequest(request);
t.is(db.name, "mydb");
const tx = db.transaction("bla", "readwrite");
const store = tx.objectStore("bla");
store.put({ x: 0, y: "a" });
store.put({ x: 2, y: "a" });
store.put({ x: 4, y: "b" });
store.put({ x: 8, y: "b" });
store.put({ x: 10, y: "c" });
store.put({ x: 12, y: "c" });
await promiseFromTransaction(tx);
async function doCursorStoreQuery(
range: IDBKeyRange | IDBValidKey | undefined,
direction: IDBCursorDirection | undefined,
expected: any[],
): Promise<void> {
const tx = db.transaction("bla", "readwrite");
const store = tx.objectStore("bla");
const vals: any[] = [];
const req = store.openCursor(range, direction);
while (1) {
await promiseFromRequest(req);
const cursor: IDBCursorWithValue = req.result;
if (!cursor) {
break;
}
cursor.continue();
vals.push(cursor.value);
}
await promiseFromTransaction(tx);
t.deepEqual(vals, expected);
}
async function doCursorIndexQuery(
range: IDBKeyRange | IDBValidKey | undefined,
direction: IDBCursorDirection | undefined,
expected: any[],
): Promise<void> {
const tx = db.transaction("bla", "readwrite");
const store = tx.objectStore("bla");
const index = store.index("by_y");
const vals: any[] = [];
const req = index.openCursor(range, direction);
while (1) {
await promiseFromRequest(req);
const cursor: IDBCursorWithValue = req.result;
if (!cursor) {
break;
}
cursor.continue();
vals.push(cursor.value);
}
await promiseFromTransaction(tx);
t.deepEqual(vals, expected);
}
await doCursorStoreQuery(undefined, undefined, [
{
x: 0,
y: "a",
},
{
x: 2,
y: "a",
},
{
x: 4,
y: "b",
},
{
x: 8,
y: "b",
},
{
x: 10,
y: "c",
},
{
x: 12,
y: "c",
},
]);
await doCursorStoreQuery(
BridgeIDBKeyRange.bound(0, 12, true, true),
undefined,
[
{
x: 2,
y: "a",
},
{
x: 4,
y: "b",
},
{
x: 8,
y: "b",
},
{
x: 10,
y: "c",
},
],
);
await doCursorIndexQuery(
BridgeIDBKeyRange.bound("a", "c", true, true),
undefined,
[
{
x: 4,
y: "b",
},
{
x: 8,
y: "b",
},
],
);
await doCursorIndexQuery(undefined, "nextunique", [
{
x: 0,
y: "a",
},
{
x: 4,
y: "b",
},
{
x: 10,
y: "c",
},
]);
await doCursorIndexQuery(undefined, "prevunique", [
{
x: 10,
y: "c",
},
{
x: 4,
y: "b",
},
{
x: 0,
y: "a",
},
]);
db.close();
t.pass();
});

View File

@ -14,38 +14,43 @@
permissions and limitations under the License.
*/
import { AsyncCondition, TransactionLevel } from "./backend-common.js";
import {
Backend,
ConnectResult,
DatabaseConnection,
DatabaseTransaction,
IndexGetQuery,
IndexMeta,
ObjectStoreGetQuery,
ObjectStoreMeta,
RecordGetResponse,
Schema,
RecordStoreRequest,
RecordStoreResponse,
IndexProperties,
RecordGetRequest,
RecordGetResponse,
ResultLevel,
StoreLevel,
RecordStoreResponse,
} from "./backend-interface.js";
import { BridgeIDBKeyRange } from "./bridge-idb.js";
import { IDBKeyRange, IDBTransactionMode, IDBValidKey } from "./idbtypes.js";
import BTree, { ISortedMapF, ISortedSetF } from "./tree/b+tree.js";
import { compareKeys } from "./util/cmp.js";
import { ConstraintError, DataError } from "./util/errors.js";
import { getIndexKeys } from "./util/getIndexKeys.js";
import { StoreKeyResult, makeStoreKeyValue } from "./util/makeStoreKeyValue.js";
import {
structuredClone,
structuredEncapsulate,
structuredRevive,
} from "./util/structuredClone.js";
import { ConstraintError, DataError } from "./util/errors.js";
import BTree, { ISortedMapF, ISortedSetF } from "./tree/b+tree.js";
import { compareKeys } from "./util/cmp.js";
import { StoreKeyResult, makeStoreKeyValue } from "./util/makeStoreKeyValue.js";
import { getIndexKeys } from "./util/getIndexKeys.js";
import { openPromise } from "./util/openPromise.js";
import { IDBKeyRange, IDBTransactionMode, IDBValidKey } from "./idbtypes.js";
import { BridgeIDBKeyRange } from "./bridge-idb.js";
type Key = IDBValidKey;
type Value = unknown;
enum TransactionLevel {
None = 0,
Read = 1,
Write = 2,
VersionChange = 3,
}
interface ObjectStore {
originalName: string;
modifiedName: string | undefined;
@ -90,39 +95,24 @@ interface Database {
connectionCookies: string[];
}
/** @public */
export interface ObjectStoreDump {
name: string;
keyGenerator: number;
records: ObjectStoreRecord[];
}
/** @public */
export interface DatabaseDump {
schema: Schema;
objectStores: { [name: string]: ObjectStoreDump };
}
/** @public */
export interface MemoryBackendDump {
databases: { [name: string]: DatabaseDump };
}
export interface ObjectStoreProperties {
keyPath: string | string[] | null;
autoIncrement: boolean;
indexes: { [nameame: string]: IndexProperties };
}
export interface IndexProperties {
keyPath: string | string[];
multiEntry: boolean;
unique: boolean;
}
export interface Schema {
databaseName: string;
databaseVersion: number;
objectStores: { [name: string]: ObjectStoreProperties };
}
interface ObjectStoreMapEntry {
store: ObjectStore;
indexMap: { [currentName: string]: Index };
@ -152,6 +142,27 @@ export interface ObjectStoreRecord {
value: Value;
}
class AsyncCondition {
_waitPromise: Promise<void>;
_resolveWaitPromise: () => void;
constructor() {
const op = openPromise<void>();
this._waitPromise = op.promise;
this._resolveWaitPromise = op.resolve;
}
wait(): Promise<void> {
return this._waitPromise;
}
trigger(): void {
this._resolveWaitPromise();
const op = openPromise<void>();
this._waitPromise = op.promise;
this._resolveWaitPromise = op.resolve;
}
}
function nextStoreKey<T>(
forward: boolean,
data: ISortedMapF<Key, ObjectStoreRecord>,
@ -167,6 +178,12 @@ function nextStoreKey<T>(
return res[1].primaryKey;
}
function assertInvariant(cond: boolean): asserts cond {
if (!cond) {
throw Error("invariant failed");
}
}
function nextKey(
forward: boolean,
tree: ISortedSetF<IDBValidKey>,
@ -213,7 +230,6 @@ function furthestKey(
}
export interface AccessStats {
primitiveStatements: number;
writeTransactions: number;
readTransactions: number;
writesPerStore: Record<string, number>;
@ -263,7 +279,6 @@ export class MemoryBackend implements Backend {
trackStats: boolean = true;
accessStats: AccessStats = {
primitiveStatements: 0,
readTransactions: 0,
writeTransactions: 0,
readsPerStore: {},
@ -444,7 +459,7 @@ export class MemoryBackend implements Backend {
delete this.databases[name];
}
async connectDatabase(name: string): Promise<ConnectResult> {
async connectDatabase(name: string): Promise<DatabaseConnection> {
if (this.enableTracing) {
console.log(`TRACING: connectDatabase(${name})`);
}
@ -483,11 +498,7 @@ export class MemoryBackend implements Backend {
this.connections[connectionCookie] = myConn;
return {
conn: { connectionCookie },
version: database.committedSchema.databaseVersion,
objectStores: Object.keys(database.committedSchema.objectStores).sort(),
};
return { connectionCookie };
}
async beginTransaction(
@ -590,6 +601,14 @@ export class MemoryBackend implements Backend {
this.disconnectCond.trigger();
}
private requireConnection(dbConn: DatabaseConnection): Connection {
const myConn = this.connections[dbConn.connectionCookie];
if (!myConn) {
throw Error(`unknown connection (${dbConn.connectionCookie})`);
}
return myConn;
}
private requireConnectionFromTransaction(
btx: DatabaseTransaction,
): Connection {
@ -600,6 +619,36 @@ export class MemoryBackend implements Backend {
return myConn;
}
getSchema(dbConn: DatabaseConnection): Schema {
if (this.enableTracing) {
console.log(`TRACING: getSchema`);
}
const myConn = this.requireConnection(dbConn);
const db = this.databases[myConn.dbName];
if (!db) {
throw Error("db not found");
}
return db.committedSchema;
}
getCurrentTransactionSchema(btx: DatabaseTransaction): Schema {
const myConn = this.requireConnectionFromTransaction(btx);
const db = this.databases[myConn.dbName];
if (!db) {
throw Error("db not found");
}
return myConn.modifiedSchema;
}
getInitialTransactionSchema(btx: DatabaseTransaction): Schema {
const myConn = this.requireConnectionFromTransaction(btx);
const db = this.databases[myConn.dbName];
if (!db) {
throw Error("db not found");
}
return db.committedSchema;
}
renameIndex(
btx: DatabaseTransaction,
objectStoreName: string,
@ -750,7 +799,7 @@ export class MemoryBackend implements Backend {
createObjectStore(
btx: DatabaseTransaction,
name: string,
keyPath: string | string[] | null,
keyPath: string[] | null,
autoIncrement: boolean,
): void {
if (this.enableTracing) {
@ -793,7 +842,7 @@ export class MemoryBackend implements Backend {
btx: DatabaseTransaction,
indexName: string,
objectStoreName: string,
keyPath: string | string[],
keyPath: string[],
multiEntry: boolean,
unique: boolean,
): void {
@ -1053,91 +1102,12 @@ export class MemoryBackend implements Backend {
}
}
async getObjectStoreRecords(
async getRecords(
btx: DatabaseTransaction,
req: ObjectStoreGetQuery,
req: RecordGetRequest,
): Promise<RecordGetResponse> {
if (this.enableTracing) {
console.log(`TRACING: getObjectStoreRecords`);
console.log("query", req);
}
const myConn = this.requireConnectionFromTransaction(btx);
const db = this.databases[myConn.dbName];
if (!db) {
throw Error("db not found");
}
if (db.txLevel < TransactionLevel.Read) {
throw Error("only allowed while running a transaction");
}
if (
db.txRestrictObjectStores &&
!db.txRestrictObjectStores.includes(req.objectStoreName)
) {
throw Error(
`Not allowed to access store '${
req.objectStoreName
}', transaction is over ${JSON.stringify(db.txRestrictObjectStores)}`,
);
}
const objectStoreMapEntry = myConn.objectStoreMap[req.objectStoreName];
if (!objectStoreMapEntry) {
throw Error("object store not found");
}
let range;
if (req.range == null) {
range = new BridgeIDBKeyRange(undefined, undefined, true, true);
} else {
range = req.range;
}
if (typeof range !== "object") {
throw Error(
"getObjectStoreRecords was given an invalid range (sanity check failed, not an object)",
);
}
if (!("lowerOpen" in range)) {
throw Error(
"getObjectStoreRecords was given an invalid range (sanity check failed, lowerOpen missing)",
);
}
const forward: boolean =
req.direction === "next" || req.direction === "nextunique";
const storeData =
objectStoreMapEntry.store.modifiedData ||
objectStoreMapEntry.store.originalData;
const resp = getObjectStoreRecords({
forward,
storeData,
limit: req.limit,
range,
resultLevel: req.resultLevel,
advancePrimaryKey: req.advancePrimaryKey,
lastObjectStorePosition: req.lastObjectStorePosition,
});
if (this.trackStats) {
const k = `${req.objectStoreName}`;
this.accessStats.readsPerStore[k] =
(this.accessStats.readsPerStore[k] ?? 0) + 1;
this.accessStats.readItemsPerStore[k] =
(this.accessStats.readItemsPerStore[k] ?? 0) + resp.count;
}
if (this.enableTracing) {
console.log(`TRACING: getRecords got ${resp.count} results`);
}
return resp;
}
async getIndexRecords(
btx: DatabaseTransaction,
req: IndexGetQuery,
): Promise<RecordGetResponse> {
if (this.enableTracing) {
console.log(`TRACING: getIndexRecords`);
console.log(`TRACING: getRecords`);
console.log("query", req);
}
const myConn = this.requireConnectionFromTransaction(btx);
@ -1191,10 +1161,15 @@ export class MemoryBackend implements Backend {
objectStoreMapEntry.store.modifiedData ||
objectStoreMapEntry.store.originalData;
const haveIndex = req.indexName !== undefined;
let resp: RecordGetResponse;
if (haveIndex) {
const index =
myConn.objectStoreMap[req.objectStoreName].indexMap[req.indexName!];
const indexData = index.modifiedData || index.originalData;
const resp = getIndexRecords({
resp = getIndexRecords({
forward,
indexData,
storeData,
@ -1214,8 +1189,30 @@ export class MemoryBackend implements Backend {
this.accessStats.readItemsPerIndex[k] =
(this.accessStats.readItemsPerIndex[k] ?? 0) + resp.count;
}
} else {
if (req.advanceIndexKey !== undefined) {
throw Error("unsupported request");
}
resp = getObjectStoreRecords({
forward,
storeData,
limit: req.limit,
range,
resultLevel: req.resultLevel,
advancePrimaryKey: req.advancePrimaryKey,
lastIndexPosition: req.lastIndexPosition,
lastObjectStorePosition: req.lastObjectStorePosition,
});
if (this.trackStats) {
const k = `${req.objectStoreName}`;
this.accessStats.readsPerStore[k] =
(this.accessStats.readsPerStore[k] ?? 0) + 1;
this.accessStats.readItemsPerStore[k] =
(this.accessStats.readItemsPerStore[k] ?? 0) + resp.count;
}
}
if (this.enableTracing) {
console.log(`TRACING: getIndexRecords got ${resp.count} results`);
console.log(`TRACING: getRecords got ${resp.count} results`);
}
return resp;
}
@ -1297,13 +1294,13 @@ export class MemoryBackend implements Backend {
let storeKeyResult: StoreKeyResult;
try {
storeKeyResult = makeStoreKeyValue({
value: storeReq.value,
key: storeReq.key,
currentKeyGenerator: keygen,
autoIncrement: autoIncrement,
keyPath: keyPath,
});
storeKeyResult = makeStoreKeyValue(
storeReq.value,
storeReq.key,
keygen,
autoIncrement,
keyPath,
);
} catch (e) {
if (e instanceof DataError) {
const kp = JSON.stringify(keyPath);
@ -1448,7 +1445,7 @@ export class MemoryBackend implements Backend {
}
}
rollback(btx: DatabaseTransaction): void {
async rollback(btx: DatabaseTransaction): Promise<void> {
if (this.enableTracing) {
console.log(`TRACING: rollback`);
}
@ -1539,57 +1536,6 @@ export class MemoryBackend implements Backend {
await this.afterCommitCallback();
}
}
getObjectStoreMeta(
dbConn: DatabaseConnection,
objectStoreName: string,
): ObjectStoreMeta | undefined {
const conn = this.connections[dbConn.connectionCookie];
if (!conn) {
throw Error("db connection not found");
}
let schema = conn.modifiedSchema;
if (!schema) {
throw Error();
}
const storeInfo = schema.objectStores[objectStoreName];
if (!storeInfo) {
return undefined;
}
return {
autoIncrement: storeInfo.autoIncrement,
indexSet: Object.keys(storeInfo.indexes).sort(),
keyPath: structuredClone(storeInfo.keyPath),
};
}
getIndexMeta(
dbConn: DatabaseConnection,
objectStoreName: string,
indexName: string,
): IndexMeta | undefined {
const conn = this.connections[dbConn.connectionCookie];
if (!conn) {
throw Error("db connection not found");
}
let schema = conn.modifiedSchema;
if (!schema) {
throw Error();
}
const storeInfo = schema.objectStores[objectStoreName];
if (!storeInfo) {
return undefined;
}
const indexInfo = storeInfo.indexes[indexName];
if (!indexInfo) {
return;
}
return {
keyPath: structuredClone(indexInfo.keyPath),
multiEntry: indexInfo.multiEntry,
unique: indexInfo.unique,
};
}
}
function getIndexRecords(req: {
@ -1788,6 +1734,7 @@ function getIndexRecords(req: {
function getObjectStoreRecords(req: {
storeData: ISortedMapF<IDBValidKey, ObjectStoreRecord>;
lastIndexPosition?: IDBValidKey;
forward: boolean;
range: IDBKeyRange;
lastObjectStorePosition?: IDBValidKey;
@ -1796,6 +1743,7 @@ function getObjectStoreRecords(req: {
resultLevel: ResultLevel;
}): RecordGetResponse {
let numResults = 0;
const indexKeys: Key[] = [];
const primaryKeys: Key[] = [];
const values: Value[] = [];
const { storeData, range, forward } = req;
@ -1803,7 +1751,8 @@ function getObjectStoreRecords(req: {
function packResult(): RecordGetResponse {
return {
count: numResults,
indexKeys: undefined,
indexKeys:
req.resultLevel >= ResultLevel.OnlyKeys ? indexKeys : undefined,
primaryKeys:
req.resultLevel >= ResultLevel.OnlyKeys ? primaryKeys : undefined,
values: req.resultLevel >= ResultLevel.Full ? values : undefined,
@ -1813,8 +1762,8 @@ function getObjectStoreRecords(req: {
const rangeStart = forward ? range.lower : range.upper;
const dataStart = forward ? storeData.minKey() : storeData.maxKey();
let storePos = req.lastObjectStorePosition;
storePos = furthestKey(forward, storePos, dataStart);
storePos = furthestKey(forward, storePos, rangeStart);
storePos = furthestKey(forward, storePos, dataStart);
storePos = furthestKey(forward, storePos, req.advancePrimaryKey);
if (storePos != null) {

View File

@ -1,83 +0,0 @@
/*
Copyright 2019 Florian Dold
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
or implied. See the License for the specific language governing
permissions and limitations under the License.
*/
import test from "ava";
import { createSqliteBackend } from "./SqliteBackend.js";
import { ResultLevel, StoreLevel } from "./backend-interface.js";
import { BridgeIDBKeyRange } from "./bridge-idb.js";
import * as fs from "node:fs";
import { createNodeSqlite3Impl } from "./node-sqlite3-impl.js";
test("sqlite3 backend", async (t) => {
const filename = "mytestdb.sqlite3";
try {
fs.unlinkSync(filename);
} catch (e) {
// Do nothing.
}
try {
const sqlite3Impl = await createNodeSqlite3Impl();
const backend = await createSqliteBackend(sqlite3Impl, {
filename,
});
const dbConnRes = await backend.connectDatabase("mydb");
const dbConn = dbConnRes.conn;
const tx = await backend.enterVersionChange(dbConn, 1);
backend.createObjectStore(tx, "books", "isbn", true);
backend.createIndex(tx, "byName", "books", "name", false, false);
await backend.storeRecord(tx, {
objectStoreName: "books",
storeLevel: StoreLevel.AllowOverwrite,
value: { name: "foo" },
key: undefined,
});
const res = await backend.getObjectStoreRecords(tx, {
direction: "next",
limit: 1,
objectStoreName: "books",
resultLevel: ResultLevel.Full,
range: BridgeIDBKeyRange.only(1),
});
t.deepEqual(res.count, 1);
t.deepEqual(res.primaryKeys![0], 1);
t.deepEqual(res.values![0].name, "foo");
const indexRes = await backend.getIndexRecords(tx, {
direction: "next",
limit: 1,
objectStoreName: "books",
indexName: "byName",
resultLevel: ResultLevel.Full,
range: BridgeIDBKeyRange.only("foo"),
});
t.deepEqual(indexRes.count, 1);
t.deepEqual(indexRes.values![0].isbn, 1);
t.deepEqual(indexRes.values![0].name, "foo");
await backend.commit(tx);
const tx2 = await backend.beginTransaction(dbConn, ["books"], "readwrite");
await backend.commit(tx2);
await backend.close(dbConn);
t.pass();
} catch (e: any) {
console.log(e);
throw e;
}
});

File diff suppressed because it is too large Load Diff

View File

@ -1,29 +0,0 @@
import { openPromise } from "./util/openPromise.js";
export class AsyncCondition {
_waitPromise: Promise<void>;
_resolveWaitPromise: () => void;
constructor() {
const op = openPromise<void>();
this._waitPromise = op.promise;
this._resolveWaitPromise = op.resolve;
}
wait(): Promise<void> {
return this._waitPromise;
}
trigger(): void {
this._resolveWaitPromise();
const op = openPromise<void>();
this._waitPromise = op.promise;
this._resolveWaitPromise = op.resolve;
}
}
export enum TransactionLevel {
None = 0,
Read = 1,
Write = 2,
VersionChange = 3,
}

View File

@ -21,45 +21,66 @@ import {
IDBValidKey,
} from "./idbtypes.js";
export interface ConnectResult {
conn: DatabaseConnection;
version: number;
objectStores: string[];
/** @public */
export interface ObjectStoreProperties {
keyPath: string[] | null;
autoIncrement: boolean;
indexes: { [nameame: string]: IndexProperties };
}
/** @public */
export interface IndexProperties {
keyPath: string[];
multiEntry: boolean;
unique: boolean;
}
/** @public */
export interface Schema {
databaseName: string;
databaseVersion: number;
objectStores: { [name: string]: ObjectStoreProperties };
}
/** @public */
export interface DatabaseConnection {
connectionCookie: string;
}
/** @public */
export interface DatabaseTransaction {
transactionCookie: string;
}
/** @public */
export enum ResultLevel {
OnlyCount,
OnlyKeys,
Full,
}
/** @public */
export enum StoreLevel {
NoOverwrite,
AllowOverwrite,
UpdateExisting,
}
export interface IndexGetQuery {
/** @public */
export interface RecordGetRequest {
direction: IDBCursorDirection;
objectStoreName: string;
indexName: string;
indexName: string | undefined;
/**
* The range of keys to return.
* The range refers to the index keys.
* If indexName is defined, the range refers to the index keys.
* Otherwise it refers to the object store keys.
*/
range: BridgeIDBKeyRange | undefined | null;
/**
* Last cursor position in terms of the index key.
* Can only be specified if lastObjectStorePosition is defined.
* Can only be specified if indexName is defined and
* lastObjectStorePosition is defined.
*
* Must either be undefined or within range.
*/
@ -71,6 +92,8 @@ export interface IndexGetQuery {
/**
* If specified, the index key of the results must be
* greater or equal to advanceIndexKey.
*
* Only applicable if indexName is specified.
*/
advanceIndexKey?: IDBValidKey;
/**
@ -86,31 +109,7 @@ export interface IndexGetQuery {
resultLevel: ResultLevel;
}
export interface ObjectStoreGetQuery {
direction: IDBCursorDirection;
objectStoreName: string;
/**
* The range of keys to return.
* Refers to the object store keys.
*/
range: BridgeIDBKeyRange | undefined | null;
/**
* Last position in terms of the object store key.
*/
lastObjectStorePosition?: IDBValidKey;
/**
* If specified, the primary key of the results must be greater
* or equal to advancePrimaryKey.
*/
advancePrimaryKey?: IDBValidKey;
/**
* Maximum number of results to return.
* If 0, return all available results
*/
limit: number;
resultLevel: ResultLevel;
}
/** @public */
export interface RecordGetResponse {
values: any[] | undefined;
indexKeys: IDBValidKey[] | undefined;
@ -118,6 +117,7 @@ export interface RecordGetResponse {
count: number;
}
/** @public */
export interface RecordStoreRequest {
objectStoreName: string;
value: any;
@ -125,6 +125,7 @@ export interface RecordStoreRequest {
storeLevel: StoreLevel;
}
/** @public */
export interface RecordStoreResponse {
/**
* Key that the record was stored under in the object store.
@ -132,79 +133,38 @@ export interface RecordStoreResponse {
key: IDBValidKey;
}
export interface ObjectStoreMeta {
indexSet: string[];
keyPath: string | string[] | null;
autoIncrement: boolean;
}
export interface IndexMeta {
keyPath: string | string[];
multiEntry: boolean;
unique: boolean;
}
// FIXME: Instead of refering to an object store by name,
// maybe refer to it via some internal, numeric ID?
// This would simplify renaming.
/** @public */
export interface Backend {
getDatabases(): Promise<BridgeIDBDatabaseInfo[]>;
connectDatabase(name: string): Promise<ConnectResult>;
connectDatabase(name: string): Promise<DatabaseConnection>;
beginTransaction(
dbConn: DatabaseConnection,
conn: DatabaseConnection,
objectStores: string[],
mode: IDBTransactionMode,
): Promise<DatabaseTransaction>;
enterVersionChange(
dbConn: DatabaseConnection,
conn: DatabaseConnection,
newVersion: number,
): Promise<DatabaseTransaction>;
deleteDatabase(name: string): Promise<void>;
close(dbConn: DatabaseConnection): Promise<void>;
close(db: DatabaseConnection): Promise<void>;
// FIXME: Use this for connection
// prepareConnect() - acquires a lock, maybe enters a version change transaction?
// finishConnect() - after possible versionchange is done, allow others to connect
getSchema(db: DatabaseConnection): Schema;
/**
* Get metadata for an object store.
*
* When dbConn is running a version change transaction,
* the current schema (and not the initial schema) is returned.
*
* Caller may mutate the result, a new object
* is returned on each call.
*/
getObjectStoreMeta(
dbConn: DatabaseConnection,
objectStoreName: string,
): ObjectStoreMeta | undefined;
getCurrentTransactionSchema(btx: DatabaseTransaction): Schema;
/**
* Get metadata for an index.
*
* When dbConn is running a version change transaction,
* the current schema (and not the initial schema) is returned.
*
* Caller may mutate the result, a new object
* is returned on each call.
*/
getIndexMeta(
dbConn: DatabaseConnection,
objectStoreName: string,
indexName: string,
): IndexMeta | undefined;
getInitialTransactionSchema(btx: DatabaseTransaction): Schema;
renameIndex(
btx: DatabaseTransaction,
objectStoreName: string,
oldIndexName: string,
newIndexName: string,
oldName: string,
newName: string,
): void;
deleteIndex(
@ -213,9 +173,8 @@ export interface Backend {
indexName: string,
): void;
rollback(btx: DatabaseTransaction): void;
rollback(btx: DatabaseTransaction): Promise<void>;
// FIXME: Should probably not be async
commit(btx: DatabaseTransaction): Promise<void>;
deleteObjectStore(btx: DatabaseTransaction, name: string): void;
@ -248,14 +207,9 @@ export interface Backend {
range: BridgeIDBKeyRange,
): Promise<void>;
getObjectStoreRecords(
getRecords(
btx: DatabaseTransaction,
req: ObjectStoreGetQuery,
): Promise<RecordGetResponse>;
getIndexRecords(
btx: DatabaseTransaction,
req: IndexGetQuery,
req: RecordGetRequest,
): Promise<RecordGetResponse>;
storeRecord(

View File

@ -1,740 +0,0 @@
/*
Copyright 2019 Florian Dold
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
or implied. See the License for the specific language governing
permissions and limitations under the License.
*/
/**
* Tests that are backend-generic.
* See testingdb.ts for the backend selection in test runs.
*/
/**
* Imports.
*/
import test from "ava";
import {
BridgeIDBCursorWithValue,
BridgeIDBDatabase,
BridgeIDBFactory,
BridgeIDBKeyRange,
BridgeIDBTransaction,
} from "./bridge-idb.js";
import {
IDBCursorDirection,
IDBCursorWithValue,
IDBDatabase,
IDBKeyRange,
IDBRequest,
IDBValidKey,
} from "./idbtypes.js";
import { initTestIndexedDB, useTestIndexedDb } from "./testingdb.js";
import { MemoryBackend } from "./MemoryBackend.js";
import { promiseFromRequest, promiseFromTransaction } from "./idbpromutil.js";
test.before("test DB initialization", initTestIndexedDB);
test("Spec: Example 1 Part 1", async (t) => {
const idb = useTestIndexedDb();
const dbname = "library-" + new Date().getTime() + Math.random();
const request = idb.open(dbname);
request.onupgradeneeded = () => {
const db = request.result as BridgeIDBDatabase;
const store = db.createObjectStore("books", { keyPath: "isbn" });
const titleIndex = store.createIndex("by_title", "title", { unique: true });
const authorIndex = store.createIndex("by_author", "author");
// Populate with initial data.
store.put({ title: "Quarry Memories", author: "Fred", isbn: 123456 });
store.put({ title: "Water Buffaloes", author: "Fred", isbn: 234567 });
store.put({ title: "Bedrock Nights", author: "Barney", isbn: 345678 });
};
await promiseFromRequest(request);
t.pass();
});
test("Spec: Example 1 Part 2", async (t) => {
const idb = useTestIndexedDb();
const dbname = "library-" + new Date().getTime() + Math.random();
const request = idb.open(dbname);
request.onupgradeneeded = () => {
const db = request.result;
const store = db.createObjectStore("books", { keyPath: "isbn" });
const titleIndex = store.createIndex("by_title", "title", { unique: true });
const authorIndex = store.createIndex("by_author", "author");
};
const db: BridgeIDBDatabase = await promiseFromRequest(request);
t.is(db.name, dbname);
const tx = db.transaction("books", "readwrite");
tx.oncomplete = () => {
console.log("oncomplete called");
};
const store = tx.objectStore("books");
store.put({ title: "Quarry Memories", author: "Fred", isbn: 123456 });
store.put({ title: "Water Buffaloes", author: "Fred", isbn: 234567 });
store.put({ title: "Bedrock Nights", author: "Barney", isbn: 345678 });
await promiseFromTransaction(tx);
t.pass();
});
test("duplicate index insertion", async (t) => {
const idb = useTestIndexedDb();
const dbname = "library-" + new Date().getTime() + Math.random();
const request = idb.open(dbname);
request.onupgradeneeded = () => {
const db = request.result;
const store = db.createObjectStore("books", { keyPath: "isbn" });
const titleIndex = store.createIndex("by_title", "title", { unique: true });
const authorIndex = store.createIndex("by_author", "author");
};
const db: BridgeIDBDatabase = await promiseFromRequest(request);
t.is(db.name, dbname);
const tx = db.transaction("books", "readwrite");
tx.oncomplete = () => {
console.log("oncomplete called");
};
const store = tx.objectStore("books");
store.put({ title: "Quarry Memories", author: "Fred", isbn: 123456 });
// Change the index key, keep primary key (isbn) the same.
store.put({ title: "Water Buffaloes", author: "Bla", isbn: 234567 });
store.put({ title: "Water Buffaloes", author: "Fred", isbn: 234567 });
store.put({ title: "Bedrock Nights", author: "Barney", isbn: 345678 });
await promiseFromTransaction(tx);
const tx3 = db.transaction(["books"], "readonly");
const store3 = tx3.objectStore("books");
const index3 = store3.index("by_author");
const request3 = index3.openCursor();
const authorList: string[] = [];
await promiseFromRequest(request3);
while (request3.result != null) {
const cursor: IDBCursorWithValue = request3.result;
authorList.push(cursor.value.author);
cursor.continue();
await promiseFromRequest(request3);
}
t.deepEqual(authorList, ["Barney", "Fred", "Fred"]);
t.pass();
});
test("simple index iteration", async (t) => {
const idb = useTestIndexedDb();
const dbname = "library-" + new Date().getTime() + Math.random();
const request = idb.open(dbname);
request.onupgradeneeded = () => {
const db = request.result;
const store = db.createObjectStore("books", { keyPath: "isbn" });
const titleIndex = store.createIndex("by_title", "title", { unique: true });
const authorIndex = store.createIndex("by_author", "author");
};
const db: BridgeIDBDatabase = await promiseFromRequest(request);
const tx = db.transaction("books", "readwrite");
const store = tx.objectStore("books");
store.put({ title: "Bedrock Nights", author: "Barney", isbn: 345678 });
store.put({ title: "Quarry Memories", author: "Fred", isbn: 123456 });
store.put({ title: "Water Buffaloes", author: "Fred", isbn: 234567 });
await promiseFromTransaction(tx);
const tx3 = db.transaction(["books"], "readonly");
const store3 = tx3.objectStore("books");
const index3 = store3.index("by_author");
const request3 = index3.openCursor(BridgeIDBKeyRange.only("Fred"));
await promiseFromRequest(request3);
let cursor: BridgeIDBCursorWithValue | null;
cursor = request3.result as BridgeIDBCursorWithValue;
t.is(cursor.value.author, "Fred");
t.is(cursor.value.isbn, 123456);
cursor.continue();
await promiseFromRequest(request3);
t.is(cursor.value.author, "Fred");
t.is(cursor.value.isbn, 234567);
cursor.continue();
await promiseFromRequest(request3);
t.is(cursor.value, undefined);
});
test("Spec: Example 1 Part 3", async (t) => {
const idb = useTestIndexedDb();
const dbname = "library-" + new Date().getTime() + Math.random();
const request = idb.open(dbname);
request.onupgradeneeded = () => {
const db = request.result;
const store = db.createObjectStore("books", { keyPath: "isbn" });
const titleIndex = store.createIndex("by_title", "title", { unique: true });
const authorIndex = store.createIndex("by_author", "author");
};
const db: BridgeIDBDatabase = await promiseFromRequest(request);
t.is(db.name, dbname);
const tx = db.transaction("books", "readwrite");
const store = tx.objectStore("books");
store.put({ title: "Bedrock Nights", author: "Barney", isbn: 345678 });
store.put({ title: "Quarry Memories", author: "Fred", isbn: 123456 });
store.put({ title: "Water Buffaloes", author: "Fred", isbn: 234567 });
await promiseFromTransaction(tx);
const tx2 = db.transaction("books", "readonly");
const store2 = tx2.objectStore("books");
var index2 = store2.index("by_title");
const request2 = index2.get("Bedrock Nights");
const result2: any = await promiseFromRequest(request2);
t.is(result2.author, "Barney");
const tx3 = db.transaction(["books"], "readonly");
const store3 = tx3.objectStore("books");
const index3 = store3.index("by_author");
const request3 = index3.openCursor(BridgeIDBKeyRange.only("Fred"));
await promiseFromRequest(request3);
let cursor: BridgeIDBCursorWithValue | null;
cursor = request3.result as BridgeIDBCursorWithValue;
t.is(cursor.value.author, "Fred");
t.is(cursor.value.isbn, 123456);
cursor.continue();
await promiseFromRequest(request3);
cursor = request3.result as BridgeIDBCursorWithValue;
t.is(cursor.value.author, "Fred");
t.is(cursor.value.isbn, 234567);
await promiseFromTransaction(tx3);
const tx4 = db.transaction("books", "readonly");
const store4 = tx4.objectStore("books");
const request4 = store4.openCursor();
await promiseFromRequest(request4);
cursor = request4.result;
if (!cursor) {
throw new Error();
}
t.is(cursor.value.isbn, 123456);
cursor.continue();
await promiseFromRequest(request4);
cursor = request4.result;
if (!cursor) {
throw new Error();
}
t.is(cursor.value.isbn, 234567);
cursor.continue();
await promiseFromRequest(request4);
cursor = request4.result;
if (!cursor) {
throw new Error();
}
t.is(cursor.value.isbn, 345678);
cursor.continue();
await promiseFromRequest(request4);
cursor = request4.result;
t.is(cursor, null);
const tx5 = db.transaction("books", "readonly");
const store5 = tx5.objectStore("books");
const index5 = store5.index("by_author");
const request5 = index5.openCursor(null, "next");
await promiseFromRequest(request5);
cursor = request5.result;
if (!cursor) {
throw new Error();
}
t.is(cursor.value.author, "Barney");
cursor.continue();
await promiseFromRequest(request5);
cursor = request5.result;
if (!cursor) {
throw new Error();
}
t.is(cursor.value.author, "Fred");
cursor.continue();
await promiseFromRequest(request5);
cursor = request5.result;
if (!cursor) {
throw new Error();
}
t.is(cursor.value.author, "Fred");
cursor.continue();
await promiseFromRequest(request5);
cursor = request5.result;
t.is(cursor, null);
const request6 = index5.openCursor(null, "nextunique");
await promiseFromRequest(request6);
cursor = request6.result;
if (!cursor) {
throw new Error();
}
t.is(cursor.value.author, "Barney");
cursor.continue();
await promiseFromRequest(request6);
cursor = request6.result;
if (!cursor) {
throw new Error();
}
t.is(cursor.value.author, "Fred");
t.is(cursor.value.isbn, 123456);
cursor.continue();
await promiseFromRequest(request6);
cursor = request6.result;
t.is(cursor, null);
console.log("---------------------------");
const request7 = index5.openCursor(null, "prevunique");
await promiseFromRequest(request7);
cursor = request7.result;
if (!cursor) {
throw new Error();
}
t.is(cursor.value.author, "Fred");
t.is(cursor.value.isbn, 123456);
cursor.continue();
await promiseFromRequest(request7);
cursor = request7.result;
if (!cursor) {
throw new Error();
}
t.is(cursor.value.author, "Barney");
cursor.continue();
await promiseFromRequest(request7);
cursor = request7.result;
t.is(cursor, null);
db.close();
t.pass();
});
test("simple deletion", async (t) => {
const idb = useTestIndexedDb();
const dbname = "library-" + new Date().getTime() + Math.random();
const request = idb.open(dbname);
request.onupgradeneeded = () => {
const db = request.result;
const store = db.createObjectStore("books", { keyPath: "isbn" });
const titleIndex = store.createIndex("by_title", "title", { unique: true });
const authorIndex = store.createIndex("by_author", "author");
};
const db: BridgeIDBDatabase = await promiseFromRequest(request);
const tx = db.transaction("books", "readwrite");
tx.oncomplete = () => {
console.log("oncomplete called");
};
const store = tx.objectStore("books");
store.put({ title: "Quarry Memories", author: "Fred", isbn: 123456 });
store.put({ title: "Water Buffaloes", author: "Fred", isbn: 234567 });
store.put({ title: "Bedrock Nights", author: "Barney", isbn: 345678 });
await promiseFromTransaction(tx);
const tx2 = db.transaction("books", "readwrite");
const store2 = tx2.objectStore("books");
const req1 = store2.get(234567);
await promiseFromRequest(req1);
t.is(req1.readyState, "done");
t.is(req1.result.author, "Fred");
store2.delete(123456);
const req2 = store2.get(123456);
await promiseFromRequest(req2);
t.is(req2.readyState, "done");
t.is(req2.result, undefined);
const req3 = store2.get(234567);
await promiseFromRequest(req3);
t.is(req3.readyState, "done");
t.is(req3.result.author, "Fred");
await promiseFromTransaction(tx2);
t.pass();
});
test("export", async (t) => {
const backend = new MemoryBackend();
const idb = new BridgeIDBFactory(backend);
const dbname = "library-" + new Date().getTime() + Math.random();
const request = idb.open(dbname, 42);
request.onupgradeneeded = () => {
const db = request.result;
const store = db.createObjectStore("books", { keyPath: "isbn" });
const titleIndex = store.createIndex("by_title", "title", { unique: true });
const authorIndex = store.createIndex("by_author", "author");
};
const db: BridgeIDBDatabase = await promiseFromRequest(request);
const tx = db.transaction("books", "readwrite");
tx.oncomplete = () => {
console.log("oncomplete called");
};
const store = tx.objectStore("books");
store.put({ title: "Quarry Memories", author: "Fred", isbn: 123456 });
store.put({ title: "Water Buffaloes", author: "Fred", isbn: 234567 });
store.put({ title: "Bedrock Nights", author: "Barney", isbn: 345678 });
await promiseFromTransaction(tx);
const exportedData = backend.exportDump();
const backend2 = new MemoryBackend();
backend2.importDump(exportedData);
const exportedData2 = backend2.exportDump();
t.assert(
exportedData.databases[dbname].objectStores["books"].records.length ===
3,
);
t.deepEqual(exportedData, exportedData2);
t.is(exportedData.databases[dbname].schema.databaseVersion, 42);
t.is(exportedData2.databases[dbname].schema.databaseVersion, 42);
t.pass();
});
test("update with non-existent index values", async (t) => {
const idb = useTestIndexedDb();
const dbname = "mydb-" + new Date().getTime() + Math.random();
const request = idb.open(dbname);
request.onupgradeneeded = () => {
const db = request.result;
const store = db.createObjectStore("bla", { keyPath: "x" });
store.createIndex("by_y", "y");
store.createIndex("by_z", "z");
};
const db: BridgeIDBDatabase = await promiseFromRequest(request);
t.is(db.name, dbname);
{
const tx = db.transaction("bla", "readwrite");
const store = tx.objectStore("bla");
store.put({ x: 0, y: "a", z: 42 });
const index = store.index("by_z");
const indRes = await promiseFromRequest(index.get(42));
t.is(indRes.x, 0);
const res = await promiseFromRequest(store.get(0));
t.is(res.z, 42);
await promiseFromTransaction(tx);
}
{
const tx = db.transaction("bla", "readwrite");
const store = tx.objectStore("bla");
store.put({ x: 0, y: "a" });
const res = await promiseFromRequest(store.get(0));
t.is(res.z, undefined);
await promiseFromTransaction(tx);
}
{
const tx = db.transaction("bla", "readwrite");
const store = tx.objectStore("bla");
const index = store.index("by_z");
{
const indRes = await promiseFromRequest(index.get(42));
t.is(indRes, undefined);
}
const res = await promiseFromRequest(store.get(0));
t.is(res.z, undefined);
await promiseFromTransaction(tx);
}
t.pass();
});
test("delete from unique index", async (t) => {
const idb = useTestIndexedDb();
const dbname = "mydb-" + new Date().getTime() + Math.random();
const request = idb.open(dbname);
request.onupgradeneeded = () => {
const db = request.result as IDBDatabase;
const store = db.createObjectStore("bla", { keyPath: "x" });
store.createIndex("by_yz", ["y", "z"], {
unique: true,
});
};
const db: BridgeIDBDatabase = await promiseFromRequest(request);
t.is(db.name, dbname);
{
const tx = db.transaction("bla", "readwrite");
const store = tx.objectStore("bla");
store.put({ x: 0, y: "a", z: 42 });
const index = store.index("by_yz");
const indRes = await promiseFromRequest(index.get(["a", 42]));
t.is(indRes.x, 0);
const res = await promiseFromRequest(store.get(0));
t.is(res.z, 42);
await promiseFromTransaction(tx);
}
{
const tx = db.transaction("bla", "readwrite");
const store = tx.objectStore("bla");
store.put({ x: 0, y: "a", z: 42, extra: 123 });
await promiseFromTransaction(tx);
}
t.pass();
});
test("range queries", async (t) => {
const idb = useTestIndexedDb();
const dbname = "mydb-" + new Date().getTime() + Math.random();
const request = idb.open(dbname);
request.onupgradeneeded = () => {
const db = request.result;
const store = db.createObjectStore("bla", { keyPath: "x" });
store.createIndex("by_y", "y");
store.createIndex("by_z", "z");
};
const db: BridgeIDBDatabase = await promiseFromRequest(request);
const tx = db.transaction("bla", "readwrite");
const store = tx.objectStore("bla");
store.put({ x: 0, y: "a" });
store.put({ x: 2, y: "a" });
store.put({ x: 4, y: "b" });
store.put({ x: 8, y: "b" });
store.put({ x: 10, y: "c" });
store.put({ x: 12, y: "c" });
await promiseFromTransaction(tx);
async function doCursorStoreQuery(
range: IDBKeyRange | IDBValidKey | undefined,
direction: IDBCursorDirection | undefined,
expected: any[],
): Promise<void> {
const tx = db.transaction("bla", "readwrite");
const store = tx.objectStore("bla");
const vals: any[] = [];
const req = store.openCursor(range, direction);
while (1) {
await promiseFromRequest(req);
const cursor: IDBCursorWithValue = req.result;
if (!cursor) {
break;
}
cursor.continue();
vals.push(cursor.value);
}
await promiseFromTransaction(tx);
t.deepEqual(vals, expected);
}
async function doCursorIndexQuery(
range: IDBKeyRange | IDBValidKey | undefined,
direction: IDBCursorDirection | undefined,
expected: any[],
): Promise<void> {
const tx = db.transaction("bla", "readwrite");
const store = tx.objectStore("bla");
const index = store.index("by_y");
const vals: any[] = [];
const req = index.openCursor(range, direction);
while (1) {
await promiseFromRequest(req);
const cursor: IDBCursorWithValue = req.result;
if (!cursor) {
break;
}
cursor.continue();
vals.push(cursor.value);
}
await promiseFromTransaction(tx);
t.deepEqual(vals, expected);
}
await doCursorStoreQuery(undefined, undefined, [
{
x: 0,
y: "a",
},
{
x: 2,
y: "a",
},
{
x: 4,
y: "b",
},
{
x: 8,
y: "b",
},
{
x: 10,
y: "c",
},
{
x: 12,
y: "c",
},
]);
await doCursorStoreQuery(
BridgeIDBKeyRange.bound(0, 12, true, true),
undefined,
[
{
x: 2,
y: "a",
},
{
x: 4,
y: "b",
},
{
x: 8,
y: "b",
},
{
x: 10,
y: "c",
},
],
);
await doCursorIndexQuery(
BridgeIDBKeyRange.bound("a", "c", true, true),
undefined,
[
{
x: 4,
y: "b",
},
{
x: 8,
y: "b",
},
],
);
await doCursorIndexQuery(undefined, "nextunique", [
{
x: 0,
y: "a",
},
{
x: 4,
y: "b",
},
{
x: 10,
y: "c",
},
]);
await doCursorIndexQuery(undefined, "prevunique", [
{
x: 10,
y: "c",
},
{
x: 4,
y: "b",
},
{
x: 0,
y: "a",
},
]);
db.close();
t.pass();
});

File diff suppressed because it is too large Load Diff

View File

@ -1,7 +1,5 @@
import test from "ava";
import { createdb, initTestIndexedDB } from "./wptsupport.js";
test.before("test DB initialization", initTestIndexedDB);
import { createdb } from "./wptsupport.js";
test("WPT test abort-in-initial-upgradeneeded.htm", async (t) => {
await new Promise<void>((resolve, reject) => {

View File

@ -1,7 +1,5 @@
import test from "ava";
import { createdb, initTestIndexedDB } from "./wptsupport.js";
test.before("test DB initialization", initTestIndexedDB);
import { createdb } from "./wptsupport.js";
// When db.close is called in upgradeneeded, the db is cleaned up on refresh
test("WPT test close-in-upgradeneeded.htm", (t) => {

View File

@ -1,9 +1,7 @@
import test from "ava";
import { BridgeIDBKeyRange } from "../bridge-idb.js";
import { IDBRequest } from "../idbtypes.js";
import { createdb, initTestIndexedDB } from "./wptsupport.js";
test.before("test DB initialization", initTestIndexedDB);
import { createdb } from "./wptsupport.js";
const IDBKeyRange = BridgeIDBKeyRange;

View File

@ -2,13 +2,10 @@ import test from "ava";
import { BridgeIDBRequest } from "../bridge-idb.js";
import {
indexeddb_test,
initTestIndexedDB,
is_transaction_active,
keep_alive,
} from "./wptsupport.js";
test.before("test DB initialization", initTestIndexedDB);
test("WPT test abort-in-initial-upgradeneeded.htm (subtest 1)", async (t) => {
// Transactions are active during success handlers
await indexeddb_test(

View File

@ -1,8 +1,6 @@
import test from "ava";
import { BridgeIDBCursor,BridgeIDBRequest } from "../bridge-idb.js";
import { createdb, initTestIndexedDB } from "./wptsupport.js";
test.before("test DB initialization", initTestIndexedDB);
import { createdb } from "./wptsupport.js";
test("WPT test idbcursor_advance_index.htm", async (t) => {
await new Promise<void>((resolve, reject) => {

View File

@ -1,9 +1,6 @@
import test from "ava";
import { BridgeIDBCursor, BridgeIDBCursorWithValue } from "../bridge-idb.js";
import { createdb, initTestIndexedDB } from "./wptsupport.js";
import { IDBDatabase } from "../idbtypes.js";
test.before("test DB initialization", initTestIndexedDB);
import { createdb } from "./wptsupport.js";
test("WPT test idbcursor_continue_index.htm", (t) => {
return new Promise((resolve, reject) => {
@ -212,7 +209,7 @@ test("WPT idbcursor-continue-index4.htm", (t) => {
// IDBCursor.continue() - index - iterate using 'prevunique'
test("WPT idbcursor-continue-index5.htm", (t) => {
return new Promise((resolve, reject) => {
var db: IDBDatabase;
var db: any;
const records = [
{ pKey: "primaryKey_0", iKey: "indexKey_0" },
{ pKey: "primaryKey_1", iKey: "indexKey_1" },

View File

@ -1,9 +1,7 @@
import test from "ava";
import { BridgeIDBCursor } from "../bridge-idb.js";
import { IDBDatabase } from "../idbtypes.js";
import { createdb, initTestIndexedDB } from "./wptsupport.js";
test.before("test DB initialization", initTestIndexedDB);
import { createdb } from "./wptsupport.js";
// IDBCursor.continue() - object store - iterate to the next record
test("WPT test idbcursor_continue_objectstore.htm", (t) => {

View File

@ -1,7 +1,5 @@
import test from "ava";
import { indexeddb_test, initTestIndexedDB } from "./wptsupport.js";
test.before("test DB initialization", initTestIndexedDB);
import { indexeddb_test } from "./wptsupport.js";
test("WPT idbcursor-delete-exception-order.htm", async (t) => {
// 'IDBCursor.delete exception order: TransactionInactiveError vs. ReadOnlyError'

View File

@ -1,9 +1,7 @@
import test from "ava";
import { BridgeIDBCursor } from "../bridge-idb.js";
import { IDBCursor } from "../idbtypes.js";
import { createdb, initTestIndexedDB } from "./wptsupport.js";
test.before("test DB initialization", initTestIndexedDB);
import { createdb } from "./wptsupport.js";
// IDBCursor.delete() - index - remove a record from the object store
test("WPT idbcursor-delete-index.htm", (t) => {

View File

@ -1,8 +1,6 @@
import test from "ava";
import { BridgeIDBCursor } from "../bridge-idb.js";
import { createdb, initTestIndexedDB } from "./wptsupport.js";
test.before("test DB initialization", initTestIndexedDB);
import { createdb } from "./wptsupport.js";
// IDBCursor.delete() - object store - remove a record from the object store
test("WPT idbcursor-delete-objectstore.htm", (t) => {

View File

@ -1,7 +1,5 @@
import test from "ava";
import { createdb, initTestIndexedDB } from "./wptsupport.js";
test.before("test DB initialization", initTestIndexedDB);
import { createdb } from "./wptsupport.js";
test("WPT idbcursor-reused.htm", async (t) => {
await new Promise<void>((resolve, reject) => {

View File

@ -3,13 +3,10 @@ import { BridgeIDBCursor, BridgeIDBKeyRange } from "../bridge-idb.js";
import {
createDatabase,
createdb,
initTestIndexedDB,
promiseForRequest,
promiseForTransaction,
} from "./wptsupport.js";
test.before("test DB initialization", initTestIndexedDB);
// IDBCursor.update() - index - modify a record in the object store
test("WPT test idbcursor_update_index.htm", (t) => {
return new Promise((resolve, reject) => {

View File

@ -1,10 +1,8 @@
import test from "ava";
import { initTestIndexedDB, useTestIndexedDb } from "./wptsupport.js";
test.before("test DB initialization", initTestIndexedDB);
import { idbFactory } from "./wptsupport.js";
test("WPT idbfactory-cmp*.html", async (t) => {
const indexedDB = useTestIndexedDb();
const indexedDB = idbFactory;
var greater = indexedDB.cmp(2, 1);
var equal = indexedDB.cmp(2, 2);
var less = indexedDB.cmp(1, 2);

View File

@ -1,10 +1,7 @@
import test from "ava";
import { BridgeIDBVersionChangeEvent } from "../bridge-idb.js";
import FakeEvent from "../util/FakeEvent.js";
import { createdb, format_value, initTestIndexedDB, useTestIndexedDb } from "./wptsupport.js";
import { IDBDatabase } from "../idbtypes.js";
test.before("test DB initialization", initTestIndexedDB);
import { createdb, format_value, idbFactory } from "./wptsupport.js";
// IDBFactory.open() - request has no source
test("WPT idbfactory-open.htm", async (t) => {
@ -39,7 +36,7 @@ test("WPT idbfactory-open2.htm", async (t) => {
// IDBFactory.open() - no version opens current database
test("WPT idbfactory-open3.htm", async (t) => {
const indexedDB = useTestIndexedDb();
const indexedDB = idbFactory;
await new Promise<void>((resolve, reject) => {
var open_rq = createdb(t, undefined, 13);
var did_upgrade = false;
@ -64,6 +61,7 @@ test("WPT idbfactory-open3.htm", async (t) => {
// IDBFactory.open() - new database has default version
test("WPT idbfactory-open4.htm", async (t) => {
const indexedDB = idbFactory;
await new Promise<void>((resolve, reject) => {
var open_rq = createdb(t, t.title + "-database_name");
@ -80,6 +78,7 @@ test("WPT idbfactory-open4.htm", async (t) => {
// IDBFactory.open() - new database is empty
test("WPT idbfactory-open5.htm", async (t) => {
const indexedDB = idbFactory;
await new Promise<void>((resolve, reject) => {
var open_rq = createdb(t, t.title + "-database_name");
@ -98,7 +97,7 @@ test("WPT idbfactory-open5.htm", async (t) => {
// IDBFactory.open() - open database with a lower version than current
test("WPT idbfactory-open6.htm", async (t) => {
const indexedDB = useTestIndexedDb();
const indexedDB = idbFactory;
await new Promise<void>((resolve, reject) => {
var open_rq = createdb(t, undefined, 13);
var open_rq2: any;
@ -132,7 +131,7 @@ test("WPT idbfactory-open6.htm", async (t) => {
// IDBFactory.open() - open database with a higher version than current
test("WPT idbfactory-open7.htm", async (t) => {
const indexedDB = useTestIndexedDb();
const indexedDB = idbFactory;
await new Promise<void>((resolve, reject) => {
var open_rq = createdb(t, undefined, 13);
var did_upgrade = false;
@ -170,7 +169,7 @@ test("WPT idbfactory-open7.htm", async (t) => {
// IDBFactory.open() - error in version change transaction aborts open
test("WPT idbfactory-open8.htm", async (t) => {
const indexedDB = useTestIndexedDb();
const indexedDB = idbFactory;
await new Promise<void>((resolve, reject) => {
var open_rq = createdb(t, undefined, 13);
var did_upgrade = false;
@ -194,7 +193,7 @@ test("WPT idbfactory-open8.htm", async (t) => {
// IDBFactory.open() - errors in version argument
test("WPT idbfactory-open9.htm", async (t) => {
const indexedDB = useTestIndexedDb();
const indexedDB = idbFactory;
function should_throw(val: any, name?: string) {
if (!name) {
name = typeof val == "object" && val ? "object" : format_value(val);
@ -282,9 +281,9 @@ test("WPT idbfactory-open9.htm", async (t) => {
// IDBFactory.open() - error in version change transaction aborts open
test("WPT idbfactory-open10.htm", async (t) => {
const indexedDB = useTestIndexedDb();
const indexedDB = idbFactory;
await new Promise<void>((resolve, reject) => {
var db: IDBDatabase, db2: IDBDatabase;
var db: any, db2: any;
var open_rq = createdb(t, undefined, 9);
open_rq.onupgradeneeded = function (e: any) {
@ -351,7 +350,7 @@ test("WPT idbfactory-open10.htm", async (t) => {
var open_rq3 = indexedDB.open(db.name);
open_rq3.onsuccess = function (e: any) {
var db3: IDBDatabase = e.target.result;
var db3 = e.target.result;
t.true(
db3.objectStoreNames.contains("store"),
@ -408,7 +407,7 @@ test("WPT idbfactory-open10.htm", async (t) => {
// IDBFactory.open() - second open's transaction is available to get objectStores
test("WPT idbfactory-open11.htm", async (t) => {
const indexedDB = useTestIndexedDb();
const indexedDB = idbFactory;
await new Promise<void>((resolve, reject) => {
var db: any;
var count_done = 0;
@ -473,6 +472,8 @@ test("WPT idbfactory-open11.htm", async (t) => {
// IDBFactory.open() - upgradeneeded gets VersionChangeEvent
test("WPT idbfactory-open12.htm", async (t) => {
const indexedDB = idbFactory;
var db: any;
var open_rq = createdb(t, undefined, 9);

View File

@ -1,9 +1,7 @@
import test from "ava";
import { BridgeIDBKeyRange } from "../bridge-idb.js";
import { IDBDatabase } from "../idbtypes.js";
import { createdb, initTestIndexedDB } from "./wptsupport.js";
test.before("test DB initialization", initTestIndexedDB);
import { createdb } from "./wptsupport.js";
// IDBIndex.get() - returns the record
test("WPT idbindex_get.htm", async (t) => {
@ -95,7 +93,7 @@ test("WPT idbindex_get3.htm", async (t) => {
// IDBIndex.get() - returns the record with the first key in the range
test("WPT idbindex_get4.htm", async (t) => {
await new Promise<void>((resolve, reject) => {
var db: IDBDatabase;
var db: any;
var open_rq = createdb(t);

View File

@ -1,7 +1,5 @@
import test from "ava";
import { createdb, initTestIndexedDB } from "./wptsupport.js";
test.before("test DB initialization", initTestIndexedDB);
import { createdb } from "./wptsupport.js";
// IDBIndex.openCursor() - throw InvalidStateError when the index is deleted
test("WPT test idbindex-openCursor.htm", (t) => {

View File

@ -1,7 +1,5 @@
import test, { ExecutionContext } from "ava";
import { indexeddb_test, initTestIndexedDB } from "./wptsupport.js";
test.before("test DB initialization", initTestIndexedDB);
import { indexeddb_test } from "./wptsupport.js";
async function t1(t: ExecutionContext, method: string): Promise<void> {
await indexeddb_test(
@ -57,6 +55,8 @@ async function t2(t: ExecutionContext, method: string): Promise<void> {
done();
}, 0);
console.log(`queued task for ${method}`);
},
"t2",
);

View File

@ -1,9 +1,7 @@
import test from "ava";
import { BridgeIDBRequest } from "../bridge-idb.js";
import { IDBDatabase } from "../idbtypes.js";
import { createdb, initTestIndexedDB } from "./wptsupport.js";
test.before("test DB initialization", initTestIndexedDB);
import { createdb } from "./wptsupport.js";
// IDBObjectStore.add() - add with an inline key
test("WPT idbobjectstore_add.htm", async (t) => {

View File

@ -1,8 +1,6 @@
import test from "ava";
import { BridgeIDBKeyRange } from "../bridge-idb.js";
import { createdb, initTestIndexedDB } from "./wptsupport.js";
test.before("test DB initialization", initTestIndexedDB);
import { createdb } from "./wptsupport.js";
// IDBObjectStore.get() - key is a number
test("WPT idbobjectstore_get.htm", (t) => {

View File

@ -1,8 +1,6 @@
import test from "ava";
import { BridgeIDBRequest } from "../bridge-idb.js";
import { createdb, initTestIndexedDB } from "./wptsupport.js";
test.before("test DB initialization", initTestIndexedDB);
import { createdb } from "./wptsupport.js";
// IDBObjectStore.put() - put with an inline key
test("WPT idbobjectstore_put.htm", (t) => {

View File

@ -6,12 +6,9 @@ import {
createBooksStore,
createDatabase,
createNotBooksStore,
initTestIndexedDB,
migrateDatabase,
} from "./wptsupport.js";
test.before("test DB initialization", initTestIndexedDB);
// IndexedDB: object store renaming support
// IndexedDB object store rename in new transaction
test("WPT idbobjectstore-rename-store.html (subtest 1)", async (t) => {

View File

@ -1,7 +1,5 @@
import test from "ava";
import { createdb, initTestIndexedDB } from "./wptsupport.js";
test.before("test DB initialization", initTestIndexedDB);
import { createdb } from "./wptsupport.js";
// IDBTransaction - complete event
test("WPT idbtransaction-oncomplete.htm", async (t) => {

View File

@ -1,7 +1,5 @@
import test from "ava";
import { assert_key_equals, createdb, initTestIndexedDB } from "./wptsupport.js";
test.before("test DB initialization", initTestIndexedDB);
import { assert_key_equals, createdb } from "./wptsupport.js";
test("WPT test keypath.htm", async (t) => {
function keypath(
@ -11,6 +9,8 @@ test("WPT test keypath.htm", async (t) => {
desc?: string,
) {
return new Promise<void>((resolve, reject) => {
console.log("key path", keypath);
console.log("checking", desc);
let db: any;
const store_name = "store-" + Date.now() + Math.random();

View File

@ -1,8 +1,6 @@
import test from "ava";
import { EventTarget } from "../idbtypes.js";
import { createdb, initTestIndexedDB } from "./wptsupport.js";
test.before("test DB initialization", initTestIndexedDB);
import { createdb } from "./wptsupport.js";
// Bubbling and capturing of request events
test("WPT request_bubble-and-capture.htm", async (t) => {

View File

@ -1,7 +1,5 @@
import test from "ava";
import { createdb, initTestIndexedDB } from "./wptsupport.js";
test.before("test DB initialization", initTestIndexedDB);
import { createdb } from "./wptsupport.js";
// Transactions have a request queue
test("transaction-requestqueue.htm", async (t) => {

View File

@ -1,8 +1,6 @@
import test from "ava";
import { IDBVersionChangeEvent } from "../idbtypes.js";
import { createdb, initTestIndexedDB } from "./wptsupport.js";
test.before("test DB initialization", initTestIndexedDB);
import { createdb } from "./wptsupport.js";
test("WPT test value.htm, array", (t) => {
return new Promise((resolve, reject) => {
@ -14,6 +12,7 @@ test("WPT test value.htm, array", (t) => {
createdb(t).onupgradeneeded = function (e: IDBVersionChangeEvent) {
(e.target as any).result.createObjectStore("store").add(value, 1);
(e.target as any).onsuccess = (e: any) => {
console.log("in first onsuccess");
e.target.result
.transaction("store")
.objectStore("store")
@ -36,10 +35,13 @@ test("WPT test value.htm, date", (t) => {
createdb(t).onupgradeneeded = function (e: IDBVersionChangeEvent) {
(e.target as any).result.createObjectStore("store").add(value, 1);
(e.target as any).onsuccess = (e: any) => {
console.log("in first onsuccess");
e.target.result
.transaction("store")
.objectStore("store")
.get(1).onsuccess = (e: any) => {
console.log("target", e.target);
console.log("result", e.target.result);
t.assert(e.target.result instanceof _instanceof, "instanceof");
resolve();
};

View File

@ -1,5 +1,5 @@
import { ExecutionContext } from "ava";
import { BridgeIDBRequest } from "../bridge-idb.js";
import { BridgeIDBFactory, BridgeIDBRequest } from "../bridge-idb.js";
import {
IDBDatabase,
IDBIndex,
@ -8,10 +8,17 @@ import {
IDBRequest,
IDBTransaction,
} from "../idbtypes.js";
import { initTestIndexedDB , useTestIndexedDb } from "../testingdb.js";
import { MemoryBackend } from "../MemoryBackend.js";
import { compareKeys } from "../util/cmp.js";
export { initTestIndexedDB, useTestIndexedDb } from "../testingdb.js"
BridgeIDBFactory.enableTracing = true;
const backend = new MemoryBackend();
backend.enableTracing = true;
export const idbFactory = new BridgeIDBFactory(backend);
const self = {
indexedDB: idbFactory,
};
export function createdb(
t: ExecutionContext<unknown>,
@ -20,8 +27,8 @@ export function createdb(
): IDBOpenDBRequest {
var rq_open: IDBOpenDBRequest;
dbname = dbname ? dbname : "testdb-" + new Date().getTime() + Math.random();
if (version) rq_open = useTestIndexedDb().open(dbname, version);
else rq_open = useTestIndexedDb().open(dbname);
if (version) rq_open = self.indexedDB.open(dbname, version);
else rq_open = self.indexedDB.open(dbname);
return rq_open;
}
@ -104,7 +111,7 @@ export async function migrateNamedDatabase(
migrationCallback: MigrationCallback,
): Promise<IDBDatabase> {
return new Promise<IDBDatabase>((resolve, reject) => {
const request = useTestIndexedDb().open(databaseName, newVersion);
const request = self.indexedDB.open(databaseName, newVersion);
request.onupgradeneeded = (event: any) => {
const database = event.target.result;
const transaction = event.target.transaction;
@ -168,7 +175,7 @@ export async function createDatabase(
setupCallback: MigrationCallback,
): Promise<IDBDatabase> {
const databaseName = makeDatabaseName(t.title);
const request = useTestIndexedDb().deleteDatabase(databaseName);
const request = self.indexedDB.deleteDatabase(databaseName);
return migrateNamedDatabase(t, databaseName, 1, setupCallback);
}
@ -456,9 +463,9 @@ export function indexeddb_test(
options = Object.assign({ upgrade_will_abort: false }, options);
const dbname =
"testdb-" + new Date().getTime() + Math.random() + (dbsuffix ?? "");
var del = useTestIndexedDb().deleteDatabase(dbname);
var del = self.indexedDB.deleteDatabase(dbname);
del.onerror = () => t.fail("deleteDatabase should succeed");
var open = useTestIndexedDb().open(dbname, 1);
var open = self.indexedDB.open(dbname, 1);
open.onupgradeneeded = function () {
var db = open.result;
t.teardown(function () {
@ -467,7 +474,7 @@ export function indexeddb_test(
e.preventDefault();
};
db.close();
useTestIndexedDb().deleteDatabase(db.name);
self.indexedDB.deleteDatabase(db.name);
});
var tx = open.transaction!;
upgrade_func(resolve, db, tx, open);

View File

@ -1,26 +0,0 @@
import { BridgeIDBTransaction } from "./bridge-idb.js";
import { IDBRequest } from "./idbtypes.js";
export function promiseFromRequest(request: IDBRequest): Promise<any> {
return new Promise((resolve, reject) => {
request.onsuccess = () => {
resolve(request.result);
};
request.onerror = () => {
reject(request.error);
};
});
}
export function promiseFromTransaction(
transaction: BridgeIDBTransaction,
): Promise<void> {
return new Promise<void>((resolve, reject) => {
transaction.oncomplete = () => {
resolve();
};
transaction.onerror = () => {
reject();
};
});
}

View File

@ -19,27 +19,48 @@ and limitations under the License.
* Instead of ambient types, we export type declarations.
*/
/**
* @public
*/
export type IDBKeyPath = string;
/**
* @public
*/
export interface EventListener {
(evt: Event): void;
}
/**
* @public
*/
export interface EventListenerObject {
handleEvent(evt: Event): void;
}
/**
* @public
*/
export interface EventListenerOptions {
capture?: boolean;
}
/**
* @public
*/
export interface AddEventListenerOptions extends EventListenerOptions {
once?: boolean;
passive?: boolean;
}
/**
* @public
*/
export type IDBTransactionMode = "readonly" | "readwrite" | "versionchange";
/**
* @public
*/
export type EventListenerOrEventListenerObject =
| EventListener
| EventListenerObject;
@ -47,6 +68,8 @@ export type EventListenerOrEventListenerObject =
/**
* EventTarget is a DOM interface implemented by objects that can receive
* events and may have listeners for them.
*
* @public
*/
export interface EventTarget {
/**

View File

@ -2,10 +2,14 @@ import {
Backend,
DatabaseConnection,
DatabaseTransaction,
IndexProperties,
ObjectStoreProperties,
RecordGetRequest,
RecordGetResponse,
RecordStoreRequest,
RecordStoreResponse,
ResultLevel,
Schema,
StoreLevel,
} from "./backend-interface.js";
import {
@ -32,9 +36,6 @@ import {
} from "./MemoryBackend.js";
import { Listener } from "./util/FakeEventTarget.js";
export * from "./SqliteBackend.js";
export * from "./sqlite3-interface.js";
export * from "./idbtypes.js";
export { MemoryBackend } from "./MemoryBackend.js";
export type { AccessStats } from "./MemoryBackend.js";
@ -54,17 +55,21 @@ export {
};
export type {
DatabaseTransaction,
RecordGetRequest,
RecordGetResponse,
Schema,
Backend,
DatabaseList,
RecordStoreRequest,
RecordStoreResponse,
DatabaseConnection,
ObjectStoreProperties,
RequestObj,
DatabaseDump,
ObjectStoreDump,
IndexRecord,
ObjectStoreRecord,
IndexProperties,
MemoryBackendDump,
Event,
Listener,

View File

@ -1,84 +0,0 @@
/*
This file is part of GNU Taler
(C) 2023 Taler Systems S.A.
GNU Taler is free software; you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software
Foundation; either version 3, or (at your option) any later version.
GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
*/
// @ts-ignore: optional dependency
import type Database from "better-sqlite3";
import {
ResultRow,
Sqlite3Interface,
Sqlite3Statement,
} from "./sqlite3-interface.js";
export async function createNodeSqlite3Impl(): Promise<Sqlite3Interface> {
// @ts-ignore: optional dependency
const bsq = (await import("better-sqlite3")).default;
return {
open(filename: string) {
const internalDbHandle = bsq(filename);
return {
internalDbHandle,
close() {
internalDbHandle.close();
},
prepare(stmtStr): Sqlite3Statement {
const stmtHandle = internalDbHandle.prepare(stmtStr);
return {
internalStatement: stmtHandle,
getAll(params): ResultRow[] {
let res: ResultRow[];
if (params === undefined) {
res = stmtHandle.all() as ResultRow[];
} else {
res = stmtHandle.all(params) as ResultRow[];
}
return res;
},
getFirst(params): ResultRow | undefined {
let res: ResultRow | undefined;
if (params === undefined) {
res = stmtHandle.get() as ResultRow | undefined;
} else {
res = stmtHandle.get(params) as ResultRow | undefined;
}
return res;
},
run(params) {
const myParams = [];
if (params !== undefined) {
myParams.push(params);
}
// The better-sqlite3 library doesn't like it we pass
// undefined directly.
let res: Database.RunResult;
if (params !== undefined) {
res = stmtHandle.run(params);
} else {
res = stmtHandle.run();
}
return {
lastInsertRowid: res.lastInsertRowid,
};
},
};
},
exec(sqlStr): void {
internalDbHandle.exec(sqlStr);
},
};
},
};
}

View File

@ -1,34 +0,0 @@
export type Sqlite3Database = {
internalDbHandle: any;
exec(sqlStr: string): void;
prepare(stmtStr: string): Sqlite3Statement;
close(): void;
};
export type Sqlite3Statement = {
internalStatement: any;
run(params?: BindParams): RunResult;
getAll(params?: BindParams): ResultRow[];
getFirst(params?: BindParams): ResultRow | undefined;
};
export interface RunResult {
lastInsertRowid: number | bigint;
}
export type Sqlite3Value = string | Uint8Array | number | null | bigint;
export type BindParams = Record<string, Sqlite3Value | undefined>;
export type ResultRow = Record<string, Sqlite3Value>;
/**
* Common interface that multiple sqlite3 bindings
* (such as better-sqlite3 or qtart's sqlite3 bindings)
* can adapt to.
*
* This does not expose full sqlite3 functionality, but just enough
* to be used by our IndexedDB sqlite3 backend.
*/
export interface Sqlite3Interface {
open(filename: string): Sqlite3Database;
}

View File

@ -1,43 +0,0 @@
/*
Copyright 2023 Taler Systems S.A.
GNU Taler is free software; you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software
Foundation; either version 3, or (at your option) any later version.
GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
*/
import { createSqliteBackend } from "./SqliteBackend.js";
import { BridgeIDBFactory } from "./bridge-idb.js";
import { IDBFactory } from "./idbtypes.js";
import { createNodeSqlite3Impl } from "./node-sqlite3-impl.js";
let idbFactory: IDBFactory | undefined = undefined;
export async function initTestIndexedDB(): Promise<void> {
// const backend = new MemoryBackend();
// backend.enableTracing = true;
const sqlite3Impl = await createNodeSqlite3Impl();
const backend = await createSqliteBackend(sqlite3Impl, {
filename: ":memory:",
});
idbFactory = new BridgeIDBFactory(backend);
backend.enableTracing = true;
BridgeIDBFactory.enableTracing = false;
}
export function useTestIndexedDb(): IDBFactory {
if (!idbFactory) {
throw Error("indexeddb factory not initialized");
}
return idbFactory;
}

View File

@ -1,103 +0,0 @@
/*
Copyright 2017 Jeremy Scheff
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
or implied. See the License for the specific language governing
permissions and limitations under the License.
*/
import FakeEventTarget from "./FakeEventTarget.js";
import { Event, EventTarget } from "../idbtypes.js";
/** @public */
export type EventType =
| "abort"
| "blocked"
| "complete"
| "error"
| "success"
| "upgradeneeded"
| "versionchange";
export class FakeDomEvent implements Event {
public eventPath: FakeEventTarget[] = [];
public type: EventType;
public readonly NONE = 0;
public readonly CAPTURING_PHASE = 1;
public readonly AT_TARGET = 2;
public readonly BUBBLING_PHASE = 3;
// Flags
public propagationStopped = false;
public immediatePropagationStopped = false;
public canceled = false;
public initialized = true;
public dispatched = false;
public target: FakeEventTarget | null = null;
public currentTarget: FakeEventTarget | null = null;
public eventPhase: 0 | 1 | 2 | 3 = 0;
public defaultPrevented = false;
public isTrusted = false;
public timeStamp = Date.now();
public bubbles: boolean;
public cancelable: boolean;
constructor(
type: EventType,
eventInitDict: { bubbles?: boolean; cancelable?: boolean } = {},
) {
this.type = type;
this.bubbles =
eventInitDict.bubbles !== undefined ? eventInitDict.bubbles : false;
this.cancelable =
eventInitDict.cancelable !== undefined ? eventInitDict.cancelable : false;
}
cancelBubble: boolean = false;
composed: boolean = false;
returnValue: boolean = false;
get srcElement(): EventTarget | null {
return this.target;
}
composedPath(): EventTarget[] {
throw new Error("Method not implemented.");
}
initEvent(
type: string,
bubbles?: boolean | undefined,
cancelable?: boolean | undefined,
): void {
throw new Error("Method not implemented.");
}
public preventDefault() {
if (this.cancelable) {
this.canceled = true;
}
}
public stopPropagation() {
this.propagationStopped = true;
}
public stopImmediatePropagation() {
this.propagationStopped = true;
this.immediatePropagationStopped = true;
}
}
export default FakeDomEvent;

View File

@ -180,7 +180,7 @@ abstract class FakeEventTarget implements EventTarget {
fe.eventPath.reverse();
fe.eventPhase = event.BUBBLING_PHASE;
if (fe.eventPath.length === 0 && event.type === "error") {
console.error("Unhandled error event on target: ", event.target);
console.error("Unhandled error event: ", event.target);
}
for (const obj of event.eventPath) {
if (!event.propagationStopped) {

View File

@ -19,11 +19,7 @@ import { IDBKeyPath, IDBValidKey } from "../idbtypes.js";
import { valueToKey } from "./valueToKey.js";
// http://www.w3.org/TR/2015/REC-IndexedDB-20150108/#dfn-steps-for-extracting-a-key-from-a-value-using-a-key-path
/**
* Algorithm to "extract a key from a value using a key path".
*/
export const extractKey = (keyPath: IDBKeyPath | IDBKeyPath[], value: any) => {
//console.log(`extracting key ${JSON.stringify(keyPath)} from ${JSON.stringify(value)}`);
if (Array.isArray(keyPath)) {
const result: IDBValidKey[] = [];

View File

@ -1,39 +0,0 @@
/*
This file is part of GNU Taler
(C) 2023 Taler Systems S.A.
GNU Taler is free software; you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software
Foundation; either version 3, or (at your option) any later version.
GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
*/
import test, { ExecutionContext } from "ava";
import { deserializeKey, serializeKey } from "./key-storage.js";
import { IDBValidKey } from "../idbtypes.js";
function checkKeySer(t: ExecutionContext, k: IDBValidKey): void {
const keyEnc = serializeKey(k);
const keyDec = deserializeKey(keyEnc);
t.deepEqual(k, keyDec);
}
test("basics", (t) => {
checkKeySer(t, "foo");
checkKeySer(t, "foo\0bar");
checkKeySer(t, "foo\u1000bar");
checkKeySer(t, "foo\u2000bar");
checkKeySer(t, "foo\u5000bar");
checkKeySer(t, "foo\uffffbar");
checkKeySer(t, 42);
checkKeySer(t, 255);
checkKeySer(t, 254);
checkKeySer(t, [1, 2, 3, 4]);
checkKeySer(t, [[[1], 3], [4]]);
});

View File

@ -1,363 +0,0 @@
/*
This file is part of GNU Taler
(C) 2023 Taler Systems S.A.
GNU Taler is free software; you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software
Foundation; either version 3, or (at your option) any later version.
GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
*/
/*
Encoding rules (inspired by Firefox, but slightly simplified):
Numbers: 0x10 n n n n n n n n
Dates: 0x20 n n n n n n n n
Strings: 0x30 s s s s ... 0
Binaries: 0x40 s s s s ... 0
Arrays: 0x50 i i i ... 0
Numbers/dates are encoded as 64-bit IEEE 754 floats with the sign bit
flipped, in order to make them sortable.
*/
/**
* Imports.
*/
import { IDBValidKey } from "../idbtypes.js";
const tagNum = 0xa0;
const tagDate = 0xb0;
const tagString = 0xc0;
const tagBinary = 0xc0;
const tagArray = 0xe0;
const oneByteOffset = 0x01;
const twoByteOffset = 0x7f;
const oneByteMax = 0x7e;
const twoByteMax = 0x3fff + twoByteOffset;
const twoByteMask = 0b1000_0000;
const threeByteMask = 0b1100_0000;
export function countEncSize(c: number): number {
if (c > twoByteMax) {
return 3;
}
if (c > oneByteMax) {
return 2;
}
return 1;
}
export function writeEnc(dv: DataView, offset: number, c: number): number {
if (c > twoByteMax) {
dv.setUint8(offset + 2, (c & 0xff) << 6);
dv.setUint8(offset + 1, (c >>> 2) & 0xff);
dv.setUint8(offset, threeByteMask | (c >>> 10));
return 3;
} else if (c > oneByteMax) {
c -= twoByteOffset;
dv.setUint8(offset + 1, c & 0xff);
dv.setUint8(offset, (c >>> 8) | twoByteMask);
return 2;
} else {
c += oneByteOffset;
dv.setUint8(offset, c);
return 1;
}
}
export function internalSerializeString(
dv: DataView,
offset: number,
key: string,
): number {
dv.setUint8(offset, tagString);
let n = 1;
for (let i = 0; i < key.length; i++) {
let c = key.charCodeAt(i);
n += writeEnc(dv, offset + n, c);
}
// Null terminator
dv.setUint8(offset + n, 0);
n++;
return n;
}
export function countSerializeKey(key: IDBValidKey): number {
if (typeof key === "number") {
return 9;
}
if (key instanceof Date) {
return 9;
}
if (key instanceof ArrayBuffer) {
let len = 2;
const uv = new Uint8Array(key);
for (let i = 0; i < uv.length; i++) {
len += countEncSize(uv[i]);
}
return len;
}
if (ArrayBuffer.isView(key)) {
let len = 2;
const uv = new Uint8Array(key.buffer, key.byteOffset, key.byteLength);
for (let i = 0; i < uv.length; i++) {
len += countEncSize(uv[i]);
}
return len;
}
if (typeof key === "string") {
let len = 2;
for (let i = 0; i < key.length; i++) {
len += countEncSize(key.charCodeAt(i));
}
return len;
}
if (Array.isArray(key)) {
let len = 2;
for (let i = 0; i < key.length; i++) {
len += countSerializeKey(key[i]);
}
return len;
}
throw Error("unsupported type for key");
}
function internalSerializeNumeric(
dv: DataView,
offset: number,
tag: number,
val: number,
): number {
dv.setUint8(offset, tagNum);
dv.setFloat64(offset + 1, val);
// Flip sign bit
let b = dv.getUint8(offset + 1);
b ^= 0x80;
dv.setUint8(offset + 1, b);
return 9;
}
function internalSerializeArray(
dv: DataView,
offset: number,
key: any[],
): number {
dv.setUint8(offset, tagArray);
let n = 1;
for (let i = 0; i < key.length; i++) {
n += internalSerializeKey(key[i], dv, offset + n);
}
dv.setUint8(offset + n, 0);
n++;
return n;
}
function internalSerializeBinary(
dv: DataView,
offset: number,
key: Uint8Array,
): number {
dv.setUint8(offset, tagBinary);
let n = 1;
for (let i = 0; i < key.length; i++) {
n += internalSerializeKey(key[i], dv, offset + n);
}
dv.setUint8(offset + n, 0);
n++;
return n;
}
function internalSerializeKey(
key: IDBValidKey,
dv: DataView,
offset: number,
): number {
if (typeof key === "number") {
return internalSerializeNumeric(dv, offset, tagNum, key);
}
if (key instanceof Date) {
return internalSerializeNumeric(dv, offset, tagDate, key.getDate());
}
if (typeof key === "string") {
return internalSerializeString(dv, offset, key);
}
if (Array.isArray(key)) {
return internalSerializeArray(dv, offset, key);
}
if (key instanceof ArrayBuffer) {
return internalSerializeBinary(dv, offset, new Uint8Array(key));
}
if (ArrayBuffer.isView(key)) {
const uv = new Uint8Array(key.buffer, key.byteOffset, key.byteLength);
return internalSerializeBinary(dv, offset, uv);
}
throw Error("unsupported type for key");
}
export function serializeKey(key: IDBValidKey): Uint8Array {
const len = countSerializeKey(key);
let buf = new Uint8Array(len);
const outLen = internalSerializeKey(key, new DataView(buf.buffer), 0);
if (len != outLen) {
throw Error("internal invariant failed");
}
let numTrailingZeroes = 0;
for (let i = buf.length - 1; i >= 0 && buf[i] == 0; i--, numTrailingZeroes++);
if (numTrailingZeroes > 0) {
buf = buf.slice(0, buf.length - numTrailingZeroes);
}
return buf;
}
function internalReadString(dv: DataView, offset: number): [number, string] {
const chars: string[] = [];
while (offset < dv.byteLength) {
const v = dv.getUint8(offset);
if (v == 0) {
// Got end-of-string.
offset += 1;
break;
}
let c: number;
if ((v & threeByteMask) === threeByteMask) {
const b1 = v;
const b2 = dv.getUint8(offset + 1);
const b3 = dv.getUint8(offset + 2);
c = (b1 << 10) | (b2 << 2) | (b3 >> 6);
offset += 3;
} else if ((v & twoByteMask) === twoByteMask) {
const b1 = v & ~twoByteMask;
const b2 = dv.getUint8(offset + 1);
c = ((b1 << 8) | b2) + twoByteOffset;
offset += 2;
} else {
c = v - oneByteOffset;
offset += 1;
}
chars.push(String.fromCharCode(c));
}
return [offset, chars.join("")];
}
function internalReadBytes(dv: DataView, offset: number): [number, Uint8Array] {
let count = 0;
while (offset + count < dv.byteLength) {
const v = dv.getUint8(offset + count);
if (v === 0) {
break;
}
count++;
}
let writePos = 0;
const bytes = new Uint8Array(count);
while (offset < dv.byteLength) {
const v = dv.getUint8(offset);
if (v == 0) {
offset += 1;
break;
}
let c: number;
if ((v & threeByteMask) === threeByteMask) {
const b1 = v;
const b2 = dv.getUint8(offset + 1);
const b3 = dv.getUint8(offset + 2);
c = (b1 << 10) | (b2 << 2) | (b3 >> 6);
offset += 3;
} else if ((v & twoByteMask) === twoByteMask) {
const b1 = v & ~twoByteMask;
const b2 = dv.getUint8(offset + 1);
c = ((b1 << 8) | b2) + twoByteOffset;
offset += 2;
} else {
c = v - oneByteOffset;
offset += 1;
}
bytes[writePos] = c;
writePos++;
}
return [offset, bytes];
}
/**
* Same as DataView.getFloat64, but logically pad input
* with zeroes on the right if read offset would be out
* of bounds.
*
* This allows reading from buffers where zeros have been
* truncated.
*/
function getFloat64Trunc(dv: DataView, offset: number): number {
if (offset + 7 >= dv.byteLength) {
const buf = new Uint8Array(8);
for (let i = offset; i < dv.byteLength; i++) {
buf[i - offset] = dv.getUint8(i);
}
const dv2 = new DataView(buf.buffer);
return dv2.getFloat64(0);
} else {
return dv.getFloat64(offset);
}
}
function internalDeserializeKey(
dv: DataView,
offset: number,
): [number, IDBValidKey] {
let tag = dv.getUint8(offset);
switch (tag) {
case tagNum: {
const num = -getFloat64Trunc(dv, offset + 1);
const newOffset = Math.min(offset + 9, dv.byteLength);
return [newOffset, num];
}
case tagDate: {
const num = -getFloat64Trunc(dv, offset + 1);
const newOffset = Math.min(offset + 9, dv.byteLength);
return [newOffset, new Date(num)];
}
case tagString: {
return internalReadString(dv, offset + 1);
}
case tagBinary: {
return internalReadBytes(dv, offset + 1);
}
case tagArray: {
const arr: any[] = [];
offset += 1;
while (offset < dv.byteLength) {
const innerTag = dv.getUint8(offset);
if (innerTag === 0) {
offset++;
break;
}
const [innerOff, innerVal] = internalDeserializeKey(dv, offset);
arr.push(innerVal);
offset = innerOff;
}
return [offset, arr];
}
default:
throw Error("invalid key (unrecognized tag)");
}
}
export function deserializeKey(encodedKey: Uint8Array): IDBValidKey {
const dv = new DataView(
encodedKey.buffer,
encodedKey.byteOffset,
encodedKey.byteLength,
);
let [off, res] = internalDeserializeKey(dv, 0);
if (off != encodedKey.byteLength) {
throw Error("internal invariant failed");
}
return res;
}

View File

@ -20,73 +20,55 @@ import { makeStoreKeyValue } from "./makeStoreKeyValue.js";
test("basics", (t) => {
let result;
result = makeStoreKeyValue({
value: { name: "Florian" },
key: undefined,
currentKeyGenerator: 42,
autoIncrement: true,
keyPath: "id",
});
result = makeStoreKeyValue({ name: "Florian" }, undefined, 42, true, "id");
t.is(result.updatedKeyGenerator, 43);
t.is(result.key, 42);
t.is(result.value.name, "Florian");
t.is(result.value.id, 42);
result = makeStoreKeyValue({
value: { name: "Florian", id: 10 },
key: undefined,
currentKeyGenerator: 5,
autoIncrement: true,
keyPath: "id",
});
result = makeStoreKeyValue(
{ name: "Florian", id: 10 },
undefined,
5,
true,
"id",
);
t.is(result.updatedKeyGenerator, 11);
t.is(result.key, 10);
t.is(result.value.name, "Florian");
t.is(result.value.id, 10);
result = makeStoreKeyValue({
value: { name: "Florian", id: 5 },
key: undefined,
currentKeyGenerator: 10,
autoIncrement: true,
keyPath: "id",
});
result = makeStoreKeyValue(
{ name: "Florian", id: 5 },
undefined,
10,
true,
"id",
);
t.is(result.updatedKeyGenerator, 10);
t.is(result.key, 5);
t.is(result.value.name, "Florian");
t.is(result.value.id, 5);
result = makeStoreKeyValue({
value: { name: "Florian", id: "foo" },
key: undefined,
currentKeyGenerator: 10,
autoIncrement: true,
keyPath: "id",
});
result = makeStoreKeyValue(
{ name: "Florian", id: "foo" },
undefined,
10,
true,
"id",
);
t.is(result.updatedKeyGenerator, 10);
t.is(result.key, "foo");
t.is(result.value.name, "Florian");
t.is(result.value.id, "foo");
result = makeStoreKeyValue({
value: { name: "Florian" },
key: "foo",
currentKeyGenerator: 10,
autoIncrement: true,
keyPath: null,
});
result = makeStoreKeyValue({ name: "Florian" }, "foo", 10, true, null);
t.is(result.updatedKeyGenerator, 10);
t.is(result.key, "foo");
t.is(result.value.name, "Florian");
t.is(result.value.id, undefined);
result = makeStoreKeyValue({
value: { name: "Florian" },
key: undefined,
currentKeyGenerator: 10,
autoIncrement: true,
keyPath: null,
});
result = makeStoreKeyValue({ name: "Florian" }, undefined, 10, true, null);
t.is(result.updatedKeyGenerator, 11);
t.is(result.key, 10);
t.is(result.value.name, "Florian");

View File

@ -75,25 +75,19 @@ function injectKey(
return newValue;
}
export interface MakeStoreKvRequest {
value: any;
key: IDBValidKey | undefined;
currentKeyGenerator: number;
autoIncrement: boolean;
keyPath: IDBKeyPath | IDBKeyPath[] | null;
}
export function makeStoreKeyValue(req: MakeStoreKvRequest): StoreKeyResult {
const { keyPath, currentKeyGenerator, autoIncrement } = req;
let { key, value } = req;
export function makeStoreKeyValue(
value: any,
key: IDBValidKey | undefined,
currentKeyGenerator: number,
autoIncrement: boolean,
keyPath: IDBKeyPath | IDBKeyPath[] | null,
): StoreKeyResult {
const haveKey = key !== null && key !== undefined;
const haveKeyPath = keyPath !== null && keyPath !== undefined;
// This models a decision table on (haveKey, haveKeyPath, autoIncrement)
try {
// FIXME: Perf: only do this if we need to inject something.
value = structuredClone(value);
} catch (e) {
throw new DataCloneError();

View File

@ -14,11 +14,6 @@
permissions and limitations under the License.
*/
/**
* Queue a task to be executed *after* the microtask
* queue has been processed, but *before* subsequent setTimeout / setImmediate
* tasks.
*/
export function queueTask(fn: () => void) {
let called = false;
const callFirst = () => {

View File

@ -15,11 +15,7 @@
*/
import test, { ExecutionContext } from "ava";
import {
structuredClone,
structuredEncapsulate,
structuredRevive,
} from "./structuredClone.js";
import { structuredClone } from "./structuredClone.js";
function checkClone(t: ExecutionContext, x: any): void {
t.deepEqual(structuredClone(x), x);
@ -63,58 +59,3 @@ test("structured clone (object cycles)", (t) => {
const obj1Clone = structuredClone(obj1);
t.is(obj1Clone, obj1Clone.c);
});
test("encapsulate", (t) => {
t.deepEqual(structuredEncapsulate(42), 42);
t.deepEqual(structuredEncapsulate(true), true);
t.deepEqual(structuredEncapsulate(false), false);
t.deepEqual(structuredEncapsulate(null), null);
t.deepEqual(structuredEncapsulate(undefined), { $: "undef" });
t.deepEqual(structuredEncapsulate(42n), { $: "bigint", val: "42" });
t.deepEqual(structuredEncapsulate(new Date(42)), { $: "date", val: 42 });
t.deepEqual(structuredEncapsulate({ x: 42 }), { x: 42 });
t.deepEqual(structuredEncapsulate({ $: "bla", x: 42 }), {
$: "obj",
val: { $: "bla", x: 42 },
});
const x = { foo: 42, bar: {} } as any;
x.bar.baz = x;
t.deepEqual(structuredEncapsulate(x), {
foo: 42,
bar: {
baz: { $: "ref", d: 2, p: [] },
},
});
});
test("revive", (t) => {
t.deepEqual(structuredRevive(42), 42);
t.deepEqual(structuredRevive([1, 2, 3]), [1, 2, 3]);
t.deepEqual(structuredRevive(true), true);
t.deepEqual(structuredRevive(false), false);
t.deepEqual(structuredRevive(null), null);
t.deepEqual(structuredRevive({ $: "undef" }), undefined);
t.deepEqual(structuredRevive({ x: { $: "undef" } }), { x: undefined });
t.deepEqual(structuredRevive({ $: "date", val: 42}), new Date(42));
{
const x = { foo: 42, bar: {} } as any;
x.bar.baz = x;
const r = {
foo: 42,
bar: {
baz: { $: "ref", d: 2, p: [] },
},
};
t.deepEqual(structuredRevive(r), x);
}
});

View File

@ -18,19 +18,20 @@
* Encoding (new, compositional version):
*
* Encapsulate object that itself might contain a "$" field:
* { $: "obj", val: ... }
* (Outer level only:) Wrap other values into object
* { $: "lit", val: ... }
* { $: { E... } }
* Circular reference:
* { $: "ref" l: uplevel, p: path }
* { $: ["ref", uplevel, field...] }
* Date:
* { $: "date", val: datestr }
* { $: ["data"], val: datestr }
* Bigint:
* { $: "bigint", val: bigintstr }
* { $: ["bigint"], val: bigintstr }
* Array with special (non-number) attributes:
* { $: "array", val: arrayobj }
* { $: ["array"], val: arrayobj }
* Undefined field
* { $: "undef" }
*
* Legacy (top-level only), for backwards compatibility:
* { $types: [...] }
*/
/**
@ -260,18 +261,22 @@ export function mkDeepCloneCheckOnly() {
function internalEncapsulate(
val: any,
outRoot: any,
path: string[],
memo: Map<any, string[]>,
types: Array<[string[], string]>,
): any {
const memoPath = memo.get(val);
if (memoPath) {
return { $: "ref", d: path.length, p: memoPath };
types.push([path, "ref"]);
return memoPath;
}
if (val === null) {
return null;
}
if (val === undefined) {
return { $: "undef" };
types.push([path, "undef"]);
return 0;
}
if (Array.isArray(val)) {
memo.set(val, path);
@ -284,33 +289,31 @@ function internalEncapsulate(
break;
}
}
if (special) {
types.push([path, "array"]);
}
for (const x in val) {
const p = [...path, x];
outArr[x] = internalEncapsulate(val[x], p, memo);
outArr[x] = internalEncapsulate(val[x], outRoot, p, memo, types);
}
if (special) {
return { $: "array", val: outArr };
} else {
return outArr;
}
}
if (val instanceof Date) {
return { $: "date", val: val.getTime() };
types.push([path, "date"]);
return val.getTime();
}
if (isUserObject(val) || isPlainObject(val)) {
memo.set(val, path);
const outObj: any = {};
for (const x in val) {
const p = [...path, x];
outObj[x] = internalEncapsulate(val[x], p, memo);
}
if ("$" in outObj) {
return { $: "obj", val: outObj };
outObj[x] = internalEncapsulate(val[x], outRoot, p, memo, types);
}
return outObj;
}
if (typeof val === "bigint") {
return { $: "bigint", val: val.toString() };
types.push([path, "bigint"]);
return val.toString();
}
if (typeof val === "boolean") {
return val;
@ -324,103 +327,123 @@ function internalEncapsulate(
throw Error();
}
function derefPath(
root: any,
p1: Array<string | number>,
n: number,
p2: Array<string | number>,
): any {
let v = root;
for (let i = 0; i < n; i++) {
v = v[p1[i]];
}
for (let i = 0; i < p2.length; i++) {
v = v[p2[i]];
}
return v;
}
function internalReviveArray(sval: any, root: any, path: string[]): any {
const newArr: any[] = [];
if (root === undefined) {
root = newArr;
}
for (let i = 0; i < sval.length; i++) {
const p = [...path, String(i)];
newArr.push(internalStructuredRevive(sval[i], root, p));
}
return newArr;
}
function internalReviveObject(sval: any, root: any, path: string[]): any {
const newObj = {} as any;
if (root === undefined) {
root = newObj;
}
for (const key of Object.keys(sval)) {
const p = [...path, key];
newObj[key] = internalStructuredRevive(sval[key], root, p);
}
return newObj;
}
function internalStructuredRevive(sval: any, root: any, path: string[]): any {
if (typeof sval === "string") {
return sval;
}
if (typeof sval === "number") {
return sval;
}
if (typeof sval === "boolean") {
return sval;
}
if (sval === null) {
return null;
}
if (Array.isArray(sval)) {
return internalReviveArray(sval, root, path);
}
if (isUserObject(sval) || isPlainObject(sval)) {
if ("$" in sval) {
const dollar = sval.$;
switch (dollar) {
case "undef":
return undefined;
case "bigint":
return BigInt((sval as any).val);
case "date":
return new Date((sval as any).val);
case "obj": {
return internalReviveObject((sval as any).val, root, path);
}
case "array":
return internalReviveArray((sval as any).val, root, path);
case "ref": {
const level = (sval as any).l;
const p2 = (sval as any).p;
return derefPath(root, path, path.length - level, p2);
}
default:
throw Error();
}
} else {
return internalReviveObject(sval, root, path);
}
}
throw Error();
}
/**
* Encapsulate a cloneable value into a plain JSON value.
* Encapsulate a cloneable value into a plain JSON object.
*/
export function structuredEncapsulate(val: any): any {
return internalEncapsulate(val, [], new Map());
const outRoot = {};
const types: Array<[string[], string]> = [];
let res;
res = internalEncapsulate(val, outRoot, [], new Map(), types);
if (res === null) {
return res;
}
// We need to further encapsulate the outer layer
if (
Array.isArray(res) ||
typeof res !== "object" ||
"$" in res ||
"$types" in res
) {
res = { $: res };
}
if (types.length > 0) {
res["$types"] = types;
}
return res;
}
export function structuredRevive(sval: any): any {
return internalStructuredRevive(sval, undefined, []);
export function applyLegacyTypeAnnotations(val: any): any {
if (val === null) {
return null;
}
if (typeof val === "number") {
return val;
}
if (typeof val === "string") {
return val;
}
if (typeof val === "boolean") {
return val;
}
if (!isPlainObject(val)) {
throw Error();
}
let types = val.$types ?? [];
delete val.$types;
let outRoot: any;
if ("$" in val) {
outRoot = val.$;
} else {
outRoot = val;
}
function mutatePath(path: string[], f: (x: any) => any): void {
if (path.length == 0) {
outRoot = f(outRoot);
return;
}
let obj = outRoot;
for (let i = 0; i < path.length - 1; i++) {
const n = path[i];
if (!(n in obj)) {
obj[n] = {};
}
obj = obj[n];
}
const last = path[path.length - 1];
obj[last] = f(obj[last]);
}
function lookupPath(path: string[]): any {
let obj = outRoot;
for (const n of path) {
obj = obj[n];
}
return obj;
}
for (const [path, type] of types) {
switch (type) {
case "bigint": {
mutatePath(path, (x) => BigInt(x));
break;
}
case "array": {
mutatePath(path, (x) => {
const newArr: any = [];
for (const k in x) {
newArr[k] = x[k];
}
return newArr;
});
break;
}
case "date": {
mutatePath(path, (x) => new Date(x));
break;
}
case "undef": {
mutatePath(path, (x) => undefined);
break;
}
case "ref": {
mutatePath(path, (x) => lookupPath(x));
break;
}
default:
throw Error(`type '${type}' not implemented`);
}
}
return outRoot;
}
export function internalStructuredRevive(val: any): any {
// FIXME: Do the newly specified, compositional encoding here.
val = JSON.parse(JSON.stringify(val));
return val;
}
export function structuredRevive(val: any): any {
const r = internalStructuredRevive(val);
return applyLegacyTypeAnnotations(r);
}
/**

View File

@ -17,11 +17,7 @@
import { IDBValidKey } from "../idbtypes.js";
import { DataError } from "./errors.js";
/**
* Algorithm to "convert a value to a key".
*
* https://www.w3.org/TR/IndexedDB/#convert-value-to-key
*/
// https://www.w3.org/TR/IndexedDB-2/#convert-a-value-to-a-key
export function valueToKey(
input: any,
seen?: Set<object>,

View File

@ -4,7 +4,7 @@
"lib": ["es6"],
"module": "ES2020",
"moduleResolution": "Node16",
"target": "ES2020",
"target": "ES6",
"allowJs": true,
"noImplicitAny": true,
"outDir": "lib",

View File

@ -17,7 +17,7 @@ msgstr ""
"Project-Id-Version: Taler Wallet\n"
"Report-Msgid-Bugs-To: taler@gnu.org\n"
"POT-Creation-Date: 2016-11-23 00:00+0100\n"
"PO-Revision-Date: 2023-08-16 12:43+0000\n"
"PO-Revision-Date: 2023-08-15 07:28+0000\n"
"Last-Translator: Krystian Baran <kiszkot@murena.io>\n"
"Language-Team: Italian <https://weblate.taler.net/projects/gnu-taler/"
"merchant-backoffice/it/>\n"
@ -1203,7 +1203,7 @@ msgstr ""
#: src/paths/instance/orders/details/DetailPage.tsx:767
#, c-format
msgid "Back"
msgstr "Indietro"
msgstr ""
#: src/paths/instance/orders/details/index.tsx:79
#, c-format
@ -2644,7 +2644,7 @@ msgstr ""
#: src/components/menu/SideBar.tsx:91
#, c-format
msgid "Settings"
msgstr "Impostazioni"
msgstr ""
#: src/components/menu/SideBar.tsx:167
#, c-format

View File

@ -68,6 +68,7 @@ export async function runBench1(configJson: any): Promise<void> {
} else {
logger.info("not trusting exchange (validating signatures)");
}
const batchWithdrawal = !!process.env["TALER_WALLET_BATCH_WITHDRAWAL"];
let wallet = {} as Wallet;
let getDbStats: () => AccessStats;
@ -90,7 +91,9 @@ export async function runBench1(configJson: any): Promise<void> {
testing: {
insecureTrustExchange: trustExchange,
},
features: {},
features: {
batchWithdrawal,
},
},
});
wallet = res.wallet;

View File

@ -76,6 +76,8 @@ export async function runBench3(configJson: any): Promise<void> {
} else {
logger.info("not trusting exchange (validating signatures)");
}
const batchWithdrawal = !!process.env["TALER_WALLET_BATCH_WITHDRAWAL"];
let wallet = {} as Wallet;
let getDbStats: () => AccessStats;
@ -94,7 +96,9 @@ export async function runBench3(configJson: any): Promise<void> {
persistentStoragePath: undefined,
httpLib: myHttpLib,
config: {
features: {},
features: {
batchWithdrawal,
},
testing: {
insecureTrustExchange: trustExchange,
},

View File

@ -0,0 +1,59 @@
/*
This file is part of GNU Taler
(C) 2020 Taler Systems S.A.
GNU Taler is free software; you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software
Foundation; either version 3, or (at your option) any later version.
GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
*/
/**
* Imports.
*/
import { GlobalTestState, MerchantPrivateApi } from "../harness/harness.js";
import {
createSimpleTestkudosEnvironment,
withdrawViaBank,
} from "../harness/helpers.js";
/**
* Run test for basic, bank-integrated withdrawal.
*/
export async function runPromptPaymentScenario(t: GlobalTestState) {
// Set up test environment
const { wallet, bank, exchange, merchant } =
await createSimpleTestkudosEnvironment(t);
// Withdraw digital cash into the wallet.
await withdrawViaBank(t, { wallet, bank, exchange, amount: "TESTKUDOS:20" });
// Set up order.
const orderResp = await MerchantPrivateApi.createOrder(merchant, "default", {
order: {
summary: "Buy me!",
amount: "TESTKUDOS:5",
fulfillment_url: "taler://fulfillment-success/thx",
},
});
let orderStatus = await MerchantPrivateApi.queryPrivateOrderStatus(merchant, {
orderId: orderResp.order_id,
});
t.assertTrue(orderStatus.order_status === "unpaid");
console.log(orderStatus);
// Wait "forever"
await new Promise(() => {});
}

View File

@ -1,59 +0,0 @@
/*
This file is part of GNU Taler
(C) 2020 Taler Systems S.A.
GNU Taler is free software; you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software
Foundation; either version 3, or (at your option) any later version.
GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
*/
/**
* Imports.
*/
import { WalletApiOperation } from "@gnu-taler/taler-wallet-core";
import { GlobalTestState } from "../harness/harness.js";
import {
createSimpleTestkudosEnvironmentV2,
withdrawViaBankV2,
makeTestPaymentV2,
} from "../harness/helpers.js";
import { j2s } from "@gnu-taler/taler-util";
/**
* Run test for basic, bank-integrated withdrawal and payment.
*/
export async function runSimplePaymentTest(t: GlobalTestState) {
// Set up test environment
const { walletClient, bank, exchange, merchant } =
await createSimpleTestkudosEnvironmentV2(t);
// Withdraw digital cash into the wallet.
await withdrawViaBankV2(t, {
walletClient,
bank,
exchange,
amount: "TESTKUDOS:20",
});
await walletClient.call(WalletApiOperation.TestingWaitTransactionsFinal, {});
const order = {
summary: "Buy me!",
amount: "TESTKUDOS:5",
fulfillment_url: "taler://fulfillment-success/thx",
};
await makeTestPaymentV2(t, { walletClient, merchant, order });
await walletClient.call(WalletApiOperation.TestingWaitTransactionsFinal, {});
}
runSimplePaymentTest.suites = ["wallet"];

View File

@ -103,7 +103,6 @@ import { runPaymentTemplateTest } from "./test-payment-template.js";
import { runExchangeDepositTest } from "./test-exchange-deposit.js";
import { runPeerRepairTest } from "./test-peer-repair.js";
import { runPaymentShareTest } from "./test-payment-share.js";
import { runSimplePaymentTest } from "./test-simple-payment.js";
/**
* Test runner.
@ -130,7 +129,6 @@ const allTests: TestMainFunction[] = [
runClauseSchnorrTest,
runDenomUnofferedTest,
runDepositTest,
runSimplePaymentTest,
runExchangeManagementTest,
runExchangeTimetravelTest,
runFeeRegressionTest,

View File

@ -11,7 +11,6 @@
".": {
"node": "./lib/index.node.js",
"browser": "./lib/index.browser.js",
"qtart": "./lib/index.qtart.js",
"default": "./lib/index.js"
},
"./twrpc": {

View File

@ -1,27 +0,0 @@
/*
This file is part of GNU Taler
(C) 2021 Taler Systems S.A.
GNU Taler is free software; you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software
Foundation; either version 3, or (at your option) any later version.
GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
*/
import { setPRNG } from "./nacl-fast.js";
setPRNG(function (x: Uint8Array, n: number) {
// @ts-ignore
const va = globalThis._tart.randomBytes(n);
const v = new Uint8Array(va);
for (let i = 0; i < n; i++) x[i] = v[i];
for (let i = 0; i < v.length; i++) v[i] = 0;
});
export * from "./index.js";

View File

@ -48,7 +48,6 @@ import {
RefreshReason,
TalerErrorDetail,
TransactionIdStr,
TransactionStateFilter,
} from "./wallet-types.js";
export interface TransactionsRequest {
@ -66,8 +65,6 @@ export interface TransactionsRequest {
* If true, include all refreshes in the transactions list.
*/
includeRefreshes?: boolean;
filterByState?: TransactionStateFilter
}
export interface TransactionState {

View File

@ -2644,10 +2644,3 @@ export const codecForValidateIbanResponse = (): Codec<ValidateIbanResponse> =>
buildCodecForObject<ValidateIbanResponse>()
.property("valid", codecForBoolean())
.build("ValidateIbanResponse");
export type TransactionStateFilter = "nonfinal";
export interface TransactionRecordFilter {
onlyState?: TransactionStateFilter;
onlyCurrency?: string;
}

View File

@ -24,9 +24,7 @@ install_target = $(prefix)/lib/taler-wallet-cli
.PHONY: install install-nodeps deps
install-nodeps:
./build-node.mjs
@echo installing wallet CLI to $(install_target)
install -d $(prefix)/bin
install -d $(install_target)/build
install -d $(install_target)/bin
install -d $(install_target)/node_modules/taler-wallet-cli
install -d $(install_target)/node_modules/taler-wallet-cli/bin
@ -34,8 +32,6 @@ install-nodeps:
install ./dist/taler-wallet-cli-bundled.cjs $(install_target)/node_modules/taler-wallet-cli/dist/
install ./dist/taler-wallet-cli-bundled.cjs.map $(install_target)/node_modules/taler-wallet-cli/dist/
install ./bin/taler-wallet-cli.mjs $(install_target)/node_modules/taler-wallet-cli/bin/
install ../idb-bridge/node_modules/better-sqlite3/build/Release/better_sqlite3.node $(install_target)/build/ \
|| echo "sqlite3 unavailable, better-sqlite3 native module not found"
ln -sf $(install_target)/node_modules/taler-wallet-cli/bin/taler-wallet-cli.mjs $(prefix)/bin/taler-wallet-cli
deps:
pnpm install --frozen-lockfile --filter @gnu-taler/taler-wallet-cli...

View File

@ -2,8 +2,3 @@
This package provides `taler-wallet-cli`, the command-line interface for the
GNU Taler wallet.
## sqlite3 backend
To be able to use the sqlite3 backend, make sure that better-sqlite3
is installed as an optional dependency in the ../idb-bridge package.

View File

@ -1,8 +0,0 @@
#!/usr/bin/env node
// Execute the wallet CLI from the source directory.
// This script is meant for testing and must not
// be installed.
import { main } from '../lib/index.js';
main();

View File

@ -59,7 +59,7 @@ export const buildConfig = {
conditions: ["qtart"],
sourcemap: true,
// quickjs standard library
external: ["std", "os", "better-sqlite3"],
external: ["std", "os"],
define: {
__VERSION__: `"${_package.version}"`,
__GIT_HASH__: `"${GIT_HASH}"`,

View File

@ -258,6 +258,7 @@ async function createLocalWallet(
cryptoWorkerType: walletCliArgs.wallet.cryptoWorker as any,
config: {
features: {
batchWithdrawal: checkEnvFlag("TALER_WALLET_BATCH_WITHDRAWAL"),
},
testing: {
devModeActive: checkEnvFlag("TALER_WALLET_DEV_MODE"),

View File

@ -119,7 +119,7 @@ export const CURRENT_DB_CONFIG_KEY = "currentMainDbName";
* backwards-compatible way or object stores and indices
* are added.
*/
export const WALLET_DB_MINOR_VERSION = 10;
export const WALLET_DB_MINOR_VERSION = 9;
/**
* Ranges for operation status fields.
@ -2687,9 +2687,6 @@ export const WalletStoresV1 = {
}),
{
byProposalId: describeIndex("byProposalId", "proposalId"),
byStatus: describeIndex("byStatus", "status", {
versionAdded: 10,
}),
},
),
refundItems: describeStore(

View File

@ -16,7 +16,7 @@
import { WalletNotification } from "@gnu-taler/taler-util";
import { HttpRequestLibrary } from "@gnu-taler/taler-util/http";
import { WalletConfigParameter } from "./index.js";
import { WalletConfig, WalletConfigParameter } from "./index.js";
/**
* Helpers to initiate a wallet in a host environment.

View File

@ -27,7 +27,6 @@ import type { IDBFactory } from "@gnu-taler/idb-bridge";
import {
BridgeIDBFactory,
MemoryBackend,
createSqliteBackend,
shimIndexedDB,
} from "@gnu-taler/idb-bridge";
import { AccessStats } from "@gnu-taler/idb-bridge";
@ -40,21 +39,24 @@ import { createPlatformHttpLib } from "@gnu-taler/taler-util/http";
import { SetTimeoutTimerAPI } from "./util/timer.js";
import { Wallet } from "./wallet.js";
import { DefaultNodeWalletArgs, makeTempfileId } from "./host-common.js";
import { createNodeSqlite3Impl } from "@gnu-taler/idb-bridge/node-sqlite3-bindings";
const logger = new Logger("host-impl.node.ts");
interface MakeDbResult {
idbFactory: BridgeIDBFactory;
getStats: () => AccessStats;
}
async function makeFileDb(
/**
* Get a wallet instance with default settings for node.
*
* Extended version that allows getting DB stats.
*/
export async function createNativeWalletHost2(
args: DefaultNodeWalletArgs = {},
): Promise<MakeDbResult> {
): Promise<{
wallet: Wallet;
getDbStats: () => AccessStats;
}> {
BridgeIDBFactory.enableTracing = false;
const myBackend = new MemoryBackend();
myBackend.enableTracing = false;
const storagePath = args.persistentStoragePath;
if (storagePath) {
try {
@ -94,41 +96,8 @@ async function makeFileDb(
BridgeIDBFactory.enableTracing = false;
const myBridgeIdbFactory = new BridgeIDBFactory(myBackend);
return {
idbFactory: myBridgeIdbFactory,
getStats: () => myBackend.accessStats,
};
}
const myIdbFactory: IDBFactory = myBridgeIdbFactory as any as IDBFactory;
async function makeSqliteDb(
args: DefaultNodeWalletArgs,
): Promise<MakeDbResult> {
BridgeIDBFactory.enableTracing = false;
const imp = await createNodeSqlite3Impl();
const myBackend = await createSqliteBackend(imp, {
filename: args.persistentStoragePath ?? ":memory:",
});
myBackend.enableTracing = false;
const myBridgeIdbFactory = new BridgeIDBFactory(myBackend);
return {
getStats() {
throw Error("not implemented");
},
idbFactory: myBridgeIdbFactory,
};
}
/**
* Get a wallet instance with default settings for node.
*
* Extended version that allows getting DB stats.
*/
export async function createNativeWalletHost2(
args: DefaultNodeWalletArgs = {},
): Promise<{
wallet: Wallet;
getDbStats: () => AccessStats;
}> {
let myHttpLib;
if (args.httpLib) {
myHttpLib = args.httpLib;
@ -146,19 +115,7 @@ export async function createNativeWalletHost2(
);
};
let dbResp: MakeDbResult;
if (args.persistentStoragePath &&args.persistentStoragePath.endsWith(".json")) {
logger.info("using legacy file-based DB backend");
dbResp = await makeFileDb(args);
} else {
logger.info("using sqlite3 DB backend");
dbResp = await makeSqliteDb(args);
}
const myIdbFactory: IDBFactory = dbResp.idbFactory as any as IDBFactory;
shimIndexedDB(dbResp.idbFactory);
shimIndexedDB(myBridgeIdbFactory);
const myDb = await openTalerDatabase(myIdbFactory, myVersionChange);
@ -201,6 +158,6 @@ export async function createNativeWalletHost2(
}
return {
wallet: w,
getDbStats: dbResp.getStats,
getDbStats: () => myBackend.accessStats,
};
}

View File

@ -22,17 +22,11 @@
/**
* Imports.
*/
import type {
IDBFactory,
ResultRow,
Sqlite3Interface,
Sqlite3Statement,
} from "@gnu-taler/idb-bridge";
import type { IDBFactory } from "@gnu-taler/idb-bridge";
// eslint-disable-next-line no-duplicate-imports
import {
BridgeIDBFactory,
MemoryBackend,
createSqliteBackend,
shimIndexedDB,
} from "@gnu-taler/idb-bridge";
import { AccessStats } from "@gnu-taler/idb-bridge";
@ -47,78 +41,12 @@ import { DefaultNodeWalletArgs, makeTempfileId } from "./host-common.js";
const logger = new Logger("host-impl.qtart.ts");
interface MakeDbResult {
idbFactory: BridgeIDBFactory;
getStats: () => AccessStats;
}
let numStmt = 0;
export async function createQtartSqlite3Impl(): Promise<Sqlite3Interface> {
const tart: any = (globalThis as any)._tart;
if (!tart) {
throw Error("globalThis._qtart not defined");
}
return {
open(filename: string) {
const internalDbHandle = tart.sqlite3Open(filename);
return {
internalDbHandle,
close() {
tart.sqlite3Close(internalDbHandle);
},
prepare(stmtStr): Sqlite3Statement {
const stmtHandle = tart.sqlite3Prepare(internalDbHandle, stmtStr);
return {
internalStatement: stmtHandle,
getAll(params): ResultRow[] {
numStmt++;
return tart.sqlite3StmtGetAll(stmtHandle, params);
},
getFirst(params): ResultRow | undefined {
numStmt++;
return tart.sqlite3StmtGetFirst(stmtHandle, params);
},
run(params) {
numStmt++;
return tart.sqlite3StmtRun(stmtHandle, params);
},
};
},
exec(sqlStr): void {
numStmt++;
tart.sqlite3Exec(internalDbHandle, sqlStr);
},
};
},
};
}
async function makeSqliteDb(
args: DefaultNodeWalletArgs,
): Promise<MakeDbResult> {
BridgeIDBFactory.enableTracing = false;
const imp = await createQtartSqlite3Impl();
const myBackend = await createSqliteBackend(imp, {
filename: args.persistentStoragePath ?? ":memory:",
});
myBackend.trackStats = true;
myBackend.enableTracing = false;
const myBridgeIdbFactory = new BridgeIDBFactory(myBackend);
return {
getStats() {
return {
...myBackend.accessStats,
primitiveStatements: numStmt,
}
},
idbFactory: myBridgeIdbFactory,
};
}
async function makeFileDb(
export async function createNativeWalletHost2(
args: DefaultNodeWalletArgs = {},
): Promise<MakeDbResult> {
): Promise<{
wallet: Wallet;
getDbStats: () => AccessStats;
}> {
BridgeIDBFactory.enableTracing = false;
const myBackend = new MemoryBackend();
myBackend.enableTracing = false;
@ -150,34 +78,12 @@ async function makeFileDb(
};
}
const myBridgeIdbFactory = new BridgeIDBFactory(myBackend);
return {
idbFactory: myBridgeIdbFactory,
getStats: () => myBackend.accessStats,
};
}
logger.info("done processing storage path");
export async function createNativeWalletHost2(
args: DefaultNodeWalletArgs = {},
): Promise<{
wallet: Wallet;
getDbStats: () => AccessStats;
}> {
BridgeIDBFactory.enableTracing = false;
let dbResp: MakeDbResult;
if (args.persistentStoragePath && args.persistentStoragePath.endsWith(".json")) {
logger.info("using JSON file DB backend (slow!)");
dbResp = await makeFileDb(args);
} else {
logger.info("using sqlite3 DB backend (experimental!)");
dbResp = await makeSqliteDb(args)
}
const myIdbFactory: IDBFactory = dbResp.idbFactory as any as IDBFactory;
shimIndexedDB(dbResp.idbFactory);
const myBridgeIdbFactory = new BridgeIDBFactory(myBackend);
const myIdbFactory: IDBFactory = myBridgeIdbFactory as any as IDBFactory;
let myHttpLib;
if (args.httpLib) {
@ -196,6 +102,8 @@ export async function createNativeWalletHost2(
);
};
shimIndexedDB(myBridgeIdbFactory);
const myDb = await openTalerDatabase(myIdbFactory, myVersionChange);
let workerFactory;
@ -216,6 +124,6 @@ export async function createNativeWalletHost2(
}
return {
wallet: w,
getDbStats: dbResp.getStats,
getDbStats: () => myBackend.accessStats,
};
}

View File

@ -16,6 +16,7 @@
import { DefaultNodeWalletArgs } from "./host-common.js";
import { Wallet } from "./index.js";
import * as hostImpl from "#host-impl";
import { AccessStats } from "@gnu-taler/idb-bridge";

View File

@ -420,10 +420,10 @@ async function handlePeerPullCreditCreatePurse(
pullIni.exchangeBaseUrl,
);
const httpResp = await ws.http.fetch(reservePurseMergeUrl.href, {
method: "POST",
body: reservePurseReqBody,
});
const httpResp = await ws.http.postJson(
reservePurseMergeUrl.href,
reservePurseReqBody,
);
if (httpResp.status === HttpStatusCode.UnavailableForLegalReasons) {
const respJson = await httpResp.json();
@ -775,7 +775,7 @@ export async function initiatePeerPullPayment(
contractPub: contractKeyPair.pub,
withdrawalGroupId,
estimatedAmountEffective: wi.withdrawalAmountEffective,
};
}
await tx.peerPullPaymentInitiations.put(ppi);
const oldTxState: TransactionState = {
major: TransactionMajorState.None,

View File

@ -34,24 +34,13 @@ import {
WithdrawalGroupStatus,
RewardRecordStatus,
DepositOperationStatus,
RefreshGroupRecord,
WithdrawalGroupRecord,
DepositGroupRecord,
RewardRecord,
PurchaseRecord,
PeerPullPaymentInitiationRecord,
PeerPullPaymentIncomingRecord,
PeerPushPaymentInitiationRecord,
PeerPushPaymentIncomingRecord,
RefundGroupRecord,
RefundGroupStatus,
} from "../db.js";
import {
PendingOperationsResponse,
PendingTaskType,
TaskId,
} from "../pending-types.js";
import { AbsoluteTime, TransactionRecordFilter } from "@gnu-taler/taler-util";
import { AbsoluteTime } from "@gnu-taler/taler-util";
import { InternalWalletState } from "../internal-wallet-state.js";
import { GetReadOnlyAccess } from "../util/query.js";
import { GlobalIDB } from "@gnu-taler/idb-bridge";
@ -116,32 +105,6 @@ async function gatherExchangePending(
});
}
/**
* Iterate refresh records based on a filter.
*/
export async function iterRecordsForRefresh(
tx: GetReadOnlyAccess<{
refreshGroups: typeof WalletStoresV1.refreshGroups;
}>,
filter: TransactionRecordFilter,
f: (r: RefreshGroupRecord) => Promise<void>,
): Promise<void> {
let refreshGroups: RefreshGroupRecord[];
if (filter.onlyState === "nonfinal") {
const keyRange = GlobalIDB.KeyRange.bound(
OperationStatusRange.ACTIVE_START,
OperationStatusRange.ACTIVE_END,
);
refreshGroups = await tx.refreshGroups.indexes.byStatus.getAll(keyRange);
} else {
refreshGroups = await tx.refreshGroups.indexes.byStatus.getAll();
}
for (const r of refreshGroups) {
await f(r);
}
}
async function gatherRefreshPending(
ws: InternalWalletState,
tx: GetReadOnlyAccess<{
@ -151,13 +114,22 @@ async function gatherRefreshPending(
now: AbsoluteTime,
resp: PendingOperationsResponse,
): Promise<void> {
await iterRecordsForRefresh(tx, { onlyState: "nonfinal" }, async (r) => {
const keyRange = GlobalIDB.KeyRange.bound(
OperationStatusRange.ACTIVE_START,
OperationStatusRange.ACTIVE_END,
);
const refreshGroups = await tx.refreshGroups.indexes.byStatus.getAll(
keyRange,
);
for (const r of refreshGroups) {
if (r.timestampFinished) {
return;
}
const opId = TaskIdentifiers.forRefresh(r);
const retryRecord = await tx.operationRetries.get(opId);
const timestampDue = retryRecord?.retryInfo.nextRetry ?? AbsoluteTime.now();
resp.pendingOperations.push({
type: PendingTaskType.Refresh,
...getPendingCommon(ws, opId, timestampDue),
@ -168,30 +140,6 @@ async function gatherRefreshPending(
),
retryInfo: retryRecord?.retryInfo,
});
});
}
export async function iterRecordsForWithdrawal(
tx: GetReadOnlyAccess<{
withdrawalGroups: typeof WalletStoresV1.withdrawalGroups;
}>,
filter: TransactionRecordFilter,
f: (r: WithdrawalGroupRecord) => Promise<void>,
): Promise<void> {
let withdrawalGroupRecords: WithdrawalGroupRecord[];
if (filter.onlyState === "nonfinal") {
const range = GlobalIDB.KeyRange.bound(
WithdrawalGroupStatus.PendingRegisteringBank,
WithdrawalGroupStatus.PendingAml,
);
withdrawalGroupRecords =
await tx.withdrawalGroups.indexes.byStatus.getAll(range);
} else {
withdrawalGroupRecords =
await tx.withdrawalGroups.indexes.byStatus.getAll();
}
for (const wgr of withdrawalGroupRecords) {
await f(wgr);
}
}
@ -205,7 +153,12 @@ async function gatherWithdrawalPending(
now: AbsoluteTime,
resp: PendingOperationsResponse,
): Promise<void> {
await iterRecordsForWithdrawal(tx, { onlyState: "nonfinal" }, async (wsr) => {
const range = GlobalIDB.KeyRange.bound(
WithdrawalGroupStatus.PendingRegisteringBank,
WithdrawalGroupStatus.PendingAml,
);
const wsrs = await tx.withdrawalGroups.indexes.byStatus.getAll(range);
for (const wsr of wsrs) {
const opTag = TaskIdentifiers.forWithdrawal(wsr);
let opr = await tx.operationRetries.get(opTag);
const now = AbsoluteTime.now();
@ -231,30 +184,6 @@ async function gatherWithdrawalPending(
lastError: opr.lastError,
retryInfo: opr.retryInfo,
});
});
}
export async function iterRecordsForDeposit(
tx: GetReadOnlyAccess<{
depositGroups: typeof WalletStoresV1.depositGroups;
}>,
filter: TransactionRecordFilter,
f: (r: DepositGroupRecord) => Promise<void>,
): Promise<void> {
let dgs: DepositGroupRecord[];
if (filter.onlyState === "nonfinal") {
dgs = await tx.depositGroups.indexes.byStatus.getAll(
GlobalIDB.KeyRange.bound(
DepositOperationStatus.PendingDeposit,
DepositOperationStatus.PendingKyc,
),
);
} else {
dgs = await tx.depositGroups.indexes.byStatus.getAll();
}
for (const dg of dgs) {
await f(dg);
}
}
@ -267,7 +196,16 @@ async function gatherDepositPending(
now: AbsoluteTime,
resp: PendingOperationsResponse,
): Promise<void> {
await iterRecordsForDeposit(tx, { onlyState: "nonfinal" }, async (dg) => {
const dgs = await tx.depositGroups.indexes.byStatus.getAll(
GlobalIDB.KeyRange.bound(
DepositOperationStatus.PendingDeposit,
DepositOperationStatus.PendingKyc,
),
);
for (const dg of dgs) {
if (dg.timestampFinished) {
return;
}
let deposited = true;
for (const d of dg.depositedPerCoin) {
if (!d) {
@ -288,28 +226,10 @@ async function gatherDepositPending(
lastError: retryRecord?.lastError,
retryInfo: retryRecord?.retryInfo,
});
});
}
export async function iterRecordsForReward(
tx: GetReadOnlyAccess<{
rewards: typeof WalletStoresV1.rewards;
}>,
filter: TransactionRecordFilter,
f: (r: RewardRecord) => Promise<void>,
): Promise<void> {
if (filter.onlyState === "nonfinal") {
const range = GlobalIDB.KeyRange.bound(
RewardRecordStatus.PendingPickup,
RewardRecordStatus.PendingPickup,
);
await tx.rewards.indexes.byStatus.iter(range).forEachAsync(f);
} else {
await tx.rewards.indexes.byStatus.iter().forEachAsync(f);
}
}
async function gatherRewardPending(
async function gatherTipPending(
ws: InternalWalletState,
tx: GetReadOnlyAccess<{
rewards: typeof WalletStoresV1.rewards;
@ -318,7 +238,15 @@ async function gatherRewardPending(
now: AbsoluteTime,
resp: PendingOperationsResponse,
): Promise<void> {
await iterRecordsForReward(tx, { onlyState: "nonfinal" }, async (tip) => {
const range = GlobalIDB.KeyRange.bound(
RewardRecordStatus.PendingPickup,
RewardRecordStatus.PendingPickup,
);
await tx.rewards.indexes.byStatus.iter(range).forEachAsync(async (tip) => {
// FIXME: The tip record needs a proper status field!
if (tip.pickedUpTimestamp) {
return;
}
const opId = TaskIdentifiers.forTipPickup(tip);
const retryRecord = await tx.operationRetries.get(opId);
const timestampDue = retryRecord?.retryInfo.nextRetry ?? AbsoluteTime.now();
@ -336,43 +264,6 @@ async function gatherRewardPending(
});
}
export async function iterRecordsForRefund(
tx: GetReadOnlyAccess<{
refundGroups: typeof WalletStoresV1.refundGroups;
}>,
filter: TransactionRecordFilter,
f: (r: RefundGroupRecord) => Promise<void>,
): Promise<void> {
if (filter.onlyState === "nonfinal") {
const keyRange = GlobalIDB.KeyRange.only(
RefundGroupStatus.Pending
);
await tx.refundGroups.indexes.byStatus
.iter(keyRange)
.forEachAsync(f);
} else {
await tx.refundGroups.iter().forEachAsync(f);
}
}
export async function iterRecordsForPurchase(
tx: GetReadOnlyAccess<{
purchases: typeof WalletStoresV1.purchases;
}>,
filter: TransactionRecordFilter,
f: (r: PurchaseRecord) => Promise<void>,
): Promise<void> {
if (filter.onlyState === "nonfinal") {
const keyRange = GlobalIDB.KeyRange.bound(
PurchaseStatus.PendingDownloadingProposal,
PurchaseStatus.PendingAcceptRefund,
);
await tx.purchases.indexes.byStatus.iter(keyRange).forEachAsync(f);
} else {
await tx.purchases.indexes.byStatus.iter().forEachAsync(f);
}
}
async function gatherPurchasePending(
ws: InternalWalletState,
tx: GetReadOnlyAccess<{
@ -382,10 +273,17 @@ async function gatherPurchasePending(
now: AbsoluteTime,
resp: PendingOperationsResponse,
): Promise<void> {
await iterRecordsForPurchase(tx, { onlyState: "nonfinal" }, async (pr) => {
const keyRange = GlobalIDB.KeyRange.bound(
PurchaseStatus.PendingDownloadingProposal,
PurchaseStatus.PendingAcceptRefund,
);
await tx.purchases.indexes.byStatus
.iter(keyRange)
.forEachAsync(async (pr) => {
const opId = TaskIdentifiers.forPay(pr);
const retryRecord = await tx.operationRetries.get(opId);
const timestampDue = retryRecord?.retryInfo.nextRetry ?? AbsoluteTime.now();
const timestampDue =
retryRecord?.retryInfo.nextRetry ?? AbsoluteTime.now();
resp.pendingOperations.push({
type: PendingTaskType.Purchase,
...getPendingCommon(ws, opId, timestampDue),
@ -464,26 +362,6 @@ async function gatherBackupPending(
});
}
export async function iterRecordsForPeerPullInitiation(
tx: GetReadOnlyAccess<{
peerPullPaymentInitiations: typeof WalletStoresV1.peerPullPaymentInitiations;
}>,
filter: TransactionRecordFilter,
f: (r: PeerPullPaymentInitiationRecord) => Promise<void>,
): Promise<void> {
if (filter.onlyState === "nonfinal") {
const keyRange = GlobalIDB.KeyRange.bound(
PeerPullPaymentInitiationStatus.PendingCreatePurse,
PeerPullPaymentInitiationStatus.AbortingDeletePurse,
);
await tx.peerPullPaymentInitiations.indexes.byStatus
.iter(keyRange)
.forEachAsync(f);
} else {
await tx.peerPullPaymentInitiations.indexes.byStatus.iter().forEachAsync(f);
}
}
async function gatherPeerPullInitiationPending(
ws: InternalWalletState,
tx: GetReadOnlyAccess<{
@ -493,10 +371,13 @@ async function gatherPeerPullInitiationPending(
now: AbsoluteTime,
resp: PendingOperationsResponse,
): Promise<void> {
await iterRecordsForPeerPullInitiation(
tx,
{ onlyState: "nonfinal" },
async (pi) => {
const keyRange = GlobalIDB.KeyRange.bound(
PeerPullPaymentInitiationStatus.PendingCreatePurse,
PeerPullPaymentInitiationStatus.AbortingDeletePurse,
);
await tx.peerPullPaymentInitiations.indexes.byStatus
.iter(keyRange)
.forEachAsync(async (pi) => {
const opId = TaskIdentifiers.forPeerPullPaymentInitiation(pi);
const retryRecord = await tx.operationRetries.get(opId);
const timestampDue =
@ -508,28 +389,7 @@ async function gatherPeerPullInitiationPending(
retryInfo: retryRecord?.retryInfo,
pursePub: pi.pursePub,
});
},
);
}
export async function iterRecordsForPeerPullDebit(
tx: GetReadOnlyAccess<{
peerPullPaymentIncoming: typeof WalletStoresV1.peerPullPaymentIncoming;
}>,
filter: TransactionRecordFilter,
f: (r: PeerPullPaymentIncomingRecord) => Promise<void>,
): Promise<void> {
if (filter.onlyState === "nonfinal") {
const keyRange = GlobalIDB.KeyRange.bound(
PeerPullDebitRecordStatus.PendingDeposit,
PeerPullDebitRecordStatus.AbortingRefresh,
);
await tx.peerPullPaymentIncoming.indexes.byStatus
.iter(keyRange)
.forEachAsync(f);
} else {
await tx.peerPullPaymentIncoming.indexes.byStatus.iter().forEachAsync(f);
}
});
}
async function gatherPeerPullDebitPending(
@ -541,10 +401,13 @@ async function gatherPeerPullDebitPending(
now: AbsoluteTime,
resp: PendingOperationsResponse,
): Promise<void> {
await iterRecordsForPeerPullDebit(
tx,
{ onlyState: "nonfinal" },
async (pi) => {
const keyRange = GlobalIDB.KeyRange.bound(
PeerPullDebitRecordStatus.PendingDeposit,
PeerPullDebitRecordStatus.AbortingRefresh,
);
await tx.peerPullPaymentIncoming.indexes.byStatus
.iter(keyRange)
.forEachAsync(async (pi) => {
const opId = TaskIdentifiers.forPeerPullPaymentDebit(pi);
const retryRecord = await tx.operationRetries.get(opId);
const timestampDue =
@ -556,28 +419,7 @@ async function gatherPeerPullDebitPending(
retryInfo: retryRecord?.retryInfo,
peerPullPaymentIncomingId: pi.peerPullPaymentIncomingId,
});
},
);
}
export async function iterRecordsForPeerPushInitiation(
tx: GetReadOnlyAccess<{
peerPushPaymentInitiations: typeof WalletStoresV1.peerPushPaymentInitiations;
}>,
filter: TransactionRecordFilter,
f: (r: PeerPushPaymentInitiationRecord) => Promise<void>,
): Promise<void> {
if (filter.onlyState === "nonfinal") {
const keyRange = GlobalIDB.KeyRange.bound(
PeerPushPaymentInitiationStatus.PendingCreatePurse,
PeerPushPaymentInitiationStatus.AbortingRefresh,
);
await tx.peerPushPaymentInitiations.indexes.byStatus
.iter(keyRange)
.forEachAsync(f);
} else {
await tx.peerPushPaymentInitiations.indexes.byStatus.iter().forEachAsync(f);
}
});
}
async function gatherPeerPushInitiationPending(
@ -589,10 +431,13 @@ async function gatherPeerPushInitiationPending(
now: AbsoluteTime,
resp: PendingOperationsResponse,
): Promise<void> {
await iterRecordsForPeerPushInitiation(
tx,
{ onlyState: "nonfinal" },
async (pi) => {
const keyRange = GlobalIDB.KeyRange.bound(
PeerPushPaymentInitiationStatus.PendingCreatePurse,
PeerPushPaymentInitiationStatus.AbortingRefresh,
);
await tx.peerPushPaymentInitiations.indexes.byStatus
.iter(keyRange)
.forEachAsync(async (pi) => {
const opId = TaskIdentifiers.forPeerPushPaymentInitiation(pi);
const retryRecord = await tx.operationRetries.get(opId);
const timestampDue =
@ -604,28 +449,7 @@ async function gatherPeerPushInitiationPending(
retryInfo: retryRecord?.retryInfo,
pursePub: pi.pursePub,
});
},
);
}
export async function iterRecordsForPeerPushCredit(
tx: GetReadOnlyAccess<{
peerPushPaymentIncoming: typeof WalletStoresV1.peerPushPaymentIncoming;
}>,
filter: TransactionRecordFilter,
f: (r: PeerPushPaymentIncomingRecord) => Promise<void>,
): Promise<void> {
if (filter.onlyState === "nonfinal") {
const keyRange = GlobalIDB.KeyRange.bound(
PeerPushPaymentIncomingStatus.PendingMerge,
PeerPushPaymentIncomingStatus.PendingWithdrawing,
);
await tx.peerPushPaymentIncoming.indexes.byStatus
.iter(keyRange)
.forEachAsync(f);
} else {
await tx.peerPushPaymentIncoming.indexes.byStatus.iter().forEachAsync(f);
}
});
}
async function gatherPeerPushCreditPending(
@ -641,10 +465,9 @@ async function gatherPeerPushCreditPending(
PeerPushPaymentIncomingStatus.PendingMerge,
PeerPushPaymentIncomingStatus.PendingWithdrawing,
);
await iterRecordsForPeerPushCredit(
tx,
{ onlyState: "nonfinal" },
async (pi) => {
await tx.peerPushPaymentIncoming.indexes.byStatus
.iter(keyRange)
.forEachAsync(async (pi) => {
const opId = TaskIdentifiers.forPeerPushCredit(pi);
const retryRecord = await tx.operationRetries.get(opId);
const timestampDue =
@ -656,8 +479,7 @@ async function gatherPeerPushCreditPending(
retryInfo: retryRecord?.retryInfo,
peerPushPaymentIncomingId: pi.peerPushPaymentIncomingId,
});
},
);
});
}
export async function getPendingOperations(
@ -691,7 +513,7 @@ export async function getPendingOperations(
await gatherRefreshPending(ws, tx, now, resp);
await gatherWithdrawalPending(ws, tx, now, resp);
await gatherDepositPending(ws, tx, now, resp);
await gatherRewardPending(ws, tx, now, resp);
await gatherTipPending(ws, tx, now, resp);
await gatherPurchasePending(ws, tx, now, resp);
await gatherRecoupPending(ws, tx, now, resp);
await gatherBackupPending(ws, tx, now, resp);

View File

@ -472,15 +472,12 @@ export async function waitUntilDone(ws: InternalWalletState): Promise<void> {
p = openPromise();
const txs = await getTransactions(ws, {
includeRefreshes: true,
filterByState: "nonfinal",
});
let finished = true;
for (const tx of txs.transactions) {
switch (tx.txState.major) {
case TransactionMajorState.Pending:
case TransactionMajorState.Aborting:
case TransactionMajorState.Suspended:
case TransactionMajorState.SuspendedAborting:
finished = false;
logger.info(
`continuing waiting, ${tx.transactionId} in ${tx.txState.major}(${tx.txState.minor})`,

View File

@ -36,7 +36,6 @@ import {
TransactionByIdRequest,
TransactionIdStr,
TransactionMajorState,
TransactionRecordFilter,
TransactionsRequest,
TransactionsResponse,
TransactionState,
@ -154,7 +153,6 @@ import {
resumePeerPushDebitTransaction,
abortPeerPushDebitTransaction,
} from "./pay-peer-push-debit.js";
import { iterRecordsForDeposit, iterRecordsForPeerPullDebit, iterRecordsForPeerPullInitiation, iterRecordsForPeerPushCredit, iterRecordsForPeerPushInitiation, iterRecordsForPurchase, iterRecordsForRefresh, iterRecordsForRefund, iterRecordsForReward, iterRecordsForWithdrawal } from "./pending.js";
const logger = new Logger("taler-wallet-core:transactions.ts");
@ -931,11 +929,6 @@ export async function getTransactions(
): Promise<TransactionsResponse> {
const transactions: Transaction[] = [];
const filter: TransactionRecordFilter = {};
if (transactionsRequest?.filterByState) {
filter.onlyState = transactionsRequest.filterByState;
}
await ws.db
.mktx((x) => [
x.coins,
@ -959,7 +952,7 @@ export async function getTransactions(
x.refundGroups,
])
.runReadOnly(async (tx) => {
await iterRecordsForPeerPushInitiation(tx, filter, async (pi) => {
tx.peerPushPaymentInitiations.iter().forEachAsync(async (pi) => {
const amount = Amounts.parseOrThrow(pi.amount);
if (shouldSkipCurrency(transactionsRequest, amount.currency)) {
@ -975,7 +968,7 @@ export async function getTransactions(
);
});
await iterRecordsForPeerPullDebit(tx, filter, async (pi) => {
tx.peerPullPaymentIncoming.iter().forEachAsync(async (pi) => {
const amount = Amounts.parseOrThrow(pi.contractTerms.amount);
if (shouldSkipCurrency(transactionsRequest, amount.currency)) {
return;
@ -993,7 +986,7 @@ export async function getTransactions(
transactions.push(buildTransactionForPullPaymentDebit(pi));
});
await iterRecordsForPeerPushCredit(tx, filter, async (pi) => {
tx.peerPushPaymentIncoming.iter().forEachAsync(async (pi) => {
if (!pi.currency) {
// Legacy transaction
return;
@ -1034,7 +1027,7 @@ export async function getTransactions(
);
});
await iterRecordsForPeerPullInitiation(tx, filter, async (pi) => {
tx.peerPullPaymentInitiations.iter().forEachAsync(async (pi) => {
const currency = Amounts.currencyOf(pi.amount);
if (shouldSkipCurrency(transactionsRequest, currency)) {
return;
@ -1067,7 +1060,7 @@ export async function getTransactions(
);
});
await iterRecordsForRefund(tx, filter, async (refundGroup) => {
tx.refundGroups.iter().forEachAsync(async (refundGroup) => {
const currency = Amounts.currencyOf(refundGroup.amountRaw);
if (shouldSkipCurrency(transactionsRequest, currency)) {
return;
@ -1079,7 +1072,7 @@ export async function getTransactions(
transactions.push(buildTransactionForRefund(refundGroup, contractData));
});
await iterRecordsForRefresh(tx, filter, async (rg) => {
tx.refreshGroups.iter().forEachAsync(async (rg) => {
if (shouldSkipCurrency(transactionsRequest, rg.currency)) {
return;
}
@ -1099,7 +1092,7 @@ export async function getTransactions(
}
});
await iterRecordsForWithdrawal(tx, filter ,async (wsr) => {
tx.withdrawalGroups.iter().forEachAsync(async (wsr) => {
if (
shouldSkipCurrency(
transactionsRequest,
@ -1153,7 +1146,7 @@ export async function getTransactions(
}
});
await iterRecordsForDeposit(tx, filter, async (dg) => {
tx.depositGroups.iter().forEachAsync(async (dg) => {
const amount = Amounts.parseOrThrow(dg.contractTermsRaw.amount);
if (shouldSkipCurrency(transactionsRequest, amount.currency)) {
return;
@ -1164,7 +1157,7 @@ export async function getTransactions(
transactions.push(buildTransactionForDeposit(dg, retryRecord));
});
await iterRecordsForPurchase(tx, filter, async (purchase) => {
tx.purchases.iter().forEachAsync(async (purchase) => {
const download = purchase.download;
if (!download) {
return;
@ -1207,7 +1200,7 @@ export async function getTransactions(
);
});
await iterRecordsForReward(tx, filter, async (tipRecord) => {
tx.rewards.iter().forEachAsync(async (tipRecord) => {
if (
shouldSkipCurrency(
transactionsRequest,
@ -1916,5 +1909,4 @@ export function notifyTransition(
transactionId,
});
}
ws.workAvailable.trigger();
}

View File

@ -714,6 +714,12 @@ async function processPlanchetGenerate(
}
interface WithdrawalRequestBatchArgs {
/**
* Use the batched request on the network level.
* Not supported by older exchanges.
*/
useBatchRequest: boolean;
coinStartIndex: number;
batchSize: number;
@ -922,6 +928,9 @@ async function processPlanchetExchangeBatchRequest(
// FIXME: handle individual error codes better!
// FIXME[oec]: add age-withdraw-request here
if (args.useBatchRequest) {
const reqUrl = new URL(
`reserves/${withdrawalGroup.reservePub}/batch-withdraw`,
withdrawalGroup.exchangeBaseUrl,
@ -947,6 +956,53 @@ async function processPlanchetExchangeBatchRequest(
coinIdxs: [],
};
}
} else {
// We emulate the batch response here by making multiple individual requests
const responses: ExchangeWithdrawBatchResponse = {
ev_sigs: [],
};
const responseCoinIdxs: number[] = [];
for (let i = 0; i < batchReq.planchets.length; i++) {
try {
const p = batchReq.planchets[i];
const reqUrl = new URL(
`reserves/${withdrawalGroup.reservePub}/withdraw`,
withdrawalGroup.exchangeBaseUrl,
).href;
const resp = await ws.http.fetch(reqUrl, { method: "POST", body: p });
if (resp.status === HttpStatusCode.UnavailableForLegalReasons) {
await handleKycRequired(
ws,
withdrawalGroup,
resp,
i,
requestCoinIdxs,
);
// We still return blinded coins that we could actually withdraw.
return {
coinIdxs: responseCoinIdxs,
batchResp: responses,
};
}
const r = await readSuccessResponseJsonOrThrow(
resp,
codecForWithdrawResponse(),
);
responses.ev_sigs.push(r);
responseCoinIdxs.push(requestCoinIdxs[i]);
} catch (e) {
if (e instanceof TalerError) {
logger.warn(`withdrawing planchet failed: ${j2s(e.errorDetail)}`);
logger.warn(`planchet denom pub hash: ${batchReq.planchets[i].denom_pub_hash}`);
}
await storeCoinError(e, requestCoinIdxs[i]);
}
}
return {
coinIdxs: responseCoinIdxs,
batchResp: responses,
};
}
}
async function processPlanchetVerifyAndStoreCoin(
@ -1499,6 +1555,7 @@ async function processWithdrawalGroupPendingReady(
const resp = await processPlanchetExchangeBatchRequest(ws, wgContext, {
batchSize: maxBatchSize,
coinStartIndex: i,
useBatchRequest: ws.config.features.batchWithdrawal,
});
let work: Promise<void>[] = [];
work = [];

View File

@ -23,8 +23,6 @@ export interface OpenedPromise<T> {
/**
* Get an unresolved promise together with its extracted resolve / reject
* function.
*
* Recent ECMAScript proposals also call this a promise capability.
*/
export function openPromise<T>(): OpenedPromise<T> {
let resolve: ((x?: any) => void) | null = null;
@ -41,20 +39,22 @@ export function openPromise<T>(): OpenedPromise<T> {
}
export class AsyncCondition {
private promCap?: OpenedPromise<void> = undefined;
constructor() {}
private _waitPromise: Promise<void>;
private _resolveWaitPromise: (val: void) => void;
constructor() {
const op = openPromise<void>();
this._waitPromise = op.promise;
this._resolveWaitPromise = op.resolve;
}
wait(): Promise<void> {
if (!this.promCap) {
this.promCap = openPromise<void>();
}
return this.promCap.promise;
return this._waitPromise;
}
trigger(): void {
if (this.promCap) {
this.promCap.resolve();
}
this.promCap = undefined;
this._resolveWaitPromise();
const op = openPromise<void>();
this._waitPromise = op.promise;
this._resolveWaitPromise = op.resolve;
}
}

View File

@ -338,7 +338,7 @@ interface IndexReadOnlyAccessor<RecordType> {
iter(query?: IDBKeyRange | IDBValidKey): ResultStream<RecordType>;
get(query: IDBValidKey): Promise<RecordType | undefined>;
getAll(
query?: IDBKeyRange | IDBValidKey,
query: IDBKeyRange | IDBValidKey,
count?: number,
): Promise<RecordType[]>;
}
@ -351,7 +351,7 @@ interface IndexReadWriteAccessor<RecordType> {
iter(query: IDBKeyRange | IDBValidKey): ResultStream<RecordType>;
get(query: IDBValidKey): Promise<RecordType | undefined>;
getAll(
query?: IDBKeyRange | IDBValidKey,
query: IDBKeyRange | IDBValidKey,
count?: number,
): Promise<RecordType[]>;
}

View File

@ -272,6 +272,7 @@ export interface WalletConfig {
* Configurations values that may be safe to show to the user
*/
features: {
batchWithdrawal: boolean;
allowHttp: boolean;
};
}

View File

@ -479,7 +479,6 @@ async function runTaskLoop(
// Wait until either the timeout, or we are notified (via the latch)
// that more work might be available.
await Promise.race([timeout, ws.workAvailable.wait()]);
logger.trace(`done waiting for available work`);
} else {
logger.trace(
`running ${pending.pendingOperations.length} pending operations`,
@ -1696,6 +1695,7 @@ export class Wallet {
],
},
features: {
batchWithdrawal: false,
allowHttp: false,
},
testing: {

View File

@ -51,7 +51,7 @@ export const buildConfig = {
target: [
'es2020'
],
external: ["os", "std", "better-sqlite3"],
external: ["os", "std"],
format: 'esm',
platform: 'neutral',
mainFields: ["module", "main"],

View File

@ -27,9 +27,9 @@ import {
CoreApiResponseSuccess,
getErrorDetailFromException,
InitRequest,
j2s,
Logger,
setGlobalLogLevelFromString,
setPRNG,
WalletNotification,
} from "@gnu-taler/taler-util";
import { createPlatformHttpLib } from "@gnu-taler/taler-util/http";
@ -47,11 +47,20 @@ import {
getRecoveryStartState,
discoverPolicies,
mergeDiscoveryAggregate,
ReducerState,
} from "@gnu-taler/anastasis-core";
import { userIdentifierDerive } from "@gnu-taler/anastasis-core/lib/crypto.js";
setGlobalLogLevelFromString("trace");
setPRNG(function (x: Uint8Array, n: number) {
// @ts-ignore
const va = globalThis._tart.randomBytes(n);
const v = new Uint8Array(va);
for (let i = 0; i < n; i++) x[i] = v[i];
for (let i = 0; i < v.length; i++) v[i] = 0;
});
const logger = new Logger("taler-wallet-embedded/index.ts");
/**
@ -213,8 +222,6 @@ async function handleAnastasisRequest(
cursor: discoverRes.cursor,
},
});
default:
throw Error("unsupported anastasis operation");
}
}
@ -288,10 +295,10 @@ export async function testWithGv() {
});
}
export async function testWithLocal(path: string) {
export async function testWithLocal() {
console.log("running local test");
const w = await createNativeWalletHost2({
persistentStoragePath: path ?? "walletdb.json",
persistentStoragePath: "walletdb.json",
config: {
features: {
allowHttp: true,
@ -303,7 +310,7 @@ export async function testWithLocal(path: string) {
skipDefaults: true,
});
console.log("initialized wallet");
await w.wallet.client.call(WalletApiOperation.RunIntegrationTest, {
await w.wallet.client.call(WalletApiOperation.RunIntegrationTestV2, {
amountToSpend: "TESTKUDOS:1",
amountToWithdraw: "TESTKUDOS:3",
bankAccessApiBaseUrl: "http://localhost:8082/taler-bank-access/",
@ -316,7 +323,6 @@ export async function testWithLocal(path: string) {
});
console.log("done with task loop");
w.wallet.stop();
console.log("DB stats:", j2s(w.getDbStats()));
}
export async function testArgon2id() {

View File

@ -34,6 +34,7 @@ function parse_json_or_undefined<T>(str: string | undefined): T | undefined {
export const codecForSettings = (): Codec<Settings> =>
buildCodecForObject<Settings>()
.property("walletAllowHttp", codecForBoolean())
.property("walletBatchWithdrawal", codecForBoolean())
.property("injectTalerSupport", codecForBoolean())
.property("advanceMode", codecForBoolean())
.property("backup", codecForBoolean())

View File

@ -17,7 +17,7 @@ msgstr ""
"Project-Id-Version: Taler Wallet\n"
"Report-Msgid-Bugs-To: languages@taler.net\n"
"POT-Creation-Date: 2016-11-23 00:00+0100\n"
"PO-Revision-Date: 2023-08-16 12:43+0000\n"
"PO-Revision-Date: 2023-08-15 07:28+0000\n"
"Last-Translator: Krystian Baran <kiszkot@murena.io>\n"
"Language-Team: Italian <https://weblate.taler.net/projects/gnu-taler/"
"webextensions/it/>\n"
@ -31,7 +31,7 @@ msgstr ""
#: src/NavigationBar.tsx:139
#, c-format
msgid "Balance"
msgstr "Saldo"
msgstr ""
#: src/NavigationBar.tsx:142
#, c-format
@ -41,12 +41,12 @@ msgstr ""
#: src/NavigationBar.tsx:147
#, c-format
msgid "QR Reader and Taler URI"
msgstr "Lettore QR e Taler URI"
msgstr ""
#: src/NavigationBar.tsx:154
#, c-format
msgid "Settings"
msgstr "Impostazioni"
msgstr ""
#: src/NavigationBar.tsx:184
#, c-format

View File

@ -119,6 +119,7 @@ export const defaultSettings: Settings = {
showJsonOnError: false,
extendedAccountTypes: false,
walletAllowHttp: false,
walletBatchWithdrawal: false,
};
/**

View File

@ -316,6 +316,10 @@ function AdvanceSettings(): VNode {
label: i18n.str`Allow HTTP connections`,
description: i18n.str`Using HTTP connection may be faster but unsafe (wallet restart required)`,
},
walletBatchWithdrawal: {
label: i18n.str`Allow batch withdrawals`,
description: i18n.str`Using the batch withdrawal API allows faster withdrawals (wallet restart required)`,
},
langSelector: {
label: i18n.str`Lang selector`,
description: i18n.str`Allows to manually change the language of the UI. Otherwise it will be automatically selected by your browser configuration.`,

View File

@ -332,6 +332,7 @@ async function reinitWallet(): Promise<void> {
{
features: {
allowHttp: settings.walletAllowHttp,
batchWithdrawal: settings.walletBatchWithdrawal,
},
},
);

File diff suppressed because it is too large Load Diff