forked from bluesky-social/atproto
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for a bunny CDN img invalidator (bluesky-social#1689)
* add support for a bunny.net img invalidator * tidy * support multiple image invalidators * tidy
- Loading branch information
Showing
11 changed files
with
137 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { handleAllSettledErrors } from '@atproto/common' | ||
import { ImageInvalidator } from './types' | ||
|
||
export type BunnyConfig = { | ||
accessKey: string | ||
urlPrefix: string | ||
} | ||
|
||
const API_PURGE_URL = 'https://api.bunny.net/purge' | ||
|
||
export class BunnyInvalidator implements ImageInvalidator { | ||
constructor(public cfg: BunnyConfig) {} | ||
async invalidate(_subject: string, paths: string[]) { | ||
const results = await Promise.allSettled( | ||
paths.map(async (path) => | ||
purgeUrl({ | ||
url: this.cfg.urlPrefix + path, | ||
accessKey: this.cfg.accessKey, | ||
}), | ||
), | ||
) | ||
handleAllSettledErrors(results) | ||
} | ||
} | ||
|
||
export default BunnyInvalidator | ||
|
||
async function purgeUrl(opts: { accessKey: string; url: string }) { | ||
const search = new URLSearchParams() | ||
search.set('async', 'true') | ||
search.set('url', opts.url) | ||
await fetch(API_PURGE_URL + '?' + search.toString(), { | ||
method: 'post', | ||
headers: { AccessKey: opts.accessKey }, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
export * from './kms' | ||
export * from './s3' | ||
export * from './cloudfront' | ||
export * from './bunny' | ||
export * from './util' | ||
export * from './types' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
// @NOTE keep in sync with same interface in bsky/src/image/invalidator.ts | ||
// this is separate to avoid the dependency on @atproto/bsky. | ||
export interface ImageInvalidator { | ||
invalidate(subject: string, paths: string[]): Promise<void> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { handleAllSettledErrors } from '@atproto/common' | ||
import { ImageInvalidator } from './types' | ||
|
||
export class MultiImageInvalidator implements ImageInvalidator { | ||
constructor(public invalidators: ImageInvalidator[]) {} | ||
async invalidate(subject: string, paths: string[]) { | ||
const results = await Promise.allSettled( | ||
this.invalidators.map((invalidator) => | ||
invalidator.invalidate(subject, paths), | ||
), | ||
) | ||
handleAllSettledErrors(results) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters