Skip to content

Commit

Permalink
Fix browser import (#136)
Browse files Browse the repository at this point in the history
* Add test to check browser import doesn't use any node api's

* Move protocol version comparison functions into './utils.ts' + update `ProtocolVersion` interface
  • Loading branch information
jaclarke authored Aug 12, 2021
1 parent 92b32fe commit fc5a9e0
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 38 deletions.
37 changes: 6 additions & 31 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) ||
Expand Down Expand Up @@ -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];
}
Expand Down
7 changes: 4 additions & 3 deletions src/codecs/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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();
Expand Down
5 changes: 1 addition & 4 deletions src/ifaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 28 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
* limitations under the License.
*/

import {ProtocolVersion} from "./ifaces";

const idCounter: {[key: string]: number} = {};

export function getUniqueId(prefix: string = ""): string {
Expand All @@ -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);
}
57 changes: 57 additions & 0 deletions test/browser.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});

0 comments on commit fc5a9e0

Please sign in to comment.