@object-ui/react-runtime 11.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/dist/index.d.ts +36 -0
- package/dist/index.js +78 -0
- package/package.json +34 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 ObjectQL
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ObjectUI — @object-ui/react-runtime (ADR: kind:'react')
|
|
3
|
+
*
|
|
4
|
+
* Inlined, vendored react-runner (github.com/nihgwu/react-runner, MIT): transpile
|
|
5
|
+
* a JSX/TSX source string with Sucrase, then eval it with an injected scope
|
|
6
|
+
* (React, your registered components, page data) and render in the MAIN React
|
|
7
|
+
* tree. NO sandbox — this is the *trusted* execution tier, gated to enterprise/
|
|
8
|
+
* private deployments where authors are trusted. Inlined (not depended) so we
|
|
9
|
+
* fully control the scope/imports surface and can lazy-load it behind a flag.
|
|
10
|
+
*/
|
|
11
|
+
import { Component, type ReactElement, type ReactNode } from 'react';
|
|
12
|
+
export type Scope = Record<string, unknown>;
|
|
13
|
+
/** Transpile JSX/TS → JS (classic runtime; imports → require). */
|
|
14
|
+
export declare const transform: (code: string) => string;
|
|
15
|
+
/** Transpile + eval a source string into a React element (or null). */
|
|
16
|
+
export declare function generateElement(code: string, scope?: Scope): ReactElement | null;
|
|
17
|
+
export interface ReactRunnerProps {
|
|
18
|
+
code: string;
|
|
19
|
+
scope?: Scope;
|
|
20
|
+
/** rendered when the code throws at transpile/eval/render time */
|
|
21
|
+
fallback?: (error: Error) => ReactNode;
|
|
22
|
+
onError?: (error: Error) => void;
|
|
23
|
+
}
|
|
24
|
+
interface ReactRunnerState {
|
|
25
|
+
element: ReactElement | null;
|
|
26
|
+
error: Error | null;
|
|
27
|
+
}
|
|
28
|
+
/** Renders a JSX/TSX source string with a built-in error boundary. */
|
|
29
|
+
export declare class ReactRunner extends Component<ReactRunnerProps, ReactRunnerState> {
|
|
30
|
+
state: ReactRunnerState;
|
|
31
|
+
static getDerivedStateFromProps(props: ReactRunnerProps): Partial<ReactRunnerState> | null;
|
|
32
|
+
static getDerivedStateFromError(error: Error): Partial<ReactRunnerState>;
|
|
33
|
+
componentDidUpdate(): void;
|
|
34
|
+
render(): ReactNode;
|
|
35
|
+
}
|
|
36
|
+
export {};
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ObjectUI — @object-ui/react-runtime (ADR: kind:'react')
|
|
3
|
+
*
|
|
4
|
+
* Inlined, vendored react-runner (github.com/nihgwu/react-runner, MIT): transpile
|
|
5
|
+
* a JSX/TSX source string with Sucrase, then eval it with an injected scope
|
|
6
|
+
* (React, your registered components, page data) and render in the MAIN React
|
|
7
|
+
* tree. NO sandbox — this is the *trusted* execution tier, gated to enterprise/
|
|
8
|
+
* private deployments where authors are trusted. Inlined (not depended) so we
|
|
9
|
+
* fully control the scope/imports surface and can lazy-load it behind a flag.
|
|
10
|
+
*/
|
|
11
|
+
import React, { Component, createElement, isValidElement } from 'react';
|
|
12
|
+
import { transform as sucrase } from 'sucrase';
|
|
13
|
+
const normalizeCode = (code) => code.replace(/^(\s*)(<[^>]*>|function[(\s]|\(\)[\s=]|class\s)(.*)/, '$1export default $2$3');
|
|
14
|
+
/** Transpile JSX/TS → JS (classic runtime; imports → require). */
|
|
15
|
+
export const transform = (code) => sucrase(code, { transforms: ['jsx', 'typescript', 'imports'], production: true }).code.substring(13);
|
|
16
|
+
const createRequire = (imports = {}) => (module) => {
|
|
17
|
+
if (!Object.prototype.hasOwnProperty.call(imports, module)) {
|
|
18
|
+
throw new Error(`Module not found: '${module}' — provide it via the runtime scope.imports`);
|
|
19
|
+
}
|
|
20
|
+
return imports[module];
|
|
21
|
+
};
|
|
22
|
+
const evalCode = (code, scope) => {
|
|
23
|
+
const { default: _d, import: imports, ...rest } = scope;
|
|
24
|
+
const finalScope = { React, require: createRequire(imports), ...rest };
|
|
25
|
+
const keys = Object.keys(finalScope);
|
|
26
|
+
// eslint-disable-next-line no-new-func
|
|
27
|
+
return new Function(...keys, code)(...keys.map((k) => finalScope[k]));
|
|
28
|
+
};
|
|
29
|
+
/** Transpile + eval a source string into a React element (or null). */
|
|
30
|
+
export function generateElement(code, scope = {}) {
|
|
31
|
+
if (!code.trim())
|
|
32
|
+
return null;
|
|
33
|
+
const exports = {};
|
|
34
|
+
evalCode(transform(normalizeCode(code)), { render: (v) => (exports.default = v), ...scope, exports });
|
|
35
|
+
const result = exports.default;
|
|
36
|
+
if (!result)
|
|
37
|
+
return null;
|
|
38
|
+
if (isValidElement(result))
|
|
39
|
+
return result;
|
|
40
|
+
if (typeof result === 'function')
|
|
41
|
+
return createElement(result);
|
|
42
|
+
return null;
|
|
43
|
+
}
|
|
44
|
+
/** Renders a JSX/TSX source string with a built-in error boundary. */
|
|
45
|
+
export class ReactRunner extends Component {
|
|
46
|
+
constructor() {
|
|
47
|
+
super(...arguments);
|
|
48
|
+
Object.defineProperty(this, "state", {
|
|
49
|
+
enumerable: true,
|
|
50
|
+
configurable: true,
|
|
51
|
+
writable: true,
|
|
52
|
+
value: { element: null, error: null }
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
static getDerivedStateFromProps(props) {
|
|
56
|
+
try {
|
|
57
|
+
return { element: generateElement(props.code, props.scope), error: null };
|
|
58
|
+
}
|
|
59
|
+
catch (error) {
|
|
60
|
+
return { element: null, error: error };
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
static getDerivedStateFromError(error) {
|
|
64
|
+
return { error };
|
|
65
|
+
}
|
|
66
|
+
componentDidUpdate() {
|
|
67
|
+
if (this.state.error)
|
|
68
|
+
this.props.onError?.(this.state.error);
|
|
69
|
+
}
|
|
70
|
+
render() {
|
|
71
|
+
if (this.state.error) {
|
|
72
|
+
return this.props.fallback
|
|
73
|
+
? this.props.fallback(this.state.error)
|
|
74
|
+
: createElement('pre', { style: { color: 'crimson', padding: 16, whiteSpace: 'pre-wrap' } }, String(this.state.error));
|
|
75
|
+
}
|
|
76
|
+
return this.state.element;
|
|
77
|
+
}
|
|
78
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@object-ui/react-runtime",
|
|
3
|
+
"version": "11.3.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"sideEffects": false,
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"description": "Trusted runtime React execution for kind:'react' pages — vendored react-runner (Sucrase transpile + scope-eval, no sandbox). Enterprise/private-deploy tier.",
|
|
8
|
+
"main": "./dist/index.js",
|
|
9
|
+
"module": "./dist/index.js",
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"exports": {
|
|
12
|
+
".": {
|
|
13
|
+
"types": "./dist/index.d.ts",
|
|
14
|
+
"import": "./dist/index.js",
|
|
15
|
+
"default": "./dist/index.js"
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
"files": [
|
|
19
|
+
"dist"
|
|
20
|
+
],
|
|
21
|
+
"dependencies": {
|
|
22
|
+
"sucrase": "^3.35.0"
|
|
23
|
+
},
|
|
24
|
+
"peerDependencies": {
|
|
25
|
+
"react": ">=18"
|
|
26
|
+
},
|
|
27
|
+
"devDependencies": {},
|
|
28
|
+
"scripts": {
|
|
29
|
+
"build": "tsc",
|
|
30
|
+
"test": "vitest run",
|
|
31
|
+
"type-check": "tsc --noEmit",
|
|
32
|
+
"lint": "eslint ."
|
|
33
|
+
}
|
|
34
|
+
}
|