Skip to content

Commit cce16cd

Browse files
committed
refactor: remove unnecessary toString convert
1 parent 3b91be8 commit cce16cd

File tree

2 files changed

+14
-20
lines changed

2 files changed

+14
-20
lines changed

src/model/events/body-formatting.ts

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -133,17 +133,14 @@ export const Formatters: { [key in ViewableContentType]: Formatter } = {
133133
isEditApplicable: false,
134134
render: (input: Buffer, headers?: Headers) => {
135135
if (input.byteLength < 10_000) {
136-
const inputAsString = bufferToString(input);
137-
138136
try {
139137
let records = new Array();
140-
jsonRecordsSeparators.forEach((separator) => {
141-
splitBuffer(input, separator).forEach((recordBuffer: Buffer) => {
142-
if (recordBuffer.length > 0) {
143-
const record = recordBuffer.toString('utf-8');
144-
records.push(JSON.parse(record.trim()));
145-
}
146-
});
138+
const separator = input[input.length - 1];
139+
splitBuffer(input, separator).forEach((recordBuffer: Buffer) => {
140+
if (recordBuffer.length > 0) {
141+
const record = recordBuffer.toString('utf-8');
142+
records.push(JSON.parse(record.trim()));
143+
}
147144
});
148145
// For short-ish inputs, we return synchronously - conveniently this avoids
149146
// showing the loading spinner that churns the layout in short content cases.
@@ -155,7 +152,7 @@ export const Formatters: { [key in ViewableContentType]: Formatter } = {
155152
// ^ Same logic as in UI-worker-formatter
156153
} catch (e) {
157154
// Fallback to showing the raw un-formatted:
158-
return inputAsString;
155+
return bufferToString(input);
159156
}
160157
} else {
161158
return observablePromise(

src/services/ui-worker-formatters.ts

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -82,21 +82,18 @@ const WorkerFormatters = {
8282
}
8383
},
8484
'json-records': (content: Buffer) => {
85-
const asString = content.toString('utf8');
86-
8785
try {
8886
let records = new Array();
89-
jsonRecordsSeparators.forEach((separator) => {
90-
splitBuffer(content, separator).forEach((recordBuffer: Buffer) => {
91-
if (recordBuffer.length > 0) {
92-
const record = recordBuffer.toString('utf-8');
93-
records.push(JSON.parse(record.trim()));
94-
}
95-
});
87+
const separator = content[content.length - 1];
88+
splitBuffer(content, separator).forEach((recordBuffer: Buffer) => {
89+
if (recordBuffer.length > 0) {
90+
const record = recordBuffer.toString('utf-8');
91+
records.push(JSON.parse(record.trim()));
92+
}
9693
});
9794
return JSON.stringify(records, null, 2);
9895
} catch (e) {
99-
return asString;
96+
return content.toString('utf8');
10097
}
10198
},
10299
javascript: (content: Buffer) => {

0 commit comments

Comments
 (0)