Skip to content

🎥 Make videos programmatically with React

start implementing static server

Jonny Burgerfc38dcf95580
diff --git a/packages/bundler/src/routes.ts b/packages/bundler/src/routes.ts
index b38725d3a6..3e9fc85a7c 100644
--- a/packages/bundler/src/routes.ts
+++ b/packages/bundler/src/routes.ts
@@ -1,8 +1,7 @@
import {createReadStream, statSync} from 'fs';
import {IncomingMessage, ServerResponse} from 'http';
import path from 'path';
-// eslint-disable-next-line no-restricted-imports
-import {parse} from 'querystring';
+import {URLSearchParams} from 'url';
import {getFileSource} from './error-overlay/react-overlay/utils/get-file-source';
import {
getDisplayNameForEditor,
@@ -58,8 +57,8 @@ export const handleFileSource = async (
throw new Error('query must start with ?');
}
- const query = parse(search.substring(1));
- const {f} = query;
+ const query = new URLSearchParams(search);
+ const f = query.get('f');
if (typeof f !== 'string') {
throw new Error('must pass `f` parameter');
}
diff --git a/packages/bundler/src/serve-static.ts b/packages/bundler/src/serve-static.ts
new file mode 100644
index 0000000000..f5387896ae
--- /dev/null
+++ b/packages/bundler/src/serve-static.ts
@@ -0,0 +1,99 @@
+/*!
+ * serve-static
+ * Copyright(c) 2010 Sencha Inc.
+ * Copyright(c) 2011 TJ Holowaychuk
+ * Copyright(c) 2014-2016 Douglas Christopher Wilson
+ * MIT Licensed
+ */
+
+import {IncomingMessage, ServerResponse} from 'http';
+import {resolve} from 'path';
+const send = require('send');
+
+/**
+ * @param {string} root
+ * @param {object} [options]
+ * @return {function}
+ * @public
+ */
+
+export const serveStatic = (
+ root: string,
+ options: {
+ cacheControl: true;
+ dotfiles: 'allow';
+ etag: true;
+ extensions: false;
+ fallthrough: false;
+ immutable: false;
+ index: false;
+ lastModified: true;
+ maxAge: 0;
+ redirect: true;
+ }
+) => {
+ if (!root) {
+ throw new TypeError('root path required');
+ }
+
+ if (typeof root !== 'string') {
+ throw new TypeError('root path must be a string');
+ }
+
+ // copy options object
+ const opts = Object.create(options || null);
+
+ // headers listener
+ const {setHeaders} = opts;
+
+ if (setHeaders && typeof setHeaders !== 'function') {
+ throw new TypeError('option setHeaders must be function');
+ }
+
+ // setup options for send
+ opts.maxage = opts.maxage || opts.maxAge || 0;
+ opts.root = resolve(root);
+
+ return function (req: IncomingMessage, res: ServerResponse) {
+ if (req.method !== 'GET' && req.method !== 'HEAD') {
+ // method not allowed
+ res.statusCode = 405;
+ res.setHeader('Allow', 'GET, HEAD');
+ res.setHeader('Content-Length', '0');
+ res.end();
+ return;
+ }
+
+ let path = new URL(req.url as string, 'http://localhost').pathname;
+
+ // make sure redirect occurs at mount
+ if (path === '/' && path.substr(-1) !== '/') {
+ path = '';
+ }
+
+ // create send stream
+ const stream = send(req, path, opts);
+
+ // add directory handler
+ stream.on('directory', () => {
+ res.writeHead(500);
+ res.write('Is a directory');
+ res.end();
+ });
+
+ // add headers listener
+ if (setHeaders) {
+ stream.on('headers', setHeaders);
+ }
+
+ // forward errors
+ stream.on('error', (err: Error) => {
+ res.writeHead(500);
+ res.write(err.toString());
+ res.end();
+ });
+
+ // pipe
+ stream.pipe(res);
+ };
+};