Skip to content

Commit deacb2e

Browse files
authored
Merge pull request #127 from nolanlawson/auto
Fix `auto` throwing an error in the browser
2 parents c764a91 + 8686cfe commit deacb2e

File tree

3 files changed

+120
-26
lines changed

3 files changed

+120
-26
lines changed

auto/index.js

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,37 @@ var globalVar =
2222
? global
2323
: Function("return this;")();
2424

25-
globalVar.indexedDB = fakeIndexedDB;
26-
globalVar.IDBCursor = FDBCursor;
27-
globalVar.IDBCursorWithValue = FDBCursorWithValue;
28-
globalVar.IDBDatabase = FDBDatabase;
29-
globalVar.IDBFactory = FDBFactory;
30-
globalVar.IDBIndex = FDBIndex;
31-
globalVar.IDBKeyRange = FDBKeyRange;
32-
globalVar.IDBObjectStore = FDBObjectStore;
33-
globalVar.IDBOpenDBRequest = FDBOpenDBRequest;
34-
globalVar.IDBRecord = FDBRecord;
35-
globalVar.IDBRequest = FDBRequest;
36-
globalVar.IDBTransaction = FDBTransaction;
37-
globalVar.IDBVersionChangeEvent = FDBVersionChangeEvent;
25+
// Match the native behavior for `globalThis.indexedDB`, `globlThis.IDBCursor`, etc.
26+
// Per the IDL, `indexedDB` is readonly but the others are readwrite
27+
// https://w3c.github.io/IndexedDB/#idl-index
28+
const createPropertyDescriptor = (value, readOnly = false) => {
29+
return {
30+
...(readOnly
31+
? {
32+
set: undefined,
33+
get: () => value,
34+
}
35+
: {
36+
value,
37+
writable: true,
38+
}),
39+
enumerable: true,
40+
configurable: true,
41+
};
42+
};
43+
44+
Object.defineProperties(globalVar, {
45+
indexedDB: createPropertyDescriptor(fakeIndexedDB, true),
46+
IDBCursor: createPropertyDescriptor(FDBCursor),
47+
IDBCursorWithValue: createPropertyDescriptor(FDBCursorWithValue),
48+
IDBDatabase: createPropertyDescriptor(FDBDatabase),
49+
IDBFactory: createPropertyDescriptor(FDBFactory),
50+
IDBIndex: createPropertyDescriptor(FDBIndex),
51+
IDBKeyRange: createPropertyDescriptor(FDBKeyRange),
52+
IDBObjectStore: createPropertyDescriptor(FDBObjectStore),
53+
IDBOpenDBRequest: createPropertyDescriptor(FDBOpenDBRequest),
54+
IDBRecord: createPropertyDescriptor(FDBRecord),
55+
IDBRequest: createPropertyDescriptor(FDBRequest),
56+
IDBTransaction: createPropertyDescriptor(FDBTransaction),
57+
IDBVersionChangeEvent: createPropertyDescriptor(FDBVersionChangeEvent),
58+
});

auto/index.mjs

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,37 @@ var globalVar =
2222
? global
2323
: Function("return this;")();
2424

25-
globalVar.indexedDB = fakeIndexedDB;
26-
globalVar.IDBCursor = FDBCursor;
27-
globalVar.IDBCursorWithValue = FDBCursorWithValue;
28-
globalVar.IDBDatabase = FDBDatabase;
29-
globalVar.IDBFactory = FDBFactory;
30-
globalVar.IDBIndex = FDBIndex;
31-
globalVar.IDBKeyRange = FDBKeyRange;
32-
globalVar.IDBObjectStore = FDBObjectStore;
33-
globalVar.IDBOpenDBRequest = FDBOpenDBRequest;
34-
globalVar.IDBRecord = FDBRecord;
35-
globalVar.IDBRequest = FDBRequest;
36-
globalVar.IDBTransaction = FDBTransaction;
37-
globalVar.IDBVersionChangeEvent = FDBVersionChangeEvent;
25+
// Match the native behavior for `globalThis.indexedDB`, `globlThis.IDBCursor`, etc.
26+
// Per the IDL, `indexedDB` is readonly but the others are readwrite
27+
// https://w3c.github.io/IndexedDB/#idl-index
28+
const createPropertyDescriptor = (value, readOnly = false) => {
29+
return {
30+
...(readOnly
31+
? {
32+
set: undefined,
33+
get: () => value,
34+
}
35+
: {
36+
value,
37+
writable: true,
38+
}),
39+
enumerable: true,
40+
configurable: true,
41+
};
42+
};
43+
44+
Object.defineProperties(globalVar, {
45+
indexedDB: createPropertyDescriptor(fakeIndexedDB, true),
46+
IDBCursor: createPropertyDescriptor(FDBCursor),
47+
IDBCursorWithValue: createPropertyDescriptor(FDBCursorWithValue),
48+
IDBDatabase: createPropertyDescriptor(FDBDatabase),
49+
IDBFactory: createPropertyDescriptor(FDBFactory),
50+
IDBIndex: createPropertyDescriptor(FDBIndex),
51+
IDBKeyRange: createPropertyDescriptor(FDBKeyRange),
52+
IDBObjectStore: createPropertyDescriptor(FDBObjectStore),
53+
IDBOpenDBRequest: createPropertyDescriptor(FDBOpenDBRequest),
54+
IDBRecord: createPropertyDescriptor(FDBRecord),
55+
IDBRequest: createPropertyDescriptor(FDBRequest),
56+
IDBTransaction: createPropertyDescriptor(FDBTransaction),
57+
IDBVersionChangeEvent: createPropertyDescriptor(FDBVersionChangeEvent),
58+
});

src/test/fakeIndexedDB/auto.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import * as assert from "assert";
2+
import * as fakeIndexedDB from "../../index.js";
3+
4+
// `indexedDB` is read-only, all others are read-write
5+
const readWriteProps = Object.keys(fakeIndexedDB).filter((prop) =>
6+
prop.startsWith("IDB"),
7+
) as Array<keyof typeof fakeIndexedDB>;
8+
9+
describe("auto", () => {
10+
it("correctly overrides globals in fake-indexeddb/auto", async () => {
11+
// simulate what the native property descriptors do
12+
Object.defineProperty(globalThis, "indexedDB", {
13+
set: undefined,
14+
get: () => undefined,
15+
enumerable: true,
16+
configurable: true,
17+
});
18+
for (const prop of readWriteProps) {
19+
Object.defineProperty(globalThis, prop, {
20+
value: undefined,
21+
enumerable: true,
22+
configurable: true,
23+
writable: true,
24+
});
25+
}
26+
27+
// @ts-expect-error relative to the build/ directory
28+
await import("../../../../auto/index.mjs");
29+
30+
// check read-only indexedDB global
31+
const descriptor = Object.getOwnPropertyDescriptor(
32+
globalThis,
33+
"indexedDB",
34+
);
35+
assert.equal(descriptor!.set, undefined);
36+
assert.equal(descriptor!.get!(), fakeIndexedDB.indexedDB);
37+
assert.equal(descriptor!.enumerable, true);
38+
assert.equal(descriptor!.configurable, true);
39+
40+
// check read-write globals
41+
for (const prop of readWriteProps) {
42+
const descriptor = Object.getOwnPropertyDescriptor(
43+
globalThis,
44+
prop,
45+
);
46+
assert.equal(descriptor!.value, fakeIndexedDB[prop]);
47+
assert.equal(descriptor!.enumerable, true);
48+
assert.equal(descriptor!.configurable, true);
49+
assert.equal(descriptor!.writable, true);
50+
}
51+
});
52+
});

0 commit comments

Comments
 (0)