backend-common: Deprecate read in favour of readUrl in UrlReader

Signed-off-by: Johan Haals <johan.haals@gmail.com>
This commit is contained in:
Johan Haals
2022-09-23 08:50:33 +02:00
parent a17735e7ec
commit a7607b5413
20 changed files with 147 additions and 125 deletions
@@ -27,8 +27,6 @@ import {
UrlReaderPredicateTuple,
} from './types';
const MIN_WARNING_INTERVAL_MS = 1000 * 60 * 15;
function notAllowedMessage(url: string) {
return (
`Reading from '${url}' is not allowed. ` +
@@ -43,7 +41,6 @@ function notAllowedMessage(url: string) {
*/
export class UrlReaderPredicateMux implements UrlReader {
private readonly readers: UrlReaderPredicateTuple[] = [];
private readonly readerWarnings: Map<UrlReader, number> = new Map();
constructor(private readonly logger: Logger) {}
@@ -71,23 +68,7 @@ export class UrlReaderPredicateMux implements UrlReader {
for (const { predicate, reader } of this.readers) {
if (predicate(parsed)) {
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,
};
return reader.readUrl(url, options);
}
}
+3 -3
View File
@@ -27,6 +27,7 @@ import { AbortSignal } from 'node-abort-controller';
export type UrlReader = {
/**
* Reads a single file and return its content.
* @deprecated use readUrl instead.
*/
read(url: string): Promise<Buffer>;
@@ -38,10 +39,9 @@ export type UrlReader = {
* This is a replacement for the read method that supports options and
* complex responses.
*
* Use this whenever it is available, as the read method will be
* deprecated and eventually removed in a future release.
* Use this as the read method will be removed in a future release.
*/
readUrl?(url: string, options?: ReadUrlOptions): Promise<ReadUrlResponse>;
readUrl(url: string, options?: ReadUrlOptions): Promise<ReadUrlResponse>;
/**
* Reads a full or partial file tree.