Skip to content

Commit

Permalink
Merge conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
rdmurphy committed Jun 16, 2020
2 parents 3c9e523 + 217b8ef commit ba51e5f
Show file tree
Hide file tree
Showing 7 changed files with 1,979 additions and 2,507 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [0.5.0] - 2020-06-16

### Added

- Added the ability to pass a `maxAgeOverride` parameter to `uploadFile` and `uploadFiles`. If `shouldCache` is `true` and `maxAgeOverride` is provided, the upload functions will use this value instead to set the cache control header.

## [0.4.0] - 2020-01-26

### Added
Expand Down
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,12 @@ Uploads a single file to S3.

- `file` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** The path to the file to upload
- `path` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** Where to upload the file relative to the base path
- `options` **{isPublic: [boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)?, shouldCache: [boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)?}** (optional, default `{}`)
- `options` **{isPublic: [boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)?, shouldCache: [boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)?, maxAgeOverride: [number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)?}** (optional, default `{}`)
- `options.isPublic` Whether a file should be made public or not on upload (optional, default `false`)
- `options.shouldCache` Whether a file should have cache headers applied (optional, default `false`)
- `options.maxAgeOverride` A custom max-age value (in seconds) that will
override the built-in lookup if shouldCache
is true

##### Examples

Expand All @@ -110,10 +113,13 @@ Upload a directory of files to S3.
##### Parameters

- `dir` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** The directory to upload to S3
- `options` **{prefix: [string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)?, isPublic: [boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)?, shouldCache: [boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)?}** (optional, default `{}`)
- `options` **{prefix: [string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)?, isPublic: [boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)?, shouldCache: [boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)?, maxAgeOverride: [number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)?}** (optional, default `{}`)
- `options.prefix` The prefix to add to the uploaded file's path (optional, default `''`)
- `options.isPublic` Whether all files uploaded should be made public (optional, default `false`)
- `options.shouldCache` Whether all files uploaded should get cache headers (optional, default `false`)
- `options.maxAgeOverride` A custom max-age value (in seconds) that will
override the built-in lookup if shouldCache
is true

##### Examples

Expand Down
4,379 changes: 1,902 additions & 2,477 deletions package-lock.json

Large diffs are not rendered by default.

19 changes: 9 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,19 @@
},
"homepage": "https://github.com/datadesk/delivery#readme",
"dependencies": {
"aws-sdk": "^2.610.0",
"fast-glob": "^3.1.1",
"fs-extra": "^9.0.0",
"aws-sdk": "^2.698.0",
"fast-glob": "^3.2.4",
"hasha": "^5.0.0",
"mime-types": "^2.1.26"
},
"devDependencies": {
"@datadesk/prettier-config": "^1.0.0",
"@types/fs-extra": "^8.0.0",
"@datagraphics/prettier-config": "^2.0.0",
"@types/mime-types": "^2.1.0",
"documentation": "^13.0.0",
"np": "^6.0.0",
"prettier": "^1.18.2",
"typebundle": "^0.7.0"
"@types/node": "^14.0.13",
"documentation": "^13.0.1",
"np": "^6.2.4",
"prettier": "^2.0.5",
"typebundle": "^0.11.0"
},
"prettier": "@datadesk/prettier-config"
"prettier": "@datagraphics/prettier-config"
}
8 changes: 4 additions & 4 deletions src/cache-lookup.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const oneMinute = 'max-age=60';
const oneHour = 'max-age=3600';
const oneYear = 'max-age=31536000';
const oneMinute = 60;
const oneHour = 3600;
const oneYear = 31536000;

export const cacheLookup = new Map<string, string>();
export const cacheLookup = new Map<string, number>();

// HTML
cacheLookup.set('text/html', oneMinute);
Expand Down
46 changes: 33 additions & 13 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
// native
import { EventEmitter } from 'events';
import { createReadStream, promises as fs } from 'fs';
import { Agent } from 'https';
import { join, relative } from 'path';

// packages
import S3 from 'aws-sdk/clients/s3';
import fs from 'fs-extra';
import hasha from 'hasha';
import mime from 'mime-types';

// local
import { cacheLookup } from './cache-lookup';
import { findFiles, resolvePath } from './utils';
import { findFiles, outputFile, resolvePath } from './utils';

/**
* A helper type to cover cases where either nullable is valid.
Expand Down Expand Up @@ -56,9 +56,9 @@ export interface UploadOutput {
* });
*/
export class Delivery extends EventEmitter {
s3: S3;
bucket: string;
basePath: string;
declare s3: S3;
declare bucket: string;
declare basePath: string;

constructor({
bucket,
Expand Down Expand Up @@ -94,6 +94,9 @@ export class Delivery extends EventEmitter {
* @param options
* @param options.isPublic Whether a file should be made public or not on upload
* @param options.shouldCache Whether a file should have cache headers applied
* @param options.maxAgeOverride A custom max-age value (in seconds) that will
* override the built-in lookup if shouldCache
* is true
* @example
* const result = await delivery.uploadFile(
* './data/counties.json', // path to the file on local drive
Expand All @@ -109,13 +112,18 @@ export class Delivery extends EventEmitter {
{
isPublic = false,
shouldCache = false,
}: { isPublic?: boolean; shouldCache?: boolean } = {}
maxAgeOverride,
}: {
isPublic?: boolean;
shouldCache?: boolean;
maxAgeOverride?: number;
} = {}
): Promise<UploadOutput> {
// prepare the Key to the file on S3
const Key = join(this.basePath, path);

// get read to read the file's as a stream
const Body = fs.createReadStream(file);
// get ready to read the file as a stream
const Body = createReadStream(file);

// grab the size of the file
const { size } = await fs.stat(file);
Expand Down Expand Up @@ -144,10 +152,12 @@ export class Delivery extends EventEmitter {
};

if (shouldCache) {
const maxAge = cacheLookup.get(ContentType);
const maxAge = maxAgeOverride
? maxAgeOverride
: cacheLookup.get(ContentType);

if (maxAge) {
params.CacheControl = maxAge;
params.CacheControl = `max-age=${maxAge}`;
}
}

Expand Down Expand Up @@ -182,6 +192,9 @@ export class Delivery extends EventEmitter {
* @param options.prefix The prefix to add to the uploaded file's path
* @param options.isPublic Whether all files uploaded should be made public
* @param options.shouldCache Whether all files uploaded should get cache headers
* @param options.maxAgeOverride A custom max-age value (in seconds) that will
* override the built-in lookup if shouldCache
* is true
* @example
* const result = await delivery.uploadFiles(
* './dist/', // path to the directory on local drive to upload
Expand All @@ -197,7 +210,13 @@ export class Delivery extends EventEmitter {
prefix = '',
isPublic = false,
shouldCache = false,
}: { prefix?: string; isPublic?: boolean; shouldCache?: boolean } = {}
maxAgeOverride,
}: {
prefix?: string;
isPublic?: boolean;
shouldCache?: boolean;
maxAgeOverride?: number;
} = {}
) {
const files = await findFiles(dir);

Expand All @@ -206,6 +225,7 @@ export class Delivery extends EventEmitter {
this.uploadFile(file, join(prefix, dest), {
isPublic,
shouldCache,
maxAgeOverride,
})
)
);
Expand Down Expand Up @@ -261,7 +281,7 @@ export class Delivery extends EventEmitter {
};

const data = await this.s3.getObject(params).promise();
await fs.outputFile(dest, data.Body);
await outputFile(dest, data.Body);
}

const output: DownloadOutput = { Key, isIdentical };
Expand Down Expand Up @@ -293,7 +313,7 @@ export class Delivery extends EventEmitter {

if (Contents) {
await Promise.all(
Contents.map(async obj => {
Contents.map(async (obj) => {
if (obj.Key == null) return;

const Key = relative(this.basePath, obj.Key);
Expand Down
18 changes: 17 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// native
import { relative, resolve } from 'path';
import { promises as fs } from 'fs';
import { dirname, relative, resolve } from 'path';

// packages
import glob from 'fast-glob';
Expand Down Expand Up @@ -35,3 +36,18 @@ export async function findFiles(dir: string) {
return { file, dest };
});
}

export async function outputFile(dest: string, data: any) {
// get the file's directory
const dir = dirname(dest);

// ensure the directory exists
await fs.mkdir(dir, { recursive: true });

// attempt to write the file
try {
await fs.writeFile(dest, data);
} catch (e) {
throw e;
}
}

0 comments on commit ba51e5f

Please sign in to comment.