2022-09-27 17:23:20 +00:00
|
|
|
import * as plugins from './cloudflare.plugins.js';
|
|
|
|
import { CloudflareAccount } from './cloudflare.classes.account.js';
|
|
|
|
import { CloudflareWorker } from './cloudflare.classes.worker.js';
|
2019-07-18 09:51:56 +00:00
|
|
|
|
|
|
|
export class WorkerManager {
|
2019-07-18 12:44:45 +00:00
|
|
|
public cfAccount: CloudflareAccount;
|
2019-07-18 09:51:56 +00:00
|
|
|
|
|
|
|
constructor(cfAccountArg: CloudflareAccount) {
|
|
|
|
this.cfAccount = cfAccountArg;
|
|
|
|
}
|
2019-07-18 12:25:10 +00:00
|
|
|
|
2019-07-19 10:39:39 +00:00
|
|
|
public async createWorker(workerName: string, workerScript: string): Promise<CloudflareWorker> {
|
2019-07-18 12:25:10 +00:00
|
|
|
const accountIdentifier = await this.cfAccount.getAccountIdentifier();
|
|
|
|
const route = `/accounts/${accountIdentifier}/workers/scripts/${workerName}`;
|
|
|
|
const responseBody = await this.cfAccount.request('PUT', route, workerScript, {
|
|
|
|
'Content-Type': 'application/javascript',
|
2021-01-22 20:46:26 +00:00
|
|
|
'Content-Length': Buffer.byteLength(workerScript),
|
2019-07-18 12:25:10 +00:00
|
|
|
});
|
2020-02-09 17:54:33 +00:00
|
|
|
return CloudflareWorker.fromApiObject(this, responseBody.result);
|
2019-07-18 12:25:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* lists workers
|
|
|
|
*/
|
|
|
|
public async listWorkers() {
|
|
|
|
const accountIdentifier = await this.cfAccount.getAccountIdentifier();
|
|
|
|
const route = `/accounts/${accountIdentifier}/workers/scripts`;
|
|
|
|
const response = await this.cfAccount.request('GET', route);
|
2020-02-09 17:36:29 +00:00
|
|
|
const results = response.result;
|
|
|
|
const workers: CloudflareWorker[] = [];
|
|
|
|
for (const apiObject of results) {
|
|
|
|
workers.push(await CloudflareWorker.fromApiObject(this, apiObject));
|
|
|
|
}
|
|
|
|
return workers;
|
2019-07-18 12:25:10 +00:00
|
|
|
}
|
2019-07-18 09:51:56 +00:00
|
|
|
}
|