Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export function fromArrayBuffer(
input: ArrayBuffer,
offset: number = 0,
length: number = input.byteLength - offset
): Buffer {
if (!isArrayBuffer(input)) {
throw new Error(
"argument passed to fromArrayBuffer was not an ArrayBuffer"
);
}
if (typeof Buffer.from === "function" && Buffer.from !== Uint8Array.from) {
return Buffer.from(input, offset, length);
}
// Any version of node that supports the optional offset and length
// parameters, which were added in Node 6.0.0, will support Buffer.from and
// have already returned. Throw if offset is not 0 or if length differs from
// the underlying buffer's length.
if (offset !== 0 || length !== input.byteLength) {
throw new Error(
`Unable to convert TypedArray to Buffer in Node ${process.version}`
export async function getPayloadHash(
{ headers, body }: HttpRequest,
hashConstructor: HashConstructor
): Promise {
for (const headerName of Object.keys(headers)) {
if (headerName.toLowerCase() === SHA256_HEADER) {
return headers[headerName];
}
}
if (body == undefined) {
return "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855";
} else if (
typeof body === "string" ||
ArrayBuffer.isView(body) ||
isArrayBuffer(body)
) {
const hashCtor = new hashConstructor();
hashCtor.update(body);
return toHex(await hashCtor.digest());
}
// As any defined body that is not a string or binary data is a stream, this
// body is unsignable. Attempt to send the request with an unsigned payload,
// which may or may not be accepted by the service.
return UNSIGNED_PAYLOAD;
}
private resolveBodyString(input: HttpResponse): Promise {
const { body = "" } = input;
if (typeof body === "string") {
return Promise.resolve(body);
}
let bufferPromise: Promise;
if (ArrayBuffer.isView(body)) {
bufferPromise = Promise.resolve(
new Uint8Array(body.buffer, body.byteLength, body.byteOffset)
);
} else if (isArrayBuffer(body)) {
bufferPromise = Promise.resolve(new Uint8Array(body, 0, body.byteLength));
} else {
bufferPromise = this.bodyCollector(body);
}
return bufferPromise.then(buffer => this.utf8Encoder(buffer));
}
}
): BuildHandler => async (
args: BuildHandlerArguments
): Promise> => {
let { request } = args;
if (HttpRequest.isInstance(request)) {
const { body, headers } = request;
if (!hasHeader("Content-MD5", headers)) {
let digest: Promise;
if (
body === undefined ||
typeof body === "string" ||
ArrayBuffer.isView(body) ||
isArrayBuffer(body)
) {
const hash = new options.md5();
hash.update(body || "");
digest = hash.digest();
} else {
digest = options.streamHasher(options.md5, body);
}
request = {
...request,
headers: {
...headers,
"Content-MD5": options.base64Encoder(await digest)
}
};
}
private resolveBody(
body: HttpResponse["body"] = ""
): Promise {
if (typeof body === "string") {
return Promise.resolve(body);
}
let bufferPromise: Promise;
if (ArrayBuffer.isView(body)) {
bufferPromise = Promise.resolve(
new Uint8Array(body.buffer, body.byteOffset, body.byteLength)
);
} else if (isArrayBuffer(body)) {
bufferPromise = Promise.resolve(new Uint8Array(body, 0, body.byteLength));
} else {
bufferPromise = this.bodyCollector(body);
}
return bufferPromise;
}
function serializeBody(message: HttpMessage): string {
if (message.body === undefined) {
return "";
}
if (typeof message.body === "string") {
return message.body;
}
if (ArrayBuffer.isView(message.body) || isArrayBuffer(message.body)) {
return "[binary payload]";
}
return "[streaming payload]";
}
private serializeBlob(prefix: string, input: any, shape: Blob): string {
if (typeof input === "string") {
input = this.utf8Decoder(input);
} else if (ArrayBuffer.isView(input)) {
input = new Uint8Array(input.buffer, input.byteOffset, input.byteLength);
} else if (isArrayBuffer(input)) {
input = new Uint8Array(input);
} else {
throw new Error(
"Unable to serialize value that is neither a string nor an" +
" ArrayBuffer nor an ArrayBufferView as a blob"
);
}
return `${prefix}=${this.base64Encoder(input)}`;
}