@wiajs/request 3.0.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/index.js ADDED
@@ -0,0 +1 @@
1
+ export {default} from './lib'
@@ -0,0 +1,24 @@
1
+ import stream from 'node:stream';
2
+ let ZlibTransform = class ZlibTransform extends stream.Transform {
3
+ __transform(chunk, encoding, callback) {
4
+ this.push(chunk);
5
+ callback();
6
+ }
7
+ _transform(chunk, encoding, callback) {
8
+ if (chunk.length !== 0) {
9
+ this._transform = this.__transform;
10
+ // Add Default Compression headers if no zlib headers are present
11
+ if (chunk[0] !== 120) {
12
+ // Hex: 78
13
+ const header = Buffer.alloc(2);
14
+ header[0] = 120 // Hex: 78
15
+ ;
16
+ header[1] = 156 // Hex: 9C
17
+ ;
18
+ this.push(header, encoding);
19
+ }
20
+ }
21
+ this.__transform(chunk, encoding, callback);
22
+ }
23
+ };
24
+ export default ZlibTransform;
@@ -0,0 +1,100 @@
1
+ export default class Caseless {
2
+ /**
3
+ * @param {*} dict
4
+ */ constructor(dict){
5
+ this.dict = dict || {};
6
+ }
7
+ /**
8
+ *
9
+ * @param {*} name
10
+ * @param {*} value
11
+ * @param {*} clobber
12
+ * @returns
13
+ */ set(name, value, clobber) {
14
+ if (typeof name === 'object') {
15
+ for (const n of name){
16
+ this.set(n, name[n], value);
17
+ }
18
+ } else {
19
+ if (typeof clobber === 'undefined') clobber = true;
20
+ const has = this.has(name);
21
+ if (!clobber && has) this.dict[has] = this.dict[has] + ',' + value;
22
+ else this.dict[has || name] = value;
23
+ return has;
24
+ }
25
+ }
26
+ /**
27
+ *
28
+ * @param {string} name
29
+ * @returns
30
+ */ has(name) {
31
+ const keys = Object.keys(this.dict);
32
+ name = name.toLowerCase();
33
+ for(let i = 0; i < keys.length; i++){
34
+ if (keys[i].toLowerCase() === name) return keys[i];
35
+ }
36
+ return false;
37
+ }
38
+ /**
39
+ *
40
+ * @param {string} name
41
+ * @returns
42
+ */ get(name) {
43
+ name = name.toLowerCase();
44
+ let result;
45
+ let _key;
46
+ const headers = this.dict;
47
+ for (const key of Object.keys(headers)){
48
+ _key = key.toLowerCase();
49
+ if (name === _key) result = headers[key];
50
+ }
51
+ return result;
52
+ }
53
+ /**
54
+ *
55
+ * @param {string} name
56
+ * @returns
57
+ */ swap(name) {
58
+ const has = this.has(name);
59
+ if (has === name) return;
60
+ if (!has) throw new Error('There is no header than matches "' + name + '"');
61
+ this.dict[name] = this.dict[has];
62
+ delete this.dict[has];
63
+ }
64
+ del(name) {
65
+ name = String(name).toLowerCase();
66
+ let deleted = false;
67
+ let changed = 0;
68
+ const dict = this.dict;
69
+ for (const key of Object.keys(this.dict)){
70
+ if (name === String(key).toLowerCase()) {
71
+ deleted = delete dict[key];
72
+ changed += 1;
73
+ }
74
+ }
75
+ return changed === 0 ? true : deleted;
76
+ }
77
+ }
78
+ /**
79
+ *
80
+ * @param {*} resp
81
+ * @param {*} headers
82
+ * @returns
83
+ */ export function httpify(resp, headers) {
84
+ const c = new Caseless(headers);
85
+ resp.setHeader = (key, value, clobber)=>{
86
+ if (typeof value === 'undefined') return;
87
+ return c.set(key, value, clobber);
88
+ };
89
+ resp.hasHeader = (key)=>{
90
+ return c.has(key);
91
+ };
92
+ resp.getHeader = (key)=>{
93
+ return c.get(key);
94
+ };
95
+ resp.removeHeader = (key)=>{
96
+ return c.del(key);
97
+ };
98
+ resp.headers = c.dict;
99
+ return c;
100
+ }
package/lib/index.js ADDED
@@ -0,0 +1,104 @@
1
+ /**
2
+ * from 'https://github.com/follow-redirects/follow-redirects'
3
+ * 修改以支持http、https 代理服务器
4
+ * 代理模式下,http or https 请求,取决于 proxy 代理服务器,而不是目的服务器。
5
+ */ import { log as Log, name } from '@wiajs/log';
6
+ import Request from './request.js';
7
+ import utils from './utils.js';
8
+ const log = Log({
9
+ env: `wia:req:${name(__filename)}`
10
+ });
11
+ (function detectUnsupportedEnvironment() {
12
+ const looksLikeNode = typeof process !== 'undefined';
13
+ const looksLikeBrowser = typeof window !== 'undefined' && typeof document !== 'undefined';
14
+ const looksLikeV8 = utils.isFunction(Error.captureStackTrace);
15
+ if (!looksLikeNode && (looksLikeBrowser || !looksLikeV8)) {
16
+ log.warn('The follow-redirects package should be excluded from browser builds.');
17
+ }
18
+ })();
19
+ /**
20
+ * 封装http(s),实现重定向
21
+ * 重定向可能切换http、https
22
+ * 支持隧道及非隧道、http(s)代理
23
+ */ /**
24
+ * 初始化参数
25
+ * @param {*} uri
26
+ * @param {*} options
27
+ * @param {*} callback
28
+ * @returns
29
+ */ function init(uri, options, callback) {
30
+ try {
31
+ // Parse parameters, ensuring that input is an object
32
+ if (utils.isURL(uri)) uri = utils.spreadUrlObject(uri);
33
+ else if (utils.isString(uri)) uri = utils.spreadUrlObject(utils.parseUrl(uri));
34
+ else {
35
+ callback = options;
36
+ options = utils.validateUrl(uri);
37
+ uri = {};
38
+ }
39
+ if (utils.isFunction(options)) {
40
+ callback = options;
41
+ options = null;
42
+ }
43
+ // copy options
44
+ options = {
45
+ ...uri,
46
+ ...options
47
+ };
48
+ if (!utils.isString(options.host) && !utils.isString(options.hostname)) options.hostname = '::1';
49
+ R = {
50
+ opts: options,
51
+ cb: callback
52
+ };
53
+ } catch (e) {
54
+ log.err(e, 'init');
55
+ }
56
+ return R;
57
+ }
58
+ /**
59
+ * Executes a request, following redirects
60
+ * 替换原 http(s).request,参数类似
61
+ * 注意变参 (options[, callback]) or (url[, options][, callback])
62
+ maxRedirects: _.maxRedirects,
63
+ maxBodyLength: _.maxBodyLength,
64
+ * @param {*} uri/options
65
+ * @param {*} options/callback
66
+ * @param {*} callback/null
67
+ * @returns
68
+ */ function request(uri, options, callback) {
69
+ let R1 = null;
70
+ try {
71
+ const { opts, cb } = init(uri, options, callback);
72
+ // log.debug('request', {options})
73
+ R1 = new Request(opts, cb);
74
+ } catch (e) {
75
+ log.err(e, 'request');
76
+ }
77
+ return R1;
78
+ }
79
+ /**
80
+ * 执行简单的非stream数据请求
81
+ * 复杂数据,请使用 @wiajs/req库(fork from axios),该库封装了当前库,提供了更多功能
82
+ * organize params for patch, post, put, head, del
83
+ * @param {string} verb
84
+ * @returns {Request} Duplex 流
85
+ */ function fn(verb) {
86
+ const method = verb.toUpperCase();
87
+ return (uri, options, callback)=>{
88
+ const { opts, cb } = init(uri, options, callback);
89
+ opts.method = method;
90
+ const req = new Request(opts, cb);
91
+ req.end();
92
+ return req;
93
+ };
94
+ }
95
+ // define like this to please codeintel/intellisense IDEs
96
+ request.get = fn('get');
97
+ request.head = fn('head');
98
+ request.options = fn('options');
99
+ request.post = fn('post');
100
+ request.put = fn('put');
101
+ request.patch = fn('patch');
102
+ request.del = fn('delete');
103
+ request['delete'] = fn('delete');
104
+ export default request;