Skip to content

Commit

Permalink
Switch everything in lib/ to a module (#249)
Browse files Browse the repository at this point in the history
* Switch everything in lib/ to a module

* Fix linter problems

* Fix undefined variable Client for v4

* Fix import of fauna import writer
  • Loading branch information
macmv authored Sep 25, 2023
1 parent 046f566 commit 8e849c4
Show file tree
Hide file tree
Showing 30 changed files with 114 additions and 146 deletions.
4 changes: 3 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
"@jest/globals"
]
}
]
],
// we use typescript so dynamic imports are fine
"node/no-unsupported-features": "off"
},
"ignorePatterns": ["dist/**/*"],
"overrides": [
Expand Down
2 changes: 1 addition & 1 deletion src/commands/add-endpoint.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const { cli } = require("cli-ux");
const { Flags, Args } = require("@oclif/core");
const { saveEndpointOrError } = require("../lib/misc.js");
const FaunaCommand = require("../lib/fauna-command.js");
const FaunaCommand = require("../lib/fauna-command.js").default;
const url = require("url");

class AddEndpointCommand extends FaunaCommand {
Expand Down
2 changes: 1 addition & 1 deletion src/commands/cloud-login.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const FaunaCommand = require("../lib/fauna-command.js");
const FaunaCommand = require("../lib/fauna-command.js").default;
const inquirer = require("inquirer");
const fetch = require("node-fetch");
const faunadb = require("faunadb");
Expand Down
2 changes: 1 addition & 1 deletion src/commands/create-database.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const FaunaCommand = require("../lib/fauna-command.js");
const FaunaCommand = require("../lib/fauna-command.js").default;
const { Args } = require("@oclif/core");
const faunadb = require("faunadb");
const q = faunadb.query;
Expand Down
2 changes: 1 addition & 1 deletion src/commands/create-key.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const FaunaCommand = require("../lib/fauna-command.js");
const FaunaCommand = require("../lib/fauna-command.js").default;
const { Args } = require("@oclif/core");
const faunadb = require("faunadb");
const q = faunadb.query;
Expand Down
2 changes: 1 addition & 1 deletion src/commands/default-endpoint.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { setDefaultEndpoint } = require("../lib/misc.js");
const { Args } = require("@oclif/core");
const FaunaCommand = require("../lib/fauna-command.js");
const FaunaCommand = require("../lib/fauna-command.js").default;

class DefaultEndpointCommand extends FaunaCommand {
async run() {
Expand Down
2 changes: 1 addition & 1 deletion src/commands/delete-database.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const FaunaCommand = require("../lib/fauna-command.js");
const FaunaCommand = require("../lib/fauna-command.js").default;
const { Args } = require("@oclif/core");
const faunadb = require("faunadb");
const q = faunadb.query;
Expand Down
2 changes: 1 addition & 1 deletion src/commands/delete-endpoint.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { deleteEndpointOrError } = require("../lib/misc.js");
const { Args } = require("@oclif/core");
const FaunaCommand = require("../lib/fauna-command.js");
const FaunaCommand = require("../lib/fauna-command.js").default;

class DeleteEndpoint extends FaunaCommand {
async run() {
Expand Down
2 changes: 1 addition & 1 deletion src/commands/delete-key.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const FaunaCommand = require("../lib/fauna-command.js");
const FaunaCommand = require("../lib/fauna-command.js").default;
const { Args } = require("@oclif/core");
const faunadb = require("faunadb");
const q = faunadb.query;
Expand Down
2 changes: 1 addition & 1 deletion src/commands/eval.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const fs = require("fs");
const esprima = require("esprima");
const { Flags, Args } = require("@oclif/core");
const faunadb = require("faunadb");
const FaunaCommand = require("../lib/fauna-command.js");
const FaunaCommand = require("../lib/fauna-command.js").default;
const { readFile, runQueries, writeFile } = require("../lib/misc.js");

const EVAL_OUTPUT_FORMATS = ["json", "json-tagged", "shell"];
Expand Down
6 changes: 3 additions & 3 deletions src/commands/import.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
const fs = require("fs");

const { Flags } = require("@oclif/core");
const FaunaCommand = require("../lib/fauna-command.js");
const StreamJson = require("../lib/json-stream");
const FaunaCommand = require("../lib/fauna-command.js").default;
const StreamJson = require("../lib/json-stream").default;
const faunadb = require("faunadb");
const { pipeline } = require("stream");
const p = require("path");
const q = faunadb.query;
const getFaunaImportWriter = require("../lib/fauna-import-writer");
const getFaunaImportWriter = require("../lib/fauna-import-writer").default;
const { parse } = require("csv-parse");
const { ImportLimits } = require("../lib/import-limits");

Expand Down
2 changes: 1 addition & 1 deletion src/commands/list-endpoints.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { loadEndpoints } = require("../lib/misc.js");
const FaunaCommand = require("../lib/fauna-command.js");
const FaunaCommand = require("../lib/fauna-command.js").default;

class ListEndpointsCommand extends FaunaCommand {
async run() {
Expand Down
2 changes: 1 addition & 1 deletion src/commands/list-keys.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const FaunaCommand = require("../lib/fauna-command.js");
const FaunaCommand = require("../lib/fauna-command.js").default;
const faunadb = require("faunadb");
const q = faunadb.query;
const Table = require("cli-table");
Expand Down
2 changes: 1 addition & 1 deletion src/commands/run-queries.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { Flags } = require("@oclif/core");
const FaunaCommand = require("../lib/fauna-command.js");
const FaunaCommand = require("../lib/fauna-command.js").default;
const EvalCommand = require("./eval");

const DEPRECATED_MSG =
Expand Down
2 changes: 1 addition & 1 deletion src/commands/schema/diff.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const SchemaCommand = require("../../lib/schema-command.js");
const SchemaCommand = require("../../lib/schema-command.js").default;
const fetch = require("node-fetch");
const { Flags } = require("@oclif/core");

Expand Down
2 changes: 1 addition & 1 deletion src/commands/schema/pull.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const SchemaCommand = require("../../lib/schema-command.js");
const SchemaCommand = require("../../lib/schema-command.js").default;
const fetch = require("node-fetch");
const fs = require("fs");
const path = require("path");
Expand Down
2 changes: 1 addition & 1 deletion src/commands/schema/push.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const SchemaCommand = require("../../lib/schema-command.js");
const SchemaCommand = require("../../lib/schema-command.js").default;
const fetch = require("node-fetch");
const { Flags, ux } = require("@oclif/core");

Expand Down
2 changes: 1 addition & 1 deletion src/commands/shell.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const FaunaCommand = require("../lib/fauna-command.js");
const FaunaCommand = require("../lib/fauna-command.js").default;
const { runQueries, stringifyEndpoint } = require("../lib/misc.js");
const faunadb = require("faunadb");
const { Flags, Args } = require("@oclif/core");
Expand Down
2 changes: 1 addition & 1 deletion src/commands/upload-graphql-schema.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const FaunaCommand = require("../lib/fauna-command.js");
const FaunaCommand = require("../lib/fauna-command.js").default;
const { Flags, Args } = require("@oclif/core");
const fetch = require("node-fetch");
const fs = require("fs");
Expand Down
20 changes: 9 additions & 11 deletions src/lib/fauna-client.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
const http2 = require("http2");
import { connect, constants } from "http2";

// Copied from the fauna-js driver:
// https://github.com/fauna/fauna-js/blob/main/src/http-client/node-http2-client.ts

module.exports = class FaunaClient {
export default class FaunaClient {
constructor(endpoint, secret, timeout) {
this.session = http2
.connect(endpoint, {
peerMaxConcurrentStreams: 50,
})
this.session = connect(endpoint, {
peerMaxConcurrentStreams: 50,
})
.once("error", () => this.close())
.once("goaway", () => this.close());
this.secret = secret;
Expand All @@ -19,8 +18,7 @@ module.exports = class FaunaClient {
return new Promise((resolvePromise, rejectPromise) => {
let req;
const onResponse = (http2ResponseHeaders) => {
const status =
http2ResponseHeaders[http2.constants.HTTP2_HEADER_STATUS];
const status = http2ResponseHeaders[constants.HTTP2_HEADER_STATUS];
let responseData = "";

req.on("data", (chunk) => {
Expand All @@ -41,8 +39,8 @@ module.exports = class FaunaClient {
Authorization: `Bearer ${this.secret}`,
"x-format": format,
"X-Fauna-Source": "Fauna Shell",
[http2.constants.HTTP2_HEADER_PATH]: "/query/1",
[http2.constants.HTTP2_HEADER_METHOD]: "POST",
[constants.HTTP2_HEADER_PATH]: "/query/1",
[constants.HTTP2_HEADER_METHOD]: "POST",
...(typecheck && { "x-typecheck": typecheck }),
...(this.timeout && { "x-query-timeout-ms": this.timeout }),
};
Expand Down Expand Up @@ -70,4 +68,4 @@ module.exports = class FaunaClient {
async close() {
this.session.close();
}
};
}
31 changes: 15 additions & 16 deletions src/lib/fauna-command.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
const { Command, Flags } = require("@oclif/core");
const { lookupEndpoint } = require("../lib/config/index.ts");
const { stringifyEndpoint } = require("../lib/misc.js");
const faunadb = require("faunadb");
const chalk = require("chalk");
const q = faunadb.query;
const FaunaClient = require("./fauna-client.js");
const fetch = require("node-fetch");
import { Command, Flags } from "@oclif/core";
import { lookupEndpoint } from "./config";
import { stringifyEndpoint } from "./misc";
import { query as q, errors, Client } from "faunadb";
import { green } from "chalk";
import FaunaClient from "./fauna-client.js";
import fetch from "node-fetch";

/**
* This is the base class for all fauna-shell commands.
Expand Down Expand Up @@ -35,7 +34,7 @@ class FaunaCommand extends Command {
}

success(msg) {
const bang = chalk.green(process.platform === "win32" ? "»" : "›");
const bang = green(process.platform === "win32" ? "»" : "›");
console.info(` ${bang} Success: ${msg}`);
}

Expand All @@ -58,7 +57,7 @@ class FaunaCommand extends Command {

const { hostname, port, protocol } = new URL(connectionOptions.url);

const client = new faunadb.Client({
const client = new Client({
domain: hostname,
port,
scheme: protocol?.replace(/:$/, ""),
Expand All @@ -80,7 +79,7 @@ class FaunaCommand extends Command {
}

mapConnectionError({ err, connectionOptions }) {
if (err instanceof faunadb.errors.Unauthorized) {
if (err instanceof errors.Unauthorized) {
return this.error(
`Could not Connect to ${stringifyEndpoint(
connectionOptions
Expand All @@ -99,7 +98,7 @@ class FaunaCommand extends Command {

const { hostname, port, protocol } = new URL(connectionOptions.url);

const client = new faunadb.Client({
const client = new Client({
domain: hostname,
port,
scheme: protocol?.replace(/:$/, ""),
Expand Down Expand Up @@ -182,9 +181,9 @@ class FaunaCommand extends Command {
}

dbExists(dbName, callback) {
return this.withClient(function (testDbClient, _) {
return testDbClient.query(q.Exists(q.Database(dbName))).then(callback);
});
return this.withClient((testDbClient, _) =>
testDbClient.query(q.Exists(q.Database(dbName))).then(callback)
);
}
}

Expand Down Expand Up @@ -222,4 +221,4 @@ FaunaCommand.flags = {
}),
};

module.exports = FaunaCommand;
export default FaunaCommand;
19 changes: 8 additions & 11 deletions src/lib/fauna-import-writer.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
const q = require("faunadb").query;
const { FaunaObjectTranslator } = require("./fauna-object-translator");
const sizeof = require("object-sizeof");
const { backOff } = require("exponential-backoff");
import { query as q } from "faunadb";
import { FaunaObjectTranslator } from "./fauna-object-translator";
import sizeof from "object-sizeof";
import { backOff } from "exponential-backoff";
const FaunaHTTPError = require("faunadb").errors.FaunaHTTPError;
const {
RateLimiterMemory,
RateLimiterQueue,
} = require("rate-limiter-flexible");
const { ImportPenalty } = require("./import-penalty");
const { RateEstimator } = require("./import-limits");
import { RateLimiterMemory, RateLimiterQueue } from "rate-limiter-flexible";
import { ImportPenalty } from "./import-penalty";
import { RateEstimator } from "./import-limits";

/**
* Creates a function that consumes a stream of objects and writes creates each object
Expand Down Expand Up @@ -329,4 +326,4 @@ this item and continuing.`
return streamConsumer;
}

module.exports = getFaunaImportWriter;
export default getFaunaImportWriter;
20 changes: 8 additions & 12 deletions src/lib/fauna-object-translator.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
const q = require("faunadb").query;
const moment = require("moment");
import { query as q } from "faunadb";
import { utc, ISO_8601, RFC_2822, unix } from "moment";

/**
* An error translating an object with a {FaunaObjectTranslater}.
*/
class TranslationError extends Error {}
export class TranslationError extends Error {}

/**
* Helper class for cleaning objects prior to persistence in Fauna.
* It has two main objectives:
* - trim input strings to remove unneeded whitespace
* - cast types as specified by input
**/
class FaunaObjectTranslator {
export class FaunaObjectTranslator {
static #NUMBER_REGEX = /(^\s*[+|-]?\d+\s*$)|(^\s*[+|-]?\d*\.\d+\s*$)/;

static #TRULY = ["true", "t", "yes", "1", 1, true];
Expand Down Expand Up @@ -117,10 +117,10 @@ class FaunaObjectTranslator {
)}' to a date.`
);
}
let theDate = moment.utc(val, moment.ISO_8601);
let theDate = utc(val, ISO_8601);
if (!theDate.isValid()) {
// fallback to other date formats
theDate = moment.utc(val, moment.RFC_2822);
theDate = utc(val, RFC_2822);
if (!theDate.isValid()) {
theDate = new Date(val);
if (Number.isNaN(theDate.getTime())) {
Expand All @@ -139,8 +139,7 @@ Making a best-effort translation to '${theDate}'`);
#epochMillisDate(val) {
try {
return q.Time(
moment
.unix(this.#getNumber(val) / 1000)
unix(this.#getNumber(val) / 1000)
.utc()
.toISOString()
);
Expand All @@ -155,7 +154,7 @@ Making a best-effort translation to '${theDate}'`);

#epochSecondsDate(val) {
try {
return q.Time(moment.unix(this.#getNumber(val)).utc().toISOString());
return q.Time(unix(this.#getNumber(val)).utc().toISOString());
} catch (e) {
throw new TranslationError(
`Cannot convert '${FaunaObjectTranslator.#getErrorValue(
Expand Down Expand Up @@ -189,6 +188,3 @@ Making a best-effort translation to '${theDate}'`);
}, obj);
}
}

exports.FaunaObjectTranslator = FaunaObjectTranslator;
exports.TranslationError = TranslationError;
7 changes: 2 additions & 5 deletions src/lib/import-limits.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// these functions are present to make testing the import function easy.

class ImportLimits {
export class ImportLimits {
/**
* @return the maximum import size limit in MB
**/
Expand All @@ -9,7 +9,7 @@ class ImportLimits {
}
}

class RateEstimator {
export class RateEstimator {
static estimateWriteOpsAsBytes(totalBytes, numberOfIndexes) {
if (totalBytes < 0) {
throw new Error("Invalid argument totalBytes must be >= 0");
Expand Down Expand Up @@ -40,6 +40,3 @@ class RateEstimator {
return Math.ceil(actualWriteOps / estimatedWriteOpsNoIndex) - 1;
}
}

module.exports.ImportLimits = ImportLimits;
module.exports.RateEstimator = RateEstimator;
4 changes: 1 addition & 3 deletions src/lib/import-penalty.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class ImportPenalty {
export class ImportPenalty {
constructor(floor, ceiling) {
this.floor = floor;
this.ceiling = ceiling;
Expand All @@ -17,5 +17,3 @@ class ImportPenalty {
return next;
}
}

exports.ImportPenalty = ImportPenalty;
Loading

0 comments on commit 8e849c4

Please sign in to comment.