Spin Framework

repository·main·Indexed 27 days ago

https://github.com/spinframework/spin

An open source framework for building, deploying, and running fast, secure, and composable cloud microservices using WebAssembly and the WebAssembly component model. It includes the spin-cli for application scaffolding, building, and execution, as well as the spin-componentize library for converting Wasm modules into components. Spin provides official SDKs for Rust, JavaScript, Go, and Python, supporting triggers like HTTP and Redis, and APIs for Key Value storage, SQLite, MySQL, PostgreSQL, and Serverless AI.

Tokens
36.8K
Snippets
79
Records
253
Agent score
92%

What's inside Spin

  1. Extend Spin functionality with plugins

    main
    Spin plugins allow users to add new subcommands and functionality to the Spin CLI. While current plugins are primarily executables that provide new CLI subcommands via spin plugin install, the design vision includes supporting features executed by Spin itself, such as custom triggers (e.g., a timer trigger that executes components at specific intervals).
  2. Understand Spin componentize adapters

    main

    The componentize process utilizes specific adapters to transform plain WebAssembly (Wasm) modules into Wasm components that are compatible with WASI Preview 2. Depending on the version of wit-bindgen being used, different adapters are required:

    1. Upstream WASI Preview 1 Adapters: Used for both commands and reactors when working with wit-bindgen version 0.5 and above. These are based on the Wasmtime 18.0.1 release.
    2. Modified Spin Adapter: A custom adapter used for compatibility with wit-bindgen version 0.2. This adapter includes specific knowledge of Spin APIs to handle the different ABI used by older wit-bindgen modules.
  3. Use the Test Codegen Macro for automatic test generation

    main
    The test-codegen-macro is a macro designed to automatically produce #[test] annotated functions based on the existing file directory structure. This is primarily used by runtime tests to eliminate the need for developers to manually write a corresponding test function whenever a new runtime test is added.
  4. Understand Spin Factors architecture

    main

    Spin Factors is a redesigned inversion of control system used to organize independent feature sets (formerly loosely-coupled 'host components') into modular 'factors'. This architecture allows for better integration across different Spin embeddings, such as the Spin CLI and SpinKube, by providing standardized lifecycle hooks and managing an acyclic dependency graph between features.

    Key lifecycle integration points include:

    • Runtime startup
    • Application initialization
    • Runtime config parsing
    • Component pre-instantiation
    • Component instantiation
    • Cleanup (post-execution)
  5. Sign artifacts using Cosign keyless mode

    main

    When building release infrastructure (e.g., in GitHub Actions), you can sign blobs using cosign sign-blob. This method uses OIDC identities to generate short-lived certificates, eliminating the need for manual private key management.

    To use this in a CI environment like GitHub Actions, ensure the job has id-token: write permissions. Use the --output-certificate flag to save the certificate and --output-signature to save the signature, which should then be released alongside the artifact.

    $ cosign sign-blob \
        --output-certificate out/spin-v1.0.0-linux-amd64-keyless.pem \
        --output-signature out/spin-v1.0.0-linux-amd64-keyless.sig \
        --yes \
        spin
  6. Manage state for Inbound WebSockets

    main

    Because Spin may process WebSocket frames using different instances or nodes in a cluster, you cannot rely on in-memory state (like mutexes) to manage connection or shared state. You must use a persistent store outside of the instance's memory.

    Consistency Considerations:

    • If using distributed, eventually consistent stores, users may not see each other's state changes immediately (e.g., in a chat app).
    • To ensure reliability, you can use a store with global, strict serializability, or use an eventually consistent store that supports asynchronous notifications.

    Spin Built-in Stores: As of the current implementation, Spin's built-in SQLite and key-value stores do not provide explicit consistency guarantees. For applications requiring strict consistency, you should provide your own external store such as PostgreSQL, MySQL, or Redis.

  7. Push a Spin application to an OCI registry

    main

    Distribute your Spin application to a compatible OCI registry using spin oci push. You can specify a custom reference and tag, or derive them from your spin.toml file.

    Custom Reference/Tag: Provide the full registry path and tag as an argument.

    Derived Reference/Tag: If you omit the argument, Spin derives the reference and tag from the name and version fields in spin.toml. For this to work, the name in spin.toml must contain the fully qualified reference (e.g., myregistry.com/myusername/myapp).

  8. Audit third-party dependencies using `cargo vet`

    main

    The Spin project uses cargo vet to manage and audit the security of its Rust dependencies. This process allows maintainers to verify that dependencies are safe to use by either performing manual audits or importing trusted audits from organizations like the Bytecode Alliance (for Wasmtime) or Mozilla.

    Audit Workflow

    1. Check Vetting Status: Run cargo vet to see if any dependencies are unvetted or require new audits due to updates.
    2. Inspect Dependencies: Use cargo vet inspect <crate> <version> to be guided through a manual audit of a specific crate version.
    3. Review Changes: Use cargo vet diff <crate> <old-version> <new-version> to review the differences between two versions before certifying.
    4. Certify Audits: Once a review is complete, use cargo vet certify <crate> <version> to record the audit.
    5. Shrink Exemptions: Use cargo vet suggest to identify dependencies that can be moved from the 'exemptions' list to a formal audit status.
  9. Use the spin cloud plugin for deployment

    main

    The spin login and spin deploy commands are implemented as part of the spin cloud plugin. To maintain backwards compatibility, the core Spin CLI aliases the standard spin login and spin deploy commands to their spin cloud counterparts. If the plugin is not installed, Spin will attempt to install it automatically upon the first invocation of these commands.

    $ spin deploy
    The `cloud` plugin is required. Installing now.
    Plugin 'cloud' was installed successfully!
    Uploading cloud_start version 0.1.0+XXXXXXXX...
    Deploying...
    Waiting for application to become ready... ready
    Available Routes:
      cloud-start: https://cloud-start-xxxxxxxx.fermyon.app/ (wildcard)