Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add url shortener example #136

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open

Conversation

paulcalvano
Copy link

No description provided.

@cosjef cosjef requested a review from mryakan January 4, 2022 15:58
@@ -0,0 +1,328 @@
/*
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should never include "edgekv.js" in any of our public examples. Instead, the documentation should instruct the user to download the latest from https://github.com/akamai/edgeworkers-examples/tree/master/edgekv/lib


export async function responseProvider (request) {
// Set Up EdgeKV
const edgeKv = new EdgeKV({namespace: "url-shortener", group: "links"});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Recommend moving this as a global.

@@ -0,0 +1,7 @@
var edgekv_access_tokens = {
"namespace-default": {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Recommend either not providing a generic edgekv_tokens.js or instead renaming this to "namespace-url-shortener" to be consistent with the namespace used in main.js.


// If the request path contains /r/ then we are serving a redirect
if (path.split('/')[1] === 'r') {
let key = path.split('/')[2];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest you sanitize the key here since it is passed in by a user without any further checks, so could present an opportunity for exploits.

// 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');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggest sanitizing the name here as well


try {
// Check to see if the redirect exists before we attempt to delete it
checkID = await edgeKv.getText({ item: name, default_value: undefined });
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an anti pattern in EdgeKV. Due to the eventual consistency properties of EdgeKV, there is no guarantee that this will return a value even if the item was created by the same edge server. As such, I suggest collapsing the create and update cases and having the URL checking be tracked by the user outside of EdgeKV or not checked at all. In the latter case you will always create a new short URL or update the existing one if the name exists, but this could happen anyway (and very likley in fact) even if you call getText. So calling getText is both misleading the users and also adding unnecessary latency.

body = '<b>' + status + '<b><br>';
}

try {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is absolutely no need to check existence of the item before deleting it, calling delete will delete if it exists or be a no-op essentially. In fact it is even worse to check since in cases when the item was just recently created, the check may not retrun anything (due to eventual consistency property of EdgeKV) and then the delete would not be called. Whereas calling delete without any checks is guaranteed to delete an item if it exists regardless.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants