@backstage/backend-defaults 0.4.4 → 0.5.0-next.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +143 -9
- package/auth/package.json +1 -1
- package/cache/package.json +1 -1
- package/config.d.ts +221 -0
- package/database/package.json +1 -1
- package/discovery/package.json +1 -1
- package/dist/auth.cjs.js +17 -45
- package/dist/auth.cjs.js.map +1 -1
- package/dist/auth.d.ts +1 -1
- package/dist/cache.cjs.js +28 -17
- package/dist/cache.cjs.js.map +1 -1
- package/dist/cache.d.ts +5 -12
- package/dist/database.cjs.js +18 -65
- package/dist/database.cjs.js.map +1 -1
- package/dist/database.d.ts +7 -23
- package/dist/discovery.cjs.js +4 -3
- package/dist/discovery.cjs.js.map +1 -1
- package/dist/discovery.d.ts +5 -7
- package/dist/httpAuth.d.ts +1 -1
- package/dist/httpRouter.d.ts +1 -1
- package/dist/index.cjs.js +118 -2
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.d.ts +22 -1
- package/dist/lifecycle.d.ts +1 -1
- package/dist/logger.d.ts +1 -1
- package/dist/permissions.cjs.js +3 -5
- package/dist/permissions.cjs.js.map +1 -1
- package/dist/permissions.d.ts +1 -1
- package/dist/rootConfig.cjs.js +1 -1
- package/dist/rootConfig.cjs.js.map +1 -1
- package/dist/rootHealth.d.ts +1 -1
- package/dist/rootHttpRouter.cjs.js +1 -1
- package/dist/rootHttpRouter.cjs.js.map +1 -1
- package/dist/rootLifecycle.d.ts +1 -1
- package/dist/rootLogger.d.ts +1 -1
- package/dist/scheduler.cjs.js +20 -5
- package/dist/scheduler.cjs.js.map +1 -1
- package/dist/scheduler.d.ts +1 -1
- package/dist/urlReader.cjs.js +2 -1
- package/dist/urlReader.cjs.js.map +1 -1
- package/dist/urlReader.d.ts +4 -5
- package/dist/userInfo.d.ts +1 -1
- package/httpAuth/package.json +1 -1
- package/httpRouter/package.json +1 -1
- package/lifecycle/package.json +1 -1
- package/logger/package.json +1 -1
- package/package.json +11 -10
- package/permissions/package.json +1 -1
- package/rootConfig/package.json +1 -1
- package/rootHealth/package.json +1 -1
- package/rootHttpRouter/package.json +1 -1
- package/rootLifecycle/package.json +1 -1
- package/rootLogger/package.json +1 -1
- package/scheduler/package.json +1 -1
- package/urlReader/package.json +1 -1
- package/userInfo/package.json +1 -1
package/dist/cache.cjs.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cache.cjs.js","sources":["../src/entrypoints/cache/CacheClient.ts","../src/entrypoints/cache/CacheManager.ts","../src/entrypoints/cache/cacheServiceFactory.ts"],"sourcesContent":["/*\n * Copyright 2021 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport {\n CacheService,\n CacheServiceOptions,\n CacheServiceSetOptions,\n} from '@backstage/backend-plugin-api';\nimport { JsonValue } from '@backstage/types';\nimport { createHash } from 'crypto';\nimport Keyv from 'keyv';\n\nexport type CacheClientFactory = (options: CacheServiceOptions) => Keyv;\n\n/**\n * A basic, concrete implementation of the CacheService, suitable for almost\n * all uses in Backstage.\n */\nexport class DefaultCacheClient implements CacheService {\n #client: Keyv;\n #clientFactory: CacheClientFactory;\n #options: CacheServiceOptions;\n\n constructor(\n client: Keyv,\n clientFactory: CacheClientFactory,\n options: CacheServiceOptions,\n ) {\n this.#client = client;\n this.#clientFactory = clientFactory;\n this.#options = options;\n }\n\n async get<TValue extends JsonValue>(\n key: string,\n ): Promise<TValue | undefined> {\n const k = this.getNormalizedKey(key);\n const value = await this.#client.get(k);\n return value as TValue | undefined;\n }\n\n async set(\n key: string,\n value: JsonValue,\n opts: CacheServiceSetOptions = {},\n ): Promise<void> {\n const k = this.getNormalizedKey(key);\n await this.#client.set(k, value, opts.ttl);\n }\n\n async delete(key: string): Promise<void> {\n const k = this.getNormalizedKey(key);\n await this.#client.delete(k);\n }\n\n withOptions(options: CacheServiceOptions): CacheService {\n const newOptions = { ...this.#options, ...options };\n return new DefaultCacheClient(\n this.#clientFactory(newOptions),\n this.#clientFactory,\n newOptions,\n );\n }\n\n /**\n * Ensures keys are well-formed for any/all cache stores.\n */\n private getNormalizedKey(candidateKey: string): string {\n // Remove potentially invalid characters.\n const wellFormedKey = Buffer.from(candidateKey).toString('base64');\n\n // Memcache in particular doesn't do well with keys > 250 bytes.\n // Padded because a plugin ID is also prepended to the key.\n if (wellFormedKey.length < 200) {\n return wellFormedKey;\n }\n\n return createHash('sha256').update(candidateKey).digest('base64');\n }\n}\n","/*\n * Copyright 2021 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport {\n CacheServiceOptions,\n LoggerService,\n} from '@backstage/backend-plugin-api';\nimport { Config } from '@backstage/config';\nimport Keyv from 'keyv';\nimport { DefaultCacheClient } from './CacheClient';\nimport { CacheManagerOptions, PluginCacheManager } from './types';\n\ntype StoreFactory = (pluginId: string, defaultTtl: number | undefined) => Keyv;\n\n/**\n * Implements a Cache Manager which will automatically create new cache clients\n * for plugins when requested. All requested cache clients are created with the\n * connection details provided.\n *\n * @public\n */\nexport class CacheManager {\n /**\n * Keys represent supported `backend.cache.store` values, mapped to factories\n * that return Keyv instances appropriate to the store.\n */\n private readonly storeFactories = {\n redis: this.createRedisStoreFactory(),\n memcache: this.createMemcacheStoreFactory(),\n memory: this.createMemoryStoreFactory(),\n };\n\n private readonly logger?: LoggerService;\n private readonly store: keyof CacheManager['storeFactories'];\n private readonly connection: string;\n private readonly useRedisSets: boolean;\n private readonly errorHandler: CacheManagerOptions['onError'];\n private readonly defaultTtl?: number;\n\n /**\n * Creates a new {@link CacheManager} instance by reading from the `backend`\n * config section, specifically the `.cache` key.\n *\n * @param config - The loaded application configuration.\n */\n static fromConfig(\n config: Config,\n options: CacheManagerOptions = {},\n ): CacheManager {\n // If no `backend.cache` config is provided, instantiate the CacheManager\n // with an in-memory cache client.\n const store = config.getOptionalString('backend.cache.store') || 'memory';\n const defaultTtl = config.getOptionalNumber('backend.cache.defaultTtl');\n const connectionString =\n config.getOptionalString('backend.cache.connection') || '';\n const useRedisSets =\n config.getOptionalBoolean('backend.cache.useRedisSets') ?? true;\n const logger = options.logger?.child({\n type: 'cacheManager',\n });\n return new CacheManager(\n store,\n connectionString,\n useRedisSets,\n options.onError,\n logger,\n defaultTtl,\n );\n }\n\n /** @internal */\n constructor(\n store: string,\n connectionString: string,\n useRedisSets: boolean,\n errorHandler: CacheManagerOptions['onError'],\n logger?: LoggerService,\n defaultTtl?: number,\n ) {\n if (!this.storeFactories.hasOwnProperty(store)) {\n throw new Error(`Unknown cache store: ${store}`);\n }\n this.logger = logger;\n this.store = store as keyof CacheManager['storeFactories'];\n this.connection = connectionString;\n this.useRedisSets = useRedisSets;\n this.errorHandler = errorHandler;\n this.defaultTtl = defaultTtl;\n }\n\n /**\n * Generates a PluginCacheManager for consumption by plugins.\n *\n * @param pluginId - The plugin that the cache manager should be created for.\n * Plugin names should be unique.\n */\n forPlugin(pluginId: string): PluginCacheManager {\n return {\n getClient: (defaultOptions = {}) => {\n const clientFactory = (options: CacheServiceOptions) => {\n return this.getClientWithTtl(\n pluginId,\n options.defaultTtl ?? this.defaultTtl,\n );\n };\n\n return new DefaultCacheClient(\n clientFactory(defaultOptions),\n clientFactory,\n defaultOptions,\n );\n },\n };\n }\n\n private getClientWithTtl(pluginId: string, ttl: number | undefined): Keyv {\n return this.storeFactories[this.store](pluginId, ttl);\n }\n\n private createRedisStoreFactory(): StoreFactory {\n const KeyvRedis = require('@keyv/redis');\n let store: typeof KeyvRedis | undefined;\n return (pluginId, defaultTtl) => {\n if (!store) {\n store = new KeyvRedis(this.connection);\n // Always provide an error handler to avoid stopping the process\n store.on('error', (err: Error) => {\n this.logger?.error('Failed to create redis cache client', err);\n this.errorHandler?.(err);\n });\n }\n return new Keyv({\n namespace: pluginId,\n ttl: defaultTtl,\n store,\n emitErrors: false,\n useRedisSets: this.useRedisSets,\n });\n };\n }\n\n private createMemcacheStoreFactory(): StoreFactory {\n const KeyvMemcache = require('@keyv/memcache');\n let store: typeof KeyvMemcache | undefined;\n return (pluginId, defaultTtl) => {\n if (!store) {\n store = new KeyvMemcache(this.connection);\n // Always provide an error handler to avoid stopping the process\n store.on('error', (err: Error) => {\n this.logger?.error('Failed to create memcache cache client', err);\n this.errorHandler?.(err);\n });\n }\n return new Keyv({\n namespace: pluginId,\n ttl: defaultTtl,\n emitErrors: false,\n store,\n });\n };\n }\n\n private createMemoryStoreFactory(): StoreFactory {\n const store = new Map();\n return (pluginId, defaultTtl) =>\n new Keyv({\n namespace: pluginId,\n ttl: defaultTtl,\n emitErrors: false,\n store,\n });\n }\n}\n","/*\n * Copyright 2022 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport {\n coreServices,\n createServiceFactory,\n} from '@backstage/backend-plugin-api';\nimport { CacheManager } from './CacheManager';\n\n/**\n * Key-value store for caching data.\n *\n * See {@link @backstage/code-plugin-api#CacheService}\n * and {@link https://backstage.io/docs/backend-system/core-services/cache | the service docs}\n * for more information.\n *\n * @public\n */\nexport const cacheServiceFactory = createServiceFactory({\n service: coreServices.cache,\n deps: {\n config: coreServices.rootConfig,\n plugin: coreServices.pluginMetadata,\n logger: coreServices.rootLogger,\n },\n async createRootContext({ config, logger }) {\n return CacheManager.fromConfig(config, { logger });\n },\n async factory({ plugin }, manager) {\n return manager.forPlugin(plugin.getId()).getClient();\n },\n});\n"],"names":["createHash","Keyv","createServiceFactory","coreServices"],"mappings":";;;;;;;;;;AA+BO,MAAM,kBAA2C,CAAA;AAAA,EACtD,OAAA,CAAA;AAAA,EACA,cAAA,CAAA;AAAA,EACA,QAAA,CAAA;AAAA,EAEA,WAAA,CACE,MACA,EAAA,aAAA,EACA,OACA,EAAA;AACA,IAAA,IAAA,CAAK,OAAU,GAAA,MAAA,CAAA;AACf,IAAA,IAAA,CAAK,cAAiB,GAAA,aAAA,CAAA;AACtB,IAAA,IAAA,CAAK,QAAW,GAAA,OAAA,CAAA;AAAA,GAClB;AAAA,EAEA,MAAM,IACJ,GAC6B,EAAA;AAC7B,IAAM,MAAA,CAAA,GAAI,IAAK,CAAA,gBAAA,CAAiB,GAAG,CAAA,CAAA;AACnC,IAAA,MAAM,KAAQ,GAAA,MAAM,IAAK,CAAA,OAAA,CAAQ,IAAI,CAAC,CAAA,CAAA;AACtC,IAAO,OAAA,KAAA,CAAA;AAAA,GACT;AAAA,EAEA,MAAM,GACJ,CAAA,GAAA,EACA,KACA,EAAA,IAAA,GAA+B,EAChB,EAAA;AACf,IAAM,MAAA,CAAA,GAAI,IAAK,CAAA,gBAAA,CAAiB,GAAG,CAAA,CAAA;AACnC,IAAA,MAAM,KAAK,OAAQ,CAAA,GAAA,CAAI,CAAG,EAAA,KAAA,EAAO,KAAK,GAAG,CAAA,CAAA;AAAA,GAC3C;AAAA,EAEA,MAAM,OAAO,GAA4B,EAAA;AACvC,IAAM,MAAA,CAAA,GA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CAAA,cAAA;AAAA,IACrB,QAAQA,6BAAa,CAAA,UAAA;AAAA,GACvB;AAAA,EACA,MAAM,iBAAA,CAAkB,EAAE,MAAA,EAAQ,QAAU,EAAA;AAC1C,IAAA,OAAO,YAAa,CAAA,UAAA,CAAW,MAAQ,EAAA,EAAE,QAAQ,CAAA,CAAA;AAAA,GACnD;AAAA,EACA,MAAM,OAAA,CAAQ,EAAE,MAAA,IAAU,OAAS,EAAA;AACjC,IAAA,OAAO,QAAQ,SAAU,CAAA,MAAA,CAAO,KAAM,EAAC,EAAE,SAAU,EAAA,CAAA;AAAA,GACrD;AACF,CAAC;;;;;"}
|
|
1
|
+
{"version":3,"file":"cache.cjs.js","sources":["../src/entrypoints/cache/types.ts","../src/entrypoints/cache/CacheClient.ts","../src/entrypoints/cache/CacheManager.ts","../src/entrypoints/cache/cacheServiceFactory.ts"],"sourcesContent":["/*\n * Copyright 2020 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { LoggerService } from '@backstage/backend-plugin-api';\nimport { HumanDuration, durationToMilliseconds } from '@backstage/types';\n\n/**\n * Options given when constructing a {@link CacheManager}.\n *\n * @public\n */\nexport type CacheManagerOptions = {\n /**\n * An optional logger for use by the PluginCacheManager.\n */\n logger?: LoggerService;\n\n /**\n * An optional handler for connection errors emitted from the underlying data\n * store.\n */\n onError?: (err: Error) => void;\n};\n\nexport function ttlToMilliseconds(ttl: number | HumanDuration): number {\n return typeof ttl === 'number' ? ttl : durationToMilliseconds(ttl);\n}\n","/*\n * Copyright 2021 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport {\n CacheService,\n CacheServiceOptions,\n CacheServiceSetOptions,\n} from '@backstage/backend-plugin-api';\nimport { JsonValue } from '@backstage/types';\nimport { createHash } from 'crypto';\nimport Keyv from 'keyv';\nimport { ttlToMilliseconds } from './types';\n\nexport type CacheClientFactory = (options: CacheServiceOptions) => Keyv;\n\n/**\n * A basic, concrete implementation of the CacheService, suitable for almost\n * all uses in Backstage.\n */\nexport class DefaultCacheClient implements CacheService {\n #client: Keyv;\n #clientFactory: CacheClientFactory;\n #options: CacheServiceOptions;\n\n constructor(\n client: Keyv,\n clientFactory: CacheClientFactory,\n options: CacheServiceOptions,\n ) {\n this.#client = client;\n this.#clientFactory = clientFactory;\n this.#options = options;\n }\n\n async get<TValue extends JsonValue>(\n key: string,\n ): Promise<TValue | undefined> {\n const k = this.getNormalizedKey(key);\n const value = await this.#client.get(k);\n return value as TValue | undefined;\n }\n\n async set(\n key: string,\n value: JsonValue,\n opts: CacheServiceSetOptions = {},\n ): Promise<void> {\n const k = this.getNormalizedKey(key);\n const ttl =\n opts.ttl !== undefined ? ttlToMilliseconds(opts.ttl) : undefined;\n await this.#client.set(k, value, ttl);\n }\n\n async delete(key: string): Promise<void> {\n const k = this.getNormalizedKey(key);\n await this.#client.delete(k);\n }\n\n withOptions(options: CacheServiceOptions): CacheService {\n const newOptions = { ...this.#options, ...options };\n return new DefaultCacheClient(\n this.#clientFactory(newOptions),\n this.#clientFactory,\n newOptions,\n );\n }\n\n /**\n * Ensures keys are well-formed for any/all cache stores.\n */\n private getNormalizedKey(candidateKey: string): string {\n // Remove potentially invalid characters.\n const wellFormedKey = Buffer.from(candidateKey).toString('base64');\n\n // Memcache in particular doesn't do well with keys > 250 bytes.\n // Padded because a plugin ID is also prepended to the key.\n if (wellFormedKey.length < 200) {\n return wellFormedKey;\n }\n\n return createHash('sha256').update(candidateKey).digest('base64');\n }\n}\n","/*\n * Copyright 2021 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport {\n CacheService,\n CacheServiceOptions,\n LoggerService,\n RootConfigService,\n} from '@backstage/backend-plugin-api';\nimport Keyv from 'keyv';\nimport { DefaultCacheClient } from './CacheClient';\nimport { CacheManagerOptions, ttlToMilliseconds } from './types';\nimport { durationToMilliseconds } from '@backstage/types';\n\ntype StoreFactory = (pluginId: string, defaultTtl: number | undefined) => Keyv;\n\n/**\n * Implements a Cache Manager which will automatically create new cache clients\n * for plugins when requested. All requested cache clients are created with the\n * connection details provided.\n *\n * @public\n */\nexport class CacheManager {\n /**\n * Keys represent supported `backend.cache.store` values, mapped to factories\n * that return Keyv instances appropriate to the store.\n */\n private readonly storeFactories = {\n redis: this.createRedisStoreFactory(),\n memcache: this.createMemcacheStoreFactory(),\n memory: this.createMemoryStoreFactory(),\n };\n\n private readonly logger?: LoggerService;\n private readonly store: keyof CacheManager['storeFactories'];\n private readonly connection: string;\n private readonly useRedisSets: boolean;\n private readonly errorHandler: CacheManagerOptions['onError'];\n private readonly defaultTtl?: number;\n\n /**\n * Creates a new {@link CacheManager} instance by reading from the `backend`\n * config section, specifically the `.cache` key.\n *\n * @param config - The loaded application configuration.\n */\n static fromConfig(\n config: RootConfigService,\n options: CacheManagerOptions = {},\n ): CacheManager {\n // If no `backend.cache` config is provided, instantiate the CacheManager\n // with an in-memory cache client.\n const store = config.getOptionalString('backend.cache.store') || 'memory';\n const defaultTtlConfig = config.getOptional('backend.cache.defaultTtl');\n const connectionString =\n config.getOptionalString('backend.cache.connection') || '';\n const useRedisSets =\n config.getOptionalBoolean('backend.cache.useRedisSets') ?? true;\n const logger = options.logger?.child({\n type: 'cacheManager',\n });\n\n let defaultTtl: number | undefined;\n if (defaultTtlConfig !== undefined && defaultTtlConfig !== null) {\n if (typeof defaultTtlConfig === 'number') {\n defaultTtl = defaultTtlConfig;\n } else if (\n typeof defaultTtlConfig === 'object' &&\n !Array.isArray(defaultTtlConfig)\n ) {\n defaultTtl = durationToMilliseconds(defaultTtlConfig);\n } else {\n throw new Error(\n `Invalid configuration backend.cache.defaultTtl: ${defaultTtlConfig}, expected milliseconds number or HumanDuration object`,\n );\n }\n }\n\n return new CacheManager(\n store,\n connectionString,\n useRedisSets,\n options.onError,\n logger,\n defaultTtl,\n );\n }\n\n /** @internal */\n constructor(\n store: string,\n connectionString: string,\n useRedisSets: boolean,\n errorHandler: CacheManagerOptions['onError'],\n logger?: LoggerService,\n defaultTtl?: number,\n ) {\n if (!this.storeFactories.hasOwnProperty(store)) {\n throw new Error(`Unknown cache store: ${store}`);\n }\n this.logger = logger;\n this.store = store as keyof CacheManager['storeFactories'];\n this.connection = connectionString;\n this.useRedisSets = useRedisSets;\n this.errorHandler = errorHandler;\n this.defaultTtl = defaultTtl;\n }\n\n /**\n * Generates a PluginCacheManager for consumption by plugins.\n *\n * @param pluginId - The plugin that the cache manager should be created for.\n * Plugin names should be unique.\n */\n forPlugin(pluginId: string): CacheService {\n const clientFactory = (options: CacheServiceOptions) => {\n const ttl = options.defaultTtl ?? this.defaultTtl;\n return this.getClientWithTtl(\n pluginId,\n ttl !== undefined ? ttlToMilliseconds(ttl) : undefined,\n );\n };\n\n return new DefaultCacheClient(clientFactory({}), clientFactory, {});\n }\n\n private getClientWithTtl(pluginId: string, ttl: number | undefined): Keyv {\n return this.storeFactories[this.store](pluginId, ttl);\n }\n\n private createRedisStoreFactory(): StoreFactory {\n const KeyvRedis = require('@keyv/redis');\n let store: typeof KeyvRedis | undefined;\n return (pluginId, defaultTtl) => {\n if (!store) {\n store = new KeyvRedis(this.connection);\n // Always provide an error handler to avoid stopping the process\n store.on('error', (err: Error) => {\n this.logger?.error('Failed to create redis cache client', err);\n this.errorHandler?.(err);\n });\n }\n return new Keyv({\n namespace: pluginId,\n ttl: defaultTtl,\n store,\n emitErrors: false,\n useRedisSets: this.useRedisSets,\n });\n };\n }\n\n private createMemcacheStoreFactory(): StoreFactory {\n const KeyvMemcache = require('@keyv/memcache');\n let store: typeof KeyvMemcache | undefined;\n return (pluginId, defaultTtl) => {\n if (!store) {\n store = new KeyvMemcache(this.connection);\n // Always provide an error handler to avoid stopping the process\n store.on('error', (err: Error) => {\n this.logger?.error('Failed to create memcache cache client', err);\n this.errorHandler?.(err);\n });\n }\n return new Keyv({\n namespace: pluginId,\n ttl: defaultTtl,\n emitErrors: false,\n store,\n });\n };\n }\n\n private createMemoryStoreFactory(): StoreFactory {\n const store = new Map();\n return (pluginId, defaultTtl) =>\n new Keyv({\n namespace: pluginId,\n ttl: defaultTtl,\n emitErrors: false,\n store,\n });\n }\n}\n","/*\n * Copyright 2022 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport {\n coreServices,\n createServiceFactory,\n} from '@backstage/backend-plugin-api';\nimport { CacheManager } from './CacheManager';\n\n/**\n * Key-value store for caching data.\n *\n * See {@link @backstage/code-plugin-api#CacheService}\n * and {@link https://backstage.io/docs/backend-system/core-services/cache | the service docs}\n * for more information.\n *\n * @public\n */\nexport const cacheServiceFactory = createServiceFactory({\n service: coreServices.cache,\n deps: {\n config: coreServices.rootConfig,\n plugin: coreServices.pluginMetadata,\n logger: coreServices.rootLogger,\n },\n async createRootContext({ config, logger }) {\n return CacheManager.fromConfig(config, { logger });\n },\n async factory({ plugin }, manager) {\n return manager.forPlugin(plugin.getId());\n },\n});\n"],"names":["durationToMilliseconds","createHash","Keyv","createServiceFactory","coreServices"],"mappings":";;;;;;;;;;;AAqCO,SAAS,kBAAkB,GAAqC,EAAA;AACrE,EAAA,OAAO,OAAO,GAAA,KAAQ,QAAW,GAAA,GAAA,GAAMA,6BAAuB,GAAG,CAAA,CAAA;AACnE;;ACPO,MAAM,kBAA2C,CAAA;AAAA,EACtD,OAAA,CAAA;AAAA,EACA,cAAA,CAAA;AAAA,EACA,QAAA,CAAA;AAAA,EAEA,WAAA,CACE,MACA,EAAA,aAAA,EACA,OACA,EAAA;AACA,IAAA,IAAA,CAAK,OAAU,GAAA,MAAA,CAAA;AACf,IAAA,IAAA,CAAK,cAAiB,GAAA,aAAA,CAAA;AACtB,IAAA,IAAA,CAAK,QAAW,GAAA,OAAA,CAAA;AAAA,GAClB;AAAA,EAEA,MAAM,IACJ,GAC6B,EAAA;AAC7B,IAAM,MAAA,CAAA,GAAI,IAAK,CAAA,gBAAA,CAAiB,GAAG,CAAA,CAAA;AACnC,IAAA,MAAM,KAAQ,GAAA,MAAM,IAAK,CAAA,OAAA,CAAQ,IAAI,CAAC,CAAA,CAAA;AACtC,IAAO,OAAA,KAAA,CAAA;AAAA,GACT;AAAA,EAEA,MAAM,GACJ,CAAA,GAAA,EACA,KACA,EAAA,IAAA,GAA+B,EAChB,EAAA;AACf,IAAM,MAAA,CAAA,GAAI,IAAK,CAAA,gBAAA,CAAiB,GAAG,CAAA,CAAA;AACnC,IAAA,MAAM,MACJ,IAAK,CAAA,GAAA,KAAQ,SAAY,iBAAkB,CAAA,IAAA,CAAK,GAAG,CAAI,GAAA,KAAA,CAA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;AAAA,OACH;AACA,MAAA,OAAO,IAAIA,qBAAK,CAAA;AAAA,QACd,SAAW,EAAA,QAAA;AAAA,QACX,GAAK,EAAA,UAAA;AAAA,QACL,UAAY,EAAA,KAAA;AAAA,QACZ,KAAA;AAAA,OACD,CAAA,CAAA;AAAA,KACH,CAAA;AAAA,GACF;AAAA,EAEQ,wBAAyC,GAAA;AAC/C,IAAM,MAAA,KAAA,uBAAY,GAAI,EAAA,CAAA;AACtB,IAAA,OAAO,CAAC,QAAA,EAAU,UAChB,KAAA,IAAIA,qBAAK,CAAA;AAAA,MACP,SAAW,EAAA,QAAA;AAAA,MACX,GAAK,EAAA,UAAA;AAAA,MACL,UAAY,EAAA,KAAA;AAAA,MACZ,KAAA;AAAA,KACD,CAAA,CAAA;AAAA,GACL;AACF;;ACtKO,MAAM,sBAAsBC,qCAAqB,CAAA;AAAA,EACtD,SAASC,6BAAa,CAAA,KAAA;AAAA,EACtB,IAAM,EAAA;AAAA,IACJ,QAAQA,6BAAa,CAAA,UAAA;AAAA,IACrB,QAAQA,6BAAa,CAAA,cAAA;AAAA,IACrB,QAAQA,6BAAa,CAAA,UAAA;AAAA,GACvB;AAAA,EACA,MAAM,iBAAA,CAAkB,EAAE,MAAA,EAAQ,QAAU,EAAA;AAC1C,IAAA,OAAO,YAAa,CAAA,UAAA,CAAW,MAAQ,EAAA,EAAE,QAAQ,CAAA,CAAA;AAAA,GACnD;AAAA,EACA,MAAM,OAAA,CAAQ,EAAE,MAAA,IAAU,OAAS,EAAA;AACjC,IAAA,OAAO,OAAQ,CAAA,SAAA,CAAU,MAAO,CAAA,KAAA,EAAO,CAAA,CAAA;AAAA,GACzC;AACF,CAAC;;;;;"}
|
package/dist/cache.d.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import * as _backstage_backend_plugin_api from '@backstage/backend-plugin-api';
|
|
2
|
-
import { LoggerService,
|
|
3
|
-
import { Config } from '@backstage/config';
|
|
2
|
+
import { LoggerService, RootConfigService, CacheService } from '@backstage/backend-plugin-api';
|
|
4
3
|
|
|
5
4
|
/**
|
|
6
5
|
* Key-value store for caching data.
|
|
@@ -11,7 +10,7 @@ import { Config } from '@backstage/config';
|
|
|
11
10
|
*
|
|
12
11
|
* @public
|
|
13
12
|
*/
|
|
14
|
-
declare const cacheServiceFactory: _backstage_backend_plugin_api.
|
|
13
|
+
declare const cacheServiceFactory: _backstage_backend_plugin_api.ServiceFactory<_backstage_backend_plugin_api.CacheService, "plugin", "singleton">;
|
|
15
14
|
|
|
16
15
|
/**
|
|
17
16
|
* Options given when constructing a {@link CacheManager}.
|
|
@@ -29,12 +28,6 @@ type CacheManagerOptions = {
|
|
|
29
28
|
*/
|
|
30
29
|
onError?: (err: Error) => void;
|
|
31
30
|
};
|
|
32
|
-
/**
|
|
33
|
-
* @public
|
|
34
|
-
*/
|
|
35
|
-
interface PluginCacheManager {
|
|
36
|
-
getClient(options?: CacheServiceOptions): CacheService;
|
|
37
|
-
}
|
|
38
31
|
|
|
39
32
|
/**
|
|
40
33
|
* Implements a Cache Manager which will automatically create new cache clients
|
|
@@ -61,18 +54,18 @@ declare class CacheManager {
|
|
|
61
54
|
*
|
|
62
55
|
* @param config - The loaded application configuration.
|
|
63
56
|
*/
|
|
64
|
-
static fromConfig(config:
|
|
57
|
+
static fromConfig(config: RootConfigService, options?: CacheManagerOptions): CacheManager;
|
|
65
58
|
/**
|
|
66
59
|
* Generates a PluginCacheManager for consumption by plugins.
|
|
67
60
|
*
|
|
68
61
|
* @param pluginId - The plugin that the cache manager should be created for.
|
|
69
62
|
* Plugin names should be unique.
|
|
70
63
|
*/
|
|
71
|
-
forPlugin(pluginId: string):
|
|
64
|
+
forPlugin(pluginId: string): CacheService;
|
|
72
65
|
private getClientWithTtl;
|
|
73
66
|
private createRedisStoreFactory;
|
|
74
67
|
private createMemcacheStoreFactory;
|
|
75
68
|
private createMemoryStoreFactory;
|
|
76
69
|
}
|
|
77
70
|
|
|
78
|
-
export { CacheManager, type CacheManagerOptions,
|
|
71
|
+
export { CacheManager, type CacheManagerOptions, cacheServiceFactory };
|
package/dist/database.cjs.js
CHANGED
|
@@ -128,29 +128,6 @@ async function ensureMysqlDatabaseExists(dbConfig, ...databases) {
|
|
|
128
128
|
await admin.destroy();
|
|
129
129
|
}
|
|
130
130
|
}
|
|
131
|
-
async function dropMysqlDatabase(dbConfig, ...databases) {
|
|
132
|
-
const admin = createMysqlDatabaseClient(dbConfig, {
|
|
133
|
-
connection: {
|
|
134
|
-
database: null
|
|
135
|
-
},
|
|
136
|
-
pool: {
|
|
137
|
-
min: 0,
|
|
138
|
-
acquireTimeoutMillis: 1e4
|
|
139
|
-
}
|
|
140
|
-
});
|
|
141
|
-
try {
|
|
142
|
-
const dropDatabase = async (database) => {
|
|
143
|
-
await admin.raw(`DROP DATABASE ??`, [database]);
|
|
144
|
-
};
|
|
145
|
-
await Promise.all(
|
|
146
|
-
databases.map(async (database) => {
|
|
147
|
-
return await ddlLimiter$1(() => dropDatabase(database));
|
|
148
|
-
})
|
|
149
|
-
);
|
|
150
|
-
} finally {
|
|
151
|
-
await admin.destroy();
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
131
|
function pluginPath$3(pluginId) {
|
|
155
132
|
return `plugin.${pluginId}`;
|
|
156
133
|
}
|
|
@@ -195,9 +172,6 @@ class MysqlConnector {
|
|
|
195
172
|
);
|
|
196
173
|
return client;
|
|
197
174
|
}
|
|
198
|
-
async dropDatabase(...databaseNames) {
|
|
199
|
-
return await dropMysqlDatabase(this.config, ...databaseNames);
|
|
200
|
-
}
|
|
201
175
|
/**
|
|
202
176
|
* Provides the canonical database name for a given plugin.
|
|
203
177
|
*
|
|
@@ -419,18 +393,6 @@ async function ensurePgSchemaExists(dbConfig, ...schemas) {
|
|
|
419
393
|
await admin.destroy();
|
|
420
394
|
}
|
|
421
395
|
}
|
|
422
|
-
async function dropPgDatabase(dbConfig, ...databases) {
|
|
423
|
-
const admin = createPgDatabaseClient(dbConfig);
|
|
424
|
-
try {
|
|
425
|
-
await Promise.all(
|
|
426
|
-
databases.map(async (database) => {
|
|
427
|
-
await ddlLimiter(() => admin.raw(`DROP DATABASE ??`, [database]));
|
|
428
|
-
})
|
|
429
|
-
);
|
|
430
|
-
} finally {
|
|
431
|
-
await admin.destroy();
|
|
432
|
-
}
|
|
433
|
-
}
|
|
434
396
|
function pluginPath$2(pluginId) {
|
|
435
397
|
return `plugin.${pluginId}`;
|
|
436
398
|
}
|
|
@@ -483,9 +445,6 @@ class PgConnector {
|
|
|
483
445
|
);
|
|
484
446
|
return client;
|
|
485
447
|
}
|
|
486
|
-
async dropDatabase(...databaseNames) {
|
|
487
|
-
return await dropPgDatabase(this.config, ...databaseNames);
|
|
488
|
-
}
|
|
489
448
|
/**
|
|
490
449
|
* Provides the canonical database name for a given plugin.
|
|
491
450
|
*
|
|
@@ -611,7 +570,7 @@ class PgConnector {
|
|
|
611
570
|
}
|
|
612
571
|
}
|
|
613
572
|
|
|
614
|
-
function createSqliteDatabaseClient(dbConfig,
|
|
573
|
+
function createSqliteDatabaseClient(pluginId, dbConfig, deps, overrides) {
|
|
615
574
|
const knexConfig = buildSqliteDatabaseConfig(dbConfig, overrides);
|
|
616
575
|
const connConfig = knexConfig.connection;
|
|
617
576
|
const filename = connConfig.filename ?? ":memory:";
|
|
@@ -623,7 +582,7 @@ function createSqliteDatabaseClient(dbConfig, overrides, deps) {
|
|
|
623
582
|
if (deps && filename === ":memory:") {
|
|
624
583
|
const devStore = backendDevUtils.DevDataStore.get();
|
|
625
584
|
if (devStore) {
|
|
626
|
-
const dataKey = `sqlite3-db-${
|
|
585
|
+
const dataKey = `sqlite3-db-${pluginId}`;
|
|
627
586
|
const connectionLoader = async () => {
|
|
628
587
|
const { data: seedData } = await devStore.load(dataKey);
|
|
629
588
|
return {
|
|
@@ -713,14 +672,13 @@ class Sqlite3Connector {
|
|
|
713
672
|
this.getDatabaseOverrides(pluginId)
|
|
714
673
|
);
|
|
715
674
|
const client = createSqliteDatabaseClient(
|
|
675
|
+
pluginId,
|
|
716
676
|
pluginConfig,
|
|
717
|
-
|
|
718
|
-
|
|
677
|
+
deps,
|
|
678
|
+
databaseClientOverrides
|
|
719
679
|
);
|
|
720
680
|
return client;
|
|
721
681
|
}
|
|
722
|
-
async dropDatabase(..._databaseNames) {
|
|
723
|
-
}
|
|
724
682
|
/**
|
|
725
683
|
* Provides the canonical database name for a given plugin.
|
|
726
684
|
*
|
|
@@ -868,8 +826,10 @@ class DatabaseManagerImpl {
|
|
|
868
826
|
);
|
|
869
827
|
}
|
|
870
828
|
const getClient = () => this.getDatabase(pluginId, connector, deps);
|
|
871
|
-
const
|
|
872
|
-
|
|
829
|
+
const skip = this.options?.migrations?.skip ?? this.config.getOptionalBoolean(
|
|
830
|
+
`backend.database.plugin.${pluginId}.skipMigrations`
|
|
831
|
+
) ?? this.config.getOptionalBoolean("backend.database.skipMigrations") ?? false;
|
|
832
|
+
return { getClient, migrations: { skip } };
|
|
873
833
|
}
|
|
874
834
|
/**
|
|
875
835
|
* Provides the client type which should be used for a given plugin.
|
|
@@ -907,11 +867,13 @@ class DatabaseManagerImpl {
|
|
|
907
867
|
const clientPromise = connector.getClient(pluginId, deps);
|
|
908
868
|
this.databaseCache.set(pluginId, clientPromise);
|
|
909
869
|
if (process.env.NODE_ENV !== "test") {
|
|
910
|
-
clientPromise.then(
|
|
870
|
+
clientPromise.then(
|
|
871
|
+
(client) => this.startKeepaliveLoop(pluginId, client, deps.logger)
|
|
872
|
+
);
|
|
911
873
|
}
|
|
912
874
|
return clientPromise;
|
|
913
875
|
}
|
|
914
|
-
startKeepaliveLoop(pluginId, client) {
|
|
876
|
+
startKeepaliveLoop(pluginId, client, logger) {
|
|
915
877
|
let lastKeepaliveFailed = false;
|
|
916
878
|
setInterval(() => {
|
|
917
879
|
client?.raw("select 1").then(
|
|
@@ -921,7 +883,7 @@ class DatabaseManagerImpl {
|
|
|
921
883
|
(error) => {
|
|
922
884
|
if (!lastKeepaliveFailed) {
|
|
923
885
|
lastKeepaliveFailed = true;
|
|
924
|
-
|
|
886
|
+
logger.warn(
|
|
925
887
|
`Database keepalive failed for plugin ${pluginId}, ${errors.stringifyError(
|
|
926
888
|
error
|
|
927
889
|
)}`
|
|
@@ -970,21 +932,13 @@ class DatabaseManager {
|
|
|
970
932
|
return this.impl.forPlugin(pluginId, deps);
|
|
971
933
|
}
|
|
972
934
|
}
|
|
973
|
-
async function dropDatabase(dbConfig, ...databaseNames) {
|
|
974
|
-
const client = dbConfig.getString("client");
|
|
975
|
-
const prefix = dbConfig.getOptionalString("prefix") || "backstage_plugin_";
|
|
976
|
-
if (client === "pg") {
|
|
977
|
-
await new PgConnector(dbConfig, prefix).dropDatabase(...databaseNames);
|
|
978
|
-
} else if (client === "mysql" || client === "mysql2") {
|
|
979
|
-
await new MysqlConnector(dbConfig, prefix).dropDatabase(...databaseNames);
|
|
980
|
-
}
|
|
981
|
-
}
|
|
982
935
|
|
|
983
936
|
const databaseServiceFactory = backendPluginApi.createServiceFactory({
|
|
984
937
|
service: backendPluginApi.coreServices.database,
|
|
985
938
|
deps: {
|
|
986
939
|
config: backendPluginApi.coreServices.rootConfig,
|
|
987
940
|
lifecycle: backendPluginApi.coreServices.lifecycle,
|
|
941
|
+
logger: backendPluginApi.coreServices.logger,
|
|
988
942
|
pluginMetadata: backendPluginApi.coreServices.pluginMetadata
|
|
989
943
|
},
|
|
990
944
|
async createRootContext({ config: config$1 }) {
|
|
@@ -996,15 +950,14 @@ const databaseServiceFactory = backendPluginApi.createServiceFactory({
|
|
|
996
950
|
})
|
|
997
951
|
);
|
|
998
952
|
},
|
|
999
|
-
async factory({ pluginMetadata, lifecycle }, databaseManager) {
|
|
953
|
+
async factory({ pluginMetadata, lifecycle, logger }, databaseManager) {
|
|
1000
954
|
return databaseManager.forPlugin(pluginMetadata.getId(), {
|
|
1001
|
-
|
|
1002
|
-
|
|
955
|
+
lifecycle,
|
|
956
|
+
logger
|
|
1003
957
|
});
|
|
1004
958
|
}
|
|
1005
959
|
});
|
|
1006
960
|
|
|
1007
961
|
exports.DatabaseManager = DatabaseManager;
|
|
1008
962
|
exports.databaseServiceFactory = databaseServiceFactory;
|
|
1009
|
-
exports.dropDatabase = dropDatabase;
|
|
1010
963
|
//# sourceMappingURL=database.cjs.js.map
|