@hkdigital/lib-sveltekit 0.0.70 → 0.0.72

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,8 +1,13 @@
1
1
  /**
2
2
  * HkPromise extends the default javascript Promise class
3
+ * - Exposes methods to interact with the state of the
4
+ * promise, such as 'resolve' and 'reject'
3
5
  */
4
6
  export default class HkPromise extends Promise<any> {
5
- constructor(initFn: any);
7
+ /**
8
+ * @param {()=>void} [initFn]
9
+ */
10
+ constructor(initFn?: () => void);
6
11
  /**
7
12
  * Get value of property [resolved]
8
13
  *
@@ -45,8 +45,24 @@ const hasThen$ = Symbol('hasThen');
45
45
 
46
46
  /**
47
47
  * HkPromise extends the default javascript Promise class
48
+ * - Exposes methods to interact with the state of the
49
+ * promise, such as 'resolve' and 'reject'
48
50
  */
49
51
  export default class HkPromise extends Promise {
52
+ // > TODO: convert to JS private properties
53
+ // #resolveFn
54
+ // #rejectFn
55
+ // #pending
56
+ // #resolved
57
+ // #rejected
58
+ // #cancelled
59
+ // #timeout
60
+ // #timeoutTimer
61
+ // #hasThen
62
+
63
+ /**
64
+ * @param {()=>void} [initFn]
65
+ */
50
66
  constructor(initFn) {
51
67
  let _resolveFn;
52
68
  let _rejectFn;
@@ -81,8 +97,6 @@ export default class HkPromise extends Promise {
81
97
  // this[ timeoutTimer$ ] = undefined;
82
98
  }
83
99
 
84
- // -------------------------------------------------------------------- Method
85
-
86
100
  /**
87
101
  * Get value of property [resolved]
88
102
  *
@@ -92,8 +106,6 @@ export default class HkPromise extends Promise {
92
106
  return this[resolved$] ? true : false;
93
107
  }
94
108
 
95
- // -------------------------------------------------------------------- Method
96
-
97
109
  /**
98
110
  * Get value of property [rejected]
99
111
  *
@@ -103,8 +115,6 @@ export default class HkPromise extends Promise {
103
115
  return this[rejected$] ? true : false;
104
116
  }
105
117
 
106
- // -------------------------------------------------------------------- Method
107
-
108
118
  /**
109
119
  * Get value of property [pending]
110
120
  *
@@ -114,8 +124,6 @@ export default class HkPromise extends Promise {
114
124
  return this[pending$];
115
125
  }
116
126
 
117
- // -------------------------------------------------------------------- Method
118
-
119
127
  /**
120
128
  * Get value of property [cancelled]
121
129
  *
@@ -125,8 +133,6 @@ export default class HkPromise extends Promise {
125
133
  return this[cancelled$] ? true : false;
126
134
  }
127
135
 
128
- // -------------------------------------------------------------------- Method
129
-
130
136
  /**
131
137
  * Get value of property [timeout]
132
138
  *
@@ -136,8 +142,6 @@ export default class HkPromise extends Promise {
136
142
  return this[timeout$] ? true : false;
137
143
  }
138
144
 
139
- // -------------------------------------------------------------------- Method
140
-
141
145
  /**
142
146
  * Resolve the promise
143
147
  *
@@ -191,8 +195,6 @@ export default class HkPromise extends Promise {
191
195
  return this;
192
196
  }
193
197
 
194
- // -------------------------------------------------------------------- Method
195
-
196
198
  /**
197
199
  * Reject the promise
198
200
  *
@@ -238,8 +240,6 @@ export default class HkPromise extends Promise {
238
240
  return this;
239
241
  }
240
242
 
241
- // -------------------------------------------------------------------- Method
242
-
243
243
  /**
244
244
  * Reject the promise if the promise is still pending
245
245
  *
@@ -256,8 +256,6 @@ export default class HkPromise extends Promise {
256
256
  return this;
257
257
  }
258
258
 
259
- // -------------------------------------------------------------------- Method
260
-
261
259
  /**
262
260
  * Reject the promise and set this.cancelled=true
263
261
  *
@@ -283,8 +281,6 @@ export default class HkPromise extends Promise {
283
281
  return this;
284
282
  }
285
283
 
286
- // -------------------------------------------------------------------- Method
287
-
288
284
  /**
289
285
  * Reject the promise and set this.cancelled=true
290
286
  *
@@ -301,8 +297,6 @@ export default class HkPromise extends Promise {
301
297
  return this;
302
298
  }
303
299
 
304
- // -------------------------------------------------------------------- Method
305
-
306
300
  /**
307
301
  * Specify the number of milliseconds until the promise should time out.
308
302
  * - When a timeout occurs: the promise is cancelled and the following
@@ -360,8 +354,6 @@ export default class HkPromise extends Promise {
360
354
  return this;
361
355
  }
362
356
 
363
- // -------------------------------------------------------------------- Method
364
-
365
357
  /**
366
358
  * Register a callback that is called when the promise resolves
367
359
  *
@@ -373,8 +365,6 @@ export default class HkPromise extends Promise {
373
365
  return super.then(...arguments);
374
366
  }
375
367
 
376
- // -------------------------------------------------------------------- Method
377
-
378
368
  /**
379
369
  * Register a callback that is called when the promise rejects, is
380
370
  * cancelled or times out
@@ -7,7 +7,7 @@
7
7
  /**
8
8
  * Make GET request
9
9
  *
10
- * @paramm {object} _
10
+ * @param {object} _
11
11
  *
12
12
  * @param {string|URL} _.url - Url string or URL object
13
13
  *
@@ -25,13 +25,19 @@
25
25
  * If defined, this request will abort after the specified number of
26
26
  * milliseconds. Values above the the built-in request timeout won't work.
27
27
  *
28
- * @returns {Promise<*>} responsePromise
28
+ * @returns {Promise<Response>} responsePromise
29
29
  */
30
- export function httpGet({ url, urlSearchParams, headers, requestHandler, timeoutMs }: string | URL): Promise<any>;
30
+ export function httpGet({ url, urlSearchParams, headers, requestHandler, timeoutMs }: {
31
+ url: string | URL;
32
+ urlSearchParams?: object;
33
+ headers?: object;
34
+ requestHandler?: requestHandler;
35
+ timeoutMs?: number;
36
+ }): Promise<Response>;
31
37
  /**
32
38
  * Make POST request
33
39
  *
34
- * @paramm {object} _
40
+ * @param {object} _
35
41
  *
36
42
  * @param {string|URL} _.url - Url string or URL object
37
43
  *
@@ -48,15 +54,21 @@ export function httpGet({ url, urlSearchParams, headers, requestHandler, timeout
48
54
  * If defined, this request will abort after the specified number of
49
55
  * milliseconds. Values above the the built-in request timeout won't work.
50
56
  *
51
- * @returns {Promise<*>} responsePromise
57
+ * @returns {Promise<Response>} responsePromise
52
58
  */
53
- export function httpPost({ url, body, headers, requestHandler, timeoutMs }: string | URL): Promise<any>;
59
+ export function httpPost({ url, body, headers, requestHandler, timeoutMs }: {
60
+ url: string | URL;
61
+ body?: any;
62
+ headers?: object;
63
+ requestHandler?: requestHandler;
64
+ timeoutMs?: number;
65
+ }): Promise<Response>;
54
66
  /**
55
67
  * Make an HTTP request
56
68
  * - This is a low level function, consider using
57
69
  * httpGet, httpPost, jsonGet or jsonPost instead
58
70
  *
59
- * @paramm {object} _
71
+ * @param {object} _
60
72
  *
61
73
  * @param {string|URL} _.url - Url string or URL object
62
74
  *
@@ -82,7 +94,15 @@ export function httpPost({ url, body, headers, requestHandler, timeoutMs }: stri
82
94
  * @note Check the `ok` property of the resolved response to check if the
83
95
  * response was successfull (e.g. in case of a 404, ok is false)
84
96
  *
85
- * @returns {Promise<*>} responsePromise
97
+ * @returns {Promise<Response>} responsePromise
86
98
  */
87
- export function httpRequest({ method, url, urlSearchParams, body, headers, requestHandler, timeoutMs }: string | URL): Promise<any>;
99
+ export function httpRequest({ method, url, urlSearchParams, body, headers, requestHandler, timeoutMs }: {
100
+ url: string | URL;
101
+ method: string;
102
+ urlSearchParams?: object;
103
+ body?: any;
104
+ headers?: object;
105
+ requestHandler?: requestHandler;
106
+ timeoutMs?: number;
107
+ }): Promise<Response>;
88
108
  export type requestHandler = (controller: AbortController, abort: (reason?: Error) => void, timeout: (delayMs: number) => void) => any;
@@ -21,7 +21,7 @@ import { waitForAndCheckResponse } from './response.js';
21
21
  /**
22
22
  * Make GET request
23
23
  *
24
- * @paramm {object} _
24
+ * @param {object} _
25
25
  *
26
26
  * @param {string|URL} _.url - Url string or URL object
27
27
  *
@@ -39,7 +39,7 @@ import { waitForAndCheckResponse } from './response.js';
39
39
  * If defined, this request will abort after the specified number of
40
40
  * milliseconds. Values above the the built-in request timeout won't work.
41
41
  *
42
- * @returns {Promise<*>} responsePromise
42
+ * @returns {Promise<Response>} responsePromise
43
43
  */
44
44
  export async function httpGet({ url, urlSearchParams, headers, requestHandler, timeoutMs }) {
45
45
  const responsePromise = httpRequest({
@@ -57,7 +57,7 @@ export async function httpGet({ url, urlSearchParams, headers, requestHandler, t
57
57
  /**
58
58
  * Make POST request
59
59
  *
60
- * @paramm {object} _
60
+ * @param {object} _
61
61
  *
62
62
  * @param {string|URL} _.url - Url string or URL object
63
63
  *
@@ -74,7 +74,7 @@ export async function httpGet({ url, urlSearchParams, headers, requestHandler, t
74
74
  * If defined, this request will abort after the specified number of
75
75
  * milliseconds. Values above the the built-in request timeout won't work.
76
76
  *
77
- * @returns {Promise<*>} responsePromise
77
+ * @returns {Promise<Response>} responsePromise
78
78
  */
79
79
  export async function httpPost({ url, body = null, headers, requestHandler, timeoutMs }) {
80
80
  const responsePromise = httpRequest({
@@ -96,7 +96,7 @@ export async function httpPost({ url, body = null, headers, requestHandler, time
96
96
  * - This is a low level function, consider using
97
97
  * httpGet, httpPost, jsonGet or jsonPost instead
98
98
  *
99
- * @paramm {object} _
99
+ * @param {object} _
100
100
  *
101
101
  * @param {string|URL} _.url - Url string or URL object
102
102
  *
@@ -122,7 +122,7 @@ export async function httpPost({ url, body = null, headers, requestHandler, time
122
122
  * @note Check the `ok` property of the resolved response to check if the
123
123
  * response was successfull (e.g. in case of a 404, ok is false)
124
124
  *
125
- * @returns {Promise<*>} responsePromise
125
+ * @returns {Promise<Response>} responsePromise
126
126
  */
127
127
  export async function httpRequest({
128
128
  method,
@@ -1,3 +1,9 @@
1
+ /**
2
+ * @callback progressCallback
3
+ * @param {object} _
4
+ * @param {number} _.bytesLoaded
5
+ * @param {number} _.size
6
+ */
1
7
  /**
2
8
  * Check if the response status is ok
3
9
  *
@@ -30,3 +36,21 @@ export function getResponseSize(response: Response): number;
30
36
  * @returns {object} response
31
37
  */
32
38
  export function waitForAndCheckResponse(responsePromise: Promise<object>, url: URL): object;
39
+ /**
40
+ * Load response body as ArrayBuffer
41
+ * - Progress can be monitored by suppying an onProgress callback
42
+ * - Loading can be aborted by calling the returned abort function
43
+ *
44
+ * @param {Response} response - Fetch response
45
+ * @param {progressCallback} onProgress
46
+ *
47
+ * @returns {{ bufferPromise: Promise<ArrayBuffer>, abort: () => void }}
48
+ */
49
+ export function loadResponseBuffer(response: Response, onProgress: progressCallback): {
50
+ bufferPromise: Promise<ArrayBuffer>;
51
+ abort: () => void;
52
+ };
53
+ export type progressCallback = (_: {
54
+ bytesLoaded: number;
55
+ size: number;
56
+ }) => any;
@@ -7,6 +7,17 @@ import { href } from './url.js';
7
7
 
8
8
  import { getErrorFromResponse } from './errors.js';
9
9
 
10
+ // > Types
11
+
12
+ /**
13
+ * @callback progressCallback
14
+ * @param {object} _
15
+ * @param {number} _.bytesLoaded
16
+ * @param {number} _.size
17
+ */
18
+
19
+ // > Exports
20
+
10
21
  /**
11
22
  * Check if the response status is ok
12
23
  *
@@ -116,3 +127,91 @@ export async function waitForAndCheckResponse(responsePromise, url) {
116
127
 
117
128
  return response;
118
129
  }
130
+
131
+ /**
132
+ * Load response body as ArrayBuffer
133
+ * - Progress can be monitored by suppying an onProgress callback
134
+ * - Loading can be aborted by calling the returned abort function
135
+ *
136
+ * @param {Response} response - Fetch response
137
+ * @param {progressCallback} onProgress
138
+ *
139
+ * @returns {{ bufferPromise: Promise<ArrayBuffer>, abort: () => void }}
140
+ */
141
+ export function loadResponseBuffer(response, onProgress) {
142
+ // @note size might be 0
143
+ const size = getResponseSize(response);
144
+
145
+ let bytesLoaded = 0;
146
+
147
+ if (onProgress && size) {
148
+ onProgress({ bytesLoaded, size });
149
+ }
150
+
151
+ if (!response.body) {
152
+ throw new Error('Missing [response.body]');
153
+ }
154
+
155
+ const reader = response.body.getReader();
156
+
157
+ let aborted = false;
158
+
159
+ /**
160
+ * @returns {Promise<ArrayBuffer>}
161
+ */
162
+ async function read() {
163
+ let loading = true;
164
+ let chunks = [];
165
+
166
+ // - Use flag 'loading'
167
+ // - Check if #abortLoading still exists
168
+ while (loading) {
169
+ const { done, value } = await reader.read();
170
+
171
+ if (done || aborted) {
172
+ // Loading complete or this.#abortLoading has been cleared,
173
+ // which is done when the request has been cancelled
174
+ loading = false;
175
+ } else {
176
+ bytesLoaded += value.length;
177
+
178
+ if (size && bytesLoaded > size) {
179
+ throw new Error(`Received more bytes that specified by header content-length`);
180
+ }
181
+
182
+ chunks.push(value);
183
+
184
+ if (onProgress && size) {
185
+ onProgress({ bytesLoaded, size });
186
+ }
187
+ }
188
+ } // end while
189
+
190
+ if (size && bytesLoaded !== size) {
191
+ throw new Error(`Received [${bytesLoaded}], but expected [${size}] bytes`);
192
+ }
193
+
194
+ // Concat the chinks into a single array
195
+ let buffer = new ArrayBuffer(bytesLoaded);
196
+ let body = new Uint8Array(buffer);
197
+
198
+ let offset = 0;
199
+
200
+ // Place the chunks in the buffer
201
+ for (let chunk of chunks) {
202
+ body.set(chunk, offset);
203
+ offset += chunk.length;
204
+ } // end for
205
+
206
+ return buffer;
207
+ }
208
+
209
+ const bufferPromise = read();
210
+
211
+ return {
212
+ bufferPromise,
213
+ abort: () => {
214
+ aborted = true;
215
+ }
216
+ };
217
+ } // end fn
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hkdigital/lib-sveltekit",
3
- "version": "0.0.70",
3
+ "version": "0.0.72",
4
4
  "author": "Jens Kleinhout, HKdigital (https://hkdigital.nl)",
5
5
  "license": "ISC",
6
6
  "repository": {