Skip to content

Commit 0def114

Browse files
authored
chore(util-buffer-from): remove usage of deprecated Buffer constructor (#1568)
1 parent d0777d4 commit 0def114

File tree

2 files changed

+54
-141
lines changed

2 files changed

+54
-141
lines changed
Lines changed: 45 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -1,145 +1,71 @@
1-
jest.mock("buffer", () => {
2-
const Buffer = jest.fn().mockReturnValue(new Uint8Array(0));
3-
(Buffer as any).from = jest.fn().mockReturnValue(new Uint8Array(0));
4-
5-
return { Buffer };
6-
});
71
import { Buffer } from "buffer";
82

93
import { fromArrayBuffer, fromString } from "./";
104

11-
describe("fromArrayBuffer", () => {
12-
it("should throw if provided an argument that is not an ArrayBuffer", () => {
13-
expect(() => fromArrayBuffer(255 as any)).toThrow();
14-
});
5+
jest.mock("buffer");
156

16-
describe("Buffer.from", () => {
17-
beforeEach(() => {
18-
(Buffer.from as any).mockClear();
19-
});
20-
21-
it("should use Buffer.from if available", () => {
22-
const underlyingBuffer = new ArrayBuffer(0);
23-
const offsetArg = 12;
24-
const lengthArg = 13;
25-
fromArrayBuffer(underlyingBuffer, offsetArg, lengthArg);
26-
27-
const { calls } = (Buffer.from as any).mock;
28-
expect(calls.length).toBe(1);
29-
expect(calls[0].length).toBe(3);
7+
afterEach(() => {
8+
jest.clearAllMocks();
9+
});
3010

31-
const [buffer, offset, length] = calls[0];
32-
expect(buffer).toBe(underlyingBuffer);
33-
expect(offset).toBe(offsetArg);
34-
expect(length).toBe(lengthArg);
35-
});
11+
describe("fromArrayBuffer", () => {
12+
it("throws if argument is not an ArrayBuffer", () => {
13+
const input = 255;
14+
// @ts-expect-error is not assignable to parameter of type 'ArrayBuffer'
15+
expect(() => fromArrayBuffer(input)).toThrow(
16+
new TypeError(`The "input" argument must be ArrayBuffer. Received type ${typeof input} (${input})`)
17+
);
3618
});
3719

38-
describe("new Buffer", () => {
39-
const bufferDotFrom = Buffer.from;
40-
41-
beforeEach(() => {
42-
(Buffer as any).mockClear();
43-
// @ts-ignore TODO: remove support as v3 is going to support Node.js v10+
44-
delete Buffer.from;
45-
});
46-
47-
afterAll(() => {
48-
Buffer.from = bufferDotFrom;
49-
});
50-
51-
it("should use the Buffer constructor if Buffer.from is not defined", () => {
52-
const underlyingBuffer = new ArrayBuffer(0);
53-
fromArrayBuffer(underlyingBuffer);
54-
55-
const { calls } = (Buffer as any).mock;
56-
expect(calls.length).toBe(1);
57-
expect(calls[0].length).toBe(1);
58-
expect(calls[0][0]).toBe(underlyingBuffer);
59-
});
60-
61-
it("should use the Buffer constructor if Buffer.from is inherited from Uint8Array", () => {
62-
Buffer.from = Uint8Array.from as any;
63-
const underlyingBuffer = new ArrayBuffer(0);
64-
fromArrayBuffer(underlyingBuffer);
20+
describe("returns if argument is an ArrayBuffer", () => {
21+
const buffer = new ArrayBuffer(16);
6522

66-
const { calls } = (Buffer as any).mock;
67-
expect(calls.length).toBe(1);
68-
expect(calls[0].length).toBe(1);
69-
expect(calls[0][0]).toBe(underlyingBuffer);
23+
it("with one arg", () => {
24+
fromArrayBuffer(buffer);
25+
expect(Buffer.from).toHaveBeenCalledTimes(1);
26+
expect(Buffer.from).toHaveBeenCalledWith(buffer, 0, buffer.byteLength);
7027
});
7128

72-
it("should throw if Buffer.from is undefined and a non-zero offset is provided", () => {
73-
expect(() => fromArrayBuffer(new ArrayBuffer(0), 1)).toThrow();
29+
it("with two args", () => {
30+
const offset = 12;
31+
fromArrayBuffer(buffer, offset);
32+
expect(Buffer.from).toHaveBeenCalledTimes(1);
33+
expect(Buffer.from).toHaveBeenCalledWith(buffer, offset, buffer.byteLength - offset);
7434
});
7535

76-
it("should not throw if Buffer.from is undefined and an offset of 0 is provided", () => {
77-
expect(() => fromArrayBuffer(new ArrayBuffer(0), 0)).not.toThrow();
78-
});
79-
80-
it("should throw if Buffer.from is undefined and a length other than the length of the underlying buffer is provided", () => {
81-
expect(() => fromArrayBuffer(new ArrayBuffer(10), 0, 9)).toThrow();
82-
});
83-
84-
it("should not throw if Buffer.from is undefined and a length of the length of the underlying buffer is provided", () => {
85-
expect(() => fromArrayBuffer(new ArrayBuffer(10), 0, 10)).not.toThrow();
36+
it("with three args", () => {
37+
const offset = 12;
38+
const length = 13;
39+
fromArrayBuffer(buffer, offset, length);
40+
expect(Buffer.from).toHaveBeenCalledTimes(1);
41+
expect(Buffer.from).toHaveBeenCalledWith(buffer, offset, length);
8642
});
8743
});
8844
});
8945

9046
describe("fromString", () => {
91-
it("should throw if provided an argument that is not an ArrayBuffer", () => {
92-
expect(() => fromString(255 as any)).toThrow();
47+
it("throws if argument is not an ArrayBuffer", () => {
48+
const input = 255;
49+
// @ts-expect-error is not assignable to parameter of type 'ArrayBuffer'
50+
expect(() => fromString(input)).toThrow(
51+
new TypeError(`The "input" argument must be of type string. Received type ${typeof input} (${input})`)
52+
);
9353
});
9454

95-
describe("Buffer.from", () => {
96-
beforeEach(() => {
97-
(Buffer.from as any).mockClear();
98-
});
55+
describe("returns if argument is an ArrayBuffer", () => {
56+
const input = "a string";
9957

100-
it("should use Buffer.from if available", () => {
101-
const inputArg = "a string";
102-
const encodingArg = "utf16le";
103-
fromString(inputArg, encodingArg);
104-
105-
const { calls } = (Buffer.from as any).mock;
106-
expect(calls.length).toBe(1);
107-
expect(calls[0].length).toBe(2);
108-
109-
const [input, encoding] = calls[0];
110-
expect(input).toBe(inputArg);
111-
expect(encoding).toBe(encodingArg);
58+
it("without explicit encoding", () => {
59+
fromString(input);
60+
expect(Buffer.from).toHaveBeenCalledTimes(1);
61+
expect(Buffer.from).toHaveBeenCalledWith(input);
11262
});
113-
});
114-
115-
describe("new Buffer", () => {
116-
const bufferDotFrom = Buffer.from;
117-
118-
beforeEach(() => {
119-
(Buffer as any).mockClear();
120-
// @ts-ignore TODO: remove support as v3 is going to support Node.js v10+
121-
delete Buffer.from;
122-
});
123-
124-
afterAll(() => {
125-
Buffer.from = bufferDotFrom;
126-
});
127-
128-
it("should use the Buffer constructor if Buffer.from is not defined", () => {
129-
fromString("string", "hex");
130-
131-
const { calls } = (Buffer as any).mock;
132-
expect(calls.length).toBe(1);
133-
expect(calls[0]).toEqual(["string", "hex"]);
134-
});
135-
136-
it("should use the Buffer constructor if Buffer.from is inherited from Uint8Array", () => {
137-
Buffer.from = Uint8Array.from as any;
138-
fromString("string", "utf8");
13963

140-
const { calls } = (Buffer as any).mock;
141-
expect(calls.length).toBe(1);
142-
expect(calls[0]).toEqual(["string", "utf8"]);
64+
it("with encoding", () => {
65+
const encoding = "utf16le";
66+
fromString(input, encoding);
67+
expect(Buffer.from).toHaveBeenCalledTimes(1);
68+
expect(Buffer.from).toHaveBeenCalledWith(input, encoding);
14369
});
14470
});
14571
});
Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,20 @@
11
import { isArrayBuffer } from "@aws-sdk/is-array-buffer";
22
import { Buffer } from "buffer";
33

4-
export function fromArrayBuffer(input: ArrayBuffer, offset = 0, length: number = input.byteLength - offset): Buffer {
4+
export const fromArrayBuffer = (input: ArrayBuffer, offset = 0, length: number = input.byteLength - offset): Buffer => {
55
if (!isArrayBuffer(input)) {
6-
throw new Error("argument passed to fromArrayBuffer was not an ArrayBuffer");
6+
throw new TypeError(`The "input" argument must be ArrayBuffer. Received type ${typeof input} (${input})`);
77
}
88

9-
if (typeof Buffer.from === "function" && Buffer.from !== Uint8Array.from) {
10-
return Buffer.from(input, offset, length);
11-
}
9+
return Buffer.from(input, offset, length);
10+
};
1211

13-
// Any version of node that supports the optional offset and length
14-
// parameters, which were added in Node 6.0.0, will support Buffer.from and
15-
// have already returned. Throw if offset is not 0 or if length differs from
16-
// the underlying buffer's length.
17-
if (offset !== 0 || length !== input.byteLength) {
18-
throw new Error(`Unable to convert TypedArray to Buffer in Node ${process.version}`);
19-
}
20-
return new Buffer(input);
21-
}
2212
export type StringEncoding = "ascii" | "utf8" | "utf16le" | "ucs2" | "base64" | "latin1" | "binary" | "hex";
23-
export function fromString(input: string, encoding?: StringEncoding): Buffer {
24-
if (typeof input !== "string") {
25-
throw new Error("argument passed to fromString was not a string");
26-
}
2713

28-
if (typeof Buffer.from === "function" && Buffer.from !== Uint8Array.from) {
29-
return Buffer.from(input, encoding as any);
14+
export const fromString = (input: string, encoding?: StringEncoding): Buffer => {
15+
if (typeof input !== "string") {
16+
throw new TypeError(`The "input" argument must be of type string. Received type ${typeof input} (${input})`);
3017
}
3118

32-
return new Buffer(input, encoding as any);
33-
}
19+
return encoding ? Buffer.from(input, encoding) : Buffer.from(input);
20+
};

0 commit comments

Comments
 (0)