reqrio 0.1.0-rc1

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/Response.js ADDED
@@ -0,0 +1,53 @@
1
+ class Header {
2
+ constructor(json) {
3
+ if (!json) return
4
+ this.uri = json.uri;
5
+ this.method = json.method;
6
+ this.status = json.status;
7
+ this.agreement = json.agreement;
8
+ this.keys = json.keys
9
+ this.cookies = []
10
+
11
+ if (this.keys["Set-Cookie"]) {
12
+ for (let i = 0; i < this.keys["Set-Cookie"].length; i++) {
13
+ this.cookies.push(this.keys["Set-Cookie"][i])
14
+ }
15
+ delete this.keys["Set-Cookie"]
16
+ }
17
+ if (this.keys["set-cookie"]) {
18
+ for (let i = 0; i < this.keys["set-cookie"].length; i++) {
19
+ this.cookies.push(this.keys["set-cookie"][i])
20
+ }
21
+ delete this.keys["set-cookie"]
22
+ }
23
+
24
+
25
+ }
26
+ }
27
+
28
+
29
+ class Response {
30
+ constructor(bytes) {
31
+ let resp_text = bytes.toString('utf8')
32
+ try {
33
+ let resp_json = JSON.parse(resp_text);
34
+ this.header = new Header(resp_json.header);
35
+ this.body = Buffer.from(resp_json.body, 'hex')
36
+ } catch (e) {
37
+ throw resp_text
38
+ }
39
+ }
40
+
41
+ status_code() {
42
+ return this.header.status
43
+ }
44
+
45
+ text() {
46
+ return this.body.toString('utf8')
47
+ }
48
+ }
49
+
50
+
51
+ module.exports = {
52
+ Response, Header
53
+ }
package/bindings.js ADDED
@@ -0,0 +1,73 @@
1
+ const ffi = require('ffi-napi');
2
+ const ref = require('ref-napi');
3
+
4
+ const voidPtr = ref.refType(ref.types.void);
5
+ const charPtr = ref.types.CString;
6
+ const bytePtr = ref.refType(ref.types.char)
7
+
8
+ const Method = {
9
+ GET: 0,
10
+ POST: 1,
11
+ PUT: 2,
12
+ HEAD: 3,
13
+ DELETE: 4,
14
+ OPTIONS: 5,
15
+ TRACE: 6,
16
+ CONNECT: 7,
17
+ }
18
+
19
+ const library = ffi.Library("./libreqrio.so", {
20
+ ScReq_new: [voidPtr, []],
21
+ ScReq_set_header_json: ['int', [voidPtr, charPtr]],
22
+ ScReq_add_header: ["int", [voidPtr, charPtr, charPtr]],
23
+ ScReq_set_alpn: ["int", [voidPtr, charPtr]],
24
+ ScReq_set_random_fingerprint: ["int", [voidPtr]],
25
+ ScReq_set_fingerprint: ["int", [voidPtr, charPtr]],
26
+ ScReq_set_ja3: ["int", [voidPtr, charPtr]],
27
+ ScReq_set_ja4: ['int', [voidPtr, charPtr]],
28
+ ScReq_set_proxy: ['int', [voidPtr, charPtr]],
29
+ ScReq_set_url: ['int', [voidPtr, charPtr]],
30
+ ScReq_add_param: ['int', [voidPtr, charPtr, charPtr]],
31
+ ScReq_set_data: ['int', [voidPtr, charPtr]],
32
+ ScReq_set_json: ['int', [voidPtr, charPtr]],
33
+ ScReq_set_bytes: ['int', [voidPtr, bytePtr, "uint32"]],
34
+ ScReq_set_text: ["int", [voidPtr, charPtr]],
35
+ ScReq_set_timeout: ["int", [voidPtr, charPtr]],
36
+ ScReq_set_cookie: ['int', [voidPtr, charPtr]],
37
+ ScReq_add_cookie: ['int', [voidPtr, charPtr, charPtr]],
38
+ ScReq_set_callback: ["int", [voidPtr, "pointer"]],
39
+ ScReq_reconnect: ['int', [voidPtr]],
40
+ ScReq_stream_io: ["pointer", [voidPtr, "int"]],
41
+ ScReq_drop: ['int', [voidPtr]],
42
+ char_free: ['int', [charPtr]],
43
+ ws_build: [voidPtr, []],
44
+ ws_add_header: ['int', [voidPtr, charPtr, charPtr]],
45
+ ws_set_proxy: ['int', [voidPtr, charPtr]],
46
+ ws_set_url: ['int', [voidPtr, charPtr]],
47
+ ws_set_uri: ['int', [voidPtr, charPtr]],
48
+ ws_open: [voidPtr, [voidPtr]],
49
+ ws_open_raw: [voidPtr, [charPtr, charPtr]],
50
+ ws_read: ['pointer', [voidPtr]],
51
+ ws_write: ['int', [voidPtr, 'int', 'bool', charPtr]],
52
+ ws_close: ['int', [voidPtr]]
53
+
54
+ })
55
+
56
+ function make_ScReq_callback(func) {
57
+ return ffi.Callback("void", ["pointer", "uint32"], function (ptr, len) {
58
+ const buffer = ref.reinterpret(ptr, len);
59
+ const data = Buffer.from(buffer);
60
+ func(data)
61
+ })
62
+ }
63
+
64
+ function read_to_string(ptr) {
65
+ return ref.readCString(ptr, 0)
66
+ }
67
+
68
+ module.exports = {
69
+ library,
70
+ Method,
71
+ make_ScReq_callback,
72
+ read_to_string
73
+ }
package/index.js ADDED
@@ -0,0 +1,10 @@
1
+ const {Session, Response, Method, ALPN} = require('session')
2
+ const {Websocket} = require("ws")
3
+
4
+ module.exports = {
5
+ Session,
6
+ Response,
7
+ Method,
8
+ ALPN,
9
+ Websocket
10
+ }
package/libreqrio.so ADDED
Binary file
package/package.json ADDED
@@ -0,0 +1,25 @@
1
+ {
2
+ "name": "reqrio",
3
+ "version": "0.1.0-rc1",
4
+ "description": "A lightweight, high concurrency HTTP request library",
5
+ "main": "index.js",
6
+ "keywords": [
7
+ "http",
8
+ "lightweight",
9
+ "tls fingerprint"
10
+ ],
11
+ "scripts": {
12
+ "test": "echo \"Error: no test specified\" && exit 1"
13
+ },
14
+ "repository": {
15
+ "type": "git",
16
+ "url": "git+https://github.com/xllgl2017/reqrio.git"
17
+ },
18
+ "license": "Apache-2.0",
19
+ "author": {
20
+ "name": "xllgl2017"
21
+ },
22
+ "dependencies": {
23
+ "ref-napi": "^3.0.3"
24
+ }
25
+ }
package/reqrio.dll ADDED
Binary file
package/session.js ADDED
@@ -0,0 +1,175 @@
1
+ const {library, Method, make_ScReq_callback, read_to_string} = require("bindings");
2
+ const {Response} = require("Response")
3
+
4
+ const ALPN = Object.freeze({
5
+ HTTP10: "http/1.0",
6
+ HTTP11: "http/1.1",
7
+ HTTP20: "h2"
8
+ })
9
+
10
+ const registry = new FinalizationRegistry(req => {
11
+ library.ScReq_drop(req)
12
+ })
13
+
14
+ class Session {
15
+ constructor(alpn, rand_tls) {
16
+ this.req = library.ScReq_new();
17
+ if (alpn) {
18
+ let ret = library.ScReq_set_alpn(this.req, alpn)
19
+ if (ret === -1) throw "set_alpn error"
20
+ }
21
+ if (rand_tls) {
22
+ let ret = library.ScReq_set_random_fingerprint(this.req);
23
+ if (ret === -2) throw "free user, set_random_fingerprint can't be used"
24
+ if (ret === -1) throw "set_random_fingerprint error"
25
+ }
26
+ registry.register(this, this.req)
27
+ }
28
+
29
+ set_fingerprint(fingerprint) {
30
+ let ret = library.ScReq_set_fingerprint(this.req, fingerprint);
31
+ if (ret === -2) throw "free user, set_fingerprint can't be used"
32
+ if (ret === -1) throw "set_fingerprint error"
33
+ }
34
+
35
+ set_ja3(ja3) {
36
+ let ret = library.ScReq_set_ja3(this.req, ja3);
37
+ if (ret === -2) throw "free user, set_ja3 can't be used"
38
+ if (ret === -1) throw "set_ja3 error"
39
+
40
+ }
41
+
42
+ set_ja4(ja4) {
43
+ let ret = library.ScReq_set_ja4(this.req, ja4);
44
+ if (ret === -2) throw "free user, set_ja4 can't be used"
45
+ if (ret === -1) throw "set_ja4 error"
46
+
47
+ }
48
+
49
+ set_header_json(header) {
50
+ let header_str = JSON.stringify(header);
51
+ let ret = library.ScReq_set_header_json(this.req, header_str)
52
+ if (ret === -1) throw "set_header_json error"
53
+ }
54
+
55
+ add_header(name, value) {
56
+ let ret = library.ScReq_add_header(name, value)
57
+ if (ret === -1) throw "add_header error"
58
+ }
59
+
60
+ set_proxy(proxy) {
61
+ let ret = library.ScReq_set_proxy(this.req, proxy);
62
+ if (ret === -1) throw "add_header error"
63
+ }
64
+
65
+
66
+ set_url(url) {
67
+ let ret = library.ScReq_set_url(this.req, url)
68
+ if (ret === -1) throw "set_url error"
69
+ }
70
+
71
+ add_param(name, value) {
72
+ let ret = library.ScReq_add_param(this.req, name, value);
73
+ if (ret === -1) throw "add_param error"
74
+ }
75
+
76
+ set_data(data) {
77
+ let data_str = JSON.stringify(data)
78
+ let ret = library.ScReq_set_data(data_str);
79
+ if (ret === -1) throw "set_data error"
80
+ }
81
+
82
+ set_json(json) {
83
+ let json_str = JSON.stringify(this.req, json);
84
+ let ret = library.ScReq_set_header_json(this.req, json_str);
85
+ if (ret === -1) throw "set_json error"
86
+ }
87
+
88
+ set_bytes(buffer) {
89
+ let ret = library.ScReq_set_bytes(this.req, buffer, buffer.length);
90
+ if (ret === -1) throw "set_bytes error"
91
+ }
92
+
93
+ set_text(text) {
94
+ let ret = library.ScReq_set_text(this.req, text);
95
+ if (ret === -1) throw "set_text error"
96
+ }
97
+
98
+ /*
99
+ Timeout{
100
+ connect:3000,
101
+ read:3000,
102
+ write:3000,
103
+ handle:30000,
104
+ connect_times:3,
105
+ handle_times:3
106
+ }
107
+ */
108
+ set_timeout(timeout) {
109
+ let timeout_str = JSON.stringify(timeout);
110
+ let ret = library.ScReq_set_timeout(this.req, timeout_str);
111
+ if (ret === -1) throw "set_timeout error"
112
+ }
113
+
114
+ set_cookie(cookie) {
115
+ let ret = library.ScReq_set_cookie(this.req, cookie)
116
+ if (ret === -1) throw "set_cookie error"
117
+ }
118
+
119
+ add_cookie(name, value) {
120
+ let ret = library.ScReq_add_cookie(this.req, name, value)
121
+ if (ret === -1) throw "add_cookie error"
122
+ }
123
+
124
+ reconnect() {
125
+ let ret = library.ScReq_reconnect(this.req)
126
+ if (ret === -1) throw "reconnect error"
127
+ }
128
+
129
+ set_callback(func) {
130
+ let callback = make_ScReq_callback(func)
131
+ library.ScReq_set_callback(this.req, callback)
132
+ }
133
+
134
+ send(method) {
135
+ let resp = library.ScReq_stream_io(this.req, Method.GET)
136
+ let buffer = Buffer.from(read_to_string(resp), "hex");
137
+ let response = new Response(buffer);
138
+ response.header.method = method;
139
+ library.char_free(resp)//;这里需要手动释放吗
140
+ return response;
141
+ }
142
+
143
+ get() {
144
+ return this.send(Method.GET)
145
+ }
146
+
147
+ post() {
148
+ return this.send(Method.POST)
149
+ }
150
+
151
+ options() {
152
+ return this.send(Method.OPTIONS)
153
+ }
154
+
155
+ head() {
156
+ return this.send(Method.HEAD)
157
+ }
158
+
159
+ trace() {
160
+ return this.send(Method.TRACE)
161
+ }
162
+
163
+ delete() {
164
+ return this.send(Method.DELETE)
165
+ }
166
+
167
+ close() {
168
+ registry.unregister(this);
169
+ library.ScReq_drop(this.req)
170
+ }
171
+ }
172
+
173
+ module.exports = {
174
+ Session, ALPN, Method, Response
175
+ }
package/test.js ADDED
@@ -0,0 +1,17 @@
1
+ const {Session, ALPN, Websocket} = require('index')
2
+
3
+ let session = new Session(ALPN.HTTP20, false);
4
+ session.set_url("https://m.so.com");
5
+ session.set_callback(function (data) {
6
+ console.log(data.length)
7
+ })
8
+ let resp = session.get();
9
+ console.log(resp.status_code())
10
+ console.log(resp.header)
11
+ session.close();
12
+
13
+
14
+ let ws = new Websocket();
15
+ ws.set_url("https://alive.github.com")
16
+ ws.open()
17
+ ws.read()
package/ws.js ADDED
@@ -0,0 +1,58 @@
1
+ const {library, read_to_string} = require("./bindings");
2
+ const registry = new FinalizationRegistry(ws => {
3
+ library.ws_close(ws)
4
+ })
5
+
6
+ class Websocket {
7
+ constructor() {
8
+ this.build = library.ws_build();
9
+ this.ws = null;
10
+ }
11
+
12
+ add_header(name, value) {
13
+ let ret = library.ws_add_header(this.build, name, value)
14
+ if (ret === -1) throw "add header error"
15
+ }
16
+
17
+ set_proxy(proxy) {
18
+ let ret = library.ws_set_proxy(this.build, proxy);
19
+ if (ret === -1) throw "set proxy error"
20
+ }
21
+
22
+ set_url(url) {
23
+ let ret = library.ws_set_url(this.build, url)
24
+ if (ret === -1) throw "set url error"
25
+ }
26
+
27
+ set_uri(uri) {
28
+ let ret = library.ws_set_uri(this.build, uri)
29
+ if (ret === -1) throw "set uri error"
30
+ }
31
+
32
+ open(url) {
33
+ if (url) this.set_url(url)
34
+ this.ws = library.ws_open(this.build)
35
+ registry.register(this, this.ws)
36
+ }
37
+
38
+ read() {
39
+ let ptr = library.ws_read(this.ws)
40
+ let s = read_to_string(ptr)
41
+ library.char_free(ptr)
42
+ return JSON.parse(s)
43
+ }
44
+
45
+ write(opcode, mask, msg) {
46
+ let ret = library.ws_write(this.ws, opcode, mask, msg)
47
+ if (ret === -1) throw "ws write error"
48
+ }
49
+
50
+ close() {
51
+ registry.unregister(this);
52
+ }
53
+ }
54
+
55
+
56
+ module.exports = {
57
+ Websocket
58
+ }