@qpoint/mask-urls
Advanced tools
Comparing version 0.1.0 to 0.1.1
import { Context } from '@qpoint/router'; | ||
interface MaskUrlsConfig { | ||
export interface MaskUrlsConfig { | ||
basePath?: string; | ||
absolute?: boolean; | ||
edgeUrl?: string; | ||
appUrl?: string; | ||
} | ||
export default function rewriteHtml(config?: MaskUrlsConfig): (context: Context, next: Function) => any; | ||
export {}; | ||
export default function rewriteHtml(config?: MaskUrlsConfig): (ctx: Context, next: Function) => any; |
@@ -7,11 +7,19 @@ "use strict"; | ||
// return middleware | ||
return function run(context, next) { | ||
// extract the config | ||
const { basePath = '' } = config; | ||
return function run(ctx, next) { | ||
// grab the response headers | ||
const headers = ctx.res.headers; | ||
// extract the content-type from the response | ||
const contentType = headers.get("Content-Type") || headers.get('content-type'); | ||
// no need to run if this isn't html | ||
if (!contentType?.startsWith("text/html")) | ||
return next(); | ||
// let's check to see if we have any dynamic config | ||
if (ctx.state['mask-urls.config']) | ||
config = ctx.state['mask-urls.config']; | ||
// attach to the rewriter | ||
context.htmlRewriter | ||
.on("a", new url_rewriter_1.UrlRewriter('href', context, basePath)) | ||
.on("link", new url_rewriter_1.UrlRewriter('href', context, basePath)) | ||
.on("img", new url_rewriter_1.UrlRewriter('src', context, basePath)) | ||
.on("script", new url_rewriter_1.UrlRewriter('src', context, basePath)); | ||
ctx.htmlRewriter | ||
.on("a", new url_rewriter_1.UrlRewriter('href', ctx, config)) | ||
.on("link", new url_rewriter_1.UrlRewriter('href', ctx, config)) | ||
.on("img", new url_rewriter_1.UrlRewriter('src', ctx, config)) | ||
.on("script", new url_rewriter_1.UrlRewriter('src', ctx, config)); | ||
// continue along | ||
@@ -18,0 +26,0 @@ return next(); |
/// <reference types="@cloudflare/workers-types" /> | ||
import { Context } from '@qpoint/router'; | ||
import { MaskUrlsConfig } from '.'; | ||
interface LinkPattern { | ||
from: string; | ||
to: string; | ||
} | ||
export declare class UrlRewriter { | ||
attribute: string; | ||
basePath: string; | ||
edgeBase: string; | ||
appBase: string; | ||
constructor(attribute: string, context: Context, basePath: string); | ||
patterns: LinkPattern[]; | ||
constructor(attribute: string, ctx: Context, config: MaskUrlsConfig); | ||
element(element: Element): void; | ||
} | ||
export {}; |
@@ -7,27 +7,44 @@ "use strict"; | ||
attribute; | ||
basePath; | ||
edgeBase; | ||
appBase; | ||
constructor(attribute, context, basePath) { | ||
patterns; | ||
constructor(attribute, ctx, config) { | ||
// extract the config | ||
let { edgeUrl, appUrl, basePath, absolute } = config; | ||
// if edgeUrl wasn't provided, let's grab it from the request | ||
if (!edgeUrl) | ||
edgeUrl = ctx.req.url; | ||
// if appUrl wasn't provided, let's grab it from the proxy | ||
if (!appUrl) | ||
appUrl = ctx.pxy.url; | ||
// basePath should be an empty string, at the least | ||
if (!basePath) | ||
basePath = ''; | ||
// extract the URLs | ||
const edgeUrl = new URL(context.request.url); | ||
const appUrl = new URL(context.proxy.url); | ||
// set the internal state | ||
const edge = new URL(edgeUrl); | ||
const app = new URL(appUrl); | ||
// generate the link patterns | ||
this.patterns = [ | ||
// https://appUrl.com/path | ||
{ from: `${app.origin}`, to: `${edge.origin}${basePath}` }, | ||
// //appUrl.com/path | ||
{ from: `//${app.host}`, to: `//${edge.host}${basePath}` }, | ||
// /path | ||
{ from: `/`, to: `${absolute ? '//' + edge.host : ''}/${basePath}` } | ||
]; | ||
// set the attribute | ||
this.attribute = attribute; | ||
this.basePath = basePath; | ||
this.edgeBase = `${edgeUrl.origin}${basePath}`; | ||
this.appBase = appUrl.origin; | ||
} | ||
element(element) { | ||
const attribute = element.getAttribute(this.attribute); | ||
if (attribute) { | ||
let value; | ||
// handle relative and absolute urls | ||
if (attribute.startsWith('/') && !attribute.startsWith(this.basePath)) { | ||
value = this.basePath + attribute; | ||
// nothing to do if the html element doesn't have the attribute containing the link | ||
if (!attribute) { | ||
return; | ||
} | ||
// run through the link patterns and adjust accordingly | ||
for (const pattern of this.patterns) { | ||
if (attribute.startsWith(pattern.from)) { | ||
// update the attribute value | ||
element.setAttribute(this.attribute, attribute.replace(pattern.from, pattern.to)); | ||
// return | ||
return; | ||
} | ||
else { | ||
value = attribute.replace(this.appBase, this.edgeBase); | ||
} | ||
element.setAttribute(this.attribute, value); | ||
} | ||
@@ -34,0 +51,0 @@ } |
{ | ||
"name": "@qpoint/mask-urls", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"description": "A Qpoint adapter for masking URLs behind a reverse proxy", | ||
@@ -18,6 +18,7 @@ "author": "Tyler Flint <tyler@qpoint.io>", | ||
"build": "tsc", | ||
"release": "npm publish --access=public" | ||
"release": "npm publish --access=public", | ||
"bundle": "esbuild src/index.ts --outdir=dist --bundle --sourcemap=external --platform=browser --format=esm" | ||
}, | ||
"dependencies": { | ||
"@qpoint/router": "^0.1.6" | ||
"@qpoint/router": "^0.1.12" | ||
}, | ||
@@ -24,0 +25,0 @@ "devDependencies": { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
19517
102
Updated@qpoint/router@^0.1.12