@torrent-tv/proxy 2.89.5 → 2.89.6

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,89 +1,92 @@
1
- /**
2
- * @file Opening an output admits and places its viewer in one synchronous call.
3
- */
4
-
5
- import test from "node:test";
6
- import assert from "node:assert/strict";
7
- import { EncodeAdmission } from "../services/encode/EncodeAdmission.js";
8
- import { ViewerRequests } from "../services/server/ViewerRequests.js";
9
-
10
- test("the first opening holds capacity before a concurrent opening is admitted", async () => {
11
- const records = new Map();
12
- const outputCosts = new Map([
13
- ["source-a:0", 0.6],
14
- ["source-b:0", 0.6]
15
- ]);
16
- const viewers = {
17
- get: () => null,
18
- forOutput(output) {
19
- return new Set(records.get(output.outputKey)?.keys() ?? []);
20
- },
21
- of(output, consumerId) {
22
- let viewersOnOutput = records.get(output.outputKey);
23
- if (!viewersOnOutput) {
24
- viewersOnOutput = new Map();
25
- records.set(output.outputKey, viewersOnOutput);
26
- }
27
- let viewer = viewersOnOutput.get(consumerId);
28
- if (!viewer) {
29
- viewer = {
30
- position: null,
31
- linkReading: () => null,
32
- noteVisiblePicture: () => {}
33
- };
34
- viewersOnOutput.set(consumerId, viewer);
35
- }
36
- return viewer;
37
- },
38
- watchedAddresses() {
39
- return new Set(
40
- [...records]
41
- .filter(([, members]) => [...members.values()].some((viewer) => viewer.position !== null))
42
- .map(([address]) => address)
43
- );
44
- }
45
- };
46
- const admission = new EncodeAdmission({
47
- liveRunsByAddress: () => new Map(),
48
- preparedAddresses: () => new Set(),
49
- watchedAddresses: () => viewers.watchedAddresses(),
50
- loadOf: (address) => {
51
- const costSec = outputCosts.get(address);
52
- return costSec === undefined ? null : { costSec, fileKey: address, fileCostSec: 0 };
53
- },
54
- availability: () => ({ share: 1, known: true }),
55
- finished: () => false
56
- });
57
- const opening = {
58
- async open({ sourceKey, fileIndex, claim }) {
59
- const output = { id: `${sourceKey}:${fileIndex}`, outputKey: `${sourceKey}:${fileIndex}` };
60
- const answer = claim(output);
61
- if (!answer.admitted) {
62
- const error = new Error(answer.reason);
63
- error.code = "OUTPUT_NO_CAPACITY";
64
- throw error;
65
- }
66
- return { output, existed: false, audio: null, verdict: null };
67
- }
68
- };
69
- const requests = new ViewerRequests({
70
- opening,
71
- viewers,
72
- admitsWatching: (output) => admission.admitsWatching(output.outputKey),
73
- outputTimes: { segmentIndexForTime: (_output, seconds) => Math.floor(seconds / 4) },
74
- planEncodersSoon: () => {},
75
- outputs: { touch: () => {} },
76
- noteServingVerdict: () => {},
77
- waitUntilReady: async () => {}
78
- });
79
-
80
- const first = requests.createOrGetSession({ sourceKey: "source-a", fileIndex: 0, consumerId: "viewer-a" });
81
- const second = requests.createOrGetSession({ sourceKey: "source-b", fileIndex: 0, consumerId: "viewer-b" });
82
- const [firstResult, secondResult] = await Promise.allSettled([first, second]);
83
-
84
- assert.equal(firstResult.status, "fulfilled");
85
- assert.equal(secondResult.status, "rejected");
86
- assert.match(secondResult.reason.message, /would make 0\.83x/);
87
- assert.equal(viewers.forOutput({ outputKey: "source-a:0" }).has("viewer-a"), true);
88
- assert.equal(viewers.forOutput({ outputKey: "source-b:0" }).has("viewer-b"), false);
89
- });
1
+ /**
2
+ * @file Opening an output admits and places its viewer in one synchronous call.
3
+ */
4
+
5
+ import test from "node:test";
6
+ import assert from "node:assert/strict";
7
+ import { EncodeAdmission } from "../services/encode/EncodeAdmission.js";
8
+ import { ViewerRequests } from "../services/server/ViewerRequests.js";
9
+
10
+ test("the first opening holds capacity before a concurrent opening is admitted", async () => {
11
+ const records = new Map();
12
+ const outputCosts = new Map([
13
+ ["source-a:0", 0.6],
14
+ ["source-b:0", 0.6]
15
+ ]);
16
+ const viewers = {
17
+ get: () => null,
18
+ forOutput(output) {
19
+ return new Set(records.get(output.outputKey)?.keys() ?? []);
20
+ },
21
+ of(output, consumerId) {
22
+ let viewersOnOutput = records.get(output.outputKey);
23
+ if (!viewersOnOutput) {
24
+ viewersOnOutput = new Map();
25
+ records.set(output.outputKey, viewersOnOutput);
26
+ }
27
+ let viewer = viewersOnOutput.get(consumerId);
28
+ if (!viewer) {
29
+ viewer = {
30
+ position: null,
31
+ linkReading: () => null,
32
+ noteVisiblePicture: () => {},
33
+ // What the request says about them, kept by the real viewer; what
34
+ // this check is about is the place on the machine, not the report.
35
+ report: () => {}
36
+ };
37
+ viewersOnOutput.set(consumerId, viewer);
38
+ }
39
+ return viewer;
40
+ },
41
+ watchedAddresses() {
42
+ return new Set(
43
+ [...records]
44
+ .filter(([, members]) => [...members.values()].some((viewer) => viewer.position !== null))
45
+ .map(([address]) => address)
46
+ );
47
+ }
48
+ };
49
+ const admission = new EncodeAdmission({
50
+ liveRunsByAddress: () => new Map(),
51
+ preparedAddresses: () => new Set(),
52
+ watchedAddresses: () => viewers.watchedAddresses(),
53
+ loadOf: (address) => {
54
+ const costSec = outputCosts.get(address);
55
+ return costSec === undefined ? null : { costSec, fileKey: address, fileCostSec: 0 };
56
+ },
57
+ availability: () => ({ share: 1, known: true }),
58
+ finished: () => false
59
+ });
60
+ const opening = {
61
+ async open({ sourceKey, fileIndex, claim }) {
62
+ const output = { id: `${sourceKey}:${fileIndex}`, outputKey: `${sourceKey}:${fileIndex}` };
63
+ const answer = claim(output);
64
+ if (!answer.admitted) {
65
+ const error = new Error(answer.reason);
66
+ error.code = "OUTPUT_NO_CAPACITY";
67
+ throw error;
68
+ }
69
+ return { output, existed: false, audio: null, verdict: null };
70
+ }
71
+ };
72
+ const requests = new ViewerRequests({
73
+ opening,
74
+ viewers,
75
+ admitsWatching: (output) => admission.admitsWatching(output.outputKey),
76
+ outputTimes: { segmentIndexForTime: (_output, seconds) => Math.floor(seconds / 4) },
77
+ planEncodersSoon: () => {},
78
+ outputs: { touch: () => {} },
79
+ noteServingVerdict: () => {},
80
+ waitUntilReady: async () => {}
81
+ });
82
+
83
+ const first = requests.createOrGetSession({ sourceKey: "source-a", fileIndex: 0, consumerId: "viewer-a" });
84
+ const second = requests.createOrGetSession({ sourceKey: "source-b", fileIndex: 0, consumerId: "viewer-b" });
85
+ const [firstResult, secondResult] = await Promise.allSettled([first, second]);
86
+
87
+ assert.equal(firstResult.status, "fulfilled");
88
+ assert.equal(secondResult.status, "rejected");
89
+ assert.match(secondResult.reason.message, /would make 0\.83x/);
90
+ assert.equal(viewers.forOutput({ outputKey: "source-a:0" }).has("viewer-a"), true);
91
+ assert.equal(viewers.forOutput({ outputKey: "source-b:0" }).has("viewer-b"), false);
92
+ });