forked from CopilotKit/CopilotKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdom.ts
More file actions
85 lines (81 loc) · 2.31 KB
/
Copy pathdom.ts
File metadata and controls
85 lines (81 loc) · 2.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/**
* Polyfill: DOMException, Headers
*
* DOMException is used by CopilotKit's isAbortError check.
* Headers is used by CopilotKit's request construction.
* Skipped if the globals are already defined.
*
* Usage:
* import "@copilotkit/react-native/polyfills/dom";
*/
export {};
const g = globalThis as Record<string, unknown>;
// DOMException
if (typeof g.DOMException === "undefined") {
class DOMExceptionPolyfill extends Error {
code: number;
constructor(message?: string, name?: string) {
super(message);
this.name = name || "DOMException";
this.code = 0;
}
}
g.DOMException = DOMExceptionPolyfill;
}
// Headers
if (typeof g.Headers === "undefined") {
class HeadersPolyfill {
private _map: Record<string, string> = {};
constructor(init?: Record<string, string> | HeadersPolyfill) {
if (init) {
if (init instanceof HeadersPolyfill) {
this._map = { ...init._map };
} else {
for (const [key, value] of Object.entries(init)) {
this._map[key.toLowerCase()] = value;
}
}
}
}
get(name: string): string | null {
return this._map[name.toLowerCase()] ?? null;
}
set(name: string, value: string): void {
this._map[name.toLowerCase()] = value;
}
has(name: string): boolean {
return name.toLowerCase() in this._map;
}
delete(name: string): void {
delete this._map[name.toLowerCase()];
}
append(name: string, value: string): void {
const key = name.toLowerCase();
if (key in this._map) {
this._map[key] += ", " + value;
} else {
this._map[key] = value;
}
}
entries(): IterableIterator<[string, string]> {
return Object.entries(this._map)[Symbol.iterator]();
}
keys(): IterableIterator<string> {
return Object.keys(this._map)[Symbol.iterator]();
}
values(): IterableIterator<string> {
return Object.values(this._map)[Symbol.iterator]();
}
forEach(
callback: (value: string, key: string, parent: HeadersPolyfill) => void,
): void {
for (const [key, value] of Object.entries(this._map)) {
callback(value, key, this);
}
}
[Symbol.iterator](): IterableIterator<[string, string]> {
return this.entries();
}
}
g.Headers = HeadersPolyfill;
}