Skip to content

Commit

Permalink
Fix Deno to run without --unstable. Add a test that that's possible.
Browse files Browse the repository at this point in the history
While there, fix Deno (and our test suite) to connect to a TCP socket,
as Deno doesn't work well with Unix sockets on macOS.
  • Loading branch information
1st1 committed May 7, 2021
1 parent 686d219 commit 2d93bec
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 13 deletions.
29 changes: 27 additions & 2 deletions src/adapter.deno.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import {process} from "https://deno.land/[email protected]/node/process.ts";
import {Buffer} from "https://deno.land/[email protected]/node/buffer.ts";
import * as crypto from "https://deno.land/[email protected]/node/crypto.ts";
import * as fs from "https://deno.land/[email protected]/node/fs.ts";
import {Sha256, HmacSha256} from "https://deno.land/[email protected]/hash/sha256.ts";
import path from "https://deno.land/[email protected]/node/path.ts";
import EventEmitter from "https://deno.land/[email protected]/node/events.ts";
import util from "https://deno.land/[email protected]/node/util.ts";

export {Buffer, path, process, util, fs, crypto};
export {Buffer, path, process, util, crypto};

export function readFileUtf8Sync(path: string): string {
return Deno.readTextFileSync(path);
}

export async function randomBytes(size: number): Promise<Buffer> {
const buf = new Uint8Array(size);
Expand Down Expand Up @@ -47,6 +50,28 @@ export function hrTime(): number {
return performance.now();
}

// TODO: replace this with
// `import * as fs from "https://deno.land/[email protected]/node/fs.ts";`
// when the 'fs' compat module does not require '--unstable' flag.
export namespace fs {
export function existsSync(fn: string | URL): boolean {
fn = fn instanceof URL ? path.fromFileUrl(fn) : fn;
try {
Deno.lstatSync(fn);
return true;
} catch (err) {
if (err instanceof Deno.errors.NotFound) {
return false;
}
throw err;
}
}

export function realpathSync(path: string): string {
return Deno.realPathSync(path);
}
}

// TODO: when 'net.Socket' is implemented in deno node compatibility library
// replace this (https://github.com/denoland/deno_std/pull/694)
export namespace net {
Expand Down
4 changes: 4 additions & 0 deletions src/adapter.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ import * as net from "net";

export {path, net, crypto, fs, existsSync, realpathSync};

export function readFileUtf8Sync(fn: string): string {
return fs.readFileSync(fn, {encoding: "utf8"});
}

export async function randomBytes(size: number): Promise<Buffer> {
return new Promise((resolve, reject) => {
crypto.randomBytes(size, (err, buf) => {
Expand Down
8 changes: 2 additions & 6 deletions src/con_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* limitations under the License.
*/

import {path, homeDir, crypto, fs} from "./adapter.node";
import {path, homeDir, crypto, fs, readFileUtf8Sync} from "./adapter.node";
import * as errors from "./errors";
import {readCredentialsFile} from "./credentials";

Expand Down Expand Up @@ -160,11 +160,7 @@ function parseConnectDsnAndArgs({
}
const stashDir = stashPath(dir);
if (fs.existsSync(stashDir)) {
dsn = fs
.readFileSync(path.join(stashDir, "instance-name"), {
encoding: "utf8",
})
.trim();
dsn = readFileUtf8Sync(path.join(stashDir, "instance-name")).trim();
} else {
throw new errors.ClientConnectionError(
"Found `edgedb.toml` but the project is not initialized. " +
Expand Down
4 changes: 2 additions & 2 deletions src/credentials.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {fs} from "./adapter.node";
import {readFileUtf8Sync} from "./adapter.node";

export interface Credentials {
host?: string;
Expand All @@ -10,7 +10,7 @@ export interface Credentials {

export function readCredentialsFile(file: string): Credentials {
try {
const data: string = fs.readFileSync(file, {encoding: "utf8"});
const data: string = readFileUtf8Sync(file);
return validateCredentials(JSON.parse(data));
} catch (e) {
throw Error(`cannot read credentials file ${file}: ${e}`);
Expand Down
35 changes: 33 additions & 2 deletions test/deno.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,17 @@
*/

import {execFile} from "child_process";
import * as fs from "fs";

test("run deno test", async () => {
jest.setTimeout(60_000);

return new Promise<void>((resolve, reject) => {
if (!fs.existsSync("test/deno")) {
console.warn("skipping deno tests; run `yarn `compileForDeno`");
return;
}

return await new Promise<void>((resolve, reject) => {
execFile(
"deno",
[
Expand All @@ -41,7 +47,32 @@ test("run deno test", async () => {
console.error(stderr);
reject(error);
}
console.log(stdout);
resolve();
}
);
});
});

test("deno check", async () => {
jest.setTimeout(60_000);

if (!fs.existsSync("test/deno")) {
console.warn("skipping deno tests; run `yarn `compileForDeno`");
return;
}

return await new Promise<void>((resolve, reject) => {
execFile(
"deno",
["eval", 'import * as edgedb from "./edgedb-deno/mod.ts"'],
{
env: process.env,
},
(error, stdout, stderr) => {
if (error) {
console.error(stderr);
reject(error);
}
resolve();
}
);
Expand Down
6 changes: 5 additions & 1 deletion test/globalSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ export default async () => {
if (m) {
const runtimeData = JSON.parse(m[1]);
process.env._JEST_EDGEDB_PORT = runtimeData.port;
process.env._JEST_EDGEDB_HOST = runtimeData.runstate_dir;

// Use runtimeData.runstate_dir instead of 127.0.0.1 to force
// testing on the UNIX socket. Deno, however, has problems with
// that, hence the TCP address.
process.env._JEST_EDGEDB_HOST = "127.0.0.1";
if (ok) {
err = null;
ok([runtimeData.runstate_dir, parseInt(runtimeData.port, 10)]);
Expand Down

0 comments on commit 2d93bec

Please sign in to comment.