128 lines
3.7 KiB
TypeScript
128 lines
3.7 KiB
TypeScript
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}`);
|
|
}
|
|
}
|
|
|
|
export async function purgeCDNCache() {
|
|
const pullZoneIdEnvName = 'BUNNY_PULL_ZONE_ID';
|
|
const pullZoneId = Deno.env.get(pullZoneIdEnvName);
|
|
const accessKeyEnvName = 'BUNNY_ACCESS_KEY';
|
|
const accessKey = Deno.env.get(accessKeyEnvName);
|
|
|
|
if (pullZoneId === undefined) {
|
|
throw new Error(
|
|
`Can't purge CDN cache for because we don't know the pull zone ID. Please set it by setting the environment variable '${pullZoneIdEnvName}'.`
|
|
);
|
|
}
|
|
|
|
if (accessKey === undefined) {
|
|
throw new Error(
|
|
`Can't purge CDN cache because we don't have an API key. Please set it by setting the environment variable '${accessKeyEnvName}'.`
|
|
);
|
|
}
|
|
|
|
const headers = {
|
|
accept: 'application/json',
|
|
'content-type': 'application/json',
|
|
accesskey: accessKey,
|
|
};
|
|
|
|
const fetchUrl = `https://api.bunny.net/pullzone/${pullZoneId}/purgeCache`;
|
|
|
|
const res = await fetch(fetchUrl.toString(), {
|
|
method: 'POST',
|
|
headers,
|
|
});
|
|
|
|
if (!res.ok) {
|
|
console.error(await res.text());
|
|
throw new Error(`Failed to purge CDN cache: ${res.statusText}`);
|
|
}
|
|
}
|