2025-02-23 23:47:07 -08:00
export async function deployScript ( scriptName : string , contents : string ) {
const scriptIdEnvName = ` SCRIPT_ID_ ${ scriptName . toUpperCase ( ) } ` ;
const scriptId = Deno . env . get ( scriptIdEnvName ) ;
const deployKeyEnvName = ` SCRIPT_KEY_ ${ scriptName . toUpperCase ( ) } ` ;
const deployKey = Deno . env . get ( deployKeyEnvName ) ;
if ( scriptId === undefined ) {
throw new Error (
` Can't deploy script ' ${ scriptName } ' because we don't know its id on bunny. Please set it by setting the environment variable ' ${ scriptIdEnvName } '. `
) ;
}
if ( deployKey === undefined ) {
throw new Error (
` Can't deploy script ' ${ scriptName } ' because we don't know its deploy key on bunny. Please set it by setting the environment variable ' ${ scriptIdEnvName } '. `
) ;
}
const headers = {
accept : 'application/json' ,
'content-type' : 'application/json' ,
deploymentkey : deployKey ,
} ;
const setCodeResp = await fetch (
` https://api.bunny.net/compute/script/ ${ scriptId } /code ` ,
{
method : 'POST' ,
headers ,
body : JSON.stringify ( { Code : contents } ) ,
}
) ;
if ( ! setCodeResp . ok ) {
console . error ( await setCodeResp . text ( ) ) ;
throw new Error (
` Failed to upload new script code: ${ setCodeResp . statusText } `
) ;
}
const publishResp = await fetch (
` https://api.bunny.net/compute/script/ ${ scriptId } /publish ` ,
{
method : 'POST' ,
headers ,
}
) ;
if ( ! publishResp . ok ) {
console . error ( await setCodeResp . text ( ) ) ;
throw new Error ( ` Failed to publish script: ${ setCodeResp . statusText } ` ) ;
}
}
export async function uploadFile ( filepath : string , contents : Uint8Array ) {
const storageZoneNameEnvName = 'STORAGE_ZONE_NAME' ;
const storageZoneName = Deno . env . get ( storageZoneNameEnvName ) ;
const storageApiKeyEnvName = 'STORAGE_API_KEY' ;
const apiKey = Deno . env . get ( storageApiKeyEnvName ) ;
if ( storageZoneName === undefined ) {
throw new Error (
` Can't upload file ' ${ filepath } ' because we don't know which storage zone to upload to. Please set it by setting the environment variable ' ${ storageZoneNameEnvName } '. `
) ;
}
if ( apiKey === undefined ) {
throw new Error (
` Can't upload file ' ${ filepath } ' because we don't have an API key. Please set it by setting the environment variable ' ${ storageApiKeyEnvName } '. `
) ;
}
const headers = {
accept : 'application/json' ,
'content-type' : 'application/octet-stream' ,
accesskey : apiKey ,
} ;
const res = await fetch (
` https://storage.bunnycdn.com/ ${ storageZoneName } / ${ filepath } ` ,
{
method : 'PUT' ,
headers ,
body : contents ,
}
) ;
if ( ! res . ok ) {
console . error ( await res . text ( ) ) ;
throw new Error ( ` Failed to upload file: ${ res . statusText } ` ) ;
}
}
2025-03-31 22:46:55 -07:00
export async function purgeCDNCache() {
const pullZoneIdEnvName = 'BUNNY_PULL_ZONE_ID' ;
const pullZoneId = Deno . env . get ( pullZoneIdEnvName ) ;
2025-02-23 23:47:07 -08:00
const accessKeyEnvName = 'BUNNY_ACCESS_KEY' ;
const accessKey = Deno . env . get ( accessKeyEnvName ) ;
2025-03-31 22:46:55 -07:00
if ( pullZoneId === undefined ) {
2025-02-23 23:47:07 -08:00
throw new Error (
2025-03-31 22:46:55 -07:00
` Can't purge CDN cache for because we don't know the pull zone ID. Please set it by setting the environment variable ' ${ pullZoneIdEnvName } '. `
2025-02-23 23:47:07 -08:00
) ;
}
if ( accessKey === undefined ) {
throw new Error (
2025-03-31 22:46:55 -07:00
` Can't purge CDN cache because we don't have an API key. Please set it by setting the environment variable ' ${ accessKeyEnvName } '. `
2025-02-23 23:47:07 -08:00
) ;
}
const headers = {
accept : 'application/json' ,
'content-type' : 'application/json' ,
accesskey : accessKey ,
} ;
2025-03-31 22:46:55 -07:00
const fetchUrl = ` https://api.bunny.net/pullzone/ ${ pullZoneId } /purgeCache ` ;
2025-02-23 23:47:07 -08:00
const res = await fetch ( fetchUrl . toString ( ) , {
method : 'POST' ,
headers ,
} ) ;
if ( ! res . ok ) {
console . error ( await res . text ( ) ) ;
2025-03-31 22:46:55 -07:00
throw new Error ( ` Failed to purge CDN cache: ${ res . statusText } ` ) ;
2025-02-23 23:47:07 -08:00
}
}