@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.
- package/.eslintrc.js +16 -0
- package/.mocharc.js +12 -0
- package/CHANGELOG.md +37 -0
- package/LICENSE +21 -0
- package/README.md +75 -0
- package/api-extractor-lint.json +4 -0
- package/api-extractor.json +4 -0
- package/api-report/client-utils.api.md +130 -0
- package/dist/base64Encoding.cjs +46 -0
- package/dist/base64Encoding.cjs.map +1 -0
- package/dist/base64Encoding.d.ts +28 -0
- package/dist/base64Encoding.d.ts.map +1 -0
- package/dist/bufferBrowser.cjs +212 -0
- package/dist/bufferBrowser.cjs.map +1 -0
- package/dist/bufferBrowser.d.ts +84 -0
- package/dist/bufferBrowser.d.ts.map +1 -0
- package/dist/bufferNode.cjs +59 -0
- package/dist/bufferNode.cjs.map +1 -0
- package/dist/bufferNode.d.ts +62 -0
- package/dist/bufferNode.d.ts.map +1 -0
- package/dist/bufferShared.cjs +21 -0
- package/dist/bufferShared.cjs.map +1 -0
- package/dist/bufferShared.d.ts +12 -0
- package/dist/bufferShared.d.ts.map +1 -0
- package/dist/client-utils-alpha.d.ts +82 -0
- package/dist/client-utils-beta.d.ts +73 -0
- package/dist/client-utils-public.d.ts +73 -0
- package/dist/client-utils-untrimmed.d.ts +247 -0
- package/dist/eventForwarder.cjs +98 -0
- package/dist/eventForwarder.cjs.map +1 -0
- package/dist/eventForwarder.d.ts +34 -0
- package/dist/eventForwarder.d.ts.map +1 -0
- package/dist/hashFileBrowser.cjs +99 -0
- package/dist/hashFileBrowser.cjs.map +1 -0
- package/dist/hashFileBrowser.d.ts +30 -0
- package/dist/hashFileBrowser.d.ts.map +1 -0
- package/dist/hashFileNode.cjs +56 -0
- package/dist/hashFileNode.cjs.map +1 -0
- package/dist/hashFileNode.d.ts +30 -0
- package/dist/hashFileNode.d.ts.map +1 -0
- package/dist/index.cjs +45 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +20 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/indexBrowser.cjs +19 -0
- package/dist/indexBrowser.cjs.map +1 -0
- package/dist/indexBrowser.d.ts +8 -0
- package/dist/indexBrowser.d.ts.map +1 -0
- package/dist/indexNode.cjs +19 -0
- package/dist/indexNode.cjs.map +1 -0
- package/dist/indexNode.d.ts +8 -0
- package/dist/indexNode.d.ts.map +1 -0
- package/dist/performanceIsomorphic.cjs +17 -0
- package/dist/performanceIsomorphic.cjs.map +1 -0
- package/dist/performanceIsomorphic.d.ts +21 -0
- package/dist/performanceIsomorphic.d.ts.map +1 -0
- package/dist/trace.cjs +36 -0
- package/dist/trace.cjs.map +1 -0
- package/dist/trace.d.ts +40 -0
- package/dist/trace.d.ts.map +1 -0
- package/dist/tsdoc-metadata.json +11 -0
- package/dist/typedEventEmitter.cjs +28 -0
- package/dist/typedEventEmitter.cjs.map +1 -0
- package/dist/typedEventEmitter.d.ts +36 -0
- package/dist/typedEventEmitter.d.ts.map +1 -0
- package/jest-puppeteer.config.js +13 -0
- package/jest.config.js +20 -0
- package/lib/base64Encoding.d.mts +28 -0
- package/lib/base64Encoding.d.mts.map +1 -0
- package/lib/base64Encoding.mjs +40 -0
- package/lib/base64Encoding.mjs.map +1 -0
- package/lib/bufferBrowser.d.mts +84 -0
- package/lib/bufferBrowser.d.mts.map +1 -0
- package/lib/bufferBrowser.mjs +181 -0
- package/lib/bufferBrowser.mjs.map +1 -0
- package/lib/bufferNode.d.mts +62 -0
- package/lib/bufferNode.d.mts.map +1 -0
- package/lib/bufferNode.mjs +53 -0
- package/lib/bufferNode.mjs.map +1 -0
- package/lib/bufferShared.d.mts +12 -0
- package/lib/bufferShared.d.mts.map +1 -0
- package/lib/bufferShared.mjs +17 -0
- package/lib/bufferShared.mjs.map +1 -0
- package/lib/client-utils-alpha.d.mts +82 -0
- package/lib/client-utils-beta.d.mts +73 -0
- package/lib/client-utils-public.d.mts +73 -0
- package/lib/client-utils-untrimmed.d.mts +247 -0
- package/lib/eventForwarder.d.mts +34 -0
- package/lib/eventForwarder.d.mts.map +1 -0
- package/lib/eventForwarder.mjs +94 -0
- package/lib/eventForwarder.mjs.map +1 -0
- package/lib/hashFileBrowser.d.mts +30 -0
- package/lib/hashFileBrowser.d.mts.map +1 -0
- package/lib/hashFileBrowser.mjs +71 -0
- package/lib/hashFileBrowser.mjs.map +1 -0
- package/lib/hashFileNode.d.mts +30 -0
- package/lib/hashFileNode.d.mts.map +1 -0
- package/lib/hashFileNode.mjs +51 -0
- package/lib/hashFileNode.mjs.map +1 -0
- package/lib/index.d.mts +12 -0
- package/lib/index.d.mts.map +1 -0
- package/lib/index.mjs +11 -0
- package/lib/index.mjs.map +1 -0
- package/lib/indexBrowser.d.mts +8 -0
- package/lib/indexBrowser.d.mts.map +1 -0
- package/lib/indexBrowser.mjs +8 -0
- package/lib/indexBrowser.mjs.map +1 -0
- package/lib/indexNode.d.mts +8 -0
- package/lib/indexNode.d.mts.map +1 -0
- package/lib/indexNode.mjs +8 -0
- package/lib/indexNode.mjs.map +1 -0
- package/lib/performanceIsomorphic.d.mts +21 -0
- package/lib/performanceIsomorphic.d.mts.map +1 -0
- package/lib/performanceIsomorphic.mjs +14 -0
- package/lib/performanceIsomorphic.mjs.map +1 -0
- package/lib/trace.d.mts +40 -0
- package/lib/trace.d.mts.map +1 -0
- package/lib/trace.mjs +32 -0
- package/lib/trace.mjs.map +1 -0
- package/lib/typedEventEmitter.d.mts +36 -0
- package/lib/typedEventEmitter.d.mts.map +1 -0
- package/lib/typedEventEmitter.mjs +22 -0
- package/lib/typedEventEmitter.mjs.map +1 -0
- package/package.json +138 -0
- package/prettier.config.cjs +8 -0
- package/src/base64Encoding.ts +45 -0
- package/src/bufferBrowser.ts +208 -0
- package/src/bufferNode.ts +89 -0
- package/src/bufferShared.ts +17 -0
- package/src/eventForwarder.ts +122 -0
- package/src/hashFileBrowser.ts +84 -0
- package/src/hashFileNode.ts +59 -0
- package/src/index.ts +22 -0
- package/src/indexBrowser.ts +14 -0
- package/src/indexNode.ts +14 -0
- package/src/performanceIsomorphic.ts +23 -0
- package/src/trace.ts +58 -0
- package/src/typedEventEmitter.ts +79 -0
- package/tsc-multi.test.json +4 -0
- package/tsconfig.json +13 -0
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* Copyright (c) Microsoft Corporation and contributors. All rights reserved.
|
|
3
|
+
* Licensed under the MIT License.
|
|
4
|
+
*/
|
|
5
|
+
export { bufferToString, isArrayBuffer, IsoBuffer, stringToBuffer, Uint8ArrayToString, } from "./bufferBrowser.mjs";
|
|
6
|
+
export { gitHashFile, hashFile } from "./hashFileBrowser.mjs";
|
|
7
|
+
export { performance } from "./performanceIsomorphic.mjs";
|
|
8
|
+
//# sourceMappingURL=indexBrowser.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"indexBrowser.mjs","sourceRoot":"","sources":["../src/indexBrowser.ts"],"names":[],"mappings":"AAAA;;;GAGG;OAEI,EACN,cAAc,EACd,aAAa,EACb,SAAS,EACT,cAAc,EACd,kBAAkB,GAClB;OACM,EAAE,WAAW,EAAE,QAAQ,EAAE;OACzB,EAAE,WAAW,EAAE","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\nexport {\n\tbufferToString,\n\tisArrayBuffer,\n\tIsoBuffer,\n\tstringToBuffer,\n\tUint8ArrayToString,\n} from \"./bufferBrowser\";\nexport { gitHashFile, hashFile } from \"./hashFileBrowser\";\nexport { performance } from \"./performanceIsomorphic\";\n"]}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* Copyright (c) Microsoft Corporation and contributors. All rights reserved.
|
|
3
|
+
* Licensed under the MIT License.
|
|
4
|
+
*/
|
|
5
|
+
export { Buffer, bufferToString, IsoBuffer, stringToBuffer, Uint8ArrayToString, } from "./bufferNode.mjs";
|
|
6
|
+
export { gitHashFile, hashFile } from "./hashFileNode.mjs";
|
|
7
|
+
export { performance } from "./performanceIsomorphic.mjs";
|
|
8
|
+
//# sourceMappingURL=indexNode.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"indexNode.d.ts","sourceRoot":"","sources":["../src/indexNode.ts"],"names":[],"mappings":"AAAA;;;GAGG;OAEI,EACN,MAAM,EACN,cAAc,EACd,SAAS,EACT,cAAc,EACd,kBAAkB,GAClB;OACM,EAAE,WAAW,EAAE,QAAQ,EAAE;OACzB,EAAE,WAAW,EAAE"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* Copyright (c) Microsoft Corporation and contributors. All rights reserved.
|
|
3
|
+
* Licensed under the MIT License.
|
|
4
|
+
*/
|
|
5
|
+
export { Buffer, bufferToString, IsoBuffer, stringToBuffer, Uint8ArrayToString, } from "./bufferNode.mjs";
|
|
6
|
+
export { gitHashFile, hashFile } from "./hashFileNode.mjs";
|
|
7
|
+
export { performance } from "./performanceIsomorphic.mjs";
|
|
8
|
+
//# sourceMappingURL=indexNode.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"indexNode.mjs","sourceRoot":"","sources":["../src/indexNode.ts"],"names":[],"mappings":"AAAA;;;GAGG;OAEI,EACN,MAAM,EACN,cAAc,EACd,SAAS,EACT,cAAc,EACd,kBAAkB,GAClB;OACM,EAAE,WAAW,EAAE,QAAQ,EAAE;OACzB,EAAE,WAAW,EAAE","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\nexport {\n\tBuffer,\n\tbufferToString,\n\tIsoBuffer,\n\tstringToBuffer,\n\tUint8ArrayToString,\n} from \"./bufferNode\";\nexport { gitHashFile, hashFile } from \"./hashFileNode\";\nexport { performance } from \"./performanceIsomorphic\";\n"]}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* Copyright (c) Microsoft Corporation and contributors. All rights reserved.
|
|
3
|
+
* Licensed under the MIT License.
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* This type contains all browser performance properties as optional, and some
|
|
7
|
+
* of the intersecting properties of node and browser performance as required.
|
|
8
|
+
*
|
|
9
|
+
* @internal
|
|
10
|
+
*/
|
|
11
|
+
export type IsomorphicPerformance = Partial<Performance> & Pick<Performance, "clearMarks" | "mark" | "measure" | "now">;
|
|
12
|
+
/**
|
|
13
|
+
* This exported "performance" member masks the built-in globalThis.performance object
|
|
14
|
+
* as an IsomorphicPerformance, which hides all of its features that aren't compatible
|
|
15
|
+
* between Node and browser implementations. Anything exposed on this performance object
|
|
16
|
+
* is considered safe to use regarless of the environment it runs in.
|
|
17
|
+
*
|
|
18
|
+
* @internal
|
|
19
|
+
*/
|
|
20
|
+
export declare const performance: IsomorphicPerformance;
|
|
21
|
+
//# sourceMappingURL=performanceIsomorphic.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"performanceIsomorphic.d.ts","sourceRoot":"","sources":["../src/performanceIsomorphic.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH;;;;;GAKG;AACH,MAAM,MAAM,qBAAqB,GAAG,OAAO,CAAC,WAAW,CAAC,GACvD,IAAI,CAAC,WAAW,EAAE,YAAY,GAAG,MAAM,GAAG,SAAS,GAAG,KAAK,CAAC,CAAC;AAE9D;;;;;;;GAOG;AACH,eAAO,MAAM,WAAW,EAAE,qBAA8C,CAAC"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* Copyright (c) Microsoft Corporation and contributors. All rights reserved.
|
|
3
|
+
* Licensed under the MIT License.
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* This exported "performance" member masks the built-in globalThis.performance object
|
|
7
|
+
* as an IsomorphicPerformance, which hides all of its features that aren't compatible
|
|
8
|
+
* between Node and browser implementations. Anything exposed on this performance object
|
|
9
|
+
* is considered safe to use regarless of the environment it runs in.
|
|
10
|
+
*
|
|
11
|
+
* @internal
|
|
12
|
+
*/
|
|
13
|
+
export const performance = globalThis.performance;
|
|
14
|
+
//# sourceMappingURL=performanceIsomorphic.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"performanceIsomorphic.mjs","sourceRoot":"","sources":["../src/performanceIsomorphic.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAWH;;;;;;;GAOG;AACH,MAAM,CAAC,MAAM,WAAW,GAA0B,UAAU,CAAC,WAAW,CAAC","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\n/**\n * This type contains all browser performance properties as optional, and some\n * of the intersecting properties of node and browser performance as required.\n *\n * @internal\n */\nexport type IsomorphicPerformance = Partial<Performance> &\n\tPick<Performance, \"clearMarks\" | \"mark\" | \"measure\" | \"now\">;\n\n/**\n * This exported \"performance\" member masks the built-in globalThis.performance object\n * as an IsomorphicPerformance, which hides all of its features that aren't compatible\n * between Node and browser implementations. Anything exposed on this performance object\n * is considered safe to use regarless of the environment it runs in.\n *\n * @internal\n */\nexport const performance: IsomorphicPerformance = globalThis.performance;\n"]}
|
package/lib/trace.d.mts
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* Copyright (c) Microsoft Corporation and contributors. All rights reserved.
|
|
3
|
+
* Licensed under the MIT License.
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Helper class for tracing performance of events
|
|
7
|
+
* Time measurements are in milliseconds as a floating point with a decimal
|
|
8
|
+
*
|
|
9
|
+
* @internal
|
|
10
|
+
*/
|
|
11
|
+
export declare class Trace {
|
|
12
|
+
readonly startTick: number;
|
|
13
|
+
static start(): Trace;
|
|
14
|
+
protected lastTick: number;
|
|
15
|
+
protected constructor(startTick: number);
|
|
16
|
+
trace(): ITraceEvent;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Event in a performance trace including time elapsed.
|
|
20
|
+
*
|
|
21
|
+
* @internal
|
|
22
|
+
*/
|
|
23
|
+
export interface ITraceEvent {
|
|
24
|
+
/**
|
|
25
|
+
* Total time elapsed since the start of the Trace.
|
|
26
|
+
* Measured in milliseconds as a floating point with a decimal
|
|
27
|
+
*/
|
|
28
|
+
readonly totalTimeElapsed: number;
|
|
29
|
+
/**
|
|
30
|
+
* Time elapsed since the last trace event.
|
|
31
|
+
* Measured in milliseconds as a floating point with a decimal
|
|
32
|
+
*/
|
|
33
|
+
readonly duration: number;
|
|
34
|
+
/**
|
|
35
|
+
* This number represents a relative time which should
|
|
36
|
+
* be consistent for all trace ticks.
|
|
37
|
+
*/
|
|
38
|
+
readonly tick: number;
|
|
39
|
+
}
|
|
40
|
+
//# sourceMappingURL=trace.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"trace.d.ts","sourceRoot":"","sources":["../src/trace.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAIH;;;;;GAKG;AACH,qBAAa,KAAK;aAOqB,SAAS,EAAE,MAAM;WANzC,KAAK,IAAI,KAAK;IAK5B,SAAS,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC3B,SAAS,aAA6B,SAAS,EAAE,MAAM;IAIhD,KAAK,IAAI,WAAW;CAU3B;AAED;;;;GAIG;AACH,MAAM,WAAW,WAAW;IAC3B;;;OAGG;IACH,QAAQ,CAAC,gBAAgB,EAAE,MAAM,CAAC;IAClC;;;OAGG;IACH,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC1B;;;OAGG;IACH,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;CACtB"}
|
package/lib/trace.mjs
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* Copyright (c) Microsoft Corporation and contributors. All rights reserved.
|
|
3
|
+
* Licensed under the MIT License.
|
|
4
|
+
*/
|
|
5
|
+
import { performance } from "./indexNode.mjs";
|
|
6
|
+
/**
|
|
7
|
+
* Helper class for tracing performance of events
|
|
8
|
+
* Time measurements are in milliseconds as a floating point with a decimal
|
|
9
|
+
*
|
|
10
|
+
* @internal
|
|
11
|
+
*/
|
|
12
|
+
export class Trace {
|
|
13
|
+
static start() {
|
|
14
|
+
const startTick = performance.now();
|
|
15
|
+
return new Trace(startTick);
|
|
16
|
+
}
|
|
17
|
+
constructor(startTick) {
|
|
18
|
+
this.startTick = startTick;
|
|
19
|
+
this.lastTick = startTick;
|
|
20
|
+
}
|
|
21
|
+
trace() {
|
|
22
|
+
const tick = performance.now();
|
|
23
|
+
const event = {
|
|
24
|
+
totalTimeElapsed: tick - this.startTick,
|
|
25
|
+
duration: tick - this.lastTick,
|
|
26
|
+
tick,
|
|
27
|
+
};
|
|
28
|
+
this.lastTick = tick;
|
|
29
|
+
return event;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
//# sourceMappingURL=trace.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"trace.mjs","sourceRoot":"","sources":["../src/trace.ts"],"names":[],"mappings":"AAAA;;;GAGG;OAEI,EAAE,WAAW,EAAE;AAEtB;;;;;GAKG;AACH,MAAM,OAAO,KAAK;IACV,MAAM,CAAC,KAAK;QAClB,MAAM,SAAS,GAAG,WAAW,CAAC,GAAG,EAAE,CAAC;QACpC,OAAO,IAAI,KAAK,CAAC,SAAS,CAAC,CAAC;IAC7B,CAAC;IAGD,YAAsC,SAAiB;QAAjB,cAAS,GAAT,SAAS,CAAQ;QACtD,IAAI,CAAC,QAAQ,GAAG,SAAS,CAAC;IAC3B,CAAC;IAEM,KAAK;QACX,MAAM,IAAI,GAAG,WAAW,CAAC,GAAG,EAAE,CAAC;QAC/B,MAAM,KAAK,GAAG;YACb,gBAAgB,EAAE,IAAI,GAAG,IAAI,CAAC,SAAS;YACvC,QAAQ,EAAE,IAAI,GAAG,IAAI,CAAC,QAAQ;YAC9B,IAAI;SACJ,CAAC;QACF,IAAI,CAAC,QAAQ,GAAG,IAAI,CAAC;QACrB,OAAO,KAAK,CAAC;IACd,CAAC;CACD","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\nimport { performance } from \"./indexNode\";\n\n/**\n * Helper class for tracing performance of events\n * Time measurements are in milliseconds as a floating point with a decimal\n *\n * @internal\n */\nexport class Trace {\n\tpublic static start(): Trace {\n\t\tconst startTick = performance.now();\n\t\treturn new Trace(startTick);\n\t}\n\n\tprotected lastTick: number;\n\tprotected constructor(public readonly startTick: number) {\n\t\tthis.lastTick = startTick;\n\t}\n\n\tpublic trace(): ITraceEvent {\n\t\tconst tick = performance.now();\n\t\tconst event = {\n\t\t\ttotalTimeElapsed: tick - this.startTick,\n\t\t\tduration: tick - this.lastTick,\n\t\t\ttick,\n\t\t};\n\t\tthis.lastTick = tick;\n\t\treturn event;\n\t}\n}\n\n/**\n * Event in a performance trace including time elapsed.\n *\n * @internal\n */\nexport interface ITraceEvent {\n\t/**\n\t * Total time elapsed since the start of the Trace.\n\t * Measured in milliseconds as a floating point with a decimal\n\t */\n\treadonly totalTimeElapsed: number;\n\t/**\n\t * Time elapsed since the last trace event.\n\t * Measured in milliseconds as a floating point with a decimal\n\t */\n\treadonly duration: number;\n\t/**\n\t * This number represents a relative time which should\n\t * be consistent for all trace ticks.\n\t */\n\treadonly tick: number;\n}\n"]}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* Copyright (c) Microsoft Corporation and contributors. All rights reserved.
|
|
3
|
+
* Licensed under the MIT License.
|
|
4
|
+
*/
|
|
5
|
+
/// <reference types="node" />
|
|
6
|
+
import { EventEmitter } from "events";
|
|
7
|
+
import { IEvent, TransformedEvent, IEventTransformer, IEventProvider } from "@fluidframework/core-interfaces";
|
|
8
|
+
/**
|
|
9
|
+
* The event emitter polyfill and the node event emitter have different event types:
|
|
10
|
+
* string | symbol vs. string | number
|
|
11
|
+
*
|
|
12
|
+
* This type allow us to correctly handle either type
|
|
13
|
+
* @public
|
|
14
|
+
*/
|
|
15
|
+
export type EventEmitterEventType = EventEmitter extends {
|
|
16
|
+
on(event: infer E, listener: any): any;
|
|
17
|
+
} ? E : never;
|
|
18
|
+
/**
|
|
19
|
+
* @public
|
|
20
|
+
*/
|
|
21
|
+
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[]>;
|
|
22
|
+
/**
|
|
23
|
+
* Event Emitter helper class the supports emitting typed events
|
|
24
|
+
* @public
|
|
25
|
+
*/
|
|
26
|
+
export declare class TypedEventEmitter<TEvent> extends EventEmitter implements IEventProvider<TEvent & IEvent> {
|
|
27
|
+
constructor();
|
|
28
|
+
readonly addListener: TypedEventTransform<this, TEvent>;
|
|
29
|
+
readonly on: TypedEventTransform<this, TEvent>;
|
|
30
|
+
readonly once: TypedEventTransform<this, TEvent>;
|
|
31
|
+
readonly prependListener: TypedEventTransform<this, TEvent>;
|
|
32
|
+
readonly prependOnceListener: TypedEventTransform<this, TEvent>;
|
|
33
|
+
readonly removeListener: TypedEventTransform<this, TEvent>;
|
|
34
|
+
readonly off: TypedEventTransform<this, TEvent>;
|
|
35
|
+
}
|
|
36
|
+
//# sourceMappingURL=typedEventEmitter.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"typedEventEmitter.d.ts","sourceRoot":"","sources":["../src/typedEventEmitter.ts"],"names":[],"mappings":"AAAA;;;GAGG;;OAII,EAAE,YAAY,EAAE,MAAM,QAAQ;OAC9B,EACN,MAAM,EACN,gBAAgB,EAChB,iBAAiB,EACjB,cAAc,EACd,MAAM,iCAAiC;AAExC;;;;;;GAMG;AAEH,MAAM,MAAM,qBAAqB,GAAG,YAAY,SAAS;IAAE,EAAE,CAAC,KAAK,EAAE,MAAM,CAAC,EAAE,QAAQ,EAAE,GAAG,OAAC;CAAE,GAC3F,CAAC,GACD,KAAK,CAAC;AAET;;GAEG;AACH,MAAM,MAAM,mBAAmB,CAAC,KAAK,EAAE,MAAM,IAK5C,gBAAgB,CACf,KAAK,EACL,aAAa,GAAG,gBAAgB,EAEhC,UAAU,CAAC,CAAC,KAAK,EAAE,MAAM,EAAE,QAAQ,EAAE,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,IAAI,KAAK,IAAI,CAAC,CACvE,GAEA,iBAAiB,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAAC,GAGzC,gBAAgB,CAAC,KAAK,EAAE,qBAAqB,EAAE,GAAG,EAAE,CAAC,CAAC;AAExD;;;GAGG;AACH,qBAAa,iBAAiB,CAAC,MAAM,CACpC,SAAQ,YACR,YAAW,cAAc,CAAC,MAAM,GAAG,MAAM,CAAC;;IAkB1C,QAAQ,CAAC,WAAW,EAAE,mBAAmB,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC;IACxD,QAAQ,CAAC,EAAE,EAAE,mBAAmB,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC;IAC/C,QAAQ,CAAC,IAAI,EAAE,mBAAmB,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC;IACjD,QAAQ,CAAC,eAAe,EAAE,mBAAmB,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC;IAC5D,QAAQ,CAAC,mBAAmB,EAAE,mBAAmB,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC;IAChE,QAAQ,CAAC,cAAc,EAAE,mBAAmB,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC;IAC3D,QAAQ,CAAC,GAAG,EAAE,mBAAmB,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC;CAChD"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* Copyright (c) Microsoft Corporation and contributors. All rights reserved.
|
|
3
|
+
* Licensed under the MIT License.
|
|
4
|
+
*/
|
|
5
|
+
import { EventEmitter } from "events";
|
|
6
|
+
/**
|
|
7
|
+
* Event Emitter helper class the supports emitting typed events
|
|
8
|
+
* @public
|
|
9
|
+
*/
|
|
10
|
+
export class TypedEventEmitter extends EventEmitter {
|
|
11
|
+
constructor() {
|
|
12
|
+
super();
|
|
13
|
+
this.addListener = super.addListener.bind(this);
|
|
14
|
+
this.on = super.on.bind(this);
|
|
15
|
+
this.once = super.once.bind(this);
|
|
16
|
+
this.prependListener = super.prependListener.bind(this);
|
|
17
|
+
this.prependOnceListener = super.prependOnceListener.bind(this);
|
|
18
|
+
this.removeListener = super.removeListener.bind(this);
|
|
19
|
+
this.off = super.off.bind(this);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
//# sourceMappingURL=typedEventEmitter.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"typedEventEmitter.mjs","sourceRoot":"","sources":["../src/typedEventEmitter.ts"],"names":[],"mappings":"AAAA;;;GAGG;OAII,EAAE,YAAY,EAAE,MAAM,QAAQ;AAwCrC;;;GAGG;AACH,MAAM,OAAO,iBACZ,SAAQ,YAAY;IAGpB;QACC,KAAK,EAAE,CAAC;QACR,IAAI,CAAC,WAAW,GAAG,KAAK,CAAC,WAAW,CAAC,IAAI,CAAC,IAAI,CAAsC,CAAC;QACrF,IAAI,CAAC,EAAE,GAAG,KAAK,CAAC,EAAE,CAAC,IAAI,CAAC,IAAI,CAAsC,CAAC;QACnE,IAAI,CAAC,IAAI,GAAG,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAsC,CAAC;QACvE,IAAI,CAAC,eAAe,GAAG,KAAK,CAAC,eAAe,CAAC,IAAI,CAAC,IAAI,CAGrD,CAAC;QACF,IAAI,CAAC,mBAAmB,GAAG,KAAK,CAAC,mBAAmB,CAAC,IAAI,CAAC,IAAI,CAG7D,CAAC;QACF,IAAI,CAAC,cAAc,GAAG,KAAK,CAAC,cAAc,CAAC,IAAI,CAAC,IAAI,CAAsC,CAAC;QAC3F,IAAI,CAAC,GAAG,GAAG,KAAK,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,CAAsC,CAAC;IACtE,CAAC;CAQD","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\n// False positive: this is an import from the `events` package, not from Node.\n// eslint-disable-next-line unicorn/prefer-node-protocol\nimport { EventEmitter } from \"events\";\nimport {\n\tIEvent,\n\tTransformedEvent,\n\tIEventTransformer,\n\tIEventProvider,\n} from \"@fluidframework/core-interfaces\";\n\n/**\n * The event emitter polyfill and the node event emitter have different event types:\n * string | symbol vs. string | number\n *\n * This type allow us to correctly handle either type\n * @public\n */\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport type EventEmitterEventType = EventEmitter extends { on(event: infer E, listener: any) }\n\t? E\n\t: never;\n\n/**\n * @public\n */\nexport type TypedEventTransform<TThis, TEvent> =\n\t// Event emitter supports some special events for the emitter itself to use\n\t// this exposes those events for the TypedEventEmitter.\n\t// Since we know what the shape of these events are, we can describe them directly via a TransformedEvent\n\t// which easier than trying to extend TEvent directly\n\tTransformedEvent<\n\t\tTThis,\n\t\t\"newListener\" | \"removeListener\",\n\t\t// eslint-disable-next-line @typescript-eslint/no-explicit-any\n\t\tParameters<(event: string, listener: (...args: any[]) => void) => void>\n\t> &\n\t\t// Expose all the events provides by TEvent\n\t\tIEventTransformer<TThis, TEvent & IEvent> &\n\t\t// Add the default overload so this is covertable to EventEmitter regardless of environment\n\t\t// eslint-disable-next-line @typescript-eslint/no-explicit-any\n\t\tTransformedEvent<TThis, EventEmitterEventType, any[]>;\n\n/**\n * Event Emitter helper class the supports emitting typed events\n * @public\n */\nexport class TypedEventEmitter<TEvent>\n\textends EventEmitter\n\timplements IEventProvider<TEvent & IEvent>\n{\n\tconstructor() {\n\t\tsuper();\n\t\tthis.addListener = super.addListener.bind(this) as TypedEventTransform<this, TEvent>;\n\t\tthis.on = super.on.bind(this) as TypedEventTransform<this, TEvent>;\n\t\tthis.once = super.once.bind(this) as TypedEventTransform<this, TEvent>;\n\t\tthis.prependListener = super.prependListener.bind(this) as TypedEventTransform<\n\t\t\tthis,\n\t\t\tTEvent\n\t\t>;\n\t\tthis.prependOnceListener = super.prependOnceListener.bind(this) as TypedEventTransform<\n\t\t\tthis,\n\t\t\tTEvent\n\t\t>;\n\t\tthis.removeListener = super.removeListener.bind(this) as TypedEventTransform<this, TEvent>;\n\t\tthis.off = super.off.bind(this) as TypedEventTransform<this, TEvent>;\n\t}\n\treadonly addListener: TypedEventTransform<this, TEvent>;\n\treadonly on: TypedEventTransform<this, TEvent>;\n\treadonly once: TypedEventTransform<this, TEvent>;\n\treadonly prependListener: TypedEventTransform<this, TEvent>;\n\treadonly prependOnceListener: TypedEventTransform<this, TEvent>;\n\treadonly removeListener: TypedEventTransform<this, TEvent>;\n\treadonly off: TypedEventTransform<this, TEvent>;\n}\n"]}
|
package/package.json
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@fluid-internal/client-utils",
|
|
3
|
+
"version": "2.0.0-dev-rc.1.0.0.224419",
|
|
4
|
+
"description": "Not intended for use outside the Fluid Framework.",
|
|
5
|
+
"homepage": "https://fluidframework.com",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "https://github.com/microsoft/FluidFramework.git",
|
|
9
|
+
"directory": "packages/common/client-utils"
|
|
10
|
+
},
|
|
11
|
+
"license": "MIT",
|
|
12
|
+
"author": "Microsoft and contributors",
|
|
13
|
+
"sideEffects": false,
|
|
14
|
+
"main": "dist/index.cjs",
|
|
15
|
+
"module": "lib/index.mjs",
|
|
16
|
+
"browser": {
|
|
17
|
+
"./dist/indexNode.cjs": "./dist/indexBrowser.cjs",
|
|
18
|
+
"./lib/indexNode.mjs": "./lib/indexBrowser.mjs"
|
|
19
|
+
},
|
|
20
|
+
"types": "dist/index.d.ts",
|
|
21
|
+
"c8": {
|
|
22
|
+
"all": true,
|
|
23
|
+
"cache-dir": "nyc/.cache",
|
|
24
|
+
"exclude": [
|
|
25
|
+
"src/test/**/*.*ts",
|
|
26
|
+
"dist/test/**/*.*js"
|
|
27
|
+
],
|
|
28
|
+
"exclude-after-remap": false,
|
|
29
|
+
"include": [
|
|
30
|
+
"src/**/*.*ts",
|
|
31
|
+
"dist/**/*.*js"
|
|
32
|
+
],
|
|
33
|
+
"report-dir": "nyc/report",
|
|
34
|
+
"reporter": [
|
|
35
|
+
"cobertura",
|
|
36
|
+
"html",
|
|
37
|
+
"text"
|
|
38
|
+
],
|
|
39
|
+
"temp-directory": "nyc/.nyc_output"
|
|
40
|
+
},
|
|
41
|
+
"dependencies": {
|
|
42
|
+
"@fluidframework/core-interfaces": "2.0.0-dev-rc.1.0.0.224419",
|
|
43
|
+
"@fluidframework/core-utils": "2.0.0-dev-rc.1.0.0.224419",
|
|
44
|
+
"@types/events": "^3.0.0",
|
|
45
|
+
"base64-js": "^1.5.1",
|
|
46
|
+
"buffer": "^6.0.3",
|
|
47
|
+
"events": "^3.1.0",
|
|
48
|
+
"lodash": "^4.17.21",
|
|
49
|
+
"sha.js": "^2.4.11"
|
|
50
|
+
},
|
|
51
|
+
"devDependencies": {
|
|
52
|
+
"@arethetypeswrong/cli": "^0.13.3",
|
|
53
|
+
"@fluid-internal/client-utils-previous": "npm:@fluid-internal/client-utils@2.0.0-internal.7.3.0",
|
|
54
|
+
"@fluid-tools/build-cli": "0.29.0-222379",
|
|
55
|
+
"@fluidframework/build-common": "^2.0.3",
|
|
56
|
+
"@fluidframework/build-tools": "0.29.0-222379",
|
|
57
|
+
"@fluidframework/eslint-config-fluid": "^3.1.0",
|
|
58
|
+
"@fluidframework/mocha-test-setup": "2.0.0-dev-rc.1.0.0.224419",
|
|
59
|
+
"@microsoft/api-extractor": "^7.38.3",
|
|
60
|
+
"@types/base64-js": "^1.3.0",
|
|
61
|
+
"@types/jest": "29.5.3",
|
|
62
|
+
"@types/jest-environment-puppeteer": "2.2.0",
|
|
63
|
+
"@types/mocha": "^9.1.1",
|
|
64
|
+
"@types/node": "^18.19.0",
|
|
65
|
+
"@types/puppeteer": "1.3.0",
|
|
66
|
+
"@types/rewire": "^2.5.28",
|
|
67
|
+
"@types/sha.js": "^2.4.4",
|
|
68
|
+
"@types/sinon": "^7.0.13",
|
|
69
|
+
"c8": "^7.7.1",
|
|
70
|
+
"concurrently": "^8.2.1",
|
|
71
|
+
"copyfiles": "^2.4.1",
|
|
72
|
+
"cross-env": "^7.0.3",
|
|
73
|
+
"eslint": "~8.50.0",
|
|
74
|
+
"eslint-config-prettier": "~9.0.0",
|
|
75
|
+
"jest": "^29.6.2",
|
|
76
|
+
"jest-junit": "^10.0.0",
|
|
77
|
+
"jest-puppeteer": "^6.2.0",
|
|
78
|
+
"mocha": "^10.2.0",
|
|
79
|
+
"mocha-json-output-reporter": "^2.0.1",
|
|
80
|
+
"mocha-multi-reporters": "^1.5.1",
|
|
81
|
+
"moment": "^2.21.0",
|
|
82
|
+
"prettier": "~3.0.3",
|
|
83
|
+
"puppeteer": "^17.1.3",
|
|
84
|
+
"renamer": "^4.0.0",
|
|
85
|
+
"rewire": "^5.0.0",
|
|
86
|
+
"rimraf": "^4.4.0",
|
|
87
|
+
"sinon": "^7.4.2",
|
|
88
|
+
"ts-jest": "^29.1.1",
|
|
89
|
+
"ts-node": "^10.9.1",
|
|
90
|
+
"tsc-multi": "^1.1.0",
|
|
91
|
+
"typescript": "~5.1.6"
|
|
92
|
+
},
|
|
93
|
+
"fluidBuild": {
|
|
94
|
+
"tasks": {
|
|
95
|
+
"build:docs": {
|
|
96
|
+
"dependsOn": [
|
|
97
|
+
"...",
|
|
98
|
+
"api-extractor:commonjs",
|
|
99
|
+
"api-extractor:esnext"
|
|
100
|
+
],
|
|
101
|
+
"script": false
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
},
|
|
105
|
+
"typeValidation": {
|
|
106
|
+
"broken": {}
|
|
107
|
+
},
|
|
108
|
+
"scripts": {
|
|
109
|
+
"api": "fluid-build . --task api",
|
|
110
|
+
"api-extractor:commonjs": "api-extractor run --local",
|
|
111
|
+
"api-extractor:esnext": "copyfiles -u 1 \"dist/**/*-@(alpha|beta|public|untrimmed).d.ts\" lib",
|
|
112
|
+
"build": "fluid-build . --task build",
|
|
113
|
+
"build:commonjs": "fluid-build . --task commonjs",
|
|
114
|
+
"build:compile": "fluid-build . --task compile",
|
|
115
|
+
"build:docs": "fluid-build . --task api",
|
|
116
|
+
"build:esnext": "tsc-multi --config ../../../common/build/build-common/tsc-multi.esm.json",
|
|
117
|
+
"build:rename-types": "renamer \"lib/**\" -f .d.ts -r .d.mts --force",
|
|
118
|
+
"build:test": "tsc-multi --config ./tsc-multi.test.json",
|
|
119
|
+
"check:are-the-types-wrong": "attw --pack",
|
|
120
|
+
"check:release-tags": "api-extractor run --local --config ./api-extractor-lint.json",
|
|
121
|
+
"ci:build:docs": "api-extractor run",
|
|
122
|
+
"clean": "rimraf --glob _api-extractor-temp dist lib \"**/*.tsbuildinfo\" \"**/*.build.log\" nyc",
|
|
123
|
+
"eslint": "eslint --format stylish src",
|
|
124
|
+
"eslint:fix": "eslint --format stylish src --fix --fix-type problem,suggestion,layout",
|
|
125
|
+
"format": "npm run prettier:fix",
|
|
126
|
+
"lint": "npm run prettier && npm run check:release-tags && npm run eslint",
|
|
127
|
+
"lint:fix": "npm run prettier:fix && npm run eslint:fix",
|
|
128
|
+
"prettier": "prettier --check . --cache --ignore-path ../../../.prettierignore",
|
|
129
|
+
"prettier:fix": "prettier --write . --cache --ignore-path ../../../.prettierignore",
|
|
130
|
+
"test": "npm run test:mocha && npm run test:jest",
|
|
131
|
+
"test:coverage": "c8 npm test",
|
|
132
|
+
"test:jest": "jest",
|
|
133
|
+
"test:mocha": "mocha --recursive \"dist/test/mocha/**/*.spec.?js\" --exit --project test/tsconfig.json",
|
|
134
|
+
"tsc": "tsc-multi --config ../../../common/build/build-common/tsc-multi.cjs.json",
|
|
135
|
+
"typetests:gen": "fluid-type-test-generator",
|
|
136
|
+
"typetests:prepare": "flub typetests --dir . --reset --previous --normalize"
|
|
137
|
+
}
|
|
138
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* Copyright (c) Microsoft Corporation and contributors. All rights reserved.
|
|
3
|
+
* Licensed under the MIT License.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { IsoBuffer } from "./indexNode";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Converts the provided {@link https://en.wikipedia.org/wiki/Base64 | base64}-encoded string
|
|
10
|
+
* to {@link https://en.wikipedia.org/wiki/UTF-8 | utf-8}.
|
|
11
|
+
*
|
|
12
|
+
* @internal
|
|
13
|
+
*/
|
|
14
|
+
export const fromBase64ToUtf8 = (input: string): string =>
|
|
15
|
+
IsoBuffer.from(input, "base64").toString("utf8");
|
|
16
|
+
|
|
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
|
+
export const fromUtf8ToBase64 = (input: string): string =>
|
|
24
|
+
IsoBuffer.from(input, "utf8").toString("base64");
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Convenience function to convert unknown encoding to utf8 that avoids
|
|
28
|
+
* buffer copies/encode ops when no conversion is needed.
|
|
29
|
+
* @param input - The source string to convert.
|
|
30
|
+
* @param encoding - The source string's encoding.
|
|
31
|
+
*
|
|
32
|
+
* @internal
|
|
33
|
+
*/
|
|
34
|
+
export const toUtf8 = (input: string, encoding: string): string => {
|
|
35
|
+
switch (encoding) {
|
|
36
|
+
case "utf8":
|
|
37
|
+
// eslint-disable-next-line unicorn/text-encoding-identifier-case -- this value is supported, just discouraged
|
|
38
|
+
case "utf-8": {
|
|
39
|
+
return input;
|
|
40
|
+
}
|
|
41
|
+
default: {
|
|
42
|
+
return IsoBuffer.from(input, encoding).toString();
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
};
|
|
@@ -0,0 +1,208 @@
|
|
|
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(
|
|
20
|
+
arr: Uint8Array,
|
|
21
|
+
// eslint-disable-next-line unicorn/text-encoding-identifier-case -- this value is supported, just discouraged
|
|
22
|
+
encoding?: "utf8" | "utf-8" | "base64",
|
|
23
|
+
): string {
|
|
24
|
+
switch (encoding) {
|
|
25
|
+
case "base64": {
|
|
26
|
+
return base64js.fromByteArray(arr);
|
|
27
|
+
}
|
|
28
|
+
case "utf8":
|
|
29
|
+
// eslint-disable-next-line unicorn/text-encoding-identifier-case -- this value is supported, just discouraged
|
|
30
|
+
case "utf-8":
|
|
31
|
+
case undefined: {
|
|
32
|
+
return new TextDecoder().decode(arr);
|
|
33
|
+
}
|
|
34
|
+
default: {
|
|
35
|
+
throw new Error("invalid/unsupported encoding");
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Converts a {@link https://en.wikipedia.org/wiki/Base64 | base64} or
|
|
42
|
+
* {@link https://en.wikipedia.org/wiki/UTF-8 | utf-8} string to array buffer.
|
|
43
|
+
*
|
|
44
|
+
* @param encoding - The input string's encoding.
|
|
45
|
+
*
|
|
46
|
+
* @internal
|
|
47
|
+
*/
|
|
48
|
+
export const stringToBuffer = (input: string, encoding: string): ArrayBufferLike =>
|
|
49
|
+
IsoBuffer.from(input, encoding).buffer;
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Convert binary blob to string format
|
|
53
|
+
*
|
|
54
|
+
* @param blob - the binary blob
|
|
55
|
+
* @param encoding - output string's encoding
|
|
56
|
+
* @returns the blob in string format
|
|
57
|
+
*
|
|
58
|
+
* @alpha
|
|
59
|
+
*/
|
|
60
|
+
export const bufferToString = (
|
|
61
|
+
blob: ArrayBufferLike,
|
|
62
|
+
// eslint-disable-next-line unicorn/text-encoding-identifier-case -- this value is supported, just discouraged
|
|
63
|
+
encoding: "utf8" | "utf-8" | "base64",
|
|
64
|
+
): string => IsoBuffer.from(blob).toString(encoding);
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Determines if an object is an array buffer.
|
|
68
|
+
*
|
|
69
|
+
* @remarks Will detect and reject TypedArrays, like Uint8Array.
|
|
70
|
+
* Reason - they can be viewport into Array, they can be accepted, but caller has to deal with
|
|
71
|
+
* math properly (i.e. Take into account byteOffset at minimum).
|
|
72
|
+
* For example, construction of new TypedArray can be in the form of new TypedArray(typedArray) or
|
|
73
|
+
* new TypedArray(buffer, byteOffset, length), but passing TypedArray will result in fist path (and
|
|
74
|
+
* ignoring byteOffice, length).
|
|
75
|
+
*
|
|
76
|
+
* @param obj - The object to determine if it is an ArrayBuffer.
|
|
77
|
+
*
|
|
78
|
+
* @internal
|
|
79
|
+
*/
|
|
80
|
+
export function isArrayBuffer(obj: unknown): obj is ArrayBuffer {
|
|
81
|
+
const maybe = obj as (Partial<ArrayBuffer> & Partial<Uint8Array>) | undefined;
|
|
82
|
+
return (
|
|
83
|
+
obj instanceof ArrayBuffer ||
|
|
84
|
+
(typeof maybe === "object" &&
|
|
85
|
+
maybe !== null &&
|
|
86
|
+
typeof maybe.byteLength === "number" &&
|
|
87
|
+
typeof maybe.slice === "function" &&
|
|
88
|
+
maybe.byteOffset === undefined &&
|
|
89
|
+
maybe.buffer === undefined)
|
|
90
|
+
);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Minimal implementation of Buffer for our usages in the browser environment.
|
|
95
|
+
*
|
|
96
|
+
* @internal
|
|
97
|
+
*/
|
|
98
|
+
export class IsoBuffer extends Uint8Array {
|
|
99
|
+
/**
|
|
100
|
+
* Convert the buffer to a string.
|
|
101
|
+
* Only supports encoding the whole string (unlike the Node Buffer equivalent)
|
|
102
|
+
* and only utf8 and base64 encodings.
|
|
103
|
+
*
|
|
104
|
+
* @param encoding - The encoding to use.
|
|
105
|
+
*/
|
|
106
|
+
// eslint-disable-next-line unicorn/text-encoding-identifier-case -- this value is supported, just discouraged
|
|
107
|
+
public toString(encoding?: "utf8" | "utf-8" | "base64"): string {
|
|
108
|
+
return Uint8ArrayToString(this, encoding);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Static constructor
|
|
113
|
+
* @param value - (string | ArrayBuffer)
|
|
114
|
+
* @param encodingOrOffset - (string | number)
|
|
115
|
+
* @param length - (number)
|
|
116
|
+
*
|
|
117
|
+
* @privateRemarks TODO: Use actual types
|
|
118
|
+
*/
|
|
119
|
+
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types, @typescript-eslint/no-explicit-any
|
|
120
|
+
static from(value: any, encodingOrOffset?: any, length?: any): IsoBuffer {
|
|
121
|
+
if (typeof value === "string") {
|
|
122
|
+
return IsoBuffer.fromString(value, encodingOrOffset as string | undefined);
|
|
123
|
+
// Capture any typed arrays, including Uint8Array (and thus - IsoBuffer!)
|
|
124
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
|
125
|
+
} else if (value !== null && typeof value === "object" && isArrayBuffer(value.buffer)) {
|
|
126
|
+
// The version of the from function for the node buffer, which takes a buffer or typed array
|
|
127
|
+
// as first parameter, does not have any offset or length parameters. Those are just silently
|
|
128
|
+
// ignored and not taken into account
|
|
129
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-member-access
|
|
130
|
+
return IsoBuffer.fromArrayBuffer(value.buffer, value.byteOffset, value.byteLength);
|
|
131
|
+
} else if (isArrayBuffer(value)) {
|
|
132
|
+
return IsoBuffer.fromArrayBuffer(
|
|
133
|
+
value,
|
|
134
|
+
encodingOrOffset as number | undefined,
|
|
135
|
+
length as number,
|
|
136
|
+
);
|
|
137
|
+
} else {
|
|
138
|
+
throw new TypeError("Input value was neither a string nor an ArrayBuffer.");
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
static fromArrayBuffer(
|
|
143
|
+
arrayBuffer: ArrayBuffer,
|
|
144
|
+
byteOffset?: number,
|
|
145
|
+
byteLength?: number,
|
|
146
|
+
): IsoBuffer {
|
|
147
|
+
const offset = byteOffset ?? 0;
|
|
148
|
+
const validLength = byteLength ?? arrayBuffer.byteLength - offset;
|
|
149
|
+
if (
|
|
150
|
+
offset < 0 ||
|
|
151
|
+
offset > arrayBuffer.byteLength ||
|
|
152
|
+
validLength < 0 ||
|
|
153
|
+
validLength + offset > arrayBuffer.byteLength
|
|
154
|
+
) {
|
|
155
|
+
throw new RangeError("Invalid range specified.");
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
return new IsoBuffer(arrayBuffer, offset, validLength);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
static fromString(str: string, encoding?: string): IsoBuffer {
|
|
162
|
+
switch (encoding) {
|
|
163
|
+
case "base64": {
|
|
164
|
+
const sanitizedString = this.sanitizeBase64(str);
|
|
165
|
+
const encoded = base64js.toByteArray(sanitizedString);
|
|
166
|
+
return new IsoBuffer(encoded.buffer);
|
|
167
|
+
}
|
|
168
|
+
case "utf8":
|
|
169
|
+
// eslint-disable-next-line unicorn/text-encoding-identifier-case -- this value is supported, just discouraged
|
|
170
|
+
case "utf-8":
|
|
171
|
+
case undefined: {
|
|
172
|
+
const encoded = new TextEncoder().encode(str);
|
|
173
|
+
return new IsoBuffer(encoded.buffer);
|
|
174
|
+
}
|
|
175
|
+
default: {
|
|
176
|
+
throw new Error("invalid/unsupported encoding");
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
static isBuffer(obj: unknown): boolean {
|
|
182
|
+
throw new Error("unimplemented");
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* Sanitize a base64 string to provide to base64-js library.
|
|
187
|
+
* {@link https://www.npmjs.com/package/base64-js} is not as tolerant of the same malformed base64 as Node'
|
|
188
|
+
* Buffer is.
|
|
189
|
+
*/
|
|
190
|
+
private static sanitizeBase64(str: string): string {
|
|
191
|
+
let sanitizedStr = str;
|
|
192
|
+
// Remove everything after padding - Node buffer ignores everything
|
|
193
|
+
// after any padding whereas base64-js does not
|
|
194
|
+
sanitizedStr = sanitizedStr.split("=")[0];
|
|
195
|
+
|
|
196
|
+
// Remove invalid characters - Node buffer strips invalid characters
|
|
197
|
+
// whereas base64-js replaces them with "A"
|
|
198
|
+
sanitizedStr = sanitizedStr.replace(/[^\w+-/]/g, "");
|
|
199
|
+
|
|
200
|
+
// Check for missing padding - Node buffer tolerates missing padding
|
|
201
|
+
// whereas base64-js does not
|
|
202
|
+
if (sanitizedStr.length % 4 !== 0) {
|
|
203
|
+
const paddingArray = ["", "===", "==", "="];
|
|
204
|
+
sanitizedStr += paddingArray[sanitizedStr.length % 4];
|
|
205
|
+
}
|
|
206
|
+
return sanitizedStr;
|
|
207
|
+
}
|
|
208
|
+
}
|