upd | new rpc tests
This commit is contained in:
+27
-48
@@ -1,4 +1,5 @@
|
||||
import { EVENT_RESPONSE } from './events'
|
||||
import type { RPCData } from './types'
|
||||
import { RPC_LISTENER } from './events'
|
||||
|
||||
export enum Environment {
|
||||
CEF = 'CEF',
|
||||
@@ -7,57 +8,35 @@ export enum Environment {
|
||||
UNKNOWN = 'UNKNOWN',
|
||||
}
|
||||
|
||||
export enum Errors {
|
||||
EVENT_NOT_REGISTERED = 'Event not registered',
|
||||
function generateUUID(): string {
|
||||
let uuid = '',
|
||||
random
|
||||
|
||||
for (let i = 0; i < 32; i++) {
|
||||
random = (Math.random() * 16) | 0
|
||||
|
||||
if (i === 8 || i === 12 || i === 16 || i === 20) {
|
||||
uuid += '-'
|
||||
}
|
||||
|
||||
uuid += (i === 12 ? 4 : i === 16 ? (random & 3) | 8 : random).toString(
|
||||
16,
|
||||
)
|
||||
}
|
||||
|
||||
return uuid
|
||||
}
|
||||
|
||||
export type RPCState = {
|
||||
eventName: string
|
||||
uuid: string
|
||||
knownError?: string
|
||||
data?: any
|
||||
calledFrom: Environment
|
||||
calledTo: Environment
|
||||
}
|
||||
|
||||
class Utils {
|
||||
public getEnvironment(): Environment {
|
||||
export const utils = {
|
||||
environment: Environment,
|
||||
getEnvironment: () => {
|
||||
if (mp.joaat) return Environment.SERVER
|
||||
if (mp.game && mp.game.joaat) return Environment.CLIENT
|
||||
if ('mp' in window) return Environment.CEF
|
||||
return Environment.UNKNOWN
|
||||
}
|
||||
|
||||
public prepareForExecute(data: string): RPCState {
|
||||
return JSON.parse(data)
|
||||
}
|
||||
|
||||
public prepareForTransfer(data: RPCState): string {
|
||||
return JSON.stringify(data)
|
||||
}
|
||||
|
||||
public generateUUID(): string {
|
||||
let uuid = '',
|
||||
random
|
||||
|
||||
for (let i = 0; i < 32; i++) {
|
||||
random = (Math.random() * 16) | 0
|
||||
|
||||
if (i === 8 || i === 12 || i === 16 || i === 20) {
|
||||
uuid += '-'
|
||||
}
|
||||
|
||||
uuid += (
|
||||
i === 12 ? 4 : i === 16 ? (random & 3) | 8 : random
|
||||
).toString(16)
|
||||
}
|
||||
|
||||
return uuid
|
||||
}
|
||||
|
||||
public generateResponseEventName(uuid: string): string {
|
||||
return `${EVENT_RESPONSE}_${uuid}`
|
||||
}
|
||||
},
|
||||
parseData: (data: string): RPCData => JSON.parse(data),
|
||||
stringifyData: (data: RPCData): string => JSON.stringify(data),
|
||||
generateResponseEventName: (uuid: string) => `${RPC_LISTENER}_${uuid}`,
|
||||
generateUUID,
|
||||
}
|
||||
|
||||
export const utils = new Utils()
|
||||
|
||||
Reference in New Issue
Block a user