Skip to content

Instantly share code, notes, and snippets.

@loderunner
Last active October 10, 2019 11:17
Show Gist options
  • Save loderunner/4c24ab4f7684f1b180c148a01de35f63 to your computer and use it in GitHub Desktop.
Save loderunner/4c24ab4f7684f1b180c148a01de35f63 to your computer and use it in GitHub Desktop.
interface Reader {
read(): Promise<Buffer>;
}
class FileReader implements Reader {
private path: string;
constructor(path: string) {
this.path = path;
}
async read() {
return fs.readFileSync(path);
}
}
class URLReader implements Reader {
private url: string;
constructor(url: string) {
this.url = url;
}
async read() {
return new Promise((resolve, reject) => {
http.get(this.url, res => {
// stream data from response and resolve Promise
});
}
}
}
interface Renamable {
name: string
modificationDate: Date;
}
function rename(r: Renamable, name: string) {
r.name = name;
r.modificationDate = new Date();
}
class File implements Renamable {
name: string;
creationDate: Date;
modificationDate: Date;
contents: Buffer;
}
class Bucket implements Renamable {
name: string;
creationDate: Date;
modificationDate: Date;
files: File[]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment