Skip to content

Commit

Permalink
fix: Prevent writing to the snapshot log once the resource sha is cre…
Browse files Browse the repository at this point in the history
…ated (#345)

If the log contents change, the resource sha will no longer match the content
sha and the API will throw errors
  • Loading branch information
wwilsman authored Sep 12, 2019
1 parent a9ff166 commit 2a46561
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/services/snapshot-service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as crypto from 'crypto'
import * as fs from 'fs'
import * as os from 'os'
import * as path from 'path'
import { AssetDiscoveryConfiguration } from '../configuration/asset-discovery-configuration'
import { SnapshotOptions } from '../percy-agent-client/snapshot-options'
Expand Down Expand Up @@ -45,10 +46,16 @@ export default class SnapshotService extends PercyClientService {
return [rootResource].concat(discoveredResources)
}

buildLogResource(localPath: string) {
const fileName = path.basename(localPath)
const buffer = fs.readFileSync(path.resolve(localPath))
buildLogResource(logFilePath: string) {
const fileName = path.basename(logFilePath)
const buffer = fs.readFileSync(path.resolve(logFilePath))
const sha = crypto.createHash('sha256').update(buffer).digest('hex')
const localPath = path.join(os.tmpdir(), sha)

// copy the file to prevent further logs from being written
if (!fs.existsSync(localPath)) {
fs.writeFileSync(localPath, buffer)
}

return this.percyClient.makeResource({
resourceUrl: `/${fileName}`,
Expand Down

0 comments on commit 2a46561

Please sign in to comment.