Skip to content

Commit

Permalink
feat: add regex pattern for custom cdn urls (#43)
Browse files Browse the repository at this point in the history
* feat: add regex pattern and use for custom cdn urls

* fix: typo in test

Co-authored-by: Espen Hovlandsdal <espen@hovlandsdal.com>

* refactor: update check for readability

---------

Co-authored-by: Espen Hovlandsdal <espen@hovlandsdal.com>
  • Loading branch information
cngonzalez and rexxars authored Nov 14, 2024
1 parent c8786cf commit 211f387
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 3 deletions.
6 changes: 6 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ export const cdnUrl = 'https://cdn.sanity.io'
*/
export const cdnUrlPattern = /^https:\/\/cdn\.sanity\./

/**
* @internal
*/
export const customCdnUrlPattern =
/^https:\/\/cdn\.[^/]+\/(images|files)\/[^/]+\/.*?[a-zA-Z0-9_]{24,40}.*$/

/**
* @internal
*/
Expand Down
3 changes: 2 additions & 1 deletion src/parse.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
cdnUrlPattern,
customCdnUrlPattern,
fileAssetIdPattern,
imageAssetFilenamePattern,
imageAssetIdPattern,
Expand Down Expand Up @@ -116,7 +117,7 @@ export function parseAssetFilename(filename: string): SanityAssetIdParts {
* @throws If URL is invalid or not a Sanity asset URL
*/
export function parseAssetUrl(url: string): SanityAssetUrlParts {
if (!cdnUrlPattern.test(url)) {
if (!cdnUrlPattern.test(url) && !customCdnUrlPattern.test(url)) {
throw new Error(`URL is not a valid Sanity asset URL: ${url}`)
}

Expand Down
3 changes: 2 additions & 1 deletion src/paths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {isAssetObjectStub, isAssetPathStub, isAssetUrlStub, isReference} from '.
import {
cdnUrl,
cdnUrlPattern,
customCdnUrlPattern,
fileAssetFilenamePattern,
imageAssetFilenamePattern,
pathPattern,
Expand Down Expand Up @@ -157,7 +158,7 @@ export function getUrlPath(url: string): string {
return url
}

if (!cdnUrlPattern.test(url)) {
if (!cdnUrlPattern.test(url) && !customCdnUrlPattern.test(url)) {
throw new UnresolvableError(`Failed to resolve path from URL "${url}"`)
}

Expand Down
8 changes: 7 additions & 1 deletion test/parse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ test('parseAssetFilename(): returns object of named image properties if legacy f
test.each([
'https://cdn.sanity.io/images/espenhov/diary/756e4bd9c0a04ada3d3cc396cf81f1c433b07870-5760x3840.jpg/vanity-filename.jpg',
'https://cdn.sanity.staging/images/espenhov/diary/756e4bd9c0a04ada3d3cc396cf81f1c433b07870-5760x3840.jpg/vanity-filename.jpg',
'https://cdn.autofoos.com/images/espenhov/diary/756e4bd9c0a04ada3d3cc396cf81f1c433b07870-5760x3840.jpg/vanity-filename.jpg',
])(
'parseAssetUrl(): returns object of named image properties on modern filename with vanity filename',
(url) => {
Expand All @@ -131,6 +132,7 @@ test.each([
test.each([
'https://cdn.sanity.io/images/espenhov/diary/756e4bd9c0a04ada3d3cc396cf81f1c433b07870-5760x3840.jpg',
'https://cdn.sanity.staging/images/espenhov/diary/756e4bd9c0a04ada3d3cc396cf81f1c433b07870-5760x3840.jpg',
'https://cdn.autofoos.com/images/espenhov/diary/756e4bd9c0a04ada3d3cc396cf81f1c433b07870-5760x3840.jpg',
])('parseAssetUrl(): returns object of named image properties on modern filename', (url) => {
expect(parseAssetUrl(url)).toMatchObject({
assetId: '756e4bd9c0a04ada3d3cc396cf81f1c433b07870',
Expand All @@ -147,6 +149,7 @@ test.each([
test.each([
'https://cdn.sanity.io/images/espenhov/diary/LA5zSofUOP0i_iQwi4B2dEbzHQseitcuORm4n-600x578.png',
'https://cdn.sanity.staging/images/espenhov/diary/LA5zSofUOP0i_iQwi4B2dEbzHQseitcuORm4n-600x578.png',
'https://cdn.autofoos.com/images/espenhov/diary/LA5zSofUOP0i_iQwi4B2dEbzHQseitcuORm4n-600x578.png',
])('parseAssetUrl(): returns object of named image properties on legacy filename', (url) => {
expect(parseAssetUrl(url)).toMatchObject({
assetId: 'LA5zSofUOP0i_iQwi4B2dEbzHQseitcuORm4n',
Expand All @@ -163,6 +166,7 @@ test.each([
test.each([
'https://cdn.sanity.io/files/espenhov/diary/ae0ef9f916843d32fef3faffb9a675d4cce046f0.pdf/oslo-guide.pdf',
'https://cdn.sanity.staging/files/espenhov/diary/ae0ef9f916843d32fef3faffb9a675d4cce046f0.pdf/oslo-guide.pdf',
'https://cdn.autofoos.com/files/espenhov/diary/ae0ef9f916843d32fef3faffb9a675d4cce046f0.pdf/oslo-guide.pdf',
])(
'parseAssetUrl(): returns object of named file properties on modern filename with vanity filename',
(url) => {
Expand All @@ -180,6 +184,7 @@ test.each([
test.each([
'https://cdn.sanity.io/files/espenhov/diary/ae0ef9f916843d32fef3faffb9a675d4cce046f0.pdf',
'https://cdn.sanity.staging/files/espenhov/diary/ae0ef9f916843d32fef3faffb9a675d4cce046f0.pdf',
'https://cdn.autofoos.com/files/espenhov/diary/ae0ef9f916843d32fef3faffb9a675d4cce046f0.pdf',
])('parseAssetUrl(): returns object of named file properties on modern filename', (url) => {
expect(parseAssetUrl(url)).toMatchObject({
assetId: 'ae0ef9f916843d32fef3faffb9a675d4cce046f0',
Expand All @@ -194,6 +199,7 @@ test.each([
test.each([
'https://cdn.sanity.io/files/espenhov/diary/LA5zSofUOP0i_iQwi4B2dEbzHQseitcuORm4n.pdf',
'https://cdn.sanity.staging/files/espenhov/diary/LA5zSofUOP0i_iQwi4B2dEbzHQseitcuORm4n.pdf',
'https://cdn.autofoos.com/files/espenhov/diary/LA5zSofUOP0i_iQwi4B2dEbzHQseitcuORm4n.pdf',
])('parseAssetUrl(): returns object of named file properties on legacy filename', (url) => {
expect(parseAssetUrl(url)).toMatchObject({
assetId: 'LA5zSofUOP0i_iQwi4B2dEbzHQseitcuORm4n',
Expand Down Expand Up @@ -239,4 +245,4 @@ test('parseAssetUrl(): throws on invalid URLs', () => {
).toThrowErrorMatchingInlineSnapshot(
`[Error: Invalid image/file asset filename: ae0e-f9f916843d-32fef3faffb9a-675d4cce046f0.pdf]`,
)
})
})
12 changes: 12 additions & 0 deletions test/resolve.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ const imgPathPretty = `${imgPath}/pretty.png`
const imgUrlPretty = `${imgUrl}/pretty.png`
const stagingImgUrl = `https://cdn.sanity.staging/${imgPath}`
const stagingImgUrlPretty = `${stagingImgUrl}/pretty.png`
const customCdnImgUrl = `https://cdn.autofoos.com/${imgPath}`
const customCdnImgUrlPretty = `${customCdnImgUrl}/pretty.png`
const validImgSources: [string, SanityImageSource][] = [
['asset id', imgId],
['reference', {_ref: imgId}],
Expand All @@ -47,12 +49,14 @@ const validImgSources: [string, SanityImageSource][] = [
['object path stub', {asset: {path: imgPath}}],
['object url stub', {asset: {url: imgUrl}}],
['staging object url stub', {asset: {url: stagingImgUrl}}],
['custom cdn object url stub', {asset: {url: customCdnImgUrl}}],
['path stub (pretty filename)', {path: imgPathPretty}],
['url stub (pretty filename)', {url: imgUrlPretty}],
['staging url stub (pretty filename)', {url: stagingImgUrlPretty}],
['object path stub (pretty filename)', {asset: {path: imgPathPretty}}],
['object url stub (pretty filename)', {asset: {url: imgUrlPretty}}],
['staging object url stub (pretty filename)', {asset: {url: stagingImgUrlPretty}}],
['custom cdn object url stub (pretty filename)', {asset: {url: customCdnImgUrlPretty}}],
]

const imgLegacyId = 'image-LA5zSofUOP0i_iQwi4B2dEbzHQseitcuORm4n-600x578-png'
Expand All @@ -62,6 +66,8 @@ const imgLegacyPathPretty = `${imgLegacyPath}/pretty.png`
const imgLegacyUrlPretty = `${imgLegacyUrl}/pretty.png`
const stagingImgLegacyUrl = `https://cdn.sanity.io/${imgLegacyPath}`
const stagingImgLegacyUrlPretty = `${stagingImgLegacyUrl}/pretty.png`
const customCdnImgLegacyUrl = `https://cdn.autofoos.com/${imgLegacyPath}`
const customCdnImgLegacyUrlPretty = `${customCdnImgLegacyUrl}/pretty.png`
const validLegacyImgSources: [string, SanityImageSource][] = [
['asset id', imgLegacyId],
['reference', {_ref: imgLegacyId}],
Expand All @@ -73,12 +79,14 @@ const validLegacyImgSources: [string, SanityImageSource][] = [
['object path stub', {asset: {path: imgLegacyPath}}],
['object url stub', {asset: {url: imgLegacyUrl}}],
['staging object url stub', {asset: {url: stagingImgLegacyUrl}}],
['custom cdn object url stub', {asset: {url: customCdnImgLegacyUrl}}],
['path stub (pretty filename)', {path: imgLegacyPathPretty}],
['url stub (pretty filename)', {url: imgLegacyUrlPretty}],
['staging url stub (pretty filename)', {url: stagingImgLegacyUrlPretty}],
['object path stub (pretty filename)', {asset: {path: imgLegacyPathPretty}}],
['object url stub (pretty filename)', {asset: {url: imgLegacyUrlPretty}}],
['staging object url stub (pretty filename)', {asset: {url: stagingImgLegacyUrlPretty}}],
['custom cdn object url stub (pretty filename)', {asset: {url: customCdnImgLegacyUrlPretty}}],
]

const fileId = 'file-def987abc12345678909877654321abcdef98765-pdf'
Expand All @@ -88,6 +96,8 @@ const filePathPretty = `${filePath}/pretty.pdf`
const fileUrlPretty = `${fileUrl}/pretty.pdf`
const stagingFileUrl = `https://cdn.sanity.staging/${filePath}`
const stagingFileUrlPretty = `${stagingFileUrl}/pretty.pdf`
const customCdnFileUrl = `https://cdn.autofoos.com/${filePath}`
const customCdnFileUrlPretty = `${customCdnFileUrl}/pretty.pdf`
const validFileSources: [string, SanityFileSource][] = [
['asset id', fileId],
['reference', {_ref: fileId}],
Expand All @@ -99,12 +109,14 @@ const validFileSources: [string, SanityFileSource][] = [
['object path stub', {asset: {path: filePath}}],
['object url stub', {asset: {url: fileUrl}}],
['staging object url stub', {asset: {url: stagingFileUrl}}],
['custom cdn object url stub', {asset: {url: customCdnFileUrl}}],
['path stub (pretty filename)', {path: filePathPretty}],
['url stub (pretty filename)', {url: fileUrlPretty}],
['staging url stub (pretty filename)', {url: stagingFileUrlPretty}],
['object path stub (pretty filename)', {asset: {path: filePathPretty}}],
['object url stub (pretty filename)', {asset: {url: fileUrlPretty}}],
['staging object url stub (pretty filename)', {asset: {url: stagingFileUrlPretty}}],
['custom cdn object url stub (pretty filename)', {asset: {url: customCdnFileUrlPretty}}],
]

// buildImagePath()
Expand Down

0 comments on commit 211f387

Please sign in to comment.