Skip to content

Commit

Permalink
Clean up interfaces, more emitting
Browse files Browse the repository at this point in the history
  • Loading branch information
rdmurphy committed Aug 5, 2019
1 parent d0ac4cc commit 7aa6267
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 21 deletions.
30 changes: 20 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,15 @@ TK TK
- [downloadFiles](#downloadfiles)
- [Parameters](#parameters-4)
- [How outputs are structured](#how-outputs-are-structured)
- [Output](#output)
- [DownloadOutput](#downloadoutput)
- [Key](#key)
- [isIdentical](#isidentical)
- [DownloadOutput](#downloadoutput)
- [UploadOutput](#uploadoutput)
- [Key](#key-1)
- [isIdentical](#isidentical-1)
- [isPublic](#ispublic)
- [size](#size)
- [Delivery#upload](#deliveryupload)

### Delivery

Expand Down Expand Up @@ -123,9 +125,9 @@ Downloads multiple files from a prefix on S3.
These represent the output objects from Delivery's commands.


### Output
### DownloadOutput

The base output of both downloaded and uploaded files.
What downloadFile and downloadFiles returns.

#### Key

Expand All @@ -139,17 +141,21 @@ Whether the file was identical on S3 or locally and was skipped.

Type: [boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)

### DownloadOutput
### UploadOutput

**Extends Output**
What uploadFile and uploadFiles returns.

What downloadFile and downloadFiles returns. Identical to [Output](#output).
#### Key

### UploadOutput
The file's path on S3.

**Extends Output**
Type: [string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)

#### isIdentical

Whether the file was identical on S3 or locally and was skipped.

What uploadFile and uploadFiles returns. Includes [Output](#output)'s fields.
Type: [boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)

#### isPublic

Expand All @@ -163,6 +169,10 @@ The size of the uploaded file in bytes.

Type: [number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)

### Delivery#upload

Type: [UploadOutput](#uploadoutput)

## License

MIT
17 changes: 17 additions & 0 deletions demo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const { Delivery } = require('./dist');

async function main() {
const delivery = new Delivery({
bucket: 'datadesk-delivery-demo',
basePath: 'example',
});

delivery.on('upload', console.log);
delivery.on('upload:all', console.log);

await delivery.uploadFiles('./example', {
shouldCache: true,
});
}

main().catch(console.error);
12 changes: 12 additions & 0 deletions example/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>@datadesk/delivery demo</title>
</head>
<body>
I'm a test page for deploying!
</body>
</html>
12 changes: 12 additions & 0 deletions output/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>@datadesk/delivery demo</title>
</head>
<body>
I'm a test page for deploying!
</body>
</html>
37 changes: 26 additions & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,27 @@ import mime from 'mime-types';
import { cacheLookup } from './cache-lookup';
import { findFiles, resolvePath } from './utils';

/**
* A helper type to cover cases where either nullable is valid.
*
* @private
*/
type Optional = null | undefined;

/** The base output of both downloaded and uploaded files. */
interface Output {
/** What downloadFile and downloadFiles returns. */
export interface DownloadOutput {
/** The file's path on S3. */
Key: string;
/** Whether the file was identical on S3 or locally and was skipped. */
isIdentical: boolean;
}

/** What downloadFile and downloadFiles returns. Identical to {@link Output}. */
export interface DownloadOutput extends Output {}

/**
* What uploadFile and uploadFiles returns. Includes {@link Output}'s fields.
*/
export interface UploadOutput extends Output {
/** What uploadFile and uploadFiles returns. */
export interface UploadOutput {
/** The file's path on S3. */
Key: string;
/** Whether the file was identical on S3 or locally and was skipped. */
isIdentical: boolean;
/** This file was made public on upload. */
isPublic: boolean;
/** The size of the uploaded file in bytes. */
Expand Down Expand Up @@ -107,7 +111,7 @@ export class Delivery extends EventEmitter {
const ACL = isPublic ? 'public-read' : 'private';

// determine the content hash for the file
const hash = await hasha.fromStream(Body, { algorithm: 'md5' });
const hash = await hasha.fromFile(file, { algorithm: 'md5' });

// we check to see if the file already exists on S3 and if it is identical
const s3ETag = await this.getS3ObjectETag(Key);
Expand All @@ -131,7 +135,11 @@ export class Delivery extends EventEmitter {
}
}

await this.s3.putObject(params).promise();
try {
await this.s3.putObject(params).promise();
} catch (err) {
throw err;
}
}

const output: UploadOutput = {
Expand All @@ -141,6 +149,10 @@ export class Delivery extends EventEmitter {
size,
};

/**
* @event Delivery#upload
* @type {UploadOutput}
*/
this.emit('upload', output);

return output;
Expand Down Expand Up @@ -174,6 +186,7 @@ export class Delivery extends EventEmitter {
)
);

this.emit('upload:all', uploadedFiles);
return uploadedFiles;
}

Expand Down Expand Up @@ -261,6 +274,7 @@ export class Delivery extends EventEmitter {
);
}

this.emit('download:all', downloadedFiles);
return downloadedFiles;
}

Expand All @@ -278,6 +292,7 @@ export class Delivery extends EventEmitter {

try {
const { ETag } = await this.s3.headObject(params).promise();

return ETag;
} catch (err) {
// the file didn't exist and that's fine
Expand Down

0 comments on commit 7aa6267

Please sign in to comment.