Skip to content

Commit

Permalink
wip tree-shakable http
Browse files Browse the repository at this point in the history
TODO test and check what happens if you don't provide one

note: the additionalHTTPRequestImplementations is copied from
additionalTransportImplementations. but I think the approach doesn't
need to be the same, because we have one connection manager which had to
make use of available transports, but for HTTP we have separate classes
for web and node

perhaps we'd be best off adding some sort of bundledImplementations
thing on the web HTTP class, such that it can be used in tests directly?
  • Loading branch information
lawrence-forooghian committed Oct 25, 2023
1 parent 667b42f commit 61a75b0
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 2 deletions.
2 changes: 2 additions & 0 deletions scripts/moduleReport.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ const moduleNames = [
'XHRPolling',
'XHRStreaming',
'WebSocketTransport',
'XHRRequest',
'FetchRequest',
];

// List of all free-standing functions exported by the library along with the
Expand Down
20 changes: 19 additions & 1 deletion src/common/lib/client/baseclient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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);
Expand Down Expand Up @@ -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');
Expand Down
3 changes: 3 additions & 0 deletions src/common/lib/client/modulesmap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -12,6 +13,8 @@ export interface ModulesMap {
WebSocketTransport?: TransportInitialiser;
XHRPolling?: TransportInitialiser;
XHRStreaming?: TransportInitialiser;
XHRRequest?: IHttp;
FetchRequest?: IHttp;
}

export const allCommonModules: ModulesMap = { Rest };
11 changes: 10 additions & 1 deletion src/common/platform.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { IPlatformConfig } from './types/IPlatformConfig';
import { IHttpStatic } from './types/http';
import { IHttp, IHttpStatic } from './types/http';
import { TransportInitialiser } from './lib/transport/connectionmanager';
import IDefaults from './types/IDefaults';
import IWebStorage from './types/IWebStorage';
Expand All @@ -15,6 +15,15 @@ type ToBufferOutput = WebBufferUtils.ToBufferOutput | NodeBufferUtils.ToBufferOu

export type TransportImplementations = Partial<Record<TransportName, TransportInitialiser>>;

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<Record<HTTPRequestMechanismName, IHttp>>;

export default class Platform {
static Config: IPlatformConfig;
/*
Expand Down
1 change: 1 addition & 0 deletions src/platform/web/modules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
2 changes: 2 additions & 0 deletions src/platform/web/modules/http.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { default as XHRRequest } from '../lib/transport/xhrrequest';
export { default as FetchRequest } from '../lib/transport/fetchrequest';

0 comments on commit 61a75b0

Please sign in to comment.