@hkdigital/lib-sveltekit 0.0.70 → 0.0.71

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
@@ -82,7 +82,7 @@ export function httpPost({ url, body, headers, requestHandler, timeoutMs }: stri
82
82
  * @note Check the `ok` property of the resolved response to check if the
83
83
  * response was successfull (e.g. in case of a 404, ok is false)
84
84
  *
85
- * @returns {Promise<*>} responsePromise
85
+ * @returns {Promise<Response>} responsePromise
86
86
  */
87
- export function httpRequest({ method, url, urlSearchParams, body, headers, requestHandler, timeoutMs }: string | URL): Promise<any>;
87
+ export function httpRequest({ method, url, urlSearchParams, body, headers, requestHandler, timeoutMs }: string | URL): Promise<Response>;
88
88
  export type requestHandler = (controller: AbortController, abort: (reason?: Error) => void, timeout: (delayMs: number) => void) => any;
@@ -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.71",
4
4
  "author": "Jens Kleinhout, HKdigital (https://hkdigital.nl)",
5
5
  "license": "ISC",
6
6
  "repository": {