Skip to content

Commit 9745f55

Browse files
CaerusKarumhevery
authored andcommittedMar 27, 2019
feat: remove @angular/http dependency from @angular/platform-server (#29408)
PR Close #29408
1 parent c981006 commit 9745f55

File tree

6 files changed

+5
-192
lines changed

6 files changed

+5
-192
lines changed
 

‎packages/platform-server/BUILD.bazel

-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ ng_module(
1616
"//packages/common/http",
1717
"//packages/compiler",
1818
"//packages/core",
19-
"//packages/http",
2019
"//packages/platform-browser",
2120
"//packages/platform-browser-dynamic",
2221
"//packages/platform-browser/animations",

‎packages/platform-server/package.json

-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
"@angular/common": "0.0.0-PLACEHOLDER",
1818
"@angular/compiler": "0.0.0-PLACEHOLDER",
1919
"@angular/core": "0.0.0-PLACEHOLDER",
20-
"@angular/http": "0.0.0-PLACEHOLDER",
2120
"@angular/platform-browser": "0.0.0-PLACEHOLDER",
2221
"@angular/platform-browser-dynamic": "0.0.0-PLACEHOLDER"
2322
},

‎packages/platform-server/src/http.ts

+4-54
Original file line numberDiff line numberDiff line change
@@ -6,33 +6,20 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9+
910
const xhr2: any = require('xhr2');
1011

11-
import {Injectable, Injector, Optional, Provider, InjectFlags} from '@angular/core';
12-
import {BrowserXhr, Connection, ConnectionBackend, Http, ReadyState, Request, RequestOptions, Response, XHRBackend, XSRFStrategy} from '@angular/http';
12+
import {Injectable, Injector, Provider} from '@angular/core';
1313

14-
import {HttpEvent, HttpRequest, HttpHandler, HttpInterceptor, HTTP_INTERCEPTORS, HttpBackend, XhrFactory, ɵHttpInterceptingHandler as HttpInterceptingHandler} from '@angular/common/http';
14+
import {HttpEvent, HttpRequest, HttpHandler, HttpBackend, XhrFactory, ɵHttpInterceptingHandler as HttpInterceptingHandler} from '@angular/common/http';
1515

1616
import {Observable, Observer, Subscription} from 'rxjs';
1717

18-
const isAbsoluteUrl = /^[a-zA-Z\-\+.]+:\/\//;
19-
20-
function validateRequestUrl(url: string): void {
21-
if (!isAbsoluteUrl.test(url)) {
22-
throw new Error(`URLs requested via Http on the server must be absolute. URL: ${url}`);
23-
}
24-
}
25-
2618
@Injectable()
27-
export class ServerXhr implements BrowserXhr {
19+
export class ServerXhr implements XhrFactory {
2820
build(): XMLHttpRequest { return new xhr2.XMLHttpRequest(); }
2921
}
3022

31-
@Injectable()
32-
export class ServerXsrfStrategy implements XSRFStrategy {
33-
configureRequest(req: Request): void {}
34-
}
35-
3623
export abstract class ZoneMacroTaskWrapper<S, R> {
3724
wrap(request: S): Observable<R> {
3825
return new Observable((observer: Observer<R>) => {
@@ -111,36 +98,6 @@ export abstract class ZoneMacroTaskWrapper<S, R> {
11198
protected abstract delegate(request: S): Observable<R>;
11299
}
113100

114-
export class ZoneMacroTaskConnection extends ZoneMacroTaskWrapper<Request, Response> implements
115-
Connection {
116-
response: Observable<Response>;
117-
// TODO(issue/24571): remove '!'.
118-
lastConnection !: Connection;
119-
120-
constructor(public request: Request, private backend: XHRBackend) {
121-
super();
122-
validateRequestUrl(request.url);
123-
this.response = this.wrap(request);
124-
}
125-
126-
delegate(request: Request): Observable<Response> {
127-
this.lastConnection = this.backend.createConnection(request);
128-
return this.lastConnection.response as Observable<Response>;
129-
}
130-
131-
get readyState(): ReadyState {
132-
return !!this.lastConnection ? this.lastConnection.readyState : ReadyState.Unsent;
133-
}
134-
}
135-
136-
export class ZoneMacroTaskBackend implements ConnectionBackend {
137-
constructor(private backend: XHRBackend) {}
138-
139-
createConnection(request: any): ZoneMacroTaskConnection {
140-
return new ZoneMacroTaskConnection(request, this.backend);
141-
}
142-
}
143-
144101
export class ZoneClientBackend extends
145102
ZoneMacroTaskWrapper<HttpRequest<any>, HttpEvent<any>> implements HttpBackend {
146103
constructor(private backend: HttpBackend) { super(); }
@@ -152,19 +109,12 @@ export class ZoneClientBackend extends
152109
}
153110
}
154111

155-
export function httpFactory(xhrBackend: XHRBackend, options: RequestOptions) {
156-
const macroBackend = new ZoneMacroTaskBackend(xhrBackend);
157-
return new Http(macroBackend, options);
158-
}
159-
160112
export function zoneWrappedInterceptingHandler(backend: HttpBackend, injector: Injector) {
161113
const realBackend: HttpBackend = new HttpInterceptingHandler(backend, injector);
162114
return new ZoneClientBackend(realBackend);
163115
}
164116

165117
export const SERVER_HTTP_PROVIDERS: Provider[] = [
166-
{provide: Http, useFactory: httpFactory, deps: [XHRBackend, RequestOptions]},
167-
{provide: BrowserXhr, useClass: ServerXhr}, {provide: XSRFStrategy, useClass: ServerXsrfStrategy},
168118
{provide: XhrFactory, useClass: ServerXhr}, {
169119
provide: HttpHandler,
170120
useFactory: zoneWrappedInterceptingHandler,

‎packages/platform-server/src/server.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import {ɵAnimationEngine} from '@angular/animations/browser';
1010
import {DOCUMENT, PlatformLocation, ViewportScroller, ɵNullViewportScroller as NullViewportScroller, ɵPLATFORM_SERVER_ID as PLATFORM_SERVER_ID} from '@angular/common';
1111
import {HttpClientModule} from '@angular/common/http';
1212
import {Injectable, InjectionToken, Injector, NgModule, NgZone, Optional, PLATFORM_ID, PLATFORM_INITIALIZER, PlatformRef, Provider, RendererFactory2, RootRenderer, StaticProvider, Testability, createPlatformFactory, platformCore, ɵALLOW_MULTIPLE_PLATFORMS as ALLOW_MULTIPLE_PLATFORMS} from '@angular/core';
13-
import {HttpModule} from '@angular/http';
1413
import {BrowserModule, EVENT_MANAGER_PLUGINS, ɵSharedStylesHost as SharedStylesHost, ɵgetDOM as getDOM} from '@angular/platform-browser';
1514
import {ɵplatformCoreDynamic as platformCoreDynamic} from '@angular/platform-browser-dynamic';
1615
import {NoopAnimationsModule, ɵAnimationRendererFactory} from '@angular/platform-browser/animations';
@@ -69,7 +68,7 @@ export const SERVER_RENDER_PROVIDERS: Provider[] = [
6968
*/
7069
@NgModule({
7170
exports: [BrowserModule],
72-
imports: [HttpModule, HttpClientModule, NoopAnimationsModule],
71+
imports: [HttpClientModule, NoopAnimationsModule],
7372
providers: [
7473
SERVER_RENDER_PROVIDERS,
7574
SERVER_HTTP_PROVIDERS,

‎packages/platform-server/test/BUILD.bazel

-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ ts_library(
1313
"//packages/compiler",
1414
"//packages/core",
1515
"//packages/core/testing",
16-
"//packages/http",
17-
"//packages/http/testing",
1816
"//packages/platform-browser",
1917
"//packages/platform-server",
2018
"//packages/private/testing",

‎packages/platform-server/test/integration_spec.ts

-132
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ import {HTTP_INTERCEPTORS, HttpClient, HttpClientModule, HttpEvent, HttpHandler,
1212
import {HttpClientTestingModule, HttpTestingController} from '@angular/common/http/testing';
1313
import {ApplicationRef, CompilerFactory, Component, HostListener, Inject, Injectable, Input, NgModule, NgZone, PLATFORM_ID, PlatformRef, ViewEncapsulation, destroyPlatform, getPlatform} from '@angular/core';
1414
import {async, inject} from '@angular/core/testing';
15-
import {Http, HttpModule, Response, ResponseOptions, XHRBackend} from '@angular/http';
16-
import {MockBackend, MockConnection} from '@angular/http/testing';
1715
import {BrowserModule, Title, TransferState, makeStateKey} from '@angular/platform-browser';
1816
import {getDOM} from '@angular/platform-browser/src/dom/dom_adapter';
1917
import {BEFORE_APP_SERIALIZED, INITIAL_CONFIG, PlatformState, ServerModule, ServerTransferStateModule, platformDynamicServer, renderModule, renderModuleFactory} from '@angular/platform-server';
@@ -29,10 +27,6 @@ class MyServerApp {
2927
bootstrap: [MyServerApp],
3028
declarations: [MyServerApp],
3129
imports: [ServerModule],
32-
providers: [
33-
MockBackend,
34-
{provide: XHRBackend, useExisting: MockBackend},
35-
]
3630
})
3731
class ExampleModule {
3832
}
@@ -232,30 +226,6 @@ class MyStylesApp {
232226
class ExampleStylesModule {
233227
}
234228

235-
@NgModule({
236-
bootstrap: [MyServerApp],
237-
declarations: [MyServerApp],
238-
imports: [HttpModule, ServerModule],
239-
providers: [
240-
MockBackend,
241-
{provide: XHRBackend, useExisting: MockBackend},
242-
]
243-
})
244-
export class HttpBeforeExampleModule {
245-
}
246-
247-
@NgModule({
248-
bootstrap: [MyServerApp],
249-
declarations: [MyServerApp],
250-
imports: [ServerModule, HttpModule],
251-
providers: [
252-
MockBackend,
253-
{provide: XHRBackend, useExisting: MockBackend},
254-
]
255-
})
256-
export class HttpAfterExampleModule {
257-
}
258-
259229
@NgModule({
260230
bootstrap: [MyServerApp],
261231
declarations: [MyServerApp],
@@ -774,108 +744,6 @@ class HiddenModule {
774744
}));
775745
});
776746

777-
describe('http', () => {
778-
it('can inject Http', async(() => {
779-
const platform = platformDynamicServer(
780-
[{provide: INITIAL_CONFIG, useValue: {document: '<app></app>'}}]);
781-
platform.bootstrapModule(ExampleModule).then(ref => {
782-
expect(ref.injector.get(Http) instanceof Http).toBeTruthy();
783-
});
784-
}));
785-
786-
it('can make Http requests', async(() => {
787-
const platform = platformDynamicServer(
788-
[{provide: INITIAL_CONFIG, useValue: {document: '<app></app>'}}]);
789-
platform.bootstrapModule(ExampleModule).then(ref => {
790-
const mock = ref.injector.get(MockBackend);
791-
const http = ref.injector.get(Http);
792-
ref.injector.get<NgZone>(NgZone).run(() => {
793-
NgZone.assertInAngularZone();
794-
mock.connections.subscribe((mc: MockConnection) => {
795-
NgZone.assertInAngularZone();
796-
expect(mc.request.url).toBe('http://localhost/testing');
797-
mc.mockRespond(new Response(new ResponseOptions({body: 'success!', status: 200})));
798-
});
799-
http.get('http://localhost/testing').subscribe(resp => {
800-
NgZone.assertInAngularZone();
801-
expect(resp.text()).toBe('success!');
802-
});
803-
});
804-
});
805-
}));
806-
807-
it('requests are macrotasks', async(() => {
808-
const platform = platformDynamicServer(
809-
[{provide: INITIAL_CONFIG, useValue: {document: '<app></app>'}}]);
810-
platform.bootstrapModule(ExampleModule).then(ref => {
811-
const mock = ref.injector.get(MockBackend);
812-
const http = ref.injector.get(Http);
813-
expect(ref.injector.get<NgZone>(NgZone).hasPendingMacrotasks).toBeFalsy();
814-
ref.injector.get<NgZone>(NgZone).run(() => {
815-
NgZone.assertInAngularZone();
816-
mock.connections.subscribe((mc: MockConnection) => {
817-
expect(ref.injector.get<NgZone>(NgZone).hasPendingMacrotasks).toBeTruthy();
818-
mc.mockRespond(new Response(new ResponseOptions({body: 'success!', status: 200})));
819-
});
820-
http.get('http://localhost/testing').subscribe(resp => {
821-
expect(resp.text()).toBe('success!');
822-
});
823-
});
824-
});
825-
}));
826-
827-
it('works when HttpModule is included before ServerModule', async(() => {
828-
const platform = platformDynamicServer(
829-
[{provide: INITIAL_CONFIG, useValue: {document: '<app></app>'}}]);
830-
platform.bootstrapModule(HttpBeforeExampleModule).then(ref => {
831-
const mock = ref.injector.get(MockBackend);
832-
const http = ref.injector.get(Http);
833-
expect(ref.injector.get<NgZone>(NgZone).hasPendingMacrotasks).toBeFalsy();
834-
ref.injector.get<NgZone>(NgZone).run(() => {
835-
NgZone.assertInAngularZone();
836-
mock.connections.subscribe((mc: MockConnection) => {
837-
expect(ref.injector.get<NgZone>(NgZone).hasPendingMacrotasks).toBeTruthy();
838-
mc.mockRespond(new Response(new ResponseOptions({body: 'success!', status: 200})));
839-
});
840-
http.get('http://localhost/testing').subscribe(resp => {
841-
expect(resp.text()).toBe('success!');
842-
});
843-
});
844-
});
845-
}));
846-
847-
it('works when HttpModule is included after ServerModule', async(() => {
848-
const platform = platformDynamicServer(
849-
[{provide: INITIAL_CONFIG, useValue: {document: '<app></app>'}}]);
850-
platform.bootstrapModule(HttpAfterExampleModule).then(ref => {
851-
const mock = ref.injector.get(MockBackend);
852-
const http = ref.injector.get(Http);
853-
expect(ref.injector.get<NgZone>(NgZone).hasPendingMacrotasks).toBeFalsy();
854-
ref.injector.get<NgZone>(NgZone).run(() => {
855-
NgZone.assertInAngularZone();
856-
mock.connections.subscribe((mc: MockConnection) => {
857-
expect(ref.injector.get<NgZone>(NgZone).hasPendingMacrotasks).toBeTruthy();
858-
mc.mockRespond(new Response(new ResponseOptions({body: 'success!', status: 200})));
859-
});
860-
http.get('http://localhost/testing').subscribe(resp => {
861-
expect(resp.text()).toBe('success!');
862-
});
863-
});
864-
});
865-
}));
866-
867-
it('throws when given a relative URL', async(() => {
868-
const platform = platformDynamicServer(
869-
[{provide: INITIAL_CONFIG, useValue: {document: '<app></app>'}}]);
870-
platform.bootstrapModule(ExampleModule).then(ref => {
871-
const http = ref.injector.get(Http);
872-
expect(() => http.get('/testing'))
873-
.toThrowError(
874-
'URLs requested via Http on the server must be absolute. URL: /testing');
875-
});
876-
}));
877-
});
878-
879747
describe('HttpClient', () => {
880748
it('can inject HttpClient', async(() => {
881749
const platform = platformDynamicServer(

0 commit comments

Comments
 (0)
Please sign in to comment.