@jayfong/x-server 2.12.1 → 2.12.3
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/cli/env_util.js +5 -6
- package/lib/_cjs/index.js +16 -0
- package/lib/_cjs/services/request.js +221 -0
- package/lib/_cjs/types/_formstream.js +1 -0
- package/lib/_cjs/x.js +3 -1
- package/lib/cli/env_util.js +5 -6
- package/lib/index.d.ts +2 -0
- package/lib/index.js +3 -1
- package/lib/services/request.d.ts +92 -0
- package/lib/services/request.js +199 -0
- package/lib/types/_formstream.d.ts +49 -0
- package/lib/types/_formstream.js +0 -0
- package/lib/x.d.ts +1 -0
- package/lib/x.js +2 -1
- package/package.json +7 -1
package/lib/_cjs/cli/env_util.js
CHANGED
|
@@ -172,12 +172,11 @@ class EnvUtil {
|
|
|
172
172
|
NODE_ENV: 'development' | 'production';
|
|
173
173
|
${envs.map(env => (0, _vtils.dedent)`
|
|
174
174
|
${env.comment ? (0, _vtils.dedent)`
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
${env.key}: ${
|
|
180
|
-
Array.isArray(env.value) ? `Array<${typeof env.value[0]}>` : typeof env.value};
|
|
175
|
+
/**
|
|
176
|
+
${env.comment.split(/[\r\n]+/g).map(line => `* ${line.trim()}`).join('\n*\n')}
|
|
177
|
+
*/
|
|
178
|
+
` : ''}
|
|
179
|
+
${env.key}: ${Array.isArray(env.value) ? `Array<${env.value[0] ? typeof env.value[0] : 'any'}>` : typeof env.value};
|
|
181
180
|
`).join('\n')}
|
|
182
181
|
}
|
|
183
182
|
}
|
package/lib/_cjs/index.js
CHANGED
|
@@ -194,6 +194,14 @@ Object.keys(_dispose).forEach(function (key) {
|
|
|
194
194
|
exports[key] = _dispose[key];
|
|
195
195
|
});
|
|
196
196
|
|
|
197
|
+
var _emoji = require("./services/emoji");
|
|
198
|
+
|
|
199
|
+
Object.keys(_emoji).forEach(function (key) {
|
|
200
|
+
if (key === "default" || key === "__esModule") return;
|
|
201
|
+
if (key in exports && exports[key] === _emoji[key]) return;
|
|
202
|
+
exports[key] = _emoji[key];
|
|
203
|
+
});
|
|
204
|
+
|
|
197
205
|
var _jwt = require("./services/jwt");
|
|
198
206
|
|
|
199
207
|
Object.keys(_jwt).forEach(function (key) {
|
|
@@ -242,6 +250,14 @@ Object.keys(_redis).forEach(function (key) {
|
|
|
242
250
|
exports[key] = _redis[key];
|
|
243
251
|
});
|
|
244
252
|
|
|
253
|
+
var _request = require("./services/request");
|
|
254
|
+
|
|
255
|
+
Object.keys(_request).forEach(function (key) {
|
|
256
|
+
if (key === "default" || key === "__esModule") return;
|
|
257
|
+
if (key in exports && exports[key] === _request[key]) return;
|
|
258
|
+
exports[key] = _request[key];
|
|
259
|
+
});
|
|
260
|
+
|
|
245
261
|
var _sensitive_words = require("./services/sensitive_words");
|
|
246
262
|
|
|
247
263
|
Object.keys(_sensitive_words).forEach(function (key) {
|
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
|
|
4
|
+
|
|
5
|
+
exports.__esModule = true;
|
|
6
|
+
exports.RequestService = exports.RequestFormUrlencoded = exports.RequestFormStream = exports.RequestFormData = void 0;
|
|
7
|
+
|
|
8
|
+
var _formData = _interopRequireDefault(require("form-data"));
|
|
9
|
+
|
|
10
|
+
var _formstream = _interopRequireDefault(require("formstream"));
|
|
11
|
+
|
|
12
|
+
var _got = _interopRequireDefault(require("got"));
|
|
13
|
+
|
|
14
|
+
var _proxyAgent = require("proxy-agent");
|
|
15
|
+
|
|
16
|
+
class RequestFormUrlencoded extends URLSearchParams {}
|
|
17
|
+
|
|
18
|
+
exports.RequestFormUrlencoded = RequestFormUrlencoded;
|
|
19
|
+
|
|
20
|
+
class RequestFormData extends _formData.default {
|
|
21
|
+
constructor(cb) {
|
|
22
|
+
super();
|
|
23
|
+
|
|
24
|
+
if (cb) {
|
|
25
|
+
cb(this);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
exports.RequestFormData = RequestFormData;
|
|
32
|
+
|
|
33
|
+
class RequestFormStream extends _formstream.default {
|
|
34
|
+
constructor(cb) {
|
|
35
|
+
super();
|
|
36
|
+
|
|
37
|
+
if (cb) {
|
|
38
|
+
cb(this);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
exports.RequestFormStream = RequestFormStream;
|
|
45
|
+
|
|
46
|
+
class RequestService {
|
|
47
|
+
constructor(options) {
|
|
48
|
+
this.options = options;
|
|
49
|
+
this.serviceName = 'request';
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
static getGotOptions(options, responseType) {
|
|
53
|
+
const gotOptions = {
|
|
54
|
+
responseType: responseType === 'stream' ? undefined : responseType,
|
|
55
|
+
rejectUnauthorized: false,
|
|
56
|
+
isStream: responseType === 'stream'
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
if (options.headers) {
|
|
60
|
+
gotOptions.headers = options.headers;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
if (options.followRedirect != null) {
|
|
64
|
+
gotOptions.followRedirect = options.followRedirect;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
if (options.timeoutMs) {
|
|
68
|
+
gotOptions.timeout = options.timeoutMs;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
if (options.proxyUrl) {
|
|
72
|
+
const proxyAgent = new _proxyAgent.ProxyAgent({
|
|
73
|
+
getProxyForUrl: () => options.proxyUrl
|
|
74
|
+
});
|
|
75
|
+
gotOptions.agent = {
|
|
76
|
+
http: proxyAgent,
|
|
77
|
+
https: proxyAgent,
|
|
78
|
+
http2: proxyAgent
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
if (options.encoding) {
|
|
83
|
+
gotOptions.encoding = options.encoding;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
if (options.cookieJar) {
|
|
87
|
+
gotOptions.cookieJar = options.cookieJar;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
return gotOptions;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
static getRaw(options, responseType) {
|
|
94
|
+
let url = options.url;
|
|
95
|
+
|
|
96
|
+
if (options.data) {
|
|
97
|
+
const _url = new URL(url);
|
|
98
|
+
|
|
99
|
+
Object.keys(options.data).forEach(key => {
|
|
100
|
+
_url.searchParams.set(key, options.data[key]);
|
|
101
|
+
});
|
|
102
|
+
url = _url.toString();
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
const res = (0, _got.default)({
|
|
106
|
+
url: url,
|
|
107
|
+
method: 'GET',
|
|
108
|
+
...this.getGotOptions(options, responseType || 'buffer')
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
if (responseType === 'stream') {
|
|
112
|
+
return res;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
return res.then(res => ({
|
|
116
|
+
status: res.statusCode,
|
|
117
|
+
headers: res.headers,
|
|
118
|
+
data: res.body
|
|
119
|
+
}));
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
static postRaw(options, responseType) {
|
|
123
|
+
const gotOptions = this.getGotOptions(options, responseType || 'buffer');
|
|
124
|
+
|
|
125
|
+
if (responseType === 'stream' && options.data instanceof _formstream.default) {
|
|
126
|
+
gotOptions.headers = options.data.headers(gotOptions.headers);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
const res = (0, _got.default)({
|
|
130
|
+
url: options.url,
|
|
131
|
+
method: 'POST',
|
|
132
|
+
...(options.data == null ? {
|
|
133
|
+
body: ''
|
|
134
|
+
} : options.data instanceof _formData.default ? {
|
|
135
|
+
body: options.data
|
|
136
|
+
} : options.data instanceof URLSearchParams ? {
|
|
137
|
+
form: options.data
|
|
138
|
+
} : options.data instanceof _formstream.default ? {} : {
|
|
139
|
+
json: options.data
|
|
140
|
+
}),
|
|
141
|
+
...gotOptions
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
if (responseType === 'stream' && options.data instanceof _formstream.default) {
|
|
145
|
+
options.data.pipe(res);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
if (responseType === 'stream') {
|
|
149
|
+
return res;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
return res.then(res => ({
|
|
153
|
+
status: res.statusCode,
|
|
154
|
+
headers: res.headers,
|
|
155
|
+
data: res.body
|
|
156
|
+
}));
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
static async getText(options) {
|
|
160
|
+
return this.getRaw(options, 'text');
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
static async postText(options) {
|
|
164
|
+
return this.postRaw(options, 'text');
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
static async get(options) {
|
|
168
|
+
return this.getRaw(options, 'json');
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
static async post(options) {
|
|
172
|
+
return this.postRaw(options, 'json');
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
static getStream(options) {
|
|
176
|
+
return this.getRaw(options, 'stream');
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
static postStream(options) {
|
|
180
|
+
return this.postRaw(options, 'stream');
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
getRaw(options, responseType) {
|
|
184
|
+
return RequestService.getRaw({ ...this.options,
|
|
185
|
+
...options
|
|
186
|
+
}, responseType);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
postRaw(options, responseType) {
|
|
190
|
+
return RequestService.postRaw({ ...this.options,
|
|
191
|
+
...options
|
|
192
|
+
}, responseType);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
async getText(options) {
|
|
196
|
+
return this.getRaw(options, 'text');
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
async postText(options) {
|
|
200
|
+
return this.postRaw(options, 'text');
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
async get(options) {
|
|
204
|
+
return this.getRaw(options, 'json');
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
async post(options) {
|
|
208
|
+
return this.postRaw(options, 'json');
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
getStream(options) {
|
|
212
|
+
return this.getRaw(options, 'stream');
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
postStream(options) {
|
|
216
|
+
return this.postRaw(options, 'stream');
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
exports.RequestService = RequestService;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";
|
package/lib/_cjs/x.js
CHANGED
|
@@ -17,6 +17,8 @@ var _emoji = require("./services/emoji");
|
|
|
17
17
|
|
|
18
18
|
var _log = require("./services/log");
|
|
19
19
|
|
|
20
|
+
var _request = require("./services/request");
|
|
21
|
+
|
|
20
22
|
const env = JSON.parse(process.env.X_SERVER_ENVS || '{}');
|
|
21
23
|
const x = {
|
|
22
24
|
appId: env.APP_ID,
|
|
@@ -35,4 +37,4 @@ _fsExtra.default.ensureDirSync(x.dataDir);
|
|
|
35
37
|
|
|
36
38
|
x.register(new _dispose.DisposeService({
|
|
37
39
|
disposeOnExit: true
|
|
38
|
-
}), new _log.LogService(), new _emoji.EmojiService());
|
|
40
|
+
}), new _log.LogService(), new _emoji.EmojiService(), new _request.RequestService());
|
package/lib/cli/env_util.js
CHANGED
|
@@ -158,12 +158,11 @@ export class EnvUtil {
|
|
|
158
158
|
NODE_ENV: 'development' | 'production';
|
|
159
159
|
${envs.map(env => dedent`
|
|
160
160
|
${env.comment ? dedent`
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
${env.key}: ${
|
|
166
|
-
Array.isArray(env.value) ? `Array<${typeof env.value[0]}>` : typeof env.value};
|
|
161
|
+
/**
|
|
162
|
+
${env.comment.split(/[\r\n]+/g).map(line => `* ${line.trim()}`).join('\n*\n')}
|
|
163
|
+
*/
|
|
164
|
+
` : ''}
|
|
165
|
+
${env.key}: ${Array.isArray(env.value) ? `Array<${env.value[0] ? typeof env.value[0] : 'any'}>` : typeof env.value};
|
|
167
166
|
`).join('\n')}
|
|
168
167
|
}
|
|
169
168
|
}
|
package/lib/index.d.ts
CHANGED
|
@@ -22,12 +22,14 @@ export * from './services/cache';
|
|
|
22
22
|
export * from './services/captcha';
|
|
23
23
|
export * from './services/dingtalk';
|
|
24
24
|
export * from './services/dispose';
|
|
25
|
+
export * from './services/emoji';
|
|
25
26
|
export * from './services/jwt';
|
|
26
27
|
export * from './services/log';
|
|
27
28
|
export * from './services/mail';
|
|
28
29
|
export * from './services/pay';
|
|
29
30
|
export * from './services/rate_limit';
|
|
30
31
|
export * from './services/redis';
|
|
32
|
+
export * from './services/request';
|
|
31
33
|
export * from './services/sensitive_words';
|
|
32
34
|
export * from './types/index';
|
|
33
35
|
export * from './x';
|
package/lib/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// @index(['./**/*.ts', '!**/*.test.ts', '!./cli/**', '!./client_helper.ts'], f => `export * from '${f.path}'`)
|
|
1
|
+
// @index(['./**/*.ts', '!**/*.test.ts', '!./cli/**', '!./client_helper.ts', '!**/_*'], f => `export * from '${f.path}'`)
|
|
2
2
|
export * from "./core/define_bus";
|
|
3
3
|
export * from "./core/define_cron";
|
|
4
4
|
export * from "./core/define_handler";
|
|
@@ -23,12 +23,14 @@ export * from "./services/cache";
|
|
|
23
23
|
export * from "./services/captcha";
|
|
24
24
|
export * from "./services/dingtalk";
|
|
25
25
|
export * from "./services/dispose";
|
|
26
|
+
export * from "./services/emoji";
|
|
26
27
|
export * from "./services/jwt";
|
|
27
28
|
export * from "./services/log";
|
|
28
29
|
export * from "./services/mail";
|
|
29
30
|
export * from "./services/pay";
|
|
30
31
|
export * from "./services/rate_limit";
|
|
31
32
|
export * from "./services/redis";
|
|
33
|
+
export * from "./services/request";
|
|
32
34
|
export * from "./services/sensitive_words";
|
|
33
35
|
export * from "./types/index";
|
|
34
36
|
export * from "./x"; // @endindex
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import FormData from 'form-data';
|
|
3
|
+
import FormStream from 'formstream';
|
|
4
|
+
import Request from 'got/dist/source/core';
|
|
5
|
+
import { BaseService } from './base';
|
|
6
|
+
import { CookieJar } from 'tough-cookie';
|
|
7
|
+
export interface RequestServiceOptions {
|
|
8
|
+
proxyUrl?: string;
|
|
9
|
+
timeoutMs?: number;
|
|
10
|
+
headers?: Record<string, any>;
|
|
11
|
+
/**
|
|
12
|
+
* @default true
|
|
13
|
+
*/
|
|
14
|
+
followRedirect?: boolean;
|
|
15
|
+
/**
|
|
16
|
+
* @default "utf-8"
|
|
17
|
+
*/
|
|
18
|
+
encoding?: BufferEncoding;
|
|
19
|
+
cookieJar?: CookieJar;
|
|
20
|
+
}
|
|
21
|
+
export interface RequestServiceResponse<T> {
|
|
22
|
+
status: number;
|
|
23
|
+
headers: Record<string, any>;
|
|
24
|
+
data: T;
|
|
25
|
+
}
|
|
26
|
+
export interface RequestServiceGetOptions extends RequestServiceOptions {
|
|
27
|
+
url: string;
|
|
28
|
+
data?: Record<string, any>;
|
|
29
|
+
}
|
|
30
|
+
export interface RequestServicePostOptions extends RequestServiceOptions {
|
|
31
|
+
url: string;
|
|
32
|
+
data?: Record<string, any> | FormData | URLSearchParams;
|
|
33
|
+
}
|
|
34
|
+
export interface RequestServiceGetStreamOptions extends RequestServiceOptions {
|
|
35
|
+
url: string;
|
|
36
|
+
data?: Record<string, any>;
|
|
37
|
+
}
|
|
38
|
+
export interface RequestServicePostStreamOptions extends RequestServiceOptions {
|
|
39
|
+
url: string;
|
|
40
|
+
data?: Record<string, any> | FormData | URLSearchParams | FormStream;
|
|
41
|
+
}
|
|
42
|
+
export declare class RequestFormUrlencoded extends URLSearchParams {
|
|
43
|
+
}
|
|
44
|
+
export declare class RequestFormData extends FormData {
|
|
45
|
+
constructor(cb?: (formData: FormData) => FormData);
|
|
46
|
+
}
|
|
47
|
+
export declare class RequestFormStream extends FormStream {
|
|
48
|
+
constructor(cb?: (formStream: FormStream) => FormStream);
|
|
49
|
+
}
|
|
50
|
+
export declare class RequestService implements BaseService {
|
|
51
|
+
private options?;
|
|
52
|
+
serviceName: string;
|
|
53
|
+
constructor(options?: RequestServiceOptions);
|
|
54
|
+
private static getGotOptions;
|
|
55
|
+
static getRaw(options: RequestServiceGetStreamOptions): Promise<RequestServiceResponse<Buffer>>;
|
|
56
|
+
static getRaw(options: RequestServiceGetStreamOptions, responseType: 'buffer'): Promise<RequestServiceResponse<Buffer>>;
|
|
57
|
+
static getRaw(options: RequestServiceGetStreamOptions, responseType: 'text'): Promise<RequestServiceResponse<string>>;
|
|
58
|
+
static getRaw<T>(options: RequestServiceGetStreamOptions, responseType: 'json'): Promise<RequestServiceResponse<T>>;
|
|
59
|
+
static getRaw(options: RequestServiceGetStreamOptions, responseType: 'stream'): Request;
|
|
60
|
+
static postRaw(options: RequestServicePostOptions): Promise<RequestServiceResponse<Buffer>>;
|
|
61
|
+
static postRaw(options: RequestServicePostOptions, responseType: 'buffer'): Promise<RequestServiceResponse<Buffer>>;
|
|
62
|
+
static postRaw(options: RequestServicePostOptions, responseType: 'text'): Promise<RequestServiceResponse<string>>;
|
|
63
|
+
static postRaw<T>(options: RequestServicePostOptions, responseType: 'json'): Promise<RequestServiceResponse<T>>;
|
|
64
|
+
static postRaw(options: RequestServicePostOptions, responseType: 'stream'): Request;
|
|
65
|
+
static getText(options: RequestServiceGetStreamOptions): Promise<RequestServiceResponse<string>>;
|
|
66
|
+
static postText(options: RequestServicePostOptions): Promise<RequestServiceResponse<string>>;
|
|
67
|
+
static get<T>(options: RequestServiceGetStreamOptions): Promise<RequestServiceResponse<T>>;
|
|
68
|
+
static post<T>(options: RequestServicePostOptions): Promise<RequestServiceResponse<T>>;
|
|
69
|
+
static getStream(options: RequestServiceGetOptions): Request;
|
|
70
|
+
static postStream(options: RequestServicePostStreamOptions): Request;
|
|
71
|
+
getRaw(options: RequestServiceGetStreamOptions): Promise<RequestServiceResponse<Buffer>>;
|
|
72
|
+
getRaw(options: RequestServiceGetStreamOptions, responseType: 'buffer'): Promise<RequestServiceResponse<Buffer>>;
|
|
73
|
+
getRaw(options: RequestServiceGetStreamOptions, responseType: 'text'): Promise<RequestServiceResponse<string>>;
|
|
74
|
+
getRaw<T>(options: RequestServiceGetStreamOptions, responseType: 'json'): Promise<RequestServiceResponse<T>>;
|
|
75
|
+
getRaw(options: RequestServiceGetStreamOptions, responseType: 'stream'): Request;
|
|
76
|
+
postRaw(options: RequestServicePostOptions): Promise<RequestServiceResponse<Buffer>>;
|
|
77
|
+
postRaw(options: RequestServicePostOptions, responseType: 'buffer'): Promise<RequestServiceResponse<Buffer>>;
|
|
78
|
+
postRaw(options: RequestServicePostOptions, responseType: 'text'): Promise<RequestServiceResponse<string>>;
|
|
79
|
+
postRaw<T>(options: RequestServicePostOptions, responseType: 'json'): Promise<RequestServiceResponse<T>>;
|
|
80
|
+
postRaw(options: RequestServicePostOptions, responseType: 'stream'): Request;
|
|
81
|
+
getText(options: RequestServiceGetStreamOptions): Promise<RequestServiceResponse<string>>;
|
|
82
|
+
postText(options: RequestServicePostOptions): Promise<RequestServiceResponse<string>>;
|
|
83
|
+
get<T>(options: RequestServiceGetStreamOptions): Promise<RequestServiceResponse<T>>;
|
|
84
|
+
post<T>(options: RequestServicePostOptions): Promise<RequestServiceResponse<T>>;
|
|
85
|
+
getStream(options: RequestServiceGetOptions): Request;
|
|
86
|
+
postStream(options: RequestServicePostStreamOptions): Request;
|
|
87
|
+
}
|
|
88
|
+
declare module '../x' {
|
|
89
|
+
interface X {
|
|
90
|
+
request: RequestService;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
import FormData from 'form-data';
|
|
2
|
+
import FormStream from 'formstream';
|
|
3
|
+
import got from 'got';
|
|
4
|
+
import { ProxyAgent } from 'proxy-agent';
|
|
5
|
+
export class RequestFormUrlencoded extends URLSearchParams {}
|
|
6
|
+
export class RequestFormData extends FormData {
|
|
7
|
+
constructor(cb) {
|
|
8
|
+
super();
|
|
9
|
+
|
|
10
|
+
if (cb) {
|
|
11
|
+
cb(this);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
}
|
|
16
|
+
export class RequestFormStream extends FormStream {
|
|
17
|
+
constructor(cb) {
|
|
18
|
+
super();
|
|
19
|
+
|
|
20
|
+
if (cb) {
|
|
21
|
+
cb(this);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
}
|
|
26
|
+
export class RequestService {
|
|
27
|
+
constructor(options) {
|
|
28
|
+
this.options = options;
|
|
29
|
+
this.serviceName = 'request';
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
static getGotOptions(options, responseType) {
|
|
33
|
+
const gotOptions = {
|
|
34
|
+
responseType: responseType === 'stream' ? undefined : responseType,
|
|
35
|
+
rejectUnauthorized: false,
|
|
36
|
+
isStream: responseType === 'stream'
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
if (options.headers) {
|
|
40
|
+
gotOptions.headers = options.headers;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
if (options.followRedirect != null) {
|
|
44
|
+
gotOptions.followRedirect = options.followRedirect;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
if (options.timeoutMs) {
|
|
48
|
+
gotOptions.timeout = options.timeoutMs;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
if (options.proxyUrl) {
|
|
52
|
+
const proxyAgent = new ProxyAgent({
|
|
53
|
+
getProxyForUrl: () => options.proxyUrl
|
|
54
|
+
});
|
|
55
|
+
gotOptions.agent = {
|
|
56
|
+
http: proxyAgent,
|
|
57
|
+
https: proxyAgent,
|
|
58
|
+
http2: proxyAgent
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
if (options.encoding) {
|
|
63
|
+
gotOptions.encoding = options.encoding;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
if (options.cookieJar) {
|
|
67
|
+
gotOptions.cookieJar = options.cookieJar;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
return gotOptions;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
static getRaw(options, responseType) {
|
|
74
|
+
let url = options.url;
|
|
75
|
+
|
|
76
|
+
if (options.data) {
|
|
77
|
+
const _url = new URL(url);
|
|
78
|
+
|
|
79
|
+
Object.keys(options.data).forEach(key => {
|
|
80
|
+
_url.searchParams.set(key, options.data[key]);
|
|
81
|
+
});
|
|
82
|
+
url = _url.toString();
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const res = got({
|
|
86
|
+
url: url,
|
|
87
|
+
method: 'GET',
|
|
88
|
+
...this.getGotOptions(options, responseType || 'buffer')
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
if (responseType === 'stream') {
|
|
92
|
+
return res;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
return res.then(res => ({
|
|
96
|
+
status: res.statusCode,
|
|
97
|
+
headers: res.headers,
|
|
98
|
+
data: res.body
|
|
99
|
+
}));
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
static postRaw(options, responseType) {
|
|
103
|
+
const gotOptions = this.getGotOptions(options, responseType || 'buffer');
|
|
104
|
+
|
|
105
|
+
if (responseType === 'stream' && options.data instanceof FormStream) {
|
|
106
|
+
gotOptions.headers = options.data.headers(gotOptions.headers);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
const res = got({
|
|
110
|
+
url: options.url,
|
|
111
|
+
method: 'POST',
|
|
112
|
+
...(options.data == null ? {
|
|
113
|
+
body: ''
|
|
114
|
+
} : options.data instanceof FormData ? {
|
|
115
|
+
body: options.data
|
|
116
|
+
} : options.data instanceof URLSearchParams ? {
|
|
117
|
+
form: options.data
|
|
118
|
+
} : options.data instanceof FormStream ? {} : {
|
|
119
|
+
json: options.data
|
|
120
|
+
}),
|
|
121
|
+
...gotOptions
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
if (responseType === 'stream' && options.data instanceof FormStream) {
|
|
125
|
+
options.data.pipe(res);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
if (responseType === 'stream') {
|
|
129
|
+
return res;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
return res.then(res => ({
|
|
133
|
+
status: res.statusCode,
|
|
134
|
+
headers: res.headers,
|
|
135
|
+
data: res.body
|
|
136
|
+
}));
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
static async getText(options) {
|
|
140
|
+
return this.getRaw(options, 'text');
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
static async postText(options) {
|
|
144
|
+
return this.postRaw(options, 'text');
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
static async get(options) {
|
|
148
|
+
return this.getRaw(options, 'json');
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
static async post(options) {
|
|
152
|
+
return this.postRaw(options, 'json');
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
static getStream(options) {
|
|
156
|
+
return this.getRaw(options, 'stream');
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
static postStream(options) {
|
|
160
|
+
return this.postRaw(options, 'stream');
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
getRaw(options, responseType) {
|
|
164
|
+
return RequestService.getRaw({ ...this.options,
|
|
165
|
+
...options
|
|
166
|
+
}, responseType);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
postRaw(options, responseType) {
|
|
170
|
+
return RequestService.postRaw({ ...this.options,
|
|
171
|
+
...options
|
|
172
|
+
}, responseType);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
async getText(options) {
|
|
176
|
+
return this.getRaw(options, 'text');
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
async postText(options) {
|
|
180
|
+
return this.postRaw(options, 'text');
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
async get(options) {
|
|
184
|
+
return this.getRaw(options, 'json');
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
async post(options) {
|
|
188
|
+
return this.postRaw(options, 'json');
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
getStream(options) {
|
|
192
|
+
return this.getRaw(options, 'stream');
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
postStream(options) {
|
|
196
|
+
return this.postRaw(options, 'stream');
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
/// <reference types="node" />
|
|
3
|
+
declare module 'formstream' {
|
|
4
|
+
import { Stream, Readable } from 'stream';
|
|
5
|
+
class FormStream extends Stream {
|
|
6
|
+
/**
|
|
7
|
+
* Add a normal field to the form.
|
|
8
|
+
*
|
|
9
|
+
* @param name Name of field
|
|
10
|
+
* @param value Value of field
|
|
11
|
+
*/
|
|
12
|
+
field(name: string, value: string): this;
|
|
13
|
+
/**
|
|
14
|
+
* Add a local file to be uploaded to the form.
|
|
15
|
+
*
|
|
16
|
+
* @param name Name of file field
|
|
17
|
+
* @param filepath Local path of the file to be uploaded
|
|
18
|
+
* @param filename Name of the file (will be the base name of filepath if empty)
|
|
19
|
+
* @param filesize Size of the file (will not generate Content-Length header if not specified)
|
|
20
|
+
*/
|
|
21
|
+
file(name: string, filepath: string, filename?: string, filesize?: number): this;
|
|
22
|
+
/**
|
|
23
|
+
* Add a buffer as a file to upload.
|
|
24
|
+
*
|
|
25
|
+
* @param name Name of field
|
|
26
|
+
* @param buffer The buffer to be uploaded
|
|
27
|
+
* @param filename The file name that tells the remote server
|
|
28
|
+
* @param contentType Content-Type (aka. MIME Type) of content (will be infered with filename if empty)
|
|
29
|
+
*/
|
|
30
|
+
buffer(name: string, buffer: Buffer, filename: string, contentType?: string): this;
|
|
31
|
+
/**
|
|
32
|
+
* Add a readable stream as a file to upload. Event 'error' will be emitted if an error occured.
|
|
33
|
+
*
|
|
34
|
+
* @param name Name of field
|
|
35
|
+
* @param stream A readable stream to be piped
|
|
36
|
+
* @param filename The file name that tells the remote server
|
|
37
|
+
* @param contentType Content-Type (aka. MIME Type) of content (will be infered with filename if empty)
|
|
38
|
+
* @param size Size of the stream (will not generate Content-Length header if not specified)
|
|
39
|
+
*/
|
|
40
|
+
stream(name: string, stream: Readable, filename: string, contentType?: string, size?: number): this;
|
|
41
|
+
/**
|
|
42
|
+
* Get headers for the request.
|
|
43
|
+
*
|
|
44
|
+
* @param additionalHeaders Additional headers
|
|
45
|
+
*/
|
|
46
|
+
headers(additionalHeaders?: Record<string, any>): Record<string, any>;
|
|
47
|
+
}
|
|
48
|
+
export = FormStream;
|
|
49
|
+
}
|
|
File without changes
|
package/lib/x.d.ts
CHANGED
package/lib/x.js
CHANGED
|
@@ -4,6 +4,7 @@ import path from 'path';
|
|
|
4
4
|
import { DisposeService } from "./services/dispose";
|
|
5
5
|
import { EmojiService } from "./services/emoji";
|
|
6
6
|
import { LogService } from "./services/log";
|
|
7
|
+
import { RequestService } from "./services/request";
|
|
7
8
|
const env = JSON.parse(process.env.X_SERVER_ENVS || '{}');
|
|
8
9
|
export const x = {
|
|
9
10
|
appId: env.APP_ID,
|
|
@@ -19,4 +20,4 @@ export const x = {
|
|
|
19
20
|
fs.ensureDirSync(x.dataDir);
|
|
20
21
|
x.register(new DisposeService({
|
|
21
22
|
disposeOnExit: true
|
|
22
|
-
}), new LogService(), new EmojiService());
|
|
23
|
+
}), new LogService(), new EmojiService(), new RequestService());
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jayfong/x-server",
|
|
3
|
-
"version": "2.12.
|
|
3
|
+
"version": "2.12.3",
|
|
4
4
|
"license": "ISC",
|
|
5
5
|
"sideEffects": false,
|
|
6
6
|
"main": "lib/_cjs/index.js",
|
|
@@ -54,7 +54,10 @@
|
|
|
54
54
|
"exit-hook": "^2.2.1",
|
|
55
55
|
"fast-xml-parser": "^4.2.5",
|
|
56
56
|
"fastify": "^4.19.2",
|
|
57
|
+
"form-data": "^4.0.0",
|
|
58
|
+
"formstream": "^1.2.0",
|
|
57
59
|
"fs-extra": "^10.0.1",
|
|
60
|
+
"get-port": "^7.0.0",
|
|
58
61
|
"globby": "^11",
|
|
59
62
|
"got": "^11.8.2",
|
|
60
63
|
"http-errors": "^2.0.0",
|
|
@@ -68,9 +71,11 @@
|
|
|
68
71
|
"nodemailer": "^6.7.3",
|
|
69
72
|
"pino-pretty": "^10.0.1",
|
|
70
73
|
"prisma": "^4.8.0",
|
|
74
|
+
"proxy-agent": "^6.2.2",
|
|
71
75
|
"select-run": "^1.1.2",
|
|
72
76
|
"supports-color": "^8",
|
|
73
77
|
"svg-captcha": "^1.4.0",
|
|
78
|
+
"tough-cookie": "^4.1.3",
|
|
74
79
|
"ts-morph": "^12.2.0",
|
|
75
80
|
"utf-8-validate": "^5.0.9",
|
|
76
81
|
"vscode-generate-index-standalone": "^1.7.1",
|
|
@@ -95,6 +100,7 @@
|
|
|
95
100
|
"node-fetch": "^3.3.1",
|
|
96
101
|
"npm-check-updates": "^12.5.9",
|
|
97
102
|
"prettier": "^2.8.8",
|
|
103
|
+
"proxy": "^2.1.1",
|
|
98
104
|
"standard-version": "^9.3.2",
|
|
99
105
|
"typescript": "^5.1.6",
|
|
100
106
|
"typescript-snapshots-plugin": "^1.7.0"
|