Backport SSR fix (#8174)
This commit is contained in:
parent
4cbc33da74
commit
aef62d9ff8
|
|
@ -4,16 +4,9 @@
|
||||||
import { AbortError } from "./Errors";
|
import { AbortError } from "./Errors";
|
||||||
import { HttpClient, HttpRequest, HttpResponse } from "./HttpClient";
|
import { HttpClient, HttpRequest, HttpResponse } from "./HttpClient";
|
||||||
import { ILogger } from "./ILogger";
|
import { ILogger } from "./ILogger";
|
||||||
|
import { NodeHttpClient } from "./NodeHttpClient";
|
||||||
import { XhrHttpClient } from "./XhrHttpClient";
|
import { XhrHttpClient } from "./XhrHttpClient";
|
||||||
|
|
||||||
let nodeHttpClientModule: any;
|
|
||||||
if (typeof XMLHttpRequest === "undefined") {
|
|
||||||
// In order to ignore the dynamic require in webpack builds we need to do this magic
|
|
||||||
// @ts-ignore: TS doesn't know about these names
|
|
||||||
const requireFunc = typeof __webpack_require__ === "function" ? __non_webpack_require__ : require;
|
|
||||||
nodeHttpClientModule = requireFunc("./NodeHttpClient");
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Default implementation of {@link @aspnet/signalr.HttpClient}. */
|
/** Default implementation of {@link @aspnet/signalr.HttpClient}. */
|
||||||
export class DefaultHttpClient extends HttpClient {
|
export class DefaultHttpClient extends HttpClient {
|
||||||
private readonly httpClient: HttpClient;
|
private readonly httpClient: HttpClient;
|
||||||
|
|
@ -24,10 +17,8 @@ export class DefaultHttpClient extends HttpClient {
|
||||||
|
|
||||||
if (typeof XMLHttpRequest !== "undefined") {
|
if (typeof XMLHttpRequest !== "undefined") {
|
||||||
this.httpClient = new XhrHttpClient(logger);
|
this.httpClient = new XhrHttpClient(logger);
|
||||||
} else if (typeof nodeHttpClientModule !== "undefined") {
|
|
||||||
this.httpClient = new nodeHttpClientModule.NodeHttpClient(logger);
|
|
||||||
} else {
|
} else {
|
||||||
throw new Error("No HttpClient could be created.");
|
this.httpClient = new NodeHttpClient(logger);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,18 @@
|
||||||
|
// Copyright (c) .NET Foundation. All rights reserved.
|
||||||
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||||
|
|
||||||
|
// This is an empty implementation of the NodeHttpClient that will be included in browser builds so the output file will be smaller
|
||||||
|
|
||||||
|
import { HttpClient, HttpResponse } from "./HttpClient";
|
||||||
|
import { ILogger } from "./ILogger";
|
||||||
|
|
||||||
|
export class NodeHttpClient extends HttpClient {
|
||||||
|
// @ts-ignore: Need ILogger to compile, but unused variables generate errors
|
||||||
|
public constructor(logger: ILogger) {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
public send(): Promise<HttpResponse> {
|
||||||
|
return Promise.reject(new Error("If using Node either provide an XmlHttpRequest polyfill or consume the cjs or esm script instead of the browser/signalr.js one."));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,23 +1,36 @@
|
||||||
// Copyright (c) .NET Foundation. All rights reserved.
|
// Copyright (c) .NET Foundation. All rights reserved.
|
||||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||||
|
|
||||||
import * as Request from "request";
|
// @ts-ignore: This will be removed from built files and is here to make the types available during dev work
|
||||||
|
import * as Request from "@types/request";
|
||||||
|
|
||||||
import { AbortError, HttpError, TimeoutError } from "./Errors";
|
import { AbortError, HttpError, TimeoutError } from "./Errors";
|
||||||
import { HttpClient, HttpRequest, HttpResponse } from "./HttpClient";
|
import { HttpClient, HttpRequest, HttpResponse } from "./HttpClient";
|
||||||
import { ILogger, LogLevel } from "./ILogger";
|
import { ILogger, LogLevel } from "./ILogger";
|
||||||
import { isArrayBuffer } from "./Utils";
|
import { isArrayBuffer } from "./Utils";
|
||||||
|
|
||||||
|
let requestModule: Request.RequestAPI<Request.Request, Request.CoreOptions, Request.RequiredUriUrl>;
|
||||||
|
if (typeof XMLHttpRequest === "undefined") {
|
||||||
|
// In order to ignore the dynamic require in webpack builds we need to do this magic
|
||||||
|
// @ts-ignore: TS doesn't know about these names
|
||||||
|
const requireFunc = typeof __webpack_require__ === "function" ? __non_webpack_require__ : require;
|
||||||
|
requestModule = requireFunc("request");
|
||||||
|
}
|
||||||
|
|
||||||
export class NodeHttpClient extends HttpClient {
|
export class NodeHttpClient extends HttpClient {
|
||||||
private readonly logger: ILogger;
|
private readonly logger: ILogger;
|
||||||
private readonly request: Request.RequestAPI<Request.Request, Request.CoreOptions, Request.RequiredUriUrl>;
|
private readonly request: typeof requestModule;
|
||||||
private readonly cookieJar: Request.CookieJar;
|
private readonly cookieJar: Request.CookieJar;
|
||||||
|
|
||||||
public constructor(logger: ILogger) {
|
public constructor(logger: ILogger) {
|
||||||
super();
|
super();
|
||||||
|
if (typeof requestModule === "undefined") {
|
||||||
|
throw new Error("The 'request' module could not be loaded.");
|
||||||
|
}
|
||||||
|
|
||||||
this.logger = logger;
|
this.logger = logger;
|
||||||
this.cookieJar = Request.jar();
|
this.cookieJar = requestModule.jar();
|
||||||
this.request = Request.defaults({ jar: this.cookieJar });
|
this.request = requestModule.defaults({ jar: this.cookieJar });
|
||||||
}
|
}
|
||||||
|
|
||||||
public send(httpRequest: HttpRequest): Promise<HttpResponse> {
|
public send(httpRequest: HttpRequest): Promise<HttpResponse> {
|
||||||
|
|
|
||||||
|
|
@ -39,7 +39,10 @@ module.exports = function (modulePath, browserBaseName, options) {
|
||||||
},
|
},
|
||||||
resolve: {
|
resolve: {
|
||||||
extensions: [".ts", ".js"],
|
extensions: [".ts", ".js"],
|
||||||
alias: options.alias,
|
alias: {
|
||||||
|
"./NodeHttpClient": path.resolve(__dirname, "signalr/src/EmptyNodeHttpClient.ts"),
|
||||||
|
...options.alias,
|
||||||
|
}
|
||||||
},
|
},
|
||||||
output: {
|
output: {
|
||||||
filename: `${browserBaseName}.js`,
|
filename: `${browserBaseName}.js`,
|
||||||
|
|
@ -72,7 +75,6 @@ module.exports = function (modulePath, browserBaseName, options) {
|
||||||
}),
|
}),
|
||||||
// ES6 Promise uses this module in certain circumstances but we don't need it.
|
// ES6 Promise uses this module in certain circumstances but we don't need it.
|
||||||
new webpack.IgnorePlugin(/vertx/),
|
new webpack.IgnorePlugin(/vertx/),
|
||||||
new webpack.IgnorePlugin(/NodeHttpClient/),
|
|
||||||
new webpack.IgnorePlugin(/eventsource/),
|
new webpack.IgnorePlugin(/eventsource/),
|
||||||
],
|
],
|
||||||
externals: options.externals,
|
externals: options.externals,
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue