-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathApp.tsx
More file actions
339 lines (319 loc) · 9.48 KB
/
Copy pathApp.tsx
File metadata and controls
339 lines (319 loc) · 9.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/**
* Origin Pooling Demo
*
* Dynamically add/remove sandboxes under two shared origins (alpha, beta)
* plus isolated sandboxes (each gets a unique origin and its own VM).
* Same-origin sandboxes share a ReactHost / Hermes VM; removing the last
* one triggers the idle TTL.
*
* Access control demo: alpha and beta only accept messages from "isolated-1".
* Other isolated sandboxes (isolated-2, isolated-3, …) will get
* AccessDeniedError when trying to ping alpha or beta.
*
* Messaging is handled inside the sandbox widget via globalThis.postMessage.
* The host only logs messages received via onMessage.
*/
import SandboxReactNativeView from '@callstack/react-native-sandbox'
import React, {useCallback, useRef, useState} from 'react'
import {
Button,
Platform,
SafeAreaView,
ScrollView,
StatusBar,
StyleSheet,
Text,
View,
} from 'react-native'
type SandboxEntry = {key: string; label: string; origin: string}
type LogEntry = {source: string; text: string; ts: number}
let nextId = 0
let nextIsolatedId = 0
const ORIGIN_ALPHA = 'alpha'
const ORIGIN_BETA = 'beta'
const ISOLATED_PREFIX = 'isolated-'
const COLOR_ALPHA = '#8232ff'
const COLOR_BETA = '#e67e22'
const COLOR_ISOLATED = '#6c757d'
/**
* Only "isolated-1" is permitted to send messages to alpha/beta.
* All other isolated origins will be denied.
*/
const PERMITTED_ISOLATED = `${ISOLATED_PREFIX}1`
/** Alpha uses a function-based TTL (4 seconds) */
const ALPHA_TTL = () => 4000
/** Beta and isolated use a static TTL (2 seconds) */
const DEFAULT_TTL = 2000
export default function App() {
const [sandboxes, setSandboxes] = useState<SandboxEntry[]>([])
const [log, setLog] = useState<LogEntry[]>([])
const logScrollRef = useRef<ScrollView>(null)
const addLog = useCallback((source: string, text: string) => {
setLog(prev => [...prev.slice(-49), {source, text, ts: Date.now()}])
}, [])
const addSandbox = useCallback((origin: string) => {
const id = String(++nextId)
const actualOrigin =
origin === ISOLATED_PREFIX
? `${ISOLATED_PREFIX}${++nextIsolatedId}`
: origin
setSandboxes(prev => [
...prev,
{key: id, label: `#${id}`, origin: actualOrigin},
])
}, [])
const removeSandbox = useCallback((key: string) => {
setSandboxes(prev => prev.filter(s => s.key !== key))
}, [])
const clearLog = useCallback(() => setLog([]), [])
const alphas = sandboxes.filter(s => s.origin === ORIGIN_ALPHA)
const betas = sandboxes.filter(s => s.origin === ORIGIN_BETA)
const isolated = sandboxes.filter(s => s.origin.startsWith(ISOLATED_PREFIX))
return (
<SafeAreaView style={styles.safe}>
<Text style={styles.heading}>Origin Pooling Demo</Text>
<Text style={styles.subtitle}>
Same-origin sandboxes share a VM. Isolated sandboxes each get a unique
origin (own VM). Only isolated-1 can message alpha/beta — others get
AccessDeniedError.
</Text>
<View style={styles.controls}>
<Button
title="+ Alpha"
color={COLOR_ALPHA}
onPress={() => addSandbox(ORIGIN_ALPHA)}
/>
<Button
title="+ Beta"
color={COLOR_BETA}
onPress={() => addSandbox(ORIGIN_BETA)}
/>
<Button
title="+ Isolated"
color={COLOR_ISOLATED}
onPress={() => addSandbox(ISOLATED_PREFIX)}
/>
<Button title="Clear Log" onPress={clearLog} />
</View>
{/* Alpha sandboxes */}
<Text style={[styles.groupLabel, {color: COLOR_ALPHA}]}>
{'origin="alpha"'} ({alphas.length})
</Text>
<ScrollView
horizontal
style={styles.cardRow}
contentContainerStyle={styles.cardRowContent}>
{alphas.map(sb => (
<SandboxCard
key={sb.key}
entry={sb}
color={COLOR_ALPHA}
componentName="SandboxAppConvention"
allowedOrigins={[ORIGIN_ALPHA, ORIGIN_BETA, PERMITTED_ISOLATED]}
idleTTL={ALPHA_TTL}
onRemove={() => removeSandbox(sb.key)}
onMessage={data =>
addLog(`alpha ${sb.label}`, JSON.stringify(data))
}
onError={err =>
addLog(`alpha ${sb.label}`, `ERROR: ${err.name} — ${err.message}`)
}
/>
))}
{alphas.length === 0 && (
<Text style={styles.empty}>No alpha sandboxes yet.</Text>
)}
</ScrollView>
{/* Beta sandboxes */}
<Text style={[styles.groupLabel, {color: COLOR_BETA}]}>
{'origin="beta"'} ({betas.length})
</Text>
<ScrollView
horizontal
style={styles.cardRow}
contentContainerStyle={styles.cardRowContent}>
{betas.map(sb => (
<SandboxCard
key={sb.key}
entry={sb}
color={COLOR_BETA}
componentName="SandboxApp"
allowedOrigins={[ORIGIN_ALPHA, ORIGIN_BETA, PERMITTED_ISOLATED]}
idleTTL={DEFAULT_TTL}
onRemove={() => removeSandbox(sb.key)}
onMessage={data => addLog(`beta ${sb.label}`, JSON.stringify(data))}
onError={err =>
addLog(`beta ${sb.label}`, `ERROR: ${err.name} — ${err.message}`)
}
/>
))}
{betas.length === 0 && (
<Text style={styles.empty}>No beta sandboxes yet.</Text>
)}
</ScrollView>
{/* Isolated sandboxes — each gets a unique origin (own VM) */}
<Text style={[styles.groupLabel, {color: COLOR_ISOLATED}]}>
isolated ({isolated.length}) — only isolated-1 can reach alpha/beta
</Text>
<ScrollView
horizontal
style={styles.cardRow}
contentContainerStyle={styles.cardRowContent}>
{isolated.map(sb => (
<SandboxCard
key={sb.key}
entry={sb}
color={COLOR_ISOLATED}
componentName="SandboxApp"
allowedOrigins={[ORIGIN_ALPHA, ORIGIN_BETA]}
idleTTL={DEFAULT_TTL}
onRemove={() => removeSandbox(sb.key)}
onMessage={data =>
addLog(`isolated ${sb.label}`, JSON.stringify(data))
}
onError={err =>
addLog(
`isolated ${sb.label}`,
`ERROR: ${err.name} — ${err.message}`
)
}
/>
))}
{isolated.length === 0 && (
<Text style={styles.empty}>No isolated sandboxes yet.</Text>
)}
</ScrollView>
{/* Event log */}
<Text style={styles.logTitle}>Event Log</Text>
<ScrollView
ref={logScrollRef}
style={styles.logScroll}
onContentSizeChange={() => logScrollRef.current?.scrollToEnd()}>
{log.map((e, i) => (
<Text key={i} style={styles.logLine}>
<Text style={styles.logSource}>[{e.source}]</Text> {e.text}
</Text>
))}
</ScrollView>
</SafeAreaView>
)
}
type SandboxCardProps = {
entry: SandboxEntry
color: string
componentName: string
allowedOrigins: string[]
idleTTL: number | (() => number)
onRemove: () => void
onMessage: (data: unknown) => void
onError: (err: {name: string; message: string}) => void
}
function SandboxCard({
entry,
color,
componentName,
allowedOrigins,
idleTTL,
onRemove,
onMessage,
onError,
}: SandboxCardProps) {
return (
<View style={[styles.card, {borderColor: color}]}>
<View style={[styles.cardHeader, {backgroundColor: color}]}>
<Text style={styles.cardLabel}>
{entry.origin} {entry.label}
</Text>
<Text style={styles.cardRemove} onPress={onRemove}>
✕
</Text>
</View>
<SandboxReactNativeView
origin={entry.origin}
allowedOrigins={allowedOrigins}
idleTTL={idleTTL}
componentName={componentName}
jsBundleSource="sandbox"
onMessage={onMessage}
onError={onError}
style={styles.sandboxView}
/>
</View>
)
}
const styles = StyleSheet.create({
safe: {
flex: 1,
backgroundColor: '#f5f5f5',
paddingTop: Platform.OS === 'android' ? StatusBar.currentHeight : 0,
},
heading: {
fontSize: 20,
fontWeight: '700',
textAlign: 'center',
marginTop: 8,
marginBottom: 2,
},
subtitle: {
fontSize: 11,
color: '#6c757d',
textAlign: 'center',
marginBottom: 6,
paddingHorizontal: 16,
},
controls: {
flexDirection: 'row',
justifyContent: 'space-evenly',
paddingHorizontal: 8,
paddingBottom: 4,
},
groupLabel: {
fontSize: 12,
fontWeight: '600',
paddingHorizontal: 12,
paddingTop: 2,
},
cardRow: {height: 150, flexGrow: 0},
cardRowContent: {paddingHorizontal: 8, gap: 8},
card: {
width: 200,
borderWidth: 2,
borderRadius: 8,
overflow: 'hidden',
},
cardLabel: {
color: '#fff',
fontSize: 11,
fontWeight: '600',
textAlign: 'center',
paddingVertical: 2,
flex: 1,
},
cardHeader: {
flexDirection: 'row',
alignItems: 'center',
},
cardRemove: {
color: '#fff',
fontSize: 14,
fontWeight: '700',
paddingHorizontal: 8,
paddingVertical: 2,
},
sandboxView: {flex: 1},
empty: {
color: '#999',
fontStyle: 'italic',
alignSelf: 'center',
paddingTop: 60,
},
logTitle: {
fontSize: 14,
fontWeight: '600',
paddingHorizontal: 12,
paddingTop: 4,
},
logScroll: {flex: 1, paddingHorizontal: 12, paddingTop: 4},
logLine: {fontSize: 11, fontFamily: 'monospace', marginBottom: 2},
logSource: {fontWeight: '700', color: '#8232ff'},
})