Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9a98412

Browse files
committed
avoid additional buffer slice on string path
DataRowMessage.fields may now be either a string or a buffer slice depending on the field format. this relies on some assumption: - fieldDescriptions is accurate when parseDataRowmessage is called. we make a similar assumption in pg/lib/result.js, so this seems fine.
1 parent e1201c1 commit 9a98412

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

‎packages/pg-protocol/src/parser.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ export class Parser {
8080
private bufferOffset: number = 0
8181
private reader = new BufferReader()
8282
private mode: Mode
83+
private fieldDescriptions: Field[] | null = null
8384

8485
constructor(opts?: StreamOptions) {
8586
if (opts?.mode === 'binary') {
@@ -253,6 +254,7 @@ export class Parser {
253254
for (let i = 0; i < fieldCount; i++) {
254255
message.fields[i] = this.parseField()
255256
}
257+
this.fieldDescriptions = message.fields
256258
return message
257259
}
258260

@@ -284,7 +286,12 @@ export class Parser {
284286
for (let i = 0; i < fieldCount; i++) {
285287
const len = this.reader.int32()
286288
// a -1 for length means the value of the field is null
287-
fields[i] = len === -1 ? null : this.reader.bytes(len)
289+
if (len === -1) {
290+
fields[i] = null
291+
} else {
292+
const format = this.fieldDescriptions?.[i]?.format
293+
fields[i] = format === 'binary' ? this.reader.bytes(len) : this.reader.string(len)
294+
}
288295
}
289296
return new DataRowMessage(length, fields)
290297
}

‎packages/pg/lib/result.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,7 @@ class Result {
5252
for (let i = 0, len = rowData.length; i < len; i++) {
5353
const rawValue = rowData[i]
5454
if (rawValue !== null) {
55-
const v = this.fields[i].format !== 'binary' ? rawValue.toString('utf8') : rawValue
56-
row[i] = this._parsers[i](v)
55+
row[i] = this._parsers[i](rawValue)
5756
} else {
5857
row[i] = null
5958
}
@@ -67,8 +66,7 @@ class Result {
6766
const rawValue = rowData[i]
6867
const field = this.fields[i].name
6968
if (rawValue !== null) {
70-
const v = this.fields[i].format !== 'binary' ? rawValue.toString('utf8') : rawValue
71-
row[field] = this._parsers[i](v)
69+
row[field] = this._parsers[i](rawValue)
7270
} else {
7371
row[field] = null
7472
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /