feat: remove @angular/http dependency from @angular/platform-server (#29408)
PR Close #29408
This commit is contained in:

committed by
Miško Hevery

parent
c9810064cb
commit
9745f55a65
@ -6,33 +6,20 @@
|
||||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
|
||||
const xhr2: any = require('xhr2');
|
||||
|
||||
import {Injectable, Injector, Optional, Provider, InjectFlags} from '@angular/core';
|
||||
import {BrowserXhr, Connection, ConnectionBackend, Http, ReadyState, Request, RequestOptions, Response, XHRBackend, XSRFStrategy} from '@angular/http';
|
||||
import {Injectable, Injector, Provider} from '@angular/core';
|
||||
|
||||
import {HttpEvent, HttpRequest, HttpHandler, HttpInterceptor, HTTP_INTERCEPTORS, HttpBackend, XhrFactory, ɵHttpInterceptingHandler as HttpInterceptingHandler} from '@angular/common/http';
|
||||
import {HttpEvent, HttpRequest, HttpHandler, HttpBackend, XhrFactory, ɵHttpInterceptingHandler as HttpInterceptingHandler} from '@angular/common/http';
|
||||
|
||||
import {Observable, Observer, Subscription} from 'rxjs';
|
||||
|
||||
const isAbsoluteUrl = /^[a-zA-Z\-\+.]+:\/\//;
|
||||
|
||||
function validateRequestUrl(url: string): void {
|
||||
if (!isAbsoluteUrl.test(url)) {
|
||||
throw new Error(`URLs requested via Http on the server must be absolute. URL: ${url}`);
|
||||
}
|
||||
}
|
||||
|
||||
@Injectable()
|
||||
export class ServerXhr implements BrowserXhr {
|
||||
export class ServerXhr implements XhrFactory {
|
||||
build(): XMLHttpRequest { return new xhr2.XMLHttpRequest(); }
|
||||
}
|
||||
|
||||
@Injectable()
|
||||
export class ServerXsrfStrategy implements XSRFStrategy {
|
||||
configureRequest(req: Request): void {}
|
||||
}
|
||||
|
||||
export abstract class ZoneMacroTaskWrapper<S, R> {
|
||||
wrap(request: S): Observable<R> {
|
||||
return new Observable((observer: Observer<R>) => {
|
||||
@ -111,36 +98,6 @@ export abstract class ZoneMacroTaskWrapper<S, R> {
|
||||
protected abstract delegate(request: S): Observable<R>;
|
||||
}
|
||||
|
||||
export class ZoneMacroTaskConnection extends ZoneMacroTaskWrapper<Request, Response> implements
|
||||
Connection {
|
||||
response: Observable<Response>;
|
||||
// TODO(issue/24571): remove '!'.
|
||||
lastConnection !: Connection;
|
||||
|
||||
constructor(public request: Request, private backend: XHRBackend) {
|
||||
super();
|
||||
validateRequestUrl(request.url);
|
||||
this.response = this.wrap(request);
|
||||
}
|
||||
|
||||
delegate(request: Request): Observable<Response> {
|
||||
this.lastConnection = this.backend.createConnection(request);
|
||||
return this.lastConnection.response as Observable<Response>;
|
||||
}
|
||||
|
||||
get readyState(): ReadyState {
|
||||
return !!this.lastConnection ? this.lastConnection.readyState : ReadyState.Unsent;
|
||||
}
|
||||
}
|
||||
|
||||
export class ZoneMacroTaskBackend implements ConnectionBackend {
|
||||
constructor(private backend: XHRBackend) {}
|
||||
|
||||
createConnection(request: any): ZoneMacroTaskConnection {
|
||||
return new ZoneMacroTaskConnection(request, this.backend);
|
||||
}
|
||||
}
|
||||
|
||||
export class ZoneClientBackend extends
|
||||
ZoneMacroTaskWrapper<HttpRequest<any>, HttpEvent<any>> implements HttpBackend {
|
||||
constructor(private backend: HttpBackend) { super(); }
|
||||
@ -152,19 +109,12 @@ export class ZoneClientBackend extends
|
||||
}
|
||||
}
|
||||
|
||||
export function httpFactory(xhrBackend: XHRBackend, options: RequestOptions) {
|
||||
const macroBackend = new ZoneMacroTaskBackend(xhrBackend);
|
||||
return new Http(macroBackend, options);
|
||||
}
|
||||
|
||||
export function zoneWrappedInterceptingHandler(backend: HttpBackend, injector: Injector) {
|
||||
const realBackend: HttpBackend = new HttpInterceptingHandler(backend, injector);
|
||||
return new ZoneClientBackend(realBackend);
|
||||
}
|
||||
|
||||
export const SERVER_HTTP_PROVIDERS: Provider[] = [
|
||||
{provide: Http, useFactory: httpFactory, deps: [XHRBackend, RequestOptions]},
|
||||
{provide: BrowserXhr, useClass: ServerXhr}, {provide: XSRFStrategy, useClass: ServerXsrfStrategy},
|
||||
{provide: XhrFactory, useClass: ServerXhr}, {
|
||||
provide: HttpHandler,
|
||||
useFactory: zoneWrappedInterceptingHandler,
|
||||
|
@ -10,7 +10,6 @@ import {ɵAnimationEngine} from '@angular/animations/browser';
|
||||
import {DOCUMENT, PlatformLocation, ViewportScroller, ɵNullViewportScroller as NullViewportScroller, ɵPLATFORM_SERVER_ID as PLATFORM_SERVER_ID} from '@angular/common';
|
||||
import {HttpClientModule} from '@angular/common/http';
|
||||
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';
|
||||
import {HttpModule} from '@angular/http';
|
||||
import {BrowserModule, EVENT_MANAGER_PLUGINS, ɵSharedStylesHost as SharedStylesHost, ɵgetDOM as getDOM} from '@angular/platform-browser';
|
||||
import {ɵplatformCoreDynamic as platformCoreDynamic} from '@angular/platform-browser-dynamic';
|
||||
import {NoopAnimationsModule, ɵAnimationRendererFactory} from '@angular/platform-browser/animations';
|
||||
@ -69,7 +68,7 @@ export const SERVER_RENDER_PROVIDERS: Provider[] = [
|
||||
*/
|
||||
@NgModule({
|
||||
exports: [BrowserModule],
|
||||
imports: [HttpModule, HttpClientModule, NoopAnimationsModule],
|
||||
imports: [HttpClientModule, NoopAnimationsModule],
|
||||
providers: [
|
||||
SERVER_RENDER_PROVIDERS,
|
||||
SERVER_HTTP_PROVIDERS,
|
||||
|
Reference in New Issue
Block a user