Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions lib/hexo/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,17 @@ class RouteStream extends Readable {
this.modified = data.modified;
}

// Assume we only accept Buffer, plain object, or string
_toBuffer(data: Buffer | object | string): Buffer | null {
// Accept Buffer, plain object, or string. Returns Buffer or string directly to avoid unnecessary conversion
_toBuffer(data: Buffer | object | string): Buffer | string | null {
if (data instanceof Buffer) {
return data;
}
if (typeof data === 'object') {
data = JSON.stringify(data);
}
if (typeof data === 'string') {
return Buffer.from(data); // Assume string is UTF-8 encoded string
// Return string directly to avoid Buffer conversion overhead for large content
return data;
}
return null;
}
Expand Down
8 changes: 8 additions & 0 deletions test/scripts/hexo/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,14 @@ describe('Router', () => {
return checkStream(router.get('test'), JSON.stringify(obj));
});

it('get() - large string content should not be converted to Buffer unnecessarily', () => {
const largeString = 'a'.repeat(100000); // 100KB string

router.set('test', largeString);

return checkStream(router.get('test'), largeString);
});

it('list()', () => {
const router = new Router();

Expand Down
Loading