Pass user agent to worker

This commit is contained in:
Alexander Zinchuk 2021-06-21 01:57:54 +03:00
parent 8d80c41c95
commit dfd3b522cf
6 changed files with 32 additions and 13 deletions

View File

@ -5,7 +5,11 @@ import { Logger as GramJsLogger } from '../../../lib/gramjs/extensions/index';
import { TwoFaParams } from '../../../lib/gramjs/client/2fa'; import { TwoFaParams } from '../../../lib/gramjs/client/2fa';
import { import {
ApiMediaFormat, ApiOnProgress, ApiSessionData, OnApiUpdate, ApiInitialArgs,
ApiMediaFormat,
ApiOnProgress,
ApiSessionData,
OnApiUpdate,
} from '../../types'; } from '../../types';
import { import {
@ -32,7 +36,7 @@ let onUpdate: OnApiUpdate;
let client: TelegramClient; let client: TelegramClient;
let isConnected = false; let isConnected = false;
export async function init(_onUpdate: OnApiUpdate, sessionData?: ApiSessionData) { export async function init(_onUpdate: OnApiUpdate, initialArgs: ApiInitialArgs) {
onUpdate = _onUpdate; onUpdate = _onUpdate;
if (DEBUG) { if (DEBUG) {
@ -40,12 +44,14 @@ export async function init(_onUpdate: OnApiUpdate, sessionData?: ApiSessionData)
console.log('>>> START INIT API'); console.log('>>> START INIT API');
} }
const { sessionData, userAgent } = initialArgs;
client = new TelegramClient( client = new TelegramClient(
new sessions.CallbackSession(sessionData, onSessionUpdate), new sessions.CallbackSession(sessionData, onSessionUpdate),
process.env.TELEGRAM_T_API_ID, process.env.TELEGRAM_T_API_ID,
process.env.TELEGRAM_T_API_HASH, process.env.TELEGRAM_T_API_HASH,
{ {
deviceModel: navigator.userAgent || DEFAULT_USER_AGENT, deviceModel: navigator.userAgent || userAgent || DEFAULT_USER_AGENT,
appVersion: `${APP_VERSION} ${APP_CODE_NAME}`, appVersion: `${APP_VERSION} ${APP_CODE_NAME}`,
useWSS: true, useWSS: true,
additionalDcsDisabled: IS_TEST, additionalDcsDisabled: IS_TEST,
@ -153,7 +159,9 @@ export async function invokeRequest<T extends GramJs.AnyRequest>(
} }
if (shouldHandleUpdates) { if (shouldHandleUpdates) {
type ResultWithUpdates = typeof result & { updates?: GramJs.Updates | GramJs.UpdatesCombined }; type ResultWithUpdates =
typeof result
& { updates?: GramJs.Updates | GramJs.UpdatesCombined };
let updatesContainer; let updatesContainer;
if (result instanceof GramJs.Updates || result instanceof GramJs.UpdatesCombined) { if (result instanceof GramJs.Updates || result instanceof GramJs.UpdatesCombined) {

View File

@ -1,5 +1,8 @@
import { import {
ApiOnProgress, ApiSessionData, ApiUpdate, OnApiUpdate, OnApiUpdate,
ApiInitialArgs,
ApiUpdate,
ApiOnProgress,
} from '../types'; } from '../types';
import { Methods, MethodArgs, MethodResponse } from './methods/types'; import { Methods, MethodArgs, MethodResponse } from './methods/types';
@ -18,7 +21,7 @@ import * as methods from './methods';
let onUpdate: OnApiUpdate; let onUpdate: OnApiUpdate;
export async function initApi(_onUpdate: OnApiUpdate, sessionData?: ApiSessionData) { export async function initApi(_onUpdate: OnApiUpdate, initialArgs: ApiInitialArgs) {
onUpdate = _onUpdate; onUpdate = _onUpdate;
initUpdater(handleUpdate); initUpdater(handleUpdate);
@ -30,7 +33,7 @@ export async function initApi(_onUpdate: OnApiUpdate, sessionData?: ApiSessionDa
initManagement(handleUpdate); initManagement(handleUpdate);
initTwoFaSettings(handleUpdate); initTwoFaSettings(handleUpdate);
await initClient(handleUpdate, sessionData); await initClient(handleUpdate, initialArgs);
} }
export function callApi<T extends keyof Methods>(fnName: T, ...args: MethodArgs<T>): MethodResponse<T> { export function callApi<T extends keyof Methods>(fnName: T, ...args: MethodArgs<T>): MethodResponse<T> {

View File

@ -1,6 +1,6 @@
import Worker from 'worker-loader!./worker'; import Worker from 'worker-loader!./worker';
import { ApiOnProgress, ApiSessionData, OnApiUpdate } from '../../types'; import { ApiInitialArgs, ApiOnProgress, OnApiUpdate } from '../../types';
import { Methods, MethodArgs, MethodResponse } from '../methods/types'; import { Methods, MethodArgs, MethodResponse } from '../methods/types';
import { WorkerMessageEvent, ThenArg, OriginRequest } from './types'; import { WorkerMessageEvent, ThenArg, OriginRequest } from './types';
@ -20,7 +20,7 @@ const requestStatesByCallback = new Map<AnyToVoidFunction, RequestStates>();
// TODO Re-use `util/WorkerConnector.ts` // TODO Re-use `util/WorkerConnector.ts`
export function initApi(onUpdate: OnApiUpdate, sessionData?: ApiSessionData) { export function initApi(onUpdate: OnApiUpdate, initialArgs: ApiInitialArgs) {
if (!worker) { if (!worker) {
if (DEBUG) { if (DEBUG) {
// eslint-disable-next-line no-console // eslint-disable-next-line no-console
@ -33,7 +33,7 @@ export function initApi(onUpdate: OnApiUpdate, sessionData?: ApiSessionData) {
return makeRequest({ return makeRequest({
type: 'initApi', type: 'initApi',
args: [sessionData], args: [initialArgs],
}); });
} }

View File

@ -1,4 +1,4 @@
import { ApiSessionData, ApiUpdate } from '../../types'; import { ApiInitialArgs, ApiUpdate } from '../../types';
import { Methods, MethodArgs, MethodResponse } from '../methods/types'; import { Methods, MethodArgs, MethodResponse } from '../methods/types';
export type ThenArg<T> = T extends Promise<infer U> ? U : T; export type ThenArg<T> = T extends Promise<infer U> ? U : T;
@ -27,7 +27,7 @@ export interface WorkerMessageEvent {
export type OriginRequest = { export type OriginRequest = {
type: 'initApi'; type: 'initApi';
messageId?: string; messageId?: string;
args: [ApiSessionData | undefined]; args: [ApiInitialArgs];
} | { } | {
type: 'callMethod'; type: 'callMethod';
messageId?: string; messageId?: string;

View File

@ -1,5 +1,10 @@
import { ApiDocument } from './messages'; import { ApiDocument } from './messages';
export interface ApiInitialArgs {
userAgent: string;
sessionData?: ApiSessionData;
}
export interface ApiOnProgress { export interface ApiOnProgress {
( (
progress: number, // Float between 0 and 1. progress: number, // Float between 0 and 1.

View File

@ -34,7 +34,10 @@ addReducer('initApi', (global: GlobalState, actions) => {
importTestSession(); importTestSession();
} }
void initApi(actions.apiUpdate, loadStoredSession()); void initApi(actions.apiUpdate, {
userAgent: navigator.userAgent,
sessionData: loadStoredSession(),
});
})(); })();
}); });