Skip to content

Commit 27cb98e

Browse files
committed
Fix 'Buffer.slice' deprecation (replace with 'subarray')
1 parent 3776c8d commit 27cb98e

File tree

3 files changed

+13
-13
lines changed

3 files changed

+13
-13
lines changed

src/buffer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export class ElasticBuffer {
3131
const offset = this.offset;
3232
this.reserve(size);
3333
this.offset += size;
34-
return this.buffer.slice(offset, offset + size);
34+
return this.buffer.subarray(offset, offset + size);
3535
}
3636

3737
consume() {

src/client.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -780,7 +780,7 @@ export class Client {
780780
const next = buffer.indexOf(0, offset);
781781
if (next < 0) break;
782782

783-
const value = buffer.slice(offset + 1, next).toString();
783+
const value = buffer.subarray(offset + 1, next).toString();
784784
switch (buffer[offset]) {
785785
case 0x53: {
786786
if (level === null) {
@@ -953,7 +953,7 @@ export class Client {
953953
}
954954
case 5: {
955955
const { user = '', password = '' } = this.config;
956-
const salt = buffer.slice(start + 4, start + 8);
956+
const salt = buffer.subarray(start + 4, start + 8);
957957
const shadow = md5(
958958
`${password || defaults.password}` +
959959
`${user || defaults.user}`
@@ -976,13 +976,13 @@ export class Client {
976976
);
977977
}
978978
case 11: {
979-
const data = buffer.slice(start + 4, start + length).toString("utf8");
979+
const data = buffer.subarray(start + 4, start + length).toString("utf8");
980980
const password = this.config.password || defaults.password || '';
981981
this.serverSignature = writer.saslResponse(data, password, this.clientNonce);
982982
break;
983983
}
984984
case 12: {
985-
const data = buffer.slice(start + 4, start + length).toString("utf8");
985+
const data = buffer.subarray(start + 4, start + length).toString("utf8");
986986
if (!this.serverSignature) throw new Error('Server signature missing');
987987
writer.saslFinal(data, this.serverSignature);
988988
break;
@@ -1038,7 +1038,7 @@ export class Client {
10381038
case Message.CommandComplete: {
10391039
const info = this.activeDataHandlerInfo;
10401040
if (info) {
1041-
const status = buffer.slice(
1041+
const status = buffer.subarray(
10421042
start, start + length - 1
10431043
).toString();
10441044

@@ -1057,7 +1057,7 @@ export class Client {
10571057
}
10581058
case Message.ErrorResponse: {
10591059
const error = this.parseError(
1060-
buffer.slice(start, start + length));
1060+
buffer.subarray(start, start + length));
10611061

10621062
if (this.connecting) throw error;
10631063

@@ -1070,7 +1070,7 @@ export class Client {
10701070
}
10711071
case Message.Notice: {
10721072
const notice = this.parseError(
1073-
buffer.slice(start, start + length));
1073+
buffer.subarray(start, start + length));
10741074
this.events.notice.emit(notice);
10751075
break;
10761076
}

src/protocol.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ function dateToStringUTC(date: Date, includeTime: boolean) {
173173

174174
function formatUuid(bytes: Buffer) {
175175
const slice = (start: number, end: number) => {
176-
return bytes.slice(start, end).toString('hex');
176+
return bytes.subarray(start, end).toString('hex');
177177
}
178178

179179
return [
@@ -256,7 +256,7 @@ export function readRowDescription(
256256

257257
while (i < length) {
258258
const j = buffer.indexOf('\0', offset);
259-
const name = buffer.slice(offset, j).toString();
259+
const name = buffer.subarray(offset, j).toString();
260260
const dataType = buffer.readInt32BE(j + 7);
261261
const innerDataType = arrayDataTypeMapping.get(dataType);
262262
const isArray = (typeof innerDataType !== 'undefined');
@@ -323,7 +323,7 @@ export function readRowData(
323323
if (streams && spec === DataType.Bytea) {
324324
const stream = streams[j];
325325
if (stream) {
326-
const slice = buffer.slice(start, end);
326+
const slice = buffer.subarray(start, end);
327327
const alloc = Buffer.allocUnsafe(slice.length);
328328
slice.copy(alloc, 0, 0, slice.length);
329329
stream.write(alloc);
@@ -451,7 +451,7 @@ export function readRowData(
451451
y: buffer.readDoubleBE(start + 8)
452452
}
453453
case DataType.Uuid:
454-
return formatUuid(buffer.slice(start, end));
454+
return formatUuid(buffer.subarray(start, end));
455455
}
456456
return null;
457457
};
@@ -643,7 +643,7 @@ export class Reader {
643643
streams?: ReadonlyArray<Writable | null>,
644644
) {
645645
return readRowData(
646-
this.buffer.slice(this.start, this.end),
646+
this.buffer.subarray(this.start, this.end),
647647
row,
648648
columnSpecification,
649649
encoding,

0 commit comments

Comments
 (0)