- 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",
"rage-fw-shared-types": "workspace:^"
},
"description": "RageFW CEF side",
"keywords": [],
"author": "SashaGoncharov19",
"license": "MIT",
"description": "CEF side for rage-fw"
"contributors": [{
"name": "rilaxik",
"email": "dev.rilaxik@gmail.com",
"url": "https://github.com/rilaxik"
}],
"license": "MIT"
}

View File

@ -14,10 +14,6 @@
"dist/**/*",
"readme.md"
],
"description": "CLI to scaffold a template project for RageFW",
"keywords": [],
"author": "rilaxik",
"license": "ISC",
"dependencies": {
"@inquirer/prompts": "^5.0.5",
"axios": "^1.7.2",
@ -31,5 +27,12 @@
"@types/yargs": "^17.0.32",
"prettier": "^3.3.2",
"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",
"rage-fw-shared-types": "workspace:^"
},
"description": "RageFW Client side",
"keywords": [],
"author": "SashaGoncharov19",
"contributors": [{
"name": "rilaxik",
"email": "dev.rilaxik@gmail.com",
"url": "https://github.com/rilaxik"
}],
"license": "MIT",
"description": "Client side of rage-fw",
"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",
"types": "dist/src/index.d.ts",
"scripts": {
"watch": "tsc -w",
"build": "tsup",
"start": "npx ./dist create"
},
"files": [
"dist/**/*"
],
"description": "CLI to scaffold a template project for RageFW",
"keywords": [],
"author": "rilaxik",
"license": "ISC",
"devDependencies": {
"prettier": "^3.3.2",
"typescript": "^5.4.5"
}
"prettier": "^3.3.2"
},
"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 { EVENT_LISTENER } from './events'
import { client } from './client'
import { server } from './server'
import type { RPCData } from './types.ts'
import { client } from './modules/client'
import { server } from './modules/server'
import { cef } from './modules/cef'
class FrameworkRpc {
private readonly _environment: Environment
private readonly _environmentGlobal: Record<string, Function>
constructor() {
this._environment = utils.getEnvironment()
this._environmentGlobal =
utils.getEnvironment() === utils.environment.CEF
class Rpc {
private _environment = utils.getEnvironment()
private _state =
utils.getEnvironment() === Environment.CEF
? window.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) {
case utils.environment.UNKNOWN:
return
case Environment.SERVER:
await server.listenEvent(player, request)
break
case utils.environment.CLIENT:
player = data
return client.listen(player)
case Environment.CLIENT:
request = player
case utils.environment.SERVER:
return server.listen(player, data)
// await client.listenEvent(request)
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,
cb: (...args: Args) => Return,
cb: (...args: Callback) => Return,
) {
if (this._environment === utils.environment.UNKNOWN) return
this._environmentGlobal[eventName] = cb
if (this._environment === Environment.UNKNOWN) return
this._state[eventName] = cb
}
public callClient<Args extends any[] = unknown[], Return = unknown>(
public async callClient<Args extends any[] = unknown[], Return = unknown>(
player: any,
eventName: string,
...args: Args
): Promise<Return | unknown> {
return new Promise((resolve, _reject) => {
const uuid = utils.generateUUID()
const data: RPCData = {
uuid,
eventName,
from: this._environment,
to: utils.environment.CLIENT,
data: args,
if (this._environment === Environment.UNKNOWN) return
if (this._environment === Environment.SERVER) {
// return client.executeClient(player, eventName, args)
}
}
player.call(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)
})
}
public callServer<Args extends any[] = unknown[], Return = unknown>(
public async callServer<Args extends any[] = unknown[], Return = unknown>(
eventName: string,
...args: Args
): Promise<Return | unknown> {
return new Promise((resolve, _reject) => {
const uuid = utils.generateUUID()
switch (this._environment) {
case Environment.UNKNOWN:
return
const data: RPCData = {
uuid,
eventName,
from: this._environment,
to: utils.environment.CLIENT,
data: args,
case Environment.SERVER:
return
case Environment.CEF:
return client
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()
export { rpc }
const testRpc = new Rpc()
export { testRpc }

View File

@ -27,7 +27,7 @@ class Cef extends Wrapper {
public async callServer<Args extends any[] = unknown[], Return = unknown>(
eventName: string,
...args: Args
): Promise<Return | unknown> {}
): Promise<Return | void> {}
private async handleReturn(
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 { Environment, RPCState } from '../utils'
import { Environment } from '../utils'
class Client extends Wrapper {
constructor() {

View File

@ -1,11 +1,14 @@
import { Environment, Errors, RPCState, utils } from '../utils'
export class Wrapper {
public _utils = utils
public _environment = utils.getEnvironment()
public _state = this._environment === Environment.CEF ? window : global
protected _utils = utils
protected _environment = utils.getEnvironment()
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)
if (!this._state[rpcData.eventName]) {
@ -15,7 +18,7 @@ export class Wrapper {
return rpcData
}
public _triggerError(rpcData: RPCState, error?: any) {
protected _triggerError(rpcData: RPCState, error?: any) {
const errorMessage = [
`${rpcData.knownError}`,
`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 { RPC_LISTENER } from './events'
import { EVENT_RESPONSE } from './events'
export enum Environment {
CEF = 'CEF',
@ -8,7 +7,36 @@ export enum Environment {
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 = '',
random
@ -19,24 +47,17 @@ function generateUUID(): string {
uuid += '-'
}
uuid += (i === 12 ? 4 : i === 16 ? (random & 3) | 8 : random).toString(
16,
)
uuid += (
i === 12 ? 4 : i === 16 ? (random & 3) | 8 : random
).toString(16)
}
return uuid
}
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
},
parseData: (data: string): RPCData => JSON.parse(data),
stringifyData: (data: RPCData): string => JSON.stringify(data),
generateResponseEventName: (uuid: string) => `${RPC_LISTENER}_${uuid}`,
generateUUID,
public generateResponseEventName(uuid: string): string {
return `${EVENT_RESPONSE}_${uuid}`
}
}
export const utils = new Utils()

View File

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

View File

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