react-stdio
Advanced tools
+2
-2
| { | ||
| "name": "react-stdio", | ||
| "version": "3.1.0", | ||
| "version": "3.2.0", | ||
| "description": "Render React.js components on any backend", | ||
@@ -11,3 +11,3 @@ "author": "Michael Jackson", | ||
| "type": "git", | ||
| "url": "https://github.com/mjackson/react-stdio" | ||
| "url": "https://github.com/ReactTraining/react-stdio" | ||
| }, | ||
@@ -14,0 +14,0 @@ "files": [ |
+58
-67
@@ -1,56 +0,53 @@ | ||
| 'use strict' | ||
| const path = require("path"); | ||
| const invariant = require("invariant"); | ||
| const EventStream = require("event-stream"); | ||
| const JSONStream = require("JSONStream"); | ||
| const ReactDOMServer = require("react-dom/server"); | ||
| const React = require("react"); | ||
| const path = require('path') | ||
| const invariant = require('invariant') | ||
| const EventStream = require('event-stream') | ||
| const JSONStream = require('JSONStream') | ||
| const ReactDOMServer = require('react-dom/server') | ||
| const React = require('react') | ||
| function getDefaultExports(moduleID) { | ||
| function loadModule(moduleId) { | ||
| // Clear the require cache, in case the file was | ||
| // changed since the server was started. | ||
| const cacheKey = require.resolve(moduleID) | ||
| delete require.cache[cacheKey] | ||
| const cacheKey = require.resolve(moduleId); | ||
| delete require.cache[cacheKey]; | ||
| const moduleExports = require(moduleID) | ||
| const moduleExports = require(moduleId); | ||
| // Return exports.default if using ES2015 modules. | ||
| if (moduleExports && moduleExports.default) | ||
| return moduleExports.default | ||
| // Return exports.default if using ES modules. | ||
| if (moduleExports && moduleExports.default) { | ||
| return moduleExports.default; | ||
| } | ||
| return moduleExports | ||
| return moduleExports; | ||
| } | ||
| function renderToStaticMarkup(element, callback) { | ||
| callback(null, ReactDOMServer.renderToStaticMarkup(element)) | ||
| callback(null, ReactDOMServer.renderToStaticMarkup(element)); | ||
| } | ||
| function renderToString(element, callback) { | ||
| callback(null, ReactDOMServer.renderToString(element)) | ||
| callback(null, ReactDOMServer.renderToString(element)); | ||
| } | ||
| function handleRequest(workingDir, request, callback) { | ||
| const componentPath = request.component | ||
| const renderMethod = request.render | ||
| const props = request.props | ||
| const componentPath = request.component; | ||
| const renderMethod = request.render; | ||
| const props = request.props; | ||
| invariant( | ||
| componentPath != null, | ||
| 'Missing { component } in request' | ||
| ) | ||
| invariant(componentPath != null, "Missing { component } in request"); | ||
| let render | ||
| if (renderMethod == null || renderMethod === 'renderToString') { | ||
| render = renderToString | ||
| } else if (renderMethod === 'renderToStaticMarkup') { | ||
| render = renderToStaticMarkup | ||
| let render; | ||
| if (renderMethod == null || renderMethod === "renderToString") { | ||
| render = renderToString; | ||
| } else if (renderMethod === "renderToStaticMarkup") { | ||
| render = renderToStaticMarkup; | ||
| } else { | ||
| const methodFile = path.resolve(workingDir, renderMethod) | ||
| const methodFile = path.resolve(workingDir, renderMethod); | ||
| try { | ||
| render = getDefaultExports(methodFile) | ||
| render = loadModule(methodFile); | ||
| } catch (error) { | ||
| if (error.code !== 'MODULE_NOT_FOUND') | ||
| process.stderr.write(error.stack + '\n') | ||
| if (error.code !== "MODULE_NOT_FOUND") { | ||
| process.stderr.write(error.stack + "\n"); | ||
| } | ||
| } | ||
@@ -60,46 +57,40 @@ } | ||
| invariant( | ||
| typeof render === 'function', | ||
| 'Cannot load render method: %s', | ||
| typeof render === "function", | ||
| "Cannot load render method: %s", | ||
| renderMethod | ||
| ) | ||
| ); | ||
| const componentFile = path.resolve(workingDir, componentPath) | ||
| const componentFile = path.resolve(workingDir, componentPath); | ||
| let component | ||
| let component; | ||
| try { | ||
| component = getDefaultExports(componentFile) | ||
| component = loadModule(componentFile); | ||
| } catch (error) { | ||
| if (error.code !== 'MODULE_NOT_FOUND') | ||
| process.stderr.write(error.stack + '\n') | ||
| if (error.code !== "MODULE_NOT_FOUND") { | ||
| process.stderr.write(error.stack + "\n"); | ||
| } | ||
| } | ||
| invariant( | ||
| component != null, | ||
| 'Cannot load component: %s', | ||
| componentPath | ||
| ) | ||
| invariant(component != null, "Cannot load component: %s", componentPath); | ||
| render( | ||
| React.createElement(component, props), | ||
| callback | ||
| ) | ||
| render(React.createElement(component, props), callback); | ||
| } | ||
| function createRequestHandler(workingDir) { | ||
| return function (request, callback) { | ||
| return function(request, callback) { | ||
| try { | ||
| handleRequest(workingDir, request, function (error, html) { | ||
| handleRequest(workingDir, request, function(error, html) { | ||
| if (error) { | ||
| callback(error) | ||
| } else if (typeof html !== 'string') { | ||
| callback(error); | ||
| } else if (typeof html !== "string") { | ||
| // Crash the server process. | ||
| callback(new Error('Render method must return a string')) | ||
| callback(new Error("Render method must return a string")); | ||
| } else { | ||
| callback(null, JSON.stringify({ html: html })) | ||
| callback(null, JSON.stringify({ html: html })); | ||
| } | ||
| }) | ||
| }); | ||
| } catch (error) { | ||
| callback(null, JSON.stringify({ error: error.message })) | ||
| callback(null, JSON.stringify({ error: error.message })); | ||
| } | ||
| } | ||
| }; | ||
| } | ||
@@ -109,16 +100,16 @@ | ||
| // still write to stdout. | ||
| const stdout = process.stdout | ||
| Object.defineProperty(process, 'stdout', { | ||
| const stdout = process.stdout; | ||
| Object.defineProperty(process, "stdout", { | ||
| configurable: true, | ||
| enumerable: true, | ||
| value: process.stderr | ||
| }) | ||
| }); | ||
| // Ensure console.log knows about the new stdout. | ||
| const Console = require('console').Console | ||
| Object.defineProperty(global, 'console', { | ||
| const Console = require("console").Console; | ||
| Object.defineProperty(global, "console", { | ||
| configurable: true, | ||
| enumerable: true, | ||
| value: new Console(process.stdout, process.stderr) | ||
| }) | ||
| }); | ||
@@ -129,2 +120,2 @@ // Read JSON blobs from stdin, pipe output to stdout. | ||
| .pipe(EventStream.map(createRequestHandler(process.cwd()))) | ||
| .pipe(stdout) | ||
| .pipe(stdout); |
Sorry, the diff of this file is not supported yet
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
7031
-0.14%99
-7.48%