Skip to content

Commit ba7036c

Browse files
committed
Avoid deprecated new Buffer
1 parent dafeb58 commit ba7036c

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
@@ -68,18 +68,18 @@ import * as assert from "assert";
6868
const offer = mx1.createChannel();
6969

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

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

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

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

8484
// Confirm the original party recognizes acceptance.
8585
await offer.acceptance;
@@ -94,11 +94,11 @@ import * as assert from "assert";
9494
const offer = mx1.createChannel();
9595

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

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

104104
mx2.rejectChannel(offer.id);
@@ -193,7 +193,7 @@ import * as assert from "assert";
193193
mx2.acceptChannelAsync("test"),
194194
]);
195195
channels[0].stream.write("abc");
196-
expect(await getBufferFrom(channels[1].stream, 3)).toEqual(new Buffer("abc"));
196+
expect(await getBufferFrom(channels[1].stream, 3)).toEqual(Buffer.from("abc"));
197197
});
198198

199199
it("Can exchange data over two channels", async () => {
@@ -206,8 +206,8 @@ import * as assert from "assert";
206206
channels[0].stream.write("abc");
207207
channels[3].stream.write("def");
208208
channels[3].stream.write("ghi");
209-
expect(await getBufferFrom(channels[2].stream, 3)).toEqual(new Buffer("abc"));
210-
expect(await getBufferFrom(channels[1].stream, 6)).toEqual(new Buffer("defghi"));
209+
expect(await getBufferFrom(channels[2].stream, 3)).toEqual(Buffer.from("abc"));
210+
expect(await getBufferFrom(channels[1].stream, 6)).toEqual(Buffer.from("defghi"));
211211
});
212212

213213
it("end of channel", async () => {
@@ -216,7 +216,7 @@ import * as assert from "assert";
216216
mx2.acceptChannelAsync("test"),
217217
]);
218218
channels[0].stream.end("finished");
219-
expect(await getBufferFrom(channels[1].stream, 8)).toEqual(new Buffer("finished"));
219+
expect(await getBufferFrom(channels[1].stream, 8)).toEqual(Buffer.from("finished"));
220220
expect(await getBufferFrom(channels[1].stream, 1, true)).toBeNull();
221221
});
222222

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)