Initial typescript tidy up (#1239)
This commit is contained in:
parent
ac236efdae
commit
1bd99f6b67
|
|
@ -31,7 +31,7 @@ describe("Connection", () => {
|
|||
return Promise.resolve("");
|
||||
}
|
||||
},
|
||||
logging: null
|
||||
logger: null
|
||||
} as IHttpConnectionOptions;
|
||||
|
||||
let connection = new HttpConnection("http://tempuri.org", options);
|
||||
|
|
@ -67,7 +67,7 @@ describe("Connection", () => {
|
|||
return Promise.resolve("");
|
||||
}
|
||||
},
|
||||
logging: null
|
||||
logger: null
|
||||
} as IHttpConnectionOptions;
|
||||
|
||||
let connection = new HttpConnection("http://tempuri.org", options);
|
||||
|
|
@ -93,7 +93,7 @@ describe("Connection", () => {
|
|||
return Promise.resolve("");
|
||||
}
|
||||
},
|
||||
logging: null
|
||||
logger: null
|
||||
} as IHttpConnectionOptions;
|
||||
|
||||
let connection = new HttpConnection("http://tempuri.org", options);
|
||||
|
|
@ -125,7 +125,7 @@ describe("Connection", () => {
|
|||
return Promise.resolve("");
|
||||
}
|
||||
},
|
||||
logging: null
|
||||
logger: null
|
||||
} as IHttpConnectionOptions;
|
||||
|
||||
let connection = new HttpConnection("http://tempuri.org", options);
|
||||
|
|
@ -156,7 +156,9 @@ describe("Connection", () => {
|
|||
send(data: any): Promise<void> {
|
||||
return Promise.reject("");
|
||||
},
|
||||
stop(): void { },
|
||||
stop(): Promise<void> {
|
||||
return Promise.resolve();
|
||||
},
|
||||
onreceive: undefined,
|
||||
onclose: undefined,
|
||||
}
|
||||
|
|
@ -171,7 +173,7 @@ describe("Connection", () => {
|
|||
}
|
||||
},
|
||||
transport: fakeTransport,
|
||||
logging: null
|
||||
logger: null
|
||||
} as IHttpConnectionOptions;
|
||||
|
||||
|
||||
|
|
@ -205,7 +207,7 @@ describe("Connection", () => {
|
|||
return Promise.resolve("");
|
||||
}
|
||||
},
|
||||
logging: null
|
||||
logger: null
|
||||
} as IHttpConnectionOptions;
|
||||
|
||||
connection = new HttpConnection(givenUrl, options);
|
||||
|
|
@ -238,7 +240,7 @@ describe("Connection", () => {
|
|||
}
|
||||
},
|
||||
transport: requestedTransport,
|
||||
logging: null
|
||||
logger: null
|
||||
} as IHttpConnectionOptions;
|
||||
|
||||
let connection = new HttpConnection("http://tempuri.org", options);
|
||||
|
|
@ -264,7 +266,7 @@ describe("Connection", () => {
|
|||
return Promise.resolve("");
|
||||
}
|
||||
},
|
||||
logging: null
|
||||
logger: null
|
||||
} as IHttpConnectionOptions;
|
||||
|
||||
let connection = new HttpConnection("http://tempuri.org", options);
|
||||
|
|
@ -290,7 +292,7 @@ describe("Connection", () => {
|
|||
}
|
||||
},
|
||||
transport: TransportType.WebSockets,
|
||||
logging: null
|
||||
logger: null
|
||||
} as IHttpConnectionOptions;
|
||||
|
||||
let connection = new HttpConnection("http://tempuri.org", options);
|
||||
|
|
@ -318,7 +320,7 @@ describe("Connection", () => {
|
|||
// mode: TransferMode : TransferMode.Text
|
||||
connect(url: string, requestedTransferMode: TransferMode): Promise<TransferMode> { return Promise.resolve(transportTransferMode); },
|
||||
send(data: any): Promise<void> { return Promise.resolve(); },
|
||||
stop(): void { },
|
||||
stop(): Promise<void> { return Promise.resolve(); },
|
||||
onreceive: null,
|
||||
onclose: null,
|
||||
mode: transportTransferMode
|
||||
|
|
@ -334,7 +336,7 @@ describe("Connection", () => {
|
|||
}
|
||||
},
|
||||
transport: fakeTransport,
|
||||
logging: null
|
||||
logger: null
|
||||
} as IHttpConnectionOptions;
|
||||
|
||||
let connection = new HttpConnection("https://tempuri.org", options);
|
||||
|
|
@ -18,7 +18,7 @@ describe("HubConnection", () => {
|
|||
describe("start", () => {
|
||||
it("sends negotiation message", async () => {
|
||||
let connection = new TestConnection();
|
||||
let hubConnection = new HubConnection(connection, { logging: null });
|
||||
let hubConnection = new HubConnection(connection, { logger: null });
|
||||
await hubConnection.start();
|
||||
expect(connection.sentData.length).toBe(1)
|
||||
expect(JSON.parse(connection.sentData[0])).toEqual({
|
||||
|
|
@ -32,7 +32,7 @@ describe("HubConnection", () => {
|
|||
it("sends a non blocking invocation", async () => {
|
||||
let connection = new TestConnection();
|
||||
|
||||
let hubConnection = new HubConnection(connection);
|
||||
let hubConnection = new HubConnection(connection, { logger: null });
|
||||
let invokePromise = hubConnection.send("testMethod", "arg", 42)
|
||||
.catch((_) => { }); // Suppress exception and unhandled promise rejection warning.
|
||||
|
||||
|
|
@ -56,7 +56,7 @@ describe("HubConnection", () => {
|
|||
it("sends an invocation", async () => {
|
||||
let connection = new TestConnection();
|
||||
|
||||
let hubConnection = new HubConnection(connection);
|
||||
let hubConnection = new HubConnection(connection, { logger: null });
|
||||
let invokePromise = hubConnection.invoke("testMethod", "arg", 42)
|
||||
.catch((_) => { }); // Suppress exception and unhandled promise rejection warning.
|
||||
|
||||
|
|
@ -79,7 +79,7 @@ describe("HubConnection", () => {
|
|||
it("rejects the promise when an error is received", async () => {
|
||||
let connection = new TestConnection();
|
||||
|
||||
let hubConnection = new HubConnection(connection);
|
||||
let hubConnection = new HubConnection(connection, { logger: null });
|
||||
let invokePromise = hubConnection.invoke("testMethod", "arg", 42);
|
||||
|
||||
connection.receive({ type: MessageType.Completion, invocationId: connection.lastInvocationId, error: "foo" });
|
||||
|
|
@ -91,7 +91,7 @@ describe("HubConnection", () => {
|
|||
it("resolves the promise when a result is received", async () => {
|
||||
let connection = new TestConnection();
|
||||
|
||||
let hubConnection = new HubConnection(connection);
|
||||
let hubConnection = new HubConnection(connection, { logger: null });
|
||||
let invokePromise = hubConnection.invoke("testMethod", "arg", 42);
|
||||
|
||||
connection.receive({ type: MessageType.Completion, invocationId: connection.lastInvocationId, result: "foo" });
|
||||
|
|
@ -102,7 +102,7 @@ describe("HubConnection", () => {
|
|||
it("completes pending invocations when stopped", async () => {
|
||||
let connection = new TestConnection();
|
||||
|
||||
let hubConnection = new HubConnection(connection);
|
||||
let hubConnection = new HubConnection(connection, { logger: null });
|
||||
let invokePromise = hubConnection.invoke("testMethod");
|
||||
hubConnection.stop();
|
||||
|
||||
|
|
@ -113,7 +113,7 @@ describe("HubConnection", () => {
|
|||
it("completes pending invocations when connection is lost", async () => {
|
||||
let connection = new TestConnection();
|
||||
|
||||
let hubConnection = new HubConnection(connection);
|
||||
let hubConnection = new HubConnection(connection, { logger: null });
|
||||
let invokePromise = hubConnection.invoke("testMethod");
|
||||
// Typically this would be called by the transport
|
||||
connection.onclose(new Error("Connection lost"));
|
||||
|
|
@ -134,7 +134,7 @@ describe("HubConnection", () => {
|
|||
}
|
||||
};
|
||||
let connection = new TestConnection();
|
||||
let hubConnection = new HubConnection(connection, { logging: logger });
|
||||
let hubConnection = new HubConnection(connection, { logger: logger });
|
||||
|
||||
connection.receive({
|
||||
type: MessageType.Invocation,
|
||||
|
|
@ -149,7 +149,7 @@ describe("HubConnection", () => {
|
|||
|
||||
it("callback invoked when servers invokes a method on the client", async () => {
|
||||
let connection = new TestConnection();
|
||||
let hubConnection = new HubConnection(connection);
|
||||
let hubConnection = new HubConnection(connection, { logger: null });
|
||||
let value = 0;
|
||||
hubConnection.on("message", v => value = v);
|
||||
|
||||
|
|
@ -166,7 +166,7 @@ describe("HubConnection", () => {
|
|||
|
||||
it("can have multiple callbacks", async () => {
|
||||
let connection = new TestConnection();
|
||||
let hubConnection = new HubConnection(connection);
|
||||
let hubConnection = new HubConnection(connection, { logger: null });
|
||||
let numInvocations1 = 0;
|
||||
let numInvocations2 = 0;
|
||||
hubConnection.on("message", () => numInvocations1++);
|
||||
|
|
@ -186,7 +186,7 @@ describe("HubConnection", () => {
|
|||
|
||||
it("can unsubscribe from on", async () => {
|
||||
let connection = new TestConnection();
|
||||
let hubConnection = new HubConnection(connection);
|
||||
let hubConnection = new HubConnection(connection, { logger: null });
|
||||
|
||||
var numInvocations = 0;
|
||||
var callback = () => numInvocations++;
|
||||
|
|
@ -215,7 +215,7 @@ describe("HubConnection", () => {
|
|||
|
||||
it("unsubscribing from non-existing callbacks no-ops", async () => {
|
||||
let connection = new TestConnection();
|
||||
let hubConnection = new HubConnection(connection);
|
||||
let hubConnection = new HubConnection(connection, { logger: null });
|
||||
|
||||
hubConnection.off("_", () => { });
|
||||
hubConnection.on("message", t => { });
|
||||
|
|
@ -234,7 +234,7 @@ describe("HubConnection", () => {
|
|||
};
|
||||
|
||||
let connection = new TestConnection();
|
||||
let hubConnection = new HubConnection(connection, { logging: logger });
|
||||
let hubConnection = new HubConnection(connection, { logger: logger });
|
||||
|
||||
hubConnection.on(null, undefined);
|
||||
hubConnection.on(undefined, null);
|
||||
|
|
@ -267,7 +267,7 @@ describe("HubConnection", () => {
|
|||
it("sends an invocation", async () => {
|
||||
let connection = new TestConnection();
|
||||
|
||||
let hubConnection = new HubConnection(connection);
|
||||
let hubConnection = new HubConnection(connection, { logger: null });
|
||||
let invokePromise = hubConnection.stream("testStream", "arg", 42);
|
||||
|
||||
// Verify the message is sent
|
||||
|
|
@ -289,7 +289,7 @@ describe("HubConnection", () => {
|
|||
it("completes with an error when an error is yielded", async () => {
|
||||
let connection = new TestConnection();
|
||||
|
||||
let hubConnection = new HubConnection(connection);
|
||||
let hubConnection = new HubConnection(connection, { logger: null });
|
||||
let observer = new TestObserver();
|
||||
hubConnection.stream<any>("testMethod", "arg", 42)
|
||||
.subscribe(observer);
|
||||
|
|
@ -303,7 +303,7 @@ describe("HubConnection", () => {
|
|||
it("completes the observer when a completion is received", async () => {
|
||||
let connection = new TestConnection();
|
||||
|
||||
let hubConnection = new HubConnection(connection);
|
||||
let hubConnection = new HubConnection(connection, { logger: null });
|
||||
let observer = new TestObserver();
|
||||
hubConnection.stream<any>("testMethod", "arg", 42)
|
||||
.subscribe(observer);
|
||||
|
|
@ -316,7 +316,7 @@ describe("HubConnection", () => {
|
|||
it("completes pending streams when stopped", async () => {
|
||||
let connection = new TestConnection();
|
||||
|
||||
let hubConnection = new HubConnection(connection);
|
||||
let hubConnection = new HubConnection(connection, { logger: null });
|
||||
let observer = new TestObserver();
|
||||
hubConnection.stream<any>("testMethod")
|
||||
.subscribe(observer);
|
||||
|
|
@ -329,7 +329,7 @@ describe("HubConnection", () => {
|
|||
it("completes pending streams when connection is lost", async () => {
|
||||
let connection = new TestConnection();
|
||||
|
||||
let hubConnection = new HubConnection(connection);
|
||||
let hubConnection = new HubConnection(connection, { logger: null });
|
||||
let observer = new TestObserver();
|
||||
hubConnection.stream<any>("testMethod")
|
||||
.subscribe(observer);
|
||||
|
|
@ -344,7 +344,7 @@ describe("HubConnection", () => {
|
|||
it("yields items as they arrive", async () => {
|
||||
let connection = new TestConnection();
|
||||
|
||||
let hubConnection = new HubConnection(connection);
|
||||
let hubConnection = new HubConnection(connection, { logger: null });
|
||||
let observer = new TestObserver();
|
||||
hubConnection.stream<any>("testMethod")
|
||||
.subscribe(observer);
|
||||
|
|
@ -365,7 +365,7 @@ describe("HubConnection", () => {
|
|||
it("does not require error function registered", async () => {
|
||||
let connection = new TestConnection();
|
||||
|
||||
let hubConnection = new HubConnection(connection);
|
||||
let hubConnection = new HubConnection(connection, { logger: null });
|
||||
let observer = hubConnection.stream("testMethod").subscribe({
|
||||
next: val => { }
|
||||
});
|
||||
|
|
@ -378,7 +378,7 @@ describe("HubConnection", () => {
|
|||
it("does not require complete function registered", async () => {
|
||||
let connection = new TestConnection();
|
||||
|
||||
let hubConnection = new HubConnection(connection);
|
||||
let hubConnection = new HubConnection(connection, { logger: null });
|
||||
let observer = hubConnection.stream("testMethod").subscribe({
|
||||
next: val => { }
|
||||
});
|
||||
|
|
@ -392,7 +392,7 @@ describe("HubConnection", () => {
|
|||
describe("onClose", () => {
|
||||
it("can have multiple callbacks", async () => {
|
||||
let connection = new TestConnection();
|
||||
let hubConnection = new HubConnection(connection);
|
||||
let hubConnection = new HubConnection(connection, { logger: null });
|
||||
let invocations = 0;
|
||||
hubConnection.onclose(e => invocations++);
|
||||
hubConnection.onclose(e => invocations++);
|
||||
|
|
@ -403,7 +403,7 @@ describe("HubConnection", () => {
|
|||
|
||||
it("callbacks receive error", async () => {
|
||||
let connection = new TestConnection();
|
||||
let hubConnection = new HubConnection(connection);
|
||||
let hubConnection = new HubConnection(connection, { logger: null });
|
||||
let error: Error;
|
||||
hubConnection.onclose(e => error = e);
|
||||
|
||||
|
|
@ -414,7 +414,7 @@ describe("HubConnection", () => {
|
|||
|
||||
it("ignores null callbacks", async () => {
|
||||
let connection = new TestConnection();
|
||||
let hubConnection = new HubConnection(connection);
|
||||
let hubConnection = new HubConnection(connection, { logger: null });
|
||||
hubConnection.onclose(null);
|
||||
hubConnection.onclose(undefined);
|
||||
// Typically this would be called by the transport
|
||||
|
|
@ -428,7 +428,7 @@ describe("HubConnection", () => {
|
|||
// Receive the ping mid-invocation so we can see that the rest of the flow works fine
|
||||
|
||||
let connection = new TestConnection();
|
||||
let hubConnection = new HubConnection(connection);
|
||||
let hubConnection = new HubConnection(connection, { logger: null });
|
||||
let invokePromise = hubConnection.invoke("testMethod", "arg", 42);
|
||||
|
||||
connection.receive({ type: MessageType.Ping });
|
||||
|
|
@ -439,7 +439,7 @@ describe("HubConnection", () => {
|
|||
|
||||
it("does not terminate if messages are received", async () => {
|
||||
let connection = new TestConnection();
|
||||
let hubConnection = new HubConnection(connection, { serverTimeoutInMilliseconds: 100 });
|
||||
let hubConnection = new HubConnection(connection, { timeoutInMilliseconds: 100, logger: null });
|
||||
|
||||
let p = new PromiseSource<Error>();
|
||||
hubConnection.onclose(error => p.resolve(error));
|
||||
|
|
@ -464,7 +464,7 @@ describe("HubConnection", () => {
|
|||
|
||||
it("terminates if no messages received within timeout interval", async () => {
|
||||
let connection = new TestConnection();
|
||||
let hubConnection = new HubConnection(connection, { serverTimeoutInMilliseconds: 100 });
|
||||
let hubConnection = new HubConnection(connection, { timeoutInMilliseconds: 100, logger: null });
|
||||
|
||||
let p = new PromiseSource<Error>();
|
||||
hubConnection.onclose(error => p.resolve(error));
|
||||
|
|
@ -500,10 +500,11 @@ class TestConnection implements IConnection {
|
|||
return Promise.resolve();
|
||||
};
|
||||
|
||||
stop(error?: Error): void {
|
||||
stop(error?: Error): Promise<void> {
|
||||
if (this.onclose) {
|
||||
this.onclose(error);
|
||||
}
|
||||
return Promise.resolve();
|
||||
};
|
||||
|
||||
receive(data: any): void {
|
||||
|
|
|
|||
|
|
@ -42,4 +42,4 @@ export class HttpClient implements IHttpClient {
|
|||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -34,7 +34,7 @@ export class HttpConnection implements IConnection {
|
|||
readonly features: any = {};
|
||||
|
||||
constructor(url: string, options: IHttpConnectionOptions = {}) {
|
||||
this.logger = LoggerFactory.createLogger(options.logging);
|
||||
this.logger = LoggerFactory.createLogger(options.logger);
|
||||
this.baseUrl = this.resolveUrl(url);
|
||||
options = options || {};
|
||||
this.httpClient = options.httpClient || new HttpClient();
|
||||
|
|
@ -62,9 +62,9 @@ export class HttpConnection implements IConnection {
|
|||
}
|
||||
else {
|
||||
let headers;
|
||||
if (this.options.jwtBearer) {
|
||||
if (this.options.accessToken) {
|
||||
headers = new Map<string, string>();
|
||||
headers.set("Authorization", `Bearer ${this.options.jwtBearer()}`);
|
||||
headers.set("Authorization", `Bearer ${this.options.accessToken()}`);
|
||||
}
|
||||
|
||||
let negotiatePayload = await this.httpClient.post(this.resolveNegotiateUrl(this.baseUrl), "", headers);
|
||||
|
|
@ -110,13 +110,13 @@ export class HttpConnection implements IConnection {
|
|||
transport = TransportType[availableTransports[0]];
|
||||
}
|
||||
if (transport === TransportType.WebSockets && availableTransports.indexOf(TransportType[transport]) >= 0) {
|
||||
return new WebSocketTransport(this.options.jwtBearer, this.logger);
|
||||
return new WebSocketTransport(this.options.accessToken, this.logger);
|
||||
}
|
||||
if (transport === TransportType.ServerSentEvents && availableTransports.indexOf(TransportType[transport]) >= 0) {
|
||||
return new ServerSentEventsTransport(this.httpClient, this.options.jwtBearer, this.logger);
|
||||
return new ServerSentEventsTransport(this.httpClient, this.options.accessToken, this.logger);
|
||||
}
|
||||
if (transport === TransportType.LongPolling && availableTransports.indexOf(TransportType[transport]) >= 0) {
|
||||
return new LongPollingTransport(this.httpClient, this.options.jwtBearer, this.logger);
|
||||
return new LongPollingTransport(this.httpClient, this.options.accessToken, this.logger);
|
||||
}
|
||||
|
||||
if (this.isITransport(transport)) {
|
||||
|
|
|
|||
|
|
@ -13,7 +13,6 @@ import { Base64EncodedHubProtocol } from "./Base64EncodedHubProtocol"
|
|||
import { ILogger, LogLevel } from "./ILogger"
|
||||
import { ConsoleLogger, NullLogger, LoggerFactory } from "./Loggers"
|
||||
import { IHubConnectionOptions } from "./IHubConnectionOptions"
|
||||
import { setTimeout, clearTimeout } from "timers";
|
||||
|
||||
export { TransportType } from "./Transports"
|
||||
export { HttpConnection } from "./HttpConnection"
|
||||
|
|
@ -21,7 +20,7 @@ export { JsonHubProtocol } from "./JsonHubProtocol"
|
|||
export { LogLevel, ILogger } from "./ILogger"
|
||||
export { ConsoleLogger, NullLogger } from "./Loggers"
|
||||
|
||||
const DEFAULT_SERVER_TIMEOUT_IN_MS: number = 30 * 1000;
|
||||
const DEFAULT_TIMEOUT_IN_MS: number = 30 * 1000;
|
||||
|
||||
export class HubConnection {
|
||||
private readonly connection: IConnection;
|
||||
|
|
@ -32,12 +31,14 @@ export class HubConnection {
|
|||
private id: number;
|
||||
private closedCallbacks: ConnectionClosed[];
|
||||
private timeoutHandle: NodeJS.Timer;
|
||||
private serverTimeoutInMilliseconds: number;
|
||||
private timeoutInMilliseconds: number;
|
||||
|
||||
constructor(url: string, options?: IHubConnectionOptions);
|
||||
constructor(connection: IConnection, options?: IHubConnectionOptions);
|
||||
constructor(urlOrConnection: string | IConnection, options: IHubConnectionOptions = {}) {
|
||||
options = options || {};
|
||||
|
||||
this.serverTimeoutInMilliseconds = options.serverTimeoutInMilliseconds || DEFAULT_SERVER_TIMEOUT_IN_MS;
|
||||
this.timeoutInMilliseconds = options.timeoutInMilliseconds || DEFAULT_TIMEOUT_IN_MS;
|
||||
|
||||
if (typeof urlOrConnection === "string") {
|
||||
this.connection = new HttpConnection(urlOrConnection, options);
|
||||
|
|
@ -46,7 +47,7 @@ export class HubConnection {
|
|||
this.connection = urlOrConnection;
|
||||
}
|
||||
|
||||
this.logger = LoggerFactory.createLogger(options.logging);
|
||||
this.logger = LoggerFactory.createLogger(options.logger);
|
||||
|
||||
this.protocol = options.protocol || new JsonHubProtocol();
|
||||
this.connection.onreceive = (data: any) => this.processIncomingData(data);
|
||||
|
|
@ -98,7 +99,7 @@ export class HubConnection {
|
|||
private configureTimeout() {
|
||||
if (!this.connection.features || !this.connection.features.inherentKeepAlive) {
|
||||
// Set the timeout timer
|
||||
this.timeoutHandle = setTimeout(() => this.serverTimeout(), this.serverTimeoutInMilliseconds);
|
||||
this.timeoutHandle = setTimeout(() => this.serverTimeout(), this.timeoutInMilliseconds);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -156,7 +157,7 @@ export class HubConnection {
|
|||
this.configureTimeout();
|
||||
}
|
||||
|
||||
stop(): void {
|
||||
stop(): Promise<void> {
|
||||
if (this.timeoutHandle) {
|
||||
clearTimeout(this.timeoutHandle);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,14 +2,14 @@
|
|||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||
|
||||
import { DataReceived, ConnectionClosed } from "./Common"
|
||||
import { TransportType, TransferMode, ITransport } from "./Transports"
|
||||
import { TransportType, TransferMode, ITransport } from "./Transports"
|
||||
|
||||
export interface IConnection {
|
||||
readonly features: any;
|
||||
|
||||
start(): Promise<void>;
|
||||
send(data: any): Promise<void>;
|
||||
stop(error?: Error): void;
|
||||
stop(error?: Error): Promise<void>;
|
||||
|
||||
onreceive: DataReceived;
|
||||
onclose: ConnectionClosed;
|
||||
|
|
|
|||
|
|
@ -8,6 +8,6 @@ import { ILogger, LogLevel } from "./ILogger";
|
|||
export interface IHttpConnectionOptions {
|
||||
httpClient?: IHttpClient;
|
||||
transport?: TransportType | ITransport;
|
||||
logging?: ILogger | LogLevel;
|
||||
jwtBearer?: () => string;
|
||||
logger?: ILogger | LogLevel;
|
||||
accessToken?: () => string;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,5 +7,5 @@ import { ILogger, LogLevel } from "./ILogger"
|
|||
|
||||
export interface IHubConnectionOptions extends IHttpConnectionOptions {
|
||||
protocol?: IHubProtocol;
|
||||
serverTimeoutInMilliseconds?: number;
|
||||
timeoutInMilliseconds?: number;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -21,27 +21,27 @@ export const enum TransferMode {
|
|||
export interface ITransport {
|
||||
connect(url: string, requestedTransferMode: TransferMode, connection: IConnection): Promise<TransferMode>;
|
||||
send(data: any): Promise<void>;
|
||||
stop(): void;
|
||||
stop(): Promise<void>;
|
||||
onreceive: DataReceived;
|
||||
onclose: TransportClosed;
|
||||
}
|
||||
|
||||
export class WebSocketTransport implements ITransport {
|
||||
private readonly logger: ILogger;
|
||||
private readonly jwtBearer: () => string;
|
||||
private readonly accessToken: () => string;
|
||||
private webSocket: WebSocket;
|
||||
|
||||
constructor(jwtBearer: () => string, logger: ILogger) {
|
||||
constructor(accessToken: () => string, logger: ILogger) {
|
||||
this.logger = logger;
|
||||
this.jwtBearer = jwtBearer;
|
||||
this.accessToken = accessToken;
|
||||
}
|
||||
|
||||
connect(url: string, requestedTransferMode: TransferMode, connection: IConnection): Promise<TransferMode> {
|
||||
|
||||
return new Promise<TransferMode>((resolve, reject) => {
|
||||
url = url.replace(/^http/, "ws");
|
||||
if (this.jwtBearer) {
|
||||
let token = this.jwtBearer();
|
||||
if (this.accessToken) {
|
||||
let token = this.accessToken();
|
||||
url += (url.indexOf("?") < 0 ? "?" : "&") + `signalRTokenHeader=${token}`;
|
||||
}
|
||||
|
||||
|
|
@ -90,11 +90,12 @@ export class WebSocketTransport implements ITransport {
|
|||
return Promise.reject("WebSocket is not in the OPEN state");
|
||||
}
|
||||
|
||||
stop(): void {
|
||||
stop(): Promise<void> {
|
||||
if (this.webSocket) {
|
||||
this.webSocket.close();
|
||||
this.webSocket = null;
|
||||
}
|
||||
return Promise.resolve();
|
||||
}
|
||||
|
||||
onreceive: DataReceived;
|
||||
|
|
@ -103,14 +104,14 @@ export class WebSocketTransport implements ITransport {
|
|||
|
||||
export class ServerSentEventsTransport implements ITransport {
|
||||
private readonly httpClient: IHttpClient;
|
||||
private readonly jwtBearer: () => string;
|
||||
private readonly accessToken: () => string;
|
||||
private readonly logger: ILogger;
|
||||
private eventSource: EventSource;
|
||||
private url: string;
|
||||
|
||||
constructor(httpClient: IHttpClient, jwtBearer: () => string, logger: ILogger) {
|
||||
constructor(httpClient: IHttpClient, accessToken: () => string, logger: ILogger) {
|
||||
this.httpClient = httpClient;
|
||||
this.jwtBearer = jwtBearer;
|
||||
this.accessToken = accessToken;
|
||||
this.logger = logger;
|
||||
}
|
||||
|
||||
|
|
@ -121,8 +122,8 @@ export class ServerSentEventsTransport implements ITransport {
|
|||
|
||||
this.url = url;
|
||||
return new Promise<TransferMode>((resolve, reject) => {
|
||||
if (this.jwtBearer) {
|
||||
let token = this.jwtBearer();
|
||||
if (this.accessToken) {
|
||||
let token = this.accessToken();
|
||||
url += (url.indexOf("?") < 0 ? "?" : "&") + `signalRTokenHeader=${token}`;
|
||||
}
|
||||
|
||||
|
|
@ -166,14 +167,15 @@ export class ServerSentEventsTransport implements ITransport {
|
|||
}
|
||||
|
||||
async send(data: any): Promise<void> {
|
||||
return send(this.httpClient, this.url, this.jwtBearer, data);
|
||||
return send(this.httpClient, this.url, this.accessToken, data);
|
||||
}
|
||||
|
||||
stop(): void {
|
||||
stop(): Promise<void> {
|
||||
if (this.eventSource) {
|
||||
this.eventSource.close();
|
||||
this.eventSource = null;
|
||||
}
|
||||
return Promise.resolve();
|
||||
}
|
||||
|
||||
onreceive: DataReceived;
|
||||
|
|
@ -182,16 +184,16 @@ export class ServerSentEventsTransport implements ITransport {
|
|||
|
||||
export class LongPollingTransport implements ITransport {
|
||||
private readonly httpClient: IHttpClient;
|
||||
private readonly jwtBearer: () => string;
|
||||
private readonly accessToken: () => string;
|
||||
private readonly logger: ILogger;
|
||||
|
||||
private url: string;
|
||||
private pollXhr: XMLHttpRequest;
|
||||
private shouldPoll: boolean;
|
||||
|
||||
constructor(httpClient: IHttpClient, jwtBearer: () => string, logger: ILogger) {
|
||||
constructor(httpClient: IHttpClient, accessToken: () => string, logger: ILogger) {
|
||||
this.httpClient = httpClient;
|
||||
this.jwtBearer = jwtBearer;
|
||||
this.accessToken = accessToken;
|
||||
this.logger = logger;
|
||||
}
|
||||
|
||||
|
|
@ -268,8 +270,8 @@ export class LongPollingTransport implements ITransport {
|
|||
this.pollXhr = pollXhr;
|
||||
|
||||
this.pollXhr.open("GET", `${url}&_=${Date.now()}`, true);
|
||||
if (this.jwtBearer) {
|
||||
this.pollXhr.setRequestHeader("Authorization", `Bearer ${this.jwtBearer()}`);
|
||||
if (this.accessToken) {
|
||||
this.pollXhr.setRequestHeader("Authorization", `Bearer ${this.accessToken()}`);
|
||||
}
|
||||
if (transferMode === TransferMode.Binary) {
|
||||
this.pollXhr.responseType = "arraybuffer";
|
||||
|
|
@ -281,26 +283,27 @@ export class LongPollingTransport implements ITransport {
|
|||
}
|
||||
|
||||
async send(data: any): Promise<void> {
|
||||
return send(this.httpClient, this.url, this.jwtBearer, data);
|
||||
return send(this.httpClient, this.url, this.accessToken, data);
|
||||
}
|
||||
|
||||
stop(): void {
|
||||
stop(): Promise<void> {
|
||||
this.shouldPoll = false;
|
||||
if (this.pollXhr) {
|
||||
this.pollXhr.abort();
|
||||
this.pollXhr = null;
|
||||
}
|
||||
return Promise.resolve();
|
||||
}
|
||||
|
||||
onreceive: DataReceived;
|
||||
onclose: TransportClosed;
|
||||
}
|
||||
|
||||
async function send(httpClient: IHttpClient, url: string, jwtBearer: () => string, data: any): Promise<void> {
|
||||
async function send(httpClient: IHttpClient, url: string, accessToken: () => string, data: any): Promise<void> {
|
||||
let headers;
|
||||
if (jwtBearer) {
|
||||
if (accessToken) {
|
||||
headers = new Map<string, string>();
|
||||
headers.set("Authorization", `Bearer ${jwtBearer()}`)
|
||||
headers.set("Authorization", `Bearer ${accessToken()}`)
|
||||
}
|
||||
|
||||
await httpClient.post(url, data, headers);
|
||||
|
|
|
|||
|
|
@ -489,7 +489,7 @@ describe('hubConnection', function () {
|
|||
var options = {
|
||||
transport: transportType,
|
||||
logging: signalR.LogLevel.Trace,
|
||||
jwtBearer: function () {
|
||||
accessToken: function () {
|
||||
return jwtToken;
|
||||
}
|
||||
};
|
||||
|
|
@ -518,7 +518,7 @@ describe('hubConnection', function () {
|
|||
var options = {
|
||||
transport: transportType,
|
||||
logging: signalR.LogLevel.Trace,
|
||||
serverTimeoutInMilliseconds: 100
|
||||
timeoutInMilliseconds: 100
|
||||
};
|
||||
|
||||
var hubConnection = new signalR.HubConnection(TESTHUBENDPOINT_URL, options);
|
||||
|
|
|
|||
|
|
@ -28,6 +28,16 @@
|
|||
"integrity": "sha512-zT+t9841g1HsjLtPMCYxmb1U4pcZ2TOegAKiomlmj6bIziuaEYHUavxLE9NRwdntY0vOCrgHho6OXjDX7fm/Kw==",
|
||||
"dev": true
|
||||
},
|
||||
"JSONStream": {
|
||||
"version": "1.3.1",
|
||||
"resolved": "https://registry.npmjs.org/JSONStream/-/JSONStream-1.3.1.tgz",
|
||||
"integrity": "sha1-cH92HgHa6eFvG8+TcDt4xwlmV5o=",
|
||||
"dev": true,
|
||||
"requires": {
|
||||
"jsonparse": "1.3.1",
|
||||
"through": "2.3.8"
|
||||
}
|
||||
},
|
||||
"acorn": {
|
||||
"version": "4.0.13",
|
||||
"resolved": "https://registry.npmjs.org/acorn/-/acorn-4.0.13.tgz",
|
||||
|
|
@ -1017,9 +1027,9 @@
|
|||
"integrity": "sha1-+GzWzvT1MAyOY+B6TVEvZfv/RTE=",
|
||||
"dev": true,
|
||||
"requires": {
|
||||
"JSONStream": "1.3.1",
|
||||
"combine-source-map": "0.7.2",
|
||||
"defined": "1.0.0",
|
||||
"JSONStream": "1.3.1",
|
||||
"through2": "2.0.3",
|
||||
"umd": "3.0.1"
|
||||
}
|
||||
|
|
@ -1047,6 +1057,7 @@
|
|||
"integrity": "sha1-tanJAgJD8McORnW+yCI7xifkFc4=",
|
||||
"dev": true,
|
||||
"requires": {
|
||||
"JSONStream": "1.3.1",
|
||||
"assert": "1.4.1",
|
||||
"browser-pack": "6.0.2",
|
||||
"browser-resolve": "1.11.2",
|
||||
|
|
@ -1068,7 +1079,6 @@
|
|||
"https-browserify": "0.0.1",
|
||||
"inherits": "2.0.3",
|
||||
"insert-module-globals": "7.0.1",
|
||||
"JSONStream": "1.3.1",
|
||||
"labeled-stream-splicer": "2.0.0",
|
||||
"module-deps": "4.1.1",
|
||||
"os-browserify": "0.1.2",
|
||||
|
|
@ -2487,10 +2497,10 @@
|
|||
"integrity": "sha1-wDv04BywhtW15azorQr+eInWOMM=",
|
||||
"dev": true,
|
||||
"requires": {
|
||||
"JSONStream": "1.3.1",
|
||||
"combine-source-map": "0.7.2",
|
||||
"concat-stream": "1.5.2",
|
||||
"is-buffer": "1.1.5",
|
||||
"JSONStream": "1.3.1",
|
||||
"lexical-scope": "1.2.0",
|
||||
"process": "0.11.10",
|
||||
"through2": "2.0.3",
|
||||
|
|
@ -2763,16 +2773,6 @@
|
|||
"integrity": "sha1-P02uSpH6wxX3EGL4UhzCOfE2YoA=",
|
||||
"dev": true
|
||||
},
|
||||
"JSONStream": {
|
||||
"version": "1.3.1",
|
||||
"resolved": "https://registry.npmjs.org/JSONStream/-/JSONStream-1.3.1.tgz",
|
||||
"integrity": "sha1-cH92HgHa6eFvG8+TcDt4xwlmV5o=",
|
||||
"dev": true,
|
||||
"requires": {
|
||||
"jsonparse": "1.3.1",
|
||||
"through": "2.3.8"
|
||||
}
|
||||
},
|
||||
"kind-of": {
|
||||
"version": "3.2.2",
|
||||
"resolved": "https://registry.npmjs.org/kind-of/-/kind-of-3.2.2.tgz",
|
||||
|
|
@ -3126,6 +3126,7 @@
|
|||
"integrity": "sha1-IyFYM/HaE/1gbMuAh7RIUty4If0=",
|
||||
"dev": true,
|
||||
"requires": {
|
||||
"JSONStream": "1.3.1",
|
||||
"browser-resolve": "1.11.2",
|
||||
"cached-path-relative": "1.0.1",
|
||||
"concat-stream": "1.5.2",
|
||||
|
|
@ -3133,7 +3134,6 @@
|
|||
"detective": "4.5.0",
|
||||
"duplexer2": "0.1.4",
|
||||
"inherits": "2.0.3",
|
||||
"JSONStream": "1.3.1",
|
||||
"parents": "1.0.1",
|
||||
"readable-stream": "2.2.11",
|
||||
"resolve": "1.3.3",
|
||||
|
|
|
|||
Loading…
Reference in New Issue