mirror of https://github.com/ghostfolio/ghostfolio
12 changed files with 163 additions and 33 deletions
@ -0,0 +1,45 @@ |
|||||
|
import { isBrowserUserAgent } from './user-agent.helper'; |
||||
|
|
||||
|
describe('isBrowserUserAgent', () => { |
||||
|
it('should tell that the user agent of a browser is one', () => { |
||||
|
// Chrome on macOS
|
||||
|
expect( |
||||
|
isBrowserUserAgent( |
||||
|
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36' |
||||
|
) |
||||
|
).toBe(true); |
||||
|
|
||||
|
// Chrome on Android
|
||||
|
expect( |
||||
|
isBrowserUserAgent( |
||||
|
'Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Mobile Safari/537.36' |
||||
|
) |
||||
|
).toBe(true); |
||||
|
|
||||
|
// Firefox on Windows
|
||||
|
expect( |
||||
|
isBrowserUserAgent( |
||||
|
'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:143.0) Gecko/20100101 Firefox/143.0' |
||||
|
) |
||||
|
).toBe(true); |
||||
|
|
||||
|
// Safari on iPhone
|
||||
|
expect( |
||||
|
isBrowserUserAgent( |
||||
|
'Mozilla/5.0 (iPhone; CPU iPhone OS 18_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.6 Mobile/15E148 Safari/604.1' |
||||
|
) |
||||
|
).toBe(true); |
||||
|
}); |
||||
|
|
||||
|
it('should tell that the user agent of another client is not one', () => { |
||||
|
expect(isBrowserUserAgent('curl/8.7.1')).toBe(false); |
||||
|
expect(isBrowserUserAgent('node')).toBe(false); |
||||
|
expect(isBrowserUserAgent('python-requests/2.32.3')).toBe(false); |
||||
|
expect(isBrowserUserAgent('Dart/3.5 (dart:io)')).toBe(false); |
||||
|
}); |
||||
|
|
||||
|
it('should tell that a missing user agent is not one', () => { |
||||
|
expect(isBrowserUserAgent()).toBe(false); |
||||
|
expect(isBrowserUserAgent('')).toBe(false); |
||||
|
}); |
||||
|
}); |
||||
@ -0,0 +1,14 @@ |
|||||
|
const PREFIX_OF_BROWSER_USER_AGENT = 'Mozilla/'; |
||||
|
|
||||
|
/** |
||||
|
* Tells if the user agent is the one of a browser. Every major browser starts |
||||
|
* its user agent with the same prefix for compatibility, while other clients, |
||||
|
* such as scripts and native apps, give the name of their library instead. |
||||
|
* The value is for analytics only, because a client can send any user agent. |
||||
|
*/ |
||||
|
export function isBrowserUserAgent(userAgent?: string) { |
||||
|
return ( |
||||
|
typeof userAgent === 'string' && |
||||
|
userAgent.startsWith(PREFIX_OF_BROWSER_USER_AGENT) |
||||
|
); |
||||
|
} |
||||
@ -0,0 +1,5 @@ |
|||||
|
-- CreateEnum |
||||
|
CREATE TYPE "RequestClient" AS ENUM ('API', 'MCP', 'SELF_HOSTING', 'WEB'); |
||||
|
|
||||
|
-- AlterTable |
||||
|
ALTER TABLE "Analytics" ADD COLUMN "lastRequestClient" "RequestClient"; |
||||
Loading…
Reference in new issue