Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/shuvijs/shuvi
Browse files Browse the repository at this point in the history
  • Loading branch information
liximomo committed Jun 30, 2020
2 parents eab0dcd + f195c17 commit 9481a3e
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 14 deletions.
2 changes: 2 additions & 0 deletions packages/shuvi/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"commander": "5.1.0",
"connect": "3.7.0",
"cross-spawn": "7.0.1",
"detect-port": "^1.3.0",
"dotenv": "^8.2.0",
"dotenv-expand": "^5.1.0",
"ejs": "3.0.1",
Expand All @@ -50,6 +51,7 @@
"devDependencies": {
"@types/connect": "^3.4.33",
"@types/cross-spawn": "6.0.1",
"@types/detect-port": "^1.3.0",
"@types/dotenv": "^8.2.0",
"@types/ejs": "3.0.4",
"@types/tapable": "^1.0.5"
Expand Down
39 changes: 27 additions & 12 deletions packages/shuvi/src/server/__tests__/server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ describe('server', () => {
res.end('api1');
})
.use('/header', (req: IIncomingMessage, res: IServerResponse) => {
Object.keys(req.headers).forEach((header) => {
Object.keys(req.headers).forEach(header => {
const val = req.headers[header];
if (typeof val !== 'undefined') {
res.setHeader(header, val);
Expand Down Expand Up @@ -91,15 +91,15 @@ describe('server', () => {
'/server1/header': {
target: `http://${host}:${proxyTarget1Port}`,
headers: {
foo: 'bar',
foo: 'bar'
},
pathRewrite: { '^/server1': '' },
pathRewrite: { '^/server1': '' }
},
'/server2/api': {
target: `http://${host}:${proxyTarget2Port}`,
pathRewrite: { '^/server2': '' },
},
},
pathRewrite: { '^/server2': '' }
}
}
});
server.use('/noproxy', (req: IIncomingMessage, res: IServerResponse) => {
res.end('no proxy');
Expand All @@ -125,22 +125,22 @@ describe('server', () => {
proxy: [
{
context: '/api',
target: `http://${host}:${proxyTarget1Port}`,
target: `http://${host}:${proxyTarget1Port}`
},
{
context: '/server1/header',
target: `http://${host}:${proxyTarget1Port}`,
headers: {
foo: 'bar',
foo: 'bar'
},
pathRewrite: { '^/server1': '' },
pathRewrite: { '^/server1': '' }
},
{
context: '/server2/api',
target: `http://${host}:${proxyTarget2Port}`,
pathRewrite: { '^/server2': '' },
},
],
pathRewrite: { '^/server2': '' }
}
]
});
server.use('/noproxy', (req: IIncomingMessage, res: IServerResponse) => {
res.end('no proxy');
Expand All @@ -161,4 +161,19 @@ describe('server', () => {
expect(resp.body).toEqual('api2');
});
});

test('should detect if port is being used', async done => {
server = new Server();
const anotherServer = new Server();
const port = await findPort();
await server.listen(port);

try {
await anotherServer.listen(port);
} catch (e) {
expect(e.code).toBe('EADDRINUSE');
expect(e.message).toMatch(/is being used./);
done();
}
});
});
13 changes: 13 additions & 0 deletions packages/shuvi/src/server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { IServerProxyConfig, IServerProxyConfigItem } from '@shuvi/types';
import { parse as parseUrl } from 'url';
import { createProxyMiddleware } from 'http-proxy-middleware';
import Connect from 'connect';
import detectPort from 'detect-port';
import {
IConnect,
IIncomingMessage,
Expand Down Expand Up @@ -76,13 +77,25 @@ export class Server {
);
}

async _checkPort(port: number) {
const _port = await detectPort(port);
if (_port !== port) {
const error = new Error(`Port ${port} is being used.`);
Object.assign(error, { code: 'EADDRINUSE' });
throw error;
}
}

async listen(port: number, hostname?: string): Promise<void> {
if (this._server) {
return;
}

await this._checkPort(port);

this.hostname = hostname;
this.port = port;

const srv = (this._server = http.createServer(this.getRequestHandler()));
await new Promise((resolve, reject) => {
// This code catches EADDRINUSE error if the port is already in use
Expand Down
22 changes: 20 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1335,7 +1335,7 @@
globals "^11.1.0"
lodash "^4.17.13"

"@babel/types@7.10.1", "@babel/types@^7.0.0", "@babel/types@^7.10.1", "@babel/types@^7.3.0", "@babel/types@^7.4.4", "@babel/types@^7.7.0", "@babel/types@^7.7.2", "@babel/types@^7.7.4", "@babel/types@^7.8.3", "@babel/types@^7.8.6", "@babel/types@^7.9.0", "@babel/types@^7.9.5":
"@babel/types@^7.0.0", "@babel/types@^7.10.1", "@babel/types@^7.3.0", "@babel/types@^7.4.4", "@babel/types@^7.7.0", "@babel/types@^7.7.2", "@babel/types@^7.7.4", "@babel/types@^7.8.3", "@babel/types@^7.8.6", "@babel/types@^7.9.0", "@babel/types@^7.9.5":
version "7.10.1"
resolved "https://registry.yarnpkg.com/@babel/types/-/types-7.10.1.tgz#6886724d31c8022160a7db895e6731ca33483921"
integrity sha512-L2yqUOpf3tzlW9GVuipgLEcZxnO+96SzR6fjXMuxxNkIgFJ5+07mHCZ+HkHqaeZu8+3LKnNJJ1bKbjBETQAsrA==
Expand Down Expand Up @@ -2484,6 +2484,11 @@
resolved "https://registry.yarnpkg.com/@types/debug/-/debug-4.1.5.tgz#b14efa8852b7768d898906613c23f688713e02cd"
integrity sha512-Q1y515GcOdTHgagaVFhHnIFQ38ygs/kmxdNpvpou+raI9UO3YZcHDngBSYKQklcKlvA7iuQlmIKbzvmxcOE9CQ==

"@types/detect-port@^1.3.0":
version "1.3.0"
resolved "https://registry.npmjs.org/@types/detect-port/-/detect-port-1.3.0.tgz#3e9cbd049ec29e84a2ff7852dbc629c81245774c"
integrity sha512-NnDUDk1Ry5cHljTFetg0BNT79FaJSddTh9RsGOS2v/97DwOUJ+hBkgxtQHF6T8IarZD4i+bFEL787Nz+xpodfA==

"@types/dotenv@^8.2.0":
version "8.2.0"
resolved "https://registry.npmjs.org/@types/dotenv/-/dotenv-8.2.0.tgz#5cd64710c3c98e82d9d15844375a33bf1b45d053"
Expand Down Expand Up @@ -2993,6 +2998,11 @@ acorn@^7.1.0:
resolved "https://registry.yarnpkg.com/acorn/-/acorn-7.1.1.tgz#e35668de0b402f359de515c5482a1ab9f89a69bf"
integrity sha512-add7dgA5ppRPxCFJoAGfMDi7PIBXq1RtGo7BhbLaxwrXPOmw8gq48Y9ozT01hUKy9byMjlR20EJhu5zlkErEkg==

address@^1.0.1:
version "1.1.2"
resolved "https://registry.npmjs.org/address/-/address-1.1.2.tgz#bf1116c9c758c51b7a933d296b72c221ed9428b6"
integrity sha512-aT6camzM4xEA54YVJYSqxz1kv4IHnQZRtThJJHhUMRExaU5spC7jX5ugSwTaTgJliIgs4VhZOk7htClvQ/LmRA==

agent-base@4, agent-base@^4.3.0:
version "4.3.0"
resolved "https://registry.yarnpkg.com/agent-base/-/agent-base-4.3.0.tgz#8165f01c436009bccad0b1d122f05ed770efc6ee"
Expand Down Expand Up @@ -4552,7 +4562,7 @@ dateformat@^3.0.0:
resolved "https://registry.yarnpkg.com/dateformat/-/dateformat-3.0.3.tgz#a6e37499a4d9a9cf85ef5872044d62901c9889ae"
integrity sha512-jyCETtSl3VMZMWeRo7iY1FL19ges1t55hMo5yaam4Jrsm5EPL89UQkoQRyiI+Yf4k8r2ZpdngkV8hr1lIdjb3Q==

debug@2.6.9, debug@^2.2.0, debug@^2.3.3, debug@^2.6.9:
debug@2.6.9, debug@^2.2.0, debug@^2.3.3, debug@^2.6.0, debug@^2.6.9:
version "2.6.9"
resolved "https://registry.yarnpkg.com/debug/-/debug-2.6.9.tgz#5d128515df134ff327e90a4c93f4e077a536341f"
integrity sha512-bC7ElrdJaJnPbAP+1EotYvqZsb3ecl5wi6Bfi6BJTUcNowp6cvspg0jXznRTKDjm/E7AdgFBVeAPVMNcKGsHMA==
Expand Down Expand Up @@ -4724,6 +4734,14 @@ detect-newline@^3.0.0:
resolved "https://registry.yarnpkg.com/detect-newline/-/detect-newline-3.1.0.tgz#576f5dfc63ae1a192ff192d8ad3af6308991b651"
integrity sha512-TLz+x/vEXm/Y7P7wn1EJFNLxYpUD4TgMosxY6fAVJUnJMbupHBOncxyWUG9OpTaH9EBD7uFI5LfEgmMOc54DsA==

detect-port@^1.3.0:
version "1.3.0"
resolved "https://registry.npmjs.org/detect-port/-/detect-port-1.3.0.tgz#d9c40e9accadd4df5cac6a782aefd014d573d1f1"
integrity sha512-E+B1gzkl2gqxt1IhUzwjrxBKRqx1UzC3WLONHinn8S3T6lwV/agVCyitiFOsGJ/eYuEUBvD71MZHy3Pv1G9doQ==
dependencies:
address "^1.0.1"
debug "^2.6.0"

dezalgo@^1.0.0:
version "1.0.3"
resolved "https://registry.yarnpkg.com/dezalgo/-/dezalgo-1.0.3.tgz#7f742de066fc748bc8db820569dddce49bf0d456"
Expand Down

0 comments on commit 9481a3e

Please sign in to comment.