-
Notifications
You must be signed in to change notification settings - Fork 351
refactor(utils): migrate utils from Flow to TypeScript #4795
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
bonchevskyi
wants to merge
1
commit into
box:master
Choose a base branch
from
bonchevskyi:refactor/flow-to-ts-utils
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| let isDashSupported: boolean | undefined; | ||
|
|
||
| class Browser { | ||
| /** | ||
| * Returns the user agent. | ||
| * Helps in mocking out. | ||
| */ | ||
| static getUserAgent(): string { | ||
| return globalThis.navigator.userAgent; | ||
| } | ||
|
|
||
| /** | ||
| * Returns whether browser is mobile, including tablets. | ||
| * | ||
| * We rely on user agent (UA) to avoid matching desktops with touchscreens. | ||
| * @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Browser_detection_using_the_user_agent#mobile_tablet_or_desktop | ||
| */ | ||
| static isMobile(): boolean { | ||
| const userAgent = Browser.getUserAgent(); | ||
| return ( | ||
| /iphone|ipad|ipod|android|blackberry|bb10|mini|windows\sce|palm/i.test(userAgent) || /Mobi/i.test(userAgent) | ||
| ); | ||
| } | ||
|
|
||
| /** Returns whether browser is IE. */ | ||
| static isIE(): boolean { | ||
| return /Trident/i.test(Browser.getUserAgent()); | ||
| } | ||
|
|
||
| /** Returns whether browser is Firefox. */ | ||
| static isFirefox(): boolean { | ||
| const userAgent = Browser.getUserAgent(); | ||
| return /Firefox/i.test(userAgent) && !/Seamonkey\//i.test(userAgent); | ||
| } | ||
|
|
||
| /** Returns whether browser is Safari. */ | ||
| static isSafari(): boolean { | ||
| const userAgent = Browser.getUserAgent(); | ||
| return /AppleWebKit/i.test(userAgent) && !/Chrome\//i.test(userAgent); | ||
| } | ||
|
|
||
| /** | ||
| * Returns whether browser is Mobile Safari. | ||
| * | ||
| * @see https://developer.chrome.com/docs/multidevice/user-agent/ | ||
| */ | ||
| static isMobileSafari(): boolean { | ||
| return Browser.isMobile() && Browser.isSafari() && !Browser.isMobileChromeOniOS(); | ||
| } | ||
|
|
||
| /** | ||
| * Returns whether browser is Mobile Chrome on iOS. | ||
| * | ||
| * @see https://developer.chrome.com/docs/multidevice/user-agent/ | ||
| */ | ||
| static isMobileChromeOniOS(): boolean { | ||
| const userAgent = Browser.getUserAgent(); | ||
| return Browser.isMobile() && /AppleWebKit/i.test(userAgent) && /CriOS\//i.test(userAgent); | ||
| } | ||
|
|
||
| /** | ||
| * Returns whether browser can download via HTML5. | ||
| * | ||
| * @see https://github.com/Modernizr/Modernizr/blob/master/feature-detects/a/download.js | ||
| */ | ||
| static canDownload(): boolean { | ||
| return ( | ||
| !Browser.isMobile() || | ||
| (!(window as Window & { externalHost?: unknown }).externalHost && 'download' in document.createElement('a')) | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Checks the browser for Dash support using H264 high. | ||
| * Dash requires MediaSource extensions to exist and be applicable | ||
| * to the H264 container (since we use H264 and not webm) | ||
| * | ||
| * @param recheck - recheck support | ||
| */ | ||
| static canPlayDash(recheck: boolean = false): boolean { | ||
| if (typeof isDashSupported === 'undefined' || recheck) { | ||
| const mse = globalThis.MediaSource; | ||
| isDashSupported = | ||
| !!mse && | ||
| typeof mse.isTypeSupported === 'function' && | ||
| mse.isTypeSupported('video/mp4; codecs="avc1.64001E"'); | ||
| } | ||
|
|
||
| return !!isDashSupported; | ||
| } | ||
|
|
||
| /** | ||
| * Checks whether the browser has support for the Clipboard API. This new API supercedes | ||
| * the `execCommand`-based API and uses Promises for detecting whether it works or not. | ||
| * | ||
| * This check determines if the browser can support writing to the clipboard. | ||
| * @see https://www.w3.org/TR/clipboard-apis/#async-clipboard-api | ||
| * @see https://developer.mozilla.org/en-US/docs/Web/API/Clipboard | ||
| */ | ||
| static canWriteToClipboard(): boolean { | ||
| return !!globalThis.navigator.clipboard?.writeText; | ||
| } | ||
|
|
||
| /** | ||
| * Checks whether the browser has support for the Clipboard API. This new API supercedes | ||
| * the `execCommand`-based API and uses Promises for detecting whether it works or not. | ||
| * | ||
| * This check determines if the browser can support reading from the clipboard. | ||
| * @see https://www.w3.org/TR/clipboard-apis/#async-clipboard-api | ||
| * @see https://developer.mozilla.org/en-US/docs/Web/API/Clipboard | ||
| */ | ||
| static canReadFromClipboard(): boolean { | ||
| return !!globalThis.navigator.clipboard?.readText; | ||
| } | ||
| } | ||
|
|
||
| export default Browser; |
File renamed without changes.
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| import merge from 'lodash/merge'; | ||
| import type { StringAnyMap } from '../common/types/core'; | ||
|
|
||
| class Cache { | ||
| cache: StringAnyMap; | ||
|
|
||
| constructor() { | ||
| this.cache = {}; | ||
| } | ||
|
|
||
| /** | ||
| * Caches a simple object in memory. | ||
| * | ||
| * @param {string} key The cache key | ||
| * @param {*} value The cache value | ||
| */ | ||
| set(key: string, value: unknown): void { | ||
| this.cache[key] = value; | ||
| } | ||
|
|
||
| /** | ||
| * Merges cached values for objects. | ||
| * | ||
| * @param {string} key The cache key | ||
| * @param {*} value The cache value | ||
| */ | ||
| merge(key: string, value: unknown): void { | ||
| if (this.has(key)) { | ||
| this.set(key, merge({}, this.get(key), value)); | ||
| } else { | ||
| throw new Error(`Key ${key} not in cache!`); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Deletes object from in-memory cache. | ||
| * | ||
| * @param {string} key The cache key | ||
| */ | ||
| unset(key: string): void { | ||
| delete this.cache[key]; | ||
| } | ||
|
|
||
| /** | ||
| * Deletes all object from in-memory cache | ||
| * that match the key as prefix. | ||
| * | ||
| * @param {string} prefix The cache key prefix | ||
| */ | ||
| unsetAll(prefix: string): void { | ||
| Object.keys(this.cache).forEach((key: string) => { | ||
| if (key.startsWith(prefix)) { | ||
| delete this.cache[key]; | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| /** Checks if cache has provided key. */ | ||
| has(key: string): boolean { | ||
| return {}.hasOwnProperty.call(this.cache, key); | ||
| } | ||
|
|
||
| /** | ||
| * Fetches a cached object from in-memory cache if available. | ||
| * | ||
| * @param {string} key Key of cached object | ||
| */ | ||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
| get(key: string): any { | ||
| if (this.has(key)) { | ||
| return this.cache[key]; | ||
| } | ||
|
|
||
| return undefined; | ||
| } | ||
| } | ||
|
|
||
| export default Cache; | ||
File renamed without changes.
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| import Cache from './Cache'; | ||
| import type APICache from './Cache'; | ||
|
|
||
| const KEY_PREFIX = 'localStore'; | ||
| const SERVICE_VERSION = '0'; | ||
|
|
||
| class LocalStore { | ||
| memoryStore: APICache; | ||
|
|
||
| localStorage: typeof localStorage; | ||
|
|
||
| isLocalStorageAvailable: boolean; | ||
|
|
||
| constructor() { | ||
| this.memoryStore = new Cache(); | ||
| try { | ||
| this.localStorage = window.localStorage; | ||
| this.isLocalStorageAvailable = this.canUseLocalStorage(); | ||
| } catch (e) { | ||
| this.isLocalStorageAvailable = false; | ||
| } | ||
| } | ||
|
|
||
| /** Builds a key for the session store. */ | ||
| buildKey(key: string): string { | ||
| return `${KEY_PREFIX}/${SERVICE_VERSION}/${key}`; | ||
| } | ||
|
|
||
| /** | ||
| * Test to see browser can use local storage. | ||
| * See http://stackoverflow.com/questions/14555347 | ||
| * Note that this will return false if we are actually hitting the maximum localStorage | ||
| * size (5MB / 2.5M chars) | ||
| * | ||
| * @private | ||
| */ | ||
| canUseLocalStorage(): boolean { | ||
| if (!this.localStorage) { | ||
| return false; | ||
| } | ||
|
|
||
| try { | ||
| this.localStorage.setItem(this.buildKey('TestKey'), 'testValue'); | ||
| this.localStorage.removeItem(this.buildKey('TestKey')); | ||
| return true; | ||
| } catch (e) { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| /** Set an item. */ | ||
| setItem(key: string, value: unknown): void { | ||
| if (this.isLocalStorageAvailable) { | ||
| try { | ||
| this.localStorage.setItem(this.buildKey(key), JSON.stringify(value)); | ||
| } catch (e) { | ||
| // no-op | ||
| } | ||
| } else { | ||
| this.memoryStore.set(key, value); | ||
| } | ||
|
bonchevskyi marked this conversation as resolved.
|
||
| } | ||
|
|
||
| /** Get an item. */ | ||
| getItem(key: string): unknown { | ||
| if (this.isLocalStorageAvailable) { | ||
| try { | ||
| const item = this.localStorage.getItem(this.buildKey(key)); | ||
| if (!item) { | ||
| return null; | ||
| } | ||
|
|
||
| return JSON.parse(item); | ||
| } catch (e) { | ||
| return null; | ||
| } | ||
| } else { | ||
| return this.memoryStore.get(key); | ||
| } | ||
| } | ||
|
|
||
| /** Remove an item. */ | ||
| removeItem(key: string): void { | ||
| if (this.isLocalStorageAvailable) { | ||
| try { | ||
| this.localStorage.removeItem(this.buildKey(key)); | ||
| } catch (e) { | ||
| // no-op | ||
| } | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| this.memoryStore.unset(key); | ||
| } | ||
| } | ||
|
|
||
| export default LocalStore; | ||
File renamed without changes.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
Use a null-prototype cache for arbitrary keys.
At Line [8],
this.cacheuses a normal object. At Line [18], direct assignment treats the key__proto__as a prototype setter.set('__proto__', value)therefore does not create a cache entry, sohas()andget()return incorrect results.Initialize the cache with
Object.create(null)or useMap.🤖 Prompt for AI Agents