All files bufferNode.ts

100% Statements 9/9
50% Branches 2/4
100% Functions 3/3
100% Lines 8/8

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82                                                        1x                                     1x   2x                   1x 4x       4x                             1x 22x  
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
 
/**
 * Declare the subset of Buffer functionality we want to make available instead of
 * exposing the entirely of Node's typings.  This should match the public interface
 * of the browser implementation, so any changes made in one should be made in both.
 *
 * @deprecated Moved to the `@fluidframework-internal/client-utils` package.
 * @internal
 */
export declare class Buffer extends Uint8Array {
	toString(encoding?: string): string;
	/**
	 * @param value - (string | ArrayBuffer).
	 * @param encodingOrOffset - (string | number).
	 * @param length - (number).
	 */
	static from(value, encodingOrOffset?, length?): IsoBuffer;
	static isBuffer(obj: any): obj is Buffer;
}
 
/**
 * @deprecated Moved to the `@fluidframework-internal/client-utils` package.
 * @internal
 */
export const IsoBuffer = Buffer;
 
/**
 * @deprecated Moved to the `@fluidframework-internal/client-utils` package.
 * @internal
 */
export type IsoBuffer = Buffer;
 
/**
 * Converts a Uint8Array to a string of the provided encoding.
 * @remarks Useful when the array might be an IsoBuffer.
 * @param arr - The array to convert.
 * @param encoding - Optional target encoding; only "utf8" and "base64" are
 * supported, with "utf8" being default.
 * @returns The converted string.
 *
 * @deprecated Moved to the `@fluidframework-internal/client-utils` package.
 * @internal
 */
export function Uint8ArrayToString(arr: Uint8Array, encoding?: string): string {
	// Make this check because Buffer.from(arr) will always do a buffer copy
	return Buffer.isBuffer(arr) ? arr.toString(encoding) : Buffer.from(arr).toString(encoding);
}
 
/**
 * Convert base64 or utf8 string to array buffer.
 * @param encoding - The input string's encoding.
 *
 * @deprecated Moved to the `@fluidframework-internal/client-utils` package.
 * @internal
 */
export function stringToBuffer(input: string, encoding: string): ArrayBufferLike {
	const iso = IsoBuffer.from(input, encoding);
	// In a Node environment, IsoBuffer may be a Node.js Buffer.  Node.js will
	// pool multiple small Buffer instances into a single ArrayBuffer, in which
	// case we need to slice the appropriate span of bytes.
	return iso.byteLength === iso.buffer.byteLength
		? iso.buffer
		: iso.buffer.slice(iso.byteOffset, iso.byteOffset + iso.byteLength);
}
 
/**
 * Convert binary blob to string format
 *
 * @param blob - The binary blob
 * @param encoding - Output string's encoding
 * @returns The blob in string format
 *
 * @deprecated Moved to the `@fluidframework-internal/client-utils` package.
 * @internal
 */
export const bufferToString = (blob: ArrayBufferLike, encoding: string): string =>
	IsoBuffer.from(blob).toString(encoding);