From fc5a9e0b218f5c5ff7fd6b9a9134e2af95de6725 Mon Sep 17 00:00:00 2001 From: James Clarke Date: Thu, 12 Aug 2021 17:28:07 +0100 Subject: [PATCH] Fix browser import (#136) * Add test to check browser import doesn't use any node api's * Move protocol version comparison functions into './utils.ts' + update `ProtocolVersion` interface --- src/client.ts | 37 +++++---------------------- src/codecs/registry.ts | 7 +++--- src/ifaces.ts | 5 +--- src/utils.ts | 28 +++++++++++++++++++++ test/browser.test.ts | 57 ++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 96 insertions(+), 38 deletions(-) create mode 100644 test/browser.test.ts diff --git a/src/client.ts b/src/client.ts index fbbf05675..ac31279bb 100644 --- a/src/client.ts +++ b/src/client.ts @@ -27,6 +27,7 @@ import { ReadBuffer, WriteBuffer, } from "./buffer"; +import {versionGreaterThan, versionGreaterThanOrEqual} from "./utils"; import {CodecsRegistry} from "./codecs/registry"; import {ICodec, uuid} from "./codecs/ifaces"; import {Set} from "./datatypes/set"; @@ -60,8 +61,8 @@ import { import {Transaction as LegacyTransaction} from "./legacy_transaction"; import {Transaction, START_TRANSACTION_IMPL} from "./transaction"; -const PROTO_VER: [number, number] = [0, 11]; -const PROTO_VER_MIN: [number, number] = [0, 9]; +const PROTO_VER: ProtocolVersion = [0, 11]; +const PROTO_VER_MIN: ProtocolVersion = [0, 9]; enum AuthenticationStatuses { AUTH_OK = 0, @@ -90,32 +91,6 @@ const OLD_ERROR_CODES = new Map([ const DEFAULT_MAX_ITERATIONS = 3; -export function versionGreaterThan( - left: [number, number], - right: [number, number] -): boolean { - if (left[0] > right[0]) { - return true; - } - - if (left[0] < right[0]) { - return false; - } - - return left[1] > right[1]; -} - -export function versionGreaterThanOrEqual( - left: [number, number], - right: [number, number] -): boolean { - if (left[0] === right[0] && left[1] === right[1]) { - return true; - } - - return versionGreaterThan(left, right); -} - export default function connect( dsn?: string | ConnectConfig | null, options?: ConnectConfig | null @@ -525,7 +500,7 @@ export class ConnectionImpl { private opInProgress: boolean = false; - protected protocolVersion: [number, number] = PROTO_VER; + protected protocolVersion: ProtocolVersion = PROTO_VER; /** @internal */ protected constructor(sock: net.Socket, config: NormalizedConnectConfig) { @@ -914,7 +889,7 @@ export class ConnectionImpl { const lo = this.buffer.readInt16(); this._parseHeaders(); this.buffer.finishMessage(); - const proposed: [number, number] = [hi, lo]; + const proposed: ProtocolVersion = [hi, lo]; if ( versionGreaterThan(proposed, PROTO_VER) || @@ -1572,7 +1547,7 @@ export class RawConnection extends ConnectionImpl { public async rawParse( query: string, headers?: PrepareMessageHeaders - ): Promise<[Buffer, Buffer, [number, number]]> { + ): Promise<[Buffer, Buffer, ProtocolVersion]> { const result = await this._parse(query, false, false, true, {headers}); return [result[3]!, result[4]!, this.protocolVersion]; } diff --git a/src/codecs/registry.ts b/src/codecs/registry.ts index f1d17aef0..a27d7cd0d 100644 --- a/src/codecs/registry.ts +++ b/src/codecs/registry.ts @@ -32,7 +32,8 @@ import {ObjectCodec} from "./object"; import {SetCodec} from "./set"; import {UUIDObjectCodec} from "./uuid"; import {UUID} from "../datatypes/uuid"; -import {versionGreaterThanOrEqual} from "../client"; +import {ProtocolVersion} from "../ifaces"; +import {versionGreaterThanOrEqual} from "../utils"; const CODECS_CACHE_SIZE = 1000; const CODECS_BUILD_CACHE_SIZE = 200; @@ -156,7 +157,7 @@ export class CodecsRegistry { return null; } - buildCodec(spec: Buffer, protocolVersion: [number, number]): ICodec { + buildCodec(spec: Buffer, protocolVersion: ProtocolVersion): ICodec { const frb = new ReadBuffer(spec); const codecsList: ICodec[] = []; let codec: ICodec | null = null; @@ -181,7 +182,7 @@ export class CodecsRegistry { private _buildCodec( frb: ReadBuffer, cl: ICodec[], - protocolVersion: [number, number] + protocolVersion: ProtocolVersion ): ICodec | null { const t = frb.readUInt8(); const tid = frb.readUUID(); diff --git a/src/ifaces.ts b/src/ifaces.ts index cb928a744..14d5003cc 100644 --- a/src/ifaces.ts +++ b/src/ifaces.ts @@ -31,10 +31,7 @@ import {PartialRetryRule} from "./options"; import {Set} from "./datatypes/set"; -export interface ProtocolVersion { - major: number; - minor: number; -} +export type ProtocolVersion = [number, number]; type QueryArgPrimitive = | number diff --git a/src/utils.ts b/src/utils.ts index e0e78e325..828ffe879 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -16,6 +16,8 @@ * limitations under the License. */ +import {ProtocolVersion} from "./ifaces"; + const idCounter: {[key: string]: number} = {}; export function getUniqueId(prefix: string = ""): string { @@ -25,3 +27,29 @@ export function getUniqueId(prefix: string = ""): string { const id = ++idCounter[prefix]; return `_edgedb_${prefix}_${id.toString(16)}_`; } + +export function versionGreaterThan( + left: ProtocolVersion, + right: ProtocolVersion +): boolean { + if (left[0] > right[0]) { + return true; + } + + if (left[0] < right[0]) { + return false; + } + + return left[1] > right[1]; +} + +export function versionGreaterThanOrEqual( + left: ProtocolVersion, + right: ProtocolVersion +): boolean { + if (left[0] === right[0] && left[1] === right[1]) { + return true; + } + + return versionGreaterThan(left, right); +} diff --git a/test/browser.test.ts b/test/browser.test.ts new file mode 100644 index 000000000..881405413 --- /dev/null +++ b/test/browser.test.ts @@ -0,0 +1,57 @@ +/** + * @jest-environment jsdom + */ + +for (const nodeModule of [ + "assert", + "async_hooks", + "buffer", + "child_process", + "cluster", + "console", + "constants", + "crypto", + "dgram", + "dns", + "domain", + "events", + "fs", + "http", + "http2", + "https", + "inspector", + "module", + "net", + "os", + "path", + "perf_hooks", + "process", + "punycode", + "querystring", + "readline", + "repl", + "stream", + "string_decoder", + "timers", + "tls", + "trace_events", + "tty", + "url", + "util", + "v8", + "vm", + "worker_threads", + "zlib", +]) { + jest.mock(nodeModule, () => { + throw new Error(`Cannot use node module '${nodeModule}' in browser`); + }); +} + +import {_CodecsRegistry} from "../src/index.browser"; + +test("create codec registry", () => { + const registry = new _CodecsRegistry(); + + expect(registry instanceof _CodecsRegistry).toBe(true); +});