Comparing version 0.0.1 to 0.0.2
@@ -32,108 +32,36 @@ (function (global, factory) { | ||
const INTERNAL_STATE_KEY = Symbol('state'); | ||
const isArr = (x) => Array.isArray(x); | ||
const isObj = (x) => Object.prototype.toString.call(x) === '[object Object]'; | ||
function proxy(original, onWrite, host) { | ||
const draftValue = isArr(original) ? [] : getCleanCopy(original); | ||
let proxiedKeyMap = Object.create(null); | ||
let draftState = { | ||
originalValue: original, | ||
draftValue, | ||
mutated: false, | ||
onWrite | ||
function proxy(base, onWirte) { | ||
const copy = isArr(base) ? [] : getCleanCopy(base); | ||
let map = Object.create(null); | ||
let draft = { | ||
base, | ||
copy, | ||
onWirte | ||
}; | ||
const draft = new Proxy(original, { | ||
get(target, key, receiver) { | ||
if (key === INTERNAL_STATE_KEY) | ||
return draftState; | ||
if (key in proxiedKeyMap) | ||
return proxiedKeyMap[key]; | ||
if (isObj(original[key]) && original[key] !== null) { | ||
proxiedKeyMap[key] = proxyProp(original[key], key, draftState); | ||
return proxiedKeyMap[key]; | ||
return new Proxy(base, { | ||
get(target, key) { | ||
if (key === 'IS_BERIAL_SANDBOX') | ||
return true; | ||
if (key in map) | ||
return map[key]; | ||
if (isObj(base[key]) || isArr(base[key])) { | ||
map[key] = proxy(base[key], (obj) => (copy[key] = obj)); | ||
return map[key]; | ||
} | ||
else { | ||
if (draftState.mutated) | ||
return draftValue[key]; | ||
return Reflect.get(target, key, receiver); | ||
return copy[key] || target[key]; | ||
} | ||
}, | ||
set(target, key, value) { | ||
if (isObj(value)) { | ||
proxiedKeyMap[key] = proxyProp(value, key, draftState); | ||
if (isObj(base[key]) || isArr(base[key])) { | ||
map[key] = proxy(value, (obj) => (copy[key] = obj)); | ||
} | ||
copyOnWrite(draftState); | ||
draftValue[key] = value; | ||
onWirte && onWirte(draft.onWirte); | ||
copy[key] = value; | ||
return true; | ||
}, | ||
has(_, ...args) { | ||
return Reflect.has(getTarget(draftState), ...args); | ||
}, | ||
ownKeys(_, ...args) { | ||
return Reflect.ownKeys(getTarget(draftState), ...args); | ||
}, | ||
getOwnPropertyDescriptor(_, ...args) { | ||
return Reflect.getOwnPropertyDescriptor(getTarget(draftState), ...args); | ||
}, | ||
getPrototypeOf(_, ...args) { | ||
return Reflect.getPrototypeOf(original, ...args); | ||
}, | ||
deleteProperty(_, ...args) { | ||
copyOnWrite(draftState); | ||
return Reflect.deleteProperty(draftValue, ...args); | ||
}, | ||
defineProperty(_, ...args) { | ||
copyOnWrite(draftState); | ||
return Reflect.defineProperty(draftValue, ...args); | ||
}, | ||
setPrototypeOf(_, ...args) { | ||
copyOnWrite(draftState); | ||
return Reflect.setPrototypeOf(draftValue, ...args); | ||
} | ||
}); | ||
return draft; | ||
} | ||
function proxyProp(props, key, host) { | ||
const { originalValue, draftValue, onWrite } = host; | ||
return proxy(props, (value) => { | ||
if (!draftValue.mutated) { | ||
host.mutated = true; | ||
copyProps(draftValue, originalValue); | ||
} | ||
draftValue[key] = value; | ||
if (onWrite) { | ||
onWrite(draftValue); | ||
} | ||
}); | ||
} | ||
function copyOnWrite(draftState) { | ||
const { originalValue, draftValue, mutated, onWrite } = draftState; | ||
if (!mutated) { | ||
draftState.mutated = true; | ||
if (onWrite) { | ||
onWrite(draftValue); | ||
} | ||
copyProps(draftValue, originalValue); | ||
} | ||
} | ||
function copyProps(target, source) { | ||
if (isArr(source)) { | ||
for (let i = 0; i < source.length; i++) { | ||
if (!(i in target)) { | ||
target[i] = source[i]; | ||
} | ||
} | ||
} | ||
else { | ||
Reflect.ownKeys(source).forEach((key) => { | ||
const desc = Object.getOwnPropertyDescriptor(source, key); | ||
if (!(key in target)) { | ||
Object.defineProperty(target, key, desc); | ||
} | ||
}); | ||
} | ||
} | ||
function getTarget(draftState) { | ||
return draftState.mutated ? draftState.draftValue : draftState.originalValue; | ||
} | ||
function getCleanCopy(obj) { | ||
@@ -200,3 +128,3 @@ return Object.create(Object.getPrototypeOf(obj)); | ||
const bodyNode = loadBody(template); | ||
const fake = proxy(window, null, app.host); | ||
const fake = proxy(window, null); | ||
const lifecycle = yield loadScript(template, fake, app.name); | ||
@@ -228,2 +156,3 @@ return { lifecycle, styleNodes, bodyNode }; | ||
const scripts = []; | ||
SCRIPT_URL_RE.lastIndex = SCRIPT_CONTENT_RE.lastIndex = 0; | ||
let match; | ||
@@ -251,17 +180,15 @@ while ((match = SCRIPT_URL_RE.exec(template))) { | ||
function runScript(script, global, umdName) { | ||
const resolver = new Function(` | ||
return function (window){ | ||
window.IS_BERIAL_SANDBOX = true | ||
with(window.IS_BERIAL_SANDBOX) { | ||
try { | ||
${script} | ||
return window['${umdName}'] | ||
} | ||
catch(e) { | ||
console.log(e) | ||
} | ||
const resolver = new Function('window', ` | ||
with(window.IS_BERIAL_SANDBOX) { | ||
try { | ||
${script} | ||
return window['${umdName}'] | ||
} | ||
}`); | ||
return resolver().bind(global)(global); | ||
catch(e) { | ||
console.log(e) | ||
} | ||
} | ||
`); | ||
return resolver.call(global, global); | ||
} | ||
function loadCSS(template) { | ||
@@ -284,2 +211,3 @@ return __awaiter(this, void 0, void 0, function* () { | ||
const styles = []; | ||
CSS_URL_RE.lastIndex = STYLE_RE.lastIndex = 0; | ||
let match; | ||
@@ -286,0 +214,0 @@ while ((match = CSS_URL_RE.exec(template))) { |
@@ -1,1 +0,1 @@ | ||
export declare function proxy(original: Record<string, unknown>, onWrite: any, host: any): Record<string, unknown>; | ||
export declare function proxy(base: Record<string, any>, onWirte: any): any; |
@@ -7,3 +7,5 @@ import { h, render } from 'fre' | ||
render(<App />, document.getElementById('root')) | ||
if (!window.IS_BERIAL_SANDBOX) { | ||
render(<App />, document.getElementById('root')) | ||
} | ||
@@ -10,0 +12,0 @@ export async function bootstrap() { |
@@ -25,3 +25,3 @@ { | ||
"devtools-detector": "^1.0.21", | ||
"fre": "^1.13.3", | ||
"fre": "^2.0.0-alpha.1", | ||
"snarkdown": "^1.2.2" | ||
@@ -28,0 +28,0 @@ }, |
@@ -13,3 +13,6 @@ const path = require('path') | ||
umdNamedDefine: true, | ||
publicPath: 'http://localhost:3002' | ||
publicPath: | ||
process.env.NODE_ENV === 'production' | ||
? 'https://s-sh-16-clicli.oss.dogecdn.com/' | ||
: 'http://localhost:3002' | ||
}, | ||
@@ -21,3 +24,13 @@ module: { | ||
use: { | ||
loader: 'babel-loader' | ||
loader: 'babel-loader', | ||
options: { | ||
plugins: [ | ||
[ | ||
'@babel/plugin-transform-react-jsx', | ||
{ | ||
pragma: 'h' | ||
} | ||
] | ||
] | ||
} | ||
} | ||
@@ -33,3 +46,3 @@ }, | ||
splitChunks: false, | ||
minimize: false, | ||
minimize: false | ||
}, | ||
@@ -36,0 +49,0 @@ plugins: [ |
@@ -25,3 +25,3 @@ { | ||
"devtools-detector": "^1.0.21", | ||
"fre": "^1.13.3", | ||
"fre": "^2.0.0-alpha.1", | ||
"snarkdown": "^1.2.2" | ||
@@ -28,0 +28,0 @@ }, |
@@ -23,3 +23,13 @@ const path = require('path') | ||
use: { | ||
loader: 'babel-loader' | ||
loader: 'babel-loader', | ||
options: { | ||
plugins: [ | ||
[ | ||
'@babel/plugin-transform-react-jsx', | ||
{ | ||
pragma: 'h' | ||
} | ||
] | ||
] | ||
} | ||
} | ||
@@ -26,0 +36,0 @@ }, |
{ | ||
"name": "berial", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "micro frontend", | ||
@@ -5,0 +5,0 @@ "main": "dist/berial.js", |
@@ -18,3 +18,3 @@ <p align="center"><img src="https://avatars0.githubusercontent.com/u/68577605?s=200&v=4" alt="berial logo" width="150"></p> | ||
- js sandbox | ||
- proxy sandbox | ||
@@ -21,0 +21,0 @@ - html loader |
@@ -50,3 +50,3 @@ import type { App, PromiseFn, Lifecycles, Lifecycle } from './types' | ||
const bodyNode = loadBody(template) | ||
const fake = proxy(window as any, null, app.host) | ||
const fake = proxy(window as any, null) | ||
const lifecycle = await loadScript(template, fake as any, app.name) | ||
@@ -91,2 +91,3 @@ return { lifecycle, styleNodes, bodyNode } | ||
const scripts: string[] = [] | ||
SCRIPT_URL_RE.lastIndex = SCRIPT_CONTENT_RE.lastIndex = 0 | ||
let match | ||
@@ -117,17 +118,17 @@ while ((match = SCRIPT_URL_RE.exec(template))) { | ||
): Lifecycle { | ||
const resolver = new Function(` | ||
return function (window){ | ||
window.IS_BERIAL_SANDBOX = true | ||
with(window.IS_BERIAL_SANDBOX) { | ||
try { | ||
${script} | ||
return window['${umdName}'] | ||
} | ||
catch(e) { | ||
console.log(e) | ||
} | ||
const resolver = new Function( | ||
'window', | ||
` | ||
with(window.IS_BERIAL_SANDBOX) { | ||
try { | ||
${script} | ||
return window['${umdName}'] | ||
} | ||
}`) | ||
return resolver().bind(global)(global) | ||
catch(e) { | ||
console.log(e) | ||
} | ||
} | ||
` | ||
) | ||
return resolver.call(global, global) | ||
} | ||
@@ -157,2 +158,3 @@ | ||
const styles: string[] = [] | ||
CSS_URL_RE.lastIndex = STYLE_RE.lastIndex = 0 | ||
let match | ||
@@ -159,0 +161,0 @@ while ((match = CSS_URL_RE.exec(template))) { |
132
src/proxy.ts
@@ -1,123 +0,37 @@ | ||
const INTERNAL_STATE_KEY = Symbol('state') | ||
const isArr = (x: unknown): x is Array<any> => Array.isArray(x) | ||
const isObj = (x: unknown): x is object => | ||
const isArr = (x: unknown) => Array.isArray(x) | ||
const isObj = (x: unknown) => | ||
Object.prototype.toString.call(x) === '[object Object]' | ||
export function proxy( | ||
original: Record<string, unknown>, | ||
onWrite: any, | ||
host: any | ||
) { | ||
const draftValue = isArr(original) ? [] : getCleanCopy(original) | ||
let proxiedKeyMap = Object.create(null) | ||
let draftState = { | ||
originalValue: original, | ||
draftValue, | ||
mutated: false, | ||
onWrite | ||
export function proxy(base: Record<string, any>, onWirte: any) { | ||
const copy = isArr(base) ? [] : getCleanCopy(base) | ||
let map = Object.create(null) | ||
let draft = { | ||
base, | ||
copy, | ||
onWirte | ||
} | ||
const draft = new Proxy(original, { | ||
get(target, key, receiver) { | ||
if (key === INTERNAL_STATE_KEY) return draftState | ||
if (key in proxiedKeyMap) return proxiedKeyMap[key] | ||
if (isObj(original[key as any]) && original[key as any] !== null) { | ||
proxiedKeyMap[key] = proxyProp(original[key as any], key, draftState) | ||
return proxiedKeyMap[key] | ||
return new Proxy(base, { | ||
get(target: any, key: string) { | ||
if (key === 'IS_BERIAL_SANDBOX') return true | ||
if (key in map) return map[key] | ||
if (isObj(base[key]) || isArr(base[key])) { | ||
map[key] = proxy(base[key], (obj: object) => (copy[key] = obj)) | ||
return map[key] | ||
} else { | ||
if (draftState.mutated) return draftValue[key] | ||
return Reflect.get(target, key, receiver) | ||
return copy[key] || target[key] | ||
} | ||
}, | ||
set(target, key, value) { | ||
if (isObj(value)) { | ||
proxiedKeyMap[key] = proxyProp(value, key, draftState) | ||
set(target, key: string, value) { | ||
if (isObj(base[key]) || isArr(base[key])) { | ||
map[key] = proxy(value, (obj: object) => (copy[key] = obj)) | ||
} | ||
copyOnWrite(draftState) | ||
draftValue[key] = value | ||
onWirte && onWirte(draft.onWirte) | ||
copy[key] = value | ||
return true | ||
}, | ||
has(_, ...args) { | ||
return Reflect.has(getTarget(draftState), ...args) | ||
}, | ||
ownKeys(_, ...args) { | ||
return Reflect.ownKeys(getTarget(draftState), ...args) | ||
}, | ||
getOwnPropertyDescriptor(_, ...args) { | ||
return Reflect.getOwnPropertyDescriptor(getTarget(draftState), ...args) | ||
}, | ||
getPrototypeOf(_, ...args) { | ||
return Reflect.getPrototypeOf(original, ...args) | ||
}, | ||
deleteProperty(_, ...args) { | ||
copyOnWrite(draftState) | ||
return Reflect.deleteProperty(draftValue, ...args) | ||
}, | ||
defineProperty(_, ...args) { | ||
copyOnWrite(draftState) | ||
return Reflect.defineProperty(draftValue, ...args) | ||
}, | ||
setPrototypeOf(_, ...args) { | ||
copyOnWrite(draftState) | ||
return Reflect.setPrototypeOf(draftValue, ...args) | ||
} | ||
}) | ||
return draft | ||
} | ||
function proxyProp(props: any, key: any, host: any) { | ||
const { originalValue, draftValue, onWrite } = host | ||
return proxy( | ||
props, | ||
(value: any) => { | ||
if (!draftValue.mutated) { | ||
host.mutated = true | ||
copyProps(draftValue, originalValue) | ||
} | ||
draftValue[key] = value | ||
if (onWrite) { | ||
onWrite(draftValue) | ||
} | ||
}, | ||
null | ||
) | ||
} | ||
function copyOnWrite(draftState: any) { | ||
const { originalValue, draftValue, mutated, onWrite } = draftState | ||
if (!mutated) { | ||
draftState.mutated = true | ||
if (onWrite) { | ||
onWrite(draftValue) | ||
} | ||
copyProps(draftValue, originalValue) | ||
} | ||
} | ||
function copyProps(target: any, source: any) { | ||
if (isArr(source)) { | ||
for (let i = 0; i < source.length; i++) { | ||
if (!(i in target)) { | ||
target[i] = source[i] | ||
} | ||
} | ||
} else { | ||
Reflect.ownKeys(source).forEach((key) => { | ||
const desc = Object.getOwnPropertyDescriptor(source, key) as any | ||
if (!(key in target)) { | ||
Object.defineProperty(target, key, desc) | ||
} | ||
}) | ||
} | ||
} | ||
function getTarget(draftState: any) { | ||
return draftState.mutated ? draftState.draftValue : draftState.originalValue | ||
} | ||
function getCleanCopy(obj: any) { | ||
function getCleanCopy(obj: object) { | ||
return Object.create(Object.getPrototypeOf(obj)) | ||
} |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
1
78274
34
1280
3