@etsoo/shared 1.1.86 → 1.1.88

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/README.md CHANGED
@@ -204,6 +204,7 @@ DOM/window related utilities
204
204
  |mergeFormData|Merge form data to primary one|
205
205
  |mergeURLSearchParams|Merge URL search parameters|
206
206
  |setFocus|Set HTML element focus by name|
207
+ |verifyPermission|Verify file system permission|
207
208
 
208
209
  ## ExtendUtils
209
210
  Extend current class/object functioning
@@ -57,7 +57,7 @@ export declare namespace DomUtils {
57
57
  * @param suggestedName Suggested file name
58
58
  * @param autoDetect Auto detect, false will use link click way
59
59
  */
60
- function downloadFile(data: ReadableStream | Blob, suggestedName?: string, autoDetect?: boolean): Promise<void>;
60
+ function downloadFile(data: ReadableStream | Blob, suggestedName?: string, autoDetect?: boolean): Promise<boolean | undefined>;
61
61
  /**
62
62
  * File to data URL
63
63
  * @param file File
@@ -141,4 +141,12 @@ export declare namespace DomUtils {
141
141
  * @param container Container, limits the element range
142
142
  */
143
143
  function setFocus(name: string | object, container?: HTMLElement): void;
144
+ /**
145
+ * Verify file system permission
146
+ * https://developer.mozilla.org/en-US/docs/Web/API/FileSystemHandle/requestPermission
147
+ * @param fileHandle FileSystemHandle
148
+ * @param withWrite With write permission
149
+ * @returns Result
150
+ */
151
+ function verifyPermission(fileHandle: any, withWrite?: boolean): Promise<boolean>;
144
152
  }
@@ -242,9 +242,12 @@ var DomUtils;
242
242
  return __awaiter(this, void 0, void 0, function* () {
243
243
  try {
244
244
  if (autoDetect && 'showSaveFilePicker' in globalThis) {
245
+ // AbortError - Use dismisses the window
245
246
  const handle = yield globalThis.showSaveFilePicker({
246
247
  suggestedName
247
248
  });
249
+ if (!(yield verifyPermission(handle, true)))
250
+ return undefined;
248
251
  const stream = yield handle.createWritable();
249
252
  if (data instanceof Blob) {
250
253
  data.stream().pipeTo(stream);
@@ -252,6 +255,7 @@ var DomUtils;
252
255
  else {
253
256
  yield data.pipeTo(stream);
254
257
  }
258
+ return true;
255
259
  }
256
260
  else {
257
261
  const url = window.URL.createObjectURL(data instanceof Blob
@@ -266,11 +270,13 @@ var DomUtils;
266
270
  a.click();
267
271
  a.remove();
268
272
  window.URL.revokeObjectURL(url);
273
+ return true;
269
274
  }
270
275
  }
271
276
  catch (e) {
272
277
  console.log(e);
273
278
  }
279
+ return false;
274
280
  });
275
281
  }
276
282
  DomUtils.downloadFile = downloadFile;
@@ -471,4 +477,31 @@ var DomUtils;
471
477
  element.focus();
472
478
  }
473
479
  DomUtils.setFocus = setFocus;
480
+ /**
481
+ * Verify file system permission
482
+ * https://developer.mozilla.org/en-US/docs/Web/API/FileSystemHandle/requestPermission
483
+ * @param fileHandle FileSystemHandle
484
+ * @param withWrite With write permission
485
+ * @returns Result
486
+ */
487
+ function verifyPermission(fileHandle, withWrite = false) {
488
+ return __awaiter(this, void 0, void 0, function* () {
489
+ if (!('queryPermission' in fileHandle) ||
490
+ !('requestPermission' in fileHandle))
491
+ return false;
492
+ // FileSystemHandlePermissionDescriptor
493
+ const opts = { mode: withWrite ? 'readwrite' : 'read' };
494
+ // Check if we already have permission, if so, return true.
495
+ if ((yield fileHandle.queryPermission(opts)) === 'granted') {
496
+ return true;
497
+ }
498
+ // Request permission to the file, if the user grants permission, return true.
499
+ if ((yield fileHandle.requestPermission(opts)) === 'granted') {
500
+ return true;
501
+ }
502
+ // The user did not grant permission, return false.
503
+ return false;
504
+ });
505
+ }
506
+ DomUtils.verifyPermission = verifyPermission;
474
507
  })(DomUtils = exports.DomUtils || (exports.DomUtils = {}));
@@ -57,7 +57,7 @@ export declare namespace DomUtils {
57
57
  * @param suggestedName Suggested file name
58
58
  * @param autoDetect Auto detect, false will use link click way
59
59
  */
60
- function downloadFile(data: ReadableStream | Blob, suggestedName?: string, autoDetect?: boolean): Promise<void>;
60
+ function downloadFile(data: ReadableStream | Blob, suggestedName?: string, autoDetect?: boolean): Promise<boolean | undefined>;
61
61
  /**
62
62
  * File to data URL
63
63
  * @param file File
@@ -141,4 +141,12 @@ export declare namespace DomUtils {
141
141
  * @param container Container, limits the element range
142
142
  */
143
143
  function setFocus(name: string | object, container?: HTMLElement): void;
144
+ /**
145
+ * Verify file system permission
146
+ * https://developer.mozilla.org/en-US/docs/Web/API/FileSystemHandle/requestPermission
147
+ * @param fileHandle FileSystemHandle
148
+ * @param withWrite With write permission
149
+ * @returns Result
150
+ */
151
+ function verifyPermission(fileHandle: any, withWrite?: boolean): Promise<boolean>;
144
152
  }
@@ -239,9 +239,12 @@ export var DomUtils;
239
239
  return __awaiter(this, void 0, void 0, function* () {
240
240
  try {
241
241
  if (autoDetect && 'showSaveFilePicker' in globalThis) {
242
+ // AbortError - Use dismisses the window
242
243
  const handle = yield globalThis.showSaveFilePicker({
243
244
  suggestedName
244
245
  });
246
+ if (!(yield verifyPermission(handle, true)))
247
+ return undefined;
245
248
  const stream = yield handle.createWritable();
246
249
  if (data instanceof Blob) {
247
250
  data.stream().pipeTo(stream);
@@ -249,6 +252,7 @@ export var DomUtils;
249
252
  else {
250
253
  yield data.pipeTo(stream);
251
254
  }
255
+ return true;
252
256
  }
253
257
  else {
254
258
  const url = window.URL.createObjectURL(data instanceof Blob
@@ -263,11 +267,13 @@ export var DomUtils;
263
267
  a.click();
264
268
  a.remove();
265
269
  window.URL.revokeObjectURL(url);
270
+ return true;
266
271
  }
267
272
  }
268
273
  catch (e) {
269
274
  console.log(e);
270
275
  }
276
+ return false;
271
277
  });
272
278
  }
273
279
  DomUtils.downloadFile = downloadFile;
@@ -468,4 +474,31 @@ export var DomUtils;
468
474
  element.focus();
469
475
  }
470
476
  DomUtils.setFocus = setFocus;
477
+ /**
478
+ * Verify file system permission
479
+ * https://developer.mozilla.org/en-US/docs/Web/API/FileSystemHandle/requestPermission
480
+ * @param fileHandle FileSystemHandle
481
+ * @param withWrite With write permission
482
+ * @returns Result
483
+ */
484
+ function verifyPermission(fileHandle, withWrite = false) {
485
+ return __awaiter(this, void 0, void 0, function* () {
486
+ if (!('queryPermission' in fileHandle) ||
487
+ !('requestPermission' in fileHandle))
488
+ return false;
489
+ // FileSystemHandlePermissionDescriptor
490
+ const opts = { mode: withWrite ? 'readwrite' : 'read' };
491
+ // Check if we already have permission, if so, return true.
492
+ if ((yield fileHandle.queryPermission(opts)) === 'granted') {
493
+ return true;
494
+ }
495
+ // Request permission to the file, if the user grants permission, return true.
496
+ if ((yield fileHandle.requestPermission(opts)) === 'granted') {
497
+ return true;
498
+ }
499
+ // The user did not grant permission, return false.
500
+ return false;
501
+ });
502
+ }
503
+ DomUtils.verifyPermission = verifyPermission;
471
504
  })(DomUtils || (DomUtils = {}));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@etsoo/shared",
3
- "version": "1.1.86",
3
+ "version": "1.1.88",
4
4
  "description": "TypeScript shared utilities and functions",
5
5
  "main": "lib/cjs/index.js",
6
6
  "module": "lib/mjs/index.js",
@@ -58,7 +58,7 @@
58
58
  "@types/lodash.isequal": "^4.5.6",
59
59
  "jest": "^29.3.1",
60
60
  "jest-environment-jsdom": "^29.3.1",
61
- "ts-jest": "^29.0.3",
61
+ "ts-jest": "^29.0.4",
62
62
  "typescript": "^4.9.4"
63
63
  },
64
64
  "dependencies": {
package/src/DomUtils.ts CHANGED
@@ -284,10 +284,13 @@ export namespace DomUtils {
284
284
  ) {
285
285
  try {
286
286
  if (autoDetect && 'showSaveFilePicker' in globalThis) {
287
+ // AbortError - Use dismisses the window
287
288
  const handle = await (globalThis as any).showSaveFilePicker({
288
289
  suggestedName
289
290
  });
290
291
 
292
+ if (!(await verifyPermission(handle, true))) return undefined;
293
+
291
294
  const stream = await handle.createWritable();
292
295
 
293
296
  if (data instanceof Blob) {
@@ -295,6 +298,8 @@ export namespace DomUtils {
295
298
  } else {
296
299
  await data.pipeTo(stream);
297
300
  }
301
+
302
+ return true;
298
303
  } else {
299
304
  const url = window.URL.createObjectURL(
300
305
  data instanceof Blob
@@ -312,10 +317,14 @@ export namespace DomUtils {
312
317
  a.remove();
313
318
 
314
319
  window.URL.revokeObjectURL(url);
320
+
321
+ return true;
315
322
  }
316
323
  } catch (e) {
317
324
  console.log(e);
318
325
  }
326
+
327
+ return false;
319
328
  }
320
329
 
321
330
  /**
@@ -541,4 +550,38 @@ export namespace DomUtils {
541
550
 
542
551
  if (element != null) element.focus();
543
552
  }
553
+
554
+ /**
555
+ * Verify file system permission
556
+ * https://developer.mozilla.org/en-US/docs/Web/API/FileSystemHandle/requestPermission
557
+ * @param fileHandle FileSystemHandle
558
+ * @param withWrite With write permission
559
+ * @returns Result
560
+ */
561
+ export async function verifyPermission(
562
+ fileHandle: any,
563
+ withWrite: boolean = false
564
+ ) {
565
+ if (
566
+ !('queryPermission' in fileHandle) ||
567
+ !('requestPermission' in fileHandle)
568
+ )
569
+ return false;
570
+
571
+ // FileSystemHandlePermissionDescriptor
572
+ const opts = { mode: withWrite ? 'readwrite' : 'read' };
573
+
574
+ // Check if we already have permission, if so, return true.
575
+ if ((await fileHandle.queryPermission(opts)) === 'granted') {
576
+ return true;
577
+ }
578
+
579
+ // Request permission to the file, if the user grants permission, return true.
580
+ if ((await fileHandle.requestPermission(opts)) === 'granted') {
581
+ return true;
582
+ }
583
+
584
+ // The user did not grant permission, return false.
585
+ return false;
586
+ }
544
587
  }