Skip to content

Commit c387043

Browse files
committed
fix(router): convert string to buffer in route stream
1 parent e30605d commit c387043

File tree

2 files changed

+43
-7
lines changed

2 files changed

+43
-7
lines changed

lib/hexo/router.js

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,28 @@ class RouteStream extends Readable {
1414
this.modified = data.modified;
1515
}
1616

17+
// Assume we only accept Buffer, plain object, or string
18+
_toBuffer(data) {
19+
if (data instanceof Buffer) {
20+
return data;
21+
}
22+
if (typeof data === 'object') {
23+
data = JSON.stringify(data);
24+
}
25+
if (typeof data === 'string') {
26+
return Buffer.from(data); // Assume string is UTF-8 encoded string
27+
}
28+
return null;
29+
}
30+
1731
_read() {
1832
const data = this._data;
1933

2034
if (typeof data !== 'function') {
21-
this.push(data);
35+
const bufferData = this._toBuffer(data);
36+
if (bufferData) {
37+
this.push(bufferData);
38+
}
2239
this.push(null);
2340
return;
2441
}
@@ -40,13 +57,11 @@ class RouteStream extends Readable {
4057
data.on('error', err => {
4158
this.emit('error', err);
4259
});
43-
} else if (data instanceof Buffer || typeof data === 'string') {
44-
this.push(data);
45-
this.push(null);
46-
} else if (typeof data === 'object') {
47-
this.push(JSON.stringify(data));
48-
this.push(null);
4960
} else {
61+
const bufferData = this._toBuffer(data);
62+
if (bufferData) {
63+
this.push(bufferData);
64+
}
5065
this.push(null);
5166
}
5267
}).catch(err => {

test/scripts/console/generate.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,27 @@ describe('generate', () => {
261261
});
262262
});
263263

264+
it('should generate all files when bail option is set to true and no errors', async () => {
265+
// Test cases for hexojs/hexo#4499
266+
hexo.extend.generator.register('resource', () =>
267+
[
268+
{
269+
path: 'resource-1',
270+
data: 'string'
271+
},
272+
{
273+
path: 'resource-2',
274+
data: {}
275+
},
276+
{
277+
path: 'resource-3',
278+
data: () => Promise.resolve(Buffer.from('string'))
279+
}
280+
]
281+
);
282+
return generate({ bail: true });
283+
});
284+
264285
it('should generate all files even when concurrency is set', async () => {
265286
await generate({ concurrency: 1 });
266287
return generate({ concurrency: 2 });

0 commit comments

Comments
 (0)