Skip to content
This repository was archived by the owner on Jun 3, 2020. It is now read-only.

Commit a15b3f5

Browse files
committed
Fix dat download-as-zip
1 parent dac62bf commit a15b3f5

4 files changed

Lines changed: 46 additions & 23 deletions

File tree

dat/protocol.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const parseRange = require('range-parser')
44
const once = require('once')
55
const debug = require('../lib/debug-logger').debugLogger('dat-serve')
66
const intoStream = require('into-stream')
7-
const toZipStream = require('hyperdrive-to-zip-stream')
7+
const {toZipStream} = require('../lib/zip')
88
const slugify = require('slugify')
99

1010
const datDns = require('./dns')
@@ -173,7 +173,7 @@ exports.electronHandler = async function (request, respond) {
173173
})
174174
} else {
175175
// serve the zip
176-
var zs = toZipStream(archive, filepath)
176+
var zs = toZipStream(checkoutFS, filepath)
177177
zs.on('error', err => console.log('Error while producing .zip file', err))
178178
return respond({
179179
statusCode: 200,

lib/zip.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
const {join} = require('path')
2+
const yazl = require('yazl')
3+
4+
exports.toZipStream = function (archive, dirpath) {
5+
var zipfile = new yazl.ZipFile()
6+
7+
// create listing stream
8+
dirpath = dirpath || '/'
9+
archive.pda.readdir(dirpath, {recursive: true}).then(async (paths) => {
10+
for (let path of paths) {
11+
let readPath = join(dirpath, path)
12+
13+
// files only
14+
try {
15+
let entry = await archive.pda.stat(readPath)
16+
if (!entry.isFile()) {
17+
continue
18+
}
19+
} catch (e) {
20+
// ignore, file must have been removed
21+
continue
22+
}
23+
24+
// pipe each entry into the zip
25+
console.log('go go go', readPath, path)
26+
zipfile.addBuffer(await archive.pda.readFile(readPath, 'binary'), path)
27+
// NOTE
28+
// for some reason using archive.createReadStream() to feed into the zipfile addReadStream() was not working with multiple files
29+
// no idea why, maybe a sign of a bug in the dat-daemon's zip rpc
30+
// -prf
31+
}
32+
zipfile.end()
33+
}).catch(onerror)
34+
35+
// on error, push to the output stream
36+
function onerror (e) {
37+
console.error('Error while producing zip stream', e)
38+
zipfile.outputStream.emit('error', e)
39+
}
40+
41+
return zipfile.outputStream
42+
}

package-lock.json

Lines changed: 0 additions & 19 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@
4747
"hypercore-protocol": "^6.9.0",
4848
"hyperdrive": "^9.14.0",
4949
"hyperdrive-network-speed": "^2.1.0",
50-
"hyperdrive-to-zip-stream": "^2.1.1",
5150
"identify-filetype": "^1.0.0",
5251
"into-stream": "^3.1.0",
5352
"lodash.debounce": "^4.0.8",
@@ -81,7 +80,8 @@
8180
"supports-sparse-files": "^1.0.2",
8281
"textextensions": "^2.4.0",
8382
"through2": "^2.0.5",
84-
"utp-native": "^2.1.3"
83+
"utp-native": "^2.1.3",
84+
"yazl": "^2.5.1"
8585
},
8686
"devDependencies": {
8787
"eslint": "^4.19.1",

0 commit comments

Comments
 (0)