@fluid-internal/client-utils 2.0.0-dev-rc.1.0.0.224419

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 (140) hide show
  1. package/.eslintrc.js +16 -0
  2. package/.mocharc.js +12 -0
  3. package/CHANGELOG.md +37 -0
  4. package/LICENSE +21 -0
  5. package/README.md +75 -0
  6. package/api-extractor-lint.json +4 -0
  7. package/api-extractor.json +4 -0
  8. package/api-report/client-utils.api.md +130 -0
  9. package/dist/base64Encoding.cjs +46 -0
  10. package/dist/base64Encoding.cjs.map +1 -0
  11. package/dist/base64Encoding.d.ts +28 -0
  12. package/dist/base64Encoding.d.ts.map +1 -0
  13. package/dist/bufferBrowser.cjs +212 -0
  14. package/dist/bufferBrowser.cjs.map +1 -0
  15. package/dist/bufferBrowser.d.ts +84 -0
  16. package/dist/bufferBrowser.d.ts.map +1 -0
  17. package/dist/bufferNode.cjs +59 -0
  18. package/dist/bufferNode.cjs.map +1 -0
  19. package/dist/bufferNode.d.ts +62 -0
  20. package/dist/bufferNode.d.ts.map +1 -0
  21. package/dist/bufferShared.cjs +21 -0
  22. package/dist/bufferShared.cjs.map +1 -0
  23. package/dist/bufferShared.d.ts +12 -0
  24. package/dist/bufferShared.d.ts.map +1 -0
  25. package/dist/client-utils-alpha.d.ts +82 -0
  26. package/dist/client-utils-beta.d.ts +73 -0
  27. package/dist/client-utils-public.d.ts +73 -0
  28. package/dist/client-utils-untrimmed.d.ts +247 -0
  29. package/dist/eventForwarder.cjs +98 -0
  30. package/dist/eventForwarder.cjs.map +1 -0
  31. package/dist/eventForwarder.d.ts +34 -0
  32. package/dist/eventForwarder.d.ts.map +1 -0
  33. package/dist/hashFileBrowser.cjs +99 -0
  34. package/dist/hashFileBrowser.cjs.map +1 -0
  35. package/dist/hashFileBrowser.d.ts +30 -0
  36. package/dist/hashFileBrowser.d.ts.map +1 -0
  37. package/dist/hashFileNode.cjs +56 -0
  38. package/dist/hashFileNode.cjs.map +1 -0
  39. package/dist/hashFileNode.d.ts +30 -0
  40. package/dist/hashFileNode.d.ts.map +1 -0
  41. package/dist/index.cjs +45 -0
  42. package/dist/index.cjs.map +1 -0
  43. package/dist/index.d.ts +20 -0
  44. package/dist/index.d.ts.map +1 -0
  45. package/dist/indexBrowser.cjs +19 -0
  46. package/dist/indexBrowser.cjs.map +1 -0
  47. package/dist/indexBrowser.d.ts +8 -0
  48. package/dist/indexBrowser.d.ts.map +1 -0
  49. package/dist/indexNode.cjs +19 -0
  50. package/dist/indexNode.cjs.map +1 -0
  51. package/dist/indexNode.d.ts +8 -0
  52. package/dist/indexNode.d.ts.map +1 -0
  53. package/dist/performanceIsomorphic.cjs +17 -0
  54. package/dist/performanceIsomorphic.cjs.map +1 -0
  55. package/dist/performanceIsomorphic.d.ts +21 -0
  56. package/dist/performanceIsomorphic.d.ts.map +1 -0
  57. package/dist/trace.cjs +36 -0
  58. package/dist/trace.cjs.map +1 -0
  59. package/dist/trace.d.ts +40 -0
  60. package/dist/trace.d.ts.map +1 -0
  61. package/dist/tsdoc-metadata.json +11 -0
  62. package/dist/typedEventEmitter.cjs +28 -0
  63. package/dist/typedEventEmitter.cjs.map +1 -0
  64. package/dist/typedEventEmitter.d.ts +36 -0
  65. package/dist/typedEventEmitter.d.ts.map +1 -0
  66. package/jest-puppeteer.config.js +13 -0
  67. package/jest.config.js +20 -0
  68. package/lib/base64Encoding.d.mts +28 -0
  69. package/lib/base64Encoding.d.mts.map +1 -0
  70. package/lib/base64Encoding.mjs +40 -0
  71. package/lib/base64Encoding.mjs.map +1 -0
  72. package/lib/bufferBrowser.d.mts +84 -0
  73. package/lib/bufferBrowser.d.mts.map +1 -0
  74. package/lib/bufferBrowser.mjs +181 -0
  75. package/lib/bufferBrowser.mjs.map +1 -0
  76. package/lib/bufferNode.d.mts +62 -0
  77. package/lib/bufferNode.d.mts.map +1 -0
  78. package/lib/bufferNode.mjs +53 -0
  79. package/lib/bufferNode.mjs.map +1 -0
  80. package/lib/bufferShared.d.mts +12 -0
  81. package/lib/bufferShared.d.mts.map +1 -0
  82. package/lib/bufferShared.mjs +17 -0
  83. package/lib/bufferShared.mjs.map +1 -0
  84. package/lib/client-utils-alpha.d.mts +82 -0
  85. package/lib/client-utils-beta.d.mts +73 -0
  86. package/lib/client-utils-public.d.mts +73 -0
  87. package/lib/client-utils-untrimmed.d.mts +247 -0
  88. package/lib/eventForwarder.d.mts +34 -0
  89. package/lib/eventForwarder.d.mts.map +1 -0
  90. package/lib/eventForwarder.mjs +94 -0
  91. package/lib/eventForwarder.mjs.map +1 -0
  92. package/lib/hashFileBrowser.d.mts +30 -0
  93. package/lib/hashFileBrowser.d.mts.map +1 -0
  94. package/lib/hashFileBrowser.mjs +71 -0
  95. package/lib/hashFileBrowser.mjs.map +1 -0
  96. package/lib/hashFileNode.d.mts +30 -0
  97. package/lib/hashFileNode.d.mts.map +1 -0
  98. package/lib/hashFileNode.mjs +51 -0
  99. package/lib/hashFileNode.mjs.map +1 -0
  100. package/lib/index.d.mts +12 -0
  101. package/lib/index.d.mts.map +1 -0
  102. package/lib/index.mjs +11 -0
  103. package/lib/index.mjs.map +1 -0
  104. package/lib/indexBrowser.d.mts +8 -0
  105. package/lib/indexBrowser.d.mts.map +1 -0
  106. package/lib/indexBrowser.mjs +8 -0
  107. package/lib/indexBrowser.mjs.map +1 -0
  108. package/lib/indexNode.d.mts +8 -0
  109. package/lib/indexNode.d.mts.map +1 -0
  110. package/lib/indexNode.mjs +8 -0
  111. package/lib/indexNode.mjs.map +1 -0
  112. package/lib/performanceIsomorphic.d.mts +21 -0
  113. package/lib/performanceIsomorphic.d.mts.map +1 -0
  114. package/lib/performanceIsomorphic.mjs +14 -0
  115. package/lib/performanceIsomorphic.mjs.map +1 -0
  116. package/lib/trace.d.mts +40 -0
  117. package/lib/trace.d.mts.map +1 -0
  118. package/lib/trace.mjs +32 -0
  119. package/lib/trace.mjs.map +1 -0
  120. package/lib/typedEventEmitter.d.mts +36 -0
  121. package/lib/typedEventEmitter.d.mts.map +1 -0
  122. package/lib/typedEventEmitter.mjs +22 -0
  123. package/lib/typedEventEmitter.mjs.map +1 -0
  124. package/package.json +138 -0
  125. package/prettier.config.cjs +8 -0
  126. package/src/base64Encoding.ts +45 -0
  127. package/src/bufferBrowser.ts +208 -0
  128. package/src/bufferNode.ts +89 -0
  129. package/src/bufferShared.ts +17 -0
  130. package/src/eventForwarder.ts +122 -0
  131. package/src/hashFileBrowser.ts +84 -0
  132. package/src/hashFileNode.ts +59 -0
  133. package/src/index.ts +22 -0
  134. package/src/indexBrowser.ts +14 -0
  135. package/src/indexNode.ts +14 -0
  136. package/src/performanceIsomorphic.ts +23 -0
  137. package/src/trace.ts +58 -0
  138. package/src/typedEventEmitter.ts +79 -0
  139. package/tsc-multi.test.json +4 -0
  140. package/tsconfig.json +13 -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,37 @@
1
+ # @fluid-internal/client-utils
2
+
3
+ ## 2.0.0-internal.8.0.0
4
+
5
+ Dependency updates only.
6
+
7
+ ## 2.0.0-internal.7.4.0
8
+
9
+ Dependency updates only.
10
+
11
+ ## 2.0.0-internal.7.3.0
12
+
13
+ Dependency updates only.
14
+
15
+ ## 2.0.0-internal.7.2.0
16
+
17
+ Dependency updates only.
18
+
19
+ ## 2.0.0-internal.7.1.0
20
+
21
+ Dependency updates only.
22
+
23
+ ## 2.0.0-internal.7.0.0
24
+
25
+ ### Minor Changes
26
+
27
+ - client-utils: Internal buffer encoding helpers now require 'utf8', 'utf-8', or 'base64' [871b3493dd](https://github.com/microsoft/FluidFramework/commits/871b3493dd0d7ea3a89be64998ceb6cb9021a04e)
28
+
29
+ Previously, the buffer encoding helpers 'Uint8ArrayToString', 'bufferToString', and 'IsoBuffer.toString' would accept a string argument, which was overly permissive.
30
+
31
+ The type of the 'encoding' argument has been narrow to just the supported values 'utf8', 'utf-8', or 'base64'.
32
+
33
+ ## 2.0.0-internal.6.4.0
34
+
35
+ Dependency updates only.
36
+
37
+ ## 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,75 @@
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
+ <!-- AUTO-GENERATED-CONTENT:START (README_PACKAGE_SCOPE_NOTICE) -->
7
+
8
+ **IMPORTANT: This package is intended strictly as an implementation detail of the Fluid Framework and is not intended for public consumption.**
9
+ **We make no stability guarantees regarding its APIs.**
10
+
11
+ <!-- AUTO-GENERATED-CONTENT:END -->
12
+
13
+ ## Adding code to this package
14
+
15
+ As a utility package, this package does not have a strong identity. This means that it's easy to become a "dumping
16
+ ground" for code that we think we should share but doesn't have an obvious home. We try to avoid dumping things into
17
+ utility packages, and this one is no exception.
18
+
19
+ New code should only be added to this package in rare circumstances. In most cases, the code would be better placed in a
20
+ package with a clear identity (e.g. an "events" package for shared event infrastructure) or not shared at all.
21
+
22
+ ## Requirements
23
+
24
+ This package has important requirements for the code within it.
25
+
26
+ 1. Code within this package should require some external dependencies. If it does not, then the **core-utils** package
27
+ is a better location.
28
+ 1. **All exports must be designated `@internal`.** This code is intended for use within the Fluid Framework only.
29
+ 1. This package should **only contain 'implementation' code, not type definitions.** This is the most flexible rule, and
30
+ there are some exceptions. If the type is _only_ necessary when using this package, then it is probably OK. However,
31
+ usually such types would be better placed in core-interfaces or in a package that corresponds to the purpose.
32
+
33
+ If you want to add code that does not meet these requirements, these other packages may be a better choice:
34
+
35
+ - **Types and interfaces** that are intended to be broadly shared across the client release group should be put in the
36
+ **core-interfaces** package.
37
+ - **Zero-dependency shared code** should be put in the **core-utils** package.
38
+
39
+ <!-- AUTO-GENERATED-CONTENT:START (README_DEPENDENCY_GUIDELINES_SECTION:includeHeading=TRUE) -->
40
+
41
+ <!-- prettier-ignore-start -->
42
+ <!-- NOTE: This section is automatically generated using @fluid-tools/markdown-magic. Do not update these generated contents directly. -->
43
+
44
+ ## Using Fluid Framework libraries
45
+
46
+ When taking a dependency on a Fluid Framework library, we recommend using a `^` (caret) version range, such as `^1.3.4`.
47
+ While Fluid Framework libraries may use different ranges with interdependencies between other Fluid Framework libraries,
48
+ library consumers should always prefer `^`.
49
+
50
+ Note that when depending on a library version of the form `2.0.0-internal.x.y.z`, called the Fluid internal version scheme,
51
+ you must use a `>= <` dependency range (such as `>=2.0.0-internal.x.y.z <2.0.0-internal.w.0.0` where `w` is `x+1`).
52
+ Standard `^` and `~` ranges will not work as expected.
53
+ See the [@fluid-tools/version-tools](https://github.com/microsoft/FluidFramework/blob/main/build-tools/packages/version-tools/README.md)
54
+ package for more information including tools to convert between version schemes.
55
+
56
+ <!-- prettier-ignore-end -->
57
+
58
+ <!-- AUTO-GENERATED-CONTENT:END -->
59
+
60
+ <!-- AUTO-GENERATED-CONTENT:START (README_TRADEMARK_SECTION:includeHeading=TRUE) -->
61
+
62
+ <!-- prettier-ignore-start -->
63
+ <!-- NOTE: This section is automatically generated using @fluid-tools/markdown-magic. Do not update these generated contents directly. -->
64
+
65
+ ## Trademark
66
+
67
+ This project may contain Microsoft trademarks or logos for Microsoft projects, products, or services.
68
+
69
+ Use of these trademarks or logos must follow Microsoft's [Trademark & Brand Guidelines](https://www.microsoft.com/en-us/legal/intellectualproperty/trademarks/usage/general).
70
+
71
+ Use of Microsoft trademarks or logos in modified versions of this project must not cause confusion or imply Microsoft sponsorship.
72
+
73
+ <!-- prettier-ignore-end -->
74
+
75
+ <!-- AUTO-GENERATED-CONTENT:END -->
@@ -0,0 +1,4 @@
1
+ {
2
+ "$schema": "https://developer.microsoft.com/json-schemas/api-extractor/v7/api-extractor.schema.json",
3
+ "extends": "../../../common/build/build-common/api-extractor-lint.json"
4
+ }
@@ -0,0 +1,4 @@
1
+ {
2
+ "$schema": "https://developer.microsoft.com/json-schemas/api-extractor/v7/api-extractor.schema.json",
3
+ "extends": "../../../common/build/build-common/api-extractor-base.json"
4
+ }
@@ -0,0 +1,130 @@
1
+ ## API Report File for "@fluid-internal/client-utils"
2
+
3
+ > Do not edit this file. It is a report generated by [API Extractor](https://api-extractor.com/).
4
+
5
+ ```ts
6
+
7
+ /// <reference types="node" />
8
+
9
+ import { EventEmitter } from 'events';
10
+ import { IDisposable } from '@fluidframework/core-interfaces';
11
+ import { IEvent } from '@fluidframework/core-interfaces';
12
+ import { IEventProvider } from '@fluidframework/core-interfaces';
13
+ import { IEventTransformer } from '@fluidframework/core-interfaces';
14
+ import { TransformedEvent } from '@fluidframework/core-interfaces';
15
+
16
+ // @internal
17
+ class Buffer_2 extends Uint8Array {
18
+ static from(value: unknown, encodingOrOffset?: unknown, length?: unknown): IsoBuffer;
19
+ // (undocumented)
20
+ static isBuffer(obj: unknown): obj is Buffer_2;
21
+ // (undocumented)
22
+ toString(encoding?: "utf8" | "utf-8" | "base64"): string;
23
+ }
24
+ export { Buffer_2 as Buffer }
25
+
26
+ // @alpha
27
+ export const bufferToString: (blob: ArrayBufferLike, encoding: "utf8" | "utf-8" | "base64") => string;
28
+
29
+ // @public
30
+ export type EventEmitterEventType = EventEmitter extends {
31
+ on(event: infer E, listener: any): any;
32
+ } ? E : never;
33
+
34
+ // @internal
35
+ export class EventForwarder<TEvent = IEvent> extends TypedEventEmitter<TEvent> implements IDisposable {
36
+ constructor(source?: EventEmitter | IEventProvider<TEvent & IEvent>);
37
+ // (undocumented)
38
+ dispose(): void;
39
+ // (undocumented)
40
+ get disposed(): boolean;
41
+ // (undocumented)
42
+ protected forwardEvent(source: EventEmitter | IEventProvider<TEvent & IEvent>, ...events: string[]): void;
43
+ // (undocumented)
44
+ protected static isEmitterEvent(event: string): boolean;
45
+ // (undocumented)
46
+ protected unforwardEvent(source: EventEmitter | IEventProvider<TEvent & IEvent>, ...events: string[]): void;
47
+ }
48
+
49
+ // @internal
50
+ export const fromBase64ToUtf8: (input: string) => string;
51
+
52
+ // @internal
53
+ export const fromUtf8ToBase64: (input: string) => string;
54
+
55
+ // @internal
56
+ export function gitHashFile(file: IsoBuffer): Promise<string>;
57
+
58
+ // @internal
59
+ export function hashFile(file: IsoBuffer, algorithm?: "SHA-1" | "SHA-256", hashEncoding?: "hex" | "base64"): Promise<string>;
60
+
61
+ // @internal (undocumented)
62
+ export const IsoBuffer: typeof Buffer_2;
63
+
64
+ // @internal (undocumented)
65
+ export type IsoBuffer = Buffer_2;
66
+
67
+ // @internal
68
+ export type IsomorphicPerformance = Partial<Performance> & Pick<Performance, "clearMarks" | "mark" | "measure" | "now">;
69
+
70
+ // @internal
71
+ export interface ITraceEvent {
72
+ readonly duration: number;
73
+ readonly tick: number;
74
+ readonly totalTimeElapsed: number;
75
+ }
76
+
77
+ // @internal
78
+ const performance_2: IsomorphicPerformance;
79
+ export { performance_2 as performance }
80
+
81
+ // @internal
82
+ export function stringToBuffer(input: string, encoding: string): ArrayBufferLike;
83
+
84
+ // @internal
85
+ export const toUtf8: (input: string, encoding: string) => string;
86
+
87
+ // @internal
88
+ export class Trace {
89
+ protected constructor(startTick: number);
90
+ // (undocumented)
91
+ protected lastTick: number;
92
+ // (undocumented)
93
+ static start(): Trace;
94
+ // (undocumented)
95
+ readonly startTick: number;
96
+ // (undocumented)
97
+ trace(): ITraceEvent;
98
+ }
99
+
100
+ // @public
101
+ export class TypedEventEmitter<TEvent> extends EventEmitter implements IEventProvider<TEvent & IEvent> {
102
+ constructor();
103
+ // (undocumented)
104
+ readonly addListener: TypedEventTransform<this, TEvent>;
105
+ // (undocumented)
106
+ readonly off: TypedEventTransform<this, TEvent>;
107
+ // (undocumented)
108
+ readonly on: TypedEventTransform<this, TEvent>;
109
+ // (undocumented)
110
+ readonly once: TypedEventTransform<this, TEvent>;
111
+ // (undocumented)
112
+ readonly prependListener: TypedEventTransform<this, TEvent>;
113
+ // (undocumented)
114
+ readonly prependOnceListener: TypedEventTransform<this, TEvent>;
115
+ // (undocumented)
116
+ readonly removeListener: TypedEventTransform<this, TEvent>;
117
+ }
118
+
119
+ // @public (undocumented)
120
+ export type TypedEventTransform<TThis, TEvent> = TransformedEvent<TThis, "newListener" | "removeListener", Parameters<(event: string, listener: (...args: any[]) => void) => void>> & IEventTransformer<TThis, TEvent & IEvent> & TransformedEvent<TThis, EventEmitterEventType, any[]>;
121
+
122
+ // @internal
123
+ export function Uint8ArrayToArrayBuffer(array: Uint8Array): ArrayBuffer;
124
+
125
+ // @internal
126
+ export function Uint8ArrayToString(arr: Uint8Array, encoding?: "utf8" | "utf-8" | "base64"): string;
127
+
128
+ // (No @packageDocumentation comment for this package)
129
+
130
+ ```
@@ -0,0 +1,46 @@
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.cjs");
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("utf8");
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
+ // eslint-disable-next-line unicorn/text-encoding-identifier-case -- this value is supported, just discouraged
37
+ case "utf-8": {
38
+ return input;
39
+ }
40
+ default: {
41
+ return indexNode_1.IsoBuffer.from(input, encoding).toString();
42
+ }
43
+ }
44
+ };
45
+ exports.toUtf8 = toUtf8;
46
+ //# sourceMappingURL=base64Encoding.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"base64Encoding.cjs","sourceRoot":"","sources":["../src/base64Encoding.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;AAEH,+CAAwC;AAExC;;;;;GAKG;AACI,MAAM,gBAAgB,GAAG,CAAC,KAAa,EAAU,EAAE,CACzD,qBAAS,CAAC,IAAI,CAAC,KAAK,EAAE,QAAQ,CAAC,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC;AADrC,QAAA,gBAAgB,oBACqB;AAElD;;;;;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,8GAA8G;QAC9G,KAAK,OAAO,CAAC,CAAC;YACb,OAAO,KAAK,CAAC;SACb;QACD,OAAO,CAAC,CAAC;YACR,OAAO,qBAAS,CAAC,IAAI,CAAC,KAAK,EAAE,QAAQ,CAAC,CAAC,QAAQ,EAAE,CAAC;SAClD;KACD;AACF,CAAC,CAAC;AAXW,QAAA,MAAM,UAWjB","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(\"utf8\");\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\t// eslint-disable-next-line unicorn/text-encoding-identifier-case -- this value is supported, just discouraged\n\t\tcase \"utf-8\": {\n\t\t\treturn input;\n\t\t}\n\t\tdefault: {\n\t\t\treturn IsoBuffer.from(input, encoding).toString();\n\t\t}\n\t}\n};\n"]}
@@ -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,MACA,CAAC;AAElD;;;;;GAKG;AACH,eAAO,MAAM,gBAAgB,UAAW,MAAM,KAAG,MACA,CAAC;AAElD;;;;;;;GAOG;AACH,eAAO,MAAM,MAAM,UAAW,MAAM,YAAY,MAAM,KAAG,MAWxD,CAAC"}
@@ -0,0 +1,212 @@
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
+ var desc = Object.getOwnPropertyDescriptor(m, k);
9
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
10
+ desc = { enumerable: true, get: function() { return m[k]; } };
11
+ }
12
+ Object.defineProperty(o, k2, desc);
13
+ }) : (function(o, m, k, k2) {
14
+ if (k2 === undefined) k2 = k;
15
+ o[k2] = m[k];
16
+ }));
17
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
18
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
19
+ }) : function(o, v) {
20
+ o["default"] = v;
21
+ });
22
+ var __importStar = (this && this.__importStar) || function (mod) {
23
+ if (mod && mod.__esModule) return mod;
24
+ var result = {};
25
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
26
+ __setModuleDefault(result, mod);
27
+ return result;
28
+ };
29
+ Object.defineProperty(exports, "__esModule", { value: true });
30
+ exports.IsoBuffer = exports.isArrayBuffer = exports.bufferToString = exports.stringToBuffer = exports.Uint8ArrayToString = void 0;
31
+ const base64js = __importStar(require("base64-js"));
32
+ /**
33
+ * Converts a Uint8Array to a string of the provided encoding
34
+ * Useful when the array might be an {@link IsoBuffer}.
35
+ *
36
+ * @param arr - The array to convert.
37
+ * @param encoding - Optional target encoding; only "utf8" and "base64" are
38
+ * supported, with "utf8" being default.
39
+ * @returns The converted string.
40
+ *
41
+ * @internal
42
+ */
43
+ function Uint8ArrayToString(arr,
44
+ // eslint-disable-next-line unicorn/text-encoding-identifier-case -- this value is supported, just discouraged
45
+ encoding) {
46
+ switch (encoding) {
47
+ case "base64": {
48
+ return base64js.fromByteArray(arr);
49
+ }
50
+ case "utf8":
51
+ // eslint-disable-next-line unicorn/text-encoding-identifier-case -- this value is supported, just discouraged
52
+ case "utf-8":
53
+ case undefined: {
54
+ return new TextDecoder().decode(arr);
55
+ }
56
+ default: {
57
+ throw new Error("invalid/unsupported encoding");
58
+ }
59
+ }
60
+ }
61
+ exports.Uint8ArrayToString = Uint8ArrayToString;
62
+ /**
63
+ * Converts a {@link https://en.wikipedia.org/wiki/Base64 | base64} or
64
+ * {@link https://en.wikipedia.org/wiki/UTF-8 | utf-8} string to array buffer.
65
+ *
66
+ * @param encoding - The input string's encoding.
67
+ *
68
+ * @internal
69
+ */
70
+ const stringToBuffer = (input, encoding) => IsoBuffer.from(input, encoding).buffer;
71
+ exports.stringToBuffer = stringToBuffer;
72
+ /**
73
+ * Convert binary blob to string format
74
+ *
75
+ * @param blob - the binary blob
76
+ * @param encoding - output string's encoding
77
+ * @returns the blob in string format
78
+ *
79
+ * @alpha
80
+ */
81
+ const bufferToString = (blob,
82
+ // eslint-disable-next-line unicorn/text-encoding-identifier-case -- this value is supported, just discouraged
83
+ encoding) => IsoBuffer.from(blob).toString(encoding);
84
+ exports.bufferToString = bufferToString;
85
+ /**
86
+ * Determines if an object is an array buffer.
87
+ *
88
+ * @remarks Will detect and reject TypedArrays, like Uint8Array.
89
+ * Reason - they can be viewport into Array, they can be accepted, but caller has to deal with
90
+ * math properly (i.e. Take into account byteOffset at minimum).
91
+ * For example, construction of new TypedArray can be in the form of new TypedArray(typedArray) or
92
+ * new TypedArray(buffer, byteOffset, length), but passing TypedArray will result in fist path (and
93
+ * ignoring byteOffice, length).
94
+ *
95
+ * @param obj - The object to determine if it is an ArrayBuffer.
96
+ *
97
+ * @internal
98
+ */
99
+ function isArrayBuffer(obj) {
100
+ const maybe = obj;
101
+ return (obj instanceof ArrayBuffer ||
102
+ (typeof maybe === "object" &&
103
+ maybe !== null &&
104
+ typeof maybe.byteLength === "number" &&
105
+ typeof maybe.slice === "function" &&
106
+ maybe.byteOffset === undefined &&
107
+ maybe.buffer === undefined));
108
+ }
109
+ exports.isArrayBuffer = isArrayBuffer;
110
+ /**
111
+ * Minimal implementation of Buffer for our usages in the browser environment.
112
+ *
113
+ * @internal
114
+ */
115
+ class IsoBuffer extends Uint8Array {
116
+ /**
117
+ * Convert the buffer to a string.
118
+ * Only supports encoding the whole string (unlike the Node Buffer equivalent)
119
+ * and only utf8 and base64 encodings.
120
+ *
121
+ * @param encoding - The encoding to use.
122
+ */
123
+ // eslint-disable-next-line unicorn/text-encoding-identifier-case -- this value is supported, just discouraged
124
+ toString(encoding) {
125
+ return Uint8ArrayToString(this, encoding);
126
+ }
127
+ /**
128
+ * Static constructor
129
+ * @param value - (string | ArrayBuffer)
130
+ * @param encodingOrOffset - (string | number)
131
+ * @param length - (number)
132
+ *
133
+ * @privateRemarks TODO: Use actual types
134
+ */
135
+ // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types, @typescript-eslint/no-explicit-any
136
+ static from(value, encodingOrOffset, length) {
137
+ if (typeof value === "string") {
138
+ return IsoBuffer.fromString(value, encodingOrOffset);
139
+ // Capture any typed arrays, including Uint8Array (and thus - IsoBuffer!)
140
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
141
+ }
142
+ else if (value !== null && typeof value === "object" && isArrayBuffer(value.buffer)) {
143
+ // The version of the from function for the node buffer, which takes a buffer or typed array
144
+ // as first parameter, does not have any offset or length parameters. Those are just silently
145
+ // ignored and not taken into account
146
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-member-access
147
+ return IsoBuffer.fromArrayBuffer(value.buffer, value.byteOffset, value.byteLength);
148
+ }
149
+ else if (isArrayBuffer(value)) {
150
+ return IsoBuffer.fromArrayBuffer(value, encodingOrOffset, length);
151
+ }
152
+ else {
153
+ throw new TypeError("Input value was neither a string nor an ArrayBuffer.");
154
+ }
155
+ }
156
+ static fromArrayBuffer(arrayBuffer, byteOffset, byteLength) {
157
+ const offset = byteOffset ?? 0;
158
+ const validLength = byteLength ?? arrayBuffer.byteLength - offset;
159
+ if (offset < 0 ||
160
+ offset > arrayBuffer.byteLength ||
161
+ validLength < 0 ||
162
+ validLength + offset > arrayBuffer.byteLength) {
163
+ throw new RangeError("Invalid range specified.");
164
+ }
165
+ return new IsoBuffer(arrayBuffer, offset, validLength);
166
+ }
167
+ static fromString(str, encoding) {
168
+ switch (encoding) {
169
+ case "base64": {
170
+ const sanitizedString = this.sanitizeBase64(str);
171
+ const encoded = base64js.toByteArray(sanitizedString);
172
+ return new IsoBuffer(encoded.buffer);
173
+ }
174
+ case "utf8":
175
+ // eslint-disable-next-line unicorn/text-encoding-identifier-case -- this value is supported, just discouraged
176
+ case "utf-8":
177
+ case undefined: {
178
+ const encoded = new TextEncoder().encode(str);
179
+ return new IsoBuffer(encoded.buffer);
180
+ }
181
+ default: {
182
+ throw new Error("invalid/unsupported encoding");
183
+ }
184
+ }
185
+ }
186
+ static isBuffer(obj) {
187
+ throw new Error("unimplemented");
188
+ }
189
+ /**
190
+ * Sanitize a base64 string to provide to base64-js library.
191
+ * {@link https://www.npmjs.com/package/base64-js} is not as tolerant of the same malformed base64 as Node'
192
+ * Buffer is.
193
+ */
194
+ static sanitizeBase64(str) {
195
+ let sanitizedStr = str;
196
+ // Remove everything after padding - Node buffer ignores everything
197
+ // after any padding whereas base64-js does not
198
+ sanitizedStr = sanitizedStr.split("=")[0];
199
+ // Remove invalid characters - Node buffer strips invalid characters
200
+ // whereas base64-js replaces them with "A"
201
+ sanitizedStr = sanitizedStr.replace(/[^\w+-/]/g, "");
202
+ // Check for missing padding - Node buffer tolerates missing padding
203
+ // whereas base64-js does not
204
+ if (sanitizedStr.length % 4 !== 0) {
205
+ const paddingArray = ["", "===", "==", "="];
206
+ sanitizedStr += paddingArray[sanitizedStr.length % 4];
207
+ }
208
+ return sanitizedStr;
209
+ }
210
+ }
211
+ exports.IsoBuffer = IsoBuffer;
212
+ //# sourceMappingURL=bufferBrowser.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"bufferBrowser.cjs","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(\n\tarr: Uint8Array,\n\t// eslint-disable-next-line unicorn/text-encoding-identifier-case -- this value is supported, just discouraged\n\tencoding?: \"utf8\" | \"utf-8\" | \"base64\",\n): string {\n\tswitch (encoding) {\n\t\tcase \"base64\": {\n\t\t\treturn base64js.fromByteArray(arr);\n\t\t}\n\t\tcase \"utf8\":\n\t\t// eslint-disable-next-line unicorn/text-encoding-identifier-case -- this value is supported, just discouraged\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 * @alpha\n */\nexport const bufferToString = (\n\tblob: ArrayBufferLike,\n\t// eslint-disable-next-line unicorn/text-encoding-identifier-case -- this value is supported, just discouraged\n\tencoding: \"utf8\" | \"utf-8\" | \"base64\",\n): string => IsoBuffer.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\t// eslint-disable-next-line unicorn/text-encoding-identifier-case -- this value is supported, just discouraged\n\tpublic toString(encoding?: \"utf8\" | \"utf-8\" | \"base64\"): 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\t// eslint-disable-next-line unicorn/text-encoding-identifier-case -- this value is supported, just discouraged\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"]}