Skip to content

Commit cef4503

Browse files
committed
Require Node.js 12.20 and support typed arrays
1 parent 315fcfb commit cef4503

File tree

4 files changed

+22
-7
lines changed

4 files changed

+22
-7
lines changed

package.json

+4-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"dist"
1212
],
1313
"engines": {
14-
"node": ">=12"
14+
"node": ">=12.20"
1515
},
1616
"scripts": {
1717
"build": "tsc",
@@ -31,12 +31,14 @@
3131
"dependencies": {
3232
"cheerio": "^1.0.0-rc.10",
3333
"content-type": "^1.0.4",
34-
"iconv-lite": "^0.6.3"
34+
"iconv-lite": "^0.6.3",
35+
"type-fest": "^2.0.0"
3536
},
3637
"devDependencies": {
3738
"@richienb/tsconfig": "^0.3.0",
3839
"@types/cheerio": "^0.22.30",
3940
"@types/content-type": "^1.1.5",
41+
"@types/node": "^16.4.13",
4042
"ava": "^3.15.0",
4143
"node-fetch": "3.0.0-beta.10",
4244
"ts-node": "^10.1.0",

readme.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ convertBody(content);
2222

2323
#### content
2424

25-
Type: [`Buffer`](https://nodejs.org/api/buffer.html#buffer_class_buffer)
25+
Type: [`Buffer`](https://nodejs.org/api/buffer.html#buffer_class_buffer) | [`TypedArray`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Typed_arrays)
2626

2727
The content to stringify.
2828

source/index.ts

+9-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import type {Buffer} from 'node:buffer';
1+
import {Buffer} from 'node:buffer';
22
import iconv from 'iconv-lite';
3+
import type {TypedArray} from 'type-fest';
34
import getCharset from './utils/get-charset.js';
45

56
/**
@@ -15,10 +16,14 @@ import convertBody from 'fetch-charset-detection';
1516
convertBody(content);
1617
```
1718
*/
18-
export default function convertBody(content: Buffer, headers?: Headers): string {
19+
export default function convertBody(content: Buffer | ArrayBuffer | SharedArrayBuffer | TypedArray, headers?: Headers): string {
20+
if (!Buffer.isBuffer(content)) {
21+
content = Buffer.from(content);
22+
}
23+
1924
// Turn raw buffers into a single utf-8 buffer
2025
return iconv.decode(
21-
content,
22-
getCharset(content, headers),
26+
content as Buffer,
27+
getCharset(content as Buffer, headers),
2328
);
2429
}

test.ts

+8
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
import type {Buffer} from 'node:buffer';
12
import test from 'ava';
23
import {Headers} from 'node-fetch';
34
import iconv from 'iconv-lite';
45
import convertBody from './source/index.js';
56

7+
const toArrayBuffer = (buffer: Buffer): ArrayBuffer => buffer.buffer.slice(0, buffer.byteLength);
8+
69
test('should support encoding decode, xml dtd detect', t => {
710
const text = '<?xml version="1.0" encoding="EUC-JP"?><title>日本語</title>';
811
t.is(convertBody(iconv.encode(text, 'EUC-JP'), new Headers({'Content-Type': 'text/xml'})), text);
@@ -39,3 +42,8 @@ test('should only do encoding detection up to 1024 bytes', t => {
3942
const padding = 'a'.repeat(1200);
4043
t.not(convertBody(iconv.encode(padding + text, 'gbk'), new Headers({'Content-Type': 'text/html', 'Transfer-Encoding': 'chunked'})), text);
4144
});
45+
46+
test('should support typed arrays', t => {
47+
const text = '<div>日本語</div>';
48+
t.is(convertBody(toArrayBuffer(iconv.encode(text, 'Shift_JIS')), new Headers({'Content-Type': 'text/html; charset=Shift-JIS'})), text);
49+
});

0 commit comments

Comments
 (0)