summaryrefslogtreecommitdiff
path: root/packages/idb-bridge
diff options
context:
space:
mode:
authorFlorian Dold <florian.dold@gmail.com>2020-08-10 14:37:20 +0530
committerFlorian Dold <florian.dold@gmail.com>2020-08-10 14:37:20 +0530
commit5f8714091aac80144be118fa6427d65222e7509c (patch)
treea897f42a894b299a78a44e8d9b96a2a9a8342d0c /packages/idb-bridge
parent6990b3b5ec09ed5c1cc9d3eab4c5f8ad31d351ff (diff)
downloadwallet-core-5f8714091aac80144be118fa6427d65222e7509c.tar.gz
wallet-core-5f8714091aac80144be118fa6427d65222e7509c.tar.bz2
wallet-core-5f8714091aac80144be118fa6427d65222e7509c.zip
use API extractor for cleaner package separation
Diffstat (limited to 'packages/idb-bridge')
-rw-r--r--packages/idb-bridge/api-extractor.json131
-rw-r--r--packages/idb-bridge/package.json11
-rw-r--r--packages/idb-bridge/src/BridgeIDBCursor.ts2
-rw-r--r--packages/idb-bridge/src/BridgeIDBDatabase.ts4
-rw-r--r--packages/idb-bridge/src/BridgeIDBFactory.ts4
-rw-r--r--packages/idb-bridge/src/BridgeIDBIndex.ts1
-rw-r--r--packages/idb-bridge/src/BridgeIDBKeyRange.ts1
-rw-r--r--packages/idb-bridge/src/BridgeIDBObjectStore.ts10
-rw-r--r--packages/idb-bridge/src/BridgeIDBOpenDBRequest.ts7
-rw-r--r--packages/idb-bridge/src/BridgeIDBRequest.ts7
-rw-r--r--packages/idb-bridge/src/BridgeIDBTransaction.ts9
-rw-r--r--packages/idb-bridge/src/MemoryBackend.ts26
-rw-r--r--packages/idb-bridge/src/backend-interface.ts12
-rw-r--r--packages/idb-bridge/src/idbtypes.ts173
-rw-r--r--packages/idb-bridge/src/index.ts82
-rw-r--r--packages/idb-bridge/src/util/FakeEvent.ts17
-rw-r--r--packages/idb-bridge/src/util/FakeEventTarget.ts77
-rw-r--r--packages/idb-bridge/src/util/types.ts23
-rw-r--r--packages/idb-bridge/tsconfig.json1
19 files changed, 508 insertions, 90 deletions
diff --git a/packages/idb-bridge/api-extractor.json b/packages/idb-bridge/api-extractor.json
new file mode 100644
index 000000000..f57d81d8c
--- /dev/null
+++ b/packages/idb-bridge/api-extractor.json
@@ -0,0 +1,131 @@
+/**
+ * Config file for API Extractor. For more info, please visit: https://api-extractor.com
+ */
+{
+ "$schema": "https://developer.microsoft.com/json-schemas/api-extractor/v7/api-extractor.schema.json",
+
+ /**
+ * (REQUIRED) Specifies the .d.ts file to be used as the starting point for analysis. API Extractor
+ * analyzes the symbols exported by this module.
+ *
+ * The file extension must be ".d.ts" and not ".ts".
+ *
+ * The path is resolved relative to the folder of the config file that contains the setting; to change this,
+ * prepend a folder token such as "<projectFolder>".
+ *
+ * SUPPORTED TOKENS: <projectFolder>, <packageName>, <unscopedPackageName>
+ */
+ "mainEntryPointFilePath": "<projectFolder>/lib/index.d.ts",
+
+ /**
+ * A list of NPM package names whose exports should be treated as part of this package.
+ *
+ * For example, suppose that Webpack is used to generate a distributed bundle for the project "library1",
+ * and another NPM package "library2" is embedded in this bundle. Some types from library2 may become part
+ * of the exported API for library1, but by default API Extractor would generate a .d.ts rollup that explicitly
+ * imports library2. To avoid this, we can specify:
+ *
+ * "bundledPackages": [ "library2" ],
+ *
+ * This would direct API Extractor to embed those types directly in the .d.ts rollup, as if they had been
+ * local files for library1.
+ */
+ "bundledPackages": [],
+
+ /**
+ * Configures how the API report file (*.api.md) will be generated.
+ */
+ "apiReport": {
+ /**
+ * (REQUIRED) Whether to generate an API report.
+ */
+ "enabled": false
+ },
+
+ /**
+ * Configures how the doc model file (*.api.json) will be generated.
+ */
+ "docModel": {
+ /**
+ * (REQUIRED) Whether to generate a doc model file.
+ */
+ "enabled": false
+ },
+
+ /**
+ * Configures how the .d.ts rollup file will be generated.
+ */
+ "dtsRollup": {
+ /**
+ * (REQUIRED) Whether to generate the .d.ts rollup file.
+ */
+ "enabled": true
+ },
+
+ /**
+ * Configures how the tsdoc-metadata.json file will be generated.
+ */
+ "tsdocMetadata": {
+ /**
+ * Whether to generate the tsdoc-metadata.json file.
+ *
+ * DEFAULT VALUE: true
+ */
+ "enabled": false
+ },
+
+ /**
+ * Specifies what type of newlines API Extractor should use when writing output files. By default, the output files
+ * will be written with Windows-style newlines. To use POSIX-style newlines, specify "lf" instead.
+ * To use the OS's default newline kind, specify "os".
+ *
+ * DEFAULT VALUE: "crlf"
+ */
+ "newlineKind": "lf",
+
+ /**
+ * Configures how API Extractor reports error and warning messages produced during analysis.
+ *
+ * There are three sources of messages: compiler messages, API Extractor messages, and TSDoc messages.
+ */
+ "messages": {
+ /**
+ * Configures handling of diagnostic messages reported by the TypeScript compiler engine while analyzing
+ * the input .d.ts files.
+ *
+ * TypeScript message identifiers start with "TS" followed by an integer. For example: "TS2551"
+ *
+ * DEFAULT VALUE: A single "default" entry with logLevel=warning.
+ */
+ "compilerMessageReporting": {
+ /**
+ * Configures the default routing for messages that don't match an explicit rule in this table.
+ */
+ "default": {
+ "logLevel": "warning"
+ }
+ },
+
+ /**
+ * Configures handling of messages reported by API Extractor during its analysis.
+ *
+ * API Extractor message identifiers start with "ae-". For example: "ae-extra-release-tag"
+ *
+ * DEFAULT VALUE: See api-extractor-defaults.json for the complete table of extractorMessageReporting mappings
+ */
+ "extractorMessageReporting": {
+ "default": {
+ "logLevel": "warning"
+ }
+ },
+
+ /**
+ * Configures handling of messages reported by the TSDoc parser when analyzing code comments.
+ */
+ "tsdocMessageReporting": {
+ "default": {
+ "logLevel": "warning"
+ }
+ }
+ }
+}
diff --git a/packages/idb-bridge/package.json b/packages/idb-bridge/package.json
index b2c22921f..cf9ce409c 100644
--- a/packages/idb-bridge/package.json
+++ b/packages/idb-bridge/package.json
@@ -4,17 +4,18 @@
"description": "IndexedDB implementation that uses SQLite3 as storage",
"main": "./dist/idb-bridge.js",
"module": "./lib/index.js",
- "types": "./lib/index.d.ts",
+ "types": "./dist/idb-bridge.d.ts",
"author": "Florian Dold",
"license": "AGPL-3.0-or-later",
"private": false,
"scripts": {
"test": "tsc && ava",
- "compile": "tsc && rollup -c",
- "clean": "rimraf dist lib",
+ "compile": "tsc && rollup -c && api-extractor run",
+ "clean": "rimraf dist lib tsconfig.tsbuildinfo",
"pretty": "prettier --config ../../.prettierrc --write src"
},
"devDependencies": {
+ "@microsoft/api-extractor": "^7.9.3",
"@rollup/plugin-typescript": "^5.0.2",
"ava": "^3.10.1",
"esm": "^3.2.25",
@@ -27,6 +28,8 @@
"tslib": "^2.0.0"
},
"ava": {
- "require": ["esm"]
+ "require": [
+ "esm"
+ ]
}
}
diff --git a/packages/idb-bridge/src/BridgeIDBCursor.ts b/packages/idb-bridge/src/BridgeIDBCursor.ts
index a85f41f2b..a18c0bebc 100644
--- a/packages/idb-bridge/src/BridgeIDBCursor.ts
+++ b/packages/idb-bridge/src/BridgeIDBCursor.ts
@@ -46,6 +46,8 @@ import { BridgeIDBFactory } from "./BridgeIDBFactory";
/**
* http://www.w3.org/TR/2015/REC-IndexedDB-20150108/#cursor
+ *
+ * @public
*/
export class BridgeIDBCursor {
_request: BridgeIDBRequest | undefined;
diff --git a/packages/idb-bridge/src/BridgeIDBDatabase.ts b/packages/idb-bridge/src/BridgeIDBDatabase.ts
index a22ad5400..ffd897f9c 100644
--- a/packages/idb-bridge/src/BridgeIDBDatabase.ts
+++ b/packages/idb-bridge/src/BridgeIDBDatabase.ts
@@ -33,6 +33,7 @@ import {
Schema,
DatabaseTransaction,
} from "./backend-interface";
+import { BridgeIDBObjectStore } from "./BridgeIDBObjectStore";
/**
* Ensure that an active version change transaction is currently running.
@@ -62,6 +63,7 @@ const confirmActiveVersionchangeTransaction = (database: BridgeIDBDatabase) => {
};
// http://www.w3.org/TR/2015/REC-IndexedDB-20150108/#database-interface
+/** @public */
export class BridgeIDBDatabase extends FakeEventTarget {
_closePending = false;
_closed = false;
@@ -120,7 +122,7 @@ export class BridgeIDBDatabase extends FakeEventTarget {
public createObjectStore(
name: string,
options: { autoIncrement?: boolean; keyPath?: KeyPath } | null = {},
- ) {
+ ): BridgeIDBObjectStore {
if (name === undefined) {
throw new TypeError();
}
diff --git a/packages/idb-bridge/src/BridgeIDBFactory.ts b/packages/idb-bridge/src/BridgeIDBFactory.ts
index 7002222d8..7954cdd99 100644
--- a/packages/idb-bridge/src/BridgeIDBFactory.ts
+++ b/packages/idb-bridge/src/BridgeIDBFactory.ts
@@ -25,8 +25,10 @@ import FakeEvent from "./util/FakeEvent";
import { Backend, DatabaseConnection } from "./backend-interface";
import queueTask from "./util/queueTask";
-type DatabaseList = Array<{ name: string; version: number }>;
+/** @public */
+export type DatabaseList = Array<{ name: string; version: number }>;
+/** @public */
export class BridgeIDBFactory {
public cmp = compareKeys;
private backend: Backend;
diff --git a/packages/idb-bridge/src/BridgeIDBIndex.ts b/packages/idb-bridge/src/BridgeIDBIndex.ts
index 3c1b39435..9b214a234 100644
--- a/packages/idb-bridge/src/BridgeIDBIndex.ts
+++ b/packages/idb-bridge/src/BridgeIDBIndex.ts
@@ -51,6 +51,7 @@ const confirmActiveTransaction = (
};
// http://www.w3.org/TR/2015/REC-IndexedDB-20150108/#idl-def-IDBIndex
+/** @public */
export class BridgeIDBIndex {
objectStore: BridgeIDBObjectStore;
diff --git a/packages/idb-bridge/src/BridgeIDBKeyRange.ts b/packages/idb-bridge/src/BridgeIDBKeyRange.ts
index 43f59fb14..d2eaa2d1b 100644
--- a/packages/idb-bridge/src/BridgeIDBKeyRange.ts
+++ b/packages/idb-bridge/src/BridgeIDBKeyRange.ts
@@ -21,6 +21,7 @@ import { Key } from "./util/types";
import valueToKey from "./util/valueToKey";
// http://www.w3.org/TR/2015/REC-IndexedDB-20150108/#range-concept
+/** @public */
export class BridgeIDBKeyRange {
public static only(value: Key) {
if (arguments.length === 0) {
diff --git a/packages/idb-bridge/src/BridgeIDBObjectStore.ts b/packages/idb-bridge/src/BridgeIDBObjectStore.ts
index a32c29275..06bd0f590 100644
--- a/packages/idb-bridge/src/BridgeIDBObjectStore.ts
+++ b/packages/idb-bridge/src/BridgeIDBObjectStore.ts
@@ -43,10 +43,14 @@ import {
RecordGetRequest,
ResultLevel,
StoreLevel,
+ Schema,
+ Backend,
+ DatabaseConnection,
} from "./backend-interface";
import { BridgeIDBFactory } from "./BridgeIDBFactory";
// http://www.w3.org/TR/2015/REC-IndexedDB-20150108/#object-store
+/** @public */
export class BridgeIDBObjectStore {
_indexesCache: Map<string, BridgeIDBIndex> = new Map();
@@ -68,7 +72,7 @@ export class BridgeIDBObjectStore {
_name: string;
- get _schema() {
+ get _schema(): Schema {
return this.transaction.db._schema;
}
@@ -83,11 +87,11 @@ export class BridgeIDBObjectStore {
return this._name;
}
- get _backend() {
+ get _backend(): Backend {
return this.transaction.db._backend;
}
- get _backendConnection() {
+ get _backendConnection(): DatabaseConnection {
return this.transaction.db._backendConnection;
}
diff --git a/packages/idb-bridge/src/BridgeIDBOpenDBRequest.ts b/packages/idb-bridge/src/BridgeIDBOpenDBRequest.ts
index 71a6495e5..306edcb7d 100644
--- a/packages/idb-bridge/src/BridgeIDBOpenDBRequest.ts
+++ b/packages/idb-bridge/src/BridgeIDBOpenDBRequest.ts
@@ -16,11 +16,12 @@
*/
import { BridgeIDBRequest } from "./BridgeIDBRequest";
-import { EventCallback } from "./util/types";
+import { EventListener } from "./idbtypes";
+/** @public */
export class BridgeIDBOpenDBRequest extends BridgeIDBRequest {
- public onupgradeneeded: EventCallback | null = null;
- public onblocked: EventCallback | null = null;
+ public onupgradeneeded: EventListener | null = null;
+ public onblocked: EventListener | null = null;
constructor() {
super();
diff --git a/packages/idb-bridge/src/BridgeIDBRequest.ts b/packages/idb-bridge/src/BridgeIDBRequest.ts
index 3ed2f9244..4800a0582 100644
--- a/packages/idb-bridge/src/BridgeIDBRequest.ts
+++ b/packages/idb-bridge/src/BridgeIDBRequest.ts
@@ -21,17 +21,18 @@ import { BridgeIDBObjectStore } from "./BridgeIDBObjectStore";
import { BridgeIDBTransaction } from "./BridgeIDBTransaction";
import { InvalidStateError } from "./util/errors";
import FakeEventTarget from "./util/FakeEventTarget";
-import { EventCallback } from "./util/types";
import FakeEvent from "./util/FakeEvent";
+import { EventListener } from "./idbtypes";
+/** @public */
export class BridgeIDBRequest extends FakeEventTarget {
_result: any = null;
_error: Error | null | undefined = null;
source: BridgeFIBCursor | BridgeIDBIndex | BridgeIDBObjectStore | null = null;
transaction: BridgeIDBTransaction | null = null;
readyState: "done" | "pending" = "pending";
- onsuccess: EventCallback | null = null;
- onerror: EventCallback | null = null;
+ onsuccess: EventListener | null = null;
+ onerror: EventListener | null = null;
get error() {
if (this.readyState === "pending") {
diff --git a/packages/idb-bridge/src/BridgeIDBTransaction.ts b/packages/idb-bridge/src/BridgeIDBTransaction.ts
index 56a4d59ef..d49ae515f 100644
--- a/packages/idb-bridge/src/BridgeIDBTransaction.ts
+++ b/packages/idb-bridge/src/BridgeIDBTransaction.ts
@@ -11,7 +11,6 @@ import fakeDOMStringList from "./util/fakeDOMStringList";
import FakeEvent from "./util/FakeEvent";
import FakeEventTarget from "./util/FakeEventTarget";
import {
- EventCallback,
FakeDOMStringList,
RequestObj,
TransactionMode,
@@ -20,8 +19,10 @@ import queueTask from "./util/queueTask";
import openPromise from "./util/openPromise";
import { DatabaseTransaction, Backend } from "./backend-interface";
import { BridgeIDBFactory } from "./BridgeIDBFactory";
+import { EventListener } from "./idbtypes";
// http://www.w3.org/TR/2015/REC-IndexedDB-20150108/#transaction
+/** @public */
export class BridgeIDBTransaction extends FakeEventTarget {
public _state: "active" | "inactive" | "committing" | "finished" = "active";
public _started = false;
@@ -33,9 +34,9 @@ export class BridgeIDBTransaction extends FakeEventTarget {
public mode: TransactionMode;
public db: BridgeIDBDatabase;
public error: Error | null = null;
- public onabort: EventCallback | null = null;
- public oncomplete: EventCallback | null = null;
- public onerror: EventCallback | null = null;
+ public onabort: EventListener | null = null;
+ public oncomplete: EventListener | null = null;
+ public onerror: EventListener | null = null;
private _waitPromise: Promise<void>;
private _resolveWait: () => void;
diff --git a/packages/idb-bridge/src/MemoryBackend.ts b/packages/idb-bridge/src/MemoryBackend.ts
index c5fac41a9..e8aec766e 100644
--- a/packages/idb-bridge/src/MemoryBackend.ts
+++ b/packages/idb-bridge/src/MemoryBackend.ts
@@ -36,7 +36,7 @@ import {
} from "./util/errors";
import BTree, { ISortedMapF } from "./tree/b+tree";
import compareKeys from "./util/cmp";
-import { Key, Value, KeyPath } from "./util/types";
+import { Key, Value, KeyPath, TransactionMode } from "./util/types";
import { StoreKeyResult, makeStoreKeyValue } from "./util/makeStoreKeyValue";
import getIndexKeys from "./util/getIndexKeys";
import openPromise from "./util/openPromise";
@@ -89,24 +89,28 @@ interface Database {
connectionCookie: string | undefined;
}
-interface IndexDump {
+/** @public */
+export interface IndexDump {
name: string;
records: IndexRecord[];
}
-interface ObjectStoreDump {
+/** @public */
+export interface ObjectStoreDump {
name: string;
keyGenerator: number;
records: ObjectStoreRecord[];
indexes: { [name: string]: IndexDump };
}
-interface DatabaseDump {
+/** @public */
+export interface DatabaseDump {
schema: Schema;
objectStores: { [name: string]: ObjectStoreDump };
}
-interface MemoryBackendDump {
+/** @public */
+export interface MemoryBackendDump {
databases: { [name: string]: DatabaseDump };
}
@@ -132,12 +136,14 @@ interface Connection {
objectStoreMap: { [currentName: string]: ObjectStoreMapEntry };
}
-interface IndexRecord {
+/** @public */
+export interface IndexRecord {
indexKey: Key;
primaryKeys: Key[];
}
-interface ObjectStoreRecord {
+/** @public */
+export interface ObjectStoreRecord {
primaryKey: Key;
value: Value;
}
@@ -210,6 +216,8 @@ function furthestKey(
/**
* Primitive in-memory backend.
+ *
+ * @public
*/
export class MemoryBackend implements Backend {
private databases: { [name: string]: Database } = {};
@@ -471,7 +479,7 @@ export class MemoryBackend implements Backend {
async beginTransaction(
conn: DatabaseConnection,
objectStores: string[],
- mode: import("./util/types").TransactionMode,
+ mode: TransactionMode,
): Promise<DatabaseTransaction> {
if (this.enableTracing) {
console.log(`TRACING: beginTransaction`);
@@ -781,7 +789,7 @@ export class MemoryBackend implements Backend {
btx: DatabaseTransaction,
indexName: string,
objectStoreName: string,
- keyPath: import("./util/types").KeyPath,
+ keyPath: KeyPath,
multiEntry: boolean,
unique: boolean,
): void {
diff --git a/packages/idb-bridge/src/backend-interface.ts b/packages/idb-bridge/src/backend-interface.ts
index 3617d21bc..4d1f107ba 100644
--- a/packages/idb-bridge/src/backend-interface.ts
+++ b/packages/idb-bridge/src/backend-interface.ts
@@ -24,44 +24,52 @@ import {
} from "./util/types";
import { BridgeIDBKeyRange } from "./BridgeIDBKeyRange";
+/** @public */
export interface ObjectStoreProperties {
keyPath: KeyPath | null;
autoIncrement: boolean;
indexes: { [nameame: string]: IndexProperties };
}
+/** @public */
export interface IndexProperties {
keyPath: KeyPath;
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,
}
+/** @public */
export interface RecordGetRequest {
direction: BridgeIDBCursorDirection;
objectStoreName: string;
@@ -104,6 +112,7 @@ export interface RecordGetRequest {
resultLevel: ResultLevel;
}
+/** @public */
export interface RecordGetResponse {
values: Value[] | undefined;
indexKeys: Key[] | undefined;
@@ -111,6 +120,7 @@ export interface RecordGetResponse {
count: number;
}
+/** @public */
export interface RecordStoreRequest {
objectStoreName: string;
value: Value;
@@ -118,6 +128,7 @@ export interface RecordStoreRequest {
storeLevel: StoreLevel;
}
+/** @public */
export interface RecordStoreResponse {
/**
* Key that the record was stored under in the object store.
@@ -125,6 +136,7 @@ export interface RecordStoreResponse {
key: Key;
}
+/** @public */
export interface Backend {
getDatabases(): Promise<BridgeIDBDatabaseInfo[]>;
diff --git a/packages/idb-bridge/src/idbtypes.ts b/packages/idb-bridge/src/idbtypes.ts
index 53f93d451..4ab2b6f3b 100644
--- a/packages/idb-bridge/src/idbtypes.ts
+++ b/packages/idb-bridge/src/idbtypes.ts
@@ -19,32 +19,58 @@ 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;
-/** EventTarget is a DOM interface implemented by objects that can receive events and may have listeners for them. */
+/**
+ * EventTarget is a DOM interface implemented by objects that can receive
+ * events and may have listeners for them.
+ *
+ * @public
+ */
export interface EventTarget {
/**
* Appends an event listener for events whose type attribute value is type. The callback argument sets the callback that will be invoked when the event is dispatched.
@@ -78,7 +104,11 @@ export interface EventTarget {
): void;
}
-/** An event which takes place in the DOM. */
+/**
+ * An event which takes place in the DOM.
+ *
+ * @public
+ */
export interface Event {
/**
* Returns true or false depending on how event was initialized. True if event goes through its target's ancestors in reverse tree order, and false otherwise.
@@ -110,7 +140,9 @@ export interface Event {
*/
readonly isTrusted: boolean;
returnValue: boolean;
- /** @deprecated */
+ /**
+ * @deprecated use target instead
+ */
readonly srcElement: EventTarget | null;
/**
* Returns the object to which event is dispatched (its target).
@@ -147,7 +179,11 @@ export interface Event {
readonly NONE: number;
}
-/** A type returned by some APIs which contains a list of DOMString (strings). */
+/**
+ * A type returned by some APIs which contains a list of DOMString (strings).
+ *
+ * @public
+ */
export interface DOMStringList {
/**
* Returns the number of strings in strings.
@@ -164,31 +200,57 @@ export interface DOMStringList {
[index: number]: string;
}
+/**
+ * @public
+ */
export type BufferSource = ArrayBufferView | ArrayBuffer;
+/**
+ * @public
+ */
export type IDBValidKey = number | string | Date | BufferSource | IDBArrayKey;
+/**
+ * @public
+ */
export interface IDBIndexParameters {
multiEntry?: boolean;
unique?: boolean;
}
+/**
+ * @public
+ */
export interface IDBObjectStoreParameters {
autoIncrement?: boolean;
keyPath?: string | string[] | null;
}
+/**
+ * @public
+ */
export interface EventInit {
bubbles?: boolean;
cancelable?: boolean;
composed?: boolean;
}
+/**
+ * @public
+ */
export interface IDBArrayKey extends Array<IDBValidKey> {}
+/**
+ * @public
+ */
export type IDBCursorDirection = "next" | "nextunique" | "prev" | "prevunique";
-/** This IndexedDB API interface represents a cursor for traversing or iterating over multiple records in a database. */
+/**
+ * This IndexedDB API interface represents a cursor for traversing or
+ * iterating over multiple records in a database.
+ *
+ * @public
+ */
export interface IDBCursor {
/**
* Returns the direction ("next", "nextunique", "prev" or "prevunique") of the cursor.
@@ -234,7 +296,13 @@ export interface IDBCursor {
update(value: any): IDBRequest<IDBValidKey>;
}
-/** This IndexedDB API interface represents a cursor for traversing or iterating over multiple records in a database. It is the same as the IDBCursor, except that it includes the value property. */
+/**
+ * This IndexedDB API interface represents a cursor for traversing or
+ * iterating over multiple records in a database. It is the same as the
+ * IDBCursor, except that it includes the value property.
+ *
+ * @public
+ */
export interface IDBCursorWithValue extends IDBCursor {
/**
* Returns the cursor's current value.
@@ -242,6 +310,9 @@ export interface IDBCursorWithValue extends IDBCursor {
readonly value: any;
}
+/**
+ * @public
+ */
export interface IDBDatabaseEventMap {
abort: Event;
close: Event;
@@ -249,7 +320,14 @@ export interface IDBDatabaseEventMap {
versionchange: IDBVersionChangeEvent;
}
-/** This IndexedDB API interface provides a connection to a database; you can use an IDBDatabase object to open a transaction on your database then create, manipulate, and delete objects (data) in that database. The interface provides the only way to get and manage versions of the database. */
+/**
+ * This IndexedDB API interface provides a connection to a database; you can
+ * use an IDBDatabase object to open a transaction on your database then
+ * create, manipulate, and delete objects (data) in that database. The
+ * interface provides the only way to get and manage versions of the database.
+ *
+ * @public
+ */
export interface IDBDatabase extends EventTarget {
/**
* Returns the name of the database.
@@ -317,7 +395,9 @@ export interface IDBDatabase extends EventTarget {
): void;
}
-/** In the following code snippet, we make a request to open a database, and include handlers for the success and error cases. For a full working example, see our To-do Notifications app (view example live.) */
+/**
+ * @public
+ */
export interface IDBFactory {
/**
* Compares two values as keys. Returns -1 if key1 precedes key2, 1 if key2 precedes key1, and 0 if the keys are equal.
@@ -326,16 +406,28 @@ export interface IDBFactory {
*/
cmp(first: any, second: any): number;
/**
- * Attempts to delete the named database. If the database already exists and there are open connections that don't close in response to a versionchange event, the request will be blocked until all they close. If the request is successful request's result will be null.
+ * Attempts to delete the named database. If the database already exists and
+ * there are open connections that don't close in response to a versionchange
+ * event, the request will be blocked until all they close. If the request is
+ * successful request's result will be null.
*/
deleteDatabase(name: string): IDBOpenDBRequest;
/**
- * Attempts to open a connection to the named database with the current version, or 1 if it does not already exist. If the request is successful request's result will be the connection.
+ * Attempts to open a connection to the named database with the current
+ * version, or 1 if it does not already exist. If the request is successful
+ * request's result will be the connection.
*/
open(name: string, version?: number): IDBOpenDBRequest;
}
-/** IDBIndex interface of the IndexedDB API provides asynchronous access to an index in a database. An index is a kind of object store for looking up records in another object store, called the referenced object store. You use this interface to retrieve data. */
+/**
+ * IDBIndex interface of the IndexedDB API provides asynchronous access to an
+ * index in a database. An index is a kind of object store for looking up
+ * records in another object store, called the referenced object store. You use
+ * this interface to retrieve data.
+ *
+ * @public
+ */
export interface IDBIndex {
readonly keyPath: string | string[];
readonly multiEntry: boolean;
@@ -404,7 +496,15 @@ export interface IDBIndex {
): IDBRequest<IDBCursor | null>;
}
-/** A key range can be a single value or a range with upper and lower bounds or endpoints. If the key range has both upper and lower bounds, then it is bounded; if it has no bounds, it is unbounded. A bounded key range can either be open (the endpoints are excluded) or closed (the endpoints are included). To retrieve all keys within a certain range, you can use the following code constructs: */
+/**
+ * A key range can be a single value or a range with upper and lower bounds or
+ * endpoints. If the key range has both upper and lower bounds, then it is
+ * bounded; if it has no bounds, it is unbounded. A bounded key range can
+ * either be open (the endpoints are excluded) or closed (the endpoints are
+ * included).
+ *
+ * @public
+ */
export interface IDBKeyRange {
/**
* Returns lower bound, or undefined if none.
@@ -428,7 +528,9 @@ export interface IDBKeyRange {
includes(key: any): boolean;
}
-/** This example shows a variety of different uses of object stores, from updating the data structure with IDBObjectStore.createIndex inside an onupgradeneeded function, to adding a new item to our object store with IDBObjectStore.add. For a full working example, see our To-do Notifications app (view example live.) */
+/**
+ * @public
+ */
export interface IDBObjectStore {
/**
* Returns true if the store has a key generator, and false otherwise.
@@ -555,12 +657,19 @@ export interface IDBObjectStore {
put(value: any, key?: IDBValidKey): IDBRequest<IDBValidKey>;
}
+/**
+ * @public
+ */
export interface IDBOpenDBRequestEventMap extends IDBRequestEventMap {
blocked: Event;
upgradeneeded: IDBVersionChangeEvent;
}
-/** Also inherits methods from its parents IDBRequest and EventTarget. */
+/**
+ * Also inherits methods from its parents IDBRequest and EventTarget.
+ *
+ * @public
+ */
export interface IDBOpenDBRequest extends IDBRequest<IDBDatabase> {
onblocked: ((this: IDBOpenDBRequest, ev: Event) => any) | null;
onupgradeneeded:
@@ -588,14 +697,25 @@ export interface IDBOpenDBRequest extends IDBRequest<IDBDatabase> {
): void;
}
+/**
+ * @public
+ */
export type IDBRequestReadyState = "done" | "pending";
+/**
+ * @public
+ */
export interface IDBRequestEventMap {
error: Event;
success: Event;
}
-/** An abnormal event (called an exception) which occurs as a result of calling a method or accessing a property of a web API. */
+/**
+ * An abnormal event (called an exception) which occurs as a result of calling
+ * a method or accessing a property of a web API.
+ *
+ * @public
+ */
export interface DOMException {
readonly code: number;
readonly message: string;
@@ -627,7 +747,14 @@ export interface DOMException {
readonly WRONG_DOCUMENT_ERR: number;
}
-/** The request object does not initially contain any information about the result of the operation, but once information becomes available, an event is fired on the request, and the information becomes available through the properties of the IDBRequest instance. */
+/**
+ * The request object does not initially contain any information about the
+ * result of the operation, but once information becomes available, an event is
+ * fired on the request, and the information becomes available through the
+ * properties of the IDBRequest instance.
+ *
+ * @public
+ */
export interface IDBRequest<T = any> extends EventTarget {
/**
* When a request is completed, returns the error (a DOMException), or null if the request succeeded. Throws a "InvalidStateError" DOMException if the request is still pending.
@@ -673,12 +800,18 @@ export interface IDBRequest<T = any> extends EventTarget {
): void;
}
+/**
+ * @public
+ */
export interface IDBTransactionEventMap {
abort: Event;
complete: Event;
error: Event;
}
+/**
+ * @public
+ */
export interface IDBTransaction extends EventTarget {
/**
* Returns the transaction's connection.
@@ -729,7 +862,13 @@ export interface IDBTransaction extends EventTarget {
): void;
}
-/** This IndexedDB API interface indicates that the version of the database has changed, as the result of an IDBOpenDBRequest.onupgradeneeded event handler function. */
+/**
+ * This IndexedDB API interface indicates that the version of the database has
+ * changed, as the result of an IDBOpenDBRequest.onupgradeneeded event handler
+ * function.
+ *
+ * @public
+ */
export interface IDBVersionChangeEvent extends Event {
readonly newVersion: number | null;
readonly oldVersion: number;
diff --git a/packages/idb-bridge/src/index.ts b/packages/idb-bridge/src/index.ts
index 8f19c9a31..a07803763 100644
--- a/packages/idb-bridge/src/index.ts
+++ b/packages/idb-bridge/src/index.ts
@@ -1,4 +1,4 @@
-import { BridgeIDBFactory } from "./BridgeIDBFactory";
+import { BridgeIDBFactory, DatabaseList } from "./BridgeIDBFactory";
import { BridgeIDBCursor } from "./BridgeIDBCursor";
import { BridgeIDBIndex } from "./BridgeIDBIndex";
import { BridgeIDBDatabase } from "./BridgeIDBDatabase";
@@ -8,8 +8,81 @@ import { BridgeIDBOpenDBRequest } from "./BridgeIDBOpenDBRequest";
import { BridgeIDBRequest } from "./BridgeIDBRequest";
import { BridgeIDBTransaction } from "./BridgeIDBTransaction";
import { BridgeIDBVersionChangeEvent } from "./BridgeIDBVersionChangeEvent";
+import {
+ Value,
+ CursorSource,
+ CursorRange,
+ BridgeIDBCursorDirection,
+ Key,
+ KeyPath,
+ TransactionMode,
+ FakeDOMStringList,
+ RequestObj,
+ BridgeIDBDatabaseInfo,
+ EventType,
+} from "./util/types";
+import {
+ DatabaseTransaction,
+ RecordGetResponse,
+ RecordGetRequest,
+ Schema,
+ Backend,
+ RecordStoreRequest,
+ RecordStoreResponse,
+ DatabaseConnection,
+ ObjectStoreProperties,
+ StoreLevel,
+ ResultLevel,
+ IndexProperties,
+} from "./backend-interface";
+import FakeEventTarget, { Listener } from "./util/FakeEventTarget";
+import { DatabaseDump, ObjectStoreDump, IndexDump, IndexRecord, ObjectStoreRecord, MemoryBackendDump } from "./MemoryBackend";
+import { Event } from "./idbtypes";
-export { BridgeIDBFactory, BridgeIDBCursor };
+export {
+ BridgeIDBCursor,
+ BridgeIDBCursorDirection,
+ BridgeIDBDatabase,
+ BridgeIDBDatabaseInfo,
+ BridgeIDBFactory,
+ BridgeIDBIndex,
+ BridgeIDBKeyRange,
+ BridgeIDBObjectStore,
+ BridgeIDBOpenDBRequest,
+ BridgeIDBRequest,
+ BridgeIDBTransaction,
+ Value,
+ CursorSource,
+ CursorRange,
+ Key,
+ DatabaseTransaction,
+ RecordGetRequest,
+ RecordGetResponse,
+ KeyPath,
+ Schema,
+ Backend,
+ TransactionMode,
+ DatabaseList,
+ RecordStoreRequest,
+ RecordStoreResponse,
+ FakeEventTarget,
+ DatabaseConnection,
+ FakeDOMStringList,
+ ObjectStoreProperties,
+ RequestObj,
+ StoreLevel,
+ ResultLevel,
+ DatabaseDump,
+ ObjectStoreDump,
+ IndexDump,
+ IndexRecord,
+ ObjectStoreRecord,
+ EventType,
+ IndexProperties,
+ MemoryBackendDump,
+ Event,
+ Listener,
+};
export { MemoryBackend } from "./MemoryBackend";
@@ -31,6 +104,8 @@ export { MemoryBackend } from "./MemoryBackend";
/**
* Populate the global name space such that the given IndexedDB factory is made
* available globally.
+ *
+ * @public
*/
export function shimIndexedDB(factory: BridgeIDBFactory): void {
// @ts-ignore: shimming
@@ -59,5 +134,4 @@ export function shimIndexedDB(factory: BridgeIDBFactory): void {
globalThis.IDBVersionChangeEvent = BridgeIDBVersionChangeEvent;
}
-import * as idbtypes from "./idbtypes";
-export type { idbtypes };
+export * from "./idbtypes";
diff --git a/packages/idb-bridge/src/util/FakeEvent.ts b/packages/idb-bridge/src/util/FakeEvent.ts
index 4457498f6..207607886 100644
--- a/packages/idb-bridge/src/util/FakeEvent.ts
+++ b/packages/idb-bridge/src/util/FakeEvent.ts
@@ -16,8 +16,9 @@
import FakeEventTarget from "./FakeEventTarget";
import { EventType } from "./types";
+import { Event, EventTarget } from "../idbtypes";
-export class Event {
+export class FakeEvent implements Event {
public eventPath: FakeEventTarget[] = [];
public type: EventType;
@@ -57,6 +58,18 @@ export class Event {
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) {
@@ -74,4 +87,4 @@ export class Event {
}
}
-export default Event;
+export default FakeEvent;
diff --git a/packages/idb-bridge/src/util/FakeEventTarget.ts b/packages/idb-bridge/src/util/FakeEventTarget.ts
index 291eaca7d..da5a5466a 100644
--- a/packages/idb-bridge/src/util/FakeEventTarget.ts
+++ b/packages/idb-bridge/src/util/FakeEventTarget.ts
@@ -16,7 +16,8 @@
import { InvalidStateError } from "./errors";
import FakeEvent from "./FakeEvent";
-import { EventCallback, EventType } from "./types";
+import { EventType } from "./types";
+import { EventTarget, Event, EventListenerOrEventListenerObject, EventListener } from "../idbtypes";
type EventTypeProp =
| "onabort"
@@ -27,8 +28,9 @@ type EventTypeProp =
| "onupgradeneeded"
| "onversionchange";
-interface Listener {
- callback: EventCallback;
+/** @public */
+export interface Listener {
+ callback: EventListenerOrEventListenerObject;
capture: boolean;
type: EventType;
}
@@ -86,35 +88,44 @@ const invokeEventListeners = (event: FakeEvent, obj: FakeEventTarget) => {
}
};
-abstract class FakeEventTarget {
+/** @public */
+abstract class FakeEventTarget implements EventTarget {
public readonly listeners: Listener[] = [];
// These will be overridden in individual subclasses and made not readonly
- public readonly onabort: EventCallback | null | undefined;
- public readonly onblocked: EventCallback | null | undefined;
- public readonly oncomplete: EventCallback | null | undefined;
- public readonly onerror: EventCallback | null | undefined;
- public readonly onsuccess: EventCallback | null | undefined;
- public readonly onupgradeneeded: EventCallback | null | undefined;
- public readonly onversionchange: EventCallback | null | undefined;
+ public readonly onabort: EventListener | null | undefined;
+ public readonly onblocked: EventListener | null | undefined;
+ public readonly oncomplete: EventListener | null | undefined;
+ public readonly onerror: EventListener | null | undefined;
+ public readonly onsuccess: EventListener | null | undefined;
+ public readonly onupgradeneeded: EventListener | null | undefined;
+ public readonly onversionchange: EventListener | null | undefined;
static enableTracing: boolean = false;
public addEventListener(
type: EventType,
- callback: EventCallback,
+ listener: EventListenerOrEventListenerObject | null,
capture = false,
) {
- this.listeners.push({
- callback,
- capture,
- type,
- });
+ if (typeof listener === "function") {
+ this.listeners.push({
+ callback: listener,
+ capture,
+ type,
+ });
+ } else if (typeof listener === "object" && listener != null) {
+ this.listeners.push({
+ callback: (e: Event) => listener.handleEvent(e),
+ capture,
+ type,
+ });
+ }
}
public removeEventListener(
type: EventType,
- callback: EventCallback,
+ callback: EventListenerOrEventListenerObject,
capture = false,
) {
const i = this.listeners.findIndex((listener) => {
@@ -129,17 +140,21 @@ abstract class FakeEventTarget {
}
// http://www.w3.org/TR/dom/#dispatching-events
- public dispatchEvent(event: FakeEvent) {
+ public dispatchEvent(event: Event): boolean {
+ if (!(event instanceof FakeEvent)) {
+ throw Error("dispatchEvent only works with FakeEvent");
+ }
+ const fe = event as FakeEvent;
if (event.dispatched || !event.initialized) {
throw new InvalidStateError("The object is in an invalid state.");
}
- event.isTrusted = false;
+ fe.isTrusted = false;
- event.dispatched = true;
- event.target = this;
+ fe.dispatched = true;
+ fe.target = this;
// NOT SURE WHEN THIS SHOULD BE SET event.eventPath = [];
- event.eventPhase = event.CAPTURING_PHASE;
+ fe.eventPhase = event.CAPTURING_PHASE;
if (FakeEventTarget.enableTracing) {
console.log(
`dispatching '${event.type}' event along path with ${event.eventPath.length} elements`,
@@ -151,15 +166,15 @@ abstract class FakeEventTarget {
}
}
- event.eventPhase = event.AT_TARGET;
+ fe.eventPhase = event.AT_TARGET;
if (!event.propagationStopped) {
- invokeEventListeners(event, event.target);
+ invokeEventListeners(event, fe.target);
}
if (event.bubbles) {
- event.eventPath.reverse();
- event.eventPhase = event.BUBBLING_PHASE;
- if (event.eventPath.length === 0 && event.type === "error") {
+ fe.eventPath.reverse();
+ fe.eventPhase = event.BUBBLING_PHASE;
+ if (fe.eventPath.length === 0 && event.type === "error") {
console.error("Unhandled error event: ", event.target);
}
for (const obj of event.eventPath) {
@@ -169,9 +184,9 @@ abstract class FakeEventTarget {
}
}
- event.dispatched = false;
- event.eventPhase = event.NONE;
- event.currentTarget = null;
+ fe.dispatched = false;
+ fe.eventPhase = event.NONE;
+ fe.currentTarget = null;
if (event.canceled) {
return false;
diff --git a/packages/idb-bridge/src/util/types.ts b/packages/idb-bridge/src/util/types.ts
index b0142b0d8..b07f5ad00 100644
--- a/packages/idb-bridge/src/util/types.ts
+++ b/packages/idb-bridge/src/util/types.ts
@@ -18,16 +18,10 @@
import { BridgeIDBRequest } from "../BridgeIDBRequest";
import { BridgeIDBKeyRange } from "../BridgeIDBKeyRange";
import { BridgeIDBIndex } from "../BridgeIDBIndex";
-import { Event } from "../util/FakeEvent";
import { BridgeIDBObjectStore } from "../BridgeIDBObjectStore";
+import { Event } from "../idbtypes";
-interface EventInCallback extends Event {
- target: any;
- error: Error | null;
-}
-
-export type EventCallback = (event: EventInCallback) => void;
-
+/** @public */
export type EventType =
| "abort"
| "blocked"
@@ -37,39 +31,52 @@ export type EventType =
| "upgradeneeded"
| "versionchange";
+/** @public */
export type CursorSource = BridgeIDBIndex | BridgeIDBObjectStore;
+/** @public */
export interface FakeDOMStringList extends Array<string> {
contains: (value: string) => boolean;
item: (i: number) => string | undefined;
}
+/**
+ * @public
+ */
export type BridgeIDBCursorDirection =
| "next"
| "nextunique"
| "prev"
| "prevunique";
+/** @public */
export type KeyPath = string | string[];
+/** @public */
export type Key = any;
+/** @public */
export type CursorRange = Key | BridgeIDBKeyRange | undefined;
+/** @public */
export type Value = any;
+/** @public */
export interface Record {
key: Key;
value: Key | Value; // For indexes, will be Key. For object stores, will be Value.
}
+/** @public */
export type TransactionMode = "readonly" | "readwrite" | "versionchange";
+/** @public */
export interface BridgeIDBDatabaseInfo {
name: string;
version: number;
}
+/** @public */
export interface RequestObj {
operation: () => Promise<any>;
request?: BridgeIDBRequest | undefined;
diff --git a/packages/idb-bridge/tsconfig.json b/packages/idb-bridge/tsconfig.json
index 6e41df65d..a385e964a 100644
--- a/packages/idb-bridge/tsconfig.json
+++ b/packages/idb-bridge/tsconfig.json
@@ -8,6 +8,7 @@
"noImplicitAny": true,
"outDir": "lib",
"declaration": true,
+ "declarationMap": true,
"noEmitOnError": true,
"strict": true,
"incremental": true,