Skip to content

Commit

Permalink
Merge pull request #327 from denodrivers/e5ys
Browse files Browse the repository at this point in the history
chore(deps): remove std/testing dependency & bump
  • Loading branch information
erfanium authored Jan 17, 2022
2 parents a3c5973 + 068d69c commit f3bea08
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 33 deletions.
20 changes: 8 additions & 12 deletions deps.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
export * as Bson from "https://deno.land/x/web_bson@v0.1.4/mod.ts";
export { crypto } from "https://deno.land/std@0.120.0/crypto/mod.ts";
export { BufReader } from "https://deno.land/std@0.120.0/io/mod.ts";
export { writeAll } from "https://deno.land/std@0.120.0/streams/conversion.ts";
export { deferred } from "https://deno.land/std@0.120.0/async/deferred.ts";
export type { Deferred } from "https://deno.land/std@0.120.0/async/deferred.ts";
export * as b64 from "https://deno.land/std@0.120.0/encoding/base64.ts";
export * as hex from "https://deno.land/std@0.120.0/encoding/hex.ts";
export {
assert,
assertEquals,
} from "https://deno.land/std@0.120.0/testing/asserts.ts";
export * as Bson from "https://deno.land/x/web_bson@v0.1.5/mod.ts";
export { crypto } from "https://deno.land/std@0.121.0/crypto/mod.ts";
export { BufReader } from "https://deno.land/std@0.121.0/io/mod.ts";
export { writeAll } from "https://deno.land/std@0.121.0/streams/conversion.ts";
export { deferred } from "https://deno.land/std@0.121.0/async/deferred.ts";
export type { Deferred } from "https://deno.land/std@0.121.0/async/deferred.ts";
export * as b64 from "https://deno.land/std@0.121.0/encoding/base64.ts";
export * as hex from "https://deno.land/std@0.121.0/encoding/hex.ts";
27 changes: 17 additions & 10 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
import { parse } from "./utils/uri.ts";
import { MongoDriverError } from "./error.ts";
import { Cluster } from "./cluster.ts";
import { assert } from "../deps.ts";

export class MongoClient {
#cluster?: Cluster;
Expand All @@ -19,6 +18,14 @@ export class MongoClient {
return this.#buildInfo;
}

getCluster() {
if (!this.#cluster) {
throw new MongoDriverError("MongoClient is no connected to the Database");
}

return this.#cluster;
}

async connect(
options: ConnectOptions | string,
): Promise<Database> {
Expand Down Expand Up @@ -49,23 +56,23 @@ export class MongoClient {
authorizedCollections?: boolean;
comment?: Document;
} = {}): Promise<ListDatabaseInfo[]> {
assert(this.#cluster);
const { databases } = await this.#cluster.protocol.commandSingle("admin", {
listDatabases: 1,
...options,
});
const { databases } = await this.getCluster().protocol.commandSingle(
"admin",
{
listDatabases: 1,
...options,
},
);
return databases;
}

// deno-lint-ignore no-explicit-any
runCommand<T = any>(db: string, body: Document): Promise<T> {
assert(this.#cluster);
return this.#cluster.protocol.commandSingle(db, body);
return this.getCluster().protocol.commandSingle(db, body);
}

database(name = this.#defaultDbName): Database {
assert(this.#cluster);
return new Database(this.#cluster, name);
return new Database(this.getCluster(), name);
}

close() {
Expand Down
5 changes: 1 addition & 4 deletions src/cluster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { WireProtocol } from "./protocol/mod.ts";
import { ConnectOptions } from "./types.ts";
import { AuthContext, ScramAuthPlugin, X509AuthPlugin } from "./auth/mod.ts";
import { MongoDriverError } from "./error.ts";
import { assert } from "../deps.ts";
import { Server } from "./types.ts";

export class Cluster {
Expand Down Expand Up @@ -113,9 +112,7 @@ export class Cluster {
}

get protocol() {
const protocol = this.getMaster().protocol;
assert(protocol);
return protocol;
return this.getMaster().protocol;
}

close() {
Expand Down
10 changes: 10 additions & 0 deletions src/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,13 @@ export class MongoInvalidArgumentError extends MongoError {
super(info);
}
}

export class MongoRuntimeError extends MongoDriverError {
constructor(message: string) {
super(message);
}

get name(): string {
return "MongoRuntimeError";
}
}
7 changes: 5 additions & 2 deletions src/gridfs/bucket.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { assert, Bson } from "../../deps.ts";
import { Bson } from "../../deps.ts";
import { Collection } from "../collection/collection.ts";
import { FindCursor } from "../collection/commands/find.ts";
import { Database } from "../database.ts";
Expand All @@ -13,6 +13,7 @@ import {
} from "../types/gridfs.ts";
import { checkIndexes } from "./indexes.ts";
import { createUploadStream } from "./upload.ts";
import { MongoRuntimeError } from "../error.ts";

export class GridFSBucket {
#chunksCollection: Collection<Chunk>;
Expand Down Expand Up @@ -149,7 +150,9 @@ export class GridFSBucket {
async delete(id: FileId) {
await this.#filesCollection.deleteOne({ _id: id });
const response = await this.#chunksCollection.deleteMany({ files_id: id });
assert(response, `File not found for id ${id}`);
if (!response) {
throw new MongoRuntimeError(`File not found for id ${id}`);
}
}

/**
Expand Down
12 changes: 8 additions & 4 deletions src/protocol/protocol.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { assert, BufReader, Deferred, deferred, writeAll } from "../../deps.ts";
import { MongoErrorInfo, MongoServerError } from "../error.ts";
import { BufReader, Deferred, deferred, writeAll } from "../../deps.ts";
import {
MongoDriverError,
MongoErrorInfo,
MongoServerError,
} from "../error.ts";
import { Document } from "../types.ts";
import { handshake } from "./handshake.ts";
import { parseHeader } from "./header.ts";
Expand Down Expand Up @@ -97,12 +101,12 @@ export class WireProtocol {
this.#isPendingResponse = true;
while (this.#pendingResponses.size > 0) {
const headerBuffer = await this.#reader.readFull(new Uint8Array(16));
assert(headerBuffer);
if (!headerBuffer) throw new MongoDriverError("Invalid response header");
const header = parseHeader(headerBuffer);
const bodyBuffer = await this.#reader.readFull(
new Uint8Array(header.messageLength - 16),
);
assert(bodyBuffer);
if (!bodyBuffer) throw new MongoDriverError("Invalid response body");
const reply = deserializeMessage(header, bodyBuffer);
const pendingMessage = this.#pendingResponses.get(header.responseTo);
this.#pendingResponses.delete(header.responseTo);
Expand Down
2 changes: 1 addition & 1 deletion tests/test.deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ export {
assertEquals,
assertRejects,
assertThrows,
} from "https://deno.land/std@0.120.0/testing/asserts.ts";
} from "https://deno.land/std@0.121.0/testing/asserts.ts";
export * as semver from "https://deno.land/x/semver@v1.4.0/mod.ts";

0 comments on commit f3bea08

Please sign in to comment.