Skip to content

Commit d46c1ec

Browse files
authored
Fix render crash when text updates (#32)
1 parent 677f1d1 commit d46c1ec

File tree

4 files changed

+13
-17
lines changed

4 files changed

+13
-17
lines changed

packages/core/src/lavadome.mjs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
from, stringify,
88
createElement,
99
appendChild,
10+
replaceChildren,
1011
textContentSet,
1112
} from './native.mjs';
1213
import {distraction, unselectable} from './element.mjs';
@@ -18,9 +19,12 @@ export function LavaDome(host, opts) {
1819
// make exported API tamper-proof
1920
defineProperties(this, {text: {value: text}});
2021

22+
// get/create shadow for host (empty shadow content if there's any already)
23+
const shadow = getShadow(host, opts);
24+
replaceChildren(shadow);
25+
2126
// child of the shadow, where the secret is set, must be unselectable
2227
const child = unselectable();
23-
const shadow = getShadow(host, opts);
2428
appendChild(shadow, child);
2529

2630
function text(text) {

packages/core/src/native.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ const randomUUID = crypto?.randomUUID?.bind(crypto);
2020
const n = (obj, prop, accessor) =>
2121
obj && Function.prototype.call.bind(getOwnPropertyDescriptor(obj, prop)[accessor]);
2222

23+
export const replaceChildren = n(globalThis?.DocumentFragment?.prototype, 'replaceChildren', 'value');
2324
export const attachShadow = n(globalThis?.Element?.prototype, 'attachShadow', 'value');
2425
export const createElement = n(globalThis?.Document?.prototype, 'createElement', 'value');
2526
export const appendChild = n(globalThis?.Node?.prototype, 'appendChild', 'value');

packages/react/demo/App.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export default function App() {
2222
<p id="PRIVATE">
2323
<LavaDomeReact
2424
unsafeOpenModeShadow={unsafeOpenModeShadow}
25-
text={toLavaDomeToken('SECRET_CONTENT_ONLY_ACCESSIBLE_TO_LAVADOME')}
25+
text={toLavaDomeToken(`SECRET_CONTENT_ONLY_ACCESSIBLE_TO_LAVADOME: "${count}"`)}
2626
/>
2727
</p>
2828
</div>

packages/react/src/lavadome.jsx

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,14 @@ export const LavaDome = ({ text, unsafeOpenModeShadow }) => {
1818
};
1919

2020
function LavaDomeShadow({ host, token, unsafeOpenModeShadow }) {
21-
let lavadome;
22-
23-
// exchange token for sensitive text before check
24-
const text = tokenToText(token, unsafeOpenModeShadow);
25-
26-
// generate a lavadome instance reference with a teardown
27-
useEffect(() => {
28-
const opts = { unsafeOpenModeShadow };
29-
lavadome = new LavaDomeCore(host.current, opts);
30-
return () => lavadome = null;
31-
}, []);
32-
33-
// use a unique and useless representation of the token as the useEffect dep
34-
const dep = tokenToDep(token);
21+
const
22+
// exchange token for sensitive text before check
23+
text = tokenToText(token, unsafeOpenModeShadow),
24+
// use a unique and useless representation of the token as the useEffect dep
25+
dep = tokenToDep(token);
3526

3627
// update lavadome secret text (given that the token is updated too)
37-
useEffect(() => lavadome.text(text), [dep]);
28+
useEffect(() => new LavaDomeCore(host.current, { unsafeOpenModeShadow }).text(text), [dep]);
3829

3930
return <></>;
4031
}

0 commit comments

Comments
 (0)