Skip to content

Commit 6885575

Browse files
authored
Merge pull request #568 from AArnott/fixes
Avoid deprecated `new Buffer`
2 parents 63594c8 + ba7036c commit 6885575

File tree

6 files changed

+30
-30
lines changed

6 files changed

+30
-30
lines changed

src/nerdbank-streams/src/MultiplexingStreamFormatters.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ export class MultiplexingStreamV1Formatter extends MultiplexingStreamFormatter {
8383
/**
8484
* The magic number to send at the start of communication when using v1 of the protocol.
8585
*/
86-
private static readonly protocolMagicNumber = new Buffer([0x2f, 0xdf, 0x1d, 0x50]);
86+
private static readonly protocolMagicNumber = Buffer.from([0x2f, 0xdf, 0x1d, 0x50]);
8787

8888
constructor(private readonly stream: NodeJS.ReadWriteStream) {
8989
super();
@@ -118,7 +118,7 @@ export class MultiplexingStreamV1Formatter extends MultiplexingStreamFormatter {
118118
}
119119

120120
async writeFrameAsync(header: FrameHeader, payload?: Buffer): Promise<void> {
121-
const headerBuffer = new Buffer(7);
121+
const headerBuffer = Buffer.alloc(7);
122122
headerBuffer.writeInt8(header.code, 0);
123123
headerBuffer.writeUInt32BE(header.channel?.id || 0, 1);
124124
headerBuffer.writeUInt16BE(payload?.length || 0, 5);
@@ -147,7 +147,7 @@ export class MultiplexingStreamV1Formatter extends MultiplexingStreamFormatter {
147147
}
148148

149149
serializeOfferParameters(offer: OfferParameters): Buffer {
150-
const payload = new Buffer(offer.name, MultiplexingStream.ControlFrameEncoding);
150+
const payload = Buffer.from(offer.name, MultiplexingStream.ControlFrameEncoding);
151151
if (payload.length > MultiplexingStream.framePayloadMaxLength) {
152152
throw new Error("Name is too long.");
153153
}
@@ -162,7 +162,7 @@ export class MultiplexingStreamV1Formatter extends MultiplexingStreamFormatter {
162162
}
163163

164164
serializeAcceptanceParameters(_: AcceptanceParameters): Buffer {
165-
return new Buffer([]);
165+
return Buffer.from([]);
166166
}
167167

168168
deserializeAcceptanceParameters(_: Buffer): AcceptanceParameters {

src/nerdbank-streams/src/Utilities.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ export async function getBufferFrom(
9797
return readBuffer;
9898
}
9999

100-
return new Buffer([]);
100+
return Buffer.from([]);
101101
}
102102

103103
export function throwIfDisposed(value: IDisposableObservable) {

src/nerdbank-streams/src/tests/FullDuplexStream.spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ describe("FullDuplexStream.CreatePair", () => {
3131

3232
async function writePropagation(first: Writable, second: Readable): Promise<void> {
3333
first.write("abc");
34-
expect(second.read()).toEqual(new Buffer("abc"));
34+
expect(second.read()).toEqual(Buffer.from("abc"));
3535
}
3636

3737
async function endPropagatesFinishEvent(first: Writable, second: Readable): Promise<void> {
@@ -70,19 +70,19 @@ describe("FullDuplexStream.Splice", () => {
7070
it("Should read from readable", async () => {
7171
readable.end("hi");
7272
const buffer = await getBufferFrom(duplex, 2);
73-
expect(buffer).toEqual(new Buffer("hi"));
73+
expect(buffer).toEqual(Buffer.from("hi"));
7474
});
7575

7676
it("Should write to writable", async () => {
7777
duplex.write("abc");
7878
const buffer = await getBufferFrom(writable, 3);
79-
expect(buffer).toEqual(new Buffer("abc"));
79+
expect(buffer).toEqual(Buffer.from("abc"));
8080
});
8181

8282
it("Terminating writing", async () => {
8383
duplex.end("the end");
8484
let buffer: Buffer | null = await getBufferFrom(writable, 7);
85-
expect(buffer).toEqual(new Buffer("the end"));
85+
expect(buffer).toEqual(Buffer.from("the end"));
8686
buffer = await getBufferFrom(writable, 1, true);
8787
expect(buffer).toBeNull();
8888
});

src/nerdbank-streams/src/tests/MultiplexingStream.SeededChannels.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ import { ChannelOptions } from "../ChannelOptions";
3939
const ch2_1 = mx2.acceptChannel(1);
4040

4141
ch1_0.stream.write("abc");
42-
expect(await getBufferFrom(ch2_0.stream, 3)).toEqual(new Buffer("abc"));
42+
expect(await getBufferFrom(ch2_0.stream, 3)).toEqual(Buffer.from("abc"));
4343

4444
ch2_1.stream.write("abc");
45-
expect(await getBufferFrom(ch1_1.stream, 3)).toEqual(new Buffer("abc"));
45+
expect(await getBufferFrom(ch1_1.stream, 3)).toEqual(Buffer.from("abc"));
4646
});
4747

4848
it('can be closed', async () => {

src/nerdbank-streams/src/tests/MultiplexingStream.spec.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -69,18 +69,18 @@ import { nextTick } from "process";
6969
const offer = mx1.createChannel();
7070

7171
// Send a few bytes on the anonymous channel.
72-
offer.stream.write(new Buffer([1, 2, 3]));
72+
offer.stream.write(Buffer.from([1, 2, 3]));
7373

7474
// await until we've confirmed the ID could have propagated
7575
// to the remote party.
76-
rpcChannels[0].stream.write(new Buffer(1));
76+
rpcChannels[0].stream.write(Buffer.alloc(1));
7777
await getBufferFrom(rpcChannels[1].stream, 1);
7878

7979
const accept = mx2.acceptChannel(offer.id);
8080

8181
// Receive the few bytes on the new channel.
8282
const recvOnChannel = await getBufferFrom(accept.stream, 3);
83-
expect(recvOnChannel).toEqual(new Buffer([1, 2, 3]));
83+
expect(recvOnChannel).toEqual(Buffer.from([1, 2, 3]));
8484

8585
// Confirm the original party recognizes acceptance.
8686
await offer.acceptance;
@@ -95,11 +95,11 @@ import { nextTick } from "process";
9595
const offer = mx1.createChannel();
9696

9797
// Send a few bytes on the anonymous channel.
98-
offer.stream.write(new Buffer([1, 2, 3]));
98+
offer.stream.write(Buffer.from([1, 2, 3]));
9999

100100
// await until we've confirmed the ID could have propagated
101101
// to the remote party.
102-
rpcChannels[0].stream.write(new Buffer(1));
102+
rpcChannels[0].stream.write(Buffer.alloc(1));
103103
await getBufferFrom(rpcChannels[1].stream, 1);
104104

105105
mx2.rejectChannel(offer.id);
@@ -212,7 +212,7 @@ import { nextTick } from "process";
212212
mx2.acceptChannelAsync("test"),
213213
]);
214214
channels[0].stream.write("abc");
215-
expect(await getBufferFrom(channels[1].stream, 3)).toEqual(new Buffer("abc"));
215+
expect(await getBufferFrom(channels[1].stream, 3)).toEqual(Buffer.from("abc"));
216216
});
217217

218218
it("Can exchange data over two channels", async () => {
@@ -225,8 +225,8 @@ import { nextTick } from "process";
225225
channels[0].stream.write("abc");
226226
channels[3].stream.write("def");
227227
channels[3].stream.write("ghi");
228-
expect(await getBufferFrom(channels[2].stream, 3)).toEqual(new Buffer("abc"));
229-
expect(await getBufferFrom(channels[1].stream, 6)).toEqual(new Buffer("defghi"));
228+
expect(await getBufferFrom(channels[2].stream, 3)).toEqual(Buffer.from("abc"));
229+
expect(await getBufferFrom(channels[1].stream, 6)).toEqual(Buffer.from("defghi"));
230230
});
231231

232232
it("end of channel", async () => {
@@ -235,7 +235,7 @@ import { nextTick } from "process";
235235
mx2.acceptChannelAsync("test"),
236236
]);
237237
channels[0].stream.end("finished");
238-
expect(await getBufferFrom(channels[1].stream, 8)).toEqual(new Buffer("finished"));
238+
expect(await getBufferFrom(channels[1].stream, 8)).toEqual(Buffer.from("finished"));
239239
expect(await getBufferFrom(channels[1].stream, 1, true)).toBeNull();
240240
});
241241

src/nerdbank-streams/src/tests/Substream.spec.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ describe("Substream", () => {
1717
});
1818

1919
it("a single chunk", async () => {
20-
const payload = new Buffer([1, 2, 3]);
20+
const payload = Buffer.from([1, 2, 3]);
2121

2222
const thru = new PassThrough();
2323

@@ -38,8 +38,8 @@ describe("Substream", () => {
3838
});
3939

4040
it("two chunks", async () => {
41-
const payload1 = new Buffer([1, 2, 3]);
42-
const payload2 = new Buffer([4, 5, 6]);
41+
const payload1 = Buffer.from([1, 2, 3]);
42+
const payload2 = Buffer.from([4, 5, 6]);
4343

4444
const thru = new PassThrough();
4545

@@ -65,8 +65,8 @@ describe("Substream", () => {
6565
});
6666

6767
it("two substreams", async () => {
68-
const payload1 = new Buffer([1, 2, 3]);
69-
const payload2 = new Buffer([4, 5, 6]);
68+
const payload1 = Buffer.from([1, 2, 3]);
69+
const payload2 = Buffer.from([4, 5, 6]);
7070

7171
const thru = new PassThrough();
7272

@@ -109,7 +109,7 @@ describe("Substream", () => {
109109

110110
it("a single chunk", async () => {
111111
const thru = new PassThrough();
112-
const payload = new Buffer([1, 2, 3]);
112+
const payload = Buffer.from([1, 2, 3]);
113113
await writeLengthHeader(thru, payload.length);
114114
await writeAsync(thru, payload);
115115
await writeLengthHeader(thru, 0);
@@ -124,8 +124,8 @@ describe("Substream", () => {
124124

125125
it("two chunks", async () => {
126126
const thru = new PassThrough();
127-
const payload1 = new Buffer([1, 2, 3]);
128-
const payload2 = new Buffer([4, 5, 6]);
127+
const payload1 = Buffer.from([1, 2, 3]);
128+
const payload2 = Buffer.from([4, 5, 6]);
129129

130130
await writeLengthHeader(thru, payload1.length);
131131
await writeAsync(thru, payload1);
@@ -146,8 +146,8 @@ describe("Substream", () => {
146146

147147
it("two substreams", async () => {
148148
const thru = new PassThrough();
149-
const payload1 = new Buffer([1, 2, 3]);
150-
const payload2 = new Buffer([4, 5, 6]);
149+
const payload1 = Buffer.from([1, 2, 3]);
150+
const payload2 = Buffer.from([4, 5, 6]);
151151

152152
await writeLengthHeader(thru, payload1.length);
153153
await writeAsync(thru, payload1);

0 commit comments

Comments
 (0)