holepunchto / pear

combined Peer-to-Peer (P2P) Runtime, Development & Deployment tool
https://docs.pears.com/
Apache License 2.0
115 stars 11 forks source link

Process No defined on terminal app pear dev #24

Closed saranshisatgit closed 9 months ago

saranshisatgit commented 9 months ago

Following the p2p file sharing doc

ReferenceError: process is not defined at pear://dev/node_modules/pear-stdio/index.js:7:15 at Module._extensions..cjs (file:///Users/saransh/Library/Application%20Support/pear/by-dkey/7216eeac5c879a2ca1dcff14bb8c1ad47d188a8c17b13e66f36d354ea8bd3b28/1/boot.bundle/node_modules/bare-module/index.js:549:117) at Module._extensions..js (file:///Users/saransh/Library/Application%20Support/pear/by-dkey/7216eeac5c879a2ca1dcff14bb8c1ad47d188a8c17b13e66f36d354ea8bd3b28/1/boot.bundle/node_modules/bare-module/index.js:519:51) at Module.load (file:///Users/saransh/Library/Application%20Support/pear/by-dkey/7216eeac5c879a2ca1dcff14bb8c1ad47d188a8c17b13e66f36d354ea8bd3b28/1/boot.bundle/node_modules/bare-module/index.js:321:36) at Module._onimport (file:///Users/saransh/Library/Application%20Support/pear/by-dkey/7216eeac5c879a2ca1dcff14bb8c1ad47d188a8c17b13e66f36d354ea8bd3b28/1/boot.bundle/node_modules/bare-module/index.js:202:25) at Module._evaluate (file:///Users/saransh/Library/Application%20Support/pear/by-dkey/7216eeac5c879a2ca1dcff14bb8c1ad47d188a8c17b13e66f36d354ea8bd3b28/1/boot.bundle/node_modules/bare-module/index.js:126:13) at Module._transform (file:///Users/saransh/Library/Application%20Support/pear/by-dkey/7216eeac5c879a2ca1dcff14bb8c1ad47d188a8c17b13e66f36d354ea8bd3b28/1/boot.bundle/node_modules/bare-module/index.js:117:12) at Module.load (file:///Users/saransh/Library/Application%20Support/pear/by-dkey/7216eeac5c879a2ca1dcff14bb8c1ad47d188a8c17b13e66f36d354ea8bd3b28/1/boot.bundle/node_modules/bare-module/index.js:325:19) at Crank.run (file:///Users/saransh/Library/Application%20Support/pear/by-dkey/7216eeac5c879a2ca1dcff14bb8c1ad47d188a8c17b13e66f36d354ea8bd3b28/1/boot.bundle/ipc/crank.js:51:14) at async file:///Users/saransh/Library/Application%20Support/pear/by-dkey/7216eeac5c879a2ca1dcff14bb8c1ad47d188a8c17b13e66f36d354ea8bd3b28/1/boot.bundle/cmd/iface.js:60:39

mafintosh commented 9 months ago

Ah ya, docs aren't super clear on this but the terminal apps are a Bare process. This means you have to provide the apis you want to use.

To use process simply npm install bare-process (https://github.com/holepunchto/bare-process) and do

const process = require('bare-process')

For fullish Node.js compat see

https://github.com/holepunchto/bare-node

mattdesl commented 9 months ago

I ran into the same trying to get the p2p file sharing demo (terminal) to work.

The problem was in a module, pear-stdio which could not find process. I changed the demo to use bare-tty for input and it seems to work better:

import Hyperswarm from "hyperswarm";
import Hyperdrive from "hyperdrive";
import Localdrive from "localdrive";
import Corestore from "corestore";
import debounce from "debounceify";
import b4a from "b4a";
import tty from "bare-tty";
import readline from "bare-readline";

// create a Corestore instance
const store = new Corestore(Pear.config.storage);
const swarm = new Hyperswarm();
Pear.teardown(() => swarm.destroy());

// replication of the corestore instance on connection with other peers
swarm.on("connection", (conn) => store.replicate(conn));

// A local drive provides a Hyperdrive interface to a local directory
const local = new Localdrive("./writer-dir");

// A Hyperdrive takes a Corestore because it needs to create many cores
// One for a file metadata Hyperbee, and one for a content Hypercore
const drive = new Hyperdrive(store);

// wait till the properties of the hyperdrive instance are initialized
await drive.ready();

// Import changes from the local drive into the Hyperdrive
const mirror = debounce(mirrorDrive);

const discovery = swarm.join(drive.discoveryKey);
await discovery.flushed();

console.log("drive key:", b4a.toString(drive.key, "hex"));

// start the mirroring process (i.e copying) of content from writer-dir to the drive
// whenever somebody enters a line into the terminal
const rl = readline.createInterface({
  input: new tty.ReadStream(0),
  output: new tty.WriteStream(1),
});

rl.input.setMode(tty.constants.MODE_RAW);
rl.on("data", async (line) => {
  await mirror();
  rl.prompt();
});
rl.prompt();

// this function copies the contents from writer-dir directory to the drive
async function mirrorDrive() {
  rl.write("started mirroring changes from './writer-dir' into the drive...\n");
  const mirror = local.mirror(drive);
  await mirror.done();
  rl.write(`finished mirroring: ${JSON.stringify(mirror.count, null, 2)}\n`);
}
mafintosh commented 9 months ago

cc @subashsn lets get the docs updated to not use pear-stdin - we stopped supporting that

subashsn commented 9 months ago

Sure

mafintosh commented 9 months ago

@mattdesl @saranshisatgit I just fixed pear-stdio to work still. @subashsn no need to update the docs then

Fix in pear-stdio 1.0.1