lixnet 1.0.1 → 1.0.3
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/README.md +0 -15
- package/dist/exports.js +4 -12
- package/dist/lib/client.d.ts +1 -1
- package/dist/lib/client.js +5 -17
- package/dist/lib/peer.js +16 -27
- package/dist/lib/server.js +45 -62
- package/dist/lib/types.js +1 -2
- package/dist/lib/util/log.js +1 -4
- package/package.json +3 -2
package/README.md
CHANGED
package/dist/exports.js
CHANGED
|
@@ -1,12 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.LixnetPeer = exports.LixnetClient = exports.LixnetServer = void 0;
|
|
7
|
-
const server_1 = __importDefault(require("./lib/server"));
|
|
8
|
-
exports.LixnetServer = server_1.default;
|
|
9
|
-
const client_1 = __importDefault(require("./lib/client"));
|
|
10
|
-
exports.LixnetClient = client_1.default;
|
|
11
|
-
const peer_1 = __importDefault(require("./lib/peer"));
|
|
12
|
-
exports.LixnetPeer = peer_1.default;
|
|
1
|
+
import LixnetServer from "./lib/server";
|
|
2
|
+
import LixnetClient from "./lib/client";
|
|
3
|
+
import LixnetPeer from "./lib/peer";
|
|
4
|
+
export { LixnetServer, LixnetClient, LixnetPeer };
|
package/dist/lib/client.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { EventCollection, CallableEvent } from "./types";
|
|
2
|
-
export default class
|
|
2
|
+
export default class LixnetClient<TEvents extends EventCollection> {
|
|
3
3
|
private baseUrl;
|
|
4
4
|
private events;
|
|
5
5
|
constructor({ events, baseUrl, }: {
|
package/dist/lib/client.js
CHANGED
|
@@ -1,15 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
class LixnetClientConstructor {
|
|
1
|
+
export default class LixnetClient {
|
|
13
2
|
constructor({ events, baseUrl, }) {
|
|
14
3
|
this.baseUrl = baseUrl;
|
|
15
4
|
this.events = events;
|
|
@@ -17,13 +6,13 @@ class LixnetClientConstructor {
|
|
|
17
6
|
getClient() {
|
|
18
7
|
const client = {};
|
|
19
8
|
Object.keys(this.events).forEach((key) => {
|
|
20
|
-
client[key] = (input) =>
|
|
21
|
-
const response =
|
|
9
|
+
client[key] = async (input) => {
|
|
10
|
+
const response = await fetch(this.baseUrl, {
|
|
22
11
|
method: "POST",
|
|
23
12
|
body: JSON.stringify({ event: key, input }),
|
|
24
13
|
});
|
|
25
14
|
try {
|
|
26
|
-
const json = (
|
|
15
|
+
const json = (await response.json());
|
|
27
16
|
if (json.error) {
|
|
28
17
|
throw new Error(json.error);
|
|
29
18
|
}
|
|
@@ -33,9 +22,8 @@ class LixnetClientConstructor {
|
|
|
33
22
|
console.error(error);
|
|
34
23
|
return null;
|
|
35
24
|
}
|
|
36
|
-
}
|
|
25
|
+
};
|
|
37
26
|
});
|
|
38
27
|
return client;
|
|
39
28
|
}
|
|
40
29
|
}
|
|
41
|
-
exports.default = LixnetClientConstructor;
|
package/dist/lib/peer.js
CHANGED
|
@@ -1,24 +1,13 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
1
|
function createLixnetId() {
|
|
13
2
|
return "LX-" + Math.random().toString(36).substring(2, 15);
|
|
14
3
|
}
|
|
15
|
-
class LixnetPeer {
|
|
4
|
+
export default class LixnetPeer {
|
|
16
5
|
constructor() {
|
|
17
6
|
this.socket = null;
|
|
18
7
|
this.rb = {};
|
|
19
8
|
this.transmissionLimit = -1;
|
|
20
9
|
this.transmissionChunksLimit = 20;
|
|
21
|
-
this.call =
|
|
10
|
+
this.call = async (event, data, { callback } = { callback: null }) => {
|
|
22
11
|
if (!this.socket)
|
|
23
12
|
throw new Error("LIXNET - Socket not set");
|
|
24
13
|
let rf = null;
|
|
@@ -65,7 +54,7 @@ class LixnetPeer {
|
|
|
65
54
|
return;
|
|
66
55
|
}
|
|
67
56
|
this.socket.send(dataToSend);
|
|
68
|
-
}
|
|
57
|
+
};
|
|
69
58
|
this.eventHandlers = {};
|
|
70
59
|
this.transmissions = {};
|
|
71
60
|
this.outgoingTransmissions = {};
|
|
@@ -239,19 +228,19 @@ class LixnetPeer {
|
|
|
239
228
|
socket,
|
|
240
229
|
});
|
|
241
230
|
}
|
|
242
|
-
handleEvent(
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
socket.send(JSON.stringify({
|
|
250
|
-
rb: rf,
|
|
251
|
-
data: result,
|
|
252
|
-
}));
|
|
253
|
-
}
|
|
231
|
+
async handleEvent({ eventName, data, rf, socket, }) {
|
|
232
|
+
const eventHandler = this.eventHandlers[eventName];
|
|
233
|
+
if (!eventHandler)
|
|
234
|
+
return;
|
|
235
|
+
const result = await eventHandler({
|
|
236
|
+
...(typeof data === "object" ? data : {}),
|
|
237
|
+
socket,
|
|
254
238
|
});
|
|
239
|
+
if (rf) {
|
|
240
|
+
socket.send(JSON.stringify({
|
|
241
|
+
rb: rf,
|
|
242
|
+
data: result,
|
|
243
|
+
}));
|
|
244
|
+
}
|
|
255
245
|
}
|
|
256
246
|
}
|
|
257
|
-
exports.default = LixnetPeer;
|
package/dist/lib/server.js
CHANGED
|
@@ -1,74 +1,58 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
-
};
|
|
14
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
-
const zod_1 = require("zod");
|
|
16
|
-
const log_1 = __importDefault(require("./util/log"));
|
|
17
|
-
class LixnetServer {
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import LixnetLog from "./util/log";
|
|
3
|
+
export default class LixnetServer {
|
|
18
4
|
constructor({ debugLog = false, logger, jsonResponseMaker = Response.json, events, }) {
|
|
19
|
-
this.logger =
|
|
5
|
+
this.logger = LixnetLog;
|
|
20
6
|
this.debugLog = false;
|
|
21
7
|
this.debugLog = debugLog;
|
|
22
8
|
logger ? (this.logger = logger) : "";
|
|
23
9
|
this.jsonResponseMaker = jsonResponseMaker;
|
|
24
10
|
this.eventCollection = events;
|
|
25
11
|
}
|
|
26
|
-
handleRequest(request) {
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
this.
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
12
|
+
async handleRequest(request) {
|
|
13
|
+
let jsonData;
|
|
14
|
+
try {
|
|
15
|
+
jsonData = (await request.json());
|
|
16
|
+
}
|
|
17
|
+
catch (error) {
|
|
18
|
+
this.debugLog
|
|
19
|
+
? this.logger({
|
|
20
|
+
error: true,
|
|
21
|
+
message: "Invalid JSON",
|
|
22
|
+
})
|
|
23
|
+
: "";
|
|
24
|
+
return this.jsonResponseMaker({
|
|
25
|
+
error: "Invalid JSON",
|
|
26
|
+
}, { status: 400 });
|
|
27
|
+
}
|
|
28
|
+
if (!jsonData.action) {
|
|
29
|
+
return this.jsonResponseMaker({ error: "Action not found" }, { status: 400 });
|
|
30
|
+
}
|
|
31
|
+
if (!jsonData.input) {
|
|
32
|
+
return this.jsonResponseMaker({ error: "Input not found" }, { status: 400 });
|
|
33
|
+
}
|
|
34
|
+
const event = this.eventCollection[jsonData.event];
|
|
35
|
+
if (!event) {
|
|
36
|
+
return this.jsonResponseMaker({
|
|
37
|
+
error: "Action not found",
|
|
38
|
+
}, { status: 404 });
|
|
39
|
+
}
|
|
40
|
+
try {
|
|
41
|
+
const validatedInput = event.schema
|
|
42
|
+
? event.schema.parse(jsonData.input)
|
|
43
|
+
: jsonData.input;
|
|
44
|
+
const result = await event.handler(validatedInput);
|
|
45
|
+
return this.jsonResponseMaker({ data: result });
|
|
46
|
+
}
|
|
47
|
+
catch (error) {
|
|
48
|
+
if (error instanceof z.ZodError) {
|
|
39
49
|
return this.jsonResponseMaker({
|
|
40
|
-
error: "Invalid
|
|
50
|
+
error: "Invalid input",
|
|
51
|
+
details: error.issues,
|
|
41
52
|
}, { status: 400 });
|
|
42
53
|
}
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
}
|
|
46
|
-
if (!jsonData.input) {
|
|
47
|
-
return this.jsonResponseMaker({ error: "Input not found" }, { status: 400 });
|
|
48
|
-
}
|
|
49
|
-
const event = this.eventCollection[jsonData.event];
|
|
50
|
-
if (!event) {
|
|
51
|
-
return this.jsonResponseMaker({
|
|
52
|
-
error: "Action not found",
|
|
53
|
-
}, { status: 404 });
|
|
54
|
-
}
|
|
55
|
-
try {
|
|
56
|
-
const validatedInput = event.schema
|
|
57
|
-
? event.schema.parse(jsonData.input)
|
|
58
|
-
: jsonData.input;
|
|
59
|
-
const result = yield event.handler(validatedInput);
|
|
60
|
-
return this.jsonResponseMaker({ data: result });
|
|
61
|
-
}
|
|
62
|
-
catch (error) {
|
|
63
|
-
if (error instanceof zod_1.z.ZodError) {
|
|
64
|
-
return this.jsonResponseMaker({
|
|
65
|
-
error: "Invalid input",
|
|
66
|
-
details: error.issues,
|
|
67
|
-
}, { status: 400 });
|
|
68
|
-
}
|
|
69
|
-
throw error;
|
|
70
|
-
}
|
|
71
|
-
});
|
|
54
|
+
throw error;
|
|
55
|
+
}
|
|
72
56
|
}
|
|
73
57
|
// Returns dummy functions that match the type signatures
|
|
74
58
|
getEventTypes() {
|
|
@@ -87,4 +71,3 @@ class LixnetServer {
|
|
|
87
71
|
return this.eventCollection;
|
|
88
72
|
}
|
|
89
73
|
}
|
|
90
|
-
exports.default = LixnetServer;
|
package/dist/lib/types.js
CHANGED
|
@@ -1,2 +1 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
1
|
+
export {};
|
package/dist/lib/util/log.js
CHANGED
|
@@ -1,7 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.default = default_1;
|
|
4
|
-
function default_1({ error = false, message, }) {
|
|
1
|
+
export default function ({ error = false, message, }) {
|
|
5
2
|
if (error) {
|
|
6
3
|
return console.error(`[LIXNET]`, message);
|
|
7
4
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "lixnet",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.3",
|
|
4
4
|
"author": "Nitlix",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -9,7 +9,8 @@
|
|
|
9
9
|
},
|
|
10
10
|
"homepage": "https://github.com/Nitlix/lixnet",
|
|
11
11
|
"description": "RPC over HTTP and WebSockets, simplified.",
|
|
12
|
-
"module": "dist/exports.js",
|
|
12
|
+
"module": "./dist/exports.js",
|
|
13
|
+
"main": "./dist/exports.js",
|
|
13
14
|
"type": "module",
|
|
14
15
|
"devDependencies": {
|
|
15
16
|
"@types/bun": "latest"
|