Skip to content

Commit

Permalink
chore: generate image IDs from relative filepath (#605)
Browse files Browse the repository at this point in the history
Co-authored-by: Ben McCann <[email protected]>
  • Loading branch information
liegeandlief and benmccann authored Oct 6, 2023
1 parent 31b48ad commit 5cbdbe8
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/tender-jobs-double.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'imagetools-core': patch
---

chore: image IDs are now generated from relative filepaths instead of absolute ones
7 changes: 6 additions & 1 deletion packages/core/src/util.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { createHash } from 'node:crypto'
import path from 'node:path'
import sharp from 'sharp'
import { ImageConfig } from './types.js'

Expand All @@ -7,7 +8,11 @@ export function loadImage(path: string) {
}

export function generateImageID(url: URL, config: ImageConfig) {
const baseURL = url.host ? new URL(url.origin + url.pathname) : new URL(url.protocol + url.pathname)
// this isn't a valid URL, but just a string used for an identifier
// use a relative path in the local case so that it's consistent across machines
const baseURL = url.host
? new URL(url.origin + url.pathname)
: new URL(url.protocol + path.relative(process.cwd(), url.pathname))

return createHash('sha1').update(baseURL.href).update(JSON.stringify(config)).digest('hex')
}

0 comments on commit 5cbdbe8

Please sign in to comment.