Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

balena-request

Package Overview
Dependencies
Maintainers
1
Versions
177
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

balena-request - npm Package Compare versions

Comparing version 13.1.0-build-otaviojacobi-does-multipart-form-when-blob-is-present-d807c01905068f81d9d353364742947142c9c272-1 to 13.1.0-build-otaviojacobi-does-multipart-form-when-blob-is-present-e56766b676d3a86d0eb01507698466dccceda9e7-1

.eslintrc.js

9

build/progress.js

@@ -20,3 +20,3 @@ "use strict";

const tslib_1 = require("tslib");
const utils = tslib_1.__importStar(require("./utils"));
const utils = require("./utils");
/**

@@ -41,3 +41,5 @@ * @module progress

const getProgressStream = function (total, onState) {
const progress = require('progress-stream');
const progress =
// eslint-disable-next-line @typescript-eslint/no-var-requires
require('progress-stream');
const progressStream = progress({

@@ -104,2 +106,3 @@ time: 500,

const response = yield requestAsync(options);
// eslint-disable-next-line @typescript-eslint/no-var-requires
const stream = require('stream');

@@ -112,2 +115,3 @@ const output = new stream.PassThrough();

if (response.body.getReader) {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const webStreams = require('@balena/node-web-streams');

@@ -123,2 +127,3 @@ // Convert browser (WHATWG) streams to Node streams

if (!isBrowser && utils.isResponseCompressed(response)) {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const zlib = require('zlib');

@@ -125,0 +130,0 @@ // Be more lenient with decoding compressed responses, since (very rarely)

@@ -59,6 +59,2 @@ /// <reference types="node" />

export type BalenaRequest = ReturnType<typeof getRequest>;
export declare class BalenaFile extends Blob implements WebResourceFile {
name: string;
constructor(name: string, blobParts?: BlobPart[], options?: BlobPropertyBag);
}
/**

@@ -65,0 +61,0 @@ * @module request

@@ -18,17 +18,7 @@ "use strict";

Object.defineProperty(exports, "__esModule", { value: true });
exports.getRequest = exports.BalenaFile = void 0;
exports.getRequest = void 0;
const tslib_1 = require("tslib");
const urlLib = tslib_1.__importStar(require("url"));
const errors = tslib_1.__importStar(require("balena-errors"));
const mime = tslib_1.__importStar(require("mime"));
const utils = tslib_1.__importStar(require("./utils"));
class BalenaFile extends Blob {
constructor(name, blobParts, options) {
var _a, _b;
options = Object.assign(Object.assign({}, options), { type: (_b = (_a = options === null || options === void 0 ? void 0 : options.type) !== null && _a !== void 0 ? _a : mime.getType(name)) !== null && _b !== void 0 ? _b : undefined });
super(blobParts, options);
this.name = name;
}
}
exports.BalenaFile = BalenaFile;
const urlLib = require("url");
const errors = require("balena-errors");
const utils = require("./utils");
/**

@@ -50,3 +40,4 @@ * @module request

const requestStream = isBrowser
? utils.getRequestAsync(require('fetch-readablestream'))
? // eslint-disable-next-line @typescript-eslint/no-var-requires
utils.getRequestAsync(require('fetch-readablestream'))
: requestAsync;

@@ -248,2 +239,3 @@ const debugRequest = !debug

function stream(options) {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const progress = require('./progress');

@@ -272,3 +264,3 @@ return prepareOptions(options)

debugRequest(options, download.response);
// @ts-expect-error
// @ts-expect-error error without request options
throw new errors.BalenaRequestError(responseError, download.response.statusCode);

@@ -275,0 +267,0 @@ }))

@@ -20,6 +20,8 @@ "use strict";

const tslib_1 = require("tslib");
const { fetch: normalFetch, Headers: HeadersPonyfill } = require('fetch-ponyfill')({ Promise });
const urlLib = tslib_1.__importStar(require("url"));
const qs = tslib_1.__importStar(require("qs"));
const errors = tslib_1.__importStar(require("balena-errors"));
const { fetch: normalFetch, Headers: HeadersPonyfill } =
// eslint-disable-next-line @typescript-eslint/no-var-requires
require('fetch-ponyfill')({ Promise });
const urlLib = require("url");
const qs = require("qs");
const errors = require("balena-errors");
const balena_auth_1 = require("balena-auth");

@@ -301,5 +303,5 @@ const stream_1 = require("stream");

// https://github.com/bitinn/node-fetch/blob/master/lib/body.js#L66
// @ts-expect-error
// @ts-expect-error if response happens to have a buffer function
if (typeof response.buffer === 'function') {
// @ts-expect-error
// @ts-expect-error calls it
return response.buffer();

@@ -321,6 +323,5 @@ }

const isFile = (value) => {
return (value &&
return (value instanceof Blob &&
value.name != null &&
typeof value.name === 'string' &&
value instanceof Blob);
typeof value.name === 'string');
};

@@ -345,4 +346,8 @@ // This is the actual implementation that hides the internal `retriesRemaining` parameter

}
const NodeFormData = (yield import('formdata-node')).FormData;
const FormDataEncoder = (yield import('form-data-encoder')).FormDataEncoder;
const { FormData: NodeFormData } =
// eslint-disable-next-line @typescript-eslint/no-var-requires
require('formdata-node');
const { FormDataEncoder } =
// eslint-disable-next-line @typescript-eslint/no-var-requires
require('form-data-encoder');
const nodeForm = new NodeFormData();

@@ -354,4 +359,7 @@ const form = IS_BROWSER ? new FormData() : nodeForm;

hasBlob = true;
form.append(k, v, v.name);
}
form.append(k, v);
else {
form.append(k, v);
}
}

@@ -361,5 +369,4 @@ if (hasBlob) {

// Browsers will handle set form data header and boundaries
// Given the correct header
// Given the correct body format
opts.headers.delete('Content-Type');
// @ts-expect-error
opts.body = form;

@@ -370,4 +377,7 @@ }

opts.headers.set('Content-Type', encoder.headers['Content-Type']);
opts.headers.set('Content-Length', encoder.headers['Content-Length']);
// @ts-expect-error
const length = encoder.headers['Content-Length'];
if (length != null) {
opts.headers.set('Content-Length', length);
}
// @ts-expect-error https://www.npmjs.com/package/form-data-encoder#usage
opts.body = stream_1.Readable.from(encoder);

@@ -374,0 +384,0 @@ }

@@ -7,6 +7,11 @@ # Change Log

## 13.1.0 - 2023-10-11
## 13.1.0 - 2023-10-23
* Add multi part request support when File-like is present [Otávio Jacobi]
* Update @balena/lint to 7.2.0 Update @balena/lint from 5.4.1 to 7.2.0 [Otávio Jacobi]
## 13.0.1 - 2023-10-18
* Update @balena/lint to 7.2.0 Update @balena/lint from 5.4.1 to 7.2.0 [Otávio Jacobi]
## 13.0.0 - 2023-10-11

@@ -13,0 +18,0 @@

@@ -21,2 +21,8 @@ const getKarmaConfig = require('balena-config-karma');

};
karmaConfig.webpack.module.rules.push({
test: /\.js/,
resolve: {
fullySpecified: false,
},
});
karmaConfig.webpack.plugins = [

@@ -23,0 +29,0 @@ new getKarmaConfig.webpack.ProvidePlugin({

@@ -46,2 +46,3 @@ /*

const progress =
// eslint-disable-next-line @typescript-eslint/no-var-requires
require('progress-stream') as typeof import('progress-stream');

@@ -132,2 +133,3 @@ const progressStream = progress({

// eslint-disable-next-line @typescript-eslint/no-var-requires
const stream = require('stream') as typeof Stream;

@@ -143,2 +145,3 @@ const output = new stream.PassThrough() as BalenaRequestPassThroughStream;

if (response.body.getReader) {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const webStreams = require('@balena/node-web-streams') as {

@@ -159,2 +162,3 @@ toNodeReadable(body: any): any;

if (!isBrowser && utils.isResponseCompressed(response)) {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const zlib = require('zlib') as typeof import('zlib');

@@ -161,0 +165,0 @@

@@ -22,3 +22,2 @@ /*

import * as errors from 'balena-errors';
import * as mime from 'mime';
import * as utils from './utils';

@@ -87,16 +86,2 @@

export class BalenaFile extends Blob implements WebResourceFile {
constructor(
public name: string,
blobParts?: BlobPart[],
options?: BlobPropertyBag,
) {
options = {
...options,
type: options?.type ?? mime.getType(name) ?? undefined,
};
super(blobParts, options);
}
}
/**

@@ -125,3 +110,4 @@ * @module request

const requestStream = isBrowser
? utils.getRequestAsync(require('fetch-readablestream') as typeof fetch)
? // eslint-disable-next-line @typescript-eslint/no-var-requires
utils.getRequestAsync(require('fetch-readablestream') as typeof fetch)
: requestAsync;

@@ -365,2 +351,3 @@

): Promise<BalenaRequestStreamResult> {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const progress = require('./progress') as typeof import('./progress');

@@ -396,3 +383,3 @@ return prepareOptions(options)

debugRequest(options, download.response);
// @ts-expect-error
// @ts-expect-error error without request options
throw new errors.BalenaRequestError(

@@ -399,0 +386,0 @@ responseError,

@@ -17,5 +17,5 @@ /*

const { fetch: normalFetch, Headers: HeadersPonyfill } = (
require('fetch-ponyfill') as typeof import('fetch-ponyfill')
)({ Promise });
const { fetch: normalFetch, Headers: HeadersPonyfill } =
// eslint-disable-next-line @typescript-eslint/no-var-requires
(require('fetch-ponyfill') as typeof import('fetch-ponyfill'))({ Promise });

@@ -354,5 +354,5 @@ import * as urlLib from 'url';

// https://github.com/bitinn/node-fetch/blob/master/lib/body.js#L66
// @ts-expect-error
// @ts-expect-error if response happens to have a buffer function
if (typeof response.buffer === 'function') {
// @ts-expect-error
// @ts-expect-error calls it
return response.buffer();

@@ -382,8 +382,7 @@ }

const isFile = (value: any): value is WebResourceFile => {
const isFile = (value: string | WebResourceFile): value is WebResourceFile => {
return (
value &&
value instanceof Blob &&
value.name != null &&
typeof value.name === 'string' &&
value instanceof Blob
typeof value.name === 'string'
);

@@ -413,13 +412,22 @@ };

}
const NodeFormData = (await import('formdata-node')).FormData;
const FormDataEncoder = (await import('form-data-encoder')).FormDataEncoder;
const { FormData: NodeFormData } =
// eslint-disable-next-line @typescript-eslint/no-var-requires
require('formdata-node') as typeof import('formdata-node');
const { FormDataEncoder } =
// eslint-disable-next-line @typescript-eslint/no-var-requires
require('form-data-encoder') as typeof import('form-data-encoder');
const nodeForm = new NodeFormData();
const form = IS_BROWSER ? new FormData() : nodeForm;
let hasBlob = false;
for (const [k, v] of Object.entries(options.body ?? {})) {
for (const [k, v] of Object.entries<string | WebResourceFile>(
options.body ?? {},
)) {
if (isFile(v)) {
hasBlob = true;
form.append(k, v, v.name);
} else {
form.append(k, v);
}
form.append(k, v as string | WebResourceFile);
}

@@ -430,13 +438,15 @@

// Browsers will handle set form data header and boundaries
// Given the correct header
// Given the correct body format
opts.headers.delete('Content-Type');
// @ts-expect-error
opts.body = form;
opts.body = form as FormData;
} else {
const encoder = new FormDataEncoder(form as typeof nodeForm);
opts.headers.set('Content-Type', encoder.headers['Content-Type']);
opts.headers.set('Content-Length', encoder.headers['Content-Length']!);
const length = encoder.headers['Content-Length'];
if (length != null) {
opts.headers.set('Content-Length', length);
}
// @ts-expect-error
// @ts-expect-error https://www.npmjs.com/package/form-data-encoder#usage
opts.body = Readable.from(encoder);

@@ -443,0 +453,0 @@ }

{
"name": "balena-request",
"version": "13.1.0-build-otaviojacobi-does-multipart-form-when-blob-is-present-d807c01905068f81d9d353364742947142c9c272-1",
"version": "13.1.0-build-otaviojacobi-does-multipart-form-when-blob-is-present-e56766b676d3a86d0eb01507698466dccceda9e7-1",
"description": "Balena HTTP client",

@@ -24,8 +24,8 @@ "main": "build/request.js",

"scripts": {
"lint": "balena-lint -e ts --typescript -e js lib tests",
"lint-fix": "balena-lint -e ts --typescript -e js --fix lib tests",
"lint": "balena-lint -e js -e ts lib tests",
"lint-fix": "balena-lint -e js -e ts --fix lib tests",
"pretest": "npm run build",
"test": "npm run test-node && npm run test-browser",
"posttest": "npm run lint",
"test-node": "mocha --reporter spec tests/**/*.spec.js",
"test-node": "mocha -r ts-node/register/transpile-only --reporter spec tests/**/*.spec.js",
"test-browser": "mockttp -c karma start",

@@ -40,6 +40,5 @@ "build": "npx tsc",

"devDependencies": {
"@balena/lint": "^5.4.1",
"@balena/lint": "^7.2.0",
"@types/chai": "^4.3.0",
"@types/chai-as-promised": "^7.1.5",
"@types/mime": "^3.0.2",
"@types/mocha": "^9.1.1",

@@ -59,3 +58,3 @@ "@types/node": "^18.0.0",

"jsdoc-to-markdown": "^6.0.1",
"karma": "^5.0.9",
"karma": "^6.3.17",
"mocha": "^10.0.0",

@@ -67,6 +66,7 @@ "mockttp": "^3.8.0",

"rindle": "^1.3.6",
"sinon": "^13.0.1",
"sinon": "^15.0.1",
"stream-browserify": "^3.0.0",
"temp": "^0.8.4",
"timekeeper": "^1.0.0",
"ts-node": "^10.9.1",
"typescript": "^5.1.6",

@@ -80,5 +80,4 @@ "util": "^0.12.4"

"fetch-readablestream": "^0.2.0",
"form-data-encoder": "^3.0.0",
"formdata-node": "^5.0.0",
"mime": "^3.0.0",
"form-data-encoder": "1.7.2",
"formdata-node": "^4.0.0",
"progress-stream": "^2.0.0",

@@ -93,4 +92,4 @@ "qs": "^6.9.4",

"versionist": {
"publishedAt": "2023-10-11T20:07:35.004Z"
"publishedAt": "2023-10-23T18:40:14.553Z"
}
}

@@ -1,10 +0,13 @@

const { expect } = require('chai');
const rindle = require('rindle');
const sinon = require('sinon');
const johnDoeFixture = require('./tokens.json').johndoe;
const utils = require('../build/utils');
import { expect } from 'chai';
import setup from './setup';
import * as rindle from 'rindle';
import * as sinon from 'sinon';
import * as mockhttp from 'mockttp';
import * as tokens from './tokens.json';
import * as utils from '../build/utils';
const mockServer = require('mockttp').getLocal();
const johnDoeFixture = tokens.johndoe;
const mockServer = mockhttp.getLocal();
const { auth, request } = require('./setup')();
const { auth, request } = setup();

@@ -11,0 +14,0 @@ describe('Request (api key):', function () {

@@ -1,10 +0,12 @@

const { expect } = require('chai');
const rindle = require('rindle');
const sinon = require('sinon');
const Bluebird = require('bluebird');
const mockServer = require('mockttp').getLocal();
import { expect } from 'chai';
import setup from './setup';
import * as Bluebird from 'bluebird';
import * as rindle from 'rindle';
import * as sinon from 'sinon';
import * as mockhttp from 'mockttp';
import * as utils from '../build/utils';
const utils = require('../build/utils');
const mockServer = mockhttp.getLocal();
const { auth, request } = require('./setup')();
const { auth, request } = setup();

@@ -295,3 +297,3 @@ describe('An interceptor', function () {

request.interceptors[0] = {
response(response) {
response() {
return rindle.getStreamFromString('replacement stream');

@@ -365,3 +367,3 @@ },

request.interceptors[0] = {
responseError(response) {
responseError() {
return request.send({

@@ -368,0 +370,0 @@ url: mockServer.urlFor('/ok'),

@@ -1,10 +0,9 @@

const Bluebird = require('bluebird');
const { expect } = require('chai');
const sinon = require('sinon');
const fs = require('fs');
import { expect } from 'chai';
import setup from './setup';
import * as sinon from 'sinon';
import * as mockhttp from 'mockttp';
const mockServer = require('mockttp').getLocal();
const mockServer = mockhttp.getLocal();
const { auth, request, getCustomRequest, IS_BROWSER, BalenaFile } =
require('./setup')();
const { auth, request, getCustomRequest, IS_BROWSER } = setup();

@@ -17,2 +16,9 @@ // Grab setTimeout before we replace it with a fake later, so

class TestFile extends Blob {
constructor(blobParts, name, type) {
super(blobParts, { type });
this.name = name;
}
}
describe('Request:', function () {

@@ -226,9 +232,11 @@ this.timeout(10000);

this.beforeEach(() => {
mockServer.forPost('/multipart-endpoint').thenCallback(async (req) => {
return { statusCode: 200, body: req.headers['content-type'] };
});
void mockServer
.forPost('/multipart-endpoint')
.thenCallback(async (req) => {
return { statusCode: 200, body: req.headers['content-type'] };
});
});
it('should send the request as multipart/form-data with boundary', async function () {
const fileName = 'testfile.txt';
let body;
const fileName = 'testfile.txt';
if (IS_BROWSER) {

@@ -240,5 +248,3 @@ body = {

body = {
content: new BalenaFile(fileName, [
new Blob(['a', 'test', 'blob']),
]),
content: new TestFile(['a', 'test', 'blob'], fileName),
};

@@ -313,5 +319,5 @@ }

describe('given an endpoint that will time out', function () {
beforeEach(function () {
beforeEach(async function () {
this.clock = sinon.useFakeTimers();
mockServer.forGet('/infinite-wait').thenTimeout();
await mockServer.forGet('/infinite-wait').thenTimeout();
});

@@ -318,0 +324,0 @@

@@ -1,9 +0,9 @@

const Bluebird = require('bluebird');
const { expect } = require('chai');
const sinon = require('sinon');
const errors = require('balena-errors');
import { expect } from 'chai';
import setup from './setup';
import * as errors from 'balena-errors';
import * as mockhttp from 'mockttp';
const mockServer = require('mockttp').getLocal();
const mockServer = mockhttp.getLocal();
const { auth, request, IS_BROWSER } = require('./setup')();
const { auth, request, IS_BROWSER } = setup();

@@ -10,0 +10,0 @@ const RESPONSE_BODY = { from: 'foobar' };

@@ -5,2 +5,3 @@ const IS_BROWSER = typeof window !== 'undefined' && window !== null;

if (!IS_BROWSER) {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const temp = require('temp').track();

@@ -10,3 +11,3 @@ dataDirectoryPath = temp.mkdirSync();

const BalenaAuth = require('balena-auth')['default'];
import BalenaAuth from 'balena-auth';

@@ -19,8 +20,7 @@ const auth = new BalenaAuth({

// Make sure any existing tokens are removed before the tests start
auth.removeKey();
void auth.removeKey();
const { BalenaFile, getRequest } = require('../build/request');
const chai = require('chai');
const chaiAsPromised = require('chai-as-promised');
import { getRequest } from '../build/request';
import * as chai from 'chai';
import * as chaiAsPromised from 'chai-as-promised';
chai.use(chaiAsPromised);

@@ -33,3 +33,3 @@

module.exports = () => ({
export default () => ({
IS_BROWSER,

@@ -39,3 +39,2 @@ auth,

getCustomRequest,
BalenaFile,
});

@@ -1,14 +0,14 @@

const Bluebird = require('bluebird');
const { expect } = require('chai');
const sinon = require('sinon');
const zlib = require('browserify-zlib');
const { PassThrough } = require('stream');
const rindle = require('rindle');
import { PassThrough } from 'stream';
import { expect } from 'chai';
import setup from './setup';
import * as Bluebird from 'bluebird';
import * as rindle from 'rindle';
import * as zlib from 'browserify-zlib';
import * as mockhttp from 'mockttp';
const mockServer = mockhttp.getLocal();
const { auth, request } = setup();
const gzip = Bluebird.promisify(zlib.gzip);
const mockServer = require('mockttp').getLocal();
const { auth, request, IS_BROWSER } = require('./setup')();
describe('Request (stream):', function () {

@@ -15,0 +15,0 @@ beforeEach(() => Promise.all([auth.removeKey(), mockServer.start()]));

@@ -1,14 +0,16 @@

const { expect } = require('chai');
const sinon = require('sinon');
const errors = require('balena-errors');
const rindle = require('rindle');
const tokens = require('./tokens.json');
import { expect } from 'chai';
import setup from './setup';
import * as rindle from 'rindle';
import * as sinon from 'sinon';
import * as errors from 'balena-errors';
import * as mockhttp from 'mockttp';
import * as tokens from './tokens.json';
import * as utils from '../build/utils';
const johnDoeFixture = tokens.johndoe;
const janeDoeFixture = tokens.janedoe;
const utils = require('../build/utils');
const mockServer = require('mockttp').getLocal();
const mockServer = mockhttp.getLocal();
const { auth, request } = require('./setup')();
const { auth, request } = setup();

@@ -145,4 +147,6 @@ describe('Request (token):', function () {

// to simplicity.
it('should use the new token in the same request', function () {
expect(auth.getKey()).to.eventually.equal(johnDoeFixture.token);
it('should use the new token in the same request', async function () {
await expect(auth.getKey()).to.eventually.equal(
johnDoeFixture.token,
);
return request

@@ -293,4 +297,4 @@ .send({

describe('given a working /user/v1/refresh-token endpoint', function () {
beforeEach(function () {
auth.setKey(johnDoeFixture.token);
beforeEach(async function () {
await auth.setKey(johnDoeFixture.token);
return mockServer

@@ -297,0 +301,0 @@ .forGet('/user/v1/refresh-token')

@@ -1,12 +0,13 @@

const ReadableStream = require('stream').Readable;
const { Headers } = require('fetch-ponyfill')({ Promise });
const { expect } = require('chai');
const sinon = require('sinon');
const johnDoeFixture = require('./tokens.json').johndoe;
const utils = require('../build/utils');
import { expect } from 'chai';
import { TokenType } from 'balena-auth/build/token';
import setup from './setup';
import * as fetchPonyfill from 'fetch-ponyfill';
import * as sinon from 'sinon';
import * as tokens from './tokens.json';
import * as utils from '../build/utils';
const { TokenType } = require('balena-auth/build/token');
const { Headers } = fetchPonyfill({ Promise });
const { auth } = setup();
const johnDoeFixture = tokens.johndoe;
const { auth } = require('./setup')();
describe('Utils:', function () {

@@ -13,0 +14,0 @@ describe('.shouldRefreshKey()', function () {

{
"compilerOptions": {
"module": "Node16",
"module": "commonjs",
"declaration": true,

@@ -13,3 +13,3 @@ "outDir": "build",

"noUnusedLocals": true,
"moduleResolution": "Node16",
"moduleResolution": "node",
"allowJs": true,

@@ -16,0 +16,0 @@ "checkJs": true,

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc