Archived
Add basic webpack build
This commit is contained in:
1 parent
b628bb9ee9
commit
d40908d8d0
7 files changed
+3690
-54
No files matched your search
Vendored
+1
@@ -0,0 +1 @@
|
||||
!function(e,r){"object"==typeof exports&&"object"==typeof module?module.exports=r():"function"==typeof define&&define.amd?define([],r):"object"==typeof exports?exports.rpc=r():e.rpc=r()}(window,function(){return function(e){var r={};function t(n){if(r[n])return r[n].exports;var o=r[n]={i:n,l:!1,exports:{}};return e[n].call(o.exports,o,o.exports,t),o.l=!0,o.exports}return t.m=e,t.c=r,t.d=function(e,r,n){t.o(e,r)||Object.defineProperty(e,r,{enumerable:!0,get:n})},t.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},t.t=function(e,r){if(1&r&&(e=t(e)),8&r)return e;if(4&r&&"object"==typeof e&&e&&e.__esModule)return e;var n=Object.create(null);if(t.r(n),Object.defineProperty(n,"default",{enumerable:!0,value:e}),2&r&&"string"!=typeof e)for(var o in e)t.d(n,o,function(r){return e[r]}.bind(null,o));return n},t.n=function(e){var r=e&&e.__esModule?function(){return e.default}:function(){return e};return t.d(r,"a",r),r},t.o=function(e,r){return Object.prototype.hasOwnProperty.call(e,r)},t.p="",t(t.s=0)}([function(e,r,t){"use strict";function n(){let e=46656*Math.random()|0,r=46656*Math.random()|0;return(e=("000"+e.toString(36)).slice(-3))+(r=("000"+r.toString(36)).slice(-3))}function o(e){return JSON.stringify(e)}t.r(r),t.d(r,"register",function(){return d}),t.d(r,"unregister",function(){return v}),t.d(r,"call",function(){return h}),t.d(r,"callServer",function(){return b}),t.d(r,"callClient",function(){return j}),t.d(r,"callBrowsers",function(){return _}),t.d(r,"callBrowser",function(){return P});const c=function(){if(mp)return mp.joaat?"server":mp.game&&mp.game.joaat?"client":mp.trigger?"cef":void 0}();if(!c)throw"Unknown RAGE environment";const s="PROCEDURE_NOT_FOUND",i="__rpc:process",a="__rpc:exists",l={},u={};let f,m;async function p(e,r,t){if(!l[e])throw s;return l[e](r,t)}"client"===c&&(f=((e,r)=>{const t=o(r);e.execute(`var process = window["${i}"]; if(process){ process('${t}'); }else{ mp.trigger("${i}", '{"ret":1,"id":"${r.id}","err":"${s}"}'); }`)}),m=(e=>{mp.browsers.forEach(r=>f(r,e))}));const g=(...e)=>{let r=e[0];"server"===c&&(r=e[1]);const t=function(e){return JSON.parse(e)}(r);if(t.req){const r={id:t.id,environment:t.fenv||t.env};"server"===c&&(r.player=e[0]);const n=p(t.name,t.args,r),s={ret:1,id:t.id};switch(c){case"server":n.then(e=>{r.player.call(i,[o({...s,res:e})])}).catch(e=>{r.player.call(i,[o({...s,err:e})])});break;case"client":"server"===t.env?n.then(e=>{mp.events.callRemote(i,o({...s,res:e}))}).catch(e=>{mp.events.callRemote(i,o({...s,err:e}))}):"cef"===t.env&&n.then(e=>{m({...s,res:e})}).catch(e=>{m({...s,err:e})});break;case"cef":n.then(e=>{mp.trigger(i,o({...s,res:e}))}).catch(e=>{mp.trigger(i,o({...s,err:e}))})}}else if(t.ret){const e=u[t.id];e&&(t.err?e.reject(t.err):e.resolve(t.res),u[t.id]=void 0)}};function d(e,r){if(2!==arguments.length)throw'register expects 2 arguments: "name" and "cb"';l[e]=r}function v(e){if(1!==arguments.length)throw'unregister expects 1 argument: "name"';l[e]=void 0}function h(e,r){return 1!==arguments.length&&2!==arguments.length?Promise.reject('call expects 1 or 2 arguments: "name" and optional "args"'):p(e,r,{environment:c})}function w(e,r,t){switch(c){case"server":return h(e,r);case"client":{const s=n();return new Promise((n,a)=>{u[s]={resolve:n,reject:a},mp.events.callRemote(i,o({req:1,id:s,name:e,env:c,args:r,...t}))})}case"cef":return j("__rpc:callServer",[e,r])}}function b(e,r){return 1!==arguments.length&&2!==arguments.length?Promise.reject('callServer expects 1 or 2 arguments: "name" and optional "args"'):w(e,r,{})}function j(e,r,t){switch(c){case"client":return t=r,r=e,1!==arguments.length&&2!==arguments.length||"string"!=typeof r?Promise.reject('callClient from the client expects 1 or 2 arguments: "name" and optional "args"'):h(r,t);case"server":{if(2!==arguments.length&&3!==arguments.length||"object"!=typeof e)return Promise.reject('callClient from the server expects 2 or 3 arguments: "player", "name", and optional "args"');const s=n();return new Promise((n,a)=>{u[s]={resolve:n,reject:a},e.call(i,[o({req:1,id:s,name:r,env:c,args:t})])})}case"cef":{if(t=r,r=e,1!==arguments.length&&2!==arguments.length||"string"!=typeof r)return Promise.reject('callClient from the browser expects 1 or 2 arguments: "name" and optional "args"');const s=n();return new Promise((e,n)=>{u[s]={resolve:e,reject:n},mp.trigger(i,o({req:1,id:s,name:r,env:c,args:t}))})}}}function y(e,r,t,n,o){return new Promise((s,i)=>{u[e]={resolve:s,reject:i},f(r,{req:1,id:e,name:t,env:c,args:n,...o})})}async function x(e,r,t,o){switch(c){case"client":{t=r,r=e;const c=n(),i=mp.browsers.length;let l;for(let e=0;e<i;e++){const t=mp.browsers.at(e);if(await new Promise(e=>{const n=r=>{const o=r.split(":");o[0]===c&&+o[1]&&(l=t),mp.events.remove(a,n),e()};mp.events.add(a,n),t.execute(`var f = window["${a}"]; mp.trigger("${a}", "${c}:"+((f && f("${r}")) ? 1 : 0));`)}),l)break}Line truncated
|
||||
Generated
+3630
-2
File diff suppressed because it is too large.
Load diff
+7
-2
@@ -2,8 +2,9 @@
|
||||
"name": "rage-rpc",
|
||||
"version": "0.0.1",
|
||||
"description": "An asynchronous RPC implementation for RAGE Multiplayer",
|
||||
"main": "index.js",
|
||||
"main": "dist/rage-rpc.min.js",
|
||||
"scripts": {
|
||||
"build": "webpack-cli --config ./webpack.config.js",
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"repository": {
|
||||
@@ -16,5 +17,9 @@
|
||||
"url": "https://github.com/micaww/rage-rpc/issues"
|
||||
},
|
||||
"homepage": "https://github.com/micaww/rage-rpc#readme",
|
||||
"dependencies": {}
|
||||
"dependencies": {},
|
||||
"devDependencies": {
|
||||
"webpack-cli": "^3.1.2",
|
||||
"webpack": "^4.23.1"
|
||||
}
|
||||
}
|
||||
+32
-37
@@ -1,4 +1,4 @@
|
||||
const util = require('./util.js');
|
||||
import * as util from './util.js';
|
||||
|
||||
const environment = util.getEnvironment();
|
||||
if(!environment) throw 'Unknown RAGE environment';
|
||||
@@ -8,8 +8,6 @@ const ERR_NOT_FOUND = 'PROCEDURE_NOT_FOUND';
|
||||
const PROCESS_EVENT = '__rpc:process';
|
||||
const PROCEDURE_EXISTS = '__rpc:exists';
|
||||
|
||||
const rpc = {};
|
||||
|
||||
const listeners = {};
|
||||
const pending = {};
|
||||
|
||||
@@ -125,19 +123,19 @@ if(environment === "cef"){
|
||||
* @param {string} name - The name of the procedure.
|
||||
* @param {function} cb - The procedure's callback. The return value will be sent back to the caller.
|
||||
*/
|
||||
rpc.register = function(name, cb){
|
||||
export function register(name, cb){
|
||||
if(arguments.length !== 2) throw 'register expects 2 arguments: "name" and "cb"';
|
||||
listeners[name] = cb;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Unregister a procedure.
|
||||
* @param {string} name - The name of the procedure.
|
||||
*/
|
||||
rpc.unregister = function(name){
|
||||
export function unregister(name){
|
||||
if(arguments.length !== 1) throw 'unregister expects 1 argument: "name"';
|
||||
listeners[name] = undefined;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Calls a local procedure. Only procedures registered in the same context will be resolved.
|
||||
@@ -148,15 +146,15 @@ rpc.unregister = function(name){
|
||||
* @param args - Any parameters for the procedure.
|
||||
* @returns {Promise} - The result from the procedure.
|
||||
*/
|
||||
rpc.call = function(name, args){
|
||||
export function call(name, args){
|
||||
if(arguments.length !== 1 && arguments.length !== 2) return Promise.reject('call expects 1 or 2 arguments: "name" and optional "args"');
|
||||
return callProcedure(name, args, { environment });
|
||||
};
|
||||
}
|
||||
|
||||
function callServer(name, args, extraData){
|
||||
function _callServer(name, args, extraData){
|
||||
switch(environment){
|
||||
case "server": {
|
||||
return rpc.call(name, args);
|
||||
return call(name, args);
|
||||
}
|
||||
case "client": {
|
||||
const id = util.uid();
|
||||
@@ -176,7 +174,7 @@ function callServer(name, args, extraData){
|
||||
});
|
||||
}
|
||||
case "cef": {
|
||||
return rpc.callClient('__rpc:callServer', [name, args]);
|
||||
return callClient('__rpc:callServer', [name, args]);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -190,10 +188,10 @@ function callServer(name, args, extraData){
|
||||
* @param args - Any parameters for the procedure.
|
||||
* @returns {Promise} - The result from the procedure.
|
||||
*/
|
||||
rpc.callServer = function(name, args){
|
||||
export function callServer(name, args){
|
||||
if(arguments.length !== 1 && arguments.length !== 2) return Promise.reject('callServer expects 1 or 2 arguments: "name" and optional "args"');
|
||||
return callServer(name, args, {});
|
||||
};
|
||||
return _callServer(name, args, {});
|
||||
}
|
||||
|
||||
/**
|
||||
* Calls a remote procedure registered on the client.
|
||||
@@ -210,13 +208,13 @@ rpc.callServer = function(name, args){
|
||||
//
|
||||
// clientside or cef
|
||||
// callClient(name, args)
|
||||
rpc.callClient = function(player, name, args){
|
||||
export function callClient(player, name, args){
|
||||
switch(environment){
|
||||
case "client": {
|
||||
args = name;
|
||||
name = player;
|
||||
if((arguments.length !== 1 && arguments.length !== 2) || typeof name !== "string") return Promise.reject('callClient from the client expects 1 or 2 arguments: "name" and optional "args"');
|
||||
return rpc.call(name, args);
|
||||
return call(name, args);
|
||||
}
|
||||
case "server": {
|
||||
if((arguments.length !== 2 && arguments.length !== 3) || typeof player !== "object") return Promise.reject('callClient from the server expects 2 or 3 arguments: "player", "name", and optional "args"');
|
||||
@@ -255,9 +253,9 @@ rpc.callClient = function(player, name, args){
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
function callBrowser(id, browser, name, args, extraData){
|
||||
function _callBrowser(id, browser, name, args, extraData){
|
||||
return new Promise((resolve, reject) => {
|
||||
pending[id] = {
|
||||
resolve,
|
||||
@@ -273,7 +271,7 @@ function callBrowser(id, browser, name, args, extraData){
|
||||
});
|
||||
});
|
||||
}
|
||||
async function callBrowsers(player, name, args, extraData){
|
||||
async function _callBrowsers(player, name, args, extraData){
|
||||
switch(environment){
|
||||
case "client": {
|
||||
args = name;
|
||||
@@ -299,16 +297,16 @@ async function callBrowsers(player, name, args, extraData){
|
||||
});
|
||||
if(browser) break;
|
||||
}
|
||||
if(browser) return callBrowser(id, browser, name, args, extraData);
|
||||
if(browser) return _callBrowser(id, browser, name, args, extraData);
|
||||
throw ERR_NOT_FOUND;
|
||||
}
|
||||
case "server": {
|
||||
return rpc.callClient(player, '__rpc:callBrowsers', [name, args]);
|
||||
return callClient(player, '__rpc:callBrowsers', [name, args]);
|
||||
}
|
||||
case "cef": {
|
||||
args = name;
|
||||
name = player;
|
||||
return rpc.callClient('__rpc:callBrowsers', [name, args]);
|
||||
return callClient('__rpc:callBrowsers', [name, args]);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -323,7 +321,7 @@ async function callBrowsers(player, name, args, extraData){
|
||||
* @param args - Any parameters for the procedure.
|
||||
* @returns {Promise} - The result from the procedure.
|
||||
*/
|
||||
rpc.callBrowsers = function(player, name, args){
|
||||
export function callBrowsers(player, name, args){
|
||||
switch(environment){
|
||||
case "client":
|
||||
if(arguments.length !== 1 && arguments.length !== 2) return Promise.reject('callBrowsers from the client expects 1 or 2 arguments: "name" and optional "args"');
|
||||
@@ -335,8 +333,8 @@ rpc.callBrowsers = function(player, name, args){
|
||||
if(arguments.length !== 1 && arguments.length !== 2) return Promise.reject('callBrowsers from the browser expects 1 or 2 arguments: "name" and optional "args"');
|
||||
break;
|
||||
}
|
||||
return callBrowsers(player, name, args, {});
|
||||
};
|
||||
return _callBrowsers(player, name, args, {});
|
||||
}
|
||||
|
||||
/**
|
||||
* Calls a remote procedure registered in a specific browser instance.
|
||||
@@ -348,26 +346,23 @@ rpc.callBrowsers = function(player, name, args){
|
||||
* @param args - Any parameters for the procedure.
|
||||
* @returns {Promise} - The result from the procedure.
|
||||
*/
|
||||
rpc.callBrowser = function(browser, name, args){
|
||||
export function callBrowser(browser, name, args){
|
||||
if(environment !== "client") return Promise.reject('callBrowser can only be used in the client environment');
|
||||
if(arguments.length !== 2 && arguments.length !== 3) return Promise.reject('callBrowser expects 2 or 3 arguments: "browser", "name", and optional "args"');
|
||||
const id = util.uid();
|
||||
return callBrowser(id, browser, name, args, {});
|
||||
};
|
||||
return _callBrowser(id, browser, name, args, {});
|
||||
}
|
||||
|
||||
// set up internal pass-through events
|
||||
if(environment === "client"){
|
||||
rpc.register('__rpc:callServer', ([name, args], info) => {
|
||||
return callServer(name, args, {
|
||||
register('__rpc:callServer', ([name, args], info) => {
|
||||
return _callServer(name, args, {
|
||||
fenv: info.environment
|
||||
});
|
||||
});
|
||||
|
||||
rpc.register('__rpc:callBrowsers', ([name, args], info) => {
|
||||
return callBrowsers(name, args, null, {
|
||||
register('__rpc:callBrowsers', ([name, args], info) => {
|
||||
return _callBrowsers(name, args, null, {
|
||||
fenv: info.environment
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = rpc;
|
||||
}
|
||||
+8
-12
@@ -1,26 +1,22 @@
|
||||
const util = {};
|
||||
|
||||
util.uid = () => {
|
||||
export function uid(){
|
||||
let firstPart = (Math.random() * 46656) | 0;
|
||||
let secondPart = (Math.random() * 46656) | 0;
|
||||
firstPart = ('000' + firstPart.toString(36)).slice(-3);
|
||||
secondPart = ('000' + secondPart.toString(36)).slice(-3);
|
||||
return firstPart + secondPart;
|
||||
};
|
||||
}
|
||||
|
||||
util.getEnvironment = () => {
|
||||
export function getEnvironment(){
|
||||
if(!mp) return undefined;
|
||||
if(mp.joaat) return 'server';
|
||||
else if(mp.game && mp.game.joaat) return 'client';
|
||||
else if(mp.trigger) return 'cef';
|
||||
};
|
||||
}
|
||||
|
||||
util.stringifyData = (data) => {
|
||||
export function stringifyData(data){
|
||||
return JSON.stringify(data);
|
||||
};
|
||||
}
|
||||
|
||||
util.parseData = (data) => {
|
||||
export function parseData(data){
|
||||
return JSON.parse(data);
|
||||
};
|
||||
|
||||
module.exports = util;
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
const path = require('path');
|
||||
|
||||
module.exports = {
|
||||
entry: './src/index.js',
|
||||
mode: 'production',
|
||||
output: {
|
||||
path: path.resolve(__dirname, 'dist'),
|
||||
filename: 'rage-rpc.min.js',
|
||||
library: 'rpc',
|
||||
libraryTarget: 'umd'
|
||||
}
|
||||
};
|
||||
Reference in new issue
Block a user