feat: initial version

This commit is contained in:
Derrick Hammer 2023-08-31 18:12:13 -04:00
parent c2d96a3de2
commit 53ddfc03c9
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
5 changed files with 19690 additions and 1 deletions

5
.presetterrc.json Normal file
View File

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

View File

@ -1,6 +1,6 @@
MIT License
Copyright (c) 2023 LumeWeb
Copyright (c) 2023 Hammer Technologies LLC
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

19576
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

31
package.json Normal file
View File

@ -0,0 +1,31 @@
{
"name": "@lumeweb/libs5-transport-hyper",
"version": "0.1.0",
"type": "module",
"main": "lib/index.js",
"repository": {
"type": "git",
"url": "gitea@git.lumeweb.com:LumeWeb/libs5-transport-hyper.git"
},
"devDependencies": {
"@lumeweb/node-library-preset": "^0.2.7",
"presetter": "*"
},
"readme": "ERROR: No README data found!",
"_id": "@lumeweb/libs5-transport-hyper@0.1.0",
"scripts": {
"prepare": "presetter bootstrap",
"build": "run build",
"semantic-release": "semantic-release"
},
"publishConfig": {
"access": "public"
},
"files": [
"lib"
],
"dependencies": {
"@lumeweb/libs5": "^0.1.0-develop.11",
"streamx": "^2.15.1"
}
}

77
src/index.ts Normal file
View File

@ -0,0 +1,77 @@
import { BasePeer, Logger } from "@lumeweb/libs5";
import { URL } from "url";
import { Buffer } from "buffer";
import { Readable } from "streamx";
export default class HyperTransportPeer extends BasePeer {
private _peer: any;
private _muxer: any;
private _protocol: string;
protected _socket = new Readable();
private _pipe?: any;
constructor(options: any) {
super(options);
const { peer, muxer, protocol } = options;
this._peer = peer;
this._muxer = muxer;
this._protocol = protocol;
}
public async init() {
const channel = await this._muxer.createChannel({
protocol: this._protocol,
});
const self = this;
this._pipe = await channel.addMessage({
async onmessage(m) {
if (m instanceof Uint8Array) {
m = Buffer.from(m);
}
self._socket.push(m);
},
});
await channel.open();
}
public static async connect(uri: URL): Promise<any> {
return Promise.reject("not supported");
}
listenForMessages(
callback: (event: any) => Promise<void>,
{
onDone,
onError,
logger,
}: {
onDone?: any;
onError?: (...args: any[]) => void;
logger: Logger;
},
): void {
this._socket.on("data", async (data: Buffer) => {
await callback(data);
});
if (onDone) {
this._socket.on("end", onDone);
}
if (onError) {
this._socket.on("error", onError);
}
}
renderLocationUri(): string {
return "Hypercore client";
}
sendMessage(message: Uint8Array): void {
this._pipe.write(message);
}
}