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
+35 -17
View File
@@ -1,7 +1,7 @@
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { z } from 'zod';
import { homeAssistantSignAndFetch } from '../../src/ha/fetch';
import { homeAssistantSignPath } from '../../src/ha/sign-path';
import { homeAssistantGetSignedURLIfNecessary } from '../../src/ha/sign-path';
import { AdvancedCameraCardError, Endpoint } from '../../src/types';
import { createHASS } from '../test-utils';
@@ -18,7 +18,6 @@ describe('homeAssistantSignAndFetch', () => {
beforeEach(() => {
vi.stubGlobal('fetch', fetchMock);
vi.mocked(homeAssistantSignPath).mockResolvedValue('http://signed');
});
afterEach(() => {
@@ -27,26 +26,31 @@ describe('homeAssistantSignAndFetch', () => {
});
it('should return parsed data on successful call with endpoint', async () => {
const endpoint: Endpoint = { endpoint: 'http://example.com' };
vi.mocked(homeAssistantGetSignedURLIfNecessary).mockResolvedValue(
'http://example.com',
);
fetchMock.mockResolvedValueOnce({
ok: true,
json: async () => response,
});
const endpoint: Endpoint = { endpoint: 'http://example.com' };
expect(await homeAssistantSignAndFetch(createHASS(), endpoint, schema)).toEqual(
response,
);
expect(homeAssistantSignPath).not.toHaveBeenCalled();
const hass = createHASS();
expect(await homeAssistantSignAndFetch(hass, endpoint, schema)).toEqual(response);
expect(homeAssistantGetSignedURLIfNecessary).toHaveBeenCalledWith(hass, endpoint);
expect(fetchMock).toHaveBeenCalledWith('http://example.com', {});
});
it('should pass timeout signal when timeoutSeconds is provided', async () => {
const endpoint: Endpoint = { endpoint: 'http://example.com' };
vi.mocked(homeAssistantGetSignedURLIfNecessary).mockResolvedValue(
'http://example.com',
);
fetchMock.mockResolvedValueOnce({
ok: true,
json: async () => response,
});
const endpoint: Endpoint = { endpoint: 'http://example.com' };
expect(
await homeAssistantSignAndFetch(createHASS(), endpoint, schema, {
timeoutSeconds: 5,
@@ -58,6 +62,7 @@ describe('homeAssistantSignAndFetch', () => {
});
it('should sign path if requested', async () => {
vi.mocked(homeAssistantGetSignedURLIfNecessary).mockResolvedValue('http://signed');
fetchMock.mockResolvedValueOnce({
ok: true,
json: async () => response,
@@ -69,12 +74,14 @@ describe('homeAssistantSignAndFetch', () => {
};
const hass = createHASS();
expect(await homeAssistantSignAndFetch(hass, endpoint, schema)).toEqual(response);
expect(homeAssistantSignPath).toHaveBeenCalledWith(hass, 'http://example.com');
expect(homeAssistantGetSignedURLIfNecessary).toHaveBeenCalledWith(hass, endpoint);
expect(fetchMock).toHaveBeenCalledWith('http://signed', {});
});
it('should throw on sign failure', async () => {
vi.mocked(homeAssistantSignPath).mockRejectedValueOnce(new Error('Sign failed'));
vi.mocked(homeAssistantGetSignedURLIfNecessary).mockRejectedValueOnce(
new Error('Sign failed'),
);
const endpoint: Endpoint = {
endpoint: 'http://example.com',
@@ -85,8 +92,8 @@ describe('homeAssistantSignAndFetch', () => {
).rejects.toThrow(/Could not sign Home Assistant URL/);
});
it('should throw if sign path returns null', async () => {
vi.mocked(homeAssistantSignPath).mockResolvedValue(null);
it('should throw if sign endpoint returns null', async () => {
vi.mocked(homeAssistantGetSignedURLIfNecessary).mockResolvedValue(null);
const endpoint: Endpoint = {
endpoint: 'http://example.com',
@@ -98,9 +105,12 @@ describe('homeAssistantSignAndFetch', () => {
});
it('should throw on fetch failure', async () => {
const endpoint: Endpoint = { endpoint: 'http://example.com' };
vi.mocked(homeAssistantGetSignedURLIfNecessary).mockResolvedValue(
'http://example.com',
);
fetchMock.mockRejectedValueOnce(new Error('Fetch failed'));
const endpoint: Endpoint = { endpoint: 'http://example.com' };
try {
await homeAssistantSignAndFetch(createHASS(), endpoint, schema);
expect.fail('Should have thrown');
@@ -117,6 +127,10 @@ describe('homeAssistantSignAndFetch', () => {
});
it('should throw on non-ok response', async () => {
const endpoint: Endpoint = { endpoint: 'http://example.com' };
vi.mocked(homeAssistantGetSignedURLIfNecessary).mockResolvedValue(
'http://example.com',
);
const response = {
ok: false,
status: 404,
@@ -124,7 +138,6 @@ describe('homeAssistantSignAndFetch', () => {
} as Response;
fetchMock.mockResolvedValueOnce(response);
const endpoint: Endpoint = { endpoint: 'http://example.com' };
try {
await homeAssistantSignAndFetch(createHASS(), endpoint, schema);
expect.fail('Should have thrown');
@@ -140,6 +153,10 @@ describe('homeAssistantSignAndFetch', () => {
});
it('should throw on JSON parse failure', async () => {
const endpoint: Endpoint = { endpoint: 'http://example.com' };
vi.mocked(homeAssistantGetSignedURLIfNecessary).mockResolvedValue(
'http://example.com',
);
const response = {
ok: true,
json: async () => {
@@ -148,7 +165,6 @@ describe('homeAssistantSignAndFetch', () => {
} as unknown as Response;
fetchMock.mockResolvedValueOnce(response);
const endpoint: Endpoint = { endpoint: 'http://example.com' };
try {
await homeAssistantSignAndFetch(createHASS(), endpoint, schema);
expect.fail('Should have thrown');
@@ -166,6 +182,10 @@ describe('homeAssistantSignAndFetch', () => {
});
it('should throw on schema validation failure', async () => {
const endpoint: Endpoint = { endpoint: 'http://example.com' };
vi.mocked(homeAssistantGetSignedURLIfNecessary).mockResolvedValue(
'http://example.com',
);
const data = { val: 'string' };
const response = {
ok: true,
@@ -173,8 +193,6 @@ describe('homeAssistantSignAndFetch', () => {
} as unknown as Response;
fetchMock.mockResolvedValueOnce(response);
const endpoint: Endpoint = { endpoint: 'http://example.com' };
try {
await homeAssistantSignAndFetch(createHASS(), endpoint, schema);
expect.fail('Should have thrown');
+53 -1
View File
@@ -1,5 +1,8 @@
import { afterEach, describe, expect, it, vi } from 'vitest';
import { homeAssistantSignPath } from '../../src/ha/sign-path';
import {
homeAssistantGetSignedURLIfNecessary,
homeAssistantSignPath,
} from '../../src/ha/sign-path';
import { homeAssistantWSRequest } from '../../src/ha/ws-request.js';
import { signedPathSchema } from '../../src/types';
import { createHASS } from '../test-utils';
@@ -36,3 +39,52 @@ describe('homeAssistantSignPath', () => {
expect(await homeAssistantSignPath(createHASS(), 'unsigned/path', 42)).toBeNull();
});
});
describe('homeAssistantSignEndpoint', () => {
afterEach(() => {
vi.clearAllMocks();
});
it('should return endpoint URL without signing when sign is false', async () => {
const endpoint = { endpoint: 'http://example.com', sign: false };
expect(await homeAssistantGetSignedURLIfNecessary(createHASS(), endpoint)).toBe(
'http://example.com',
);
expect(homeAssistantWSRequest).not.toHaveBeenCalled();
});
it('should return endpoint URL without signing when sign is undefined', async () => {
const endpoint = { endpoint: 'http://example.com' };
expect(await homeAssistantGetSignedURLIfNecessary(createHASS(), endpoint)).toBe(
'http://example.com',
);
expect(homeAssistantWSRequest).not.toHaveBeenCalled();
});
it('should sign endpoint when sign is true', async () => {
const hass = createHASS();
vi.mocked(homeAssistantWSRequest).mockResolvedValue({
path: 'signed/path',
});
vi.mocked(hass.hassUrl).mockImplementation((url) => 'hass:' + url);
const endpoint = { endpoint: 'http://example.com', sign: true };
expect(await homeAssistantGetSignedURLIfNecessary(hass, endpoint, 60)).toBe(
'hass:signed/path',
);
expect(homeAssistantWSRequest).toHaveBeenCalledWith(hass, signedPathSchema, {
type: 'auth/sign_path',
path: 'http://example.com',
expires: 60,
});
});
it('should return null when signing fails', async () => {
vi.mocked(homeAssistantWSRequest).mockResolvedValue(null);
const endpoint = { endpoint: 'http://example.com', sign: true };
expect(
await homeAssistantGetSignedURLIfNecessary(createHASS(), endpoint),
).toBeNull();
});
});
+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',