create-portfoliable 1.2.11 → 1.2.12
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/package.json +1 -1
- package/src/App.js +5 -25
- package/src/runtime-media.contract.js +61 -0
- package/src/runtime-media.contract.test.js +96 -0
package/package.json
CHANGED
package/src/App.js
CHANGED
|
@@ -26,6 +26,7 @@ import {
|
|
|
26
26
|
resolveHeaderVisibilityAttributeValue,
|
|
27
27
|
resolveReturnOnlyVisibilityOverrides
|
|
28
28
|
} from './header-visibility.contract.js';
|
|
29
|
+
import { pickReachableRuntimeMediaUrl } from './runtime-media.contract.js';
|
|
29
30
|
import Lenis from 'lenis';
|
|
30
31
|
import {
|
|
31
32
|
AudioPlayer,
|
|
@@ -5435,9 +5436,7 @@ class AppShell extends HTMLElement {
|
|
|
5435
5436
|
});
|
|
5436
5437
|
}
|
|
5437
5438
|
|
|
5438
|
-
const videoSrcCandidates = this._buildRuntimeMediaCandidates(rawVideoSrc
|
|
5439
|
-
preferTemplatePrefix: true
|
|
5440
|
-
});
|
|
5439
|
+
const videoSrcCandidates = this._buildRuntimeMediaCandidates(rawVideoSrc);
|
|
5441
5440
|
|
|
5442
5441
|
if (videoSrcCandidates.length > 1) {
|
|
5443
5442
|
const resolveToken = `${activeCaseId}:${activeLocale}:${Date.now()}`;
|
|
@@ -5791,28 +5790,9 @@ class AppShell extends HTMLElement {
|
|
|
5791
5790
|
return candidates[0] || '';
|
|
5792
5791
|
}
|
|
5793
5792
|
|
|
5794
|
-
|
|
5795
|
-
|
|
5796
|
-
|
|
5797
|
-
if (/^(?:https?:|data:|blob:)/i.test(candidate)) {
|
|
5798
|
-
return candidate;
|
|
5799
|
-
}
|
|
5800
|
-
|
|
5801
|
-
try {
|
|
5802
|
-
const response = await fetch(candidate, {
|
|
5803
|
-
method: 'HEAD',
|
|
5804
|
-
cache: 'no-store'
|
|
5805
|
-
});
|
|
5806
|
-
|
|
5807
|
-
if (response.ok) {
|
|
5808
|
-
return candidate;
|
|
5809
|
-
}
|
|
5810
|
-
} catch {
|
|
5811
|
-
// Try next candidate when HEAD checks are unavailable.
|
|
5812
|
-
}
|
|
5813
|
-
}
|
|
5814
|
-
|
|
5815
|
-
return candidates[0] || '';
|
|
5793
|
+
// Resolves to '' when no candidate is confirmed so callers keep the already bound source.
|
|
5794
|
+
_pickReachableRuntimeMediaUrl(candidates = []) {
|
|
5795
|
+
return pickReachableRuntimeMediaUrl(candidates);
|
|
5816
5796
|
}
|
|
5817
5797
|
|
|
5818
5798
|
_buildRuntimeImageCandidates(value) {
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
// File: src/runtime-media.contract.js
|
|
2
|
+
// Purpose: Confirm that a runtime media candidate URL points at real media before it is bound.
|
|
3
|
+
// Author: Lio Schimanko
|
|
4
|
+
|
|
5
|
+
// MARK: RESPONSE CLASSIFICATION
|
|
6
|
+
// Dev servers answer unknown paths with an HTML SPA fallback at status 200, so HTML must never count as media.
|
|
7
|
+
export function isRuntimeMediaResponse(response) {
|
|
8
|
+
if (!response || !response.ok) return false;
|
|
9
|
+
|
|
10
|
+
const contentType = String(response.headers?.get?.('content-type') || '').toLowerCase();
|
|
11
|
+
if (!contentType) return true;
|
|
12
|
+
|
|
13
|
+
return !(contentType.startsWith('text/html') || contentType.startsWith('application/xhtml'));
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
// MARK: CANDIDATE PROBING
|
|
17
|
+
// Returns '' when no candidate is confirmed so callers keep the already bound source.
|
|
18
|
+
export async function pickReachableRuntimeMediaUrl(candidates = [], fetchImpl = globalThis.fetch) {
|
|
19
|
+
if (typeof fetchImpl !== 'function') return '';
|
|
20
|
+
|
|
21
|
+
for (const candidate of candidates) {
|
|
22
|
+
if (!candidate) continue;
|
|
23
|
+
if (/^(?:https?:|data:|blob:)/i.test(candidate)) {
|
|
24
|
+
return candidate;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
let headResponse = null;
|
|
28
|
+
try {
|
|
29
|
+
headResponse = await fetchImpl(candidate, {
|
|
30
|
+
method: 'HEAD',
|
|
31
|
+
cache: 'no-store'
|
|
32
|
+
});
|
|
33
|
+
} catch {
|
|
34
|
+
headResponse = null;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
if (isRuntimeMediaResponse(headResponse)) {
|
|
38
|
+
return candidate;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const headUnsupported = !headResponse || headResponse.status === 405 || headResponse.status === 501;
|
|
42
|
+
if (!headUnsupported) continue;
|
|
43
|
+
|
|
44
|
+
try {
|
|
45
|
+
// Single-byte range confirms the resource without downloading the media payload.
|
|
46
|
+
const rangedResponse = await fetchImpl(candidate, {
|
|
47
|
+
method: 'GET',
|
|
48
|
+
cache: 'no-store',
|
|
49
|
+
headers: { Range: 'bytes=0-0' }
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
if (isRuntimeMediaResponse(rangedResponse)) {
|
|
53
|
+
return candidate;
|
|
54
|
+
}
|
|
55
|
+
} catch {
|
|
56
|
+
// Try next candidate when ranged probing is unavailable.
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
return '';
|
|
61
|
+
}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
// File: src/runtime-media.contract.test.js
|
|
2
|
+
// Purpose: Validate that media candidate probing rejects SPA fallbacks and never guesses a source.
|
|
3
|
+
// Author: Lio Schimanko
|
|
4
|
+
|
|
5
|
+
// MARK: IMPORTS
|
|
6
|
+
import { describe, expect, it, vi } from 'vitest';
|
|
7
|
+
|
|
8
|
+
import {
|
|
9
|
+
isRuntimeMediaResponse,
|
|
10
|
+
pickReachableRuntimeMediaUrl,
|
|
11
|
+
} from './runtime-media.contract.js';
|
|
12
|
+
|
|
13
|
+
// MARK: TEST HELPERS
|
|
14
|
+
// Builds a minimal fetch Response stand-in with the headers the probe inspects.
|
|
15
|
+
function makeResponse({ status = 200, contentType = '' } = {}) {
|
|
16
|
+
return {
|
|
17
|
+
ok: status >= 200 && status < 300,
|
|
18
|
+
status,
|
|
19
|
+
headers: { get: (name) => (name.toLowerCase() === 'content-type' ? contentType : null) },
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const CANONICAL = '/src/content/cases/template-case/template-video.mp4';
|
|
24
|
+
const TEMPLATE = '/templates/src/content/cases/template-case/template-video.mp4';
|
|
25
|
+
|
|
26
|
+
// MARK: RESPONSE CLASSIFICATION TESTS
|
|
27
|
+
describe('isRuntimeMediaResponse', () => {
|
|
28
|
+
// Vite answers unknown dev paths with index.html at status 200.
|
|
29
|
+
it('rejects HTML responses served as a dev-server SPA fallback', () => {
|
|
30
|
+
expect(isRuntimeMediaResponse(makeResponse({ contentType: 'text/html; charset=utf-8' }))).toBe(false);
|
|
31
|
+
expect(isRuntimeMediaResponse(makeResponse({ contentType: 'application/xhtml+xml' }))).toBe(false);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
// Real media and untyped static responses must stay acceptable.
|
|
35
|
+
it('accepts media and content-type-less responses', () => {
|
|
36
|
+
expect(isRuntimeMediaResponse(makeResponse({ contentType: 'video/mp4' }))).toBe(true);
|
|
37
|
+
expect(isRuntimeMediaResponse(makeResponse({ contentType: 'application/octet-stream' }))).toBe(true);
|
|
38
|
+
expect(isRuntimeMediaResponse(makeResponse({ contentType: '' }))).toBe(true);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
// Non-success statuses and missing responses are never media.
|
|
42
|
+
it('rejects failed and absent responses', () => {
|
|
43
|
+
expect(isRuntimeMediaResponse(makeResponse({ status: 404, contentType: 'video/mp4' }))).toBe(false);
|
|
44
|
+
expect(isRuntimeMediaResponse(null)).toBe(false);
|
|
45
|
+
});
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
// MARK: CANDIDATE PROBING TESTS
|
|
49
|
+
describe('pickReachableRuntimeMediaUrl', () => {
|
|
50
|
+
// Regression: the SPA fallback used to win and overwrite the working source.
|
|
51
|
+
it('skips a candidate answered by the SPA fallback and keeps probing', async () => {
|
|
52
|
+
const fetchImpl = vi.fn(async (url) =>
|
|
53
|
+
url === CANONICAL
|
|
54
|
+
? makeResponse({ contentType: 'video/mp4' })
|
|
55
|
+
: makeResponse({ contentType: 'text/html' }));
|
|
56
|
+
|
|
57
|
+
await expect(pickReachableRuntimeMediaUrl([TEMPLATE, CANONICAL], fetchImpl)).resolves.toBe(CANONICAL);
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
// Absolute URLs are trusted without a network round-trip.
|
|
61
|
+
it('returns absolute URLs without probing', async () => {
|
|
62
|
+
const fetchImpl = vi.fn();
|
|
63
|
+
|
|
64
|
+
await expect(pickReachableRuntimeMediaUrl(['https://cdn.test/video.mp4'], fetchImpl))
|
|
65
|
+
.resolves.toBe('https://cdn.test/video.mp4');
|
|
66
|
+
expect(fetchImpl).not.toHaveBeenCalled();
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
// Some static hosts answer HEAD with 405, so a single-byte range confirms the file instead.
|
|
70
|
+
it('falls back to a ranged GET when HEAD is unsupported', async () => {
|
|
71
|
+
const fetchImpl = vi.fn(async (_url, options) =>
|
|
72
|
+
options.method === 'HEAD'
|
|
73
|
+
? makeResponse({ status: 405 })
|
|
74
|
+
: makeResponse({ status: 206, contentType: 'video/mp4' }));
|
|
75
|
+
|
|
76
|
+
await expect(pickReachableRuntimeMediaUrl([CANONICAL], fetchImpl)).resolves.toBe(CANONICAL);
|
|
77
|
+
expect(fetchImpl).toHaveBeenCalledTimes(2);
|
|
78
|
+
expect(fetchImpl.mock.calls[1][1].headers).toEqual({ Range: 'bytes=0-0' });
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
// Returning '' is what stops the caller from replacing an already bound source.
|
|
82
|
+
it('returns an empty string when no candidate is confirmed', async () => {
|
|
83
|
+
const fetchImpl = vi.fn(async () => makeResponse({ contentType: 'text/html' }));
|
|
84
|
+
|
|
85
|
+
await expect(pickReachableRuntimeMediaUrl([TEMPLATE, CANONICAL], fetchImpl)).resolves.toBe('');
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
// Network failures must not promote an unverified candidate either.
|
|
89
|
+
it('returns an empty string when probing throws', async () => {
|
|
90
|
+
const fetchImpl = vi.fn(async () => {
|
|
91
|
+
throw new Error('network down');
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
await expect(pickReachableRuntimeMediaUrl([TEMPLATE, CANONICAL], fetchImpl)).resolves.toBe('');
|
|
95
|
+
});
|
|
96
|
+
});
|