@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
@@ -0,0 +1,198 @@
1
+ /*!
2
+ * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+
6
+ import * as base64js from "base64-js";
7
+
8
+ /**
9
+ * Converts a Uint8Array to a string of the provided encoding
10
+ * Useful when the array might be an {@link IsoBuffer}.
11
+ *
12
+ * @param arr - The array to convert.
13
+ * @param encoding - Optional target encoding; only "utf8" and "base64" are
14
+ * supported, with "utf8" being default.
15
+ * @returns The converted string.
16
+ *
17
+ * @internal
18
+ */
19
+ export function Uint8ArrayToString(arr: Uint8Array, encoding?: string): string {
20
+ switch (encoding) {
21
+ case "base64": {
22
+ return base64js.fromByteArray(arr);
23
+ }
24
+ case "utf8":
25
+ case "utf-8":
26
+ case undefined: {
27
+ return new TextDecoder().decode(arr);
28
+ }
29
+ default: {
30
+ throw new Error("invalid/unsupported encoding");
31
+ }
32
+ }
33
+ }
34
+
35
+ /**
36
+ * Converts a {@link https://en.wikipedia.org/wiki/Base64 | base64} or
37
+ * {@link https://en.wikipedia.org/wiki/UTF-8 | utf-8} string to array buffer.
38
+ *
39
+ * @param encoding - The input string's encoding.
40
+ *
41
+ * @internal
42
+ */
43
+ export const stringToBuffer = (input: string, encoding: string): ArrayBufferLike =>
44
+ IsoBuffer.from(input, encoding).buffer;
45
+
46
+ /**
47
+ * Convert binary blob to string format
48
+ *
49
+ * @param blob - the binary blob
50
+ * @param encoding - output string's encoding
51
+ * @returns the blob in string format
52
+ *
53
+ * @internal
54
+ */
55
+ export const bufferToString = (blob: ArrayBufferLike, encoding: string): string =>
56
+ IsoBuffer.from(blob).toString(encoding);
57
+
58
+ /**
59
+ * Determines if an object is an array buffer.
60
+ *
61
+ * @remarks Will detect and reject TypedArrays, like Uint8Array.
62
+ * Reason - they can be viewport into Array, they can be accepted, but caller has to deal with
63
+ * math properly (i.e. Take into account byteOffset at minimum).
64
+ * For example, construction of new TypedArray can be in the form of new TypedArray(typedArray) or
65
+ * new TypedArray(buffer, byteOffset, length), but passing TypedArray will result in fist path (and
66
+ * ignoring byteOffice, length).
67
+ *
68
+ * @param obj - The object to determine if it is an ArrayBuffer.
69
+ *
70
+ * @internal
71
+ */
72
+ export function isArrayBuffer(obj: unknown): obj is ArrayBuffer {
73
+ const maybe = obj as (Partial<ArrayBuffer> & Partial<Uint8Array>) | undefined;
74
+ return (
75
+ obj instanceof ArrayBuffer ||
76
+ (typeof maybe === "object" &&
77
+ maybe !== null &&
78
+ typeof maybe.byteLength === "number" &&
79
+ typeof maybe.slice === "function" &&
80
+ maybe.byteOffset === undefined &&
81
+ maybe.buffer === undefined)
82
+ );
83
+ }
84
+
85
+ /**
86
+ * Minimal implementation of Buffer for our usages in the browser environment.
87
+ *
88
+ * @internal
89
+ */
90
+ export class IsoBuffer extends Uint8Array {
91
+ /**
92
+ * Convert the buffer to a string.
93
+ * Only supports encoding the whole string (unlike the Node Buffer equivalent)
94
+ * and only utf8 and base64 encodings.
95
+ *
96
+ * @param encoding - The encoding to use.
97
+ */
98
+ public toString(encoding?: string): string {
99
+ return Uint8ArrayToString(this, encoding);
100
+ }
101
+
102
+ /**
103
+ * Static constructor
104
+ * @param value - (string | ArrayBuffer)
105
+ * @param encodingOrOffset - (string | number)
106
+ * @param length - (number)
107
+ *
108
+ * @privateRemarks TODO: Use actual types
109
+ */
110
+ // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types, @typescript-eslint/no-explicit-any
111
+ static from(value: any, encodingOrOffset?: any, length?: any): IsoBuffer {
112
+ if (typeof value === "string") {
113
+ return IsoBuffer.fromString(value, encodingOrOffset as string | undefined);
114
+ // Capture any typed arrays, including Uint8Array (and thus - IsoBuffer!)
115
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
116
+ } else if (value !== null && typeof value === "object" && isArrayBuffer(value.buffer)) {
117
+ // The version of the from function for the node buffer, which takes a buffer or typed array
118
+ // as first parameter, does not have any offset or length parameters. Those are just silently
119
+ // ignored and not taken into account
120
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-member-access
121
+ return IsoBuffer.fromArrayBuffer(value.buffer, value.byteOffset, value.byteLength);
122
+ } else if (isArrayBuffer(value)) {
123
+ return IsoBuffer.fromArrayBuffer(
124
+ value,
125
+ encodingOrOffset as number | undefined,
126
+ length as number,
127
+ );
128
+ } else {
129
+ throw new TypeError("Input value was neither a string nor an ArrayBuffer.");
130
+ }
131
+ }
132
+
133
+ static fromArrayBuffer(
134
+ arrayBuffer: ArrayBuffer,
135
+ byteOffset?: number,
136
+ byteLength?: number,
137
+ ): IsoBuffer {
138
+ const offset = byteOffset ?? 0;
139
+ const validLength = byteLength ?? arrayBuffer.byteLength - offset;
140
+ if (
141
+ offset < 0 ||
142
+ offset > arrayBuffer.byteLength ||
143
+ validLength < 0 ||
144
+ validLength + offset > arrayBuffer.byteLength
145
+ ) {
146
+ throw new RangeError("Invalid range specified.");
147
+ }
148
+
149
+ return new IsoBuffer(arrayBuffer, offset, validLength);
150
+ }
151
+
152
+ static fromString(str: string, encoding?: string): IsoBuffer {
153
+ switch (encoding) {
154
+ case "base64": {
155
+ const sanitizedString = this.sanitizeBase64(str);
156
+ const encoded = base64js.toByteArray(sanitizedString);
157
+ return new IsoBuffer(encoded.buffer);
158
+ }
159
+ case "utf8":
160
+ case "utf-8":
161
+ case undefined: {
162
+ const encoded = new TextEncoder().encode(str);
163
+ return new IsoBuffer(encoded.buffer);
164
+ }
165
+ default: {
166
+ throw new Error("invalid/unsupported encoding");
167
+ }
168
+ }
169
+ }
170
+
171
+ static isBuffer(obj: unknown): boolean {
172
+ throw new Error("unimplemented");
173
+ }
174
+
175
+ /**
176
+ * Sanitize a base64 string to provide to base64-js library.
177
+ * {@link https://www.npmjs.com/package/base64-js} is not as tolerant of the same malformed base64 as Node'
178
+ * Buffer is.
179
+ */
180
+ private static sanitizeBase64(str: string): string {
181
+ let sanitizedStr = str;
182
+ // Remove everything after padding - Node buffer ignores everything
183
+ // after any padding whereas base64-js does not
184
+ sanitizedStr = sanitizedStr.split("=")[0];
185
+
186
+ // Remove invalid characters - Node buffer strips invalid characters
187
+ // whereas base64-js replaces them with "A"
188
+ sanitizedStr = sanitizedStr.replace(/[^\w+-/]/g, "");
189
+
190
+ // Check for missing padding - Node buffer tolerates missing padding
191
+ // whereas base64-js does not
192
+ if (sanitizedStr.length % 4 !== 0) {
193
+ const paddingArray = ["", "===", "==", "="];
194
+ sanitizedStr += paddingArray[sanitizedStr.length % 4];
195
+ }
196
+ return sanitizedStr;
197
+ }
198
+ }
@@ -0,0 +1,80 @@
1
+ /*!
2
+ * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+
6
+ /**
7
+ * Declare the subset of Buffer functionality we want to make available instead of
8
+ * exposing the entirely of Node's typings. This should match the public interface
9
+ * of the browser implementation, so any changes made in one should be made in both.
10
+ *
11
+ * @public
12
+ */
13
+ export declare class Buffer extends Uint8Array {
14
+ toString(encoding?: string): string;
15
+
16
+ /**
17
+ * Static constructor
18
+ *
19
+ * @param value - (string | ArrayBuffer).
20
+ * @param encodingOrOffset - (string | number).
21
+ * @param length - (number).
22
+ *
23
+ * @privateRemarks TODO: Use actual types
24
+ */
25
+ static from(value: unknown, encodingOrOffset?: unknown, length?: unknown): IsoBuffer;
26
+ static isBuffer(obj: unknown): obj is Buffer;
27
+ }
28
+
29
+ /**
30
+ * @public
31
+ */
32
+ export const IsoBuffer = Buffer;
33
+
34
+ /**
35
+ * @public
36
+ */
37
+ export type IsoBuffer = Buffer;
38
+
39
+ /**
40
+ * Converts a Uint8Array to a string of the provided encoding.
41
+ * @remarks Useful when the array might be an IsoBuffer.
42
+ * @param arr - The array to convert.
43
+ * @param encoding - Optional target encoding; only "utf8" and "base64" are
44
+ * supported, with "utf8" being default.
45
+ * @returns The converted string.
46
+ *
47
+ * @internal
48
+ */
49
+ export function Uint8ArrayToString(arr: Uint8Array, encoding?: string): string {
50
+ // Make this check because Buffer.from(arr) will always do a buffer copy
51
+ return Buffer.isBuffer(arr) ? arr.toString(encoding) : Buffer.from(arr).toString(encoding);
52
+ }
53
+
54
+ /**
55
+ * Convert base64 or utf8 string to array buffer.
56
+ * @param encoding - The input string's encoding.
57
+ *
58
+ * @internal
59
+ */
60
+ export function stringToBuffer(input: string, encoding: string): ArrayBufferLike {
61
+ const iso = IsoBuffer.from(input, encoding);
62
+ // In a Node environment, IsoBuffer may be a Node.js Buffer. Node.js will
63
+ // pool multiple small Buffer instances into a single ArrayBuffer, in which
64
+ // case we need to slice the appropriate span of bytes.
65
+ return iso.byteLength === iso.buffer.byteLength
66
+ ? iso.buffer
67
+ : iso.buffer.slice(iso.byteOffset, iso.byteOffset + iso.byteLength);
68
+ }
69
+
70
+ /**
71
+ * Convert binary blob to string format
72
+ *
73
+ * @param blob - The binary blob
74
+ * @param encoding - Output string's encoding
75
+ * @returns The blob in string format
76
+ *
77
+ * @internal
78
+ */
79
+ export const bufferToString = (blob: ArrayBufferLike, encoding: string): string =>
80
+ IsoBuffer.from(blob).toString(encoding);
@@ -0,0 +1,17 @@
1
+ /*!
2
+ * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+
6
+ /**
7
+ * Converts a Uint8Array array to an ArrayBuffer.
8
+ * @param array - Array to convert to ArrayBuffer.
9
+ *
10
+ * @internal
11
+ */
12
+ export function Uint8ArrayToArrayBuffer(array: Uint8Array): ArrayBuffer {
13
+ if (array.byteOffset === 0 && array.byteLength === array.buffer.byteLength) {
14
+ return array.buffer;
15
+ }
16
+ return array.buffer.slice(array.byteOffset, array.byteOffset + array.byteLength);
17
+ }
@@ -0,0 +1,120 @@
1
+ /*!
2
+ * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+
6
+ import { EventEmitter } from "events";
7
+ import { IDisposable, IEvent, IEventProvider } from "@fluidframework/core-interfaces";
8
+ import { TypedEventEmitter } from "./typedEventEmitter";
9
+
10
+ /**
11
+ * Base class used for forwarding events from a source EventEmitter.
12
+ * This can be useful when all arbitrary listeners need to be removed,
13
+ * but the primary source needs to stay intact.
14
+ *
15
+ * @internal
16
+ */
17
+ export class EventForwarder<TEvent = IEvent>
18
+ extends TypedEventEmitter<TEvent>
19
+ implements IDisposable
20
+ {
21
+ protected static isEmitterEvent(event: string): boolean {
22
+ return (
23
+ event === EventForwarder.newListenerEvent ||
24
+ event === EventForwarder.removeListenerEvent
25
+ );
26
+ }
27
+
28
+ private static readonly newListenerEvent = "newListener";
29
+ private static readonly removeListenerEvent = "removeListener";
30
+
31
+ /**
32
+ * {@inheritDoc @fluidframework/core-interfaces#IDisposable.disposed}
33
+ */
34
+ public get disposed(): boolean {
35
+ return this.isDisposed;
36
+ }
37
+ private isDisposed: boolean = false;
38
+
39
+ private readonly forwardingEvents = new Map<
40
+ string,
41
+ Map<EventEmitter | IEventProvider<TEvent & IEvent>, () => void>
42
+ >();
43
+
44
+ constructor(source?: EventEmitter | IEventProvider<TEvent & IEvent>) {
45
+ super();
46
+ if (source !== undefined) {
47
+ // NewListener event is raised whenever someone starts listening to this events, so
48
+ // we keep track of events being listened to, and start forwarding from the source
49
+ // event emitter per event listened to on this
50
+ const removeListenerHandler = (event: string): void =>
51
+ this.unforwardEvent(source, event);
52
+ const newListenerHandler = (event: string): void => this.forwardEvent(source, event);
53
+ this.on(EventForwarder.removeListenerEvent, removeListenerHandler);
54
+ this.on(EventForwarder.newListenerEvent, newListenerHandler);
55
+ }
56
+ }
57
+
58
+ /**
59
+ * {@inheritDoc @fluidframework/core-interfaces#IDisposable.dispose}
60
+ */
61
+ public dispose(): void {
62
+ this.isDisposed = true;
63
+ for (const listenerRemovers of this.forwardingEvents.values()) {
64
+ for (const listenerRemover of listenerRemovers.values()) {
65
+ try {
66
+ listenerRemover();
67
+ } catch {
68
+ // Should be fine because of removeAllListeners below
69
+ }
70
+ }
71
+ }
72
+ this.removeAllListeners();
73
+ this.forwardingEvents.clear();
74
+ }
75
+
76
+ protected forwardEvent(
77
+ source: EventEmitter | IEventProvider<TEvent & IEvent>,
78
+ ...events: string[]
79
+ ): void {
80
+ for (const event of events) {
81
+ if (
82
+ source !== undefined &&
83
+ event !== undefined &&
84
+ !EventForwarder.isEmitterEvent(event)
85
+ ) {
86
+ let sources = this.forwardingEvents.get(event);
87
+ if (sources === undefined) {
88
+ sources = new Map();
89
+ this.forwardingEvents.set(event, sources);
90
+ }
91
+ if (!sources.has(source)) {
92
+ const listener = (...args: unknown[]): boolean => this.emit(event, ...args);
93
+ sources.set(source, () => source.off(event, listener));
94
+ source.on(event, listener);
95
+ }
96
+ }
97
+ }
98
+ }
99
+
100
+ protected unforwardEvent(
101
+ source: EventEmitter | IEventProvider<TEvent & IEvent>,
102
+ ...events: string[]
103
+ ): void {
104
+ for (const event of events) {
105
+ if (event !== undefined && !EventForwarder.isEmitterEvent(event)) {
106
+ const sources = this.forwardingEvents.get(event);
107
+ if (sources?.has(source) === true && this.listenerCount(event) === 0) {
108
+ const listenerRemover = sources.get(source);
109
+ if (listenerRemover !== undefined) {
110
+ listenerRemover();
111
+ }
112
+ sources.delete(source);
113
+ if (sources.size === 0) {
114
+ this.forwardingEvents.delete(event);
115
+ }
116
+ }
117
+ }
118
+ }
119
+ }
120
+ }
@@ -0,0 +1,84 @@
1
+ /*!
2
+ * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+
6
+ import * as base64js from "base64-js";
7
+ import { IsoBuffer } from "./bufferBrowser";
8
+
9
+ async function digestBuffer(file: IsoBuffer, algorithm: "SHA-1" | "SHA-256"): Promise<Uint8Array> {
10
+ const hash = await crypto.subtle.digest(algorithm, file);
11
+ return new Uint8Array(hash);
12
+ }
13
+
14
+ function encodeDigest(hashArray: Uint8Array, encoding: "hex" | "base64"): string {
15
+ // eslint-disable-next-line default-case
16
+ switch (encoding) {
17
+ case "hex": {
18
+ const hashHex = Array.prototype.map
19
+ .call(hashArray, (byte) => {
20
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
21
+ return byte.toString(16).padStart(2, "0") as string;
22
+ })
23
+ .join("");
24
+ return hashHex;
25
+ }
26
+ case "base64": {
27
+ return base64js.fromByteArray(hashArray);
28
+ }
29
+ }
30
+ }
31
+
32
+ /**
33
+ * Hash a file. Consistent within a session, but should not be persisted and
34
+ * is not consistent with git.
35
+ * If called under an insecure context for a browser, this will fallback to
36
+ * using the node implementation.
37
+ *
38
+ * @param file - The contents of the file in a buffer.
39
+ * @param algorithm - The hash algorithm to use, artificially constrained by what is used internally.
40
+ * @param hashEncoding - The encoding of the returned hash, also artificially constrained.
41
+ * @returns The hash of the content of the buffer.
42
+ *
43
+ * @internal
44
+ */
45
+ export async function hashFile(
46
+ file: IsoBuffer,
47
+ algorithm: "SHA-1" | "SHA-256" = "SHA-1",
48
+ hashEncoding: "hex" | "base64" = "hex",
49
+ ): Promise<string> {
50
+ // Handle insecure contexts (e.g. running with local services)
51
+ // by deferring to Node version, which uses a hash polyfill
52
+ // When packed, this chunk will show as "FluidFramework-HashFallback" separately
53
+ // from the main chunk and will be of non-trivial size. It will not be served
54
+ // under normal circumstances.
55
+ if (crypto.subtle === undefined) {
56
+ return import(
57
+ /* webpackChunkName: "FluidFramework-HashFallback" */
58
+ "./hashFileNode"
59
+ ).then(async (m) => m.hashFile(file, algorithm, hashEncoding));
60
+ }
61
+
62
+ // This is split up this way to facilitate testing (see the test for more info)
63
+ const hashArray = await digestBuffer(file, algorithm);
64
+ return encodeDigest(hashArray, hashEncoding);
65
+ }
66
+
67
+ /**
68
+ * Create a github hash (Github hashes the string with blob and size)
69
+ * Must be called under secure context for browsers
70
+ *
71
+ * @param file - The contents of the file in a buffer
72
+ * @returns The sha1 hash of the content of the buffer with the `blob` prefix and size
73
+ *
74
+ * @internal
75
+ */
76
+ export async function gitHashFile(file: IsoBuffer): Promise<string> {
77
+ const size = file.byteLength;
78
+ // eslint-disable-next-line unicorn/prefer-code-point
79
+ const filePrefix = `blob ${size.toString()}${String.fromCharCode(0)}`;
80
+ const hashBuffer = IsoBuffer.from(filePrefix + file.toString());
81
+
82
+ // hashFile uses sha1; if that changes this will need to change too
83
+ return hashFile(hashBuffer);
84
+ }
@@ -0,0 +1,66 @@
1
+ /*!
2
+ * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+
6
+ // eslint-disable-next-line import/no-internal-modules
7
+ import sha1 from "sha.js/sha1";
8
+ // eslint-disable-next-line import/no-internal-modules
9
+ import sha256 from "sha.js/sha256";
10
+
11
+ import type { IsoBuffer } from "./bufferNode";
12
+
13
+ /**
14
+ * Hash a file. Consistent within a session, but should not be persisted and
15
+ * is not consistent with git.
16
+ * If called under an insecure context for a browser, this will fallback to
17
+ * using the node implementation.
18
+ *
19
+ * @param file - The contents of the file in a buffer.
20
+ * @param algorithm - The hash algorithm to use, artificially constrained by what is used internally.
21
+ * @param hashEncoding - The encoding of the returned hash, also artificially constrained.
22
+ * @returns The hash of the content of the buffer.
23
+ *
24
+ * @internal
25
+ */
26
+ export async function hashFile(
27
+ file: IsoBuffer,
28
+ algorithm: "SHA-1" | "SHA-256" = "SHA-1",
29
+ hashEncoding: "hex" | "base64" = "hex",
30
+ ): Promise<string> {
31
+ let engine;
32
+ // eslint-disable-next-line default-case
33
+ switch (algorithm) {
34
+ case "SHA-1": {
35
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-call
36
+ engine = new sha1();
37
+ break;
38
+ }
39
+ case "SHA-256": {
40
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-call
41
+ engine = new sha256();
42
+ break;
43
+ }
44
+ }
45
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
46
+ return engine.update(file).digest(hashEncoding) as string;
47
+ }
48
+
49
+ /**
50
+ * Create a github hash (Github hashes the string with blob and size)
51
+ * Must be called under secure context for browsers
52
+ *
53
+ * @param file - The contents of the file in a buffer
54
+ * @returns The sha1 hash of the content of the buffer with the `blob` prefix and size
55
+ *
56
+ * @internal
57
+ */
58
+ export async function gitHashFile(file: IsoBuffer): Promise<string> {
59
+ const size = file.byteLength;
60
+ // eslint-disable-next-line unicorn/prefer-code-point
61
+ const filePrefix = `blob ${size.toString()}${String.fromCharCode(0)}`;
62
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-call
63
+ const engine = new sha1();
64
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
65
+ return engine.update(filePrefix).update(file).digest("hex") as string;
66
+ }
package/src/index.ts ADDED
@@ -0,0 +1,22 @@
1
+ /*!
2
+ * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+
6
+ export { fromBase64ToUtf8, fromUtf8ToBase64, toUtf8 } from "./base64Encoding";
7
+ export { Uint8ArrayToArrayBuffer } from "./bufferShared";
8
+ export { EventForwarder } from "./eventForwarder";
9
+ /**
10
+ * NOTE: This export is remapped to export from "./indexBrowser" in browser environments via package.json.
11
+ * Because the two files don't have fully isomorphic exports, using named exports for the full API surface
12
+ * is problematic if that named export includes values not in their intersection.
13
+ *
14
+ * In a future breaking change of client-utils, we could use a named export for their intersection if we
15
+ * desired.
16
+ */
17
+ // eslint-disable-next-line no-restricted-syntax
18
+ export * from "./indexNode";
19
+ export { IsomorphicPerformance } from "./performanceIsomorphic";
20
+ // export { IRange, IRangeTrackerSnapshot, RangeTracker } from "./rangeTracker";
21
+ export { ITraceEvent, Trace } from "./trace";
22
+ export { EventEmitterEventType, TypedEventEmitter, TypedEventTransform } from "./typedEventEmitter";
@@ -0,0 +1,14 @@
1
+ /*!
2
+ * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+
6
+ export {
7
+ bufferToString,
8
+ isArrayBuffer,
9
+ IsoBuffer,
10
+ stringToBuffer,
11
+ Uint8ArrayToString,
12
+ } from "./bufferBrowser";
13
+ export { gitHashFile, hashFile } from "./hashFileBrowser";
14
+ export { performance } from "./performanceBrowser";
@@ -0,0 +1,14 @@
1
+ /*!
2
+ * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+
6
+ export {
7
+ Buffer,
8
+ bufferToString,
9
+ IsoBuffer,
10
+ stringToBuffer,
11
+ Uint8ArrayToString,
12
+ } from "./bufferNode";
13
+ export { gitHashFile, hashFile } from "./hashFileNode";
14
+ export { performance } from "./performanceNode";
@@ -0,0 +1,11 @@
1
+ /*!
2
+ * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+
6
+ import { IsomorphicPerformance } from "./performanceIsomorphic";
7
+
8
+ /**
9
+ * @internal
10
+ */
11
+ export const performance: IsomorphicPerformance = globalThis.performance;
@@ -0,0 +1,13 @@
1
+ /*!
2
+ * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+
6
+ /**
7
+ * This type contains all browser performance properties as optional, and some
8
+ * of the intersecting properties of node and browser performance as required.
9
+ *
10
+ * @internal
11
+ */
12
+ export type IsomorphicPerformance = Partial<Performance> &
13
+ Pick<Performance, "clearMarks" | "mark" | "measure" | "now">;
@@ -0,0 +1,13 @@
1
+ /*!
2
+ * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+
6
+ // eslint-disable-next-line import/no-nodejs-modules
7
+ import { performance as nodePerformance } from "perf_hooks";
8
+ import { IsomorphicPerformance } from "./performanceIsomorphic";
9
+
10
+ /**
11
+ * @internal
12
+ */
13
+ export const performance: IsomorphicPerformance = nodePerformance;