Add basic webpack build
This commit is contained in:
parent
b628bb9ee9
commit
d40908d8d0
1
dist/rage-rpc.min.js
vendored
Normal file
1
dist/rage-rpc.min.js
vendored
Normal file
File diff suppressed because one or more lines are too long
3632
package-lock.json
generated
3632
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -2,8 +2,9 @@
|
|||||||
"name": "rage-rpc",
|
"name": "rage-rpc",
|
||||||
"version": "0.0.1",
|
"version": "0.0.1",
|
||||||
"description": "An asynchronous RPC implementation for RAGE Multiplayer",
|
"description": "An asynchronous RPC implementation for RAGE Multiplayer",
|
||||||
"main": "index.js",
|
"main": "dist/rage-rpc.min.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
"build": "webpack-cli --config ./webpack.config.js",
|
||||||
"test": "echo \"Error: no test specified\" && exit 1"
|
"test": "echo \"Error: no test specified\" && exit 1"
|
||||||
},
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
@ -16,5 +17,9 @@
|
|||||||
"url": "https://github.com/micaww/rage-rpc/issues"
|
"url": "https://github.com/micaww/rage-rpc/issues"
|
||||||
},
|
},
|
||||||
"homepage": "https://github.com/micaww/rage-rpc#readme",
|
"homepage": "https://github.com/micaww/rage-rpc#readme",
|
||||||
"dependencies": {}
|
"dependencies": {},
|
||||||
|
"devDependencies": {
|
||||||
|
"webpack-cli": "^3.1.2",
|
||||||
|
"webpack": "^4.23.1"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
69
src/index.js
69
src/index.js
@ -1,4 +1,4 @@
|
|||||||
const util = require('./util.js');
|
import * as util from './util.js';
|
||||||
|
|
||||||
const environment = util.getEnvironment();
|
const environment = util.getEnvironment();
|
||||||
if(!environment) throw 'Unknown RAGE environment';
|
if(!environment) throw 'Unknown RAGE environment';
|
||||||
@ -8,8 +8,6 @@ const ERR_NOT_FOUND = 'PROCEDURE_NOT_FOUND';
|
|||||||
const PROCESS_EVENT = '__rpc:process';
|
const PROCESS_EVENT = '__rpc:process';
|
||||||
const PROCEDURE_EXISTS = '__rpc:exists';
|
const PROCEDURE_EXISTS = '__rpc:exists';
|
||||||
|
|
||||||
const rpc = {};
|
|
||||||
|
|
||||||
const listeners = {};
|
const listeners = {};
|
||||||
const pending = {};
|
const pending = {};
|
||||||
|
|
||||||
@ -125,19 +123,19 @@ if(environment === "cef"){
|
|||||||
* @param {string} name - The name of the procedure.
|
* @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.
|
* @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"';
|
if(arguments.length !== 2) throw 'register expects 2 arguments: "name" and "cb"';
|
||||||
listeners[name] = cb;
|
listeners[name] = cb;
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Unregister a procedure.
|
* Unregister a procedure.
|
||||||
* @param {string} name - The name of the 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"';
|
if(arguments.length !== 1) throw 'unregister expects 1 argument: "name"';
|
||||||
listeners[name] = undefined;
|
listeners[name] = undefined;
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Calls a local procedure. Only procedures registered in the same context will be resolved.
|
* 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.
|
* @param args - Any parameters for the procedure.
|
||||||
* @returns {Promise} - The result from 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"');
|
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 });
|
return callProcedure(name, args, { environment });
|
||||||
};
|
}
|
||||||
|
|
||||||
function callServer(name, args, extraData){
|
function _callServer(name, args, extraData){
|
||||||
switch(environment){
|
switch(environment){
|
||||||
case "server": {
|
case "server": {
|
||||||
return rpc.call(name, args);
|
return call(name, args);
|
||||||
}
|
}
|
||||||
case "client": {
|
case "client": {
|
||||||
const id = util.uid();
|
const id = util.uid();
|
||||||
@ -176,7 +174,7 @@ function callServer(name, args, extraData){
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
case "cef": {
|
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.
|
* @param args - Any parameters for the procedure.
|
||||||
* @returns {Promise} - The result from 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"');
|
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.
|
* Calls a remote procedure registered on the client.
|
||||||
@ -210,13 +208,13 @@ rpc.callServer = function(name, args){
|
|||||||
//
|
//
|
||||||
// clientside or cef
|
// clientside or cef
|
||||||
// callClient(name, args)
|
// callClient(name, args)
|
||||||
rpc.callClient = function(player, name, args){
|
export function callClient(player, name, args){
|
||||||
switch(environment){
|
switch(environment){
|
||||||
case "client": {
|
case "client": {
|
||||||
args = name;
|
args = name;
|
||||||
name = player;
|
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"');
|
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": {
|
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"');
|
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) => {
|
return new Promise((resolve, reject) => {
|
||||||
pending[id] = {
|
pending[id] = {
|
||||||
resolve,
|
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){
|
switch(environment){
|
||||||
case "client": {
|
case "client": {
|
||||||
args = name;
|
args = name;
|
||||||
@ -299,16 +297,16 @@ async function callBrowsers(player, name, args, extraData){
|
|||||||
});
|
});
|
||||||
if(browser) break;
|
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;
|
throw ERR_NOT_FOUND;
|
||||||
}
|
}
|
||||||
case "server": {
|
case "server": {
|
||||||
return rpc.callClient(player, '__rpc:callBrowsers', [name, args]);
|
return callClient(player, '__rpc:callBrowsers', [name, args]);
|
||||||
}
|
}
|
||||||
case "cef": {
|
case "cef": {
|
||||||
args = name;
|
args = name;
|
||||||
name = player;
|
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.
|
* @param args - Any parameters for the procedure.
|
||||||
* @returns {Promise} - The result from the procedure.
|
* @returns {Promise} - The result from the procedure.
|
||||||
*/
|
*/
|
||||||
rpc.callBrowsers = function(player, name, args){
|
export function callBrowsers(player, name, args){
|
||||||
switch(environment){
|
switch(environment){
|
||||||
case "client":
|
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"');
|
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"');
|
if(arguments.length !== 1 && arguments.length !== 2) return Promise.reject('callBrowsers from the browser expects 1 or 2 arguments: "name" and optional "args"');
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
return callBrowsers(player, name, args, {});
|
return _callBrowsers(player, name, args, {});
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Calls a remote procedure registered in a specific browser instance.
|
* 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.
|
* @param args - Any parameters for the procedure.
|
||||||
* @returns {Promise} - The result from 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(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"');
|
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();
|
const id = util.uid();
|
||||||
return callBrowser(id, browser, name, args, {});
|
return _callBrowser(id, browser, name, args, {});
|
||||||
};
|
}
|
||||||
|
|
||||||
// set up internal pass-through events
|
// set up internal pass-through events
|
||||||
if(environment === "client"){
|
if(environment === "client"){
|
||||||
rpc.register('__rpc:callServer', ([name, args], info) => {
|
register('__rpc:callServer', ([name, args], info) => {
|
||||||
return callServer(name, args, {
|
return _callServer(name, args, {
|
||||||
fenv: info.environment
|
fenv: info.environment
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
register('__rpc:callBrowsers', ([name, args], info) => {
|
||||||
rpc.register('__rpc:callBrowsers', ([name, args], info) => {
|
return _callBrowsers(name, args, null, {
|
||||||
return callBrowsers(name, args, null, {
|
|
||||||
fenv: info.environment
|
fenv: info.environment
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = rpc;
|
|
20
src/util.js
20
src/util.js
@ -1,26 +1,22 @@
|
|||||||
const util = {};
|
export function uid(){
|
||||||
|
|
||||||
util.uid = () => {
|
|
||||||
let firstPart = (Math.random() * 46656) | 0;
|
let firstPart = (Math.random() * 46656) | 0;
|
||||||
let secondPart = (Math.random() * 46656) | 0;
|
let secondPart = (Math.random() * 46656) | 0;
|
||||||
firstPart = ('000' + firstPart.toString(36)).slice(-3);
|
firstPart = ('000' + firstPart.toString(36)).slice(-3);
|
||||||
secondPart = ('000' + secondPart.toString(36)).slice(-3);
|
secondPart = ('000' + secondPart.toString(36)).slice(-3);
|
||||||
return firstPart + secondPart;
|
return firstPart + secondPart;
|
||||||
};
|
}
|
||||||
|
|
||||||
util.getEnvironment = () => {
|
export function getEnvironment(){
|
||||||
if(!mp) return undefined;
|
if(!mp) return undefined;
|
||||||
if(mp.joaat) return 'server';
|
if(mp.joaat) return 'server';
|
||||||
else if(mp.game && mp.game.joaat) return 'client';
|
else if(mp.game && mp.game.joaat) return 'client';
|
||||||
else if(mp.trigger) return 'cef';
|
else if(mp.trigger) return 'cef';
|
||||||
};
|
}
|
||||||
|
|
||||||
util.stringifyData = (data) => {
|
export function stringifyData(data){
|
||||||
return JSON.stringify(data);
|
return JSON.stringify(data);
|
||||||
};
|
}
|
||||||
|
|
||||||
util.parseData = (data) => {
|
export function parseData(data){
|
||||||
return JSON.parse(data);
|
return JSON.parse(data);
|
||||||
};
|
}
|
||||||
|
|
||||||
module.exports = util;
|
|
12
webpack.config.js
Normal file
12
webpack.config.js
Normal file
@ -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