forked from deployd/deployd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresource.js
196 lines (169 loc) · 4.85 KB
/
resource.js
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
var parse = require('url').parse
, EventEmitter = require('events').EventEmitter
, util = require('util')
, path = require('path')
, fs = require('fs')
, Script = require('./script');
/**
* A `Resource` handles incoming requests at a matched url. The base class is designed
* to be extended by overriding methods that will be called by a `Router`.
*
* A `Resource` is also an `EventEmitter`. The following events are available.
*
* - `changed` after a resource config has changed
* - `deleted` after a resource config has been deleted
*
* Options:
*
* - `path` the base path a resource should handle
* - `db` the database a resource will use for persistence
*
* Example:
*
* The following resource would respond with a file at the url `/my-file.html`.
*
* function MyFileResource(name, options) {
* Resource.apply(this, arguments);
*
* this.on('changed', function(config) {
* console.log('MyFileResource changed', config);
* });
* }
* util.inherits(MyFileResource, Resource);
*
* FileResource.prototype.handle = function (ctx, next) {
* if (ctx.url === '/my-file.html') {
* fs.createReadStream('my-file.html').pipe(ctx.res);
* } else {
* next();
* }
* }
*
* @param {Object} options
* @api private
*/
function Resource(name, options) {
EventEmitter.call(this);
this.name = name;
this.path = '/' + name;
options = this.options = options || {};
this.config = options.config || {};
this.events = {};
var instance = this;
if(this.constructor.external) {
instance.external = {};
Object.keys(this.constructor.external).forEach(function (key) {
if(typeof instance.constructor.external[key] == 'function') {
instance.external[key] = function () {
instance.constructor.external[key].apply(instance, arguments);
};
}
});
}
}
/**
* The external prototype for exposing methods over http and to dpd.js
*/
Resource.external = {};
util.inherits(Resource, EventEmitter);
/**
* Parse the `url` into a basepath, query, and parts.
*
* @param {String} url
* @return {Object}
* @api private
*/
Resource.prototype.parse = function (url) {
var parsed = parse(url, true)
, pathname = parsed.pathname
, parts = parsed.parts = pathname.split('/');
// remove empty
parts.shift();
parsed.basepath = parts[0];
// remove empty trailing slash part
if(parts[parts.length - 1] === '') parts.pop();
// the last part is always the identifier
if(parts.length > 1) parsed.id = parts[parts.length - 1];
if(parsed.query.q && parsed.query.q[0] === '{' && parsed.query.q[parsed.query.q.length - 1] === '}') {
parsed.query.q = JSON.parse(parsed.query.q);
}
return parsed;
};
Resource.prototype.load = function (fn) {
var resource = this
, eventNames = this.constructor && this.constructor.events
, remaining = eventNames && eventNames.length
, configPath = this.options && this.options.configPath
, events = this.events = {};
if(remaining) {
eventNames.forEach(function(e) {
var fileName = e.toLowerCase() + '.js'
, filePath = path.join(configPath, fileName);
Script.load(filePath, function (err, script) {
if (script) {
events[e] = script;
}
remaining--;
if (remaining <= 0) {
fn();
}
});
});
} else {
fn();
}
};
/**
* Handle an incoming request. This gets called by the router.
* Call `next()` if the resource cannot handle the request.
* Otherwise call `cxt.done(err, res)` when the resource
* is ready to respond.
*
* Example:
*
* Override the handle method to return a string:
*
* function MyResource(settings) {
* Resource.apply(this, arguments);
* }
* util.inherits(MyResource, Resource);
*
* MyResource.prototype.handle = function (ctx, next) {
* // respond with the file contents (or an error if one occurs)
* fs.readFile('myfile.txt', ctx.done);
* }
*
* @param {Context} ctx
* @param {function} next
*/
Resource.prototype.handle = function (ctx, next) {
ctx.end();
};
/**
* Turn a resource constructor into an object ready
* for JSON. It should atleast include the `type`
* and `defaultPath`.
*/
Resource.toJSON = function() {
return {
type: this.name,
defaultPath: '/my-resource'
};
};
/*!
* If true, generates utility functions for this resource in dpd.js
*/
Resource.prototype.clientGeneration = false;
/*!
* If clientGeneration is true, generates utility functions that alias to get(path)
*/
Resource.prototype.clientGenerationGet = [];
/*!
* If clientGeneration is true, generates utility functions that alias to do(path)
*/
Resource.prototype.clientGenerationExec = [];
/*!
* Resource tag, for duck typing
*/
Resource.prototype.__resource__ = true;
module.exports = Resource;