@rush-fs/core 0.1.0-alpha.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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2020 N-API for Rust
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,503 @@
1
+ <div align="center">
2
+
3
+ # Rush-FS
4
+
5
+ [English](./README.md) | [中文](./README.zh-CN.md)
6
+
7
+ <p align="center">
8
+ <img src="https://img.shields.io/badge/Written%20in-Rust-orange?style=flat-square" alt="Written in Rust">
9
+ <img src="https://img.shields.io/npm/v/@rush-fs/core?style=flat-square" alt="NPM Version">
10
+ <img src="https://img.shields.io/npm/l/@rush-fs/core?style=flat-square" alt="License">
11
+ <img src="https://img.shields.io/badge/status-alpha-orange?style=flat-square" alt="Alpha">
12
+ <a href="https://github.com/CoderSerio/rush-fs/graphs/contributors"><img src="https://img.shields.io/github/contributors/CoderSerio/rush-fs?style=flat-square" alt="Contributors"></a>
13
+ </p>
14
+
15
+ <p align="center">
16
+ API-aligned with Node.js <code>fs</code> for painless drop-in replacement in existing projects; get multi-fold performance in heavy file operations, powered by Rust.
17
+ </p>
18
+
19
+ <p align="center"><strong>⚠️ Alpha:</strong> The package is currently in <strong>alpha</strong>. API and behavior may change before 0.1.0 stable.</p>
20
+
21
+ </div>
22
+
23
+ ## Installation
24
+
25
+ ```bash
26
+ npm install @rush-fs/core
27
+ # or
28
+ pnpm add @rush-fs/core
29
+ ```
30
+
31
+ When you install `@rush-fs/core`, the package manager should automatically install the **platform-specific native binding** for your OS/arch via `optionalDependencies` (e.g. `@rush-fs/rush-fs-darwin-arm64` on macOS ARM). If the native binding is missing and you see "Cannot find native binding", try:
32
+
33
+ 1. Remove `node_modules` and the lockfile (`package-lock.json` or `pnpm-lock.yaml`), then run `pnpm install` (or `npm i`) again.
34
+ 2. Or install the platform package explicitly:
35
+ **macOS ARM:** `pnpm add @rush-fs/rush-fs-darwin-arm64`
36
+ **macOS x64:** `pnpm add @rush-fs/rush-fs-darwin-x64`
37
+ **Windows x64:** `pnpm add @rush-fs/rush-fs-win32-x64-msvc`
38
+ **Linux x64 (glibc):** `pnpm add @rush-fs/rush-fs-linux-x64-gnu`
39
+
40
+ **Migration from `rush-fs`:** The package was renamed to `@rush-fs/core`. See [CHANGELOG.md](./CHANGELOG.md#010-alpha0) for details.
41
+
42
+ ## Usage
43
+
44
+ ```ts
45
+ import { readdir, stat, readFile, writeFile, mkdir, rm } from '@rush-fs/core'
46
+
47
+ // Read directory
48
+ const files = await readdir('./src')
49
+
50
+ // Recursive with file types
51
+ const entries = await readdir('./src', {
52
+ recursive: true,
53
+ withFileTypes: true,
54
+ })
55
+
56
+ // Read / write files
57
+ const content = await readFile('./package.json', { encoding: 'utf8' })
58
+ await writeFile('./output.txt', 'hello world')
59
+
60
+ // File stats
61
+ const s = await stat('./package.json')
62
+ console.log(s.size, s.isFile())
63
+
64
+ // Create directory
65
+ await mkdir('./new-dir', { recursive: true })
66
+
67
+ // Remove
68
+ await rm('./temp', { recursive: true, force: true })
69
+ ```
70
+
71
+ ## Benchmarks
72
+
73
+ > Tested on Apple Silicon (arm64), Node.js 24.0.2, release build with LTO.
74
+ > Run `pnpm build && pnpm bench` to reproduce.
75
+
76
+ ### Where Rush-FS Shines
77
+
78
+ These are the scenarios where Rust's parallelism and zero-copy I/O make a real difference:
79
+
80
+ | Scenario | Node.js | Rush-FS | Speedup |
81
+ | ------------------------------------------------ | --------- | -------- | --------- |
82
+ | `readdir` recursive (node_modules, ~30k entries) | 281 ms | 23 ms | **12x** |
83
+ | `glob` recursive (`**/*.rs`) | 25 ms | 1.46 ms | **17x** |
84
+ | `glob` recursive vs fast-glob | 102 ms | 1.46 ms | **70x** |
85
+ | `copyFile` 4 MB | 4.67 ms | 0.09 ms | **50x** |
86
+ | `readFile` 4 MB utf8 | 1.86 ms | 0.92 ms | **2x** |
87
+ | `readFile` 64 KB utf8 | 42 µs | 18 µs | **2.4x** |
88
+ | `rm` 2000 files (4 threads) | 92 ms | 53 ms | **1.75x** |
89
+ | `access` R_OK (directory) | 4.18 µs | 1.55 µs | **2.7x** |
90
+ | `cp` 500-file flat dir (4 threads) | 86.45 ms | 32.88 ms | **2.6x** |
91
+ | `cp` tree dir ~363 nodes (4 threads) | 108.73 ms | 46.88 ms | **2.3x** |
92
+
93
+ ### On Par with Node.js
94
+
95
+ Single-file operations have a ~0.3 µs napi bridge overhead, making them roughly equivalent:
96
+
97
+ | Scenario | Node.js | Rush-FS | Ratio |
98
+ | -------------------------- | ------- | ------- | ----- |
99
+ | `stat` (single file) | 1.45 µs | 1.77 µs | 1.2x |
100
+ | `readFile` small (Buffer) | 8.86 µs | 9.46 µs | 1.1x |
101
+ | `writeFile` small (string) | 74 µs | 66 µs | 0.9x |
102
+ | `writeFile` small (Buffer) | 115 µs | 103 µs | 0.9x |
103
+ | `appendFile` | 30 µs | 27 µs | 0.9x |
104
+
105
+ ### Where Node.js Wins
106
+
107
+ Lightweight built-in calls where napi overhead is proportionally large:
108
+
109
+ | Scenario | Node.js | Rush-FS | Note |
110
+ | ---------------------------- | ------- | ------- | --------------------------------- |
111
+ | `existsSync` (existing file) | 444 ns | 1.34 µs | Node.js internal fast path |
112
+ | `accessSync` F_OK | 456 ns | 1.46 µs | Same — napi overhead dominates |
113
+ | `writeFile` 4 MB string | 2.93 ms | 5.69 ms | Large string crossing napi bridge |
114
+
115
+ ### Parallelism
116
+
117
+ Rush-FS uses multi-threaded parallelism for operations that traverse the filesystem:
118
+
119
+ | API | Library | `concurrency` option | Default |
120
+ | --------------------- | ------------------------------------------------------------------------- | -------------------- | ------- |
121
+ | `readdir` (recursive) | [jwalk](https://github.com/Byron/jwalk) | ✅ | auto |
122
+ | `glob` | [ignore](https://github.com/BurntSushi/ripgrep/tree/master/crates/ignore) | ✅ | 4 |
123
+ | `rm` (recursive) | [rayon](https://github.com/rayon-rs/rayon) | ✅ | 1 |
124
+ | `cp` (recursive) | [rayon](https://github.com/rayon-rs/rayon) | ✅ | 1 |
125
+
126
+ Single-file operations (`stat`, `readFile`, `writeFile`, `chmod`, etc.) are atomic syscalls — parallelism does not apply.
127
+
128
+ ### Key Takeaway
129
+
130
+ **Rush-FS excels at recursive / batch filesystem operations** (readdir, glob, rm, cp) where Rust's parallel walkers deliver 2–70x speedups. For single-file operations it performs on par with Node.js. The napi bridge adds a fixed ~0.3 µs overhead per call, which only matters for sub-microsecond operations like `existsSync`.
131
+
132
+ **`cp` benchmark detail** (Apple Silicon, release build):
133
+
134
+ | Scenario | Node.js | Rush-FS 1T | Rush-FS 4T | Rush-FS 8T |
135
+ | ----------------------------------------- | --------- | ---------- | ---------- | ---------- |
136
+ | Flat dir (500 files) | 86.45 ms | 61.56 ms | 32.88 ms | 36.67 ms |
137
+ | Tree dir (breadth=4, depth=3, ~84 nodes) | 23.80 ms | 16.94 ms | 10.62 ms | 9.76 ms |
138
+ | Tree dir (breadth=3, depth=5, ~363 nodes) | 108.73 ms | 75.39 ms | 46.88 ms | 46.18 ms |
139
+
140
+ Optimal concurrency for `cp` is **4 threads** on Apple Silicon — beyond that, I/O bandwidth becomes the bottleneck and diminishing returns set in.
141
+
142
+ ## How it works
143
+
144
+ For the original Node.js, it works serially and cost lots of memory to parse os object and string into JS style:
145
+
146
+ ```mermaid
147
+ graph TD
148
+ A["JS: readdir"] -->|Call| B("Node.js C++ Binding")
149
+ B -->|Submit Task| C{"Libuv Thread Pool"}
150
+
151
+ subgraph "Native Layer (Serial)"
152
+ C -->|"Syscall: getdents"| D[OS Kernel]
153
+ D -->|"Return File List"| C
154
+ C -->|"Process Paths"| C
155
+ end
156
+
157
+ C -->|"Results Ready"| E("V8 Main Thread")
158
+
159
+ subgraph "V8 Interaction (Heavy)"
160
+ E -->|"Create JS String 1"| F[V8 Heap]
161
+ E -->|"String 2"| F
162
+ E -->|"String N..."| F
163
+ F -->|"GC Pressure Rising"| F
164
+ end
165
+
166
+ E -->|"Return Array"| G["JS Callback/Promise"]
167
+ ```
168
+
169
+ But, it's saved with Rust now:
170
+
171
+ ```mermaid
172
+ graph TD
173
+ A["JS: readdir"] -->|"N-API Call"| B("Rust Wrapper")
174
+ B -->|"Spawn Thread/Task"| C{"Rust Thread Pool"}
175
+
176
+ subgraph "Rust 'Black Box'"
177
+ C -->|"Rayon: Parallel work"| D[OS Kernel]
178
+ D -->|"Syscall: getdents"| C
179
+ C -->|"Store as Rust Vec<String>"| H[Rust Heap]
180
+ H -->|"No V8 Interaction yet"| H
181
+ end
182
+
183
+ C -->|"All Done"| I("Convert to JS")
184
+
185
+ subgraph "N-API Bridge"
186
+ I -->|"Batch Create JS Array"| J[V8 Heap]
187
+ end
188
+
189
+ J -->|Return| K["JS Result"]
190
+ ```
191
+
192
+ ## Status & Roadmap
193
+
194
+ We are rewriting `fs` APIs one by one.
195
+
196
+ > **Legend**
197
+ >
198
+ > - ✅: Fully Supported
199
+ > - 🚧: Partially Supported / WIP
200
+ > - ✨: New feature from @rush-fs/core
201
+ > - ❌: Not Supported Yet
202
+
203
+ ### `readdir`
204
+
205
+ - **Node.js Arguments**:
206
+ ```ts
207
+ path: string; // ✅
208
+ options?: {
209
+ encoding?: string; // 🚧 ('utf8' default; 'buffer' not supported)
210
+ withFileTypes?: boolean; // ✅
211
+ recursive?: boolean; // ✅
212
+ concurrency?: number; // ✨
213
+ };
214
+ ```
215
+ - **Return Type**:
216
+ ```ts
217
+ string[]
218
+ | {
219
+ name: string, // ✅
220
+ parentPath: string, // ✅
221
+ isDir: boolean // ✅
222
+ }[]
223
+ ```
224
+
225
+ ### `readFile`
226
+
227
+ - **Node.js Arguments**:
228
+ ```ts
229
+ path: string; // ✅
230
+ options?: {
231
+ encoding?: string; // ✅ (utf8, ascii, latin1, base64, base64url, hex)
232
+ flag?: string; // ✅ (r, r+, w+, a+, etc.)
233
+ };
234
+ ```
235
+ - **Return Type**: `string | Buffer`
236
+
237
+ ### `writeFile`
238
+
239
+ - **Node.js Arguments**:
240
+ ```ts
241
+ path: string; // ✅
242
+ data: string | Buffer; // ✅
243
+ options?: {
244
+ encoding?: string; // ✅ (utf8, ascii, latin1, base64, base64url, hex)
245
+ mode?: number; // ✅
246
+ flag?: string; // ✅ (w, wx, a, ax)
247
+ };
248
+ ```
249
+
250
+ ### `appendFile`
251
+
252
+ - **Node.js Arguments**:
253
+ ```ts
254
+ path: string; // ✅
255
+ data: string | Buffer; // ✅
256
+ options?: {
257
+ encoding?: string; // ✅ (utf8, ascii, latin1, base64, base64url, hex)
258
+ mode?: number; // ✅
259
+ flag?: string; // ✅
260
+ };
261
+ ```
262
+
263
+ ### `copyFile`
264
+
265
+ - **Node.js Arguments**:
266
+ ```ts
267
+ src: string; // ✅
268
+ dest: string; // ✅
269
+ mode?: number; // ✅ (COPYFILE_EXCL)
270
+ ```
271
+
272
+ ### `cp`
273
+
274
+ - **Node.js Arguments** (Node 16.7+):
275
+ ```ts
276
+ src: string; // ✅
277
+ dest: string; // ✅
278
+ options?: {
279
+ recursive?: boolean; // ✅
280
+ force?: boolean; // ✅ (default: true)
281
+ errorOnExist?: boolean; // ✅
282
+ preserveTimestamps?: boolean; // ✅
283
+ dereference?: boolean; // ✅
284
+ verbatimSymlinks?: boolean; // ✅
285
+ concurrency?: number; // ✨
286
+ };
287
+ ```
288
+
289
+ ### `mkdir`
290
+
291
+ - **Node.js Arguments**:
292
+ ```ts
293
+ path: string; // ✅
294
+ options?: {
295
+ recursive?: boolean; // ✅
296
+ mode?: number; // ✅
297
+ };
298
+ ```
299
+ - **Return Type**: `string | undefined` (first created path when recursive)
300
+
301
+ ### `rm`
302
+
303
+ - **Node.js Arguments**:
304
+ ```ts
305
+ path: string; // ✅
306
+ options?: {
307
+ force?: boolean; // ✅
308
+ maxRetries?: number; // ✅
309
+ recursive?: boolean; // ✅
310
+ retryDelay?: number; // ✅ (default: 100ms)
311
+ concurrency?: number; // ✨
312
+ };
313
+ ```
314
+
315
+ ### `rmdir`
316
+
317
+ - **Node.js Arguments**:
318
+ ```ts
319
+ path: string // ✅
320
+ ```
321
+
322
+ ### `stat`
323
+
324
+ - **Node.js Arguments**:
325
+ ```ts
326
+ path: string // ✅
327
+ ```
328
+ - **Return Type**: `Stats`
329
+ - Numeric fields: `dev`, `mode`, `nlink`, `uid`, `gid`, `rdev`, `blksize`, `ino`, `size`, `blocks`, `atimeMs`, `mtimeMs`, `ctimeMs`, `birthtimeMs`
330
+ - **Date fields**: `atime`, `mtime`, `ctime`, `birthtime` → `Date` objects ✅
331
+ - Methods: `isFile()`, `isDirectory()`, `isSymbolicLink()`, ...
332
+ - **Error distinction**: `ENOENT` vs `EACCES` ✅
333
+
334
+ ### `lstat`
335
+
336
+ - **Node.js Arguments**:
337
+ ```ts
338
+ path: string // ✅
339
+ ```
340
+ - **Return Type**: `Stats`
341
+
342
+ ### `fstat`
343
+
344
+ - **Status**: ❌
345
+
346
+ ### `access`
347
+
348
+ - **Node.js Arguments**:
349
+ ```ts
350
+ path: string; // ✅
351
+ mode?: number; // ✅ (F_OK, R_OK, W_OK, X_OK)
352
+ ```
353
+
354
+ ### `exists`
355
+
356
+ - **Node.js Arguments**:
357
+ ```ts
358
+ path: string // ✅
359
+ ```
360
+ - **Return Type**: `boolean`
361
+
362
+ ### `open`
363
+
364
+ - **Status**: ❌
365
+
366
+ ### `opendir`
367
+
368
+ - **Status**: ❌
369
+
370
+ ### `close`
371
+
372
+ - **Status**: ❌
373
+
374
+ ### `unlink`
375
+
376
+ - **Node.js Arguments**:
377
+ ```ts
378
+ path: string // ✅
379
+ ```
380
+
381
+ ### `rename`
382
+
383
+ - **Node.js Arguments**:
384
+ ```ts
385
+ oldPath: string // ✅
386
+ newPath: string // ✅
387
+ ```
388
+
389
+ ### `readlink`
390
+
391
+ - **Node.js Arguments**:
392
+ ```ts
393
+ path: string // ✅
394
+ ```
395
+ - **Return Type**: `string`
396
+
397
+ ### `realpath`
398
+
399
+ - **Node.js Arguments**:
400
+ ```ts
401
+ path: string // ✅
402
+ ```
403
+ - **Return Type**: `string`
404
+
405
+ ### `chmod`
406
+
407
+ - **Node.js Arguments**:
408
+ ```ts
409
+ path: string // ✅
410
+ mode: number // ✅
411
+ ```
412
+
413
+ ### `chown`
414
+
415
+ - **Node.js Arguments**:
416
+ ```ts
417
+ path: string // ✅
418
+ uid: number // ✅
419
+ gid: number // ✅
420
+ ```
421
+
422
+ ### `utimes`
423
+
424
+ - **Node.js Arguments**:
425
+ ```ts
426
+ path: string // ✅
427
+ atime: number // ✅
428
+ mtime: number // ✅
429
+ ```
430
+
431
+ ### `truncate`
432
+
433
+ - **Node.js Arguments**:
434
+ ```ts
435
+ path: string; // ✅
436
+ len?: number; // ✅
437
+ ```
438
+
439
+ ### `glob`
440
+
441
+ - **Node.js Arguments**:
442
+ ```ts
443
+ pattern: string; // ✅
444
+ options?: {
445
+ cwd?: string; // ✅
446
+ withFileTypes?: boolean; // ✅
447
+ exclude?: string[]; // ✅
448
+ concurrency?: number; // ✨
449
+ gitIgnore?: boolean; // ✨
450
+ };
451
+ ```
452
+
453
+ ### `symlink`
454
+
455
+ - **Node.js Arguments**:
456
+ ```ts
457
+ target: string // ✅
458
+ path: string // ✅
459
+ type?: 'file' | 'dir' | 'junction' // ✅ (Windows only, ignored on Unix)
460
+ ```
461
+
462
+ ### `link`
463
+
464
+ - **Node.js Arguments**:
465
+ ```ts
466
+ existingPath: string // ✅
467
+ newPath: string // ✅
468
+ ```
469
+
470
+ ### `mkdtemp`
471
+
472
+ - **Node.js Arguments**:
473
+ ```ts
474
+ prefix: string // ✅
475
+ ```
476
+ - **Return Type**: `string`
477
+ - Uses OS-level random source (`/dev/urandom` on Unix, `BCryptGenRandom` on Windows) with up to 10 retries ✅
478
+
479
+ ### `watch`
480
+
481
+ - **Status**: ❌
482
+
483
+ ## Changelog
484
+
485
+ See [CHANGELOG.md](./CHANGELOG.md) for a summary of changes in each version. Release tags are listed in [GitHub Releases](https://github.com/CoderSerio/rush-fs/releases).
486
+
487
+ ## Contributing
488
+
489
+ See [CONTRIBUTING.md](./CONTRIBUTING.md) for the full development guide: environment setup, Node.js reference, Rust implementation, testing, and benchmarking.
490
+
491
+ ## Publishing (Maintainers Only)
492
+
493
+ Releases are handled by the [Release workflow](.github/workflows/Release.yml): it builds native binaries for macOS (x64/arm64), Windows, and Linux, then publishes the platform packages and the main package to npm.
494
+
495
+ 1. **Secrets:** In the repo **Settings → Secrets and variables → Actions**, add **NPM_TOKEN** (npm Classic or Automation token with Publish permission).
496
+ 2. **Release:** Either run **Actions → Release → Run workflow** (uses the current `package.json` version on `main`), or bump version in `package.json` and `Cargo.toml`, push to `main`, then create and push a tag: `git tag v<version> && git push origin v<version>`.
497
+ 3. **Changelog:** Update [CHANGELOG.md](./CHANGELOG.md) before or right after the release (move entries from `[Unreleased]` to a new version heading and add the compare link).
498
+
499
+ The workflow injects `optionalDependencies` and publishes all packages; no need to edit `package.json` manually for release.
500
+
501
+ ## License
502
+
503
+ MIT