@fluid-internal/client-utils 2.118.1 → 3.0.1

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 (120) hide show
  1. package/CHANGELOG.md +1 -5
  2. package/README.md +13 -15
  3. package/dist/base64EncodingBrowser.d.ts +2 -1
  4. package/dist/base64EncodingBrowser.d.ts.map +1 -1
  5. package/dist/base64EncodingBrowser.js +1 -1
  6. package/dist/base64EncodingBrowser.js.map +1 -1
  7. package/dist/base64EncodingNode.d.ts +2 -1
  8. package/dist/base64EncodingNode.d.ts.map +1 -1
  9. package/dist/base64EncodingNode.js.map +1 -1
  10. package/dist/buffer.d.ts +93 -0
  11. package/dist/buffer.d.ts.map +1 -0
  12. package/dist/buffer.js +7 -0
  13. package/dist/buffer.js.map +1 -0
  14. package/dist/bufferBrowser.d.ts +18 -49
  15. package/dist/bufferBrowser.d.ts.map +1 -1
  16. package/dist/bufferBrowser.js +49 -52
  17. package/dist/bufferBrowser.js.map +1 -1
  18. package/dist/bufferNode.d.ts +67 -19
  19. package/dist/bufferNode.d.ts.map +1 -1
  20. package/dist/bufferNode.js +10 -12
  21. package/dist/bufferNode.js.map +1 -1
  22. package/dist/bufferShared.d.ts +38 -3
  23. package/dist/bufferShared.d.ts.map +1 -1
  24. package/dist/bufferShared.js +67 -5
  25. package/dist/bufferShared.js.map +1 -1
  26. package/dist/events/emitter.d.ts.map +1 -1
  27. package/dist/events/emitter.js +4 -3
  28. package/dist/events/emitter.js.map +1 -1
  29. package/dist/hashFileBrowser.d.ts +1 -1
  30. package/dist/hashFileBrowser.d.ts.map +1 -1
  31. package/dist/hashFileBrowser.js +23 -11
  32. package/dist/hashFileBrowser.js.map +1 -1
  33. package/dist/hashFileNode.d.ts +10 -2
  34. package/dist/hashFileNode.d.ts.map +1 -1
  35. package/dist/hashFileNode.js +10 -3
  36. package/dist/hashFileNode.js.map +1 -1
  37. package/dist/indexBrowser.d.ts +5 -3
  38. package/dist/indexBrowser.d.ts.map +1 -1
  39. package/dist/indexBrowser.js +5 -5
  40. package/dist/indexBrowser.js.map +1 -1
  41. package/dist/indexNode.d.ts +4 -3
  42. package/dist/indexNode.d.ts.map +1 -1
  43. package/dist/indexNode.js +5 -2
  44. package/dist/indexNode.js.map +1 -1
  45. package/dist/layerCompat.d.ts +37 -13
  46. package/dist/layerCompat.d.ts.map +1 -1
  47. package/dist/layerCompat.js +19 -14
  48. package/dist/layerCompat.js.map +1 -1
  49. package/dist/trace.js +2 -0
  50. package/dist/trace.js.map +1 -1
  51. package/dist/typedEventEmitter.js +7 -0
  52. package/dist/typedEventEmitter.js.map +1 -1
  53. package/eslint.config.mts +2 -2
  54. package/lib/base64EncodingBrowser.d.ts +2 -1
  55. package/lib/base64EncodingBrowser.d.ts.map +1 -1
  56. package/lib/base64EncodingBrowser.js +1 -1
  57. package/lib/base64EncodingBrowser.js.map +1 -1
  58. package/lib/base64EncodingNode.d.ts +2 -1
  59. package/lib/base64EncodingNode.d.ts.map +1 -1
  60. package/lib/base64EncodingNode.js.map +1 -1
  61. package/lib/buffer.d.ts +93 -0
  62. package/lib/buffer.d.ts.map +1 -0
  63. package/lib/buffer.js +6 -0
  64. package/lib/buffer.js.map +1 -0
  65. package/lib/bufferBrowser.d.ts +18 -49
  66. package/lib/bufferBrowser.d.ts.map +1 -1
  67. package/lib/bufferBrowser.js +29 -40
  68. package/lib/bufferBrowser.js.map +1 -1
  69. package/lib/bufferNode.d.ts +67 -19
  70. package/lib/bufferNode.d.ts.map +1 -1
  71. package/lib/bufferNode.js +7 -9
  72. package/lib/bufferNode.js.map +1 -1
  73. package/lib/bufferShared.d.ts +38 -3
  74. package/lib/bufferShared.d.ts.map +1 -1
  75. package/lib/bufferShared.js +64 -3
  76. package/lib/bufferShared.js.map +1 -1
  77. package/lib/events/emitter.d.ts.map +1 -1
  78. package/lib/events/emitter.js +2 -1
  79. package/lib/events/emitter.js.map +1 -1
  80. package/lib/hashFileBrowser.d.ts +1 -1
  81. package/lib/hashFileBrowser.d.ts.map +1 -1
  82. package/lib/hashFileBrowser.js +4 -1
  83. package/lib/hashFileBrowser.js.map +1 -1
  84. package/lib/hashFileNode.d.ts +10 -2
  85. package/lib/hashFileNode.d.ts.map +1 -1
  86. package/lib/hashFileNode.js +8 -0
  87. package/lib/hashFileNode.js.map +1 -1
  88. package/lib/indexBrowser.d.ts +5 -3
  89. package/lib/indexBrowser.d.ts.map +1 -1
  90. package/lib/indexBrowser.js +3 -5
  91. package/lib/indexBrowser.js.map +1 -1
  92. package/lib/indexNode.d.ts +4 -3
  93. package/lib/indexNode.d.ts.map +1 -1
  94. package/lib/indexNode.js +2 -2
  95. package/lib/indexNode.js.map +1 -1
  96. package/lib/layerCompat.d.ts +37 -13
  97. package/lib/layerCompat.d.ts.map +1 -1
  98. package/lib/layerCompat.js +17 -12
  99. package/lib/layerCompat.js.map +1 -1
  100. package/lib/trace.js +2 -0
  101. package/lib/trace.js.map +1 -1
  102. package/lib/typedEventEmitter.js +7 -0
  103. package/lib/typedEventEmitter.js.map +1 -1
  104. package/package.json +65 -62
  105. package/playwright.config.ts +23 -0
  106. package/src/base64EncodingBrowser.ts +3 -2
  107. package/src/base64EncodingNode.ts +2 -1
  108. package/src/buffer.ts +112 -0
  109. package/src/bufferBrowser.ts +98 -60
  110. package/src/bufferNode.ts +86 -30
  111. package/src/bufferShared.ts +74 -4
  112. package/src/hashFileBrowser.ts +6 -3
  113. package/src/hashFileNode.ts +10 -2
  114. package/src/indexBrowser.ts +13 -2
  115. package/src/indexNode.ts +13 -2
  116. package/src/layerCompat.ts +41 -13
  117. package/tsconfig.json +1 -2
  118. package/webpack.playwright.cjs +22 -0
  119. package/jest-puppeteer.config.cjs +0 -16
  120. package/jest.config.cjs +0 -21
@@ -1 +1 @@
1
- {"version":3,"file":"bufferBrowser.js","sourceRoot":"","sources":["../src/bufferBrowser.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;;;;;;;;;;;;;;;;;;;;;;;;AAEH,oDAA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AAG,CAAC,CAAC,CAAC,CAAC,CAAC;QAE1C,oEAAoE;QACpE,2CAA2C;QAC3C,YAAY,GAAG,YAAY,CAAC,OAAO,CAAC,WAAW,EAAE,EAAE,CAAC,CAAC;QAErD,oEAAoE;QACpE,6BAA6B;QAC7B,IAAI,YAAY,CAAC,MAAM,GAAG,CAAC,KAAK,CAAC,EAAE,CAAC;YACnC,MAAM,YAAY,GAAG,CAAC,EAAE,EAAE,KAAK,EAAE,IAAI,EAAE,GAAG,CAAC,CAAC;YAC5C,YAAY,IAAI,YAAY,CAAC,YAAY,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC;QACvD,CAAC;QACD,OAAO,YAAY,CAAC;IACrB,CAAC;CACD;AA9GD,8BA8GC","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): ArrayBuffer =>\n\tIsoBuffer.from(input, encoding).buffer;\n\n/**\n * Convert binary blob to string format\n *\n * @param blob - the binary blob\n * @param encoding - output string's encoding\n * @returns the blob in string format\n *\n * @internal\n */\nexport const bufferToString = (\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\tpublic static 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\tpublic static 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\tpublic static 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\tpublic static 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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C9E,CAAC;YACD,OAAO,CAAC,CAAC,CAAC;gBACT,MAAM,IAAI,KAAK,CAAC,8BAA8B,CAAC,CAAC;YACjD,CAAC;QACF,CAAC;IACF,CAAC;IAEM,MAAM,CAAC,QAAQ,CAAC,GAAY;QAClC,OAAO,GAAG,YAAY,SAAS,CAAC;IACjC,CAAC;IAED;;;;OAIG;IACK,MAAM,CAAC,cAAc,CAAC,GAAW;QACxC,IAAI,YAAY,GAAG,GAAG,CAAC;QACvB,mEAAmE;QACnE,+CAA+C;QAC/C,YAAY,GAAG,YAAY,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC;QAE1C,oEAAoE;QACpE,2CAA2C;QAC3C,YAAY,GAAG,YAAY,CAAC,OAAO,CAAC,WAAW,EAAE,EAAE,CAAC,CAAC;QAErD,oEAAoE;QACpE,6BAA6B;QAC7B,IAAI,YAAY,CAAC,MAAM,GAAG,CAAC,KAAK,CAAC,EAAE,CAAC;YACnC,MAAM,YAAY,GAAG,CAAC,EAAE,EAAE,KAAK,EAAE,IAAI,EAAE,GAAG,CAAC,CAAC;YAC5C,YAAY,IAAI,YAAY,CAAC,YAAY,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC;QACvD,CAAC;QACD,OAAO,YAAY,CAAC;IACrB,CAAC;CAC8B,CAAC","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\nimport type {\n\tIsoBufferConstructor,\n\tIsoBufferEncoding,\n\tIsoBuffer as IsoBufferInterface,\n} from \"./buffer.js\";\n\nexport type {\n\tIsoBufferConstructor,\n\tIsoBufferEncoding,\n} from \"./buffer.js\";\n/**\n * Minimal `Buffer` interface for our usages in the browser or Node.js environments.\n *\n * @internal\n *\n * @privateRemarks\n * This type is exported specifically from here to respect combo type+value of same name rules.\n */\nexport type IsoBuffer<TArrayBuffer extends ArrayBufferLike = ArrayBufferLike> =\n\tIsoBufferInterface<TArrayBuffer>;\n\n/**\n * Converts a `Uint8Array` to a string of the provided encoding\n * Useful when the array might be an {@link (IsoBuffer:type)}.\n *\n * @param arr - The array to convert.\n * @param encoding - Optional target encoding; only \"utf8\" and \"base64\" are\n * supported, with \"utf8\" being default.\n * @returns The converted string.\n *\n * @internal\n */\nexport function Uint8ArrayToString(arr: Uint8Array, encoding?: IsoBufferEncoding): 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: IsoBufferEncoding): ArrayBuffer =>\n\tIsoBuffer.from(input, encoding).buffer;\n\n/**\n * Convert binary blob to string format\n *\n * @param blob - the binary blob\n * @param encoding - output string's encoding\n * @returns the blob in string format\n *\n * @internal\n */\nexport const bufferToString = (\n\tblob: ArrayBufferLike | Uint8Array,\n\tencoding: IsoBufferEncoding,\n): string =>\n\tUint8ArrayToString(blob instanceof Uint8Array ? blob : IsoBuffer.from(blob), 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 `ArrayBufferLike`.\n *\n * @privateRemarks\n * This function preserves runtime functionality as it has been in the past.\n * Typing has been adjusted to reflect the only use that remains and (assuming\n * is called as expected) will always return true as the input is already\n * expected to be `ArrayBufferLike`.\n */\nfunction isArrayBufferLike(obj: ArrayBufferLike): obj is ArrayBufferLike {\n\tconst maybe = obj as (Partial<ArrayBufferLike> & 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 */\n// eslint-disable-next-line @typescript-eslint/no-shadow -- Preserve the public constructor name at runtime.\nexport const IsoBuffer: IsoBufferConstructor = class IsoBuffer<\n\t\tTArrayBuffer extends ArrayBufferLike = ArrayBufferLike,\n\t>\n\textends Uint8Array<TArrayBuffer>\n\timplements IsoBufferInterface<TArrayBuffer>\n{\n\tpublic constructor(\n\t\tvalue: number | ArrayLike<number> | TArrayBuffer,\n\t\tbyteOffset?: number,\n\t\tlength?: number,\n\t) {\n\t\tsuper(value as TArrayBuffer, byteOffset, length);\n\t}\n\n\tpublic toString(encoding?: IsoBufferEncoding): string {\n\t\treturn Uint8ArrayToString(this, encoding);\n\t}\n\n\tpublic static from(\n\t\tvalue: string,\n\t\tencoding?: IsoBufferEncoding,\n\t): IsoBufferInterface<ArrayBuffer>;\n\tpublic static from(value: Uint8Array): IsoBufferInterface<ArrayBuffer>;\n\tpublic static from<TFArrayBuffer extends ArrayBufferLike>(\n\t\tvalue: TFArrayBuffer,\n\t\tbyteOffset?: number,\n\t\tlength?: number,\n\t): IsoBufferInterface<TFArrayBuffer>;\n\tpublic static from<TFArrayBuffer extends ArrayBufferLike>(\n\t\tvalue: string | Uint8Array | TFArrayBuffer,\n\t\tencodingOrOffset?: IsoBufferEncoding | number,\n\t\tlength?: number,\n\t): IsoBufferInterface<ArrayBufferLike> {\n\t\tif (typeof value === \"string\") {\n\t\t\treturn IsoBuffer.fromString(\n\t\t\t\tvalue,\n\t\t\t\tencodingOrOffset as IsoBufferEncoding | undefined,\n\t\t\t) satisfies IsoBufferInterface<ArrayBuffer>;\n\t\t\t// Capture any typed arrays, including Uint8Array (and thus - IsoBuffer!)\n\t\t} else if (value instanceof Uint8Array) {\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\tconst copy = new Uint8Array(value.byteLength);\n\t\t\tcopy.set(value);\n\t\t\treturn new IsoBuffer<ArrayBuffer>(copy.buffer) satisfies IsoBufferInterface<ArrayBuffer>;\n\t\t} else if (isArrayBufferLike(value)) {\n\t\t\treturn IsoBuffer.fromArrayBuffer(value, encodingOrOffset as number | undefined, length);\n\t\t} else {\n\t\t\tthrow new TypeError(\n\t\t\t\t\"Input value was neither a string, a Uint8Array, nor an ArrayBuffer.\",\n\t\t\t);\n\t\t}\n\t}\n\n\tpublic static fromArrayBuffer<TFArrayBuffer extends ArrayBufferLike>(\n\t\tarrayBuffer: TFArrayBuffer,\n\t\tbyteOffset?: number,\n\t\tbyteLength?: number,\n\t): IsoBufferInterface<TFArrayBuffer> {\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<TFArrayBuffer>(arrayBuffer, offset, validLength);\n\t}\n\n\tpublic static fromString(\n\t\tstr: string,\n\t\tencoding?: IsoBufferEncoding,\n\t): IsoBufferInterface<ArrayBuffer> {\n\t\tswitch (encoding) {\n\t\t\tcase \"base64\": {\n\t\t\t\tconst sanitizedString = this.sanitizeBase64(str);\n\t\t\t\t// base64-js will always return a Uint8Array<ArrayBuffer>, but typing has not\n\t\t\t\t// been updated to reflect that yet. Cast to the correct type here.\n\t\t\t\tconst encoded = base64js.toByteArray(sanitizedString) as Uint8Array<ArrayBuffer>;\n\t\t\t\treturn new IsoBuffer(encoded.buffer, encoded.byteOffset, encoded.byteLength);\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, encoded.byteOffset, encoded.byteLength);\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\tpublic static isBuffer(obj: unknown): obj is IsoBuffer {\n\t\treturn obj instanceof IsoBuffer;\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} satisfies IsoBufferConstructor;\n"]}
@@ -2,38 +2,86 @@
2
2
  * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
3
3
  * Licensed under the MIT License.
4
4
  */
5
+ import type { IsoBufferConstructor, IsoBufferEncoding, IsoBuffer as IsoBufferInterface } from "./buffer.js";
6
+ export type { IsoBufferConstructor, IsoBufferEncoding } from "./buffer.js";
7
+ /**
8
+ * Minimal `Buffer` interface for our usages in the browser or Node.js environments.
9
+ *
10
+ * @internal
11
+ *
12
+ * @privateRemarks
13
+ * This type is exported specifically from here to respect combo type+value of same name rules.
14
+ */
15
+ export type IsoBuffer<TArrayBuffer extends ArrayBufferLike = ArrayBufferLike> = IsoBufferInterface<TArrayBuffer>;
5
16
  /**
6
17
  * Declare the subset of Buffer functionality we want to make available instead of
7
18
  * exposing the entirely of Node's typings. This should match the public interface
8
- * of the browser implementation, so any changes made in one should be made in both.
19
+ * of the browser implementation -- see {@link (IsoBuffer:type)} and {@link IsoBufferConstructor}.
9
20
  *
10
21
  * @internal
11
22
  */
12
- export declare class Buffer extends Uint8Array {
13
- toString(encoding?: "utf8" | "utf-8" | "base64"): string;
23
+ export declare class Buffer<TArrayBuffer extends ArrayBufferLike = ArrayBufferLike> extends Uint8Array<TArrayBuffer> implements IsoBuffer<TArrayBuffer> {
24
+ /**
25
+ * Creates a new `IsoBuffer` containing `string`. The `encoding` parameter identifies
26
+ * the character encoding to be used when converting `string` into bytes.
27
+ *
28
+ * @param string - A string to encode.
29
+ * @param encoding - The encoding of `string`. **Default:** `'utf8'`.
30
+ *
31
+ * @privateRemarks
32
+ * Node buffer.buffer.d.ts declaration:
33
+ * `from(string: WithImplicitCoercion<string>, encoding?: IsoBufferEncoding): Buffer<ArrayBuffer>;`
34
+ */
35
+ static from(string: string, encoding?: IsoBufferEncoding): IsoBuffer<ArrayBuffer>;
14
36
  /**
15
- * Static constructor
37
+ * Allocates a new `IsoBuffer` using an `array` of bytes in the range `0` – `255`.
38
+ * Array entries outside that range will be truncated to fit into it.
16
39
  *
17
- * @param value - (string | ArrayBuffer).
18
- * @param encodingOrOffset - (string | number).
19
- * @param length - (number).
40
+ * @param value - `Uint8Array`.
20
41
  *
21
- * @privateRemarks TODO: Use actual types
42
+ * @remarks
43
+ * This is limited to `Uint8Array` input to match Node implementation that
44
+ * takes `ArrayLike<number>` and returns `IsoBuffer<ArrayBuffer>`.
45
+ *
46
+ * @privateRemarks
47
+ * Node buffer.buffer.d.ts declaration:
48
+ * `from(array: WithImplicitCoercion<ArrayLike<number>>): IsoBuffer<ArrayBuffer>;`
49
+ */
50
+ static from(array: Uint8Array): IsoBuffer<ArrayBuffer>;
51
+ /**
52
+ * This creates a view of the `ArrayBuffer` without copying the underlying
53
+ * memory. For example, when passed a reference to the `.buffer` property of a
54
+ * `TypedArray` instance, the newly created `Buffer` will share the same
55
+ * allocated memory as the `TypedArray`'s underlying `ArrayBuffer`.
56
+ *
57
+ * @param arrayBuffer - An `ArrayBuffer`, `SharedArrayBuffer`, for example the
58
+ * `.buffer` property of a `TypedArray`.
59
+ * @param byteOffset - Index of first byte to expose. **Default:** `0`.
60
+ * @param length - Number of bytes to expose. **Default:**
61
+ * `arrayBuffer.byteLength - byteOffset`.
62
+ *
63
+ * @privateRemarks
64
+ * Node buffer.buffer.d.ts declaration:
65
+ * ```
66
+ * public static from<TArrayBuffer extends WithImplicitCoercion<ArrayBufferLike>>(
67
+ * arrayBuffer: TArrayBuffer,
68
+ * byteOffset?: number,
69
+ * length?: number,
70
+ * ): Buffer<ImplicitArrayBuffer<TArrayBuffer>>;
71
+ * ```
22
72
  */
23
- static from(value: unknown, encodingOrOffset?: unknown, length?: unknown): IsoBuffer;
73
+ static from<TArrayBuffer extends ArrayBufferLike>(arrayBuffer: TArrayBuffer, byteOffset?: number, length?: number): IsoBuffer<TArrayBuffer>;
24
74
  static isBuffer(obj: unknown): obj is Buffer;
25
75
  }
26
76
  /**
77
+ * The native Node.js Buffer implementation.
78
+ *
27
79
  * @internal
28
80
  */
29
- export declare const IsoBuffer: typeof Buffer;
30
- /**
31
- * @internal
32
- */
33
- export type IsoBuffer = Buffer;
81
+ export declare const IsoBuffer: IsoBufferConstructor;
34
82
  /**
35
- * Converts a Uint8Array to a string of the provided encoding.
36
- * @remarks Useful when the array might be an IsoBuffer.
83
+ * Converts a `Uint8Array` to a string of the provided encoding.
84
+ * @remarks Useful when the array might be an `IsoBuffer`.
37
85
  * @param arr - The array to convert.
38
86
  * @param encoding - Optional target encoding; only "utf8" and "base64" are
39
87
  * supported, with "utf8" being default.
@@ -41,14 +89,14 @@ export type IsoBuffer = Buffer;
41
89
  *
42
90
  * @internal
43
91
  */
44
- export declare function Uint8ArrayToString(arr: Uint8Array, encoding?: "utf8" | "utf-8" | "base64"): string;
92
+ export declare function Uint8ArrayToString(arr: Uint8Array, encoding?: IsoBufferEncoding): string;
45
93
  /**
46
94
  * Convert base64 or utf8 string to array buffer.
47
95
  * @param encoding - The input string's encoding.
48
96
  *
49
97
  * @internal
50
98
  */
51
- export declare function stringToBuffer(input: string, encoding: string): ArrayBuffer;
99
+ export declare function stringToBuffer(input: string, encoding: IsoBufferEncoding): ArrayBuffer;
52
100
  /**
53
101
  * Convert binary blob to string format
54
102
  *
@@ -58,5 +106,5 @@ export declare function stringToBuffer(input: string, encoding: string): ArrayBu
58
106
  *
59
107
  * @internal
60
108
  */
61
- export declare const bufferToString: (blob: ArrayBufferLike, encoding: "utf8" | "utf-8" | "base64") => string;
109
+ export declare const bufferToString: (blob: ArrayBufferLike | Uint8Array, encoding: IsoBufferEncoding) => string;
62
110
  //# sourceMappingURL=bufferNode.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"bufferNode.d.ts","sourceRoot":"","sources":["../src/bufferNode.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH;;;;;;GAMG;AACH,MAAM,CAAC,OAAO,OAAO,MAAO,SAAQ,UAAU;IAEtC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C/B,YAAW,SAAS,CAAC,YAAY,CAAC;IAElC;;;;;;;;;;OAUG;WACW,IAAI,CAAC,MAAM,EAAE,MAAM,EAAE,QAAQ,CAAC,EAAE,iBAAiB,GAAG,SAAS,CAAC,WAAW,CAAC;IACxF;;;;;;;;;;;;;OAaG;WACW,IAAI,CAAC,KAAK,EAAE,UAAU,GAAG,SAAS,CAAC,WAAW,CAAC;IAC7D;;;;;;;;;;;;;;;;;;;;;OAqBG;WACW,IAAI,CAAC,YAAY,SAAS,eAAe,EACtD,WAAW,EAAE,YAAY,EACzB,UAAU,CAAC,EAAE,MAAM,EACnB,MAAM,CAAC,EAAE,MAAM,GACb,SAAS,CAAC,YAAY,CAAC;WAEZ,QAAQ,CAAC,GAAG,EAAE,OAAO,GAAG,GAAG,IAAI,MAAM;CACnD;AAED;;;;GAIG;AACH,eAAO,MAAM,SAAS,EAAE,oBAA6B,CAAC;AAEtD;;;;;;;;;GASG;AACH,wBAAgB,kBAAkB,CAAC,GAAG,EAAE,UAAU,EAAE,QAAQ,CAAC,EAAE,iBAAiB,GAAG,MAAM,CAIxF;AAED;;;;;GAKG;AACH,wBAAgB,cAAc,CAAC,KAAK,EAAE,MAAM,EAAE,QAAQ,EAAE,iBAAiB,GAAG,WAAW,CAQtF;AAED;;;;;;;;GAQG;AACH,eAAO,MAAM,cAAc,GAC1B,MAAM,eAAe,GAAG,UAAU,EAClC,UAAU,iBAAiB,KACzB,MACoF,CAAC"}
@@ -4,14 +4,18 @@
4
4
  * Licensed under the MIT License.
5
5
  */
6
6
  Object.defineProperty(exports, "__esModule", { value: true });
7
- exports.bufferToString = exports.stringToBuffer = exports.Uint8ArrayToString = exports.IsoBuffer = void 0;
7
+ exports.bufferToString = exports.IsoBuffer = void 0;
8
+ exports.Uint8ArrayToString = Uint8ArrayToString;
9
+ exports.stringToBuffer = stringToBuffer;
8
10
  /**
11
+ * The native Node.js Buffer implementation.
12
+ *
9
13
  * @internal
10
14
  */
11
15
  exports.IsoBuffer = Buffer;
12
16
  /**
13
- * Converts a Uint8Array to a string of the provided encoding.
14
- * @remarks Useful when the array might be an IsoBuffer.
17
+ * Converts a `Uint8Array` to a string of the provided encoding.
18
+ * @remarks Useful when the array might be an `IsoBuffer`.
15
19
  * @param arr - The array to convert.
16
20
  * @param encoding - Optional target encoding; only "utf8" and "base64" are
17
21
  * supported, with "utf8" being default.
@@ -19,14 +23,11 @@ exports.IsoBuffer = Buffer;
19
23
  *
20
24
  * @internal
21
25
  */
22
- function Uint8ArrayToString(arr,
23
- // eslint-disable-next-line unicorn/text-encoding-identifier-case -- this value is supported, just discouraged
24
- encoding) {
26
+ function Uint8ArrayToString(arr, encoding) {
25
27
  // Buffer extends Uint8Array. Therefore, 'arr' may already be a Buffer, in
26
28
  // which case we can avoid copying the Uint8Array into a new Buffer instance.
27
- return (Buffer.isBuffer(arr) ? arr : Buffer.from(arr)).toString(encoding);
29
+ return (exports.IsoBuffer.isBuffer(arr) ? arr : exports.IsoBuffer.from(arr)).toString(encoding);
28
30
  }
29
- exports.Uint8ArrayToString = Uint8ArrayToString;
30
31
  /**
31
32
  * Convert base64 or utf8 string to array buffer.
32
33
  * @param encoding - The input string's encoding.
@@ -42,7 +43,6 @@ function stringToBuffer(input, encoding) {
42
43
  ? iso.buffer
43
44
  : iso.buffer.slice(iso.byteOffset, iso.byteOffset + iso.byteLength);
44
45
  }
45
- exports.stringToBuffer = stringToBuffer;
46
46
  /**
47
47
  * Convert binary blob to string format
48
48
  *
@@ -52,8 +52,6 @@ exports.stringToBuffer = stringToBuffer;
52
52
  *
53
53
  * @internal
54
54
  */
55
- const bufferToString = (blob,
56
- // eslint-disable-next-line unicorn/text-encoding-identifier-case -- this value is supported, just discouraged
57
- encoding) => exports.IsoBuffer.from(blob).toString(encoding);
55
+ const bufferToString = (blob, encoding) => Uint8ArrayToString(blob instanceof Uint8Array ? blob : exports.IsoBuffer.from(blob), encoding);
58
56
  exports.bufferToString = bufferToString;
59
57
  //# sourceMappingURL=bufferNode.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"bufferNode.js","sourceRoot":"","sources":["../src/bufferNode.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;AA0BH;;GAEG;AACU,QAAA,SAAS,GAAG,MAAM,CAAC;AAOhC;;;;;;;;;GASG;AACH,SAAgB,kBAAkB,CACjC,GAAe;AACf,8GAA8G;AAC9G,QAAsC;IAEtC,2EAA2E;IAC3E,6EAA6E;IAC7E,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAC;AAC3E,CAAC;AARD,gDAQC;AAED;;;;;GAKG;AACH,SAAgB,cAAc,CAAC,KAAa,EAAE,QAAgB;IAC7D,MAAM,GAAG,GAAG,iBAAS,CAAC,IAAI,CAAC,KAAK,EAAE,QAAQ,CAAC,CAAC;IAC5C,0EAA0E;IAC1E,2EAA2E;IAC3E,uDAAuD;IACvD,OAAO,GAAG,CAAC,UAAU,KAAK,GAAG,CAAC,MAAM,CAAC,UAAU;QAC9C,CAAC,CAAC,GAAG,CAAC,MAAM;QACZ,CAAC,CAAC,GAAG,CAAC,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,UAAU,EAAE,GAAG,CAAC,UAAU,GAAG,GAAG,CAAC,UAAU,CAAC,CAAC;AACtE,CAAC;AARD,wCAQC;AAED;;;;;;;;GAQG;AACI,MAAM,cAAc,GAAG,CAC7B,IAAqB;AACrB,8GAA8G;AAC9G,QAAqC,EAC5B,EAAE,CAAC,iBAAS,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAC;AAJxC,QAAA,cAAc,kBAI0B","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\n/**\n * Declare the subset of Buffer functionality we want to make available instead of\n * exposing the entirely of Node's typings. This should match the public interface\n * of the browser implementation, so any changes made in one should be made in both.\n *\n * @internal\n */\nexport declare class Buffer extends Uint8Array {\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\n\t/**\n\t * Static constructor\n\t *\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\tpublic static from(value: unknown, encodingOrOffset?: unknown, length?: unknown): IsoBuffer;\n\tpublic static isBuffer(obj: unknown): obj is Buffer;\n}\n\n/**\n * @internal\n */\nexport const IsoBuffer = Buffer;\n\n/**\n * @internal\n */\nexport type IsoBuffer = Buffer;\n\n/**\n * Converts a Uint8Array to a string of the provided encoding.\n * @remarks Useful when the array might be an IsoBuffer.\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\t// Buffer extends Uint8Array. Therefore, 'arr' may already be a Buffer, in\n\t// which case we can avoid copying the Uint8Array into a new Buffer instance.\n\treturn (Buffer.isBuffer(arr) ? arr : Buffer.from(arr)).toString(encoding);\n}\n\n/**\n * Convert base64 or utf8 string to array buffer.\n * @param encoding - The input string's encoding.\n *\n * @internal\n */\nexport function stringToBuffer(input: string, encoding: string): ArrayBuffer {\n\tconst iso = IsoBuffer.from(input, encoding);\n\t// In a Node environment, IsoBuffer may be a Node.js Buffer. Node.js will\n\t// pool multiple small Buffer instances into a single ArrayBuffer, in which\n\t// case we need to slice the appropriate span of bytes.\n\treturn iso.byteLength === iso.buffer.byteLength\n\t\t? iso.buffer\n\t\t: iso.buffer.slice(iso.byteOffset, iso.byteOffset + iso.byteLength);\n}\n\n/**\n * Convert binary blob to string format\n *\n * @param blob - The binary blob\n * @param encoding - Output string's encoding\n * @returns The blob in string format\n *\n * @internal\n */\nexport const bufferToString = (\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"]}
1
+ {"version":3,"file":"bufferNode.js","sourceRoot":"","sources":["../src/bufferNode.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;AA0GH,gDAIC;AAQD,wCAQC;AArCD;;;;GAIG;AACU,QAAA,SAAS,GAAyB,MAAM,CAAC;AAEtD;;;;;;;;;GASG;AACH,SAAgB,kBAAkB,CAAC,GAAe,EAAE,QAA4B;IAC/E,2EAA2E;IAC3E,6EAA6E;IAC7E,OAAO,CAAC,iBAAS,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,iBAAS,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAC;AACjF,CAAC;AAED;;;;;GAKG;AACH,SAAgB,cAAc,CAAC,KAAa,EAAE,QAA2B;IACxE,MAAM,GAAG,GAAG,iBAAS,CAAC,IAAI,CAAC,KAAK,EAAE,QAAQ,CAAC,CAAC;IAC5C,0EAA0E;IAC1E,2EAA2E;IAC3E,uDAAuD;IACvD,OAAO,GAAG,CAAC,UAAU,KAAK,GAAG,CAAC,MAAM,CAAC,UAAU;QAC9C,CAAC,CAAC,GAAG,CAAC,MAAM;QACZ,CAAC,CAAC,GAAG,CAAC,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,UAAU,EAAE,GAAG,CAAC,UAAU,GAAG,GAAG,CAAC,UAAU,CAAC,CAAC;AACtE,CAAC;AAED;;;;;;;;GAQG;AACI,MAAM,cAAc,GAAG,CAC7B,IAAkC,EAClC,QAA2B,EAClB,EAAE,CACX,kBAAkB,CAAC,IAAI,YAAY,UAAU,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,iBAAS,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,QAAQ,CAAC,CAAC;AAJ3E,QAAA,cAAc,kBAI6D","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\nimport type {\n\tIsoBufferConstructor,\n\tIsoBufferEncoding,\n\tIsoBuffer as IsoBufferInterface,\n} from \"./buffer.js\";\n\nexport type { IsoBufferConstructor, IsoBufferEncoding } from \"./buffer.js\";\n/**\n * Minimal `Buffer` interface for our usages in the browser or Node.js environments.\n *\n * @internal\n *\n * @privateRemarks\n * This type is exported specifically from here to respect combo type+value of same name rules.\n */\nexport type IsoBuffer<TArrayBuffer extends ArrayBufferLike = ArrayBufferLike> =\n\tIsoBufferInterface<TArrayBuffer>;\n\n/**\n * Declare the subset of Buffer functionality we want to make available instead of\n * exposing the entirely of Node's typings. This should match the public interface\n * of the browser implementation -- see {@link (IsoBuffer:type)} and {@link IsoBufferConstructor}.\n *\n * @internal\n */\nexport declare class Buffer<TArrayBuffer extends ArrayBufferLike = ArrayBufferLike>\n\textends Uint8Array<TArrayBuffer>\n\timplements IsoBuffer<TArrayBuffer>\n{\n\t/**\n\t * Creates a new `IsoBuffer` containing `string`. The `encoding` parameter identifies\n\t * the character encoding to be used when converting `string` into bytes.\n\t *\n\t * @param string - A string to encode.\n\t * @param encoding - The encoding of `string`. **Default:** `'utf8'`.\n\t *\n\t * @privateRemarks\n\t * Node buffer.buffer.d.ts declaration:\n\t * `from(string: WithImplicitCoercion<string>, encoding?: IsoBufferEncoding): Buffer<ArrayBuffer>;`\n\t */\n\tpublic static from(string: string, encoding?: IsoBufferEncoding): IsoBuffer<ArrayBuffer>;\n\t/**\n\t * Allocates a new `IsoBuffer` using an `array` of bytes in the range `0` – `255`.\n\t * Array entries outside that range will be truncated to fit into it.\n\t *\n\t * @param value - `Uint8Array`.\n\t *\n\t * @remarks\n\t * This is limited to `Uint8Array` input to match Node implementation that\n\t * takes `ArrayLike<number>` and returns `IsoBuffer<ArrayBuffer>`.\n\t *\n\t * @privateRemarks\n\t * Node buffer.buffer.d.ts declaration:\n\t * `from(array: WithImplicitCoercion<ArrayLike<number>>): IsoBuffer<ArrayBuffer>;`\n\t */\n\tpublic static from(array: Uint8Array): IsoBuffer<ArrayBuffer>;\n\t/**\n\t * This creates a view of the `ArrayBuffer` without copying the underlying\n\t * memory. For example, when passed a reference to the `.buffer` property of a\n\t * `TypedArray` instance, the newly created `Buffer` will share the same\n\t * allocated memory as the `TypedArray`'s underlying `ArrayBuffer`.\n\t *\n\t * @param arrayBuffer - An `ArrayBuffer`, `SharedArrayBuffer`, for example the\n\t * `.buffer` property of a `TypedArray`.\n\t * @param byteOffset - Index of first byte to expose. **Default:** `0`.\n\t * @param length - Number of bytes to expose. **Default:**\n\t * `arrayBuffer.byteLength - byteOffset`.\n\t *\n\t * @privateRemarks\n\t * Node buffer.buffer.d.ts declaration:\n\t * ```\n\t * public static from<TArrayBuffer extends WithImplicitCoercion<ArrayBufferLike>>(\n\t * \t\tarrayBuffer: TArrayBuffer,\n\t * \t\tbyteOffset?: number,\n\t * \t\tlength?: number,\n\t * ): Buffer<ImplicitArrayBuffer<TArrayBuffer>>;\n\t * ```\n\t */\n\tpublic static from<TArrayBuffer extends ArrayBufferLike>(\n\t\tarrayBuffer: TArrayBuffer,\n\t\tbyteOffset?: number,\n\t\tlength?: number,\n\t): IsoBuffer<TArrayBuffer>;\n\n\tpublic static isBuffer(obj: unknown): obj is Buffer;\n}\n\n/**\n * The native Node.js Buffer implementation.\n *\n * @internal\n */\nexport const IsoBuffer: IsoBufferConstructor = Buffer;\n\n/**\n * Converts a `Uint8Array` to a string of the provided encoding.\n * @remarks Useful when the array might be an `IsoBuffer`.\n * @param arr - The array to convert.\n * @param encoding - Optional target encoding; only \"utf8\" and \"base64\" are\n * supported, with \"utf8\" being default.\n * @returns The converted string.\n *\n * @internal\n */\nexport function Uint8ArrayToString(arr: Uint8Array, encoding?: IsoBufferEncoding): string {\n\t// Buffer extends Uint8Array. Therefore, 'arr' may already be a Buffer, in\n\t// which case we can avoid copying the Uint8Array into a new Buffer instance.\n\treturn (IsoBuffer.isBuffer(arr) ? arr : IsoBuffer.from(arr)).toString(encoding);\n}\n\n/**\n * Convert base64 or utf8 string to array buffer.\n * @param encoding - The input string's encoding.\n *\n * @internal\n */\nexport function stringToBuffer(input: string, encoding: IsoBufferEncoding): ArrayBuffer {\n\tconst iso = IsoBuffer.from(input, encoding);\n\t// In a Node environment, IsoBuffer may be a Node.js Buffer. Node.js will\n\t// pool multiple small Buffer instances into a single ArrayBuffer, in which\n\t// case we need to slice the appropriate span of bytes.\n\treturn iso.byteLength === iso.buffer.byteLength\n\t\t? iso.buffer\n\t\t: iso.buffer.slice(iso.byteOffset, iso.byteOffset + iso.byteLength);\n}\n\n/**\n * Convert binary blob to string format\n *\n * @param blob - The binary blob\n * @param encoding - Output string's encoding\n * @returns The blob in string format\n *\n * @internal\n */\nexport const bufferToString = (\n\tblob: ArrayBufferLike | Uint8Array,\n\tencoding: IsoBufferEncoding,\n): string =>\n\tUint8ArrayToString(blob instanceof Uint8Array ? blob : IsoBuffer.from(blob), encoding);\n"]}
@@ -3,10 +3,45 @@
3
3
  * Licensed under the MIT License.
4
4
  */
5
5
  /**
6
- * Converts a Uint8Array array to an ArrayBuffer.
7
- * @param array - Array to convert to ArrayBuffer.
6
+ * Distills an `ArrayBuffer` from an `ArrayBufferLike` or creates one from `SharedArrayBuffer` if needed.
7
+ *
8
+ * @param buffer - `ArrayBuffer` or any `ArrayBufferLike` to convert to `ArrayBuffer`.
9
+ *
10
+ * @remarks The returned buffer may be the input array's backing buffer or a copy of its bytes.
11
+ * Callers should not mutate the returned buffer unless they own the input array and its backing
12
+ * storage, and should not rely on mutations being reflected in the input array.
13
+ *
14
+ * @internal
15
+ */
16
+ export declare function ArrayBufferLikeToArrayBuffer(buffer: ArrayBufferLike): ArrayBuffer;
17
+ /**
18
+ * Distills a `Uint8Array` array to an `ArrayBuffer | SharedArrayBuffer` copying
19
+ * data as needed per the array's `byteOffset` and `byteLength` to have a full
20
+ * buffer (no offset and entire length).
21
+ *
22
+ * @param array - Array to extract buffer from.
23
+ *
24
+ * @remarks The returned buffer may be the input array's backing buffer or a copy of its bytes.
25
+ * Callers should not mutate the returned buffer unless they own the input array and its backing
26
+ * storage, and should not rely on mutations being reflected in the input array.
27
+ *
28
+ * @internal
29
+ */
30
+ export declare function Uint8ArrayToArrayBufferLike<TArrayBuffer extends ArrayBufferLike>(array: Uint8Array<TArrayBuffer>): TArrayBuffer;
31
+ /**
32
+ * Distills a `Uint8Array` array to an `ArrayBuffer` copying data as needed per the array's
33
+ * `byteOffset` and `byteLength` or if underlying buffer is not `ArrayBuffer`.
34
+ *
35
+ * @param array - Array to convert to `ArrayBuffer`.
36
+ *
37
+ * @remarks The returned buffer may be the input array's backing buffer or a copy of its bytes.
38
+ * Callers should not mutate the returned buffer unless they own the input array and its backing
39
+ * storage, and should not rely on mutations being reflected in the input array.
40
+ *
41
+ * Prefer to use {@link Uint8ArrayToArrayBufferLike} if the call site doesn't require
42
+ * an explicit `ArrayBuffer` (can also handle `SharedArrayBuffer`).
8
43
  *
9
44
  * @internal
10
45
  */
11
- export declare function Uint8ArrayToArrayBuffer(array: Uint8Array): ArrayBuffer;
46
+ export declare function AnyUint8ArrayToArrayBuffer(array: Uint8Array): ArrayBuffer;
12
47
  //# sourceMappingURL=bufferShared.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"bufferShared.d.ts","sourceRoot":"","sources":["../src/bufferShared.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH;;;;;GAKG;AACH,wBAAgB,uBAAuB,CAAC,KAAK,EAAE,UAAU,GAAG,WAAW,CAKtE"}
1
+ {"version":3,"file":"bufferShared.d.ts","sourceRoot":"","sources":["../src/bufferShared.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH;;;;;;;;;;GAUG;AACH,wBAAgB,4BAA4B,CAAC,MAAM,EAAE,eAAe,GAAG,WAAW,CASjF;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,2BAA2B,CAAC,YAAY,SAAS,eAAe,EAC/E,KAAK,EAAE,UAAU,CAAC,YAAY,CAAC,GAC7B,YAAY,CAWd;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,0BAA0B,CAAC,KAAK,EAAE,UAAU,GAAG,WAAW,CAgBzE"}
@@ -4,18 +4,80 @@
4
4
  * Licensed under the MIT License.
5
5
  */
6
6
  Object.defineProperty(exports, "__esModule", { value: true });
7
- exports.Uint8ArrayToArrayBuffer = void 0;
7
+ exports.ArrayBufferLikeToArrayBuffer = ArrayBufferLikeToArrayBuffer;
8
+ exports.Uint8ArrayToArrayBufferLike = Uint8ArrayToArrayBufferLike;
9
+ exports.AnyUint8ArrayToArrayBuffer = AnyUint8ArrayToArrayBuffer;
8
10
  /**
9
- * Converts a Uint8Array array to an ArrayBuffer.
10
- * @param array - Array to convert to ArrayBuffer.
11
+ * Distills an `ArrayBuffer` from an `ArrayBufferLike` or creates one from `SharedArrayBuffer` if needed.
12
+ *
13
+ * @param buffer - `ArrayBuffer` or any `ArrayBufferLike` to convert to `ArrayBuffer`.
14
+ *
15
+ * @remarks The returned buffer may be the input array's backing buffer or a copy of its bytes.
16
+ * Callers should not mutate the returned buffer unless they own the input array and its backing
17
+ * storage, and should not rely on mutations being reflected in the input array.
18
+ *
19
+ * @internal
20
+ */
21
+ function ArrayBufferLikeToArrayBuffer(buffer) {
22
+ if (buffer instanceof ArrayBuffer) {
23
+ return buffer;
24
+ }
25
+ // Use .slice to get `ArrayBuffer` backing that `Blob` requires.
26
+ // Note that `buffer.slice` preserves the underlying buffer type, so we
27
+ // need to use .slice on a `Uint8Array` to ensure we get an `ArrayBuffer`.
28
+ // eslint-disable-next-line unicorn/prefer-spread -- spread is not the same as slice for `Uint8Array`
29
+ return new Uint8Array(buffer).slice().buffer;
30
+ }
31
+ /**
32
+ * Distills a `Uint8Array` array to an `ArrayBuffer | SharedArrayBuffer` copying
33
+ * data as needed per the array's `byteOffset` and `byteLength` to have a full
34
+ * buffer (no offset and entire length).
35
+ *
36
+ * @param array - Array to extract buffer from.
37
+ *
38
+ * @remarks The returned buffer may be the input array's backing buffer or a copy of its bytes.
39
+ * Callers should not mutate the returned buffer unless they own the input array and its backing
40
+ * storage, and should not rely on mutations being reflected in the input array.
11
41
  *
12
42
  * @internal
13
43
  */
14
- function Uint8ArrayToArrayBuffer(array) {
44
+ function Uint8ArrayToArrayBufferLike(array) {
15
45
  if (array.byteOffset === 0 && array.byteLength === array.buffer.byteLength) {
16
46
  return array.buffer;
17
47
  }
48
+ // Both ArrayBuffer and SharedArrayBuffer have a slice method and will return
49
+ // their same type, but TypeScript does not correctly determine that will
50
+ // satisfy TArrayBuffer. Cast to TArrayBuffer to indicate that.
18
51
  return array.buffer.slice(array.byteOffset, array.byteOffset + array.byteLength);
19
52
  }
20
- exports.Uint8ArrayToArrayBuffer = Uint8ArrayToArrayBuffer;
53
+ /**
54
+ * Distills a `Uint8Array` array to an `ArrayBuffer` copying data as needed per the array's
55
+ * `byteOffset` and `byteLength` or if underlying buffer is not `ArrayBuffer`.
56
+ *
57
+ * @param array - Array to convert to `ArrayBuffer`.
58
+ *
59
+ * @remarks The returned buffer may be the input array's backing buffer or a copy of its bytes.
60
+ * Callers should not mutate the returned buffer unless they own the input array and its backing
61
+ * storage, and should not rely on mutations being reflected in the input array.
62
+ *
63
+ * Prefer to use {@link Uint8ArrayToArrayBufferLike} if the call site doesn't require
64
+ * an explicit `ArrayBuffer` (can also handle `SharedArrayBuffer`).
65
+ *
66
+ * @internal
67
+ */
68
+ function AnyUint8ArrayToArrayBuffer(array) {
69
+ // Shortcut to provider underlying buffer as-is when possible.
70
+ if (array.byteOffset === 0 &&
71
+ array.byteLength === array.buffer.byteLength &&
72
+ array.buffer instanceof ArrayBuffer) {
73
+ return array.buffer;
74
+ }
75
+ // .slice is used to ensure underlying buffer is:
76
+ // 1. ArrayBuffer
77
+ // 2. the complete data (no offset, full length)
78
+ // Cloning specified per https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/slice
79
+ // and ECMAScript https://tc39.es/ecma262/multipage/indexed-collections.html#sec-%typedarray%.prototype.slice.
80
+ // eslint-disable-next-line unicorn/prefer-spread -- spread is not the same as slice for `Uint8Array`
81
+ return array.slice().buffer;
82
+ }
21
83
  //# sourceMappingURL=bufferShared.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"bufferShared.js","sourceRoot":"","sources":["../src/bufferShared.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;AAEH;;;;;GAKG;AACH,SAAgB,uBAAuB,CAAC,KAAiB;IACxD,IAAI,KAAK,CAAC,UAAU,KAAK,CAAC,IAAI,KAAK,CAAC,UAAU,KAAK,KAAK,CAAC,MAAM,CAAC,UAAU,EAAE,CAAC;QAC5E,OAAO,KAAK,CAAC,MAAM,CAAC;IACrB,CAAC;IACD,OAAO,KAAK,CAAC,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC,UAAU,EAAE,KAAK,CAAC,UAAU,GAAG,KAAK,CAAC,UAAU,CAAC,CAAC;AAClF,CAAC;AALD,0DAKC","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\n/**\n * Converts a Uint8Array array to an ArrayBuffer.\n * @param array - Array to convert to ArrayBuffer.\n *\n * @internal\n */\nexport function Uint8ArrayToArrayBuffer(array: Uint8Array): ArrayBuffer {\n\tif (array.byteOffset === 0 && array.byteLength === array.buffer.byteLength) {\n\t\treturn array.buffer;\n\t}\n\treturn array.buffer.slice(array.byteOffset, array.byteOffset + array.byteLength);\n}\n"]}
1
+ {"version":3,"file":"bufferShared.js","sourceRoot":"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right (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\n/**\n * Distills an `ArrayBuffer` from an `ArrayBufferLike` or creates one from `SharedArrayBuffer` if needed.\n *\n * @param buffer - `ArrayBuffer` or any `ArrayBufferLike` to convert to `ArrayBuffer`.\n *\n * @remarks The returned buffer may be the input array's backing buffer or a copy of its bytes.\n * Callers should not mutate the returned buffer unless they own the input array and its backing\n * storage, and should not rely on mutations being reflected in the input array.\n *\n * @internal\n */\nexport function ArrayBufferLikeToArrayBuffer(buffer: ArrayBufferLike): ArrayBuffer {\n\tif (buffer instanceof ArrayBuffer) {\n\t\treturn buffer;\n\t}\n\t// Use .slice to get `ArrayBuffer` backing that `Blob` requires.\n\t// Note that `buffer.slice` preserves the underlying buffer type, so we\n\t// need to use .slice on a `Uint8Array` to ensure we get an `ArrayBuffer`.\n\t// eslint-disable-next-line unicorn/prefer-spread -- spread is not the same as slice for `Uint8Array`\n\treturn new Uint8Array(buffer).slice().buffer;\n}\n\n/**\n * Distills a `Uint8Array` array to an `ArrayBuffer | SharedArrayBuffer` copying\n * data as needed per the array's `byteOffset` and `byteLength` to have a full\n * buffer (no offset and entire length).\n *\n * @param array - Array to extract buffer from.\n *\n * @remarks The returned buffer may be the input array's backing buffer or a copy of its bytes.\n * Callers should not mutate the returned buffer unless they own the input array and its backing\n * storage, and should not rely on mutations being reflected in the input array.\n *\n * @internal\n */\nexport function Uint8ArrayToArrayBufferLike<TArrayBuffer extends ArrayBufferLike>(\n\tarray: Uint8Array<TArrayBuffer>,\n): TArrayBuffer {\n\tif (array.byteOffset === 0 && array.byteLength === array.buffer.byteLength) {\n\t\treturn array.buffer;\n\t}\n\t// Both ArrayBuffer and SharedArrayBuffer have a slice method and will return\n\t// their same type, but TypeScript does not correctly determine that will\n\t// satisfy TArrayBuffer. Cast to TArrayBuffer to indicate that.\n\treturn array.buffer.slice(\n\t\tarray.byteOffset,\n\t\tarray.byteOffset + array.byteLength,\n\t) as TArrayBuffer;\n}\n\n/**\n * Distills a `Uint8Array` array to an `ArrayBuffer` copying data as needed per the array's\n * `byteOffset` and `byteLength` or if underlying buffer is not `ArrayBuffer`.\n *\n * @param array - Array to convert to `ArrayBuffer`.\n *\n * @remarks The returned buffer may be the input array's backing buffer or a copy of its bytes.\n * Callers should not mutate the returned buffer unless they own the input array and its backing\n * storage, and should not rely on mutations being reflected in the input array.\n *\n * Prefer to use {@link Uint8ArrayToArrayBufferLike} if the call site doesn't require\n * an explicit `ArrayBuffer` (can also handle `SharedArrayBuffer`).\n *\n * @internal\n */\nexport function AnyUint8ArrayToArrayBuffer(array: Uint8Array): ArrayBuffer {\n\t// Shortcut to provider underlying buffer as-is when possible.\n\tif (\n\t\tarray.byteOffset === 0 &&\n\t\tarray.byteLength === array.buffer.byteLength &&\n\t\tarray.buffer instanceof ArrayBuffer\n\t) {\n\t\treturn array.buffer;\n\t}\n\t// .slice is used to ensure underlying buffer is:\n\t// 1. ArrayBuffer\n\t// 2. the complete data (no offset, full length)\n\t// Cloning specified per https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/slice\n\t// and ECMAScript https://tc39.es/ecma262/multipage/indexed-collections.html#sec-%typedarray%.prototype.slice.\n\t// eslint-disable-next-line unicorn/prefer-spread -- spread is not the same as slice for `Uint8Array`\n\treturn array.slice().buffer;\n}\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"emitter.d.ts","sourceRoot":"","sources":["../../src/events/emitter.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EACX,YAAY,EACZ,QAAQ,EACR,UAAU,EACV,SAAS,EAET,mBAAmB,EACnB,GAAG,EACH,MAAM,0CAA0C,CAAC;AAmBlD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoDG;AACH,qBAAa,kBAAkB,CAAC,UAAU,SAAS,SAAS,CAAC,UAAU,CAAC,CACvE,YAAW,UAAU,CAAC,UAAU,CAAC,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UAAU,GAAG,OAAO;CAM1D;AA6BD;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,wBAAgB,aAAa,CAAC,UAAU,SAAS,MAAM,EACtD,WAAW,CAAC,EAAE,mBAAmB,CAAC,UAAU,CAAC,GAC3C,UAAU,CAAC,UAAU,CAAC,GAAG,QAAQ,CAAC,UAAU,CAAC,GAAG,YAAY,CAAC,UAAU,CAAC,CAE1E"}
@@ -4,7 +4,8 @@
4
4
  * Licensed under the MIT License.
5
5
  */
6
6
  Object.defineProperty(exports, "__esModule", { value: true });
7
- exports.createEmitter = exports.CustomEventEmitter = void 0;
7
+ exports.CustomEventEmitter = void 0;
8
+ exports.createEmitter = createEmitter;
8
9
  /**
9
10
  * Retrieve a value from a map with the given key, or create a new entry if the key is not in the map.
10
11
  * @param map - The map to query/update
@@ -75,11 +76,12 @@ function getOrCreate(map, key, defaultValue) {
75
76
  * @internal
76
77
  */
77
78
  class CustomEventEmitter {
79
+ noListeners;
80
+ listeners = new Map();
78
81
  // Because this is protected and not public, calling this externally (not from a subclass) makes sending events to the constructed instance impossible.
79
82
  // Instead, use the static `create` function to get an instance which allows emitting events.
80
83
  constructor(noListeners) {
81
84
  this.noListeners = noListeners;
82
- this.listeners = new Map();
83
85
  }
84
86
  emit(eventName, ...args) {
85
87
  const listeners = this.listeners.get(eventName);
@@ -178,5 +180,4 @@ class ComposableEventEmitter extends CustomEventEmitter {
178
180
  function createEmitter(noListeners) {
179
181
  return new ComposableEventEmitter(noListeners);
180
182
  }
181
- exports.createEmitter = createEmitter;
182
183
  //# sourceMappingURL=emitter.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"emitter.js","sourceRoot":"","sources":["../../src/events/emitter.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;AAYH;;;;;;;GAOG;AACH,SAAS,WAAW,CAAO,GAAoB,EAAE,GAAM,EAAE,YAA2B;IACnF,IAAI,KAAK,GAAG,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,CAA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GAAG,CAAC,CAAC;QAChC,CAAC;QACD,OAAO,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC;IACtC,CAAC;CACD;AAhFD,gDAgFC;AAED;;;GAGG;AACH,MAAM,sBACL,SAAQ,kBAA8B;IAGtC,YAAmB,WAA6C;QAC/D,KAAK,CAAC,WAAW,CAAC,CAAC;IACpB,CAAC;IAEe,IAAI,CACnB,SAAY,EACZ,GAAG,IAA+B;QAElC,OAAO,KAAK,CAAC,IAAI,CAAC,SAAS,EAAE,GAAG,IAAI,CAAC,CAAC;IACvC,CAAC;IAEe,cAAc,CAC7B,SAAY,EACZ,GAAG,IAA+B;QAElC,OAAO,KAAK,CAAC,cAAc,CAAC,SAAS,EAAE,GAAG,IAAI,CAAC,CAAC;IACjD,CAAC;CACD;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,SAAgB,aAAa,CAC5B,WAA6C;IAE7C,OAAO,IAAI,sBAAsB,CAAa,WAAW,CAAC,CAAC;AAC5D,CAAC;AAJD,sCAIC","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\nimport type {\n\tHasListeners,\n\tIEmitter,\n\tListenable,\n\tListeners,\n\tMapGetSet,\n\tNoListenersCallback,\n\tOff,\n} from \"@fluidframework/core-interfaces/internal\";\n\n/**\n * Retrieve a value from a map with the given key, or create a new entry if the key is not in the map.\n * @param map - The map to query/update\n * @param key - The key to lookup in the map\n * @param defaultValue - a function which returns a default value. This is called and used to set an initial value for the given key in the map if none exists\n * @returns either the existing value for the given key, or the newly-created value (the result of `defaultValue`)\n * @internal\n */\nfunction getOrCreate<K, V>(map: MapGetSet<K, V>, key: K, defaultValue: (key: K) => V): V {\n\tlet value = map.get(key);\n\tif (value === undefined) {\n\t\tvalue = defaultValue(key);\n\t\tmap.set(key, value);\n\t}\n\treturn value;\n}\n\n/**\n * Provides an API for subscribing to and listening to events.\n *\n * @remarks Classes wishing to emit events may either extend this class, compose over it, or expose it as a property of type {@link @fluidframework/core-interfaces#Listenable}.\n *\n * @example Extending this class\n *\n * ```typescript\n * interface MyEvents {\n * \tloaded: () => void;\n * \tcomputed: () => number;\n * }\n *\n * class MyInheritanceClass extends EventEmitter<MyEvents> {\n * \tprivate load() {\n * \t\tthis.emit(\"loaded\");\n * \t\tconst results: number[] = this.emitAndCollect(\"computed\");\n * \t}\n * }\n * ```\n *\n * @example Composing over this class\n *\n * ```typescript\n * class MyCompositionClass implements Listenable<MyEvents> {\n * \tprivate readonly events = createEmitter<MyEvents>();\n *\n * \tprivate load() {\n * \t\tthis.events.emit(\"loaded\");\n * \t\tconst results: number[] = this.events.emitAndCollect(\"computed\");\n * \t}\n *\n * \tpublic on<K extends keyof MyEvents>(eventName: K, listener: MyEvents[K]): () => void {\n * \t\treturn this.events.on(eventName, listener);\n * \t}\n * }\n * ```\n *\n * @example Exposing this class as a property\n *\n * ```typescript\n * class MyExposingClass {\n * \tprivate readonly _events = createEmitter<MyEvents>();\n * \tpublic readonly events: Listenable<MyEvents> = this._events;\n *\n * \tprivate load() {\n * \t\tthis._events.emit(\"loaded\");\n * \t\tconst results: number[] = this._events.emitAndCollect(\"computed\");\n * \t}\n * }\n * ```\n * @internal\n */\nexport class CustomEventEmitter<TListeners extends Listeners<TListeners>>\n\timplements Listenable<TListeners>, HasListeners<TListeners>\n{\n\tprotected readonly listeners = new Map<\n\t\tkeyof TListeners,\n\t\tSet<(...args: any[]) => TListeners[keyof TListeners]>\n\t>();\n\n\t// Because this is protected and not public, calling this externally (not from a subclass) makes sending events to the constructed instance impossible.\n\t// Instead, use the static `create` function to get an instance which allows emitting events.\n\tprotected constructor(private readonly noListeners?: NoListenersCallback<TListeners>) {}\n\n\tprotected emit<K extends keyof TListeners>(\n\t\teventName: K,\n\t\t...args: Parameters<TListeners[K]>\n\t): void {\n\t\tconst listeners = this.listeners.get(eventName);\n\t\tif (listeners !== undefined) {\n\t\t\t// Current tsc (5.4.5) cannot spread `args` into `listener()`.\n\t\t\tconst argArray: unknown[] = args;\n\t\t\t// This explicitly copies listeners so that new listeners added during this call to emit will not receive this event.\n\t\t\tfor (const listener of [...listeners]) {\n\t\t\t\t// If listener has been unsubscribed while invoking other listeners, skip it.\n\t\t\t\tif (listeners.has(listener)) {\n\t\t\t\t\tlistener(...argArray);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\tprotected emitAndCollect<K extends keyof TListeners>(\n\t\teventName: K,\n\t\t...args: Parameters<TListeners[K]>\n\t): ReturnType<TListeners[K]>[] {\n\t\tconst listeners = this.listeners.get(eventName);\n\t\tif (listeners !== undefined) {\n\t\t\tconst argArray: unknown[] = args;\n\t\t\tconst resultArray: ReturnType<TListeners[K]>[] = [];\n\t\t\tfor (const listener of [...listeners.values()]) {\n\t\t\t\tresultArray.push(listener(...argArray));\n\t\t\t}\n\t\t\treturn resultArray;\n\t\t}\n\t\treturn [];\n\t}\n\n\tpublic on<K extends keyof Listeners<TListeners>>(\n\t\teventName: K,\n\t\tlistener: TListeners[K],\n\t): Off {\n\t\tconst listeners = getOrCreate(this.listeners, eventName, () => new Set());\n\t\tif (listeners.has(listener)) {\n\t\t\tconst eventDescription =\n\t\t\t\ttypeof eventName === \"symbol\" ? eventName.description : String(eventName.toString());\n\n\t\t\tthrow new Error(\n\t\t\t\t`Attempted to register the same listener object twice for event ${eventDescription}`,\n\t\t\t);\n\t\t}\n\t\tlisteners.add(listener);\n\t\treturn () => this.off(eventName, listener);\n\t}\n\n\tpublic off<K extends keyof Listeners<TListeners>>(\n\t\teventName: K,\n\t\tlistener: TListeners[K],\n\t): void {\n\t\tconst listeners = this.listeners.get(eventName);\n\t\tif (listeners?.delete(listener) === true && listeners.size === 0) {\n\t\t\tthis.listeners.delete(eventName);\n\t\t\tthis.noListeners?.(eventName);\n\t\t}\n\t}\n\n\tpublic hasListeners(eventName?: keyof TListeners): boolean {\n\t\tif (eventName === undefined) {\n\t\t\treturn this.listeners.size > 0;\n\t\t}\n\t\treturn this.listeners.has(eventName);\n\t}\n}\n\n/**\n * This class exposes the constructor and the `emit` method of `EventEmitter`, elevating them from protected to public\n * @internal\n */\nclass ComposableEventEmitter<TListeners extends Listeners<TListeners>>\n\textends CustomEventEmitter<TListeners>\n\timplements IEmitter<TListeners>\n{\n\tpublic constructor(noListeners?: NoListenersCallback<TListeners>) {\n\t\tsuper(noListeners);\n\t}\n\n\tpublic override emit<K extends keyof TListeners>(\n\t\teventName: K,\n\t\t...args: Parameters<TListeners[K]>\n\t): void {\n\t\treturn super.emit(eventName, ...args);\n\t}\n\n\tpublic override emitAndCollect<K extends keyof TListeners>(\n\t\teventName: K,\n\t\t...args: Parameters<TListeners[K]>\n\t): ReturnType<TListeners[K]>[] {\n\t\treturn super.emitAndCollect(eventName, ...args);\n\t}\n}\n\n/**\n * Create a {@link @fluidframework/core-interfaces#Listenable} that can be instructed to emit events via the {@link @fluidframework/core-interfaces#IEmitter} interface.\n *\n * A class can delegate handling {@link @fluidframework/core-interfaces#Listenable} to the returned value while using it to emit the events.\n * See also CustomEventEmitter which be used as a base class to implement {@link @fluidframework/core-interfaces#Listenable} via extension.\n * @example Forwarding events to the emitter\n * ```typescript\n * interface MyEvents {\n * \tloaded(): void;\n * }\n *\n * class MyClass implements Listenable<MyEvents> {\n * \tprivate readonly events = createEmitter<MyEvents>();\n *\n * \tprivate load(): void {\n * \t\tthis.events.emit(\"loaded\");\n * \t}\n *\n * \tpublic on<K extends keyof MyEvents>(eventName: K, listener: MyEvents[K]): Off {\n * \t\treturn this.events.on(eventName, listener);\n * \t}\n *\n * \tpublic off<K extends keyof MyEvents>(eventName: K, listener: MyEvents[K]): void {\n * \t\treturn this.events.off(eventName, listener);\n * \t}\n * }\n * ```\n * @internal\n */\nexport function createEmitter<TListeners extends object>(\n\tnoListeners?: NoListenersCallback<TListeners>,\n): Listenable<TListeners> & IEmitter<TListeners> & HasListeners<TListeners> {\n\treturn new ComposableEventEmitter<TListeners>(noListeners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AAC;YAClE,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC;YACjC,IAAI,CAAC,WAAW,EAAE,CAAC,SAAS,CAAC,CAAC;QAC/B,CAAC;IACF,CAAC;IAEM,YAAY,CAAC,SAA4B;QAC/C,IAAI,SAAS,KAAK,SAAS,EAAE,CAAC;YAC7B,OAAO,IAAI,CAAC,SAAS,CAAC,IAAI,GAAG,CAAC,CAAC;QAChC,CAAC;QACD,OAAO,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC;IACtC,CAAC;CACD;AAhFD,gDAgFC;AAED;;;GAGG;AACH,MAAM,sBACL,SAAQ,kBAA8B;IAGtC,YAAmB,WAA6C;QAC/D,KAAK,CAAC,WAAW,CAAC,CAAC;IACpB,CAAC;IAEe,IAAI,CACnB,SAAY,EACZ,GAAG,IAA+B;QAElC,OAAO,KAAK,CAAC,IAAI,CAAC,SAAS,EAAE,GAAG,IAAI,CAAC,CAAC;IACvC,CAAC;IAEe,cAAc,CAC7B,SAAY,EACZ,GAAG,IAA+B;QAElC,OAAO,KAAK,CAAC,cAAc,CAAC,SAAS,EAAE,GAAG,IAAI,CAAC,CAAC;IACjD,CAAC;CACD;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,SAAgB,aAAa,CAC5B,WAA6C;IAE7C,OAAO,IAAI,sBAAsB,CAAa,WAAW,CAAC,CAAC;AAC5D,CAAC","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\nimport type {\n\tHasListeners,\n\tIEmitter,\n\tListenable,\n\tListeners,\n\tMapGetSet,\n\tNoListenersCallback,\n\tOff,\n} from \"@fluidframework/core-interfaces/internal\";\n\n/**\n * Retrieve a value from a map with the given key, or create a new entry if the key is not in the map.\n * @param map - The map to query/update\n * @param key - The key to lookup in the map\n * @param defaultValue - a function which returns a default value. This is called and used to set an initial value for the given key in the map if none exists\n * @returns either the existing value for the given key, or the newly-created value (the result of `defaultValue`)\n * @internal\n */\nfunction getOrCreate<K, V>(map: MapGetSet<K, V>, key: K, defaultValue: (key: K) => V): V {\n\tlet value = map.get(key);\n\tif (value === undefined) {\n\t\tvalue = defaultValue(key);\n\t\tmap.set(key, value);\n\t}\n\treturn value;\n}\n\n/**\n * Provides an API for subscribing to and listening to events.\n *\n * @remarks Classes wishing to emit events may either extend this class, compose over it, or expose it as a property of type {@link @fluidframework/core-interfaces#Listenable}.\n *\n * @example Extending this class\n *\n * ```typescript\n * interface MyEvents {\n * \tloaded: () => void;\n * \tcomputed: () => number;\n * }\n *\n * class MyInheritanceClass extends EventEmitter<MyEvents> {\n * \tprivate load() {\n * \t\tthis.emit(\"loaded\");\n * \t\tconst results: number[] = this.emitAndCollect(\"computed\");\n * \t}\n * }\n * ```\n *\n * @example Composing over this class\n *\n * ```typescript\n * class MyCompositionClass implements Listenable<MyEvents> {\n * \tprivate readonly events = createEmitter<MyEvents>();\n *\n * \tprivate load() {\n * \t\tthis.events.emit(\"loaded\");\n * \t\tconst results: number[] = this.events.emitAndCollect(\"computed\");\n * \t}\n *\n * \tpublic on<K extends keyof MyEvents>(eventName: K, listener: MyEvents[K]): () => void {\n * \t\treturn this.events.on(eventName, listener);\n * \t}\n * }\n * ```\n *\n * @example Exposing this class as a property\n *\n * ```typescript\n * class MyExposingClass {\n * \tprivate readonly _events = createEmitter<MyEvents>();\n * \tpublic readonly events: Listenable<MyEvents> = this._events;\n *\n * \tprivate load() {\n * \t\tthis._events.emit(\"loaded\");\n * \t\tconst results: number[] = this._events.emitAndCollect(\"computed\");\n * \t}\n * }\n * ```\n * @internal\n */\nexport class CustomEventEmitter<TListeners extends Listeners<TListeners>>\n\timplements Listenable<TListeners>, HasListeners<TListeners>\n{\n\tprotected readonly listeners = new Map<\n\t\tkeyof TListeners,\n\t\tSet<(...args: any[]) => TListeners[keyof TListeners]>\n\t>();\n\n\t// Because this is protected and not public, calling this externally (not from a subclass) makes sending events to the constructed instance impossible.\n\t// Instead, use the static `create` function to get an instance which allows emitting events.\n\tprotected constructor(private readonly noListeners?: NoListenersCallback<TListeners>) {}\n\n\tprotected emit<K extends keyof TListeners>(\n\t\teventName: K,\n\t\t...args: Parameters<TListeners[K]>\n\t): void {\n\t\tconst listeners = this.listeners.get(eventName);\n\t\tif (listeners !== undefined) {\n\t\t\t// Current tsc (5.4.5) cannot spread `args` into `listener()`.\n\t\t\tconst argArray: unknown[] = args;\n\t\t\t// This explicitly copies listeners so that new listeners added during this call to emit will not receive this event.\n\t\t\tfor (const listener of [...listeners]) {\n\t\t\t\t// If listener has been unsubscribed while invoking other listeners, skip it.\n\t\t\t\tif (listeners.has(listener)) {\n\t\t\t\t\tlistener(...argArray);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\tprotected emitAndCollect<K extends keyof TListeners>(\n\t\teventName: K,\n\t\t...args: Parameters<TListeners[K]>\n\t): ReturnType<TListeners[K]>[] {\n\t\tconst listeners = this.listeners.get(eventName);\n\t\tif (listeners !== undefined) {\n\t\t\tconst argArray: unknown[] = args;\n\t\t\tconst resultArray: ReturnType<TListeners[K]>[] = [];\n\t\t\tfor (const listener of [...listeners.values()]) {\n\t\t\t\tresultArray.push(listener(...argArray));\n\t\t\t}\n\t\t\treturn resultArray;\n\t\t}\n\t\treturn [];\n\t}\n\n\tpublic on<K extends keyof Listeners<TListeners>>(\n\t\teventName: K,\n\t\tlistener: TListeners[K],\n\t): Off {\n\t\tconst listeners = getOrCreate(this.listeners, eventName, () => new Set());\n\t\tif (listeners.has(listener)) {\n\t\t\tconst eventDescription =\n\t\t\t\ttypeof eventName === \"symbol\" ? eventName.description : String(eventName.toString());\n\n\t\t\tthrow new Error(\n\t\t\t\t`Attempted to register the same listener object twice for event ${eventDescription}`,\n\t\t\t);\n\t\t}\n\t\tlisteners.add(listener);\n\t\treturn () => this.off(eventName, listener);\n\t}\n\n\tpublic off<K extends keyof Listeners<TListeners>>(\n\t\teventName: K,\n\t\tlistener: TListeners[K],\n\t): void {\n\t\tconst listeners = this.listeners.get(eventName);\n\t\tif (listeners?.delete(listener) === true && listeners.size === 0) {\n\t\t\tthis.listeners.delete(eventName);\n\t\t\tthis.noListeners?.(eventName);\n\t\t}\n\t}\n\n\tpublic hasListeners(eventName?: keyof TListeners): boolean {\n\t\tif (eventName === undefined) {\n\t\t\treturn this.listeners.size > 0;\n\t\t}\n\t\treturn this.listeners.has(eventName);\n\t}\n}\n\n/**\n * This class exposes the constructor and the `emit` method of `EventEmitter`, elevating them from protected to public\n * @internal\n */\nclass ComposableEventEmitter<TListeners extends Listeners<TListeners>>\n\textends CustomEventEmitter<TListeners>\n\timplements IEmitter<TListeners>\n{\n\tpublic constructor(noListeners?: NoListenersCallback<TListeners>) {\n\t\tsuper(noListeners);\n\t}\n\n\tpublic override emit<K extends keyof TListeners>(\n\t\teventName: K,\n\t\t...args: Parameters<TListeners[K]>\n\t): void {\n\t\treturn super.emit(eventName, ...args);\n\t}\n\n\tpublic override emitAndCollect<K extends keyof TListeners>(\n\t\teventName: K,\n\t\t...args: Parameters<TListeners[K]>\n\t): ReturnType<TListeners[K]>[] {\n\t\treturn super.emitAndCollect(eventName, ...args);\n\t}\n}\n\n/**\n * Create a {@link @fluidframework/core-interfaces#Listenable} that can be instructed to emit events via the {@link @fluidframework/core-interfaces#IEmitter} interface.\n *\n * A class can delegate handling {@link @fluidframework/core-interfaces#Listenable} to the returned value while using it to emit the events.\n * See also CustomEventEmitter which be used as a base class to implement {@link @fluidframework/core-interfaces#Listenable} via extension.\n * @example Forwarding events to the emitter\n * ```typescript\n * interface MyEvents {\n * \tloaded(): void;\n * }\n *\n * class MyClass implements Listenable<MyEvents> {\n * \tprivate readonly events = createEmitter<MyEvents>();\n *\n * \tprivate load(): void {\n * \t\tthis.events.emit(\"loaded\");\n * \t}\n *\n * \tpublic on<K extends keyof MyEvents>(eventName: K, listener: MyEvents[K]): Off {\n * \t\treturn this.events.on(eventName, listener);\n * \t}\n *\n * \tpublic off<K extends keyof MyEvents>(eventName: K, listener: MyEvents[K]): void {\n * \t\treturn this.events.off(eventName, listener);\n * \t}\n * }\n * ```\n * @internal\n */\nexport function createEmitter<TListeners extends object>(\n\tnoListeners?: NoListenersCallback<TListeners>,\n): Listenable<TListeners> & IEmitter<TListeners> & HasListeners<TListeners> {\n\treturn new ComposableEventEmitter<TListeners>(noListeners);\n}\n"]}
@@ -16,7 +16,7 @@ import { IsoBuffer } from "./bufferBrowser.js";
16
16
  *
17
17
  * @internal
18
18
  */
19
- export declare function hashFile(file: IsoBuffer, algorithm?: "SHA-1" | "SHA-256", hashEncoding?: "hex" | "base64"): Promise<string>;
19
+ export declare function hashFile(file: IsoBuffer<ArrayBuffer>, algorithm?: "SHA-1" | "SHA-256", hashEncoding?: "hex" | "base64"): Promise<string>;
20
20
  /**
21
21
  * Create a github hash (Github hashes the string with blob and size)
22
22
  * Must be called under secure context for browsers
@@ -1 +1 @@
1
- {"version":3,"file":"hashFileBrowser.d.ts","sourceRoot":"","sources":["../src/hashFileBrowser.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAKH,OAAO,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAC;AA4B/C;;;;;;;;;;;;GAYG;AACH,wBAAsB,QAAQ,CAC7B,IAAI,EAAE,SAAS,EACf,SAAS,GAAE,OAAO,GAAG,SAAmB,EACxC,YAAY,GAAE,KAAK,GAAG,QAAgB,GACpC,OAAO,CAAC,MAAM,CAAC,CAgBjB;AAED;;;;;;;;GAQG;AACH,wBAAsB,WAAW,CAAC,IAAI,EAAE,SAAS,GAAG,OAAO,CAAC,MAAM,CAAC,CAQlE"}
1
+ {"version":3,"file":"hashFileBrowser.d.ts","sourceRoot":"","sources":["../src/hashFileBrowser.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAKH,OAAO,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAC;AA4B/C;;;;;;;;;;;;GAYG;AACH,wBAAsB,QAAQ,CAC7B,IAAI,EAAE,SAAS,CAAC,WAAW,CAAC,EAC5B,SAAS,GAAE,OAAO,GAAG,SAAmB,EACxC,YAAY,GAAE,KAAK,GAAG,QAAgB,GACpC,OAAO,CAAC,MAAM,CAAC,CAgBjB;AAED;;;;;;;;GAQG;AACH,wBAAsB,WAAW,CAAC,IAAI,EAAE,SAAS,GAAG,OAAO,CAAC,MAAM,CAAC,CAWlE"}
@@ -19,15 +19,26 @@ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (
19
19
  }) : function(o, v) {
20
20
  o["default"] = v;
21
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
- };
22
+ var __importStar = (this && this.__importStar) || (function () {
23
+ var ownKeys = function(o) {
24
+ ownKeys = Object.getOwnPropertyNames || function (o) {
25
+ var ar = [];
26
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
27
+ return ar;
28
+ };
29
+ return ownKeys(o);
30
+ };
31
+ return function (mod) {
32
+ if (mod && mod.__esModule) return mod;
33
+ var result = {};
34
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
35
+ __setModuleDefault(result, mod);
36
+ return result;
37
+ };
38
+ })();
29
39
  Object.defineProperty(exports, "__esModule", { value: true });
30
- exports.gitHashFile = exports.hashFile = void 0;
40
+ exports.hashFile = hashFile;
41
+ exports.gitHashFile = gitHashFile;
31
42
  const base64js = __importStar(require("base64-js"));
32
43
  // Note: See 'Isomorphic Code' section in the package README.md
33
44
  const bufferBrowser_js_1 = require("./bufferBrowser.js");
@@ -80,7 +91,6 @@ async function hashFile(file, algorithm = "SHA-1", hashEncoding = "hex") {
80
91
  const hashArray = await digestBuffer(file, algorithm);
81
92
  return encodeDigest(hashArray, hashEncoding);
82
93
  }
83
- exports.hashFile = hashFile;
84
94
  /**
85
95
  * Create a github hash (Github hashes the string with blob and size)
86
96
  * Must be called under secure context for browsers
@@ -94,9 +104,11 @@ async function gitHashFile(file) {
94
104
  const size = file.byteLength;
95
105
  // eslint-disable-next-line unicorn/prefer-code-point
96
106
  const filePrefix = `blob ${size.toString()}${String.fromCharCode(0)}`;
97
- const hashBuffer = bufferBrowser_js_1.IsoBuffer.from(filePrefix + file.toString());
107
+ const prefixBuffer = bufferBrowser_js_1.IsoBuffer.from(filePrefix);
108
+ const hashBuffer = new bufferBrowser_js_1.IsoBuffer(prefixBuffer.byteLength + file.byteLength);
109
+ hashBuffer.set(prefixBuffer);
110
+ hashBuffer.set(file, prefixBuffer.byteLength);
98
111
  // hashFile uses sha1; if that changes this will need to change too
99
112
  return hashFile(hashBuffer);
100
113
  }
101
- exports.gitHashFile = gitHashFile;
102
114
  //# sourceMappingURL=hashFileBrowser.js.map