forked from ipfs/js-ipfs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
License: MIT Signed-off-by: Alan Shaw <alan.shaw@protocol.ai>
- Loading branch information
Alan Shaw
authored
Sep 4, 2019
1 parent
4271c7a
commit 9616aed
Showing
22 changed files
with
260 additions
and
312 deletions.
There are no files selected for viewing
This file contains 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 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 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,3 @@ | ||
'use strict' | ||
|
||
module.exports = () => () => { throw new Error('unavailable in the browser') } |
This file contains 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,9 @@ | ||
'use strict' | ||
|
||
const configure = require('../lib/configure') | ||
const globSource = require('ipfs-utils/src/files/glob-source') | ||
|
||
module.exports = configure(({ ky }) => { | ||
const add = require('../add')({ ky }) | ||
return (path, options) => add(globSource(path, options), options) | ||
}) |
This file contains 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,22 @@ | ||
'use strict' | ||
|
||
const kyDefault = require('ky-universal').default | ||
const configure = require('./lib/configure') | ||
const toIterable = require('./lib/stream-to-iterable') | ||
|
||
module.exports = configure(({ ky }) => { | ||
const add = require('./add')({ ky }) | ||
|
||
return (url, options) => (async function * () { | ||
options = options || {} | ||
|
||
const { body } = await kyDefault.get(url) | ||
|
||
const input = { | ||
path: decodeURIComponent(new URL(url).pathname.split('/').pop() || ''), | ||
content: toIterable(body) | ||
} | ||
|
||
yield * add(input, options) | ||
})() | ||
}) |
This file contains 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,31 @@ | ||
'use strict' | ||
/* eslint-env browser */ | ||
|
||
const { Buffer } = require('buffer') | ||
const normaliseInput = require('ipfs-utils/src/files/normalise-input') | ||
|
||
exports.toFormData = async input => { | ||
const files = normaliseInput(input) | ||
const formData = new FormData() | ||
let i = 0 | ||
|
||
for await (const file of files) { | ||
if (file.content) { | ||
// In the browser there's _currently_ no streaming upload, buffer up our | ||
// async iterator chunks and append a big Blob :( | ||
// One day, this will be browser streams | ||
const bufs = [] | ||
for await (const chunk of file.content) { | ||
bufs.push(Buffer.isBuffer(chunk) ? chunk.buffer : chunk) | ||
} | ||
|
||
formData.append(`file-${i}`, new Blob(bufs, { type: 'application/octet-stream' }), file.path) | ||
} else { | ||
formData.append(`dir-${i}`, new Blob([], { type: 'application/x-directory' }), file.path) | ||
} | ||
|
||
i++ | ||
} | ||
|
||
return formData | ||
} |
This file contains 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,42 @@ | ||
'use strict' | ||
|
||
const FormData = require('form-data') | ||
const { Buffer } = require('buffer') | ||
const toStream = require('it-to-stream') | ||
const normaliseInput = require('ipfs-utils/src/files/normalise-input') | ||
|
||
exports.toFormData = async input => { | ||
const files = normaliseInput(input) | ||
const formData = new FormData() | ||
let i = 0 | ||
|
||
for await (const file of files) { | ||
if (file.content) { | ||
// In Node.js, FormData can be passed a stream so no need to buffer | ||
formData.append( | ||
`file-${i}`, | ||
// FIXME: add a `path` property to the stream so `form-data` doesn't set | ||
// a Content-Length header that is only the sum of the size of the | ||
// header/footer when knownLength option (below) is null. | ||
Object.assign( | ||
toStream.readable(file.content), | ||
{ path: file.path || `file-${i}` } | ||
), | ||
{ | ||
filepath: encodeURIComponent(file.path), | ||
contentType: 'application/octet-stream', | ||
knownLength: file.content.length // Send Content-Length header if known | ||
} | ||
) | ||
} else { | ||
formData.append(`dir-${i}`, Buffer.alloc(0), { | ||
filepath: encodeURIComponent(file.path), | ||
contentType: 'application/x-directory' | ||
}) | ||
} | ||
|
||
i++ | ||
} | ||
|
||
return formData | ||
} |
This file contains 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,54 @@ | ||
'use strict' | ||
|
||
const ndjson = require('iterable-ndjson') | ||
const configure = require('../lib/configure') | ||
const toIterable = require('../lib/stream-to-iterable') | ||
const { toFormData } = require('./form-data') | ||
const toCamel = require('../lib/object-to-camel') | ||
|
||
module.exports = configure(({ ky }) => { | ||
return (input, options) => (async function * () { | ||
options = options || {} | ||
|
||
const searchParams = new URLSearchParams(options.searchParams) | ||
|
||
searchParams.set('stream-channels', true) | ||
if (options.chunker) searchParams.set('chunker', options.chunker) | ||
if (options.cidVersion) searchParams.set('cid-version', options.cidVersion) | ||
if (options.cidBase) searchParams.set('cid-base', options.cidBase) | ||
if (options.enableShardingExperiment != null) searchParams.set('enable-sharding-experiment', options.enableShardingExperiment) | ||
if (options.hashAlg) searchParams.set('hash', options.hashAlg) | ||
if (options.onlyHash != null) searchParams.set('only-hash', options.onlyHash) | ||
if (options.pin != null) searchParams.set('pin', options.pin) | ||
if (options.progress) searchParams.set('progress', true) | ||
if (options.quiet != null) searchParams.set('quiet', options.quiet) | ||
if (options.quieter != null) searchParams.set('quieter', options.quieter) | ||
if (options.rawLeaves != null) searchParams.set('raw-leaves', options.rawLeaves) | ||
if (options.shardSplitThreshold) searchParams.set('shard-split-threshold', options.shardSplitThreshold) | ||
if (options.silent) searchParams.set('silent', options.silent) | ||
if (options.trickle != null) searchParams.set('trickle', options.trickle) | ||
if (options.wrapWithDirectory != null) searchParams.set('wrap-with-directory', options.wrapWithDirectory) | ||
|
||
const res = await ky.post('add', { | ||
timeout: options.timeout, | ||
signal: options.signal, | ||
headers: options.headers, | ||
searchParams, | ||
body: await toFormData(input) | ||
}) | ||
|
||
for await (let file of ndjson(toIterable(res.body))) { | ||
file = toCamel(file) | ||
// console.log(file) | ||
if (options.progress && file.bytes) { | ||
options.progress(file.bytes) | ||
} else { | ||
yield toCoreInterface(file) | ||
} | ||
} | ||
})() | ||
}) | ||
|
||
function toCoreInterface ({ name, hash, size }) { | ||
return { path: name, hash, size: parseInt(size) } | ||
} |
This file contains 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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.