Skip to content

Commit

Permalink
chore: update socket.js
Browse files Browse the repository at this point in the history
  • Loading branch information
pmmmwh authored and Michael Mok committed Apr 4, 2022
1 parent 54b2c20 commit 24114c0
Show file tree
Hide file tree
Showing 23 changed files with 1,327 additions and 1,129 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ jobs:
- name: Lint
run: npm run lint

- name: Build types
run: npm run build:types

- name: Check types
run: if [ -n "$(git status types --porcelain)" ]; then echo "Missing types. Update types by running 'npm run build:types'"; exit 1; else echo "All types are valid"; fi

- name: Security audit
run: npm audit --production

Expand Down
2 changes: 1 addition & 1 deletion client-src/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* global __resourceQuery, __webpack_hash__ */
/// <reference types="webpack/module" />
import webpackHotLog from "webpack/hot/log.js";
import stripAnsi from "./modules/strip-ansi/index.js";
import stripAnsi from "./utils/stripAnsi.js";
import parseURL from "./utils/parseURL.js";
import socket from "./socket.js";
import { formatProblem, show, hide } from "./overlay.js";
Expand Down
3 changes: 0 additions & 3 deletions client-src/modules/strip-ansi/index.js

This file was deleted.

2 changes: 2 additions & 0 deletions client-src/socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ let retries = 0;
let maxRetries = 10;

// Initialized client is exported so external consumers can utilize the same instance
// It is mutable to enforce singleton
// eslint-disable-next-line no-mutable-exports
export let client = null;

/**
Expand Down
2 changes: 1 addition & 1 deletion client-src/utils/createSocketURL.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
function format(objURL) {
let protocol = objURL.protocol || "";

if (protocol && protocol.substr(-1) !== ":") {
if (protocol && !protocol.endsWith(":")) {
protocol += ":";
}

Expand Down
2 changes: 1 addition & 1 deletion client-src/utils/parseURL.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ function parseURL(resourceQuery) {
let options = {};

if (typeof resourceQuery === "string" && resourceQuery !== "") {
const searchParams = resourceQuery.substr(1).split("&");
const searchParams = resourceQuery.slice(1).split("&");

for (let i = 0; i < searchParams.length; i++) {
const pair = searchParams[i].split("=");
Expand Down
26 changes: 26 additions & 0 deletions client-src/utils/stripAnsi.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const ansiRegex = new RegExp(
[
"[\\u001B\\u009B][[\\]()#;?]*(?:(?:(?:(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]+)*|[a-zA-Z\\d]+(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?\\u0007)",
"(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PR-TZcf-nq-uy=><~]))",
].join("|"),
"g"
);

/**
*
* Strip [ANSI escape codes](https://en.wikipedia.org/wiki/ANSI_escape_code) from a string.
* Adapted from code originally released by Sindre Sorhus
* Licensed the MIT License
*
* @param {string} string
* @return {string}
*/
function stripAnsi(string) {
if (typeof string !== "string") {
throw new TypeError(`Expected a \`string\`, got \`${typeof string}\``);
}

return string.replace(ansiRegex, "");
}

export default stripAnsi;
7 changes: 0 additions & 7 deletions client-src/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,6 @@ module.exports = [
),
],
}),
merge(baseForModules, {
entry: path.join(__dirname, "modules/strip-ansi/index.js"),
output: {
// @ts-ignore
filename: "strip-ansi/index.js",
},
}),
merge(baseForModules, {
entry: path.join(__dirname, "modules/sockjs-client/index.js"),
output: {
Expand Down
15 changes: 9 additions & 6 deletions examples/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,17 @@ module.exports = {
})
);

if (result.devServer.onBeforeSetupMiddleware) {
const proxy = result.devServer.onBeforeSetupMiddleware;
result.devServer.onBeforeSetupMiddleware = function replace(app) {
onBeforeSetupMiddleware(app);
proxy(app);
if (result.devServer.setupMiddlewares) {
const proxy = result.devServer.setupMiddlewares;
result.devServer.setupMiddlewares = (middlewares, devServer) => {
onBeforeSetupMiddleware(devServer);
return proxy(middlewares, devServer);
};
} else {
result.devServer.onBeforeSetupMiddleware = onBeforeSetupMiddleware;
result.devServer.setupMiddlewares = (middlewares, devServer) => {
onBeforeSetupMiddleware(devServer);
return middlewares;
};
}

const output = {
Expand Down
14 changes: 9 additions & 5 deletions lib/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ const schema = require("./options.json");
/** @typedef {import("chokidar").WatchOptions} WatchOptions */
/** @typedef {import("chokidar").FSWatcher} FSWatcher */
/** @typedef {import("connect-history-api-fallback").Options} ConnectHistoryApiFallbackOptions */
/** @typedef {import("bonjour").Bonjour} Bonjour */
/** @typedef {import("bonjour").BonjourOptions} BonjourOptions */
/** @typedef {import("bonjour-service").Bonjour} Bonjour */
/** @typedef {import("bonjour-service").Service} BonjourOptions */
/** @typedef {import("http-proxy-middleware").RequestHandler} RequestHandler */
/** @typedef {import("http-proxy-middleware").Options} HttpProxyMiddlewareOptions */
/** @typedef {import("http-proxy-middleware").Filter} HttpProxyMiddlewareOptionsFilter */
Expand Down Expand Up @@ -188,7 +188,7 @@ const schema = require("./options.json");
* @property {"auto" | "all" | string | string[]} [allowedHosts]
* @property {boolean | ConnectHistoryApiFallbackOptions} [historyApiFallback]
* @property {boolean} [setupExitSignals]
* @property {boolean | BonjourOptions} [bonjour]
* @property {boolean | Record<string, never> | BonjourOptions} [bonjour]
* @property {string | string[] | WatchFiles | Array<string | WatchFiles>} [watchFiles]
* @property {boolean | string | Static | Array<string | Static>} [static]
* @property {boolean | ServerOptions} [https]
Expand Down Expand Up @@ -2594,14 +2594,18 @@ class Server {
* @returns {void}
*/
runBonjour() {
const { Bonjour } = require("bonjour-service");
/**
* @private
* @type {import("bonjour").Bonjour | undefined}
* @type {Bonjour | undefined}
*/
this.bonjour = require("bonjour")();
this.bonjour = new Bonjour();
this.bonjour.publish({
// @ts-expect-error
name: `Webpack Dev Server ${os.hostname()}:${this.options.port}`,
// @ts-expect-error
port: /** @type {number} */ (this.options.port),
// @ts-expect-error
type:
/** @type {ServerConfiguration} */
(this.options.server).type === "http" ? "http" : "https",
Expand Down

0 comments on commit 24114c0

Please sign in to comment.