Skip to content

Instantly share code, notes, and snippets.

@jimpick
Last active September 26, 2018 23:41
Show Gist options
  • Save jimpick/2df9b96d1332e5e28bb626b8071e6677 to your computer and use it in GitHub Desktop.
Save jimpick/2df9b96d1332e5e28bb626b8071e6677 to your computer and use it in GitHub Desktop.
Help test the multiwriter Dat command-line

Install

$ npm install -g @jimpick/dat-next

Make a directory with some sample files

$ mkdir sample-files
$ cd sample-files
$ echo 'Hello world!' > hello-world.txt
$ ls -l
total 8
-rw-r--r--  1 jim  staff  13 Sep 24 09:50 hello-world.txt

Share directory as a multiwriter Dat

(use the --new option to tell the cli to create a "new" multiwriter format archive)

$ dat-next share --new
dat v13.11.3-17
dat://6b96abfccc965b3ab1b3e8bb429850e80740faca06ebea1f6694d02691ec39f3
Format: multiwriter (authorized)
Sharing dat: 1 files (13 B)

0 connections | Download 0 B/s Upload 0 B/s

Watching for file updates


Ctrl+C to Exit

Leave it running for now.

Use the dat://... url that you create in the following steps.

Clone the Dat

On another computer, install dat-next (npm install -g @jimpick/dat-next), and clone the Dat using the url you created:

(Use the --new flag so it knows to expect a new format "multiwriter" archive. In the future, the dat cli will detect the format)

$ dat-next clone --new dat://6b96abfccc965b3ab1b3e8bb429850e80740faca06ebea1f6694d02691ec39f3
dat v13.11.3-17
Created new dat in /Users/jim/tmp/6b96abfccc965b3ab1b3e8bb429850e80740faca06ebea1f6694d02691ec39f3/.dat
dat://6b96abfccc965b3ab1b3e8bb429850e80740faca06ebea1f6694d02691ec39f3
Format: multiwriter (not authorized)
Local key: 9df3edc523cfc995cc854167bc93f52c3c434de58aa027e11bf77fa9a6ccc65d
Cloning: 1 files (13 B)

0 connections | Download 114 B/s Upload 0 B/s

Downloading updates...
[==========================================] 100.00%


Exiting the Dat program...

Make a note of the 'Local key'. You'll use that in a subsequent step to authorize this computer to be able to write to the archive.

Examine the files

$ cd 6b96abfccc965b3ab1b3e8bb429850e80740faca06ebea1f6694d02691ec39f3
$ ls -l
total 8
-rw-r--r--  1 jim  staff  13 Sep 24 09:53 hello-world.txt
$ cat hello-world.txt
Hello world!

On the first computer, authorize the second computer as a writer

First, press Ctrl-C to stop sharing.

Use the 'Local key' that was displayed on the second computer from when you cloned the archive.

$ dat-next authorize 9df3edc523cfc995cc854167bc93f52c3c434de58aa027e11bf77fa9a6ccc65d
Authorization succeeded.

On the first computer, re-share the archive

You can now just do dat-next sync instead of dat-next share --new. (both are equivalent)

$ dat-next sync
dat v13.11.3-17
dat://6b96abfccc965b3ab1b3e8bb429850e80740faca06ebea1f6694d02691ec39f3
Format: multiwriter (authorized)
Sharing dat: 1 files (0 B)

0 connections | Download 0 B/s Upload 0 B/s

Watching for file updates


Ctrl+C to Exit

On the second computer, re-connect to the swarm

We need to do this twice (for now) - the cli doesn't yet have the code written to watch for the authorization change.

First, run dat-next sync just to sync the changes:

$ dat-next sync
dat v13.11.3-17
dat://6b96abfccc965b3ab1b3e8bb429850e80740faca06ebea1f6694d02691ec39f3
Format: multiwriter (not authorized)
Local key: 9df3edc523cfc995cc854167bc93f52c3c434de58aa027e11bf77fa9a6ccc65d
Sharing dat: 1 files (0 B)

0 connections | Download 0 B/s Upload 0 B/s

Watching for file updates


Ctrl+C to Exit

Then, press Ctrl-C, and then run dat-next sync again.

$ dat-next sync
dat v13.11.3-17
dat://6b96abfccc965b3ab1b3e8bb429850e80740faca06ebea1f6694d02691ec39f3
Format: multiwriter (authorized)
Sharing dat: 1 files (0 B)

0 connections | Download 0 B/s Upload 0 B/s

Watching for file updates


Ctrl+C to Exit

It says that it is authorized now.

Good. Both computers can now write to the Dat archive. Let's try that out.

Write a file on the second computer

On the second computer, leave the dat-next sync running in one terminal window, and start another terminal.

Make sure you are in the cloned directory (eg. '6b96abfccc965b3ab1b3e8bb429850e80740faca06ebea1f6694d02691ec39f3')

$ basename $PWD
6b96abfccc965b3ab1b3e8bb429850e80740faca06ebea1f6694d02691ec39f3
$ ls -l
total 8
-rw-r--r--  1 jim  staff  13 Sep 24 09:50 hello-world.txt
$ echo 'file from computer 2' > from-computer-2.txt
$ ls -l
total 16
-rw-r--r--  1 jim  staff  21 Sep 24 11:00 from-computer-2.txt
-rw-r--r--  1 jim  staff  13 Sep 24 09:53 hello-world.txt

Since dat-next sync is open in the same directory in another terminal, the new file should sync across to the first computer.

Open another terminal window on the first computer (in the same 'sample-files' directory), and check it:

$ basename $PWD
sample-files
$ ls -l
total 16
-rw-r--r--  1 jim  staff  21 Sep 24 11:00 from-computer-2.txt
-rw-r--r--  1 jim  staff  13 Sep 24 09:50 hello-world.txt
$ cat from-computer-2.txt
file from computer 2

Modify a file on the first computer

Warning: This causes the dat-next instance on the second computer to crash

Just to confirm that both ends can still write, in the second terminal window on the first computer (in the 'sample-files' directory):

$ basename $PWD
sample-files
$ ls -l
total 16
-rw-r--r--  1 jim  staff  21 Sep 24 11:00 from-computer-2.txt
-rw-r--r--  1 jim  staff  13 Sep 24 09:50 hello-world.txt
$ echo 'Add a line to hello-world.txt' >> hello-world.txt
jim@jpimac ~/tmp/sample-files $ cat hello-world.txt
Hello world!
Add a line to hello-world.txt

On the second computer, in the the second terminal window:

$ basename $PWD
6b96abfccc965b3ab1b3e8bb429850e80740faca06ebea1f6694d02691ec39f3
$ ls -l
total 16
-rw-r--r--  1 jim  staff  21 Sep 24 11:00 from-computer-2.txt
-rw-r--r--  1 jim  staff  43 Sep 24 11:07 hello-world.txt
$ cat hello-world.txt
Hello world!
Add a line to hello-world.txt
@jimpick
Copy link
Author

jimpick commented Sep 24, 2018

Stack trace for the crash on the last step:

/opt/node/node-v10.8.0-darwin-x64/lib/node_modules/@jimpick/dat-next/node_modules/@jimpick/hyperdrive-next/lib/messages.js:207
    if (!end) end = buf.length
                        ^

TypeError: Cannot read property 'length' of null
    at Object.decode (/opt/node/node-v10.8.0-darwin-x64/lib/node_modules/@jimpick/dat-next/node_modules/@jimpick/hyperdrive-next/lib/messages.js:207:25)
    at loop (/opt/node/node-v10.8.0-darwin-x64/lib/node_modules/@jimpick/dat-next/node_modules/@jimpick/hyperdrive-next/lib/storage.js:152:22)
    at Request._callback (/opt/node/node-v10.8.0-darwin-x64/lib/node_modules/@jimpick/dat-next/node_modules/hypercore/index.js:1305:5)
    at Request.callback (/opt/node/node-v10.8.0-darwin-x64/lib/node_modules/@jimpick/dat-next/node_modules/random-access-storage/index.js:161:8)
    at onread (/opt/node/node-v10.8.0-darwin-x64/lib/node_modules/@jimpick/dat-next/node_modules/random-access-file/index.js:80:31)
    at FSReqWrap.wrapper [as oncomplete] (fs.js:458:17)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment