@celljs/ta-moves 4.34.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 +305 -32
  2. package/package.json +4 -3
package/host/index.mjs CHANGED
@@ -1,11 +1,287 @@
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, readFileSync, renameSync, rmSync, statSync, writeFileSync } from "node:fs";
5
- import { delimiter, join } 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
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 = {}));
42
+
43
+ // ../../packages/core/esm/node/fs/file-util.js
44
+ import { chmodSync, mkdirSync, renameSync, rmSync, writeFileSync } from "node:fs";
45
+ import { chmod, mkdir, rename, rm, writeFile } from "node:fs/promises";
46
+ import { basename, dirname, join } from "node:path";
47
+ var written = 0;
48
+ var FileUtil;
49
+ (function(FileUtil2) {
50
+ function temporary(file, hidden = false) {
51
+ const n = `${process.pid}.${++written}`;
52
+ return hidden ? join(dirname(file), `.${basename(file)}.${n}.tmp`) : `${file}.${n}.tmp`;
53
+ }
54
+ FileUtil2.temporary = temporary;
55
+ function ownTemporary(name) {
56
+ return new RegExp(`\\.${process.pid}\\.\\d+\\.tmp$`).test(name);
57
+ }
58
+ FileUtil2.ownTemporary = ownTemporary;
59
+ function write(file, data, options = {}) {
60
+ if (options.mkdir !== false) {
61
+ mkdirSync(dirname(file), { recursive: true });
62
+ }
63
+ const draft = temporary(file, options.hidden);
64
+ try {
65
+ writeFileSync(draft, data, options.mode === undefined ? undefined : { mode: options.mode });
66
+ if (options.mode !== undefined) {
67
+ chmodSync(draft, options.mode);
68
+ }
69
+ renameSync(draft, file);
70
+ } catch (error) {
71
+ rmSync(draft, { force: true });
72
+ throw error;
73
+ }
74
+ }
75
+ FileUtil2.write = write;
76
+ async function writeAsync(file, data, options = {}) {
77
+ if (options.mkdir !== false) {
78
+ await mkdir(dirname(file), { recursive: true });
79
+ }
80
+ const draft = temporary(file, options.hidden);
81
+ try {
82
+ await writeFile(draft, data, options.mode === undefined ? undefined : { mode: options.mode });
83
+ if (options.mode !== undefined) {
84
+ await chmod(draft, options.mode);
85
+ }
86
+ await rename(draft, file);
87
+ } catch (error) {
88
+ await rm(draft, { force: true });
89
+ throw error;
90
+ }
91
+ }
92
+ FileUtil2.writeAsync = writeAsync;
93
+ function writeJson(file, value, options = {}) {
94
+ write(file, json(value, options.pretty), options);
95
+ }
96
+ FileUtil2.writeJson = writeJson;
97
+ function writeJsonAsync(file, value, options = {}) {
98
+ return writeAsync(file, json(value, options.pretty), options);
99
+ }
100
+ FileUtil2.writeJsonAsync = writeJsonAsync;
101
+ function json(value, pretty = false) {
102
+ return pretty ? `${JSON.stringify(value, undefined, 2)}
103
+ ` : JSON.stringify(value);
104
+ }
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 = {}));
7
284
  // web-src/moves-protocol.ts
8
- var NAME = "@celljs/ta-moves";
9
285
  var WIDTH = 720;
10
286
  var HEIGHT = 1178;
11
287
  var FPS = 24;
@@ -145,7 +421,7 @@ class MovesHost {
145
421
  }
146
422
  }
147
423
  if (!this.uploads.size) {
148
- rmSync(join(this.dir, "incoming"), { recursive: true, force: true });
424
+ rmSync3(join3(this.dir, "incoming"), { recursive: true, force: true });
149
425
  }
150
426
  }
151
427
  static UPLOAD_IDLE_MS = 30 * 60 * 1000;
@@ -172,10 +448,10 @@ class MovesHost {
172
448
  }
173
449
  this.sweep();
174
450
  const id = randomBytes(8).toString("hex");
175
- const incoming = join(this.dir, "incoming");
176
- mkdirSync(incoming, { recursive: true });
177
- const file = join(incoming, id);
178
- writeFileSync(file, "");
451
+ const incoming = join3(this.dir, "incoming");
452
+ mkdirSync3(incoming, { recursive: true });
453
+ const file = join3(incoming, id);
454
+ writeFileSync2(file, "");
179
455
  this.uploads.set(id, { draft: { title, kind, words, replace, size: draft.size }, file, received: 0, at: this.now() });
180
456
  return { id };
181
457
  }
@@ -188,7 +464,7 @@ class MovesHost {
188
464
  this.drop(id);
189
465
  throw MoveErrors.error("too-big", MAX_BYTES / 1024 / 1024);
190
466
  }
191
- appendFileSync(upload.file, bytes);
467
+ appendFileSync2(upload.file, bytes);
192
468
  return { received: upload.received };
193
469
  }
194
470
  async finish(id) {
@@ -204,16 +480,16 @@ class MovesHost {
204
480
  if (!move) {
205
481
  throw new Error(`No move ${name}`);
206
482
  }
207
- return { data: readFileSync(join(this.dir, move.file)).toString("base64") };
483
+ return { data: readFileSync(join3(this.dir, move.file)).toString("base64") };
208
484
  }
209
485
  clips() {
210
- return this.read().filter((move) => !move.problem).map((move) => ({ clip: `own:${move.name}`, path: join(this.dir, move.file) }));
486
+ return this.read().filter((move) => !move.problem).map((move) => ({ clip: `own:${move.name}`, path: join3(this.dir, move.file) }));
211
487
  }
212
488
  remove(name) {
213
489
  const moves = this.read();
214
490
  const move = moves.find((one) => one.name === name);
215
491
  if (move) {
216
- rmSync(join(this.dir, move.file), { force: true });
492
+ rmSync3(join3(this.dir, move.file), { force: true });
217
493
  }
218
494
  return this.write(moves.filter((one) => one.name !== name));
219
495
  }
@@ -234,7 +510,7 @@ class MovesHost {
234
510
  const work = `${input}.mp4`;
235
511
  const fit = `scale=${WIDTH}:${HEIGHT}:force_original_aspect_ratio=increase,crop=${WIDTH}:${HEIGHT},fps=${FPS},format=yuv420p,setsar=1`;
236
512
  await this.encode(["-i", input, "-an", "-vf", fit], work);
237
- const seated = this.faces && existsSync(join(this.faces, "poster.jpg")) ? join(this.faces, "poster.jpg") : undefined;
513
+ const seated = this.faces && existsSync2(join3(this.faces, "poster.jpg")) ? join3(this.faces, "poster.jpg") : undefined;
238
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;
239
515
  let samples = await this.samples(work);
240
516
  const far = (frame) => seat && frame ? ClipCheck.distance(frame, seat) : 0;
@@ -246,19 +522,19 @@ class MovesHost {
246
522
  }
247
523
  const name = draft.replace ?? `m${Date.now().toString(36)}`;
248
524
  const file = fileOf(name, draft.kind);
249
- mkdirSync(join(this.dir, file, ".."), { recursive: true });
525
+ mkdirSync3(join3(this.dir, file, ".."), { recursive: true });
250
526
  const moves = this.read();
251
527
  const old = moves.find((one) => one.name === name);
252
528
  if (old) {
253
- rmSync(join(this.dir, old.file), { force: true });
529
+ rmSync3(join3(this.dir, old.file), { force: true });
254
530
  }
255
- renameSync(work, join(this.dir, file));
531
+ renameSync3(work, join3(this.dir, file));
256
532
  const move = {
257
533
  name,
258
534
  title: draft.title,
259
535
  kind: draft.kind,
260
536
  words: draft.words,
261
- frames: await this.frames(join(this.dir, file)),
537
+ frames: await this.frames(join3(this.dir, file)),
262
538
  file,
263
539
  seat: was,
264
540
  blended,
@@ -303,7 +579,7 @@ class MovesHost {
303
579
  const still = ["-loop", "1", "-t", String(BLEND_S), "-i", seated];
304
580
  const out = `${work}.blend.mp4`;
305
581
  await this.encode(["-i", work, ...still, ...still, "-filter_complex", parts.join(";"), "-map", `[${at}]`], out);
306
- renameSync(out, work);
582
+ renameSync3(out, work);
307
583
  }
308
584
  async encode(args, out) {
309
585
  await this.run(this.ffmpeg, [
@@ -321,7 +597,7 @@ class MovesHost {
321
597
  "+faststart",
322
598
  out
323
599
  ]).catch((error) => {
324
- rmSync(out, { force: true });
600
+ rmSync3(out, { force: true });
325
601
  throw MoveErrors.error("not-video", String(error.message).split(`
326
602
  `)[0]);
327
603
  });
@@ -390,48 +666,45 @@ class MovesHost {
390
666
  const upload = this.uploads.get(id);
391
667
  this.uploads.delete(id);
392
668
  if (upload) {
393
- rmSync(upload.file, { force: true });
394
- rmSync(`${upload.file}.mp4`, { force: true });
669
+ rmSync3(upload.file, { force: true });
670
+ rmSync3(`${upload.file}.mp4`, { force: true });
395
671
  }
396
672
  }
397
673
  read() {
398
674
  try {
399
- const file = JSON.parse(readFileSync(join(this.dir, "moves.json"), "utf8"));
675
+ const file = JSON.parse(readFileSync(join3(this.dir, "moves.json"), "utf8"));
400
676
  return Array.isArray(file.moves) ? file.moves : [];
401
677
  } catch {
402
678
  return [];
403
679
  }
404
680
  }
405
681
  write(moves) {
406
- mkdirSync(this.dir, { recursive: true });
407
- const file = join(this.dir, "moves.json");
408
- writeFileSync(`${file}.tmp`, JSON.stringify({ version: 1, moves }, null, 1));
409
- renameSync(`${file}.tmp`, file);
682
+ FileUtil.write(join3(this.dir, "moves.json"), JSON.stringify({ version: 1, moves }, null, 1));
410
683
  return moves;
411
684
  }
412
685
  static find(name, path = process.env.PATH ?? "") {
413
686
  const exe = process.platform === "win32" ? `${name}.exe` : name;
414
687
  const dirs = [...path.split(delimiter), "/opt/homebrew/bin", "/usr/local/bin", "/usr/bin"];
415
- return dirs.map((dir) => join(dir, exe)).find(isFile);
688
+ return dirs.map((dir) => join3(dir, exe)).find(isFile);
416
689
  }
417
690
  static faces(root) {
418
691
  if (!root) {
419
692
  return;
420
693
  }
421
- const built = join(root, "..", "..", "..", "public", "faces");
422
- return existsSync(join(built, "poster.jpg")) ? built : join(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");
423
696
  }
424
697
  }
425
698
  function isFile(file) {
426
699
  try {
427
- return statSync(file).isFile();
700
+ return statSync2(file).isFile();
428
701
  } catch {
429
702
  return false;
430
703
  }
431
704
  }
432
705
  async function activate(host) {
433
- const root = (await host.listPackages()).find((pkg) => pkg.name === NAME)?.root;
434
- const moves = new MovesHost(join(host.agentDir, "call-moves"), MovesHost.faces(root));
706
+ const root = host.root;
707
+ const moves = new MovesHost(join3(host.agentDir, "call-moves"), MovesHost.faces(root));
435
708
  moves.sweep();
436
709
  return {
437
710
  list: () => moves.list(),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@celljs/ta-moves",
3
- "version": "4.34.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,8 +26,9 @@
26
26
  "watch": "tsc -b -w --preserveWatchOutput"
27
27
  },
28
28
  "devDependencies": {
29
- "@celljs/ta-protocol": "4.34.0",
30
- "@celljs/ta-web": "4.34.0",
29
+ "@celljs/core": "4.36.0",
30
+ "@celljs/ta-protocol": "4.36.0",
31
+ "@celljs/ta-web": "4.36.0",
31
32
  "@types/react": "^19.3.0"
32
33
  },
33
34
  "publishConfig": {