Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

Commit

Permalink
fix: prevent returning from http write command early
Browse files Browse the repository at this point in the history
Also splits bufferStream out into it's own module as sometimes it's useful
to have a pull stream that emits a known number of buffers.

License: MIT
Signed-off-by: achingbrain <[email protected]>
  • Loading branch information
achingbrain committed Jul 31, 2018
1 parent 36bde9d commit 1018e7d
Show file tree
Hide file tree
Showing 12 changed files with 17 additions and 71 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"detect-webworker": "^1.0.0",
"dirty-chai": "^2.0.1",
"ipfs": "~0.30.0",
"pull-buffer-stream": "^1.0.0",
"safe-buffer": "^5.1.1",
"tmp": "~0.0.33"
},
Expand Down
1 change: 0 additions & 1 deletion src/core/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ module.exports = {
countStreamBytes: require('./count-stream-bytes'),
createLock: require('./create-lock'),
createNode: require('./create-node'),
endPullStream: require('./end-pull-stream'),
formatCid: require('./format-cid'),
limitStreamBytes: require('./limit-stream-bytes'),
loadNode: require('./load-node'),
Expand Down
3 changes: 1 addition & 2 deletions src/core/utils/limit-stream-bytes.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
'use strict'

const asyncMap = require('pull-stream/throughs/async-map')
const endPullStream = require('./end-pull-stream')

const limitStreamBytes = (limit) => {
let bytesRead = 0

return asyncMap((buffer, cb) => {
if (bytesRead > limit) {
endPullStream(cb)
cb(true) // eslint-disable-line standard/no-callback-literal
}

// If we only need to return part of this buffer, slice it to make it smaller
Expand Down
8 changes: 6 additions & 2 deletions src/core/write/update-tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const updateTree = (ipfs, root, fileSize, streamStart, streamEnd, source, option
nodeStart: streamPosition,
nodeEnd: fileSize
}, findDAGNodesWithRequestedData),
paramap(updateNodeData(source)),
asyncMap(updateNodeData(source)),
filter(Boolean),
asyncMap((link, next) => {
if (!link.parent || link.index === undefined) {
Expand Down Expand Up @@ -179,7 +179,11 @@ const updateTree = (ipfs, root, fileSize, streamStart, streamEnd, source, option
sourceEnd = undefined
}

sourceData.copy(newData, targetStart, sourceStart, sourceEnd)
try {
sourceData.copy(newData, targetStart, sourceStart, sourceEnd)
} catch (error) {
return cb(error)
}

cb(null, new UnixFs(meta.type, newData).marshal())
},
Expand Down
13 changes: 1 addition & 12 deletions src/http/write.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ const mfsWrite = (api) => {
strategy,
flush
})
.then(() => reply())
.catch(error => {
reply({
Message: error.message,
Expand All @@ -79,18 +80,6 @@ const mfsWrite = (api) => {
Type: 'error'
}).code(500).takeover()
})

parser.on('end', () => {
if (!filesParsed) {
return reply({
Message: "File argument 'data' is required.",
Code: 0,
Type: 'error'
}).code(400).takeover()
}

reply()
})
},
validate: {
options: {
Expand Down
4 changes: 2 additions & 2 deletions test/cp.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
const chai = require('chai')
chai.use(require('dirty-chai'))
const expect = chai.expect
const bufferStream = require('pull-buffer-stream')

const {
createMfs,
bufferStream
createMfs
} = require('./helpers')

describe('cp', function () {
Expand Down
43 changes: 0 additions & 43 deletions test/helpers/buffer-stream.js

This file was deleted.

1 change: 0 additions & 1 deletion test/helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ const createMfs = promisify((cb) => {

module.exports = {
createMfs,
bufferStream: require('./buffer-stream'),
collectLeafCids: require('./collect-leaf-cids'),
EMPTY_DIRECTORY_HASH: 'QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn',
EMPTY_DIRECTORY_HASH_BASE32: 'bafybeiczsscdsbs7ffqz55asqdf3smv6klcw3gofszvwlyarci47bgf354'
Expand Down
4 changes: 2 additions & 2 deletions test/mv.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
const chai = require('chai')
chai.use(require('dirty-chai'))
const expect = chai.expect
const bufferStream = require('pull-buffer-stream')
const {
createMfs,
bufferStream
createMfs
} = require('./helpers')

describe('mv', function () {
Expand Down
4 changes: 1 addition & 3 deletions test/read.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ chai.use(require('dirty-chai'))
const expect = chai.expect
const path = require('path')
const loadFixture = require('aegir/fixtures')
const {
bufferStream
} = require('./helpers')
const bufferStream = require('pull-buffer-stream')
const pull = require('pull-stream/pull')
const collect = require('pull-stream/sinks/collect')
const {
Expand Down
4 changes: 2 additions & 2 deletions test/rm.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
const chai = require('chai')
chai.use(require('dirty-chai'))
const expect = chai.expect
const bufferStream = require('pull-buffer-stream')
const {
createMfs,
bufferStream
createMfs
} = require('./helpers')
const {
FILE_SEPARATOR
Expand Down
2 changes: 1 addition & 1 deletion test/write.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ const path = require('path')
const loadFixture = require('aegir/fixtures')
const isNode = require('detect-node')
const values = require('pull-stream/sources/values')
const bufferStream = require('pull-buffer-stream')
const {
bufferStream,
collectLeafCids,
createMfs
} = require('./helpers')
Expand Down

0 comments on commit 1018e7d

Please sign in to comment.