Compare commits
12 Commits
Author | SHA1 | Date | |
---|---|---|---|
04d70a4d12 | |||
8da43a79d3 | |||
3153021190 | |||
82701c19e7 | |||
d3a68b4fef | |||
c4c1367306 | |||
1f5352d9f5 | |||
f652cc72fe | |||
f510408fce | |||
e250e9b1a2 | |||
5a9b7bbeee | |||
1158b4ff99 |
@ -4,13 +4,13 @@ image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
|||||||
cache:
|
cache:
|
||||||
paths:
|
paths:
|
||||||
- .npmci_cache/
|
- .npmci_cache/
|
||||||
key: "$CI_BUILD_STAGE"
|
key: '$CI_BUILD_STAGE'
|
||||||
|
|
||||||
stages:
|
stages:
|
||||||
- security
|
- security
|
||||||
- test
|
- test
|
||||||
- release
|
- release
|
||||||
- metadata
|
- metadata
|
||||||
|
|
||||||
# ====================
|
# ====================
|
||||||
# security stage
|
# security stage
|
||||||
@ -20,17 +20,19 @@ mirror:
|
|||||||
script:
|
script:
|
||||||
- npmci git mirror
|
- npmci git mirror
|
||||||
tags:
|
tags:
|
||||||
|
- lossless
|
||||||
- docker
|
- docker
|
||||||
- notpriv
|
- notpriv
|
||||||
|
|
||||||
snyk:
|
snyk:
|
||||||
|
image: registry.gitlab.com/hosttoday/ht-docker-node:snyk
|
||||||
stage: security
|
stage: security
|
||||||
script:
|
script:
|
||||||
- npmci npm prepare
|
- npmci npm prepare
|
||||||
- npmci command npm install -g snyk
|
|
||||||
- npmci command npm install --ignore-scripts
|
- npmci command npm install --ignore-scripts
|
||||||
- npmci command snyk test
|
- npmci command snyk test
|
||||||
tags:
|
tags:
|
||||||
|
- lossless
|
||||||
- docker
|
- docker
|
||||||
- notpriv
|
- notpriv
|
||||||
|
|
||||||
@ -38,38 +40,41 @@ snyk:
|
|||||||
# test stage
|
# test stage
|
||||||
# ====================
|
# ====================
|
||||||
|
|
||||||
testLTS:
|
testStable:
|
||||||
stage: test
|
stage: test
|
||||||
script:
|
script:
|
||||||
- npmci npm prepare
|
- npmci npm prepare
|
||||||
- npmci node install lts
|
- npmci node install stable
|
||||||
- npmci npm install
|
- npmci npm install
|
||||||
- npmci npm test
|
- npmci npm test
|
||||||
coverage: /\d+.?\d+?\%\s*coverage/
|
coverage: /\d+.?\d+?\%\s*coverage/
|
||||||
tags:
|
tags:
|
||||||
|
- lossless
|
||||||
- docker
|
- docker
|
||||||
- notpriv
|
- priv
|
||||||
|
|
||||||
testBuild:
|
testBuild:
|
||||||
stage: test
|
stage: test
|
||||||
script:
|
script:
|
||||||
- npmci npm prepare
|
- npmci npm prepare
|
||||||
- npmci node install lts
|
- npmci node install stable
|
||||||
- npmci npm install
|
- npmci npm install
|
||||||
- npmci command npm run build
|
- npmci command npm run build
|
||||||
coverage: /\d+.?\d+?\%\s*coverage/
|
coverage: /\d+.?\d+?\%\s*coverage/
|
||||||
tags:
|
tags:
|
||||||
|
- lossless
|
||||||
- docker
|
- docker
|
||||||
- notpriv
|
- notpriv
|
||||||
|
|
||||||
release:
|
release:
|
||||||
stage: release
|
stage: release
|
||||||
script:
|
script:
|
||||||
- npmci node install lts
|
- npmci node install stable
|
||||||
- npmci npm publish
|
- npmci npm publish
|
||||||
only:
|
only:
|
||||||
- tags
|
- tags
|
||||||
tags:
|
tags:
|
||||||
|
- lossless
|
||||||
- docker
|
- docker
|
||||||
- notpriv
|
- notpriv
|
||||||
|
|
||||||
@ -81,9 +86,11 @@ codequality:
|
|||||||
allow_failure: true
|
allow_failure: true
|
||||||
script:
|
script:
|
||||||
- npmci command npm install -g tslint typescript
|
- npmci command npm install -g tslint typescript
|
||||||
|
- npmci npm prepare
|
||||||
- npmci npm install
|
- npmci npm install
|
||||||
- npmci command "tslint -c tslint.json ./ts/**/*.ts"
|
- npmci command "tslint -c tslint.json ./ts/**/*.ts"
|
||||||
tags:
|
tags:
|
||||||
|
- lossless
|
||||||
- docker
|
- docker
|
||||||
- priv
|
- priv
|
||||||
|
|
||||||
@ -94,18 +101,20 @@ trigger:
|
|||||||
only:
|
only:
|
||||||
- tags
|
- tags
|
||||||
tags:
|
tags:
|
||||||
|
- lossless
|
||||||
- docker
|
- docker
|
||||||
- notpriv
|
- notpriv
|
||||||
|
|
||||||
pages:
|
pages:
|
||||||
image: hosttoday/ht-docker-node:npmci
|
|
||||||
stage: metadata
|
stage: metadata
|
||||||
script:
|
script:
|
||||||
|
- npmci node install lts
|
||||||
- npmci command npm install -g @gitzone/tsdoc
|
- npmci command npm install -g @gitzone/tsdoc
|
||||||
- npmci npm prepare
|
- npmci npm prepare
|
||||||
- npmci npm install
|
- npmci npm install
|
||||||
- npmci command tsdoc
|
- npmci command tsdoc
|
||||||
tags:
|
tags:
|
||||||
|
- lossless
|
||||||
- docker
|
- docker
|
||||||
- notpriv
|
- notpriv
|
||||||
only:
|
only:
|
||||||
|
29
.vscode/launch.json
vendored
Normal file
29
.vscode/launch.json
vendored
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
{
|
||||||
|
"version": "0.2.0",
|
||||||
|
"configurations": [
|
||||||
|
{
|
||||||
|
"name": "current file",
|
||||||
|
"type": "node",
|
||||||
|
"request": "launch",
|
||||||
|
"args": [
|
||||||
|
"${relativeFile}"
|
||||||
|
],
|
||||||
|
"runtimeArgs": ["-r", "@gitzone/tsrun"],
|
||||||
|
"cwd": "${workspaceRoot}",
|
||||||
|
"protocol": "inspector",
|
||||||
|
"internalConsoleOptions": "openOnSessionStart"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "test.ts",
|
||||||
|
"type": "node",
|
||||||
|
"request": "launch",
|
||||||
|
"args": [
|
||||||
|
"test/test.ts"
|
||||||
|
],
|
||||||
|
"runtimeArgs": ["-r", "@gitzone/tsrun"],
|
||||||
|
"cwd": "${workspaceRoot}",
|
||||||
|
"protocol": "inspector",
|
||||||
|
"internalConsoleOptions": "openOnSessionStart"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
26
.vscode/settings.json
vendored
Normal file
26
.vscode/settings.json
vendored
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
{
|
||||||
|
"json.schemas": [
|
||||||
|
{
|
||||||
|
"fileMatch": ["/npmextra.json"],
|
||||||
|
"schema": {
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"npmci": {
|
||||||
|
"type": "object",
|
||||||
|
"description": "settings for npmci"
|
||||||
|
},
|
||||||
|
"gitzone": {
|
||||||
|
"type": "object",
|
||||||
|
"description": "settings for gitzone",
|
||||||
|
"properties": {
|
||||||
|
"projectType": {
|
||||||
|
"type": "string",
|
||||||
|
"enum": ["website", "element", "service", "npm"]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
50
README.md
50
README.md
@ -1,50 +0,0 @@
|
|||||||
# @mojoio/cloudflare
|
|
||||||
easy cloudflare management
|
|
||||||
|
|
||||||
## Availabililty and Links
|
|
||||||
* [npmjs.org (npm package)](https://www.npmjs.com/package/@mojoio/cloudflare)
|
|
||||||
* [gitlab.com (source)](https://gitlab.com/mojoio/cloudflare)
|
|
||||||
* [github.com (source mirror)](https://github.com/mojoio/cloudflare)
|
|
||||||
* [docs (typedoc)](https://mojoio.gitlab.io/cloudflare/)
|
|
||||||
|
|
||||||
## Status for master
|
|
||||||
[](https://gitlab.com/mojoio/cloudflare/commits/master)
|
|
||||||
[](https://gitlab.com/mojoio/cloudflare/commits/master)
|
|
||||||
[](https://www.npmjs.com/package/@mojoio/cloudflare)
|
|
||||||
[](https://snyk.io/test/npm/@mojoio/cloudflare)
|
|
||||||
[](https://nodejs.org/dist/latest-v10.x/docs/api/)
|
|
||||||
[](https://nodejs.org/dist/latest-v10.x/docs/api/)
|
|
||||||
[](https://prettier.io/)
|
|
||||||
|
|
||||||
## Usage
|
|
||||||
|
|
||||||
Use TypeScript for best in class instellisense.
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
import * as cflare from 'cflare'
|
|
||||||
|
|
||||||
let myCflareAccount = new cflare.CflareAccount()
|
|
||||||
testCflareAccount.auth({
|
|
||||||
email: 'someuser@example.com',
|
|
||||||
key: 'someLongApiKey'
|
|
||||||
})
|
|
||||||
|
|
||||||
let myAsyncCflareManagement = async () => {
|
|
||||||
// get things
|
|
||||||
let myZones = await myCflareAccount.listZones() // zones are fully typed
|
|
||||||
let myIdForADomain = await myCflareAccount.getZoneId('example.com') // type number
|
|
||||||
let myRecordsForADomain = await myCflareAccount.listRecords('example.com') // records are fully typed
|
|
||||||
|
|
||||||
// set things
|
|
||||||
myCflareAccount.updateRecord(...)
|
|
||||||
myCflareAccount.createRecord(...)
|
|
||||||
myCflareAccount.deleteRecord(...)
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
For further information read the linked docs at the top of this readme.
|
|
||||||
|
|
||||||
> MIT licensed | **©** [Lossless GmbH](https://lossless.gmbh)
|
|
||||||
| By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy.html)
|
|
||||||
|
|
||||||
[](https://maintainedby.lossless.com)
|
|
@ -4,6 +4,7 @@
|
|||||||
"npmAccessLevel": "public"
|
"npmAccessLevel": "public"
|
||||||
},
|
},
|
||||||
"gitzone": {
|
"gitzone": {
|
||||||
|
"projectType": "npm",
|
||||||
"module": {
|
"module": {
|
||||||
"githost": "gitlab.com",
|
"githost": "gitlab.com",
|
||||||
"gitscope": "mojoio",
|
"gitscope": "mojoio",
|
||||||
|
838
package-lock.json
generated
838
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
38
package.json
38
package.json
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@mojoio/cloudflare",
|
"name": "@mojoio/cloudflare",
|
||||||
"version": "3.0.0",
|
"version": "3.0.6",
|
||||||
"private": false,
|
"private": false,
|
||||||
"description": "easy cloudflare management",
|
"description": "easy cloudflare management",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
@ -24,30 +24,30 @@
|
|||||||
},
|
},
|
||||||
"homepage": "https://gitlab.com/pushrocks/cflare#readme",
|
"homepage": "https://gitlab.com/pushrocks/cflare#readme",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@pushrocks/smartdelay": "^2.0.3",
|
"@pushrocks/smartdelay": "^2.0.6",
|
||||||
"@pushrocks/smartlog": "^2.0.19",
|
"@pushrocks/smartlog": "^2.0.21",
|
||||||
"@pushrocks/smartpromise": "^3.0.2",
|
"@pushrocks/smartpromise": "^3.0.6",
|
||||||
"@pushrocks/smartrequest": "^1.1.16",
|
"@pushrocks/smartrequest": "^1.1.47",
|
||||||
"@pushrocks/smartstring": "^3.0.10",
|
"@pushrocks/smartstring": "^3.0.18",
|
||||||
"@tsclass/tsclass": "^2.0.1"
|
"@tsclass/tsclass": "^3.0.4"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@gitzone/tsbuild": "^2.1.11",
|
"@gitzone/tsbuild": "^2.1.17",
|
||||||
"@gitzone/tsrun": "^1.2.8",
|
"@gitzone/tsrun": "^1.2.8",
|
||||||
"@gitzone/tstest": "^1.0.24",
|
"@gitzone/tstest": "^1.0.28",
|
||||||
"@pushrocks/qenv": "^4.0.0",
|
"@pushrocks/qenv": "^4.0.6",
|
||||||
"@pushrocks/tapbundle": "^3.0.11",
|
"@pushrocks/tapbundle": "^3.2.0",
|
||||||
"@types/node": "^12.6.6",
|
"@types/node": "^13.7.0",
|
||||||
"tslint": "^5.18.0",
|
"tslint": "^6.0.0",
|
||||||
"tslint-config-prettier": "^1.18.0"
|
"tslint-config-prettier": "^1.18.0"
|
||||||
},
|
},
|
||||||
"files": [
|
"files": [
|
||||||
"ts/*",
|
"ts/**/*",
|
||||||
"ts_web/*",
|
"ts_web/**/*",
|
||||||
"dist/*",
|
"dist/**/*",
|
||||||
"dist_web/*",
|
"dist_web/**/*",
|
||||||
"dist_ts_web/*",
|
"dist_ts_web/**/*",
|
||||||
"assets/*",
|
"assets/**/*",
|
||||||
"cli.js",
|
"cli.js",
|
||||||
"npmextra.json",
|
"npmextra.json",
|
||||||
"readme.md"
|
"readme.md"
|
||||||
|
23
test/test.ts
23
test/test.ts
@ -17,9 +17,7 @@ tap.test('should create a valid instance of CloudflareAccount', async () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
tap.test('.listZones() -> should display an entire account', async tools => {
|
||||||
|
|
||||||
tap.skip.test('.listZones() -> should display an entire account', async tools => {
|
|
||||||
tools.timeout(600000);
|
tools.timeout(600000);
|
||||||
const result = await testCloudflareAccount.listZones();
|
const result = await testCloudflareAccount.listZones();
|
||||||
console.log(result);
|
console.log(result);
|
||||||
@ -50,7 +48,9 @@ tap.test('should create a valid record for a subdomain', async tools => {
|
|||||||
|
|
||||||
tap.test('should get a record from Cloudflare', async tools => {
|
tap.test('should get a record from Cloudflare', async tools => {
|
||||||
tools.timeout(600000);
|
tools.timeout(600000);
|
||||||
await testCloudflareAccount.getRecord(`${randomPrefix}subdomain.bleu.de`, 'A').then(responseArg => {
|
await testCloudflareAccount
|
||||||
|
.getRecord(`${randomPrefix}subdomain.bleu.de`, 'A')
|
||||||
|
.then(responseArg => {
|
||||||
console.log(responseArg);
|
console.log(responseArg);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@ -70,11 +70,22 @@ tap.test('.purge(some.domain) -> should purge everything', async () => {
|
|||||||
|
|
||||||
// WORKERS
|
// WORKERS
|
||||||
tap.test('should create a worker', async () => {
|
tap.test('should create a worker', async () => {
|
||||||
await testCloudflareAccount.workerManager.createWorker('myawesomescript', `addEventListener('fetch', event => { event.respondWith(fetch(event.request)) })`);
|
const worker = await testCloudflareAccount.workerManager.createWorker(
|
||||||
|
'myawesomescript',
|
||||||
|
`addEventListener('fetch', event => { event.respondWith(fetch(event.request)) })`
|
||||||
|
);
|
||||||
|
await worker.setRoutes([
|
||||||
|
{
|
||||||
|
zoneName: 'bleu.de',
|
||||||
|
pattern: 'https://*bleu.de/hello'
|
||||||
|
}
|
||||||
|
]);
|
||||||
|
console.log(worker);
|
||||||
});
|
});
|
||||||
|
|
||||||
tap.test('should get workers', async () => {
|
tap.test('should get workers', async () => {
|
||||||
await testCloudflareAccount.workerManager.listWorkers();
|
const workerArray = await testCloudflareAccount.workerManager.listWorkers();
|
||||||
|
console.log(workerArray);
|
||||||
});
|
});
|
||||||
|
|
||||||
tap.start();
|
tap.start();
|
||||||
|
@ -1,8 +1,7 @@
|
|||||||
import plugins = require('./cloudflare.plugins');
|
import plugins = require('./cloudflare.plugins');
|
||||||
import * as interfaces from './interfaces/cloudflare.interfaces';
|
import * as interfaces from './interfaces';
|
||||||
|
|
||||||
// interfaces
|
// interfaces
|
||||||
import { TDnsRecord } from '@tsclass/tsclass';
|
|
||||||
import { WorkerManager } from './cloudflare.classes.workermanager';
|
import { WorkerManager } from './cloudflare.classes.workermanager';
|
||||||
import { ZoneManager } from './cloudflare.classes.zonemanager';
|
import { ZoneManager } from './cloudflare.classes.zonemanager';
|
||||||
|
|
||||||
@ -24,10 +23,12 @@ export class CloudflareAccount {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public async getAccountIdentifier() {
|
public async getAccountIdentifier() {
|
||||||
|
if (!this.accountIdentifier) {
|
||||||
const route = `/accounts?page=1&per_page=20&direction=desc`;
|
const route = `/accounts?page=1&per_page=20&direction=desc`;
|
||||||
const response: any = await this.request('GET', route);
|
const response: any = await this.request('GET', route);
|
||||||
this.accountIdentifier = response.result[0].id;
|
this.accountIdentifier = response.result[0].id;
|
||||||
console.log('Account identifier is: ' + this.accountIdentifier);
|
// console.log('Account identifier is: ' + this.accountIdentifier);
|
||||||
|
}
|
||||||
return this.accountIdentifier;
|
return this.accountIdentifier;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -59,7 +60,7 @@ export class CloudflareAccount {
|
|||||||
*/
|
*/
|
||||||
public async getRecord(
|
public async getRecord(
|
||||||
domainNameArg: string,
|
domainNameArg: string,
|
||||||
typeArg: TDnsRecord
|
typeArg: plugins.tsclass.network.TDnsRecord
|
||||||
): Promise<interfaces.ICflareRecord> {
|
): Promise<interfaces.ICflareRecord> {
|
||||||
const domain = new plugins.smartstring.Domain(domainNameArg);
|
const domain = new plugins.smartstring.Domain(domainNameArg);
|
||||||
const recordArrayArg = await this.listRecords(domain.zoneName);
|
const recordArrayArg = await this.listRecords(domain.zoneName);
|
||||||
@ -71,7 +72,7 @@ export class CloudflareAccount {
|
|||||||
|
|
||||||
public async createRecord(
|
public async createRecord(
|
||||||
domainNameArg: string,
|
domainNameArg: string,
|
||||||
typeArg: TDnsRecord,
|
typeArg: plugins.tsclass.network.TDnsRecord,
|
||||||
contentArg: string
|
contentArg: string
|
||||||
): Promise<any> {
|
): Promise<any> {
|
||||||
const domain = new plugins.smartstring.Domain(domainNameArg);
|
const domain = new plugins.smartstring.Domain(domainNameArg);
|
||||||
@ -94,7 +95,10 @@ export class CloudflareAccount {
|
|||||||
* @param domainNameArg
|
* @param domainNameArg
|
||||||
* @param typeArg
|
* @param typeArg
|
||||||
*/
|
*/
|
||||||
public async removeRecord(domainNameArg: string, typeArg: TDnsRecord): Promise<any> {
|
public async removeRecord(
|
||||||
|
domainNameArg: string,
|
||||||
|
typeArg: plugins.tsclass.network.TDnsRecord
|
||||||
|
): Promise<any> {
|
||||||
const domain = new plugins.smartstring.Domain(domainNameArg);
|
const domain = new plugins.smartstring.Domain(domainNameArg);
|
||||||
const cflareRecord = await this.getRecord(domain.fullName, typeArg);
|
const cflareRecord = await this.getRecord(domain.fullName, typeArg);
|
||||||
if (cflareRecord) {
|
if (cflareRecord) {
|
||||||
@ -161,7 +165,12 @@ export class CloudflareAccount {
|
|||||||
const respone = await this.request('DELETE', requestUrl, payload);
|
const respone = await this.request('DELETE', requestUrl, payload);
|
||||||
}
|
}
|
||||||
|
|
||||||
public request(methodArg: string, routeArg: string, dataArg: any = {}, requestHeadersArg = {}): Promise<any> {
|
public request(
|
||||||
|
methodArg: string,
|
||||||
|
routeArg: string,
|
||||||
|
dataArg: any = {},
|
||||||
|
requestHeadersArg = {}
|
||||||
|
): Promise<any> {
|
||||||
const done = plugins.smartpromise.defer();
|
const done = plugins.smartpromise.defer();
|
||||||
const options: plugins.smartrequest.ISmartRequestOptions = {
|
const options: plugins.smartrequest.ISmartRequestOptions = {
|
||||||
method: methodArg,
|
method: methodArg,
|
||||||
@ -172,7 +181,7 @@ export class CloudflareAccount {
|
|||||||
'Content-Length': Buffer.byteLength(JSON.stringify(dataArg)),
|
'Content-Length': Buffer.byteLength(JSON.stringify(dataArg)),
|
||||||
...requestHeadersArg
|
...requestHeadersArg
|
||||||
},
|
},
|
||||||
requestBody: dataArg,
|
requestBody: dataArg
|
||||||
};
|
};
|
||||||
|
|
||||||
// console.log(options);
|
// console.log(options);
|
||||||
@ -214,4 +223,6 @@ export class CloudflareAccount {
|
|||||||
private authCheck() {
|
private authCheck() {
|
||||||
return this.authEmail && this.authKey; // check if auth is available
|
return this.authEmail && this.authKey; // check if auth is available
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// acme convenience functions
|
||||||
}
|
}
|
||||||
|
@ -1,26 +1,92 @@
|
|||||||
|
import * as plugins from './cloudflare.plugins';
|
||||||
|
import * as interfaces from './interfaces';
|
||||||
import { WorkerManager } from './cloudflare.classes.workermanager';
|
import { WorkerManager } from './cloudflare.classes.workermanager';
|
||||||
|
|
||||||
export class Worker {
|
export interface IWorkerRoute extends interfaces.ICflareWorkerRoute {
|
||||||
|
zoneName: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export class CloudflareWorker {
|
||||||
// STATIC
|
// STATIC
|
||||||
public static async fromApiObject(workerManager: WorkerManager, apiObject): Promise<Worker> {
|
public static async fromApiObject(
|
||||||
console.log(apiObject);
|
workerManager: WorkerManager,
|
||||||
return new Worker(workerManager);
|
apiObject
|
||||||
|
): Promise<CloudflareWorker> {
|
||||||
|
const newWorker = new CloudflareWorker(workerManager);
|
||||||
|
Object.assign(newWorker, apiObject.result);
|
||||||
|
await newWorker.getRoutes();
|
||||||
|
return newWorker;
|
||||||
}
|
}
|
||||||
|
|
||||||
// INSTANCE
|
// INSTANCE
|
||||||
private workerManager: WorkerManager;
|
private workerManager: WorkerManager;
|
||||||
|
|
||||||
|
public script: string;
|
||||||
public id: string;
|
public id: string;
|
||||||
public etag: string;
|
public etag: string;
|
||||||
public createdOn: string;
|
// tslint:disable-next-line: variable-name
|
||||||
public modifiedOn: string;
|
public created_on: string;
|
||||||
|
// tslint:disable-next-line: variable-name
|
||||||
|
public modified_on: string;
|
||||||
|
|
||||||
public routes: string[] = [];
|
public routes: IWorkerRoute[] = [];
|
||||||
constructor(workerManagerArg: WorkerManager) {
|
constructor(workerManagerArg: WorkerManager) {
|
||||||
this.workerManager = workerManagerArg;
|
this.workerManager = workerManagerArg;
|
||||||
}
|
}
|
||||||
|
|
||||||
public setRoutes(routeArray: string[]) {
|
/**
|
||||||
|
* gets all routes for a worker
|
||||||
|
*/
|
||||||
|
public async getRoutes() {
|
||||||
|
const zones = await this.workerManager.cfAccount.listZones();
|
||||||
|
for (const zone of zones) {
|
||||||
|
const requestRoute = `/zones/${zone.id}/workers/routes`;
|
||||||
|
const response: {
|
||||||
|
result: interfaces.ICflareWorkerRoute[];
|
||||||
|
} = await this.workerManager.cfAccount.request('GET', requestRoute);
|
||||||
|
for (const route of response.result) {
|
||||||
|
console.log('hey');
|
||||||
|
console.log(route);
|
||||||
|
console.log(this.id);
|
||||||
|
if (route.script === this.id) {
|
||||||
|
this.routes.push({ ...route, zoneName: zone.name });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public async setRoutes(routeArray: Array<{ zoneName: string; pattern: string }>) {
|
||||||
|
for (const newRoute of routeArray) {
|
||||||
|
// lets determine wether a route is new, needs an update or already up to date.
|
||||||
|
let routeStatus: 'new' | 'needsUpdate' | 'alreadyUpToDate' = 'new';
|
||||||
|
let routeIdForUpdate: string;
|
||||||
|
for (const existingRoute of this.routes) {
|
||||||
|
if (existingRoute.pattern === newRoute.pattern) {
|
||||||
|
routeStatus = 'needsUpdate';
|
||||||
|
routeIdForUpdate = existingRoute.id;
|
||||||
|
if (existingRoute.script === this.id) {
|
||||||
|
routeStatus = 'alreadyUpToDate';
|
||||||
|
plugins.smartlog.defaultLogger.log('info', `route already exists, no update needed`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// lets care about actually setting routes
|
||||||
|
if (routeStatus === 'new') {
|
||||||
|
const zoneId = await this.workerManager.cfAccount.getZoneId(newRoute.zoneName);
|
||||||
|
const requestRoute = `/zones/${zoneId}/workers/routes`;
|
||||||
|
await this.workerManager.cfAccount.request('POST', requestRoute, {
|
||||||
|
pattern: newRoute.pattern,
|
||||||
|
script: this.id
|
||||||
|
});
|
||||||
|
} else if (routeStatus === 'needsUpdate') {
|
||||||
|
const zoneId = await this.workerManager.cfAccount.getZoneId(newRoute.zoneName);
|
||||||
|
const requestRoute = `/zones/${zoneId}/workers/routes/${routeIdForUpdate}`;
|
||||||
|
await this.workerManager.cfAccount.request('PUT', requestRoute, {
|
||||||
|
pattern: newRoute.pattern,
|
||||||
|
script: this.id
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,22 +1,22 @@
|
|||||||
import * as plugins from './cloudflare.plugins';
|
import * as plugins from './cloudflare.plugins';
|
||||||
import { CloudflareAccount } from './cloudflare.classes.account';
|
import { CloudflareAccount } from './cloudflare.classes.account';
|
||||||
import { Worker } from './cloudflare.classes.worker';
|
import { CloudflareWorker } from './cloudflare.classes.worker';
|
||||||
|
|
||||||
export class WorkerManager {
|
export class WorkerManager {
|
||||||
private cfAccount: CloudflareAccount;
|
public cfAccount: CloudflareAccount;
|
||||||
|
|
||||||
constructor(cfAccountArg: CloudflareAccount) {
|
constructor(cfAccountArg: CloudflareAccount) {
|
||||||
this.cfAccount = cfAccountArg;
|
this.cfAccount = cfAccountArg;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async createWorker(workerName: string, workerScript: string): Promise<Worker> {
|
public async createWorker(workerName: string, workerScript: string): Promise<CloudflareWorker> {
|
||||||
const accountIdentifier = await this.cfAccount.getAccountIdentifier();
|
const accountIdentifier = await this.cfAccount.getAccountIdentifier();
|
||||||
const route = `/accounts/${accountIdentifier}/workers/scripts/${workerName}`;
|
const route = `/accounts/${accountIdentifier}/workers/scripts/${workerName}`;
|
||||||
const responseBody = await this.cfAccount.request('PUT', route, workerScript, {
|
const responseBody = await this.cfAccount.request('PUT', route, workerScript, {
|
||||||
'Content-Type': 'application/javascript',
|
'Content-Type': 'application/javascript',
|
||||||
'Content-Length': Buffer.byteLength(workerScript)
|
'Content-Length': Buffer.byteLength(workerScript)
|
||||||
});
|
});
|
||||||
return Worker.fromApiObject(this, responseBody);
|
return CloudflareWorker.fromApiObject(this, responseBody);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -26,6 +26,11 @@ export class WorkerManager {
|
|||||||
const accountIdentifier = await this.cfAccount.getAccountIdentifier();
|
const accountIdentifier = await this.cfAccount.getAccountIdentifier();
|
||||||
const route = `/accounts/${accountIdentifier}/workers/scripts`;
|
const route = `/accounts/${accountIdentifier}/workers/scripts`;
|
||||||
const response = await this.cfAccount.request('GET', route);
|
const response = await this.cfAccount.request('GET', route);
|
||||||
console.log(response);
|
const results = response.result;
|
||||||
|
const workers: CloudflareWorker[] = [];
|
||||||
|
for (const apiObject of results) {
|
||||||
|
workers.push(await CloudflareWorker.fromApiObject(this, apiObject));
|
||||||
|
}
|
||||||
|
return workers;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
6
ts/cloudflare.classes.zone.ts
Normal file
6
ts/cloudflare.classes.zone.ts
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
import * as plugins from './cloudflare.plugins';
|
||||||
|
import * as interfaces from './interfaces';
|
||||||
|
|
||||||
|
export class CloudflareZone {
|
||||||
|
// TODO
|
||||||
|
}
|
@ -2,7 +2,7 @@ import * as plugins from './cloudflare.plugins';
|
|||||||
import { CloudflareAccount } from './cloudflare.classes.account';
|
import { CloudflareAccount } from './cloudflare.classes.account';
|
||||||
|
|
||||||
export class ZoneManager {
|
export class ZoneManager {
|
||||||
private cfAccount: CloudflareAccount;
|
public cfAccount: CloudflareAccount;
|
||||||
public zoneName: string;
|
public zoneName: string;
|
||||||
|
|
||||||
constructor(cfAccountArg: CloudflareAccount) {
|
constructor(cfAccountArg: CloudflareAccount) {
|
||||||
|
@ -1 +1,2 @@
|
|||||||
export { CloudflareAccount } from './cloudflare.classes.account';
|
export { CloudflareAccount } from './cloudflare.classes.account';
|
||||||
|
export { CloudflareWorker } from './cloudflare.classes.worker';
|
||||||
|
15
ts/interfaces/cloudflare.api.record.ts
Normal file
15
ts/interfaces/cloudflare.api.record.ts
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
export interface ICflareRecord {
|
||||||
|
id: string;
|
||||||
|
type: string;
|
||||||
|
name: string;
|
||||||
|
content: string;
|
||||||
|
proxiable: boolean;
|
||||||
|
proxied: boolean;
|
||||||
|
ttl: number;
|
||||||
|
locked: boolean;
|
||||||
|
zone_id: string;
|
||||||
|
zone_name: string;
|
||||||
|
created_on: string;
|
||||||
|
modified_on: string;
|
||||||
|
data: any;
|
||||||
|
}
|
5
ts/interfaces/cloudflare.api.workerroute.ts
Normal file
5
ts/interfaces/cloudflare.api.workerroute.ts
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
export interface ICflareWorkerRoute {
|
||||||
|
id: string;
|
||||||
|
pattern: string;
|
||||||
|
script: string;
|
||||||
|
}
|
@ -1,5 +1,3 @@
|
|||||||
import * as plugins from '../cloudflare.plugins';
|
|
||||||
|
|
||||||
export interface ICflareZone {
|
export interface ICflareZone {
|
||||||
id: string;
|
id: string;
|
||||||
name: string;
|
name: string;
|
||||||
@ -41,19 +39,3 @@ export interface ICflareZone {
|
|||||||
type: string;
|
type: string;
|
||||||
checked_on: string;
|
checked_on: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface ICflareRecord {
|
|
||||||
id: string;
|
|
||||||
type: string;
|
|
||||||
name: string;
|
|
||||||
content: string;
|
|
||||||
proxiable: boolean;
|
|
||||||
proxied: boolean;
|
|
||||||
ttl: number;
|
|
||||||
locked: boolean;
|
|
||||||
zone_id: string;
|
|
||||||
zone_name: string;
|
|
||||||
created_on: string;
|
|
||||||
modified_on: string;
|
|
||||||
data: any;
|
|
||||||
}
|
|
@ -1 +1,3 @@
|
|||||||
export * from './cloudflare.interfaces';
|
export * from './cloudflare.api.record';
|
||||||
|
export * from './cloudflare.api.zone';
|
||||||
|
export * from './cloudflare.api.workerroute';
|
||||||
|
@ -3,7 +3,6 @@
|
|||||||
"rules": {
|
"rules": {
|
||||||
"semicolon": [true, "always"],
|
"semicolon": [true, "always"],
|
||||||
"no-console": false,
|
"no-console": false,
|
||||||
"no-return-await": false,
|
|
||||||
"ordered-imports": false,
|
"ordered-imports": false,
|
||||||
"object-literal-sort-keys": false,
|
"object-literal-sort-keys": false,
|
||||||
"member-ordering": {
|
"member-ordering": {
|
||||||
|
Reference in New Issue
Block a user