rx-player 4.5.0-dev.2026012302 → 4.5.0-dev.2026031700
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.
- package/CHANGELOG.md +21 -3
- package/VERSION +1 -1
- package/dist/commonjs/__GENERATED_CODE/embedded_dash_wasm.d.ts.map +1 -1
- package/dist/commonjs/__GENERATED_CODE/embedded_dash_wasm.js +1 -1
- package/dist/commonjs/__GENERATED_CODE/embedded_worker.d.ts.map +1 -1
- package/dist/commonjs/__GENERATED_CODE/embedded_worker.js +1 -1
- package/dist/commonjs/compat/should_seek_at_zero.d.ts +12 -0
- package/dist/commonjs/compat/should_seek_at_zero.d.ts.map +1 -0
- package/dist/commonjs/compat/should_seek_at_zero.js +23 -0
- package/dist/commonjs/core/cmcd/cmcd_data_builder.js +2 -2
- package/dist/commonjs/core/entry/FreezeResolver.js +2 -2
- package/dist/commonjs/core/entry/content_preparer.d.ts +1 -6
- package/dist/commonjs/core/entry/content_preparer.d.ts.map +1 -1
- package/dist/commonjs/core/entry/content_preparer.js +2 -125
- package/dist/commonjs/core/entry/content_time_boundaries_observer.d.ts.map +1 -1
- package/dist/commonjs/core/entry/content_time_boundaries_observer.js +8 -0
- package/dist/commonjs/core/entry/core_entry.d.ts +1 -1
- package/dist/commonjs/core/entry/core_entry.d.ts.map +1 -1
- package/dist/commonjs/core/entry/core_entry.js +2 -7
- package/dist/commonjs/core/entry/core_text_displayer_interface.d.ts.map +1 -1
- package/dist/commonjs/core/entry/core_text_displayer_interface.js +3 -1
- package/dist/commonjs/core/entry/index.d.ts +1 -1
- package/dist/commonjs/core/entry/index.d.ts.map +1 -1
- package/dist/commonjs/core/entry/utils.d.ts +64 -0
- package/dist/commonjs/core/entry/utils.d.ts.map +1 -1
- package/dist/commonjs/core/entry/utils.js +165 -0
- package/dist/commonjs/core/fetchers/segment/segment_queue.d.ts +5 -6
- package/dist/commonjs/core/fetchers/segment/segment_queue.d.ts.map +1 -1
- package/dist/commonjs/core/fetchers/thumbnails/thumbnail_fetcher.d.ts.map +1 -1
- package/dist/commonjs/core/fetchers/thumbnails/thumbnail_fetcher.js +34 -30
- package/dist/commonjs/core/stream/representation/encryption_data_notifier.d.ts +70 -0
- package/dist/commonjs/core/stream/representation/encryption_data_notifier.d.ts.map +1 -0
- package/dist/commonjs/core/stream/representation/encryption_data_notifier.js +107 -0
- package/dist/commonjs/core/stream/representation/representation_stream.d.ts.map +1 -1
- package/dist/commonjs/core/stream/representation/representation_stream.js +12 -60
- package/dist/commonjs/core/stream/representation/utils/get_needed_segments.js +1 -1
- package/dist/commonjs/default_config.d.ts +5 -0
- package/dist/commonjs/default_config.d.ts.map +1 -1
- package/dist/commonjs/default_config.js +5 -0
- package/dist/commonjs/experimental/features/worker/index.d.ts +2 -1
- package/dist/commonjs/experimental/features/worker/index.d.ts.map +1 -1
- package/dist/commonjs/experimental/features/worker/index.js +4 -3
- package/dist/commonjs/main_thread/api/public_api.d.ts.map +1 -1
- package/dist/commonjs/main_thread/api/public_api.js +28 -4
- package/dist/commonjs/main_thread/init/directfile_content_initializer.d.ts.map +1 -1
- package/dist/commonjs/main_thread/init/directfile_content_initializer.js +23 -1
- package/dist/commonjs/main_thread/init/utils/initial_seek_and_play.d.ts.map +1 -1
- package/dist/commonjs/main_thread/init/utils/initial_seek_and_play.js +3 -1
- package/dist/commonjs/manifest/classes/index.d.ts +2 -1
- package/dist/commonjs/manifest/classes/index.d.ts.map +1 -1
- package/dist/commonjs/manifest/classes/representation.d.ts +1 -1
- package/dist/commonjs/manifest/classes/representation.d.ts.map +1 -1
- package/dist/commonjs/manifest/classes/representation.js +2 -2
- package/dist/commonjs/manifest/classes/representation_index/types.d.ts +2 -2
- package/dist/commonjs/parsers/texttracks/ttml/html/apply_line_height.d.ts.map +1 -1
- package/dist/commonjs/parsers/texttracks/ttml/html/apply_line_height.js +16 -1
- package/dist/commonjs/utils/base64.d.ts +15 -1
- package/dist/commonjs/utils/base64.d.ts.map +1 -1
- package/dist/commonjs/utils/base64.js +6 -0
- package/dist/commonjs/utils/ranges.d.ts +1 -1
- package/dist/commonjs/utils/ranges.js +1 -1
- package/dist/es2017/__GENERATED_CODE/embedded_dash_wasm.d.ts.map +1 -1
- package/dist/es2017/__GENERATED_CODE/embedded_dash_wasm.js +1 -1
- package/dist/es2017/__GENERATED_CODE/embedded_worker.d.ts.map +1 -1
- package/dist/es2017/__GENERATED_CODE/embedded_worker.js +1 -1
- package/dist/es2017/compat/should_seek_at_zero.d.ts +12 -0
- package/dist/es2017/compat/should_seek_at_zero.d.ts.map +1 -0
- package/dist/es2017/compat/should_seek_at_zero.js +20 -0
- package/dist/es2017/core/cmcd/cmcd_data_builder.js +2 -2
- package/dist/es2017/core/entry/FreezeResolver.js +1 -1
- package/dist/es2017/core/entry/content_preparer.d.ts +1 -6
- package/dist/es2017/core/entry/content_preparer.d.ts.map +1 -1
- package/dist/es2017/core/entry/content_preparer.js +1 -76
- package/dist/es2017/core/entry/content_time_boundaries_observer.d.ts.map +1 -1
- package/dist/es2017/core/entry/content_time_boundaries_observer.js +8 -0
- package/dist/es2017/core/entry/core_entry.d.ts +1 -1
- package/dist/es2017/core/entry/core_entry.d.ts.map +1 -1
- package/dist/es2017/core/entry/core_entry.js +2 -7
- package/dist/es2017/core/entry/core_text_displayer_interface.d.ts.map +1 -1
- package/dist/es2017/core/entry/core_text_displayer_interface.js +3 -1
- package/dist/es2017/core/entry/index.d.ts +1 -1
- package/dist/es2017/core/entry/index.d.ts.map +1 -1
- package/dist/es2017/core/entry/utils.d.ts +64 -0
- package/dist/es2017/core/entry/utils.d.ts.map +1 -1
- package/dist/es2017/core/entry/utils.js +98 -0
- package/dist/es2017/core/fetchers/segment/segment_queue.d.ts +5 -6
- package/dist/es2017/core/fetchers/segment/segment_queue.d.ts.map +1 -1
- package/dist/es2017/core/fetchers/thumbnails/thumbnail_fetcher.d.ts.map +1 -1
- package/dist/es2017/core/fetchers/thumbnails/thumbnail_fetcher.js +21 -16
- package/dist/es2017/core/stream/representation/encryption_data_notifier.d.ts +70 -0
- package/dist/es2017/core/stream/representation/encryption_data_notifier.d.ts.map +1 -0
- package/dist/es2017/core/stream/representation/encryption_data_notifier.js +80 -0
- package/dist/es2017/core/stream/representation/representation_stream.d.ts.map +1 -1
- package/dist/es2017/core/stream/representation/representation_stream.js +10 -38
- package/dist/es2017/core/stream/representation/utils/get_needed_segments.js +1 -1
- package/dist/es2017/default_config.d.ts +5 -0
- package/dist/es2017/default_config.d.ts.map +1 -1
- package/dist/es2017/default_config.js +5 -0
- package/dist/es2017/experimental/features/worker/index.d.ts +2 -1
- package/dist/es2017/experimental/features/worker/index.d.ts.map +1 -1
- package/dist/es2017/experimental/features/worker/index.js +2 -3
- package/dist/es2017/main_thread/api/public_api.d.ts.map +1 -1
- package/dist/es2017/main_thread/api/public_api.js +28 -4
- package/dist/es2017/main_thread/init/directfile_content_initializer.d.ts.map +1 -1
- package/dist/es2017/main_thread/init/directfile_content_initializer.js +23 -1
- package/dist/es2017/main_thread/init/utils/initial_seek_and_play.d.ts.map +1 -1
- package/dist/es2017/main_thread/init/utils/initial_seek_and_play.js +3 -1
- package/dist/es2017/manifest/classes/index.d.ts +2 -1
- package/dist/es2017/manifest/classes/index.d.ts.map +1 -1
- package/dist/es2017/manifest/classes/representation.d.ts +1 -1
- package/dist/es2017/manifest/classes/representation.d.ts.map +1 -1
- package/dist/es2017/manifest/classes/representation.js +1 -1
- package/dist/es2017/manifest/classes/representation_index/types.d.ts +2 -2
- package/dist/es2017/parsers/texttracks/ttml/html/apply_line_height.d.ts.map +1 -1
- package/dist/es2017/parsers/texttracks/ttml/html/apply_line_height.js +16 -1
- package/dist/es2017/utils/base64.d.ts +15 -1
- package/dist/es2017/utils/base64.d.ts.map +1 -1
- package/dist/es2017/utils/base64.js +6 -0
- package/dist/es2017/utils/ranges.d.ts +1 -1
- package/dist/es2017/utils/ranges.js +1 -1
- package/dist/mpd-parser.wasm +0 -0
- package/dist/worker.js +8 -8
- package/package.json +26 -23
- package/src/__GENERATED_CODE/embedded_dash_wasm.ts +1 -1
- package/src/__GENERATED_CODE/embedded_worker.ts +1 -1
- package/src/compat/should_seek_at_zero.ts +23 -0
- package/src/core/adaptive/__tests__/adaptive_representation_selector.test.ts +734 -0
- package/src/core/adaptive/__tests__/guess_based_chooser.test.ts +793 -0
- package/src/core/adaptive/__tests__/network_analyzer.test.ts +514 -0
- package/src/core/adaptive/utils/__tests__/bandwidth_estimator.test.ts +117 -0
- package/src/core/adaptive/utils/__tests__/last_estimate_storage.test.ts +76 -0
- package/src/core/adaptive/utils/__tests__/pending_requests_store.test.ts +288 -0
- package/src/core/adaptive/utils/__tests__/representation_score_calculator.test.ts +249 -0
- package/src/core/cmcd/__tests__/cmcd_data_builder.test.ts +464 -0
- package/src/core/cmcd/cmcd_data_builder.ts +2 -2
- package/src/core/entry/FreezeResolver.ts +1 -1
- package/src/core/entry/__tests__/FreezeResolver.test.ts +542 -0
- package/src/core/entry/__tests__/content_time_boundaries_observer.test.ts +867 -0
- package/src/core/entry/__tests__/core_text_displayer_interface.test.ts +205 -0
- package/src/core/entry/__tests__/get_buffered_data_per_media_buffer.test.ts +95 -0
- package/src/core/entry/__tests__/track_choice_setter.test.ts +288 -0
- package/src/core/entry/__tests__/utils.test.ts +309 -0
- package/src/core/entry/content_preparer.ts +7 -118
- package/src/core/entry/content_time_boundaries_observer.ts +10 -0
- package/src/core/entry/core_entry.ts +7 -14
- package/src/core/entry/core_text_displayer_interface.ts +3 -1
- package/src/core/entry/index.ts +1 -1
- package/src/core/entry/utils.ts +151 -0
- package/src/core/fetchers/__tests__/cdn_prioritizer.test.ts +166 -0
- package/src/core/fetchers/manifest/__tests__/manifest_fetcher.test.ts +803 -0
- package/src/core/fetchers/segment/__tests__/segment_fetcher.test.ts +872 -0
- package/src/core/fetchers/segment/__tests__/segment_queue.test.ts +557 -0
- package/src/core/fetchers/segment/segment_queue.ts +5 -6
- package/src/core/fetchers/thumbnails/__tests__/thumbnail_fetcher.test.ts +435 -0
- package/src/core/fetchers/thumbnails/thumbnail_fetcher.ts +28 -20
- package/src/core/fetchers/utils/__tests__/schedule_request.test.ts +331 -0
- package/src/core/stream/adaptation/__tests__/get_representations_switch_strategy.test.ts +593 -0
- package/src/core/stream/period/utils/__tests__/get_adaptation_switch_strategy.test.ts +646 -0
- package/src/core/stream/representation/__tests__/encryption_data_notifier.test.ts +531 -0
- package/src/core/stream/representation/encryption_data_notifier.ts +122 -0
- package/src/core/stream/representation/representation_stream.ts +11 -49
- package/src/core/stream/representation/utils/__tests__/append_segment_to_buffer.test.ts +321 -0
- package/src/core/stream/representation/utils/__tests__/check_for_discontinuity.test.ts +513 -0
- package/src/core/stream/representation/utils/__tests__/get_segment_priority.test.ts +132 -0
- package/src/core/stream/representation/utils/__tests__/push_init_segment.test.ts +209 -0
- package/src/core/stream/representation/utils/__tests__/push_media_segment.test.ts +343 -0
- package/src/core/stream/representation/utils/get_needed_segments.ts +1 -1
- package/src/default_config.ts +6 -0
- package/src/experimental/features/worker/index.ts +2 -4
- package/src/main_thread/api/public_api.ts +33 -4
- package/src/main_thread/init/directfile_content_initializer.ts +26 -1
- package/src/main_thread/init/utils/initial_seek_and_play.ts +5 -1
- package/src/manifest/classes/index.ts +2 -0
- package/src/manifest/classes/representation.ts +2 -1
- package/src/manifest/classes/representation_index/types.ts +2 -2
- package/src/mse/__tests__/main_media_source_interface.test.ts +846 -0
- package/src/parsers/texttracks/ttml/html/apply_line_height.ts +16 -1
- package/src/utils/__tests__/base64.test.ts +159 -191
- package/src/utils/base64.ts +30 -1
- package/src/utils/ranges.ts +1 -1
|
@@ -35,7 +35,22 @@ export default function applyLineHeight(element: HTMLElement, lineHeight: string
|
|
|
35
35
|
}
|
|
36
36
|
if (
|
|
37
37
|
firstLineHeight[2] === "px" ||
|
|
38
|
-
|
|
38
|
+
// TODO: For now, we disable the percentage unit as its rules are complex to
|
|
39
|
+
// implement in our current logic:
|
|
40
|
+
// A percentage `lineHeight` depends on the `fontSize` computed at that point,
|
|
41
|
+
// yet a lineHeight applies to a `p` in TTML and `fontSize` applies to a `span`
|
|
42
|
+
// (below `p`) and our translation applies that same level of style: CSS `lineHeight`
|
|
43
|
+
// is set on the element defining it, but `fontSize` is set at the end on each cue.
|
|
44
|
+
//
|
|
45
|
+
// Thus we cannot easily just rely on CSS' similar behavior for percentage
|
|
46
|
+
// `lineHeight`s because the `fontSize` might not be known at that point by
|
|
47
|
+
// the CSS (yet it might be for TTML). Work-around tricks are not straightforward
|
|
48
|
+
// either (e.g. we cannot just repeat the `fontSize` at both places because
|
|
49
|
+
// the potential percentage values in which they are expressed could coumpound).
|
|
50
|
+
// Seeing other players, it also seems poorly supported, so for now I think
|
|
51
|
+
// we can just ignore those. In a perfect world, it should be implemented
|
|
52
|
+
// though!
|
|
53
|
+
// firstLineHeight[2] === "%" ||
|
|
39
54
|
firstLineHeight[2] === "em"
|
|
40
55
|
) {
|
|
41
56
|
element.style.lineHeight = firstLineHeight[1] + firstLineHeight[2];
|
|
@@ -1,213 +1,181 @@
|
|
|
1
|
-
import { describe,
|
|
2
|
-
import
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
"F",
|
|
31
|
-
"W",
|
|
32
|
-
"n",
|
|
33
|
-
"4",
|
|
34
|
-
"G",
|
|
35
|
-
"X",
|
|
36
|
-
"o",
|
|
37
|
-
"5",
|
|
38
|
-
"H",
|
|
39
|
-
"Y",
|
|
40
|
-
"p",
|
|
41
|
-
"6",
|
|
42
|
-
"I",
|
|
43
|
-
"Z",
|
|
44
|
-
"q",
|
|
45
|
-
"7",
|
|
46
|
-
"J",
|
|
47
|
-
"a",
|
|
48
|
-
"r",
|
|
49
|
-
"8",
|
|
50
|
-
"K",
|
|
51
|
-
"b",
|
|
52
|
-
"s",
|
|
53
|
-
"9",
|
|
54
|
-
"L",
|
|
55
|
-
"c",
|
|
56
|
-
"t",
|
|
57
|
-
"+",
|
|
58
|
-
"M",
|
|
59
|
-
"d",
|
|
60
|
-
"u",
|
|
61
|
-
"/",
|
|
62
|
-
"N",
|
|
63
|
-
"e",
|
|
64
|
-
"v",
|
|
65
|
-
"O",
|
|
66
|
-
"f",
|
|
67
|
-
"w",
|
|
68
|
-
"P",
|
|
69
|
-
"g",
|
|
70
|
-
"x",
|
|
71
|
-
"Q",
|
|
72
|
-
"h",
|
|
73
|
-
"y",
|
|
74
|
-
];
|
|
75
|
-
|
|
76
|
-
describe("base64ToBytes", () => {
|
|
77
|
-
const logWarn = vi.fn();
|
|
1
|
+
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
|
|
2
|
+
import { bytesToBase64, base64ToBytes } from "../base64";
|
|
3
|
+
|
|
4
|
+
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
|
|
5
|
+
/* eslint-disable @typescript-eslint/unbound-method */
|
|
6
|
+
|
|
7
|
+
const { mockWarn } = vi.hoisted(() => {
|
|
8
|
+
return { mockWarn: vi.fn() };
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
vi.mock("../../log", () => ({
|
|
12
|
+
default: {
|
|
13
|
+
warn: mockWarn,
|
|
14
|
+
},
|
|
15
|
+
}));
|
|
16
|
+
|
|
17
|
+
function removeNativeBytesToBase64() {
|
|
18
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
19
|
+
delete (Uint8Array.prototype as any).toBase64;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function removeNativeBase64ToBytes() {
|
|
23
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
24
|
+
delete (Uint8Array as any).fromBase64;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
describe("bytesToBase64 – non-native fallback", () => {
|
|
28
|
+
let originalToBase64: ((options?: object) => string) | undefined;
|
|
29
|
+
|
|
78
30
|
beforeEach(() => {
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
})
|
|
31
|
+
mockWarn.mockClear();
|
|
32
|
+
// Stash and remove the native method so the fallback is always exercised
|
|
33
|
+
originalToBase64 = (Uint8Array.prototype as { toBase64?: () => string }).toBase64;
|
|
34
|
+
removeNativeBytesToBase64();
|
|
82
35
|
});
|
|
36
|
+
|
|
83
37
|
afterEach(() => {
|
|
84
|
-
|
|
85
|
-
|
|
38
|
+
if (originalToBase64 !== undefined) {
|
|
39
|
+
(Uint8Array.prototype as { toBase64?: () => string }).toBase64 = originalToBase64;
|
|
40
|
+
}
|
|
86
41
|
});
|
|
87
42
|
|
|
88
|
-
it("
|
|
89
|
-
|
|
90
|
-
.base64ToBytes as typeof IBase64ToBytes;
|
|
91
|
-
expect(base64ToBytes("")).toEqual(new Uint8Array([]));
|
|
92
|
-
expect(logWarn).not.toHaveBeenCalled();
|
|
43
|
+
it("encodes an empty Uint8Array to an empty string", () => {
|
|
44
|
+
expect(bytesToBase64(new Uint8Array([]))).toBe("");
|
|
93
45
|
});
|
|
94
46
|
|
|
95
|
-
it("
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
expect(base64ToBytes("woDCge+/vg==")).toEqual(
|
|
99
|
-
new Uint8Array([194, 128, 194, 129, 239, 191, 190]),
|
|
100
|
-
);
|
|
101
|
-
expect(base64ToBytes("dG90b/CfjIM=")).toEqual(
|
|
102
|
-
new Uint8Array([116, 111, 116, 111, 240, 159, 140, 131]),
|
|
103
|
-
);
|
|
104
|
-
expect(base64ToBytes("JGV4cGVjdChiYXNlNjRUb1VpbnQ4QXJyYXkoIiIp")).toEqual(
|
|
105
|
-
new Uint8Array([
|
|
106
|
-
36, 101, 120, 112, 101, 99, 116, 40, 98, 97, 115, 101, 54, 52, 84, 111, 85, 105,
|
|
107
|
-
110, 116, 56, 65, 114, 114, 97, 121, 40, 34, 34, 41,
|
|
108
|
-
]),
|
|
109
|
-
);
|
|
110
|
-
expect(base64ToBytes(globalScope.btoa("toto"))).toEqual(
|
|
111
|
-
new Uint8Array([116, 111, 116, 111]),
|
|
112
|
-
);
|
|
113
|
-
expect(logWarn).not.toHaveBeenCalled();
|
|
114
|
-
|
|
115
|
-
const bytesToBase64 = (await vi.importActual("../base64"))
|
|
116
|
-
.bytesToBase64 as typeof IBytesToBase64;
|
|
117
|
-
|
|
118
|
-
for (let i = 0; i < CHARS.length; i++) {
|
|
119
|
-
const char1 = CHARS[i];
|
|
120
|
-
for (let j = 0; j < CHARS.length; j++) {
|
|
121
|
-
const char2 = CHARS[j];
|
|
122
|
-
// for (let k = 0; k < CHARS.length; k++) {
|
|
123
|
-
// const char3 = CHARS[j];
|
|
124
|
-
// for (let l = 0; l < CHARS.length; l++) {
|
|
125
|
-
// const char4 = CHARS[j];
|
|
126
|
-
const combination = char1 + char2 + char2 + char1;
|
|
127
|
-
expect(bytesToBase64(base64ToBytes(combination))).toEqual(combination);
|
|
128
|
-
// }
|
|
129
|
-
// }
|
|
130
|
-
}
|
|
131
|
-
}
|
|
47
|
+
it("encodes a single byte – 1 octet path (== padding)", () => {
|
|
48
|
+
// 0x4D => "TQ=="
|
|
49
|
+
expect(bytesToBase64(new Uint8Array([0x4d]))).toBe("TQ==");
|
|
132
50
|
});
|
|
133
51
|
|
|
134
|
-
it("
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
expect(base64ToBytes("woDCge+/vg")).toEqual(
|
|
138
|
-
new Uint8Array([194, 128, 194, 129, 239, 191, 190]),
|
|
139
|
-
);
|
|
140
|
-
expect(logWarn).toHaveBeenCalledTimes(1);
|
|
141
|
-
expect(logWarn).toHaveBeenNthCalledWith(
|
|
142
|
-
1,
|
|
143
|
-
"utils",
|
|
144
|
-
"base64ToBytes: base64 given miss padding",
|
|
145
|
-
{ padding: 2 },
|
|
146
|
-
);
|
|
147
|
-
expect(base64ToBytes("dG90b/CfjIM")).toEqual(
|
|
148
|
-
new Uint8Array([116, 111, 116, 111, 240, 159, 140, 131]),
|
|
149
|
-
);
|
|
150
|
-
expect(logWarn).toHaveBeenCalledTimes(2);
|
|
151
|
-
expect(logWarn).toHaveBeenNthCalledWith(
|
|
152
|
-
2,
|
|
153
|
-
"utils",
|
|
154
|
-
"base64ToBytes: base64 given miss padding",
|
|
155
|
-
{ padding: 3 },
|
|
156
|
-
);
|
|
52
|
+
it("encodes two bytes – 2 octet path (= padding)", () => {
|
|
53
|
+
// 0x4D 0x61 => "TWE="
|
|
54
|
+
expect(bytesToBase64(new Uint8Array([0x4d, 0x61]))).toBe("TWE=");
|
|
157
55
|
});
|
|
158
56
|
|
|
159
|
-
it("
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
);
|
|
57
|
+
it("encodes three bytes – full group, no padding", () => {
|
|
58
|
+
// 0x4D 0x61 0x6E => "TWFu"
|
|
59
|
+
expect(bytesToBase64(new Uint8Array([0x4d, 0x61, 0x6e]))).toBe("TWFu");
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it("encodes multiple full groups", () => {
|
|
63
|
+
const bytes = new Uint8Array([0x4d, 0x61, 0x6e, 0x4d, 0x61, 0x6e]);
|
|
64
|
+
expect(bytesToBase64(bytes)).toBe("TWFuTWFu");
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it("encodes all-zero bytes", () => {
|
|
68
|
+
expect(bytesToBase64(new Uint8Array([0, 0, 0]))).toBe("AAAA");
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it("encodes all-max-value bytes", () => {
|
|
72
|
+
expect(bytesToBase64(new Uint8Array([255, 255, 255]))).toBe("////");
|
|
73
|
+
});
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
describe("bytesToBase64 – native path", () => {
|
|
77
|
+
it("delegates to toBase64 when it exists on the instance", () => {
|
|
78
|
+
const nativeResult = "nativeBase64==";
|
|
79
|
+
const bytes = new Uint8Array([1, 2, 3]) as Uint8Array & { toBase64: () => string };
|
|
80
|
+
bytes.toBase64 = vi.fn().mockReturnValue(nativeResult);
|
|
81
|
+
expect(bytesToBase64(bytes)).toBe(nativeResult);
|
|
82
|
+
expect(bytes.toBase64).toHaveBeenCalledOnce();
|
|
171
83
|
});
|
|
172
84
|
});
|
|
173
85
|
|
|
174
|
-
describe("
|
|
175
|
-
|
|
86
|
+
describe("base64ToBytes – non-native fallback", () => {
|
|
87
|
+
let originalFromBase64: ((base64: string, options?: object) => Uint8Array) | undefined;
|
|
88
|
+
|
|
176
89
|
beforeEach(() => {
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
}));
|
|
90
|
+
mockWarn.mockClear();
|
|
91
|
+
// Stash and remove the native static method so the fallback is always exercised
|
|
92
|
+
originalFromBase64 = Uint8Array.fromBase64;
|
|
93
|
+
removeNativeBase64ToBytes();
|
|
182
94
|
});
|
|
183
95
|
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
96
|
+
afterEach(() => {
|
|
97
|
+
if (originalFromBase64 !== undefined) {
|
|
98
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
99
|
+
(Uint8Array as any).fromBase64 = originalFromBase64;
|
|
100
|
+
}
|
|
189
101
|
});
|
|
190
102
|
|
|
191
|
-
it("
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
).toEqual(
|
|
208
|
-
|
|
209
|
-
|
|
103
|
+
it("decodes a standard base64 string with no padding needed", () => {
|
|
104
|
+
// "TWFu" => "Man"
|
|
105
|
+
expect(base64ToBytes("TWFu")).toEqual(new Uint8Array([0x4d, 0x61, 0x6e]));
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
it("decodes base64 with a single trailing padding char", () => {
|
|
109
|
+
// "TWE=" => "Ma"
|
|
110
|
+
expect(base64ToBytes("TWE=")).toEqual(new Uint8Array([0x4d, 0x61]));
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
it("decodes base64 with double trailing padding chars", () => {
|
|
114
|
+
// "TQ==" => "M"
|
|
115
|
+
expect(base64ToBytes("TQ==")).toEqual(new Uint8Array([0x4d]));
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
it("decodes an empty string to an empty Uint8Array", () => {
|
|
119
|
+
expect(base64ToBytes("")).toEqual(new Uint8Array([]));
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
it("decodes multiple full groups", () => {
|
|
123
|
+
expect(base64ToBytes("TWFuTWFu")).toEqual(
|
|
124
|
+
new Uint8Array([0x4d, 0x61, 0x6e, 0x4d, 0x61, 0x6e]),
|
|
210
125
|
);
|
|
211
|
-
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
it("warns and recovers when 1 padding char is missing (length % 4 === 3)", () => {
|
|
129
|
+
// "TWE" is "TWE=" without the trailing "="
|
|
130
|
+
const result = base64ToBytes("TWE");
|
|
131
|
+
expect(mockWarn).toHaveBeenCalledOnce();
|
|
132
|
+
expect(result).toEqual(new Uint8Array([0x4d, 0x61]));
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
it("warns and recovers when 2 padding chars are missing (length % 4 === 2)", () => {
|
|
136
|
+
// "TQ" is "TQ==" without the trailing "=="
|
|
137
|
+
const result = base64ToBytes("TQ");
|
|
138
|
+
expect(mockWarn).toHaveBeenCalledOnce();
|
|
139
|
+
expect(result).toEqual(new Uint8Array([0x4d]));
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
it("does not warn when input is already correctly padded", () => {
|
|
143
|
+
base64ToBytes("TWFu");
|
|
144
|
+
expect(mockWarn).not.toHaveBeenCalled();
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
it("throws when an invalid base64 character is encountered", () => {
|
|
148
|
+
expect(() => base64ToBytes("TQ=!")).toThrow("Unable to parse base64 string.");
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
it("throws when a high char code (>= 123) is encountered", () => {
|
|
152
|
+
// '~' has char code 126, beyond the base64codes table
|
|
153
|
+
expect(() => base64ToBytes("TQ=~")).toThrow("Unable to parse base64 string.");
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
it("throws when a padding char appears in a non-terminal position", () => {
|
|
157
|
+
expect(() => base64ToBytes("TQ==A")).toThrow("Unable to parse base64 string.");
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
it("is the inverse of bytesToBase64 for arbitrary byte sequences", () => {
|
|
161
|
+
const original = new Uint8Array([0, 1, 127, 128, 200, 255, 42, 7]);
|
|
162
|
+
const encoded = bytesToBase64(original);
|
|
163
|
+
expect(base64ToBytes(encoded)).toEqual(original);
|
|
164
|
+
});
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
describe("base64ToBytes – native path", () => {
|
|
168
|
+
it("delegates to Uint8Array.fromBase64 when it exists", () => {
|
|
169
|
+
const fakeResult = new Uint8Array([1, 2, 3]);
|
|
170
|
+
const spy = vi.fn().mockReturnValue(fakeResult);
|
|
171
|
+
(Uint8Array as { fromBase64?: unknown }).fromBase64 = spy;
|
|
172
|
+
|
|
173
|
+
try {
|
|
174
|
+
const result = base64ToBytes("AQID");
|
|
175
|
+
expect(spy).toHaveBeenCalledWith("AQID");
|
|
176
|
+
expect(result).toBe(fakeResult);
|
|
177
|
+
} finally {
|
|
178
|
+
delete (Uint8Array as { fromBase64?: unknown }).fromBase64;
|
|
179
|
+
}
|
|
212
180
|
});
|
|
213
181
|
});
|
package/src/utils/base64.ts
CHANGED
|
@@ -126,6 +126,29 @@ const base64codes = [
|
|
|
126
126
|
47, 48, 49, 50, 51,
|
|
127
127
|
];
|
|
128
128
|
|
|
129
|
+
// Not yet in ECMAScript, but present in most browsers
|
|
130
|
+
// (2024 for SpiderMonkey, 2025 for V8)
|
|
131
|
+
declare global {
|
|
132
|
+
interface Uint8Array {
|
|
133
|
+
// https://tc39.es/proposal-arraybuffer-base64/spec/#sec-uint8array.prototype.tobase64
|
|
134
|
+
toBase64(options?: {
|
|
135
|
+
alphabet?: "base64" | "base64url";
|
|
136
|
+
omitPadding?: boolean;
|
|
137
|
+
}): string;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
interface Uint8ArrayConstructor {
|
|
141
|
+
// https://tc39.es/proposal-arraybuffer-base64/spec/#sec-uint8array.frombase64
|
|
142
|
+
fromBase64(
|
|
143
|
+
base64: string,
|
|
144
|
+
options?: {
|
|
145
|
+
alphabet?: "base64" | "base64url";
|
|
146
|
+
lastChunkHandling?: "loose" | "strict" | "stop-before-partial";
|
|
147
|
+
},
|
|
148
|
+
): Uint8Array<ArrayBuffer>;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
129
152
|
/**
|
|
130
153
|
* Obtain the value corresponding to a base64 char code.
|
|
131
154
|
* /!\ Can throw if the char code given is invalid.
|
|
@@ -148,7 +171,10 @@ function getBase64Code(charCode: number): number {
|
|
|
148
171
|
* @param {Array.<number>|Uint8Array} bytes
|
|
149
172
|
* @returns {string}
|
|
150
173
|
*/
|
|
151
|
-
export function bytesToBase64(bytes:
|
|
174
|
+
export function bytesToBase64(bytes: Uint8Array): string {
|
|
175
|
+
if (typeof bytes.toBase64 === "function") {
|
|
176
|
+
return bytes.toBase64();
|
|
177
|
+
}
|
|
152
178
|
let result = "";
|
|
153
179
|
let i;
|
|
154
180
|
const length = bytes.length;
|
|
@@ -182,6 +208,9 @@ export function bytesToBase64(bytes: number[] | Uint8Array): string {
|
|
|
182
208
|
* @returns {string}
|
|
183
209
|
*/
|
|
184
210
|
export function base64ToBytes(str: string): Uint8Array<ArrayBuffer> {
|
|
211
|
+
if (typeof Uint8Array.fromBase64 === "function") {
|
|
212
|
+
return Uint8Array.fromBase64(str);
|
|
213
|
+
}
|
|
185
214
|
const paddingNeeded = str.length % 4;
|
|
186
215
|
let paddedStr = str;
|
|
187
216
|
if (paddingNeeded !== 0) {
|
package/src/utils/ranges.ts
CHANGED
|
@@ -135,7 +135,7 @@ function isTimeInRanges(ranges: IRange[], time: number): boolean {
|
|
|
135
135
|
/**
|
|
136
136
|
* Returns true if the time given can be considered as part of the given range.
|
|
137
137
|
* @param {Object} range1
|
|
138
|
-
* @param {
|
|
138
|
+
* @param {number} time
|
|
139
139
|
* @returns {Boolean}
|
|
140
140
|
*/
|
|
141
141
|
function isTimeInRange({ start, end }: IRange, time: number): boolean {
|