Comparing version 1.0.1 to 1.0.2
@@ -8,5 +8,5 @@ "use strict"; | ||
const events_1 = require("events"); | ||
const shortid_1 = require("shortid"); | ||
const isSocketResetError = require("is-socket-reset-error"); | ||
const util_1 = require("./util"); | ||
var oid = 0; | ||
class ServiceInstance { | ||
@@ -53,2 +53,3 @@ constructor() { | ||
for (let [event, ...data] of util_1.receive(buf)) { | ||
event = isNaN(event) ? event : util_1.RPCEvents[event]; | ||
socket.emit(event, ...data); | ||
@@ -60,20 +61,20 @@ } | ||
} | ||
}).on("rpc-connect", (oid, name, id, ...args) => { | ||
}).on(util_1.RPCEvents[0], (oid, name, id, ...args) => { | ||
if (this.services[id]) { | ||
this.instances[oid] = new this.services[id](...args); | ||
socket.write(util_1.send("rpc-connected", oid, id)); | ||
socket.write(util_1.send(util_1.RPCEvents.CONNECTED, oid, id)); | ||
} | ||
else { | ||
let err = new Error(`service '${name}' not registered`); | ||
socket.write(util_1.send("rpc-connect-error", oid, err)); | ||
socket.write(util_1.send(util_1.RPCEvents.CONNECT_ERROR, oid, err)); | ||
} | ||
}).on("rpc-disconnect", (oid) => { | ||
}).on(util_1.RPCEvents[3], (oid) => { | ||
delete this.instances[oid]; | ||
}).on("rpc-request", (oid, taskId, method, ...args) => tslib_1.__awaiter(this, void 0, void 0, function* () { | ||
}).on(util_1.RPCEvents[4], (oid, taskId, method, ...args) => tslib_1.__awaiter(this, void 0, void 0, function* () { | ||
try { | ||
let service = this.instances[oid], res = yield service[method](...args); | ||
yield new Promise(resolve => socket.write(util_1.send("rpc-response", oid, taskId, res), () => resolve())); | ||
yield new Promise(resolve => socket.write(util_1.send(util_1.RPCEvents.RESPONSE, oid, taskId, res), () => resolve())); | ||
} | ||
catch (err) { | ||
socket.write(util_1.send("rpc-error", oid, taskId, err)); | ||
socket.write(util_1.send(util_1.RPCEvents.ERROR, oid, taskId, err)); | ||
} | ||
@@ -133,2 +134,3 @@ })); | ||
for (let [event, oid, ...data] of util_1.receive(buf)) { | ||
event = isNaN(event) ? event : util_1.RPCEvents[event]; | ||
this.instances[oid][util_1.eventEmitter].emit(event, ...data); | ||
@@ -144,17 +146,21 @@ } | ||
let srv = new target(...args); | ||
let oid = srv[util_1.objectId] = shortid_1.generate(); | ||
let _oid = oid; | ||
let clsId = srv[util_1.classId] = target[util_1.classId] | ||
|| (target[util_1.classId] = util_1.getClassId(target)); | ||
srv[util_1.objectId] = oid; | ||
srv[util_1.eventEmitter] = new events_1.EventEmitter; | ||
this.instances[oid] = srv; | ||
this.client.write(util_1.send("rpc-connect", oid, target.name, clsId, ...args)); | ||
srv[util_1.eventEmitter].once("rpc-connected", () => { | ||
resolve(util_1.proxify(srv, oid, this)); | ||
}).once("rpc-connect-error", (err) => { | ||
this.client.write(util_1.send(util_1.RPCEvents.CONNECT, oid, target.name, clsId, ...args)); | ||
srv[util_1.eventEmitter].once(util_1.RPCEvents[1], () => { | ||
resolve(util_1.proxify(srv, _oid, this)); | ||
}).once(util_1.RPCEvents[2], (err) => { | ||
reject(err); | ||
}).on("rpc-response", (taskId, res) => { | ||
}).on(util_1.RPCEvents[5], (taskId, res) => { | ||
util_1.tasks[taskId].resolve(res); | ||
}).on("rpc-error", (taskId, err) => { | ||
}).on(util_1.RPCEvents[6], (taskId, err) => { | ||
util_1.tasks[taskId].reject(err); | ||
}); | ||
oid++; | ||
if (oid === Number.MAX_SAFE_INTEGER) | ||
oid = 0; | ||
}); | ||
@@ -169,3 +175,3 @@ }); | ||
delete this.instances[oid]; | ||
this.client.write(util_1.send("rpc-disconnect", oid), () => { | ||
this.client.write(util_1.send(util_1.RPCEvents.DISCONNECT, oid), () => { | ||
resolve(); | ||
@@ -172,0 +178,0 @@ }); |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const shortid_1 = require("shortid"); | ||
const hash = require("object-hash"); | ||
@@ -12,3 +11,14 @@ const path = require("path"); | ||
exports.tasks = {}; | ||
var RPCEvents; | ||
(function (RPCEvents) { | ||
RPCEvents[RPCEvents["CONNECT"] = 0] = "CONNECT"; | ||
RPCEvents[RPCEvents["CONNECTED"] = 1] = "CONNECTED"; | ||
RPCEvents[RPCEvents["CONNECT_ERROR"] = 2] = "CONNECT_ERROR"; | ||
RPCEvents[RPCEvents["DISCONNECT"] = 3] = "DISCONNECT"; | ||
RPCEvents[RPCEvents["REQUEST"] = 4] = "REQUEST"; | ||
RPCEvents[RPCEvents["RESPONSE"] = 5] = "RESPONSE"; | ||
RPCEvents[RPCEvents["ERROR"] = 6] = "ERROR"; | ||
})(RPCEvents = exports.RPCEvents || (exports.RPCEvents = {})); | ||
const proxified = Symbol("proxified"); | ||
var taskId = 0; | ||
function getClassId(target) { | ||
@@ -18,5 +28,5 @@ return hash(target).slice(0, 8); | ||
exports.getClassId = getClassId; | ||
function send(event, uniqid, ...data) { | ||
function send(event, id, ...data) { | ||
return Buffer.concat([ | ||
encoded_buffer_1.encode([event, uniqid, ...data]), | ||
encoded_buffer_1.encode([event, id, ...data]), | ||
Buffer.from("\r\n\r\n") | ||
@@ -35,3 +45,3 @@ ]); | ||
exports.receive = receive; | ||
function proxify(srv, srvId, ins) { | ||
function proxify(srv, oid, ins) { | ||
return new Proxy(srv, { | ||
@@ -46,3 +56,2 @@ get: (srv, prop) => { | ||
return new Promise((resolve, reject) => { | ||
let taskId = shortid_1.generate(); | ||
let timer = setTimeout(() => { | ||
@@ -52,3 +61,3 @@ let num = Math.round(ins.timeout / 1000), unit = num === 1 ? "second" : "seconds"; | ||
}, ins.timeout); | ||
ins["client"].write(send("rpc-request", srvId, taskId, prop, ...args)); | ||
ins["client"].write(send(RPCEvents.REQUEST, oid, taskId, prop, ...args)); | ||
exports.tasks[taskId] = { | ||
@@ -66,2 +75,5 @@ resolve: (res) => { | ||
}; | ||
taskId++; | ||
if (taskId === Number.MAX_SAFE_INTEGER) | ||
taskId = 0; | ||
}); | ||
@@ -68,0 +80,0 @@ }; |
{ | ||
"name": "asrpc", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "A tool to make your class as an RPC service.", | ||
@@ -26,4 +26,2 @@ "main": "dist/index.js", | ||
"@types/fs-extra": "^5.0.4", | ||
"@types/node": "^10.12.0", | ||
"@types/shortid": "0.0.29", | ||
"encoded-buffer": "^0.2.1", | ||
@@ -33,6 +31,6 @@ "fs-extra": "^7.0.0", | ||
"object-hash": "^1.3.0", | ||
"shortid": "^2.2.13", | ||
"tslib": "^1.9.3" | ||
}, | ||
"devDependencies": { | ||
"@types/node": "^10.12.6", | ||
"@types/object-hash": "^1.2.0", | ||
@@ -39,0 +37,0 @@ "source-map-support": "^0.5.9" |
@@ -191,2 +191,6 @@ # AS-RPC | ||
The socket channel sends minimal data (numbers starts from `0`) to explain which | ||
service and method is being called, along with user input data, to provide the | ||
best performance of data transmission. | ||
## Supported Data Types | ||
@@ -197,3 +201,3 @@ | ||
package uses [encoded-buffer](https://github.com/hyurl/encoded-buffer) to | ||
transfer data in socket, it supports many common types (more than `JSON`, | ||
transfer data in socket, it supports many common types (more than `JSON` and | ||
`BSON`), currently, these types are supported: | ||
@@ -241,3 +245,3 @@ | ||
So it's recommended the properties, except constants or `readonly` properties, | ||
So it's recommended for properties, except constants or `readonly` properties, | ||
should be set private or protected, since they should only be accessed inside | ||
@@ -244,0 +248,0 @@ the class itself. There is not point you will access those properties on the |
@@ -5,3 +5,2 @@ import * as net from "net"; | ||
import { EventEmitter } from "events"; | ||
import { generate as uniqid } from "shortid"; | ||
import isSocketResetError = require("is-socket-reset-error"); | ||
@@ -17,3 +16,4 @@ import { | ||
absPath, | ||
classId | ||
classId, | ||
RPCEvents | ||
} from './util'; | ||
@@ -30,2 +30,4 @@ | ||
var oid = 0; | ||
/** A type that represents a service instance shipped on the server. */ | ||
@@ -44,3 +46,3 @@ export class ServiceInstance implements ServiceOptions { | ||
private instances: { | ||
[oid: string]: any | ||
[oid: number]: any | ||
} = {}; | ||
@@ -58,4 +60,4 @@ | ||
/** | ||
* De-registers the target class bound by `register()`. Once a class is | ||
* de-registered, it can no longer be connected on the client. | ||
* Deregisters the target class bound by `register()`. Once a class is | ||
* deregistered, it can no longer be connected on the client. | ||
*/ | ||
@@ -102,3 +104,4 @@ deregister<T>(target: ServiceClass<T>): void { | ||
for (let [event, ...data] of receive(buf)) { | ||
socket.emit(event, ...data); | ||
event = isNaN(<any>event) ? event : RPCEvents[event]; | ||
socket.emit(<string>event, ...data); | ||
} | ||
@@ -109,4 +112,4 @@ }).on("error", err => { | ||
} | ||
}).on("rpc-connect", ( | ||
oid: string, | ||
}).on(RPCEvents[0], ( | ||
oid: number, | ||
name: string, | ||
@@ -118,12 +121,12 @@ id: string, | ||
this.instances[oid] = new this.services[id](...args); | ||
socket.write(send("rpc-connected", oid, id)); | ||
socket.write(send(RPCEvents.CONNECTED, oid, id)); | ||
} else { | ||
let err = new Error(`service '${name}' not registered`); | ||
socket.write(send("rpc-connect-error", oid, err)); | ||
socket.write(send(RPCEvents.CONNECT_ERROR, oid, err)); | ||
} | ||
}).on("rpc-disconnect", (oid: string) => { | ||
}).on(RPCEvents[3], (oid: number) => { | ||
delete this.instances[oid]; | ||
}).on("rpc-request", async ( | ||
oid: string, | ||
taskId: string, | ||
}).on(RPCEvents[4], async ( | ||
oid: number, | ||
taskId: number, | ||
method: string, | ||
@@ -137,7 +140,7 @@ ...args | ||
await new Promise(resolve => socket.write( | ||
send("rpc-response", oid, taskId, res), | ||
send(RPCEvents.RESPONSE, oid, taskId, res), | ||
() => resolve() | ||
)); | ||
} catch (err) { | ||
socket.write(send("rpc-error", oid, taskId, err)); | ||
socket.write(send(RPCEvents.ERROR, oid, taskId, err)); | ||
} | ||
@@ -209,2 +212,3 @@ }); | ||
for (let [event, oid, ...data] of receive(buf)) { | ||
event = isNaN(<any>event) ? event : RPCEvents[event]; | ||
this.instances[oid][eventEmitter].emit(event, ...data); | ||
@@ -219,21 +223,27 @@ } | ||
let srv = new target(...args); | ||
let oid = srv[objectId] = uniqid(); | ||
let clsId = srv[classId] = target[classId] | ||
let _oid = oid; | ||
let clsId: string = srv[classId] = target[classId] | ||
|| (target[classId] = getClassId(target)); | ||
srv[objectId] = oid; | ||
srv[eventEmitter] = new EventEmitter; | ||
this.instances[oid] = srv; | ||
this.client.write( | ||
send("rpc-connect", oid, target.name, clsId, ...args) | ||
send(RPCEvents.CONNECT, oid, target.name, clsId, ...args) | ||
); | ||
srv[eventEmitter].once("rpc-connected", () => { | ||
resolve(proxify(srv, oid, this)); | ||
}).once("rpc-connect-error", (err: any) => { | ||
srv[eventEmitter].once(RPCEvents[1], () => { | ||
resolve(proxify(srv, _oid, this)); | ||
}).once(RPCEvents[2], (err: any) => { | ||
reject(err); | ||
}).on("rpc-response", (taskId: string, res: any) => { | ||
}).on(RPCEvents[5], (taskId: number, res: any) => { | ||
tasks[taskId].resolve(res); | ||
}).on("rpc-error", (taskId: string, err: any) => { | ||
}).on(RPCEvents[6], (taskId: number, err: any) => { | ||
tasks[taskId].reject(err); | ||
}); | ||
oid++; | ||
if (oid === Number.MAX_SAFE_INTEGER) | ||
oid = 0; | ||
}); | ||
@@ -254,3 +264,3 @@ }); | ||
delete this.instances[oid]; | ||
this.client.write(send("rpc-disconnect", oid), () => { | ||
this.client.write(send(RPCEvents.DISCONNECT, oid), () => { | ||
resolve(); | ||
@@ -257,0 +267,0 @@ }); |
@@ -1,2 +0,1 @@ | ||
import { generate as uniqid } from "shortid"; | ||
import hash = require("object-hash"); | ||
@@ -12,3 +11,3 @@ import * as path from "path"; | ||
export const tasks: { | ||
[uniqid: string]: { | ||
[id: number]: { | ||
resolve: (res) => void, | ||
@@ -19,3 +18,14 @@ reject: (err) => void | ||
export enum RPCEvents { | ||
CONNECT, | ||
CONNECTED, | ||
CONNECT_ERROR, | ||
DISCONNECT, | ||
REQUEST, | ||
RESPONSE, | ||
ERROR, | ||
} | ||
const proxified = Symbol("proxified"); | ||
var taskId = 0; | ||
@@ -26,5 +36,5 @@ export function getClassId<T>(target: ServiceClass<T>): string { | ||
export function send(event: string, uniqid: string, ...data: any[]) { | ||
export function send(event: string | number, id: string | number, ...data: any[]) { | ||
return Buffer.concat([ | ||
encode([event, uniqid, ...data]), | ||
encode([event, id, ...data]), | ||
Buffer.from("\r\n\r\n") | ||
@@ -34,3 +44,3 @@ ]); | ||
export function receive(buf: Buffer): Array<[string, string, any]> { | ||
export function receive(buf: Buffer): Array<[string | number, string | number, any]> { | ||
let pack = splitBuffer(buf, "\r\n\r\n"), | ||
@@ -46,3 +56,3 @@ parts = []; | ||
export function proxify(srv: any, srvId: string, ins: ServiceInstance): any { | ||
export function proxify(srv: any, oid: number, ins: ServiceInstance): any { | ||
return new Proxy(srv, { | ||
@@ -56,3 +66,2 @@ get: (srv, prop: string) => { | ||
return new Promise((resolve, reject) => { | ||
let taskId = uniqid(); | ||
let timer = setTimeout(() => { | ||
@@ -68,3 +77,3 @@ let num = Math.round(ins.timeout / 1000), | ||
ins["client"].write( | ||
send("rpc-request", srvId, taskId, prop, ...args) | ||
send(RPCEvents.REQUEST, oid, taskId, prop, ...args) | ||
); | ||
@@ -83,2 +92,6 @@ tasks[taskId] = { | ||
}; | ||
taskId++; | ||
if (taskId === Number.MAX_SAFE_INTEGER) | ||
taskId = 0; | ||
}); | ||
@@ -85,0 +98,0 @@ }; |
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
53979
6
815
278
3
+ Added@types/node@22.10.2(transitive)
+ Addedundici-types@6.20.0(transitive)
- Removed@types/node@^10.12.0
- Removed@types/shortid@0.0.29
- Removedshortid@^2.2.13
- Removed@types/node@10.17.60(transitive)
- Removed@types/shortid@0.0.29(transitive)
- Removednanoid@2.1.11(transitive)
- Removedshortid@2.2.16(transitive)