@gta5urban/rage-rpc 0.3.0

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/LICENSE ADDED
@@ -0,0 +1,24 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2021 Micah Allen (micaww)
4
+ Copyright (c) 2021 Sergei Marochkin (ziggi)
5
+ Copyright (c) 2021 Yiin
6
+ Copyright (c) 2021 Narcis B. (LeonardSSH)
7
+
8
+ Permission is hereby granted, free of charge, to any person obtaining a copy
9
+ of this software and associated documentation files (the "Software"), to deal
10
+ in the Software without restriction, including without limitation the rights
11
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12
+ copies of the Software, and to permit persons to whom the Software is
13
+ furnished to do so, subject to the following conditions:
14
+
15
+ The above copyright notice and this permission notice shall be included in all
16
+ copies or substantial portions of the Software.
17
+
18
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,582 @@
1
+ > fork of [micaww/rage-rpc](https://github.com/micaww/rage-rpc/)
2
+
3
+ - [Motivation](#motivation)
4
+ - [Installation](#installation)
5
+ - [Examples](#examples)
6
+ - [Server to Client](#server-to-client)
7
+ - [CEF to Server](#cef-to-server)
8
+ - [Client to Server](#client-to-server)
9
+ - [API](#api)
10
+ - [Universal](#universal)
11
+ - [setDebugMode(state)](#setdebugmodestate)
12
+ - [register(name, callback)](#registername-callback)
13
+ - [unregister(name)](#unregistername)
14
+ - [call(name, args)](#callname-args-options)
15
+ - [callServer(name, args)](#callservername-args-options)
16
+ - [on(name, callback)](#onname-callback)
17
+ - [off(name, callback)](#offname-callback)
18
+ - [trigger(name, args)](#triggername-args)
19
+ - triggerServer(name, args)
20
+ - [Server-side](#server-side-3)
21
+ - [callClient(player, name, args)](#callclientplayer-name-args)
22
+ - [callBrowsers(player, name, args)](#callbrowsersplayer-name-args-options)
23
+ - triggerClient(player, name, args)
24
+ - triggerBrowsers(player, name, args)
25
+ - [Client-side](#client-side-2)
26
+ - [callBrowser(browser, name, args)](#callbrowserbrowser-name-args-options)
27
+ - triggerBrowser(browser, name, args)
28
+ - [CEF or Client-side](#cef-or-client-side)
29
+ - [callBrowsers(name, args)](#callbrowsersplayer-name-args-options)
30
+ - [callClient(name, args)](#callclientname-args-options)
31
+ - [triggerBrowsers(name, args)](#triggerbrowsersname-args)
32
+ - [triggerClient(name, args)](#triggerclientname-args)
33
+ - [Options](#options)
34
+ - [Events](#events)
35
+ - [Changelog](#changelog)
36
+
37
+ ## Motivation
38
+
39
+ A very common workflow when developing with any kind of client-server platform is not only sending data between the server and clients, but also receiving data back after performing some kind of action. An example would be a client asking for information from a database in order to display to the user. One technique to achieve this is called [remote procedure calls (RPC)](https://en.wikipedia.org/wiki/Remote_procedure_call) which allows one application context to call code in a completely separate context and return the result back to the caller, as if it were local to begin with.
40
+
41
+ In RAGE Multiplayer, this kind of functionality is not supported natively. In order for a player to ask something of the server, the server must set up an event handler that the player calls remotely, then the server does its processing and calls _another_ event handler that resides on the client. There are many pitfalls to this approach, including but not limited to messy code and false identification (am i sending the response to the right caller instance?). Natively, the server cannot directly communicate with CEF instances at all. You have to route _all requests_ through the client. Suddenly, you have 16 different events to handle one simple data request. It's horrible. And when your codebase starts growing, it becomes a huge hassle to deal with.
42
+
43
+ This is pretty much what everybody has learned to deal with, until now. `rage-rpc` simplifies two-way communication between the RAGE Multiplayer server, client, and browser instances by providing a easy-to-use API for calling remote code and retrieving results. **Any context can call a function that resides in any other context and immediately get access to its return value without messing with events.** This means any CEF instance can call code on the server, the client, or any other CEF instances and easily see the result.
44
+
45
+ ---
46
+
47
+ ## Installation
48
+
49
+ #### Option 1
50
+
51
+ You can install via [github](https://github.com/LeonardSSH/rage-rpc)
52
+
53
+ > Use `github:leonardssh/rage-rpc#build` to latest build
54
+
55
+ ```bash
56
+ # With npm
57
+ npm i github:leonardssh/rage-rpc#v0.2.5
58
+
59
+ # With yarn
60
+ yarn add github:leonardssh/rage-rpc#v0.2.5
61
+
62
+ # With pnpm
63
+ pnpm add github:leonardssh/rage-rpc#v0.2.5
64
+ ```
65
+
66
+ From here, you can simply require the package in any RAGE context:
67
+
68
+ ```javascript
69
+ const rpc = require('rage-rpc');
70
+
71
+ rpc.register('hi', () => 'hello!');
72
+ ```
73
+
74
+ #### Option 2
75
+
76
+ In the [`dist/`](https://github.com/rysemultiplayer/rpc/tree/build/dist) folder of [`build`](https://github.com/rysemultiplayer/rpc/tree/build) branch is a JS file ([`rage-rpc.js`](https://github.com/rysemultiplayer/rpc/blob/build/dist/rage-rpc.js)) that you can download and request in any RAGE context. It works the same as the above option, but you will have to manually reload the file when new versions are released.
77
+
78
+ ```javascript
79
+ const rpc = require('./rage-rpc.js');
80
+
81
+ rpc.register('hi', () => 'hello!');
82
+ ```
83
+
84
+ #### Option 3 (Browser Only)
85
+
86
+ In order to use `require` in the browser, you'll need either an AMD loader or some kind of bundler like Webpack or Rollup. If those options don't suit your project, you can load the file into browser contexts with just a script tag before the code you use it in. It will expose a global `rpc` variable that you can use on your page.
87
+
88
+ ```html
89
+ <html>
90
+ <head>
91
+ <title>My CEF Page</title>
92
+ <script type="text/javascript" src="./rage-rpc.umd.js"></script>
93
+ <script type="text/javascript">
94
+ rpc.register('hi', () => 'hello from cef!');
95
+
96
+ // ...
97
+ </script>
98
+ </head>
99
+ </html>
100
+ ```
101
+
102
+ ## Examples
103
+
104
+ ### Server to Client
105
+
106
+ **Situation:** The server wants to ask a specific player if they are currently climbing anything.
107
+
108
+ ##### Client-side
109
+
110
+ ```javascript
111
+ const rpc = require('rage-rpc');
112
+
113
+ rpc.register('getIsClimbing', () => mp.players.local.isClimbing());
114
+ ```
115
+
116
+ ##### Server-side
117
+
118
+ ```javascript
119
+ const rpc = require('rage-rpc');
120
+
121
+ const player = mp.players.at(0);
122
+
123
+ rpc.callClient(player, 'getIsClimbing').then((climbing) => {
124
+ if (climbing) {
125
+ console.log('The player is climbing!');
126
+ } else {
127
+ console.log('The player is not climbing!');
128
+ }
129
+ });
130
+
131
+ // or even just this inside an async function:
132
+ const isClimbing = await rpc.callClient(player, 'getIsClimbing');
133
+ ```
134
+
135
+ **_That's it!_** No extra code to sort out who is asking for what, or setting up multiple events on each side just to send a single piece of data back to the caller.
136
+
137
+ ---
138
+
139
+ ### CEF to Server
140
+
141
+ **Situation:** A CEF instance wants a list of all vehicle license plates directly from the server.
142
+
143
+ ##### Server-side
144
+
145
+ ```javascript
146
+ const rpc = require('rage-rpc');
147
+
148
+ rpc.register('getAllLicensePlates', () => mp.vehicles.toArray().map((vehicle) => vehicle.numberPlate));
149
+ ```
150
+
151
+ ##### Client-side
152
+
153
+ ```javascript
154
+ // even if not using RPC on the client, it must be required somewhere before CEF can send any events
155
+ require('rage-rpc');
156
+ ```
157
+
158
+ ##### Browser
159
+
160
+ ```javascript
161
+ const rpc = require('rage-rpc');
162
+
163
+ rpc.callServer('getAllLicensePlates').then((plates) => {
164
+ alert(plates.join(', '));
165
+ });
166
+ ```
167
+
168
+ With `rage-rpc`, CEF can directly communicate with the server and vice-versa, without having to pass everything through the client-side JS.
169
+
170
+ ###### In vanilla RAGE, you would have to set up multiple events for sending/receiving on the client-side, call them from CEF, then resend the data to the server and back. It's a huge hassle.
171
+
172
+ ---
173
+
174
+ ### Client to Server
175
+
176
+ **Situation:** Give the clients/CEF the ability to log to the server's console.
177
+
178
+ ##### Server-side
179
+
180
+ ```javascript
181
+ const rpc = require('rage-rpc');
182
+
183
+ rpc.register('log', (message, info) => {
184
+ /*
185
+ the second argument, info, gives information about the request such as
186
+ - the internal ID of the request
187
+ - the environment in which the request was sent (server, client, or cef)
188
+ - the player who sent the request, if any
189
+ */
190
+
191
+ console.log(info.player.name + ': ' + message);
192
+ });
193
+ ```
194
+
195
+ ##### Client-side OR Browser
196
+
197
+ ```javascript
198
+ const rpc = require('rage-rpc');
199
+
200
+ function log(message) {
201
+ return rpc.callServer('log', message);
202
+ }
203
+
204
+ // send it and forget it
205
+ log('Hello, Server!');
206
+
207
+ // send it again, but make sure it was successfully received
208
+ log('Hello again!')
209
+ .then(() => {
210
+ // the server acknowledged and processed the message
211
+ })
212
+ .catch(() => {
213
+ // the message either timed out or the procedure was never registered
214
+ });
215
+ ```
216
+
217
+ **Note:** Once any side of the game registers a procedure, any context can immediately start accessing it. You could call `rpc.callServer('log', message);` from any CEF instance or anywhere in the client without any further setup.
218
+
219
+ ## API
220
+
221
+ This library is universal to RAGE, which means you can load the same package into all 3 contexts: browser, client JS, and server JS.
222
+
223
+ There are only 7 functions that you can use almost anywhere around your game. However, depending on the current context, the usage of some functions might differ slightly.
224
+
225
+ ### Universal
226
+
227
+ #### setDebugMode(state)
228
+
229
+ Set whether the rpc to display debugging messages or not.
230
+
231
+ ##### Examples
232
+
233
+ ```js
234
+ rpc.setDebugMode(true); // disabled by default
235
+
236
+ rpc.register('hello', () => 'hi!');
237
+ ```
238
+
239
+ Logs `RPC (server): Registered procedure "hello"`.
240
+
241
+ ---
242
+
243
+ ```js
244
+ rpc.setDebugMode(false);
245
+
246
+ rpc.register('hello', () => 'hi!');
247
+ ```
248
+
249
+ No logs
250
+
251
+ ##### Parameters
252
+
253
+ - `state` [boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean) - Set whether the rpc to display debugging messages or not.
254
+
255
+ #### register(name, callback)
256
+
257
+ Registers a procedure in the current context.
258
+
259
+ The return value of the `callback` will be sent back to the caller, even if it fails. If a [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) is returned, it will finish before returning its result or error to the caller.
260
+
261
+ **The return value must be JSON-able in order to be sent over the network.** This doesn't matter if the procedure call is local.
262
+
263
+ ##### Parameters
264
+
265
+ - `name` [string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) - The unique identifier, relative to the current context, of the procedure.
266
+ - `callback` [function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function) - The procedure. This function will receive 2 arguments.
267
+ - `args` - The arguments that were provided by the caller. This parameter's type will be the same that was sent by the caller. `undefined` if no arguments were sent.
268
+ - `info` [object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object) - Various information about the caller.
269
+ - `id` [string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) - The internal ID used to keep track of this request.
270
+ - `environment` [string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) - The caller's environment. Can be `cef`, `client`, or `server`.
271
+ - `player` [Player](https://wiki.rage.mp/index.php?title=Server-side_functions#Player) - The caller. _Only exists in the server context if remotely called from `cef` or `client`._
272
+
273
+ ##### Examples
274
+
275
+ ```javascript
276
+ rpc.register('hello', () => 'hi!');
277
+ ```
278
+
279
+ Returns `hi!` to the caller.
280
+
281
+ ---
282
+
283
+ ```javascript
284
+ rpc.register('getUser', async (id) => {
285
+ const user = await someLongOperationThatReturnsUserFromId(id);
286
+ return user;
287
+ });
288
+ ```
289
+
290
+ Waits for the returned [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) to finish before returning the resolved user to the caller.
291
+
292
+ ---
293
+
294
+ ```javascript
295
+ rpc.register('echo', (message, info) => {
296
+ console.log(`${info.player.name} via ${info.environment}: ${message}`);
297
+ });
298
+ ```
299
+
300
+ _Server-side example only._ The passed argument will be logged to the console along with the caller's name and the environment which they called from.
301
+
302
+ #### unregister(name)
303
+
304
+ Unregisters a procedure from the current context. It will no longer take requests unless it is re-registered.
305
+
306
+ - `name` [string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) - The unique identifier, relative to the current context, of the procedure.
307
+
308
+ #### call(name, args?, options?)
309
+
310
+ Calls a procedure that has been registered in the current context.
311
+
312
+ - `name` [string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) - The name of the previously registered procedure.
313
+ - `args?` - Optional arguments to pass to the procedure. Can be of any type, since `call` does not traverse the network.
314
+ - `options?` - Optional [options](#options) to control how the procedure is called.
315
+
316
+ ##### Example
317
+
318
+ ```javascript
319
+ rpc.register('hi', () => 'hello!');
320
+
321
+ rpc.call('hi')
322
+ .then((result) => {
323
+ // result = hello!
324
+ console.log(result);
325
+ })
326
+ .catch((err) => {
327
+ console.error(err);
328
+ });
329
+ ```
330
+
331
+ ###### Returns [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) resolving or failing due to the procedure's result. If the procedure called does not exist, `PROCEDURE_NOT_FOUND` will be thrown.
332
+
333
+ #### callServer(name, args?, options?)
334
+
335
+ Calls a procedure that has been registered on the server.
336
+
337
+ - `name` [string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) - The name of the previously registered procedure.
338
+ - `args?` - Optional arguments to pass to the procedure. Must be JSON-able if the current context is not the server. Use an array or object to pass multiple arguments.
339
+ - `options?` - Optional [options](#options) to control how the procedure is called.
340
+
341
+ ##### Example
342
+
343
+ Server-side:
344
+
345
+ ```javascript
346
+ rpc.register('getWeather', () => mp.world.weather);
347
+ ```
348
+
349
+ Client-side OR Browser OR Server:
350
+
351
+ ```javascript
352
+ rpc.callServer('getWeather')
353
+ .then((weather) => {
354
+ mp.gui.chat.push(`The current weather is ${weather}.`);
355
+ })
356
+ .catch((err) => {
357
+ // handle error
358
+ });
359
+ ```
360
+
361
+ ###### Returns [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) resolving or failing due to the procedure's result. If the procedure called does not exist, `PROCEDURE_NOT_FOUND` will be thrown.
362
+
363
+ ### Server-side
364
+
365
+ #### callClient(player, name, args?)
366
+
367
+ Calls a procedure that has been registered on a specific client.
368
+
369
+ - `player` [Player](https://wiki.rage.mp/index.php?title=Server-side_functions#Player) - The player to call the procedure on.
370
+ - `name` [string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) - The name of the registered procedure.
371
+ - `args?` - Optional arguments to pass to the procedure. Must be JSON-able. Use an array or object to pass multiple arguments.
372
+ - `options?` - Optional [options](#options) to control how the procedure is called.
373
+
374
+ ##### Example
375
+
376
+ Client-side:
377
+
378
+ ```javascript
379
+ rpc.register('toggleChat', (toggle) => {
380
+ mp.gui.chat.show(toggle);
381
+ });
382
+ ```
383
+
384
+ Server-side:
385
+
386
+ ```javascript
387
+ mp.players.forEach((player) => {
388
+ rpc.callClient(player, 'toggleChat', false);
389
+ });
390
+ ```
391
+
392
+ ###### Returns [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) resolving or failing due to the procedure's result. If the procedure called does not exist, `PROCEDURE_NOT_FOUND` will be thrown.
393
+
394
+ #### callBrowsers(player, name, args?, options?)
395
+
396
+ Calls a procedure that has been registered in any CEF instance on a specific client.
397
+
398
+ Any CEF instance can register the procedure. The client will iterate through each instance and call the procedure on the first instance that it exists on.
399
+
400
+ - `player` [Player](https://wiki.rage.mp/index.php?title=Server-side_functions#Player) - The player to call the procedure on.
401
+ - `name` [string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) - The name of the registered procedure.
402
+ - `args?` - Optional arguments to pass to the procedure. Must be JSON-able. Use an array or object to pass multiple arguments.
403
+ - `options?` - Optional [options](#options) to control how the procedure is called.
404
+
405
+ ##### Example
406
+
407
+ Browser:
408
+
409
+ ```javascript
410
+ rpc.register('toggleHUD', (toggle) => {
411
+ // if jQuery is your thing
412
+ $('#hud').toggle(toggle);
413
+ });
414
+ ```
415
+
416
+ Server-side:
417
+
418
+ ```javascript
419
+ mp.players.forEach((player) => {
420
+ rpc.callClient(player, 'toggleChat', false);
421
+ });
422
+ ```
423
+
424
+ ###### Returns [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) resolving or failing due to the procedure's result. If the procedure called does not exist, `PROCEDURE_NOT_FOUND` will be thrown.
425
+
426
+ ### Client-side
427
+
428
+ #### callBrowser(browser, name, args?, options?)
429
+
430
+ Calls a procedure that has been registered in a specific CEF instance.
431
+
432
+ - `browser` [Browser](https://wiki.rage.mp/index.php?title=Client-side_functions#Browser) - The browser to call the procedure on.
433
+ - `name` [string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) - The name of the registered procedure.
434
+ - `args?` - Optional arguments to pass to the procedure. Must be JSON-able. Use an array or object to pass multiple arguments.
435
+ - `options?` - Optional [options](#options) to control how the procedure is called.
436
+
437
+ ##### Example
438
+
439
+ Browser:
440
+
441
+ ```javascript
442
+ rpc.register('getInputValue', () => {
443
+ // if jQuery is your thing
444
+ return $('#input').val();
445
+ });
446
+ ```
447
+
448
+ Client-side:
449
+
450
+ ```javascript
451
+ const browser = mp.browsers.at(0);
452
+
453
+ rpc.callBrowser(browser, 'getInputValue')
454
+ .then((value) => {
455
+ mp.gui.chat.push(`The CEF input value is: ${value}`);
456
+ })
457
+ .catch((err) => {
458
+ // handle errors
459
+ });
460
+ ```
461
+
462
+ ###### Returns [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) resolving or failing due to the procedure's result. If the procedure called does not exist, `PROCEDURE_NOT_FOUND` will be thrown.
463
+
464
+ ### CEF or Client-side
465
+
466
+ #### callBrowsers(name, args?, options?)
467
+
468
+ Calls a procedure that has been registered in any CEF instance on a specific client.
469
+
470
+ Any CEF instance can register the procedure. The client will iterate through each instance and call the procedure on the first instance that it exists on.
471
+
472
+ - `name` [string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) - The name of the registered procedure.
473
+ - `args?` - Optional arguments to pass to the procedure. Must be JSON-able. Use an array or object to pass multiple arguments.
474
+ - `options?` - Optional [options](#options) to control how the procedure is called.
475
+
476
+ ##### Example
477
+
478
+ Browser:
479
+
480
+ ```javascript
481
+ rpc.register('toggleHUD', (toggle) => {
482
+ // if jQuery is your thing
483
+ $('#hud').toggle(toggle);
484
+ });
485
+ ```
486
+
487
+ Client-side OR Browser:
488
+
489
+ ```javascript
490
+ rpc.callBrowsers('toggleChat', false);
491
+ ```
492
+
493
+ ###### Returns [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) resolving or failing due to the procedure's result. If the procedure called does not exist, `PROCEDURE_NOT_FOUND` will be thrown.
494
+
495
+ #### callClient(name, args?, options?)
496
+
497
+ Calls a procedure that has been registered on the local client.
498
+
499
+ - `name` [string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) - The name of the registered procedure.
500
+ - `args?` - Optional arguments to pass to the procedure. Must be JSON-able if the current context is not this client. Use an array or object to pass multiple arguments.
501
+ - `options?` - Optional [options](#options) to control how the procedure is called.
502
+
503
+ ##### Example
504
+
505
+ Client-side:
506
+
507
+ ```javascript
508
+ rpc.register('toggleChat', (toggle) => {
509
+ mp.gui.chat.show(toggle);
510
+ });
511
+ ```
512
+
513
+ Client-side OR Browser:
514
+
515
+ ```javascript
516
+ rpc.callClient('toggleChat', false);
517
+ ```
518
+
519
+ ###### Returns [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) resolving or failing due to the procedure's result. If the procedure called does not exist, `PROCEDURE_NOT_FOUND` will be thrown.
520
+
521
+ ## Options
522
+
523
+ For remote procedure calling functions, there are optional options you can pass as the last parameter:
524
+
525
+ - timeout (number): The amount of time in milliseconds to reject the call automatically
526
+ - noRet (boolean): Prevent the remote context from sending data back. Saves bandwidth, but the promise will never return or reject. Similar to using `trigger`.
527
+
528
+ ## Events
529
+
530
+ You can now use rage-rpc as a full on replacement for mp.events. API functions that start with "trigger" use the same syntax as the ones that start with "call", except they do not return anything. They call remote events on any context where there can be many handlers or none.
531
+
532
+ ## Changelog
533
+
534
+ Check the releases tab for an up-to-date changelog.
535
+
536
+ #### 0.2.5
537
+
538
+ - FIX: Use Player & Browser instead of Mp version
539
+ - FIX: ProcedureListenerInfo player & browser types
540
+
541
+ #### 0.2.4
542
+
543
+ - IMPROVE: Type-safe & Type Definitions
544
+
545
+ #### 0.2.3
546
+
547
+ - FIX: Player null type assignment
548
+
549
+ #### 0.2.2
550
+
551
+ - ADD: Terser to minify generated bundle
552
+
553
+ #### 0.2.1
554
+
555
+ - ADD: Logs for `on` & `off` listeners
556
+
557
+ #### 0.2.0
558
+
559
+ - FIX: ES6, CommonJS and UMD compatibility
560
+ - ADD: Debug Mode
561
+ - ADD: Generics type-safe to call functions
562
+
563
+ #### 0.1.0
564
+
565
+ - ADD: Bundled Typescript definitions
566
+ - IMPROVE: CEF outgoing call returning performance
567
+ - IMRPOVE: `callBrowsers` performance on all contexts
568
+ - FIX: Some code simplifications
569
+
570
+ #### 0.0.3
571
+
572
+ - ADD: Extra player verification for outgoing server calls
573
+ - FIX: Bug that prevented multiple resources from using RPC at the same time
574
+ - FIX: False alarm for multiple CEF instances receiving the same result
575
+
576
+ #### 0.0.2
577
+
578
+ - FIX: UMD exposing for correct Node.js importing
579
+
580
+ #### 0.0.1
581
+
582
+ - Initial commit
package/package.json ADDED
@@ -0,0 +1,75 @@
1
+ {
2
+ "name": "@gta5urban/rage-rpc",
3
+ "version": "0.3.0",
4
+ "publishConfig": {
5
+ "access": "public"
6
+ },
7
+ "description": "A universal, asynchronous RPC implementation for RAGE Multiplayer",
8
+ "scripts": {
9
+ "lint": "eslint src --ext mjs,js,ts,tsx --fix",
10
+ "format": "prettier --write src/**/*.{mjs,ts,js}",
11
+ "prebuild": "npm run clean",
12
+ "build": "rollup -c ./scripts/rollup.config.js --configProduction",
13
+ "clean": "rimraf {dist,*.tgz}",
14
+ "watch": "rollup -w -c ./scripts/rollup.config.js"
15
+ },
16
+ "main": "dist/rage-rpc.js",
17
+ "module": "dist/rage-rpc.esm.mjs",
18
+ "types": "types/index.d.ts",
19
+ "files": [
20
+ "dist/*",
21
+ "types/*"
22
+ ],
23
+ "exports": {
24
+ "require": "./dist/rage-rpc.js",
25
+ "import": "./dist/rage-rpc.esm.mjs"
26
+ },
27
+ "repository": {
28
+ "type": "git",
29
+ "url": "git+https://github.com/gta5urban/rage-rpc.git"
30
+ },
31
+ "author": "@gta5urban",
32
+ "license": "MIT",
33
+ "bugs": {
34
+ "url": "https://github.com/gta5urban/rage-rpc/issues"
35
+ },
36
+ "homepage": "https://github.com/gta5urban/rage-rpc#readme",
37
+ "devDependencies": {
38
+ "@rollup/plugin-node-resolve": "^13.1.1",
39
+ "@types/node": "^17.0.2",
40
+ "@typescript-eslint/eslint-plugin": "^5.8.0",
41
+ "@typescript-eslint/parser": "^5.8.0",
42
+ "eslint": "^8.5.0",
43
+ "eslint-config-prettier": "^8.3.0",
44
+ "eslint-plugin-prettier": "^4.0.0",
45
+ "minimist": "^1.2.5",
46
+ "prettier": "^2.5.1",
47
+ "pretty-quick": "^3.1.3",
48
+ "rimraf": "^3.0.2",
49
+ "rollup": "^2.61.1",
50
+ "rollup-plugin-terser": "^7.0.2",
51
+ "rollup-plugin-typescript2": "^0.31.1",
52
+ "rollup-plugin-version-injector": "^1.3.3",
53
+ "typescript": "^4.5.4"
54
+ },
55
+ "prettier": {
56
+ "$schema": "http://json.schemastore.org/prettierrc",
57
+ "endOfLine": "lf",
58
+ "printWidth": 150,
59
+ "quoteProps": "as-needed",
60
+ "semi": true,
61
+ "singleQuote": true,
62
+ "tabWidth": 4,
63
+ "trailingComma": "none",
64
+ "useTabs": true,
65
+ "overrides": [
66
+ {
67
+ "files": "*.yml",
68
+ "options": {
69
+ "tabWidth": 2,
70
+ "useTabs": false
71
+ }
72
+ }
73
+ ]
74
+ }
75
+ }
@@ -0,0 +1,179 @@
1
+ declare interface Browser {
2
+ url: string;
3
+ execute: (code: string) => void;
4
+ [property: string]: any;
5
+ }
6
+
7
+ declare interface Player {
8
+ call: (eventName: string, args?: any[]) => void;
9
+ [property: string]: any;
10
+ }
11
+
12
+ export declare type ProcedureListener = (args: any, info: ProcedureListenerInfo) => any;
13
+
14
+ export declare interface ProcedureListenerInfo<T = any, K = any> {
15
+ environment: string;
16
+ id?: string;
17
+ player?: T;
18
+ browser?: K;
19
+ }
20
+
21
+ /**
22
+ * Calls a local procedure. Only procedures registered in the same context will be resolved.
23
+ *
24
+ * Can be called from any environment.
25
+ *
26
+ * @param name - The name of the locally registered procedure.
27
+ * @param args - Any parameters for the procedure.
28
+ * @param options - Any options.
29
+ * @returns The result from the procedure.
30
+ */
31
+ export declare function call<T = any>(name: string, args?: any, options?: CallOptions): Promise<T>;
32
+
33
+ /**
34
+ * Calls a remote procedure registered in a specific browser instance.
35
+ *
36
+ * Client-side environment only.
37
+ *
38
+ * @param browser - The browser instance.
39
+ * @param name - The name of the registered procedure.
40
+ * @param args - Any parameters for the procedure.
41
+ * @param options - Any options.
42
+ * @returns The result from the procedure.
43
+ */
44
+ export declare function callBrowser<T = any>(browser: Browser, name: string, args?: any, options?: CallOptions): Promise<T>;
45
+
46
+ /**
47
+ * Calls a remote procedure registered in any browser context.
48
+ *
49
+ * Can be called from any environment.
50
+ *
51
+ * @param player - The player to call the procedure on.
52
+ * @param name - The name of the registered procedure.
53
+ * @param args - Any parameters for the procedure.
54
+ * @param options - Any options.
55
+ * @returns The result from the procedure.
56
+ */
57
+ export declare function callBrowsers<T = any>(name: string, args?: any, options?: CallOptions): Promise<T> | undefined;
58
+ export declare function callBrowsers<T = any>(player: Player, name: string, args?: any, options?: CallOptions): Promise<T> | undefined;
59
+
60
+ /**
61
+ * Calls a remote procedure registered on the client.
62
+ *
63
+ * Can be called from any environment.
64
+ *
65
+ * @param player - The player to call the procedure on.
66
+ * @param name - The name of the registered procedure.
67
+ * @param args - Any parameters for the procedure.
68
+ * @param options - Any options.
69
+ * @returns The result from the procedure.
70
+ */
71
+ export declare function callClient<T = any>(name: string, args?: any, options?: CallOptions): Promise<T>;
72
+ export declare function callClient<T = any>(player: Player, name: string, args?: any, options?: CallOptions): Promise<T>;
73
+
74
+ export declare interface CallOptions {
75
+ timeout?: number;
76
+ noRet?: boolean;
77
+ }
78
+
79
+ /**
80
+ * Calls a remote procedure registered on the server.
81
+ *
82
+ * Can be called from any environment.
83
+ *
84
+ * @param name - The name of the registered procedure.
85
+ * @param args - Any parameters for the procedure.
86
+ * @param options - Any options.
87
+ * @returns The result from the procedure.
88
+ */
89
+ export declare function callServer<T = any>(name: string, args?: any, options?: CallOptions): Promise<T>;
90
+
91
+ /**
92
+ * Unregister an event handler.
93
+ * @param {string} name - The name of the event.
94
+ * @param {ProcedureListener} cb - The callback for the event.
95
+ */
96
+ export declare function off(name: string, cb: ProcedureListener): void;
97
+
98
+ /**
99
+ * Register an event handler.
100
+ * @param {string} name - The name of the event.
101
+ * @param {ProcedureListener} cb - The callback for the event.
102
+ * @returns The function, which off the event.
103
+ */
104
+ export declare function on(name: string, cb: ProcedureListener): () => void;
105
+
106
+ /**
107
+ * Register a procedure.
108
+ * @param {string} name - The name of the procedure.
109
+ * @param {ProcedureListener} cb - The procedure's callback. The return value will be sent back to the caller.
110
+ * @returns The function, which unregister the event.
111
+ */
112
+ export declare function register(name: string, cb: ProcedureListener): () => void;
113
+
114
+ export declare function setDebugMode(state: boolean): void;
115
+
116
+ /**
117
+ * Triggers a local event. Only events registered in the same context will be triggered.
118
+ *
119
+ * Can be called from any environment.
120
+ *
121
+ * @param name - The name of the locally registered event.
122
+ * @param args - Any parameters for the event.
123
+ */
124
+ export declare function trigger(name: string, args?: any): void;
125
+
126
+ /**
127
+ * Triggers an event registered in a specific browser instance.
128
+ *
129
+ * Client-side environment only.
130
+ *
131
+ * @param browser - The browser instance.
132
+ * @param name - The name of the event.
133
+ * @param args - Any parameters for the event.
134
+ */
135
+ export declare function triggerBrowser(browser: Browser, name: string, args?: any): void;
136
+
137
+ /**
138
+ * Triggers an event registered in any browser context.
139
+ *
140
+ * Can be called from any environment.
141
+ *
142
+ * @param player - The player to call the procedure on.
143
+ * @param name - The name of the event.
144
+ * @param args - Any parameters for the event.
145
+ */
146
+ export declare function triggerBrowsers(name: string, args?: any): void;
147
+ export declare function triggerBrowsers(player: Player, name: string, args?: any): void;
148
+
149
+ /**
150
+ * Triggers an event registered on the client.
151
+ *
152
+ * Can be called from any environment.
153
+ *
154
+ * @param player - The player to call the procedure on.
155
+ * @param name - The name of the event.
156
+ * @param args - Any parameters for the event.
157
+ */
158
+ export declare function triggerClient(name: string, args?: any): void;
159
+ export declare function triggerClient(player: Player, name: string, args?: any): void;
160
+
161
+ /**
162
+ * Triggers an event registered on the server.
163
+ *
164
+ * Can be called from any environment.
165
+ *
166
+ * @param name - The name of the event.
167
+ * @param args - Any parameters for the event.
168
+ */
169
+ export declare function triggerServer(name: string, args?: any): void;
170
+
171
+ /**
172
+ * Unregister a procedure.
173
+ * @param {string} name - The name of the procedure.
174
+ */
175
+ export declare function unregister(name: string): void;
176
+
177
+ export declare const version: string;
178
+
179
+ export {};