From ac1421e8d5eab97159f009c4b8b41c0b882f8b9a Mon Sep 17 00:00:00 2001 From: "Alex N. Jose" Date: Wed, 10 Jan 2024 11:19:54 -0800 Subject: [PATCH] Implement Redirect operation - Pre-UA and On-UA implementations (#17) * Redirect op first cut * Markdown updates --------- Co-authored-by: Micah Heineck --- sdks/npm/src/cf/on-ua-applicator.ts | 8 ++- sdks/npm/src/cf/pre-ua-applicator.ts | 6 ++ specs/EXPLAINER.md | 88 ++++++++++------------------ 3 files changed, 42 insertions(+), 60 deletions(-) diff --git a/sdks/npm/src/cf/on-ua-applicator.ts b/sdks/npm/src/cf/on-ua-applicator.ts index 785059f..a1ac740 100644 --- a/sdks/npm/src/cf/on-ua-applicator.ts +++ b/sdks/npm/src/cf/on-ua-applicator.ts @@ -8,8 +8,12 @@ export default class UATransformApplicator implements HTMLRewriterElementConten } static serializeOperation(operation: Experimentation.Operations, args: any[]) { - if (operation === Experimentation.Operations.CustomJs) { - return [operation, `$=>{${args[0]}}`]; + switch (operation) { + case Experimentation.Operations.CustomJs: + return [operation, `$=>{${args[0]}}`]; + case Experimentation.Operations.Redirect: + // In case of a redirect, we use a CustomJs operation to rewrite location. + return [Experimentation.Operations.CustomJs, `$=>{window.location.href="${JSON.stringify(args[0])}"}`]; } return [operation, ...args.map(arg => JSON.stringify(arg))]; } diff --git a/sdks/npm/src/cf/pre-ua-applicator.ts b/sdks/npm/src/cf/pre-ua-applicator.ts index 693c6c9..82f0330 100644 --- a/sdks/npm/src/cf/pre-ua-applicator.ts +++ b/sdks/npm/src/cf/pre-ua-applicator.ts @@ -37,5 +37,11 @@ export default class PreUATransformApplicator implements HTMLRewriterElementCont } // Handle any complex operations that involves multiple CF operations. + switch (this.op) { + case Experimentation.Operations.Redirect: + const [url, code] = this.args; + Response.redirect(url, code); + break; + } } } \ No newline at end of file diff --git a/specs/EXPLAINER.md b/specs/EXPLAINER.md index 210deb5..40f6211 100644 --- a/specs/EXPLAINER.md +++ b/specs/EXPLAINER.md @@ -109,25 +109,22 @@ where each field can be defined as follows Operation Code Description + Arguments OP_CUSTOM_JS 0 Executes a custom Javascript block of code against the element. -

- -Arguments: - -`code`: Javascript code serialized as a string. The applicator code will call this code as a function ($) => code, $ referring to the element selected. + + + `code` : Javascript code serialized as a string. The applicator code will call this code as a function ($) => code, $ referring to the element selected. OP_INSERT_BEFORE 1 - Inserts content right before the element. -

- -Arguments: + Inserts content right before the element. + `content`: HTML markup @@ -135,99 +132,74 @@ Arguments: OP_INSERT_AFTER 2 - Inserts content right after the element. -

+ Inserts content right after the element. + -Arguments: - -

`content`: HTML markup OP_PREPEND 3 - Inserts content right after the start tag of the element. -

- -Arguments: - -

-`content`: HTML markup + Inserts content right after the start tag of the element. + + + `content`: HTML markup OP_APPEND 4 - Inserts content right before the end tag of the element. -

- -Arguments: + Inserts content right before the end tag of the element. + -

`content`: HTML markup OP_REPLACE 5 - Replaces the element with the provided content. -

+ Replaces the element with the provided content. + -Arguments: - -

`content`: HTML markup OP_SET_INNERHTML 6 - Replaces the content of the element with provided content. -

- -Arguments: - -

+ Replaces the content of the element with provided content. + + `content`: HTML markup OP_REMOVE 7 - Removes the element and its children -

- -Arguments: - -

-none. - + Removes the element and its children + none OP_SET_ATTRIBUTE 8 - Sets an attribute’s value on the element. -

- -Arguments: - -

+ Sets an attribute’s value on the element. + + `name`: Name of the attribute -

+ `value`: Value to be set on the attribute. OP_REDIRECT 9 - Redirect the user to a different page or URL. -

- -Arguments: - -

+ Redirect the user to a different page or URL. During `PRE_UA` phase, this will perform an HTTP redirect and during `ON_UA`, this will result in client side redirection. Additional arguments supplied can select the response code during PRE_UA phase. + + `URL`: URL to redirect the page to. + +`code`: HTTP code to use for redirection during PRE_UA phase.