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