File sharing package without the need to use a server. The objetive is to allow to comunicate servers and webs directly to transfer files.
npm install file-portals
Tested on node 20
To make it work in node, it is necessary to install a package that implements WebRTC.
Examples:
npm install wrtc
After it, is necessary to set it as global with something similar to this
// @ts-ignore
import * as wrtc from 'wrtc';
Object.assign(globalThis, wrtc);
First of all you need to create a IFilePeer to connect with other IFilePeer
You have to indicate what type of data will be use to write on filesystem. In this case Buffer
const peerA = new FilePeer();
const peerB = new FilePeer();
// Get offer
const offer = await peerA.connect() as RTCSessionDescription;
// Answer
const answer = await peerB.connect(offer) as RTCSessionDescription;
// Reply
await peerA.connect(answer);
In a real scenario, you have to exchange offers with a server, mail, QR o another data transport
const candidatesA = await peerA.candidates.export();
peerB.candidates.import(candidatesA);
In a real scenario, same as offers
import { IReader, IWriter, NodeReader, NodeWriter } from 'file-agents';
const reader = new NodeReader('path/where/files/will/be/shared');
const writer = new NodeWriter({ name: 'file-to-share' }); // Forget it at this moment
const portal = new FilePortal(reader, writer, peerA/*, { name: 'Portal', type: 'client' }*/); // Optionally can added a name and a type. By default type is client
const files = await portalA.files(); // If the other portal is a client, it has to accept the request, it`s a security breach that its parched.
const [{ uuid }] = await portalA.files();
const readed = await portalA.read(uuid, { start: 0, end: 6 });
const text = await readed.text();
const writable = await portalA.create({ name: 'file.txt', size: 6 });
const text = 'text';
const writable = await portalA.create({ name: 'file.txt', size: text.length });
await portalA.write(writable, new Blob(text.split('')), 0);
const text = 'text';
const writable = await portalA.create({ name: 'file.txt', size: text.length });
await portalA.write(writable, new Blob(text.split('')), 0);
await portalA.close(writable);