feat: Add proxying support for images (#2427)

- Closes #2418
This commit is contained in:
Dermot Duffy
2026-06-30 17:45:12 -07:00
committed by dermotduffy
parent 9384785d37
commit 1cd5520154
51 changed files with 2404 additions and 687 deletions
+56 -97
View File
@@ -1,10 +1,9 @@
import { describe, expect, it } from 'vitest';
import { CameraProxyConfig } from '../../src/camera-manager/types.js';
import { EnabledProxyConfig } from '../../src/config/schema/common/proxy.js';
import {
addDynamicProxyURL,
createProxiedEndpointIfNecessary,
getWebProxiedURL,
shouldUseWebProxy,
} from '../../src/ha/web-proxy.js';
import { createHASS } from '../test-utils.js';
@@ -28,50 +27,19 @@ describe('getWebProxiedURL', () => {
});
});
describe('shouldUseWebProxy', () => {
const createProxyConfig = (
config: Partial<CameraProxyConfig> = {},
): CameraProxyConfig => ({
media: true,
live: true,
ssl_verification: true,
ssl_ciphers: 'default',
dynamic: true,
...config,
});
it('should return false without a the proxy installed', () => {
const hass = createHASS();
hass.config.components = [];
expect(shouldUseWebProxy(hass, createProxyConfig())).toBe(false);
});
it('should return when proxy config does not want proxying', () => {
const hass = createHASS();
hass.config.components = ['hass_web_proxy'];
const proxyConfig = createProxyConfig({ media: false });
expect(shouldUseWebProxy(hass, proxyConfig, 'media')).toBe(false);
});
it('should return when proxy config does want proxying', () => {
const hass = createHASS();
hass.config.components = ['hass_web_proxy'];
const proxyConfig = createProxyConfig({ media: true });
expect(shouldUseWebProxy(hass, proxyConfig, 'media')).toBe(true);
});
});
describe('addDynamicProxyURL', () => {
it('should add dynamic proxy URL', async () => {
it('should add dynamic proxy URL with proxy config', async () => {
const hass = createHASS();
const proxyConfig: EnabledProxyConfig = {
dynamic: true,
ssl_verification: true,
ssl_ciphers: 'modern',
enabled: true,
};
await addDynamicProxyURL(hass, 'http://example.com', {
proxyConfig,
urlID: 'id',
sslVerification: true,
sslCiphers: 'modern',
openLimit: 5,
ttl: 60,
allowUnauthenticated: false,
@@ -92,40 +60,29 @@ describe('addDynamicProxyURL', () => {
);
});
it('should add dynamic proxy URL using config defaults', async () => {
it('should add dynamic proxy URL without options', async () => {
const hass = createHASS();
const proxyConfig: CameraProxyConfig = {
media: true,
live: true,
ssl_verification: false,
ssl_ciphers: 'insecure',
dynamic: true,
};
await addDynamicProxyURL(hass, 'http://example.com', {
proxyConfig: proxyConfig,
});
await addDynamicProxyURL(hass, 'http://example.com');
expect(hass.callService).toHaveBeenCalledWith(
'hass_web_proxy',
'create_proxied_url',
expect.objectContaining({
ssl_verification: false,
ssl_ciphers: 'insecure',
}),
{
url_pattern: 'http://example.com',
},
);
});
});
describe('createProxiedEndpointIfNecessary', () => {
const createProxyConfig = (
config: Partial<CameraProxyConfig> = {},
): CameraProxyConfig => ({
media: true,
live: true,
const createEnabledProxyConfig = (
config: Partial<EnabledProxyConfig> = {},
): EnabledProxyConfig => ({
ssl_verification: true,
ssl_ciphers: 'default',
dynamic: true,
enabled: true,
...config,
});
@@ -133,33 +90,41 @@ describe('createProxiedEndpointIfNecessary', () => {
it('should return original endpoint when proxyConfig is undefined', async () => {
const hass = createHASS();
hass.config.components = ['hass_web_proxy'];
const result = await createProxiedEndpointIfNecessary(hass, testEndpoint);
expect(result).toBe(testEndpoint);
});
it('should return original endpoint when proxy is not available', async () => {
const hass = createHASS();
hass.config.components = [];
const result = await createProxiedEndpointIfNecessary(
hass,
testEndpoint,
createProxyConfig(),
);
expect(result).toBe(testEndpoint);
});
it('should return original endpoint when context is not enabled', async () => {
it('should return original endpoint when enabled is false', async () => {
const hass = createHASS();
hass.config.components = ['hass_web_proxy'];
const result = await createProxiedEndpointIfNecessary(
hass,
testEndpoint,
createProxyConfig({ media: false }),
{ context: 'media' },
createEnabledProxyConfig({ enabled: false }),
);
expect(result).toBe(testEndpoint);
});
it('should return null when web proxy is not available and enforced', async () => {
const hass = createHASS();
const result = await createProxiedEndpointIfNecessary(
hass,
testEndpoint,
createEnabledProxyConfig({ enforce: true }),
);
expect(result).toBeNull();
});
it('should return original endpoint when web proxy is not available but not enforced', async () => {
const hass = createHASS();
const result = await createProxiedEndpointIfNecessary(
hass,
testEndpoint,
createEnabledProxyConfig(),
);
expect(result).toBe(testEndpoint);
});
@@ -171,8 +136,8 @@ describe('createProxiedEndpointIfNecessary', () => {
const result = await createProxiedEndpointIfNecessary(
hass,
testEndpoint,
createProxyConfig(),
{ context: 'media', ttl: 300, openLimit: 5 },
createEnabledProxyConfig(),
{ ttl: 300, openLimit: 5 },
);
expect(hass.callService).toHaveBeenCalledWith(
@@ -200,7 +165,11 @@ describe('createProxiedEndpointIfNecessary', () => {
sign: false,
};
await createProxiedEndpointIfNecessary(hass, endpointWithHash, createProxyConfig());
await createProxiedEndpointIfNecessary(
hass,
endpointWithHash,
createEnabledProxyConfig(),
);
expect(hass.callService).toHaveBeenCalledWith(
'hass_web_proxy',
@@ -218,7 +187,7 @@ describe('createProxiedEndpointIfNecessary', () => {
const result = await createProxiedEndpointIfNecessary(
hass,
testEndpoint,
createProxyConfig({ dynamic: false }),
createEnabledProxyConfig({ dynamic: false }),
);
expect(hass.callService).not.toHaveBeenCalled();
@@ -235,7 +204,7 @@ describe('createProxiedEndpointIfNecessary', () => {
const result = await createProxiedEndpointIfNecessary(
hass,
testEndpoint,
createProxyConfig({ dynamic: false }),
createEnabledProxyConfig({ dynamic: false }),
{ websocket: true },
);
@@ -245,25 +214,15 @@ describe('createProxiedEndpointIfNecessary', () => {
});
});
it('should use live context when specified', async () => {
const hass = createHASS();
hass.config.components = ['hass_web_proxy'];
const result = await createProxiedEndpointIfNecessary(
hass,
testEndpoint,
createProxyConfig({ media: false, live: true }),
{ context: 'live' },
);
expect(result.endpoint).toContain('/api/hass_web_proxy/');
});
it('should default openLimit to 0 when not specified', async () => {
const hass = createHASS();
hass.config.components = ['hass_web_proxy'];
await createProxiedEndpointIfNecessary(hass, testEndpoint, createProxyConfig());
await createProxiedEndpointIfNecessary(
hass,
testEndpoint,
createEnabledProxyConfig(),
);
expect(hass.callService).toHaveBeenCalledWith(
'hass_web_proxy',