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

Commit cd0d90a

Browse files
improve chunker
1 parent 349d4a1 commit cd0d90a

File tree

4 files changed

+17
-13
lines changed

4 files changed

+17
-13
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
"ipfs-unixfs": "^0.1.4",
5454
"is-ipfs": "^0.2.0",
5555
"multihashes": "^0.2.2",
56+
"pull-block": "^1.0.0",
5657
"pull-pushable": "^2.0.1",
5758
"pull-stream": "^3.4.3",
5859
"pull-traverse": "^1.0.3",

src/chunker-fixed-size.js

+2-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
'use strict'
22

3-
const window = require('pull-window')
4-
const pull = require('pull-stream')
3+
const block = require('pull-block')
54

65
exports = module.exports = function (size) {
7-
return pull(
8-
pull.flatten(),
9-
window.recent(size)
10-
)
6+
return block(size, {zeroPadding: false})
117
}

src/exporters/file.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const pull = require('pull-stream')
55

66
function extractContent (node) {
77
const raw = UnixFS.unmarshal(node.data)
8-
return pull.values(raw.data)
8+
return pull.values([raw.data])
99
}
1010

1111
// Logic to export a single (possibly chunked) unixfs file.

test/test-fixed-size-chunker.js

+13-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* eslint-env mocha */
22
'use strict'
33

4-
const FixedSizeChunker = require('./../src/chunker-fixed-size')
4+
const chunker = require('./../src/chunker-fixed-size')
55
const fs = require('fs')
66
const expect = require('chai').expect
77
const path = require('path')
@@ -23,9 +23,10 @@ describe('chunker: fixed size', () => {
2323

2424
pull(
2525
pull.values([b1, b2, b3]),
26-
FixedSizeChunker(256),
26+
chunker(256),
2727
pull.collect((err, chunks) => {
2828
expect(err).to.not.exists
29+
expect(chunks).to.have.length(8)
2930
chunks.forEach((chunk) => {
3031
expect(chunk).to.have.length(256)
3132
})
@@ -36,11 +37,12 @@ describe('chunker: fixed size', () => {
3637

3738
it('256 Bytes chunks', (done) => {
3839
pull(
39-
pull.infinite(() => 1),
40+
pull.infinite(() => Buffer([1])),
4041
pull.take(256 * 12),
41-
FixedSizeChunker(256),
42+
chunker(256),
4243
pull.collect((err, chunks) => {
4344
expect(err).to.not.exists
45+
expect(chunks).to.have.length(12)
4446
chunks.forEach((chunk) => {
4547
expect(chunk).to.have.length(256)
4648
})
@@ -53,9 +55,11 @@ describe('chunker: fixed size', () => {
5355
const KiB256 = 262144
5456
pull(
5557
pull.values(rawFile),
56-
FixedSizeChunker(KiB256),
58+
chunker(KiB256),
5759
pull.collect((err, chunks) => {
5860
expect(err).to.not.exists
61+
62+
expect(chunks).to.have.length(4)
5963
chunks.forEach((chunk) => {
6064
expect(chunk).to.have.length(KiB256)
6165
})
@@ -70,10 +74,13 @@ describe('chunker: fixed size', () => {
7074

7175
pull(
7276
pull.values(file),
73-
FixedSizeChunker(KiB256),
77+
chunker(KiB256),
7478
pull.collect((err, chunks) => {
7579
expect(err).to.not.exists
80+
81+
expect(chunks).to.have.length(5)
7682
let counter = 0
83+
7784
chunks.forEach((chunk) => {
7885
if (chunk.length < KiB256) {
7986
counter++

0 commit comments

Comments
 (0)