From 2c7398e87d2bcc6193ab54f8f7a3caa0e1abce0d Mon Sep 17 00:00:00 2001 From: Blayne Chard Date: Wed, 15 Sep 2021 17:32:13 +1200 Subject: [PATCH 1/2] feat: add options to .write This allows content encoding and other options to be set --- packages/core/src/fs.ts | 9 ++++++++- packages/core/src/index.ts | 2 +- packages/source-aws/src/s3.fs.ts | 14 +++++++++++--- packages/source-aws/src/type.ts | 6 +++++- 4 files changed, 25 insertions(+), 6 deletions(-) diff --git a/packages/core/src/fs.ts b/packages/core/src/fs.ts index 1c64d258..f35de047 100644 --- a/packages/core/src/fs.ts +++ b/packages/core/src/fs.ts @@ -11,6 +11,13 @@ export interface FileInfo { size?: number; } +export interface WriteOptions { + /** Encoding of the file eg "gzip" */ + contentEncoding: string; + /** Content type of the file eg "text/plain" */ + contentType: string; +} + export interface FileSystem { /** * Protocol used for communication @@ -25,7 +32,7 @@ export interface FileSystem { /** Create a read stream */ stream(filePath: string): Readable; /** Write a file from either a buffer or stream */ - write(filePath: string, buffer: Buffer | Readable | string): Promise; + write(filePath: string, buffer: Buffer | Readable | string, opts?: Partial): Promise; /** Recursively list all files in path */ list(filePath: string): AsyncGenerator; /** Recursively list all files in path with additional details */ diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index 39be901d..b3ca6b14 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -4,7 +4,7 @@ export { LogType } from './log.js'; export { SourceMemory } from './chunk.source.memory.js'; export { ChunkSource } from './source.js'; export { ErrorCodes, CompositeError } from './composite.js'; -export { FileSystem, FileInfo } from './fs.js'; +export { FileSystem, FileInfo, WriteOptions } from './fs.js'; export function isRecord(value: unknown): value is Record { return typeof value === 'object' && value !== null; diff --git a/packages/source-aws/src/s3.fs.ts b/packages/source-aws/src/s3.fs.ts index dc7b2597..46dd73dc 100644 --- a/packages/source-aws/src/s3.fs.ts +++ b/packages/source-aws/src/s3.fs.ts @@ -1,4 +1,4 @@ -import { FileInfo, FileSystem, isRecord } from '@chunkd/core'; +import { FileInfo, FileSystem, isRecord, WriteOptions } from '@chunkd/core'; import S3 from 'aws-sdk/clients/s3.js'; import { Credentials } from 'aws-sdk/lib/credentials.js'; import ctc from 'aws-sdk/lib/credentials/chainable_temporary_credentials.js'; @@ -147,12 +147,20 @@ export class FsAwsS3 implements FileSystem { } } - async write(filePath: string, buf: Buffer | Readable | string): Promise { + async write(filePath: string, buf: Buffer | Readable | string, ctx?: WriteOptions): Promise { const opts = this.parse(filePath); if (opts.key == null) throw new Error(`Failed to write: "${filePath}"`); try { - await this.s3.upload({ Bucket: opts.bucket, Key: opts.key, Body: buf }).promise(); + await this.s3 + .upload({ + Bucket: opts.bucket, + Key: opts.key, + Body: buf, + ContentEncoding: ctx?.contentEncoding, + ContentType: ctx?.contentType, + }) + .promise(); } catch (e) { throw getCompositeError(e, `Failed to write: "${filePath}"`); } diff --git a/packages/source-aws/src/type.ts b/packages/source-aws/src/type.ts index b0eea2bc..b52d00ed 100644 --- a/packages/source-aws/src/type.ts +++ b/packages/source-aws/src/type.ts @@ -13,7 +13,11 @@ export type Location = { Bucket: string; Key: string }; export type GetObjectReq = Location & { Range?: string }; export type GetObjectRes = { Body?: Buffer | unknown; ContentRange?: string }; -export type UploadReq = Location & { Body?: Buffer | string | Readable }; +export type UploadReq = Location & { + Body?: Buffer | string | Readable; + ContentEncoding?: string; + ContentType?: string; +}; export type UploadRes = unknown; export type ListReq = { Bucket: string; Prefix?: string; ContinuationToken?: string }; From a128127b6d3f97df6fc2c4128269d1fcaa61b6f5 Mon Sep 17 00:00:00 2001 From: Blayne Chard Date: Wed, 15 Sep 2021 18:36:15 +1200 Subject: [PATCH 2/2] refactor: remove make folder option --- packages/source-file/src/file.fs.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/source-file/src/file.fs.ts b/packages/source-file/src/file.fs.ts index 785bdf5b..aff78bec 100644 --- a/packages/source-file/src/file.fs.ts +++ b/packages/source-file/src/file.fs.ts @@ -68,9 +68,9 @@ export class FsFile implements FileSystem { return this.head(filePath).then((f) => f != null); } - async write(filePath: string, buf: Buffer | Readable, makeFolder = true): Promise { + async write(filePath: string, buf: Buffer | Readable): Promise { const folderPath = path.dirname(filePath); - if (makeFolder) await fs.promises.mkdir(folderPath, { recursive: true }); + await fs.promises.mkdir(folderPath, { recursive: true }); try { if (Buffer.isBuffer(buf)) { await fs.promises.writeFile(filePath, buf);