Skip to content

Commit aef701d

Browse files
committed
Make sure we subarray (not slice) buffers to avoid copies
Buffer _should_ create views with slice() anyway, but Uint8Array does not. The incompatibility means slice() is deprecated, and subarray() is the correct way to do this that will not copy data in any scenarios.
1 parent 3eae898 commit aef701d

File tree

6 files changed

+8
-8
lines changed

6 files changed

+8
-8
lines changed

src/components/view/stream-message-rows.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ export const StreamMessageCollapsedRow = React.memo((p: {
6262
p.message.content
6363
// Limit the length - no point showing huge messages here. On a typical UI, we show about
6464
// 100 chars here, so this should give us a good bit of buffer
65-
.slice(0, 200)
65+
.subarray(0, 200)
6666
)
6767
}
6868
</CollapsedStreamContent>

src/model/events/content-types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ export function getCompatibleTypes(
187187
}
188188

189189
// Examine the first char of the body, assuming it's ascii
190-
const firstChar = body && body.slice(0, 1).toString('ascii');
190+
const firstChar = body && body.subarray(0, 1).toString('ascii');
191191

192192
// Allow optionally formatting non-JSON as JSON, if it looks like it might be
193193
if (firstChar === '{' || firstChar === '[') {

src/model/events/stream-message.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export class StreamMessage {
5252
}
5353

5454
// prefix+JSON is very common, so we try to parse anything JSON-ish optimistically:
55-
const startOfMessage = this.content.slice(0, 10).toString('utf-8').trim();
55+
const startOfMessage = this.content.subarray(0, 10).toString('utf-8').trim();
5656
if (
5757
startOfMessage.includes('{') ||
5858
startOfMessage.includes('[') ||

src/services/ui-worker-formatters.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ const WorkerFormatters = {
4343
// available by downloading the whole body.
4444
const needsTruncation = content.length > FIVE_MB;
4545
if (needsTruncation) {
46-
content = content.slice(0, FIVE_MB)
46+
content = content.subarray(0, FIVE_MB)
4747
}
4848

4949
const formattedContent = bufferToHex(content);

src/util/buffer.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export function isProbablyUtf8(buffer: Buffer) {
3131
// decode everything up to that point.
3232

3333
const lastUtf8IndexBefore1024 = buffer
34-
.slice(1024, 1028) // 4 bytes should be enough - max length of UTF8 char
34+
.subarray(1024, 1028) // 4 bytes should be enough - max length of UTF8 char
3535
.findIndex((byte) =>
3636
(byte & 0xC0) != 0x80 // 0x80 === 0b10... => continuation byte
3737
);
@@ -40,7 +40,7 @@ export function isProbablyUtf8(buffer: Buffer) {
4040
if (lastUtf8IndexBefore1024 === -1) return false;
4141
const cleanEndOfUtf8Data = 1024 + lastUtf8IndexBefore1024;
4242

43-
dataToCheck = buffer.slice(0, cleanEndOfUtf8Data);
43+
dataToCheck = buffer.subarray(0, cleanEndOfUtf8Data);
4444
} else {
4545
dataToCheck = buffer;
4646
}

src/util/protobuf.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export const extractProtobufFromGrpc = (input: Buffer, headers: Headers) => {
6666
const errorPrefix = `gRPC message #${msgIndex} @${offset}: `
6767
const compressionFlag = input.readUInt8();
6868
const length = input.readUInt32BE(1);
69-
let message = input.slice(5, 5 + length);
69+
let message = input.subarray(5, 5 + length);
7070
if (message.length != length) {
7171
throw new Error(`${errorPrefix}length of message is corrupted`);
7272
}
@@ -103,7 +103,7 @@ export const isProbablyGrpcProto = (input: Buffer, headers: Headers) => {
103103
}
104104
const compressionFlag = input.readUInt8();
105105
const length = input.readUInt32BE(1);
106-
const firstMessage = input.slice(5, 5 + length);
106+
const firstMessage = input.subarray(5, 5 + length);
107107
return length >= 2 && // at least two bytes for Protobuf message (tag & value)
108108
firstMessage.length == length &&
109109
(

0 commit comments

Comments
 (0)