Macrometa JSC8 driver on Fastly Compute@Edge

This tutorial demonstrates CRUD operations on a GDN collection using a jsc8 driver with Fastly Compute@Edge.

Known limitations

  • 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).

Prerequisites

  1. 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.

  2. Install and configure Fastly CLI. Refer to the Fastly CLI installation procedure.

  3. 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.

  4. Update fastly.toml with the generated service ID.

  5. 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)
    

Run and Publish with Compute@Edge

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

Prerequisites for using JSC8 with Compute@Edge

  1. Run the following commands to install the required packages:

    npm install path-browserify
    npm install url
    
  2. 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"),
       },
    },
    
  3. 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"
    

Integrate jsC8 with Compute@Edge

  1. 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 backend

    For 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.

  2. Test jsC8 to verify that you can read, write, and edit data in Macrometa GDN.

Sample

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

References