real-size 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +366 -2
- package/bin/real-size +45 -0
- package/index.d.mts +69 -0
- package/index.mjs +138 -0
- package/package.json +61 -4
- package/prebuilds/darwin-arm64/real-size +0 -0
- package/prebuilds/darwin-x64/real-size +0 -0
- package/prebuilds/linux-arm64/real-size +0 -0
- package/prebuilds/linux-x64/real-size +0 -0
- package/prebuilds/win32-arm64/real-size.exe +0 -0
- package/prebuilds/win32-x64/real-size.exe +0 -0
- package/skill/SKILL.md +38 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 App & Flow
|
|
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
CHANGED
|
@@ -1,3 +1,367 @@
|
|
|
1
|
-
#
|
|
1
|
+
# real-size
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
How many bytes deleting a folder would actually free, as opposed to what `du`
|
|
4
|
+
shows. A command-line tool and a Node library for macOS, Linux and Windows. It
|
|
5
|
+
ships an [agent skill](#agent-skill) so coding agents reach for it instead of
|
|
6
|
+
`du`.
|
|
7
|
+
|
|
8
|
+
```console
|
|
9
|
+
$ du -sh node_modules
|
|
10
|
+
4.4G node_modules
|
|
11
|
+
$ npx real-size node_modules
|
|
12
|
+
node_modules: at least 90.3 MB private, 4.8 GB allocated, 168433 files
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Why `du` misleads
|
|
16
|
+
|
|
17
|
+
`du` adds up the blocks of every file under a path. That is the space the files
|
|
18
|
+
occupy, but not the space their deletion returns, because modern filesystems
|
|
19
|
+
share blocks between files:
|
|
20
|
+
|
|
21
|
+
- **Clones.** On APFS, `cp -c`, Finder copies and tools such as pnpm and
|
|
22
|
+
Xcode create copy-on-write clones. A clone shares every block with its
|
|
23
|
+
original until one of them changes. Deleting the clone frees nothing.
|
|
24
|
+
- **Hard links.** A package store (pnpm, Nix, Bazel caches) links one file
|
|
25
|
+
into many folders. Deleting one folder removes a link, not the data, while
|
|
26
|
+
another link exists outside it.
|
|
27
|
+
- **Reflinks and block clones.** Btrfs and XFS (`cp --reflink`) and ReFS or
|
|
28
|
+
Dev Drive on Windows share extents between files that each have a single
|
|
29
|
+
link, which a link count does not show.
|
|
30
|
+
|
|
31
|
+
`real-size` walks the tree and counts only the bytes no file outside it
|
|
32
|
+
shares.
|
|
33
|
+
|
|
34
|
+
## Install
|
|
35
|
+
|
|
36
|
+
```bash
|
|
37
|
+
npm install --global real-size
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
Or run it without installing: `npx real-size <path>`.
|
|
41
|
+
|
|
42
|
+
The package ships prebuilt binaries and has no install scripts and no runtime
|
|
43
|
+
dependencies.
|
|
44
|
+
|
|
45
|
+
On macOS and Linux the `real-size` command is a `#!/bin/sh` script that finds
|
|
46
|
+
the binary for the machine and `exec`s it, so no Node process is involved and
|
|
47
|
+
it adds a few milliseconds to the binary's start-up. It works when installed
|
|
48
|
+
with npm, pnpm, Yarn classic and Yarn Berry's `node-modules` linker, locally
|
|
49
|
+
or globally, through `npx`, `pnpm dlx` and `yarn dlx`, and with BusyBox `sh`
|
|
50
|
+
on Alpine. `yarn real-size` under Yarn Berry, including Plug'n'Play, runs the
|
|
51
|
+
command through Node; the same file is also a Node script that starts the
|
|
52
|
+
binary.
|
|
53
|
+
|
|
54
|
+
On Windows the command is **`real-size-windows`**, with the same arguments:
|
|
55
|
+
|
|
56
|
+
```bat
|
|
57
|
+
npm install --global real-size
|
|
58
|
+
real-size-windows node_modules
|
|
59
|
+
npx --package real-size real-size-windows node_modules
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
npm and pnpm generate Windows launchers from a command's `#!` line, so the
|
|
63
|
+
`real-size` command's `#!/bin/sh` needs an `sh` on Windows (Git Bash has one;
|
|
64
|
+
cmd.exe and PowerShell do not). `real-size-windows` is the x64 binary itself,
|
|
65
|
+
which the launchers start directly in cmd.exe, Windows PowerShell and
|
|
66
|
+
PowerShell, without Node. On Windows on Arm it starts the arm64 binary beside
|
|
67
|
+
it with the same arguments and returns its exit status. `real-size --version`
|
|
68
|
+
prints the version and the binary's platform, for example `0.1.0
|
|
69
|
+
(win32-arm64)`.
|
|
70
|
+
|
|
71
|
+
## Command line
|
|
72
|
+
|
|
73
|
+
```text
|
|
74
|
+
real-size [--json] [--no-progress] <path>...
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
Each path is measured on its own. Plain output prints one line per path, and a
|
|
78
|
+
total when several paths are given:
|
|
79
|
+
|
|
80
|
+
```console
|
|
81
|
+
$ real-size build packages
|
|
82
|
+
build: at least 24.6 kB private, 167.9 kB allocated, 3 files
|
|
83
|
+
packages: at least 94.7 MB private, 19.8 GB allocated, 68181 files
|
|
84
|
+
total: at least 94.7 MB private, 19.8 GB allocated, 68184 files
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
Sizes use decimal units (1 MB is 1,000,000 bytes). "at least" marks a result
|
|
88
|
+
that is not exact.
|
|
89
|
+
|
|
90
|
+
`--json` prints exactly one JSON document on stdout:
|
|
91
|
+
|
|
92
|
+
```json
|
|
93
|
+
{
|
|
94
|
+
"paths": [
|
|
95
|
+
{
|
|
96
|
+
"path": "node_modules",
|
|
97
|
+
"allocated": 4767227904,
|
|
98
|
+
"private": 90292224,
|
|
99
|
+
"files": 168433,
|
|
100
|
+
"exact": false,
|
|
101
|
+
"skipped": 0
|
|
102
|
+
}
|
|
103
|
+
],
|
|
104
|
+
"total": {
|
|
105
|
+
"allocated": 4767227904,
|
|
106
|
+
"private": 90292224,
|
|
107
|
+
"files": 168433,
|
|
108
|
+
"exact": false,
|
|
109
|
+
"skipped": 0
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
### Errors and exit status
|
|
115
|
+
|
|
116
|
+
An entry below a path that cannot be read, such as a directory without read
|
|
117
|
+
permission, is skipped and the walk goes on. Each one is named on stderr with
|
|
118
|
+
its own path, relative to the path as given, and the reason, up to ten lines;
|
|
119
|
+
the rest are counted in a last line:
|
|
120
|
+
|
|
121
|
+
```console
|
|
122
|
+
$ real-size ~/Library
|
|
123
|
+
real-size: /Users/me/Library/Mail: Operation not permitted
|
|
124
|
+
real-size: and 3 more entries could not be read
|
|
125
|
+
/Users/me/Library: at least 2.1 GB private, 9.8 GB allocated, 210377 files (4 entries could not be read)
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
The sizes are then lower bounds: `skipped` counts the entries left out and
|
|
129
|
+
`exact` is `false`. Like `du`, the command prints the result and exits with
|
|
130
|
+
status 1. An entry deleted while the walk runs is not an error: it frees
|
|
131
|
+
nothing, so it is left out without a message and does not count in `skipped`.
|
|
132
|
+
|
|
133
|
+
| Exit status | Meaning |
|
|
134
|
+
| ----------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
135
|
+
| 0 | Every path was measured in full. |
|
|
136
|
+
| 1 | Some entries could not be read: the result is on stdout with `skipped` above 0. Or a usage error, or a path itself could not be read: the reason is on stderr, stdout is empty. |
|
|
137
|
+
| 2 | The filesystem cannot report what deleting a path would free. Stdout is empty. |
|
|
138
|
+
|
|
139
|
+
With exit status 1, a result on stdout tells the two cases apart.
|
|
140
|
+
|
|
141
|
+
### Progress
|
|
142
|
+
|
|
143
|
+
When stderr is a terminal and a walk has run for more than two seconds,
|
|
144
|
+
`real-size` shows one status line on stderr with the files counted so far and
|
|
145
|
+
the directory it is in, and clears it before printing the result. It shows
|
|
146
|
+
nothing when stderr is not a terminal, so pipes, scripts and the library never
|
|
147
|
+
see it. `--no-progress` turns it off.
|
|
148
|
+
|
|
149
|
+
`--version` prints the version and `--help` prints usage.
|
|
150
|
+
|
|
151
|
+
## Library
|
|
152
|
+
|
|
153
|
+
```js
|
|
154
|
+
import { realSize, binaryPath, RealSizeError } from "real-size";
|
|
155
|
+
|
|
156
|
+
const { paths, total } = await realSize(["node_modules", "ios/Pods"], {
|
|
157
|
+
timeoutMs: 30_000,
|
|
158
|
+
});
|
|
159
|
+
console.log(`${total.private} bytes reclaimable`);
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
`realSize(paths, { signal, timeoutMs })` runs the binary in a child process
|
|
163
|
+
and resolves with the same document `--json` prints. The walk runs in its own
|
|
164
|
+
process so that a slow or hung filesystem cannot block the caller: at
|
|
165
|
+
`timeoutMs`, or when `signal` aborts, the child is killed with `SIGKILL` and
|
|
166
|
+
the promise rejects at once. It rejects with a `RealSizeError` whose `code` is:
|
|
167
|
+
|
|
168
|
+
- `unsupported`: no prebuilt binary for this platform, or exit status 2.
|
|
169
|
+
- `timeout`: `timeoutMs` ran out.
|
|
170
|
+
- `failed`: exit status 1 without a result (a path that cannot be read) or the
|
|
171
|
+
binary could not start. `message` holds the binary's error.
|
|
172
|
+
|
|
173
|
+
When some entries below a path could not be read, the promise resolves with
|
|
174
|
+
the result and `total.skipped` above 0, rather than rejecting. Version 0.1.0
|
|
175
|
+
rejected the whole measurement instead.
|
|
176
|
+
|
|
177
|
+
An aborted `signal` rejects with the signal's reason.
|
|
178
|
+
|
|
179
|
+
`binaryPath()` returns the absolute path of the prebuilt binary for this
|
|
180
|
+
platform, or `null` when the package has none, so you can spawn it yourself,
|
|
181
|
+
for example at a lower CPU or I/O priority.
|
|
182
|
+
|
|
183
|
+
TypeScript types are included. Node `^20.19.4 || >=22.12.0`.
|
|
184
|
+
|
|
185
|
+
## Output fields
|
|
186
|
+
|
|
187
|
+
- `allocated`: the bytes the tree's regular files occupy on disk. A file with
|
|
188
|
+
several hard links in the tree counts once.
|
|
189
|
+
- `private`: the bytes deleting the tree would free.
|
|
190
|
+
- `files`: the regular files counted, each hard-linked file once.
|
|
191
|
+
- `exact`: `true` when `private` is the exact answer, `false` when it is a
|
|
192
|
+
lower bound.
|
|
193
|
+
- `skipped`: entries that could not be read and were left out. Above 0, every
|
|
194
|
+
size is a lower bound and `exact` is `false`. Added in 0.1.1.
|
|
195
|
+
|
|
196
|
+
Directories, symbolic links and metadata are not counted. Symbolic links are
|
|
197
|
+
never followed, and neither are Windows junctions.
|
|
198
|
+
|
|
199
|
+
A file with more links than the tree holds is shared with a path outside the
|
|
200
|
+
tree and contributes nothing to `private`. That rule is exact on every
|
|
201
|
+
platform.
|
|
202
|
+
|
|
203
|
+
`exact` is `false` when some counted file shares blocks with another file
|
|
204
|
+
(a clone, reflink or block clone) and the other file could be inside the tree
|
|
205
|
+
too. Each copy then reports only its unshared bytes, so deleting both may free
|
|
206
|
+
more than the sum. The answer is a lower bound and the true figure lies
|
|
207
|
+
between `private` and `allocated`.
|
|
208
|
+
|
|
209
|
+
## Platforms
|
|
210
|
+
|
|
211
|
+
| Platform | Prebuilt | Hard links | Clones and shared extents | `exact` |
|
|
212
|
+
| --------------------------- | ---------- | ---------- | --------------------------------- | --------------------------------- |
|
|
213
|
+
| macOS, APFS | arm64, x64 | Exact | APFS private size per file | `false` when a file shares blocks |
|
|
214
|
+
| macOS, other volumes | | | | Exit status 2 |
|
|
215
|
+
| Linux, ext2/3/4, tmpfs | x64, arm64 | Exact | Not possible on these filesystems | Always `true` |
|
|
216
|
+
| Linux, btrfs, XFS | x64, arm64 | Exact | `FIEMAP` shared extent flag | `false` when an extent is shared |
|
|
217
|
+
| Linux, other filesystems | | | | Exit status 2 |
|
|
218
|
+
| Windows, NTFS, FAT, exFAT | x64, arm64 | Exact | Not possible on these volumes | Always `true` |
|
|
219
|
+
| Windows, ReFS and Dev Drive | x64, arm64 | Exact | Per-extent reference counts | `false` when an extent is shared |
|
|
220
|
+
| Windows, network shares | | | | Exit status 2 |
|
|
221
|
+
|
|
222
|
+
The Linux binaries are statically linked, so they run on glibc and musl
|
|
223
|
+
distributions alike.
|
|
224
|
+
|
|
225
|
+
### macOS
|
|
226
|
+
|
|
227
|
+
The walk uses `getattrlistbulk` and reads each file's
|
|
228
|
+
`ATTR_CMNEXT_PRIVATESIZE`, which APFS reports as the bytes the file does not
|
|
229
|
+
share with a clone. On a volume that does not report it (HFS+, FAT, SMB) the
|
|
230
|
+
exit status is 2. The walk descends into volumes mounted inside the tree.
|
|
231
|
+
|
|
232
|
+
APFS also reports a private size below the allocation for transparently
|
|
233
|
+
compressed files (decmpfs, for example files installed by some system tools),
|
|
234
|
+
so a tree that holds them reports `exact: false` and a lower bound even
|
|
235
|
+
without clones.
|
|
236
|
+
|
|
237
|
+
### Linux
|
|
238
|
+
|
|
239
|
+
The walk uses `fstatat`. `allocated` is `st_blocks` times 512. The filesystem
|
|
240
|
+
is identified by its `statfs` `f_type`, again for each volume mounted inside
|
|
241
|
+
the tree:
|
|
242
|
+
|
|
243
|
+
- ext2, ext3, ext4 and tmpfs share blocks only through hard links, so the
|
|
244
|
+
result is exact.
|
|
245
|
+
- Btrfs and XFS can share extents between files. Each file is opened
|
|
246
|
+
read-only and mapped with the `FS_IOC_FIEMAP` ioctl; extents flagged
|
|
247
|
+
`FIEMAP_EXTENT_SHARED` do not count as private, and any shared extent sets
|
|
248
|
+
`exact: false`. A file on these filesystems that cannot be opened, or whose
|
|
249
|
+
mapping fails, counts as entirely shared: it adds to `allocated`, nothing to
|
|
250
|
+
`private`, and sets `exact: false`.
|
|
251
|
+
- Any other filesystem (overlayfs, NFS, ZFS, bcachefs, FUSE mounts, sysfs
|
|
252
|
+
and so on) exits with status 2, because it may share blocks in ways the
|
|
253
|
+
walk cannot see. Container users meet this first: a Docker container's root
|
|
254
|
+
is overlayfs, while a bind-mounted host folder on ext4 works.
|
|
255
|
+
|
|
256
|
+
### Windows
|
|
257
|
+
|
|
258
|
+
The walk uses `FindFirstFileExW` with `\\?\` long paths, so paths longer than
|
|
259
|
+
`MAX_PATH` and any Unicode name work. Each file is opened for its attributes
|
|
260
|
+
only. `allocated` is the allocation size from `FILE_STANDARD_INFO`, or for a
|
|
261
|
+
compressed or sparse NTFS file its compressed size from
|
|
262
|
+
`FILE_COMPRESSION_INFO`, whichever is smaller. Hard links use
|
|
263
|
+
`nNumberOfLinks` with the volume serial number and the 128-bit `FILE_ID_INFO`
|
|
264
|
+
file id, which is unique on ReFS where the 64-bit index is not.
|
|
265
|
+
|
|
266
|
+
Reparse points whose tag is a name surrogate (symbolic links, junctions,
|
|
267
|
+
volume mount points) are not followed, so the walk never leaves the volume.
|
|
268
|
+
|
|
269
|
+
On a volume that supports block cloning (`FILE_SUPPORTS_BLOCK_REFCOUNTING`:
|
|
270
|
+
ReFS, and Dev Drive), each file's extents are read with
|
|
271
|
+
[`FSCTL_GET_RETRIEVAL_POINTERS_AND_REFCOUNT`](https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-fscc/80ac720b-374c-40b7-847c-e57874260b64).
|
|
272
|
+
An extent whose
|
|
273
|
+
[reference count](https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-fscc/dd2be29e-4f60-4bb4-b68a-20f2c34ac0d2)
|
|
274
|
+
is above 1 is shared and does not count as private, and sets
|
|
275
|
+
`exact: false`. ReFS supports this control code from Windows 10 1703 and
|
|
276
|
+
Windows Server 2019
|
|
277
|
+
([product notes](https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-fsa/4e3695bd-7574-4f24-a223-b4679c065b63)).
|
|
278
|
+
Microsoft documents it in the protocol specifications only, and the count it
|
|
279
|
+
reports is the count of the cluster each extent starts at. A file whose
|
|
280
|
+
extents cannot be read counts as entirely shared, as on Linux.
|
|
281
|
+
|
|
282
|
+
Cloud placeholders (OneDrive and other Cloud Files providers) are not read:
|
|
283
|
+
the walk opens each file for its attributes only, which Microsoft's
|
|
284
|
+
placeholder guidance treats as an attribute-only open that should not recall
|
|
285
|
+
data, though no page guarantees it. A file marked
|
|
286
|
+
`FILE_ATTRIBUTE_RECALL_ON_DATA_ACCESS`, `RECALL_ON_OPEN` or `OFFLINE`
|
|
287
|
+
contributes its local allocation, which for a dehydrated placeholder is close
|
|
288
|
+
to zero. Data Deduplication on Windows Server stores optimized files in a
|
|
289
|
+
chunk store that only a garbage collection job reclaims; those files report
|
|
290
|
+
their own allocation, so `private` overstates what deleting them frees at
|
|
291
|
+
once. Network shares (UNC paths and mapped drives) exit with status 2.
|
|
292
|
+
|
|
293
|
+
## Speed
|
|
294
|
+
|
|
295
|
+
Warm-cache wall time of the bare binary against `du -sk` (macOS, Linux) or
|
|
296
|
+
`robocopy /L` (Windows), 10 runs each. Method, cold-cache runs and every
|
|
297
|
+
runner are in [docs/benchmarks.md](docs/benchmarks.md).
|
|
298
|
+
|
|
299
|
+
| Machine | Filesystem | Tree | real-size | Baseline | Ratio |
|
|
300
|
+
| ------------------------------ | ---------- | ------------------------------------- | --------- | -------- | ----- |
|
|
301
|
+
| Apple M4 Pro, 14 cores | APFS | pnpm `node_modules`, 168,433 files | 1.01 s | 876.4 ms | 1.15x |
|
|
302
|
+
| Apple M4 Pro | APFS | 30,000 files hard-linked from a store | 184.4 ms | 123.8 ms | 1.49x |
|
|
303
|
+
| Apple M4 Pro | APFS | 117,186 small files, 7 levels | 754.7 ms | 551.9 ms | 1.37x |
|
|
304
|
+
| GitHub ubuntu-latest, 4 cores | ext4 | 30,000 hard-linked files | 91.4 ms | 123.8 ms | 0.74x |
|
|
305
|
+
| GitHub ubuntu-latest | ext4 | 117,186 small files | 352.0 ms | 594.5 ms | 0.59x |
|
|
306
|
+
| GitHub ubuntu-latest | btrfs | 117,186 small files | 1.47 s | 619.8 ms | 2.37x |
|
|
307
|
+
| GitHub ubuntu-latest | XFS | 117,186 small files | 747.1 ms | 521.4 ms | 1.43x |
|
|
308
|
+
| GitHub windows-latest, 4 cores | NTFS | 30,000 hard-linked files | 337.8 ms | 135.1 ms | 2.50x |
|
|
309
|
+
| GitHub windows-latest | NTFS | 117,186 small files | 1.66 s | 690.4 ms | 2.40x |
|
|
310
|
+
|
|
311
|
+
The extra time is the per-file question `du` does not ask: the private size
|
|
312
|
+
on APFS, the extent map on btrfs and XFS, and the link count on Windows.
|
|
313
|
+
|
|
314
|
+
## Limitations
|
|
315
|
+
|
|
316
|
+
- Each path is measured on its own and the total is their sum. Two paths that
|
|
317
|
+
hard-link or clone each other's files each report those bytes as shared, so
|
|
318
|
+
the total can be lower than what deleting both would free.
|
|
319
|
+
- The walks stop at 256 nested directories; a deeper directory is skipped and
|
|
320
|
+
counted in `skipped`.
|
|
321
|
+
- Files changed during the walk may be counted in either state.
|
|
322
|
+
- The plain output's sizes are rounded; use `--json` for bytes.
|
|
323
|
+
- On Linux, filesystems other than ext2/3/4, tmpfs, btrfs and XFS exit with
|
|
324
|
+
status 2 even where an answer could be given, for example ZFS without
|
|
325
|
+
block cloning.
|
|
326
|
+
- On Windows the command is `real-size-windows`, not `real-size`, in cmd.exe
|
|
327
|
+
and PowerShell (see [Install](#install)). Yarn classic's Windows launchers
|
|
328
|
+
are not guaranteed.
|
|
329
|
+
- On macOS and Linux, `real-size-windows` is installed too and points at a
|
|
330
|
+
Windows binary that does not run there.
|
|
331
|
+
- On Windows, files compressed by the Windows Overlay Filter (`compact
|
|
332
|
+
/exe`) report their main stream's allocation.
|
|
333
|
+
|
|
334
|
+
## Agent skill
|
|
335
|
+
|
|
336
|
+
The package includes a skill at [`skill/SKILL.md`](skill/SKILL.md) that tells
|
|
337
|
+
coding agents to use `real-size` rather than `du` when the question is how
|
|
338
|
+
much space deleting something would free. Install it with:
|
|
339
|
+
|
|
340
|
+
```bash
|
|
341
|
+
npx skills add appandflow/real-size
|
|
342
|
+
```
|
|
343
|
+
|
|
344
|
+
Or copy the `skill` folder into your agent's skills directory under the name
|
|
345
|
+
`real-size`, for example `~/.claude/skills/real-size/SKILL.md` for Claude
|
|
346
|
+
Code. The installed npm package also contains it at
|
|
347
|
+
`node_modules/real-size/skill/SKILL.md`.
|
|
348
|
+
|
|
349
|
+
## Development
|
|
350
|
+
|
|
351
|
+
Building needs a C11 compiler: `cc` on macOS and Linux (a static glibc for the
|
|
352
|
+
Linux build), MSVC `cl` on Windows.
|
|
353
|
+
|
|
354
|
+
```sh
|
|
355
|
+
npm ci --ignore-scripts
|
|
356
|
+
npm run build
|
|
357
|
+
npm run typecheck
|
|
358
|
+
npm run format:check
|
|
359
|
+
npm test
|
|
360
|
+
```
|
|
361
|
+
|
|
362
|
+
CI builds and tests every platform, then assembles the release tarball with
|
|
363
|
+
`npm run pack:release`. See [RELEASING.md](RELEASING.md).
|
|
364
|
+
|
|
365
|
+
## License
|
|
366
|
+
|
|
367
|
+
MIT
|
package/bin/real-size
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
#!/bin/sh
|
|
2
|
+
true /*
|
|
3
|
+
self=$0
|
|
4
|
+
while [ -h "$self" ]; do
|
|
5
|
+
link=$(readlink "$self")
|
|
6
|
+
case $link in
|
|
7
|
+
/*) self=$link ;;
|
|
8
|
+
*) self=${self%/*}/$link ;;
|
|
9
|
+
esac
|
|
10
|
+
done
|
|
11
|
+
dir=${self%/*}
|
|
12
|
+
[ "$dir" = "$self" ] && dir=.
|
|
13
|
+
case $(uname -sm) in
|
|
14
|
+
"Darwin arm64") target=darwin-arm64 ;;
|
|
15
|
+
"Darwin x86_64") target=darwin-x64 ;;
|
|
16
|
+
"Linux x86_64" | "Linux amd64") target=linux-x64 ;;
|
|
17
|
+
"Linux aarch64" | "Linux arm64") target=linux-arm64 ;;
|
|
18
|
+
*) target= ;;
|
|
19
|
+
esac
|
|
20
|
+
binary=$dir/../prebuilds/$target/real-size
|
|
21
|
+
if [ -z "$target" ] || [ ! -x "$binary" ]; then
|
|
22
|
+
echo "real-size: no prebuilt binary for $(uname -sm)" >&2
|
|
23
|
+
exit 2
|
|
24
|
+
fi
|
|
25
|
+
exec "$binary" "$@"
|
|
26
|
+
: <<'//'
|
|
27
|
+
*/
|
|
28
|
+
const { spawnSync } = process.getBuiltinModule("node:child_process");
|
|
29
|
+
const { existsSync, realpathSync } = process.getBuiltinModule("node:fs");
|
|
30
|
+
const { dirname, join } = process.getBuiltinModule("node:path");
|
|
31
|
+
const target = `${process.platform}-${process.arch}`;
|
|
32
|
+
const name = process.platform === "win32" ? "real-size.exe" : "real-size";
|
|
33
|
+
const binary = join(dirname(realpathSync(process.argv[1])), "..", "prebuilds", target, name);
|
|
34
|
+
if (!existsSync(binary)) {
|
|
35
|
+
console.error(`real-size: no prebuilt binary for ${target}`);
|
|
36
|
+
process.exit(2);
|
|
37
|
+
}
|
|
38
|
+
const result = spawnSync(binary, process.argv.slice(2), { stdio: "inherit" });
|
|
39
|
+
if (result.error) {
|
|
40
|
+
console.error(`real-size: ${result.error.message}`);
|
|
41
|
+
process.exit(2);
|
|
42
|
+
}
|
|
43
|
+
if (result.signal) process.kill(process.pid, result.signal);
|
|
44
|
+
process.exit(result.status ?? 1);
|
|
45
|
+
//
|
package/index.d.mts
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
export interface TreeSize {
|
|
2
|
+
/** Bytes the regular files occupy on disk, each file counted once. */
|
|
3
|
+
allocated: number;
|
|
4
|
+
/** Bytes deleting the tree would free. A lower bound when `exact` is false. */
|
|
5
|
+
private: number;
|
|
6
|
+
/** Regular files, each counted once however many links it has in the tree. */
|
|
7
|
+
files: number;
|
|
8
|
+
/**
|
|
9
|
+
* False when `private` is a lower bound: some counted file shares blocks with another file, or some entries could
|
|
10
|
+
* not be read (`skipped` above 0).
|
|
11
|
+
*/
|
|
12
|
+
exact: boolean;
|
|
13
|
+
/**
|
|
14
|
+
* Entries the walk could not read, for example a directory without read permission, and left out of every other
|
|
15
|
+
* field. When above 0 every size is a lower bound; the binary names the entries on stderr and exits with status 1.
|
|
16
|
+
* Entries deleted during the walk are not counted here.
|
|
17
|
+
*/
|
|
18
|
+
skipped: number;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface PathSize extends TreeSize {
|
|
22
|
+
/** The path as it was passed. */
|
|
23
|
+
path: string;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export interface RealSizeResult {
|
|
27
|
+
paths: PathSize[];
|
|
28
|
+
/** The sum of `paths`, each measured on its own. */
|
|
29
|
+
total: TreeSize;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface RealSizeOptions {
|
|
33
|
+
/** Aborting kills the walk and rejects with the signal's reason. */
|
|
34
|
+
signal?: AbortSignal;
|
|
35
|
+
/** Kills the walk and rejects with a `timeout` error after this many milliseconds. */
|
|
36
|
+
timeoutMs?: number;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export type RealSizeErrorCode = "unsupported" | "timeout" | "failed";
|
|
40
|
+
|
|
41
|
+
export declare class RealSizeError extends Error {
|
|
42
|
+
constructor(
|
|
43
|
+
code: RealSizeErrorCode,
|
|
44
|
+
message: string,
|
|
45
|
+
options?: { cause?: unknown; exitCode?: number | null },
|
|
46
|
+
);
|
|
47
|
+
readonly name: "RealSizeError";
|
|
48
|
+
/**
|
|
49
|
+
* `unsupported`: no prebuilt binary for this platform, or the filesystem cannot report the bytes deleting the path
|
|
50
|
+
* would free (exit status 2). `timeout`: `timeoutMs` ran out. `failed`: a usage error or a path that cannot be read (exit
|
|
51
|
+
* status 1 without a result), or the binary could not run.
|
|
52
|
+
*/
|
|
53
|
+
readonly code: RealSizeErrorCode;
|
|
54
|
+
/** The binary's exit status, null when it did not exit on its own. */
|
|
55
|
+
readonly exitCode: number | null;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Measures each directory in its own walk, run in a child process. Resolves when every path was measured, including
|
|
60
|
+
* when some entries below a path could not be read (`total.skipped` above 0). Rejects when a path itself cannot be
|
|
61
|
+
* read.
|
|
62
|
+
*/
|
|
63
|
+
export declare function realSize(
|
|
64
|
+
paths: string | readonly string[],
|
|
65
|
+
options?: RealSizeOptions,
|
|
66
|
+
): Promise<RealSizeResult>;
|
|
67
|
+
|
|
68
|
+
/** The absolute path of the prebuilt binary for this platform, or null when the package has none. */
|
|
69
|
+
export declare function binaryPath(): string | null;
|
package/index.mjs
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import { spawn } from "node:child_process";
|
|
2
|
+
import { existsSync } from "node:fs";
|
|
3
|
+
import { fileURLToPath } from "node:url";
|
|
4
|
+
|
|
5
|
+
export class RealSizeError extends Error {
|
|
6
|
+
/**
|
|
7
|
+
* @param {"unsupported" | "timeout" | "failed"} code
|
|
8
|
+
* @param {string} message
|
|
9
|
+
* @param {{ cause?: unknown; exitCode?: number | null }} [options]
|
|
10
|
+
*/
|
|
11
|
+
constructor(code, message, options = {}) {
|
|
12
|
+
super(message, { cause: options.cause });
|
|
13
|
+
this.name = "RealSizeError";
|
|
14
|
+
this.code = code;
|
|
15
|
+
this.exitCode = options.exitCode ?? null;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** @returns {string | null} */
|
|
20
|
+
export function binaryPath() {
|
|
21
|
+
const name = process.platform === "win32" ? "real-size.exe" : "real-size";
|
|
22
|
+
const file = fileURLToPath(
|
|
23
|
+
new URL(
|
|
24
|
+
`./prebuilds/${process.platform}-${process.arch}/${name}`,
|
|
25
|
+
import.meta.url,
|
|
26
|
+
),
|
|
27
|
+
);
|
|
28
|
+
return existsSync(file) ? file : null;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* @param {string | readonly string[]} paths
|
|
33
|
+
* @param {{ signal?: AbortSignal; timeoutMs?: number }} [options]
|
|
34
|
+
* @returns {Promise<import("./index.d.mts").RealSizeResult>}
|
|
35
|
+
*/
|
|
36
|
+
export function realSize(paths, options = {}) {
|
|
37
|
+
const { signal, timeoutMs } = options;
|
|
38
|
+
const list = typeof paths === "string" ? [paths] : [...paths];
|
|
39
|
+
return new Promise((resolve, reject) => {
|
|
40
|
+
if (signal?.aborted) return reject(signal.reason);
|
|
41
|
+
if (list.length === 0)
|
|
42
|
+
return reject(new TypeError("realSize needs at least one path"));
|
|
43
|
+
const binary = binaryPath();
|
|
44
|
+
if (!binary)
|
|
45
|
+
return reject(
|
|
46
|
+
new RealSizeError(
|
|
47
|
+
"unsupported",
|
|
48
|
+
`real-size has no prebuilt binary for ${process.platform}-${process.arch}`,
|
|
49
|
+
),
|
|
50
|
+
);
|
|
51
|
+
const child = spawn(binary, ["--json", "--", ...list], {
|
|
52
|
+
stdio: ["ignore", "pipe", "pipe"],
|
|
53
|
+
windowsHide: true,
|
|
54
|
+
});
|
|
55
|
+
let stdout = "";
|
|
56
|
+
let stderr = "";
|
|
57
|
+
child.stdout.setEncoding("utf8").on("data", (chunk) => (stdout += chunk));
|
|
58
|
+
child.stderr.setEncoding("utf8").on("data", (chunk) => (stderr += chunk));
|
|
59
|
+
let settled = false;
|
|
60
|
+
/** @type {NodeJS.Timeout | undefined} */
|
|
61
|
+
let timer;
|
|
62
|
+
const onAbort = () => {
|
|
63
|
+
child.kill("SIGKILL");
|
|
64
|
+
finish(() => reject(signal?.reason));
|
|
65
|
+
};
|
|
66
|
+
/** @param {() => void} settle */
|
|
67
|
+
const finish = (settle) => {
|
|
68
|
+
if (settled) return;
|
|
69
|
+
settled = true;
|
|
70
|
+
clearTimeout(timer);
|
|
71
|
+
signal?.removeEventListener("abort", onAbort);
|
|
72
|
+
settle();
|
|
73
|
+
};
|
|
74
|
+
if (timeoutMs !== undefined)
|
|
75
|
+
timer = setTimeout(() => {
|
|
76
|
+
child.kill("SIGKILL");
|
|
77
|
+
finish(() =>
|
|
78
|
+
reject(
|
|
79
|
+
new RealSizeError(
|
|
80
|
+
"timeout",
|
|
81
|
+
`real-size did not finish within ${timeoutMs} ms`,
|
|
82
|
+
),
|
|
83
|
+
),
|
|
84
|
+
);
|
|
85
|
+
}, timeoutMs);
|
|
86
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
87
|
+
child.on("error", (error) =>
|
|
88
|
+
finish(() =>
|
|
89
|
+
reject(new RealSizeError("failed", error.message, { cause: error })),
|
|
90
|
+
),
|
|
91
|
+
);
|
|
92
|
+
child.on("close", (exitCode, exitSignal) =>
|
|
93
|
+
finish(() => {
|
|
94
|
+
const message = (stderr.trim().split(/\r?\n/).at(-1) ?? "").replace(
|
|
95
|
+
/^real-size: /,
|
|
96
|
+
"",
|
|
97
|
+
);
|
|
98
|
+
const partial = exitCode === 1 ? parsePartial(stdout) : null;
|
|
99
|
+
if (partial) resolve(partial);
|
|
100
|
+
else if (exitCode === 0) {
|
|
101
|
+
try {
|
|
102
|
+
resolve(JSON.parse(stdout));
|
|
103
|
+
} catch (error) {
|
|
104
|
+
reject(
|
|
105
|
+
new RealSizeError("failed", "real-size printed invalid JSON", {
|
|
106
|
+
cause: error,
|
|
107
|
+
exitCode,
|
|
108
|
+
}),
|
|
109
|
+
);
|
|
110
|
+
}
|
|
111
|
+
} else if (exitCode === 2)
|
|
112
|
+
reject(new RealSizeError("unsupported", message, { exitCode }));
|
|
113
|
+
else
|
|
114
|
+
reject(
|
|
115
|
+
new RealSizeError(
|
|
116
|
+
"failed",
|
|
117
|
+
message || `real-size exited with ${exitCode ?? exitSignal}`,
|
|
118
|
+
{ exitCode },
|
|
119
|
+
),
|
|
120
|
+
);
|
|
121
|
+
}),
|
|
122
|
+
);
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Exit status 1 with a document on stdout means some entries could not be read and were skipped.
|
|
128
|
+
* @param {string} stdout
|
|
129
|
+
* @returns {import("./index.d.mts").RealSizeResult | null}
|
|
130
|
+
*/
|
|
131
|
+
function parsePartial(stdout) {
|
|
132
|
+
try {
|
|
133
|
+
const result = JSON.parse(stdout);
|
|
134
|
+
return result?.total?.skipped > 0 ? result : null;
|
|
135
|
+
} catch {
|
|
136
|
+
return null;
|
|
137
|
+
}
|
|
138
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,63 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "real-size",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "The bytes deleting a folder would actually free, on APFS clones, hard links and reflinks",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"engines": {
|
|
7
|
+
"node": "^20.19.4 || >=22.12.0"
|
|
8
|
+
},
|
|
9
|
+
"os": [
|
|
10
|
+
"darwin",
|
|
11
|
+
"linux",
|
|
12
|
+
"win32"
|
|
13
|
+
],
|
|
14
|
+
"bin": {
|
|
15
|
+
"real-size": "bin/real-size",
|
|
16
|
+
"real-size-windows": "prebuilds/win32-x64/real-size.exe"
|
|
17
|
+
},
|
|
18
|
+
"exports": {
|
|
19
|
+
".": {
|
|
20
|
+
"types": "./index.d.mts",
|
|
21
|
+
"import": "./index.mjs"
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
"files": [
|
|
25
|
+
"bin",
|
|
26
|
+
"index.mjs",
|
|
27
|
+
"index.d.mts",
|
|
28
|
+
"prebuilds",
|
|
29
|
+
"skill",
|
|
30
|
+
"LICENSE",
|
|
31
|
+
"README.md"
|
|
32
|
+
],
|
|
33
|
+
"keywords": [
|
|
34
|
+
"disk-usage",
|
|
35
|
+
"du",
|
|
36
|
+
"apfs",
|
|
37
|
+
"clone",
|
|
38
|
+
"hard-link",
|
|
39
|
+
"reflink",
|
|
40
|
+
"btrfs",
|
|
41
|
+
"xfs",
|
|
42
|
+
"refs"
|
|
43
|
+
],
|
|
44
|
+
"repository": {
|
|
45
|
+
"type": "git",
|
|
46
|
+
"url": "git+https://github.com/appandflow/real-size.git"
|
|
47
|
+
},
|
|
48
|
+
"license": "MIT",
|
|
49
|
+
"scripts": {
|
|
50
|
+
"build": "node scripts/build.mjs",
|
|
51
|
+
"test": "node --test --test-concurrency=1 test/cli.test.mjs test/errors.test.mjs test/api.test.mjs test/launcher.test.mjs test/darwin.test.mjs test/linux.test.mjs test/windows.test.mjs test/package.test.mjs",
|
|
52
|
+
"test:package": "node scripts/package-smoke.mjs",
|
|
53
|
+
"pack:release": "node scripts/pack-release.mjs",
|
|
54
|
+
"typecheck": "tsc",
|
|
55
|
+
"format": "prettier --write .",
|
|
56
|
+
"format:check": "prettier --check ."
|
|
57
|
+
},
|
|
58
|
+
"devDependencies": {
|
|
59
|
+
"@types/node": "^22.0.0",
|
|
60
|
+
"prettier": "^3.0.0",
|
|
61
|
+
"typescript": "^5.0.0"
|
|
62
|
+
}
|
|
63
|
+
}
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
package/skill/SKILL.md
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: real-size
|
|
3
|
+
description: Use real-size to measure how much disk space a folder really uses or how much deleting it would free. Use it when deciding what to delete to free space, when sizing node_modules, build outputs, caches, worktrees or clones on macOS (APFS clones), Linux (hard links, btrfs and XFS reflinks) or Windows (NTFS hard links, ReFS block clones), and any time `du` or `du -sh` is about to be used to answer "how much will I get back".
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# real-size
|
|
7
|
+
|
|
8
|
+
`du` adds up every file's blocks. On a copy-on-write or hard-linked tree that is the wrong answer to "what would deleting this free": an APFS clone, a reflink copy, a ReFS block clone or a pnpm store hard link shares its blocks with a file elsewhere, and deleting it frees nothing. Use `real-size` for that question. `du` stays the right tool for apparent size or for how much a tree occupies regardless of sharing.
|
|
9
|
+
|
|
10
|
+
```bash
|
|
11
|
+
npx real-size <path>... # one line per path, plus a total
|
|
12
|
+
npx real-size --json <path>... # one JSON document: { paths: [...], total }
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
On Windows in cmd.exe or PowerShell the command is `real-size-windows` (`npx --package real-size real-size-windows <path>`).
|
|
16
|
+
|
|
17
|
+
Fields:
|
|
18
|
+
|
|
19
|
+
- `allocated`: bytes the tree's files occupy on disk, each file counted once.
|
|
20
|
+
- `private`: bytes deleting the tree would free. Report this one as reclaimable.
|
|
21
|
+
- `files`: regular files counted.
|
|
22
|
+
- `exact`: `false` means `private` is a lower bound: some file shares blocks with another file, possibly one inside the same tree, or some entries could not be read. Deleting the whole tree may free more. Say "at least".
|
|
23
|
+
- `skipped`: entries that could not be read (no permission, an I/O error, nesting deeper than 256 directories) and were left out of every number.
|
|
24
|
+
|
|
25
|
+
Exit status 1 with a result on stdout means some entries were skipped: stderr names each one with its reason (the first ten, then a count). Still use the result: report `private` as "at least", give the number of entries that could not be read and the paths stderr names, and suggest rerunning with the needed permissions if the user wants the full figure. Exit status 1 with nothing on stdout is a usage error or a path that cannot be read at all, with the reason on stderr.
|
|
26
|
+
|
|
27
|
+
Exit status 2 means the filesystem cannot report this (for example NFS, overlayfs, a network share, or a non-APFS volume on macOS). Then say real-size cannot tell on this filesystem, fall back to `du -sh`, and present that number as an upper bound.
|
|
28
|
+
|
|
29
|
+
Entries deleted while the walk runs are left out without a message; they free nothing.
|
|
30
|
+
|
|
31
|
+
Example, a pnpm worktree's `node_modules` on macOS:
|
|
32
|
+
|
|
33
|
+
```
|
|
34
|
+
$ du -sh node_modules 4.4G node_modules
|
|
35
|
+
$ npx real-size node_modules node_modules: at least 90.3 MB private, 4.8 GB allocated, 168433 files
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
Deleting it gets back about 90 MB, not 4.4 GB. Symbolic links and Windows junctions are never followed. On Windows, NTFS hard links are exact; ReFS and Dev Drive block clones are read from the volume's per-extent reference counts and give `exact: false`, and network shares exit 2.
|