balena-request
Advanced tools
Comparing version 12.0.3 to 12.1.0-build-otaviojacobi-does-multipart-form-when-blob-is-present-49219e9755546df6cd34c6404af9d88abc559389-1
@@ -25,2 +25,4 @@ "use strict"; | ||
const balena_auth_1 = require("balena-auth"); | ||
const NodeFormData = require("form-data"); | ||
const buffer_1 = require("buffer"); | ||
const IS_BROWSER = typeof window !== 'undefined' && window !== null; | ||
@@ -318,2 +320,16 @@ /** | ||
exports.getBody = getBody; | ||
const isBlob = (value) => { | ||
return ((typeof Blob !== 'undefined' && value instanceof Blob) || | ||
(typeof buffer_1.Blob !== 'undefined' && value instanceof buffer_1.Blob)); | ||
}; | ||
const hasBlob = (requestBody) => { | ||
if (requestBody) { | ||
for (const value of Object.values(requestBody)) { | ||
if (isBlob(value)) { | ||
return true; | ||
} | ||
} | ||
} | ||
return false; | ||
}; | ||
// This is the actual implementation that hides the internal `retriesRemaining` parameter | ||
@@ -336,2 +352,30 @@ function requestAsync(fetch, options, retriesRemaining) { | ||
} | ||
if (hasBlob(options.body)) { | ||
if (IS_BROWSER) { | ||
const form = new FormData(); | ||
for (const [k, v] of Object.entries(options.body)) { | ||
form.append(k, v); | ||
} | ||
opts.body = form; | ||
} | ||
else { | ||
const form = new NodeFormData(); | ||
for (const [k, v] of Object.entries(options.body)) { | ||
if (v instanceof buffer_1.Blob) { | ||
form.append(k, Buffer.from(yield v.arrayBuffer()), { | ||
filename: 'upload', | ||
contentType: v.type, | ||
}); | ||
} | ||
else { | ||
form.append(k, v); | ||
} | ||
} | ||
for (const [k, v] of Object.entries(form.getHeaders())) { | ||
opts.headers.set(k, v); | ||
} | ||
// @ts-expect-error | ||
opts.body = form; | ||
} | ||
} | ||
try { | ||
@@ -338,0 +382,0 @@ const requestTime = Date.now(); |
@@ -7,2 +7,6 @@ # Change Log | ||
## 12.1.0 - 2023-08-09 | ||
* Add multi part request support when Blob is present [Otávio Jacobi] | ||
## 12.0.3 - 2023-08-09 | ||
@@ -9,0 +13,0 @@ |
@@ -27,2 +27,4 @@ /* | ||
import type { BalenaRequestOptions, BalenaRequestResponse } from './request'; | ||
import * as NodeFormData from 'form-data'; | ||
import { Blob as BufferBlob } from 'buffer'; | ||
@@ -376,2 +378,20 @@ const IS_BROWSER = typeof window !== 'undefined' && window !== null; | ||
const isBlob = (value: any) => { | ||
return ( | ||
(typeof Blob !== 'undefined' && value instanceof Blob) || | ||
(typeof BufferBlob !== 'undefined' && value instanceof BufferBlob) | ||
); | ||
}; | ||
const hasBlob = (requestBody?: any) => { | ||
if (requestBody) { | ||
for (const value of Object.values(requestBody)) { | ||
if (isBlob(value)) { | ||
return true; | ||
} | ||
} | ||
} | ||
return false; | ||
}; | ||
// This is the actual implementation that hides the internal `retriesRemaining` parameter | ||
@@ -400,2 +420,31 @@ | ||
if (hasBlob(options.body)) { | ||
if (IS_BROWSER) { | ||
const form = new FormData(); | ||
for (const [k, v] of Object.entries(options.body)) { | ||
form.append(k, v as string | Blob); | ||
} | ||
opts.body = form; | ||
} else { | ||
const form = new NodeFormData(); | ||
for (const [k, v] of Object.entries(options.body)) { | ||
if (v instanceof BufferBlob) { | ||
form.append(k, Buffer.from(await v.arrayBuffer()), { | ||
filename: 'upload', | ||
contentType: v.type, | ||
}); | ||
} else { | ||
form.append(k, v); | ||
} | ||
} | ||
for (const [k, v] of Object.entries(form.getHeaders())) { | ||
opts.headers.set(k, v); | ||
} | ||
// @ts-expect-error | ||
opts.body = form; | ||
} | ||
} | ||
try { | ||
@@ -402,0 +451,0 @@ const requestTime = Date.now(); |
{ | ||
"name": "balena-request", | ||
"version": "12.0.3", | ||
"version": "12.1.0-build-otaviojacobi-does-multipart-form-when-blob-is-present-49219e9755546df6cd34c6404af9d88abc559389-1", | ||
"description": "Balena HTTP client", | ||
@@ -43,2 +43,3 @@ "main": "build/request.js", | ||
"@types/mocha": "^9.1.1", | ||
"@types/node": "^16.0.0", | ||
"@types/progress-stream": "^2.0.0", | ||
@@ -75,2 +76,3 @@ "@types/qs": "^6.9.3", | ||
"fetch-readablestream": "^0.2.0", | ||
"form-data": "^4.0.0", | ||
"progress-stream": "^2.0.0", | ||
@@ -85,4 +87,4 @@ "qs": "^6.9.4", | ||
"versionist": { | ||
"publishedAt": "2023-08-09T12:12:01.537Z" | ||
"publishedAt": "2023-08-09T17:31:18.645Z" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
215217
4127
10
30
1
+ Addedform-data@^4.0.0
+ Addedasynckit@0.4.0(transitive)
+ Addedcombined-stream@1.0.8(transitive)
+ Addeddelayed-stream@1.0.0(transitive)
+ Addedform-data@4.0.1(transitive)
+ Addedmime-db@1.52.0(transitive)
+ Addedmime-types@2.1.35(transitive)