{"id":13498003,"url":"https://github.com/swift-cloud/Compute","last_synced_at":"2025-03-28T22:32:05.019Z","repository":{"id":39621688,"uuid":"447352663","full_name":"swift-cloud/Compute","owner":"swift-cloud","description":"Swift runtime for Fastly Compute@Edge","archived":false,"fork":false,"pushed_at":"2024-05-30T14:12:30.000Z","size":4194,"stargazers_count":82,"open_issues_count":0,"forks_count":3,"subscribers_count":11,"default_branch":"main","last_synced_at":"2024-08-06T00:17:26.594Z","etag":null,"topics":["fastly","swift","wasm"],"latest_commit_sha":null,"homepage":"https://swiftpackageindex.com/swift-cloud/Compute/documentation","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mpl-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/swift-cloud.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-01-12T19:56:13.000Z","updated_at":"2024-08-06T00:17:31.734Z","dependencies_parsed_at":"2022-07-13T09:10:35.179Z","dependency_job_id":"89cf6030-b3cc-4dfd-8272-d38dd6904bd1","html_url":"https://github.com/swift-cloud/Compute","commit_stats":{"total_commits":300,"total_committers":2,"mean_commits":150.0,"dds":"0.0033333333333332993","last_synced_commit":"9b5bc752fc532b47a6f273405d5b480d83ac8b2c"},"previous_names":["andrewbarba/swift-compute-runtime"],"tags_count":37,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/swift-cloud%2FCompute","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/swift-cloud%2FCompute/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/swift-cloud%2FCompute/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/swift-cloud%2FCompute/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/swift-cloud","download_url":"https://codeload.github.com/swift-cloud/Compute/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246110447,"owners_count":20725056,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["fastly","swift","wasm"],"created_at":"2024-07-31T20:00:48.175Z","updated_at":"2025-03-28T22:32:04.634Z","avatar_url":"https://github.com/swift-cloud.png","language":"Swift","funding_links":[],"categories":["Libraries and frameworks"],"sub_categories":[],"readme":"# Compute\n\nSwift runtime for Fastly Compute@Edge\n\n[![](https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2Fswift-cloud%2FCompute%2Fbadge%3Ftype%3Dswift-versions)](https://swiftpackageindex.com/swift-cloud/Compute) [![](https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2Fswift-cloud%2FCompute%2Fbadge%3Ftype%3Dplatforms)](https://swiftpackageindex.com/swift-cloud/Compute)\n\n## Getting Started\n\nCreate a new swift package using the `executable` template\n\n```sh\nswift package init --type executable\n```\n\nInstall the Compute runtime:\n\n```swift\n.package(url: \"https://github.com/swift-cloud/Compute\", from: \"3.0.0\")\n```\n\nAdd it as a target dependency:\n\n```swift\n.executableTarget(\n    name: \"MyApp\",\n    dependencies: [\"Compute\"]\n)\n```\n\n## Deploy\n\nThe easiest way to deploy a Swift app to Fastly is through [Swift Cloud](https://swift.cloud).\n\nSwift Cloud is a fully managed platform as a service for deploying SwiftWasm apps to Fastly. You can connect your Github account and deploy your apps in 1 click.\n\n## Documentation\n\nComplete documentation is very much a work in progress:\n\n[https://compute-runtime.swift.cloud/documentation/compute/](https://compute-runtime.swift.cloud/documentation/compute/)\n\n## Sample App\n\nHere's what a Swift app looks like on Compute@Edge\n\n```swift\nimport Compute\n\ntry await onIncomingRequest { req, res in\n    let fetchResponse = try await fetch(\"https://httpbin.org/json\", .options(\n        headers: [\"user-agent\": \"swift-compute-runtime\"]\n    ))\n    let text = try await fetchResponse.text()\n    try await res.status(200).send(text)\n}\n```\n\n## SwiftWasm\n\nSwift on Compute@Edge is made possible by the incredible [SwiftWasm](https://swiftwasm.org) project, but probably not in the way you might think. Web Assembly is making waves as being a new high performance environment for web application development but it's also powering cloud computing platforms like Fastly Compute@Edge. Deploying to Fastly involves compiling your code to Web Assembly and then deploying it to a Fastly service. This can be accomplished via GitHub actions but the absolute easiest way to deploy your code is through [Swift Cloud](https://swift.cloud). Swift Cloud builds your code on [AWS Fargate instances](https://github.com/swift-cloud/build) and then optimizes the WASM binary using Binaryen. Fastly services are managed behind the scenes and we make it really simple to integrate things like Edge Dictionaries, Backends and environment variables.\n\n## Routing\n\nThe Compute package incldues an Express style router based on [Vapor's routing-kit](https://github.com/vapor/routing-kit). Here are some basic ways to use the router:\n\n### GET Route\n\n```swift\nimport Compute\n\nlet router = Router()\n\nrouter.get(\"/status\") { req, res in\n    try await res.status(.ok).send(\"OK\")\n}\n\nrouter.get(\"/user/:name\") { req, res in\n    let name = req.pathParams[\"name\"] ?? \"\"\n    let text = \"Hello, \\(name)!\"\n    try await res.status(.ok).send(text)\n}\n\ntry await router.listen()\n```\n\n### POST Route\n\n```swift\nimport Compute\n\nstruct User: Codable {\n    let name: String\n}\n\nlet router = Router()\n\nrouter.post(\"/user\") { req, res in\n    let user = try await req.body.decode(User.self)\n    try await res.status(.created).send(user)\n}\n\ntry await router.listen()\n```\n\n## Fetching Data\n\nOne of the main drawbacks of WebAssembly today is no standardized way to handle sockets. Because of this SwiftWasm does not have access to some Foundation classes like URLSession. This requires each runtime to provide their own networking layer until a standardized threading and socket model is introduced into the WASM spec.\n\nThe Compute package provides the `fetch(_ url: String)` function for sending asynchronous HTTP requests. The implementation is largely modeled on the HTML5 `fetch()` spec including full support for streaming request and response bodies, async/await, and Codable support.\n\n**Important:** In order to use `fetch` you must pre-define the hostnames that you will be calling out to. For example, if you are integrating with the Stripe API you must pre-define `api.stripe.com` in your Fastly service. Swift Cloud also makes this really easy to define all of your external origins when creating your project. We sincerely hope this is a short term limitation of the Fastly platform and believe they are working on a mechanism to call arbitrary backends without the need to pre-define them.\n\n### GET Request\n\n```swift\nlet data = try await fetch(\"https://httpbin.org/json\").json()\n```\n\n### POST Request\n\n```swift\nlet res = try await fetch(\"https://httpbin.org/json\", .options(\n    method: .post,\n    body: .json([\"name\": \"Andrew\"])\n))\n```\n\n### Proxy Request\n\nA really powerful feature of the Compute package is the ability to proxy to another origin. Fasty's edge platform is a globally distributed CDN that provides unprecendented performance compared to a single region origin. Using Compute you can put a full featured CDN in front of your origin to do things like force HTTPS, enable HTTP/3, provide robust origin failover, and a lot more.\n\n```swift\n/// req: IncomingRequest, res: OutgoingResponse\nlet data = try await fetch(req, origin: \"https://httpbin.org\", .options(\n    cachePolicy: .ttl(10, staleWhileRevalidate: 30)\n))\ntry await res.proxy(data)\n```\n\n## Cache\n\nThe Compute package provdes access to [Fastly's Cache API](https://developer.fastly.com/learning/concepts/edge-state/cache#simple-cache) allowing you to cache and retrieve arbitrary data during a request:\n\n\n```swift\nlet data = try await Cache.getOrSet(\"my-page\") {\n    let res = try await expensivePageRender()\n    return (res, .ttl(60))\n}\n\ntry await res\n    .status(200)\n    .header(.contentLength, \"\\(data.contentLength)\")\n    .send(data.body)\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fswift-cloud%2FCompute","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fswift-cloud%2FCompute","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fswift-cloud%2FCompute/lists"}