rust-fetch 0.7.2 → 1.0.1

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 ADDED
@@ -0,0 +1,288 @@
1
+ # rust-fetch
2
+
3
+ A WebAssembly-powered HTTP client library for JavaScript/TypeScript applications, built with Rust. This library provides fast, memory-safe HTTP operations through WebAssembly bindings, allowing you to leverage Rust's performance and safety guarantees in your web applications.
4
+
5
+ ## Features
6
+
7
+ - 🚀 **High Performance**: Leverage Rust's performance for HTTP operations through WebAssembly
8
+ - 🔐 **Memory Safe**: Built with Rust's memory safety guarantees
9
+ - 🌐 **Cross-Platform**: Works in both browser and Node.js environments
10
+ - 📦 **TypeScript Support**: Full TypeScript definitions included
11
+ - 🛠️ **Multiple Utilities**: HTTP fetching, JSON parsing, HTML retrieval, and utility functions
12
+
13
+ ## Installation
14
+
15
+ ```bash
16
+ npm install rust-fetch
17
+ ```
18
+
19
+ or
20
+
21
+ ```bash
22
+ yarn add rust-fetch
23
+ ```
24
+
25
+ ## Project Structure
26
+
27
+ ```
28
+ rust-fetch/
29
+ ├── rust-fetch-package/ # Main Rust package (currently basic implementation)
30
+ │ ├── Cargo.toml
31
+ │ └── src/
32
+ │ └── lib.rs
33
+ ├── wasm-module/ # WebAssembly module with HTTP and utility functions
34
+ │ ├── Cargo.toml
35
+ │ ├── pkg/ # Generated WASM package
36
+ │ └── src/
37
+ │ ├── greetings/ # Greeting utilities
38
+ │ ├── http/ # HTTP client functions
39
+ │ ├── math/ # Math utilities
40
+ │ └── lib.rs
41
+ ├── client.ts # Client-side entry point
42
+ ├── server.ts # Server-side entry point
43
+ ├── package.json # NPM package configuration
44
+ └── tsconfig.json # TypeScript configuration
45
+ ```
46
+
47
+ ## Usage
48
+
49
+ ### Client-Side (Browser)
50
+
51
+ ```typescript
52
+ import initWasm, {
53
+ fetch_wasm_json,
54
+ fetch_wasm_html,
55
+ greet,
56
+ add
57
+ } from 'rust-fetch/client';
58
+
59
+ // Initialize WASM module
60
+ await initWasm();
61
+
62
+ // Fetch JSON data
63
+ try {
64
+ const data = await fetch_wasm_json('https://api.example.com/data');
65
+ console.log('JSON data:', data);
66
+ } catch (error) {
67
+ console.error('Error fetching JSON:', error);
68
+ }
69
+
70
+ // Fetch HTML content
71
+ try {
72
+ const html = await fetch_wasm_html('https://example.com');
73
+ console.log('HTML content:', html);
74
+ } catch (error) {
75
+ console.error('Error fetching HTML:', error);
76
+ }
77
+
78
+ // Use utility functions
79
+ const greeting = greet('World');
80
+ console.log(greeting); // "Hello, World!"
81
+
82
+ const sum = add(5, 3);
83
+ console.log(sum); // 8
84
+ ```
85
+
86
+ ### Server-Side (Node.js)
87
+
88
+ ```typescript
89
+ import initWasm, {
90
+ fetch_wasm_json,
91
+ fetch_wasm_html,
92
+ greet,
93
+ add
94
+ } from 'rust-fetch/server';
95
+
96
+ // WASM is automatically initialized on import
97
+
98
+ // Fetch JSON data
99
+ try {
100
+ const data = await fetch_wasm_json('https://api.example.com/data');
101
+ console.log('JSON data:', data);
102
+ } catch (error) {
103
+ console.error('Error fetching JSON:', error);
104
+ }
105
+
106
+ // Use other functions similarly
107
+ ```
108
+
109
+ ## API Reference
110
+
111
+ ### HTTP Functions
112
+
113
+ #### `fetch_wasm_json(url: string): Promise<any>`
114
+ Fetches JSON data from the specified URL and returns it as a JavaScript object.
115
+
116
+ ```typescript
117
+ const data = await fetch_wasm_json('https://jsonplaceholder.typicode.com/todos/1');
118
+ ```
119
+
120
+ #### `fetch_wasm_json_new(url: string): Promise<any>`
121
+ Alternative implementation that returns a Promise directly (useful for different async patterns).
122
+
123
+ ```typescript
124
+ const promise = fetch_wasm_json_new('https://api.example.com/data');
125
+ const data = await promise;
126
+ ```
127
+
128
+ #### `fetch_wasm_map(url: string): Promise<any>`
129
+ Fetches JSON data and returns it as a JavaScript object using `serde_wasm_bindgen`.
130
+
131
+ ```typescript
132
+ const data = await fetch_wasm_map('https://api.example.com/data');
133
+ ```
134
+
135
+ #### `fetch_wasm_html(url: string): Promise<string>`
136
+ Fetches HTML content from the specified URL and returns it as a string.
137
+
138
+ ```typescript
139
+ const html = await fetch_wasm_html('https://example.com');
140
+ ```
141
+
142
+ #### `fetch_wasm_api(): Promise<any>`
143
+ Fetches data from a predefined API endpoint (JSONPlaceholder todo item).
144
+
145
+ ```typescript
146
+ const todo = await fetch_wasm_api();
147
+ ```
148
+
149
+ ### Utility Functions
150
+
151
+ #### `greet(name: string): string`
152
+ Returns a greeting message.
153
+
154
+ ```typescript
155
+ const message = greet('Alice'); // "Hello, Alice!"
156
+ ```
157
+
158
+ #### `add(a: number, b: number): number`
159
+ Adds two numbers together.
160
+
161
+ ```typescript
162
+ const result = add(10, 20); // 30
163
+ ```
164
+
165
+ ## Building from Source
166
+
167
+ ### Prerequisites
168
+
169
+ - Rust (latest stable version)
170
+ - Node.js (v14 or higher)
171
+ - wasm-pack (`cargo install wasm-pack`)
172
+
173
+ ### Build Steps
174
+
175
+ 1. Clone the repository:
176
+ ```bash
177
+ git clone https://github.com/yourusername/rust-fetch.git
178
+ cd rust-fetch
179
+ ```
180
+
181
+ 2. Install dependencies:
182
+ ```bash
183
+ npm install
184
+ ```
185
+
186
+ 3. Build the WASM module:
187
+ ```bash
188
+ npm run build:wasm
189
+ ```
190
+
191
+ 4. Build TypeScript files:
192
+ ```bash
193
+ npm run build
194
+ ```
195
+
196
+ ## Development
197
+
198
+ ### Building the WASM Module
199
+
200
+ ```bash
201
+ cd wasm-module
202
+ wasm-pack build --target web
203
+ ```
204
+
205
+ This generates the `pkg` directory with JavaScript bindings and TypeScript definitions.
206
+
207
+ ### Running Tests
208
+
209
+ For Rust tests:
210
+ ```bash
211
+ cd wasm-module
212
+ cargo test
213
+
214
+ cd ../rust-fetch-package
215
+ cargo test
216
+ ```
217
+
218
+ ## Performance Considerations
219
+
220
+ - The WASM module adds a small overhead for initialization (~100-200ms on first load)
221
+ - Once loaded, HTTP operations benefit from Rust's efficient memory management
222
+ - Best suited for applications that make multiple HTTP requests or need predictable performance
223
+
224
+ ## Browser Support
225
+
226
+ - Chrome 57+
227
+ - Firefox 52+
228
+ - Safari 11+
229
+ - Edge 16+
230
+ - Node.js 14+
231
+
232
+ ## Contributing
233
+
234
+ Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
235
+
236
+ 1. Fork the repository
237
+ 2. Create your feature branch (`git checkout -b feature/amazing-feature`)
238
+ 3. Commit your changes (`git commit -m 'Add some amazing feature'`)
239
+ 4. Push to the branch (`git push origin feature/amazing-feature`)
240
+ 5. Open a Pull Request
241
+
242
+ ## License
243
+
244
+ This project is licensed under the ISC License - see the LICENSE file for details.
245
+
246
+ ## Troubleshooting
247
+
248
+ ### WASM Module Not Loading
249
+
250
+ If you encounter issues with the WASM module not loading:
251
+
252
+ 1. Ensure your server supports the `application/wasm` MIME type
253
+ 2. Check that the WASM file is being served correctly
254
+ 3. Verify you're awaiting the `initWasm()` call before using any functions
255
+
256
+ ### CORS Issues
257
+
258
+ When fetching from external APIs, ensure:
259
+
260
+ 1. The target server allows CORS requests
261
+ 2. You're using HTTPS URLs in production
262
+ 3. Consider using a proxy server for APIs that don't support CORS
263
+
264
+ ### Node.js Path Issues
265
+
266
+ If you encounter path resolution issues in Node.js:
267
+
268
+ 1. Ensure the package is installed correctly
269
+ 2. Check that the WASM file exists in `node_modules/rust-fetch/wasm-module/pkg/`
270
+ 3. Try reinstalling the package
271
+
272
+ ## Acknowledgments
273
+
274
+ - Built with [wasm-bindgen](https://github.com/rustwasm/wasm-bindgen)
275
+ - Uses [reqwest](https://github.com/seanmonstar/reqwest) for HTTP operations
276
+ - Inspired by the Rust and WebAssembly community
277
+
278
+ ## Roadmap
279
+
280
+ - [ ] Add support for POST, PUT, DELETE methods
281
+ - [ ] Implement request headers customization
282
+ - [ ] Add streaming support for large responses
283
+ - [ ] Implement request cancellation
284
+ - [ ] Add comprehensive test suite
285
+ - [ ] Performance benchmarks against native fetch
286
+ - [ ] Support for form data and file uploads
287
+ - [ ] WebSocket support
288
+ - [ ] Request/response interceptors
package/package.json CHANGED
@@ -1,30 +1,31 @@
1
1
  {
2
2
  "name": "rust-fetch",
3
- "version": "0.7.2",
4
- "publishConfig": {
5
- "access": "public"
3
+ "type": "module",
4
+ "collaborators": [
5
+ "Asem Qaffaf <asem@qaffaf.com>"
6
+ ],
7
+ "description": "A world-class WebAssembly HTTP client library with excellent error handling and clean API",
8
+ "version": "1.0.1",
9
+ "license": "MIT OR Apache-2.0",
10
+ "repository": {
11
+ "type": "git",
12
+ "url": "https://github.com/asemqaffaf/rust-fetch"
6
13
  },
7
- "export": {
8
- "fetch": "./wasm-module/pkg/wasm_module",
9
- "server": "server.ts",
10
- "client": "client.ts"
11
- },
12
- "scripts": {
13
- "build": "cd wasm-module && tsc",
14
- "build:wasm": "cd wasm-module && rm -rf pkg && wasm-pack build --target web && cd pkg && rm -rf .gitignore",
15
- "test": "echo \"Error: no test specified\" && exit 1"
16
- },
17
- "keywords": [],
18
- "author": "",
19
- "license": "ISC",
20
- "dependencies": {
21
- "path": "^0.12.7",
22
- "wasm-module": "file:wasm-module/pkg"
23
- },
24
- "devDependencies": {
25
- "@types/node": "^22.13.5",
26
- "typescript": "^5.7.3"
27
- },
28
- "main": "index.js",
29
- "description": ""
14
+ "files": [
15
+ "rust_fetch_bg.wasm",
16
+ "rust_fetch.js",
17
+ "rust_fetch.d.ts"
18
+ ],
19
+ "main": "rust_fetch.js",
20
+ "types": "rust_fetch.d.ts",
21
+ "sideEffects": [
22
+ "./snippets/*"
23
+ ],
24
+ "keywords": [
25
+ "wasm",
26
+ "http",
27
+ "client",
28
+ "fetch",
29
+ "webassembly"
30
+ ]
30
31
  }
@@ -0,0 +1,106 @@
1
+ /* tslint:disable */
2
+ /* eslint-disable */
3
+ /**
4
+ * Simple fetch function for JSON data
5
+ */
6
+ export function fetch_json(url: string): Promise<any>;
7
+ /**
8
+ * Fetch JSON data and return as a Promise
9
+ */
10
+ export function fetch_json_promise(url: string): Promise<any>;
11
+ /**
12
+ * Simple fetch function for text/HTML data
13
+ */
14
+ export function fetch_text(url: string): Promise<string>;
15
+ /**
16
+ * Advanced fetch function with full options
17
+ */
18
+ export function fetch_with_options(url: string, method: string, headers: any, body?: string): Promise<any>;
19
+ /**
20
+ * Create a configured HTTP client (exported for advanced use)
21
+ */
22
+ export function create_client(): WasmClient;
23
+ /**
24
+ * Fetch JSON data (deprecated, use fetch_json instead)
25
+ */
26
+ export function fetch_wasm_json(url: string): Promise<any>;
27
+ /**
28
+ * Fetch HTML data (deprecated, use fetch_text instead)
29
+ */
30
+ export function fetch_wasm_html(url: string): Promise<string>;
31
+ /**
32
+ * Fetch data and return as map (deprecated, use fetch_json instead)
33
+ */
34
+ export function fetch_wasm_map(url: string): Promise<any>;
35
+ /**
36
+ * Fetch API data (deprecated, use fetch_json instead)
37
+ */
38
+ export function fetch_wasm_api(url: string): Promise<any>;
39
+ /**
40
+ * WASM bindings for the client
41
+ */
42
+ export class WasmClient {
43
+ free(): void;
44
+ /**
45
+ * Create a new client
46
+ */
47
+ constructor();
48
+ /**
49
+ * Make a GET request
50
+ */
51
+ get(url: string): Promise<any>;
52
+ /**
53
+ * Make a POST request with JSON body
54
+ */
55
+ post_json(url: string, body: any): Promise<any>;
56
+ }
57
+
58
+ export type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembly.Module;
59
+
60
+ export interface InitOutput {
61
+ readonly memory: WebAssembly.Memory;
62
+ readonly fetch_json: (a: number, b: number) => any;
63
+ readonly fetch_json_promise: (a: number, b: number) => any;
64
+ readonly fetch_text: (a: number, b: number) => any;
65
+ readonly fetch_with_options: (a: number, b: number, c: number, d: number, e: any, f: number, g: number) => any;
66
+ readonly create_client: () => [number, number, number];
67
+ readonly fetch_wasm_json: (a: number, b: number) => any;
68
+ readonly fetch_wasm_html: (a: number, b: number) => any;
69
+ readonly fetch_wasm_map: (a: number, b: number) => any;
70
+ readonly fetch_wasm_api: (a: number, b: number) => any;
71
+ readonly __wbg_wasmclient_free: (a: number, b: number) => void;
72
+ readonly wasmclient_new: () => [number, number, number];
73
+ readonly wasmclient_get: (a: number, b: number, c: number) => any;
74
+ readonly wasmclient_post_json: (a: number, b: number, c: number, d: any) => any;
75
+ readonly __wbindgen_malloc: (a: number, b: number) => number;
76
+ readonly __wbindgen_realloc: (a: number, b: number, c: number, d: number) => number;
77
+ readonly __wbindgen_exn_store: (a: number) => void;
78
+ readonly __externref_table_alloc: () => number;
79
+ readonly __wbindgen_export_4: WebAssembly.Table;
80
+ readonly __wbindgen_export_5: WebAssembly.Table;
81
+ readonly __externref_table_dealloc: (a: number) => void;
82
+ readonly closure243_externref_shim: (a: number, b: number, c: any) => void;
83
+ readonly closure265_externref_shim: (a: number, b: number, c: any, d: any) => void;
84
+ readonly __wbindgen_start: () => void;
85
+ }
86
+
87
+ export type SyncInitInput = BufferSource | WebAssembly.Module;
88
+ /**
89
+ * Instantiates the given `module`, which can either be bytes or
90
+ * a precompiled `WebAssembly.Module`.
91
+ *
92
+ * @param {{ module: SyncInitInput }} module - Passing `SyncInitInput` directly is deprecated.
93
+ *
94
+ * @returns {InitOutput}
95
+ */
96
+ export function initSync(module: { module: SyncInitInput } | SyncInitInput): InitOutput;
97
+
98
+ /**
99
+ * If `module_or_path` is {RequestInfo} or {URL}, makes a request and
100
+ * for everything else, calls `WebAssembly.instantiate` directly.
101
+ *
102
+ * @param {{ module_or_path: InitInput | Promise<InitInput> }} module_or_path - Passing `InitInput` directly is deprecated.
103
+ *
104
+ * @returns {Promise<InitOutput>}
105
+ */
106
+ export default function __wbg_init (module_or_path?: { module_or_path: InitInput | Promise<InitInput> } | InitInput | Promise<InitInput>): Promise<InitOutput>;