|
| 1 | +import { type DeserializeOptions } from 'bson'; |
| 2 | + |
1 | 3 | import { |
2 | 4 | type BSONElement, |
3 | 5 | type BSONSerializeOptions, |
4 | 6 | BSONType, |
5 | 7 | type Document, |
6 | 8 | Long, |
7 | 9 | parseToElementsToArray, |
| 10 | + parseUtf8ValidationOption, |
| 11 | + pluckBSONSerializeOptions, |
8 | 12 | type Timestamp |
9 | 13 | } from '../../bson'; |
10 | 14 | import { MongoUnexpectedServerResponseError } from '../../error'; |
11 | 15 | import { type ClusterTime } from '../../sdam/common'; |
12 | 16 | import { decorateDecryptionResult, ns } from '../../utils'; |
13 | | -import { type JSTypeOf, OnDemandDocument } from './on_demand/document'; |
| 17 | +import { |
| 18 | + type JSTypeOf, |
| 19 | + OnDemandDocument, |
| 20 | + type OnDemandDocumentDeserializeOptions |
| 21 | +} from './on_demand/document'; |
14 | 22 |
|
15 | 23 | // eslint-disable-next-line no-restricted-syntax |
16 | 24 | const enum BSONElementOffset { |
@@ -112,7 +120,8 @@ export class MongoDBResponse extends OnDemandDocument { |
112 | 120 | this.get('recoveryToken', BSONType.object)?.toObject({ |
113 | 121 | promoteValues: false, |
114 | 122 | promoteLongs: false, |
115 | | - promoteBuffers: false |
| 123 | + promoteBuffers: false, |
| 124 | + validation: { utf8: true } |
116 | 125 | }) ?? null |
117 | 126 | ); |
118 | 127 | } |
@@ -165,6 +174,14 @@ export class MongoDBResponse extends OnDemandDocument { |
165 | 174 | } |
166 | 175 | return this.clusterTime ?? null; |
167 | 176 | } |
| 177 | + |
| 178 | + public override toObject(options?: BSONSerializeOptions): Record<string, any> { |
| 179 | + const exactBSONOptions = { |
| 180 | + ...pluckBSONSerializeOptions(options ?? {}), |
| 181 | + validation: parseUtf8ValidationOption(options) |
| 182 | + }; |
| 183 | + return super.toObject(exactBSONOptions); |
| 184 | + } |
168 | 185 | } |
169 | 186 |
|
170 | 187 | /** @internal */ |
@@ -248,12 +265,13 @@ export class CursorResponse extends MongoDBResponse { |
248 | 265 | this.cursor.get('postBatchResumeToken', BSONType.object)?.toObject({ |
249 | 266 | promoteValues: false, |
250 | 267 | promoteLongs: false, |
251 | | - promoteBuffers: false |
| 268 | + promoteBuffers: false, |
| 269 | + validation: { utf8: true } |
252 | 270 | }) ?? null |
253 | 271 | ); |
254 | 272 | } |
255 | 273 |
|
256 | | - public shift(options?: BSONSerializeOptions): any { |
| 274 | + public shift(options: OnDemandDocumentDeserializeOptions): any { |
257 | 275 | if (this.iterated >= this.batchSize) { |
258 | 276 | return null; |
259 | 277 | } |
@@ -305,7 +323,7 @@ export class ExplainedCursorResponse extends CursorResponse { |
305 | 323 | return this._length; |
306 | 324 | } |
307 | 325 |
|
308 | | - override shift(options?: BSONSerializeOptions | undefined) { |
| 326 | + override shift(options?: DeserializeOptions) { |
309 | 327 | if (this._length === 0) return null; |
310 | 328 | this._length -= 1; |
311 | 329 | return this.toObject(options); |
|
0 commit comments