Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
c9fc786
chore: reuse parse work
nbbeeken May 9, 2024
91418d8
chore: turn back on cursorResponse
nbbeeken May 9, 2024
414cdfa
wip
nbbeeken May 9, 2024
cd29ba1
wip
nbbeeken May 10, 2024
a342412
feat(NODE-6136): parse cursor responses on demand
nbbeeken May 15, 2024
9eff48a
wip
nbbeeken May 10, 2024
4534ada
fix: FLE
nbbeeken May 17, 2024
0075729
test: needs fail points
nbbeeken May 17, 2024
79acc71
test: undo bench changes
nbbeeken May 17, 2024
dde8250
chore: cleanup
nbbeeken May 17, 2024
0b825f1
wip
nbbeeken Jun 3, 2024
e0c2250
wip
nbbeeken Jun 4, 2024
1c10a1f
chore: fix unit tests
nbbeeken Jun 4, 2024
be871b8
chore: fix wc throw location
nbbeeken Jun 4, 2024
6989a54
fix: add get overload properly
nbbeeken Jun 5, 2024
9e07ea8
chore: use serialize to make empty_v
nbbeeken Jun 6, 2024
6d081f4
docs: add comment about type crime
nbbeeken Jun 6, 2024
241a08e
cruft
nbbeeken Jun 6, 2024
de3c271
chore: type annotation
nbbeeken Jun 6, 2024
4743695
chore: move ExecutionResult and document
nbbeeken Jun 6, 2024
cba9c49
test: add match to expected errors
nbbeeken Jun 6, 2024
acbb323
test: uncomment wc error ctor tests
nbbeeken Jun 6, 2024
1ffa752
fix: super generic
nbbeeken Jun 7, 2024
fb7de90
Merge branch 'main' into NODE-6136-cursor-response
nbbeeken Jun 7, 2024
827e3f7
chore: fix nullish documents
nbbeeken Jun 7, 2024
00b90ea
fix: pass through options
nbbeeken Jun 7, 2024
e28dbbb
Merge branch 'main' into NODE-6136-cursor-response
nbbeeken Jun 7, 2024
e517ab3
refactor: move CountDocument logic into collection API (#4138)
nbbeeken Jun 10, 2024
9d75303
Revert "refactor: move CountDocument logic into collection API" (#4139)
nbbeeken Jun 10, 2024
b9cfb7c
Merge branch 'main' into NODE-6136-cursor-response
nbbeeken Jun 10, 2024
c1c8ba4
chore: only attach encryptedResponse to cursor response
nbbeeken Jun 10, 2024
d5214c8
chore: clean up TS for "required"
nbbeeken Jun 11, 2024
eb0b618
Merge branch 'main' into NODE-6136-cursor-response
baileympearson Jun 13, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
chore: reuse parse work
  • Loading branch information
nbbeeken committed Jun 4, 2024
commit c9fc7869c22d4139c66cbf4a2dfd933ba3426f8a
59 changes: 14 additions & 45 deletions src/cmap/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,7 @@ import type { ClientMetadata } from './handshake/client_metadata';
import { StreamDescription, type StreamDescriptionOptions } from './stream_description';
import { type CompressorName, decompressResponse } from './wire_protocol/compression';
import { onData } from './wire_protocol/on_data';
import {
isErrorResponse,
MongoDBResponse,
type MongoDBResponseConstructor
} from './wire_protocol/responses';
import { MongoDBResponse, type MongoDBResponseConstructor } from './wire_protocol/responses';
import { getReadPreference, isSharded } from './wire_protocol/shared';

/** @internal */
Expand Down Expand Up @@ -448,12 +444,7 @@ export class Connection extends TypedEventEmitter<ConnectionEvents> {
this.socket.setTimeout(0);
const bson = response.parse();

const document =
responseType == null
? new MongoDBResponse(bson)
: isErrorResponse(bson)
? new MongoDBResponse(bson)
: new responseType(bson);
const document = (responseType ?? MongoDBResponse).make(bson);

yield document;
this.throwIfAborted();
Expand Down Expand Up @@ -517,11 +508,6 @@ export class Connection extends TypedEventEmitter<ConnectionEvents> {
this.emit(Connection.CLUSTER_TIME_RECEIVED, document.$clusterTime);
}

if (document.has('writeConcernError')) {
object ??= document.toObject(bsonOptions);
throw new MongoWriteConcernError(object.writeConcernError, object);
}

if (document.isError) {
throw new MongoServerError((object ??= document.toObject(bsonOptions)));
}
Expand Down Expand Up @@ -552,35 +538,13 @@ export class Connection extends TypedEventEmitter<ConnectionEvents> {
}
} catch (error) {
if (this.shouldEmitAndLogCommand) {
if (error.name === 'MongoWriteConcernError') {
this.emitAndLogCommand(
this.monitorCommands,
Connection.COMMAND_SUCCEEDED,
message.databaseName,
this.established,
new CommandSucceededEvent(
this,
message,
options.noResponse ? undefined : (object ??= document?.toObject(bsonOptions)),
started,
this.description.serverConnectionId
)
);
} else {
this.emitAndLogCommand(
this.monitorCommands,
Connection.COMMAND_FAILED,
message.databaseName,
this.established,
new CommandFailedEvent(
this,
message,
error,
started,
this.description.serverConnectionId
)
);
}
this.emitAndLogCommand(
this.monitorCommands,
Connection.COMMAND_FAILED,
message.databaseName,
this.established,
new CommandFailedEvent(this, message, error, started, this.description.serverConnectionId)
);
}
throw error;
}
Expand All @@ -607,8 +571,13 @@ export class Connection extends TypedEventEmitter<ConnectionEvents> {
): Promise<Document> {
this.throwIfAborted();
for await (const document of this.sendCommand(ns, command, options, responseType)) {
if (document.has('writeConcernError')) {
const object = MongoDBResponse.is(document) ? document.toObject(options) : document;
throw new MongoWriteConcernError(object.writeConcernError, object);
}
return document;
}

throw new MongoUnexpectedServerResponseError('Unable to get response from server');
}

Expand Down
6 changes: 4 additions & 2 deletions src/cmap/wire_protocol/on_demand/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,11 @@ export class OnDemandDocument {
/** The start of the document */
private readonly offset = 0,
/** If this is an embedded document, indicates if this was a BSON array */
public readonly isArray = false
public readonly isArray = false,
/** If elements was already calculated */
elements?: BSONElement[]
) {
this.elements = parseToElementsToArray(this.bson, offset);
this.elements = elements ?? parseToElementsToArray(this.bson, offset);
}

/** Only supports basic latin strings */
Expand Down
13 changes: 11 additions & 2 deletions src/cmap/wire_protocol/responses.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
type BSONElement,
type BSONSerializeOptions,
BSONType,
type Document,
Expand Down Expand Up @@ -30,8 +31,7 @@ const enum BSONElementOffset {
*
* @param bytes - BSON document returned from the server
*/
export function isErrorResponse(bson: Uint8Array): boolean {
const elements = parseToElementsToArray(bson, 0);
export function isErrorResponse(bson: Uint8Array, elements: BSONElement[]): boolean {
for (let eIdx = 0; eIdx < elements.length; eIdx++) {
const element = elements[eIdx];

Expand Down Expand Up @@ -60,6 +60,7 @@ export function isErrorResponse(bson: Uint8Array): boolean {
/** @internal */
export type MongoDBResponseConstructor = {
new (bson: Uint8Array, offset?: number, isArray?: boolean): MongoDBResponse;
make(bson: Uint8Array): MongoDBResponse;
};

/** @internal */
Expand All @@ -68,6 +69,14 @@ export class MongoDBResponse extends OnDemandDocument {
return value instanceof MongoDBResponse;
}

static make(bson: Uint8Array) {
const elements = parseToElementsToArray(bson, 0);
const isError = isErrorResponse(bson, elements);
return isError
? new MongoDBResponse(bson, 0, false, elements)
: new this(bson, 0, false, elements);
}

// {ok:1}
static empty = new MongoDBResponse(new Uint8Array([13, 0, 0, 0, 16, 111, 107, 0, 1, 0, 0, 0, 0]));

Expand Down