Skip to content

Commit

Permalink
feat: ✨ Allow wildcard hostnames (#347)
Browse files Browse the repository at this point in the history
  • Loading branch information
wwilsman committed Sep 18, 2019
1 parent eacf136 commit 730161f
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@
"preversion": "yarn clean",
"test": "yarn build-client && PERCY_TOKEN=abc mocha --forbid-only \"test/**/*.test.ts\" --exclude \"test/percy-agent-client/**/*.test.ts\" --exclude \"test/integration/**/*\"",
"test-client": "karma start ./test/percy-agent-client/karma.conf.js",
"test-integration": "yarn build-client && node ./bin/run exec -h localtest.me -- mocha test/integration/**/*.test.ts",
"test-integration": "yarn build-client && node ./bin/run exec -h *.localtest.me -- mocha test/integration/**/*.test.ts",
"test-snapshot-command": "./bin/run snapshot test/integration/test-static-site -b /dummy-base-url -i '(red-keep)' -c '\\.(html)$'",
"version": "oclif-dev readme && git add README.md",
"watch": "npm-watch"
Expand Down
4 changes: 2 additions & 2 deletions src/services/response-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as os from 'os'
import * as path from 'path'
import * as puppeteer from 'puppeteer'
import { URL } from 'url'
import domainMatch from '../utils/domain-match'
import Constants from './constants'
import PercyClientService from './percy-client-service'
import ResourceService from './resource-service'
Expand All @@ -28,8 +29,7 @@ export default class ResponseService extends PercyClientService {
}

// Capture if the resourceUrl has a hostname in the allowedHostnames
const parsedResourceUrl = new URL(resourceUrl)
if (this.allowedHostnames.some((hostname) => parsedResourceUrl.hostname === hostname)) {
if (this.allowedHostnames.some((hostname) => domainMatch(hostname, resourceUrl))) {
return true
}

Expand Down
41 changes: 41 additions & 0 deletions src/utils/domain-match.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { URL } from 'url'

function domainCheck(domain: string, host: string, isWild: boolean) {
if (domain === host) {
return true
}

if (isWild && host) {
const last = host.lastIndexOf(domain)
return (last >= 0 && ((last + domain.length) === host.length))
}

return false
}

function pathCheck(pathprefix: string, pathname: string) {
return pathname.indexOf(pathprefix) === 0
}

export default function domainMatch(pattern: string, siteUrl: string) {
if (pattern === '*') {
return true
} else if (!pattern) {
return false
}

const isWild = ((pattern.indexOf('*.') === 0) || (pattern.indexOf('*/') === 0))

// tslint:disable-next-line
let slashed = pattern.split('/') // tslint wants this to be `const` even though it's mutated
let domain = slashed.shift() as string

const pathprefix = `/${slashed.join('/')}`
const parsedUrl = new URL(siteUrl)

if (isWild) {
domain = domain.substr(2)
}

return (domainCheck(domain, parsedUrl.hostname, isWild) && pathCheck(pathprefix, parsedUrl.pathname))
}

0 comments on commit 730161f

Please sign in to comment.