Skip to content

🎥 Make videos programmatically with React

inline

Jonny Burger6dc336a283f2
diff --git a/packages/renderer/package.json b/packages/renderer/package.json
index a7c8ed9693..29db4fef0d 100644
--- a/packages/renderer/package.json
+++ b/packages/renderer/package.json
@@ -24,13 +24,7 @@
"puppeteer-core": "13.5.1",
"remotion": "3.0.16",
"source-map": "^0.8.0-beta.0",
- "bytes": "3.0.0",
- "content-disposition": "0.5.2",
- "mime-types": "2.1.35",
- "minimatch": "3.0.4",
- "path-is-inside": "1.0.2",
- "path-to-regexp": "2.2.1",
- "range-parser": "1.2.0"
+ "mime-types": "2.1.35"
},
"peerDependencies": {
"react": ">=16.8.0",
@@ -41,12 +35,7 @@
"@testing-library/dom": "^8.7.2",
"@testing-library/react": "13.1.1",
"@types/jest": "^27.4.0",
- "@types/minimatch": "3.0.5",
"@types/mime-types": "2.1.1",
- "@types/bytes": "3.1.1",
- "@types/content-disposition": "0.5.5",
- "@types/path-is-inside": "1.0.0",
- "@types/range-parser": "1.2.4",
"@types/node": "^16.7.5",
"@types/react": "18.0.1",
"@types/react-dom": "18.0.0",
diff --git a/packages/renderer/src/serve-handler/index.ts b/packages/renderer/src/serve-handler/index.ts
index 4f226b91de..f64623ff4c 100644
--- a/packages/renderer/src/serve-handler/index.ts
+++ b/packages/renderer/src/serve-handler/index.ts
@@ -5,10 +5,9 @@ import path from 'path';
import url from 'url';
import {promisify} from 'util';
// Packages
-import contentDisposition from 'content-disposition';
import mime from 'mime-types';
-import isPathInside from 'path-is-inside';
-import parseRange from 'range-parser';
+import {isPathInside} from './is-path-inside';
+import {rangeParser} from './range-parser';
const getHeaders = (absolutePath: string, stats: Stats | null) => {
const related = {};
@@ -19,12 +18,6 @@ const getHeaders = (absolutePath: string, stats: Stats | null) => {
if (stats) {
defaultHeaders = {
'Content-Length': String(stats.size),
- // Default to "inline", which always tries to render in the browser,
- // if that's not working, it will save the file. But to be clear: This
- // only happens if it cannot find a appropiate value.
- 'Content-Disposition': contentDisposition(base, {
- type: 'inline',
- }),
'Accept-Ranges': 'bytes',
};
@@ -128,12 +121,10 @@ export const serveHandler = async (
response: ServerResponse,
config: {
public: string;
- directoryListing: false;
- cleanUrls: false;
}
) => {
const cwd = process.cwd();
- const current = config.public ? path.resolve(cwd, config.public) : cwd;
+ const current = path.resolve(cwd, config.public);
let relativePath = null;
@@ -257,10 +248,10 @@ export const serveHandler = async (
} | null = null;
if (request.headers.range && stats.size) {
- const range = parseRange(stats.size, request.headers.range);
+ const range = rangeParser(stats.size, request.headers.range);
if (typeof range === 'object' && range.type === 'bytes') {
- const {start, end} = range[0];
+ const {start, end} = range.ranges[0];
streamOpts = {
start,
diff --git a/packages/renderer/src/serve-handler/is-path-inside.ts b/packages/renderer/src/serve-handler/is-path-inside.ts
new file mode 100644
index 0000000000..bfd6f5c6ff
--- /dev/null
+++ b/packages/renderer/src/serve-handler/is-path-inside.ts
@@ -0,0 +1,30 @@
+import path from 'path';
+
+export const isPathInside = function (
+ thePath: string,
+ potentialParent: string
+) {
+ // For inside-directory checking, we want to allow trailing slashes, so normalize.
+ thePath = stripTrailingSep(thePath);
+ potentialParent = stripTrailingSep(potentialParent);
+
+ // Node treats only Windows as case-insensitive in its path module; we follow those conventions.
+ if (process.platform === 'win32') {
+ thePath = thePath.toLowerCase();
+ potentialParent = potentialParent.toLowerCase();
+ }
+
+ return (
+ thePath.lastIndexOf(potentialParent, 0) === 0 &&
+ (thePath[potentialParent.length] === path.sep ||
+ thePath[potentialParent.length] === undefined)
+ );
+};
+
+function stripTrailingSep(thePath: string) {
+ if (thePath[thePath.length - 1] === path.sep) {
+ return thePath.slice(0, -1);
+ }
+
+ return thePath;
+}
diff --git a/packages/renderer/src/serve-handler/range-parser.ts b/packages/renderer/src/serve-handler/range-parser.ts
new file mode 100644
index 0000000000..5d5d3747b2
--- /dev/null
+++ b/packages/renderer/src/serve-handler/range-parser.ts
@@ -0,0 +1,76 @@
+/*!
+ * range-parser
+ * Copyright(c) 2012-2014 TJ Holowaychuk
+ * Copyright(c) 2015-2016 Douglas Christopher Wilson
+ * MIT Licensed
+ */
+
+export const rangeParser = (
+ size: number,
+ str: string
+):
+ | {
+ type: string;
+ ranges: {
+ start: number;
+ end: number;
+ }[];
+ }
+ | -1
+ | -2 => {
+ if (typeof str !== 'string') {
+ throw new TypeError('argument str must be a string');
+ }
+
+ const index = str.indexOf('=');
+
+ if (index === -1) {
+ return -2;
+ }
+
+ // split the range string
+ const arr = str.slice(index + 1).split(',');
+ const ranges = [];
+
+ // add ranges type
+ const type = str.slice(0, index);
+
+ // parse all ranges
+ for (let i = 0; i < arr.length; i++) {
+ const range = arr[i].split('-');
+ let start = parseInt(range[0], 10);
+ let end = parseInt(range[1], 10);
+
+ // -nnn
+ if (isNaN(start)) {
+ start = size - end;
+ end = size - 1;
+ // nnn-
+ } else if (isNaN(end)) {
+ end = size - 1;
+ }
+
+ // limit last-byte-pos to current length
+ if (end > size - 1) {
+ end = size - 1;
+ }
+
+ // invalid or unsatisifiable
+ if (isNaN(start) || isNaN(end) || start > end || start < 0) {
+ continue;
+ }
+
+ // add range
+ ranges.push({
+ start,
+ end,
+ });
+ }
+
+ if (ranges.length < 1) {
+ // unsatisifiable
+ return -1;
+ }
+
+ return {ranges, type};
+};
diff --git a/packages/renderer/src/serve-static.ts b/packages/renderer/src/serve-static.ts
index e3998324d2..1abeedaffd 100644
--- a/packages/renderer/src/serve-static.ts
+++ b/packages/renderer/src/serve-static.ts
@@ -46,8 +46,6 @@ export const serveStatic = async (
serveHandler(request, response, {
public: path,
- directoryListing: false,
- cleanUrls: false,
}).catch(() => {
response.statusCode = 500;
response.end('Error serving file');
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index b1fded0841..e3c4e85c7c 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -729,29 +729,18 @@ importers:
'@jonny/eslint-config': 3.0.259
'@testing-library/dom': ^8.7.2
'@testing-library/react': 13.1.1
- '@types/bytes': 3.1.1
- '@types/content-disposition': 0.5.5
'@types/jest': ^27.4.0
'@types/mime-types': 2.1.1
- '@types/minimatch': 3.0.5
'@types/node': ^16.7.5
- '@types/path-is-inside': 1.0.0
- '@types/range-parser': 1.2.4
'@types/react': 18.0.1
'@types/react-dom': 18.0.0
- bytes: 3.0.0
- content-disposition: 0.5.2
eslint: 8.13.0
execa: 5.1.1
jest: ^27.2.4
mime-types: 2.1.35
- minimatch: 3.0.4
- path-is-inside: 1.0.2
- path-to-regexp: 2.2.1
prettier: ^2.0.5
prettier-plugin-organize-imports: ^2.3.4
puppeteer-core: 13.5.1
- range-parser: 1.2.0
react: 18.0.0
react-dom: 18.0.0
remotion: 3.0.16
@@ -759,29 +748,18 @@ importers:
ts-jest: ^27.0.5
typescript: ^4.7.0
dependencies:
- bytes: 3.0.0
- content-disposition: 0.5.2
execa: 5.1.1
mime-types: 2.1.35
- minimatch: 3.0.4
- path-is-inside: 1.0.2
- path-to-regexp: 2.2.1
puppeteer-core: 13.5.1
- range-parser: 1.2.0
remotion: link:../core
source-map: 0.8.0-beta.0
devDependencies:
'@jonny/eslint-config': [email protected][email protected]
'@testing-library/dom': 8.7.2
'@testing-library/react': [email protected][email protected]
- '@types/bytes': 3.1.1
- '@types/content-disposition': 0.5.5
'@types/jest': 27.4.0
'@types/mime-types': 2.1.1
- '@types/minimatch': 3.0.5
'@types/node': 16.10.2
- '@types/path-is-inside': 1.0.0
- '@types/range-parser': 1.2.4
'@types/react': 18.0.1
'@types/react-dom': 18.0.0
eslint: 8.13.0
@@ -7682,10 +7660,6 @@ packages:
'@types/node': 17.0.17
dev: false
- /@types/bytes/3.1.1:
- resolution: {integrity: sha512-lOGyCnw+2JVPKU3wIV0srU0NyALwTBJlVSx5DfMQOFuuohA8y9S8orImpuIQikZ0uIQ8gehrRjxgQC1rLRi11w==}
- dev: true
-
/@types/color-namer/1.3.0:
resolution: {integrity: sha512-iyuC9rR+3QYd+VuC8E9MvKM2fTForr9RcUa4YurqRJyXcDALYIt+GPsy1skrri2QcYxWVNYYyoznxe8Y42K4/g==}
dev: true
@@ -7702,10 +7676,6 @@ packages:
dependencies:
'@types/node': 16.10.2
- /@types/content-disposition/0.5.5:
- resolution: {integrity: sha512-v6LCdKfK6BwcqMo+wYW05rLS12S0ZO0Fl4w1h4aaZMD7bqT3gVUns6FvLJKGZHQmYn3SX55JWGpziwJRwVgutA==}
- dev: true
-
/@types/degit/2.8.3:
resolution: {integrity: sha512-CL7y71j2zaDmtPLD5Xq5S1Gv2dFoHl0/GBZm6s39Mj/ls28L3NzAOqf7H4H0/2TNVMgMjMVf9CAFYSjmXhi3bw==}
dev: true
@@ -7817,6 +7787,7 @@ packages:
/@types/minimatch/3.0.5:
resolution: {integrity: sha512-Klz949h02Gz2uZCMGwDUSDS1YBlTdDDgbWHi+81l29tQALUtvz4rAYi5uoVhE5Lagoq6DeqAUlbrHvW/mXDgdQ==}
+ dev: false
/@types/minimist/1.2.2:
resolution: {integrity: sha512-jhuKLIRrhvCPLqwPcx6INqmKeiA5EWrsCOPhrlFSrbrmU4ZMPjj5Ul/oLCMDO98XRUIwVm78xICz4EPCektzeQ==}
@@ -7853,10 +7824,6 @@ packages:
resolution: {integrity: sha512-kUNnecmtkunAoQ3CnjmMkzNU/gtxG8guhi+Fk2U/kOpIKjIMKnXGp4IJCgQJrXSgMsWYimYG4TGjz/UzbGEBTw==}
dev: false
- /@types/path-is-inside/1.0.0:
- resolution: {integrity: sha512-hfnXRGugz+McgX2jxyy5qz9sB21LRzlGn24zlwN2KEgoPtEvjzNRrLtUkOOebPDPZl3Rq7ywKxYvylVcEZDnEw==}
- dev: true
-
/@types/prettier/2.4.1:
resolution: {integrity: sha512-Fo79ojj3vdEZOHg3wR9ksAMRz4P3S5fDB5e/YWZiFnyFQI1WY2Vftu9XoXVVtJfxB7Bpce/QTqWSSntkz2Znrw==}