Skip to content
Open
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
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,7 @@ typings/
# Ignore Terraform stuff in examples
.terraform
*.tfstate
*.tfstate.backup
*.tfstate.backup

# WebStorm
.idea
26 changes: 26 additions & 0 deletions src/FileItem.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import * as Path from 'path'
import * as fs from 'fs-extra'
import { Item, Container } from './types'
import * as crypto from 'crypto'

export default class FileItem implements Item {
private baseDirectory: string
private computedEtag?: string

public key: string
public modtime: Date
Expand All @@ -21,6 +23,30 @@ export default class FileItem implements Item {
this.isSymbolicLink = stats.isSymbolicLink()
}

public etag(): string {

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be expensive to compute, so this is a method instead of a property so that it can be lazily computed.

if (this.computedEtag === undefined) {
this.computedEtag = FileItem.computeEtag(Path.join(this.baseDirectory, this.key))
}
return this.computedEtag!
}

private static computeEtag(filepath: string): string {
const hash = crypto.createHash('md5')
const buffer = Buffer.alloc(8192)

const fd = fs.openSync(filepath, 'r')
try {
let bytesRead
do {
bytesRead = fs.readSync(fd, buffer, 0, 8192, null)
hash.update(buffer.slice(0, bytesRead))
} while (bytesRead === 8192)
} finally {
fs.closeSync(fd)
}
return `"${hash.digest('hex')}"`
}

read(): Promise<Buffer> {
return fs.readFile(Path.join(this.baseDirectory, this.key))
}
Expand Down
1 change: 1 addition & 0 deletions src/S3Container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ export default class S3Container extends S3Prefixer implements Container {
const s3Object: S3.Object = {
Key: key,
LastModified: objectHead.LastModified,
ETag: objectHead.ETag,
Size: body.length,
}
const ret: Item = new S3Item(item.key, s3Object)
Expand Down
6 changes: 6 additions & 0 deletions src/S3Item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const s3 = new S3()
export default class S3Item extends S3Prefixer implements Item {
private s3Object: S3.Object
private bucketName: string
private remoteEtag?: string

public key: string
public modtime: Date
Expand All @@ -30,6 +31,7 @@ export default class S3Item extends S3Prefixer implements Item {
}
this.key = s3Object.Key
this.modtime = s3Object.LastModified
this.remoteEtag = s3Object.ETag
this.size = s3Object.Size
this.isSymbolicLink = false
}
Expand Down Expand Up @@ -59,4 +61,8 @@ export default class S3Item extends S3Prefixer implements Item {
public read(): Promise<Buffer> {
return this.getBody()
}

public etag(): string {
return this.remoteEtag || ''
}
}
4 changes: 2 additions & 2 deletions src/Stats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ export interface StatsObject {
}

export default class Stats implements StatsObject {
public source: string
public target: string
public source: string = ''
public target: string = ''
public diffs: ItemDiff[] = []
public distributions?: CloudFront.DistributionSummary[]
public invalidations?: string[]
Expand Down
10 changes: 6 additions & 4 deletions src/Websync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,12 @@ export default class Websync extends EventEmitter implements WebsyncEmitter {
private initialized: boolean = false
private completed: boolean = false

private diffs: ItemDiff[]
private diffs: ItemDiff[] = []
private stats: Stats
private transfer: Transfer
private transfer?: Transfer
private invalidations: string[] | undefined
private invalidator: CloudFrontInvalidator | undefined
private distributions: string[]
private distributions?: string[]

private completeCount = 0

Expand Down Expand Up @@ -197,7 +197,9 @@ export default class Websync extends EventEmitter implements WebsyncEmitter {
}

try {
await this.transfer.complete()
if (this.transfer !== undefined) {
await this.transfer.complete()
}
} catch (err) {
throw new Errors.TransferFailed(err)
}
Expand Down
2 changes: 1 addition & 1 deletion src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Options:
--config Provide a configuration file (js or json)
--include Pattern to include
--exclude Patter to exclude
--diffBy Property by which objects are diffed (modtime or size), default is modtime
--diffBy Property by which objects are diffed (etag, modtime or size), default is modtime
--wildcardPolicy Set the wildcard policy (majority, minority, or unanmious)
--wildcardAll Append wildcard to all invalidations
--invalidateDeletes Invalidate delete transfers
Expand Down
4 changes: 3 additions & 1 deletion src/diff.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Container, Item, ItemDiffType, ItemDiff, FilterOptions } from './types'

export type DiffKey = 'modtime' | 'size'
export type DiffKey = 'modtime' | 'size' | 'etag'

const getItem = (items: Item[], key: string): Item | undefined => {
for (let i = 0; i < items.length; i++) {
Expand All @@ -14,6 +14,8 @@ const shouldUpdate = (source: Item, target: Item, diffKey: DiffKey): boolean =>
return source.modtime.getTime() > target.modtime.getTime()
case 'size':
return source.size !== target.size
case 'etag':
return source.etag() !== target.etag()
}
}

Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export interface Item {
modtime: Date
size: number
isSymbolicLink: boolean
etag(): string
read(): Promise<Buffer>
}

Expand Down
3 changes: 3 additions & 0 deletions test/unit/S3Container.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ const createContainerTest = (prefix: string = '') => () => {
const item: Item = {
key: 'bang/a/b/d.txt',
modtime: new Date(),
etag: () => '3e25960a79dbc69b674cd4ec67a72c62',
size: body.length,
isSymbolicLink: false,
read: () => Promise.resolve(body),
Expand All @@ -89,6 +90,7 @@ const createContainerTest = (prefix: string = '') => () => {
const item: Item = {
key: 'test.txt',
modtime: new Date(),
etag: () => '3e25960a79dbc69b674cd4ec67a72c62',
size: body.length,
isSymbolicLink: false,
read: () => Promise.resolve(body),
Expand All @@ -114,6 +116,7 @@ const createContainerTest = (prefix: string = '') => () => {
const item: Item = {
key: 'index.html',
modtime: new Date(),
etag: () => '3e25960a79dbc69b674cd4ec67a72c62',
size: body.length,
isSymbolicLink: false,
read: () => Promise.resolve(body),
Expand Down
4 changes: 4 additions & 0 deletions test/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ export class MockItem implements Item {
public read(): Promise<Buffer> {
return Promise.resolve(new Buffer(this.size))
}

etag(): string {
return faker.random.word()
}
}

const randomContainerType = (): ContainerType =>
Expand Down