Skip to content

Commit

Permalink
fix: Add rootResourceURL in front of percy specific resources (#388)
Browse files Browse the repository at this point in the history
* fix: Add rootResourceURL in front of percy specific resources

* fix: import `URL` into snapshot-service
  • Loading branch information
Robdel12 authored Oct 3, 2019
1 parent dcfb0e8 commit ebc3594
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
7 changes: 4 additions & 3 deletions src/services/agent-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ export class AgentService {
profile('agentService.handleSnapshot')

// truncate domSnapshot for the logs if it's very large
const rootURL = request.body.url
let domSnapshotLog = request.body.domSnapshot
if (domSnapshotLog.length > Constants.MAX_LOG_LENGTH) {
domSnapshotLog = domSnapshotLog.substring(0, Constants.MAX_LOG_LENGTH)
Expand Down Expand Up @@ -116,7 +117,7 @@ export class AgentService {
}

let resources = await this.snapshotService.buildResources(
request.body.url,
rootURL,
domSnapshot,
snapshotOptions,
snapshotLogger,
Expand All @@ -133,9 +134,9 @@ export class AgentService {
}

resources = resources.concat(
this.snapshotService.buildRootResource(request.body.url, domSnapshot),
this.snapshotService.buildRootResource(rootURL, domSnapshot),
// @ts-ignore we won't write anything if css is not is passed
this.snapshotService.buildPercyCSSResource(percyCSSFileName, snapshotOptions.percyCSS, snapshotLogger),
this.snapshotService.buildPercyCSSResource(rootURL, percyCSSFileName, snapshotOptions.percyCSS, snapshotLogger),
this.snapshotService.buildLogResource(snapshotLog),
)

Expand Down
11 changes: 8 additions & 3 deletions src/services/snapshot-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as crypto from 'crypto'
import * as fs from 'fs'
import * as os from 'os'
import * as path from 'path'
import { URL } from 'url'
import { AssetDiscoveryConfiguration } from '../configuration/asset-discovery-configuration'
import { SnapshotOptions } from '../percy-agent-client/snapshot-options'
import { logError, profile } from '../utils/logger'
Expand Down Expand Up @@ -65,9 +66,13 @@ export default class SnapshotService extends PercyClientService {
})
}

buildPercyCSSResource(fileName: string, css: string, logger: any) {
buildPercyCSSResource(url: string, fileName: string, css: string, logger: any) {
if (!css) { return [] }
logger.debug(`Creating Percy Specific file: ${fileName}. CSS string: ${css}`)

const parsedRootResourceUrl = new URL(url)
const rootURL = `${parsedRootResourceUrl.protocol}//${parsedRootResourceUrl.host}`

logger.debug(`Creating Percy Specific file: ${fileName}. Root URL: ${rootURL}. CSS string: ${css}`)

const buffer = Buffer.from(css, 'utf8')
const sha = crypto.createHash('sha256').update(buffer).digest('hex')
Expand All @@ -81,7 +86,7 @@ export default class SnapshotService extends PercyClientService {
}

return this.percyClient.makeResource({
resourceUrl: `/${fileName}`,
resourceUrl: `${rootURL}/${fileName}`,
mimetype: 'text/css',
localPath,
sha,
Expand Down

0 comments on commit ebc3594

Please sign in to comment.