@fgv/ts-web-extras 5.0.1-1 → 5.0.1-10
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/.rush/temp/chunked-rush-logs/ts-web-extras.build.chunks.jsonl +12 -10
- package/.rush/temp/operation/build/all.log +12 -10
- package/.rush/temp/operation/build/log-chunks.jsonl +12 -10
- package/.rush/temp/operation/build/state.json +1 -1
- package/.rush/temp/shrinkwrap-deps.json +129 -130
- package/config/jest.config.json +1 -1
- package/config/rig.json +1 -1
- package/dist/index.browser.js +24 -0
- package/dist/index.browser.js.map +1 -0
- package/dist/index.js +42 -0
- package/dist/index.js.map +1 -0
- package/dist/packlets/crypto/browserHashProvider.js +66 -0
- package/dist/packlets/crypto/browserHashProvider.js.map +1 -0
- package/dist/packlets/crypto/index.js +27 -0
- package/dist/packlets/crypto/index.js.map +1 -0
- package/dist/packlets/file-api-types/index.js +156 -0
- package/dist/packlets/file-api-types/index.js.map +1 -0
- package/dist/packlets/file-tree/fileApiTreeAccessors.js +330 -0
- package/dist/packlets/file-tree/fileApiTreeAccessors.js.map +1 -0
- package/dist/packlets/file-tree/index.js +27 -0
- package/dist/packlets/file-tree/index.js.map +1 -0
- package/dist/packlets/helpers/fileTreeHelpers.js +94 -0
- package/dist/packlets/helpers/fileTreeHelpers.js.map +1 -0
- package/dist/packlets/helpers/index.js +27 -0
- package/dist/packlets/helpers/index.js.map +1 -0
- package/dist/packlets/url-utils/index.js +27 -0
- package/dist/packlets/url-utils/index.js.map +1 -0
- package/dist/packlets/url-utils/urlParams.js +157 -0
- package/dist/packlets/url-utils/urlParams.js.map +1 -0
- package/dist/test/setupTests.js +74 -0
- package/dist/test/setupTests.js.map +1 -0
- package/dist/test/unit/browserHashProvider.test.js +140 -0
- package/dist/test/unit/browserHashProvider.test.js.map +1 -0
- package/dist/test/unit/fileApiTreeAccessors.test.js +1137 -0
- package/dist/test/unit/fileApiTreeAccessors.test.js.map +1 -0
- package/dist/test/unit/fileApiTypes.test.js +442 -0
- package/dist/test/unit/fileApiTypes.test.js.map +1 -0
- package/dist/test/unit/fileTreeHelpers.test.js +590 -0
- package/dist/test/unit/fileTreeHelpers.test.js.map +1 -0
- package/dist/test/unit/urlParams.test.js +393 -0
- package/dist/test/unit/urlParams.test.js.map +1 -0
- package/dist/test/utils/testHelpers.js +124 -0
- package/dist/test/utils/testHelpers.js.map +1 -0
- package/dist/tsdoc-metadata.json +1 -1
- package/lib/index.browser.d.ts +2 -0
- package/lib/index.browser.d.ts.map +1 -0
- package/lib/index.browser.js +40 -0
- package/lib/index.browser.js.map +1 -0
- package/package.json +37 -22
- package/rush-logs/ts-web-extras.build.cache.log +1 -3
- package/rush-logs/ts-web-extras.build.log +12 -10
- package/src/index.browser.ts +24 -0
- package/temp/build/typescript/ts_vnCx6LlY.json +1 -0
- package/temp/coverage/crypto/browserHashProvider.ts.html +1 -1
- package/temp/coverage/crypto/index.html +1 -1
- package/temp/coverage/file-tree/fileApiTreeAccessors.ts.html +1 -1
- package/temp/coverage/file-tree/index.html +1 -1
- package/temp/coverage/helpers/fileTreeHelpers.ts.html +1 -1
- package/temp/coverage/helpers/index.html +1 -1
- package/temp/coverage/index.html +1 -1
- package/temp/coverage/lcov-report/crypto/browserHashProvider.ts.html +1 -1
- package/temp/coverage/lcov-report/crypto/index.html +1 -1
- package/temp/coverage/lcov-report/file-tree/fileApiTreeAccessors.ts.html +1 -1
- package/temp/coverage/lcov-report/file-tree/index.html +1 -1
- package/temp/coverage/lcov-report/helpers/fileTreeHelpers.ts.html +1 -1
- package/temp/coverage/lcov-report/helpers/index.html +1 -1
- package/temp/coverage/lcov-report/index.html +1 -1
- package/temp/coverage/lcov-report/url-utils/index.html +1 -1
- package/temp/coverage/lcov-report/url-utils/urlParams.ts.html +1 -1
- package/temp/coverage/url-utils/index.html +1 -1
- package/temp/coverage/url-utils/urlParams.ts.html +1 -1
- package/temp/test/jest/haste-map-7492f1b44480e0cdd1f220078fb3afd8-c8dd6c3430605adeb2f1cadf4f75e791-8c9336785555d572065b28c111982ba4 +0 -0
- package/temp/test/jest/perf-cache-7492f1b44480e0cdd1f220078fb3afd8-da39a3ee5e6b4b0d3255bfef95601890 +1 -1
- package/temp/ts-web-extras.api.json +1 -1
- package/.rush/temp/451cb8882bab8e836993986c9bcd3d92edea57c2.tar.log +0 -121
- package/temp/build/typescript/ts_l9Fw4VUO.json +0 -1
- /package/temp/test/jest/jest-transform-cache-7492f1b44480e0cdd1f220078fb3afd8-79ef2876fae7ca75eedb2aa53dc48338/{63/package_63a8257b0e4d0e7ff33f927d75f27a75 → 0e/package_0eb6535f5987849d93ea51ef33a14cf6} +0 -0
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2025 Erik Fortune
|
|
3
|
+
*
|
|
4
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
6
|
+
* in the Software without restriction, including without limitation the rights
|
|
7
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
9
|
+
* furnished to do so, subject to the following conditions:
|
|
10
|
+
*
|
|
11
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
12
|
+
* copies or substantial portions of the Software.
|
|
13
|
+
*
|
|
14
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
18
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
19
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
20
|
+
* SOFTWARE.
|
|
21
|
+
*/
|
|
22
|
+
// Browser entry point - re-exports everything from main index
|
|
23
|
+
export * from './index';
|
|
24
|
+
//# sourceMappingURL=index.browser.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.browser.js","sourceRoot":"","sources":["../src/index.browser.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AAEH,8DAA8D;AAC9D,cAAc,SAAS,CAAC","sourcesContent":["/*\n * Copyright (c) 2025 Erik Fortune\n *\n * Permission is hereby granted, free of charge, to any person obtaining a copy\n * of this software and associated documentation files (the \"Software\"), to deal\n * in the Software without restriction, including without limitation the rights\n * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n * copies of the Software, and to permit persons to whom the Software is\n * furnished to do so, subject to the following conditions:\n *\n * The above copyright notice and this permission notice shall be included in all\n * copies or substantial portions of the Software.\n *\n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n * SOFTWARE.\n */\n\n// Browser entry point - re-exports everything from main index\nexport * from './index';\n"]}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2025 Erik Fortune
|
|
3
|
+
*
|
|
4
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
6
|
+
* in the Software without restriction, including without limitation the rights
|
|
7
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
9
|
+
* furnished to do so, subject to the following conditions:
|
|
10
|
+
*
|
|
11
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
12
|
+
* copies or substantial portions of the Software.
|
|
13
|
+
*
|
|
14
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
18
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
19
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
20
|
+
* SOFTWARE.
|
|
21
|
+
*/
|
|
22
|
+
/**
|
|
23
|
+
* Browser-compatible utilities and FileTree implementations.
|
|
24
|
+
*
|
|
25
|
+
* This library provides browser-compatible alternatives to Node.js-specific functionality,
|
|
26
|
+
* including Web Crypto API-based hashing, File API-based file tree implementations,
|
|
27
|
+
* and URL parameter parsing utilities.
|
|
28
|
+
* All exports are designed to be tree-shakeable for optimal bundle size.
|
|
29
|
+
*
|
|
30
|
+
* @packageDocumentation
|
|
31
|
+
*/
|
|
32
|
+
// Export crypto functionality
|
|
33
|
+
export * from './packlets/crypto';
|
|
34
|
+
// Export file tree functionality
|
|
35
|
+
export * from './packlets/file-tree';
|
|
36
|
+
// Export helper functions
|
|
37
|
+
export * from './packlets/helpers';
|
|
38
|
+
// Export File System Access API types
|
|
39
|
+
export * from './packlets/file-api-types';
|
|
40
|
+
// Export URL utilities
|
|
41
|
+
export * from './packlets/url-utils';
|
|
42
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AAEH;;;;;;;;;GASG;AAEH,8BAA8B;AAC9B,cAAc,mBAAmB,CAAC;AAElC,iCAAiC;AACjC,cAAc,sBAAsB,CAAC;AAErC,0BAA0B;AAC1B,cAAc,oBAAoB,CAAC;AAEnC,sCAAsC;AACtC,cAAc,2BAA2B,CAAC;AAE1C,uBAAuB;AACvB,cAAc,sBAAsB,CAAC","sourcesContent":["/*\n * Copyright (c) 2025 Erik Fortune\n *\n * Permission is hereby granted, free of charge, to any person obtaining a copy\n * of this software and associated documentation files (the \"Software\"), to deal\n * in the Software without restriction, including without limitation the rights\n * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n * copies of the Software, and to permit persons to whom the Software is\n * furnished to do so, subject to the following conditions:\n *\n * The above copyright notice and this permission notice shall be included in all\n * copies or substantial portions of the Software.\n *\n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n * SOFTWARE.\n */\n\n/**\n * Browser-compatible utilities and FileTree implementations.\n *\n * This library provides browser-compatible alternatives to Node.js-specific functionality,\n * including Web Crypto API-based hashing, File API-based file tree implementations,\n * and URL parameter parsing utilities.\n * All exports are designed to be tree-shakeable for optimal bundle size.\n *\n * @packageDocumentation\n */\n\n// Export crypto functionality\nexport * from './packlets/crypto';\n\n// Export file tree functionality\nexport * from './packlets/file-tree';\n\n// Export helper functions\nexport * from './packlets/helpers';\n\n// Export File System Access API types\nexport * from './packlets/file-api-types';\n\n// Export URL utilities\nexport * from './packlets/url-utils';\n"]}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2025 Erik Fortune
|
|
3
|
+
*
|
|
4
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
6
|
+
* in the Software without restriction, including without limitation the rights
|
|
7
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
9
|
+
* furnished to do so, subject to the following conditions:
|
|
10
|
+
*
|
|
11
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
12
|
+
* copies or substantial portions of the Software.
|
|
13
|
+
*
|
|
14
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
18
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
19
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
20
|
+
* SOFTWARE.
|
|
21
|
+
*/
|
|
22
|
+
import { succeed, fail } from '@fgv/ts-utils';
|
|
23
|
+
/**
|
|
24
|
+
* Browser-compatible hash provider using the Web Crypto API.
|
|
25
|
+
* Supports common hash algorithms available in browsers.
|
|
26
|
+
* @public
|
|
27
|
+
*/
|
|
28
|
+
export class BrowserHashProvider {
|
|
29
|
+
/**
|
|
30
|
+
* Hash a string using the specified algorithm.
|
|
31
|
+
* @param data - The string to hash
|
|
32
|
+
* @param algorithm - The hash algorithm to use
|
|
33
|
+
* @returns Promise resolving to the hex-encoded hash
|
|
34
|
+
*/
|
|
35
|
+
static async hashString(data, algorithm = 'SHA-256') {
|
|
36
|
+
try {
|
|
37
|
+
/* c8 ignore next 3 - defense in depth */
|
|
38
|
+
if (!crypto.subtle) {
|
|
39
|
+
return fail('Web Crypto API not available in this environment');
|
|
40
|
+
}
|
|
41
|
+
const encoder = new TextEncoder();
|
|
42
|
+
const dataBuffer = encoder.encode(data);
|
|
43
|
+
const hashBuffer = await crypto.subtle.digest(algorithm, dataBuffer);
|
|
44
|
+
const hashArray = new Uint8Array(hashBuffer);
|
|
45
|
+
const hashHex = Array.from(hashArray)
|
|
46
|
+
.map((b) => b.toString(16).padStart(2, '0'))
|
|
47
|
+
.join('');
|
|
48
|
+
return succeed(hashHex);
|
|
49
|
+
}
|
|
50
|
+
catch (error) {
|
|
51
|
+
return fail(`Hash computation failed: ${error instanceof Error ? error.message : String(error)}`);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Hash multiple strings concatenated with a separator.
|
|
56
|
+
* @param parts - Array of strings to concatenate and hash
|
|
57
|
+
* @param algorithm - The hash algorithm to use
|
|
58
|
+
* @param separator - Separator to use between parts (default: '|')
|
|
59
|
+
* @returns Promise resolving to the hex-encoded hash
|
|
60
|
+
*/
|
|
61
|
+
static async hashParts(parts, algorithm = 'SHA-256', separator = '|') {
|
|
62
|
+
const combined = parts.join(separator);
|
|
63
|
+
return this.hashString(combined, algorithm);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
//# sourceMappingURL=browserHashProvider.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"browserHashProvider.js","sourceRoot":"","sources":["../../../src/packlets/crypto/browserHashProvider.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AAEH,OAAO,EAAU,OAAO,EAAE,IAAI,EAAE,MAAM,eAAe,CAAC;AAEtD;;;;GAIG;AACH,MAAM,OAAO,mBAAmB;IAC9B;;;;;OAKG;IACI,MAAM,CAAC,KAAK,CAAC,UAAU,CAAC,IAAY,EAAE,YAAoB,SAAS;QACxE,IAAI,CAAC;YACH,yCAAyC;YACzC,IAAI,CAAC,MAAM,CAAC,MAAM,EAAE,CAAC;gBACnB,OAAO,IAAI,CAAC,kDAAkD,CAAC,CAAC;YAClE,CAAC;YAED,MAAM,OAAO,GAAG,IAAI,WAAW,EAAE,CAAC;YAClC,MAAM,UAAU,GAAG,OAAO,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;YACxC,MAAM,UAAU,GAAG,MAAM,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,SAAS,EAAE,UAAU,CAAC,CAAC;YACrE,MAAM,SAAS,GAAG,IAAI,UAAU,CAAC,UAAU,CAAC,CAAC;YAC7C,MAAM,OAAO,GAAG,KAAK,CAAC,IAAI,CAAC,SAAS,CAAC;iBAClC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC,QAAQ,CAAC,CAAC,EAAE,GAAG,CAAC,CAAC;iBAC3C,IAAI,CAAC,EAAE,CAAC,CAAC;YAEZ,OAAO,OAAO,CAAC,OAAO,CAAC,CAAC;QAC1B,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,OAAO,IAAI,CAAC,4BAA4B,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC;QACpG,CAAC;IACH,CAAC;IAED;;;;;;OAMG;IACI,MAAM,CAAC,KAAK,CAAC,SAAS,CAC3B,KAAe,EACf,YAAoB,SAAS,EAC7B,YAAoB,GAAG;QAEvB,MAAM,QAAQ,GAAG,KAAK,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;QACvC,OAAO,IAAI,CAAC,UAAU,CAAC,QAAQ,EAAE,SAAS,CAAC,CAAC;IAC9C,CAAC;CACF","sourcesContent":["/*\n * Copyright (c) 2025 Erik Fortune\n *\n * Permission is hereby granted, free of charge, to any person obtaining a copy\n * of this software and associated documentation files (the \"Software\"), to deal\n * in the Software without restriction, including without limitation the rights\n * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n * copies of the Software, and to permit persons to whom the Software is\n * furnished to do so, subject to the following conditions:\n *\n * The above copyright notice and this permission notice shall be included in all\n * copies or substantial portions of the Software.\n *\n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n * SOFTWARE.\n */\n\nimport { Result, succeed, fail } from '@fgv/ts-utils';\n\n/**\n * Browser-compatible hash provider using the Web Crypto API.\n * Supports common hash algorithms available in browsers.\n * @public\n */\nexport class BrowserHashProvider {\n /**\n * Hash a string using the specified algorithm.\n * @param data - The string to hash\n * @param algorithm - The hash algorithm to use\n * @returns Promise resolving to the hex-encoded hash\n */\n public static async hashString(data: string, algorithm: string = 'SHA-256'): Promise<Result<string>> {\n try {\n /* c8 ignore next 3 - defense in depth */\n if (!crypto.subtle) {\n return fail('Web Crypto API not available in this environment');\n }\n\n const encoder = new TextEncoder();\n const dataBuffer = encoder.encode(data);\n const hashBuffer = await crypto.subtle.digest(algorithm, dataBuffer);\n const hashArray = new Uint8Array(hashBuffer);\n const hashHex = Array.from(hashArray)\n .map((b) => b.toString(16).padStart(2, '0'))\n .join('');\n\n return succeed(hashHex);\n } catch (error) {\n return fail(`Hash computation failed: ${error instanceof Error ? error.message : String(error)}`);\n }\n }\n\n /**\n * Hash multiple strings concatenated with a separator.\n * @param parts - Array of strings to concatenate and hash\n * @param algorithm - The hash algorithm to use\n * @param separator - Separator to use between parts (default: '|')\n * @returns Promise resolving to the hex-encoded hash\n */\n public static async hashParts(\n parts: string[],\n algorithm: string = 'SHA-256',\n separator: string = '|'\n ): Promise<Result<string>> {\n const combined = parts.join(separator);\n return this.hashString(combined, algorithm);\n }\n}\n"]}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2025 Erik Fortune
|
|
3
|
+
*
|
|
4
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
6
|
+
* in the Software without restriction, including without limitation the rights
|
|
7
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
9
|
+
* furnished to do so, subject to the following conditions:
|
|
10
|
+
*
|
|
11
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
12
|
+
* copies or substantial portions of the Software.
|
|
13
|
+
*
|
|
14
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
18
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
19
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
20
|
+
* SOFTWARE.
|
|
21
|
+
*/
|
|
22
|
+
/**
|
|
23
|
+
* Browser-compatible cryptographic utilities using the Web Crypto API.
|
|
24
|
+
* @packageDocumentation
|
|
25
|
+
*/
|
|
26
|
+
export * from './browserHashProvider';
|
|
27
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/packlets/crypto/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AAEH;;;GAGG;AAEH,cAAc,uBAAuB,CAAC","sourcesContent":["/*\n * Copyright (c) 2025 Erik Fortune\n *\n * Permission is hereby granted, free of charge, to any person obtaining a copy\n * of this software and associated documentation files (the \"Software\"), to deal\n * in the Software without restriction, including without limitation the rights\n * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n * copies of the Software, and to permit persons to whom the Software is\n * furnished to do so, subject to the following conditions:\n *\n * The above copyright notice and this permission notice shall be included in all\n * copies or substantial portions of the Software.\n *\n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n * SOFTWARE.\n */\n\n/**\n * Browser-compatible cryptographic utilities using the Web Crypto API.\n * @packageDocumentation\n */\n\nexport * from './browserHashProvider';\n"]}
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2025 Erik Fortune
|
|
3
|
+
*
|
|
4
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
6
|
+
* in the Software without restriction, including without limitation the rights
|
|
7
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
9
|
+
* furnished to do so, subject to the following conditions:
|
|
10
|
+
*
|
|
11
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
12
|
+
* copies or substantial portions of the Software.
|
|
13
|
+
*
|
|
14
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
18
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
19
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
20
|
+
* SOFTWARE.
|
|
21
|
+
*/
|
|
22
|
+
/**
|
|
23
|
+
* Type guard to check if the browser supports the File System Access API
|
|
24
|
+
* @param window - The window object to check
|
|
25
|
+
* @returns True if the window supports File System Access API
|
|
26
|
+
* @public
|
|
27
|
+
*/
|
|
28
|
+
export function supportsFileSystemAccess(window) {
|
|
29
|
+
return 'showOpenFilePicker' in window && 'showSaveFilePicker' in window && 'showDirectoryPicker' in window;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Type guard to check if a FileSystemHandle is a file handle
|
|
33
|
+
* @param handle - The handle to check
|
|
34
|
+
* @returns True if the handle is a FileSystemFileHandle
|
|
35
|
+
* @public
|
|
36
|
+
*/
|
|
37
|
+
export function isFileHandle(handle) {
|
|
38
|
+
return handle.kind === 'file';
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Type guard to check if a FileSystemHandle is a directory handle
|
|
42
|
+
* @param handle - The handle to check
|
|
43
|
+
* @returns True if the handle is a FileSystemDirectoryHandle
|
|
44
|
+
* @public
|
|
45
|
+
*/
|
|
46
|
+
export function isDirectoryHandle(handle) {
|
|
47
|
+
return handle.kind === 'directory';
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Safely access showOpenFilePicker with proper type checking
|
|
51
|
+
* @param window - The window object
|
|
52
|
+
* @param options - Options for the file picker
|
|
53
|
+
* @returns Promise with file handles or null if not supported
|
|
54
|
+
* @public
|
|
55
|
+
*/
|
|
56
|
+
export async function safeShowOpenFilePicker(window, options) {
|
|
57
|
+
if (supportsFileSystemAccess(window)) {
|
|
58
|
+
return window.showOpenFilePicker(options);
|
|
59
|
+
}
|
|
60
|
+
return null;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Safely access showSaveFilePicker with proper type checking
|
|
64
|
+
* @param window - The window object
|
|
65
|
+
* @param options - Options for the file picker
|
|
66
|
+
* @returns Promise with file handle or null if not supported
|
|
67
|
+
* @public
|
|
68
|
+
*/
|
|
69
|
+
export async function safeShowSaveFilePicker(window, options) {
|
|
70
|
+
if (supportsFileSystemAccess(window)) {
|
|
71
|
+
return window.showSaveFilePicker(options);
|
|
72
|
+
}
|
|
73
|
+
return null;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Safely access showDirectoryPicker with proper type checking
|
|
77
|
+
* @param window - The window object
|
|
78
|
+
* @param options - Options for the directory picker
|
|
79
|
+
* @returns Promise with directory handle or null if not supported
|
|
80
|
+
* @public
|
|
81
|
+
*/
|
|
82
|
+
export async function safeShowDirectoryPicker(window, options) {
|
|
83
|
+
if (supportsFileSystemAccess(window)) {
|
|
84
|
+
return window.showDirectoryPicker(options);
|
|
85
|
+
}
|
|
86
|
+
return null;
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Export data as JSON file using legacy blob download method.
|
|
90
|
+
* Creates a temporary download link and triggers file download.
|
|
91
|
+
* @param data - Data to export as JSON
|
|
92
|
+
* @param filename - Name for the downloaded file
|
|
93
|
+
* @public
|
|
94
|
+
*/
|
|
95
|
+
export function exportAsJson(data, filename) {
|
|
96
|
+
const json = JSON.stringify(data, null, 2);
|
|
97
|
+
const blob = new Blob([json], { type: 'application/json' });
|
|
98
|
+
const url = URL.createObjectURL(blob);
|
|
99
|
+
const a = document.createElement('a');
|
|
100
|
+
a.href = url;
|
|
101
|
+
a.download = filename;
|
|
102
|
+
document.body.appendChild(a);
|
|
103
|
+
a.click();
|
|
104
|
+
document.body.removeChild(a);
|
|
105
|
+
URL.revokeObjectURL(url);
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Export data using File System Access API with fallback to blob download.
|
|
109
|
+
* @param data - Data to export as JSON
|
|
110
|
+
* @param suggestedName - Suggested filename for the save dialog
|
|
111
|
+
* @param description - Description for file type filter (default: 'JSON files')
|
|
112
|
+
* @param window - Window object for API access (default: globalThis.window)
|
|
113
|
+
* @returns Promise resolving to true if saved via File System Access API, false if fallback used
|
|
114
|
+
* @public
|
|
115
|
+
*/
|
|
116
|
+
export async function exportUsingFileSystemAPI(data, suggestedName, description = 'JSON files', window = globalThis.window) {
|
|
117
|
+
if (!supportsFileSystemAccess(window)) {
|
|
118
|
+
// Fallback to blob download
|
|
119
|
+
exportAsJson(data, suggestedName);
|
|
120
|
+
return false;
|
|
121
|
+
}
|
|
122
|
+
try {
|
|
123
|
+
const fileHandle = await safeShowSaveFilePicker(window, {
|
|
124
|
+
suggestedName,
|
|
125
|
+
types: [
|
|
126
|
+
{
|
|
127
|
+
description,
|
|
128
|
+
accept: {
|
|
129
|
+
'application/json': ['.json']
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
]
|
|
133
|
+
});
|
|
134
|
+
if (!fileHandle) {
|
|
135
|
+
// User cancelled - fallback to blob download
|
|
136
|
+
exportAsJson(data, suggestedName);
|
|
137
|
+
return false;
|
|
138
|
+
}
|
|
139
|
+
const json = JSON.stringify(data, null, 2);
|
|
140
|
+
const writable = await fileHandle.createWritable();
|
|
141
|
+
await writable.write(json);
|
|
142
|
+
await writable.close();
|
|
143
|
+
return true;
|
|
144
|
+
}
|
|
145
|
+
catch (error) {
|
|
146
|
+
// Handle errors by falling back to blob download
|
|
147
|
+
if (error.name === 'AbortError') {
|
|
148
|
+
// User cancelled - fallback
|
|
149
|
+
exportAsJson(data, suggestedName);
|
|
150
|
+
return false;
|
|
151
|
+
}
|
|
152
|
+
// Other errors - re-throw as they indicate a real problem
|
|
153
|
+
throw error;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/packlets/file-api-types/index.ts"],"names":[],"mappings":"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","sourcesContent":["/*\n * Copyright (c) 2025 Erik Fortune\n *\n * Permission is hereby granted, free of charge, to any person obtaining a copy\n * of this software and associated documentation files (the \"Software\"), to deal\n * in the Software without restriction, including without limitation the rights\n * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n * copies of the Software, and to permit persons to whom the Software is\n * furnished to do so, subject to the following conditions:\n *\n * The above copyright notice and this permission notice shall be included in all\n * copies or substantial portions of the Software.\n *\n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n * SOFTWARE.\n */\n\n/**\n * File System Access API type definitions and utilities for browser compatibility.\n * @packageDocumentation\n */\n\n// Local type definitions for File System Access API\n// Based on https://wicg.github.io/file-system-access/\n\n/**\n * File System Access API methods available on Window\n * @public\n */\nexport interface IFsAccessApis {\n showDirectoryPicker(options?: ShowDirectoryPickerOptions): Promise<FileSystemDirectoryHandle>;\n showOpenFilePicker(options?: ShowOpenFilePickerOptions): Promise<FileSystemFileHandle[]>;\n showSaveFilePicker(options?: ShowSaveFilePickerOptions): Promise<FileSystemFileHandle>;\n}\n\n/**\n * Window interface extended with File System Access API\n * @public\n */\nexport type WindowWithFsAccess = Window & IFsAccessApis;\n\n/**\n * Base interface for file system handles\n * @public\n */\nexport interface FileSystemHandle {\n readonly kind: 'file' | 'directory';\n readonly name: string;\n isSameEntry(other: FileSystemHandle): Promise<boolean>;\n queryPermission(descriptor?: FileSystemHandlePermissionDescriptor): Promise<PermissionState>;\n requestPermission(descriptor?: FileSystemHandlePermissionDescriptor): Promise<PermissionState>;\n}\n\n/**\n * File handle interface\n * @public\n */\nexport interface FileSystemFileHandle extends FileSystemHandle {\n readonly kind: 'file';\n getFile(): Promise<File>;\n createWritable(options?: FileSystemCreateWritableOptions): Promise<FileSystemWritableFileStream>;\n}\n\n/**\n * Directory handle interface\n * @public\n */\nexport interface FileSystemDirectoryHandle extends FileSystemHandle {\n readonly kind: 'directory';\n getDirectoryHandle(\n name: string,\n options?: FileSystemGetDirectoryOptions\n ): Promise<FileSystemDirectoryHandle>;\n getFileHandle(name: string, options?: FileSystemGetFileOptions): Promise<FileSystemFileHandle>;\n removeEntry(name: string, options?: FileSystemRemoveOptions): Promise<void>;\n resolve(possibleDescendant: FileSystemHandle): Promise<string[] | null>;\n keys(): AsyncIterableIterator<string>;\n values(): AsyncIterableIterator<FileSystemHandle>;\n entries(): AsyncIterableIterator<[string, FileSystemHandle]>;\n [Symbol.asyncIterator](): AsyncIterableIterator<[string, FileSystemHandle]>;\n}\n\n/**\n * Permission descriptor for file system handles\n * @public\n */\nexport interface FileSystemHandlePermissionDescriptor {\n mode?: 'read' | 'readwrite';\n}\n\n/**\n * Options for creating writable file streams\n * @public\n */\nexport interface FileSystemCreateWritableOptions {\n keepExistingData?: boolean;\n}\n\n/**\n * Options for getting directory handles\n * @public\n */\nexport interface FileSystemGetDirectoryOptions {\n create?: boolean;\n}\n\n/**\n * Options for getting file handles\n * @public\n */\nexport interface FileSystemGetFileOptions {\n create?: boolean;\n}\n\n/**\n * Options for removing entries\n * @public\n */\nexport interface FileSystemRemoveOptions {\n recursive?: boolean;\n}\n\n/**\n * Writable file stream interface\n * @public\n */\nexport interface FileSystemWritableFileStream extends WritableStream {\n write(data: BufferSource | Blob | string): Promise<void>;\n seek(position: number): Promise<void>;\n truncate(size: number): Promise<void>;\n}\n\n/**\n * Directory picker options\n * @public\n */\nexport interface ShowDirectoryPickerOptions {\n id?: string;\n mode?: 'read' | 'readwrite';\n startIn?: FileSystemHandle | WellKnownDirectory;\n}\n\n/**\n * File picker options\n * @public\n */\nexport interface ShowOpenFilePickerOptions {\n multiple?: boolean;\n excludeAcceptAllOption?: boolean;\n id?: string;\n startIn?: FileSystemHandle | WellKnownDirectory;\n types?: FilePickerAcceptType[];\n}\n\n/**\n * Save file picker options\n * @public\n */\nexport interface ShowSaveFilePickerOptions {\n excludeAcceptAllOption?: boolean;\n id?: string;\n startIn?: FileSystemHandle | WellKnownDirectory;\n suggestedName?: string;\n types?: FilePickerAcceptType[];\n}\n\n/**\n * File picker accept type\n * @public\n */\nexport interface FilePickerAcceptType {\n description?: string;\n accept: Record<string, string | string[]>;\n}\n\n/**\n * Well-known directory type\n * @public\n */\nexport type WellKnownDirectory = 'desktop' | 'documents' | 'downloads' | 'music' | 'pictures' | 'videos';\n\n/**\n * Type guard to check if the browser supports the File System Access API\n * @param window - The window object to check\n * @returns True if the window supports File System Access API\n * @public\n */\nexport function supportsFileSystemAccess(window: Window): window is WindowWithFsAccess {\n return 'showOpenFilePicker' in window && 'showSaveFilePicker' in window && 'showDirectoryPicker' in window;\n}\n\n/**\n * Type guard to check if a FileSystemHandle is a file handle\n * @param handle - The handle to check\n * @returns True if the handle is a FileSystemFileHandle\n * @public\n */\nexport function isFileHandle(handle: FileSystemHandle): handle is FileSystemFileHandle {\n return handle.kind === 'file';\n}\n\n/**\n * Type guard to check if a FileSystemHandle is a directory handle\n * @param handle - The handle to check\n * @returns True if the handle is a FileSystemDirectoryHandle\n * @public\n */\nexport function isDirectoryHandle(handle: FileSystemHandle): handle is FileSystemDirectoryHandle {\n return handle.kind === 'directory';\n}\n\n/**\n * Safely access showOpenFilePicker with proper type checking\n * @param window - The window object\n * @param options - Options for the file picker\n * @returns Promise with file handles or null if not supported\n * @public\n */\nexport async function safeShowOpenFilePicker(\n window: Window,\n options?: ShowOpenFilePickerOptions\n): Promise<FileSystemFileHandle[] | null> {\n if (supportsFileSystemAccess(window)) {\n return window.showOpenFilePicker(options);\n }\n return null;\n}\n\n/**\n * Safely access showSaveFilePicker with proper type checking\n * @param window - The window object\n * @param options - Options for the file picker\n * @returns Promise with file handle or null if not supported\n * @public\n */\nexport async function safeShowSaveFilePicker(\n window: Window,\n options?: ShowSaveFilePickerOptions\n): Promise<FileSystemFileHandle | null> {\n if (supportsFileSystemAccess(window)) {\n return window.showSaveFilePicker(options);\n }\n return null;\n}\n\n/**\n * Safely access showDirectoryPicker with proper type checking\n * @param window - The window object\n * @param options - Options for the directory picker\n * @returns Promise with directory handle or null if not supported\n * @public\n */\nexport async function safeShowDirectoryPicker(\n window: Window,\n options?: ShowDirectoryPickerOptions\n): Promise<FileSystemDirectoryHandle | null> {\n if (supportsFileSystemAccess(window)) {\n return window.showDirectoryPicker(options);\n }\n return null;\n}\n\n/**\n * Export data as JSON file using legacy blob download method.\n * Creates a temporary download link and triggers file download.\n * @param data - Data to export as JSON\n * @param filename - Name for the downloaded file\n * @public\n */\nexport function exportAsJson(data: unknown, filename: string): void {\n const json = JSON.stringify(data, null, 2);\n const blob = new Blob([json], { type: 'application/json' });\n const url = URL.createObjectURL(blob);\n\n const a = document.createElement('a');\n a.href = url;\n a.download = filename;\n document.body.appendChild(a);\n a.click();\n document.body.removeChild(a);\n URL.revokeObjectURL(url);\n}\n\n/**\n * Export data using File System Access API with fallback to blob download.\n * @param data - Data to export as JSON\n * @param suggestedName - Suggested filename for the save dialog\n * @param description - Description for file type filter (default: 'JSON files')\n * @param window - Window object for API access (default: globalThis.window)\n * @returns Promise resolving to true if saved via File System Access API, false if fallback used\n * @public\n */\nexport async function exportUsingFileSystemAPI(\n data: unknown,\n suggestedName: string,\n description: string = 'JSON files',\n window: Window = globalThis.window\n): Promise<boolean> {\n if (!supportsFileSystemAccess(window)) {\n // Fallback to blob download\n exportAsJson(data, suggestedName);\n return false;\n }\n\n try {\n const fileHandle = await safeShowSaveFilePicker(window, {\n suggestedName,\n types: [\n {\n description,\n accept: {\n 'application/json': ['.json']\n }\n }\n ]\n });\n\n if (!fileHandle) {\n // User cancelled - fallback to blob download\n exportAsJson(data, suggestedName);\n return false;\n }\n\n const json = JSON.stringify(data, null, 2);\n const writable = await fileHandle.createWritable();\n await writable.write(json);\n await writable.close();\n\n return true;\n } catch (error) {\n // Handle errors by falling back to blob download\n if ((error as Error).name === 'AbortError') {\n // User cancelled - fallback\n exportAsJson(data, suggestedName);\n return false;\n }\n // Other errors - re-throw as they indicate a real problem\n throw error;\n }\n}\n"]}
|