-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
suggestion(server): refactor isWebviewEnvironment
- Loading branch information
Вахрамеев Сергей Сергеевич
authored and
Вахрамеев Сергей Сергеевич
committed
Dec 19, 2024
1 parent
577aa2a
commit cd7f623
Showing
9 changed files
with
43 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
export const THEME_QUERY = 'theme'; | ||
export const TITLE = 'title'; | ||
export const WEBVIEW_IOS_APP_ID_QUERY = 'applicationId'; | ||
export const WEBVIEW_IOS_APP_VERSION_QUERY = 'device_app_version'; | ||
export const WEBVIEW_NEXT_PAGE_ID_QUERY = 'nextPageId'; | ||
export const NATIVE_PARAMS_COOKIE_NAME = 'app_native_params'; | ||
export const APP_VERSION_HEADER_KEY = 'app-version'; | ||
export const IOS_APP_ID_QUERY_KEY = 'applicationId'; | ||
export const IOS_APP_VERSION_QUERY_KEY = 'device_app_version'; | ||
export const NATIVE_PARAMS_COOKIE_KEY = 'app_native_params'; | ||
export const NEXT_PAGE_ID_QUERY_KEY = 'nextPageId'; | ||
export const THEME_QUERY_KEY = 'theme'; | ||
export const TITLE_QUERY_KEY = 'title'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,20 @@ | ||
import { | ||
extractAppVersion, | ||
extractUserAgent, | ||
extractNativeParamsFromCookieHeader | ||
} from './utils'; | ||
import { RequestHeaderType } from './types'; | ||
import { extractAppVersion, extractUserAgent, extractNativeParams } from './utils'; | ||
import { UniversalRequest } from './types'; | ||
import { versionPattern, webviewUaIOSPattern } from './reg-exp-patterns'; | ||
|
||
export const isWebviewByUserAgent = ( | ||
userAgent: string, | ||
appVersion: string | undefined, | ||
) => { | ||
return ( | ||
(appVersion && versionPattern.test(appVersion)) || !!userAgent?.match(webviewUaIOSPattern) | ||
); | ||
}; | ||
export const isWebviewEnvironment = (request: UniversalRequest) => { | ||
const nativeParams = extractNativeParams(request); | ||
|
||
export const isWebviewByCookies = (nativeParamsFromCookies: Record<string, any> | null) => { | ||
return !!(nativeParamsFromCookies && nativeParamsFromCookies.isWebview) | ||
} | ||
export const isWebviewEnvironment = ( | ||
request: RequestHeaderType, | ||
): boolean => { | ||
const userAgent = extractUserAgent(request); | ||
if (nativeParams) { | ||
return true; | ||
} | ||
|
||
// `app-version` в заголовках – индикатор вебвью. В iOS есть только в первом запросе от webview | ||
// `app-version` в заголовках – индикатор вебвью. В iOS есть только в первом запросе от webview. | ||
const appVersion = extractAppVersion(request); | ||
const nativeParams = extractNativeParamsFromCookieHeader(request); | ||
const userAgent = extractUserAgent(request); | ||
|
||
return isWebviewByCookies(nativeParams) || isWebviewByUserAgent(userAgent, appVersion); | ||
return !!( | ||
(appVersion && versionPattern.test(appVersion)) || | ||
(userAgent && webviewUaIOSPattern.test(userAgent)) | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
import { NATIVE_PARAMS_COOKIE_NAME } from "./constants"; | ||
import { NATIVE_PARAMS_COOKIE_KEY } from "./constants"; | ||
import { NativeParams } from "../shared/types"; | ||
|
||
export const setNativeParamsCookie = (params: NativeParams, setCookie: (name: string, value: string) => void): void => { | ||
setCookie(NATIVE_PARAMS_COOKIE_NAME, encodeURIComponent(JSON.stringify(params))) | ||
setCookie(NATIVE_PARAMS_COOKIE_KEY, encodeURIComponent(JSON.stringify(params))) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters