jiren 1.0.13 → 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/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/package.json CHANGED
@@ -1,22 +1,21 @@
1
1
  {
2
2
  "name": "jiren",
3
- "version": "1.0.13",
3
+ "version": "1.0.14",
4
4
  "author": "",
5
- "main": "index.ts",
6
- "module": "index.ts",
7
- "types": "index.ts",
5
+ "main": "dist/index.js",
6
+ "module": "dist/index.js",
7
+ "types": "dist/index.d.ts",
8
8
  "devDependencies": {
9
- "@types/bun": "^1.3.4"
9
+ "@types/bun": "^1.3.4",
10
+ "bun-types": "^1.3.4"
10
11
  },
11
12
  "peerDependencies": {
12
13
  "typescript": "^5"
13
14
  },
14
15
  "description": "Jiren is a high-performance HTTP/HTTPS client, Faster than any other HTTP/HTTPS client.",
15
16
  "files": [
16
- "index.ts",
17
- "types",
18
- "lib",
19
- "components"
17
+ "dist",
18
+ "lib"
20
19
  ],
21
20
  "keywords": [
22
21
  "http",
@@ -33,6 +32,9 @@
33
32
  "license": "MIT",
34
33
  "scripts": {
35
34
  "build:zig": "cd .. && zig build --release=fast",
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",
36
38
  "test": "bun run examples/basic.ts"
37
39
  },
38
40
  "engines": {
@@ -1,188 +0,0 @@
1
- import { CString, type Pointer } from "bun:ffi";
2
- import { lib } from "./native";
3
- import type {
4
- RequestOptions,
5
- GetRequestOptions,
6
- PostRequestOptions,
7
- HttpResponse,
8
- } from "../types";
9
-
10
- export class JirenClient {
11
- private ptr: Pointer | null;
12
-
13
- constructor() {
14
- this.ptr = lib.symbols.zclient_new();
15
- if (!this.ptr) throw new Error("Failed to create native client instance");
16
- }
17
-
18
- /**
19
- * Free the native client resources.
20
- * Must be called when the client is no longer needed.
21
- */
22
- public close(): void {
23
- if (this.ptr) {
24
- lib.symbols.zclient_free(this.ptr);
25
- this.ptr = null;
26
- }
27
- }
28
-
29
- /**
30
- * Perform a HTTP request.
31
- * @param url - The URL to request
32
- * @param options - Request options (method, headers, body)
33
- * @returns Response object
34
- */
35
- public request(url: string, options: RequestOptions = {}): HttpResponse {
36
- if (!this.ptr) throw new Error("Client is closed");
37
-
38
- const method = options.method || "GET";
39
- const methodBuffer = Buffer.from(method + "\0");
40
- const urlBuffer = Buffer.from(url + "\0");
41
-
42
- let headersBuffer: Buffer | null = null;
43
- if (options.headers) {
44
- const headerStr = Object.entries(options.headers)
45
- .map(([k, v]) => `${k.toLowerCase()}: ${v}`)
46
- .join("\r\n");
47
- if (headerStr.length > 0) {
48
- headersBuffer = Buffer.from(headerStr + "\0");
49
- }
50
- }
51
-
52
- let bodyBuffer: Buffer | null = null;
53
- if (options.body) {
54
- const bodyStr =
55
- typeof options.body === "string"
56
- ? options.body
57
- : JSON.stringify(options.body);
58
- bodyBuffer = Buffer.from(bodyStr + "\0");
59
- }
60
-
61
- const respPtr = lib.symbols.zclient_request(
62
- this.ptr,
63
- methodBuffer,
64
- urlBuffer,
65
- headersBuffer,
66
- bodyBuffer
67
- );
68
-
69
- const response = this.parseResponse(respPtr);
70
-
71
- // Handle Redirects
72
- if (
73
- options.maxRedirects &&
74
- options.maxRedirects > 0 &&
75
- response.status >= 300 &&
76
- response.status < 400 &&
77
- response.headers &&
78
- response.headers["location"]
79
- ) {
80
- const location = response.headers["location"];
81
- const newUrl = new URL(location, url).toString(); // Resolve relative URLs
82
- const newOptions = { ...options, maxRedirects: options.maxRedirects - 1 };
83
-
84
- return this.request(newUrl, newOptions);
85
- }
86
-
87
- return response;
88
- }
89
-
90
- public get(url: string, options: GetRequestOptions = {}) {
91
- return this.request(url, { ...options, method: "GET" });
92
- }
93
-
94
- public post(
95
- url: string,
96
- body: string | object,
97
- options: PostRequestOptions = {}
98
- ) {
99
- return this.request(url, { ...options, method: "POST", body });
100
- }
101
-
102
- public put(
103
- url: string,
104
- body: string | object,
105
- options: PostRequestOptions = {}
106
- ) {
107
- return this.request(url, { ...options, method: "PUT", body });
108
- }
109
-
110
- public delete(url: string, options: GetRequestOptions = {}) {
111
- return this.request(url, { ...options, method: "DELETE" });
112
- }
113
-
114
- public patch(
115
- url: string,
116
- body: string | object,
117
- options: PostRequestOptions = {}
118
- ) {
119
- return this.request(url, { ...options, method: "PATCH", body });
120
- }
121
-
122
- public head(url: string, options: GetRequestOptions = {}) {
123
- return this.request(url, { ...options, method: "HEAD" });
124
- }
125
-
126
- /**
127
- * Prefetch URLs to warm up connections (resolve DNS & Handshake).
128
- * @param urls - List of URLs to prefetch
129
- */
130
- public prefetch(urls: string[]) {
131
- if (!this.ptr) throw new Error("Client is closed");
132
-
133
- for (const url of urls) {
134
- const urlBuffer = Buffer.from(url + "\0");
135
- lib.symbols.zclient_prefetch(this.ptr, urlBuffer);
136
- }
137
- }
138
-
139
- private parseResponse(respPtr: Pointer | null) {
140
- if (!respPtr)
141
- throw new Error("Native request failed (returned null pointer)");
142
-
143
- try {
144
- const status = lib.symbols.zclient_response_status(respPtr);
145
- const bodyLen = Number(lib.symbols.zclient_response_body_len(respPtr));
146
- const bodyPtr = lib.symbols.zclient_response_body(respPtr);
147
- const headersLen = Number(
148
- lib.symbols.zclient_response_headers_len(respPtr)
149
- );
150
- const headersPtr = lib.symbols.zclient_response_headers(respPtr);
151
-
152
- let bodyString = "";
153
- if (bodyLen > 0 && bodyPtr) {
154
- // CString uses the full length if not null-terminated or we can just read the ptr
155
- // Since we know the length, we can be more precise, but CString assumes null-terminated
156
- // for now we trust it is null terminated from Zig
157
- bodyString = new CString(bodyPtr).toString();
158
- }
159
-
160
- const headers: Record<string, string> = {};
161
- if (headersLen > 0 && headersPtr) {
162
- const headersStr = new CString(headersPtr).toString();
163
- const lines = headersStr.split("\r\n");
164
- for (const line of lines) {
165
- if (!line) continue;
166
- const colonIdx = line.indexOf(":");
167
- if (colonIdx !== -1) {
168
- const key = line.substring(0, colonIdx).trim().toLowerCase();
169
- const val = line.substring(colonIdx + 1).trim();
170
- headers[key] = val;
171
- }
172
- }
173
- }
174
-
175
- return {
176
- status,
177
- body: bodyString,
178
- headers,
179
- json: <T = any>() => {
180
- if (!bodyString) return null as T;
181
- return JSON.parse(bodyString) as T;
182
- },
183
- };
184
- } finally {
185
- lib.symbols.zclient_response_free(respPtr);
186
- }
187
- }
188
- }
@@ -1,13 +0,0 @@
1
- /**
2
- * flous - Ultra-fast HTTP/HTTPS client powered by native Zig
3
- *
4
- * @packageDocumentation
5
- */
6
-
7
- // Main client
8
- export { JirenClient } from "./client";
9
-
10
- // Types
11
- export type { JirenHttpConfig, ParsedUrl } from "../types/index";
12
-
13
- // Remove broken exports
@@ -1,64 +0,0 @@
1
- import { dlopen, FFIType, suffix } from "bun:ffi";
2
- import { join } from "path";
3
-
4
- // Resolve library path relative to this module
5
- const libPath = join(import.meta.dir, `../lib/libhttpclient.${suffix}`);
6
-
7
- export const ffiDef = {
8
- zclient_new: {
9
- args: [],
10
- returns: FFIType.ptr,
11
- },
12
- zclient_free: {
13
- args: [FFIType.ptr],
14
- returns: FFIType.void,
15
- },
16
- zclient_get: {
17
- args: [FFIType.ptr, FFIType.cstring],
18
- returns: FFIType.ptr,
19
- },
20
- zclient_post: {
21
- args: [FFIType.ptr, FFIType.cstring, FFIType.cstring],
22
- returns: FFIType.ptr,
23
- },
24
- zclient_prefetch: {
25
- args: [FFIType.ptr, FFIType.cstring],
26
- returns: FFIType.void,
27
- },
28
- zclient_response_status: {
29
- args: [FFIType.ptr],
30
- returns: FFIType.u16,
31
- },
32
- zclient_response_body: {
33
- args: [FFIType.ptr],
34
- returns: FFIType.ptr,
35
- },
36
- zclient_response_body_len: {
37
- args: [FFIType.ptr],
38
- returns: FFIType.u64,
39
- },
40
- zclient_response_headers: {
41
- args: [FFIType.ptr],
42
- returns: FFIType.ptr,
43
- },
44
- zclient_response_headers_len: {
45
- args: [FFIType.ptr],
46
- returns: FFIType.u64,
47
- },
48
- zclient_response_free: {
49
- args: [FFIType.ptr],
50
- returns: FFIType.void,
51
- },
52
- zclient_request: {
53
- args: [
54
- FFIType.ptr, // client
55
- FFIType.cstring, // method
56
- FFIType.cstring, // url
57
- FFIType.cstring, // headers (nullable)
58
- FFIType.cstring, // body (nullable)
59
- ],
60
- returns: FFIType.ptr,
61
- },
62
- } as const;
63
-
64
- export const lib = dlopen(libPath, ffiDef);
@@ -1,61 +0,0 @@
1
- /**
2
- * pow - Ultra-fast HTTP/HTTPS client powered by native Zigr than any other HTTP/HTTPS client
3
- */
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
-
13
- /** Result of a batch request operation */
14
- export interface BatchResult {
15
- /** Number of successful requests */
16
- success: number;
17
- /** Total requests attempted */
18
- total: number;
19
- /** Duration in seconds */
20
- duration: number;
21
- /** Requests per second */
22
- requestsPerSecond: number;
23
- }
24
-
25
- /** Options for single HTTP requests */
26
- export interface RequestOptions {
27
- /** HTTP method (default: GET) */
28
- method?: "GET" | "POST" | "PUT" | "DELETE" | "PATCH" | "HEAD";
29
- /** Request headers */
30
- headers?: Record<string, string>;
31
- /** Request body (for POST, PUT, PATCH) */
32
- body?: string | object;
33
- /** Request timeout in milliseconds */
34
- timeout?: number;
35
- }
36
-
37
- /** HTTP Response */
38
- export interface HttpResponse {
39
- /** HTTP status code */
40
- status: number;
41
- /** Response body as string */
42
- body: string;
43
- /** Response headers */
44
- headers?: Record<string, string>;
45
- }
46
-
47
- /** Configuration for JirenHttpClient */
48
- export interface JirenHttpConfig {
49
- /** Default number of threads for batch operations */
50
- defaultThreads?: number;
51
- /** Base URL prefix for all requests */
52
- baseUrl?: string;
53
- }
54
-
55
- /** Parsed URL components */
56
- export interface ParsedUrl {
57
- protocol: "http" | "https";
58
- host: string;
59
- port: number;
60
- path: string;
61
- }
package/index.ts DELETED
@@ -1,29 +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
-
25
- // Main client
26
- export { JirenClient } from "./components";
27
-
28
- // Types
29
- export * from "./types";
package/types/index.ts DELETED
@@ -1,73 +0,0 @@
1
- /**
2
- * jiren - Ultra-fast HTTP/HTTPS Client Types
3
- * 56% faster than llhttp
4
- */
5
-
6
- /** Options for batch HTTP requests */
7
- export interface BatchOptions {
8
- /** Number of requests to send (default: 1000) */
9
- count?: number;
10
- /** Number of concurrent threads (default: 100) */
11
- threads?: number;
12
- }
13
-
14
- /** Result of a batch request operation */
15
- export interface BatchResult {
16
- /** Number of successful requests */
17
- success: number;
18
- /** Total requests attempted */
19
- total: number;
20
- /** Duration in seconds */
21
- duration: number;
22
- /** Requests per second */
23
- requestsPerSecond: number;
24
- }
25
-
26
- /** Options for single HTTP requests */
27
- /** Options for single HTTP requests */
28
- export interface RequestOptions {
29
- /** HTTP method (default: GET) */
30
- method?: string;
31
- /** Request headers */
32
- headers?: Record<string, string>;
33
- /** Request body (for POST, PUT, PATCH) */
34
- body?: string | object;
35
- /** Request timeout in milliseconds */
36
- timeout?: number;
37
- /** Maximum number of redirects to follow (default: 0) */
38
- maxRedirects?: number;
39
- }
40
-
41
- /** Options for requests without a body (GET, DELETE, HEAD) */
42
- export type GetRequestOptions = Omit<RequestOptions, "method" | "body">;
43
-
44
- /** Options for requests with a body (POST, PUT, PATCH) where body is a separate argument */
45
- export type PostRequestOptions = Omit<RequestOptions, "method" | "body">;
46
-
47
- /** HTTP Response */
48
- export interface HttpResponse {
49
- /** HTTP status code */
50
- status: number;
51
- /** Response body as string */
52
- body: string;
53
- /** Response headers */
54
- headers?: Record<string, string>;
55
- /** Helper to parse JSON body */
56
- json: <T = any>() => T;
57
- }
58
-
59
- /** Configuration for JirenHttpClient */
60
- export interface JirenHttpConfig {
61
- /** Default number of threads for batch operations */
62
- defaultThreads?: number;
63
- /** Base URL prefix for all requests */
64
- baseUrl?: string;
65
- }
66
-
67
- /** Parsed URL components */
68
- export interface ParsedUrl {
69
- protocol: "http" | "https";
70
- host: string;
71
- port: number;
72
- path: string;
73
- }