This repository was archived by the owner on Mar 10, 2020. It is now read-only.
  
  
  - 
                Notifications
    You must be signed in to change notification settings 
- Fork 296
[WIP] feat: add support for chunked uploads #851
          
     Open
      
      
            hugomrdias
  wants to merge
  15
  commits into
  master
  
    
      
        
          
  
    
      Choose a base branch
      
     
    
      
        
      
      
        
          
          
        
        
          
            
              
              
              
  
           
        
        
          
            
              
              
           
        
       
     
  
        
          
            
          
            
          
        
       
    
      
from
fix/http-upload
  
      
      
   
  
    
  
  
  
 
  
      
    base: master
Could not load branches
            
              
  
    Branch not found: {{ refName }}
  
            
                
      Loading
              
            Could not load tags
            
            
              Nothing to show
            
              
  
            
                
      Loading
              
            Are you sure you want to change the base?
            Some commits from the old base branch may be removed from the timeline,
            and old review comments may become outdated.
          
          
  
     Open
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            15 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      775213a
              
                feat: add support for chunked uploads
              
              
                hugomrdias 01a575e
              
                fix: fix sendChunkRequest
              
              
                hugomrdias 695b23e
              
                fix: cleanup code, headers, chunkSize options
              
              
                hugomrdias f9df326
              
                feat: support File directly from input elements
              
              
                hugomrdias 52a6117
              
                fix: error handling and stream backpressure
              
              
                hugomrdias 0da5783
              
                feat: refactor to send to SendStream, non-chunked, add*Stream methods
              
              
                hugomrdias d58e8cb
              
                feat: improved header and curl test files
              
              
                hugomrdias 300af44
              
                fix: use nanoid for uuid
              
              
                hugomrdias 3cd19e7
              
                feat: callbackify and top level jsdoc and more
              
              
                hugomrdias 4191525
              
                fix: integration with old add
              
              
                hugomrdias 0a4f008
              
                feat: tests, concurrency, simplification
              
              
                hugomrdias 90c4036
              
                fix: remove concurrency for now
              
              
                hugomrdias d596295
              
                fix: feedback changes
              
              
                hugomrdias 7489447
              
                fix: add some property descriptions
              
              
                hugomrdias a3bf0a6
              
                fix: add jsdoc to pushFile
              
              
                hugomrdias File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| 'use strict' | ||
|  | ||
| const { Readable } = require('stream') | ||
| const toPull = require('stream-to-pull-stream') | ||
| const promiseNodeify = require('promise-nodeify') | ||
| const concatStream = require('concat-stream') | ||
| const pump = require('pump') | ||
| const SendStream = require('../utils/send-stream-experimental') | ||
|  | ||
| /** @module api/add */ | ||
|  | ||
| /** | ||
| * Converts an array to a stream | ||
| * | ||
| * @ignore | ||
| * @param {Array} data | ||
| * @returns {Readable} | ||
| */ | ||
| const arrayToStream = (data) => { | ||
| let i = 0 | ||
| return new Readable({ | ||
| objectMode: true, | ||
| read () { | ||
| this.push(i < data.length ? data[i++] : null) | ||
| } | ||
| }) | ||
| } | ||
|  | ||
| /** | ||
| * @typedef {Object} AddOptions | ||
| * @property {number} chunkSize - Value of array element | ||
| * @property {number} [cidVersion=0] - Defaults to 0. The CID version to use when storing the data (storage keys are based on the CID, including it's version) | ||
| * @property {function(bytes: number): void} progress - function that will be called with the byte length of chunks as a file is added to ipfs. | ||
| * @property {Boolean} recursive - When a Path is passed, this option can be enabled to add recursively all the files. | ||
| * @property {string} hashAlg - Multihash hashing algorithm to use. (default: sha2-256) The list of all possible values {@link https://github.com/multiformats/js-multihash/blob/master/src/constants.js#L5-L343 hashAlg values} | ||
| * @property {Boolean} wrapWithDirectory - Adds a wrapping node around the content. | ||
| * @property {Boolean} onlyHash - Doesn't actually add the file to IPFS, but rather calculates its hash. | ||
| * @property {Boolean} [pin=true] - Defaults to true. Pin this object when adding. | ||
| * @property {Boolean} [rawLeaves=false] - Defaults to false. If true, DAG leaves will contain raw file data and not be wrapped in a protobuf | ||
| * @property {string} [chunker=size-262144] Chunking algorithm used to build ipfs DAGs. Available formats: | ||
| * - size-{size} | ||
| * - rabin | ||
| * - rabin-{avg} | ||
| * - rabin-{min}-{avg}-{max} | ||
| */ | ||
|  | ||
| /** | ||
| * @typedef {Object} AddResult | ||
| * @property {string} path - Object path | ||
| * @property {string} hash - Object CID | ||
| * @property {number} size - Object size | ||
| */ | ||
|  | ||
| /** | ||
| * @callback AddCallback | ||
| * @param {Error} err | ||
| * @param {AddResult[]} res | ||
| */ | ||
|  | ||
| /** @typedef {Function} PullStream */ | ||
| /** @typedef {(Object[]|Readable|File|PullStream|Buffer)} AddData */ | ||
| /** @typedef {function(AddData, AddOptions, AddCallback): (Promise.<AddResult[]>|void)} AddFunction */ | ||
|  | ||
| /** | ||
| * Add to data to ipfs | ||
| * | ||
| * @param {Function} send | ||
| * @returns {AddFunction} | ||
| * @memberof api/add | ||
| */ | ||
| const add = (send) => (data, options, callback) => { | ||
| if (typeof options === 'function') { | ||
| callback = options | ||
| options = {} | ||
| } | ||
|  | ||
| let result = [] | ||
| const r = new Promise((resolve, reject) => { | ||
| pump( | ||
| arrayToStream([].concat(data)), | ||
| new SendStream(send, options), | ||
| concatStream(r => (result = r)), | ||
| (err) => { | ||
| if (err) { | ||
| return reject(err) | ||
| } | ||
| resolve(result) | ||
| } | ||
| ) | ||
| }) | ||
|  | ||
| return promiseNodeify(r, callback) | ||
| } | ||
|  | ||
| /** | ||
| * Add to data to ipfs | ||
| * | ||
| * @param {Function} send | ||
| * @returns {function(AddOptions): Readable} | ||
| * @memberof api/add | ||
| */ | ||
| const addReadableStream = (send) => (options = {}) => { | ||
| return new SendStream(send, options) | ||
| } | ||
|  | ||
| /** | ||
| * Add to data to ipfs | ||
| * | ||
| * @param {Function} send | ||
| * @returns {function(AddOptions): PullStream} | ||
| * @memberof api/add | ||
| */ | ||
| const addPullStream = (send) => (options = {}) => { | ||
| return toPull(new SendStream(send, options)) | ||
| } | ||
|  | ||
| module.exports = { | ||
| add, | ||
| addReadableStream, | ||
| addPullStream | ||
| } | ||
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.