backend-common: print warning if supplied readers do not implement readUrl

Co-authored-by: Johan Haals <johan.haals@gmail.com>
Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2021-07-01 14:31:59 +02:00
parent 78e89bea46
commit 75faa7be4b
2 changed files with 17 additions and 1 deletions
@@ -15,6 +15,7 @@
*/
import { NotAllowedError } from '@backstage/errors';
import { Logger } from 'winston';
import {
ReadTreeOptions,
ReadTreeResponse,
@@ -26,12 +27,17 @@ import {
UrlReaderPredicateTuple,
} from './types';
const MIN_WARNING_INTERVAL_MS = 1000 * 60 * 15;
/**
* A UrlReader implementation that selects from a set of UrlReaders
* based on a predicate tied to each reader.
*/
export class UrlReaderPredicateMux implements UrlReader {
private readonly readers: UrlReaderPredicateTuple[] = [];
private readonly readerWarnings: Map<UrlReader, number> = new Map();
constructor(private readonly logger: Logger) {}
register(tuple: UrlReaderPredicateTuple): void {
this.readers.push(tuple);
@@ -60,6 +66,16 @@ export class UrlReaderPredicateMux implements UrlReader {
if (reader.readUrl) {
return reader.readUrl(url, options);
}
const now = Date.now();
const lastWarned = this.readerWarnings.get(reader) ?? 0;
if (now > lastWarned + MIN_WARNING_INTERVAL_MS) {
this.readerWarnings.set(reader, now);
this.logger.warn(
`No implementation of readUrl found for ${reader}, this method will be required in the ` +
`future and will replace the 'read' method. See the changelog for more details here: ` +
'https://github.com/backstage/backstage/blob/master/packages/backend-common/CHANGELOG.md#085',
);
}
const buffer = await reader.read(url);
return {
buffer: async () => buffer,
@@ -43,7 +43,7 @@ export class UrlReaders {
* Creates a UrlReader without any known types.
*/
static create({ logger, config, factories }: CreateOptions): UrlReader {
const mux = new UrlReaderPredicateMux();
const mux = new UrlReaderPredicateMux(logger);
const treeResponseFactory = DefaultReadTreeResponseFactory.create({
config,
});