refactor: 重构优化

This commit is contained in:
xiaojunnuo
2021-02-04 18:44:16 +08:00
parent a39dac4dbd
commit a25a15ca6e
59 changed files with 3903 additions and 967 deletions
@@ -0,0 +1,2 @@
import { Registry } from '../registry/registry.js'
export const accessProviderRegistry = new Registry()
@@ -1,8 +1,9 @@
import _ from 'lodash-es'
import logger from '../utils/util.log.js'
export class AbstractDnsProvider {
constructor () {
constructor ({ accessProviders }) {
this.logger = logger
this.accessProviders = accessProviders
}
async createRecord ({ fullRecord, type, value }) {
@@ -31,4 +32,11 @@ export class AbstractDnsProvider {
}
return domain
}
getAccessProvider (accessProvider, accessProviders = this.accessProviders) {
if (typeof accessProvider === 'string' && accessProviders) {
accessProvider = accessProviders[accessProvider]
}
return accessProvider
}
}
+3 -2
View File
@@ -1,2 +1,3 @@
export { providerRegistry } from './provider-registry.js'
export { AbstractDnsProvider } from './abstract-provider.js'
import { Registry } from '../registry/registry.js'
export { AbstractDnsProvider } from './abstract-dns-provider.js'
export const dnsProviderRegistry = new Registry()
@@ -1,26 +0,0 @@
export class ProviderRegistry {
constructor () {
this.providers = {}
}
install (provider) {
if (provider == null) {
return
}
if (this.providers == null) {
this.providers = {}
}
const name = provider.name || (provider.define && provider.define.name)
this.providers[name] = provider
}
get (name) {
if (name) {
return this.providers[name]
}
throw new Error(`找不到授权提供者:${name}`)
}
}
export const providerRegistry = new ProviderRegistry()
+1
View File
@@ -1,4 +1,5 @@
export * from './dns-provider/index.js'
export * from './plugin/index.js'
export * from './access-provider/index.js'
export { Store } from './store/store.js'
export { util } from './utils/index.js'
+1 -3
View File
@@ -3,8 +3,6 @@ import logger from '../utils/util.log.js'
import dayjs from 'dayjs'
import Sleep from '../utils/util.sleep.js'
import { pluginRegistry } from './plugin-registry.js'
export class AbstractPlugin {
constructor ({ accessProviders }) {
this.logger = logger
@@ -55,7 +53,7 @@ export class AbstractPlugin {
}
/**
* 回退,如有必要
* 回退,用于单元测试
* @param options
*/
async rollback (options) {
+2 -1
View File
@@ -1,2 +1,3 @@
export { pluginRegistry } from './plugin-registry.js'
import { Registry } from '../registry/registry.js'
export { AbstractPlugin } from './abstract-plugin.js'
export const pluginRegistry = new Registry()
@@ -1,27 +0,0 @@
export class PluginRegistry {
constructor () {
this.plugins = {}
}
install (plugin) {
if (plugin == null) {
return
}
if (this.plugins == null) {
this.plugins = {}
}
const name = plugin.name || (plugin.define && plugin.define.name)
this.plugins[name] = plugin
}
get (name) {
if (name) {
return this.plugins[name]
}
throw new Error(`找不到${name}插件`)
}
}
export const pluginRegistry = new PluginRegistry()
+34
View File
@@ -0,0 +1,34 @@
export class Registry {
constructor () {
this.collection = {}
}
install (target) {
if (target == null) {
return
}
if (this.collection == null) {
this.collection = {}
}
const className = target.name
this.register(className, target)
const defineName = target.define && target.define().name
this.register(defineName, target)
}
register (key, value) {
if (!key || value == null) {
return
}
this.collection[key] = value
}
get (name) {
if (name) {
return this.collection[name]
}
throw new Error(`${name} not found`)
}
}