-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmirrorpublisher.ts
52 lines (42 loc) · 1.5 KB
/
mirrorpublisher.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import stream = require('stream');
import Publisher from './publisher';
class MirrorPublisher implements Publisher {
publishers: Publisher[];
constructor(config: {primary: any, secondary: any}) {
this.publishers = [config.primary, config.secondary].map(c => Publisher.getInstance(c));
}
async put(path: string, obj: string | NodeJS.ReadableStream, contentType?: string) {
await Promise.all(this.publishers.map(p => {
if (typeof obj === 'string') {
return p.put(path, obj, contentType);
} else {
let clone = new stream.PassThrough();
obj.pipe(clone);
// work around aws-sdk kludge
if (typeof obj.path === 'string') {
clone.path = obj.path;
}
return p.put(path, clone, contentType);
}
}));
}
async delete(path: string, contentType: string) {
await Promise.all(this.publishers.map(p => p.delete(path, contentType)));
}
get(path: string) {
return this.publishers[0].get(path);
}
exists(path: string) {
return this.publishers[0].exists(path);
}
list() {
return this.publishers[0].list();
}
async rollback() {
await Promise.all(this.publishers.map(p => p.rollback()));
}
async commit(msg: string) {
await Promise.all(this.publishers.map(p => p.commit(msg)));
}
}
export = MirrorPublisher;