2022-09-27 19:23:20 +02:00
|
|
|
import * as plugins from './cloudflare.plugins.js';
|
|
|
|
import { CloudflareAccount } from './cloudflare.classes.account.js';
|
|
|
|
import { CloudflareWorker } from './cloudflare.classes.worker.js';
|
2025-03-19 07:07:08 +00:00
|
|
|
import { logger } from './cloudflare.logger.js';
|
2019-07-18 11:51:56 +02:00
|
|
|
|
|
|
|
export class WorkerManager {
|
2019-07-18 14:44:45 +02:00
|
|
|
public cfAccount: CloudflareAccount;
|
2019-07-18 11:51:56 +02:00
|
|
|
|
|
|
|
constructor(cfAccountArg: CloudflareAccount) {
|
|
|
|
this.cfAccount = cfAccountArg;
|
|
|
|
}
|
2019-07-18 14:25:10 +02:00
|
|
|
|
2025-03-19 07:07:08 +00:00
|
|
|
/**
|
|
|
|
* Creates a new worker or updates an existing one
|
|
|
|
* @param workerName Name of the worker
|
|
|
|
* @param workerScript JavaScript content of the worker
|
|
|
|
* @returns The created or updated worker
|
|
|
|
*/
|
2024-06-17 01:36:39 +02:00
|
|
|
public async createWorker(workerName: string, workerScript: string): Promise<plugins.ICloudflareTypes['Script']> {
|
|
|
|
if (!this.cfAccount.preselectedAccountId) {
|
|
|
|
throw new Error('No account selected. Please select it first on the account.');
|
|
|
|
}
|
|
|
|
const worker = await this.cfAccount.apiAccount.workers.scripts.content.update(workerName, {
|
|
|
|
account_id: this.cfAccount.preselectedAccountId,
|
|
|
|
"CF-WORKER-BODY-PART": workerScript,
|
2019-07-18 14:25:10 +02:00
|
|
|
});
|
2024-06-17 01:36:39 +02:00
|
|
|
return worker;
|
2019-07-18 14:25:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2025-03-19 07:07:08 +00:00
|
|
|
* Get a worker by name
|
|
|
|
* @param workerName Name of the worker to retrieve
|
|
|
|
* @returns CloudflareWorker instance or undefined if not found
|
|
|
|
*/
|
|
|
|
public async getWorker(workerName: string): Promise<CloudflareWorker | undefined> {
|
|
|
|
if (!this.cfAccount.preselectedAccountId) {
|
|
|
|
throw new Error('No account selected. Please select it first on the account.');
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
const script = await this.cfAccount.apiAccount.workers.scripts.get(workerName, {
|
|
|
|
account_id: this.cfAccount.preselectedAccountId
|
|
|
|
});
|
|
|
|
|
|
|
|
return CloudflareWorker.fromApiObject(this, script);
|
|
|
|
} catch (error) {
|
|
|
|
logger.log('warn', `Worker '${workerName}' not found: ${error.message}`);
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Lists all worker scripts
|
|
|
|
* @returns Array of worker scripts
|
2019-07-18 14:25:10 +02:00
|
|
|
*/
|
2024-06-17 01:36:39 +02:00
|
|
|
public async listWorkerScripts() {
|
|
|
|
if (!this.cfAccount.preselectedAccountId) {
|
|
|
|
throw new Error('No account selected. Please select it first on the account.');
|
|
|
|
}
|
|
|
|
const workerScripts: plugins.ICloudflareTypes['Script'][] = [];
|
|
|
|
for await (const scriptArg of this.cfAccount.apiAccount.workers.scripts.list({
|
|
|
|
account_id: this.cfAccount.preselectedAccountId,
|
|
|
|
})) {
|
|
|
|
workerScripts.push(scriptArg);
|
2020-02-09 17:36:29 +00:00
|
|
|
}
|
2024-06-17 01:36:39 +02:00
|
|
|
return workerScripts;
|
2019-07-18 14:25:10 +02:00
|
|
|
}
|
2025-03-19 07:07:08 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Deletes a worker script
|
|
|
|
* @param workerName Name of the worker to delete
|
|
|
|
* @returns True if deletion was successful
|
|
|
|
*/
|
|
|
|
public async deleteWorker(workerName: string): Promise<boolean> {
|
|
|
|
if (!this.cfAccount.preselectedAccountId) {
|
|
|
|
throw new Error('No account selected. Please select it first on the account.');
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
await this.cfAccount.apiAccount.workers.scripts.delete(workerName, {
|
|
|
|
account_id: this.cfAccount.preselectedAccountId
|
|
|
|
});
|
|
|
|
logger.log('info', `Worker '${workerName}' deleted successfully`);
|
|
|
|
return true;
|
|
|
|
} catch (error) {
|
|
|
|
logger.log('error', `Failed to delete worker '${workerName}': ${error.message}`);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|