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
 
Handle case where ky responses have no body with a getter for a ReadableStream #1224
          
     Open
      
      
            pcowgill
  wants to merge
  13
  commits into
  ipfs-inactive:master
  
    
      
        
          
  
    
      Choose a base branch
      
     
    
      
        
      
      
        
          
          
        
        
          
            
              
              
              
  
           
        
        
          
            
              
              
           
        
       
     
  
        
          
            
          
            
          
        
       
    
      
from
pcowgill:feature/handle-no-response-body
  
      
      
   
  
    
  
  
  
 
  
      
    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 5 commits
      Commits
    
    
            Show all changes
          
          
            13 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      f0d4016
              
                Handling case where res.body is undefined
              
              
                pcowgill 37b7d27
              
                Fix tests, add test, rename to async iterable, fix addFromURL
              
              
                pcowgill 3ee0016
              
                Added local editor config dir to .gitignore
              
              
                pcowgill 74c286e
              
                Handling case where body and arrayBuffer both are not defined
              
              
                pcowgill 528a0b9
              
                Reverting whitespace changes
              
              
                pcowgill 4ea0b03
              
                Reverting more automatic style changes
              
              
                pcowgill 02b6269
              
                Reverting paren change for single param arrow function
              
              
                pcowgill 4819f0e
              
                Reverting generator function style change
              
              
                pcowgill 7a0d504
              
                Fixed linting errors
              
              
                pcowgill 5fffc22
              
                Merge branch 'master' into feature/handle-no-response-body
              
              
                pcowgill 2508489
              
                Augmenting the behavior of stream-to-it/source
              
              
                pcowgill 6585971
              
                Fixed relative imports
              
              
                pcowgill 843a97e
              
                Fixed test failures
              
              
                pcowgill 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
    
  
  
    
              
              
  
    
      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 | 
|---|---|---|
| 
          
            
          
           | 
    @@ -2,12 +2,12 @@ | |
| 
     | 
||
| const ndjson = require('iterable-ndjson') | ||
| const configure = require('../lib/configure') | ||
| const toIterable = require('../lib/stream-to-iterable') | ||
| const toAsyncIterable = require('../lib/stream-to-async-iterable') | ||
| const { toFormData } = require('./form-data') | ||
| const toCamel = require('../lib/object-to-camel') | ||
| 
     | 
||
| module.exports = configure(({ ky }) => { | ||
| return async function * add (input, options) { | ||
| return async function* add(input, options) { | ||
                
      
                  pcowgill marked this conversation as resolved.
               
              
                Outdated
          
            Show resolved
            Hide resolved
         | 
||
| options = options || {} | ||
| 
     | 
||
| const searchParams = new URLSearchParams(options.searchParams) | ||
| 
          
            
          
           | 
    @@ -40,7 +40,7 @@ module.exports = configure(({ ky }) => { | |
| body: await toFormData(input) | ||
| }) | ||
| 
     | 
||
| for await (let file of ndjson(toIterable(res.body))) { | ||
| for await (let file of ndjson(toAsyncIterable(res))) { | ||
| file = toCamel(file) | ||
| // console.log(file) | ||
| if (options.progress && file.bytes) { | ||
| 
        
          
        
         | 
    @@ -52,7 +52,7 @@ module.exports = configure(({ ky }) => { | |
| } | ||
| }) | ||
| 
     | 
||
| function toCoreInterface ({ name, hash, size, mode, mtime }) { | ||
| function toCoreInterface({ name, hash, size, mode, mtime }) { | ||
                
       | 
||
| const output = { | ||
| path: name, | ||
| hash, | ||
| 
          
            
          
           | 
    ||
  
    
      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
    
  
  
    
              
  
    
      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
    
  
  
    
              
  
    
      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
    
  
  
    
              
  
    
      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
    
  
  
    
              
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| 'use strict' | ||
| 
     | 
||
| module.exports = function toAsyncIterable (res) { | ||
| const { body } = res | ||
| 
     | 
||
| // An env where res.body getter for ReadableStream with getReader | ||
| // is not supported, for example in React Native | ||
| if (!body) { | ||
| if (res.arrayBuffer) { | ||
| 
         There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @achingbrain @hugomrdias Do we want to   | 
||
| return (async function * () { | ||
| const arrayBuffer = await res.arrayBuffer() | ||
| yield arrayBuffer | ||
| })() | ||
| } else { | ||
| throw new Error('Neither Response.body nor Response.arrayBuffer is defined') | ||
| } | ||
                
      
                  pcowgill marked this conversation as resolved.
               
          
            Show resolved
            Hide resolved
         | 
||
| } | ||
| 
     | 
||
| // Node.js stream | ||
| if (body[Symbol.asyncIterator]) return body | ||
| 
     | 
||
| // Browser ReadableStream | ||
| if (body.getReader) { | ||
| return (async function * () { | ||
| const reader = body.getReader() | ||
| 
     | 
||
| try { | ||
| while (true) { | ||
| const { done, value } = await reader.read() | ||
| if (done) return | ||
| yield value | ||
| } | ||
| } finally { | ||
| reader.releaseLock() | ||
| } | ||
| })() | ||
| } | ||
| 
     | 
||
| throw new Error('unknown stream') | ||
| } | ||
This file was deleted.
      
      Oops, something went wrong.
      
    
  
  
    
      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.