frameos-wasm 2026.11.13 → 2026.11.15

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/README.md CHANGED
@@ -148,6 +148,35 @@ await preview.resetAssets() // wipe + regenerate the samples
148
148
  `onReady(sceneInfo, assets)` reports whether the folder is persistent or in-memory.
149
149
  Pass `browserAssets: false` for an empty in-memory folder instead.
150
150
 
151
+ ### Files from and to the computer
152
+
153
+ The Files scene has Upload and Download here (`fileTransfer`, on by default; a bundle that has them
154
+ says `runtimeInfo.fileTransfer`). A file picker and a download need a press on the preview itself
155
+ (a pointer or a key on a canvas `attachPointerInput` / `attachKeyboardInput` forwards, within the
156
+ last five seconds: `previewHasGesture()`), because any scene's code can ask and a click elsewhere on
157
+ the page is not its permission. The tap on Upload is such a press, and still the page's gesture
158
+ when the request arrives a few milliseconds later, so the preview opens the picker (or saves the
159
+ file) at once. With no press behind the request, or when the page has no gesture left
160
+ (`navigator.userActivation.isActive` is false, or the browser cannot say), it calls
161
+ `onFileRequest(request)` instead: show a button whose click calls `preview.answerFileRequest()`,
162
+ and one for `preview.cancelFileRequest()`; `onFileRequest(null)` says it is over. Without
163
+ `onFileRequest` a request that follows a press is tried anyway and any other is turned down (the
164
+ scene hears `cancelled`). The files picked go into the folder the scene shows, in the browser asset
165
+ folder above. A file is saved under its name cleaned by `safeFileName` (no folders, no control or
166
+ direction-changing characters, no leading dot, 120 characters at most), which is also the `name`
167
+ the request carries.
168
+
169
+ The frame's Copy and Cut reach the computer's clipboard under the same rule: only after a press on
170
+ the preview (`writeHostClipboard`).
171
+
172
+ ```ts
173
+ const preview = createFrameOSPreview({
174
+ ...options,
175
+ onFileRequest: (request) => showAsk(request), // {id, op: 'upload' | 'download', name} or null
176
+ })
177
+ askButton.onclick = () => preview.answerFileRequest()
178
+ ```
179
+
151
180
  Helpers for building your own UI are exported too: `evaluateShowIf`, `visiblePublicStateFields`,
152
181
  `coerceStateFieldValue`, `sceneEventButtons`, and the `StateField`/`FrameOSScene` types.
153
182
 
@@ -4343,6 +4343,25 @@ var stringToUTF8Array = (str, heap, outIdx, maxBytesToWrite) => {
4343
4343
  return 'The preview cannot reach ' + host + ': its server does not connect to that address.'
4344
4344
  }
4345
4345
 
4346
+ function frameosRoute(url) {
4347
+ var inBrowser = typeof self !== 'undefined' && self && self.location && typeof self.location.href === 'string'
4348
+ var target = null
4349
+ try {
4350
+ target = inBrowser ? new URL(url, self.location.href) : new URL(url)
4351
+ } catch (e) {}
4352
+ var refusedDirect = null
4353
+ if (inBrowser) {
4354
+ if (!target) {
4355
+ refusedDirect = 'unparseable'
4356
+ } else if (target.origin === self.location.origin || (target.protocol !== 'http:' && target.protocol !== 'https:')) {
4357
+ refusedDirect = 'same-origin'
4358
+ } else if (frameosIsPrivateHost(target.hostname) && !frameosIsPrivateHost(self.location.hostname)) {
4359
+ refusedDirect = 'private-network'
4360
+ }
4361
+ }
4362
+ return { inBrowser: inBrowser, target: target, refusedDirect: refusedDirect }
4363
+ }
4364
+
4346
4365
  function _fos_nim_http_request(
4347
4366
  methodPtr,
4348
4367
  urlPtr,
@@ -4376,6 +4395,61 @@ var stringToUTF8Array = (str, heap, outIdx, maxBytesToWrite) => {
4376
4395
  error.frameosRefusal = true
4377
4396
  return error
4378
4397
  }
4398
+ // The bytes into the runtime's heap, with the status and the response's
4399
+ // header lines, as one answer.
4400
+ var answer = function (status, bytes, head) {
4401
+ if (maxBytes > 0 && bytes.length > maxBytes) {
4402
+ if (Module['onFrameosLog']) {
4403
+ Module['onFrameosLog']('http: response for ' + frameosLogUrl(url) + ' exceeded ' + maxBytes + ' bytes')
4404
+ }
4405
+ return 0
4406
+ }
4407
+ var ptr = _malloc(bytes.length > 0 ? bytes.length : 1)
4408
+ if (!ptr) return 0
4409
+ HEAPU8.set(bytes, ptr)
4410
+ if (outHeadersPtr) {
4411
+ // frameos.httpRequest's `headers`: "name: value" lines into the
4412
+ // caller's 2048-byte block (FOS_NIM_HTTP_HEADERS_BYTES, as the ESP32
4413
+ // glue writes it), whole lines only. Through the proxy they are the
4414
+ // proxy's answer's.
4415
+ var lines = new TextEncoder().encode(head || '')
4416
+ var fit = lines.length
4417
+ if (fit > 2047) fit = lines.lastIndexOf(10, 2046) + 1
4418
+ HEAPU8.set(lines.subarray(0, fit), outHeadersPtr)
4419
+ HEAPU8[outHeadersPtr + fit] = 0
4420
+ }
4421
+ HEAP32[((outStatusPtr)>>2)] = status
4422
+ HEAP32[((outLenPtr)>>2)] = bytes.length
4423
+ return ptr
4424
+ }
4425
+ // A refusal's words as the request's error (status 0): they name a host,
4426
+ // never a path or a query.
4427
+ var refusalAnswer = function (message) {
4428
+ var said = new TextEncoder().encode(message)
4429
+ var saidPtr = _malloc(said.length > 0 ? said.length : 1)
4430
+ if (saidPtr) {
4431
+ HEAPU8.set(said, saidPtr)
4432
+ HEAP32[((outLenPtr)>>2)] = said.length
4433
+ }
4434
+ return saidPtr
4435
+ }
4436
+ // A picture fetched ahead (frameosPictureFetch): answered from what came,
4437
+ // and a failure said once, then asked again next time.
4438
+ var pictures = method === 'GET' && bodyLen === 0 ? Module['frameosPictures'] : null
4439
+ var kept = pictures ? pictures.entries.get(url) : null
4440
+ if (kept && kept.state === 'here') {
4441
+ pictures.entries.delete(url)
4442
+ pictures.entries.set(url, kept)
4443
+ return answer(kept.status, kept.bytes, kept.head)
4444
+ }
4445
+ if (kept && kept.state === 'failed') {
4446
+ pictures.entries.delete(url)
4447
+ if (Module['onFrameosLog']) {
4448
+ var failedAt = frameosLogUrl(url)
4449
+ Module['onFrameosLog']('http: GET ' + failedAt + ' failed: ' + (url ? String(kept.error).split(url).join(failedAt) : kept.error))
4450
+ }
4451
+ return kept.refusal ? refusalAnswer(kept.refusal) : 0
4452
+ }
4379
4453
  try {
4380
4454
  var body = null
4381
4455
  if (bodyLen > 0) {
@@ -4394,35 +4468,12 @@ var stringToUTF8Array = (str, heap, outIdx, maxBytesToWrite) => {
4394
4468
  }
4395
4469
  }
4396
4470
 
4397
- // Scene code is untrusted (it may come from the public store). This
4398
- // worker shares the app's origin, so a direct request to the app's own
4399
- // API would carry the user's session cookie and read the answer — a
4400
- // same-origin target must only ever go through the server-side proxy,
4401
- // which fetches without cookies and applies the SSRF guard.
4402
- // The same goes for the viewer's own network when the app itself is
4403
- // on the public internet: a request from the viewer's browser to
4404
- // http://192.168.1.1/ cannot read the reply without CORS, but it still
4405
- // reaches the router (timing, and side effects of a GET). Chrome's
4406
- // Private Network Access blocks that; Firefox and Safari do not. A
4407
- // self-hosted app on the LAN (or localhost) keeps direct access to
4408
- // private hosts, as a frame on that LAN would have.
4409
- // (Under Node — the headless renderers — there is no origin to protect;
4410
- // the guard only applies inside a browser context.)
4411
- var refusedDirect = null
4412
- var inBrowser = typeof self !== 'undefined' && self && self.location && typeof self.location.href === 'string'
4413
- var target = null
4414
- try {
4415
- target = inBrowser ? new URL(url, self.location.href) : new URL(url)
4416
- } catch (e) {}
4417
- if (inBrowser) {
4418
- if (!target) {
4419
- refusedDirect = 'unparseable'
4420
- } else if (target.origin === self.location.origin || (target.protocol !== 'http:' && target.protocol !== 'https:')) {
4421
- refusedDirect = 'same-origin'
4422
- } else if (frameosIsPrivateHost(target.hostname) && !frameosIsPrivateHost(self.location.hostname)) {
4423
- refusedDirect = 'private-network'
4424
- }
4425
- }
4471
+ // Where it may go from here (frameosRoute: never the app's own origin
4472
+ // straight, nor the viewer's network from a public page).
4473
+ var route = frameosRoute(url)
4474
+ var refusedDirect = route.refusedDirect
4475
+ var inBrowser = route.inBrowser
4476
+ var target = route.target
4426
4477
 
4427
4478
  // A cloud frame's local network access (docs/lan-access.md), when the
4428
4479
  // host page says what it is (`frameosLanAccess: false`): a preview of a
@@ -4518,30 +4569,8 @@ var stringToUTF8Array = (str, heap, outIdx, maxBytesToWrite) => {
4518
4569
  } else {
4519
4570
  bytes = new TextEncoder().encode(xhr.responseText || '')
4520
4571
  }
4521
- if (maxBytes > 0 && bytes.length > maxBytes) {
4522
- if (Module['onFrameosLog']) {
4523
- Module['onFrameosLog']('http: response for ' + frameosLogUrl(url) + ' exceeded ' + maxBytes + ' bytes')
4524
- }
4525
- return 0
4526
- }
4527
- var ptr = _malloc(bytes.length > 0 ? bytes.length : 1)
4528
- if (!ptr) return 0
4529
- HEAPU8.set(bytes, ptr)
4530
- if (outHeadersPtr) {
4531
- // frameos.httpRequest's `headers`: "name: value" lines into the
4532
- // caller's 2048-byte block (FOS_NIM_HTTP_HEADERS_BYTES, as the ESP32
4533
- // glue writes it), whole lines only. Through the proxy they are the
4534
- // proxy's answer's.
4535
- // A Node renderer's XHR shim may have no getAllResponseHeaders.
4536
- var head = new TextEncoder().encode((xhr.getAllResponseHeaders && xhr.getAllResponseHeaders()) || '')
4537
- var fit = head.length
4538
- if (fit > 2047) fit = head.lastIndexOf(10, 2046) + 1
4539
- HEAPU8.set(head.subarray(0, fit), outHeadersPtr)
4540
- HEAPU8[outHeadersPtr + fit] = 0
4541
- }
4542
- HEAP32[((outStatusPtr)>>2)] = xhr.status
4543
- HEAP32[((outLenPtr)>>2)] = bytes.length
4544
- return ptr
4572
+ // A Node renderer's XHR shim may have no getAllResponseHeaders.
4573
+ return answer(xhr.status, bytes, (xhr.getAllResponseHeaders && xhr.getAllResponseHeaders()) || '')
4545
4574
  } catch (e) {
4546
4575
  if (Module['onFrameosLog']) {
4547
4576
  // A browser's network error can quote the URL it failed on, too.
@@ -4550,26 +4579,176 @@ var stringToUTF8Array = (str, heap, outIdx, maxBytesToWrite) => {
4550
4579
  Module['onFrameosLog']('http: ' + method + ' ' + origin + ' failed: ' + reason)
4551
4580
  }
4552
4581
  if (e && e.frameosRefusal) {
4553
- // Its words go to the scene as the request's error: they name a host,
4554
- // never a path or a query.
4555
- var said = new TextEncoder().encode(e.message)
4556
- var saidPtr = _malloc(said.length > 0 ? said.length : 1)
4557
- if (saidPtr) {
4558
- HEAPU8.set(said, saidPtr)
4559
- HEAP32[((outLenPtr)>>2)] = said.length
4560
- }
4561
- return saidPtr
4582
+ // Its words go to the scene as the request's error.
4583
+ return refusalAnswer(e.message)
4562
4584
  }
4563
4585
  return 0
4564
4586
  }
4565
4587
  }
4566
4588
 
4589
+ function frameosPictures() {
4590
+ if (!Module['frameosPictures']) {
4591
+ // `atOnce` fetches in flight, `keepBytes` kept, none over `largest`.
4592
+ Module['frameosPictures'] = {
4593
+ entries: new Map(),
4594
+ bytes: 0,
4595
+ flying: 0,
4596
+ queue: [],
4597
+ atOnce: 6,
4598
+ keepBytes: 64 * 1024 * 1024,
4599
+ largest: 16 * 1024 * 1024,
4600
+ }
4601
+ }
4602
+ return Module['frameosPictures']
4603
+ }
4604
+
4605
+
4606
+
4607
+ function frameosPictureFetch(url, entry) {
4608
+ var TIMEOUT_MS = 15000
4609
+ var pictures = frameosPictures()
4610
+ pictures.flying++
4611
+ entry.state = 'coming'
4612
+ var proxyUrl = Module['frameosProxyUrl']
4613
+ var route = frameosRoute(url)
4614
+ var origin = route.target ? route.target.origin : null
4615
+ var directBlocked = Module['frameosDirectBlocked'] || (Module['frameosDirectBlocked'] = {})
4616
+ var get = function (proxied) {
4617
+ var controller = typeof AbortController === 'function' ? new AbortController() : null
4618
+ var timer = controller ? setTimeout(function () { controller.abort() }, TIMEOUT_MS) : null
4619
+ var options = proxied
4620
+ ? {
4621
+ method: 'POST',
4622
+ credentials: 'include',
4623
+ headers: { 'Content-Type': 'application/json' },
4624
+ body: JSON.stringify({ method: 'GET', url: url, headers: {}, bodyBase64: '', timeoutMs: TIMEOUT_MS }),
4625
+ }
4626
+ : { credentials: 'omit' }
4627
+ if (controller) options.signal = controller.signal
4628
+ return fetch(proxied ? proxyUrl : url, options)
4629
+ .then(function (response) {
4630
+ var refusedBy = proxied && response.status === 403 ? response.headers.get('X-FrameOS-Proxy-Refused') : null
4631
+ if (refusedBy) {
4632
+ var refused = new Error(frameosProxyRefusal(refusedBy, route.target ? route.target.hostname : ''))
4633
+ refused.frameosRefusal = true
4634
+ throw refused
4635
+ }
4636
+ return response.arrayBuffer().then(function (body) {
4637
+ return { response: response, body: body }
4638
+ })
4639
+ })
4640
+ .then(
4641
+ function (got) {
4642
+ if (timer) clearTimeout(timer)
4643
+ return got
4644
+ },
4645
+ function (error) {
4646
+ if (timer) clearTimeout(timer)
4647
+ throw error
4648
+ }
4649
+ )
4650
+ }
4651
+ var viaProxy = !!proxyUrl && (Module['frameosProxyFirst'] === true || (origin !== null && directBlocked[origin] === true))
4652
+ var attempt = viaProxy
4653
+ ? get(true)
4654
+ : get(false).catch(function (error) {
4655
+ // The browser could not reach it (CORS, mostly): the proxy, from
4656
+ // now on for that origin, as fos_nim_http_request goes.
4657
+ if (!proxyUrl || origin === null) throw error
4658
+ directBlocked[origin] = true
4659
+ return get(true)
4660
+ })
4661
+ var settle = function () {
4662
+ pictures.flying--
4663
+ while (pictures.flying < pictures.atOnce && pictures.queue.length > 0) {
4664
+ var nextUrl = pictures.queue.shift()
4665
+ var next = pictures.entries.get(nextUrl)
4666
+ if (next && next.state === 'queued') frameosPictureFetch(nextUrl, next)
4667
+ }
4668
+ if (Module['onFrameosPictures']) Module['onFrameosPictures']()
4669
+ }
4670
+ attempt.then(
4671
+ function (got) {
4672
+ if (pictures.entries.get(url) !== entry) return settle()
4673
+ var bytes = new Uint8Array(got.body)
4674
+ if (bytes.length > pictures.largest) {
4675
+ // Too big to keep: the runtime's own request fetches it (`skip`),
4676
+ // and says so if it is too big for the runtime too.
4677
+ entry.state = 'skip'
4678
+ return settle()
4679
+ }
4680
+ var head = ''
4681
+ got.response.headers.forEach(function (value, name) {
4682
+ head += name + ': ' + value + '\r\n'
4683
+ })
4684
+ entry.state = 'here'
4685
+ entry.status = got.response.status
4686
+ entry.bytes = bytes
4687
+ entry.head = head
4688
+ pictures.bytes += bytes.length
4689
+ // The least recently used go first, never this one.
4690
+ var keys = pictures.entries.keys()
4691
+ while (pictures.bytes > pictures.keepBytes) {
4692
+ var step = keys.next()
4693
+ if (step.done) break
4694
+ var old = pictures.entries.get(step.value)
4695
+ if (old === entry || old.state !== 'here') continue
4696
+ pictures.bytes -= old.bytes.length
4697
+ pictures.entries.delete(step.value)
4698
+ }
4699
+ settle()
4700
+ },
4701
+ function (error) {
4702
+ if (pictures.entries.get(url) === entry) {
4703
+ entry.state = 'failed'
4704
+ entry.refusal = error && error.frameosRefusal ? error.message : ''
4705
+ entry.error = String(error)
4706
+ }
4707
+ settle()
4708
+ }
4709
+ )
4710
+ }
4711
+
4712
+
4713
+
4714
+ function _fos_wasm_picture(urlPtr) {
4715
+ var url = UTF8ToString(urlPtr)
4716
+ if (typeof fetch !== 'function') return 0
4717
+ var route = frameosRoute(url)
4718
+ if (!route.inBrowser || !route.target || route.refusedDirect) return 0
4719
+ if (Module['frameosLanAccess'] === false && frameosIsPrivateHost(route.target.hostname)) return 0
4720
+ var pictures = frameosPictures()
4721
+ var entry = pictures.entries.get(url)
4722
+ if (entry) {
4723
+ if (entry.state === 'skip') return 0
4724
+ return entry.state === 'here' || entry.state === 'failed' ? 2 : 1
4725
+ }
4726
+ entry = { state: 'queued' }
4727
+ pictures.entries.set(url, entry)
4728
+ if (pictures.flying < pictures.atOnce) {
4729
+ frameosPictureFetch(url, entry)
4730
+ } else {
4731
+ pictures.queue.push(url)
4732
+ }
4733
+ return 1
4734
+ }
4735
+
4736
+ function _fos_wasm_pictures_waiting() {
4737
+ if (Module['onFrameosPictures']) Module['onFrameosPictures']()
4738
+ }
4739
+
4567
4740
  function _frameos_wasm_js_event(eventPtr, payloadPtr) {
4568
4741
  if (Module['onFrameosEvent']) {
4569
4742
  Module['onFrameosEvent'](UTF8ToString(eventPtr), UTF8ToString(payloadPtr))
4570
4743
  }
4571
4744
  }
4572
4745
 
4746
+ function _frameos_wasm_js_file_request(requestPtr) {
4747
+ if (Module['onFrameosFileRequest']) {
4748
+ Module['onFrameosFileRequest'](UTF8ToString(requestPtr))
4749
+ }
4750
+ }
4751
+
4573
4752
  function _frameos_wasm_js_log(msgPtr) {
4574
4753
  var msg = UTF8ToString(msgPtr)
4575
4754
  if (Module['onFrameosLog']) {
@@ -4768,6 +4947,8 @@ var _free,
4768
4947
  _frameos_wasm_memory_render_headroom,
4769
4948
  _frameos_wasm_memory_used,
4770
4949
  _frameos_wasm_set_store_preview,
4950
+ _frameos_wasm_set_file_transfer,
4951
+ _frameos_wasm_file_transfer_done,
4771
4952
  _frameos_wasm_init,
4772
4953
  _frameos_wasm_load_scenes,
4773
4954
  _frameos_wasm_set_save_assets,
@@ -4832,6 +5013,8 @@ function assignWasmExports(wasmExports) {
4832
5013
  _frameos_wasm_memory_render_headroom = Module['_frameos_wasm_memory_render_headroom'] = wasmExports['frameos_wasm_memory_render_headroom'];
4833
5014
  _frameos_wasm_memory_used = Module['_frameos_wasm_memory_used'] = wasmExports['frameos_wasm_memory_used'];
4834
5015
  _frameos_wasm_set_store_preview = Module['_frameos_wasm_set_store_preview'] = wasmExports['frameos_wasm_set_store_preview'];
5016
+ _frameos_wasm_set_file_transfer = Module['_frameos_wasm_set_file_transfer'] = wasmExports['frameos_wasm_set_file_transfer'];
5017
+ _frameos_wasm_file_transfer_done = Module['_frameos_wasm_file_transfer_done'] = wasmExports['frameos_wasm_file_transfer_done'];
4835
5018
  _frameos_wasm_init = Module['_frameos_wasm_init'] = wasmExports['frameos_wasm_init'];
4836
5019
  _frameos_wasm_load_scenes = Module['_frameos_wasm_load_scenes'] = wasmExports['frameos_wasm_load_scenes'];
4837
5020
  _frameos_wasm_set_save_assets = Module['_frameos_wasm_set_save_assets'] = wasmExports['frameos_wasm_set_save_assets'];
@@ -4951,8 +5134,14 @@ var wasmImports = {
4951
5134
  /** @export */
4952
5135
  fos_nim_http_request: _fos_nim_http_request,
4953
5136
  /** @export */
5137
+ fos_wasm_picture: _fos_wasm_picture,
5138
+ /** @export */
5139
+ fos_wasm_pictures_waiting: _fos_wasm_pictures_waiting,
5140
+ /** @export */
4954
5141
  frameos_wasm_js_event: _frameos_wasm_js_event,
4955
5142
  /** @export */
5143
+ frameos_wasm_js_file_request: _frameos_wasm_js_file_request,
5144
+ /** @export */
4956
5145
  frameos_wasm_js_log: _frameos_wasm_js_log,
4957
5146
  /** @export */
4958
5147
  frameos_wasm_js_open_link: _frameos_wasm_js_open_link,
Binary file