UNPKG

1.18 kBJavaScriptView Raw
1// Require initiator.listener
2
3var events = require ('events'),
4 urlUtil = require ('url'),
5 util = require ('util');
6
7
8var model = module.exports = function (url, optionalParams) {
9
10 var self = this;
11
12 if (url.constructor === String) {
13 try {
14 this.url = urlUtil.parse (url, true);
15 var a = this.url.protocol.length;
16 } catch (e) {
17 self.emit ('error', e);
18 }
19 } else {
20 this.url = url;
21 }
22
23 // convert:
24 // http: -> http
25 // https: -> http
26 // ftp: -> ftp
27 // sftp: -> ftp
28 this.modelName = this.url.protocol.replace(/(^s|:$|s:$)/g, '');
29
30 // console.log (this.modelName);
31 var requiredModel = require ('./'+this.modelName);
32 this.dataSource = new requiredModel (this, optionalParams);
33
34 // fetch method
35
36 this.fetch = function (target) {
37 self.dataSource.fetch(target);
38 }
39
40 this.store = function (target) {
41 self.dataSource.store(target);
42 }
43
44 this.stop = function (reason) {
45 if (self.dataSource.stop)
46 self.dataSource.stop (reason);
47 }
48
49 // this.init();
50}
51
52util.inherits (model, events.EventEmitter);
53
54// there are some examples:
55// fetch: url (call GET for http, RETR for ftp)
56// store: url (call POST for http, PUT for ftp, send email for mailto)