@wtfalch/chat 0.8.0 → 0.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,3 +0,0 @@
1
- /** Route browser uploads through the same fake transport as sync. Other
2
- * origins keep the native XHR, including its real upload progress. */
3
- export declare function installFakeUploads(): void;
@@ -1,104 +0,0 @@
1
- /** Route browser uploads through the same fake transport as sync. Other
2
- * origins keep the native XHR, including its real upload progress. */
3
- export function installFakeUploads() {
4
- const Native = globalThis.XMLHttpRequest;
5
- if (!Native || installed.has(Native))
6
- return;
7
- class FakeUpload extends Native {
8
- fakeUrl = null;
9
- headers = new Headers();
10
- controller = new AbortController();
11
- code = 0;
12
- state = 0;
13
- text = '';
14
- responseHeaders = new Headers();
15
- open(method, url, async = true) {
16
- const parsed = new URL(url, globalThis.location?.href);
17
- if (parsed.hostname.endsWith('.example.test') &&
18
- parsed.pathname === '/_matrix/media/v3/upload') {
19
- this.fakeUrl = parsed.href;
20
- this.state = 1;
21
- }
22
- else {
23
- super.open(method, url, async);
24
- }
25
- }
26
- get readyState() {
27
- return this.fakeUrl ? this.state : super.readyState;
28
- }
29
- get status() {
30
- return this.fakeUrl ? this.code : super.status;
31
- }
32
- get statusText() {
33
- return this.fakeUrl ? '' : super.statusText;
34
- }
35
- get responseText() {
36
- return this.fakeUrl ? this.text : super.responseText;
37
- }
38
- getResponseHeader(name) {
39
- return this.fakeUrl ? this.responseHeaders.get(name) : super.getResponseHeader(name);
40
- }
41
- setRequestHeader(name, value) {
42
- if (this.fakeUrl)
43
- this.headers.set(name, value);
44
- else
45
- super.setRequestHeader(name, value);
46
- }
47
- abort() {
48
- if (!this.fakeUrl) {
49
- super.abort();
50
- return;
51
- }
52
- this.controller.abort();
53
- this.code = 0;
54
- this.state = 4;
55
- this.dispatchEvent(new Event('readystatechange'));
56
- }
57
- send(body) {
58
- if (!this.fakeUrl) {
59
- super.send(body);
60
- return;
61
- }
62
- const url = this.fakeUrl;
63
- const signal = this.controller.signal;
64
- void (async () => {
65
- // FileReader also handles jsdom's Blob, which has no arrayBuffer().
66
- const bytes = body instanceof Blob ? await readBlob(body) : body;
67
- if (signal.aborted)
68
- return;
69
- const size = body instanceof Blob ? body.size : 0;
70
- this.upload.dispatchEvent(new ProgressEvent('progress', { loaded: size, total: size, lengthComputable: true }));
71
- const response = await fetch(url, {
72
- method: 'POST',
73
- headers: this.headers,
74
- body: bytes,
75
- signal,
76
- });
77
- const text = await response.text();
78
- if (signal.aborted)
79
- return;
80
- this.text = text;
81
- this.code = response.status;
82
- this.responseHeaders = response.headers;
83
- this.state = 4;
84
- this.dispatchEvent(new Event('readystatechange'));
85
- })().catch(() => {
86
- if (!signal.aborted)
87
- this.abort();
88
- });
89
- }
90
- }
91
- installed.add(FakeUpload);
92
- globalThis.XMLHttpRequest = FakeUpload;
93
- }
94
- const installed = new WeakSet();
95
- function readBlob(blob) {
96
- if (blob.arrayBuffer)
97
- return blob.arrayBuffer();
98
- return new Promise((resolve, reject) => {
99
- const reader = new FileReader();
100
- reader.onload = () => resolve(reader.result);
101
- reader.onerror = () => reject(reader.error);
102
- reader.readAsArrayBuffer(blob);
103
- });
104
- }