Skip to content

Commit f7f790b

Browse files
authored
Merge pull request #574 from minrk/esm
migrate commonjs to esm
2 parents fde8be0 + 74c16de commit f7f790b

19 files changed

+400
-1512
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ repos:
1717
additional_dependencies:
1818
- setuptools
1919

20-
# Autoformat: markdown, javacsript
20+
# Autoformat: markdown, javascript
2121
- repo: https://github.com/pre-commit/mirrors-prettier
2222
rev: v4.0.0-alpha.8
2323
hooks:

bin/configurable-http-proxy

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,16 @@
55
// Copyright (c) Jupyter Development Team.
66
// Distributed under the terms of the Modified BSD License.
77
//
8-
"use strict";
98

10-
const fs = require("fs"),
11-
pkg = require("../package.json"),
12-
{ Command } = require("commander"),
13-
tls = require("tls"),
14-
winston = require("winston");
9+
import fs from "node:fs";
10+
import { Command } from "commander";
11+
12+
import ConfigurableProxy from "../lib/configproxy.js";
13+
import { defaultLogger } from "../lib/log.js";
14+
15+
import { createRequire } from "node:module";
16+
const require = createRequire(import.meta.url);
17+
const pkg = require("../package.json");
1518

1619
const cli = new Command();
1720
cli
@@ -130,9 +133,7 @@ function collectHeadersIntoObject(value, previous) {
130133
cli.parse(process.argv);
131134
var args = cli.opts();
132135

133-
var ConfigurableProxy = require("../lib/configproxy.js").ConfigurableProxy;
134-
135-
var log = require("../lib/log.js").defaultLogger({ level: args.logLevel.toLowerCase() });
136+
var log = defaultLogger({ level: args.logLevel.toLowerCase() });
136137
var options = { log: log };
137138

138139
var sslCiphers;

eslint.config.cjs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,22 @@ const js = require("@eslint/js");
55
module.exports = defineConfig([
66
js.configs.recommended,
77
{
8-
files: ["bin/*", "**/*.js"],
8+
files: ["bin/configurable-http-proxy", "**/*js"],
99
languageOptions: {
10-
sourceType: "commonjs",
10+
ecmaVersion: 2022,
1111
globals: {
12-
...globals.node,
12+
...globals.nodeBuiltin,
1313
},
1414
},
1515
rules: {
1616
"no-unused-vars": ["error", { args: "none" }],
1717
},
1818
},
1919
{
20-
files: ["test/**/*.js"],
20+
files: ["test/**/*js"],
2121
languageOptions: {
22-
sourceType: "commonjs",
2322
globals: {
24-
...globals.node,
23+
...globals.nodeBuiltin,
2524
...globals.jasmine,
2625
},
2726
},

index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
module.exports = require("./lib/configproxy.js");
1+
export * from "./lib/configproxy.js";
2+
export { default } from "./lib/configproxy.js";

lib/configproxy.js

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,23 @@
66
// POST, DELETE to /api/routes[:/path/to/proxy] to update the routing table
77
// GET /api/routes to see the current routing table
88
//
9-
"use strict";
109

11-
var http = require("http"),
12-
https = require("https"),
13-
fs = require("fs"),
14-
path = require("path"),
15-
EventEmitter = require("events").EventEmitter,
16-
httpProxy = require("http-proxy-3"),
17-
defaultLogger = require("./log").defaultLogger,
18-
metrics = require("./metrics");
10+
import fs from "node:fs";
11+
import http from "node:http";
12+
import https from "node:https";
13+
import { createRequire } from "node:module";
14+
import path from "node:path";
15+
import { fileURLToPath } from "node:url";
16+
import { EventEmitter } from "node:events";
17+
import { default as httpProxy } from "http-proxy-3";
18+
19+
import { defaultLogger } from "./log.js";
20+
import * as metrics from "./metrics.js";
21+
import { MemoryStore } from "./store.js";
22+
23+
const require = createRequire(import.meta.url);
24+
25+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
1926

2027
function bound(that, method) {
2128
// bind a method, to ensure `this=that` when it is called
@@ -123,16 +130,15 @@ const loadStorage = (options) => {
123130
}
124131

125132
// loads default storage strategy
126-
const store = require("./store.js");
127-
return new store.MemoryStore(options);
133+
return new MemoryStore(options);
128134
};
129135

130136
function _logUrl(url) {
131137
// format a url for logging, e.g. strip url params
132138
if (url) return url.split("?", 1)[0];
133139
}
134140

135-
class ConfigurableProxy extends EventEmitter {
141+
export class ConfigurableProxy extends EventEmitter {
136142
constructor(options) {
137143
super();
138144
var that = this;
@@ -671,4 +677,4 @@ class ConfigurableProxy extends EventEmitter {
671677
}
672678
}
673679

674-
exports.ConfigurableProxy = ConfigurableProxy;
680+
export default ConfigurableProxy;

lib/log.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
"use strict";
2-
const winston = require("winston");
1+
import winston from "winston";
32

43
const simpleFormat = winston.format.printf((info) => {
54
// console.log(info);
65
return `${info.timestamp} [${info.label}] ${info.level}: ${info.message}`;
76
});
87

9-
function defaultLogger(options) {
8+
export function defaultLogger(options) {
109
options = options || {};
1110
options.format = winston.format.combine(
1211
winston.format.colorize(),
@@ -18,5 +17,3 @@ function defaultLogger(options) {
1817
options.transports = [new winston.transports.Console()];
1918
return winston.createLogger(options);
2019
}
21-
22-
exports.defaultLogger = defaultLogger;

lib/metrics.js

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
"use strict";
1+
import * as client from "prom-client";
22

3-
var client = require("prom-client");
4-
5-
class Metrics {
3+
export class Metrics {
64
constructor() {
75
this.register = new client.Registry();
86
client.collectDefaultMetrics({ register: this.register });
@@ -73,7 +71,7 @@ class Metrics {
7371
}
7472
}
7573

76-
class MockMetrics {
74+
export class MockMetrics {
7775
constructor() {
7876
return new Proxy(this, {
7977
get(target, name) {
@@ -106,8 +104,3 @@ class MockMetrics {
106104
return Promise.resolve();
107105
}
108106
}
109-
110-
module.exports = {
111-
Metrics,
112-
MockMetrics,
113-
};

lib/store.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"use strict";
22

3-
var trie = require("./trie.js");
3+
import * as trie from "./trie.js";
44

55
var NotImplemented = function (name) {
66
return {
@@ -9,7 +9,7 @@ var NotImplemented = function (name) {
99
};
1010
};
1111

12-
class BaseStore {
12+
export class BaseStore {
1313
// "abstract" methods
1414
getTarget(path) {
1515
throw NotImplemented("getTarget");
@@ -39,7 +39,7 @@ class BaseStore {
3939
}
4040
}
4141

42-
class MemoryStore extends BaseStore {
42+
export class MemoryStore extends BaseStore {
4343
constructor() {
4444
super();
4545
this.routes = {};
@@ -77,5 +77,3 @@ class MemoryStore extends BaseStore {
7777
return Promise.resolve(route);
7878
}
7979
}
80-
81-
exports.MemoryStore = MemoryStore;

lib/testutil.js

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
"use strict";
22

3-
var http = require("http");
4-
var extend = require("util")._extend;
5-
var WebSocketServer = require("ws").WebSocketServer;
6-
7-
var configproxy = require("./configproxy");
8-
var defaultLogger = require("./log").defaultLogger;
3+
import http from "node:http";
4+
import { WebSocketServer } from "ws";
5+
import { ConfigurableProxy } from "./configproxy.js";
6+
import { defaultLogger } from "./log.js";
97

108
var servers = [];
119

12-
function addTarget(proxy, path, port, websocket, targetPath) {
10+
export function addTarget(proxy, path, port, websocket, targetPath) {
1311
var target = "http://127.0.0.1:" + port;
1412
if (targetPath) {
1513
target = target + targetPath;
@@ -21,7 +19,8 @@ function addTarget(proxy, path, port, websocket, targetPath) {
2119
};
2220

2321
server = http.createServer(function (req, res) {
24-
var reply = extend({}, data);
22+
var reply = {};
23+
Object.assign(reply, data);
2524
reply.url = req.url;
2625
reply.headers = req.headers;
2726
res.write(JSON.stringify(reply));
@@ -33,7 +32,8 @@ function addTarget(proxy, path, port, websocket, targetPath) {
3332
});
3433
wss.on("connection", function (ws) {
3534
ws.on("message", function (message) {
36-
var reply = extend({}, data);
35+
var reply = {};
36+
Object.assign(reply, data);
3737
reply.message = message.toString();
3838
ws.send(JSON.stringify(reply));
3939
});
@@ -50,9 +50,7 @@ function addTarget(proxy, path, port, websocket, targetPath) {
5050
});
5151
}
5252

53-
exports.addTarget = addTarget;
54-
55-
exports.addTargetRedirecting = function (proxy, path, port, targetPath, redirectTo) {
53+
export function addTargetRedirecting(proxy, path, port, targetPath, redirectTo) {
5654
// Like the above, but the server returns a redirect response with a Location header.
5755
// Cannot use default arguments as they are apparently not supported.
5856
var target = "http://127.0.0.1:" + port;
@@ -71,9 +69,9 @@ exports.addTargetRedirecting = function (proxy, path, port, targetPath, redirect
7169
server.listen(port);
7270
servers.push(server);
7371
});
74-
};
72+
}
7573

76-
function addTargets(proxy, paths, port) {
74+
export function addTargets(proxy, paths, port) {
7775
if (paths.length === 0) {
7876
return Promise.resolve();
7977
}
@@ -82,12 +80,12 @@ function addTargets(proxy, paths, port) {
8280
});
8381
}
8482

85-
exports.setupProxy = function (port, options, paths) {
83+
export function setupProxy(port, options, paths) {
8684
options = options || {};
8785
options.authToken = "secret";
8886
options.log = defaultLogger({ level: "error" });
8987

90-
var proxy = new configproxy.ConfigurableProxy(options);
88+
var proxy = new ConfigurableProxy(options);
9189
proxy._setup_timestamp = new Date(new Date().getTime() - 60000);
9290
var ip = "127.0.0.1";
9391
var countdown = 2;
@@ -136,9 +134,9 @@ exports.setupProxy = function (port, options, paths) {
136134
}
137135
});
138136
return p;
139-
};
137+
}
140138

141-
exports.teardownServers = function (callback) {
139+
export function teardownServers(callback) {
142140
var count = 0;
143141
var onclose = function () {
144142
count = count + 1;
@@ -153,4 +151,4 @@ exports.teardownServers = function (callback) {
153151
// but this avoids waits between all tests with node 18
154152
servers[i].closeAllConnections();
155153
}
156-
};
154+
}

lib/trie.js

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@
77
//
88
// Get data for a prefix with Trie.get("/path/to/something/inside")
99
//
10-
"use strict";
1110

12-
function trimPrefix(prefix) {
11+
export function trimPrefix(prefix) {
1312
// cleanup prefix form: /foo/bar
1413
// ensure prefix starts with /
1514
if (prefix.length === 0 || prefix[0] !== "/") {
@@ -22,17 +21,16 @@ function trimPrefix(prefix) {
2221
return prefix;
2322
}
2423

25-
exports.trimPrefix = trimPrefix;
26-
27-
function URLTrie(prefix) {
24+
export function URLTrie(prefix) {
2825
// create a new URLTrie with data
2926
this.prefix = trimPrefix(prefix || "/");
3027
this.branches = {};
3128
this.size = 0;
3229
}
3330

3431
var _slashesRe = /^[/]+|[/]+$/g;
35-
function stringToPath(s) {
32+
33+
export function stringToPath(s) {
3634
// turn a /prefix/string/ into ['prefix', 'string']
3735
s = s.replace(_slashesRe, "");
3836
if (s.length === 0) {
@@ -43,8 +41,6 @@ function stringToPath(s) {
4341
}
4442
}
4543

46-
exports.stringToPath = stringToPath;
47-
4844
URLTrie.prototype.add = function (path, data) {
4945
// add data to a node in the trie at path
5046
if (typeof path === "string") {
@@ -125,5 +121,3 @@ URLTrie.prototype.get = function (path) {
125121
}
126122
}
127123
};
128-
129-
exports.URLTrie = URLTrie;

0 commit comments

Comments
 (0)