From d5a0f8350d12c7bd7ff8a144a9441c6b98f4b9dd Mon Sep 17 00:00:00 2001 From: Lawrence Forooghian Date: Tue, 5 Sep 2023 10:26:07 +0100 Subject: [PATCH] wip tree-shakable http TODO test and check what happens if you don't provide one --- scripts/moduleReport.js | 2 ++ src/common/lib/client/baseclient.ts | 20 +++++++++++++++++++- src/common/lib/client/modulesmap.ts | 3 +++ src/common/platform.ts | 9 +++++++++ src/platform/web/modules.ts | 1 + src/platform/web/modules/http.ts | 2 ++ 6 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 src/platform/web/modules/http.ts diff --git a/scripts/moduleReport.js b/scripts/moduleReport.js index a8937d594b..609a3d1e93 100644 --- a/scripts/moduleReport.js +++ b/scripts/moduleReport.js @@ -9,6 +9,8 @@ const moduleNames = [ 'XHRPolling', 'XHRStreaming', 'WebSocketTransport', + 'XHRRequest', + 'FetchRequest', ]; // List of all free-standing functions exported by the library along with the diff --git a/src/common/lib/client/baseclient.ts b/src/common/lib/client/baseclient.ts index f22864fb08..c6c5fddf3a 100644 --- a/src/common/lib/client/baseclient.ts +++ b/src/common/lib/client/baseclient.ts @@ -8,7 +8,7 @@ import { StandardCallback } from '../../types/utils'; import { IHttp, RequestParams } from '../../types/http'; import ClientOptions, { NormalisedClientOptions } from '../../types/ClientOptions'; -import Platform from '../../platform'; +import Platform, { HTTPRequestImplementations, HTTPRequestMechanismNames } from '../../platform'; import { ModulesMap } from './modulesmap'; import { Rest } from './rest'; import { IUntypedCryptoStatic } from 'common/types/ICryptoStatic'; @@ -31,8 +31,12 @@ class BaseClient { private readonly _rest: Rest | null; readonly _Crypto: IUntypedCryptoStatic | null; readonly _MsgPack: MsgPack | null; + // Extra HTTP request implementations available to this client, in addition to those directly accessed in our HTTP code + readonly _additionalHTTPRequestImplementations: HTTPRequestImplementations; constructor(options: ClientOptions | string, modules: ModulesMap) { + this._additionalHTTPRequestImplementations = BaseClient.httpRequestImplementationsFromModules(modules); + if (!options) { const msg = 'no options provided'; Logger.logAction(Logger.LOG_ERROR, 'BaseClient()', msg); @@ -85,6 +89,20 @@ class BaseClient { this._Crypto = modules.Crypto ?? null; } + private static httpRequestImplementationsFromModules(modules: ModulesMap) { + const implementations: HTTPRequestImplementations = {}; + + if (modules.XHRRequest) { + implementations[HTTPRequestMechanismNames.XHR] = modules.XHRRequest; + } + + if (modules.FetchRequest) { + implementations[HTTPRequestMechanismNames.Fetch] = modules.FetchRequest; + } + + return implementations; + } + private get rest(): Rest { if (!this._rest) { throwMissingModuleError('Crypto'); diff --git a/src/common/lib/client/modulesmap.ts b/src/common/lib/client/modulesmap.ts index ada7de44ee..140dd9e41c 100644 --- a/src/common/lib/client/modulesmap.ts +++ b/src/common/lib/client/modulesmap.ts @@ -3,6 +3,7 @@ import { IUntypedCryptoStatic } from '../../types/ICryptoStatic'; import { MsgPack } from 'common/types/msgpack'; import RealtimePresence from './realtimepresence'; import { TransportInitialiser } from '../transport/connectionmanager'; +import { IHttp } from 'common/types/http'; export interface ModulesMap { Rest?: typeof Rest; @@ -12,6 +13,8 @@ export interface ModulesMap { WebSocketTransport?: TransportInitialiser; XHRPolling?: TransportInitialiser; XHRStreaming?: TransportInitialiser; + XHRRequest?: IHttp; + FetchRequest?: IHttp; } export const allCommonModules: ModulesMap = { Rest }; diff --git a/src/common/platform.ts b/src/common/platform.ts index 609b232687..0dc163a30c 100644 --- a/src/common/platform.ts +++ b/src/common/platform.ts @@ -15,6 +15,15 @@ type ToBufferOutput = WebBufferUtils.ToBufferOutput | NodeBufferUtils.ToBufferOu export type TransportImplementations = Partial>; +export namespace HTTPRequestMechanismNames { + export const XHR = 'xhr' as const; + export const Fetch = 'fetch' as const; +} + +export type HTTPRequestMechanismName = typeof HTTPRequestMechanismNames.XHR | typeof HTTPRequestMechanismNames.Fetch; + +export type HTTPRequestImplementations = Partial>; + export default class Platform { static Config: IPlatformConfig; /* diff --git a/src/platform/web/modules.ts b/src/platform/web/modules.ts index 50d342306d..5a4f778390 100644 --- a/src/platform/web/modules.ts +++ b/src/platform/web/modules.ts @@ -44,5 +44,6 @@ export * from './modules/presencemessage'; export * from './modules/msgpack'; export * from './modules/realtimepresence'; export * from './modules/transports'; +export * from './modules/http'; export { Rest } from '../../common/lib/client/rest'; export { BaseRest, BaseRealtime }; diff --git a/src/platform/web/modules/http.ts b/src/platform/web/modules/http.ts new file mode 100644 index 0000000000..a966fa4691 --- /dev/null +++ b/src/platform/web/modules/http.ts @@ -0,0 +1,2 @@ +export { default as XHRRequest } from '../lib/transport/xhrrequest'; +export { default as FetchRequest } from '../lib/transport/fetchrequest';