From 5818d29320b55a13154db1307a3c9596e2e84731 Mon Sep 17 00:00:00 2001 From: uiolee <22849383+uiolee@users.noreply.github.com> Date: Tue, 11 Feb 2025 21:56:56 +0800 Subject: [PATCH 1/5] feat: display server's listening address --- lib/server.js | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/lib/server.js b/lib/server.js index 588ed0e..f20e3ca 100644 --- a/lib/server.js +++ b/lib/server.js @@ -2,7 +2,7 @@ const connect = require('connect'); const http = require('http'); -const { underline } = require('picocolors'); +const { underline, bold } = require('picocolors'); const Promise = require('bluebird'); const open = require('open'); const net = require('net'); @@ -21,14 +21,19 @@ module.exports = function(args) { return this.watch(); }).then(() => startServer(http.createServer(app), port, ip)).then(server => { + const listeningAddress = getListeningAddress(server.address()); + this.log.info('hexo-server listening on %s . Press Ctrl+C to stop.', bold(listeningAddress)); + + return server; + }).then(server => { const addr = server.address(); - const addrString = formatAddress(ip || addr.address, addr.port, root); + const localAddress = getLocalAddress(ip || addr.address, addr.port, root); - this.log.info('Hexo is running at %s . Press Ctrl+C to stop.', underline(addrString)); + this.log.info('Preview your site via: %s ', underline(localAddress)); this.emit('server'); if (args.o || args.open) { - open(addrString); + open(localAddress); } return server; @@ -69,7 +74,18 @@ function checkPort(ip, port) { }).then(() => { server.close(); }); } -function formatAddress(ip, port, root) { +function getListeningAddress(serverAddress) { + const {address, port, family} = serverAddress; + let host; + if (family === 'IPv6') { + host = `[${address}]`; + } else { + host = address; + } + return `${host}:${port}`; +} + +function getLocalAddress(ip, port, root) { let hostname = ip; if (ip === '0.0.0.0' || ip === '::') { hostname = 'localhost'; From cbdf4c1694ae42feb9a056c244cbe87d21348dc5 Mon Sep 17 00:00:00 2001 From: uiolee <22849383+uiolee@users.noreply.github.com> Date: Tue, 11 Feb 2025 21:57:20 +0800 Subject: [PATCH 2/5] fix fail test --- test/index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/index.js b/test/index.js index 93d4277..8d4ad17 100644 --- a/test/index.js +++ b/test/index.js @@ -259,7 +259,7 @@ describe('server', () => { stub.callsFake(spy); return Promise.using(prepareServer(), app => { - spy.args[1][1].should.contain('localhost'); + spy.args[2][1].should.contain('localhost'); }).finally(() => { stub.restore(); }); @@ -271,7 +271,7 @@ describe('server', () => { stub.callsFake(spy); return Promise.using(prepareServer({ip: '::'}), app => { - spy.args[1][1].should.contain('localhost'); + spy.args[2][1].should.contain('localhost'); }).finally(() => { stub.restore(); }); From 2059078bd31b9b9c9ea299522e5789331b2d3822 Mon Sep 17 00:00:00 2001 From: uiolee <22849383+uiolee@users.noreply.github.com> Date: Tue, 11 Feb 2025 21:57:33 +0800 Subject: [PATCH 3/5] add test case for changes --- test/index.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/test/index.js b/test/index.js index 8d4ad17..85f72cd 100644 --- a/test/index.js +++ b/test/index.js @@ -277,6 +277,18 @@ describe('server', () => { }); }); + it('display listening address', () => { + const spy = sinon.spy(); + const stub = sinon.stub(hexo.log, 'info'); + stub.callsFake(spy); + + return Promise.using(prepareServer({ip: 'localhost'}), app => { + spy.args[1][1].should.contain('::1'); + }).finally(() => { + stub.restore(); + }); + }); + it('static', () => { const spy = sinon.spy(); const stub = sinon.stub(hexo, 'load'); From 99d014aa84f2bd6bac8e1a6a7db09de26ad10dbd Mon Sep 17 00:00:00 2001 From: uiolee <22849383+uiolee@users.noreply.github.com> Date: Tue, 11 Feb 2025 22:38:13 +0800 Subject: [PATCH 4/5] test: add ipv4 address --- test/index.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/test/index.js b/test/index.js index 85f72cd..7635842 100644 --- a/test/index.js +++ b/test/index.js @@ -283,7 +283,9 @@ describe('server', () => { stub.callsFake(spy); return Promise.using(prepareServer({ip: 'localhost'}), app => { - spy.args[1][1].should.contain('::1'); + spy.args[1][1].should.satisfy(addr => { + return addr.includes('::1') || addr.includes('127.0.0.1') + }); }).finally(() => { stub.restore(); }); From 56ed44a64d8973df40b02c4d4d62aeb76c2b9647 Mon Sep 17 00:00:00 2001 From: uiolee <22849383+uiolee@users.noreply.github.com> Date: Tue, 11 Feb 2025 22:44:15 +0800 Subject: [PATCH 5/5] test: rename variable --- test/index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/index.js b/test/index.js index 7635842..4c558d3 100644 --- a/test/index.js +++ b/test/index.js @@ -283,8 +283,8 @@ describe('server', () => { stub.callsFake(spy); return Promise.using(prepareServer({ip: 'localhost'}), app => { - spy.args[1][1].should.satisfy(addr => { - return addr.includes('::1') || addr.includes('127.0.0.1') + spy.args[1][1].should.satisfy(listenAddress => { + return listenAddress.includes('::1') || listenAddress.includes('127.0.0.1'); }); }).finally(() => { stub.restore();