shipmates 0.0.0 → 0.2.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 (69) hide show
  1. package/LICENSE +661 -0
  2. package/dist/cli-run.d.ts +9 -0
  3. package/dist/cli-run.d.ts.map +1 -0
  4. package/dist/cli-run.js +237 -0
  5. package/dist/cli-run.js.map +1 -0
  6. package/dist/cli.d.ts +3 -0
  7. package/dist/cli.d.ts.map +1 -0
  8. package/dist/cli.js +11 -0
  9. package/dist/cli.js.map +1 -0
  10. package/dist/delivery.d.ts +37 -0
  11. package/dist/delivery.d.ts.map +1 -0
  12. package/dist/delivery.js +13 -0
  13. package/dist/delivery.js.map +1 -0
  14. package/dist/host.d.ts +132 -0
  15. package/dist/host.d.ts.map +1 -0
  16. package/dist/host.js +66 -0
  17. package/dist/host.js.map +1 -0
  18. package/dist/ids.d.ts +3 -0
  19. package/dist/ids.d.ts.map +1 -0
  20. package/dist/ids.js +6 -0
  21. package/dist/ids.js.map +1 -0
  22. package/dist/index.d.ts +17 -0
  23. package/dist/index.d.ts.map +1 -0
  24. package/dist/index.js +11 -0
  25. package/dist/index.js.map +1 -0
  26. package/dist/liveness.d.ts +25 -0
  27. package/dist/liveness.d.ts.map +1 -0
  28. package/dist/liveness.js +75 -0
  29. package/dist/liveness.js.map +1 -0
  30. package/dist/quiet.d.ts +7 -0
  31. package/dist/quiet.d.ts.map +1 -0
  32. package/dist/quiet.js +19 -0
  33. package/dist/quiet.js.map +1 -0
  34. package/dist/shipmates.d.ts +401 -0
  35. package/dist/shipmates.d.ts.map +1 -0
  36. package/dist/shipmates.js +1112 -0
  37. package/dist/shipmates.js.map +1 -0
  38. package/dist/store.d.ts +19 -0
  39. package/dist/store.d.ts.map +1 -0
  40. package/dist/store.js +236 -0
  41. package/dist/store.js.map +1 -0
  42. package/dist/tail.d.ts +27 -0
  43. package/dist/tail.d.ts.map +1 -0
  44. package/dist/tail.js +127 -0
  45. package/dist/tail.js.map +1 -0
  46. package/dist/types.d.ts +129 -0
  47. package/dist/types.d.ts.map +1 -0
  48. package/dist/types.js +11 -0
  49. package/dist/types.js.map +1 -0
  50. package/dist/watch.d.ts +13 -0
  51. package/dist/watch.d.ts.map +1 -0
  52. package/dist/watch.js +30 -0
  53. package/dist/watch.js.map +1 -0
  54. package/package.json +50 -13
  55. package/src/cli-run.ts +263 -0
  56. package/src/cli.ts +11 -0
  57. package/src/delivery.ts +49 -0
  58. package/src/host.ts +161 -0
  59. package/src/ids.ts +6 -0
  60. package/src/index.ts +44 -0
  61. package/src/liveness.ts +86 -0
  62. package/src/quiet.ts +22 -0
  63. package/src/shipmates.ts +1634 -0
  64. package/src/store.ts +252 -0
  65. package/src/tail.ts +143 -0
  66. package/src/types.ts +138 -0
  67. package/src/watch.ts +32 -0
  68. package/.npm-publish.log +0 -0
  69. package/README.md +0 -5
@@ -0,0 +1,86 @@
1
+ import {execFileSync} from 'node:child_process';
2
+ import fs from 'node:fs';
3
+ import os from 'node:os';
4
+
5
+ /** Who a process is, precisely enough that a reused pid is not mistaken for it. */
6
+ export interface ProcessIdentity {
7
+ pid: number;
8
+ /** Process start time as the OS reports it; null when it cannot be read. */
9
+ startTime: string | null;
10
+ bootId: string | null;
11
+ host: string;
12
+ }
13
+
14
+ export interface ProcessProbe {
15
+ self(): ProcessIdentity;
16
+ isAlive(p: ProcessIdentity): boolean;
17
+ }
18
+
19
+ /**
20
+ * The same rule as wherever's assistant lock: a process is alive when its pid
21
+ * runs on this boot with the start time recorded. On another host we cannot
22
+ * know, so it counts as alive (single machine is the stated scope).
23
+ */
24
+ export class OsProbe implements ProcessProbe {
25
+ private readonly host = os.hostname();
26
+ private readonly bootId = readBootId();
27
+ private me: ProcessIdentity | undefined;
28
+
29
+ self(): ProcessIdentity {
30
+ this.me ??= {
31
+ pid: process.pid,
32
+ startTime: startTimeOf(process.pid),
33
+ bootId: this.bootId,
34
+ host: this.host,
35
+ };
36
+ return this.me;
37
+ }
38
+
39
+ isAlive(p: ProcessIdentity): boolean {
40
+ if (p.host !== this.host) return true;
41
+ if (p.bootId && this.bootId && p.bootId !== this.bootId) return false;
42
+ try {
43
+ process.kill(p.pid, 0);
44
+ } catch (e) {
45
+ // EPERM means it exists but belongs to someone else.
46
+ if ((e as NodeJS.ErrnoException).code !== 'EPERM') return false;
47
+ }
48
+ if (!p.startTime) return true;
49
+ const now = startTimeOf(p.pid);
50
+ return now === null || now === p.startTime;
51
+ }
52
+ }
53
+
54
+ function readBootId(): string | null {
55
+ try {
56
+ return fs.readFileSync('/proc/sys/kernel/random/boot_id', 'utf8').trim();
57
+ } catch {}
58
+ if (process.platform === 'darwin') {
59
+ try {
60
+ return execFileSync('sysctl', ['-n', 'kern.boottime'], {
61
+ encoding: 'utf8',
62
+ }).trim();
63
+ } catch {}
64
+ }
65
+ return null;
66
+ }
67
+
68
+ function startTimeOf(pid: number): string | null {
69
+ try {
70
+ const stat = fs.readFileSync(`/proc/${pid}/stat`, 'utf8');
71
+ // Field 22 (starttime) counts after the ")" that closes the command name,
72
+ // which may itself contain spaces or parentheses.
73
+ const rest = stat.slice(stat.lastIndexOf(')') + 2).split(' ');
74
+ return rest[19] ?? null;
75
+ } catch {}
76
+ if (process.platform === 'darwin') {
77
+ try {
78
+ return (
79
+ execFileSync('ps', ['-o', 'lstart=', '-p', String(pid)], {
80
+ encoding: 'utf8',
81
+ }).trim() || null
82
+ );
83
+ } catch {}
84
+ }
85
+ return null;
86
+ }
package/src/quiet.ts ADDED
@@ -0,0 +1,22 @@
1
+ /**
2
+ * `node:sqlite` is still flagged experimental and prints a warning on first
3
+ * use. That is noise in a CLI the user runs fifty times a day, so it is
4
+ * silenced here and ONLY here, narrowly: any other warning still gets through.
5
+ */
6
+ export function silenceSqliteWarning(): void {
7
+ const proc = process as unknown as {
8
+ emit: (name: string, ...args: unknown[]) => boolean;
9
+ };
10
+ const original = proc.emit.bind(process);
11
+ proc.emit = (name: string, ...args: unknown[]): boolean => {
12
+ if (name === 'warning') {
13
+ const warning = args[0] as {name?: string; message?: string} | undefined;
14
+ if (
15
+ warning?.name === 'ExperimentalWarning' &&
16
+ /SQLite/i.test(warning.message ?? '')
17
+ )
18
+ return false;
19
+ }
20
+ return original(name, ...args);
21
+ };
22
+ }