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

Commit 6b24463

Browse files
authored
fix: send blobs when running ipfs-http-client in the browser (#3184)
To support streaming of native types with no buffering, normalise add input to blobs and upload using native FormData when the http client is run in the browser. That is, if the user passes a blob to the http client in the browser leave it alone as enumerating blob contents cause the file data to be read. Browser FormData objects do not allow you to specify headers for each multipart part which means we can't pass UnixFS metadata via the headers so we turn the metadata into a querystring and append it to the field name for each multipart part as a workaround. Fixes #3138 BREAKING CHANGES: - Removes the `mode`, `mtime` and `mtime-nsec` headers from multipart requests - Passes `mode`, `mtime` and `mtime-nsec` as querystring parameters appended to the field name of multipart requests
1 parent 6a498e9 commit 6b24463

18 files changed

+527
-388
lines changed

docs/core-api/FILES.md

+18-6
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ _Explore the Mutable File System through interactive coding challenges in our [P
77
- [The Regular API](#the-regular-api)
88
- [`ipfs.add(data, [options])`](#ipfsadddata-options)
99
- [Parameters](#parameters)
10+
- [FileStream](#filestream)
11+
- [FileObject](#fileobject)
12+
- [FileContent](#filecontent)
1013
- [Options](#options)
1114
- [Returns](#returns)
1215
- [`ipfs.addAll(source, [options])`](#ipfsaddallsource-options)
@@ -108,12 +111,19 @@ The regular, top-level API for add, cat, get and ls Files on IPFS
108111

109112
`data` may be:
110113

111-
* `Blob`
112-
* `String`
113-
* `Uint8Array`
114+
* `FileContent` (see below for definition)
114115
* `FileObject` (see below for definition)
115-
* `Iterable<Uint8Array>`
116-
* `AsyncIterable<Uint8Array>`
116+
* `FileStream<FileContent>` (see below for definition)
117+
118+
##### FileStream
119+
120+
`FileStream` is a stream of `FileContent` or `FileObject` entries of the type:
121+
122+
```js
123+
Iterable<FileContent|FileObject> | AsyncIterable<FileContent|FileObject> | ReadableStream<FileContent|FileObject>
124+
```
125+
126+
##### FileObject
117127

118128
`FileObject` is a plain JS object of the following form:
119129

@@ -136,10 +146,12 @@ If no `content` is passed, then the item is treated as an empty directory.
136146

137147
One of `path` or `content` _must_ be passed.
138148

149+
##### FileContent
150+
139151
`FileContent` is one of the following types:
140152

141153
```js
142-
Uint8Array | Blob | String | Iterable<Uint8Array> | AsyncIterable<Uint8Array>
154+
Uint8Array | Blob | String | Iterable<Uint8Array | Number> | AsyncIterable<Uint8Array> | ReadableStream<Uint8Array>
143155
```
144156

145157
`UnixTime` is one of the following types:

packages/ipfs-core-utils/package.json

+7-3
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,21 @@
2828
},
2929
"license": "MIT",
3030
"dependencies": {
31+
"blob-to-it": "0.0.1",
32+
"browser-readablestream-to-it": "0.0.1",
3133
"buffer": "^5.6.0",
3234
"cids": "^0.8.3",
3335
"err-code": "^2.0.0",
34-
"ipfs-utils": "^2.2.2"
36+
"ipfs-utils": "^2.2.2",
37+
"it-all": "^1.0.1",
38+
"it-map": "^1.0.0",
39+
"it-peekable": "0.0.1"
3540
},
3641
"devDependencies": {
3742
"aegir": "^23.0.0",
3843
"chai": "^4.2.0",
3944
"chai-as-promised": "^7.1.1",
4045
"delay": "^4.3.0",
41-
"dirty-chai": "^2.0.1",
42-
"it-all": "^1.0.1"
46+
"dirty-chai": "^2.0.1"
4347
}
4448
}

packages/ipfs-core-utils/src/files/normalise-input.js

-298
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
'use strict'
2+
3+
const normaliseContent = require('./normalise-content.browser')
4+
const normaliseInput = require('./normalise-input')
5+
6+
/*
7+
* Transforms any of the `ipfs.add` input types into
8+
*
9+
* ```
10+
* AsyncIterable<{ path, mode, mtime, content: Blob }>
11+
* ```
12+
*
13+
* See https://github.com/ipfs/js-ipfs/blob/master/docs/core-api/FILES.md#ipfsadddata-options
14+
*
15+
* @param input Object
16+
* @return AsyncInterable<{ path, mode, mtime, content: Blob }>
17+
*/
18+
module.exports = (input) => normaliseInput(input, normaliseContent)

0 commit comments

Comments
 (0)