UNPKG

@itwin/core-backend

Version:
71 lines 3.3 kB
/*--------------------------------------------------------------------------------------------- * Copyright (c) Bentley Systems, Incorporated. All rights reserved. * See LICENSE.md in the project root for license terms and full copyright notice. *--------------------------------------------------------------------------------------------*/ /** @packageDocumentation * @module RpcInterface */ import { BentleyStatus } from "@itwin/core-bentley"; import { IModelError, MarshalingBinaryMarker, RpcContentType, RpcMultipart, RpcResponseCacheControl, RpcSerializedValue, WEB_RPC_CONSTANTS, WebAppRpcProtocol, } from "@itwin/core-common"; /* eslint-disable @typescript-eslint/no-deprecated */ function parseHeaders(protocol, req) { const headerNames = protocol.serializedClientRequestContextHeaderNames; const parsedHeaders = { id: req.header(headerNames.id) || "", applicationId: req.header(headerNames.applicationId) || "", applicationVersion: req.header(headerNames.applicationVersion) || "", sessionId: req.header(headerNames.sessionId) || "", authorization: (headerNames.authorization ? req.header(headerNames.authorization) : "") ?? "", }; return parsedHeaders; } function parseFromPath(operation) { const decoded = operation.encodedRequest ? Buffer.from(operation.encodedRequest, "base64").toString("binary") : ""; return RpcSerializedValue.create(decoded); } async function parseFromBody(req) { const contentType = WebAppRpcProtocol.computeContentType(req.header(WEB_RPC_CONSTANTS.CONTENT)); if (contentType === RpcContentType.Binary) { const objects = JSON.stringify([MarshalingBinaryMarker.createDefault()]); const data = [req.body]; return RpcSerializedValue.create(objects, data); } else if (contentType === RpcContentType.Multipart) { return RpcMultipart.parseRequest(req); } else { return RpcSerializedValue.create(req.body); } } /** @internal */ export async function parseRequest(protocol, req) { // eslint-disable-next-line @typescript-eslint/no-non-null-assertion const operation = protocol.getOperationFromPath(req.url); const parsedHeaders = parseHeaders(protocol, req); const request = { ...parsedHeaders, operation: { interfaceDefinition: operation.interfaceDefinition, operationName: operation.operationName, interfaceVersion: operation.interfaceVersion, }, method: req.method, // eslint-disable-next-line @typescript-eslint/no-non-null-assertion path: req.url, parameters: operation.encodedRequest ? parseFromPath(operation) : await parseFromBody(req), caching: operation.encodedRequest ? RpcResponseCacheControl.Immutable : RpcResponseCacheControl.None, }; request.ip = req.ip; request.protocolVersion = 0; if (protocol.protocolVersionHeaderName) { const version = req.header(protocol.protocolVersionHeaderName); if (version) { request.protocolVersion = parseInt(version, 10); } } if (!request.id) { throw new IModelError(BentleyStatus.ERROR, `Invalid request: Missing required activity ID.`); } return request; } //# sourceMappingURL=request.js.map