@phiresky/redis-remotify
Advanced tools
Comparing version 0.1.9 to 0.2.1
import * as redis from "redis"; | ||
export { redis }; | ||
export declare function getAllRelevantFunctions<T, A extends keyof T>(obj: T): A[]; | ||
export declare class RemotifyListen { | ||
export declare function getAllRelevantFunctions<T, A extends Extract<keyof T, string>>(obj: T): A[]; | ||
export declare class Listen { | ||
private serverid; | ||
@@ -15,3 +15,3 @@ pubClient: redis.RedisClient; | ||
remotifyListen(fn: (...args: any[]) => any, fnname?: string): void; | ||
remotifyListenAll<T>(obj: T, prefix: string): void; | ||
remotifyListenAll<T>(obj: T, prefix?: string): void; | ||
} | ||
@@ -29,7 +29,7 @@ export declare class Remotify { | ||
cbCounter: number; | ||
constructor(serverid: string, clientid: string, clients: { | ||
constructor(serverid: string, clients: { | ||
pub: redis.RedisClient; | ||
sub: redis.RedisClient; | ||
}, callbackTimeout?: number); | ||
private addCallback<T>(); | ||
}, clientid?: string, callbackTimeout?: number); | ||
private addCallback; | ||
onCallback: (ns: string, str: string) => void; | ||
@@ -42,2 +42,4 @@ remotify<T>(fnname: string): T; | ||
remotifyAll<T>(prefix: string, fnnames?: string[] | null): T; | ||
remotifyFunction<T extends (...args: any[]) => any>(fn: T): T; | ||
remotifyClass<T>(cls: new (...args: any[]) => T, prefix?: string): T; | ||
} | ||
@@ -44,0 +46,0 @@ export declare class RedisEventPublisher<T extends { |
@@ -15,2 +15,3 @@ "use strict"; | ||
const util_1 = require("./util"); | ||
const crypto = require("crypto"); | ||
const debug = _debug("remotify"); | ||
@@ -40,3 +41,3 @@ function prepareJsonify(e) { | ||
const timedout = Symbol("timedout"); | ||
class RemotifyListen { | ||
class Listen { | ||
constructor(serverid, clients) { | ||
@@ -90,3 +91,3 @@ this.serverid = serverid; | ||
} | ||
remotifyListenAll(obj, prefix) { | ||
remotifyListenAll(obj, prefix = obj.constructor.name) { | ||
for (const unbound of getAllRelevantFunctions(obj)) { | ||
@@ -97,3 +98,3 @@ this.remotifyListen(obj[unbound].bind(obj), prefix + "." + unbound); | ||
} | ||
exports.RemotifyListen = RemotifyListen; | ||
exports.Listen = Listen; | ||
const reservedNamesArray = [ | ||
@@ -104,4 +105,7 @@ ...Object.getOwnPropertyNames(Object.prototype), | ||
const reservedNames = Object.assign({}, ...reservedNamesArray.map(k => ({ [k]: true }))); | ||
function randomid() { | ||
return crypto.randomBytes(20).toString("hex"); | ||
} | ||
class Remotify { | ||
constructor(serverid, clientid, clients, callbackTimeout = 10000) { | ||
constructor(serverid, clients, clientid = randomid(), callbackTimeout = 10000) { | ||
this.serverid = serverid; | ||
@@ -166,8 +170,8 @@ this.clientid = clientid; | ||
rej(err); | ||
else if (listenedCount === 0) | ||
rej({ | ||
cause: "remotifyBackendDown", | ||
message: this.serverid + | ||
" backend is down or method does not exist", | ||
}); | ||
else if (listenedCount === 0) { | ||
const error = new Error(this.serverid + | ||
" backend is down or method does not exist"); | ||
error.cause = "remotifyBackendDown"; | ||
rej(error); | ||
} | ||
else | ||
@@ -204,6 +208,6 @@ return res(); | ||
get: (_, fnname) => { | ||
if (typeof fnname === "symbol") | ||
return undefined; | ||
if (reservedNames[fnname]) | ||
return undefined; | ||
if (typeof fnname === "symbol") | ||
return undefined; | ||
return this.remotify(prefix + "." + fnname); | ||
@@ -221,2 +225,9 @@ }, | ||
} | ||
remotifyFunction(fn) { | ||
return this.remotify(fn.name); | ||
} | ||
remotifyClass(cls, prefix = cls.name) { | ||
const fns = getAllRelevantFunctions(cls.prototype); | ||
return this.remotifyAll(prefix, fns); | ||
} | ||
} | ||
@@ -223,0 +234,0 @@ exports.Remotify = Remotify; |
@@ -37,9 +37,12 @@ "use strict"; | ||
if (process.argv[2] === "b") { | ||
const r = new remotify_1.Remotify("remotifytest", "cli-" + Math.random(), { | ||
const r = new remotify_1.Remotify("remotifytest", { | ||
pub, | ||
sub, | ||
}); | ||
const squareR = r.remotify(square.name); | ||
const squareR = r.remotifyFunction(square); | ||
// do it by name (this way the function won't be imported into your client process at all) | ||
const testMultiR = r.remotify(testMulti.name); | ||
const tester = r.remotifyAll("tester", ["test1", "test2"]); | ||
const tester = r.remotifyClass(Tester); | ||
// same thing but allowing typescript to eliminate the class import for runtime | ||
// const tester = r.remotifyAll<Tester>("tester", ["test1", "test2"]); | ||
for (const x of [1, 2, 3, 4, 5, 11]) { | ||
@@ -59,3 +62,3 @@ try { | ||
const t = new Tester(); | ||
const r = new remotify_1.RemotifyListen("rr", { pub, sub }); | ||
const r = new remotify_1.Listen("rr", { pub, sub }); | ||
r.remotifyListen(square); | ||
@@ -62,0 +65,0 @@ r.remotifyListen(testMulti); |
{ | ||
"name": "@phiresky/redis-remotify", | ||
"version": "0.1.9", | ||
"version": "0.2.1", | ||
"description": "a tiny library for remote calls and events via redis", | ||
@@ -34,3 +34,7 @@ "main": "bin/index.js", | ||
"husky": "^0.14.3", | ||
"lint-staged": "^6.0.0" | ||
"lint-staged": "^6.0.0", | ||
"prettier": "^1.15.3", | ||
"redis": "^2.8.0", | ||
"ts-node": "^7.0.1", | ||
"typescript": "^3.2.1" | ||
}, | ||
@@ -37,0 +41,0 @@ "dependencies": { |
# redis-remotify | ||
A tiny library for remote calls and events via redis. | ||
Install via npm: | ||
@@ -7,4 +9,44 @@ | ||
A tiny library for remote calls and events via redis. | ||
## Example use | ||
Example use can be seen in [src/test.ts](src/test.ts). | ||
Say you have a backend like this: | ||
```typescript | ||
class Squarer { | ||
public async square(x: number) { | ||
// maybe do some more interesting stuff here | ||
return x ** 2; | ||
} | ||
} | ||
``` | ||
But you want to use the functions it provides in different processes. With this library, you can start a server process like this: | ||
```typescript | ||
import * as redis from "redis"; | ||
import { Listen } from "@phiresky/redis-remotify"; | ||
const squarer = new Squarer(); | ||
const pub = redis.createClient(); | ||
const sub = redis.createClient(); | ||
const r = new Listen("backend", { pub, sub }); | ||
// add methods to RPC interface with a name like Squarer.square() | ||
r.listenAll(squarer); | ||
``` | ||
And then call the functions from one or more separate node processes like this: | ||
```typescript | ||
import { Remotify } from "@phiresky/redis-remotify"; | ||
const backend = new Remotify("backend", { pub, sub }); | ||
const remoteSquarer = backend.remotifyClass(Squarer); | ||
for (const x of [1, 2, 3, 4, 5, 11]) { | ||
const res = await remoteSquarer.square(x); | ||
// typeof res is number as expected | ||
console.log(x, "^2 =", res); | ||
} | ||
``` | ||
Of course it also works with single functions or other non-class objects. A more complete example can be seen in [examples/elaborate.ts](examples/elaborate.ts). |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
59757
32
935
52
8