- rpc d.ts configured correctly
- package.json credentials updated
This commit is contained in:
Danya H 2024-10-01 18:19:46 +01:00
parent fafcdb50a8
commit ee4baca74a
21 changed files with 162 additions and 369 deletions

View File

@ -17,8 +17,13 @@
"@ragempcommunity/types-cef": "^2.1.8", "@ragempcommunity/types-cef": "^2.1.8",
"rage-fw-shared-types": "workspace:^" "rage-fw-shared-types": "workspace:^"
}, },
"description": "RageFW CEF side",
"keywords": [], "keywords": [],
"author": "SashaGoncharov19", "author": "SashaGoncharov19",
"license": "MIT", "contributors": [{
"description": "CEF side for rage-fw" "name": "rilaxik",
"email": "dev.rilaxik@gmail.com",
"url": "https://github.com/rilaxik"
}],
"license": "MIT"
} }

View File

@ -14,10 +14,6 @@
"dist/**/*", "dist/**/*",
"readme.md" "readme.md"
], ],
"description": "CLI to scaffold a template project for RageFW",
"keywords": [],
"author": "rilaxik",
"license": "ISC",
"dependencies": { "dependencies": {
"@inquirer/prompts": "^5.0.5", "@inquirer/prompts": "^5.0.5",
"axios": "^1.7.2", "axios": "^1.7.2",
@ -31,5 +27,12 @@
"@types/yargs": "^17.0.32", "@types/yargs": "^17.0.32",
"prettier": "^3.3.2", "prettier": "^3.3.2",
"typescript": "^5.4.5" "typescript": "^5.4.5"
} },
"description": "CLI to scaffold a template project for RageFW",
"keywords": [],
"author": "rilaxik",
"contributors": [{
"name": "SashaGoncharov19"
}],
"license": "MIT"
} }

View File

@ -17,9 +17,14 @@
"@ragempcommunity/types-client": "^2.1.8", "@ragempcommunity/types-client": "^2.1.8",
"rage-fw-shared-types": "workspace:^" "rage-fw-shared-types": "workspace:^"
}, },
"description": "RageFW Client side",
"keywords": [], "keywords": [],
"author": "SashaGoncharov19", "author": "SashaGoncharov19",
"contributors": [{
"name": "rilaxik",
"email": "dev.rilaxik@gmail.com",
"url": "https://github.com/rilaxik"
}],
"license": "MIT", "license": "MIT",
"description": "Client side of rage-fw",
"gitHead": "053e4fd12aa120d53e11e0d2009c0df78c1a2ad0" "gitHead": "053e4fd12aa120d53e11e0d2009c0df78c1a2ad0"
} }

13
rpc/index.d.ts vendored Normal file
View File

@ -0,0 +1,13 @@
declare const mp: any
// declare const console: any
// declare const setTimeout: (fn: Function, time: number) => number
// declare const clearTimeout: (id: number) => void
declare const global: {
[p: string]: (...args: any[]) => unknown
}
// declare const window: {
// [p: string]: (...args: any[]) => unknown
// }

View File

@ -4,19 +4,25 @@
"main": "dist/index.js", "main": "dist/index.js",
"types": "dist/src/index.d.ts", "types": "dist/src/index.d.ts",
"scripts": { "scripts": {
"watch": "tsc -w",
"build": "tsup", "build": "tsup",
"start": "npx ./dist create" "start": "npx ./dist create"
}, },
"files": [ "files": [
"dist/**/*" "dist/**/*"
], ],
"description": "CLI to scaffold a template project for RageFW",
"keywords": [],
"author": "rilaxik",
"license": "ISC",
"devDependencies": { "devDependencies": {
"prettier": "^3.3.2", "prettier": "^3.3.2"
"typescript": "^5.4.5" },
} "peerDependencies": {
"typescript": "^5.0.0"
},
"description": "RageFW RPC",
"keywords": [],
"author": "SashaGoncharov19",
"contributors": [{
"name": "rilaxik",
"email": "dev.rilaxik@gmail.com",
"url": "https://github.com/rilaxik"
}],
"license": "MIT"
} }

View File

@ -1,5 +0,0 @@
export const EVENT_LISTENER = '__rpc:listener'
export const EVENT_RESPONSE = '__rpc:response'
export const CEF_EVENT_LISTENER = '__rpc:cef_listener'
export const CLIENT_ROUTER_LISTENER = '__rpc:clientRouter'

View File

@ -1,76 +0,0 @@
import { Environment, utils } from './utils'
import { EVENT_LISTENER } from './events'
import { client } from './modules/client'
import { server } from './modules/server'
import { cef } from './modules/cef'
const environment = utils.getEnvironment()
const state = environment === Environment.CEF ? window : global
class rpc {
constructor() {
if (environment === Environment.UNKNOWN) return
mp.events.add(EVENT_LISTENER, async (player: any, request: string) => {
switch (environment) {
case Environment.SERVER:
await server.listenEvent(player, request)
break
case Environment.CLIENT:
request = player
await client.listenEvent(request)
break
case Environment.CEF:
request = player
await cef
}
})
}
public register<Callback extends any[] = unknown[], Return = unknown>(
eventName: string,
cb: (...args: Callback) => Return,
) {
if (environment === Environment.UNKNOWN) return
state[eventName] = cb
}
public async callClient<Args extends any[] = unknown[], Return = unknown>(
player: any,
eventName: string,
...args: Args
): Promise<Return | unknown> {
if (environment === Environment.UNKNOWN) return
if (environment === Environment.SERVER) {
return client.executeClient(player, eventName, args)
}
}
public async callServer<Args extends any[] = unknown[], Return = unknown>(
eventName: string,
...args: Args
): Promise<Return | unknown> {
switch (environment) {
case Environment.UNKNOWN:
return
case Environment.SERVER:
return
case Environment.CEF:
return client
case Environment.CLIENT:
return server.executeServer(eventName, args)
}
}
}
const testRpc = new rpc()
export { testRpc }

View File

@ -1,13 +0,0 @@
declare const mp: any
declare const console: any
declare const setTimeout: (fn: Function, time: number) => number
declare const clearTimeout: (id: number) => void
declare const global: {
[p: string]: (...args: any[]) => unknown
}
declare const window: {
[p: string]: (...args: any[]) => unknown
}

View File

@ -1,63 +0,0 @@
import { EVENT_RESPONSE } from './events'
export enum Environment {
CEF = 'CEF',
CLIENT = 'CLIENT',
SERVER = 'SERVER',
UNKNOWN = 'UNKNOWN',
}
export enum Errors {
EVENT_NOT_REGISTERED = 'Event not registered',
}
export type RPCState = {
eventName: string
uuid: string
knownError?: string
data?: any
calledFrom: Environment
calledTo: Environment
}
class Utils {
public getEnvironment(): Environment {
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}`
}
}
export const utils = new Utils()

View File

@ -1,44 +0,0 @@
import { utils } from './utils'
import type { RPCData } from './types'
export class Client {
public async listen(data: string) {
const parsedData = utils.parseData(data)
await this.transferTo(parsedData)
}
private transferTo(data: RPCData) {
switch (data.to) {
case utils.environment.CLIENT:
return this.executeLocal(data)
case utils.environment.CEF:
// todo transfer to cef
case utils.environment.SERVER:
// todo transfer to server
}
}
private async executeLocal(data: RPCData) {
const state =
utils.getEnvironment() === utils.environment.CEF
? window.rpcEvents
: global.rpcEvents
const fnResponse = await state[data.eventName](...data.data)
const response = {
data: fnResponse,
...data,
}
this.sendResponseToServer(response)
}
private sendResponseToServer(data: RPCData) {
const eventName = utils.generateResponseEventName(data.uuid)
const prepareForTransfer = utils.stringifyData(data)
mp.events.callRemote(eventName, prepareForTransfer)
}
}
export const client = new Client()

View File

@ -1,3 +1,5 @@
export const RPC_LISTENER = 'rpc::listener' export const EVENT_LISTENER = '__rpc:listener'
export const EVENT_RESPONSE = '__rpc:response'
export const CEF_EVENT_LISTENER = '__rpc:cef_listener'
export const CLIENT_ROUTER_LISTENER = 'rpc::clientRouterListener' export const CLIENT_ROUTER_LISTENER = '__rpc:clientRouter'

View File

@ -1,104 +1,78 @@
import { RPC_LISTENER } from './events'
import { Environment, utils } from './utils' import { Environment, utils } from './utils'
import { EVENT_LISTENER } from './events'
import { client } from './client' import { client } from './modules/client'
import { server } from './server' import { server } from './modules/server'
import type { RPCData } from './types.ts' import { cef } from './modules/cef'
class FrameworkRpc { class Rpc {
private readonly _environment: Environment private _environment = utils.getEnvironment()
private readonly _environmentGlobal: Record<string, Function> private _state =
utils.getEnvironment() === Environment.CEF
constructor() {
this._environment = utils.getEnvironment()
this._environmentGlobal =
utils.getEnvironment() === utils.environment.CEF
? window.rpcEvents ? window.rpcEvents
: global.rpcEvents : global.rpcEvents
mp.events.add(RPC_LISTENER, async (player: any, data: string) => { constructor() {
if (this._environment === Environment.UNKNOWN) return
mp.events.add(EVENT_LISTENER, async (player: any, request: string) => {
switch (this._environment) { switch (this._environment) {
case utils.environment.UNKNOWN: case Environment.SERVER:
return await server.listenEvent(player, request)
break
case utils.environment.CLIENT: case Environment.CLIENT:
player = data request = player
return client.listen(player)
case utils.environment.SERVER: // await client.listenEvent(request)
return server.listen(player, data) break
case utils.environment.CEF: case Environment.CEF:
request = player
//
// await cef
} }
}) })
} }
public register<Args extends any[] = unknown[], Return = unknown>( public register<Callback extends any[] = unknown[], Return = unknown>(
eventName: string, eventName: string,
cb: (...args: Args) => Return, cb: (...args: Callback) => Return,
) { ) {
if (this._environment === utils.environment.UNKNOWN) return if (this._environment === Environment.UNKNOWN) return
this._environmentGlobal[eventName] = cb this._state[eventName] = cb
} }
public callClient<Args extends any[] = unknown[], Return = unknown>( public async callClient<Args extends any[] = unknown[], Return = unknown>(
player: any, player: any,
eventName: string, eventName: string,
...args: Args ...args: Args
): Promise<Return | unknown> { ): Promise<Return | unknown> {
return new Promise((resolve, _reject) => { if (this._environment === Environment.UNKNOWN) return
const uuid = utils.generateUUID() if (this._environment === Environment.SERVER) {
// return client.executeClient(player, eventName, args)
const data: RPCData = { }
uuid,
eventName,
from: this._environment,
to: utils.environment.CLIENT,
data: args,
} }
player.call(RPC_LISTENER, [utils.stringifyData(data)]) public async callServer<Args extends any[] = unknown[], Return = unknown>(
const responseEventName = utils.generateResponseEventName(uuid)
const handler = (_player: any, data: string) => {
resolve(utils.parseData(data).data)
mp.events.remove(responseEventName)
}
mp.events.add(responseEventName, handler)
})
}
public callServer<Args extends any[] = unknown[], Return = unknown>(
eventName: string, eventName: string,
...args: Args ...args: Args
): Promise<Return | unknown> { ): Promise<Return | unknown> {
return new Promise((resolve, _reject) => { switch (this._environment) {
const uuid = utils.generateUUID() case Environment.UNKNOWN:
return
const data: RPCData = { case Environment.SERVER:
uuid, return
eventName,
from: this._environment, case Environment.CEF:
to: utils.environment.CLIENT, return client
data: args,
case Environment.CLIENT:
return server.executeServer(eventName, args)
} }
mp.events.callRemote(RPC_LISTENER, utils.stringifyData(data))
const responseEventName = utils.generateResponseEventName(uuid)
const handler = (_player: any, data: string) => {
resolve(utils.parseData(data).data)
mp.events.remove(responseEventName)
}
mp.events.add(responseEventName, handler)
})
} }
} }
const rpc = new FrameworkRpc() const testRpc = new Rpc()
export { testRpc }
export { rpc }

View File

@ -27,7 +27,7 @@ class Cef extends Wrapper {
public async callServer<Args extends any[] = unknown[], Return = unknown>( public async callServer<Args extends any[] = unknown[], Return = unknown>(
eventName: string, eventName: string,
...args: Args ...args: Args
): Promise<Return | unknown> {} ): Promise<Return | void> {}
private async handleReturn( private async handleReturn(
uuid: string, uuid: string,

View File

@ -1,6 +1,6 @@
import { CLIENT_ROUTER_LISTENER, EVENT_LISTENER } from '../events' import { CLIENT_ROUTER_LISTENER } from '../events'
import { Wrapper } from './wrapper' import { Wrapper } from './wrapper'
import { Environment, RPCState } from '../utils' import { Environment } from '../utils'
class Client extends Wrapper { class Client extends Wrapper {
constructor() { constructor() {

View File

@ -1,11 +1,14 @@
import { Environment, Errors, RPCState, utils } from '../utils' import { Environment, Errors, RPCState, utils } from '../utils'
export class Wrapper { export class Wrapper {
public _utils = utils protected _utils = utils
public _environment = utils.getEnvironment() protected _environment = utils.getEnvironment()
public _state = this._environment === Environment.CEF ? window : global protected _state =
this._environment === Environment.CEF
? window.rpcEvents
: global.rpcEvents
public _verifyEvent(data: string): RPCState { protected _verifyEvent(data: string): RPCState {
const rpcData = utils.prepareForExecute(data) const rpcData = utils.prepareForExecute(data)
if (!this._state[rpcData.eventName]) { if (!this._state[rpcData.eventName]) {
@ -15,7 +18,7 @@ export class Wrapper {
return rpcData return rpcData
} }
public _triggerError(rpcData: RPCState, error?: any) { protected _triggerError(rpcData: RPCState, error?: any) {
const errorMessage = [ const errorMessage = [
`${rpcData.knownError}`, `${rpcData.knownError}`,
`Caller: ${rpcData.calledFrom}`, `Caller: ${rpcData.calledFrom}`,

View File

@ -1,33 +0,0 @@
import { utils } from './utils'
import type { RPCData } from './types'
export class Server {
public async listen(player: any, data: string) {
const parsedData = utils.parseData(data)
await this.executeLocal(player, parsedData)
}
private async executeLocal(player: any, data: RPCData) {
const state =
utils.getEnvironment() === utils.environment.CEF
? window.rpcEvents
: global.rpcEvents
const fnResponse = await state[data.eventName](...data.data)
const response = {
data: fnResponse,
...data,
}
this.sendResponseToClient(player, response)
}
private sendResponseToClient(player: any, data: RPCData) {
const eventName = utils.generateResponseEventName(data.uuid)
const prepareForTransfer = utils.stringifyData(data)
player.call(eventName, prepareForTransfer)
}
}
export const server = new Server()

View File

@ -1,9 +0,0 @@
import { Environment } from './utils'
export type RPCData = {
data?: any
from: Environment
to: Environment
eventName: string
uuid: string
}

View File

@ -1,5 +1,4 @@
import type { RPCData } from './types' import { EVENT_RESPONSE } from './events'
import { RPC_LISTENER } from './events'
export enum Environment { export enum Environment {
CEF = 'CEF', CEF = 'CEF',
@ -8,7 +7,36 @@ export enum Environment {
UNKNOWN = 'UNKNOWN', UNKNOWN = 'UNKNOWN',
} }
function generateUUID(): string { export enum Errors {
EVENT_NOT_REGISTERED = 'Event not registered',
}
export type RPCState = {
eventName: string
uuid: string
knownError?: string
data?: any
calledFrom: Environment
calledTo: Environment
}
class Utils {
public getEnvironment(): Environment {
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 = '', let uuid = '',
random random
@ -19,24 +47,17 @@ function generateUUID(): string {
uuid += '-' uuid += '-'
} }
uuid += (i === 12 ? 4 : i === 16 ? (random & 3) | 8 : random).toString( uuid += (
16, i === 12 ? 4 : i === 16 ? (random & 3) | 8 : random
) ).toString(16)
} }
return uuid return uuid
}
public generateResponseEventName(uuid: string): string {
return `${EVENT_RESPONSE}_${uuid}`
}
} }
export const utils = { export const utils = new 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
},
parseData: (data: string): RPCData => JSON.parse(data),
stringifyData: (data: RPCData): string => JSON.stringify(data),
generateResponseEventName: (uuid: string) => `${RPC_LISTENER}_${uuid}`,
generateUUID,
}

View File

@ -4,7 +4,6 @@ export default defineConfig({
entry: ['src/index.ts'], entry: ['src/index.ts'],
outDir: './dist', outDir: './dist',
format: ['cjs'], format: ['cjs'],
noExternal: ['rage-rpc'],
experimentalDts: true, experimentalDts: true,
splitting: false, splitting: false,
sourcemap: false, sourcemap: false,

View File

@ -17,9 +17,14 @@
"@ragempcommunity/types-server": "^2.1.8", "@ragempcommunity/types-server": "^2.1.8",
"rage-fw-shared-types": "workspace:^" "rage-fw-shared-types": "workspace:^"
}, },
"description": "RageFW Server side",
"keywords": [], "keywords": [],
"author": "SashaGoncharov19", "author": "SashaGoncharov19",
"contributors": [{
"name": "rilaxik",
"email": "dev.rilaxik@gmail.com",
"url": "https://github.com/rilaxik"
}],
"license": "MIT", "license": "MIT",
"description": "Server side for rage-fw",
"gitHead": "053e4fd12aa120d53e11e0d2009c0df78c1a2ad0" "gitHead": "053e4fd12aa120d53e11e0d2009c0df78c1a2ad0"
} }