Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consider a static file API #1778

Closed
FrederikBolding opened this issue Sep 25, 2023 · 4 comments · Fixed by #1836
Closed

Consider a static file API #1778

FrederikBolding opened this issue Sep 25, 2023 · 4 comments · Fixed by #1836
Assignees
Labels
area-capabilities Related to the kinds of things that snaps are able to do. type-enhancement New feature or request type-research A research task.

Comments

@FrederikBolding
Copy link
Member

Seems that a general problem for larger snaps that require large WASM modules (or other large files) is that the cold start is slow. What if instead of using our state API for this (which is kind of hacky) we build an API that let's snaps lazy load it's own static files at runtime?

E.g.

export const onRpcRequest: OnRpcRequestHandler = async ({ origin, request }) => {
  const program = await snap.request({ method: 'snap_loadFile', params: { fileName: 'program.wasm' }})
  const module = await WebAssembly.compile(program);
  const wasm = await instantiate(module, {});
  (...)
};

A snap could then specify the static files it needs at runtime in the manifest.

@FrederikBolding FrederikBolding added type-enhancement New feature or request type-research A research task. area-capabilities Related to the kinds of things that snaps are able to do. labels Sep 25, 2023
@martines3000
Copy link

Hi. I have 2 questions:

  1. Where would the snap load the files from. Would we just package the needed files together with the snap.js file and publish it on NPM?
  2. Would this be extensible, as to be able to load other large files like JSON (file.json, etc...), or would this be limited to .wasm files?

Overall, I find the idea great 👍 , and it would definitely help projects with large files.

@FrederikBolding
Copy link
Member Author

  1. Where would the snap load the files from. Would we just package the needed files together with the snap.js file and publish it on NPM?
  2. Would this be extensible, as to be able to load other large files like JSON (file.json, etc...), or would this be limited to .wasm files?
  1. Yeah, I imagine they would just go in the dist folder next to the snap bundle. You would have to reference them in the manifest, e.g. files: ["./dist/foo.json"] and that should make them loadable asynchronously at snap runtime.
  2. I don't see why this couldn't work for any file type, loadFile() could just return an Uint8Array and the snap parse that as it wants to.

@martines3000
Copy link

Thanks for clarifying, the concept makes sense and I like it. 🚀

FrederikBolding added a commit that referenced this issue Oct 16, 2023
Adds an API for snaps to access auxiliary files at runtime. Adds support
for this new API in the simulator and adds an example snap that uses the
API.

The API lets devs specify a files field in the manifest under
`source.files`:
```json
{
"source": {
    "shasum": ...,
    "location": ...,
    "files": [
      "./src/foo.json"
    ]
  },
}
```

At install-time any files in this array are attempted loaded and stored
in snaps state alongside the source code, icon etc. These files are then
retrievable at run-time by calling `snap.request({ method:
'snap_getFile', params: { path: './src/foo.json' }})`. The files
themselves are currently stored in base64 and returned to the snap in
either base64 or hexadecimal.

Fixes #1778
@FrederikBolding
Copy link
Member Author

@martines3000 Coming soon to Flask 👀

benefacto pushed a commit to zenchain-protocol/zazen that referenced this issue Jul 3, 2024
Adds an API for snaps to access auxiliary files at runtime. Adds support
for this new API in the simulator and adds an example snap that uses the
API.

The API lets devs specify a files field in the manifest under
`source.files`:
```json
{
"source": {
    "shasum": ...,
    "location": ...,
    "files": [
      "./src/foo.json"
    ]
  },
}
```

At install-time any files in this array are attempted loaded and stored
in snaps state alongside the source code, icon etc. These files are then
retrievable at run-time by calling `snap.request({ method:
'snap_getFile', params: { path: './src/foo.json' }})`. The files
themselves are currently stored in base64 and returned to the snap in
either base64 or hexadecimal.

Fixes MetaMask/snaps#1778
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-capabilities Related to the kinds of things that snaps are able to do. type-enhancement New feature or request type-research A research task.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants