1 | "use strict";
|
2 | var __extends = (this && this.__extends) || function (d, b) {
|
3 | for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
|
4 | function __() { this.constructor = d; }
|
5 | d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
6 | };
|
7 | var core_1 = require('@angular/core');
|
8 | var http_1 = require('@angular/http');
|
9 | var Observable_1 = require('rxjs/Observable');
|
10 | require('rxjs/add/operator/map');
|
11 | var http = require('http');
|
12 | var https = require('https');
|
13 | var url = require('url');
|
14 | var tokens_1 = require('./tokens');
|
15 | var helper_1 = require('./helper');
|
16 | var JSONP_ERR_WRONG_METHOD = 'JSONP requests must use GET request method.';
|
17 | var PreloadHttp = (function (_super) {
|
18 | __extends(PreloadHttp, _super);
|
19 | function PreloadHttp(_backend, _defaultOptions) {
|
20 | _super.call(this, _backend, _defaultOptions);
|
21 | this._backend = _backend;
|
22 | this._defaultOptions = _defaultOptions;
|
23 | this._async = 0;
|
24 | }
|
25 | PreloadHttp.prototype.preload = function (_url, factory) {
|
26 | var _this = this;
|
27 | var obs = new core_1.EventEmitter(false);
|
28 | this._async += 1;
|
29 | var request = factory();
|
30 | request
|
31 | .subscribe({
|
32 | next: function (response) {
|
33 | obs.next(response);
|
34 | },
|
35 | error: function (e) {
|
36 | obs.error(e);
|
37 | _this._async -= 1;
|
38 | },
|
39 | complete: function () {
|
40 | obs.complete();
|
41 | _this._async -= 1;
|
42 | }
|
43 | });
|
44 | return obs;
|
45 | };
|
46 | PreloadHttp.prototype.request = function (url, options) {
|
47 | var _this = this;
|
48 | return this.preload(url, function () { return _super.prototype.request.call(_this, url, options); });
|
49 | };
|
50 | PreloadHttp.prototype.get = function (url, options) {
|
51 | var _this = this;
|
52 | return this.preload(url, function () { return _super.prototype.get.call(_this, url, options); });
|
53 | };
|
54 | PreloadHttp.prototype.post = function (url, body, options) {
|
55 | var _this = this;
|
56 | return this.preload(url, function () { return _super.prototype.post.call(_this, url, body, options); });
|
57 | };
|
58 | PreloadHttp.prototype.put = function (url, body, options) {
|
59 | var _this = this;
|
60 | return this.preload(url, function () { return _super.prototype.put.call(_this, url, body, options); });
|
61 | };
|
62 | PreloadHttp.prototype.delete = function (url, options) {
|
63 | var _this = this;
|
64 | return this.preload(url, function () { return _super.prototype.delete.call(_this, url, options); });
|
65 | };
|
66 | PreloadHttp.prototype.patch = function (url, body, options) {
|
67 | var _this = this;
|
68 | return this.preload(url, function () { return _super.prototype.patch.call(_this, url, body, options); });
|
69 | };
|
70 | PreloadHttp.prototype.head = function (url, options) {
|
71 | var _this = this;
|
72 | return this.preload(url, function () { return _super.prototype.head.call(_this, url, options); });
|
73 | };
|
74 | return PreloadHttp;
|
75 | }(http_1.Http));
|
76 | exports.PreloadHttp = PreloadHttp;
|
77 | var NodeConnection = (function () {
|
78 | function NodeConnection(req, baseResponseOptions, originUrl, baseUrl) {
|
79 | if (originUrl === void 0) { originUrl = ''; }
|
80 | this.setDetectedContentType = http_1.XHRConnection.prototype.setDetectedContentType;
|
81 | this.request = req;
|
82 | baseUrl = baseUrl || '/';
|
83 | if (originUrl === null) {
|
84 | throw new Error('ERROR: Please move ORIGIN_URL to platformProviders');
|
85 | }
|
86 | var _reqInfo = url.parse(url.resolve(url.resolve(originUrl, baseUrl), req.url));
|
87 | _reqInfo.method = http_1.RequestMethod[req.method].toUpperCase();
|
88 | if (helper_1.isPresent(req.headers)) {
|
89 | _reqInfo.headers = {};
|
90 | req.headers.forEach(function (values, name) { return _reqInfo.headers[name] = values.join(','); });
|
91 | }
|
92 | _reqInfo.headers = _reqInfo.headers || {};
|
93 | _reqInfo.headers['user-agent'] = _reqInfo.headers['user-agent'] || 'Angular 2 Universal';
|
94 | this.response = new Observable_1.Observable(function (responseObserver) {
|
95 | var nodeReq;
|
96 | var xhrHttp = http;
|
97 | if (_reqInfo.protocol === 'https:') {
|
98 | xhrHttp = https;
|
99 | }
|
100 | nodeReq = xhrHttp.request(_reqInfo, function (res) {
|
101 | var body = '';
|
102 | res.on('data', function (chunk) { return body += chunk; });
|
103 | var status = res.statusCode;
|
104 | var headers = new http_1.Headers(res.headers);
|
105 | var url = res.url;
|
106 | res.on('end', function () {
|
107 | var responseOptions = new http_1.ResponseOptions({ body: body, status: status, headers: headers, url: url });
|
108 | var response = new http_1.Response(responseOptions);
|
109 | if (helper_1.isSuccess(status)) {
|
110 | responseObserver.next(response);
|
111 | responseObserver.complete();
|
112 | return;
|
113 | }
|
114 | responseObserver.error(response);
|
115 | });
|
116 | });
|
117 | var onError = function (err) {
|
118 | var responseOptions = new http_1.ResponseOptions({ body: err, type: http_1.ResponseType.Error });
|
119 | if (helper_1.isPresent(baseResponseOptions)) {
|
120 | responseOptions = baseResponseOptions.merge(responseOptions);
|
121 | }
|
122 | responseObserver.error(new http_1.Response(responseOptions));
|
123 | };
|
124 | nodeReq.on('error', onError);
|
125 | nodeReq.write(req.text());
|
126 | nodeReq.end();
|
127 | return function () {
|
128 | nodeReq.removeListener('error', onError);
|
129 | nodeReq.abort();
|
130 | };
|
131 | });
|
132 | }
|
133 | NodeConnection.decorators = [
|
134 | { type: core_1.Injectable },
|
135 | ];
|
136 | NodeConnection.ctorParameters = [
|
137 | { type: http_1.Request, },
|
138 | { type: http_1.ResponseOptions, },
|
139 | { type: undefined, decorators: [{ type: core_1.Inject, args: [tokens_1.ORIGIN_URL,] },] },
|
140 | { type: undefined, decorators: [{ type: core_1.Optional }, { type: core_1.Inject, args: [tokens_1.APP_BASE_HREF,] },] },
|
141 | ];
|
142 | return NodeConnection;
|
143 | }());
|
144 | exports.NodeConnection = NodeConnection;
|
145 | var NodeBackend = (function (_super) {
|
146 | __extends(NodeBackend, _super);
|
147 | function NodeBackend(baseResponseOptions, _browserXHR, _xsrfStrategy, _ngZone, _baseUrl, _originUrl) {
|
148 | _super.call(this, _browserXHR, baseResponseOptions, _xsrfStrategy);
|
149 | this.baseResponseOptions = baseResponseOptions;
|
150 | this._baseUrl = _baseUrl;
|
151 | this._originUrl = _originUrl;
|
152 | }
|
153 | NodeBackend.prototype.createConnection = function (request) {
|
154 | return new NodeConnection(request, this.baseResponseOptions, this._baseUrl, this._originUrl);
|
155 | };
|
156 | NodeBackend.decorators = [
|
157 | { type: core_1.Injectable },
|
158 | ];
|
159 | NodeBackend.ctorParameters = [
|
160 | { type: http_1.ResponseOptions, },
|
161 | { type: http_1.BrowserXhr, },
|
162 | { type: http_1.XSRFStrategy, },
|
163 | { type: core_1.NgZone, },
|
164 | { type: undefined, decorators: [{ type: core_1.Inject, args: [tokens_1.APP_BASE_HREF,] },] },
|
165 | { type: undefined, decorators: [{ type: core_1.Inject, args: [tokens_1.ORIGIN_URL,] },] },
|
166 | ];
|
167 | return NodeBackend;
|
168 | }(http_1.XHRBackend));
|
169 | exports.NodeBackend = NodeBackend;
|
170 | var NodeJSONPConnection = (function () {
|
171 | function NodeJSONPConnection(req, baseResponseOptions, ngZone, originUrl, baseUrl) {
|
172 | if (originUrl === void 0) { originUrl = ''; }
|
173 | if (req.method !== http_1.RequestMethod.Get) {
|
174 | throw new TypeError(JSONP_ERR_WRONG_METHOD);
|
175 | }
|
176 | this.request = req;
|
177 | baseUrl = baseUrl || '/';
|
178 | if (originUrl === null) {
|
179 | throw new Error('ERROR: Please move ORIGIN_URL to platformProviders');
|
180 | }
|
181 | var _reqInfo = url.parse(url.resolve(url.resolve(originUrl, baseUrl), req.url));
|
182 | _reqInfo.method = http_1.RequestMethod[req.method].toUpperCase();
|
183 | if (helper_1.isPresent(req.headers)) {
|
184 | _reqInfo.headers = {};
|
185 | req.headers.forEach(function (values, name) { return _reqInfo.headers[name] = values.join(','); });
|
186 | }
|
187 | _reqInfo.headers = _reqInfo.headers || {};
|
188 | _reqInfo.headers['user-agent'] = _reqInfo.headers['user-agent'] || 'Angular 2 Universal';
|
189 | this.response = new Observable_1.Observable(function (responseObserver) {
|
190 | var nodeReq;
|
191 | var xhrHttp = http;
|
192 | function DONE(response) {
|
193 | responseObserver.next(response);
|
194 | responseObserver.complete();
|
195 | }
|
196 | var __done = Zone.current.wrap(DONE, 'jsonp');
|
197 | if (_reqInfo.protocol === 'https:') {
|
198 | xhrHttp = https;
|
199 | }
|
200 | nodeReq = xhrHttp.request(_reqInfo, function (res) {
|
201 | var body = '';
|
202 | res.on('data', function (chunk) { return body += chunk; });
|
203 | var status = res.statusCode;
|
204 | var headers = new http_1.Headers(res.headers);
|
205 | var url = res.url;
|
206 | res.on('end', function () {
|
207 | var responseJson;
|
208 | try {
|
209 | if (body.indexOf('JSONP_CALLBACK') === -1) {
|
210 | throw new Error('Http request ' + req.url + ' did not return the response with JSONP_CALLBACK()');
|
211 | }
|
212 | var responseFactory = new Function('JSONP_CALLBACK', body);
|
213 | responseFactory(function (json) {
|
214 | responseJson = json;
|
215 | });
|
216 | }
|
217 | catch (e) {
|
218 | console.log('JSONP Error:', e);
|
219 | return onError(e);
|
220 | }
|
221 | var responseOptions = new http_1.ResponseOptions({ body: responseJson, status: status, headers: headers, url: url });
|
222 | var response = new http_1.Response(responseOptions);
|
223 | if (helper_1.isSuccess(status)) {
|
224 | __done(response);
|
225 | return;
|
226 | }
|
227 | ngZone.run(function () {
|
228 | responseObserver.error(response);
|
229 | });
|
230 | });
|
231 | });
|
232 | function onError(err) {
|
233 | var responseOptions = new http_1.ResponseOptions({ body: err, type: http_1.ResponseType.Error });
|
234 | if (helper_1.isPresent(baseResponseOptions)) {
|
235 | responseOptions = baseResponseOptions.merge(responseOptions);
|
236 | }
|
237 | responseObserver.error(new http_1.Response(responseOptions));
|
238 | }
|
239 | ;
|
240 | nodeReq.on('error', onError);
|
241 | nodeReq.end();
|
242 | return function () {
|
243 | nodeReq.removeListener('error', onError);
|
244 | nodeReq.abort();
|
245 | };
|
246 | });
|
247 | }
|
248 | NodeJSONPConnection.ctorParameters = [
|
249 | { type: http_1.Request, },
|
250 | { type: http_1.ResponseOptions, },
|
251 | { type: core_1.NgZone, },
|
252 | { type: undefined, decorators: [{ type: core_1.Optional }, { type: core_1.Inject, args: [tokens_1.ORIGIN_URL,] },] },
|
253 | { type: undefined, decorators: [{ type: core_1.Optional }, { type: core_1.Inject, args: [tokens_1.APP_BASE_HREF,] },] },
|
254 | ];
|
255 | return NodeJSONPConnection;
|
256 | }());
|
257 | exports.NodeJSONPConnection = NodeJSONPConnection;
|
258 | var NodeJsonpBackend = (function (_super) {
|
259 | __extends(NodeJsonpBackend, _super);
|
260 | function NodeJsonpBackend() {
|
261 | _super.apply(this, arguments);
|
262 | }
|
263 | return NodeJsonpBackend;
|
264 | }(http_1.ConnectionBackend));
|
265 | exports.NodeJsonpBackend = NodeJsonpBackend;
|
266 | var NodeJsonpBackend_ = (function (_super) {
|
267 | __extends(NodeJsonpBackend_, _super);
|
268 | function NodeJsonpBackend_(_baseResponseOptions, _ngZone, _baseUrl, _originUrl) {
|
269 | _super.call(this);
|
270 | this._baseResponseOptions = _baseResponseOptions;
|
271 | this._ngZone = _ngZone;
|
272 | this._baseUrl = _baseUrl;
|
273 | this._originUrl = _originUrl;
|
274 | }
|
275 | NodeJsonpBackend_.prototype.createConnection = function (request) {
|
276 | return new NodeJSONPConnection(request, this._baseResponseOptions, this._ngZone, this._baseUrl, this._originUrl);
|
277 | };
|
278 | NodeJsonpBackend_.decorators = [
|
279 | { type: core_1.Injectable },
|
280 | ];
|
281 | NodeJsonpBackend_.ctorParameters = [
|
282 | { type: http_1.ResponseOptions, },
|
283 | { type: core_1.NgZone, },
|
284 | { type: undefined, decorators: [{ type: core_1.Inject, args: [tokens_1.APP_BASE_HREF,] },] },
|
285 | { type: undefined, decorators: [{ type: core_1.Inject, args: [tokens_1.ORIGIN_URL,] },] },
|
286 | ];
|
287 | return NodeJsonpBackend_;
|
288 | }(NodeJsonpBackend));
|
289 | exports.NodeJsonpBackend_ = NodeJsonpBackend_;
|
290 | function _noop() {
|
291 | }
|
292 | exports._noop = _noop;
|
293 | exports.NODE_HTTP_PROVIDERS_COMMON = [
|
294 | { provide: http_1.XSRFStrategy, useValue: _noop },
|
295 | { provide: http_1.BrowserXhr, useValue: _noop },
|
296 | { provide: http_1.RequestOptions, useClass: http_1.BaseRequestOptions },
|
297 | { provide: http_1.ResponseOptions, useClass: http_1.BaseResponseOptions }
|
298 | ];
|
299 | exports.NODE_HTTP_PROVIDERS = exports.NODE_HTTP_PROVIDERS_COMMON.concat([
|
300 | { provide: http_1.Http, useFactory: httpFactory, deps: [http_1.XHRBackend, http_1.RequestOptions] },
|
301 | { provide: http_1.XHRBackend, useClass: NodeBackend },
|
302 | ]);
|
303 | exports.NODE_JSONP_PROVIDERS = exports.NODE_HTTP_PROVIDERS_COMMON.concat([
|
304 | { provide: http_1.Jsonp, useFactory: jsonpFactory, deps: [http_1.JSONPBackend, http_1.RequestOptions] },
|
305 | { provide: http_1.JSONPBackend, useClass: NodeJsonpBackend_ },
|
306 | ]);
|
307 | function httpFactory(xhrBackend, requestOptions) {
|
308 | return new PreloadHttp(xhrBackend, requestOptions);
|
309 | }
|
310 | exports.httpFactory = httpFactory;
|
311 | function jsonpFactory(jsonpBackend, requestOptions) {
|
312 | return new PreloadHttp(jsonpBackend, requestOptions);
|
313 | }
|
314 | exports.jsonpFactory = jsonpFactory;
|
315 | var NodeHttpModule = (function () {
|
316 | function NodeHttpModule() {
|
317 | }
|
318 | NodeHttpModule.forRoot = function (config) {
|
319 | if (config === void 0) { config = {}; }
|
320 | return NodeHttpModule.withConfig(config);
|
321 | };
|
322 | NodeHttpModule.withConfig = function (config) {
|
323 | if (config === void 0) { config = {}; }
|
324 | var providers = [];
|
325 | if (config.baseUrl) {
|
326 | providers.push({ provide: tokens_1.APP_BASE_HREF, useValue: config.baseUrl });
|
327 | }
|
328 | if (config.requestUrl) {
|
329 | providers.push({ provide: tokens_1.REQUEST_URL, useValue: config.requestUrl });
|
330 | }
|
331 | if (config.originUrl) {
|
332 | providers.push({ provide: tokens_1.ORIGIN_URL, useValue: config.originUrl });
|
333 | }
|
334 | return {
|
335 | ngModule: NodeHttpModule,
|
336 | providers: providers
|
337 | };
|
338 | };
|
339 | NodeHttpModule.decorators = [
|
340 | { type: core_1.NgModule, args: [{
|
341 | providers: exports.NODE_HTTP_PROVIDERS
|
342 | },] },
|
343 | ];
|
344 | NodeHttpModule.ctorParameters = [];
|
345 | return NodeHttpModule;
|
346 | }());
|
347 | exports.NodeHttpModule = NodeHttpModule;
|
348 | var NodeJsonpModule = (function () {
|
349 | function NodeJsonpModule() {
|
350 | }
|
351 | NodeJsonpModule.forRoot = function (config) {
|
352 | if (config === void 0) { config = {}; }
|
353 | return NodeJsonpModule.withConfig(config);
|
354 | };
|
355 | NodeJsonpModule.withConfig = function (config) {
|
356 | if (config === void 0) { config = {}; }
|
357 | var providers = [];
|
358 | if (config.baseUrl) {
|
359 | providers.push({ provide: tokens_1.APP_BASE_HREF, useValue: config.baseUrl });
|
360 | }
|
361 | if (config.requestUrl) {
|
362 | providers.push({ provide: tokens_1.REQUEST_URL, useValue: config.requestUrl });
|
363 | }
|
364 | if (config.originUrl) {
|
365 | providers.push({ provide: tokens_1.ORIGIN_URL, useValue: config.originUrl });
|
366 | }
|
367 | return {
|
368 | ngModule: NodeJsonpModule,
|
369 | providers: providers
|
370 | };
|
371 | };
|
372 | NodeJsonpModule.decorators = [
|
373 | { type: core_1.NgModule, args: [{
|
374 | providers: exports.NODE_JSONP_PROVIDERS
|
375 | },] },
|
376 | ];
|
377 | NodeJsonpModule.ctorParameters = [];
|
378 | return NodeJsonpModule;
|
379 | }());
|
380 | exports.NodeJsonpModule = NodeJsonpModule;
|
381 |
|
\ | No newline at end of file |