├── .github ├── CODEOWNERS └── workflows │ └── test.yml ├── .gitignore ├── CHANGELOG.md ├── LICENSE ├── README.md ├── SECURITY.md ├── asconfig.json ├── assembly ├── index.ts └── tsconfig.json ├── fastly.toml ├── package-lock.json └── package.json /.github/CODEOWNERS: -------------------------------------------------------------------------------- 1 | /README.md @fastly/developer-relations 2 | /fastly.toml @fastly/developer-relations 3 | assembly/index.ts @fastly/ecp-sdk-sme-assemblyscript 4 | -------------------------------------------------------------------------------- /.github/workflows/test.yml: -------------------------------------------------------------------------------- 1 | on: pull_request 2 | name: Test 3 | jobs: 4 | test: 5 | strategy: 6 | matrix: 7 | platform: [ubuntu-latest] 8 | runs-on: ${{ matrix.platform }} 9 | steps: 10 | - name: Checkout code 11 | uses: actions/checkout@v2 12 | - uses: actions/setup-node@v1 13 | with: 14 | node-version: '12' 15 | - uses: actions/cache@v2 16 | with: 17 | path: ~/.npm 18 | key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }} 19 | restore-keys: | 20 | ${{ runner.os }}-node- 21 | - run: npm ci 22 | - name: build 23 | run: npm run asbuild 24 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /node_modules 2 | /bin 3 | /pkg 4 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | ## [v0.2.0](https://github.com/fastly/compute-starter-kit-assemblyscript-default/releases/tag/v0.1.0) (2020-10-27) 4 | 5 | [Full Changelog](https://github.com/fastly/compute-starter-kit-assemblyscript-default/compare/v0.1.0...v0.2.0) 6 | 7 | **Enhancements:** 8 | 9 | - Follow default AssemblyScript conventions for project structure. [\#2](https://github.com/fastly/compute-starter-kit-assemblyscript-default/pull/2) 10 | 11 | ## [v0.1.0](https://github.com/fastly/compute-starter-kit-assemblyscript-default/releases/tag/v0.1.0) (2020-10-23) 12 | 13 | [Full Changelog](https://github.com/fastly/compute-starter-kit-assemblyscript-default/compare/cebb010c421cc305af78f5cd9f67ab0af15e0f50...v0.1.0) 14 | 15 | Initial release :tada: 16 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Fastly 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Default Starter Kit for AssemblyScript 2 | 3 | [![Deploy to Fastly](https://deploy.edgecompute.app/button)](https://deploy.edgecompute.app/deploy) 4 | 5 | Get to know the Fastly Compute@Edge environment with a basic starter that demonstrates routing, simple synthetic responses and code comments that cover common patterns. 6 | 7 | **For more details about other starter kits for Compute@Edge, see the [Fastly developer hub](https://developer.fastly.com/solutions/starters)** 8 | 9 | ## Features 10 | 11 | * Allow only requests with particular HTTP methods 12 | * Match request URL path and methods for routing 13 | * Build synthetic responses at the edge 14 | 15 | ## Understanding the code 16 | 17 | This starter is intentionally lightweight, and requires no dependencies aside from the [`@fastly/as-compute`](https://www.npmjs.com/package/@fastly/as-compute) npm package. It will help you understand the basics of processing requests at the edge using Fastly. This starter includes implementations of common patterns explained in our [using Compute@Edge](https://developer.fastly.com/learning/compute/assemblyscript/) and [VCL migration](https://developer.fastly.com/learning/compute/migrate/) guides. 18 | 19 | The starter doesn't require the use of any backends. Once deployed, you will have a Fastly service running on Compute@Edge that can generate synthetic responses at the edge. 20 | 21 | ## Security issues 22 | 23 | Please see our [SECURITY.md](SECURITY.md) for guidance on reporting security-related issues. 24 | -------------------------------------------------------------------------------- /SECURITY.md: -------------------------------------------------------------------------------- 1 | ## Report a security issue 2 | 3 | The compute-starter-kit-assemblyscript-default project team welcomes security reports and is committed to providing prompt attention to security issues. Security issues should be reported privately via [Fastly’s security issue reporting process](https://www.fastly.com/security/report-security-issue). 4 | 5 | ## Security advisories 6 | 7 | Remediation of security vulnerabilities is prioritized by the project team. The project team endeavors to coordinate remediation with third-party stakeholders, and is committed to transparency in the disclosure process. The team announces security issues via the [Fastly Developer Hub Starter Kits](https://developer.fastly.com/solutions/starters/) site on a best-effort basis. 8 | 9 | Note that communications related to security issues in Fastly-maintained OSS as described here are distinct from [Fastly Security Advisories](https://www.fastly.com/security-advisories). 10 | -------------------------------------------------------------------------------- /asconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "targets": { 3 | "debug": { 4 | "binaryFile": "bin/untouched.wasm", 5 | "textFile": "bin/untouched.wat", 6 | "sourceMap": true, 7 | "debug": true 8 | }, 9 | "release": { 10 | "binaryFile": "bin/main.wasm", 11 | "textFile": "bin/main.wat", 12 | "sourceMap": true, 13 | "optimize": true 14 | } 15 | }, 16 | "options": {} 17 | } 18 | -------------------------------------------------------------------------------- /assembly/index.ts: -------------------------------------------------------------------------------- 1 | 2 | //! Default Compute@Edge template program. 3 | import { Request, Response, Headers, URL, Fastly } from "@fastly/as-compute"; 4 | 5 | // The entry point for your application. 6 | // 7 | // Use this function to define your main request handling logic. It could be 8 | // used to route based on the request properties (such as method or path), send 9 | // the request to a backend, make completely new requests, and/or generate 10 | // synthetic responses. 11 | 12 | function main(req: Request): Response { 13 | // Filter requests that have unexpected methods. 14 | if (["POST", "PUT", "PATCH", "DELETE"].includes(req.method)) { 15 | return new Response(String.UTF8.encode("This method is not allowed"), { 16 | status: 405, 17 | headers: null, 18 | url: null 19 | }); 20 | } 21 | 22 | let url = new URL(req.url); 23 | 24 | // If request is to the `/` path... 25 | if (url.pathname == "/") { 26 | // Below are some common patterns for Compute@Edge services using AssemblyScript. 27 | // Head to https://developer.fastly.com/learning/compute/assemblyscript/ to discover more. 28 | 29 | // Create a new request. 30 | // let bereq = new Request("http://example.com", { 31 | // method: "GET", 32 | // headers: null, 33 | // body: null 34 | // }); 35 | 36 | // Add request headers. 37 | // req.headers.set("X-Custom-Header", "Welcome to Compute@Edge!"); 38 | // req.headers.set( 39 | // "X-Another-Custom-Header", 40 | // "Recommended reading: https://developer.fastly.com/learning/compute" 41 | // ); 42 | 43 | // Create a cache override. 44 | // let cacheOverride = new Fastly.CacheOverride(); 45 | // cacheOverride.setTTL(60); 46 | 47 | // Forward the request to a backend. 48 | // let beresp = Fastly.fetch(req, { 49 | // backend: "backend_name", 50 | // cacheOverride, 51 | // }).wait(); 52 | 53 | // Remove response headers. 54 | // beresp.headers.delete("X-Another-Custom-Header"); 55 | 56 | // Log to a Fastly endpoint. 57 | // const logger = Fastly.getLogEndpoint("my_endpoint"); 58 | // logger.log("Hello from the edge!"); 59 | 60 | // Send a default synthetic response. 61 | let headers = new Headers(); 62 | headers.set('Content-Type', 'text/html; charset=utf-8'); 63 | 64 | return new Response(String.UTF8.encode("\n"), { 65 | status: 200, 66 | headers, 67 | url: null 68 | }); 69 | } 70 | 71 | // Catch all other requests and return a 404. 72 | return new Response(String.UTF8.encode("The page you requested could not be found"), { 73 | status: 404, 74 | headers: null, 75 | url: null 76 | }); 77 | } 78 | 79 | // Get the request from the client. 80 | let req = Fastly.getClientRequest(); 81 | 82 | // Pass the request to the main request handler function. 83 | let resp = main(req); 84 | 85 | // Send the response back to the client. 86 | Fastly.respondWith(resp); 87 | -------------------------------------------------------------------------------- /assembly/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "assemblyscript/std/assembly.json", 3 | "include": [ 4 | "./**/*.ts" 5 | ] 6 | } 7 | -------------------------------------------------------------------------------- /fastly.toml: -------------------------------------------------------------------------------- 1 | manifest_version = 3 2 | name = "Default starter for AssemblyScript" 3 | description = "A basic starter kit that demonstrates routing, simple synthetic responses and overriding caching rules." 4 | authors = [""] 5 | language = "assemblyscript" 6 | -------------------------------------------------------------------------------- /package-lock.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "compute-starter-kit-assemblyscript-default", 3 | "version": "1.0.0", 4 | "lockfileVersion": 2, 5 | "requires": true, 6 | "packages": { 7 | "": { 8 | "version": "1.0.0", 9 | "license": "MIT", 10 | "dependencies": { 11 | "@fastly/as-compute": "^0.5.0" 12 | }, 13 | "devDependencies": { 14 | "assemblyscript": "^0.19.18" 15 | }, 16 | "engines": { 17 | "node": "^16" 18 | } 19 | }, 20 | "node_modules/@fastly/as-compute": { 21 | "version": "0.5.0", 22 | "resolved": "https://registry.npmjs.org/@fastly/as-compute/-/as-compute-0.5.0.tgz", 23 | "integrity": "sha512-HMxc+W/j+n8QpsuIyse0m56Nv1Mz7EmMyfloztzgxXyrS8iHE6XTNlPr57qr41KMP2jWbS36L/+k1jJnhgofLA==", 24 | "dependencies": { 25 | "@fastly/as-fetch": "0.4.1", 26 | "@fastly/as-url": "0.1.2", 27 | "assemblyscript-json": "^1.0.0" 28 | }, 29 | "peerDependencies": { 30 | "assemblyscript": "0.19.x" 31 | } 32 | }, 33 | "node_modules/@fastly/as-fetch": { 34 | "version": "0.4.1", 35 | "resolved": "https://registry.npmjs.org/@fastly/as-fetch/-/as-fetch-0.4.1.tgz", 36 | "integrity": "sha512-9RBjm+BSG2ilt9gWsakqXt9PWIu9pVWxSPjMM+Eb7bRS5fhXdDIieSKW5n7u1JfAenylKfSfPU8RKlMQn869Rg==" 37 | }, 38 | "node_modules/@fastly/as-url": { 39 | "version": "0.1.2", 40 | "resolved": "https://registry.npmjs.org/@fastly/as-url/-/as-url-0.1.2.tgz", 41 | "integrity": "sha512-qAEm06yZka7Px3YRfZ0ZNXo+nicjC94gPf6cquqpuWPwBTxGpK3HRvX1ZNZijBtqoWxWRrTZZ1ZIkoKWOPuYRg==" 42 | }, 43 | "node_modules/assemblyscript": { 44 | "version": "0.19.23", 45 | "resolved": "https://registry.npmjs.org/assemblyscript/-/assemblyscript-0.19.23.tgz", 46 | "integrity": "sha512-fwOQNZVTMga5KRsfY80g7cpOl4PsFQczMwHzdtgoqLXaYhkhavufKb0sB0l3T1DUxpAufA0KNhlbpuuhZUwxMA==", 47 | "dependencies": { 48 | "binaryen": "102.0.0-nightly.20211028", 49 | "long": "^5.2.0", 50 | "source-map-support": "^0.5.20" 51 | }, 52 | "bin": { 53 | "asc": "bin/asc", 54 | "asinit": "bin/asinit" 55 | }, 56 | "funding": { 57 | "type": "opencollective", 58 | "url": "https://opencollective.com/assemblyscript" 59 | } 60 | }, 61 | "node_modules/assemblyscript-json": { 62 | "version": "1.1.0", 63 | "resolved": "https://registry.npmjs.org/assemblyscript-json/-/assemblyscript-json-1.1.0.tgz", 64 | "integrity": "sha512-UbE8ts8csTWQgd5TnSPN7MRV9NveuHv1bVnKmDLoo/tzjqxkmsZb3lu59Uk8H7SGoqdkDSEE049alx/nHnSdFw==" 65 | }, 66 | "node_modules/binaryen": { 67 | "version": "102.0.0-nightly.20211028", 68 | "resolved": "https://registry.npmjs.org/binaryen/-/binaryen-102.0.0-nightly.20211028.tgz", 69 | "integrity": "sha512-GCJBVB5exbxzzvyt8MGDv/MeUjs6gkXDvf4xOIItRBptYl0Tz5sm1o/uG95YK0L0VeG5ajDu3hRtkBP2kzqC5w==", 70 | "bin": { 71 | "wasm-opt": "bin/wasm-opt" 72 | } 73 | }, 74 | "node_modules/buffer-from": { 75 | "version": "1.1.2", 76 | "resolved": "https://registry.npmjs.org/buffer-from/-/buffer-from-1.1.2.tgz", 77 | "integrity": "sha512-E+XQCRwSbaaiChtv6k6Dwgc+bx+Bs6vuKJHHl5kox/BaKbhiXzqQOwK4cO22yElGp2OCmjwVhT3HmxgyPGnJfQ==" 78 | }, 79 | "node_modules/long": { 80 | "version": "5.2.0", 81 | "resolved": "https://registry.npmjs.org/long/-/long-5.2.0.tgz", 82 | "integrity": "sha512-9RTUNjK60eJbx3uz+TEGF7fUr29ZDxR5QzXcyDpeSfeH28S9ycINflOgOlppit5U+4kNTe83KQnMEerw7GmE8w==" 83 | }, 84 | "node_modules/source-map": { 85 | "version": "0.6.1", 86 | "resolved": "https://registry.npmjs.org/source-map/-/source-map-0.6.1.tgz", 87 | "integrity": "sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==", 88 | "engines": { 89 | "node": ">=0.10.0" 90 | } 91 | }, 92 | "node_modules/source-map-support": { 93 | "version": "0.5.21", 94 | "resolved": "https://registry.npmjs.org/source-map-support/-/source-map-support-0.5.21.tgz", 95 | "integrity": "sha512-uBHU3L3czsIyYXKX88fdrGovxdSCoTGDRZ6SYXtSRxLZUzHg5P/66Ht6uoUlHu9EZod+inXhKo3qQgwXUT/y1w==", 96 | "dependencies": { 97 | "buffer-from": "^1.0.0", 98 | "source-map": "^0.6.0" 99 | } 100 | } 101 | }, 102 | "dependencies": { 103 | "@fastly/as-compute": { 104 | "version": "0.5.0", 105 | "resolved": "https://registry.npmjs.org/@fastly/as-compute/-/as-compute-0.5.0.tgz", 106 | "integrity": "sha512-HMxc+W/j+n8QpsuIyse0m56Nv1Mz7EmMyfloztzgxXyrS8iHE6XTNlPr57qr41KMP2jWbS36L/+k1jJnhgofLA==", 107 | "requires": { 108 | "@fastly/as-fetch": "0.4.1", 109 | "@fastly/as-url": "0.1.2", 110 | "assemblyscript-json": "^1.0.0" 111 | } 112 | }, 113 | "@fastly/as-fetch": { 114 | "version": "0.4.1", 115 | "resolved": "https://registry.npmjs.org/@fastly/as-fetch/-/as-fetch-0.4.1.tgz", 116 | "integrity": "sha512-9RBjm+BSG2ilt9gWsakqXt9PWIu9pVWxSPjMM+Eb7bRS5fhXdDIieSKW5n7u1JfAenylKfSfPU8RKlMQn869Rg==" 117 | }, 118 | "@fastly/as-url": { 119 | "version": "0.1.2", 120 | "resolved": "https://registry.npmjs.org/@fastly/as-url/-/as-url-0.1.2.tgz", 121 | "integrity": "sha512-qAEm06yZka7Px3YRfZ0ZNXo+nicjC94gPf6cquqpuWPwBTxGpK3HRvX1ZNZijBtqoWxWRrTZZ1ZIkoKWOPuYRg==" 122 | }, 123 | "assemblyscript": { 124 | "version": "0.19.23", 125 | "resolved": "https://registry.npmjs.org/assemblyscript/-/assemblyscript-0.19.23.tgz", 126 | "integrity": "sha512-fwOQNZVTMga5KRsfY80g7cpOl4PsFQczMwHzdtgoqLXaYhkhavufKb0sB0l3T1DUxpAufA0KNhlbpuuhZUwxMA==", 127 | "requires": { 128 | "binaryen": "102.0.0-nightly.20211028", 129 | "long": "^5.2.0", 130 | "source-map-support": "^0.5.20" 131 | } 132 | }, 133 | "assemblyscript-json": { 134 | "version": "1.1.0", 135 | "resolved": "https://registry.npmjs.org/assemblyscript-json/-/assemblyscript-json-1.1.0.tgz", 136 | "integrity": "sha512-UbE8ts8csTWQgd5TnSPN7MRV9NveuHv1bVnKmDLoo/tzjqxkmsZb3lu59Uk8H7SGoqdkDSEE049alx/nHnSdFw==" 137 | }, 138 | "binaryen": { 139 | "version": "102.0.0-nightly.20211028", 140 | "resolved": "https://registry.npmjs.org/binaryen/-/binaryen-102.0.0-nightly.20211028.tgz", 141 | "integrity": "sha512-GCJBVB5exbxzzvyt8MGDv/MeUjs6gkXDvf4xOIItRBptYl0Tz5sm1o/uG95YK0L0VeG5ajDu3hRtkBP2kzqC5w==" 142 | }, 143 | "buffer-from": { 144 | "version": "1.1.2", 145 | "resolved": "https://registry.npmjs.org/buffer-from/-/buffer-from-1.1.2.tgz", 146 | "integrity": "sha512-E+XQCRwSbaaiChtv6k6Dwgc+bx+Bs6vuKJHHl5kox/BaKbhiXzqQOwK4cO22yElGp2OCmjwVhT3HmxgyPGnJfQ==" 147 | }, 148 | "long": { 149 | "version": "5.2.0", 150 | "resolved": "https://registry.npmjs.org/long/-/long-5.2.0.tgz", 151 | "integrity": "sha512-9RTUNjK60eJbx3uz+TEGF7fUr29ZDxR5QzXcyDpeSfeH28S9ycINflOgOlppit5U+4kNTe83KQnMEerw7GmE8w==" 152 | }, 153 | "source-map": { 154 | "version": "0.6.1", 155 | "resolved": "https://registry.npmjs.org/source-map/-/source-map-0.6.1.tgz", 156 | "integrity": "sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==" 157 | }, 158 | "source-map-support": { 159 | "version": "0.5.21", 160 | "resolved": "https://registry.npmjs.org/source-map-support/-/source-map-support-0.5.21.tgz", 161 | "integrity": "sha512-uBHU3L3czsIyYXKX88fdrGovxdSCoTGDRZ6SYXtSRxLZUzHg5P/66Ht6uoUlHu9EZod+inXhKo3qQgwXUT/y1w==", 162 | "requires": { 163 | "buffer-from": "^1.0.0", 164 | "source-map": "^0.6.0" 165 | } 166 | } 167 | } 168 | } 169 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "compute-starter-kit-assemblyscript-default", 3 | "version": "1.0.0", 4 | "description": "Default package starter kit for AssemblyScript based Compute@Edge projects", 5 | "main": "src/index.ts", 6 | "repository": { 7 | "type": "git", 8 | "url": "git+https://github.com/fastly/compute-starter-kit-assemblyscript-default.git" 9 | }, 10 | "keywords": [], 11 | "author": "oss@fastly.com", 12 | "license": "MIT", 13 | "bugs": { 14 | "url": "https://github.com/fastly/compute-starter-kit-assemblyscript-default/issues" 15 | }, 16 | "homepage": "https://github.com/fastly/compute-starter-kit-assemblyscript-default#readme", 17 | "engines": { 18 | "node": "^16" 19 | }, 20 | "devDependencies": { 21 | "assemblyscript": "^0.19.18" 22 | }, 23 | "dependencies": { 24 | "@fastly/as-compute": "^0.5.0" 25 | }, 26 | "scripts": { 27 | "asbuild:untouched": "asc assembly/index.ts --target debug", 28 | "asbuild:optimized": "asc assembly/index.ts --target release", 29 | "asbuild": "npm run asbuild:untouched && npm run asbuild:optimized" 30 | } 31 | } 32 | --------------------------------------------------------------------------------