Skip to content

Commit

Permalink
Merge pull request #108 from HuolalaTech/fix/harmony-ua
Browse files Browse the repository at this point in the history
feat: update the ua in harmony SDK
  • Loading branch information
wqcstrong authored Aug 28, 2024
2 parents 0a5611f + e18819b commit 6c4118a
Show file tree
Hide file tree
Showing 29 changed files with 176 additions and 364 deletions.
2 changes: 1 addition & 1 deletion packages/page-spy-alipay/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
},
"scripts": {
"build": "rollup -c",
"build:watch": "rollup -c -w"
"watch": "rollup -c -w"
},
"dependencies": {
"@huolala-tech/page-spy-base": "^1.0.5",
Expand Down
1 change: 1 addition & 0 deletions packages/page-spy-base/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"types": "./dist/index.d.ts",
"scripts": {
"build": "yarn clean && tsc",
"watch": "tsc -w",
"clean": "rm -rf ./dist"
},
"publishConfig": {
Expand Down
86 changes: 14 additions & 72 deletions packages/page-spy-base/src/client.ts
Original file line number Diff line number Diff line change
@@ -1,76 +1,6 @@
/* eslint-disable no-restricted-syntax */
import { SpyClient } from '@huolala-tech/page-spy-types';

const browsers = {
WeChat: /MicroMessenger\/([\d.]+)/,
QQ: /(?:QQBrowser|MQQBrowser|QQ)\/([\d.]+)/,
UC: /(?:UCBrowser|UCBS)\/([\d.]+)/,
Baidu: /(?:BIDUBrowser|baiduboxapp)[/]?([\d.]*)/,
Edge: /Edg(?:e|A|iOS)?\/([\d.]+)/,
Chrome: /(?:Chrome|CriOS)\/([\d.]+)/,
Firefox: /(?:Firefox|FxiOS)\/([\d.]+)/,
Safari: /Version\/([\d.]+).*Safari/,
};

const platforms = {
Windows: /Windows NT ([\d_.]+)/,
iPhone: /iPhone OS ([\d_.]+)/,
iPad: /iPad.*OS ([\d_.]+)/,
Mac: /Mac OS X ([\d_.]+)/,
Android: /Android ([\d_.]+)/,
Linux: /Linux/,
};

export function parseUserAgent(
uaString: string = window.navigator.userAgent,
): SpyClient.ClientInfo {
let osType: SpyClient.OS = 'unknown';
let osVersion = 'unknown';
let browserType: SpyClient.Browser = 'unknown';
let browserVersion = 'unknown';

// 判断操作系统
for (const platform in platforms) {
if (Object.prototype.hasOwnProperty.call(platforms, platform)) {
const reg = platforms[platform as keyof typeof platforms];
const match = uaString.match(reg);
if (match) {
osType = platform as SpyClient.OS;
osVersion = match[1]?.replaceAll('_', '.');
break;
}
}
}

// 判断浏览器
for (const browser in browsers) {
if (Object.prototype.hasOwnProperty.call(browsers, browser)) {
const match = uaString.match(browsers[browser as keyof typeof browsers]);
if (match) {
browserType = browser as SpyClient.Browser;
// eslint-disable-next-line prefer-destructuring
browserVersion = match[1];
break;
}
}
}

return {
osType,
osVersion,
browserType,
browserVersion,
};
}

export const combineName = ({
osType,
osVersion,
browserType,
browserVersion,
}: SpyClient.ClientInfo) =>
`${osType}/${osVersion} ${browserType}/${browserVersion}`;

export class Client {
static info: SpyClient.ClientInfo = {
// browserName and framework should be overwritten by package implementation\
Expand All @@ -86,13 +16,25 @@ export class Client {
static plugins: string[] = [];

static makeClientInfoMsg() {
const ua = Client.info.ua || combineName(Client.info);
const msg: SpyClient.DataItem = {
sdk: Client.info.sdk,
isDevTools: Client.info.isDevTools,
ua,
ua: Client.getName(),
plugins: Client.plugins,
};
return msg;
}

private static _name: string;

static getName() {
if (!Client._name) {
const { ua, osType, osVersion, browserType, browserVersion } =
Client.info;

Client._name =
ua || `${osType}/${osVersion} ${browserType}/${browserVersion}`;
}
return Client._name;
}
}
45 changes: 39 additions & 6 deletions packages/page-spy-base/src/socket-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
* 不同平台 socket 的 api 不同但功能相同,这里抽象一层
*/

import { SpyMessage, SpySocket, SpyBase } from '@huolala-tech/page-spy-types';
import {
SpyMessage,
SpySocket,
SpyBase,
InitConfigBase,
} from '@huolala-tech/page-spy-types';
import { PackedEvent } from '@huolala-tech/page-spy-types/lib/socket-event';
import { getRandomId, psLog, stringifyData } from './utils';
import {
Expand Down Expand Up @@ -95,10 +100,6 @@ export abstract class SocketWrapper {
}

export abstract class SocketStoreBase {
protected abstract socketWrapper: SocketWrapper;

protected abstract updateRoomInfo(): void;

public socketUrl: string = '';

public socketConnection: SpySocket.Connection | null = null;
Expand Down Expand Up @@ -142,7 +143,39 @@ export abstract class SocketStoreBase {
// initial retry interval.
public retryInterval = INIT_RETRY_INTERVAL;

connectable = true;
public connectable = true;

protected abstract socketWrapper: SocketWrapper;

public getSocket() {
return this.socketWrapper;
}

public getPageSpyConfig: (() => Required<InitConfigBase>) | null = null;

updateRoomInfo() {
if (this.getPageSpyConfig) {
const { project, title } = this.getPageSpyConfig();
const name = Client.getName();
this.send(
{
type: SERVER_MESSAGE_TYPE.UPDATE_ROOM_INFO,
content: {
info: {
name,
group: project,
tags: {
title,
name,
group: project,
},
},
},
},
true,
);
}
}

// response message filters, to handle some wired messages
public static messageFilters: ((data: any) => any)[] = [];
Expand Down
2 changes: 1 addition & 1 deletion packages/page-spy-browser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
],
"scripts": {
"build": "rollup -c",
"build:watch": "rollup -c -w"
"watch": "rollup -c -w"
},
"dependencies": {
"@babel/runtime": "^7.13.0",
Expand Down
31 changes: 0 additions & 31 deletions packages/page-spy-browser/src/helpers/socket.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import {
stringifyData,
ROOM_SESSION_KEY,
UPDATE_ROOM_INFO,
SocketStoreBase,
SocketState,
SocketWrapper,
WebSocketEvents,
} from '@huolala-tech/page-spy-base';
import { InitConfig } from '../config';

export class WebSocketWrapper extends SocketWrapper {
public socketInstance: WebSocket | null = null;
Expand Down Expand Up @@ -41,35 +39,6 @@ export class WebSocketStore extends SocketStoreBase {
// websocket instance
protected socketWrapper: WebSocketWrapper = new WebSocketWrapper();

public getPageSpyConfig: (() => Required<InitConfig>) | null = null;

updateRoomInfo() {
if (this.getPageSpyConfig) {
const { project, title } = this.getPageSpyConfig();
this.send(
{
type: UPDATE_ROOM_INFO,
content: {
info: {
name: navigator.userAgent,
group: project,
tags: {
title,
name: navigator.userAgent,
group: project,
},
},
},
},
true,
);
}
}

public getSocket() {
return this.socketWrapper;
}

// disable lint: this is an abstract method of parent class, so it cannot be static
// eslint-disable-next-line class-methods-use-this
onOffline(): void {
Expand Down
4 changes: 2 additions & 2 deletions packages/page-spy-harmony/library/src/main/ets/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import url from '@ohos.url';
import http from '@ohos.net.http';
import { InitConfig } from '../types';
import { getRandomId } from '../utils';
import Client, { combineName } from '../utils/client';
import Client from '../utils/client';

interface TResponse<T> {
code: string;
Expand Down Expand Up @@ -50,7 +50,7 @@ export default class Request {
const scheme = this.getScheme();
const query = joinQuery({
// TODO
name: combineName(Client.info),
name: Client.getName(),
group: project,
title,
});
Expand Down
34 changes: 0 additions & 34 deletions packages/page-spy-harmony/library/src/main/ets/helpers/socket.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import webSocket from '@ohos.net.webSocket';
import { InitConfig } from '../types';
import { psLog, stringifyData } from '../utils';
import Client, { combineName } from '../utils/client';
import { UPDATE_ROOM_INFO } from '../utils/message/server-type';
import {
SocketStoreBase,
SocketState,
Expand Down Expand Up @@ -59,37 +56,6 @@ export class OHSocketWrapper extends SocketWrapper {
export class OHSocketStore extends SocketStoreBase {
protected socketWrapper: OHSocketWrapper = new OHSocketWrapper();

public getPageSpyConfig: (() => Required<InitConfig>) | null = null;

updateRoomInfo() {
if (this.getPageSpyConfig) {
const { project, title } = this.getPageSpyConfig();
const name = combineName(Client.info);

this.send(
{
type: UPDATE_ROOM_INFO,
content: {
info: {
name,
group: project,
tags: {
title,
name,
group: project,
},
},
},
},
true,
);
}
}

public getSocket() {
return this.socketWrapper;
}

onOffline(): void {
// AppStorage.delete(ROOM_SESSION_KEY);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import socketStore from '../helpers/socket';
import { InitConfig, OnInitParams, PageSpyPlugin, SpySystem } from '../types';
import Client, { combineName } from '../utils/client';
import Client from '../utils/client';
import { makeMessage } from '../utils/message';

export default class SystemPlugin implements PageSpyPlugin {
Expand Down Expand Up @@ -38,7 +38,7 @@ export default class SystemPlugin implements PageSpyPlugin {
public static getSystemInfo() {
return {
system: {
ua: combineName(Client.info),
ua: Client.getName(),
},
features: {},
};
Expand Down
Loading

0 comments on commit 6c4118a

Please sign in to comment.