holepunchto / hyperdrive

Hyperdrive is a secure, real time distributed file system
Apache License 2.0
1.86k stars 135 forks source link

Hyperdrive is not a constructor #320

Closed raphael10-collab closed 2 years ago

raphael10-collab commented 2 years ago

Trying to execute this demo app: https://hypercore-protocol.org/guides/examples/hyperdrive-app/

(base) raphy@pc:~/hypercore/hyperdrive/hyperdrive-app$ node hyperdrive_app.js
Hyperspace daemon connected, status:
{
  apiVersion: '1.15.1',
  holepunchable: false,
  remoteAddress: '',
  version: '3.19.0'
}
(node:132481) UnhandledPromiseRejectionWarning: TypeError: Hyperdrive is not a constructor
    at main (/home/raphy/hypercore/hyperdrive/hyperdrive-app/hyperdrive_app.js:16:15)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:132481) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:132481) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

How to solve the problem?

andrewosh commented 2 years ago

Hi @raphael10-collab, I just re-ran the example in a freshly initialized module (in a temporary directory), and it gave the correct expected output:

Hyperspace daemon connected, status:
{
  apiVersion: '1.15.1',
  holepunchable: false,
  remoteAddress: '',
  version: '3.19.0'
}
New drive created, key:
   a9f9ac5c18cc1527741f677ba89f5095cf98ded36b60ba2dea1d68de2b1b24e4
readdir(/)
   [ 'stuff', 'file1.txt' ]
readFile(/file1.txt, utf8)
   Hello world!
readFile(/stuff/file2.bin, hex)
   00010204
Shutting down Hyperspace, this may take a few seconds...

Are you sure you're not importing an old version of Hyperdrive?

raphael10-collab commented 2 years ago

Hi @andrewosh !

This is the content of my package.json file :

{
  "dependencies": {
    "hyperdrive": "^10.21.0",
    "hyperspace": "^3.19.0"
  }
}

And this is the content of the hyperdrive_app.js file :

// https://hypercore-protocol.org/guides/examples/hyperdrive-app/

const { Client, Server } = require('hyperspace')
const { Hyperdrive } = require('hyperdrive')

async function main () {
  // Setup the Hyperspace Daemon connection
  // =
  const {client, cleanup} = await setupHyperspace()
  console.log('Hyperspace daemon connected, status:')
  console.log(await client.status())

  // Create a Hyperdrive
  // =
  let drive = new Hyperdrive(client.corestore(), null)
  await drive.promises.ready()
  console.log('New drive created, key:')
  console.log('  ', drive.key.toString('hex'))

  // File writes
  // =
  await drive.promises.mkdir('/stuff')
  await drive.promises.mkdir('/stuff/things')
  await drive.promises.writeFile('/file1.txt', 'Hello world!')
  await drive.promises.writeFile('/stuff/file2.bin', Buffer.from([0,1,2,4]))

  // File reads
  // =
  console.log('readdir(/)')
  console.log('  ', await drive.promises.readdir('/'))
  console.log('readFile(/file1.txt, utf8)')
  console.log('  ', await drive.promises.readFile('/file1.txt', 'utf8'))
  console.log('readFile(/stuff/file2.bin, hex)')
  console.log('  ', await drive.promises.readFile('/stuff/file2.bin', 'hex'))

  // Swarm on the network
  // =
  await client.replicate(drive)
  await new Promise(r => setTimeout(r, 3e3)) // just a few seconds
  await client.network.configure(drive, {announce: false, lookup: false})

  await cleanup()
}

async function setupHyperspace () {
  let client
  let server

  try {
    client = new Client()
    await client.ready()
  } catch (e) {
    // no daemon, start it in-process
    server = new Server()
    await server.ready()
    client = new Client()
    await client.ready()
  }

  return {
    client,
    async cleanup () {
      await client.close()
      if (server) {
        console.log('Shutting down Hyperspace, this may take a few seconds...')
        await server.stop()
      }
    }
  }
}

main()

node: v14.15.5 O.S. : Ubuntu 20.04 Desktop

heapwolf commented 2 years ago

This looks like it was caused by package-lock.json, I think this should be resolved now. Try rm -rf node_modules && rm -rf package-lock.json && npm install, the issue should be resolved but reopen if not.