@jayfong/x-request 2.67.0 → 2.68.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/_cjs/index.js +18 -8
- package/lib/index.d.ts +1 -1
- package/lib/index.js +18 -8
- package/package.json +1 -1
package/lib/_cjs/index.js
CHANGED
|
@@ -5,6 +5,7 @@ exports.__esModule = true;
|
|
|
5
5
|
exports.xr = exports.XRequestFormUrlencoded = exports.XRequestFormStream = exports.XRequestFormFile = exports.XRequestFormData = exports.XRequest = void 0;
|
|
6
6
|
var _assert = _interopRequireDefault(require("assert"));
|
|
7
7
|
var _fs = _interopRequireDefault(require("fs"));
|
|
8
|
+
var _nodeUtil = require("node:util");
|
|
8
9
|
var _stream = require("stream");
|
|
9
10
|
var _formData = _interopRequireDefault(require("form-data"));
|
|
10
11
|
var _formstream = _interopRequireDefault(require("formstream"));
|
|
@@ -89,7 +90,7 @@ class XRequest {
|
|
|
89
90
|
}
|
|
90
91
|
const gotOptions = {
|
|
91
92
|
url: options.url,
|
|
92
|
-
responseType: responseType === 'stream' ? undefined : responseType,
|
|
93
|
+
responseType: responseType === 'stream' ? undefined : responseType === 'text' && options.autoTransformEncoding ? 'buffer' : responseType,
|
|
93
94
|
https: {
|
|
94
95
|
rejectUnauthorized: false
|
|
95
96
|
},
|
|
@@ -129,14 +130,23 @@ class XRequest {
|
|
|
129
130
|
}
|
|
130
131
|
static afterResponse(options, responseType, res) {
|
|
131
132
|
if (responseType === 'text' && options.autoTransformEncoding) {
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
133
|
+
if (options.autoTransformEncoding === true) {
|
|
134
|
+
const contentType = String(res.headers['content-type']);
|
|
135
|
+
if (contentType.includes('charset=')) {
|
|
136
|
+
const charset = contentType.match(/charset=([^;]+)/)[1].toLowerCase();
|
|
137
|
+
if (charset !== 'utf-8' && charset !== 'utf8') {
|
|
138
|
+
try {
|
|
139
|
+
res.data = new _nodeUtil.TextDecoder(charset).decode(res.data);
|
|
140
|
+
} catch {}
|
|
141
|
+
}
|
|
139
142
|
}
|
|
143
|
+
} else {
|
|
144
|
+
try {
|
|
145
|
+
res.data = new _nodeUtil.TextDecoder(options.autoTransformEncoding).decode(res.data);
|
|
146
|
+
} catch {}
|
|
147
|
+
}
|
|
148
|
+
if (Buffer.isBuffer(res.data)) {
|
|
149
|
+
res.data = res.data.toString('utf-8');
|
|
140
150
|
}
|
|
141
151
|
}
|
|
142
152
|
return res;
|
package/lib/index.d.ts
CHANGED
package/lib/index.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import assert from 'assert';
|
|
2
2
|
import fs from 'fs';
|
|
3
|
+
import { TextDecoder } from 'node:util';
|
|
3
4
|
import { Readable } from 'stream';
|
|
4
5
|
import FormData from 'form-data';
|
|
5
6
|
import FormStream from 'formstream';
|
|
@@ -80,7 +81,7 @@ export class XRequest {
|
|
|
80
81
|
}
|
|
81
82
|
const gotOptions = {
|
|
82
83
|
url: options.url,
|
|
83
|
-
responseType: responseType === 'stream' ? undefined : responseType,
|
|
84
|
+
responseType: responseType === 'stream' ? undefined : responseType === 'text' && options.autoTransformEncoding ? 'buffer' : responseType,
|
|
84
85
|
https: {
|
|
85
86
|
rejectUnauthorized: false
|
|
86
87
|
},
|
|
@@ -120,14 +121,23 @@ export class XRequest {
|
|
|
120
121
|
}
|
|
121
122
|
static afterResponse(options, responseType, res) {
|
|
122
123
|
if (responseType === 'text' && options.autoTransformEncoding) {
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
124
|
+
if (options.autoTransformEncoding === true) {
|
|
125
|
+
const contentType = String(res.headers['content-type']);
|
|
126
|
+
if (contentType.includes('charset=')) {
|
|
127
|
+
const charset = contentType.match(/charset=([^;]+)/)[1].toLowerCase();
|
|
128
|
+
if (charset !== 'utf-8' && charset !== 'utf8') {
|
|
129
|
+
try {
|
|
130
|
+
res.data = new TextDecoder(charset).decode(res.data);
|
|
131
|
+
} catch {}
|
|
132
|
+
}
|
|
130
133
|
}
|
|
134
|
+
} else {
|
|
135
|
+
try {
|
|
136
|
+
res.data = new TextDecoder(options.autoTransformEncoding).decode(res.data);
|
|
137
|
+
} catch {}
|
|
138
|
+
}
|
|
139
|
+
if (Buffer.isBuffer(res.data)) {
|
|
140
|
+
res.data = res.data.toString('utf-8');
|
|
131
141
|
}
|
|
132
142
|
}
|
|
133
143
|
return res;
|