This tutorial demonstrates CRUD operations on a GDN collection using a jsc8 driver with Fastly Compute@Edge.
- Compute@Edge does not support WebSocket connection. You cannot use JSC8 producer/subscriber features for streams or collection streams.
- Requests forwarded to a backend are sent through the Fastly cache, and the response may come from the cache. If a request doesn't find a matching result in the cache, it is sent to the backend (Macrometa GDN).
- Compute@Edge only provides one parameter to handle caching:
CacheOverride
- If you want to clear the Compute@Edge cache, refer to Fastly's Purge Cache documentation.
- Compute@Edge only provides one parameter to handle caching:
-
Create a Fastly API token by following the Creating API tokens procedure. The generated token requires
Global API access
scope to configure the Fastly CLI. -
Install and configure Fastly CLI. Refer to the Fastly CLI installation procedure.
-
Create the Compute@Edge service:
$ fastly service create --name="Fastly Compute-JSC8 tutorial" --type=wasm SUCCESS: Created service AAAAAAAAAAA
Note the service ID generated by this command.
-
Update
fastly.toml
with the generated service ID. -
Create the Compute@Edge backend and connect it to GDN:
$ fastly backend create --version=latest --name="gdn_url" --address="api-gdn.paas.macrometa.io" --port=443 SUCCESS: Created backend gdn_url (service AAAAAAAAAAA version 1)
Use the following commands to install and run Compute@Edge. When prompted for a domain, you can select a Fastly generated domain or type your own. This domain is used to access the Compute@Edge service once deployment is complete.
git clone git@github.com:Macrometacorp/tutorial-fastly-compute-jsc8.git
cd tutorial-fastly-compute-jsc8
npm install
npm run dev
npm run deploy
-
Run the following commands to install the required packages:
npm install path-browserify npm install url
-
Add the following code to webpack.config.js.
This fixes the known issue: Webpack 5 Node.js Polyfills Removed
resolve: { extensions: [".web.js", ".web.ts", ".js", ".ts", ".json"], fallback: { path: require.resolve("path-browserify"), url: require.resolve("url"), }, },
-
Add the following code to
fastly.toml
.This enables Compute@Edge to forward API requests from the JSC8 driver to Macrometa GDN. Only required when running development server
[local_server] [local_server.backends] [local_server.backends.gdn_url] url = "https://api-gdn.paas.macrometa.io"
-
Create a jsC8 client:
const jsC8 = require("jsc8") jsc8Client = new jsC8({ url: "https://gdn.paas.macrometa.io", fabricName: "xxxx", apiKey: "xxxx", agent: fetch, agentOptions: { backend: "gdn_url", cacheOverride: new CacheOverride("override", { ttl: 0 }), }, })
Note: Replace the value for
backend
with the--name
value you specified when you created the Compute@Edge backendFor authentication, this tutorial uses the jsC8 client's login method. You can use the
apiKey
field to specify an API key generated in the Macrometa GDN GUI. Refer to the API Keys section in the GDN reference documentation. -
Test jsC8 to verify that you can read, write, and edit data in Macrometa GDN.
const collectioName= "fastly_compute_tutorial"
await jsc8Client.hasCollection(collectionName) //Check if given collection exists or not
await jsc8Client.insertDocument(collectionName, content) //Insert data into collection