diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 00000000..8d449ea6 Binary files /dev/null and b/.DS_Store differ diff --git a/edgecompute/examples/traffic-routing/url-shortner/README.md b/edgecompute/examples/traffic-routing/url-shortner/README.md new file mode 100644 index 00000000..8ee920d1 --- /dev/null +++ b/edgecompute/examples/traffic-routing/url-shortner/README.md @@ -0,0 +1,28 @@ +Akamai EdgeWorkers - URL Shortener +================================== + +This is a demo of a URL shortner application that is fully contained within Akamai EdgeWorkers. The code used to serve the redirects and manage the redirects lives within an EdgeWorker application that you can review here. All of the Redirect mappings are stored in EdgeKV + +**Testing redirects:** + +URLs in the format with /r/\[name\] will attempt to perform a redirect by looking up the \[name\]. For example you can test the following URLs: + +* [https://edgeworkers.paulcalvano.com/r/paul](https://edgeworkers.paulcalvano.com/r/paul) +* [https://edgeworkers.paulcalvano.com/r/goog](https://edgeworkers.paulcalvano.com/r/goog) + +**Managing redirects:** + +URLs in the format with /m/op=\[add|update|delete\] are used to manage redirects. The parameter name is used for the shortened URL, and url is used for the destination URL. + +To create a redirect, you can use /m/op=add&name=\[name\]&url=\[url\]. If you exclude the name parameter, one will be created randomly for you. + +* /m/?op=add&url=https://test.com will create a redirect with a randomized name +* /m/?op=add&url=https://test.com&name=test will create a redirect with the path /r/test + +To update an existing redirect, you can use /m/op=update&name=\[name\]&url=\[url\]. + +* /m/?op=update&url=https://example.com&name=test will create update the redirect at /r/test to point to https://example.com + +To delete an existing redirect, you can use /m/op=delete&name=\[name\] + +* /m/?op=delete&name=test will delete the redirect at /r/test diff --git a/edgecompute/examples/traffic-routing/url-shortner/bundle.json b/edgecompute/examples/traffic-routing/url-shortner/bundle.json new file mode 100644 index 00000000..814863bd --- /dev/null +++ b/edgecompute/examples/traffic-routing/url-shortner/bundle.json @@ -0,0 +1,4 @@ +{ + "edgeworker-version": "0.51", + "description" : "URL Shortener Example" +} \ No newline at end of file diff --git a/edgecompute/examples/traffic-routing/url-shortner/edgekv.js b/edgecompute/examples/traffic-routing/url-shortner/edgekv.js new file mode 100644 index 00000000..f2833670 --- /dev/null +++ b/edgecompute/examples/traffic-routing/url-shortner/edgekv.js @@ -0,0 +1,328 @@ +/* +(c) Copyright 2020 Akamai Technologies, Inc. Licensed under Apache 2 license. +Version: 0.5.0 +Purpose: Provide a helper class to simplify the interaction with EdgeKV in an EdgeWorker. +Repo: https://github.com/akamai/edgeworkers-examples/tree/master/edgekv/lib +*/ + +import { TextDecoderStream } from 'text-encode-transform'; +import { WritableStream } from 'streams'; +import { httpRequest } from 'http-request'; +/** + * You must include edgekv_tokens.js in your bundle for this class to function properly. + * edgekv_tokens.js must include all namespaces you are going to use in the bundle. + */ +import { edgekv_access_tokens } from './edgekv_tokens.js'; + +export class EdgeKV { + #namespace; + #group; + #edgekv_uri; + #token_override; + #num_retries_on_timeout; + + /** + * Constructor to allow setting default namespace and group + * These defaults can be overriden when making individual GET, PUT, and DELETE operations + * @param {string} [$0.namepsace] the default namespace to use for all GET, PUT, and DELETE operations + * Namespace must be 32 characters or less, consisting of A-Z a-z 0-9 _ or - + * @param {string} [$0.group] the default group to use for all GET, PUT, and DELETE operations + * Group must be 128 characters or less, consisting of A-Z a-z 0-9 _ or - + * @param {number} [$0.num_retries_on_timeout=0] the number of times to retry a GET requests when the sub request times out + */ + constructor(namespace = "default", group = "default") { + if (typeof namespace === "object") { + this.#namespace = namespace.namespace || "default"; + this.#group = namespace.group || "default"; + this.#edgekv_uri = namespace.edgekv_uri || "https://edgekv.akamai-edge-svcs.net"; + this.#token_override = namespace.token_override || null; + this.#num_retries_on_timeout = namespace.num_retries_on_timeout || 0; + } else { + this.#namespace = namespace; + this.#group = group; + this.#edgekv_uri = "https://edgekv.akamai-edge-svcs.net"; + this.#token_override = null; + this.#num_retries_on_timeout = 0; + } + } + + throwError(failed_reason, status, body) { + throw { + failed: failed_reason, + status: status, + body: body, + toString: function () { return JSON.stringify(this); } + }; + } + + async requestHandlerTemplate(http_request, handler_200, handler_large_200, error_text, default_value, num_retries_on_timeout) { + try { + let response = await http_request(); + switch (response.status) { + case 200: + // need to handle content length > 128000 bytes differently in EdgeWorkers + let contentLength = response.getHeader('Content-Length'); + if (!contentLength || contentLength.length == 0 || contentLength[0] >= 128000) { + return handler_large_200(response); + } else { + return handler_200(response); + } + case 404: + return default_value; + default: + let content = ""; + try { + content = await response.text(); + content = JSON.parse(content); + } catch (error) { } + throw { status: response.status, body: content }; // to be caught in surrounding catch block + } + } catch (error) { + if (num_retries_on_timeout > 0 && /^.*subrequest to URL.*timed out.*$/.test(error.toString())) { + return this.requestHandlerTemplate(http_request, handler_200, handler_large_200, error_text, default_value, num_retries_on_timeout - 1); + } + if (error.hasOwnProperty('status')) { + this.throwError(error_text + " FAILED", error.status, error.body); + } + this.throwError(error_text + " FAILED", 0, error.toString()); + } + } + + validate({ namespace = null, group = null, item = null }) { + if (!namespace || !/^[A-Za-z0-9_-]{1,32}$/.test(namespace)) { + throw "Namespace is not valid. Must be 32 characters or less, consisting of A-Z a-z 0-9 _ or -"; + } + if (!group || !/^[A-Za-z0-9_-]{1,128}$/.test(group)) { + throw "Group is not valid. Must be 128 characters or less, consisting of A-Z a-z 0-9 _ or -"; + } + if (!item || !/^[A-Za-z0-9_-]{1,512}$/.test(item)) { + throw "Item is not valid. Must be 512 characters or less, consisting of A-Z a-z 0-9 _ or -"; + } + } + + getNamespaceToken(namespace) { + if (this.#token_override) { + return this.#token_override; + } + let name = "namespace-" + namespace; + if (!(name in edgekv_access_tokens)) { + throw "MISSING ACCESS TOKEN. No EdgeKV Access Token defined for namespace '" + namespace + "'."; + } + return edgekv_access_tokens[name]["value"]; + } + + addTimeout(options, timeout) { + if (timeout && (typeof timeout !== 'number' || !isFinite(timeout) || timeout <= 0 || timeout > 1000)) { + throw "Timeout is not valid. Must be a number greater than 0 and less than 1000."; + } + if (timeout) { + options.timeout = timeout; + } + return options; + } + + async streamText(response_body) { + let result = ""; + await response_body + .pipeThrough(new TextDecoderStream()) + .pipeTo(new WritableStream({ + write(chunk) { + result += chunk; + } + }), { preventAbort: true }); + return result; + } + + async streamJson(response_body) { + return JSON.parse(await this.streamText(response_body)); + } + + putRequest({ namespace = this.#namespace, group = this.#group, item, value, timeout = null } = {}) { + this.validate({ namespace: namespace, group: group, item: item }); + let uri = this.#edgekv_uri + "/api/v1/namespaces/" + namespace + "/groups/" + group + "/items/" + item; + return httpRequest(uri, this.addTimeout({ + method: "PUT", + body: typeof value === "object" ? JSON.stringify(value) : value, + headers: { "X-Akamai-EdgeDB-Auth": [this.getNamespaceToken(namespace)] } + }, timeout)); + } + + /** + * async PUT text into an item in the EdgeKV. + * @param {string} [$0.namepsace=this.#namespace] specify a namespace other than the default + * @param {string} [$0.group=this.#group] specify a group other than the default + * @param {string} $0.item item key to put into the EdgeKV + * @param {string} $0.value text value to put into the EdgeKV + * @param {number} [$0.timeout=null] the maximum time, between 1 and 1000 milliseconds, to wait for the response + * @returns {Promise} if the operation was successful, the response from the EdgeKV + * @throws {object} if the operation was not successful, + * an object describing the non-200 response from the EdgeKV: {failed, status, body} + */ + async putText({ namespace = this.#namespace, group = this.#group, item, value, timeout = null } = {}) { + return this.requestHandlerTemplate( + () => this.putRequest({ namespace: namespace, group: group, item: item, value: value, timeout: timeout }), + (response) => response.text(), + (response) => this.streamText(response.body), + "PUT", + null, + 0 + ); + } + + /** + * PUT text into an item in the EdgeKV while only waiting for the request to send and not for the response. + * @param {string} [$0.namepsace=this.#namespace] specify a namespace other than the default + * @param {string} [$0.group=this.#group] specify a group other than the default + * @param {string} $0.item item key to put into the EdgeKV + * @param {string} $0.value text value to put into the EdgeKV + * @throws {object} if the operation was not successful at sending the request, + * an object describing the error: {failed, status, body} + */ + putTextNoWait({ namespace = this.#namespace, group = this.#group, item, value } = {}) { + try { + this.putRequest({ namespace: namespace, group: group, item: item, value: value }); + } catch (error) { + this.throwError("PUT FAILED", 0, error.toString()); + } + } + + /** + * async PUT json into an item in the EdgeKV. + * @param {string} [$0.namepsace=this.#namespace] specify a namespace other than the default + * @param {string} [$0.group=this.#group] specify a group other than the default + * @param {string} $0.item item key to put into the EdgeKV + * @param {object} $0.value json value to put into the EdgeKV + * @param {number} [$0.timeout=null] the maximum time, between 1 and 1000 milliseconds, to wait for the response + * @returns {Promise} if the operation was successful, the response from the EdgeKV + * @throws {object} if the operation was not successful, + * an object describing the non-200 response from the EdgeKV: {failed, status, body} + */ + async putJson({ namespace = this.#namespace, group = this.#group, item, value, timeout = null } = {}) { + return this.requestHandlerTemplate( + () => this.putRequest({ namespace: namespace, group: group, item: item, value: JSON.stringify(value), timeout: timeout }), + (response) => response.text(), + (response) => this.streamText(response.body), + "PUT", + null, + 0 + ); + } + + /** + * PUT json into an item in the EdgeKV while only waiting for the request to send and not for the response. + * @param {string} [$0.namepsace=this.#namespace] specify a namespace other than the default + * @param {string} [$0.group=this.#group] specify a group other than the default + * @param {string} $0.item item key to put into the EdgeKV + * @param {object} $0.value json value to put into the EdgeKV + * @throws {object} if the operation was not successful at sending the request, + * an object describing the error: {failed, status, body} + */ + putJsonNoWait({ namespace = this.#namespace, group = this.#group, item, value } = {}) { + try { + this.putRequest({ namespace: namespace, group: group, item: item, value: JSON.stringify(value) }); + } catch (error) { + this.throwError("PUT FAILED", 0, error.toString()); + } + } + + getRequest({ namespace = this.#namespace, group = this.#group, item, timeout = null } = {}) { + this.validate({ namespace: namespace, group: group, item: item }); + let uri = this.#edgekv_uri + "/api/v1/namespaces/" + namespace + "/groups/" + group + "/items/" + item; + return httpRequest(uri, this.addTimeout({ + method: "GET", + headers: { "X-Akamai-EdgeDB-Auth": [this.getNamespaceToken(namespace)] } + }, timeout)); + } + + /** + * async GET text from an item in the EdgeKV. + * @param {string} [$0.namepsace=this.#namespace] specify a namespace other than the default + * @param {string} [$0.group=this.#group] specify a group other than the default + * @param {string} $0.item item key to get from the EdgeKV + * @param {string} [$0.default_value=null] the default value to return if a 404 response is returned from EdgeKV + * @param {number} [$0.timeout=null] the maximum time, between 1 and 1000 milliseconds, to wait for the response + * @param {number} [$0.num_retries_on_timeout=null] the number of times to retry a requests when the sub request times out + * @returns {Promise} if the operation was successful, the text response from the EdgeKV or the default_value on 404 + * @throws {object} if the operation was not successful, + * an object describing the non-200 and non-404 response from the EdgeKV: {failed, status, body} + */ + async getText({ namespace = this.#namespace, group = this.#group, item, default_value = null, timeout = null, num_retries_on_timeout = null } = {}) { + return this.requestHandlerTemplate( + () => this.getRequest({ namespace: namespace, group: group, item: item, timeout: timeout }), + (response) => response.text(), + (response) => this.streamText(response.body), + "GET TEXT", + default_value, + num_retries_on_timeout ?? this.#num_retries_on_timeout + ); + } + + /** + * async GET json from an item in the EdgeKV. + * @param {string} [$0.namepsace=this.#namespace] specify a namespace other than the default + * @param {string} [$0.group=this.#group] specify a group other than the default + * @param {string} $0.item item key to get from the EdgeKV + * @param {object} [$0.default_value=null] the default value to return if a 404 response is returned from EdgeKV + * @param {number} [$0.timeout=null] the maximum time, between 1 and 1000 milliseconds, to wait for the response + * @param {number} [$0.num_retries_on_timeout=null] the number of times to retry a requests when the sub request times out + * @returns {Promise} if the operation was successful, the json response from the EdgeKV or the default_value on 404 + * @throws {object} if the operation was not successful, + * an object describing the non-200 and non-404 response from the EdgeKV: {failed, status, body} + */ + async getJson({ namespace = this.#namespace, group = this.#group, item, default_value = null, timeout = null, num_retries_on_timeout = null } = {}) { + return this.requestHandlerTemplate( + () => this.getRequest({ namespace: namespace, group: group, item: item, timeout: timeout }), + (response) => response.json(), + (response) => this.streamJson(response.body), + "GET JSON", + default_value, + num_retries_on_timeout ?? this.#num_retries_on_timeout + ); + } + + deleteRequest({ namespace = this.#namespace, group = this.#group, item, timeout = null } = {}) { + this.validate({ namespace: namespace, group: group, item: item }); + let uri = this.#edgekv_uri + "/api/v1/namespaces/" + namespace + "/groups/" + group + "/items/" + item; + return httpRequest(uri, this.addTimeout({ + method: "DELETE", + headers: { "X-Akamai-EdgeDB-Auth": [this.getNamespaceToken(namespace)] } + }, timeout)); + } + + /** + * async DELETE an item in the EdgeKV. + * @param {string} [$0.namepsace=this.#namespace] specify a namespace other than the default + * @param {string} [$0.group=this.#group] specify a group other than the default + * @param {string} $0.item item key to delete from the EdgeKV + * @param {number} [$0.timeout=null] the maximum time, between 1 and 1000 milliseconds, to wait for the response + * @returns {Promise} if the operation was successful, the text response from the EdgeKV + * @throws {object} if the operation was not successful, + * an object describing the non-200 response from the EdgeKV: {failed, status, body} + */ + async delete({ namespace = this.#namespace, group = this.#group, item, timeout = null} = {}) { + return this.requestHandlerTemplate( + () => this.deleteRequest({ namespace: namespace, group: group, item: item, timeout: timeout }), + (response) => response.text(), + (response) => this.streamText(response.body), + "DELETE", + null, + 0 + ); + } + + /** + * DELETE an item in the EdgeKV while only waiting for the request to send and not for the response. + * @param {string} [$0.namepsace=this.#namespace] specify a namespace other than the default + * @param {string} [$0.group=this.#group] specify a group other than the default + * @param {string} $0.item item key to delete from the EdgeKV + * @throws {object} if the operation was not successful at sending the request, + * an object describing the error: {failed, status, body} + */ + deleteNoWait({ namespace = this.#namespace, group = this.#group, item } = {}) { + try { + this.delete({ namespace: namespace, group: group, item: item }); + } catch (error) { + this.throwError("DELETE FAILED", 0, error.toString()); + } + } +} \ No newline at end of file diff --git a/edgecompute/examples/traffic-routing/url-shortner/edgekv_tokens.js b/edgecompute/examples/traffic-routing/url-shortner/edgekv_tokens.js new file mode 100644 index 00000000..7a4dfd17 --- /dev/null +++ b/edgecompute/examples/traffic-routing/url-shortner/edgekv_tokens.js @@ -0,0 +1,7 @@ +var edgekv_access_tokens = { + "namespace-default": { + "name": "default_token", + "value": "insert_customer_token_here" + } +} +export { edgekv_access_tokens }; \ No newline at end of file diff --git a/edgecompute/examples/traffic-routing/url-shortner/main.js b/edgecompute/examples/traffic-routing/url-shortner/main.js new file mode 100644 index 00000000..27dc36cc --- /dev/null +++ b/edgecompute/examples/traffic-routing/url-shortner/main.js @@ -0,0 +1,147 @@ +/* +(c) Copyright 2022 Akamai Technologies, Inc. Licensed under Apache 2 license. + +Version: 0.5 +Purpose: Using EdgeWorkers and EdgeKV, manage a list of shortened URLs and serve redirects for them +*/ + +import URLSearchParams from 'url-search-params'; +import { logger } from 'log'; +import { EdgeKV } from './edgekv.js'; +import { createResponse } from 'create-response'; + +export async function responseProvider (request) { + // Set Up EdgeKV + const edgeKv = new EdgeKV({namespace: "url-shortener", group: "links"}); + + // Request path + const path = request.path; + + // If the request path contains /r/ then we are serving a redirect + if (path.split('/')[1] === 'r') { + let key = path.split('/')[2]; + + // Retrieve a redirect URL associated with the key + let redirectUrl; + try { + redirectUrl = await edgeKv.getText({ item: key, default_value: undefined }); + logger.log('checking ID ' + key + ". EdgeKV contained: "+ redirectUrl ); + } catch (error) { + logger.log("Error serving redirect: " + error); + } + + // If the redirect exists, return a 302 response. Otherwise server a 404 error + if (redirectUrl != undefined && redirectUrl != null) { + return Promise.resolve(createResponse(302, {'Location': [redirectUrl] }, '')); + } else { + return Promise.resolve(createResponse(404, {'Content-Type': ['text/html'] }, 'Redirect not found')); + } + } + +// If the request path contains /m/ then we are managing redirects +if (path.split('/')[1] === 'm') { + var params = new URLSearchParams(request.query); + var name = params.get('name'); + var url = params.get('url'); + var status; + var body; + var checkID; + + // Supported operations are 'add', 'update' and 'delete' + switch (params.get('op')) { + case 'add': + // If a custom redirect name was not defined, then randomly create a name to use as the key + if (name === undefined || name === null) { + name = Math.random().toString(36).substr(2, 10); + } + + // Add redirect to EKV + try { + checkID = await edgeKv.getText({ item: name, default_value: undefined }); + + if(checkID != undefined) { + status = "Error - Redirect already exists for /r/" + name; + body = '' + status + '
'; + logger.log(status); + } else { + await edgeKv.putText({ item: name, value: url }); + status = "Redirect Successfully added"; + body = '' + status + '
\ +

URL: https://' +request.host + '/r/' + name +'

\ +

Redirects to: ' + url +'

'; + } + } catch (error) { + status = "error creating redirect"; + body = '' + status + '
'; + logger.log(status); + } + + return Promise.resolve(createResponse(200, {'Content-Type': ['text/html'] }, '' + body + '')); + break; + + case 'update': + if (name === undefined || url === undefined || name === null || url === null) { + status = "error - missing name or url"; + } + + try { + // Check to see if the redirect exists before we attempt to delete it + checkID = await edgeKv.getText({ item: name, default_value: undefined }); + if(checkID != undefined) { + // Update redirect + await edgeKv.putText({ item: name, value: url }); + status = "Redirect Successfully Updated" + body = '' + status + '
\ +

URL: https://' +request.host + '/r/' + name +'

\ +

Redirects to: ' + url +'

'; + + } else { + // Redirect doesn't exist + status = "Error - Redirect doesn't exist for /r/" + name; + body = '' + status + '
'; + logger.log(status); + } + } catch (error) { + status = "Error updating redirect: " + error; + body = '' + status + '
'; + logger.log(status); + } + + return Promise.resolve(createResponse(200, {'Content-Type': ['text/html'] }, '' + body + '')); + break; + + case 'delete': + if (name === undefined || name === null) { + status = "error - missing name"; + body = '' + status + '
'; + } + + try { + // Check to see if the redirect exists before we attempt to delete it + checkID = await edgeKv.getText({ item: name, default_value: undefined }); + + if(checkID != undefined) { + // Delete redirect + await edgeKv.delete({item: name}); + status = "Redirect Successfully Deleted" + body = '' + status + '
\

https://' +request.host + '/r/' + name +'

'; + } else { + // Redirect doesn't exist + status = "Error - Redirect doesn't exist for /r/" + name; + body = '' + status + '
'; + } + } catch (error) { + status = "Error deleting redirect " + error; + body = '' + status + '
'; + logger.log(status); + } + + return Promise.resolve(createResponse(200, {'Content-Type': ['text/html'] }, '' + body + '')); + break; + + case 'default': + return Promise.resolve(createResponse(404, {'Content-Type': ['text/html'] }, 'Error - invalid operation')); + break; + } + } +}