Skip to content

🎥 Make videos programmatically with React

`@remotion/renderer`: Properly check all network interfaces when determining which hosts to check

Jonny Burgerf48465e47cef
diff --git a/packages/renderer/src/get-port.ts b/packages/renderer/src/get-port.ts
index 3e3820461b..a49ff8d5c6 100644
--- a/packages/renderer/src/get-port.ts
+++ b/packages/renderer/src/get-port.ts
@@ -1,5 +1,4 @@
import net from 'net';
-import {isIpV6Supported} from './is-ipv6-supported';
import {createLock} from './locks';
type PortStatus = 'available' | 'unavailable';
@@ -81,15 +80,6 @@ const getPort = async ({
const portLocks = createLock({timeout: 10000});
-export const getPortConfig = () => {
- const host = isIpV6Supported() ? '::' : '0.0.0.0';
- const hostsToTry = isIpV6Supported()
- ? ['::', '::1', '0.0.0.0', '127.0.0.1']
- : ['0.0.0.0', '127.0.0.1'];
-
- return {host, hostsToTry};
-};
-
export const getDesiredPort = async ({
desiredPort,
from,
diff --git a/packages/renderer/src/index.ts b/packages/renderer/src/index.ts
index 88b4d60e8e..02ed52f59c 100644
--- a/packages/renderer/src/index.ts
+++ b/packages/renderer/src/index.ts
@@ -37,7 +37,7 @@ import {
import {getExtensionOfFilename} from './get-extension-of-filename';
import {getRealFrameRange} from './get-frame-to-render';
import {ensureLocalBrowser} from './get-local-browser-executable';
-import {getDesiredPort, getPortConfig} from './get-port';
+import {getDesiredPort} from './get-port';
import {
DEFAULT_STILL_IMAGE_FORMAT,
DEFAULT_VIDEO_IMAGE_FORMAT,
@@ -45,7 +45,6 @@ import {
validVideoImageFormats,
} from './image-format';
import {isAudioCodec} from './is-audio-codec';
-import {isIpV6Supported} from './is-ipv6-supported';
import {isServeUrl} from './is-serve-url';
import {DEFAULT_JPEG_QUALITY, validateJpegQuality} from './jpeg-quality';
import {isEqualOrBelowLogLevel, isValidLogLevel, logLevels} from './log-level';
@@ -60,6 +59,7 @@ import {
import {parseStack} from './parse-browser-error-stack';
import * as perf from './perf';
import {DEFAULT_PIXEL_FORMAT, validPixelFormats} from './pixel-format';
+import {getPortConfig, isIpV6Supported} from './port-config';
import {makeOrReuseServer, prepareServer} from './prepare-server';
import {internalRenderFrames} from './render-frames';
import {internalRenderMedia} from './render-media';
diff --git a/packages/renderer/src/is-ipv6-supported.ts b/packages/renderer/src/is-ipv6-supported.ts
deleted file mode 100644
index 3db848bfc7..0000000000
--- a/packages/renderer/src/is-ipv6-supported.ts
+++ /dev/null
@@ -1,27 +0,0 @@
-import os from 'os';
-
-let cache: null | boolean = null;
-
-const calculate = (): boolean => {
- const interfaces = os.networkInterfaces();
-
- for (const iface in interfaces) {
- for (const configuration of interfaces[
- iface
- ] as os.NetworkInterfaceInfo[]) {
- if (configuration.family === 'IPv6' && !configuration.internal) {
- return true;
- }
- }
- }
-
- return false;
-};
-
-export const isIpV6Supported = (): boolean => {
- if (cache === null) {
- cache = calculate();
- }
-
- return cache;
-};
diff --git a/packages/renderer/src/port-config.ts b/packages/renderer/src/port-config.ts
new file mode 100644
index 0000000000..a4d8ce881c
--- /dev/null
+++ b/packages/renderer/src/port-config.ts
@@ -0,0 +1,98 @@
+import type {NetworkInterfaceInfo} from 'os';
+import os from 'os';
+import {truthy} from './truthy';
+
+type PortConfig = {
+ host: string;
+ hostsToTry: string[];
+};
+
+let cached: PortConfig | null = null;
+
+export const getPortConfig = (): PortConfig => {
+ if (cached) {
+ return cached;
+ }
+
+ const networkInterfaces = os.networkInterfaces();
+ const flattened = flattenNetworkInterfaces(networkInterfaces);
+ const host = getHostToBind(flattened);
+ const hostsToTry = getHostsToTry(flattened);
+
+ const response: PortConfig = {host, hostsToTry};
+ cached = response;
+ return response;
+};
+
+export const getHostToBind = (flattened: os.NetworkInterfaceInfo[]) => {
+ return isIpV6Supported(flattened) ? '::' : '0.0.0.0';
+};
+
+export const getHostsToTry = (flattened: os.NetworkInterfaceInfo[]) => {
+ return [
+ hasIPv6LoopbackAddress(flattened) ? '::1' : null,
+ hasIpv4LoopbackAddress(flattened) ? '127.0.0.1' : null,
+ isIpV6Supported(flattened) ? '::' : null,
+ '0.0.0.0',
+ ].filter(truthy);
+};
+
+export const flattenNetworkInterfaces = (
+ networkInterfaces: NodeJS.Dict<NetworkInterfaceInfo[]>,
+): NetworkInterfaceInfo[] => {
+ const result: NetworkInterfaceInfo[] = [];
+
+ for (const iface in networkInterfaces) {
+ for (const configuration of networkInterfaces[
+ iface
+ ] as os.NetworkInterfaceInfo[]) {
+ result.push(configuration);
+ }
+ }
+
+ return result;
+};
+
+export const isIpV6Supported = (
+ flattened: os.NetworkInterfaceInfo[],
+): boolean => {
+ for (const configuration of flattened) {
+ if (configuration.family === 'IPv6' && !configuration.internal) {
+ return true;
+ }
+ }
+
+ return false;
+};
+
+export const hasIPv6LoopbackAddress = (
+ flattened: os.NetworkInterfaceInfo[],
+) => {
+ for (const configuration of flattened) {
+ if (
+ configuration.family === 'IPv6' &&
+ configuration.internal &&
+ configuration.address === '::1'
+ ) {
+ return true;
+ }
+ }
+
+ return false;
+};
+
+export const hasIpv4LoopbackAddress = (
+ flattened: os.NetworkInterfaceInfo[],
+): boolean => {
+ for (const configuration of flattened) {
+ if (
+ configuration.family === 'IPv4' &&
+ configuration.internal &&
+ configuration.address === '127.0.0.1'
+ ) {
+ return true;
+ }
+ }
+
+ return false;
+};
diff --git a/packages/renderer/src/serve-static.ts b/packages/renderer/src/serve-static.ts
index 4a46037a65..642dc3600b 100644
--- a/packages/renderer/src/serve-static.ts
+++ b/packages/renderer/src/serve-static.ts
@@ -2,9 +2,10 @@ import type {Socket} from 'net';
import http from 'node:http';
import type {DownloadMap} from './assets/download-map';
import type {Compositor} from './compositor/compositor';
-import {getDesiredPort, getPortConfig} from './get-port';
+import {getDesiredPort} from './get-port';
import type {LogLevel} from './log-level';
import {startOffthreadVideoServer} from './offthread-video-server';
+import {getPortConfig} from './port-config';
import {serveHandler} from './serve-handler';
export const serveStatic = async (
diff --git a/packages/renderer/src/test/port-config.test.ts b/packages/renderer/src/test/port-config.test.ts
new file mode 100644
index 0000000000..ffe23cf381
--- /dev/null
+++ b/packages/renderer/src/test/port-config.test.ts
@@ -0,0 +1,287 @@
+import type {NetworkInterfaceInfo} from 'os';
+import {expect, test} from 'vitest';
+import {
+ flattenNetworkInterfaces,
+ getHostsToTry,
+ getHostToBind,
+} from '../port-config';
+
+const dockerInterfaces: NodeJS.Dict<NetworkInterfaceInfo[]> = {
+ lo: [
+ {
+ address: '127.0.0.1',
+ netmask: '255.0.0.0',
+ family: 'IPv4',
+ mac: '00:00:00:00:00:00',
+ internal: true,
+ cidr: '127.0.0.1/8',
+ },
+ ],
+ eno1: [
+ {
+ address: '192.168.1.9',
+ netmask: '255.255.255.0',
+ family: 'IPv4',
+ mac: '00:d8:61:58:06:d5',
+ internal: false,
+ cidr: '192.168.1.9/24',
+ },
+ {
+ address: '2405:4803:b492:5430:1518:f88c:90e8:6aa6',
+ netmask: 'ffff:ffff:ffff:ffff::',
+ family: 'IPv6',
+ mac: '00:d8:61:58:06:d5',
+ internal: false,
+ cidr: '2405:4803:b492:5430:1518:f88c:90e8:6aa6/64',
+ scopeid: 0,
+ },
+ {
+ address: '2405:4803:b492:5430:d49b:a573:c741:fd19',
+ netmask: 'ffff:ffff:ffff:ffff::',
+ family: 'IPv6',
+ mac: '00:d8:61:58:06:d5',
+ internal: false,
+ cidr: '2405:4803:b492:5430:d49b:a573:c741:fd19/64',
+ scopeid: 0,
+ },
+ {
+ address: 'fe80::b56c:f898:93f1:d529',
+ netmask: 'ffff:ffff:ffff:ffff::',
+ family: 'IPv6',
+ mac: '00:d8:61:58:06:d5',
+ internal: false,
+ cidr: 'fe80::b56c:f898:93f1:d529/64',
+ scopeid: 2,
+ },
+ ],
+};
+
+const macOsM1Interfaces: NodeJS.Dict<NetworkInterfaceInfo[]> = {
+ lo0: [
+ {
+ address: '127.0.0.1',
+ netmask: '255.0.0.0',
+ family: 'IPv4',
+ mac: '00:00:00:00:00:00',
+ internal: true,
+ cidr: '127.0.0.1/8',
+ },
+ {
+ address: '::1',
+ netmask: 'ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff',
+ family: 'IPv6',
+ mac: '00:00:00:00:00:00',
+ internal: true,
+ cidr: '::1/128',
+ scopeid: 0,
+ },
+ {
+ address: 'fe80::1',
+ netmask: 'ffff:ffff:ffff:ffff::',
+ family: 'IPv6',
+ mac: '00:00:00:00:00:00',
+ internal: true,
+ cidr: 'fe80::1/64',
+ scopeid: 1,
+ },
+ ],
+ anpi1: [
+ {
+ address: 'fe80::c865:f4ff:fe1d:5e45',
+ netmask: 'ffff:ffff:ffff:ffff::',
+ family: 'IPv6',
+ mac: 'ca:65:f4:1d:5e:45',
+ internal: false,
+ cidr: 'fe80::c865:f4ff:fe1d:5e45/64',
+ scopeid: 4,
+ },
+ ],
+ anpi0: [
+ {
+ address: 'fe80::c865:f4ff:fe1d:5e44',
+ netmask: 'ffff:ffff:ffff:ffff::',
+ family: 'IPv6',
+ mac: 'ca:65:f4:1d:5e:44',
+ internal: false,
+ cidr: 'fe80::c865:f4ff:fe1d:5e44/64',
+ scopeid: 5,
+ },
+ ],
+ ap1: [
+ {
+ address: 'fe80::3498:77ff:fe4e:59a9',
+ netmask: 'ffff:ffff:ffff:ffff::',
+ family: 'IPv6',
+ mac: '36:98:77:4e:59:a9',
+ internal: false,
+ cidr: 'fe80::3498:77ff:fe4e:59a9/64',
+ scopeid: 12,
+ },
+ ],
+ en1: [
+ {
+ address: 'fe80::101b:a1ad:96cc:4b60',
+ netmask: 'ffff:ffff:ffff:ffff::',
+ family: 'IPv6',
+ mac: '14:98:77:4e:59:a9',
+ internal: false,
+ cidr: 'fe80::101b:a1ad:96cc:4b60/64',
+ scopeid: 13,
+ },
+ {
+ address: '192.168.1.74',
+ netmask: '255.255.255.0',
+ family: 'IPv4',
+ mac: '14:98:77:4e:59:a9',
+ internal: false,
+ cidr: '192.168.1.74/24',
+ },
+ ],
+ awdl0: [
+ {
+ address: 'fe80::d820:13ff:fe78:47b3',
+ netmask: 'ffff:ffff:ffff:ffff::',
+ family: 'IPv6',
+ mac: 'da:20:13:78:47:b3',
+ internal: false,
+ cidr: 'fe80::d820:13ff:fe78:47b3/64',
+ scopeid: 14,
+ },
+ ],
+ llw0: [
+ {
+ address: 'fe80::d820:13ff:fe78:47b3',
+ netmask: 'ffff:ffff:ffff:ffff::',
+ family: 'IPv6',
+ mac: 'da:20:13:78:47:b3',
+ internal: false,
+ cidr: 'fe80::d820:13ff:fe78:47b3/64',
+ scopeid: 15,
+ },
+ ],
+ bridge100: [
+ {
+ address: '192.168.64.1',
+ netmask: '255.255.255.0',
+ family: 'IPv4',
+ mac: '16:98:77:15:94:64',
+ internal: false,
+ cidr: '192.168.64.1/24',
+ },
+ {
+ address: 'fe80::1498:77ff:fe15:9464',
+ netmask: 'ffff:ffff:ffff:ffff::',
+ family: 'IPv6',
+ mac: '16:98:77:15:94:64',
+ internal: false,
+ cidr: 'fe80::1498:77ff:fe15:9464/64',
+ scopeid: 22,
+ },
+ {
+ address: 'fdd2:235a:bccb:777e:c50:13d2:d192:aa39',
+ netmask: 'ffff:ffff:ffff:ffff::',
+ family: 'IPv6',
+ mac: '16:98:77:15:94:64',
+ internal: false,
+ cidr: 'fdd2:235a:bccb:777e:c50:13d2:d192:aa39/64',
+ scopeid: 0,
+ },
+ ],
+ utun0: [
+ {
+ address: 'fe80::3ab9:d3dc:219a:150a',
+ netmask: 'ffff:ffff:ffff:ffff::',
+ family: 'IPv6',
+ mac: '00:00:00:00:00:00',
+ internal: false,
+ cidr: 'fe80::3ab9:d3dc:219a:150a/64',
+ scopeid: 16,
+ },
+ ],
+ utun1: [
+ {
+ address: 'fe80::ce81:b1c:bd2c:69e',
+ netmask: 'ffff:ffff:ffff:ffff::',
+ family: 'IPv6',
+ mac: '00:00:00:00:00:00',
+ internal: false,
+ cidr: 'fe80::ce81:b1c:bd2c:69e/64',
+ scopeid: 17,
+ },
+ ],
+ utun2: [
+ {
+ address: 'fe80::b6db:8405:b7e7:5763',
+ netmask: 'ffff:ffff:ffff:ffff::',
+ family: 'IPv6',
+ mac: '00:00:00:00:00:00',
+ internal: false,
+ cidr: 'fe80::b6db:8405:b7e7:5763/64',
+ scopeid: 18,
+ },
+ ],
+ utun3: [
+ {
+ address: 'fe80::6ac5:9997:e6e0:87b5',
+ netmask: 'ffff:ffff:ffff:ffff::',
+ family: 'IPv6',
+ mac: '00:00:00:00:00:00',
+ internal: false,
+ cidr: 'fe80::6ac5:9997:e6e0:87b5/64',
+ scopeid: 19,
+ },
+ ],
+ utun4: [
+ {
+ address: 'fe80::d94f:34bc:eb3e:978c',
+ netmask: 'ffff:ffff:ffff:ffff::',
+ family: 'IPv6',
+ mac: '00:00:00:00:00:00',
+ internal: false,
+ cidr: 'fe80::d94f:34bc:eb3e:978c/64',
+ scopeid: 20,
+ },
+ ],
+ utun5: [
+ {
+ address: 'fe80::a0ca:c320:7562:2d4e',
+ netmask: 'ffff:ffff:ffff:ffff::',
+ family: 'IPv6',
+ mac: '00:00:00:00:00:00',
+ internal: false,
+ cidr: 'fe80::a0ca:c320:7562:2d4e/64',
+ scopeid: 23,
+ },
+ ],
+ utun6: [
+ {
+ address: 'fe80::20ae:1de4:db23:d05d',
+ netmask: 'ffff:ffff:ffff:ffff::',
+ family: 'IPv6',
+ mac: '00:00:00:00:00:00',
+ internal: false,
+ cidr: 'fe80::20ae:1de4:db23:d05d/64',
+ scopeid: 24,
+ },
+ ],
+};
+
+test('Port config on Docker with IPv6 support', () => {
+ const hostToBind = getHostToBind(flattenNetworkInterfaces(dockerInterfaces));
+ expect(hostToBind).toBe('::');
+});
+
+test('Port config on Docker with no loopback address', () => {
+ const hostToBind = getHostsToTry(flattenNetworkInterfaces(dockerInterfaces));
+ expect(hostToBind).toEqual(['127.0.0.1', '::', '0.0.0.0']);
+});
+
+test('Port config on M1 Mac', () => {
+ const hostToBind = getHostToBind(flattenNetworkInterfaces(macOsM1Interfaces));
+ expect(hostToBind).toBe('::');
+});
+
+test('Port config on M1 Mac', () => {
+ const hostToBind = getHostsToTry(flattenNetworkInterfaces(macOsM1Interfaces));
+ expect(hostToBind).toEqual(['::1', '127.0.0.1', '::', '0.0.0.0']);
+});