jiren 1.0.15 → 1.1.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.
@@ -0,0 +1,105 @@
1
+ import type {
2
+ RequestOptions,
3
+ GetRequestOptions,
4
+ PostRequestOptions,
5
+ HttpResponse,
6
+ } from "../types/index.js";
7
+
8
+ /**
9
+ * Node.js fallback client using native fetch
10
+ */
11
+ export class NodeJirenClient {
12
+ constructor() {
13
+ // No initialization needed for Node.js fetch
14
+ }
15
+
16
+ public close(): void {
17
+ // No cleanup needed
18
+ }
19
+
20
+ public async request(
21
+ url: string,
22
+ options: RequestOptions = {}
23
+ ): Promise<HttpResponse> {
24
+ const method = options.method || "GET";
25
+ const headers = options.headers || {};
26
+
27
+ const fetchOptions: RequestInit = {
28
+ method,
29
+ headers,
30
+ redirect:
31
+ options.maxRedirects && options.maxRedirects > 0 ? "follow" : "manual",
32
+ };
33
+
34
+ if (options.body) {
35
+ fetchOptions.body =
36
+ typeof options.body === "string"
37
+ ? options.body
38
+ : JSON.stringify(options.body);
39
+ }
40
+
41
+ try {
42
+ const response = await fetch(url, fetchOptions);
43
+ const body = await response.text();
44
+
45
+ // Convert Headers to plain object
46
+ const responseHeaders: Record<string, string> = {};
47
+ response.headers.forEach((value, key) => {
48
+ responseHeaders[key.toLowerCase()] = value;
49
+ });
50
+
51
+ return {
52
+ status: response.status,
53
+ body,
54
+ headers: responseHeaders,
55
+ json: <T = any>() => {
56
+ if (!body) return null as T;
57
+ return JSON.parse(body) as T;
58
+ },
59
+ };
60
+ } catch (error) {
61
+ throw new Error(`HTTP request failed: ${error}`);
62
+ }
63
+ }
64
+
65
+ public async get(url: string, options: GetRequestOptions = {}) {
66
+ return this.request(url, { ...options, method: "GET" });
67
+ }
68
+
69
+ public async post(
70
+ url: string,
71
+ body: string | object,
72
+ options: PostRequestOptions = {}
73
+ ) {
74
+ return this.request(url, { ...options, method: "POST", body });
75
+ }
76
+
77
+ public async put(
78
+ url: string,
79
+ body: string | object,
80
+ options: PostRequestOptions = {}
81
+ ) {
82
+ return this.request(url, { ...options, method: "PUT", body });
83
+ }
84
+
85
+ public async delete(url: string, options: GetRequestOptions = {}) {
86
+ return this.request(url, { ...options, method: "DELETE" });
87
+ }
88
+
89
+ public async patch(
90
+ url: string,
91
+ body: string | object,
92
+ options: PostRequestOptions = {}
93
+ ) {
94
+ return this.request(url, { ...options, method: "PATCH", body });
95
+ }
96
+
97
+ public async head(url: string, options: GetRequestOptions = {}) {
98
+ return this.request(url, { ...options, method: "HEAD" });
99
+ }
100
+
101
+ public prefetch(urls: string[]) {
102
+ // No-op in Node.js
103
+ // Could implement DNS prefetch here if needed
104
+ }
105
+ }
@@ -1,13 +1,13 @@
1
- /**
2
- * flous - Ultra-fast HTTP/HTTPS client powered by native Zig
3
- *
4
- * @packageDocumentation
5
- */
1
+ import { isBun } from "./runtime.js";
6
2
 
7
- // Main client
8
- export { JirenClient } from "./client";
3
+ // Conditionally export the right client
4
+ export const JirenClient = isBun
5
+ ? (await import("./client-bun.js")).JirenClient
6
+ : (await import("./client-node.js")).NodeJirenClient;
9
7
 
10
- // Types
11
- export type { JirenHttpConfig, ParsedUrl } from "../types/index";
12
-
13
- // Remove broken exports
8
+ export type {
9
+ HttpResponse,
10
+ RequestOptions,
11
+ GetRequestOptions,
12
+ PostRequestOptions,
13
+ } from "../types/index.js";
@@ -0,0 +1,11 @@
1
+ // Runtime detection
2
+ export const isBun = typeof Bun !== "undefined";
3
+ export const isNode = !isBun && typeof process !== "undefined";
4
+
5
+ // Re-export types
6
+ export type {
7
+ HttpResponse,
8
+ RequestOptions,
9
+ GetRequestOptions,
10
+ PostRequestOptions,
11
+ } from "../types/index.js";
package/dist/index.js CHANGED
@@ -1,70 +1,88 @@
1
1
  // @bun
2
- // components/client.ts
3
- import { CString } from "bun:ffi";
2
+ var __defProp = Object.defineProperty;
3
+ var __export = (target, all) => {
4
+ for (var name in all)
5
+ __defProp(target, name, {
6
+ get: all[name],
7
+ enumerable: true,
8
+ configurable: true,
9
+ set: (newValue) => all[name] = () => newValue
10
+ });
11
+ };
12
+ var __esm = (fn, res) => () => (fn && (res = fn(fn = 0)), res);
4
13
 
5
14
  // components/native.ts
6
15
  import { dlopen, FFIType, suffix } from "bun:ffi";
7
16
  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);
17
+ var libPath, ffiDef, lib;
18
+ var init_native = __esm(() => {
19
+ libPath = join(import.meta.dir, `../lib/libhttpclient.${suffix}`);
20
+ ffiDef = {
21
+ zclient_new: {
22
+ args: [],
23
+ returns: FFIType.ptr
24
+ },
25
+ zclient_free: {
26
+ args: [FFIType.ptr],
27
+ returns: FFIType.void
28
+ },
29
+ zclient_get: {
30
+ args: [FFIType.ptr, FFIType.cstring],
31
+ returns: FFIType.ptr
32
+ },
33
+ zclient_post: {
34
+ args: [FFIType.ptr, FFIType.cstring, FFIType.cstring],
35
+ returns: FFIType.ptr
36
+ },
37
+ zclient_prefetch: {
38
+ args: [FFIType.ptr, FFIType.cstring],
39
+ returns: FFIType.void
40
+ },
41
+ zclient_response_status: {
42
+ args: [FFIType.ptr],
43
+ returns: FFIType.u16
44
+ },
45
+ zclient_response_body: {
46
+ args: [FFIType.ptr],
47
+ returns: FFIType.ptr
48
+ },
49
+ zclient_response_body_len: {
50
+ args: [FFIType.ptr],
51
+ returns: FFIType.u64
52
+ },
53
+ zclient_response_headers: {
54
+ args: [FFIType.ptr],
55
+ returns: FFIType.ptr
56
+ },
57
+ zclient_response_headers_len: {
58
+ args: [FFIType.ptr],
59
+ returns: FFIType.u64
60
+ },
61
+ zclient_response_free: {
62
+ args: [FFIType.ptr],
63
+ returns: FFIType.void
64
+ },
65
+ zclient_request: {
66
+ args: [
67
+ FFIType.ptr,
68
+ FFIType.cstring,
69
+ FFIType.cstring,
70
+ FFIType.cstring,
71
+ FFIType.cstring
72
+ ],
73
+ returns: FFIType.ptr
74
+ }
75
+ };
76
+ lib = dlopen(libPath, ffiDef);
77
+ });
78
+
79
+ // components/client-bun.ts
80
+ var exports_client_bun = {};
81
+ __export(exports_client_bun, {
82
+ JirenClient: () => JirenClient
83
+ });
84
+ import { CString } from "bun:ffi";
66
85
 
67
- // components/client.ts
68
86
  class JirenClient {
69
87
  ptr;
70
88
  constructor() {
@@ -177,6 +195,77 @@ class JirenClient {
177
195
  }
178
196
  }
179
197
  }
198
+ var init_client_bun = __esm(() => {
199
+ init_native();
200
+ });
201
+
202
+ // components/client-node.ts
203
+ var exports_client_node = {};
204
+ __export(exports_client_node, {
205
+ NodeJirenClient: () => NodeJirenClient
206
+ });
207
+
208
+ class NodeJirenClient {
209
+ constructor() {}
210
+ close() {}
211
+ async request(url, options = {}) {
212
+ const method = options.method || "GET";
213
+ const headers = options.headers || {};
214
+ const fetchOptions = {
215
+ method,
216
+ headers,
217
+ redirect: options.maxRedirects && options.maxRedirects > 0 ? "follow" : "manual"
218
+ };
219
+ if (options.body) {
220
+ fetchOptions.body = typeof options.body === "string" ? options.body : JSON.stringify(options.body);
221
+ }
222
+ try {
223
+ const response = await fetch(url, fetchOptions);
224
+ const body = await response.text();
225
+ const responseHeaders = {};
226
+ response.headers.forEach((value, key) => {
227
+ responseHeaders[key.toLowerCase()] = value;
228
+ });
229
+ return {
230
+ status: response.status,
231
+ body,
232
+ headers: responseHeaders,
233
+ json: () => {
234
+ if (!body)
235
+ return null;
236
+ return JSON.parse(body);
237
+ }
238
+ };
239
+ } catch (error) {
240
+ throw new Error(`HTTP request failed: ${error}`);
241
+ }
242
+ }
243
+ async get(url, options = {}) {
244
+ return this.request(url, { ...options, method: "GET" });
245
+ }
246
+ async post(url, body, options = {}) {
247
+ return this.request(url, { ...options, method: "POST", body });
248
+ }
249
+ async put(url, body, options = {}) {
250
+ return this.request(url, { ...options, method: "PUT", body });
251
+ }
252
+ async delete(url, options = {}) {
253
+ return this.request(url, { ...options, method: "DELETE" });
254
+ }
255
+ async patch(url, body, options = {}) {
256
+ return this.request(url, { ...options, method: "PATCH", body });
257
+ }
258
+ async head(url, options = {}) {
259
+ return this.request(url, { ...options, method: "HEAD" });
260
+ }
261
+ prefetch(urls) {}
262
+ }
263
+
264
+ // components/runtime.ts
265
+ var isBun = typeof Bun !== "undefined";
266
+
267
+ // components/index.ts
268
+ var JirenClient2 = isBun ? (await Promise.resolve().then(() => (init_client_bun(), exports_client_bun))).JirenClient : (await Promise.resolve().then(() => exports_client_node)).NodeJirenClient;
180
269
  export {
181
- JirenClient
270
+ JirenClient2 as JirenClient
182
271
  };
package/package.json CHANGED
@@ -1,18 +1,19 @@
1
1
  {
2
2
  "name": "jiren",
3
- "version": "1.0.15",
3
+ "version": "1.1.0",
4
4
  "author": "",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.js",
7
7
  "types": "types/index.ts",
8
8
  "devDependencies": {
9
9
  "@types/bun": "^1.3.4",
10
+ "@types/node": "^20",
10
11
  "bun-types": "^1.3.4"
11
12
  },
12
13
  "peerDependencies": {
13
14
  "typescript": "^5"
14
15
  },
15
- "description": "Jiren is a high-performance HTTP/HTTPS client, Faster than any other HTTP/HTTPS client.",
16
+ "description": "Jiren is a high-performance HTTP/HTTPS client. Ultra-fast with Bun FFI, falls back to fetch in Node.js.",
16
17
  "files": [
17
18
  "dist",
18
19
  "lib",
@@ -29,6 +30,7 @@
29
30
  "benchmark",
30
31
  "performance",
31
32
  "bun",
33
+ "node",
32
34
  "parser"
33
35
  ],
34
36
  "license": "MIT",
@@ -39,6 +41,7 @@
39
41
  "test": "bun run examples/basic.ts"
40
42
  },
41
43
  "engines": {
44
+ "node": ">=18.0.0",
42
45
  "bun": ">=1.1.0"
43
46
  },
44
47
  "type": "module"
File without changes