Compare commits

..

No commits in common. "v0.0.2-develop.19" and "master" have entirely different histories.

9 changed files with 288 additions and 17922 deletions

View File

@ -1,13 +0,0 @@
name: Build/Publish
on:
push:
branches:
- master
- develop
- develop-*
jobs:
main:
uses: lumeweb/github-node-deploy-workflow/.github/workflows/main.yml@master
secrets: inherit

View File

@ -1,5 +0,0 @@
{
"preset": [
"@lumeweb/node-library-preset"
]
}

View File

@ -1,42 +0,0 @@
## [0.0.2-develop.19](https://git.lumeweb.com/LumeWeb/kernel-protomux-client/compare/v0.0.2-develop.18...v0.0.2-develop.19) (2023-09-02)
## [0.0.2-develop.18](https://git.lumeweb.com/LumeWeb/kernel-protomux-client/compare/v0.0.2-develop.17...v0.0.2-develop.18) (2023-09-02)
## [0.0.2-develop.17](https://git.lumeweb.com/LumeWeb/kernel-protomux-client/compare/v0.0.2-develop.16...v0.0.2-develop.17) (2023-09-02)
## [0.0.2-develop.16](https://git.lumeweb.com/LumeWeb/kernel-protomux-client/compare/v0.0.2-develop.15...v0.0.2-develop.16) (2023-07-29)
## [0.0.2-develop.15](https://git.lumeweb.com/LumeWeb/kernel-protomux-client/compare/v0.0.2-develop.14...v0.0.2-develop.15) (2023-07-24)
## [0.0.2-develop.14](https://git.lumeweb.com/LumeWeb/kernel-protomux-client/compare/v0.0.2-develop.13...v0.0.2-develop.14) (2023-07-23)
## [0.0.2-develop.13](https://git.lumeweb.com/LumeWeb/kernel-protomux-client/compare/v0.0.2-develop.12...v0.0.2-develop.13) (2023-07-23)
## [0.0.2-develop.12](https://git.lumeweb.com/LumeWeb/kernel-protomux-client/compare/v0.0.2-develop.11...v0.0.2-develop.12) (2023-07-22)
## [0.0.2-develop.11](https://git.lumeweb.com/LumeWeb/kernel-protomux-client/compare/v0.0.2-develop.10...v0.0.2-develop.11) (2023-07-22)
## [0.0.2-develop.10](https://git.lumeweb.com/LumeWeb/kernel-protomux-client/compare/v0.0.2-develop.9...v0.0.2-develop.10) (2023-07-22)
## [0.0.2-develop.9](https://git.lumeweb.com/LumeWeb/kernel-protomux-client/compare/v0.0.2-develop.8...v0.0.2-develop.9) (2023-07-22)
## [0.0.2-develop.8](https://git.lumeweb.com/LumeWeb/kernel-protomux-client/compare/v0.0.2-develop.7...v0.0.2-develop.8) (2023-07-22)
## [0.0.2-develop.7](https://git.lumeweb.com/LumeWeb/kernel-protomux-client/compare/v0.0.2-develop.6...v0.0.2-develop.7) (2023-07-12)
## [0.0.2-develop.6](https://git.lumeweb.com/LumeWeb/kernel-protomux-client/compare/v0.0.2-develop.5...v0.0.2-develop.6) (2023-07-08)
## [0.0.2-develop.5](https://git.lumeweb.com/LumeWeb/kernel-protomux-client/compare/v0.0.2-develop.4...v0.0.2-develop.5) (2023-07-08)
## [0.0.2-develop.4](https://git.lumeweb.com/LumeWeb/kernel-protomux-client/compare/v0.0.2-develop.3...v0.0.2-develop.4) (2023-07-05)
## [0.0.2-develop.3](https://git.lumeweb.com/LumeWeb/kernel-protomux-client/compare/v0.0.2-develop.2...v0.0.2-develop.3) (2023-07-05)
## [0.0.2-develop.2](https://git.lumeweb.com/LumeWeb/kernel-protomux-client/compare/v0.0.2-develop.1...v0.0.2-develop.2) (2023-07-04)
## [0.0.2-develop.1](https://git.lumeweb.com/LumeWeb/kernel-protomux-client/compare/v0.0.1...v0.0.2-develop.1) (2023-07-04)
### Reverts
* Revert "*add destroy method" ([10ad05f](https://git.lumeweb.com/LumeWeb/kernel-protomux-client/commit/10ad05f1dad6bf52198ab910d0da7eac51e206d3))

57
dist/index.d.ts vendored Normal file
View File

@ -0,0 +1,57 @@
import { Client } from "@lumeweb/libkernel-universal";
export default class Protomux {
private isProtomux;
constructor(stream: any);
private _stream;
get stream(): any;
static from(stream: any): any;
createChannel({ protocol, id, handshake, onopen, onclose, ondestroy, }: {
protocol: string;
id: any;
handshake: any;
onopen?: Function;
onclose?: Function;
ondestroy?: Function;
}): Promise<Channel>;
}
declare class Channel extends Client {
private protocol;
private id;
private handshake;
private onopen?;
private onclose?;
private ondestroy?;
private _created;
private _send?;
private _queue;
private _inited;
constructor(mux: Protomux, protocol: string, id: any, handshake: any, onopen?: Function, onclose?: Function, ondestroy?: Function);
private _ready;
get ready(): Promise<void>;
private _mux;
get mux(): Protomux;
private _channelId;
get channelId(): number;
open(): Promise<void>;
addMessage({ encoding, onmessage, }: {
encoding?: any;
onmessage: Function;
}): Message;
queueMessage(message: Message): Promise<void>;
private init;
destroy(error: Error): void;
}
declare class Message extends Client {
private encoding;
private onmessage;
private channel;
private _send?;
constructor({ channel, encoding, onmessage, }: {
channel: Channel;
encoding?: any;
onmessage: Function;
});
init(): Promise<void>;
send(data: any): void;
}
export {};

189
dist/index.js vendored Normal file
View File

@ -0,0 +1,189 @@
import { Client, factory } from "@lumeweb/libkernel-universal";
import { MODULE } from "@lumeweb/kernel-swarm-client";
import defer from "p-defer";
import { Buffer } from "buffer";
export default class Protomux {
isProtomux = true;
constructor(stream) {
this._stream = stream;
if (!stream.userData) {
stream.userData = this;
}
}
_stream;
get stream() {
return this._stream;
}
static from(stream) {
if (stream.userData && stream.userData.isProtomux)
return stream.userData;
if (stream.isProtomux)
return stream;
return new this(stream);
}
async createChannel({ protocol, id = null, handshake = null, onopen = undefined, onclose = undefined, ondestroy = undefined, }) {
return createChannel(this, protocol, id, handshake, onopen, onclose, ondestroy);
}
}
class Channel extends Client {
protocol;
id;
handshake;
onopen;
onclose;
ondestroy;
_created = defer();
_send;
_queue = [];
_inited = false;
constructor(mux, protocol, id, handshake, onopen, onclose, ondestroy) {
super();
this._mux = mux;
this.protocol = protocol;
this.id = id;
this.handshake = handshake;
this.onopen = onopen;
this.onclose = onclose;
this.ondestroy = ondestroy;
}
_ready = defer();
get ready() {
return this._ready.promise;
}
_mux;
get mux() {
return this._mux;
}
_channelId = -1;
get channelId() {
return this._channelId;
}
async open() {
await this.init();
await this._created;
while (this._queue.length) {
await this._queue.shift()?.init();
}
this._ready.resolve();
}
addMessage({ encoding = undefined, onmessage, }) {
return createMessage({ channel: this, encoding, onmessage });
}
async queueMessage(message) {
this._queue.push(message);
}
async init() {
if (this._inited) {
return;
}
this._inited = true;
const [update, ret] = this.connectModule("createProtomuxChannel", {
id: this._mux.stream.id,
data: {
protocol: this.protocol,
id: this.id,
handshake: this.handshake,
onopen: !!this.onopen,
onclose: !!this.onclose,
ondestroy: !!this.ondestroy,
},
}, (data) => {
switch (data.action) {
case "onopen":
this.onopen?.(...data.args);
break;
case "onclose":
this.onclose?.(...data.args);
break;
case "ondestroy":
this.ondestroy?.(...data.args);
break;
default:
this._channelId = data;
this._created.resolve();
}
});
this._send = update;
ret.catch((e) => this._created.reject(e));
return this._created.promise;
}
destroy(error) {
this._send?.({ action: "destroy", args: [error] });
}
}
class Message extends Client {
encoding;
onmessage;
channel;
_send;
constructor({ channel, encoding = undefined, onmessage = () => { }, }) {
super();
this.channel = channel;
this.encoding = encoding;
this.onmessage = onmessage;
this.channel.queueMessage(this);
}
async init() {
const created = defer();
await this.loadLibs(MODULE);
const [update] = this.connectModule("createProtomuxMessage", {
id: this.channel.mux.stream.id,
channelId: this.channel.channelId,
data: {
encoding: !!this.encoding,
onmessage: !!this.onmessage,
},
}, async (data) => {
if (data.args) {
data.args = data.args.filter((arg) => {
if (arg instanceof Uint8Array) {
return Buffer.from(arg);
}
return arg;
});
}
if (data?.args && data?.args[0]?.buffer instanceof Uint8Array) {
data.args[0].buffer = Buffer.from(data.args[0].buffer);
}
switch (data.action) {
case "encode":
update({
id: data.id,
action: "encode",
args: [await this.encoding.encode?.(...data.args), data.args[0]],
});
break;
case "decode":
update({
id: data.id,
action: "decode",
args: [await this.encoding.decode?.(...data.args), data.args[0]],
});
break;
case "preencode":
update({
id: data.id,
action: "preencode",
args: [
await this.encoding.preencode?.(...data.args),
data.args[0],
],
});
break;
case "onmessage":
this.onmessage?.(...data.args);
break;
case "created":
created.resolve();
break;
}
});
this._send = update;
return created.promise;
}
send(data) {
this._send?.({ action: "send", args: [data] });
}
}
const createChannel = factory(Channel, MODULE);
const createMessage = factory(Message, MODULE);

17818
npm-shrinkwrap.json generated

File diff suppressed because it is too large Load Diff

View File

@ -1,32 +1,17 @@
{
"name": "@lumeweb/kernel-protomux-client",
"version": "0.0.2-develop.19",
"type": "module",
"main": "lib/index.js",
"repository": {
"type": "git",
"url": "gitea@git.lumeweb.com:LumeWeb/kernel-protomux-client.git"
},
"readme": "ERROR: No README data found!",
"scripts": {
"prepare": "presetter bootstrap",
"build": "run build",
"semantic-release": "semantic-release"
},
"devDependencies": {
"@lumeweb/node-library-preset": "^0.2.7",
"presetter": "*"
},
"version": "0.1.0",
"main": "dist/index.js",
"dependencies": {
"@lumeweb/kernel-swarm-client": "^0.1.0-develop.8",
"@lumeweb/libkernel": "^0.1.0-develop.38",
"@lumeweb/kernel-swarm-client": "git+https://git.lumeweb.com/LumeWeb/kernel-swarm-client.git",
"@lumeweb/libkernel-universal": "git+https://git.lumeweb.com/LumeWeb/libkernel-universal.git",
"b4a": "^1.6.3",
"buffer": "^6.0.3",
"p-defer": "^4.0.0"
},
"files": [
"lib"
],
"publishConfig": {
"access": "public"
"devDependencies": {
"@types/b4a": "^1.6.0",
"prettier": "^2.8.7",
"typescript": "^5.0.4"
}
}

View File

@ -1,8 +1,9 @@
import { Client, factory } from "@lumeweb/libkernel/module";
import { Client, factory } from "@lumeweb/libkernel-universal";
import { MODULE } from "@lumeweb/kernel-swarm-client";
import defer from "p-defer";
import b4a from "b4a";
import { Buffer } from "buffer";
export default class Protomux {
@ -49,7 +50,7 @@ export default class Protomux {
handshake,
onopen,
onclose,
ondestroy,
ondestroy
);
}
}
@ -67,16 +68,15 @@ class Channel extends Client {
private _inited = false;
constructor(
module: string,
mux: Protomux,
protocol: string,
id: any,
handshake: any,
onopen?: Function,
onclose?: Function,
ondestroy?: Function,
ondestroy?: Function
) {
super(module);
super();
this._mux = mux;
this.protocol = protocol;
this.id = id;
@ -164,7 +164,7 @@ class Channel extends Client {
this._channelId = data;
this._created.resolve();
}
},
}
);
this._send = update;
@ -185,19 +185,16 @@ class Message extends Client {
private _send?: (data?: any) => void;
constructor(
module: string,
{
channel,
encoding = undefined,
onmessage = () => {},
}: {
channel: Channel;
encoding?: any;
onmessage: Function;
},
) {
super(module);
constructor({
channel,
encoding = undefined,
onmessage = () => {},
}: {
channel: Channel;
encoding?: any;
onmessage: Function;
}) {
super();
this.channel = channel;
this.encoding = encoding;
this.onmessage = onmessage;
@ -206,6 +203,9 @@ class Message extends Client {
async init(): Promise<void> {
const created = defer();
await this.loadLibs(MODULE);
const [update] = this.connectModule(
"createProtomuxMessage",
{
@ -262,7 +262,7 @@ class Message extends Client {
created.resolve();
break;
}
},
}
);
this._send = update;

13
tsconfig.json Normal file
View File

@ -0,0 +1,13 @@
{
"compilerOptions": {
"target": "esnext",
"declaration": true,
"moduleResolution": "node",
"outDir": "./dist",
"strict": true,
"allowSyntheticDefaultImports": true,
"esModuleInterop": true
},
"include": ["src"],
"exclude": ["node_modules", "**/__tests__/*"]
}