Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add options to .write #35

Merged
merged 2 commits into from
Sep 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion packages/core/src/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T extends ChunkSource = ChunkSource> {
/**
* Protocol used for communication
Expand All @@ -25,7 +32,7 @@ export interface FileSystem<T extends ChunkSource = ChunkSource> {
/** 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<void>;
write(filePath: string, buffer: Buffer | Readable | string, opts?: Partial<WriteOptions>): Promise<void>;
/** Recursively list all files in path */
list(filePath: string): AsyncGenerator<string>;
/** Recursively list all files in path with additional details */
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T = unknown>(value: unknown): value is Record<string, T> {
return typeof value === 'object' && value !== null;
Expand Down
14 changes: 11 additions & 3 deletions packages/source-aws/src/s3.fs.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -147,12 +147,20 @@ export class FsAwsS3 implements FileSystem<SourceAwsS3> {
}
}

async write(filePath: string, buf: Buffer | Readable | string): Promise<void> {
async write(filePath: string, buf: Buffer | Readable | string, ctx?: WriteOptions): Promise<void> {
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}"`);
}
Expand Down
6 changes: 5 additions & 1 deletion packages/source-aws/src/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
Expand Down
4 changes: 2 additions & 2 deletions packages/source-file/src/file.fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ export class FsFile implements FileSystem<SourceFile> {
return this.head(filePath).then((f) => f != null);
}

async write(filePath: string, buf: Buffer | Readable, makeFolder = true): Promise<void> {
async write(filePath: string, buf: Buffer | Readable): Promise<void> {
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);
Expand Down