broader-vids 0.1.0 → 0.1.4

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,151 +1,151 @@
1
- "use strict";
2
-
3
- const fs = require("node:fs");
4
- const path = require("node:path");
5
- const { randomUUID } = require("node:crypto");
6
-
7
- function busy(message) {
8
- const error = new Error(message);
9
- error.code = "RUNTIME_BUSY";
10
- return error;
11
- }
12
-
13
- function alive(pid) {
14
- if (!Number.isSafeInteger(pid) || pid <= 0) return true;
15
- try {
16
- process.kill(pid, 0);
17
- return true;
18
- } catch (error) {
19
- return error.code !== "ESRCH";
20
- }
21
- }
22
-
23
- function read(file) {
24
- try {
25
- const record = JSON.parse(fs.readFileSync(file, "utf8"));
26
- if (
27
- !record ||
28
- typeof record !== "object" ||
29
- !Number.isSafeInteger(record.pid) ||
30
- record.pid <= 0
31
- )
32
- throw busy(
33
- "A runtime lock has an invalid owner. Check the operation before repairing the lock.",
34
- );
35
- return record;
36
- } catch (error) {
37
- if (error.code === "ENOENT") return undefined;
38
- throw busy(
39
- "A runtime lock is unreadable. Check its owner before repairing the lock.",
40
- );
41
- }
42
- }
43
-
44
- function gate(root, task) {
45
- const folder = path.join(root, ".launcher");
46
- fs.mkdirSync(folder, { recursive: true });
47
- const file = path.join(folder, "gate.lock");
48
- let descriptor;
49
- const deadline = performance.now() + 1000;
50
- while (descriptor === undefined) {
51
- try {
52
- descriptor = fs.openSync(file, "wx", 0o600);
53
- } catch (error) {
54
- if (error.code !== "EEXIST") throw error;
55
- if (performance.now() >= deadline)
56
- throw busy(
57
- "Another launcher operation holds the runtime gate. Retry, or check the gate owner before repairing a stale lock.",
58
- );
59
- Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, 10);
60
- }
61
- }
62
- try {
63
- fs.writeFileSync(descriptor, JSON.stringify({ pid: process.pid }));
64
- return task(folder);
65
- } finally {
66
- fs.closeSync(descriptor);
67
- fs.unlinkSync(file);
68
- }
69
- }
70
-
71
- function activeSetup(folder) {
72
- const file = path.join(folder, "setup.json");
73
- const record = read(file);
74
- if (!record) return false;
75
- return true;
76
- }
77
-
78
- function release(file, token) {
79
- if (read(file)?.token === token) fs.unlinkSync(file);
80
- }
81
-
82
- function withRuntimeUse(root, task) {
83
- const token = randomUUID();
84
- const file = gate(root, (folder) => {
85
- if (activeSetup(folder))
86
- throw busy(
87
- "Runtime installation is active. Wait for setup to finish, then retry.",
88
- );
89
- const file = path.join(folder, `${token}.use.json`);
90
- fs.writeFileSync(file, JSON.stringify({ pid: process.pid, token }), {
91
- flag: "wx",
92
- mode: 0o600,
93
- });
94
- return file;
95
- });
96
- try {
97
- return task();
98
- } finally {
99
- release(file, token);
100
- }
101
- }
102
-
103
- function withSetup(root, task) {
104
- const token = randomUUID();
105
- const file = gate(root, (folder) => {
106
- if (activeSetup(folder))
107
- throw busy(
108
- "Another runtime installation is active. Retry after it finishes.",
109
- );
110
- for (const name of fs
111
- .readdirSync(folder)
112
- .filter((name) => name.endsWith(".use.json"))) {
113
- const file = path.join(folder, name);
114
- const record = read(file);
115
- if (record)
116
- throw busy(
117
- "A launcher command holds this profile. Finish it before setup. If its parent exited, check for surviving children before repairing the lock.",
118
- );
119
- }
120
- if (
121
- fs.readdirSync(root).some((name) => /^[a-f0-9]{32}\.lock$/.test(name)) ||
122
- fs.existsSync(path.join(root, "access", "bilibili", "browser.lock"))
123
- ) {
124
- throw busy(
125
- "An engine or Bilibili browser operation holds this profile. Finish it before setup.",
126
- );
127
- }
128
- const session = read(path.join(root, ".dashboard", "server.json"));
129
- if (session && alive(session.pid))
130
- throw busy(
131
- "A dashboard server uses this profile. Stop it with dashboard stop before setup.",
132
- );
133
- const file = path.join(folder, "setup.json");
134
- fs.writeFileSync(file, JSON.stringify({ pid: process.pid, token }), {
135
- flag: "wx",
136
- mode: 0o600,
137
- });
138
- return file;
139
- });
140
- try {
141
- return task();
142
- } finally {
143
- release(file, token);
144
- }
145
- }
146
-
147
- function setupInProgress(root) {
148
- return !!read(path.join(root, ".launcher", "setup.json"));
149
- }
150
-
151
- module.exports = { withRuntimeUse, withSetup, setupInProgress };
1
+ "use strict";
2
+
3
+ const fs = require("node:fs");
4
+ const path = require("node:path");
5
+ const { randomUUID } = require("node:crypto");
6
+
7
+ function busy(message) {
8
+ const error = new Error(message);
9
+ error.code = "RUNTIME_BUSY";
10
+ return error;
11
+ }
12
+
13
+ function alive(pid) {
14
+ if (!Number.isSafeInteger(pid) || pid <= 0) return true;
15
+ try {
16
+ process.kill(pid, 0);
17
+ return true;
18
+ } catch (error) {
19
+ return error.code !== "ESRCH";
20
+ }
21
+ }
22
+
23
+ function read(file) {
24
+ try {
25
+ const record = JSON.parse(fs.readFileSync(file, "utf8"));
26
+ if (
27
+ !record ||
28
+ typeof record !== "object" ||
29
+ !Number.isSafeInteger(record.pid) ||
30
+ record.pid <= 0
31
+ )
32
+ throw busy(
33
+ "A runtime lock has an invalid owner. Check the operation before repairing the lock.",
34
+ );
35
+ return record;
36
+ } catch (error) {
37
+ if (error.code === "ENOENT") return undefined;
38
+ throw busy(
39
+ "A runtime lock is unreadable. Check its owner before repairing the lock.",
40
+ );
41
+ }
42
+ }
43
+
44
+ function gate(root, task) {
45
+ const folder = path.join(root, ".launcher");
46
+ fs.mkdirSync(folder, { recursive: true });
47
+ const file = path.join(folder, "gate.lock");
48
+ let descriptor;
49
+ const deadline = performance.now() + 1000;
50
+ while (descriptor === undefined) {
51
+ try {
52
+ descriptor = fs.openSync(file, "wx", 0o600);
53
+ } catch (error) {
54
+ if (error.code !== "EEXIST") throw error;
55
+ if (performance.now() >= deadline)
56
+ throw busy(
57
+ "Another launcher operation holds the runtime gate. Retry, or check the gate owner before repairing a stale lock.",
58
+ );
59
+ Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, 10);
60
+ }
61
+ }
62
+ try {
63
+ fs.writeFileSync(descriptor, JSON.stringify({ pid: process.pid }));
64
+ return task(folder);
65
+ } finally {
66
+ fs.closeSync(descriptor);
67
+ fs.unlinkSync(file);
68
+ }
69
+ }
70
+
71
+ function activeSetup(folder) {
72
+ const file = path.join(folder, "setup.json");
73
+ const record = read(file);
74
+ if (!record) return false;
75
+ return true;
76
+ }
77
+
78
+ function release(file, token) {
79
+ if (read(file)?.token === token) fs.unlinkSync(file);
80
+ }
81
+
82
+ function withRuntimeUse(root, task) {
83
+ const token = randomUUID();
84
+ const file = gate(root, (folder) => {
85
+ if (activeSetup(folder))
86
+ throw busy(
87
+ "Runtime installation is active. Wait for setup to finish, then retry.",
88
+ );
89
+ const file = path.join(folder, `${token}.use.json`);
90
+ fs.writeFileSync(file, JSON.stringify({ pid: process.pid, token }), {
91
+ flag: "wx",
92
+ mode: 0o600,
93
+ });
94
+ return file;
95
+ });
96
+ try {
97
+ return task();
98
+ } finally {
99
+ release(file, token);
100
+ }
101
+ }
102
+
103
+ function withSetup(root, task) {
104
+ const token = randomUUID();
105
+ const file = gate(root, (folder) => {
106
+ if (activeSetup(folder))
107
+ throw busy(
108
+ "Another runtime installation is active. Retry after it finishes.",
109
+ );
110
+ for (const name of fs
111
+ .readdirSync(folder)
112
+ .filter((name) => name.endsWith(".use.json"))) {
113
+ const file = path.join(folder, name);
114
+ const record = read(file);
115
+ if (record)
116
+ throw busy(
117
+ "A launcher command holds this profile. Finish it before setup. If its parent exited, check for surviving children before repairing the lock.",
118
+ );
119
+ }
120
+ if (
121
+ fs.readdirSync(root).some((name) => /^[a-f0-9]{32}\.lock$/.test(name)) ||
122
+ fs.existsSync(path.join(root, "access", "bilibili", "browser.lock"))
123
+ ) {
124
+ throw busy(
125
+ "An engine or Bilibili browser operation holds this profile. Finish it before setup.",
126
+ );
127
+ }
128
+ const session = read(path.join(root, ".dashboard", "server.json"));
129
+ if (session && alive(session.pid))
130
+ throw busy(
131
+ "A dashboard server uses this profile. Stop it with dashboard stop before setup.",
132
+ );
133
+ const file = path.join(folder, "setup.json");
134
+ fs.writeFileSync(file, JSON.stringify({ pid: process.pid, token }), {
135
+ flag: "wx",
136
+ mode: 0o600,
137
+ });
138
+ return file;
139
+ });
140
+ try {
141
+ return task();
142
+ } finally {
143
+ release(file, token);
144
+ }
145
+ }
146
+
147
+ function setupInProgress(root) {
148
+ return !!read(path.join(root, ".launcher", "setup.json"));
149
+ }
150
+
151
+ module.exports = { withRuntimeUse, withSetup, setupInProgress };