From b15ca84663c803a17165dfec2039c1cc404a10c4 Mon Sep 17 00:00:00 2001 From: uiolee <22849383+uiolee@users.noreply.github.com> Date: Wed, 12 Feb 2025 12:07:54 +0800 Subject: [PATCH 1/2] fix: fail to bind ipv6 fix hexojs/hexo-server#361 --- lib/server.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/server.js b/lib/server.js index 588ed0e..2571d10 100644 --- a/lib/server.js +++ b/lib/server.js @@ -75,6 +75,10 @@ function formatAddress(ip, port, root) { hostname = 'localhost'; } + if (hostname.includes(':')) { + hostname = `[${hostname}]`; + } + const path = root.startsWith('/') ? root : `/${root}`; return new URL(`http://${hostname}:${port}${path}`).toString(); } From d8c70f25c4766aa62e221fbd0e7d7f5e51c253bc Mon Sep 17 00:00:00 2001 From: uiolee <22849383+uiolee@users.noreply.github.com> Date: Wed, 12 Feb 2025 12:08:08 +0800 Subject: [PATCH 2/2] add test case --- test/index.js | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/test/index.js b/test/index.js index 93d4277..83bac7c 100644 --- a/test/index.js +++ b/test/index.js @@ -277,6 +277,42 @@ describe('server', () => { }); }); + it('bind ip `::1`', () => { + const spy = sinon.spy(); + const stub = sinon.stub(hexo.log, 'info'); + stub.callsFake(spy); + + return Promise.using(prepareServer({ip: '::1'}), app => { + spy.args[1][1].should.contain('[::1]:4000'); + }).finally(() => { + stub.restore(); + }); + }); + + it('bind ip `127.0.0.1`', () => { + const spy = sinon.spy(); + const stub = sinon.stub(hexo.log, 'info'); + stub.callsFake(spy); + + return Promise.using(prepareServer({ip: '127.0.0.1'}), app => { + spy.args[1][1].should.contain('127.0.0.1:4000'); + }).finally(() => { + stub.restore(); + }); + }); + + it('bind ip `localhost`', () => { + 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('localhost:4000'); + }).finally(() => { + stub.restore(); + }); + }); + it('static', () => { const spy = sinon.spy(); const stub = sinon.stub(hexo, 'load');