Skip to content

Commit cd8e27c

Browse files
authored
Merge pull request #135 from nolanlawson/fix-globals
Fix `globalThis.indexedDB = ...`
2 parents 0a8ad3f + ad11ae9 commit cd8e27c

File tree

3 files changed

+27
-43
lines changed

3 files changed

+27
-43
lines changed

auto/index.js

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

25-
// Match the native behavior for `globalThis.indexedDB`, `globlThis.IDBCursor`, etc.
26-
// Per the IDL, `indexedDB` is readonly but the others are readwrite
25+
// Partly match the native behavior for `globalThis.indexedDB`, `globalThis.IDBCursor`, etc.
26+
// Per the IDL, `indexedDB` is readonly but the others are readwrite. For us, though, we want it to still
27+
// be overwritable with `globalThis.<global> = ...`, so we make them all readwrite.
2728
// https://w3c.github.io/IndexedDB/#idl-index
28-
const createPropertyDescriptor = (value, readOnly = false) => {
29+
const createPropertyDescriptor = (value) => {
2930
return {
30-
...(readOnly
31-
? {
32-
set: undefined,
33-
get: () => value,
34-
}
35-
: {
36-
value,
37-
writable: true,
38-
}),
31+
value,
3932
enumerable: true,
4033
configurable: true,
34+
writable: true,
4135
};
4236
};
4337

4438
Object.defineProperties(globalVar, {
45-
indexedDB: createPropertyDescriptor(fakeIndexedDB, true),
39+
indexedDB: createPropertyDescriptor(fakeIndexedDB),
4640
IDBCursor: createPropertyDescriptor(FDBCursor),
4741
IDBCursorWithValue: createPropertyDescriptor(FDBCursorWithValue),
4842
IDBDatabase: createPropertyDescriptor(FDBDatabase),

auto/index.mjs

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

25-
// Match the native behavior for `globalThis.indexedDB`, `globlThis.IDBCursor`, etc.
26-
// Per the IDL, `indexedDB` is readonly but the others are readwrite
25+
// Partly match the native behavior for `globalThis.indexedDB`, `globalThis.IDBCursor`, etc.
26+
// Per the IDL, `indexedDB` is readonly but the others are readwrite. For us, though, we want it to still
27+
// be overwritable with `globalThis.<global> = ...`, so we make them all readwrite.
2728
// https://w3c.github.io/IndexedDB/#idl-index
28-
const createPropertyDescriptor = (value, readOnly = false) => {
29+
const createPropertyDescriptor = (value) => {
2930
return {
30-
...(readOnly
31-
? {
32-
set: undefined,
33-
get: () => value,
34-
}
35-
: {
36-
value,
37-
writable: true,
38-
}),
31+
value,
3932
enumerable: true,
4033
configurable: true,
34+
writable: true,
4135
};
4236
};
4337

4438
Object.defineProperties(globalVar, {
45-
indexedDB: createPropertyDescriptor(fakeIndexedDB, true),
39+
indexedDB: createPropertyDescriptor(fakeIndexedDB),
4640
IDBCursor: createPropertyDescriptor(FDBCursor),
4741
IDBCursorWithValue: createPropertyDescriptor(FDBCursorWithValue),
4842
IDBDatabase: createPropertyDescriptor(FDBDatabase),

src/test/fakeIndexedDB/auto.ts

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import * as assert from "assert";
22
import * as fakeIndexedDB from "../../index.js";
33

44
// `indexedDB` is read-only, all others are read-write
5-
const readWriteProps = Object.keys(fakeIndexedDB).filter((prop) =>
6-
prop.startsWith("IDB"),
5+
const props = Object.keys(fakeIndexedDB).filter(
6+
(prop) => prop.startsWith("IDB") || prop === "indexedDB",
77
) as Array<keyof typeof fakeIndexedDB>;
88

99
describe("auto", () => {
@@ -15,7 +15,7 @@ describe("auto", () => {
1515
enumerable: true,
1616
configurable: true,
1717
});
18-
for (const prop of readWriteProps) {
18+
for (const prop of props) {
1919
Object.defineProperty(globalThis, prop, {
2020
value: undefined,
2121
enumerable: true,
@@ -27,18 +27,8 @@ describe("auto", () => {
2727
// @ts-expect-error relative to the build/ directory
2828
await import("../../../../auto/index.mjs");
2929

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) {
30+
// check our own globals
31+
for (const prop of props) {
4232
const descriptor = Object.getOwnPropertyDescriptor(
4333
globalThis,
4434
prop,
@@ -48,15 +38,21 @@ describe("auto", () => {
4838
assert.equal(descriptor!.configurable, true);
4939
assert.equal(descriptor!.writable, true);
5040
}
41+
42+
// check that we can still overwrite them with `globalThis.<prop> = ...`
43+
for (const prop of props) {
44+
const fake = {};
45+
(globalThis as any)[prop] = fake;
46+
assert.equal((globalThis as any)[prop], fake);
47+
}
5148
});
5249

5350
it("exports as cjs directly, without `default` member - issue #130", async () => {
5451
// @ts-expect-error relative to the build/ directory
5552
await import("../../../../auto/index.js");
5653

5754
// ensure we directly set the export as `module.exports` rather than `module.exports.default`
58-
assert.ok(!(globalThis as any).indexedDB.default);
59-
for (const prop of readWriteProps) {
55+
for (const prop of props) {
6056
assert.ok(!(globalThis as any)[prop].default);
6157
}
6258
});

0 commit comments

Comments
 (0)