1 |
|
2 | (function() {
|
3 | var BufferStream, ClientRack, ConfigRack, EventEmitter, async, extend, fs, pathutil, pkgcloud, _ref,
|
4 | __hasProp = {}.hasOwnProperty,
|
5 | __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
|
6 |
|
7 | async = require('async');
|
8 |
|
9 | pkgcloud = require('pkgcloud');
|
10 |
|
11 | fs = require('fs');
|
12 |
|
13 | pathutil = require('path');
|
14 |
|
15 | _ref = require('./util'), BufferStream = _ref.BufferStream, extend = _ref.extend;
|
16 |
|
17 | ClientRack = require('./.').ClientRack;
|
18 |
|
19 | EventEmitter = require('events').EventEmitter;
|
20 |
|
21 | exports.Rack = (function(_super) {
|
22 |
|
23 | __extends(Rack, _super);
|
24 |
|
25 | function Rack(assets, options) {
|
26 | var asset, _i, _len,
|
27 | _this = this;
|
28 | Rack.__super__.constructor.call(this);
|
29 | if (options == null) {
|
30 | options = {};
|
31 | }
|
32 | this.maxAge = options.maxAge;
|
33 | this.allowNoHashCache = options.allowNoHashCache;
|
34 | this.on('complete', function() {
|
35 | return _this.completed = true;
|
36 | });
|
37 | this.on('newListener', function(event, listener) {
|
38 | if (event === 'complete' && _this.completed === true) {
|
39 | return listener();
|
40 | }
|
41 | });
|
42 | this.on('error', function(error) {
|
43 | if (_this.listeners('error').length === 1) {
|
44 | throw error;
|
45 | }
|
46 | });
|
47 | for (_i = 0, _len = assets.length; _i < _len; _i++) {
|
48 | asset = assets[_i];
|
49 | asset.rack = this;
|
50 | }
|
51 | this.assets = [];
|
52 | async.forEachSeries(assets, function(asset, next) {
|
53 | asset.on('error', function(error) {
|
54 | return next(error);
|
55 | });
|
56 | asset.on('complete', function() {
|
57 | if (asset.contents != null) {
|
58 | _this.assets.push(asset);
|
59 | }
|
60 | if (asset.assets != null) {
|
61 | _this.assets = _this.assets.concat(asset.assets);
|
62 | }
|
63 | return next();
|
64 | });
|
65 | asset.rack = _this;
|
66 | return asset.emit('start');
|
67 | }, function(error) {
|
68 | if (error != null) {
|
69 | return _this.emit('error', error);
|
70 | }
|
71 | return _this.emit('complete');
|
72 | });
|
73 | }
|
74 |
|
75 | Rack.prototype.createClientRack = function() {
|
76 | var clientRack;
|
77 | clientRack = new ClientRack;
|
78 | clientRack.rack = this;
|
79 | clientRack.emit('start');
|
80 | return clientRack;
|
81 | };
|
82 |
|
83 | Rack.prototype.addClientRack = function(next) {
|
84 | var clientRack,
|
85 | _this = this;
|
86 | clientRack = this.createClientRack();
|
87 | return clientRack.on('complete', function() {
|
88 | _this.assets.push(clientRack);
|
89 | return next();
|
90 | });
|
91 | };
|
92 |
|
93 | Rack.prototype.handle = function(request, response, next) {
|
94 | var handle,
|
95 | _this = this;
|
96 | response.locals({
|
97 | assets: this
|
98 | });
|
99 | handle = function() {
|
100 | var asset, check, _i, _len, _ref1;
|
101 | _ref1 = _this.assets;
|
102 | for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
|
103 | asset = _ref1[_i];
|
104 | check = asset.checkUrl(request.url);
|
105 | if (check) {
|
106 | return asset.respond(request, response);
|
107 | }
|
108 | }
|
109 | return next();
|
110 | };
|
111 | if (this.completed) {
|
112 | return handle();
|
113 | } else {
|
114 | return this.on('complete', handle);
|
115 | }
|
116 | };
|
117 |
|
118 | Rack.prototype.writeConfigFile = function(filename) {
|
119 | var asset, config, _i, _len, _ref1;
|
120 | config = {};
|
121 | _ref1 = this.assets;
|
122 | for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
|
123 | asset = _ref1[_i];
|
124 | config[asset.url] = asset.specificUrl;
|
125 | }
|
126 | return fs.writeFileSync(filename, JSON.stringify(config));
|
127 | };
|
128 |
|
129 | Rack.prototype.deploy = function(options, next) {
|
130 | var deploy,
|
131 | _this = this;
|
132 | options.keyId = options.accessKey;
|
133 | options.key = options.secretKey;
|
134 | deploy = function() {
|
135 | var assets, client;
|
136 | client = pkgcloud.storage.createClient(options);
|
137 | assets = _this.assets;
|
138 | if (options.provider === 'rackspace') {
|
139 | assets = _this.assets.concat(_this.assets[0]);
|
140 | }
|
141 | return async.forEachSeries(assets, function(asset, next) {
|
142 | var clientOptions, headers, key, stream, url, value, _ref1;
|
143 | console.log(asset.url);
|
144 | stream = null;
|
145 | if (asset.gzip) {
|
146 | stream = new BufferStream(asset.gzipContents);
|
147 | } else {
|
148 | stream = new BufferStream(asset.contents);
|
149 | }
|
150 | url = asset.specificUrl.slice(1, asset.specificUrl.length);
|
151 | headers = {};
|
152 | _ref1 = asset.headers;
|
153 | for (key in _ref1) {
|
154 | value = _ref1[key];
|
155 | headers[key] = value;
|
156 | }
|
157 | if (options.provider === 'amazon') {
|
158 | headers['x-amz-acl'] = 'public-read';
|
159 | }
|
160 | clientOptions = {
|
161 | container: options.container,
|
162 | remote: url,
|
163 | headers: headers,
|
164 | stream: stream
|
165 | };
|
166 | return client.upload(clientOptions, function(error) {
|
167 | if (error != null) {
|
168 | return next(error);
|
169 | }
|
170 | return next();
|
171 | });
|
172 | }, function(error) {
|
173 | if (error != null) {
|
174 | if (next != null) {
|
175 | return next(error);
|
176 | }
|
177 | throw error;
|
178 | }
|
179 | if (options.configFile != null) {
|
180 | _this.writeConfigFile(options.configFile);
|
181 | }
|
182 | if (next != null) {
|
183 | return next();
|
184 | }
|
185 | });
|
186 | };
|
187 | if (this.completed) {
|
188 | return deploy();
|
189 | } else {
|
190 | return this.on('complete', deploy);
|
191 | }
|
192 | };
|
193 |
|
194 | Rack.prototype.tag = function(url) {
|
195 | var asset, _i, _len, _ref1;
|
196 | _ref1 = this.assets;
|
197 | for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
|
198 | asset = _ref1[_i];
|
199 | if (asset.url === url) {
|
200 | return asset.tag();
|
201 | }
|
202 | }
|
203 | throw new Error("No asset found for url: " + url);
|
204 | };
|
205 |
|
206 | Rack.prototype.url = function(url) {
|
207 | var asset, _i, _len, _ref1;
|
208 | _ref1 = this.assets;
|
209 | for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
|
210 | asset = _ref1[_i];
|
211 | if (url === asset.url) {
|
212 | return asset.specificUrl;
|
213 | }
|
214 | }
|
215 | };
|
216 |
|
217 | Rack.extend = extend;
|
218 |
|
219 | return Rack;
|
220 |
|
221 | })(EventEmitter);
|
222 |
|
223 | ConfigRack = (function() {
|
224 |
|
225 | function ConfigRack(options) {
|
226 | if (options.configFile == null) {
|
227 | throw new Error('options.configFile is required');
|
228 | }
|
229 | if (options.hostname == null) {
|
230 | throw new Error('options.hostname is required');
|
231 | }
|
232 | this.assetMap = require(options.configFile);
|
233 | this.hostname = options.hostname;
|
234 | }
|
235 |
|
236 | ConfigRack.prototype.handle = function(request, response, next) {
|
237 | var specificUrl, url, _ref1;
|
238 | response.locals({
|
239 | assets: this
|
240 | });
|
241 | _ref1 = this.assetMap;
|
242 | for (url in _ref1) {
|
243 | specificUrl = _ref1[url];
|
244 | if (request.url === url || request.url === specificUrl) {
|
245 | return response.redirect("//" + this.hostname + specificUrl);
|
246 | }
|
247 | }
|
248 | return next();
|
249 | };
|
250 |
|
251 | ConfigRack.prototype.tag = function(url) {
|
252 | var tag;
|
253 | switch (pathutil.extname(url)) {
|
254 | case '.js':
|
255 | tag = "\n<script type=\"text/javascript\" ";
|
256 | return tag += "src=\"//" + this.hostname + this.assetMap[url] + "\"></script>";
|
257 | case '.css':
|
258 | return "\n<link rel=\"stylesheet\" href=\"//" + this.hostname + this.assetMap[url] + "\">";
|
259 | }
|
260 | };
|
261 |
|
262 | ConfigRack.prototype.url = function(url) {
|
263 | return "//" + this.hostname + this.assetMap[url];
|
264 | };
|
265 |
|
266 | return ConfigRack;
|
267 |
|
268 | })();
|
269 |
|
270 | exports.fromConfigFile = function(options) {
|
271 | return new ConfigRack(options);
|
272 | };
|
273 |
|
274 | }).call(this);
|