@yaks/file 0.0.0-stage → 0.2.36

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.
Files changed (54) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +121 -2
  3. package/_dist/blobs.d.ts +16 -0
  4. package/_dist/blobs.d.ts.map +1 -0
  5. package/_dist/io.d.ts +148 -0
  6. package/_dist/io.d.ts.map +1 -0
  7. package/_dist/local.d.ts +18 -0
  8. package/_dist/local.d.ts.map +1 -0
  9. package/_dist/mod.d.ts +27 -0
  10. package/_dist/mod.d.ts.map +1 -0
  11. package/_dist/occupancy.d.ts +207 -0
  12. package/_dist/occupancy.d.ts.map +1 -0
  13. package/_dist/service.d.ts +31 -0
  14. package/_dist/service.d.ts.map +1 -0
  15. package/_dist/views.d.ts +24 -0
  16. package/_dist/views.d.ts.map +1 -0
  17. package/_dist/vocab.d.ts +9 -0
  18. package/_dist/vocab.d.ts.map +1 -0
  19. package/blobs.js +47 -0
  20. package/blobs.js.map +1 -0
  21. package/blobs.ts +52 -0
  22. package/deno.json +1 -0
  23. package/io.js +361 -0
  24. package/io.js.map +1 -0
  25. package/io.ts +528 -0
  26. package/local.js +105 -0
  27. package/local.js.map +1 -0
  28. package/local.ts +91 -0
  29. package/measure.js +88 -0
  30. package/measure.js.map +1 -0
  31. package/measure.ts +109 -0
  32. package/mod.js +86 -0
  33. package/mod.js.map +1 -0
  34. package/mod.ts +84 -0
  35. package/occupancy.js +256 -0
  36. package/occupancy.js.map +1 -0
  37. package/occupancy.ts +341 -0
  38. package/package.json +54 -4
  39. package/page.js +296 -0
  40. package/page.js.map +1 -0
  41. package/page.ts +444 -0
  42. package/service.js +116 -0
  43. package/service.js.map +1 -0
  44. package/service.ts +155 -0
  45. package/upkeep.js +52 -0
  46. package/upkeep.js.map +1 -0
  47. package/upkeep.ts +78 -0
  48. package/views.js +27 -0
  49. package/views.js.map +1 -0
  50. package/views.ts +25 -0
  51. package/vocab.js +19 -0
  52. package/vocab.js.map +1 -0
  53. package/vocab.json +163 -0
  54. package/vocab.ts +20 -0
package/occupancy.js ADDED
@@ -0,0 +1,256 @@
1
+ /**
2
+ * What fills a volume, as a look at it records it:
3
+ * `occupancy{volume, day, at, size, used, free, counted, paths}`, one row a
4
+ * volume a UTC day. Here is reading a run
5
+ * of them the way the Disks page does: what a directory holds, what is too
6
+ * small to list, which directories are drawn open, the largest files, the
7
+ * change between two looks, and which file is the graph's own. Pure; looking
8
+ * is ./service.ts, drawing ./page.ts.
9
+ *
10
+ * @module
11
+ */ import { identityEid } from "@yaks/graph/identity";
12
+ import { fileOf, placed, size } from "./mod.js";
13
+ /// occupancyOf('v', '2026-10-21') == occupancyOf('v', '2026-10-21') -> true
14
+ /// occupancyOf('v', '2026-10-21') == occupancyOf('v', '2026-10-22') -> false
15
+ /** The entity a volume's day is: the eid `occupancy{volume, day}` derives. */ export let occupancyOf = (volume, day)=>identityEid('occupancy', [
16
+ volume,
17
+ day
18
+ ]);
19
+ /** A bundle's `occupancy`, if it has one. */ export let occupancyIn = (b)=>b?.occupancy;
20
+ /** The share of a volume a path takes to be listed: a thousandth. */ export let SHARE = 1 / 1000;
21
+ /// listed(190e9) -> 190000000
22
+ /** The fewest bytes a path on a volume of `size` bytes holds to be listed. */ export let listed = (size)=>Math.ceil(size * SHARE);
23
+ /**
24
+ * What `du -0 -B1` printed: each path it listed, with its bytes.
25
+ *
26
+ * ```ts
27
+ * import { entries } from './occupancy.ts'
28
+ * import { equal } from '@yaks/testing'
29
+ *
30
+ * equal(entries('300\t/home/a.db\x00900\t/home\x00'), [[300, '/home/a.db'], [900, '/home']])
31
+ * ```
32
+ */ export let entries = (out)=>out.split('\0').flatMap((line)=>{
33
+ let tab = line.indexOf('\t');
34
+ return tab > 0 ? [
35
+ [
36
+ +line.slice(0, tab),
37
+ line.slice(tab + 1)
38
+ ]
39
+ ] : [];
40
+ });
41
+ /**
42
+ * A walk under `mount`, from what `du` listed and which of those paths are
43
+ * directories: the bytes counted under the mount, and each path below it from
44
+ * the mount, a directory's ending in `/`.
45
+ *
46
+ * ```ts
47
+ * import { walked } from './occupancy.ts'
48
+ * import { equal } from '@yaks/testing'
49
+ *
50
+ * let found: [number, string][] = [[300, '/home/a.db'], [900, '/home'], [1000, '/']]
51
+ * equal(walked(found, '/', new Set(['/home', '/'])), {
52
+ * counted: 1000,
53
+ * paths: { 'home/a.db': 300, 'home/': 900 },
54
+ * })
55
+ * ```
56
+ */ export let walked = (found, mount, dirs)=>{
57
+ let walk = {
58
+ counted: 0,
59
+ paths: {}
60
+ };
61
+ for (let [bytes, absolute] of found){
62
+ let at = placed([
63
+ mount
64
+ ], absolute);
65
+ if (!at) continue;
66
+ if (!at.path) walk.counted = bytes;
67
+ else walk.paths[at.path + (dirs.has(absolute) ? '/' : '')] = bytes;
68
+ }
69
+ return walk;
70
+ };
71
+ /// parent('home/yaks/') -> 'home/'
72
+ /// parent('home/') -> ''
73
+ /// parent('home/yaks/yak.db') -> 'home/yaks/'
74
+ /** The directory a path is listed in; `''` is the mount. */ export let parent = (path)=>path.replace(/[^/]+\/?$/, '');
75
+ /// named('home/yaks/') -> 'yaks/'
76
+ /// named('home/yaks/yak.db') -> 'yak.db'
77
+ /** A path's own name, a directory's ending in `/`. */ export let named = (path)=>path.slice(parent(path).length);
78
+ /// directory('home/') -> true
79
+ /// directory('home/a.db') -> false
80
+ /** Whether a listed path is a directory. */ export let directory = (path)=>path.endsWith('/');
81
+ /** The bytes a path holds: `''`, the mount, holds what the walk counted. */ export let held = (o, path)=>path == '' ? o.counted : o.paths[path] ?? 0;
82
+ let largest = (a, b)=>b[1] - a[1];
83
+ /**
84
+ * What a directory holds: the paths listed in it, largest first, and the
85
+ * bytes of everything in it too small to list.
86
+ *
87
+ * ```ts
88
+ * import { within } from './occupancy.ts'
89
+ * import { equal } from '@yaks/testing'
90
+ *
91
+ * let o = { counted: 1000, paths: { 'home/': 900, 'home/a.db': 300, 'home/b/': 500, 'usr/': 80 } }
92
+ * equal(within(o, ''), { listed: [['home/', 900], ['usr/', 80]], rest: 20 })
93
+ * equal(within(o, 'home/'), { listed: [['home/b/', 500], ['home/a.db', 300]], rest: 100 })
94
+ * ```
95
+ */ export let within = (o, dir)=>{
96
+ let listed = Object.entries(o.paths).filter(([p])=>p != dir && parent(p) == dir).toSorted(largest);
97
+ let rest = held(o, dir) - listed.reduce((n, [, b])=>n + b, 0);
98
+ return {
99
+ listed,
100
+ rest: Math.max(0, rest)
101
+ };
102
+ };
103
+ /** The share of what is used a directory holds to be drawn open by itself. */ export let OPEN = 1 / 10;
104
+ /**
105
+ * Whether a directory is drawn open: as the person left it, or else when it
106
+ * holds a tenth of what is used.
107
+ *
108
+ * ```ts
109
+ * import { opens } from './occupancy.ts'
110
+ * import { equal } from '@yaks/testing'
111
+ *
112
+ * equal([opens(undefined, 70, 600), opens(undefined, 50, 600)], [true, false])
113
+ * equal([opens(false, 500, 600), opens(true, 5, 600)], [false, true])
114
+ * ```
115
+ */ export let opens = (chosen, bytes, used)=>chosen ?? bytes >= used * OPEN;
116
+ /**
117
+ * A change in bytes as read: blank where there is none, `new` where the older
118
+ * look did not list the path.
119
+ *
120
+ * ```ts
121
+ * import { change } from './occupancy.ts'
122
+ * import { equal } from '@yaks/testing'
123
+ *
124
+ * equal([change(undefined, false), change(undefined, true), change(0, true)], ['', 'new', ''])
125
+ * equal([change(1e8, true), change(-2e9, true)], ['+100 MB', '−2 GB'])
126
+ * ```
127
+ */ export let change = (n, compared)=>n == null ? compared ? 'new' : '' : n ? `${n > 0 ? '+' : ''}${size(n)}` : '';
128
+ /**
129
+ * The bytes of a path that are free pages, where it is the file the graph
130
+ * keeps its pages in, as the store's day's measurement names it
131
+ * (`kept{file, free}`, @yaks/journal).
132
+ *
133
+ * ```ts
134
+ * import { pagesFree } from './occupancy.ts'
135
+ * import { fileOf } from './mod.ts'
136
+ * import { equal } from '@yaks/testing'
137
+ *
138
+ * let kept = { file: fileOf('v', 'home/.yak/yak.db'), free: 3e9 }
139
+ * equal(pagesFree(kept, 'v', 'home/.yak/yak.db'), 3e9)
140
+ * equal(pagesFree(kept, 'v', 'tmp/copy/yak.db'), undefined)
141
+ * ```
142
+ */ export let pagesFree = (kept, volume, path)=>kept?.file && kept.file == fileOf(volume, path) ? kept.free : undefined;
143
+ /**
144
+ * A volume's tree as lines, from `dir` down: each path listed in it, largest
145
+ * first, an open directory's own lines beneath it, and last what the
146
+ * directory holds too small to list. `open` says which directories are.
147
+ *
148
+ * ```ts
149
+ * import { lines } from './occupancy.ts'
150
+ * import { equal } from '@yaks/testing'
151
+ *
152
+ * let o = { counted: 100, paths: { 'home/': 90, 'home/a.db': 60, 'usr/': 5 } }
153
+ * equal(lines(o, (path) => path == 'home/'), [
154
+ * { path: 'home/', depth: 0, bytes: 90, open: true },
155
+ * { path: 'home/a.db', depth: 1, bytes: 60 },
156
+ * { path: 'home/', depth: 1, bytes: 30, rest: true },
157
+ * { path: 'usr/', depth: 0, bytes: 5, open: false },
158
+ * { path: '', depth: 0, bytes: 5, rest: true },
159
+ * ])
160
+ * ```
161
+ */ export let lines = (o, open, dir = '', depth = 0)=>{
162
+ let { listed, rest } = within(o, dir);
163
+ return [
164
+ ...listed.flatMap(([path, bytes])=>{
165
+ if (!directory(path)) return [
166
+ {
167
+ path,
168
+ depth,
169
+ bytes
170
+ }
171
+ ];
172
+ let shown = open(path, bytes);
173
+ return [
174
+ {
175
+ path,
176
+ depth,
177
+ bytes,
178
+ open: shown
179
+ },
180
+ ...shown ? lines(o, open, path, depth + 1) : []
181
+ ];
182
+ }),
183
+ ...rest ? [
184
+ {
185
+ path: dir,
186
+ depth,
187
+ bytes: rest,
188
+ rest: true
189
+ }
190
+ ] : []
191
+ ];
192
+ };
193
+ /**
194
+ * The largest files listed, largest first.
195
+ *
196
+ * ```ts
197
+ * import { files } from './occupancy.ts'
198
+ * import { equal } from '@yaks/testing'
199
+ *
200
+ * equal(files({ counted: 9, paths: { 'a/': 9, 'a/x': 2, 'a/y': 5 } }, 1), [['a/y', 5]])
201
+ * ```
202
+ */ export let files = (o, n)=>Object.entries(o.paths).filter(([p])=>!directory(p)).toSorted(largest).slice(0, n);
203
+ /// uncounted({ used: 100, counted: 70 }) -> 30
204
+ /** The bytes in use the walk did not count: in directories it could not
205
+ * read, or kept by the filesystem itself. */ export let uncounted = (o)=>Math.max(0, o.used - o.counted);
206
+ /**
207
+ * How much a path grew from an older look to a newer one; undefined where
208
+ * the older look did not list it, so nothing is known of what it held then.
209
+ *
210
+ * ```ts
211
+ * import { grew } from './occupancy.ts'
212
+ * import { equal } from '@yaks/testing'
213
+ *
214
+ * let older = { counted: 10, paths: { 'a/': 4 } }
215
+ * let newer = { counted: 16, paths: { 'a/': 9, 'b/': 3 } }
216
+ * equal([grew(older, newer, ''), grew(older, newer, 'a/'), grew(older, newer, 'b/')], [6, 5, undefined])
217
+ * ```
218
+ */ export let grew = (older, newer, path)=>older && (path == '' || path in older.paths) ? held(newer, path) - held(older, path) : undefined;
219
+ let DAY = 864e5;
220
+ /**
221
+ * The look to compare the newest against, from looks newest first: the
222
+ * newest at least `days` older than it, or else the oldest there is.
223
+ *
224
+ * ```ts
225
+ * import { since } from './occupancy.ts'
226
+ * import { equal } from '@yaks/testing'
227
+ *
228
+ * let at = (day: string) => ({ at: `${day}T04:00:00Z` })
229
+ * let looks = [at('2026-10-21'), at('2026-10-15'), at('2026-10-14'), at('2026-10-01')]
230
+ * equal(since(looks, 7), at('2026-10-14'))
231
+ * equal(since(looks.slice(0, 2), 7), at('2026-10-15'))
232
+ * equal(since(looks.slice(0, 1), 7), undefined)
233
+ * ```
234
+ */ export let since = (looks, days)=>{
235
+ let [newest, ...older] = looks;
236
+ if (!newest) return undefined;
237
+ let by = Date.parse(newest.at) - days * DAY;
238
+ return older.find((l)=>Date.parse(l.at) <= by) ?? older.at(-1);
239
+ };
240
+ /**
241
+ * `of`'s growth a day between an older look and a newer one.
242
+ *
243
+ * ```ts
244
+ * import { perDay } from './occupancy.ts'
245
+ * import { equal } from '@yaks/testing'
246
+ *
247
+ * let older = { at: '2026-10-14T04:00:00Z', used: 100 }
248
+ * let newer = { at: '2026-10-21T04:00:00Z', used: 800 }
249
+ * equal(perDay(older, newer, (o) => o.used), 100)
250
+ * equal(perDay(newer, newer, (o) => o.used), undefined)
251
+ * ```
252
+ */ export let perDay = (older, newer, of)=>{
253
+ let days = (Date.parse(newer.at) - Date.parse(older.at)) / DAY;
254
+ return days > 0 ? (of(newer) - of(older)) / days : undefined;
255
+ };
256
+ //# sourceMappingURL=occupancy.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"occupancy.js","sources":["./occupancy.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;CAUC,GAED,SAAS,WAAW,oCAAyC;AAC7D,SAAS,MAAM,EAAE,MAAM,EAAE,IAAI,mBAAkB;AAoB/C,4EAA4E;AAC5E,6EAA6E;AAC7E,4EAA4E,GAC5E,OAAO,IAAI,cAAc,CAAC,QAAa,MACrC,YAAY,aAAa;IAAC;IAAQ;GAAI,EAAC;AAEzC,2CAA2C,GAC3C,OAAO,IAAI,cAAc,CAAC,IACxB,GAAG,UAAkC;AAEvC,mEAAmE,GACnE,OAAO,IAAI,QAAQ,IAAI,KAAI;AAE3B,8BAA8B;AAC9B,4EAA4E,GAC5E,OAAO,IAAI,SAAS,CAAC,OAAyB,KAAK,IAAI,CAAC,OAAO,OAAM;AAErE;;;;;;;;;CASC,GACD,OAAO,IAAI,UAAU,CAAC,MACpB,IAAI,KAAK,CAAC,MAAM,OAAO,CAAC,CAAC;IACvB,IAAI,MAAM,KAAK,OAAO,CAAC;IACvB,OAAO,MAAM,IAAI;MAAC;QAAC,CAAC,KAAK,KAAK,CAAC,GAAG;QAAM,KAAK,KAAK,CAAC,MAAM;OAAG;KAAC,GAAG,EAAE;EACpE,GAAE;AAEJ;;;;;;;;;;;;;;;CAeC,GACD,OAAO,IAAI,SAAS,CAClB,OACA,OACA;EAEA,IAAI,OAAa;IAAE,SAAS;IAAG,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/C;2CAC2C,GAC3C,OAAO,IAAI,YAAY,CAAC,IACtB,KAAK,GAAG,CAAC,GAAG,EAAE,IAAI,GAAG,EAAE,OAAO,EAAC;AAEjC;;;;;;;;;;;;CAYC,GACD,OAAO,IAAI,OAAO,CAChB,OACA,OACA,OAEA,SAAS,CAAC,QAAQ,MAAM,QAAQ,MAAM,KAAK,IACvC,KAAK,OAAO,QAAQ,KAAK,OAAO,QAChC,UAAS;AAEf,IAAI,MAAM;AAEV;;;;;;;;;;;;;;CAcC,GACD,OAAO,IAAI,QAAQ,CACjB,OACA;EAEA,IAAI,CAAC,QAAQ,GAAG,MAAM,GAAG;EACzB,IAAI,CAAC,QAAQ,OAAO;EACpB,IAAI,KAAK,KAAK,KAAK,CAAC,OAAO,EAAE,IAAI,OAAO;EACxC,OAAO,MAAM,IAAI,CAAC,CAAC,IAAM,KAAK,KAAK,CAAC,EAAE,EAAE,KAAK,OAAO,MAAM,EAAE,CAAC,CAAC;AAChE,EAAC;AAED;;;;;;;;;;;;CAYC,GACD,OAAO,IAAI,SAAS,CAClB,OACA,OACA;EAEA,IAAI,OAAO,CAAC,KAAK,KAAK,CAAC,MAAM,EAAE,IAAI,KAAK,KAAK,CAAC,MAAM,EAAE,CAAC,IAAI;EAC3D,OAAO,OAAO,IAAI,CAAC,GAAG,SAAS,GAAG,MAAM,IAAI,OAAO;AACrD,EAAC"}
package/occupancy.ts ADDED
@@ -0,0 +1,341 @@
1
+ /**
2
+ * What fills a volume, as a look at it records it:
3
+ * `occupancy{volume, day, at, size, used, free, counted, paths}`, one row a
4
+ * volume a UTC day. Here is reading a run
5
+ * of them the way the Disks page does: what a directory holds, what is too
6
+ * small to list, which directories are drawn open, the largest files, the
7
+ * change between two looks, and which file is the graph's own. Pure; looking
8
+ * is ./service.ts, drawing ./page.ts.
9
+ *
10
+ * @module
11
+ */
12
+ import type { Bundle, Eid } from 'jsr:@yaks/graph@0.2.36'
13
+ import { identityEid } from '@yaks/graph/identity'
14
+ import { fileOf, placed, size } from './mod.js'
15
+
16
+ /** A look at a volume. */
17
+ export type Occupancy = {
18
+ volume: Eid
19
+ day: string
20
+ at: string
21
+ size: number
22
+ used: number
23
+ free: number
24
+ counted: number
25
+ paths: Record<string, number>
26
+ }
27
+
28
+ /** A filesystem this computer keeps files on, at its mount. */
29
+ export type Disk = { mount: string; size: number; used: number; free: number }
30
+
31
+ /** What a walk of a volume counted. */
32
+ export type Walk = Pick<Occupancy, 'counted' | 'paths'>
33
+
34
+ /// occupancyOf('v', '2026-10-21') == occupancyOf('v', '2026-10-21') -> true
35
+ /// occupancyOf('v', '2026-10-21') == occupancyOf('v', '2026-10-22') -> false
36
+ /** The entity a volume's day is: the eid `occupancy{volume, day}` derives. */
37
+ export let occupancyOf = (volume: Eid, day: string): Eid =>
38
+ identityEid('occupancy', [volume, day])
39
+
40
+ /** A bundle's `occupancy`, if it has one. */
41
+ export let occupancyIn = (b?: Bundle): Occupancy | undefined =>
42
+ b?.occupancy as Occupancy | undefined
43
+
44
+ /** The share of a volume a path takes to be listed: a thousandth. */
45
+ export let SHARE = 1 / 1000
46
+
47
+ /// listed(190e9) -> 190000000
48
+ /** The fewest bytes a path on a volume of `size` bytes holds to be listed. */
49
+ export let listed = (size: number): number => Math.ceil(size * SHARE)
50
+
51
+ /**
52
+ * What `du -0 -B1` printed: each path it listed, with its bytes.
53
+ *
54
+ * ```ts
55
+ * import { entries } from './occupancy.ts'
56
+ * import { equal } from '@yaks/testing'
57
+ *
58
+ * equal(entries('300\t/home/a.db\x00900\t/home\x00'), [[300, '/home/a.db'], [900, '/home']])
59
+ * ```
60
+ */
61
+ export let entries = (out: string): [bytes: number, absolute: string][] =>
62
+ out.split('\0').flatMap((line) => {
63
+ let tab = line.indexOf('\t')
64
+ return tab > 0 ? [[+line.slice(0, tab), line.slice(tab + 1)]] : []
65
+ })
66
+
67
+ /**
68
+ * A walk under `mount`, from what `du` listed and which of those paths are
69
+ * directories: the bytes counted under the mount, and each path below it from
70
+ * the mount, a directory's ending in `/`.
71
+ *
72
+ * ```ts
73
+ * import { walked } from './occupancy.ts'
74
+ * import { equal } from '@yaks/testing'
75
+ *
76
+ * let found: [number, string][] = [[300, '/home/a.db'], [900, '/home'], [1000, '/']]
77
+ * equal(walked(found, '/', new Set(['/home', '/'])), {
78
+ * counted: 1000,
79
+ * paths: { 'home/a.db': 300, 'home/': 900 },
80
+ * })
81
+ * ```
82
+ */
83
+ export let walked = (
84
+ found: [bytes: number, absolute: string][],
85
+ mount: string,
86
+ dirs: Set<string>,
87
+ ): Walk => {
88
+ let walk: Walk = { counted: 0, paths: {} }
89
+ for (let [bytes, absolute] of found) {
90
+ let at = placed([mount], absolute)
91
+ if (!at) continue
92
+ if (!at.path) walk.counted = bytes
93
+ else walk.paths[at.path + (dirs.has(absolute) ? '/' : '')] = bytes
94
+ }
95
+ return walk
96
+ }
97
+
98
+ /// parent('home/yaks/') -> 'home/'
99
+ /// parent('home/') -> ''
100
+ /// parent('home/yaks/yak.db') -> 'home/yaks/'
101
+ /** The directory a path is listed in; `''` is the mount. */
102
+ export let parent = (path: string): string => path.replace(/[^/]+\/?$/, '')
103
+
104
+ /// named('home/yaks/') -> 'yaks/'
105
+ /// named('home/yaks/yak.db') -> 'yak.db'
106
+ /** A path's own name, a directory's ending in `/`. */
107
+ export let named = (path: string): string => path.slice(parent(path).length)
108
+
109
+ /// directory('home/') -> true
110
+ /// directory('home/a.db') -> false
111
+ /** Whether a listed path is a directory. */
112
+ export let directory = (path: string): boolean => path.endsWith('/')
113
+
114
+ /** The bytes a path holds: `''`, the mount, holds what the walk counted. */
115
+ export let held = (o: Walk, path: string): number =>
116
+ path == '' ? o.counted : o.paths[path] ?? 0
117
+
118
+ let largest = (a: [string, number], b: [string, number]) => b[1] - a[1]
119
+
120
+ /**
121
+ * What a directory holds: the paths listed in it, largest first, and the
122
+ * bytes of everything in it too small to list.
123
+ *
124
+ * ```ts
125
+ * import { within } from './occupancy.ts'
126
+ * import { equal } from '@yaks/testing'
127
+ *
128
+ * let o = { counted: 1000, paths: { 'home/': 900, 'home/a.db': 300, 'home/b/': 500, 'usr/': 80 } }
129
+ * equal(within(o, ''), { listed: [['home/', 900], ['usr/', 80]], rest: 20 })
130
+ * equal(within(o, 'home/'), { listed: [['home/b/', 500], ['home/a.db', 300]], rest: 100 })
131
+ * ```
132
+ */
133
+ export let within = (
134
+ o: Walk,
135
+ dir: string,
136
+ ): { listed: [string, number][]; rest: number } => {
137
+ let listed = Object.entries(o.paths)
138
+ .filter(([p]) => p != dir && parent(p) == dir)
139
+ .toSorted(largest)
140
+ let rest = held(o, dir) - listed.reduce((n, [, b]) => n + b, 0)
141
+ return { listed, rest: Math.max(0, rest) }
142
+ }
143
+
144
+ /** The share of what is used a directory holds to be drawn open by itself. */
145
+ export let OPEN = 1 / 10
146
+
147
+ /**
148
+ * Whether a directory is drawn open: as the person left it, or else when it
149
+ * holds a tenth of what is used.
150
+ *
151
+ * ```ts
152
+ * import { opens } from './occupancy.ts'
153
+ * import { equal } from '@yaks/testing'
154
+ *
155
+ * equal([opens(undefined, 70, 600), opens(undefined, 50, 600)], [true, false])
156
+ * equal([opens(false, 500, 600), opens(true, 5, 600)], [false, true])
157
+ * ```
158
+ */
159
+ export let opens = (
160
+ chosen: boolean | undefined,
161
+ bytes: number,
162
+ used: number,
163
+ ): boolean => chosen ?? bytes >= used * OPEN
164
+
165
+ /**
166
+ * A change in bytes as read: blank where there is none, `new` where the older
167
+ * look did not list the path.
168
+ *
169
+ * ```ts
170
+ * import { change } from './occupancy.ts'
171
+ * import { equal } from '@yaks/testing'
172
+ *
173
+ * equal([change(undefined, false), change(undefined, true), change(0, true)], ['', 'new', ''])
174
+ * equal([change(1e8, true), change(-2e9, true)], ['+100 MB', '−2 GB'])
175
+ * ```
176
+ */
177
+ export let change = (n: number | undefined, compared: boolean): string =>
178
+ n == null ? (compared ? 'new' : '') : n ? `${n > 0 ? '+' : ''}${size(n)}` : ''
179
+
180
+ /**
181
+ * The bytes of a path that are free pages, where it is the file the graph
182
+ * keeps its pages in, as the store's day's measurement names it
183
+ * (`kept{file, free}`, @yaks/journal).
184
+ *
185
+ * ```ts
186
+ * import { pagesFree } from './occupancy.ts'
187
+ * import { fileOf } from './mod.ts'
188
+ * import { equal } from '@yaks/testing'
189
+ *
190
+ * let kept = { file: fileOf('v', 'home/.yak/yak.db'), free: 3e9 }
191
+ * equal(pagesFree(kept, 'v', 'home/.yak/yak.db'), 3e9)
192
+ * equal(pagesFree(kept, 'v', 'tmp/copy/yak.db'), undefined)
193
+ * ```
194
+ */
195
+ export let pagesFree = (
196
+ kept: { file?: string; free?: number } | undefined,
197
+ volume: Eid,
198
+ path: string,
199
+ ): number | undefined =>
200
+ kept?.file && kept.file == fileOf(volume, path) ? kept.free : undefined
201
+
202
+ /** A line of a volume's tree: a path listed, or what the directory `path`
203
+ * holds too small to list (`rest`); how deep it sits, its bytes, and whether
204
+ * it is a directory drawn open. */
205
+ export type Line = {
206
+ path: string
207
+ depth: number
208
+ bytes: number
209
+ open?: boolean
210
+ rest?: true
211
+ }
212
+
213
+ /**
214
+ * A volume's tree as lines, from `dir` down: each path listed in it, largest
215
+ * first, an open directory's own lines beneath it, and last what the
216
+ * directory holds too small to list. `open` says which directories are.
217
+ *
218
+ * ```ts
219
+ * import { lines } from './occupancy.ts'
220
+ * import { equal } from '@yaks/testing'
221
+ *
222
+ * let o = { counted: 100, paths: { 'home/': 90, 'home/a.db': 60, 'usr/': 5 } }
223
+ * equal(lines(o, (path) => path == 'home/'), [
224
+ * { path: 'home/', depth: 0, bytes: 90, open: true },
225
+ * { path: 'home/a.db', depth: 1, bytes: 60 },
226
+ * { path: 'home/', depth: 1, bytes: 30, rest: true },
227
+ * { path: 'usr/', depth: 0, bytes: 5, open: false },
228
+ * { path: '', depth: 0, bytes: 5, rest: true },
229
+ * ])
230
+ * ```
231
+ */
232
+ export let lines = (
233
+ o: Walk,
234
+ open: (path: string, bytes: number) => boolean,
235
+ dir = '',
236
+ depth = 0,
237
+ ): Line[] => {
238
+ let { listed, rest } = within(o, dir)
239
+ return [
240
+ ...listed.flatMap(([path, bytes]): Line[] => {
241
+ if (!directory(path)) return [{ path, depth, bytes }]
242
+ let shown = open(path, bytes)
243
+ return [
244
+ { path, depth, bytes, open: shown },
245
+ ...shown ? lines(o, open, path, depth + 1) : [],
246
+ ]
247
+ }),
248
+ ...rest ? [{ path: dir, depth, bytes: rest, rest: true as const }] : [],
249
+ ]
250
+ }
251
+
252
+ /**
253
+ * The largest files listed, largest first.
254
+ *
255
+ * ```ts
256
+ * import { files } from './occupancy.ts'
257
+ * import { equal } from '@yaks/testing'
258
+ *
259
+ * equal(files({ counted: 9, paths: { 'a/': 9, 'a/x': 2, 'a/y': 5 } }, 1), [['a/y', 5]])
260
+ * ```
261
+ */
262
+ export let files = (o: Walk, n: number): [string, number][] =>
263
+ Object.entries(o.paths).filter(([p]) => !directory(p)).toSorted(largest)
264
+ .slice(0, n)
265
+
266
+ /// uncounted({ used: 100, counted: 70 }) -> 30
267
+ /** The bytes in use the walk did not count: in directories it could not
268
+ * read, or kept by the filesystem itself. */
269
+ export let uncounted = (o: Pick<Occupancy, 'used' | 'counted'>): number =>
270
+ Math.max(0, o.used - o.counted)
271
+
272
+ /**
273
+ * How much a path grew from an older look to a newer one; undefined where
274
+ * the older look did not list it, so nothing is known of what it held then.
275
+ *
276
+ * ```ts
277
+ * import { grew } from './occupancy.ts'
278
+ * import { equal } from '@yaks/testing'
279
+ *
280
+ * let older = { counted: 10, paths: { 'a/': 4 } }
281
+ * let newer = { counted: 16, paths: { 'a/': 9, 'b/': 3 } }
282
+ * equal([grew(older, newer, ''), grew(older, newer, 'a/'), grew(older, newer, 'b/')], [6, 5, undefined])
283
+ * ```
284
+ */
285
+ export let grew = (
286
+ older: Walk | undefined,
287
+ newer: Walk,
288
+ path: string,
289
+ ): number | undefined =>
290
+ older && (path == '' || path in older.paths)
291
+ ? held(newer, path) - held(older, path)
292
+ : undefined
293
+
294
+ let DAY = 864e5
295
+
296
+ /**
297
+ * The look to compare the newest against, from looks newest first: the
298
+ * newest at least `days` older than it, or else the oldest there is.
299
+ *
300
+ * ```ts
301
+ * import { since } from './occupancy.ts'
302
+ * import { equal } from '@yaks/testing'
303
+ *
304
+ * let at = (day: string) => ({ at: `${day}T04:00:00Z` })
305
+ * let looks = [at('2026-10-21'), at('2026-10-15'), at('2026-10-14'), at('2026-10-01')]
306
+ * equal(since(looks, 7), at('2026-10-14'))
307
+ * equal(since(looks.slice(0, 2), 7), at('2026-10-15'))
308
+ * equal(since(looks.slice(0, 1), 7), undefined)
309
+ * ```
310
+ */
311
+ export let since = <T extends { at: string }>(
312
+ looks: T[],
313
+ days: number,
314
+ ): T | undefined => {
315
+ let [newest, ...older] = looks
316
+ if (!newest) return undefined
317
+ let by = Date.parse(newest.at) - days * DAY
318
+ return older.find((l) => Date.parse(l.at) <= by) ?? older.at(-1)
319
+ }
320
+
321
+ /**
322
+ * `of`'s growth a day between an older look and a newer one.
323
+ *
324
+ * ```ts
325
+ * import { perDay } from './occupancy.ts'
326
+ * import { equal } from '@yaks/testing'
327
+ *
328
+ * let older = { at: '2026-10-14T04:00:00Z', used: 100 }
329
+ * let newer = { at: '2026-10-21T04:00:00Z', used: 800 }
330
+ * equal(perDay(older, newer, (o) => o.used), 100)
331
+ * equal(perDay(newer, newer, (o) => o.used), undefined)
332
+ * ```
333
+ */
334
+ export let perDay = <T extends { at: string }>(
335
+ older: T,
336
+ newer: T,
337
+ of: (o: T) => number,
338
+ ): number | undefined => {
339
+ let days = (Date.parse(newer.at) - Date.parse(older.at)) / DAY
340
+ return days > 0 ? (of(newer) - of(older)) / days : undefined
341
+ }
package/package.json CHANGED
@@ -1,6 +1,56 @@
1
1
  {
2
2
  "name": "@yaks/file",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.2.36",
4
+ "homepage": "https://jsr.io/@yaks/file",
5
+ "type": "module",
6
+ "exports": {
7
+ ".": {
8
+ "types": "./_dist/mod.d.ts",
9
+ "default": "./mod.js"
10
+ },
11
+ "./vocab": {
12
+ "types": "./_dist/vocab.d.ts",
13
+ "default": "./vocab.js"
14
+ },
15
+ "./local": {
16
+ "types": "./_dist/local.d.ts",
17
+ "default": "./local.js"
18
+ },
19
+ "./occupancy": {
20
+ "types": "./_dist/occupancy.d.ts",
21
+ "default": "./occupancy.js"
22
+ },
23
+ "./service": {
24
+ "types": "./_dist/service.d.ts",
25
+ "default": "./service.js"
26
+ },
27
+ "./views": {
28
+ "types": "./_dist/views.d.ts",
29
+ "default": "./views.js"
30
+ },
31
+ "./io": {
32
+ "types": "./_dist/io.d.ts",
33
+ "default": "./io.js"
34
+ },
35
+ "./blobs": {
36
+ "types": "./_dist/blobs.d.ts",
37
+ "default": "./blobs.js"
38
+ }
39
+ },
40
+ "license": "Apache-2.0",
41
+ "dependencies": {
42
+ "@yaks/blob": "0.2.36",
43
+ "@yaks/fp": "0.2.36",
44
+ "@yaks/graph": "0.2.36",
45
+ "@yaks/inspect": "0.2.36",
46
+ "@yaks/query": "0.2.36",
47
+ "@yaks/ui": "0.2.36",
48
+ "@yaks/ux": "0.2.36",
49
+ "@yaks/vocab": "0.2.36",
50
+ "@types/react": "19.3.0",
51
+ "lucide": "1.31.0",
52
+ "react": "19.3.0"
53
+ },
54
+ "description": "Files as entities: a path read in a root (a repository's tree, or a volume a machine mounts), the lines of a file an entity sits at, and what fills each volume of a machine, day by day.",
55
+ "repository": "https://github.com/yak-sh/yak.git"
56
+ }