@fluid-internal/client-utils 2.0.0-dev.6.4.0.191258

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.
Files changed (149) hide show
  1. package/.eslintrc.js +16 -0
  2. package/.mocharc.js +12 -0
  3. package/CHANGELOG.md +3 -0
  4. package/LICENSE +21 -0
  5. package/README.md +70 -0
  6. package/api-extractor.json +16 -0
  7. package/dist/base64Encoding.d.ts +28 -0
  8. package/dist/base64Encoding.d.ts.map +1 -0
  9. package/dist/base64Encoding.js +43 -0
  10. package/dist/base64Encoding.js.map +1 -0
  11. package/dist/bufferBrowser.d.ts +84 -0
  12. package/dist/bufferBrowser.d.ts.map +1 -0
  13. package/dist/bufferBrowser.js +201 -0
  14. package/dist/bufferBrowser.js.map +1 -0
  15. package/dist/bufferNode.d.ts +62 -0
  16. package/dist/bufferNode.d.ts.map +1 -0
  17. package/dist/bufferNode.js +54 -0
  18. package/dist/bufferNode.js.map +1 -0
  19. package/dist/bufferShared.d.ts +12 -0
  20. package/dist/bufferShared.d.ts.map +1 -0
  21. package/dist/bufferShared.js +21 -0
  22. package/dist/bufferShared.js.map +1 -0
  23. package/dist/eventForwarder.d.ts +33 -0
  24. package/dist/eventForwarder.d.ts.map +1 -0
  25. package/dist/eventForwarder.js +98 -0
  26. package/dist/eventForwarder.js.map +1 -0
  27. package/dist/hashFileBrowser.d.ts +30 -0
  28. package/dist/hashFileBrowser.d.ts.map +1 -0
  29. package/dist/hashFileBrowser.js +97 -0
  30. package/dist/hashFileBrowser.js.map +1 -0
  31. package/dist/hashFileNode.d.ts +30 -0
  32. package/dist/hashFileNode.d.ts.map +1 -0
  33. package/dist/hashFileNode.js +66 -0
  34. package/dist/hashFileNode.js.map +1 -0
  35. package/dist/index.d.ts +20 -0
  36. package/dist/index.d.ts.map +1 -0
  37. package/dist/index.js +41 -0
  38. package/dist/index.js.map +1 -0
  39. package/dist/indexBrowser.d.ts +8 -0
  40. package/dist/indexBrowser.d.ts.map +1 -0
  41. package/dist/indexBrowser.js +19 -0
  42. package/dist/indexBrowser.js.map +1 -0
  43. package/dist/indexNode.d.ts +8 -0
  44. package/dist/indexNode.d.ts.map +1 -0
  45. package/dist/indexNode.js +19 -0
  46. package/dist/indexNode.js.map +1 -0
  47. package/dist/performanceBrowser.d.ts +10 -0
  48. package/dist/performanceBrowser.d.ts.map +1 -0
  49. package/dist/performanceBrowser.js +12 -0
  50. package/dist/performanceBrowser.js.map +1 -0
  51. package/dist/performanceIsomorphic.d.ts +12 -0
  52. package/dist/performanceIsomorphic.d.ts.map +1 -0
  53. package/dist/performanceIsomorphic.js +7 -0
  54. package/dist/performanceIsomorphic.js.map +1 -0
  55. package/dist/performanceNode.d.ts +10 -0
  56. package/dist/performanceNode.d.ts.map +1 -0
  57. package/dist/performanceNode.js +14 -0
  58. package/dist/performanceNode.js.map +1 -0
  59. package/dist/trace.d.ts +40 -0
  60. package/dist/trace.d.ts.map +1 -0
  61. package/dist/trace.js +36 -0
  62. package/dist/trace.js.map +1 -0
  63. package/dist/tsdoc-metadata.json +11 -0
  64. package/dist/typedEventEmitter.d.ts +37 -0
  65. package/dist/typedEventEmitter.d.ts.map +1 -0
  66. package/dist/typedEventEmitter.js +27 -0
  67. package/dist/typedEventEmitter.js.map +1 -0
  68. package/jest-puppeteer.config.js +13 -0
  69. package/jest.config.js +20 -0
  70. package/lib/base64Encoding.d.ts +28 -0
  71. package/lib/base64Encoding.d.ts.map +1 -0
  72. package/lib/base64Encoding.js +37 -0
  73. package/lib/base64Encoding.js.map +1 -0
  74. package/lib/bufferBrowser.d.ts +84 -0
  75. package/lib/bufferBrowser.d.ts.map +1 -0
  76. package/lib/bufferBrowser.js +174 -0
  77. package/lib/bufferBrowser.js.map +1 -0
  78. package/lib/bufferNode.d.ts +62 -0
  79. package/lib/bufferNode.d.ts.map +1 -0
  80. package/lib/bufferNode.js +48 -0
  81. package/lib/bufferNode.js.map +1 -0
  82. package/lib/bufferShared.d.ts +12 -0
  83. package/lib/bufferShared.d.ts.map +1 -0
  84. package/lib/bufferShared.js +17 -0
  85. package/lib/bufferShared.js.map +1 -0
  86. package/lib/eventForwarder.d.ts +33 -0
  87. package/lib/eventForwarder.d.ts.map +1 -0
  88. package/lib/eventForwarder.js +94 -0
  89. package/lib/eventForwarder.js.map +1 -0
  90. package/lib/hashFileBrowser.d.ts +30 -0
  91. package/lib/hashFileBrowser.d.ts.map +1 -0
  92. package/lib/hashFileBrowser.js +73 -0
  93. package/lib/hashFileBrowser.js.map +1 -0
  94. package/lib/hashFileNode.d.ts +30 -0
  95. package/lib/hashFileNode.d.ts.map +1 -0
  96. package/lib/hashFileNode.js +58 -0
  97. package/lib/hashFileNode.js.map +1 -0
  98. package/lib/index.d.ts +20 -0
  99. package/lib/index.d.ts.map +1 -0
  100. package/lib/index.js +21 -0
  101. package/lib/index.js.map +1 -0
  102. package/lib/indexBrowser.d.ts +8 -0
  103. package/lib/indexBrowser.d.ts.map +1 -0
  104. package/lib/indexBrowser.js +8 -0
  105. package/lib/indexBrowser.js.map +1 -0
  106. package/lib/indexNode.d.ts +8 -0
  107. package/lib/indexNode.d.ts.map +1 -0
  108. package/lib/indexNode.js +8 -0
  109. package/lib/indexNode.js.map +1 -0
  110. package/lib/performanceBrowser.d.ts +10 -0
  111. package/lib/performanceBrowser.d.ts.map +1 -0
  112. package/lib/performanceBrowser.js +9 -0
  113. package/lib/performanceBrowser.js.map +1 -0
  114. package/lib/performanceIsomorphic.d.ts +12 -0
  115. package/lib/performanceIsomorphic.d.ts.map +1 -0
  116. package/lib/performanceIsomorphic.js +6 -0
  117. package/lib/performanceIsomorphic.js.map +1 -0
  118. package/lib/performanceNode.d.ts +10 -0
  119. package/lib/performanceNode.d.ts.map +1 -0
  120. package/lib/performanceNode.js +11 -0
  121. package/lib/performanceNode.js.map +1 -0
  122. package/lib/trace.d.ts +40 -0
  123. package/lib/trace.d.ts.map +1 -0
  124. package/lib/trace.js +32 -0
  125. package/lib/trace.js.map +1 -0
  126. package/lib/typedEventEmitter.d.ts +37 -0
  127. package/lib/typedEventEmitter.d.ts.map +1 -0
  128. package/lib/typedEventEmitter.js +23 -0
  129. package/lib/typedEventEmitter.js.map +1 -0
  130. package/package.json +140 -0
  131. package/prettier.config.cjs +8 -0
  132. package/src/base64Encoding.ts +42 -0
  133. package/src/bufferBrowser.ts +198 -0
  134. package/src/bufferNode.ts +80 -0
  135. package/src/bufferShared.ts +17 -0
  136. package/src/eventForwarder.ts +120 -0
  137. package/src/hashFileBrowser.ts +84 -0
  138. package/src/hashFileNode.ts +66 -0
  139. package/src/index.ts +22 -0
  140. package/src/indexBrowser.ts +14 -0
  141. package/src/indexNode.ts +14 -0
  142. package/src/performanceBrowser.ts +11 -0
  143. package/src/performanceIsomorphic.ts +13 -0
  144. package/src/performanceNode.ts +13 -0
  145. package/src/trace.ts +58 -0
  146. package/src/typedEventEmitter.ts +79 -0
  147. package/tsconfig.esnext.json +7 -0
  148. package/tsconfig.json +14 -0
  149. package/types/perf_hooks.d.ts +10 -0
package/.eslintrc.js ADDED
@@ -0,0 +1,16 @@
1
+ /*!
2
+ * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+
6
+ module.exports = {
7
+ extends: [require.resolve("@fluidframework/eslint-config-fluid"), "prettier"],
8
+ parserOptions: {
9
+ project: [
10
+ "./tsconfig.json",
11
+ "./src/test/mocha/tsconfig.json",
12
+ "./src/test/jest/tsconfig.json",
13
+ "./src/test/types/tsconfig.json",
14
+ ],
15
+ },
16
+ };
package/.mocharc.js ADDED
@@ -0,0 +1,12 @@
1
+ /*!
2
+ * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+
6
+ "use strict";
7
+
8
+ const getFluidTestMochaConfig = require("@fluidframework/mocha-test-setup/mocharc-common");
9
+
10
+ const packageDir = __dirname;
11
+ const config = getFluidTestMochaConfig(packageDir);
12
+ module.exports = config;
package/CHANGELOG.md ADDED
@@ -0,0 +1,3 @@
1
+ # @fluid-internal/client-utils
2
+
3
+ ## 2.0.0-internal.6.3.0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ Copyright (c) Microsoft Corporation and contributors. All rights reserved.
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,70 @@
1
+ # @fluid-internal/client-utils
2
+
3
+ This package is intended for sharing and promoting utility functions across packages in the Fluid Framework repo,
4
+ primarily within the client release group.
5
+
6
+ Use outside of the Fluid Framework repo is not supported or recommended.
7
+
8
+ ## Adding code to this package
9
+
10
+ As a utility package, this package does not have a strong identity. This means that it's easy to become a "dumping
11
+ ground" for code that we think we should share but doesn't have an obvious home. We try to avoid dumping things into
12
+ utility packages, and this one is no exception.
13
+
14
+ New code should only be added to this package in rare circumstances. In most cases, the code would be better placed in a
15
+ package with a clear identity (e.g. an "events" package for shared event infrastructure) or not shared at all.
16
+
17
+ ## Requirements
18
+
19
+ This package has important requirements for the code within it.
20
+
21
+ 1. Code within this package should require some external dependencies. If it does not, then the **core-utils** package
22
+ is a better location.
23
+ 1. **All exports must be designated `@internal`.** This code is intended for use within the Fluid Framework only.
24
+ 1. This package should **only contain 'implementation' code, not type definitions.** This is the most flexible rule, and
25
+ there are some exceptions. If the type is _only_ necessary when using this package, then it is probably OK. However,
26
+ usually such types would be better placed in core-interfaces or in a package that corresponds to the purpose.
27
+
28
+ If you want to add code that does not meet these requirements, these other packages may be a better choice:
29
+
30
+ - **Types and interfaces** that are intended to be broadly shared across the client release group should be put in the
31
+ **core-interfaces** package.
32
+ - **Zero-dependency shared code** should be put in the **core-utils** package.
33
+
34
+ <!-- AUTO-GENERATED-CONTENT:START (README_DEPENDENCY_GUIDELINES_SECTION:includeHeading=TRUE) -->
35
+
36
+ <!-- prettier-ignore-start -->
37
+ <!-- NOTE: This section is automatically generated using @fluid-tools/markdown-magic. Do not update these generated contents directly. -->
38
+
39
+ ## Using Fluid Framework libraries
40
+
41
+ When taking a dependency on a Fluid Framework library, we recommend using a `^` (caret) version range, such as `^1.3.4`.
42
+ While Fluid Framework libraries may use different ranges with interdependencies between other Fluid Framework libraries,
43
+ library consumers should always prefer `^`.
44
+
45
+ Note that when depending on a library version of the form 2.0.0-internal.x.y.z, called the Fluid internal version
46
+ scheme, you must use a `>= <` dependency range. Standard `^` and `~` ranges will not work as expected. See the
47
+ [@fluid-tools/version-tools](https://github.com/microsoft/FluidFramework/blob/main/build-tools/packages/version-tools/README.md)
48
+ package for more information including tools to convert between version schemes.
49
+
50
+ <!-- prettier-ignore-end -->
51
+
52
+ <!-- AUTO-GENERATED-CONTENT:END -->
53
+
54
+ <!-- AUTO-GENERATED-CONTENT:START (README_TRADEMARK_SECTION:includeHeading=TRUE) -->
55
+
56
+ <!-- prettier-ignore-start -->
57
+ <!-- NOTE: This section is automatically generated using @fluid-tools/markdown-magic. Do not update these generated contents directly. -->
58
+
59
+ ## Trademark
60
+
61
+ This project may contain Microsoft trademarks or logos for Microsoft projects, products, or services.
62
+
63
+ Use of these trademarks or logos must follow Microsoft's [Trademark & Brand
64
+ Guidelines](https://www.microsoft.com/en-us/legal/intellectualproperty/trademarks/usage/general).
65
+
66
+ Use of Microsoft trademarks or logos in modified versions of this project must not cause confusion or imply Microsoft sponsorship.
67
+
68
+ <!-- prettier-ignore-end -->
69
+
70
+ <!-- AUTO-GENERATED-CONTENT:END -->
@@ -0,0 +1,16 @@
1
+ {
2
+ "$schema": "https://developer.microsoft.com/json-schemas/api-extractor/v7/api-extractor.schema.json",
3
+ "extends": "@fluidframework/build-common/api-extractor-common-report.json",
4
+ "messages": {
5
+ "extractorMessageReporting": {
6
+ "ae-missing-release-tag": {
7
+ "logLevel": "error",
8
+ "addToApiReportFile": false
9
+ },
10
+ "ae-incompatible-release-tags": {
11
+ "logLevel": "error",
12
+ "addToApiReportFile": false
13
+ }
14
+ }
15
+ }
16
+ }
@@ -0,0 +1,28 @@
1
+ /*!
2
+ * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+ /**
6
+ * Converts the provided {@link https://en.wikipedia.org/wiki/Base64 | base64}-encoded string
7
+ * to {@link https://en.wikipedia.org/wiki/UTF-8 | utf-8}.
8
+ *
9
+ * @internal
10
+ */
11
+ export declare const fromBase64ToUtf8: (input: string) => string;
12
+ /**
13
+ * Converts the provided {@link https://en.wikipedia.org/wiki/UTF-8 | utf-8}-encoded string
14
+ * to {@link https://en.wikipedia.org/wiki/Base64 | base64}.
15
+ *
16
+ * @internal
17
+ */
18
+ export declare const fromUtf8ToBase64: (input: string) => string;
19
+ /**
20
+ * Convenience function to convert unknown encoding to utf8 that avoids
21
+ * buffer copies/encode ops when no conversion is needed.
22
+ * @param input - The source string to convert.
23
+ * @param encoding - The source string's encoding.
24
+ *
25
+ * @internal
26
+ */
27
+ export declare const toUtf8: (input: string, encoding: string) => string;
28
+ //# sourceMappingURL=base64Encoding.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"base64Encoding.d.ts","sourceRoot":"","sources":["../src/base64Encoding.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAIH;;;;;GAKG;AACH,eAAO,MAAM,gBAAgB,UAAW,MAAM,KAAG,MACC,CAAC;AAEnD;;;;;GAKG;AACH,eAAO,MAAM,gBAAgB,UAAW,MAAM,KAAG,MACA,CAAC;AAElD;;;;;;;GAOG;AACH,eAAO,MAAM,MAAM,UAAW,MAAM,YAAY,MAAM,KAAG,MAQxD,CAAC"}
@@ -0,0 +1,43 @@
1
+ "use strict";
2
+ /*!
3
+ * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
4
+ * Licensed under the MIT License.
5
+ */
6
+ Object.defineProperty(exports, "__esModule", { value: true });
7
+ exports.toUtf8 = exports.fromUtf8ToBase64 = exports.fromBase64ToUtf8 = void 0;
8
+ const indexNode_1 = require("./indexNode");
9
+ /**
10
+ * Converts the provided {@link https://en.wikipedia.org/wiki/Base64 | base64}-encoded string
11
+ * to {@link https://en.wikipedia.org/wiki/UTF-8 | utf-8}.
12
+ *
13
+ * @internal
14
+ */
15
+ const fromBase64ToUtf8 = (input) => indexNode_1.IsoBuffer.from(input, "base64").toString("utf-8");
16
+ exports.fromBase64ToUtf8 = fromBase64ToUtf8;
17
+ /**
18
+ * Converts the provided {@link https://en.wikipedia.org/wiki/UTF-8 | utf-8}-encoded string
19
+ * to {@link https://en.wikipedia.org/wiki/Base64 | base64}.
20
+ *
21
+ * @internal
22
+ */
23
+ const fromUtf8ToBase64 = (input) => indexNode_1.IsoBuffer.from(input, "utf8").toString("base64");
24
+ exports.fromUtf8ToBase64 = fromUtf8ToBase64;
25
+ /**
26
+ * Convenience function to convert unknown encoding to utf8 that avoids
27
+ * buffer copies/encode ops when no conversion is needed.
28
+ * @param input - The source string to convert.
29
+ * @param encoding - The source string's encoding.
30
+ *
31
+ * @internal
32
+ */
33
+ const toUtf8 = (input, encoding) => {
34
+ switch (encoding) {
35
+ case "utf8":
36
+ case "utf-8":
37
+ return input;
38
+ default:
39
+ return indexNode_1.IsoBuffer.from(input, encoding).toString();
40
+ }
41
+ };
42
+ exports.toUtf8 = toUtf8;
43
+ //# sourceMappingURL=base64Encoding.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"base64Encoding.js","sourceRoot":"","sources":["../src/base64Encoding.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;AAEH,2CAAwC;AAExC;;;;;GAKG;AACI,MAAM,gBAAgB,GAAG,CAAC,KAAa,EAAU,EAAE,CACzD,qBAAS,CAAC,IAAI,CAAC,KAAK,EAAE,QAAQ,CAAC,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC;AADtC,QAAA,gBAAgB,oBACsB;AAEnD;;;;;GAKG;AACI,MAAM,gBAAgB,GAAG,CAAC,KAAa,EAAU,EAAE,CACzD,qBAAS,CAAC,IAAI,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAC;AADrC,QAAA,gBAAgB,oBACqB;AAElD;;;;;;;GAOG;AACI,MAAM,MAAM,GAAG,CAAC,KAAa,EAAE,QAAgB,EAAU,EAAE;IACjE,QAAQ,QAAQ,EAAE;QACjB,KAAK,MAAM,CAAC;QACZ,KAAK,OAAO;YACX,OAAO,KAAK,CAAC;QACd;YACC,OAAO,qBAAS,CAAC,IAAI,CAAC,KAAK,EAAE,QAAQ,CAAC,CAAC,QAAQ,EAAE,CAAC;KACnD;AACF,CAAC,CAAC;AARW,QAAA,MAAM,UAQjB","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\nimport { IsoBuffer } from \"./indexNode\";\n\n/**\n * Converts the provided {@link https://en.wikipedia.org/wiki/Base64 | base64}-encoded string\n * to {@link https://en.wikipedia.org/wiki/UTF-8 | utf-8}.\n *\n * @internal\n */\nexport const fromBase64ToUtf8 = (input: string): string =>\n\tIsoBuffer.from(input, \"base64\").toString(\"utf-8\");\n\n/**\n * Converts the provided {@link https://en.wikipedia.org/wiki/UTF-8 | utf-8}-encoded string\n * to {@link https://en.wikipedia.org/wiki/Base64 | base64}.\n *\n * @internal\n */\nexport const fromUtf8ToBase64 = (input: string): string =>\n\tIsoBuffer.from(input, \"utf8\").toString(\"base64\");\n\n/**\n * Convenience function to convert unknown encoding to utf8 that avoids\n * buffer copies/encode ops when no conversion is needed.\n * @param input - The source string to convert.\n * @param encoding - The source string's encoding.\n *\n * @internal\n */\nexport const toUtf8 = (input: string, encoding: string): string => {\n\tswitch (encoding) {\n\t\tcase \"utf8\":\n\t\tcase \"utf-8\":\n\t\t\treturn input;\n\t\tdefault:\n\t\t\treturn IsoBuffer.from(input, encoding).toString();\n\t}\n};\n"]}
@@ -0,0 +1,84 @@
1
+ /*!
2
+ * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+ /**
6
+ * Converts a Uint8Array to a string of the provided encoding
7
+ * Useful when the array might be an {@link IsoBuffer}.
8
+ *
9
+ * @param arr - The array to convert.
10
+ * @param encoding - Optional target encoding; only "utf8" and "base64" are
11
+ * supported, with "utf8" being default.
12
+ * @returns The converted string.
13
+ *
14
+ * @internal
15
+ */
16
+ export declare function Uint8ArrayToString(arr: Uint8Array, encoding?: string): string;
17
+ /**
18
+ * Converts a {@link https://en.wikipedia.org/wiki/Base64 | base64} or
19
+ * {@link https://en.wikipedia.org/wiki/UTF-8 | utf-8} string to array buffer.
20
+ *
21
+ * @param encoding - The input string's encoding.
22
+ *
23
+ * @internal
24
+ */
25
+ export declare const stringToBuffer: (input: string, encoding: string) => ArrayBufferLike;
26
+ /**
27
+ * Convert binary blob to string format
28
+ *
29
+ * @param blob - the binary blob
30
+ * @param encoding - output string's encoding
31
+ * @returns the blob in string format
32
+ *
33
+ * @internal
34
+ */
35
+ export declare const bufferToString: (blob: ArrayBufferLike, encoding: string) => string;
36
+ /**
37
+ * Determines if an object is an array buffer.
38
+ *
39
+ * @remarks Will detect and reject TypedArrays, like Uint8Array.
40
+ * Reason - they can be viewport into Array, they can be accepted, but caller has to deal with
41
+ * math properly (i.e. Take into account byteOffset at minimum).
42
+ * For example, construction of new TypedArray can be in the form of new TypedArray(typedArray) or
43
+ * new TypedArray(buffer, byteOffset, length), but passing TypedArray will result in fist path (and
44
+ * ignoring byteOffice, length).
45
+ *
46
+ * @param obj - The object to determine if it is an ArrayBuffer.
47
+ *
48
+ * @internal
49
+ */
50
+ export declare function isArrayBuffer(obj: unknown): obj is ArrayBuffer;
51
+ /**
52
+ * Minimal implementation of Buffer for our usages in the browser environment.
53
+ *
54
+ * @internal
55
+ */
56
+ export declare class IsoBuffer extends Uint8Array {
57
+ /**
58
+ * Convert the buffer to a string.
59
+ * Only supports encoding the whole string (unlike the Node Buffer equivalent)
60
+ * and only utf8 and base64 encodings.
61
+ *
62
+ * @param encoding - The encoding to use.
63
+ */
64
+ toString(encoding?: string): string;
65
+ /**
66
+ * Static constructor
67
+ * @param value - (string | ArrayBuffer)
68
+ * @param encodingOrOffset - (string | number)
69
+ * @param length - (number)
70
+ *
71
+ * @privateRemarks TODO: Use actual types
72
+ */
73
+ static from(value: any, encodingOrOffset?: any, length?: any): IsoBuffer;
74
+ static fromArrayBuffer(arrayBuffer: ArrayBuffer, byteOffset?: number, byteLength?: number): IsoBuffer;
75
+ static fromString(str: string, encoding?: string): IsoBuffer;
76
+ static isBuffer(obj: unknown): boolean;
77
+ /**
78
+ * Sanitize a base64 string to provide to base64-js library.
79
+ * {@link https://www.npmjs.com/package/base64-js} is not as tolerant of the same malformed base64 as Node'
80
+ * Buffer is.
81
+ */
82
+ private static sanitizeBase64;
83
+ }
84
+ //# sourceMappingURL=bufferBrowser.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"bufferBrowser.d.ts","sourceRoot":"","sources":["../src/bufferBrowser.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAIH;;;;;;;;;;GAUG;AACH,wBAAgB,kBAAkB,CAAC,GAAG,EAAE,UAAU,EAAE,QAAQ,CAAC,EAAE,MAAM,GAAG,MAAM,CAc7E;AAED;;;;;;;GAOG;AACH,eAAO,MAAM,cAAc,UAAW,MAAM,YAAY,MAAM,KAAG,eAC1B,CAAC;AAExC;;;;;;;;GAQG;AACH,eAAO,MAAM,cAAc,SAAU,eAAe,YAAY,MAAM,KAAG,MACjC,CAAC;AAEzC;;;;;;;;;;;;;GAaG;AACH,wBAAgB,aAAa,CAAC,GAAG,EAAE,OAAO,GAAG,GAAG,IAAI,WAAW,CAW9D;AAED;;;;GAIG;AACH,qBAAa,SAAU,SAAQ,UAAU;IACxC;;;;;;OAMG;IACI,QAAQ,CAAC,QAAQ,CAAC,EAAE,MAAM,GAAG,MAAM;IAI1C;;;;;;;OAOG;IAEH,MAAM,CAAC,IAAI,CAAC,KAAK,EAAE,GAAG,EAAE,gBAAgB,CAAC,EAAE,GAAG,EAAE,MAAM,CAAC,EAAE,GAAG,GAAG,SAAS;IAsBxE,MAAM,CAAC,eAAe,CACrB,WAAW,EAAE,WAAW,EACxB,UAAU,CAAC,EAAE,MAAM,EACnB,UAAU,CAAC,EAAE,MAAM,GACjB,SAAS;IAeZ,MAAM,CAAC,UAAU,CAAC,GAAG,EAAE,MAAM,EAAE,QAAQ,CAAC,EAAE,MAAM,GAAG,SAAS;IAmB5D,MAAM,CAAC,QAAQ,CAAC,GAAG,EAAE,OAAO,GAAG,OAAO;IAItC;;;;OAIG;IACH,OAAO,CAAC,MAAM,CAAC,cAAc;CAkB7B"}
@@ -0,0 +1,201 @@
1
+ "use strict";
2
+ /*!
3
+ * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
4
+ * Licensed under the MIT License.
5
+ */
6
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
7
+ if (k2 === undefined) k2 = k;
8
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.IsoBuffer = exports.isArrayBuffer = exports.bufferToString = exports.stringToBuffer = exports.Uint8ArrayToString = void 0;
27
+ const base64js = __importStar(require("base64-js"));
28
+ /**
29
+ * Converts a Uint8Array to a string of the provided encoding
30
+ * Useful when the array might be an {@link IsoBuffer}.
31
+ *
32
+ * @param arr - The array to convert.
33
+ * @param encoding - Optional target encoding; only "utf8" and "base64" are
34
+ * supported, with "utf8" being default.
35
+ * @returns The converted string.
36
+ *
37
+ * @internal
38
+ */
39
+ function Uint8ArrayToString(arr, encoding) {
40
+ switch (encoding) {
41
+ case "base64": {
42
+ return base64js.fromByteArray(arr);
43
+ }
44
+ case "utf8":
45
+ case "utf-8":
46
+ case undefined: {
47
+ return new TextDecoder().decode(arr);
48
+ }
49
+ default: {
50
+ throw new Error("invalid/unsupported encoding");
51
+ }
52
+ }
53
+ }
54
+ exports.Uint8ArrayToString = Uint8ArrayToString;
55
+ /**
56
+ * Converts a {@link https://en.wikipedia.org/wiki/Base64 | base64} or
57
+ * {@link https://en.wikipedia.org/wiki/UTF-8 | utf-8} string to array buffer.
58
+ *
59
+ * @param encoding - The input string's encoding.
60
+ *
61
+ * @internal
62
+ */
63
+ const stringToBuffer = (input, encoding) => IsoBuffer.from(input, encoding).buffer;
64
+ exports.stringToBuffer = stringToBuffer;
65
+ /**
66
+ * Convert binary blob to string format
67
+ *
68
+ * @param blob - the binary blob
69
+ * @param encoding - output string's encoding
70
+ * @returns the blob in string format
71
+ *
72
+ * @internal
73
+ */
74
+ const bufferToString = (blob, encoding) => IsoBuffer.from(blob).toString(encoding);
75
+ exports.bufferToString = bufferToString;
76
+ /**
77
+ * Determines if an object is an array buffer.
78
+ *
79
+ * @remarks Will detect and reject TypedArrays, like Uint8Array.
80
+ * Reason - they can be viewport into Array, they can be accepted, but caller has to deal with
81
+ * math properly (i.e. Take into account byteOffset at minimum).
82
+ * For example, construction of new TypedArray can be in the form of new TypedArray(typedArray) or
83
+ * new TypedArray(buffer, byteOffset, length), but passing TypedArray will result in fist path (and
84
+ * ignoring byteOffice, length).
85
+ *
86
+ * @param obj - The object to determine if it is an ArrayBuffer.
87
+ *
88
+ * @internal
89
+ */
90
+ function isArrayBuffer(obj) {
91
+ const maybe = obj;
92
+ return (obj instanceof ArrayBuffer ||
93
+ (typeof maybe === "object" &&
94
+ maybe !== null &&
95
+ typeof maybe.byteLength === "number" &&
96
+ typeof maybe.slice === "function" &&
97
+ maybe.byteOffset === undefined &&
98
+ maybe.buffer === undefined));
99
+ }
100
+ exports.isArrayBuffer = isArrayBuffer;
101
+ /**
102
+ * Minimal implementation of Buffer for our usages in the browser environment.
103
+ *
104
+ * @internal
105
+ */
106
+ class IsoBuffer extends Uint8Array {
107
+ /**
108
+ * Convert the buffer to a string.
109
+ * Only supports encoding the whole string (unlike the Node Buffer equivalent)
110
+ * and only utf8 and base64 encodings.
111
+ *
112
+ * @param encoding - The encoding to use.
113
+ */
114
+ toString(encoding) {
115
+ return Uint8ArrayToString(this, encoding);
116
+ }
117
+ /**
118
+ * Static constructor
119
+ * @param value - (string | ArrayBuffer)
120
+ * @param encodingOrOffset - (string | number)
121
+ * @param length - (number)
122
+ *
123
+ * @privateRemarks TODO: Use actual types
124
+ */
125
+ // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types, @typescript-eslint/no-explicit-any
126
+ static from(value, encodingOrOffset, length) {
127
+ if (typeof value === "string") {
128
+ return IsoBuffer.fromString(value, encodingOrOffset);
129
+ // Capture any typed arrays, including Uint8Array (and thus - IsoBuffer!)
130
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
131
+ }
132
+ else if (value !== null && typeof value === "object" && isArrayBuffer(value.buffer)) {
133
+ // The version of the from function for the node buffer, which takes a buffer or typed array
134
+ // as first parameter, does not have any offset or length parameters. Those are just silently
135
+ // ignored and not taken into account
136
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-member-access
137
+ return IsoBuffer.fromArrayBuffer(value.buffer, value.byteOffset, value.byteLength);
138
+ }
139
+ else if (isArrayBuffer(value)) {
140
+ return IsoBuffer.fromArrayBuffer(value, encodingOrOffset, length);
141
+ }
142
+ else {
143
+ throw new TypeError("Input value was neither a string nor an ArrayBuffer.");
144
+ }
145
+ }
146
+ static fromArrayBuffer(arrayBuffer, byteOffset, byteLength) {
147
+ const offset = byteOffset ?? 0;
148
+ const validLength = byteLength ?? arrayBuffer.byteLength - offset;
149
+ if (offset < 0 ||
150
+ offset > arrayBuffer.byteLength ||
151
+ validLength < 0 ||
152
+ validLength + offset > arrayBuffer.byteLength) {
153
+ throw new RangeError("Invalid range specified.");
154
+ }
155
+ return new IsoBuffer(arrayBuffer, offset, validLength);
156
+ }
157
+ static fromString(str, encoding) {
158
+ switch (encoding) {
159
+ case "base64": {
160
+ const sanitizedString = this.sanitizeBase64(str);
161
+ const encoded = base64js.toByteArray(sanitizedString);
162
+ return new IsoBuffer(encoded.buffer);
163
+ }
164
+ case "utf8":
165
+ case "utf-8":
166
+ case undefined: {
167
+ const encoded = new TextEncoder().encode(str);
168
+ return new IsoBuffer(encoded.buffer);
169
+ }
170
+ default: {
171
+ throw new Error("invalid/unsupported encoding");
172
+ }
173
+ }
174
+ }
175
+ static isBuffer(obj) {
176
+ throw new Error("unimplemented");
177
+ }
178
+ /**
179
+ * Sanitize a base64 string to provide to base64-js library.
180
+ * {@link https://www.npmjs.com/package/base64-js} is not as tolerant of the same malformed base64 as Node'
181
+ * Buffer is.
182
+ */
183
+ static sanitizeBase64(str) {
184
+ let sanitizedStr = str;
185
+ // Remove everything after padding - Node buffer ignores everything
186
+ // after any padding whereas base64-js does not
187
+ sanitizedStr = sanitizedStr.split("=")[0];
188
+ // Remove invalid characters - Node buffer strips invalid characters
189
+ // whereas base64-js replaces them with "A"
190
+ sanitizedStr = sanitizedStr.replace(/[^\w+-/]/g, "");
191
+ // Check for missing padding - Node buffer tolerates missing padding
192
+ // whereas base64-js does not
193
+ if (sanitizedStr.length % 4 !== 0) {
194
+ const paddingArray = ["", "===", "==", "="];
195
+ sanitizedStr += paddingArray[sanitizedStr.length % 4];
196
+ }
197
+ return sanitizedStr;
198
+ }
199
+ }
200
+ exports.IsoBuffer = IsoBuffer;
201
+ //# sourceMappingURL=bufferBrowser.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"bufferBrowser.js","sourceRoot":"","sources":["../src/bufferBrowser.ts"],"names":[],"mappings":"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","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\nimport * as base64js from \"base64-js\";\n\n/**\n * Converts a Uint8Array to a string of the provided encoding\n * Useful when the array might be an {@link IsoBuffer}.\n *\n * @param arr - The array to convert.\n * @param encoding - Optional target encoding; only \"utf8\" and \"base64\" are\n * supported, with \"utf8\" being default.\n * @returns The converted string.\n *\n * @internal\n */\nexport function Uint8ArrayToString(arr: Uint8Array, encoding?: string): string {\n\tswitch (encoding) {\n\t\tcase \"base64\": {\n\t\t\treturn base64js.fromByteArray(arr);\n\t\t}\n\t\tcase \"utf8\":\n\t\tcase \"utf-8\":\n\t\tcase undefined: {\n\t\t\treturn new TextDecoder().decode(arr);\n\t\t}\n\t\tdefault: {\n\t\t\tthrow new Error(\"invalid/unsupported encoding\");\n\t\t}\n\t}\n}\n\n/**\n * Converts a {@link https://en.wikipedia.org/wiki/Base64 | base64} or\n * {@link https://en.wikipedia.org/wiki/UTF-8 | utf-8} string to array buffer.\n *\n * @param encoding - The input string's encoding.\n *\n * @internal\n */\nexport const stringToBuffer = (input: string, encoding: string): ArrayBufferLike =>\n\tIsoBuffer.from(input, encoding).buffer;\n\n/**\n * Convert binary blob to string format\n *\n * @param blob - the binary blob\n * @param encoding - output string's encoding\n * @returns the blob in string format\n *\n * @internal\n */\nexport const bufferToString = (blob: ArrayBufferLike, encoding: string): string =>\n\tIsoBuffer.from(blob).toString(encoding);\n\n/**\n * Determines if an object is an array buffer.\n *\n * @remarks Will detect and reject TypedArrays, like Uint8Array.\n * Reason - they can be viewport into Array, they can be accepted, but caller has to deal with\n * math properly (i.e. Take into account byteOffset at minimum).\n * For example, construction of new TypedArray can be in the form of new TypedArray(typedArray) or\n * new TypedArray(buffer, byteOffset, length), but passing TypedArray will result in fist path (and\n * ignoring byteOffice, length).\n *\n * @param obj - The object to determine if it is an ArrayBuffer.\n *\n * @internal\n */\nexport function isArrayBuffer(obj: unknown): obj is ArrayBuffer {\n\tconst maybe = obj as (Partial<ArrayBuffer> & Partial<Uint8Array>) | undefined;\n\treturn (\n\t\tobj instanceof ArrayBuffer ||\n\t\t(typeof maybe === \"object\" &&\n\t\t\tmaybe !== null &&\n\t\t\ttypeof maybe.byteLength === \"number\" &&\n\t\t\ttypeof maybe.slice === \"function\" &&\n\t\t\tmaybe.byteOffset === undefined &&\n\t\t\tmaybe.buffer === undefined)\n\t);\n}\n\n/**\n * Minimal implementation of Buffer for our usages in the browser environment.\n *\n * @internal\n */\nexport class IsoBuffer extends Uint8Array {\n\t/**\n\t * Convert the buffer to a string.\n\t * Only supports encoding the whole string (unlike the Node Buffer equivalent)\n\t * and only utf8 and base64 encodings.\n\t *\n\t * @param encoding - The encoding to use.\n\t */\n\tpublic toString(encoding?: string): string {\n\t\treturn Uint8ArrayToString(this, encoding);\n\t}\n\n\t/**\n\t * Static constructor\n\t * @param value - (string | ArrayBuffer)\n\t * @param encodingOrOffset - (string | number)\n\t * @param length - (number)\n\t *\n\t * @privateRemarks TODO: Use actual types\n\t */\n\t// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types, @typescript-eslint/no-explicit-any\n\tstatic from(value: any, encodingOrOffset?: any, length?: any): IsoBuffer {\n\t\tif (typeof value === \"string\") {\n\t\t\treturn IsoBuffer.fromString(value, encodingOrOffset as string | undefined);\n\t\t\t// Capture any typed arrays, including Uint8Array (and thus - IsoBuffer!)\n\t\t\t// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access\n\t\t} else if (value !== null && typeof value === \"object\" && isArrayBuffer(value.buffer)) {\n\t\t\t// The version of the from function for the node buffer, which takes a buffer or typed array\n\t\t\t// as first parameter, does not have any offset or length parameters. Those are just silently\n\t\t\t// ignored and not taken into account\n\t\t\t// eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-member-access\n\t\t\treturn IsoBuffer.fromArrayBuffer(value.buffer, value.byteOffset, value.byteLength);\n\t\t} else if (isArrayBuffer(value)) {\n\t\t\treturn IsoBuffer.fromArrayBuffer(\n\t\t\t\tvalue,\n\t\t\t\tencodingOrOffset as number | undefined,\n\t\t\t\tlength as number,\n\t\t\t);\n\t\t} else {\n\t\t\tthrow new TypeError(\"Input value was neither a string nor an ArrayBuffer.\");\n\t\t}\n\t}\n\n\tstatic fromArrayBuffer(\n\t\tarrayBuffer: ArrayBuffer,\n\t\tbyteOffset?: number,\n\t\tbyteLength?: number,\n\t): IsoBuffer {\n\t\tconst offset = byteOffset ?? 0;\n\t\tconst validLength = byteLength ?? arrayBuffer.byteLength - offset;\n\t\tif (\n\t\t\toffset < 0 ||\n\t\t\toffset > arrayBuffer.byteLength ||\n\t\t\tvalidLength < 0 ||\n\t\t\tvalidLength + offset > arrayBuffer.byteLength\n\t\t) {\n\t\t\tthrow new RangeError(\"Invalid range specified.\");\n\t\t}\n\n\t\treturn new IsoBuffer(arrayBuffer, offset, validLength);\n\t}\n\n\tstatic fromString(str: string, encoding?: string): IsoBuffer {\n\t\tswitch (encoding) {\n\t\t\tcase \"base64\": {\n\t\t\t\tconst sanitizedString = this.sanitizeBase64(str);\n\t\t\t\tconst encoded = base64js.toByteArray(sanitizedString);\n\t\t\t\treturn new IsoBuffer(encoded.buffer);\n\t\t\t}\n\t\t\tcase \"utf8\":\n\t\t\tcase \"utf-8\":\n\t\t\tcase undefined: {\n\t\t\t\tconst encoded = new TextEncoder().encode(str);\n\t\t\t\treturn new IsoBuffer(encoded.buffer);\n\t\t\t}\n\t\t\tdefault: {\n\t\t\t\tthrow new Error(\"invalid/unsupported encoding\");\n\t\t\t}\n\t\t}\n\t}\n\n\tstatic isBuffer(obj: unknown): boolean {\n\t\tthrow new Error(\"unimplemented\");\n\t}\n\n\t/**\n\t * Sanitize a base64 string to provide to base64-js library.\n\t * {@link https://www.npmjs.com/package/base64-js} is not as tolerant of the same malformed base64 as Node'\n\t * Buffer is.\n\t */\n\tprivate static sanitizeBase64(str: string): string {\n\t\tlet sanitizedStr = str;\n\t\t// Remove everything after padding - Node buffer ignores everything\n\t\t// after any padding whereas base64-js does not\n\t\tsanitizedStr = sanitizedStr.split(\"=\")[0];\n\n\t\t// Remove invalid characters - Node buffer strips invalid characters\n\t\t// whereas base64-js replaces them with \"A\"\n\t\tsanitizedStr = sanitizedStr.replace(/[^\\w+-/]/g, \"\");\n\n\t\t// Check for missing padding - Node buffer tolerates missing padding\n\t\t// whereas base64-js does not\n\t\tif (sanitizedStr.length % 4 !== 0) {\n\t\t\tconst paddingArray = [\"\", \"===\", \"==\", \"=\"];\n\t\t\tsanitizedStr += paddingArray[sanitizedStr.length % 4];\n\t\t}\n\t\treturn sanitizedStr;\n\t}\n}\n"]}
@@ -0,0 +1,62 @@
1
+ /*!
2
+ * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+ /**
6
+ * Declare the subset of Buffer functionality we want to make available instead of
7
+ * exposing the entirely of Node's typings. This should match the public interface
8
+ * of the browser implementation, so any changes made in one should be made in both.
9
+ *
10
+ * @public
11
+ */
12
+ export declare class Buffer extends Uint8Array {
13
+ toString(encoding?: string): string;
14
+ /**
15
+ * Static constructor
16
+ *
17
+ * @param value - (string | ArrayBuffer).
18
+ * @param encodingOrOffset - (string | number).
19
+ * @param length - (number).
20
+ *
21
+ * @privateRemarks TODO: Use actual types
22
+ */
23
+ static from(value: unknown, encodingOrOffset?: unknown, length?: unknown): IsoBuffer;
24
+ static isBuffer(obj: unknown): obj is Buffer;
25
+ }
26
+ /**
27
+ * @public
28
+ */
29
+ export declare const IsoBuffer: typeof Buffer;
30
+ /**
31
+ * @public
32
+ */
33
+ export declare type IsoBuffer = Buffer;
34
+ /**
35
+ * Converts a Uint8Array to a string of the provided encoding.
36
+ * @remarks Useful when the array might be an IsoBuffer.
37
+ * @param arr - The array to convert.
38
+ * @param encoding - Optional target encoding; only "utf8" and "base64" are
39
+ * supported, with "utf8" being default.
40
+ * @returns The converted string.
41
+ *
42
+ * @internal
43
+ */
44
+ export declare function Uint8ArrayToString(arr: Uint8Array, encoding?: string): string;
45
+ /**
46
+ * Convert base64 or utf8 string to array buffer.
47
+ * @param encoding - The input string's encoding.
48
+ *
49
+ * @internal
50
+ */
51
+ export declare function stringToBuffer(input: string, encoding: string): ArrayBufferLike;
52
+ /**
53
+ * Convert binary blob to string format
54
+ *
55
+ * @param blob - The binary blob
56
+ * @param encoding - Output string's encoding
57
+ * @returns The blob in string format
58
+ *
59
+ * @internal
60
+ */
61
+ export declare const bufferToString: (blob: ArrayBufferLike, encoding: string) => string;
62
+ //# sourceMappingURL=bufferNode.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"bufferNode.d.ts","sourceRoot":"","sources":["../src/bufferNode.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH;;;;;;GAMG;AACH,MAAM,CAAC,OAAO,OAAO,MAAO,SAAQ,UAAU;IAC7C,QAAQ,CAAC,QAAQ,CAAC,EAAE,MAAM,GAAG,MAAM;IAEnC;;;;;;;;OAQG;IACH,MAAM,CAAC,IAAI,CAAC,KAAK,EAAE,OAAO,EAAE,gBAAgB,CAAC,EAAE,OAAO,EAAE,MAAM,CAAC,EAAE,OAAO,GAAG,SAAS;IACpF,MAAM,CAAC,QAAQ,CAAC,GAAG,EAAE,OAAO,GAAG,GAAG,IAAI,MAAM;CAC5C;AAED;;GAEG;AACH,eAAO,MAAM,SAAS,eAAS,CAAC;AAEhC;;GAEG;AACH,oBAAY,SAAS,GAAG,MAAM,CAAC;AAE/B;;;;;;;;;GASG;AACH,wBAAgB,kBAAkB,CAAC,GAAG,EAAE,UAAU,EAAE,QAAQ,CAAC,EAAE,MAAM,GAAG,MAAM,CAG7E;AAED;;;;;GAKG;AACH,wBAAgB,cAAc,CAAC,KAAK,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,eAAe,CAQ/E;AAED;;;;;;;;GAQG;AACH,eAAO,MAAM,cAAc,SAAU,eAAe,YAAY,MAAM,KAAG,MACjC,CAAC"}
@@ -0,0 +1,54 @@
1
+ "use strict";
2
+ /*!
3
+ * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
4
+ * Licensed under the MIT License.
5
+ */
6
+ Object.defineProperty(exports, "__esModule", { value: true });
7
+ exports.bufferToString = exports.stringToBuffer = exports.Uint8ArrayToString = exports.IsoBuffer = void 0;
8
+ /**
9
+ * @public
10
+ */
11
+ exports.IsoBuffer = Buffer;
12
+ /**
13
+ * Converts a Uint8Array to a string of the provided encoding.
14
+ * @remarks Useful when the array might be an IsoBuffer.
15
+ * @param arr - The array to convert.
16
+ * @param encoding - Optional target encoding; only "utf8" and "base64" are
17
+ * supported, with "utf8" being default.
18
+ * @returns The converted string.
19
+ *
20
+ * @internal
21
+ */
22
+ function Uint8ArrayToString(arr, encoding) {
23
+ // Make this check because Buffer.from(arr) will always do a buffer copy
24
+ return Buffer.isBuffer(arr) ? arr.toString(encoding) : Buffer.from(arr).toString(encoding);
25
+ }
26
+ exports.Uint8ArrayToString = Uint8ArrayToString;
27
+ /**
28
+ * Convert base64 or utf8 string to array buffer.
29
+ * @param encoding - The input string's encoding.
30
+ *
31
+ * @internal
32
+ */
33
+ function stringToBuffer(input, encoding) {
34
+ const iso = exports.IsoBuffer.from(input, encoding);
35
+ // In a Node environment, IsoBuffer may be a Node.js Buffer. Node.js will
36
+ // pool multiple small Buffer instances into a single ArrayBuffer, in which
37
+ // case we need to slice the appropriate span of bytes.
38
+ return iso.byteLength === iso.buffer.byteLength
39
+ ? iso.buffer
40
+ : iso.buffer.slice(iso.byteOffset, iso.byteOffset + iso.byteLength);
41
+ }
42
+ exports.stringToBuffer = stringToBuffer;
43
+ /**
44
+ * Convert binary blob to string format
45
+ *
46
+ * @param blob - The binary blob
47
+ * @param encoding - Output string's encoding
48
+ * @returns The blob in string format
49
+ *
50
+ * @internal
51
+ */
52
+ const bufferToString = (blob, encoding) => exports.IsoBuffer.from(blob).toString(encoding);
53
+ exports.bufferToString = bufferToString;
54
+ //# sourceMappingURL=bufferNode.js.map