Skip to content

Commit 870c587

Browse files
authored
fix: improve rendering of binary and json fields (#42)
1 parent 990ff6c commit 870c587

2 files changed

Lines changed: 18 additions & 6 deletions

File tree

src/controller.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -140,17 +140,29 @@ function getMaxRows(): number {
140140
return maxRows ?? fallbackMaxRows;
141141
}
142142

143-
function escapeNewline(a: string | number | null): string | number | null {
144-
if (typeof a === 'string') {
145-
return a.replace(/\n/g, '\\n').replace(/\r/g, '\\r');
143+
function serializeCell(a: any): any {
144+
try {
145+
// serialize buffers as hex strings
146+
if (Buffer.isBuffer(a)) {
147+
return `0x${a.toString('hex')}`;
148+
}
149+
// attempt to serialize all remaining "object" values as JSON
150+
if (typeof a === 'object') {
151+
return JSON.stringify(a);
152+
}
153+
if (typeof a === 'string') {
154+
return a.replace(/\n/g, '\\n').replace(/\r/g, '\\r');
155+
}
156+
return a;
157+
} catch {
158+
return a;
146159
}
147-
return a;
148160
}
149161

150162
function markdownRow(row: Row): string {
151163
const middle = Object.entries(row)
152164
.map((pair) => pair[1])
153-
.map(escapeNewline)
165+
.map(serializeCell)
154166
.join(' | ');
155167
return `| ${middle} |`;
156168
}

src/driver.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export type ExecutionResult = TabularResult[];
1919
export type TabularResult = Row[];
2020

2121
// Row represents an arbitrary map of data with marshallable values.
22-
export type Row = { [key: string]: string | number | null };
22+
export type Row = { [key: string]: any };
2323

2424
// Conn is an abstraction over driver-specific connection interfaces.
2525
interface Conn {

0 commit comments

Comments
 (0)