Skip to content

Commit 2022ce1

Browse files
committed
Fixes for browser
1 parent b715eaa commit 2022ce1

5 files changed

Lines changed: 41 additions & 83 deletions

File tree

zeronet-common/lib/storage/wrapper.js

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
"use strict"
22

33
const pull = require("pull-stream")
4-
const queue = require("pull-queue")
54
const defer = require("pull-defer")
65

76
module.exports = function StorageWrapper(storage) {
87
const self = this
8+
self.storage = storage
99

1010
/* start/stop */
1111

@@ -32,40 +32,31 @@ module.exports = function StorageWrapper(storage) {
3232
if (storage.file.readStream) {
3333
const d = defer.source()
3434
storage.file.readStream(zite, v, path, (err, stream) => {
35-
if (err) throw err
35+
if (err) return d.resolve(pull.error(err))
3636
d.resolve(stream)
3737
})
3838
return d
3939
} else {
40-
return pull(
41-
pull.values(Array.isArray(path) ? path : [path]),
42-
queue(function (end, file, cb) {
43-
if (end) return cb(end)
44-
storage.file.read(zite, v, path, cb)
45-
})
46-
)
40+
const d = defer.source()
41+
storage.file.read(zite, v, path, (err, res) => {
42+
if (err) d.resolve(pull.error(err))
43+
else d.resolve(pull.values([res]))
44+
})
45+
return d
4746
}
4847
}
4948
self.writeStream = (zite, v, path) => {
5049
if (storage.file.writeStream) {
5150
const d = defer.sink()
5251
storage.file.writeStream(zite, v, path, (err, stream) => {
53-
if (err) throw err
52+
if (err) d.resolve(pull.error(err))
5453
d.resolve(stream)
5554
})
5655
return d
5756
} else {
58-
let d = []
59-
return queue(function (end, data, cb) {
60-
if (end) {
61-
if (typeof end == "boolean")
62-
storage.file.write(zite, v, path, Buffer.concat(d), err => cb(err || end))
63-
else
64-
cb(end)
65-
} else {
66-
d.push(data)
67-
return cb(null, data)
68-
}
57+
return pull.collect((err, chunks) => {
58+
if (err) return console.error(err)
59+
storage.file.write(zite, v, path, Buffer.concat(chunks), () => {})
6960
})
7061
}
7162
}

zeronet-fileserver/lib/index.js

Lines changed: 15 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -86,67 +86,22 @@ module.exports = function FileServer(protocol, zeronet) {
8686
if (!zeronet.zites[data.site]) return cb(new Error("Unknown site"))
8787
//FIXME: tmp hack. can be easily abused for force-seeding
8888
const zite = zeronet.zites[data.site]
89-
const fs = zite.fs
90-
//if (data.location % FILE_CHUNK) return cb(new Error("Currently supports only full chunks (" + FILE_CHUNK + ")"))
91-
//const chunkid = data.location / FILE_CHUNK
92-
fs.getFile(data.inner_path, (err, stream) => {
93-
if (err) return cb(err)
94-
//let leftover = Buffer.from("")
95-
//let size = 0
96-
pull(
97-
stream,
98-
pull.collect((err, chunks) => {
99-
if (err) return cb(err)
100-
const file = Buffer.concat(chunks)
101-
if (file.length < data.location) return cb(new Error("Oversize"))
102-
return cb(null, {
103-
body: file.slice(data.location, data.location + FILE_CHUNK),
104-
location: data.location + file.slice(data.location, data.location + FILE_CHUNK).length,
105-
size: file.length
106-
})
89+
log("got a getFile for %s@%s", data.site, data.inner_path)
90+
const stream = zite.tree.storage.readStream(data.site, 0, data.inner_path)
91+
pull(
92+
stream,
93+
pull.collect((err, chunks) => {
94+
log("got OUT a getFile for %s@%s", data.site, data.inner_path, err, chunks)
95+
if (err) return cb(err)
96+
const file = Buffer.concat(chunks)
97+
if (file.length < data.location) return cb(new Error("Oversize"))
98+
return cb(null, {
99+
body: file.slice(data.location, data.location + FILE_CHUNK),
100+
location: data.location + file.slice(data.location, data.location + FILE_CHUNK).length,
101+
size: file.length
107102
})
108-
/*queue((end, data, cb) => {
109-
console.log(data, leftover)
110-
const rt = () => {
111-
let send = []
112-
data = Buffer.concat([leftover, data])
113-
if (data.length < FILE_CHUNK) return send
114-
if (data.length == FILE_CHUNK) {
115-
send.push(data)
116-
leftover = Buffer.from("")
117-
return send
118-
}
119-
while (data.length >= FILE_CHUNK) {
120-
send.push(data.slice(0, FILE_CHUNK))
121-
data = data.slice(FILE_CHUNK + 1)
122-
}
123-
leftover = data
124-
return send
125-
}
126-
if (end && leftover.length) {
127-
cb(null, [leftover])
128-
leftover = null
129-
} else if (end) {
130-
cb(end)
131-
} else {
132-
size += data.length
133-
cb(null, rt())
134-
}
135-
}, {
136-
sendMultiple: true
137-
}),
138-
pull.collect((err, chunks) => {
139-
if (err) return cb(err)
140-
if (chunkid * FILE_CHUNK > size) return cb(new Error("Oversize"))
141-
console.log(chunks[chunkid], chunks)
142-
return cb(null, {
143-
body: chunks[chunkid],
144-
location: data.location + chunks[chunkid].length,
145-
size
146-
})
147-
})*/
148-
)
149-
})
103+
})
104+
)
150105
})
151106

152107
protocol.handle("hasZite", { in: {

zeronet-storage-memory/lib/index.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ module.exports = function ZeroNetStorageFS() {
1818
const getPath = (a, b) => a + "/" + b
1919

2020
self.file = {
21-
exists: (zite, version, inner_path, cb) => fs.exists(getPath(zite, inner_path), res => cb(null, res)),
21+
exists: (zite, version, inner_path, cb) => fs.exists(getPath(zite, inner_path), cb),
2222
read: (zite, version, inner_path, cb) => fs.readFile(getPath(zite, inner_path), cb),
2323
write: (zite, version, inner_path, data, cb) => fs.writeFile(getPath(zite, inner_path), data, cb),
2424
remove: (zite, version, inner_path, cb) => fs.unlink(getPath(zite, inner_path), cb)
@@ -35,7 +35,13 @@ module.exports = function ZeroNetStorageFS() {
3535
json = {}
3636
jfs = new FSMock(json)
3737
file = {}
38-
fs = new FSMock(json)
38+
fs = new FSMock(file)
39+
self.mocks = {
40+
fs,
41+
jfs,
42+
json,
43+
file
44+
}
3945
cb()
4046
}
4147

@@ -44,6 +50,7 @@ module.exports = function ZeroNetStorageFS() {
4450
jfs = null
4551
file = null
4652
fs = null
53+
self.mocks = null
4754
cb()
4855
}
4956

zeronet-swarm/lib/lp2p/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,13 @@ function Libp2pSwarm(opt, protocol, zeronet) {
8080
const lp2p = self.lp2p = self.libp2p = new libp2p(modules, peerInfo /*, peerBook*/ )
8181
const swarm = lp2p
8282
swarm.on("peer:discovery", pi => {
83+
if (pi.id.toB58String() == peerInfo.id.toB58String()) return
8384
const next = () => swarm.dial(pi, () => {})
8485
if (swarm.isStarted()) next()
8586
else swarm.once("start", () => process.nextTick(next))
8687
})
8788
swarm.on("peer:connect", peer => {
89+
if (peer.id.toB58String() == peerInfo.id.toB58String()) return
8890
const npeer = zeronet.peerPool.add(peer)
8991
const next = () => npeer.dial(() => {})
9092
if (swarm.isStarted()) next()

zeronet-zite/lib/tree/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,9 @@ class FileTreeRoot extends FileTreeBranchObject {
268268
this.json = json
269269
this.updateTree()
270270
}
271+
maybeValid( /*path*/ ) {
272+
return false //TODO: add
273+
}
271274
getRuleBook(path, data) {
272275
let valid_signers = []
273276
if (path == "content.json") {

0 commit comments

Comments
 (0)