stream-doctor 0.0.4 → 0.0.5

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.
@@ -1,5 +1,4 @@
1
1
  import { type ChildProcess } from "node:child_process";
2
- export declare function bundledBinary(): string | null;
3
2
  export type Status = "streaming" | "receiving" | "waiting_for_playlist" | "ended" | "failed" | "stopped";
4
3
  export interface StreamerStatus {
5
4
  input: string;
@@ -32,6 +31,7 @@ export declare function session({ server, binary, }?: {
32
31
  server?: string;
33
32
  binary?: string;
34
33
  }): Promise<Session>;
34
+ export declare function bundledBinary(): string | null;
35
35
  declare class Session {
36
36
  server: string;
37
37
  child: ChildProcess | null;
@@ -1,48 +1,15 @@
1
- // Client for the stream_doctor server. `session()` spawns the binary (the one
2
- // bundled for this platform, or `binary`) when no server is listening;
3
- // `session.close()` stops it. The daemon's output goes to `session.logFile`.
4
1
  import { spawn } from "node:child_process";
5
2
  import fs from "node:fs";
6
3
  import { createRequire } from "node:module";
7
4
  import os from "node:os";
8
5
  import path from "node:path";
9
6
  const DEFAULT_SERVER = "http://localhost:4040";
7
+ const LOG_FILE = path.join(os.tmpdir(), "stream_doctor.log");
10
8
  const PLATFORM_PACKAGES = {
11
9
  "darwin-arm64": "@stream-doctor/darwin-arm64",
12
10
  "linux-arm64": "@stream-doctor/linux-arm64",
13
11
  "linux-x64": "@stream-doctor/linux-x64",
14
12
  };
15
- // The daemon ships as one package per platform, all optional dependencies of
16
- // this one, so only the matching one is installed.
17
- export function bundledBinary() {
18
- const pkg = PLATFORM_PACKAGES[`${process.platform}-${process.arch}`];
19
- if (!pkg)
20
- return null;
21
- try {
22
- return createRequire(import.meta.url).resolve(`${pkg}/bin/stream_doctor`);
23
- }
24
- catch {
25
- return null;
26
- }
27
- }
28
- async function api(method, path, body, server) {
29
- let res;
30
- try {
31
- res = await fetch(server + path, {
32
- method,
33
- headers: body ? { "content-type": "application/json" } : undefined,
34
- body: body ? JSON.stringify(body) : undefined,
35
- });
36
- }
37
- catch (e) {
38
- throw new Error(`${method} ${path}: cannot reach ${server} (is the server running?): ${e.message}`);
39
- }
40
- const text = await res.text();
41
- if (!res.ok)
42
- throw new Error(`${method} ${path}: HTTP ${res.status}: ${text}`);
43
- return JSON.parse(text);
44
- }
45
- const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
46
13
  const TERMINAL_STATUSES = ["ended", "failed", "stopped"];
47
14
  export async function session({ server = DEFAULT_SERVER, binary, } = {}) {
48
15
  try {
@@ -51,48 +18,27 @@ export async function session({ server = DEFAULT_SERVER, binary, } = {}) {
51
18
  }
52
19
  catch (e) {
53
20
  let installDir;
54
- const logFile = path.join(os.tmpdir(), "stream_doctor.log");
55
21
  if (!binary) {
56
22
  binary = bundledBinary() ?? undefined;
57
23
  if (!binary) {
58
24
  throw new Error(`${e.message}; no stream_doctor binary bundled for ${process.platform}-${process.arch}, pass one with \`binary\``);
59
25
  }
60
- // Burrito unpacks the payload once per release name + ERTS + app version,
61
- // none of which change between npm releases, so its default cache under
62
- // ~/.local/share would keep serving the previous package's daemon.
63
26
  installDir = path.join(path.dirname(binary), "..", ".burrito");
64
27
  }
65
- const child = await spawnServer(binary, server, logFile, installDir);
66
- return new Session(server, child, logFile);
28
+ const child = await spawnServer(binary, server, installDir);
29
+ return new Session(server, child, LOG_FILE);
67
30
  }
68
31
  }
69
- async function spawnServer(binary, server, logFile, installDir) {
70
- if (!fs.existsSync(binary)) {
71
- throw new Error(`${binary} not found, build it with: MIX_ENV=prod mix release`);
32
+ export function bundledBinary() {
33
+ const pkg = PLATFORM_PACKAGES[`${process.platform}-${process.arch}`];
34
+ if (!pkg)
35
+ return null;
36
+ try {
37
+ return createRequire(import.meta.url).resolve(`${pkg}/bin/stream_doctor`);
72
38
  }
73
- const env = {
74
- ...process.env,
75
- // the daemon quits once its stdin (the pipe below) breaks, i.e. when this
76
- // process dies, however it dies
77
- STREAM_DOCTOR_EXIT_ON_STDIN_EOF: "1",
78
- ...(installDir ? { STREAM_DOCTOR_INSTALL_DIR: installDir } : {}),
79
- };
80
- const log = fs.openSync(logFile, "a");
81
- // own process group, so that killing the burrito launcher takes the BEAM with it
82
- const child = spawn(binary, [], { stdio: ["pipe", log, log], detached: true, env });
83
- child.on("exit", () => fs.closeSync(log));
84
- for (const deadline = Date.now() + 120_000; Date.now() < deadline;) {
85
- if (child.exitCode !== null) {
86
- throw new Error(`server exited with ${child.exitCode}, see ${logFile}`);
87
- }
88
- await sleep(1000);
89
- try {
90
- await api("GET", "/status", null, server);
91
- return child;
92
- }
93
- catch { }
39
+ catch {
40
+ return null;
94
41
  }
95
- throw new Error(`server didn't come up in 2 minutes, see ${logFile}`);
96
42
  }
97
43
  class Session {
98
44
  server;
@@ -185,3 +131,46 @@ class Viewer {
185
131
  return metrics;
186
132
  }
187
133
  }
134
+ async function spawnServer(binary, server, installDir) {
135
+ if (!fs.existsSync(binary)) {
136
+ throw new Error(`${binary} not found, build it with: MIX_ENV=prod mix release`);
137
+ }
138
+ const env = {
139
+ ...process.env,
140
+ STREAM_DOCTOR_EXIT_ON_STDIN_EOF: "1",
141
+ ...(installDir ? { STREAM_DOCTOR_INSTALL_DIR: installDir } : {}),
142
+ };
143
+ const log = fs.openSync(LOG_FILE, "a");
144
+ const child = spawn(binary, [], { stdio: ["pipe", log, log], detached: true, env });
145
+ child.on("exit", () => fs.closeSync(log));
146
+ for (const deadline = Date.now() + 120_000; Date.now() < deadline;) {
147
+ if (child.exitCode !== null) {
148
+ throw new Error(`server exited with ${child.exitCode}, see ${LOG_FILE}`);
149
+ }
150
+ await sleep(1000);
151
+ try {
152
+ await api("GET", "/status", null, server);
153
+ return child;
154
+ }
155
+ catch { }
156
+ }
157
+ throw new Error(`server didn't come up in 2 minutes, see ${LOG_FILE}`);
158
+ }
159
+ async function api(method, path, body, server) {
160
+ let res;
161
+ try {
162
+ res = await fetch(server + path, {
163
+ method,
164
+ headers: body ? { "content-type": "application/json" } : undefined,
165
+ body: body ? JSON.stringify(body) : undefined,
166
+ });
167
+ }
168
+ catch (e) {
169
+ throw new Error(`${method} ${path}: cannot reach ${server} (is the server running?): ${e.message}`);
170
+ }
171
+ const text = await res.text();
172
+ if (!res.ok)
173
+ throw new Error(`${method} ${path}: HTTP ${res.status}: ${text}`);
174
+ return JSON.parse(text);
175
+ }
176
+ const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "stream-doctor",
3
- "version": "0.0.4",
3
+ "version": "0.0.5",
4
4
  "description": "Automated end-to-end testing for video infrastructure (early proof of concept)",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
@@ -33,8 +33,8 @@
33
33
  "typescript-eslint": "^8.70.0"
34
34
  },
35
35
  "optionalDependencies": {
36
- "@stream-doctor/darwin-arm64": "0.0.4",
37
- "@stream-doctor/linux-arm64": "0.0.4",
38
- "@stream-doctor/linux-x64": "0.0.4"
36
+ "@stream-doctor/darwin-arm64": "0.0.5",
37
+ "@stream-doctor/linux-arm64": "0.0.5",
38
+ "@stream-doctor/linux-x64": "0.0.5"
39
39
  }
40
40
  }