@sqlrooms/utils 0.0.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.
package/src/xhr.ts ADDED
@@ -0,0 +1,124 @@
1
+ export const postData = async ({
2
+ url,
3
+ data,
4
+ }: {
5
+ url: string;
6
+ data?: Record<string, any>;
7
+ }) => {
8
+ const res: Response = await fetch(url, {
9
+ method: 'POST',
10
+ headers: new Headers({'Content-Type': 'application/json'}),
11
+ credentials: 'same-origin',
12
+ body: JSON.stringify(data),
13
+ });
14
+ if (!res.ok) {
15
+ console.error('Error in postData', {url, data, res});
16
+ let message = res.statusText;
17
+ try {
18
+ message = (await res.json()).error.message;
19
+ console.error(message);
20
+ } catch {
21
+ // ignore
22
+ }
23
+ throw new Error(message);
24
+ }
25
+
26
+ return res.json();
27
+ };
28
+
29
+ export type ProgressInfo = {loaded: number; total: number; ratio: number};
30
+
31
+ // TODO: use range requests to download in chunks
32
+ // https://github.com/duckdb/duckdb-wasm/blob/d9ea9c919b6301e7c6dc8a9b3fd527e86f69a38e/packages/duckdb-wasm/src/bindings/runtime_browser.ts#L307
33
+
34
+ export async function downloadFile(
35
+ url: string,
36
+ opts: {
37
+ method?: string;
38
+ headers?: Record<string, string>;
39
+ onProgress?: (info: ProgressInfo) => void;
40
+ } = {},
41
+ ): Promise<Uint8Array> {
42
+ const {method = 'GET', headers = {}, onProgress} = opts;
43
+ return await new Promise((resolve, reject) => {
44
+ const xhr = new XMLHttpRequest();
45
+
46
+ // https://www.html5rocks.com/en/tutorials/file/xhr2/#toc-bin-data
47
+ xhr.open(method, url, true);
48
+ xhr.responseType = 'arraybuffer';
49
+ Object.keys(headers).map((key) => {
50
+ if (headers[key]) {
51
+ xhr.setRequestHeader(key, headers[key]);
52
+ }
53
+ });
54
+
55
+ xhr.onload = () => resolve(new Uint8Array(xhr.response));
56
+
57
+ xhr.onreadystatechange = () => {
58
+ if (xhr.readyState === XMLHttpRequest.DONE) {
59
+ if (xhr.status >= 200 && xhr.status < 300) {
60
+ // already handled by onload
61
+ } else {
62
+ reject({status: xhr.status, error: `File download failed`});
63
+ }
64
+ }
65
+ };
66
+ xhr.onerror = () =>
67
+ reject({status: xhr.status, error: `File download failed`});
68
+
69
+ if (onProgress) {
70
+ xhr.onprogress = (event) => {
71
+ const {lengthComputable, loaded, total} = event;
72
+ if (lengthComputable) {
73
+ onProgress({loaded, total, ratio: total ? loaded / total : 0});
74
+ }
75
+ };
76
+ }
77
+ xhr.send(null);
78
+ });
79
+ }
80
+
81
+ // TODO: upload in chunks https://www.html5rocks.com/en/tutorials/file/xhr2/
82
+
83
+ export async function uploadFile(
84
+ url: string,
85
+ content: File | Blob | FormData,
86
+ opts: {
87
+ method?: string;
88
+ headers?: Record<string, string>;
89
+ onProgress?: (info: ProgressInfo) => void;
90
+ } = {},
91
+ ): Promise<Response> {
92
+ const {method = 'POST', headers = {}, onProgress} = opts;
93
+ return await new Promise((resolve, reject) => {
94
+ const xhr = new XMLHttpRequest();
95
+ xhr.open(method, url, true);
96
+ Object.keys(headers).map((key) => {
97
+ if (headers[key]) {
98
+ xhr.setRequestHeader(key, headers[key]);
99
+ }
100
+ });
101
+ xhr.onreadystatechange = () => {
102
+ if (xhr.readyState === XMLHttpRequest.DONE) {
103
+ if (xhr.status >= 200 && xhr.status < 300) {
104
+ resolve(xhr.response);
105
+ } else {
106
+ reject({
107
+ status: xhr.status,
108
+ error: xhr.responseText,
109
+ });
110
+ }
111
+ }
112
+ };
113
+ xhr.onerror = () => reject({status: xhr.status, error: xhr.responseText});
114
+ if (onProgress) {
115
+ xhr.upload.onprogress = (event) => {
116
+ const {lengthComputable, loaded, total} = event;
117
+ if (lengthComputable) {
118
+ onProgress({loaded, total, ratio: total ? loaded / total : 0});
119
+ }
120
+ };
121
+ }
122
+ xhr.send(content);
123
+ });
124
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,5 @@
1
+ {
2
+ "extends": "../../tsconfig.json",
3
+ "compilerOptions": {"outDir": "dist"},
4
+ "include": ["src/**/*", "test/**/*", "../../typings.d.ts"]
5
+ }