|
1 | 1 | import { Headers } from '../../types'; |
2 | 2 | import { styled } from '../../styles'; |
3 | 3 |
|
4 | | -import { ViewableContentType } from '../events/content-types'; |
| 4 | +import { jsonRecordsSeparators, ViewableContentType } from '../events/content-types'; |
5 | 5 | import { ObservablePromise, observablePromise } from '../../util/observable'; |
6 | | -import { bufferToString, bufferToHex } from '../../util/buffer'; |
| 6 | +import { bufferToString, bufferToHex, splitBuffer } from '../../util/buffer'; |
7 | 7 |
|
8 | 8 | import type { WorkerFormatterKey } from '../../services/ui-worker-formatters'; |
9 | 9 | import { formatBufferAsync } from '../../services/ui-worker-api'; |
@@ -127,6 +127,43 @@ export const Formatters: { [key in ViewableContentType]: Formatter } = { |
127 | 127 | } |
128 | 128 | } |
129 | 129 | }, |
| 130 | + 'json-records': { |
| 131 | + language: 'json', |
| 132 | + cacheKey: Symbol('json-records'), |
| 133 | + isEditApplicable: false, |
| 134 | + render: (input: Buffer, headers?: Headers) => { |
| 135 | + if (input.byteLength < 10_000) { |
| 136 | + const inputAsString = bufferToString(input); |
| 137 | + |
| 138 | + try { |
| 139 | + 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 | + }); |
| 147 | + }); |
| 148 | + // For short-ish inputs, we return synchronously - conveniently this avoids |
| 149 | + // showing the loading spinner that churns the layout in short content cases. |
| 150 | + return JSON.stringify( |
| 151 | + records, |
| 152 | + null, |
| 153 | + 2 |
| 154 | + ); |
| 155 | + // ^ Same logic as in UI-worker-formatter |
| 156 | + } catch (e) { |
| 157 | + // Fallback to showing the raw un-formatted: |
| 158 | + return inputAsString; |
| 159 | + } |
| 160 | + } else { |
| 161 | + return observablePromise( |
| 162 | + formatBufferAsync(input, 'json', headers) |
| 163 | + ); |
| 164 | + } |
| 165 | + } |
| 166 | + }, |
130 | 167 | javascript: { |
131 | 168 | language: 'javascript', |
132 | 169 | cacheKey: Symbol('javascript'), |
|
0 commit comments