├── .github ├── CODEOWNERS └── workflows │ └── test.yml ├── .gitignore ├── LICENSE ├── README.md ├── SECURITY.md ├── fastly.toml ├── package.json └── src ├── index.js └── welcome-to-compute.html /.github/CODEOWNERS: -------------------------------------------------------------------------------- 1 | /README.md @fastly/developer-relations 2 | /fastly.toml @fastly/developer-relations 3 | *.js @fastly/ecp-sdk-sme-javascript 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@v3 12 | - name: Setup Node 13 | uses: actions/setup-node@v3 14 | with: 15 | node-version: '18' 16 | - name: Setup Fastly CLI 17 | uses: fastly/compute-actions/setup@v5 18 | - name: Install Dependencies 19 | run: npm install 20 | - name: Build Compute Package 21 | uses: fastly/compute-actions/build@v5 22 | with: 23 | verbose: true 24 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /node_modules 2 | /bin 3 | /pkg 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2025 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 JavaScript 2 | 3 | [![Deploy to Fastly](https://deploy.edgecompute.app/button)](https://deploy.edgecompute.app/fastly/compute-starter-kit-javascript-default) 4 | 5 | Get to know the Fastly Compute 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, see the [Fastly Documentation Hub](https://www.fastly.com/documentation/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/js-compute`](https://www.npmjs.com/package/@fastly/js-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](https://www.fastly.com/documentation/guides/compute/javascript/) and [VCL migration](https://www.fastly.com/documentation/guides/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 that can generate synthetic responses at the edge. 20 | 21 | ## Running the application 22 | 23 | To create an application using this starter kit, create a new directory for your application and switch to it, and then type the following command: 24 | 25 | ```shell 26 | npm create @fastly/compute@latest -- --language=javascript --default-starter-kit 27 | ``` 28 | 29 | To build and run your new application in the local development environment, type the following command: 30 | 31 | ```shell 32 | npm run start 33 | ``` 34 | 35 | To build and deploy your application to your Fastly account, type the following command. The first time you deploy the application, you will be prompted to create a new service in your account. 36 | 37 | ```shell 38 | npm run deploy 39 | ``` 40 | 41 | ## New to Fastly Compute? 42 | 43 | The [Fastly Compute platform](https://www.fastly.com/documentation/guides/compute/) is an advanced edge computing system that runs your code, in your favorite language, on its global edge network. Security and portability are provided by compiling your code to WebAssembly. 44 | 45 | Get started with your [free Fastly developer account](https://www.fastly.com/signup/?tier=free), and join the [Fastly community forum](https://community.fastly.com/) to ask any questions and show off the sites that you build! 46 | 47 | ## Security issues 48 | 49 | Please see our [SECURITY.md](SECURITY.md) for guidance on reporting security-related issues. 50 | -------------------------------------------------------------------------------- /SECURITY.md: -------------------------------------------------------------------------------- 1 | ## Report a security issue 2 | 3 | The 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 | -------------------------------------------------------------------------------- /fastly.toml: -------------------------------------------------------------------------------- 1 | # This file describes a Fastly Compute package. To learn more visit: 2 | # https://developer.fastly.com/reference/fastly-toml/ 3 | 4 | authors = [""] 5 | description = "A basic starter kit that demonstrates routing, simple synthetic responses and overriding caching rules." 6 | language = "javascript" 7 | manifest_version = 3 8 | name = "Default starter for JavaScript" 9 | 10 | [scripts] 11 | build = "npm run build" 12 | post_init = "npm install" 13 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "type": "module", 3 | "private": true, 4 | "dependencies": { 5 | "@fastly/js-compute": "^3.33.2" 6 | }, 7 | "devDependencies": { 8 | "@fastly/cli": "^11.0.0" 9 | }, 10 | "scripts": { 11 | "build": "js-compute-runtime src/index.js bin/main.wasm", 12 | "start": "fastly compute serve", 13 | "deploy": "fastly compute publish" 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | //! Default Compute template program. 2 | 3 | /// 4 | // import { CacheOverride } from "fastly:cache-override"; 5 | // import { Logger } from "fastly:logger"; 6 | import { env } from "fastly:env"; 7 | import { includeBytes } from "fastly:experimental"; 8 | 9 | // Load a static file as a Uint8Array at compile time. 10 | // File path is relative to root of project, not to this file 11 | const welcomePage = includeBytes("./src/welcome-to-compute.html"); 12 | 13 | // The entry point for your application. 14 | // 15 | // Use this fetch event listener to define your main request handling logic. It could be 16 | // used to route based on the request properties (such as method or path), send 17 | // the request to a backend, make completely new requests, and/or generate 18 | // synthetic responses. 19 | 20 | addEventListener("fetch", (event) => event.respondWith(handleRequest(event))); 21 | 22 | async function handleRequest(event) { 23 | // Log service version 24 | console.log("FASTLY_SERVICE_VERSION:", env('FASTLY_SERVICE_VERSION') || 'local'); 25 | 26 | // Get the client request. 27 | const req = event.request; 28 | 29 | // Filter requests that have unexpected methods. 30 | if (["POST", "PUT", "PATCH", "DELETE"].includes(req.method)) { 31 | return new Response("This method is not allowed", { 32 | status: 405, 33 | }); 34 | } 35 | 36 | const url = new URL(req.url); 37 | 38 | // If request is to the `/` path... 39 | if (url.pathname === "/") { 40 | // Below are some common patterns for Compute services using JavaScript. 41 | // Head to https://developer.fastly.com/learning/compute/javascript/ to discover more. 42 | 43 | // Create a new request. 44 | // const bereq = new Request("http://example.com"); 45 | 46 | // Add request headers. 47 | // req.headers.set("X-Custom-Header", "Welcome to Compute!"); 48 | // req.headers.set( 49 | // "X-Another-Custom-Header", 50 | // "Recommended reading: https://developer.fastly.com/learning/compute" 51 | // ); 52 | 53 | // Create a cache override. 54 | // To use this, uncomment the import statement at the top of this file for CacheOverride. 55 | // const cacheOverride = new CacheOverride({ ttl: 60 }); 56 | 57 | // Forward the request to a backend. 58 | // const beresp = await fetch(req, { 59 | // backend: "backend_name", 60 | // cacheOverride, 61 | // }); 62 | 63 | // Remove response headers. 64 | // beresp.headers.delete("X-Another-Custom-Header"); 65 | 66 | // Log to a Fastly endpoint. 67 | // To use this, uncomment the import statement at the top of this file for Logger. 68 | // const logger = new Logger("my_endpoint"); 69 | // logger.log("Hello from the edge!"); 70 | 71 | // Send a default synthetic response. 72 | return new Response(welcomePage, { 73 | status: 200, 74 | headers: new Headers({ "Content-Type": "text/html; charset=utf-8" }), 75 | }); 76 | } 77 | 78 | // Catch all other requests and return a 404. 79 | return new Response("The page you requested could not be found", { 80 | status: 404, 81 | }); 82 | } 83 | -------------------------------------------------------------------------------- /src/welcome-to-compute.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 10 | Welcome to Compute 11 | 16 | 17 | 18 | 29 | 30 | 31 | --------------------------------------------------------------------------------