diff --git a/deps.ts b/deps.ts index cecdbe13..dfdacd3b 100644 --- a/deps.ts +++ b/deps.ts @@ -1,8 +1,8 @@ -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"; +export * as Bson from "https://deno.land/x/web_bson@v0.1.6/mod.ts"; +export { crypto } from "https://deno.land/std@0.122.0/crypto/mod.ts"; +export { BufReader } from "https://deno.land/std@0.122.0/io/mod.ts"; +export { writeAll } from "https://deno.land/std@0.122.0/streams/conversion.ts"; +export { deferred } from "https://deno.land/std@0.122.0/async/deferred.ts"; +export type { Deferred } from "https://deno.land/std@0.122.0/async/deferred.ts"; +export * as b64 from "https://deno.land/std@0.122.0/encoding/base64.ts"; +export * as hex from "https://deno.land/std@0.122.0/encoding/hex.ts"; diff --git a/src/protocol/message.ts b/src/protocol/message.ts index 1290b1b4..92cd3aae 100644 --- a/src/protocol/message.ts +++ b/src/protocol/message.ts @@ -1,6 +1,6 @@ import { MessageHeader, OpCode, setHeader } from "./header.ts"; import { Document } from "../types.ts"; -import { deserializeBson, serializeBson } from "../utils/bson.ts"; +import { Bson } from "../../deps.ts"; type MessageFlags = number; @@ -32,7 +32,7 @@ function serializeSections( let totalLen = 0; const buffers = sections.map((section) => { if ("document" in section) { - const document = serializeBson(section.document); + const document = Bson.serialize(section.document); const section0 = new Uint8Array(1 + document.byteLength); new DataView(section0.buffer).setUint8(0, 0); section0.set(document, 1); @@ -42,7 +42,7 @@ function serializeSections( const identifier = encoder.encode(section.identifier + "\0"); let documentsLength = 0; const docs = section.documents.map((doc) => { - const document = serializeBson(doc); + const document = Bson.serialize(doc); documentsLength += document.byteLength; return document; }); @@ -68,7 +68,9 @@ function serializeSections( return { length: totalLen, sections: buffers }; } -export function serializeMessage(message: Message): Uint8Array { +export function serializeMessage( + message: Message, +): Uint8Array { const { length: sectionsLength, sections } = serializeSections( message.sections, ); @@ -112,7 +114,7 @@ export function deserializeMessage( pos++; if (kind === 0) { const docLen = view.getInt32(pos, true); - const document = deserializeBson( + const document = Bson.deserialize( new Uint8Array(view.buffer.slice(pos, pos + docLen)), ); pos += docLen; @@ -147,7 +149,7 @@ function parseDocuments(buffer: Uint8Array): Document[] { const view = new DataView(buffer); while (pos < buffer.byteLength) { const docLen = view.getInt32(pos, true); - const doc = deserializeBson(buffer.slice(pos, pos + docLen)); + const doc = Bson.deserialize(buffer.slice(pos, pos + docLen)); docs.push(doc); pos += docLen; } diff --git a/src/protocol/protocol.ts b/src/protocol/protocol.ts index c6befc8d..3a516dad 100644 --- a/src/protocol/protocol.ts +++ b/src/protocol/protocol.ts @@ -35,11 +35,13 @@ export class WireProtocol { const { connectionId: _connectionId } = await handshake(this); } - async commandSingle(db: string, body: Document): Promise { - const [doc] = await this.command(db, body); - const maybeError = doc as MongoErrorInfo; - if (maybeError.ok === 0) { - throw new MongoServerError(maybeError); + async commandSingle( + db: string, + body: Document, + ): Promise { + const [doc] = await this.command(db, body); + if (doc.ok === 0) { + throw new MongoServerError(doc as MongoErrorInfo); } return doc as T; } diff --git a/src/utils/bson.ts b/src/utils/bson.ts deleted file mode 100644 index d76ffc0c..00000000 --- a/src/utils/bson.ts +++ /dev/null @@ -1,13 +0,0 @@ -import { Bson } from "../../deps.ts"; - -export function serializeBson(target: Bson.Document): Uint8Array { - return Bson.serialize(target); -} - -export function deserializeBson(buffer: Uint8Array): Bson.Document { - return Bson.deserialize(buffer); -} - -export function ObjectId(oid: string): Bson.ObjectId { - return new Bson.ObjectId(oid); -} diff --git a/tests/cases/06_gridfs.ts b/tests/cases/06_gridfs.ts index ecf51381..c3f4b3d6 100644 --- a/tests/cases/06_gridfs.ts +++ b/tests/cases/06_gridfs.ts @@ -1,10 +1,10 @@ import { GridFSBucket } from "../../mod.ts"; -import { - assertArrayBufferEquals, - assertArrayBufferNotEquals, - testWithClient, -} from "../common.ts"; -import { assert, assertEquals } from "../test.deps.ts"; +import { testWithClient } from "../common.ts"; +import { assert, assertEquals, bytesEquals } from "../test.deps.ts"; + +function bufferEquals(a: ArrayBuffer, b: ArrayBuffer) { + return bytesEquals(new Uint8Array(a), new Uint8Array(b)); +} export default function gridfsTests() { testWithClient("GridFS: Echo small Hello World", async (client) => { @@ -48,7 +48,7 @@ export default function gridfsTests() { const actual = await new Response(await bucket.openDownloadStream(getId)) .arrayBuffer(); - assertArrayBufferEquals(actual, await expected.arrayBuffer()); + assert(bufferEquals(actual, await expected.arrayBuffer())); }); testWithClient( @@ -72,7 +72,7 @@ export default function gridfsTests() { const actual = await new Response(await bucket.openDownloadStream(getId)) .arrayBuffer(); - assertArrayBufferNotEquals(actual, await expected.arrayBuffer()); + assert(!bufferEquals(actual, await expected.arrayBuffer())); }, ); diff --git a/tests/common.ts b/tests/common.ts index c0bc8f37..22eef0e1 100644 --- a/tests/common.ts +++ b/tests/common.ts @@ -1,5 +1,4 @@ import { Database, MongoClient } from "../mod.ts"; -import { assertEquals } from "./test.deps.ts"; const hostname = "127.0.0.1"; @@ -32,39 +31,3 @@ async function getClient(): Promise { await client.connect(`mongodb://${hostname}:27017`); return client; } - -export function assertArrayBufferEquals( - actual: ArrayBuffer, - expected: ArrayBuffer, -) { - assertEquals(arrayBufferEquals(actual, expected), true); -} - -export function assertArrayBufferNotEquals( - actual: ArrayBuffer, - expected: ArrayBuffer, -) { - assertEquals(arrayBufferEquals(actual, expected), false); -} - -function arrayBufferEquals(buf1: ArrayBuffer, buf2: ArrayBuffer): unknown { - if (buf1 === buf2) { - return true; - } - - if (buf1.byteLength !== buf2.byteLength) { - return false; - } - - const view1 = new DataView(buf1); - const view2 = new DataView(buf2); - - let i = buf1.byteLength; - while (i--) { - if (view1.getUint8(i) !== view2.getUint8(i)) { - return false; - } - } - - return true; -} diff --git a/tests/test.deps.ts b/tests/test.deps.ts index 7f990369..70293bab 100644 --- a/tests/test.deps.ts +++ b/tests/test.deps.ts @@ -1,7 +1,9 @@ export { assert, assertEquals, + assertNotEquals, assertRejects, assertThrows, -} from "https://deno.land/std@0.121.0/testing/asserts.ts"; +} from "https://deno.land/std@0.122.0/testing/asserts.ts"; +export { equals as bytesEquals } from "https://deno.land/std@0.122.0/bytes/equals.ts"; export * as semver from "https://deno.land/x/semver@v1.4.0/mod.ts";