Skip to content

Commit

Permalink
chore: bump deps and improve some codes (#328)
Browse files Browse the repository at this point in the history
* chore: bump deps and improve some codes

* chore: remove useless file

* bump(deps): update web_bson
  • Loading branch information
erfanium authored Jan 21, 2022
1 parent f3bea08 commit a0e09f6
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 78 deletions.
16 changes: 8 additions & 8 deletions deps.ts
Original file line number Diff line number Diff line change
@@ -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";
14 changes: 8 additions & 6 deletions src/protocol/message.ts
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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);
Expand All @@ -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;
});
Expand All @@ -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,
);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down
12 changes: 7 additions & 5 deletions src/protocol/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,13 @@ export class WireProtocol {
const { connectionId: _connectionId } = await handshake(this);
}

async commandSingle<T = Document>(db: string, body: Document): Promise<T> {
const [doc] = await this.command<MongoErrorInfo | T>(db, body);
const maybeError = doc as MongoErrorInfo;
if (maybeError.ok === 0) {
throw new MongoServerError(maybeError);
async commandSingle<T = Document>(
db: string,
body: Document,
): Promise<T> {
const [doc] = await this.command(db, body);
if (doc.ok === 0) {
throw new MongoServerError(doc as MongoErrorInfo);
}
return doc as T;
}
Expand Down
13 changes: 0 additions & 13 deletions src/utils/bson.ts

This file was deleted.

16 changes: 8 additions & 8 deletions tests/cases/06_gridfs.ts
Original file line number Diff line number Diff line change
@@ -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) => {
Expand Down Expand Up @@ -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(
Expand All @@ -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()));
},
);

Expand Down
37 changes: 0 additions & 37 deletions tests/common.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Database, MongoClient } from "../mod.ts";
import { assertEquals } from "./test.deps.ts";

const hostname = "127.0.0.1";

Expand Down Expand Up @@ -32,39 +31,3 @@ async function getClient(): Promise<MongoClient> {
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;
}
4 changes: 3 additions & 1 deletion tests/test.deps.ts
Original file line number Diff line number Diff line change
@@ -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";

0 comments on commit a0e09f6

Please sign in to comment.