Skip to content

Commit 7563330

Browse files
author
Sylvestre
authored
Merge pull request #38 from observablehq/syl/esm-migration
Use modules
2 parents 3f52de5 + be59c28 commit 7563330

File tree

8 files changed

+42
-48
lines changed

8 files changed

+42
-48
lines changed

bin/observable-database-proxy renamed to bin/observable-database-proxy.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
#!/usr/bin/env node
22

3-
require = require("esm")(module);
4-
const exit = require("../lib/errors").exit;
5-
const {start, add, remove, reset, list} = require("../lib/commands");
3+
import {start, add, remove, reset, list} from "../lib/commands.js";
4+
import argv from "yargs";
65

7-
const name = yargs =>
6+
const name = (yargs) =>
87
yargs.positional("name", {
98
describe: "Database connector name",
10-
type: "string"
9+
type: "string",
1110
});
1211

13-
const argv = require("yargs")
12+
argv
1413
.usage(`Usage: $0 <command> <name> [options]`)
1514
.command(
1615
`start <name> [options]`,

lib/commands.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ import {
66
readConfig,
77
readDecodedConfig,
88
writeConfig,
9-
decodeSecret
10-
} from "./config";
11-
import {server} from "./server";
12-
import {exit} from "./errors";
9+
decodeSecret,
10+
} from "./config.js";
11+
import {server} from "./server.js";
12+
import {exit} from "./errors.js";
1313

1414
export function start(argv) {
1515
const config = readDecodedConfig(argv.name);
@@ -25,9 +25,10 @@ export async function add(argv, reset = false) {
2525

2626
const rl = createInterface({
2727
input: process.stdin,
28-
output: process.stdout
28+
output: process.stdout,
2929
});
30-
const question = query => new Promise(resolve => rl.question(query, resolve));
30+
const question = (query) =>
31+
new Promise((resolve) => rl.question(query, resolve));
3132

3233
if (reset) {
3334
if (!config[name]) exit(`No configuration found for "${name}"`);
@@ -87,7 +88,9 @@ export function list() {
8788
if (!config) exit(`No database proxies configured`);
8889
console.log(
8990
config
90-
.map(c => `${c.name} (${c.type}) ${c.ssl === "required" ? `(SSL)` : ``}`)
91+
.map(
92+
(c) => `${c.name} (${c.type}) ${c.ssl === "required" ? `(SSL)` : ``}`
93+
)
9194
.join("\n")
9295
);
9396
}

lib/config.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {existsSync, readFileSync, writeFileSync} from "fs";
22
import {homedir} from "os";
33
import {join} from "path";
4-
import {exit} from "./errors";
4+
import {exit} from "./errors.js";
55

66
const configFile = join(homedir(), ".observablehq");
77
const key = `database-proxy`;
@@ -23,9 +23,9 @@ export function readDecodedConfig(name) {
2323
if (!raw) exit(`No configuration found for "${name}"`);
2424
return {...decodeSecret(raw.secret), url: raw.url};
2525
} else {
26-
return Object.values(config).map(c => ({
26+
return Object.values(config).map((c) => ({
2727
...decodeSecret(c.secret),
28-
url: c.url
28+
url: c.url,
2929
}));
3030
}
3131
}

lib/mysql.js

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import {json} from "micro";
22
import {createPool} from "mysql";
3-
import {parseUrl} from "mysql/lib/ConnectionConfig";
4-
import types from "mysql/lib/protocol/constants/types";
3+
import connectionConfig from "mysql/lib/ConnectionConfig.js";
4+
import types from "mysql/lib/protocol/constants/types.js";
55
import JSONStream from "JSONStream";
66

7-
export default url => {
7+
const {parseUrl} = connectionConfig;
8+
9+
export default (url) => {
810
const pool = createPool(parseUrl(url));
911

1012
return async function query(req, res) {
@@ -14,18 +16,15 @@ export default url => {
1416
await new Promise((resolve, reject) => {
1517
const stream = pool
1618
.query({sql, timeout: 30e3}, params)
17-
.on("fields", f => (fields = f))
19+
.on("fields", (f) => (fields = f))
1820
.stream()
1921
.on("end", resolve)
20-
.on("error", error => {
22+
.on("error", (error) => {
2123
stream.destroy();
2224
reject(error);
2325
})
2426
.pipe(JSONStream.stringify(`{"data":[`, ",", "]"));
25-
stream.pipe(
26-
res,
27-
{end: false}
28-
);
27+
stream.pipe(res, {end: false});
2928
});
3029

3130
const schema = {
@@ -37,8 +36,8 @@ export default url => {
3736
(schema[name] = dataTypeSchema({type, charsetNr})), schema
3837
),
3938
{}
40-
)
41-
}
39+
),
40+
},
4241
};
4342
res.end(`,"schema":${JSON.stringify(schema)}}`);
4443
};

lib/postgres.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import {json} from "micro";
2-
import {Pool} from "pg";
2+
import pg from "pg";
33
import QueryStream from "pg-query-stream";
44
import JSONStream from "JSONStream";
55

6-
export default url => {
6+
const {Pool} = pg;
7+
8+
export default (url) => {
79
const pool = new Pool({connectionString: url});
810

911
return async function query(req, res) {
@@ -19,10 +21,7 @@ export default url => {
1921
.on("end", resolve)
2022
.on("error", reject)
2123
.pipe(JSONStream.stringify(`{"data":[`, ",", "]"))
22-
.pipe(
23-
res,
24-
{end: false}
25-
);
24+
.pipe(res, {end: false});
2625
});
2726

2827
const schema = {
@@ -34,8 +33,8 @@ export default url => {
3433
(schema[name] = dataTypeSchema(dataTypeID)), schema
3534
),
3635
{}
37-
)
38-
}
36+
),
37+
},
3938
};
4039
res.end(`,"schema":${JSON.stringify(schema)}}`);
4140
} finally {

lib/server.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ import https from "https";
66
import {readFileSync} from "fs";
77
import {run} from "micro";
88
import {createHmac, timingSafeEqual} from "crypto";
9-
import serializeErrors from "./serialize-errors";
10-
import {notFound, unauthorized, exit} from "./errors";
11-
import mysql from "./mysql";
12-
import postgres from "./postgres";
13-
import snowflake from "./snowflake";
9+
import serializeErrors from "./serialize-errors.js";
10+
import {notFound, unauthorized, exit} from "./errors.js";
11+
import mysql from "./mysql.js";
12+
import postgres from "./postgres.js";
13+
import snowflake from "./snowflake.js";
1414

1515
export function server(config, argv) {
1616
const development = process.env.NODE_ENV === "development";

package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,10 @@
1212
"./snowflake.js": "./lib/snowflake.js"
1313
},
1414
"bin": {
15-
"observable-database-proxy": "./bin/observable-database-proxy"
15+
"observable-database-proxy": "./bin/observable-database-proxy.js"
1616
},
1717
"dependencies": {
1818
"JSONStream": "^1.3.5",
19-
"esm": "^3.2.25",
2019
"micro": "^9.3.4",
2120
"mysql": "^2.17.1",
2221
"open": "^6.3.0",
@@ -30,7 +29,7 @@
3029
"nodemon": "^1.19.1"
3130
},
3231
"scripts": {
33-
"dev": "NODE_ENV=development nodemon bin/observable-database-proxy",
32+
"dev": "NODE_ENV=development nodemon bin/observable-database-proxy.js",
3433
"test": "echo \"Error: no test specified\" && exit 1"
3534
},
3635
"author": "Observable",

yarn.lock

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -708,11 +708,6 @@ escape-string-regexp@^1.0.5:
708708
resolved "https://registry.yarnpkg.com/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz#1b61c0562190a8dff6ae3bb2cf0200ca130b86d4"
709709
integrity sha1-G2HAViGQqN/2rjuyzwIAyhMLhtQ=
710710

711-
esm@^3.2.25:
712-
version "3.2.25"
713-
resolved "https://registry.yarnpkg.com/esm/-/esm-3.2.25.tgz#342c18c29d56157688ba5ce31f8431fbb795cc10"
714-
integrity sha512-U1suiZ2oDVWv4zPO56S0NcR5QriEahGtdN2OR6FiOG4WJvcjBVFB0qI4+eKoWFH483PKGuLuu6V8Z4T5g63UVA==
715-
716711
execa@^0.7.0:
717712
version "0.7.0"
718713
resolved "https://registry.yarnpkg.com/execa/-/execa-0.7.0.tgz#944becd34cc41ee32a63a9faf27ad5a65fc59777"

0 commit comments

Comments
 (0)