Skip to content

Commit

Permalink
Drop structuredClone polyfill and support for Node 16
Browse files Browse the repository at this point in the history
  • Loading branch information
dumbmatter committed Oct 13, 2023
1 parent 92256e5 commit 81291cd
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 124 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 5.0.0 ()

- Dropped support for Node.js 16, which allows me to get rid of the structuredClone polyfill, which reduces the package size by roughly 50%.

# 4.0.2 (2023-07-14)

- #84 - Fix the TypeScript types in some situations.
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@
"lib",
"types.d.ts"
],
"dependencies": {
"realistic-structured-clone": "^3.0.0"
"engines": {
"node": ">=18"
},
"devDependencies": {
"@babel/cli": "^7.18.6",
Expand Down
11 changes: 5 additions & 6 deletions src/FDBCursor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
TransactionInactiveError,
} from "./lib/errors.js";
import extractKey from "./lib/extractKey.js";
import structuredClone from "./lib/structuredClone.js";
import {
CursorRange,
CursorSource,
Expand All @@ -34,7 +33,7 @@ const getEffectiveObjectStore = (cursor: FDBCursor) => {
const makeKeyRange = (
range: FDBKeyRange,
lowers: (Key | undefined)[],
uppers: (Key | undefined)[],
uppers: (Key | undefined)[]
) => {
// Start with bounds from range
let lower = range !== undefined ? range.lower : undefined;
Expand Down Expand Up @@ -91,7 +90,7 @@ class FDBCursor {
range: CursorRange,
direction: FDBCursorDirection = "next",
request?: FDBRequest,
keyOnly: boolean = false,
keyOnly: boolean = false
) {
this._range = range;
this._source = source;
Expand Down Expand Up @@ -335,7 +334,7 @@ class FDBCursor {
}
const value =
this.source.objectStore._rawObjectStore.getValue(
foundRecord.value,
foundRecord.value
);
(this as any).value = structuredClone(value);
}
Expand Down Expand Up @@ -407,7 +406,7 @@ class FDBCursor {
effectiveObjectStore._rawObjectStore,
record,
false,
transaction._rollbackLog,
transaction._rollbackLog
),
source: this,
});
Expand Down Expand Up @@ -613,7 +612,7 @@ class FDBCursor {
operation: effectiveObjectStore._rawObjectStore.deleteRecord.bind(
effectiveObjectStore._rawObjectStore,
effectiveKey,
transaction._rollbackLog,
transaction._rollbackLog
),
source: this,
});
Expand Down
47 changes: 23 additions & 24 deletions src/FDBObjectStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import extractKey from "./lib/extractKey.js";
import FakeDOMStringList from "./lib/FakeDOMStringList.js";
import Index from "./lib/Index.js";
import ObjectStore from "./lib/ObjectStore.js";
import structuredClone from "./lib/structuredClone.js";
import { FDBCursorDirection, Key, KeyPath, Value } from "./lib/types.js";
import validateKeyPath from "./lib/validateKeyPath.js";
import valueToKey from "./lib/valueToKey.js";
Expand All @@ -38,7 +37,7 @@ const confirmActiveTransaction = (objectStore: FDBObjectStore) => {
const buildRecordAddPut = (
objectStore: FDBObjectStore,
value: Value,
key: Key,
key: Key
) => {
confirmActiveTransaction(objectStore);

Expand Down Expand Up @@ -106,7 +105,7 @@ class FDBObjectStore {
this.autoIncrement = rawObjectStore.autoIncrement;
this.transaction = transaction;
this.indexNames = new FakeDOMStringList(
...Array.from(rawObjectStore.rawIndexes.keys()).sort(),
...Array.from(rawObjectStore.rawIndexes.keys()).sort()
);
}

Expand Down Expand Up @@ -144,20 +143,20 @@ class FDBObjectStore {
this._rawObjectStore.rawDatabase.rawObjectStores.delete(oldName);
this._rawObjectStore.rawDatabase.rawObjectStores.set(
name,
this._rawObjectStore,
this._rawObjectStore
);
transaction.db.objectStoreNames = new FakeDOMStringList(
...Array.from(
this._rawObjectStore.rawDatabase.rawObjectStores.keys(),
this._rawObjectStore.rawDatabase.rawObjectStores.keys()
)
.filter((objectStoreName) => {
const objectStore =
this._rawObjectStore.rawDatabase.rawObjectStores.get(
objectStoreName,
objectStoreName
);
return objectStore && !objectStore.deleted;
})
.sort(),
.sort()
);

const oldScope = new Set(transaction._scope);
Expand All @@ -167,7 +166,7 @@ class FDBObjectStore {
this.transaction._scope.delete(oldName);
transaction._scope.add(name);
transaction.objectStoreNames = new FakeDOMStringList(
...Array.from(transaction._scope).sort(),
...Array.from(transaction._scope).sort()
);

transaction._rollbackLog.push(() => {
Expand All @@ -178,15 +177,15 @@ class FDBObjectStore {
this._rawObjectStore.rawDatabase.rawObjectStores.delete(name);
this._rawObjectStore.rawDatabase.rawObjectStores.set(
oldName,
this._rawObjectStore,
this._rawObjectStore
);
transaction.db.objectStoreNames = new FakeDOMStringList(
...oldObjectStoreNames,
...oldObjectStoreNames
);

transaction._scope = oldScope;
transaction.objectStoreNames = new FakeDOMStringList(
...oldTransactionObjectStoreNames,
...oldTransactionObjectStoreNames
);
});
}
Expand All @@ -202,7 +201,7 @@ class FDBObjectStore {
this._rawObjectStore,
record,
false,
this.transaction._rollbackLog,
this.transaction._rollbackLog
),
source: this,
});
Expand All @@ -219,7 +218,7 @@ class FDBObjectStore {
this._rawObjectStore,
record,
true,
this.transaction._rollbackLog,
this.transaction._rollbackLog
),
source: this,
});
Expand All @@ -243,7 +242,7 @@ class FDBObjectStore {
operation: this._rawObjectStore.deleteRecord.bind(
this._rawObjectStore,
key,
this.transaction._rollbackLog,
this.transaction._rollbackLog
),
source: this,
});
Expand All @@ -262,7 +261,7 @@ class FDBObjectStore {
return this.transaction._execRequestAsync({
operation: this._rawObjectStore.getValue.bind(
this._rawObjectStore,
key,
key
),
source: this,
});
Expand All @@ -281,7 +280,7 @@ class FDBObjectStore {
operation: this._rawObjectStore.getAllValues.bind(
this._rawObjectStore,
range,
count,
count
),
source: this,
});
Expand All @@ -301,7 +300,7 @@ class FDBObjectStore {
return this.transaction._execRequestAsync({
operation: this._rawObjectStore.getKey.bind(
this._rawObjectStore,
key,
key
),
source: this,
});
Expand All @@ -320,7 +319,7 @@ class FDBObjectStore {
operation: this._rawObjectStore.getAllKeys.bind(
this._rawObjectStore,
range,
count,
count
),
source: this,
});
Expand All @@ -336,15 +335,15 @@ class FDBObjectStore {
return this.transaction._execRequestAsync({
operation: this._rawObjectStore.clear.bind(
this._rawObjectStore,
this.transaction._rollbackLog,
this.transaction._rollbackLog
),
source: this,
});
}

public openCursor(
range?: FDBKeyRange | Key,
direction?: FDBCursorDirection,
direction?: FDBCursorDirection
) {
confirmActiveTransaction(this);

Expand All @@ -370,7 +369,7 @@ class FDBObjectStore {

public openKeyCursor(
range?: FDBKeyRange | Key,
direction?: FDBCursorDirection,
direction?: FDBCursorDirection
) {
confirmActiveTransaction(this);

Expand Down Expand Up @@ -399,7 +398,7 @@ class FDBObjectStore {
public createIndex(
name: string,
keyPath: KeyPath,
optionalParameters: { multiEntry?: boolean; unique?: boolean } = {},
optionalParameters: { multiEntry?: boolean; unique?: boolean } = {}
) {
if (arguments.length < 2) {
throw new TypeError();
Expand Down Expand Up @@ -453,7 +452,7 @@ class FDBObjectStore {
name,
keyPath,
multiEntry,
unique,
unique
);
this.indexNames._push(name);
this.indexNames._sort();
Expand Down Expand Up @@ -519,7 +518,7 @@ class FDBObjectStore {
this.indexNames = new FakeDOMStringList(
...Array.from(this.indexNames).filter((indexName) => {
return indexName !== name;
}),
})
);
rawIndex.deleted = true; // Not sure if this is supposed to happen synchronously

Expand Down
3 changes: 1 addition & 2 deletions src/lib/Index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { ConstraintError } from "./errors.js";
import extractKey from "./extractKey.js";
import ObjectStore from "./ObjectStore.js";
import RecordStore from "./RecordStore.js";
import structuredClone from "./structuredClone.js";
import { Key, KeyPath, Record } from "./types.js";
import valueToKey from "./valueToKey.js";

Expand All @@ -26,7 +25,7 @@ class Index {
name: string,
keyPath: KeyPath,
multiEntry: boolean,
unique: boolean,
unique: boolean
) {
this.rawObjectStore = rawObjectStore;

Expand Down
7 changes: 3 additions & 4 deletions src/lib/ObjectStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import extractKey from "./extractKey.js";
import Index from "./Index.js";
import KeyGenerator from "./KeyGenerator.js";
import RecordStore from "./RecordStore.js";
import structuredClone from "./structuredClone.js";
import { Key, KeyPath, Record, RollbackLog } from "./types.js";

// http://www.w3.org/TR/2015/REC-IndexedDB-20150108/#dfn-object-store
Expand All @@ -23,7 +22,7 @@ class ObjectStore {
rawDatabase: Database,
name: string,
keyPath: KeyPath | null,
autoIncrement: boolean,
autoIncrement: boolean
) {
this.rawDatabase = rawDatabase;
this.keyGenerator = autoIncrement === true ? new KeyGenerator() : null;
Expand Down Expand Up @@ -86,7 +85,7 @@ class ObjectStore {
public storeRecord(
newRecord: Record,
noOverwrite: boolean,
rollbackLog?: RollbackLog,
rollbackLog?: RollbackLog
) {
if (this.keyPath !== null) {
const key = extractKey(this.keyPath, newRecord.value);
Expand All @@ -112,7 +111,7 @@ class ObjectStore {
if (this.keyPath !== null) {
if (Array.isArray(this.keyPath)) {
throw new Error(
"Cannot have an array key path in an object store with a key generator",
"Cannot have an array key path in an object store with a key generator"
);
}
let remainingKeyPath = this.keyPath;
Expand Down
1 change: 0 additions & 1 deletion src/lib/extractKey.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import structuredClone from "./structuredClone.js";
import { Key, KeyPath, Value } from "./types.js";
import valueToKey from "./valueToKey.js";

Expand Down
19 changes: 0 additions & 19 deletions src/lib/structuredClone.ts

This file was deleted.

Loading

0 comments on commit 81291cd

Please sign in to comment.