Skip to content

fix: type ironing with string buffer #1251

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions packages/hash-node/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { Hash as IHash, SourceData } from "@aws-sdk/types";
import { Buffer } from "buffer";
import { fromArrayBuffer, fromString } from "@aws-sdk/util-buffer-from";
import {
fromArrayBuffer,
fromString,
StringEncoding
} from "@aws-sdk/util-buffer-from";
import { createHash, createHmac, Hash as NodeHash, Hmac } from "crypto";

export class Hash implements IHash {
Expand All @@ -21,7 +25,7 @@ export class Hash implements IHash {
}
}

function castSourceData(toCast: SourceData, encoding?: string): Buffer {
function castSourceData(toCast: SourceData, encoding?: StringEncoding): Buffer {
if (Buffer.isBuffer(toCast)) {
return toCast;
}
Expand Down
28 changes: 12 additions & 16 deletions packages/util-buffer-from/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,27 +27,23 @@ export function fromArrayBuffer(
}
return new Buffer(input);
}

export function fromString(
input: string,
encoding?:
| "ascii"
| "utf8"
| "utf16le"
| "ucs2"
| "base64"
| "latin1"
| "binary"
| "hex"
| string
): Buffer {
export type StringEncoding =
| "ascii"
| "utf8"
| "utf16le"
| "ucs2"
| "base64"
| "latin1"
| "binary"
| "hex";
export function fromString(input: string, encoding?: StringEncoding): Buffer {
if (typeof input !== "string") {
throw new Error("argument passed to fromString was not a string");
}

if (typeof Buffer.from === "function" && Buffer.from !== Uint8Array.from) {
return Buffer.from(input, encoding);
return Buffer.from(input, encoding as any);
}

return new Buffer(input, encoding);
return new Buffer(input, encoding as any);
}