diff options
Diffstat (limited to 'src/sources')
| -rw-r--r-- | src/sources/FileSource.ts | 27 | ||||
| -rw-r--r-- | src/sources/HttpSource.ts | 22 | ||||
| -rw-r--r-- | src/sources/ISource.ts | 3 |
3 files changed, 52 insertions, 0 deletions
diff --git a/src/sources/FileSource.ts b/src/sources/FileSource.ts new file mode 100644 index 0000000..fd4f6f7 --- /dev/null +++ b/src/sources/FileSource.ts @@ -0,0 +1,27 @@ +import {readFile} from 'fs'; + +import {ISource} from './ISource'; + +export class FileSource implements ISource { + private _path: string; + private _data: string = null; + + constructor(path: string) { + this._path = path; + } + + getData(): Promise<string> { + if (this._data) { + return Promise.resolve(this._data); + } + + return new Promise<string>((resolve, reject) => { + readFile(this._path, (err, data) => { + if (err) return reject(err); + + this._data = data.toString(); + resolve(this._data); + }); + }); + } +} diff --git a/src/sources/HttpSource.ts b/src/sources/HttpSource.ts new file mode 100644 index 0000000..45ddd7a --- /dev/null +++ b/src/sources/HttpSource.ts @@ -0,0 +1,22 @@ +import {get} from 'http'; + +import {ISource} from './ISource'; + +export class HttpSource implements ISource { + private _uri: string; + + constructor(path: string) { + this._uri = path; + } + + getData(): Promise<string> { + return new Promise<string>((resolve, reject) => { + get(this._uri, (res) => { + let data = ''; + res.on('error', reject); + res.on('data', chunk => data += chunk); + res.on('end', () => resolve(data)); + }); + }); + } +} diff --git a/src/sources/ISource.ts b/src/sources/ISource.ts new file mode 100644 index 0000000..042aaae --- /dev/null +++ b/src/sources/ISource.ts @@ -0,0 +1,3 @@ +export interface ISource { + getData(): Promise<string>; +} |
