Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
titaniumnetwork-dev
GitHub Repository: titaniumnetwork-dev/Ultraviolet
Path: blob/main/src/client/idb.js
304 views
1
import EventEmitter from "events";
2
import HookEvent from "./hook.js";
3
4
/**
5
* @typedef {import('./index').default} Ultraviolet
6
*/
7
8
class IDBApi extends EventEmitter {
9
/**
10
*
11
* @param {Ultraviolet} ctx
12
*/
13
constructor(ctx) {
14
super();
15
this.ctx = ctx;
16
this.window = this.ctx.window;
17
this.IDBDatabase = this.window.IDBDatabase || {};
18
this.idbDatabaseProto = this.IDBDatabase.prototype || {};
19
this.IDBFactory = this.window.IDBFactory || {};
20
this.idbFactoryProto = this.IDBFactory.prototype || {};
21
this.open = this.idbFactoryProto.open;
22
}
23
overrideOpen() {
24
this.ctx.override(
25
this.IDBFactory.prototype,
26
"open",
27
(target, that, args) => {
28
if (!args.length) return target.apply(that, args);
29
30
if (!args.length) return target.apply(that, args);
31
const [name, version] = args;
32
33
const event = new HookEvent({ name, version }, target, that);
34
this.emit("idbFactoryOpen", event);
35
36
if (event.intercepted) return event.returnValue;
37
return event.target.call(
38
event.that,
39
event.data.name,
40
event.data.version
41
);
42
}
43
);
44
}
45
overrideName() {
46
this.ctx.overrideDescriptor(this.idbDatabaseProto, "name", {
47
get: (target, that) => {
48
const event = new HookEvent({ value: target.call(that) }, target, that);
49
this.emit("idbFactoryName", event);
50
51
if (event.intercepted) return event.returnValue;
52
return event.data.value;
53
},
54
});
55
}
56
}
57
58
export default IDBApi;
59
60