@mui/toolpad-core
Advanced tools
Comparing version 0.1.1 to 0.1.2
@@ -1,13 +0,3 @@ | ||
import { RuntimeOptions } from 'quickjs-emscripten'; | ||
import * as React from 'react'; | ||
import { JsRuntime } from './types.js'; | ||
export interface JsRuntimeProviderProps { | ||
options?: RuntimeOptions; | ||
children?: React.ReactNode; | ||
} | ||
export declare const JsRuntimeProvider: React.LazyExoticComponent<{ | ||
(props: JsRuntimeProviderProps): JSX.Element; | ||
displayName: string; | ||
}>; | ||
export declare function createServerJsRuntime(): Promise<JsRuntime>; | ||
export declare function useServerJsRuntime(): JsRuntime; |
@@ -25,91 +25,11 @@ "use strict"; | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.useServerJsRuntime = exports.createServerJsRuntime = exports.JsRuntimeProvider = void 0; | ||
const invariant_1 = __importDefault(require("invariant")); | ||
const quickjs_emscripten_1 = require("quickjs-emscripten"); | ||
exports.useServerJsRuntime = exports.createServerJsRuntime = void 0; | ||
const vm = __importStar(require("vm")); | ||
const React = __importStar(require("react")); | ||
const errors_js_1 = require("./utils/errors.js"); | ||
const JsRuntimeContext = React.createContext(null); | ||
exports.JsRuntimeProvider = React.lazy(async () => { | ||
const quickJs = await (0, quickjs_emscripten_1.getQuickJS)(); | ||
function Context(props) { | ||
const [runtime, setRuntime] = React.useState(() => quickJs.newRuntime(props.options)); | ||
// Make sure to dispose of runtime when it changes or unmounts | ||
React.useEffect(() => { | ||
return () => { | ||
if (runtime.alive) { | ||
runtime.dispose(); | ||
} | ||
}; | ||
}, [runtime]); | ||
React.useEffect(() => setRuntime(quickJs.newRuntime(props.options)), [props.options]); | ||
return React.createElement(JsRuntimeContext.Provider, { value: runtime, ...props }); | ||
} | ||
Context.displayName = 'JsRuntimeProvider'; | ||
return { default: Context }; | ||
}); | ||
function useQuickJsRuntime() { | ||
const runtime = React.useContext(JsRuntimeContext); | ||
if (!runtime) { | ||
throw new Error(`No JsRuntime context found`); | ||
} | ||
return runtime; | ||
} | ||
function newSerializable(ctx, json) { | ||
switch (typeof json) { | ||
case 'string': | ||
return ctx.newString(json); | ||
case 'number': | ||
return ctx.newNumber(json); | ||
case 'boolean': | ||
return json ? ctx.true : ctx.false; | ||
case 'object': { | ||
if (!json) { | ||
return ctx.null; | ||
} | ||
if (Array.isArray(json)) { | ||
const result = ctx.newArray(); | ||
Object.values(json).forEach((value, i) => { | ||
const valueHandle = newSerializable(ctx, value); | ||
ctx.setProp(result, i, valueHandle); | ||
valueHandle.dispose(); | ||
}); | ||
return result; | ||
} | ||
const result = ctx.newObject(); | ||
Object.entries(json).forEach(([key, value]) => { | ||
const valueHandle = newSerializable(ctx, value); | ||
ctx.setProp(result, key, valueHandle); | ||
valueHandle.dispose(); | ||
}); | ||
return result; | ||
} | ||
case 'function': { | ||
const result = ctx.newFunction('anonymous', (...args) => { | ||
const dumpedArgs = args.map((arg) => ctx.dump(arg)); | ||
const fnResult = json(...dumpedArgs); | ||
return newSerializable(ctx, fnResult); | ||
}); | ||
return result; | ||
} | ||
case 'undefined': | ||
return ctx.undefined; | ||
default: | ||
return (0, invariant_1.default)(false, `invalid value: ${json}`); | ||
} | ||
} | ||
function evalExpressionInContext(ctx, expression, globalScope = {}) { | ||
function evalExpressionInContext(expression, globalScope = {}) { | ||
try { | ||
Object.entries(globalScope).forEach(([key, value]) => { | ||
const valueHandle = newSerializable(ctx, value); | ||
ctx.setProp(ctx.global, key, valueHandle); | ||
valueHandle.dispose(); | ||
}); | ||
const result = ctx.unwrapResult(ctx.evalCode(expression)); | ||
const resultValue = ctx.dump(result); | ||
result.dispose(); | ||
return { value: resultValue }; | ||
const value = vm.runInNewContext(expression, globalScope); | ||
return { value }; | ||
} | ||
@@ -121,6 +41,4 @@ catch (rawError) { | ||
async function createServerJsRuntime() { | ||
const quickJs = await (0, quickjs_emscripten_1.getQuickJS)(); | ||
const ctx = quickJs.newContext(); | ||
return { | ||
evaluateExpression: (code, globalScope) => evalExpressionInContext(ctx, code, globalScope), | ||
evaluateExpression: (code, globalScope) => evalExpressionInContext(code, globalScope), | ||
}; | ||
@@ -130,8 +48,6 @@ } | ||
function useServerJsRuntime() { | ||
const quickJs = useQuickJsRuntime(); | ||
const ctx = quickJs.newContext(); | ||
return React.useMemo(() => ({ | ||
evaluateExpression: (code, globalScope) => evalExpressionInContext(ctx, code, globalScope), | ||
}), [ctx]); | ||
evaluateExpression: (code, globalScope) => evalExpressionInContext(code, globalScope), | ||
}), []); | ||
} | ||
exports.useServerJsRuntime = useServerJsRuntime; |
@@ -1,13 +0,3 @@ | ||
import { RuntimeOptions } from 'quickjs-emscripten'; | ||
import * as React from 'react'; | ||
import { JsRuntime } from './types.js'; | ||
export interface JsRuntimeProviderProps { | ||
options?: RuntimeOptions; | ||
children?: React.ReactNode; | ||
} | ||
export declare const JsRuntimeProvider: React.LazyExoticComponent<{ | ||
(props: JsRuntimeProviderProps): JSX.Element; | ||
displayName: string; | ||
}>; | ||
export declare function createServerJsRuntime(): Promise<JsRuntime>; | ||
export declare function useServerJsRuntime(): JsRuntime; |
@@ -1,85 +0,8 @@ | ||
import invariant from 'invariant'; | ||
import { getQuickJS, } from 'quickjs-emscripten'; | ||
import * as vm from 'vm'; | ||
import * as React from 'react'; | ||
import { errorFrom } from './utils/errors.js'; | ||
const JsRuntimeContext = React.createContext(null); | ||
export const JsRuntimeProvider = React.lazy(async () => { | ||
const quickJs = await getQuickJS(); | ||
function Context(props) { | ||
const [runtime, setRuntime] = React.useState(() => quickJs.newRuntime(props.options)); | ||
// Make sure to dispose of runtime when it changes or unmounts | ||
React.useEffect(() => { | ||
return () => { | ||
if (runtime.alive) { | ||
runtime.dispose(); | ||
} | ||
}; | ||
}, [runtime]); | ||
React.useEffect(() => setRuntime(quickJs.newRuntime(props.options)), [props.options]); | ||
return React.createElement(JsRuntimeContext.Provider, { value: runtime, ...props }); | ||
} | ||
Context.displayName = 'JsRuntimeProvider'; | ||
return { default: Context }; | ||
}); | ||
function useQuickJsRuntime() { | ||
const runtime = React.useContext(JsRuntimeContext); | ||
if (!runtime) { | ||
throw new Error(`No JsRuntime context found`); | ||
} | ||
return runtime; | ||
} | ||
function newSerializable(ctx, json) { | ||
switch (typeof json) { | ||
case 'string': | ||
return ctx.newString(json); | ||
case 'number': | ||
return ctx.newNumber(json); | ||
case 'boolean': | ||
return json ? ctx.true : ctx.false; | ||
case 'object': { | ||
if (!json) { | ||
return ctx.null; | ||
} | ||
if (Array.isArray(json)) { | ||
const result = ctx.newArray(); | ||
Object.values(json).forEach((value, i) => { | ||
const valueHandle = newSerializable(ctx, value); | ||
ctx.setProp(result, i, valueHandle); | ||
valueHandle.dispose(); | ||
}); | ||
return result; | ||
} | ||
const result = ctx.newObject(); | ||
Object.entries(json).forEach(([key, value]) => { | ||
const valueHandle = newSerializable(ctx, value); | ||
ctx.setProp(result, key, valueHandle); | ||
valueHandle.dispose(); | ||
}); | ||
return result; | ||
} | ||
case 'function': { | ||
const result = ctx.newFunction('anonymous', (...args) => { | ||
const dumpedArgs = args.map((arg) => ctx.dump(arg)); | ||
const fnResult = json(...dumpedArgs); | ||
return newSerializable(ctx, fnResult); | ||
}); | ||
return result; | ||
} | ||
case 'undefined': | ||
return ctx.undefined; | ||
default: | ||
return invariant(false, `invalid value: ${json}`); | ||
} | ||
} | ||
function evalExpressionInContext(ctx, expression, globalScope = {}) { | ||
function evalExpressionInContext(expression, globalScope = {}) { | ||
try { | ||
Object.entries(globalScope).forEach(([key, value]) => { | ||
const valueHandle = newSerializable(ctx, value); | ||
ctx.setProp(ctx.global, key, valueHandle); | ||
valueHandle.dispose(); | ||
}); | ||
const result = ctx.unwrapResult(ctx.evalCode(expression)); | ||
const resultValue = ctx.dump(result); | ||
result.dispose(); | ||
return { value: resultValue }; | ||
const value = vm.runInNewContext(expression, globalScope); | ||
return { value }; | ||
} | ||
@@ -91,14 +14,10 @@ catch (rawError) { | ||
export async function createServerJsRuntime() { | ||
const quickJs = await getQuickJS(); | ||
const ctx = quickJs.newContext(); | ||
return { | ||
evaluateExpression: (code, globalScope) => evalExpressionInContext(ctx, code, globalScope), | ||
evaluateExpression: (code, globalScope) => evalExpressionInContext(code, globalScope), | ||
}; | ||
} | ||
export function useServerJsRuntime() { | ||
const quickJs = useQuickJsRuntime(); | ||
const ctx = quickJs.newContext(); | ||
return React.useMemo(() => ({ | ||
evaluateExpression: (code, globalScope) => evalExpressionInContext(ctx, code, globalScope), | ||
}), [ctx]); | ||
evaluateExpression: (code, globalScope) => evalExpressionInContext(code, globalScope), | ||
}), []); | ||
} |
{ | ||
"name": "@mui/toolpad-core", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"description": "Build MUI apps quickly", | ||
@@ -68,3 +68,3 @@ "author": "MUI Toolpad team", | ||
}, | ||
"gitHead": "c21fac688cfd8270cf36126d215698ae989ff899" | ||
"gitHead": "dcb31076f097fc44028a6fb704691d0f40489593" | ||
} |
Debug access
Supply chain riskUses debug, reflection and dynamic code execution features.
Found 1 instance in 1 package
98106
2471
2