This repository has been archived by the owner on Jan 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
test.js
134 lines (129 loc) · 4.54 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
var fs = require('fs')
var path = require('path')
var http = require('http')
var tape = require('tape')
var concat = require('concat-stream')
var Dat = require('dat-node')
var ram = require('random-access-memory')
var hyperdrive = require('hyperdrive')
var tmp = require('temporary-directory')
var ecstatic = require('ecstatic')
var datHttp = require('./')
tape('can end replication immediately', function (t) {
makeTestServer(t, function runTest (datDir, destDir, cleanup) {
var storage = datHttp('http://localhost:9988')
var httpDrive = hyperdrive(storage, {latest: true})
httpDrive.on('ready', function () {
Dat(destDir, {key: httpDrive.key, sparse: true}, function (err, dat) {
if (err) return t.ifErr(err, 'error')
var localReplicate = dat.archive.replicate()
var httpReplicate = httpDrive.replicate()
localReplicate.pipe(httpReplicate).pipe(localReplicate)
localReplicate.end()
httpReplicate.end()
var pending = 2
localReplicate.on('end', function () {
console.log('local replicate ended')
if (--pending === 0) onEnd()
})
httpReplicate.on('end', function () {
console.log('http replicate ended')
if (--pending === 0) onEnd()
})
function onEnd () {
httpDrive.close(function () {
dat.close(function () {
cleanup()
})
})
}
})
})
})
})
tape('replicate file', function (t) {
makeTestServer(t, function runTest (datDir, destDir, cleanup) {
var storage = datHttp('http://localhost:9988')
var httpDrive = hyperdrive(storage, {latest: true})
httpDrive.on('ready', function () {
Dat(destDir, {key: httpDrive.key, sparse: true}, function (err, dat) {
if (err) return t.ifErr(err, 'error')
var localReplicate = dat.archive.replicate()
localReplicate.pipe(httpDrive.replicate()).pipe(localReplicate)
dat.archive.readFile('/hello.txt', function (err, content) {
if (err) return t.ifErr(err, 'error')
t.equals(content.toString(), 'hello', 'got hello')
t.equals(fs.readFileSync(path.join(destDir, 'hello.txt')).toString(), 'hello', 'file exists and matches')
httpDrive.close()
dat.close()
cleanup()
})
})
})
})
})
tape('replicate byte range', function (t) {
makeTestServer(t, function runTest (datDir, destDir, cleanup) {
var storage = datHttp('http://localhost:9988')
var httpDrive = hyperdrive(storage, {latest: true})
httpDrive.on('ready', function () {
Dat(destDir, {key: httpDrive.key, sparse: true}, function (err, dat) {
if (err) return t.ifErr(err, 'error')
var localReplicate = dat.archive.replicate()
localReplicate.pipe(httpDrive.replicate()).pipe(localReplicate)
var rs = dat.archive.createReadStream('/numbers.txt', {start: 1000, end: 1099})
rs.pipe(concat(function (content) {
t.equals(content.length, 100, 'length 100')
t.equals(content.readUInt32BE(0), 1000/4, '250')
t.ok(fs.readFileSync(path.join(destDir, 'numbers.txt')), 'file exists')
cleanup()
}))
})
})
})
})
function makeTestServer (t, cb) {
tmpDat(t, function (err, datDir, datCleanup) {
if (err) t.ifErr(err)
tmp(function (err, destDir, tmpCleanup) {
if (err) t.ifErr(err)
var server = http.createServer(function (req, res) {
console.log(req.method, req.url, '-', JSON.stringify(req.headers))
ecstatic({ root: datDir })(req, res)
})
server.listen(9988, function (err) {
if (err) t.ifErr(err)
var cleanup = function () {
tmpCleanup(function (err) {
if (err) t.ifErr(err)
datCleanup(function (err) {
if (err) t.ifErr(err)
server.close(function () {
t.end()
})
})
})
}
cb(datDir, destDir, cleanup)
})
})
})
}
function tmpDat (t, cb) {
tmp(function created (err, dir, cleanup) {
if (err) return cb(err)
var bigBuf = new Buffer(1024 * 1024 * 10)
for (var i = 0; i < bigBuf.length; i+=4) bigBuf.writeUInt32BE(i/4, i)
fs.writeFileSync(path.join(dir, 'numbers.txt'), bigBuf)
fs.writeFileSync(path.join(dir, 'hello.txt'), 'hello')
Dat(dir, function (err, dat) {
if (err) return cb(err)
dat.importFiles(function (err) {
if (err) return cb(err)
dat.close(function () {
cb(null, dir, cleanup)
})
})
})
})
}