Giter VIP home page Giter VIP logo

edgeworkers-examples's Introduction

Akamai EdgeWorkers and EdgeKV

Akamai EdgeWorkers helps deliver superior web experiences by enabling developers to run JavaScript at the Edge. EdgeKV provides a global, low-latency key-value data store to complement EdgeWorkers. The code and tools in this repository provide a starting point to help you build applications that solve business problems with Akamai EdgeWorkers and EdgeKV.

Subfolder organization

  • /examples: EdgeWorker and EdgeKV code samples, organized by use-case
  • /edgekv: EdgeKV helper library, API documentation, and utilities
  • /public_examples: Customer-contributed code to enhance the EdgeWorker ecosystem

Resources

For more information on EdgeWorkers and EdgeKV, refer to the following resources:

If you have not already installed Postman, visit the Postman website and install the preferred version for your system. Click the "Run in Postman" button to import the EdgeCompute API Postman collection into Postman.

Run in Postman

Reporting Issues

These are working code samples that you may use, modify and extend at your discretion. If you experience any problems, please raise a Github issue or create a pull request with fixes, suggestions, or code contributions.

For information on using EdgeWorkers and EdgeKV, please review Akamai's product policy

edgeworkers-examples's People

Contributors

ahsankamal avatar ajjohnsonn avatar bmatthew avatar cdtickshah avatar cosjef avatar dabaka avatar dawchihliou avatar dctheobald avatar dependabot[bot] avatar fklasen avatar gabrioakamai avatar guillaume-fr avatar hikaneko avatar joshjohnson avatar kayoub-akamai avatar leewalter avatar lperra avatar martinflack avatar moritzsteiner avatar mryakan avatar philmcguinness avatar pjaiprakakamai avatar rcalendi avatar snyk-bot avatar sscian avatar sverekar avatar swathimr avatar tvereecke avatar vpanchak avatar zdavidb avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

edgeworkers-examples's Issues

Compiling rollup bundles without secrets

Currently need a way to produce a rollup bundle without secrets.

Example would be to compile a rollup bundle with different edgekv_tokens so that the same bundle can be deployed to different environments, but use different keys (without having to compile a new bundle each time).

This use case should also apply to anything that may rely on environment variables. The Edgeworker may use a different set of Environment Variables to point to different systems or APIs.

Due to the fact that EdgeKv has to be copied into the source project of the Edgeworker, compilation of the project will fail because it needs credentials for Akamai CLI to download the edgekv_tokens. If it were injected in a different manner, it would not need to be recompiled each time edgekv_tokens change.

Unable to install edgekv-importer due to permissions issue

Note: Similar issue has been reported here npm/cli#4158

Hello Folks, after installing npm locally, when i tried to install the edgekv-importer locally, i am seeing this error:

xyz@abc edgekv-importer % sudo npm install -g
Password:
npm ERR! code EPERM
npm ERR! syscall lchown
npm ERR! path /usr/local/bin/sentinelctl
npm ERR! errno -1
npm ERR! Error: EPERM: operation not permitted, lchown '/usr/local/bin/sentinelctl'
npm ERR! [Error: EPERM: operation not permitted, lchown '/usr/local/bin/sentinelctl'] {
npm ERR! errno: -1,
npm ERR! code: 'EPERM',
npm ERR! syscall: 'lchown',
npm ERR! path: '/usr/local/bin/sentinelctl'
npm ERR! }
npm ERR!
npm ERR! The operation was rejected by your operating system.
npm ERR! It is likely you do not have the permissions to access this file as the current user
npm ERR!
npm ERR! If you believe this might be a permissions issue, please double-check the
npm ERR! permissions of the file and its containing directories, or try running
npm ERR! the command again as root/Administrator.

npm ERR! A complete log of this run can be found in:
npm ERR! /Users/xyz/.npm/_logs/2022-04-04T13_11_12_440Z-debug-0.log

Anyone faced this error before? Even as a root, its the same issue:

xyz:edgekv-importer root# npm install -g
npm ERR! code EPERM
npm ERR! syscall lchown
npm ERR! path /usr/local/bin/sentinelctl
npm ERR! errno -1
npm ERR! Error: EPERM: operation not permitted, lchown '/usr/local/bin/sentinelctl'
npm ERR! [Error: EPERM: operation not permitted, lchown '/usr/local/bin/sentinelctl'] {
npm ERR! errno: -1,
npm ERR! code: 'EPERM',
npm ERR! syscall: 'lchown',
npm ERR! path: '/usr/local/bin/sentinelctl'
npm ERR! }
npm ERR!
npm ERR! The operation was rejected by your operating system.
npm ERR! It is likely you do not have the permissions to access this file as the current user
npm ERR!
npm ERR! If you believe this might be a permissions issue, please double-check the
npm ERR! permissions of the file and its containing directories, or try running
npm ERR! the command again as root/Administrator.

npm ERR! A complete log of this run can be found in:
npm ERR! /var/root/.npm/_logs/2022-04-04T13_13_23_529Z-debug-0.log

Thank you,
H

Improvement: show request URL & response status code in debug output

This change can show request URL and response status code in debug output.

function constructResponseBody(request, response) {
  let responseBody = "<html><body>";
  responseBody += "<h2>Request URL:</h2>"+request.method+" "+request.scheme+"//"+request.host+request.url+"<br>";
  responseBody += "<h2>Response status code:</h2>"+response.status+"<br>";
  responseBody += "<br>";
  responseBody +=  "<h2>User location:</h2>"+request.userLocation.city+", "+request.userLocation.country+"<br>";
  responseBody += "<br>";
  responseBody +=  "<h2>Request Headers:</h2><br>";

"edgeworker auth" no long accepting token

edgeworkers/examples/getting-started/hello-world/README.md step 21 seems to be out of date.

EdgeWorker CLI v1.1.0 only accepts <hostName> .

Usage: akamai-edgeworkers create-auth-token|auth [options] <hostName>

[Feature] Add support for React SSR apps

Looking for support to add Remix or NextJS support into EdgeWorkers. Some of the missing features that are needed are described here,

remix-run/remix#1539

TL;DR

  • Add body to the request object used in onClientRequest and onOriginRequest in order to support POST requests

OR

  • Add support for other HTTP methods for responseProvider

IMHO, the second option feels like the more appropriate spot to handle application requests

Failure when using external (npm) modules

Hi repo maintainers,

Thanks for the examples. I am having a hard time integrating external modules from npm. I have followed the pattern shown in the storelocator example (i.e. using rollup). After a bit of trial and error I can deploy the code to the EdgeWorker, however when triggering the EdgeWorker on our staging network I get a 503: Service Unavailable response. Unfortunately there isn't much information other than that.

What I have noticed is that when importing the modules and bundling them with rollup, the EdgeWorker will execute if the module is not called, so bundling the code is fine but It's having an issue calling the bundled code. I'm getting the same issue with both a commonJS module (https://github.com/auth0/node-jsonwebtoken) and with an ES6 module (https://github.com/uuidjs/uuid). I've tested them separately but I'm somewhat out of ideas - hopefully you can help 🙂

Clarity on Request setVariable method

Hello!

By studying the doc https://learn.akamai.com/en-us/webhelp/edgeworkers/edgeworkers-user-guide/GUID-C05789C6-9DFE-430F-8DD9-A50389AFF748.html is not clear to me how does setVariable works

My Use Case

In my PM I've created the variable PMUSER_MyVariable and in my code, I've done

//PMUSER_MyVariable: "MyValue" -> Default Value defined at PM
if(someConditionIsTrue) {
   request.setVariable('PMUSER_MyVariable',’MyNewValue’); // Expected new Value
}

Expected Result: After request "A" set PMUSER_MyVariable to MyNewValue, a new request B, when performing request.getVariable('PMUSER_MyVariable') would result MyNewValue. AND, when check the value in PM, PMUSER_MyVariable should be MyNewValue

Actual Result: Request B request.getVariable('PMUSER_MyVariable') results MyValue. AND when check the value in PM, PMUSER_MyVariable is MyValue. [In other words, setVariable didn't work as I would expect].

Question

  • Is request.setVariable a persistent operation among all requests? Or it is a request-lived operation?
  • If setVariable is a request-lived operation, rather than a persistent operation, is there an alternative to persist data in edge-workers?

Why do I need to persist data in EW: Ask for advice

A change in PM or EW takes at least 10 minutes to activate and this time is not acceptable for my scenario. I would expect using setVariable to check some conditions, and when these conditions are true, change the behavior of all subsequent requests.

Add example with jsdoc

It is possible to get full IDE support without setting up any transpilers.
Typescript supports .js files if they add the /// @ts-check comment and use JSDoc.

Maybe the following example would be a good reference for fast typesafe implementation without bundling/transpiling:

npm install --save-dev @types/akamai-edgeworkers
/// @ts-check
/// <reference types="akamai-edgeworkers" />

/**
 * @param {EW.ImmutableRequest & EW.HasRespondWith} request 
 * @param {EW.Response} response 
 */
export function onClientRequest(
  request,
  response
) {
  request.respondWith(
    200,
    {},
    "<html><body><h1>Hello World From Akamai EdgeWorkers</h1></body></html>"
  );
}

edge

Signature of "request"

I cannot find any documentation on the signature of the "request" parameter that is passed into the lifrecycle methods.

Can you supply the signature of the parameter?

What are the limitations currently on the edge-workers? I cannot find the documentation that lists what functionality isn't there such as subrequests/etc.

EdgeWorkers - external calls

Hi all,

trying to work with EdgeWorkers to solve a problem on hand:

• We have an application that is available in 2 datacenters
• 1 of the data centers is not super stable and we want an automatic "instant" failover
• Of course there is a standard behavior for that in DSA – BUT the challenge is that this app is firing various requests where some of them might be failing and some of them won’t. so if we only failover for some requests the app is going to chaos state
• Of course there is also GTM and application loadbalancer – but the problem there is that checking if the app is working fine is only possible post login/ADFS etc. so it is not a single http call – hence this will also not work
• What we finally ended up with is doing a robot that can check the app functions and is then triggering the failover – working fine, but has the problem that property activation takes 10 mins and failover should be more or less instant
• So what we did wondered is – can we use an Edgeworker to check for a status and do a redirect of requests to a maintenance page before the actual failover gets active

A simple idea was to have some sort of configuration "somewhere" (in our case we'd probably have a NetStorage file that can be queried and update) that is queried by the EdgeWorker and then a redirect to a maintenance page happens.

Below is some sample code that I tried - but EdgeWorkers throws an exception saying code is not accepted.

I think the code will not hit limitations as the request to Nestorage is very fast (25-30 ms) - but apparently Node syntax is not supported.

Can you advise on any options to implement such a scenario since EdgeKV is not an option yet?

Thanks,
Johannes

var options = {
  host: 'SOMEHOST',
  port: 80,
  path: 'SOMEPATH',
  method: 'GET'
};
 
var req = http.request(options, function(res) {
 
     // callback invoked when response is received
  res.setEncoding('utf8');
 
  res.on('data', function (chunk) {
 
                if(chunk == 'failover') {
                               // redirect to new host
                               request.respondWith(302, {
                                               'Location' : [ 'SOMEREDIRECT']
                               }, '');
                }
 
      });
});
 
req.on('error', function(e) {
  // not sure how to do error logging on edge workers – see your comment
});
 
req.end();

built-in modules are confusing

When I read import cookies from 'cookies' I thought it would use the cookies packages from npm..

This was quite confusing for me.

Maybe you could use a namespace instead e.g. @akamai/cookies to express that this packages is developed by you

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.