fix(techdocs): getObject stream
This commit is contained in:
@@ -14,6 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
import type { S3ClientConfig } from '@aws-sdk/client-s3';
|
||||
import { EventEmitter } from 'events';
|
||||
import fs from 'fs';
|
||||
|
||||
export class S3 {
|
||||
@@ -36,8 +37,13 @@ export class S3 {
|
||||
getObject({ Key }: { Key: string }) {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (fs.existsSync(Key)) {
|
||||
const emitter = new EventEmitter();
|
||||
process.nextTick(() => {
|
||||
emitter.emit('data', Buffer.from(fs.readFileSync(Key)));
|
||||
emitter.emit('end');
|
||||
});
|
||||
resolve({
|
||||
Body: Buffer.from(fs.readFileSync(Key)),
|
||||
Body: emitter,
|
||||
});
|
||||
} else {
|
||||
reject({ message: `The file ${Key} doest not exist.` });
|
||||
|
||||
@@ -22,6 +22,15 @@ import { Config } from '@backstage/config';
|
||||
import { getHeadersForFileExtension, getFileTreeRecursively } from './helpers';
|
||||
import { PublisherBase, PublishRequest } from './types';
|
||||
import fs from 'fs-extra';
|
||||
import { Readable } from 'stream';
|
||||
|
||||
const streamToString = (stream: Readable): Promise<string> =>
|
||||
new Promise((resolve, reject) => {
|
||||
const chunks: any[] = [];
|
||||
stream.on('data', chunk => chunks.push(chunk));
|
||||
stream.on('error', reject);
|
||||
stream.on('end', () => resolve(Buffer.concat(chunks).toString('utf8')));
|
||||
});
|
||||
|
||||
export class AwsS3Publish implements PublisherBase {
|
||||
static fromConfig(config: Config, logger: Logger): PublisherBase {
|
||||
@@ -139,8 +148,10 @@ export class AwsS3Publish implements PublisherBase {
|
||||
Bucket: this.bucketName,
|
||||
Key: `${entityRootDir}/techdocs_metadata.json`,
|
||||
})
|
||||
.then(file => {
|
||||
const techdocsMetadataJson = file?.Body?.toString();
|
||||
.then(async file => {
|
||||
const techdocsMetadataJson = await streamToString(
|
||||
file.Body as Readable,
|
||||
);
|
||||
|
||||
if (!techdocsMetadataJson) {
|
||||
throw new Error(
|
||||
@@ -175,8 +186,8 @@ export class AwsS3Publish implements PublisherBase {
|
||||
|
||||
this.storageClient
|
||||
.getObject({ Bucket: this.bucketName, Key: filePath })
|
||||
.then(object => {
|
||||
const fileContent = object?.Body?.toString();
|
||||
.then(async object => {
|
||||
const fileContent = await streamToString(object.Body as Readable);
|
||||
if (!fileContent) {
|
||||
throw new Error(`Unable to parse the file ${filePath}.`);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user