@jolibox/implement 1.1.4-beta.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintrc.js +3 -0
- package/.rush/temp/package-deps_build.json +104 -0
- package/.rush/temp/shrinkwrap-deps.json +79 -0
- package/README.md +1 -0
- package/dist/common/api-factory/index.d.ts +21 -0
- package/dist/common/api-factory/validator/__tests__/validate/any.test.d.ts +1 -0
- package/dist/common/api-factory/validator/__tests__/validate/array.test.d.ts +1 -0
- package/dist/common/api-factory/validator/__tests__/validate/arraybuffer.test.d.ts +1 -0
- package/dist/common/api-factory/validator/__tests__/validate/boolean.test.d.ts +1 -0
- package/dist/common/api-factory/validator/__tests__/validate/enum.test.d.ts +1 -0
- package/dist/common/api-factory/validator/__tests__/validate/function.test.d.ts +1 -0
- package/dist/common/api-factory/validator/__tests__/validate/literal.test.d.ts +1 -0
- package/dist/common/api-factory/validator/__tests__/validate/nullish.test.d.ts +1 -0
- package/dist/common/api-factory/validator/__tests__/validate/number.test.d.ts +1 -0
- package/dist/common/api-factory/validator/__tests__/validate/object.test.d.ts +1 -0
- package/dist/common/api-factory/validator/__tests__/validate/or.test.d.ts +1 -0
- package/dist/common/api-factory/validator/__tests__/validate/record.test.d.ts +1 -0
- package/dist/common/api-factory/validator/__tests__/validate/string.test.d.ts +1 -0
- package/dist/common/api-factory/validator/__tests__/validate/symbol.test.d.ts +1 -0
- package/dist/common/api-factory/validator/__tests__/validate/tuple.test.d.ts +1 -0
- package/dist/common/api-factory/validator/__tests__/validate/type-asserts.test.d.ts +1 -0
- package/dist/common/api-factory/validator/__tests__/validate/utils.test.d.ts +1 -0
- package/dist/common/api-factory/validator/index.d.ts +29 -0
- package/dist/common/api-factory/validator/validate.d.ts +119 -0
- package/dist/common/can-i-use.d.ts +2 -0
- package/dist/common/context/index.d.ts +16 -0
- package/dist/common/context/types.d.ts +5 -0
- package/dist/common/context/url-parse.d.ts +22 -0
- package/dist/common/http/index.d.ts +13 -0
- package/dist/common/http/uuid.d.ts +2 -0
- package/dist/common/http/xua.d.ts +17 -0
- package/dist/common/report/base-tracker.d.ts +13 -0
- package/dist/common/report/create-trace.d.ts +7 -0
- package/dist/common/report/errors/error-types.d.ts +122 -0
- package/dist/common/report/errors/index.d.ts +13 -0
- package/dist/common/report/errors/report/index.d.ts +51 -0
- package/dist/common/report/errors/report/listeners.d.ts +1 -0
- package/dist/common/report/index.d.ts +3 -0
- package/dist/common/report/task-track/index.d.ts +23 -0
- package/dist/common/report/track.d.ts +3 -0
- package/dist/common/report/types.d.ts +75 -0
- package/dist/h5/ads/ads-action-detection.d.ts +6 -0
- package/dist/h5/ads/anti-cheating.d.ts +61 -0
- package/dist/h5/ads/index.d.ts +275 -0
- package/dist/h5/api/base.d.ts +13 -0
- package/dist/h5/api/get-system-info.d.ts +1 -0
- package/dist/h5/api/index.d.ts +4 -0
- package/dist/h5/api/lifecycle.d.ts +1 -0
- package/dist/h5/api/storage.d.ts +27 -0
- package/dist/h5/api/task.d.ts +1 -0
- package/dist/h5/bootstrap/index.d.ts +1 -0
- package/dist/h5/http/index.d.ts +33 -0
- package/dist/h5/http/utils/__tests__/uuid.test.d.ts +1 -0
- package/dist/h5/http/utils/__tests__/xua.test.d.ts +1 -0
- package/dist/h5/http/utils/index.d.ts +14 -0
- package/dist/h5/http/utils/session.d.ts +1 -0
- package/dist/h5/report/errors/index.d.ts +4 -0
- package/dist/h5/report/event-tracker.d.ts +8 -0
- package/dist/h5/report/index.d.ts +10 -0
- package/dist/h5/report/task-tracker.d.ts +14 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +5026 -0
- package/dist/index.native.d.ts +2 -0
- package/dist/index.native.js +3743 -0
- package/dist/native/api/base.d.ts +13 -0
- package/dist/native/api/get-system-info.d.ts +1 -0
- package/dist/native/api/index.d.ts +7 -0
- package/dist/native/api/keyboard.d.ts +3 -0
- package/dist/native/api/lifecycle.d.ts +1 -0
- package/dist/native/api/login.d.ts +1 -0
- package/dist/native/api/request.d.ts +1 -0
- package/dist/native/api/storage.d.ts +25 -0
- package/dist/native/api/task.d.ts +1 -0
- package/dist/native/bootstrap/bridge.d.ts +4 -0
- package/dist/native/bootstrap/index.d.ts +1 -0
- package/dist/native/js-bridge/const.d.ts +5 -0
- package/dist/native/js-bridge/index.d.ts +2 -0
- package/dist/native/js-bridge/invoke.d.ts +21 -0
- package/dist/native/js-bridge/js-bridge.d.ts +6 -0
- package/dist/native/js-bridge/report.d.ts +63 -0
- package/dist/native/js-bridge/subscribe.d.ts +8 -0
- package/dist/native/js-bridge/types.d.ts +14 -0
- package/dist/native/js-bridge/utils.d.ts +17 -0
- package/dist/native/js-core/index.d.ts +3 -0
- package/dist/native/js-core/jolibox-js-core.d.ts +45 -0
- package/dist/native/js-core/message-port.d.ts +12 -0
- package/dist/native/js-core/utils.d.ts +7 -0
- package/dist/native/network/create-fetch.d.ts +27 -0
- package/dist/native/network/index.d.ts +11 -0
- package/dist/native/network/report.d.ts +15 -0
- package/dist/native/network/types.d.ts +61 -0
- package/dist/native/network/utils.d.ts +9 -0
- package/dist/native/report/errors/index.d.ts +4 -0
- package/dist/native/report/index.d.ts +10 -0
- package/dist/native/report/task-tracker.d.ts +20 -0
- package/dist/utils/index.d.ts +0 -0
- package/esbuild.config.js +66 -0
- package/implement.build.log +9 -0
- package/package.json +30 -0
- package/src/common/api-factory/index.ts +188 -0
- package/src/common/api-factory/validator/__tests__/validate/any.test.ts +68 -0
- package/src/common/api-factory/validator/__tests__/validate/array.test.ts +402 -0
- package/src/common/api-factory/validator/__tests__/validate/arraybuffer.test.ts +48 -0
- package/src/common/api-factory/validator/__tests__/validate/boolean.test.ts +27 -0
- package/src/common/api-factory/validator/__tests__/validate/enum.test.ts +106 -0
- package/src/common/api-factory/validator/__tests__/validate/function.test.ts +54 -0
- package/src/common/api-factory/validator/__tests__/validate/literal.test.ts +130 -0
- package/src/common/api-factory/validator/__tests__/validate/nullish.test.ts +41 -0
- package/src/common/api-factory/validator/__tests__/validate/number.test.ts +147 -0
- package/src/common/api-factory/validator/__tests__/validate/object.test.ts +131 -0
- package/src/common/api-factory/validator/__tests__/validate/or.test.ts +96 -0
- package/src/common/api-factory/validator/__tests__/validate/record.test.ts +274 -0
- package/src/common/api-factory/validator/__tests__/validate/string.test.ts +187 -0
- package/src/common/api-factory/validator/__tests__/validate/symbol.test.ts +23 -0
- package/src/common/api-factory/validator/__tests__/validate/tuple.test.ts +86 -0
- package/src/common/api-factory/validator/__tests__/validate/type-asserts.test.ts +13 -0
- package/src/common/api-factory/validator/__tests__/validate/utils.test.ts +44 -0
- package/src/common/api-factory/validator/index.ts +107 -0
- package/src/common/api-factory/validator/validate.ts +639 -0
- package/src/common/can-i-use.ts +19 -0
- package/src/common/context/index.ts +91 -0
- package/src/common/context/types.ts +5 -0
- package/src/common/context/url-parse.ts +63 -0
- package/src/common/http/index.ts +29 -0
- package/src/common/http/uuid.ts +11 -0
- package/src/common/http/xua.ts +79 -0
- package/src/common/report/base-tracker.ts +134 -0
- package/src/common/report/create-trace.ts +17 -0
- package/src/common/report/errors/error-types.ts +206 -0
- package/src/common/report/errors/index.ts +20 -0
- package/src/common/report/errors/report/index.ts +63 -0
- package/src/common/report/errors/report/listeners.ts +80 -0
- package/src/common/report/index.ts +3 -0
- package/src/common/report/task-track/index.ts +97 -0
- package/src/common/report/track.ts +49 -0
- package/src/common/report/types.ts +90 -0
- package/src/h5/ads/ads-action-detection.ts +31 -0
- package/src/h5/ads/anti-cheating.ts +244 -0
- package/src/h5/ads/index.ts +641 -0
- package/src/h5/api/base.ts +9 -0
- package/src/h5/api/get-system-info.ts +55 -0
- package/src/h5/api/index.ts +4 -0
- package/src/h5/api/lifecycle.ts +110 -0
- package/src/h5/api/storage.ts +173 -0
- package/src/h5/api/task.ts +197 -0
- package/src/h5/bootstrap/index.ts +16 -0
- package/src/h5/http/index.ts +189 -0
- package/src/h5/http/utils/__tests__/uuid.test.ts +16 -0
- package/src/h5/http/utils/__tests__/xua.test.ts +27 -0
- package/src/h5/http/utils/index.ts +19 -0
- package/src/h5/http/utils/session.ts +10 -0
- package/src/h5/report/errors/index.ts +40 -0
- package/src/h5/report/event-tracker.ts +40 -0
- package/src/h5/report/index.ts +56 -0
- package/src/h5/report/task-tracker.ts +35 -0
- package/src/index.native.ts +7 -0
- package/src/index.ts +9 -0
- package/src/native/api/base.ts +8 -0
- package/src/native/api/get-system-info.ts +41 -0
- package/src/native/api/index.ts +7 -0
- package/src/native/api/keyboard.ts +75 -0
- package/src/native/api/lifecycle.ts +76 -0
- package/src/native/api/login.ts +71 -0
- package/src/native/api/request.ts +154 -0
- package/src/native/api/storage.ts +287 -0
- package/src/native/api/task.ts +267 -0
- package/src/native/bootstrap/bridge.ts +59 -0
- package/src/native/bootstrap/index.ts +58 -0
- package/src/native/js-bridge/const.ts +11 -0
- package/src/native/js-bridge/index.ts +2 -0
- package/src/native/js-bridge/invoke.ts +210 -0
- package/src/native/js-bridge/js-bridge.ts +23 -0
- package/src/native/js-bridge/report.ts +311 -0
- package/src/native/js-bridge/subscribe.ts +50 -0
- package/src/native/js-bridge/types.ts +26 -0
- package/src/native/js-bridge/utils.ts +116 -0
- package/src/native/js-core/index.ts +4 -0
- package/src/native/js-core/jolibox-js-core.ts +188 -0
- package/src/native/js-core/message-port.ts +52 -0
- package/src/native/js-core/utils.ts +9 -0
- package/src/native/network/create-fetch.ts +237 -0
- package/src/native/network/index.ts +15 -0
- package/src/native/network/report.ts +58 -0
- package/src/native/network/types.ts +77 -0
- package/src/native/network/utils.ts +90 -0
- package/src/native/report/errors/index.ts +27 -0
- package/src/native/report/index.ts +51 -0
- package/src/native/report/task-tracker.ts +64 -0
- package/src/native/types/global.d.ts +26 -0
- package/src/native/types/native-method-map.d.ts +282 -0
- package/src/native/types/native-method.d.ts +30 -0
- package/src/utils/index.ts +0 -0
- package/tsconfig.json +18 -0
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
// import { expect, test } from 'jest';
|
|
2
|
+
import { xUserAgent, getAppVersion } from '../../../../common/http/xua';
|
|
3
|
+
import { isValidUUIDV4 } from '../../../../common/http/uuid';
|
|
4
|
+
|
|
5
|
+
describe('xua', () => {
|
|
6
|
+
it('test xua', () => {
|
|
7
|
+
// window.__JOLIBOX_LOCAL_SDK_VERSION__ = '1.0.0';
|
|
8
|
+
const xua = xUserAgent();
|
|
9
|
+
const deviceInfoRegex = /.* (\(.*\) ).*/;
|
|
10
|
+
const deviceInfoMatch = xua.match(deviceInfoRegex);
|
|
11
|
+
const deviceInfo = deviceInfoMatch?.[1] ?? '';
|
|
12
|
+
const rest = xua.replace(deviceInfo, '');
|
|
13
|
+
expect(deviceInfo).toBe('(JoliboxSDKPC; UnknownModel; UnknownSystemVersion; en-US) ');
|
|
14
|
+
|
|
15
|
+
const [appName, uuidPart, adidPart, versionPart] = rest.split(' ');
|
|
16
|
+
const uuid = uuidPart.split('/')[1];
|
|
17
|
+
const adid = adidPart.split('/')[1];
|
|
18
|
+
const version = versionPart.split('/')[1];
|
|
19
|
+
expect(appName).toBe('JoliboxWebSDK');
|
|
20
|
+
expect(uuidPart.startsWith('uuid/')).toBe(true);
|
|
21
|
+
expect(adidPart.startsWith('adid/')).toBe(true);
|
|
22
|
+
expect(versionPart.startsWith('version/')).toBe(true);
|
|
23
|
+
expect(isValidUUIDV4(uuid)).toBe(true);
|
|
24
|
+
expect(isValidUUIDV4(adid)).toBe(true);
|
|
25
|
+
expect(version).toBe(getAppVersion());
|
|
26
|
+
});
|
|
27
|
+
});
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import * as xua from '@/common/http/xua';
|
|
2
|
+
import * as session from './session';
|
|
3
|
+
import * as uuid from '@/common/http/uuid';
|
|
4
|
+
|
|
5
|
+
declare global {
|
|
6
|
+
interface Window {
|
|
7
|
+
JoliboxUtil: typeof JoliboxUtil;
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
class JoliboxUtil {
|
|
12
|
+
static xua = xua;
|
|
13
|
+
static session = session;
|
|
14
|
+
static uuid = uuid;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
window.JoliboxUtil = JoliboxUtil;
|
|
18
|
+
|
|
19
|
+
export { JoliboxUtil };
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { uuidv4 } from '../../../common/http/uuid';
|
|
2
|
+
|
|
3
|
+
export const getGameSessionId = () => {
|
|
4
|
+
let sessionId = sessionStorage.getItem('gameSessionId');
|
|
5
|
+
if (!sessionId) {
|
|
6
|
+
sessionId = uuidv4();
|
|
7
|
+
sessionStorage.setItem('gameSessionId', sessionId);
|
|
8
|
+
}
|
|
9
|
+
return sessionId;
|
|
10
|
+
};
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* h5 错误上报实现
|
|
3
|
+
*/
|
|
4
|
+
import '@/common/report/errors/report/listeners';
|
|
5
|
+
import { BaseError, logger } from '@jolibox/common';
|
|
6
|
+
import { errorReportEmitter } from '@/common/report/errors';
|
|
7
|
+
import { ErrorData } from '@/common/report/errors/report';
|
|
8
|
+
import { IEvent, EventType, EProject } from '@/common/report';
|
|
9
|
+
import { tracker } from '../event-tracker';
|
|
10
|
+
import { context } from '@/common/context';
|
|
11
|
+
|
|
12
|
+
const doReport = (eventName: string, error: Error | BaseError) => {
|
|
13
|
+
const extra = {
|
|
14
|
+
message: error.message,
|
|
15
|
+
stack: error.stack ?? '',
|
|
16
|
+
errorType: error.name,
|
|
17
|
+
source: 0
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
const eventBody: IEvent = {
|
|
21
|
+
name: eventName,
|
|
22
|
+
type: EventType.ErrorTrace,
|
|
23
|
+
location: null,
|
|
24
|
+
target: null,
|
|
25
|
+
extra: extra,
|
|
26
|
+
timestamp: Date.now(),
|
|
27
|
+
userId: context.hostUserInfo?.uid ?? null
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
tracker.trackEvent(eventBody, EProject.WebSDK);
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
errorReportEmitter.on('GLOBAL_ERROR', (error, errorData: ErrorData) => {
|
|
34
|
+
doReport(errorData.tag, error);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
errorReportEmitter.on('GLOBAL_USER_ERROR', (error, errorData: ErrorData) => {
|
|
38
|
+
// TODO: User Error
|
|
39
|
+
logger.log('UserError', error, errorData);
|
|
40
|
+
});
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { context } from '@/common/context';
|
|
2
|
+
import { logger } from '@jolibox/common';
|
|
3
|
+
import { EProject, EventTracker, EventType } from '@/common/report';
|
|
4
|
+
import JoliboxHttpClient from '@/h5/http';
|
|
5
|
+
|
|
6
|
+
export class H5EventTracker extends EventTracker {
|
|
7
|
+
private hostToApiMap: Record<string | 'default', { test: string; prod: string }> = {
|
|
8
|
+
default: {
|
|
9
|
+
test: 'https://stg-collect.jolibox.com',
|
|
10
|
+
prod: 'https://collect.jolibox.com'
|
|
11
|
+
},
|
|
12
|
+
'oss.jolibox.com': {
|
|
13
|
+
test: 'https://stg-collect.jolibox.com',
|
|
14
|
+
prod: 'https://collect.jolibox.com'
|
|
15
|
+
},
|
|
16
|
+
'oss.pico-game.com': {
|
|
17
|
+
test: 'https://stg-collect.pico-game.com',
|
|
18
|
+
prod: 'https://collect.pico-game.com'
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
private get apiBaseURL() {
|
|
23
|
+
const api = this.hostToApiMap[window.location.host] ?? this.hostToApiMap.default;
|
|
24
|
+
return context.testMode ?? false ? api.test : api.prod;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
private httpClient = new JoliboxHttpClient({
|
|
28
|
+
baseUrl: this.apiBaseURL
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
doReport(event: unknown[]): void {
|
|
32
|
+
// this.httpClient.post("/report", {
|
|
33
|
+
// data: event,
|
|
34
|
+
// timeout: 5000,
|
|
35
|
+
// });
|
|
36
|
+
logger.info('Track', event);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export const tracker = new H5EventTracker();
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import './errors';
|
|
2
|
+
import { createCommands, EventEmitter, isNumber, isObject, isString } from '@jolibox/common';
|
|
3
|
+
import { CommonReportConfig, createTracks, EProject, EventType, IEvent } from '../../common/report';
|
|
4
|
+
import { tracker } from './event-tracker';
|
|
5
|
+
import { H5TaskTracker } from './task-tracker';
|
|
6
|
+
import { context } from '@/common/context';
|
|
7
|
+
export * from '../../common/report/types';
|
|
8
|
+
|
|
9
|
+
const commands = createCommands();
|
|
10
|
+
|
|
11
|
+
const commonParams: CommonReportConfig = {
|
|
12
|
+
type: EProject.WebSDK,
|
|
13
|
+
platform: 'h5',
|
|
14
|
+
jssdk_version: context.sdkInfo.jssdkVersion,
|
|
15
|
+
source_id: context.game_id,
|
|
16
|
+
source_version: context.game_version
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
const { track, trackPerformance } = createTracks((...args) => {
|
|
20
|
+
const [, data] = args;
|
|
21
|
+
|
|
22
|
+
const _data = data as Record<string, unknown>;
|
|
23
|
+
|
|
24
|
+
const originExtra = isObject(_data.extra)
|
|
25
|
+
? _data.extra
|
|
26
|
+
: isString(_data.extra)
|
|
27
|
+
? JSON.parse(_data.extra)
|
|
28
|
+
: {};
|
|
29
|
+
const extra = {
|
|
30
|
+
...originExtra,
|
|
31
|
+
source_id: (_data.source_id as string) ?? '',
|
|
32
|
+
source_version: (_data.source_version as string) ?? ''
|
|
33
|
+
};
|
|
34
|
+
const eventBody: IEvent = {
|
|
35
|
+
name: data.tag,
|
|
36
|
+
type: EventType.System,
|
|
37
|
+
location: null,
|
|
38
|
+
target: null,
|
|
39
|
+
extra: extra,
|
|
40
|
+
timestamp: Date.now(),
|
|
41
|
+
userId: context.hostUserInfo?.uid ?? null
|
|
42
|
+
};
|
|
43
|
+
tracker.trackEvent(eventBody, EProject.WebSDK);
|
|
44
|
+
}, commonParams);
|
|
45
|
+
export { track, trackPerformance };
|
|
46
|
+
|
|
47
|
+
commands.registerCommand('ReportSDK.traceSystemTimeline', ({ event, duration }) => {
|
|
48
|
+
trackPerformance(event, duration);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
commands.registerCommand('ReportSDK.traceSystem', ({ event, info }) => {
|
|
52
|
+
track(event, info);
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
export const h5TaskEmitter = new EventEmitter<{ visible: [boolean] }>();
|
|
56
|
+
export const taskTracker = new H5TaskTracker(h5TaskEmitter);
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* H5任务上报
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { context } from '@/common/context';
|
|
6
|
+
import { TaskTracker, TaskPoint } from '@/common/report/task-track';
|
|
7
|
+
import { EventEmitter, logger } from '@jolibox/common';
|
|
8
|
+
import JoliboxHttpClient from '../http';
|
|
9
|
+
|
|
10
|
+
export class H5TaskTracker extends TaskTracker {
|
|
11
|
+
private gameId: string;
|
|
12
|
+
private sessionId: string;
|
|
13
|
+
private httpClient = new JoliboxHttpClient();
|
|
14
|
+
|
|
15
|
+
constructor(eventEmitter: EventEmitter<{ visible: [boolean] }>, interval?: number) {
|
|
16
|
+
super(eventEmitter, interval);
|
|
17
|
+
this.gameId = context.game_id;
|
|
18
|
+
this.sessionId = context.session_id;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
async reporter(point: TaskPoint): Promise<void> {
|
|
22
|
+
const { event, params } = point;
|
|
23
|
+
// await this.httpClient.post(`/api/base/app-event`, {
|
|
24
|
+
// data: {
|
|
25
|
+
// eventType: event,
|
|
26
|
+
// gameInfo: {
|
|
27
|
+
// gameId: this.gameId,
|
|
28
|
+
// sessionId: this.sessionId,
|
|
29
|
+
// ...params
|
|
30
|
+
// }
|
|
31
|
+
// }
|
|
32
|
+
// });
|
|
33
|
+
logger.log('-----task tracker-----', event, params);
|
|
34
|
+
}
|
|
35
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { registerCanIUseFactory } from '@/common/can-i-use';
|
|
2
|
+
import { track } from '../report';
|
|
3
|
+
import { createAPIFactory } from '@/common/api-factory';
|
|
4
|
+
export const { createAPI, createSyncAPI } = createAPIFactory(track);
|
|
5
|
+
|
|
6
|
+
export const registerCanIUse = registerCanIUseFactory('native');
|
|
7
|
+
import { t } from '@/common/api-factory/validator';
|
|
8
|
+
export { t };
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { createCommands } from '@jolibox/common';
|
|
2
|
+
import { createSyncAPI, registerCanIUse } from './base';
|
|
3
|
+
import { invokeNative } from '../bootstrap/bridge';
|
|
4
|
+
|
|
5
|
+
const commands = createCommands();
|
|
6
|
+
|
|
7
|
+
const API_ENV = 'env';
|
|
8
|
+
const API_GET_SYSTEM_SYNC = 'getSystemInfoSync';
|
|
9
|
+
|
|
10
|
+
const getSystemInfoSync = createSyncAPI(API_GET_SYSTEM_SYNC, {
|
|
11
|
+
implement: () => {
|
|
12
|
+
const res = invokeNative('envSync');
|
|
13
|
+
const { data } = res;
|
|
14
|
+
return {
|
|
15
|
+
system: data.deviceInfo.system,
|
|
16
|
+
platform: data.platform,
|
|
17
|
+
version: data.sdkInfo.jssdkVersion,
|
|
18
|
+
pixelRatio: data.deviceInfo.pixelRatio,
|
|
19
|
+
language: data.deviceInfo.lang
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
const getEnv = createSyncAPI(API_ENV, {
|
|
25
|
+
implement: () => {
|
|
26
|
+
const res = invokeNative('envSync');
|
|
27
|
+
|
|
28
|
+
return res?.data ?? undefined;
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
commands.registerCommand('API.getSystemInfoSync', getSystemInfoSync);
|
|
33
|
+
commands.registerCommand('API.env', getEnv);
|
|
34
|
+
|
|
35
|
+
registerCanIUse('env', {
|
|
36
|
+
version: '1.0.0'
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
registerCanIUse('getSystemInfoSync', {
|
|
40
|
+
version: '1.0.0'
|
|
41
|
+
});
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { createCommands } from '@jolibox/common';
|
|
2
|
+
import { applyNative } from '../bootstrap/bridge';
|
|
3
|
+
import { createAPI, registerCanIUse, t } from './base';
|
|
4
|
+
|
|
5
|
+
export const showKeyboard = createAPI('showKeyboard', {
|
|
6
|
+
paramsSchema: t.tuple(
|
|
7
|
+
t.object({
|
|
8
|
+
defaultValue: t.string().default(''),
|
|
9
|
+
multiple: t.boolean().default(false),
|
|
10
|
+
maxLength: t.unknown().optional().default(100000)
|
|
11
|
+
})
|
|
12
|
+
),
|
|
13
|
+
async implement(params) {
|
|
14
|
+
const maxLength = Math.floor(Number(params.maxLength) ?? 100000);
|
|
15
|
+
let { defaultValue } = params;
|
|
16
|
+
|
|
17
|
+
if (defaultValue && maxLength) {
|
|
18
|
+
defaultValue = defaultValue.slice(0, maxLength);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
applyNative('showKeyboardSync', { ...params, defaultValue, maxLength });
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
registerCanIUse('keyboard.showKeyboard', {
|
|
26
|
+
version: '1.0.0',
|
|
27
|
+
properties: {
|
|
28
|
+
params: {
|
|
29
|
+
defaultValue: '1.0.0',
|
|
30
|
+
multiple: '1.0.0',
|
|
31
|
+
confirmHold: '1.0.0',
|
|
32
|
+
confirmType: '1.0.0',
|
|
33
|
+
maxLength: '1.0.0'
|
|
34
|
+
}
|
|
35
|
+
},
|
|
36
|
+
success: {
|
|
37
|
+
errMsg: '1.0.0'
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
export const updateKeyboard = createAPI('updateKeyboard', {
|
|
42
|
+
paramsSchema: t.tuple(
|
|
43
|
+
t.object({
|
|
44
|
+
value: t.string()
|
|
45
|
+
})
|
|
46
|
+
),
|
|
47
|
+
async implement(params) {
|
|
48
|
+
applyNative('updateKeyboardSync', params);
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
registerCanIUse('keyboard.updateKeyboard', {
|
|
53
|
+
version: '1.0.0',
|
|
54
|
+
properties: {
|
|
55
|
+
params: {
|
|
56
|
+
value: '1.0.0'
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
export const hideKeyboard = createAPI('hideKeyboard', {
|
|
62
|
+
async implement() {
|
|
63
|
+
applyNative('hideKeyboardSync');
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
registerCanIUse('keyboard.hideKeyboard', {
|
|
68
|
+
version: '1.0.0'
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
const commands = createCommands();
|
|
72
|
+
|
|
73
|
+
commands.registerCommand('KeyboardSDK.showKeyboard', showKeyboard);
|
|
74
|
+
commands.registerCommand('KeyboardSDK.updateKeyboard', updateKeyboard);
|
|
75
|
+
commands.registerCommand('KeyboardSDK.hideKeyboard', hideKeyboard);
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { BaseError, createCommands, wrapUserFunction, hostEmitter } from '@jolibox/common';
|
|
2
|
+
import { createAPI, createSyncAPI, registerCanIUse, t } from './base';
|
|
3
|
+
import { reportError } from '@/common/report/errors/report';
|
|
4
|
+
import { applyNative, onNative } from '../bootstrap/bridge';
|
|
5
|
+
import { nativeTaskEmitter } from '../report';
|
|
6
|
+
|
|
7
|
+
const EXIT_GAME = 'exitGame';
|
|
8
|
+
const ON_READY = 'onReady';
|
|
9
|
+
const ON_GAME_SHOW = 'onGameShow';
|
|
10
|
+
const ON_GAME_HIDE = 'onGameHide';
|
|
11
|
+
|
|
12
|
+
const commands = createCommands();
|
|
13
|
+
|
|
14
|
+
const safeCallbackWrapper = wrapUserFunction(reportError as (err: Error | BaseError) => void);
|
|
15
|
+
const exitGame = createAPI(EXIT_GAME, {
|
|
16
|
+
paramsSchema: t.tuple(t.function()),
|
|
17
|
+
implement: async (onBeforeExit: () => void) => {
|
|
18
|
+
const safeCallback = safeCallbackWrapper(onBeforeExit);
|
|
19
|
+
// 集中上报
|
|
20
|
+
safeCallback.call(this);
|
|
21
|
+
await applyNative('exitAppAsync');
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
const onGameShow = createSyncAPI(ON_GAME_SHOW, {
|
|
26
|
+
paramsSchema: t.tuple(t.function()),
|
|
27
|
+
implement(callback) {
|
|
28
|
+
const safeCallback = safeCallbackWrapper(callback);
|
|
29
|
+
onNative('onJoliboxEnterForeground', () => {
|
|
30
|
+
safeCallback.call(this);
|
|
31
|
+
nativeTaskEmitter.emit('visible', true);
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
const onGameHide = createSyncAPI(ON_GAME_HIDE, {
|
|
37
|
+
paramsSchema: t.tuple(t.function()),
|
|
38
|
+
implement(callback) {
|
|
39
|
+
const safeCallback = safeCallbackWrapper(callback);
|
|
40
|
+
onNative('onJoliboxEnterBackground', () => {
|
|
41
|
+
safeCallback.call(this);
|
|
42
|
+
nativeTaskEmitter.emit('visible', false);
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
const onReadyHandler = createSyncAPI(ON_READY, {
|
|
48
|
+
paramsSchema: t.tuple(t.function()),
|
|
49
|
+
implement(callback) {
|
|
50
|
+
const safeCallback = safeCallbackWrapper(callback);
|
|
51
|
+
hostEmitter.on('LifecycleEvent.onReady', (info) => {
|
|
52
|
+
safeCallback(info);
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
commands.registerCommand('LifecycleSDK.exit', exitGame);
|
|
58
|
+
commands.registerCommand('LifecycleSDK.onReady', onReadyHandler);
|
|
59
|
+
commands.registerCommand('LifecycleSDK.onJoliboxShow', onGameShow);
|
|
60
|
+
commands.registerCommand('LifecycleSDK.onJoliboxHide', onGameHide);
|
|
61
|
+
|
|
62
|
+
registerCanIUse('lifeCycle.exit', {
|
|
63
|
+
version: '1.0.0' // mock
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
registerCanIUse('lifeCycle.onReady', {
|
|
67
|
+
version: '1.0.0' // mock
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
registerCanIUse('lifeCycle.onGameShow', {
|
|
71
|
+
version: '1.0.0' // mock
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
registerCanIUse('lifeCycle.onGameHide', {
|
|
75
|
+
version: '1.0.0' // mock
|
|
76
|
+
});
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { context } from '@/common/context';
|
|
2
|
+
import { applyNative, invokeNative, onNative } from '../bootstrap/bridge';
|
|
3
|
+
import { createAPI, t, registerCanIUse } from './base';
|
|
4
|
+
import { APIError, createCommands, Deferred, hostEmitter } from '@jolibox/common';
|
|
5
|
+
import { createAPIError } from '@/common/report/errors';
|
|
6
|
+
|
|
7
|
+
registerCanIUse('login', {
|
|
8
|
+
version: '1.0.0',
|
|
9
|
+
properties: {
|
|
10
|
+
force: '1.0.0'
|
|
11
|
+
},
|
|
12
|
+
success: {
|
|
13
|
+
errMsg: '1.0.0',
|
|
14
|
+
code: '1.0.0',
|
|
15
|
+
token: '1.0.0',
|
|
16
|
+
isLogin: '1.0.0'
|
|
17
|
+
}
|
|
18
|
+
});
|
|
19
|
+
registerCanIUse('checkSession', {
|
|
20
|
+
version: '1.0.0',
|
|
21
|
+
success: {
|
|
22
|
+
errMsg: '1.0.0'
|
|
23
|
+
}
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
const login = createAPI('login', {
|
|
27
|
+
async implement() {
|
|
28
|
+
const isLogin = await applyNative('checkLoginAsync');
|
|
29
|
+
if (isLogin) {
|
|
30
|
+
return { isLogin: true, token: context.hostUserInfo?.token };
|
|
31
|
+
}
|
|
32
|
+
onNative('onLoginStateChange', ({ isLogin, token, uuid }) => {
|
|
33
|
+
if (uuid == loginUUID) {
|
|
34
|
+
resolve({ isLogin, token });
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
const res = invokeNative('loginSync');
|
|
38
|
+
|
|
39
|
+
const {
|
|
40
|
+
data: { uuid: loginUUID }
|
|
41
|
+
} = res;
|
|
42
|
+
if (!loginUUID) {
|
|
43
|
+
throw createAPIError({
|
|
44
|
+
code: -1,
|
|
45
|
+
msg: 'login failed'
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
const { promise, resolve } = new Deferred<{ isLogin: boolean; token?: string }>();
|
|
49
|
+
|
|
50
|
+
const loginRes = await promise;
|
|
51
|
+
context.onEnvConfigChanged({ hostUserInfo: loginRes });
|
|
52
|
+
hostEmitter.emit('onLoginComplete', loginRes);
|
|
53
|
+
|
|
54
|
+
return loginRes;
|
|
55
|
+
}
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
const checkSession = createAPI('checkSession', {
|
|
59
|
+
async implement() {
|
|
60
|
+
const {
|
|
61
|
+
data: { isLogin }
|
|
62
|
+
} = await applyNative('checkLoginAsync');
|
|
63
|
+
return {
|
|
64
|
+
isLogin
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
const commands = createCommands();
|
|
70
|
+
commands.registerCommand('API.login', login);
|
|
71
|
+
commands.registerCommand('API.checkSession', checkSession);
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
import { createAPIError } from '@/common/report/errors';
|
|
2
|
+
import { createFetch } from '../network/create-fetch';
|
|
3
|
+
import { createAPI, registerCanIUse, t } from './base';
|
|
4
|
+
import { dirtyURL, normalizeHeader, normalizeMethod, normalizeTimeout } from '../network/utils';
|
|
5
|
+
import { BaseError, createCommands, Deferred, wrapUserFunction } from '@jolibox/common';
|
|
6
|
+
import { AnyFunction, ResponseType } from '@jolibox/types';
|
|
7
|
+
import { reportError } from '@/common/report/errors/report';
|
|
8
|
+
import { FetchOptions } from '../network/types';
|
|
9
|
+
|
|
10
|
+
const LIMIT = 15;
|
|
11
|
+
|
|
12
|
+
const publicFetch = createFetch('createRequestTaskSync', 'operateRequestTaskSync', {
|
|
13
|
+
type: 'public'
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
/** current active count */
|
|
17
|
+
let activeCount = 0;
|
|
18
|
+
|
|
19
|
+
const deferQueue: AnyFunction[] = [];
|
|
20
|
+
|
|
21
|
+
const safeCallbackWrapper = wrapUserFunction(reportError as (err: Error | BaseError) => void);
|
|
22
|
+
|
|
23
|
+
const remove = <T>(array: T[], predicate: (item: T) => boolean) => {
|
|
24
|
+
const removedElements = array.filter(predicate);
|
|
25
|
+
|
|
26
|
+
const remainingElements = array.filter((item) => !predicate(item));
|
|
27
|
+
array.length = 0;
|
|
28
|
+
array.push(...remainingElements);
|
|
29
|
+
|
|
30
|
+
return removedElements;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
const request = (_params: unknown) => {
|
|
34
|
+
const deferred = new Deferred<{
|
|
35
|
+
code: ResponseType;
|
|
36
|
+
data?: string | JSON | ArrayBuffer | undefined;
|
|
37
|
+
message: string;
|
|
38
|
+
}>();
|
|
39
|
+
|
|
40
|
+
let teardown = () => {
|
|
41
|
+
// noop
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
const _request = createAPI('request', {
|
|
45
|
+
paramsSchema: t.tuple(
|
|
46
|
+
t.object({
|
|
47
|
+
url: t.string(),
|
|
48
|
+
method: t.string(),
|
|
49
|
+
header: t.object(),
|
|
50
|
+
data: t.object().optional(),
|
|
51
|
+
query: t.object().optional(),
|
|
52
|
+
dataType: t.string().default('json'),
|
|
53
|
+
responseType: t.string().default('text'),
|
|
54
|
+
enableCache: t.boolean().default(false),
|
|
55
|
+
appendHostCookie: t.boolean().default(true),
|
|
56
|
+
timeout: t.number().default(30000),
|
|
57
|
+
success: t.function(),
|
|
58
|
+
fail: t.function()
|
|
59
|
+
})
|
|
60
|
+
),
|
|
61
|
+
implement(params) {
|
|
62
|
+
const action = async () => {
|
|
63
|
+
const failCallback = safeCallbackWrapper(params.fail);
|
|
64
|
+
try {
|
|
65
|
+
activeCount += 1;
|
|
66
|
+
|
|
67
|
+
// 在 action 执行前, 如果用户就调用了 abort 的话就会命中该逻辑
|
|
68
|
+
if (deferred.state !== 'pending') {
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
const { data, dataType, responseType, enableCache, appendHostCookie } = params;
|
|
72
|
+
|
|
73
|
+
const header = normalizeHeader(params.header);
|
|
74
|
+
const method = normalizeMethod(params.method);
|
|
75
|
+
const timeout = normalizeTimeout(params.timeout);
|
|
76
|
+
const url = dirtyURL(params.url, method, data);
|
|
77
|
+
const successCallback = safeCallbackWrapper(params.success);
|
|
78
|
+
|
|
79
|
+
const _params = Object.assign({}, params, {
|
|
80
|
+
method,
|
|
81
|
+
header,
|
|
82
|
+
data: method === 'GET' || method === 'HEAD' ? undefined : data, // GET HEAD 请求不需要 data 参数
|
|
83
|
+
enableCache,
|
|
84
|
+
query: params.query ?? ({} as Record<string, string>),
|
|
85
|
+
dataType,
|
|
86
|
+
responseType,
|
|
87
|
+
appendHostCookie
|
|
88
|
+
}) as FetchOptions;
|
|
89
|
+
|
|
90
|
+
if (timeout) _params['timeout'] = timeout;
|
|
91
|
+
|
|
92
|
+
const task = publicFetch<string | ArrayBuffer | JSON>(url, _params);
|
|
93
|
+
|
|
94
|
+
{
|
|
95
|
+
const { response } = await task;
|
|
96
|
+
const { code, data, message } = response;
|
|
97
|
+
|
|
98
|
+
deferred.resolve({
|
|
99
|
+
code: (code ?? 'SUCCESS') as ResponseType,
|
|
100
|
+
data,
|
|
101
|
+
message: message ?? 'request:ok'
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
successCallback({
|
|
105
|
+
code: 'SUCCESS',
|
|
106
|
+
message: 'request:ok',
|
|
107
|
+
data
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
} catch (err) {
|
|
111
|
+
deferred.reject(err as Error);
|
|
112
|
+
failCallback({
|
|
113
|
+
code: 'FAILURE',
|
|
114
|
+
message: 'httpRequst: failed'
|
|
115
|
+
});
|
|
116
|
+
} finally {
|
|
117
|
+
activeCount -= 1;
|
|
118
|
+
deferQueue.shift()?.();
|
|
119
|
+
}
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
if (activeCount >= LIMIT) {
|
|
123
|
+
teardown = () => {
|
|
124
|
+
remove(deferQueue, (i) => i === action);
|
|
125
|
+
};
|
|
126
|
+
deferQueue.push(action);
|
|
127
|
+
} else {
|
|
128
|
+
void action();
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
return deferred.promise;
|
|
132
|
+
}
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
_request(_params);
|
|
136
|
+
|
|
137
|
+
return {
|
|
138
|
+
/**
|
|
139
|
+
* 1. requesting...
|
|
140
|
+
* 2. not in deferred
|
|
141
|
+
*/
|
|
142
|
+
abort() {
|
|
143
|
+
deferred.reject(createAPIError({ code: -1, msg: 'request:fail abort' }));
|
|
144
|
+
teardown();
|
|
145
|
+
}
|
|
146
|
+
};
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
const commands = createCommands();
|
|
150
|
+
|
|
151
|
+
commands.registerCommand('HttpSDK.request', request);
|
|
152
|
+
registerCanIUse('request', {
|
|
153
|
+
version: '1.0.0'
|
|
154
|
+
});
|