@celljs/ta-moves 4.35.0 → 4.36.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.
Files changed (2) hide show
  1. package/host/index.mjs +240 -26
  2. package/package.json +4 -4
package/host/index.mjs CHANGED
@@ -1,8 +1,44 @@
1
1
  // web-src/host.ts
2
2
  import { spawn } from "node:child_process";
3
3
  import { randomBytes } from "node:crypto";
4
- import { appendFileSync, existsSync, mkdirSync as mkdirSync2, readFileSync, renameSync as renameSync2, rmSync as rmSync2, statSync, writeFileSync as writeFileSync2 } from "node:fs";
5
- import { delimiter, join as join2 } from "node:path";
4
+ import { appendFileSync as appendFileSync2, existsSync as existsSync2, mkdirSync as mkdirSync3, readFileSync, renameSync as renameSync3, rmSync as rmSync3, statSync as statSync2, writeFileSync as writeFileSync2 } from "node:fs";
5
+ import { delimiter, join as join3 } from "node:path";
6
+
7
+ // ../../packages/core/esm/node/fs/file-retention.js
8
+ import { appendFileSync, chmodSync as chmodSync2, existsSync, mkdirSync as mkdirSync2, renameSync as renameSync2, rmSync as rmSync2, statSync } from "node:fs";
9
+ import { appendFile, chmod as chmod2, mkdir as mkdir2, readdir, rename as rename2, rm as rm2, stat } from "node:fs/promises";
10
+ import { dirname as dirname2, join as join2 } from "node:path";
11
+
12
+ // ../../packages/core/esm/common/utils/retention-util.js
13
+ var RetentionUtil;
14
+ (function(RetentionUtil2) {
15
+ RetentionUtil2.DAY_MS = 24 * 60 * 60 * 1000;
16
+ function select(items, limits, now = Date.now()) {
17
+ var _a;
18
+ const sorted = [...items].sort((a, b) => a.at - b.at);
19
+ const newest = limits.keepNewest === false ? undefined : sorted[sorted.length - 1];
20
+ let total = ((_a = limits.otherBytes) !== null && _a !== undefined ? _a : 0) + sorted.reduce((sum, one) => sum + one.bytes, 0);
21
+ const removed = [];
22
+ for (const one of sorted) {
23
+ if (one.pinned || one === newest) {
24
+ continue;
25
+ }
26
+ const old = limits.maxAgeMs !== undefined && now - one.at > limits.maxAgeMs;
27
+ const over = limits.maxBytes !== undefined && total > limits.maxBytes;
28
+ if (old || over) {
29
+ removed.push(one.key);
30
+ total -= one.bytes;
31
+ }
32
+ }
33
+ return removed;
34
+ }
35
+ RetentionUtil2.select = select;
36
+ function megabytes(value, fallback, max = 1024 * 1024) {
37
+ const mb = Number(value);
38
+ return Math.round((Number.isFinite(mb) && mb > 0 ? Math.min(max, Math.max(1, mb)) : fallback) * 1024 * 1024);
39
+ }
40
+ RetentionUtil2.megabytes = megabytes;
41
+ })(RetentionUtil || (RetentionUtil = {}));
6
42
 
7
43
  // ../../packages/core/esm/node/fs/file-util.js
8
44
  import { chmodSync, mkdirSync, renameSync, rmSync, writeFileSync } from "node:fs";
@@ -67,6 +103,184 @@ var FileUtil;
67
103
  ` : JSON.stringify(value);
68
104
  }
69
105
  })(FileUtil || (FileUtil = {}));
106
+
107
+ // ../../packages/core/esm/node/fs/file-retention.js
108
+ var STALE_MS = 60 * 60 * 1000;
109
+ var FileRetention;
110
+ (function(FileRetention2) {
111
+ async function prune(dir, options = {}) {
112
+ var _a;
113
+ var _b, _c, _d;
114
+ const now = (_b = options.now) !== null && _b !== undefined ? _b : Date.now();
115
+ const all = await list(dir, (_c = options.each) !== null && _c !== undefined ? _c : "file");
116
+ const stale = all.filter((one) => isStaleTemporary(one.key, one.at, now));
117
+ const kept = all.filter((one) => !stale.includes(one));
118
+ const items = [];
119
+ let otherBytes = 0;
120
+ for (const one of kept) {
121
+ if (options.match && !options.match(one.key)) {
122
+ otherBytes += one.bytes;
123
+ } else {
124
+ items.push({ ...one, pinned: (_a = options.pinned) === null || _a === undefined ? undefined : _a.call(options, one.key) });
125
+ }
126
+ }
127
+ const limits = {
128
+ maxAgeMs: options.keepDays === undefined ? undefined : options.keepDays * RetentionUtil.DAY_MS,
129
+ maxBytes: options.maxBytes,
130
+ otherBytes,
131
+ keepNewest: options.keepNewest
132
+ };
133
+ const removed = [...stale.map((one) => one.key), ...RetentionUtil.select(items, limits, now)];
134
+ const sizes = new Map(all.map((one) => [one.key, one.bytes]));
135
+ let freedBytes = 0;
136
+ for (const path of removed) {
137
+ await rm2(join2(dir, path), { recursive: true, force: true });
138
+ freedBytes += (_d = sizes.get(path)) !== null && _d !== undefined ? _d : 0;
139
+ }
140
+ const bytes = all.reduce((sum, one) => sum + one.bytes, 0) - freedBytes;
141
+ return { removed, freedBytes, bytes };
142
+ }
143
+ FileRetention2.prune = prune;
144
+ async function usage(dir, options = {}) {
145
+ const files = (await list(dir, "file")).filter((one) => !options.match || options.match(one.key));
146
+ return { files: files.length, bytes: files.reduce((sum, one) => sum + one.bytes, 0) };
147
+ }
148
+ FileRetention2.usage = usage;
149
+ async function clear(dir, options = {}) {
150
+ const entries = (await list(dir, "entry")).filter((one) => !options.match || options.match(one.key));
151
+ for (const one of entries) {
152
+ await rm2(join2(dir, one.key), { recursive: true, force: true });
153
+ }
154
+ return { files: entries.length, bytes: entries.reduce((sum, one) => sum + one.bytes, 0) };
155
+ }
156
+ FileRetention2.clear = clear;
157
+ function rotate(file, options) {
158
+ var _a;
159
+ let size;
160
+ try {
161
+ size = statSync(file).size;
162
+ } catch (_b) {
163
+ return false;
164
+ }
165
+ if (size <= options.maxBytes) {
166
+ return false;
167
+ }
168
+ const keep = Math.max(0, (_a = options.keep) !== null && _a !== undefined ? _a : 1);
169
+ if (keep === 0) {
170
+ rmSync2(file, { force: true });
171
+ return true;
172
+ }
173
+ rmSync2(`${file}.${keep}`, { force: true });
174
+ for (let n = keep - 1;n >= 1; n--) {
175
+ if (existsSync(`${file}.${n}`)) {
176
+ renameSync2(`${file}.${n}`, `${file}.${n + 1}`);
177
+ }
178
+ }
179
+ renameSync2(file, `${file}.1`);
180
+ return true;
181
+ }
182
+ FileRetention2.rotate = rotate;
183
+ function append(file, text, options) {
184
+ mkdirSync2(dirname2(file), { recursive: true });
185
+ rotate(file, options);
186
+ appendFileSync(file, text, options.mode === undefined ? undefined : { mode: options.mode });
187
+ if (options.mode !== undefined) {
188
+ chmodSync2(file, options.mode);
189
+ }
190
+ }
191
+ FileRetention2.append = append;
192
+ async function rotateAsync(file, options) {
193
+ var _a;
194
+ var _b;
195
+ const size = (_a = await stat(file).catch(() => {
196
+ return;
197
+ })) === null || _a === undefined ? undefined : _a.size;
198
+ if (size === undefined || size <= options.maxBytes) {
199
+ return false;
200
+ }
201
+ const keep = Math.max(0, (_b = options.keep) !== null && _b !== undefined ? _b : 1);
202
+ if (keep === 0) {
203
+ await rm2(file, { force: true });
204
+ return true;
205
+ }
206
+ await rm2(`${file}.${keep}`, { force: true });
207
+ for (let n = keep - 1;n >= 1; n--) {
208
+ await rename2(`${file}.${n}`, `${file}.${n + 1}`).catch(() => {
209
+ return;
210
+ });
211
+ }
212
+ await rename2(file, `${file}.1`);
213
+ return true;
214
+ }
215
+ FileRetention2.rotateAsync = rotateAsync;
216
+ async function appendAsync(file, text, options) {
217
+ await mkdir2(dirname2(file), { recursive: true });
218
+ await rotateAsync(file, options);
219
+ await appendFile(file, text, options.mode === undefined ? undefined : { mode: options.mode });
220
+ if (options.mode !== undefined) {
221
+ await chmod2(file, options.mode);
222
+ }
223
+ }
224
+ FileRetention2.appendAsync = appendAsync;
225
+ async function list(dir, each) {
226
+ var _a;
227
+ var _b;
228
+ const found = [];
229
+ const walk = async (path, into) => {
230
+ const entries = await readdir(join2(dir, path), { withFileTypes: true }).catch(() => []);
231
+ for (const entry of entries) {
232
+ const below = path ? join2(path, entry.name) : entry.name;
233
+ if (entry.isDirectory()) {
234
+ await walk(below, into);
235
+ } else {
236
+ const info = await stat(join2(dir, below)).catch(() => {
237
+ return;
238
+ });
239
+ if (info) {
240
+ into(info.mtimeMs, info.size);
241
+ if (each === "file") {
242
+ found.push({ key: below, at: info.mtimeMs, bytes: info.size });
243
+ }
244
+ }
245
+ }
246
+ }
247
+ };
248
+ if (each === "file") {
249
+ await walk("", () => {
250
+ return;
251
+ });
252
+ return found;
253
+ }
254
+ for (const entry of await readdir(dir, { withFileTypes: true }).catch(() => [])) {
255
+ if (entry.isDirectory()) {
256
+ const one = { key: entry.name, at: 0, bytes: 0 };
257
+ await walk(entry.name, (at, bytes) => {
258
+ one.at = Math.max(one.at, at);
259
+ one.bytes += bytes;
260
+ });
261
+ if (!one.at) {
262
+ one.at = (_b = (_a = await stat(join2(dir, entry.name)).catch(() => {
263
+ return;
264
+ })) === null || _a === undefined ? undefined : _a.mtimeMs) !== null && _b !== undefined ? _b : 0;
265
+ }
266
+ found.push(one);
267
+ } else {
268
+ const info = await stat(join2(dir, entry.name)).catch(() => {
269
+ return;
270
+ });
271
+ if (info) {
272
+ found.push({ key: entry.name, at: info.mtimeMs, bytes: info.size });
273
+ }
274
+ }
275
+ }
276
+ return found;
277
+ }
278
+ function isStaleTemporary(path, at, now) {
279
+ var _a;
280
+ const name = (_a = path.split(/[\\/]/).pop()) !== null && _a !== undefined ? _a : "";
281
+ return name.endsWith(".tmp") && /\.\d+\.\d+\.tmp$/.test(name) && !FileUtil.ownTemporary(name) && now - at > STALE_MS;
282
+ }
283
+ })(FileRetention || (FileRetention = {}));
70
284
  // web-src/moves-protocol.ts
71
285
  var WIDTH = 720;
72
286
  var HEIGHT = 1178;
@@ -207,7 +421,7 @@ class MovesHost {
207
421
  }
208
422
  }
209
423
  if (!this.uploads.size) {
210
- rmSync2(join2(this.dir, "incoming"), { recursive: true, force: true });
424
+ rmSync3(join3(this.dir, "incoming"), { recursive: true, force: true });
211
425
  }
212
426
  }
213
427
  static UPLOAD_IDLE_MS = 30 * 60 * 1000;
@@ -234,9 +448,9 @@ class MovesHost {
234
448
  }
235
449
  this.sweep();
236
450
  const id = randomBytes(8).toString("hex");
237
- const incoming = join2(this.dir, "incoming");
238
- mkdirSync2(incoming, { recursive: true });
239
- const file = join2(incoming, id);
451
+ const incoming = join3(this.dir, "incoming");
452
+ mkdirSync3(incoming, { recursive: true });
453
+ const file = join3(incoming, id);
240
454
  writeFileSync2(file, "");
241
455
  this.uploads.set(id, { draft: { title, kind, words, replace, size: draft.size }, file, received: 0, at: this.now() });
242
456
  return { id };
@@ -250,7 +464,7 @@ class MovesHost {
250
464
  this.drop(id);
251
465
  throw MoveErrors.error("too-big", MAX_BYTES / 1024 / 1024);
252
466
  }
253
- appendFileSync(upload.file, bytes);
467
+ appendFileSync2(upload.file, bytes);
254
468
  return { received: upload.received };
255
469
  }
256
470
  async finish(id) {
@@ -266,16 +480,16 @@ class MovesHost {
266
480
  if (!move) {
267
481
  throw new Error(`No move ${name}`);
268
482
  }
269
- return { data: readFileSync(join2(this.dir, move.file)).toString("base64") };
483
+ return { data: readFileSync(join3(this.dir, move.file)).toString("base64") };
270
484
  }
271
485
  clips() {
272
- return this.read().filter((move) => !move.problem).map((move) => ({ clip: `own:${move.name}`, path: join2(this.dir, move.file) }));
486
+ return this.read().filter((move) => !move.problem).map((move) => ({ clip: `own:${move.name}`, path: join3(this.dir, move.file) }));
273
487
  }
274
488
  remove(name) {
275
489
  const moves = this.read();
276
490
  const move = moves.find((one) => one.name === name);
277
491
  if (move) {
278
- rmSync2(join2(this.dir, move.file), { force: true });
492
+ rmSync3(join3(this.dir, move.file), { force: true });
279
493
  }
280
494
  return this.write(moves.filter((one) => one.name !== name));
281
495
  }
@@ -296,7 +510,7 @@ class MovesHost {
296
510
  const work = `${input}.mp4`;
297
511
  const fit = `scale=${WIDTH}:${HEIGHT}:force_original_aspect_ratio=increase,crop=${WIDTH}:${HEIGHT},fps=${FPS},format=yuv420p,setsar=1`;
298
512
  await this.encode(["-i", input, "-an", "-vf", fit], work);
299
- const seated = this.faces && existsSync(join2(this.faces, "poster.jpg")) ? join2(this.faces, "poster.jpg") : undefined;
513
+ const seated = this.faces && existsSync2(join3(this.faces, "poster.jpg")) ? join3(this.faces, "poster.jpg") : undefined;
300
514
  const seat = seated ? ClipCheck.split(await this.run(this.ffmpeg, ["-v", "error", "-i", seated, "-vf", `${fit},${MovesHost.SAMPLE}`, "-f", "rawvideo", "pipe:1"]))[0] : undefined;
301
515
  let samples = await this.samples(work);
302
516
  const far = (frame) => seat && frame ? ClipCheck.distance(frame, seat) : 0;
@@ -308,19 +522,19 @@ class MovesHost {
308
522
  }
309
523
  const name = draft.replace ?? `m${Date.now().toString(36)}`;
310
524
  const file = fileOf(name, draft.kind);
311
- mkdirSync2(join2(this.dir, file, ".."), { recursive: true });
525
+ mkdirSync3(join3(this.dir, file, ".."), { recursive: true });
312
526
  const moves = this.read();
313
527
  const old = moves.find((one) => one.name === name);
314
528
  if (old) {
315
- rmSync2(join2(this.dir, old.file), { force: true });
529
+ rmSync3(join3(this.dir, old.file), { force: true });
316
530
  }
317
- renameSync2(work, join2(this.dir, file));
531
+ renameSync3(work, join3(this.dir, file));
318
532
  const move = {
319
533
  name,
320
534
  title: draft.title,
321
535
  kind: draft.kind,
322
536
  words: draft.words,
323
- frames: await this.frames(join2(this.dir, file)),
537
+ frames: await this.frames(join3(this.dir, file)),
324
538
  file,
325
539
  seat: was,
326
540
  blended,
@@ -365,7 +579,7 @@ class MovesHost {
365
579
  const still = ["-loop", "1", "-t", String(BLEND_S), "-i", seated];
366
580
  const out = `${work}.blend.mp4`;
367
581
  await this.encode(["-i", work, ...still, ...still, "-filter_complex", parts.join(";"), "-map", `[${at}]`], out);
368
- renameSync2(out, work);
582
+ renameSync3(out, work);
369
583
  }
370
584
  async encode(args, out) {
371
585
  await this.run(this.ffmpeg, [
@@ -383,7 +597,7 @@ class MovesHost {
383
597
  "+faststart",
384
598
  out
385
599
  ]).catch((error) => {
386
- rmSync2(out, { force: true });
600
+ rmSync3(out, { force: true });
387
601
  throw MoveErrors.error("not-video", String(error.message).split(`
388
602
  `)[0]);
389
603
  });
@@ -452,45 +666,45 @@ class MovesHost {
452
666
  const upload = this.uploads.get(id);
453
667
  this.uploads.delete(id);
454
668
  if (upload) {
455
- rmSync2(upload.file, { force: true });
456
- rmSync2(`${upload.file}.mp4`, { force: true });
669
+ rmSync3(upload.file, { force: true });
670
+ rmSync3(`${upload.file}.mp4`, { force: true });
457
671
  }
458
672
  }
459
673
  read() {
460
674
  try {
461
- const file = JSON.parse(readFileSync(join2(this.dir, "moves.json"), "utf8"));
675
+ const file = JSON.parse(readFileSync(join3(this.dir, "moves.json"), "utf8"));
462
676
  return Array.isArray(file.moves) ? file.moves : [];
463
677
  } catch {
464
678
  return [];
465
679
  }
466
680
  }
467
681
  write(moves) {
468
- FileUtil.write(join2(this.dir, "moves.json"), JSON.stringify({ version: 1, moves }, null, 1));
682
+ FileUtil.write(join3(this.dir, "moves.json"), JSON.stringify({ version: 1, moves }, null, 1));
469
683
  return moves;
470
684
  }
471
685
  static find(name, path = process.env.PATH ?? "") {
472
686
  const exe = process.platform === "win32" ? `${name}.exe` : name;
473
687
  const dirs = [...path.split(delimiter), "/opt/homebrew/bin", "/usr/local/bin", "/usr/bin"];
474
- return dirs.map((dir) => join2(dir, exe)).find(isFile);
688
+ return dirs.map((dir) => join3(dir, exe)).find(isFile);
475
689
  }
476
690
  static faces(root) {
477
691
  if (!root) {
478
692
  return;
479
693
  }
480
- const built = join2(root, "..", "..", "..", "public", "faces");
481
- return existsSync(join2(built, "poster.jpg")) ? built : join2(root, "..", "ta-web", "public", "faces");
694
+ const built = join3(root, "..", "..", "..", "public", "faces");
695
+ return existsSync2(join3(built, "poster.jpg")) ? built : join3(root, "..", "ta-web", "public", "faces");
482
696
  }
483
697
  }
484
698
  function isFile(file) {
485
699
  try {
486
- return statSync(file).isFile();
700
+ return statSync2(file).isFile();
487
701
  } catch {
488
702
  return false;
489
703
  }
490
704
  }
491
705
  async function activate(host) {
492
706
  const root = host.root;
493
- const moves = new MovesHost(join2(host.agentDir, "call-moves"), MovesHost.faces(root));
707
+ const moves = new MovesHost(join3(host.agentDir, "call-moves"), MovesHost.faces(root));
494
708
  moves.sweep();
495
709
  return {
496
710
  list: () => moves.list(),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@celljs/ta-moves",
3
- "version": "4.35.0",
3
+ "version": "4.36.0",
4
4
  "description": "A pi package for Ta: the user's own clips of the person of video calls, checked, made like Ta's own and played with her moves",
5
5
  "keywords": [
6
6
  "pi-package"
@@ -26,9 +26,9 @@
26
26
  "watch": "tsc -b -w --preserveWatchOutput"
27
27
  },
28
28
  "devDependencies": {
29
- "@celljs/core": "4.35.0",
30
- "@celljs/ta-protocol": "4.35.0",
31
- "@celljs/ta-web": "4.35.0",
29
+ "@celljs/core": "4.36.0",
30
+ "@celljs/ta-protocol": "4.36.0",
31
+ "@celljs/ta-web": "4.36.0",
32
32
  "@types/react": "^19.3.0"
33
33
  },
34
34
  "publishConfig": {