jiren 1.0.12 → 1.0.14
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/README.md +2 -0
- package/dist/index.js +182 -0
- package/lib/libhttpclient.dylib +0 -0
- package/package.json +12 -21
- package/components/client.d.ts +0 -29
- package/components/client.js +0 -140
- package/components/index.d.ts +0 -7
- package/components/index.js +0 -8
- package/components/native.d.ts +0 -18
- package/components/native.js +0 -132
- package/components/runtime.d.ts +0 -7
- package/components/runtime.js +0 -16
- package/components/types.d.ts +0 -55
- package/components/types.js +0 -4
- package/index.d.ts +0 -25
- package/index.js +0 -27
- package/types/index.d.ts +0 -69
- package/types/index.js +0 -5
package/README.md
CHANGED
package/dist/index.js
ADDED
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
// components/client.ts
|
|
3
|
+
import { CString } from "bun:ffi";
|
|
4
|
+
|
|
5
|
+
// components/native.ts
|
|
6
|
+
import { dlopen, FFIType, suffix } from "bun:ffi";
|
|
7
|
+
import { join } from "path";
|
|
8
|
+
var libPath = join(import.meta.dir, `../lib/libhttpclient.${suffix}`);
|
|
9
|
+
var ffiDef = {
|
|
10
|
+
zclient_new: {
|
|
11
|
+
args: [],
|
|
12
|
+
returns: FFIType.ptr
|
|
13
|
+
},
|
|
14
|
+
zclient_free: {
|
|
15
|
+
args: [FFIType.ptr],
|
|
16
|
+
returns: FFIType.void
|
|
17
|
+
},
|
|
18
|
+
zclient_get: {
|
|
19
|
+
args: [FFIType.ptr, FFIType.cstring],
|
|
20
|
+
returns: FFIType.ptr
|
|
21
|
+
},
|
|
22
|
+
zclient_post: {
|
|
23
|
+
args: [FFIType.ptr, FFIType.cstring, FFIType.cstring],
|
|
24
|
+
returns: FFIType.ptr
|
|
25
|
+
},
|
|
26
|
+
zclient_prefetch: {
|
|
27
|
+
args: [FFIType.ptr, FFIType.cstring],
|
|
28
|
+
returns: FFIType.void
|
|
29
|
+
},
|
|
30
|
+
zclient_response_status: {
|
|
31
|
+
args: [FFIType.ptr],
|
|
32
|
+
returns: FFIType.u16
|
|
33
|
+
},
|
|
34
|
+
zclient_response_body: {
|
|
35
|
+
args: [FFIType.ptr],
|
|
36
|
+
returns: FFIType.ptr
|
|
37
|
+
},
|
|
38
|
+
zclient_response_body_len: {
|
|
39
|
+
args: [FFIType.ptr],
|
|
40
|
+
returns: FFIType.u64
|
|
41
|
+
},
|
|
42
|
+
zclient_response_headers: {
|
|
43
|
+
args: [FFIType.ptr],
|
|
44
|
+
returns: FFIType.ptr
|
|
45
|
+
},
|
|
46
|
+
zclient_response_headers_len: {
|
|
47
|
+
args: [FFIType.ptr],
|
|
48
|
+
returns: FFIType.u64
|
|
49
|
+
},
|
|
50
|
+
zclient_response_free: {
|
|
51
|
+
args: [FFIType.ptr],
|
|
52
|
+
returns: FFIType.void
|
|
53
|
+
},
|
|
54
|
+
zclient_request: {
|
|
55
|
+
args: [
|
|
56
|
+
FFIType.ptr,
|
|
57
|
+
FFIType.cstring,
|
|
58
|
+
FFIType.cstring,
|
|
59
|
+
FFIType.cstring,
|
|
60
|
+
FFIType.cstring
|
|
61
|
+
],
|
|
62
|
+
returns: FFIType.ptr
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
var lib = dlopen(libPath, ffiDef);
|
|
66
|
+
|
|
67
|
+
// components/client.ts
|
|
68
|
+
class JirenClient {
|
|
69
|
+
ptr;
|
|
70
|
+
constructor() {
|
|
71
|
+
this.ptr = lib.symbols.zclient_new();
|
|
72
|
+
if (!this.ptr)
|
|
73
|
+
throw new Error("Failed to create native client instance");
|
|
74
|
+
}
|
|
75
|
+
close() {
|
|
76
|
+
if (this.ptr) {
|
|
77
|
+
lib.symbols.zclient_free(this.ptr);
|
|
78
|
+
this.ptr = null;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
request(url, options = {}) {
|
|
82
|
+
if (!this.ptr)
|
|
83
|
+
throw new Error("Client is closed");
|
|
84
|
+
const method = options.method || "GET";
|
|
85
|
+
const methodBuffer = Buffer.from(method + "\x00");
|
|
86
|
+
const urlBuffer = Buffer.from(url + "\x00");
|
|
87
|
+
let headersBuffer = null;
|
|
88
|
+
if (options.headers) {
|
|
89
|
+
const headerStr = Object.entries(options.headers).map(([k, v]) => `${k.toLowerCase()}: ${v}`).join(`\r
|
|
90
|
+
`);
|
|
91
|
+
if (headerStr.length > 0) {
|
|
92
|
+
headersBuffer = Buffer.from(headerStr + "\x00");
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
let bodyBuffer = null;
|
|
96
|
+
if (options.body) {
|
|
97
|
+
const bodyStr = typeof options.body === "string" ? options.body : JSON.stringify(options.body);
|
|
98
|
+
bodyBuffer = Buffer.from(bodyStr + "\x00");
|
|
99
|
+
}
|
|
100
|
+
const respPtr = lib.symbols.zclient_request(this.ptr, methodBuffer, urlBuffer, headersBuffer, bodyBuffer);
|
|
101
|
+
const response = this.parseResponse(respPtr);
|
|
102
|
+
if (options.maxRedirects && options.maxRedirects > 0 && response.status >= 300 && response.status < 400 && response.headers && response.headers["location"]) {
|
|
103
|
+
const location = response.headers["location"];
|
|
104
|
+
const newUrl = new URL(location, url).toString();
|
|
105
|
+
const newOptions = { ...options, maxRedirects: options.maxRedirects - 1 };
|
|
106
|
+
return this.request(newUrl, newOptions);
|
|
107
|
+
}
|
|
108
|
+
return response;
|
|
109
|
+
}
|
|
110
|
+
get(url, options = {}) {
|
|
111
|
+
return this.request(url, { ...options, method: "GET" });
|
|
112
|
+
}
|
|
113
|
+
post(url, body, options = {}) {
|
|
114
|
+
return this.request(url, { ...options, method: "POST", body });
|
|
115
|
+
}
|
|
116
|
+
put(url, body, options = {}) {
|
|
117
|
+
return this.request(url, { ...options, method: "PUT", body });
|
|
118
|
+
}
|
|
119
|
+
delete(url, options = {}) {
|
|
120
|
+
return this.request(url, { ...options, method: "DELETE" });
|
|
121
|
+
}
|
|
122
|
+
patch(url, body, options = {}) {
|
|
123
|
+
return this.request(url, { ...options, method: "PATCH", body });
|
|
124
|
+
}
|
|
125
|
+
head(url, options = {}) {
|
|
126
|
+
return this.request(url, { ...options, method: "HEAD" });
|
|
127
|
+
}
|
|
128
|
+
prefetch(urls) {
|
|
129
|
+
if (!this.ptr)
|
|
130
|
+
throw new Error("Client is closed");
|
|
131
|
+
for (const url of urls) {
|
|
132
|
+
const urlBuffer = Buffer.from(url + "\x00");
|
|
133
|
+
lib.symbols.zclient_prefetch(this.ptr, urlBuffer);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
parseResponse(respPtr) {
|
|
137
|
+
if (!respPtr)
|
|
138
|
+
throw new Error("Native request failed (returned null pointer)");
|
|
139
|
+
try {
|
|
140
|
+
const status = lib.symbols.zclient_response_status(respPtr);
|
|
141
|
+
const bodyLen = Number(lib.symbols.zclient_response_body_len(respPtr));
|
|
142
|
+
const bodyPtr = lib.symbols.zclient_response_body(respPtr);
|
|
143
|
+
const headersLen = Number(lib.symbols.zclient_response_headers_len(respPtr));
|
|
144
|
+
const headersPtr = lib.symbols.zclient_response_headers(respPtr);
|
|
145
|
+
let bodyString = "";
|
|
146
|
+
if (bodyLen > 0 && bodyPtr) {
|
|
147
|
+
bodyString = new CString(bodyPtr).toString();
|
|
148
|
+
}
|
|
149
|
+
const headers = {};
|
|
150
|
+
if (headersLen > 0 && headersPtr) {
|
|
151
|
+
const headersStr = new CString(headersPtr).toString();
|
|
152
|
+
const lines = headersStr.split(`\r
|
|
153
|
+
`);
|
|
154
|
+
for (const line of lines) {
|
|
155
|
+
if (!line)
|
|
156
|
+
continue;
|
|
157
|
+
const colonIdx = line.indexOf(":");
|
|
158
|
+
if (colonIdx !== -1) {
|
|
159
|
+
const key = line.substring(0, colonIdx).trim().toLowerCase();
|
|
160
|
+
const val = line.substring(colonIdx + 1).trim();
|
|
161
|
+
headers[key] = val;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
return {
|
|
166
|
+
status,
|
|
167
|
+
body: bodyString,
|
|
168
|
+
headers,
|
|
169
|
+
json: () => {
|
|
170
|
+
if (!bodyString)
|
|
171
|
+
return null;
|
|
172
|
+
return JSON.parse(bodyString);
|
|
173
|
+
}
|
|
174
|
+
};
|
|
175
|
+
} finally {
|
|
176
|
+
lib.symbols.zclient_response_free(respPtr);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
export {
|
|
181
|
+
JirenClient
|
|
182
|
+
};
|
package/lib/libhttpclient.dylib
CHANGED
|
Binary file
|
package/package.json
CHANGED
|
@@ -1,30 +1,21 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "jiren",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.14",
|
|
4
4
|
"author": "",
|
|
5
|
-
"main": "index.js",
|
|
6
|
-
"module": "index.js",
|
|
7
|
-
"types": "index.d.ts",
|
|
8
|
-
"dependencies": {},
|
|
9
|
-
"optionalDependencies": {
|
|
10
|
-
"koffi": "^2.9.0"
|
|
11
|
-
},
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"module": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
12
8
|
"devDependencies": {
|
|
13
|
-
"
|
|
14
|
-
"
|
|
9
|
+
"@types/bun": "^1.3.4",
|
|
10
|
+
"bun-types": "^1.3.4"
|
|
15
11
|
},
|
|
16
12
|
"peerDependencies": {
|
|
17
13
|
"typescript": "^5"
|
|
18
14
|
},
|
|
19
15
|
"description": "Jiren is a high-performance HTTP/HTTPS client, Faster than any other HTTP/HTTPS client.",
|
|
20
16
|
"files": [
|
|
21
|
-
"
|
|
22
|
-
"
|
|
23
|
-
"types/*.js",
|
|
24
|
-
"types/*.d.ts",
|
|
25
|
-
"lib",
|
|
26
|
-
"components/*.js",
|
|
27
|
-
"components/*.d.ts"
|
|
17
|
+
"dist",
|
|
18
|
+
"lib"
|
|
28
19
|
],
|
|
29
20
|
"keywords": [
|
|
30
21
|
"http",
|
|
@@ -40,13 +31,13 @@
|
|
|
40
31
|
],
|
|
41
32
|
"license": "MIT",
|
|
42
33
|
"scripts": {
|
|
43
|
-
"build": "tsc",
|
|
44
34
|
"build:zig": "cd .. && zig build --release=fast",
|
|
45
|
-
"
|
|
46
|
-
"
|
|
35
|
+
"build": "bun build ./index.ts --outdir dist --target bun && bun run types",
|
|
36
|
+
"types": "bun x tsc --declaration --emitDeclarationOnly --outDir dist",
|
|
37
|
+
"prepare": "bun run build",
|
|
38
|
+
"test": "bun run examples/basic.ts"
|
|
47
39
|
},
|
|
48
40
|
"engines": {
|
|
49
|
-
"node": ">=18.0.0",
|
|
50
41
|
"bun": ">=1.1.0"
|
|
51
42
|
},
|
|
52
43
|
"type": "module"
|
package/components/client.d.ts
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import type { RequestOptions, GetRequestOptions, PostRequestOptions, HttpResponse } from "../types";
|
|
2
|
-
export declare class JirenClient {
|
|
3
|
-
private ptr;
|
|
4
|
-
constructor();
|
|
5
|
-
/**
|
|
6
|
-
* Free the native client resources.
|
|
7
|
-
* Must be called when the client is no longer needed.
|
|
8
|
-
*/
|
|
9
|
-
close(): void;
|
|
10
|
-
/**
|
|
11
|
-
* Perform a HTTP request.
|
|
12
|
-
* @param url - The URL to request
|
|
13
|
-
* @param options - Request options (method, headers, body)
|
|
14
|
-
* @returns Response object
|
|
15
|
-
*/
|
|
16
|
-
request(url: string, options?: RequestOptions): HttpResponse;
|
|
17
|
-
get(url: string, options?: GetRequestOptions): HttpResponse;
|
|
18
|
-
post(url: string, body: string | object, options?: PostRequestOptions): HttpResponse;
|
|
19
|
-
put(url: string, body: string | object, options?: PostRequestOptions): HttpResponse;
|
|
20
|
-
delete(url: string, options?: GetRequestOptions): HttpResponse;
|
|
21
|
-
patch(url: string, body: string | object, options?: PostRequestOptions): HttpResponse;
|
|
22
|
-
head(url: string, options?: GetRequestOptions): HttpResponse;
|
|
23
|
-
/**
|
|
24
|
-
* Prefetch URLs to warm up connections (resolve DNS & Handshake).
|
|
25
|
-
* @param urls - List of URLs to prefetch
|
|
26
|
-
*/
|
|
27
|
-
prefetch(urls: string[]): void;
|
|
28
|
-
private parseResponse;
|
|
29
|
-
}
|
package/components/client.js
DELETED
|
@@ -1,140 +0,0 @@
|
|
|
1
|
-
import { lib, readCString } from "./native";
|
|
2
|
-
export class JirenClient {
|
|
3
|
-
ptr;
|
|
4
|
-
constructor() {
|
|
5
|
-
this.ptr = lib.symbols.zclient_new();
|
|
6
|
-
if (!this.ptr)
|
|
7
|
-
throw new Error("Failed to create native client instance");
|
|
8
|
-
}
|
|
9
|
-
/**
|
|
10
|
-
* Free the native client resources.
|
|
11
|
-
* Must be called when the client is no longer needed.
|
|
12
|
-
*/
|
|
13
|
-
close() {
|
|
14
|
-
if (this.ptr) {
|
|
15
|
-
lib.symbols.zclient_free(this.ptr);
|
|
16
|
-
this.ptr = null;
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
/**
|
|
20
|
-
* Perform a HTTP request.
|
|
21
|
-
* @param url - The URL to request
|
|
22
|
-
* @param options - Request options (method, headers, body)
|
|
23
|
-
* @returns Response object
|
|
24
|
-
*/
|
|
25
|
-
request(url, options = {}) {
|
|
26
|
-
if (!this.ptr)
|
|
27
|
-
throw new Error("Client is closed");
|
|
28
|
-
const method = options.method || "GET";
|
|
29
|
-
const methodBuffer = Buffer.from(method + "\0");
|
|
30
|
-
const urlBuffer = Buffer.from(url + "\0");
|
|
31
|
-
let headersBuffer = null;
|
|
32
|
-
if (options.headers) {
|
|
33
|
-
const headerStr = Object.entries(options.headers)
|
|
34
|
-
.map(([k, v]) => `${k.toLowerCase()}: ${v}`)
|
|
35
|
-
.join("\r\n");
|
|
36
|
-
if (headerStr.length > 0) {
|
|
37
|
-
headersBuffer = Buffer.from(headerStr + "\0");
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
let bodyBuffer = null;
|
|
41
|
-
if (options.body) {
|
|
42
|
-
const bodyStr = typeof options.body === "string"
|
|
43
|
-
? options.body
|
|
44
|
-
: JSON.stringify(options.body);
|
|
45
|
-
bodyBuffer = Buffer.from(bodyStr + "\0");
|
|
46
|
-
}
|
|
47
|
-
const respPtr = lib.symbols.zclient_request(this.ptr, methodBuffer, urlBuffer, headersBuffer, bodyBuffer);
|
|
48
|
-
const response = this.parseResponse(respPtr);
|
|
49
|
-
// Handle Redirects
|
|
50
|
-
if (options.maxRedirects &&
|
|
51
|
-
options.maxRedirects > 0 &&
|
|
52
|
-
response.status >= 300 &&
|
|
53
|
-
response.status < 400 &&
|
|
54
|
-
response.headers &&
|
|
55
|
-
response.headers["location"]) {
|
|
56
|
-
const location = response.headers["location"];
|
|
57
|
-
const newUrl = new URL(location, url).toString(); // Resolve relative URLs
|
|
58
|
-
const newOptions = { ...options, maxRedirects: options.maxRedirects - 1 };
|
|
59
|
-
return this.request(newUrl, newOptions);
|
|
60
|
-
}
|
|
61
|
-
return response;
|
|
62
|
-
}
|
|
63
|
-
get(url, options = {}) {
|
|
64
|
-
return this.request(url, { ...options, method: "GET" });
|
|
65
|
-
}
|
|
66
|
-
post(url, body, options = {}) {
|
|
67
|
-
return this.request(url, { ...options, method: "POST", body });
|
|
68
|
-
}
|
|
69
|
-
put(url, body, options = {}) {
|
|
70
|
-
return this.request(url, { ...options, method: "PUT", body });
|
|
71
|
-
}
|
|
72
|
-
delete(url, options = {}) {
|
|
73
|
-
return this.request(url, { ...options, method: "DELETE" });
|
|
74
|
-
}
|
|
75
|
-
patch(url, body, options = {}) {
|
|
76
|
-
return this.request(url, { ...options, method: "PATCH", body });
|
|
77
|
-
}
|
|
78
|
-
head(url, options = {}) {
|
|
79
|
-
return this.request(url, { ...options, method: "HEAD" });
|
|
80
|
-
}
|
|
81
|
-
/**
|
|
82
|
-
* Prefetch URLs to warm up connections (resolve DNS & Handshake).
|
|
83
|
-
* @param urls - List of URLs to prefetch
|
|
84
|
-
*/
|
|
85
|
-
prefetch(urls) {
|
|
86
|
-
if (!this.ptr)
|
|
87
|
-
throw new Error("Client is closed");
|
|
88
|
-
for (const url of urls) {
|
|
89
|
-
const urlBuffer = Buffer.from(url + "\0");
|
|
90
|
-
lib.symbols.zclient_prefetch(this.ptr, urlBuffer);
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
parseResponse(respPtr) {
|
|
94
|
-
if (!respPtr)
|
|
95
|
-
throw new Error("Native request failed (returned null pointer)");
|
|
96
|
-
try {
|
|
97
|
-
const status = lib.symbols.zclient_response_status(respPtr);
|
|
98
|
-
const bodyLen = Number(lib.symbols.zclient_response_body_len(respPtr));
|
|
99
|
-
const bodyPtr = lib.symbols.zclient_response_body(respPtr);
|
|
100
|
-
const headersLen = Number(lib.symbols.zclient_response_headers_len(respPtr));
|
|
101
|
-
const headersPtr = lib.symbols.zclient_response_headers(respPtr);
|
|
102
|
-
let bodyString = "";
|
|
103
|
-
if (bodyLen > 0 && bodyPtr) {
|
|
104
|
-
bodyString = readCString(bodyPtr);
|
|
105
|
-
}
|
|
106
|
-
const headers = {};
|
|
107
|
-
if (headersLen > 0 && headersPtr) {
|
|
108
|
-
const headersStr = readCString(headersPtr);
|
|
109
|
-
const lines = headersStr.split("\r\n");
|
|
110
|
-
for (const line of lines) {
|
|
111
|
-
if (!line)
|
|
112
|
-
continue;
|
|
113
|
-
const colonIdx = line.indexOf(":");
|
|
114
|
-
if (colonIdx !== -1) {
|
|
115
|
-
const key = line.substring(0, colonIdx).trim().toLowerCase();
|
|
116
|
-
const val = line.substring(colonIdx + 1).trim();
|
|
117
|
-
headers[key] = val;
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
return {
|
|
122
|
-
status,
|
|
123
|
-
body: bodyString,
|
|
124
|
-
headers,
|
|
125
|
-
get ok() {
|
|
126
|
-
return status >= 200 && status < 300;
|
|
127
|
-
},
|
|
128
|
-
text: async () => bodyString,
|
|
129
|
-
json: () => {
|
|
130
|
-
if (!bodyString)
|
|
131
|
-
return null;
|
|
132
|
-
return JSON.parse(bodyString);
|
|
133
|
-
},
|
|
134
|
-
};
|
|
135
|
-
}
|
|
136
|
-
finally {
|
|
137
|
-
lib.symbols.zclient_response_free(respPtr);
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
}
|
package/components/index.d.ts
DELETED
package/components/index.js
DELETED
package/components/native.d.ts
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
export type NativeLib = {
|
|
2
|
-
symbols: {
|
|
3
|
-
zclient_new: () => any;
|
|
4
|
-
zclient_free: (ptr: any) => void;
|
|
5
|
-
zclient_get: (client: any, url: any) => any;
|
|
6
|
-
zclient_post: (client: any, url: any, body: any) => any;
|
|
7
|
-
zclient_prefetch: (client: any, url: any) => void;
|
|
8
|
-
zclient_response_status: (resp: any) => number;
|
|
9
|
-
zclient_response_body: (resp: any) => any;
|
|
10
|
-
zclient_response_body_len: (resp: any) => bigint | number;
|
|
11
|
-
zclient_response_headers: (resp: any) => any;
|
|
12
|
-
zclient_response_headers_len: (resp: any) => bigint | number;
|
|
13
|
-
zclient_response_free: (resp: any) => void;
|
|
14
|
-
zclient_request: (client: any, method: any, url: any, headers: any, body: any) => any;
|
|
15
|
-
};
|
|
16
|
-
};
|
|
17
|
-
export declare const lib: NativeLib;
|
|
18
|
-
export declare function readCString(ptr: any): string;
|
package/components/native.js
DELETED
|
@@ -1,132 +0,0 @@
|
|
|
1
|
-
import { isBun } from "./runtime";
|
|
2
|
-
import { join, dirname } from "path";
|
|
3
|
-
import { fileURLToPath } from "url";
|
|
4
|
-
// Helper to get library path
|
|
5
|
-
function getLibPath() {
|
|
6
|
-
const platform = process.platform;
|
|
7
|
-
const ext = platform === "darwin" ? "dylib" : platform === "win32" ? "dll" : "so";
|
|
8
|
-
if (isBun) {
|
|
9
|
-
// @ts-ignore - Bun-specific
|
|
10
|
-
return join(import.meta.dir, `../lib/libhttpclient.${ext}`);
|
|
11
|
-
}
|
|
12
|
-
else {
|
|
13
|
-
// Node.js
|
|
14
|
-
const __filename = fileURLToPath(import.meta.url);
|
|
15
|
-
const __dirname = dirname(__filename);
|
|
16
|
-
return join(__dirname, `../lib/libhttpclient.${ext}`);
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
// Load the appropriate FFI library
|
|
20
|
-
async function loadLibrary() {
|
|
21
|
-
const libPath = getLibPath();
|
|
22
|
-
if (isBun) {
|
|
23
|
-
// Use Bun FFI
|
|
24
|
-
const { dlopen, FFIType } = await import("bun:ffi");
|
|
25
|
-
const ffiDef = {
|
|
26
|
-
zclient_new: { args: [], returns: FFIType.ptr },
|
|
27
|
-
zclient_free: { args: [FFIType.ptr], returns: FFIType.void },
|
|
28
|
-
zclient_get: {
|
|
29
|
-
args: [FFIType.ptr, FFIType.cstring],
|
|
30
|
-
returns: FFIType.ptr,
|
|
31
|
-
},
|
|
32
|
-
zclient_post: {
|
|
33
|
-
args: [FFIType.ptr, FFIType.cstring, FFIType.cstring],
|
|
34
|
-
returns: FFIType.ptr,
|
|
35
|
-
},
|
|
36
|
-
zclient_prefetch: {
|
|
37
|
-
args: [FFIType.ptr, FFIType.cstring],
|
|
38
|
-
returns: FFIType.void,
|
|
39
|
-
},
|
|
40
|
-
zclient_response_status: { args: [FFIType.ptr], returns: FFIType.u16 },
|
|
41
|
-
zclient_response_body: { args: [FFIType.ptr], returns: FFIType.ptr },
|
|
42
|
-
zclient_response_body_len: { args: [FFIType.ptr], returns: FFIType.u64 },
|
|
43
|
-
zclient_response_headers: { args: [FFIType.ptr], returns: FFIType.ptr },
|
|
44
|
-
zclient_response_headers_len: {
|
|
45
|
-
args: [FFIType.ptr],
|
|
46
|
-
returns: FFIType.u64,
|
|
47
|
-
},
|
|
48
|
-
zclient_response_free: { args: [FFIType.ptr], returns: FFIType.void },
|
|
49
|
-
zclient_request: {
|
|
50
|
-
args: [
|
|
51
|
-
FFIType.ptr,
|
|
52
|
-
FFIType.cstring,
|
|
53
|
-
FFIType.cstring,
|
|
54
|
-
FFIType.cstring,
|
|
55
|
-
FFIType.cstring,
|
|
56
|
-
],
|
|
57
|
-
returns: FFIType.ptr,
|
|
58
|
-
},
|
|
59
|
-
};
|
|
60
|
-
return dlopen(libPath, ffiDef);
|
|
61
|
-
}
|
|
62
|
-
else {
|
|
63
|
-
// Use koffi for Node.js
|
|
64
|
-
const koffi = await import("koffi");
|
|
65
|
-
const nativeLib = koffi.load(libPath);
|
|
66
|
-
return {
|
|
67
|
-
symbols: {
|
|
68
|
-
zclient_new: nativeLib.func("zclient_new", "void*", []),
|
|
69
|
-
zclient_free: nativeLib.func("zclient_free", "void", ["void*"]),
|
|
70
|
-
zclient_get: nativeLib.func("zclient_get", "void*", ["void*", "str"]),
|
|
71
|
-
zclient_post: nativeLib.func("zclient_post", "void*", [
|
|
72
|
-
"void*",
|
|
73
|
-
"str",
|
|
74
|
-
"str",
|
|
75
|
-
]),
|
|
76
|
-
zclient_prefetch: nativeLib.func("zclient_prefetch", "void", [
|
|
77
|
-
"void*",
|
|
78
|
-
"str",
|
|
79
|
-
]),
|
|
80
|
-
zclient_response_status: nativeLib.func("zclient_response_status", "uint16", ["void*"]),
|
|
81
|
-
zclient_response_body: nativeLib.func("zclient_response_body", "void*", ["void*"]),
|
|
82
|
-
zclient_response_body_len: nativeLib.func("zclient_response_body_len", "uint64", ["void*"]),
|
|
83
|
-
zclient_response_headers: nativeLib.func("zclient_response_headers", "void*", ["void*"]),
|
|
84
|
-
zclient_response_headers_len: nativeLib.func("zclient_response_headers_len", "uint64", ["void*"]),
|
|
85
|
-
zclient_response_free: nativeLib.func("zclient_response_free", "void", [
|
|
86
|
-
"void*",
|
|
87
|
-
]),
|
|
88
|
-
zclient_request: nativeLib.func("zclient_request", "void*", [
|
|
89
|
-
"void*",
|
|
90
|
-
"str",
|
|
91
|
-
"str",
|
|
92
|
-
"str",
|
|
93
|
-
"str",
|
|
94
|
-
]),
|
|
95
|
-
},
|
|
96
|
-
};
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
// Export the loaded library (initialized on first import)
|
|
100
|
-
export const lib = await loadLibrary();
|
|
101
|
-
// Helper to read C string from pointer (runtime-agnostic)
|
|
102
|
-
export function readCString(ptr) {
|
|
103
|
-
if (!ptr)
|
|
104
|
-
return "";
|
|
105
|
-
if (isBun) {
|
|
106
|
-
// For Bun, we need to dynamically get CString
|
|
107
|
-
// Use a simple approach: read bytes until null terminator
|
|
108
|
-
const view = new DataView(new ArrayBuffer(0));
|
|
109
|
-
try {
|
|
110
|
-
// In Bun, pointers can be read as buffers
|
|
111
|
-
// @ts-ignore - Bun specific
|
|
112
|
-
const buf = Buffer.from(ptr);
|
|
113
|
-
const nullIdx = buf.indexOf(0);
|
|
114
|
-
return buf.toString("utf8", 0, nullIdx >= 0 ? nullIdx : undefined);
|
|
115
|
-
}
|
|
116
|
-
catch {
|
|
117
|
-
// Fallback: try to read as string directly
|
|
118
|
-
return String(ptr);
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
else {
|
|
122
|
-
// Node.js with koffi
|
|
123
|
-
try {
|
|
124
|
-
const koffi = require("koffi");
|
|
125
|
-
return koffi.decode(ptr, "string");
|
|
126
|
-
}
|
|
127
|
-
catch (e) {
|
|
128
|
-
console.error("Failed to decode C string:", e);
|
|
129
|
-
return "";
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
}
|
package/components/runtime.d.ts
DELETED
package/components/runtime.js
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Runtime detection utility for Jiren
|
|
3
|
-
* Detects whether we're running in Bun or Node.js
|
|
4
|
-
*/
|
|
5
|
-
export const isBun = typeof Bun !== "undefined";
|
|
6
|
-
export const isNode = !isBun &&
|
|
7
|
-
typeof process !== "undefined" &&
|
|
8
|
-
process.versions &&
|
|
9
|
-
process.versions.node;
|
|
10
|
-
export function getRuntime() {
|
|
11
|
-
if (isBun)
|
|
12
|
-
return "bun";
|
|
13
|
-
if (isNode)
|
|
14
|
-
return "node";
|
|
15
|
-
return "unknown";
|
|
16
|
-
}
|
package/components/types.d.ts
DELETED
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* pow - Ultra-fast HTTP/HTTPS client powered by native Zigr than any other HTTP/HTTPS client
|
|
3
|
-
*/
|
|
4
|
-
/** Options for batch HTTP requests */
|
|
5
|
-
export interface BatchOptions {
|
|
6
|
-
/** Number of requests to send (default: 1000) */
|
|
7
|
-
count?: number;
|
|
8
|
-
/** Number of concurrent threads (default: 100) */
|
|
9
|
-
threads?: number;
|
|
10
|
-
}
|
|
11
|
-
/** Result of a batch request operation */
|
|
12
|
-
export interface BatchResult {
|
|
13
|
-
/** Number of successful requests */
|
|
14
|
-
success: number;
|
|
15
|
-
/** Total requests attempted */
|
|
16
|
-
total: number;
|
|
17
|
-
/** Duration in seconds */
|
|
18
|
-
duration: number;
|
|
19
|
-
/** Requests per second */
|
|
20
|
-
requestsPerSecond: number;
|
|
21
|
-
}
|
|
22
|
-
/** Options for single HTTP requests */
|
|
23
|
-
export interface RequestOptions {
|
|
24
|
-
/** HTTP method (default: GET) */
|
|
25
|
-
method?: "GET" | "POST" | "PUT" | "DELETE" | "PATCH" | "HEAD";
|
|
26
|
-
/** Request headers */
|
|
27
|
-
headers?: Record<string, string>;
|
|
28
|
-
/** Request body (for POST, PUT, PATCH) */
|
|
29
|
-
body?: string | object;
|
|
30
|
-
/** Request timeout in milliseconds */
|
|
31
|
-
timeout?: number;
|
|
32
|
-
}
|
|
33
|
-
/** HTTP Response */
|
|
34
|
-
export interface HttpResponse {
|
|
35
|
-
/** HTTP status code */
|
|
36
|
-
status: number;
|
|
37
|
-
/** Response body as string */
|
|
38
|
-
body: string;
|
|
39
|
-
/** Response headers */
|
|
40
|
-
headers?: Record<string, string>;
|
|
41
|
-
}
|
|
42
|
-
/** Configuration for JirenHttpClient */
|
|
43
|
-
export interface JirenHttpConfig {
|
|
44
|
-
/** Default number of threads for batch operations */
|
|
45
|
-
defaultThreads?: number;
|
|
46
|
-
/** Base URL prefix for all requests */
|
|
47
|
-
baseUrl?: string;
|
|
48
|
-
}
|
|
49
|
-
/** Parsed URL components */
|
|
50
|
-
export interface ParsedUrl {
|
|
51
|
-
protocol: "http" | "https";
|
|
52
|
-
host: string;
|
|
53
|
-
port: number;
|
|
54
|
-
path: string;
|
|
55
|
-
}
|
package/components/types.js
DELETED
package/index.d.ts
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* jiren - Ultra-fast HTTP/HTTPS client powered by native Zig
|
|
3
|
-
*
|
|
4
|
-
* Faster than any other HTTP/HTTPS client | 1.5M+ requests/sec
|
|
5
|
-
*
|
|
6
|
-
* @example
|
|
7
|
-
* ```typescript
|
|
8
|
-
* import { JirenClient } from 'jiren';
|
|
9
|
-
*
|
|
10
|
-
* const client = new JirenClient();
|
|
11
|
-
*
|
|
12
|
-
* // High-performance batch requests
|
|
13
|
-
* const result = client.batch('http://localhost:3000/', {
|
|
14
|
-
* count: 10000,
|
|
15
|
-
* threads: 100
|
|
16
|
-
* });
|
|
17
|
-
*
|
|
18
|
-
* console.log(`Success: ${result.success}/${result.total}`);
|
|
19
|
-
* console.log(`Speed: ${result.requestsPerSecond.toFixed(0)} req/sec`);
|
|
20
|
-
* ```
|
|
21
|
-
*
|
|
22
|
-
* @packageDocumentation
|
|
23
|
-
*/
|
|
24
|
-
export { JirenClient } from "./components";
|
|
25
|
-
export * from "./types";
|
package/index.js
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* jiren - Ultra-fast HTTP/HTTPS client powered by native Zig
|
|
3
|
-
*
|
|
4
|
-
* Faster than any other HTTP/HTTPS client | 1.5M+ requests/sec
|
|
5
|
-
*
|
|
6
|
-
* @example
|
|
7
|
-
* ```typescript
|
|
8
|
-
* import { JirenClient } from 'jiren';
|
|
9
|
-
*
|
|
10
|
-
* const client = new JirenClient();
|
|
11
|
-
*
|
|
12
|
-
* // High-performance batch requests
|
|
13
|
-
* const result = client.batch('http://localhost:3000/', {
|
|
14
|
-
* count: 10000,
|
|
15
|
-
* threads: 100
|
|
16
|
-
* });
|
|
17
|
-
*
|
|
18
|
-
* console.log(`Success: ${result.success}/${result.total}`);
|
|
19
|
-
* console.log(`Speed: ${result.requestsPerSecond.toFixed(0)} req/sec`);
|
|
20
|
-
* ```
|
|
21
|
-
*
|
|
22
|
-
* @packageDocumentation
|
|
23
|
-
*/
|
|
24
|
-
// Main client
|
|
25
|
-
export { JirenClient } from "./components";
|
|
26
|
-
// Types
|
|
27
|
-
export * from "./types";
|
package/types/index.d.ts
DELETED
|
@@ -1,69 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* jiren - Ultra-fast HTTP/HTTPS Client Types
|
|
3
|
-
* 56% faster than llhttp
|
|
4
|
-
*/
|
|
5
|
-
/** Options for batch HTTP requests */
|
|
6
|
-
export interface BatchOptions {
|
|
7
|
-
/** Number of requests to send (default: 1000) */
|
|
8
|
-
count?: number;
|
|
9
|
-
/** Number of concurrent threads (default: 100) */
|
|
10
|
-
threads?: number;
|
|
11
|
-
}
|
|
12
|
-
/** Result of a batch request operation */
|
|
13
|
-
export interface BatchResult {
|
|
14
|
-
/** Number of successful requests */
|
|
15
|
-
success: number;
|
|
16
|
-
/** Total requests attempted */
|
|
17
|
-
total: number;
|
|
18
|
-
/** Duration in seconds */
|
|
19
|
-
duration: number;
|
|
20
|
-
/** Requests per second */
|
|
21
|
-
requestsPerSecond: number;
|
|
22
|
-
}
|
|
23
|
-
/** Options for single HTTP requests */
|
|
24
|
-
/** Options for single HTTP requests */
|
|
25
|
-
export interface RequestOptions {
|
|
26
|
-
/** HTTP method (default: GET) */
|
|
27
|
-
method?: string;
|
|
28
|
-
/** Request headers */
|
|
29
|
-
headers?: Record<string, string>;
|
|
30
|
-
/** Request body (for POST, PUT, PATCH) */
|
|
31
|
-
body?: string | object;
|
|
32
|
-
/** Request timeout in milliseconds */
|
|
33
|
-
timeout?: number;
|
|
34
|
-
/** Maximum number of redirects to follow (default: 0) */
|
|
35
|
-
maxRedirects?: number;
|
|
36
|
-
}
|
|
37
|
-
/** Options for requests without a body (GET, DELETE, HEAD) */
|
|
38
|
-
export type GetRequestOptions = Omit<RequestOptions, "method" | "body">;
|
|
39
|
-
/** Options for requests with a body (POST, PUT, PATCH) where body is a separate argument */
|
|
40
|
-
export type PostRequestOptions = Omit<RequestOptions, "method" | "body">;
|
|
41
|
-
/** HTTP Response */
|
|
42
|
-
export interface HttpResponse {
|
|
43
|
-
/** HTTP status code */
|
|
44
|
-
status: number;
|
|
45
|
-
/** Response body as string */
|
|
46
|
-
body: string;
|
|
47
|
-
/** Response headers */
|
|
48
|
-
headers?: Record<string, string>;
|
|
49
|
-
/** True if status is 2xx */
|
|
50
|
-
ok: boolean;
|
|
51
|
-
/** Helper to get body as string (Promise for Fetch compatibility) */
|
|
52
|
-
text: () => Promise<string>;
|
|
53
|
-
/** Helper to parse JSON body */
|
|
54
|
-
json: <T = any>() => T;
|
|
55
|
-
}
|
|
56
|
-
/** Configuration for JirenHttpClient */
|
|
57
|
-
export interface JirenHttpConfig {
|
|
58
|
-
/** Default number of threads for batch operations */
|
|
59
|
-
defaultThreads?: number;
|
|
60
|
-
/** Base URL prefix for all requests */
|
|
61
|
-
baseUrl?: string;
|
|
62
|
-
}
|
|
63
|
-
/** Parsed URL components */
|
|
64
|
-
export interface ParsedUrl {
|
|
65
|
-
protocol: "http" | "https";
|
|
66
|
-
host: string;
|
|
67
|
-
port: number;
|
|
68
|
-
path: string;
|
|
69
|
-
}
|