Skip to content

Commit df5121a

Browse files
committed
perf: define methods on cursor response
1 parent 5fb54d3 commit df5121a

File tree

2 files changed

+41
-46
lines changed

2 files changed

+41
-46
lines changed

src/cmap/wire_protocol/responses.ts

Lines changed: 39 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -95,35 +95,31 @@ export class MongoDBResponse extends OnDemandDocument {
9595
return this.clusterTime ?? null;
9696
}
9797

98-
public override toObject(options: BSONSerializeOptions = {}): Record<string, any> {
98+
public override toObject(options?: BSONSerializeOptions): Record<string, any> {
9999
const exactBSONOptions = {
100-
useBigInt64: options.useBigInt64,
101-
promoteLongs: options.promoteLongs,
102-
promoteValues: options.promoteValues,
103-
promoteBuffers: options.promoteBuffers,
104-
bsonRegExp: options.bsonRegExp,
105-
raw: options.raw ?? false,
106-
fieldsAsRaw: options.fieldsAsRaw ?? {},
100+
useBigInt64: options?.useBigInt64,
101+
promoteLongs: options?.promoteLongs,
102+
promoteValues: options?.promoteValues,
103+
promoteBuffers: options?.promoteBuffers,
104+
bsonRegExp: options?.bsonRegExp,
105+
raw: options?.raw ?? false,
106+
fieldsAsRaw: options?.fieldsAsRaw ?? {},
107107
validation: this.parseBsonSerializationOptions(options)
108108
};
109109
return super.toObject(exactBSONOptions);
110110
}
111111

112-
private parseBsonSerializationOptions({ enableUtf8Validation }: BSONSerializeOptions): {
112+
private parseBsonSerializationOptions(options?: { enableUtf8Validation?: boolean }): {
113113
utf8: { writeErrors: false } | false;
114114
} {
115+
const enableUtf8Validation = options?.enableUtf8Validation;
115116
if (enableUtf8Validation === false) {
116117
return { utf8: false };
117118
}
118-
119119
return { utf8: { writeErrors: false } };
120120
}
121121
}
122122

123-
function throwUnsupportedError() {
124-
throw new Error('Unsupported method');
125-
}
126-
127123
/** @internal */
128124
export class CursorResponse extends MongoDBResponse {
129125
static emptyGetMore = new CursorResponse(
@@ -139,7 +135,6 @@ export class CursorResponse extends MongoDBResponse {
139135

140136
public id: Long | null = null;
141137
public ns: MongoDBNamespace | null = null;
142-
public documents: any | null = null;
143138
public batchSize = 0;
144139

145140
private batch: OnDemandDocument | null = null;
@@ -163,35 +158,35 @@ export class CursorResponse extends MongoDBResponse {
163158
else if (cursor.has('nextBatch')) this.batch = cursor.get('nextBatch', BSONType.array, true);
164159
else throw new MongoUnexpectedServerResponseError('Cursor document did not contain a batch');
165160

166-
this.values = this.batch.valuesAs(BSONType.object);
167161
this.batchSize = this.batch.size();
168-
this.iterated = 0;
169-
this.documents = Object.defineProperties(Object.create(null), {
170-
length: {
171-
get: () => {
172-
return Math.max(this.batchSize - this.iterated, 0);
173-
}
174-
},
175-
shift: {
176-
value: (options?: BSONSerializeOptions) => {
177-
this.iterated += 1;
178-
const result = this.values?.next();
179-
if (!result || result.done) return null;
180-
if (options?.raw) {
181-
return result.value.toBytes();
182-
} else {
183-
return result.value.toObject(options);
184-
}
185-
}
186-
},
187-
clear: {
188-
value: () => {
189-
this.iterated = this.batchSize;
190-
this.values?.return();
191-
}
192-
},
193-
pushMany: { value: throwUnsupportedError },
194-
push: { value: throwUnsupportedError }
195-
});
162+
}
163+
164+
get length() {
165+
return Math.max(this.batchSize - this.iterated, 0);
166+
}
167+
168+
shift(options?: BSONSerializeOptions): any {
169+
this.iterated += 1;
170+
this.values ??= this.batch?.valuesAs(BSONType.object) ?? null;
171+
const result = this.values?.next();
172+
if (!result || result.done) return null;
173+
if (options?.raw) {
174+
return result.value.toBytes();
175+
} else {
176+
return result.value.toObject(options);
177+
}
178+
}
179+
180+
clear() {
181+
this.iterated = this.batchSize;
182+
this.values?.return();
183+
}
184+
185+
pushMany() {
186+
throw new Error('pushMany Unsupported method');
187+
}
188+
189+
push() {
190+
throw new Error('push Unsupported method');
196191
}
197192
}

src/cursor/abstract_cursor.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -660,7 +660,7 @@ export abstract class AbstractCursor<
660660
if (CursorResponse.is(response)) {
661661
this[kId] = response.id;
662662
if (response.ns) this[kNamespace] = response.ns;
663-
this[kDocuments] = response.documents;
663+
this[kDocuments] = response;
664664
} else if (response.cursor) {
665665
// TODO(NODE-2674): Preserve int64 sent from MongoDB
666666
this[kId] =
@@ -800,7 +800,7 @@ async function next<T>(
800800
const response = await cursor.getMore(batchSize);
801801
if (CursorResponse.is(response)) {
802802
cursor[kId] = response.id;
803-
cursor[kDocuments] = response.documents;
803+
cursor[kDocuments] = response;
804804
} else if (response) {
805805
const cursorId =
806806
typeof response.cursor.id === 'number'

0 commit comments

Comments
 (0)