Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix initial pending request always being treated as an error. (backport #3660) #3724

Merged
merged 2 commits into from
Jun 1, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion common/api/core-common.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -7544,6 +7544,8 @@ export abstract class RpcProtocol {
getOperationFromPath(path: string): SerializedRpcOperation;
getStatus(code: number): RpcRequestStatus;
inflateToken(tokenFromBody: IModelRpcProps, _request: SerializedRpcRequest): IModelRpcProps;
// (undocumented)
initialize(_token?: IModelRpcProps): Promise<void>;
readonly invocationType: typeof RpcInvocation;
// (undocumented)
onRpcClientInitialized(_definition: RpcInterfaceDefinition, _client: RpcInterface): void;
Expand Down Expand Up @@ -9526,7 +9528,7 @@ export abstract class WebAppRpcProtocol extends RpcProtocol {
handleOperationPostRequest(req: HttpServerRequest, res: HttpServerResponse): Promise<void>;
abstract info: OpenAPIInfo;
// (undocumented)
initialize(): Promise<void>;
initialize(token?: IModelRpcProps): Promise<void>;
isTimeout(code: number): boolean;
get openAPIDescription(): RpcOpenAPIDescription;
pathPrefix: string;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@itwin/core-common",
"comment": "",
"type": "none"
}
],
"packageName": "@itwin/core-common"
}
3 changes: 3 additions & 0 deletions core/common/src/rpc/core/RpcProtocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,9 @@ export abstract class RpcProtocol {
return new (this.invocationType)(this, request).fulfillment;
}

/** @internal */
public async initialize(_token?: IModelRpcProps): Promise<void> { }

/** Serializes a request. */
public async serialize(request: RpcRequest): Promise<SerializedRpcRequest> {
const serializedContext = await RpcConfiguration.requestContext.serialize(request);
Expand Down
1 change: 1 addition & 0 deletions core/common/src/rpc/core/RpcRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,7 @@ export abstract class RpcRequest<TResponse = any> {
this._connecting = true;
RpcRequest._activeRequests.set(this.id, this);
this.protocol.events.raiseEvent(RpcProtocolEvent.RequestCreated, this);
await this.protocol.initialize(this.operation.policy.token(this));
this._sending = new Cancellable(this.setHeaders().then(async () => this.send()));
this.operation.policy.sentCallback(this);

Expand Down
11 changes: 6 additions & 5 deletions core/common/src/rpc/web/WebAppRpcProtocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@ import { CommonLoggerCategory } from "../../CommonLoggerCategory";
import { RpcInterface } from "../../RpcInterface";
import { RpcManager } from "../../RpcManager";
import { RpcRoutingToken } from "../core/RpcRoutingToken";
import { IModelRpcProps } from "../../IModel";

class InitializeInterface extends RpcInterface {
public static readonly interfaceName = "InitializeInterface";
public static readonly interfaceVersion = "1.0.0";
public async initialize() { return this.forward(arguments); }
public async initialize(_token?: IModelRpcProps) { return this.forward(arguments); }

public static createRequest(protocol: WebAppRpcProtocol) {
public static createRequest(protocol: WebAppRpcProtocol, token?: IModelRpcProps) {
const routing = RpcRoutingToken.generate();

const config = class extends RpcConfiguration {
Expand All @@ -39,7 +40,7 @@ class InitializeInterface extends RpcInterface {
RpcConfiguration.initializeInterfaces(instance);

const client = RpcManager.getClientForInterface(InitializeInterface, routing);
return new (protocol.requestType)(client, "initialize", []);
return new (protocol.requestType)(client, "initialize", [token]);
}
}

Expand Down Expand Up @@ -91,14 +92,14 @@ export abstract class WebAppRpcProtocol extends RpcProtocol {
public allowedHeaders: Set<string> = new Set();

/** @internal */
public async initialize() {
public override async initialize(token?: IModelRpcProps) {
if (this._initialized) {
return this._initialized;
}

return this._initialized = new Promise(async (resolve) => {
try {
const request = InitializeInterface.createRequest(this);
const request = InitializeInterface.createRequest(this, token);
const response = await request.preflight();
if (response && response.ok) {
(response.headers.get("Access-Control-Allow-Headers") || "").split(",").forEach((v) => this.allowedHeaders.add(v.trim()));
Expand Down
4 changes: 0 additions & 4 deletions core/common/src/rpc/web/WebAppRpcRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,10 +183,6 @@ export class WebAppRpcRequest extends RpcRequest {

/** Sends the request. */
protected async send(): Promise<number> {
if (this.method !== "options") {
await this.protocol.initialize();
}

this._loading = true;
await this.setupTransport();

Expand Down
25 changes: 22 additions & 3 deletions full-stack-tests/rpc/src/frontend/_Setup.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@
*--------------------------------------------------------------------------------------------*/

import { executeBackendCallback } from "@itwin/certa/lib/utils/CallbackUtils";
import { BentleyCloudRpcConfiguration, BentleyCloudRpcManager, RpcConfiguration } from "@itwin/core-common";
import { Logger, LogLevel } from "@itwin/core-bentley";
import { BentleyCloudRpcConfiguration, BentleyCloudRpcManager, RpcConfiguration, WebAppRpcProtocol } from "@itwin/core-common";
import { ElectronApp } from "@itwin/core-electron/lib/cjs/ElectronFrontend";
import { MobileRpcManager } from "@itwin/core-mobile/lib/cjs/MobileFrontend";
import { BackendTestCallbacks } from "../common/SideChannels";
import { AttachedInterface, MobileTestInterface, MultipleClientsInterface, rpcInterfaces } from "../common/TestRpcInterface";
import { AttachedInterface, MobileTestInterface, MultipleClientsInterface, rpcInterfaces, TestRpcInterface } from "../common/TestRpcInterface";
import { assert } from "chai";

RpcConfiguration.disableRoutingValidation = true;
Logger.initializeToConsole();
Logger.setLevelDefault(LogLevel.Warning);
RpcConfiguration.disableRoutingValidation = false;

function initializeCloud(protocol: string) {
const port = Number(window.location.port) + 2000;
Expand Down Expand Up @@ -62,3 +66,18 @@ before(async () => {
return ElectronApp.startup({ iModelApp: { rpcInterfaces } });
}
});

describe("BentleyCloudRpcManager", () => {
it("should initialize correctly when routing validation is enabled", async () => {
if (currentEnvironment === "http") {
const protocol = TestRpcInterface.getClient().configuration.protocol as WebAppRpcProtocol;
assert.equal(protocol.allowedHeaders.size, 0);
await TestRpcInterface.getClient().op16({ iModelId: "foo", key: "bar" }, { iModelId: "foo", key: "bar" });
assert.isAtLeast(protocol.allowedHeaders.size, 1);
}
});

after(() => {
RpcConfiguration.disableRoutingValidation = true;
});
});