-
-
Notifications
You must be signed in to change notification settings - Fork 10.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added support for spec-compliant FormData & Blob types; #5316
Merged
DigitalBrainJS
merged 7 commits into
axios:v1.x
from
DigitalBrainJS:feat/form-data-encoder
Jan 30, 2023
Merged
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
bf78297
Draft;
DigitalBrainJS 0c03d22
Added support for spec-compliant FormData & Blob types;
DigitalBrainJS fae3bc6
Merge branch 'v1.x' into feat/form-data-encoder
jasonsaayman 1b22baf
Merge branch 'v1.x' of https://github.com/axios/axios into feat/form-…
DigitalBrainJS d99c466
Merge remote-tracking branch 'origin/feat/form-data-encoder' into fea…
DigitalBrainJS 0bbfa19
Merge branch 'v1.x' of https://github.com/axios/axios into feat/form-…
DigitalBrainJS c69c1ad
feat(helper): improved `readBlob` helper;
DigitalBrainJS File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 |
---|---|---|
@@ -1,2 +1,2 @@ | ||
import FormData from 'form-data'; | ||
export default FormData; | ||
import _FormData from 'form-data'; | ||
export default typeof FormData !== 'undefined' ? FormData : _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,110 @@ | ||
import {Readable} from 'stream'; | ||
import utils from "../utils.js"; | ||
import readBlob from "./readBlob.js"; | ||
|
||
const BOUNDARY_ALPHABET = utils.ALPHABET.ALPHA_DIGIT + '-_'; | ||
|
||
const textEncoder = new TextEncoder(); | ||
|
||
const CRLF = '\r\n'; | ||
const CRLF_BYTES = textEncoder.encode(CRLF); | ||
const CRLF_BYTES_COUNT = 2; | ||
|
||
class FormDataPart { | ||
constructor(name, value) { | ||
const {escapeName} = this.constructor; | ||
const isStringValue = utils.isString(value); | ||
|
||
let headers = `Content-Disposition: form-data; name="${escapeName(name)}"${ | ||
!isStringValue && value.name ? `; filename="${escapeName(value.name)}"` : '' | ||
}${CRLF}`; | ||
|
||
if (isStringValue) { | ||
value = textEncoder.encode(String(value).replace(/\r?\n|\r\n?/g, CRLF)); | ||
} else { | ||
headers += `Content-Type: ${value.type || "application/octet-stream"}${CRLF}` | ||
} | ||
|
||
this.headers = textEncoder.encode(headers + CRLF); | ||
|
||
this.contentLength = isStringValue ? value.byteLength : value.size; | ||
|
||
this.size = this.headers.byteLength + this.contentLength + CRLF_BYTES_COUNT; | ||
|
||
this.name = name; | ||
this.value = value; | ||
} | ||
|
||
async *encode(){ | ||
yield this.headers; | ||
|
||
const {value} = this; | ||
|
||
if(utils.isTypedArray(value)) { | ||
yield value; | ||
} else { | ||
yield* readBlob(value); | ||
} | ||
|
||
yield CRLF_BYTES; | ||
} | ||
|
||
static escapeName(name) { | ||
return String(name).replace(/[\r\n"]/g, (match) => ({ | ||
'\r' : '%0D', | ||
'\n' : '%0A', | ||
'"' : '%22', | ||
}[match])); | ||
} | ||
} | ||
|
||
const formDataToStream = (form, headersHandler, options) => { | ||
const { | ||
tag = 'form-data-boundary', | ||
size = 25, | ||
boundary = tag + '-' + utils.generateString(size, BOUNDARY_ALPHABET) | ||
} = options || {}; | ||
|
||
if(!utils.isFormData(form)) { | ||
throw TypeError('FormData instance required'); | ||
} | ||
|
||
if (boundary.length < 1 || boundary.length > 70) { | ||
throw Error('boundary must be 10-70 characters long') | ||
} | ||
|
||
const boundaryBytes = textEncoder.encode('--' + boundary + CRLF); | ||
const footerBytes = textEncoder.encode('--' + boundary + '--' + CRLF + CRLF); | ||
let contentLength = footerBytes.byteLength; | ||
|
||
const parts = Array.from(form.entries()).map(([name, value]) => { | ||
const part = new FormDataPart(name, value); | ||
contentLength += part.size; | ||
return part; | ||
}); | ||
|
||
contentLength += boundaryBytes.byteLength * parts.length; | ||
|
||
contentLength = utils.toFiniteNumber(contentLength); | ||
|
||
const computedHeaders = { | ||
'Content-Type': `multipart/form-data; boundary=${boundary}` | ||
} | ||
|
||
if (Number.isFinite(contentLength)) { | ||
computedHeaders['Content-Length'] = contentLength; | ||
} | ||
|
||
headersHandler && headersHandler(computedHeaders); | ||
|
||
return Readable.from((async function *() { | ||
for(const part of parts) { | ||
yield boundaryBytes; | ||
yield* part.encode(); | ||
} | ||
|
||
yield footerBytes; | ||
})()); | ||
}; | ||
|
||
export default formDataToStream; |
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 @@ | ||
const readBlob = async function *(blob) { | ||
if (blob.stream) { | ||
yield* blob.stream() | ||
} else { | ||
yield await blob.arrayBuffer() | ||
} | ||
} | ||
|
||
export default readBlob; |
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
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.
Check notice
Code scanning / CodeQL
Semicolon insertion