From 36a5a620bec0b181451927f13ac85b9888b86cec Mon Sep 17 00:00:00 2001 From: Dmitriy Mozgovoy Date: Wed, 26 Feb 2025 10:30:55 +0200 Subject: [PATCH] fix(utils): move `generateString` to platform utils to avoid importing crypto module into client builds; (#6789) * chore(ci): Add release-it script; * fix(utils): move generateString util to platform utils to avoid importing crypto module into client build; --------- Co-authored-by: Jay --- lib/helpers/formDataToStream.js | 5 +++-- lib/platform/node/index.js | 26 ++++++++++++++++++++++++++ lib/utils.js | 25 ------------------------- test/unit/platform/index.js | 22 ++++++++++++++++++++++ test/unit/utils/utils.js | 20 -------------------- 5 files changed, 51 insertions(+), 47 deletions(-) create mode 100644 test/unit/platform/index.js diff --git a/lib/helpers/formDataToStream.js b/lib/helpers/formDataToStream.js index 77ffab1..972b99f 100644 --- a/lib/helpers/formDataToStream.js +++ b/lib/helpers/formDataToStream.js @@ -2,8 +2,9 @@ import util from 'util'; import {Readable} from 'stream'; import utils from "../utils.js"; import readBlob from "./readBlob.js"; +import platform from "../platform/index.js"; -const BOUNDARY_ALPHABET = utils.ALPHABET.ALPHA_DIGIT + '-_'; +const BOUNDARY_ALPHABET = platform.ALPHABET.ALPHA_DIGIT + '-_'; const textEncoder = typeof TextEncoder === 'function' ? new TextEncoder() : new util.TextEncoder(); @@ -63,7 +64,7 @@ const formDataToStream = (form, headersHandler, options) => { const { tag = 'form-data-boundary', size = 25, - boundary = tag + '-' + utils.generateString(size, BOUNDARY_ALPHABET) + boundary = tag + '-' + platform.generateString(size, BOUNDARY_ALPHABET) } = options || {}; if(!utils.isFormData(form)) { diff --git a/lib/platform/node/index.js b/lib/platform/node/index.js index aef514a..cd1ca0c 100644 --- a/lib/platform/node/index.js +++ b/lib/platform/node/index.js @@ -1,6 +1,30 @@ +import crypto from 'crypto'; import URLSearchParams from './classes/URLSearchParams.js' import FormData from './classes/FormData.js' +const ALPHA = 'abcdefghijklmnopqrstuvwxyz' + +const DIGIT = '0123456789'; + +const ALPHABET = { + DIGIT, + ALPHA, + ALPHA_DIGIT: ALPHA + ALPHA.toUpperCase() + DIGIT +} + +const generateString = (size = 16, alphabet = ALPHABET.ALPHA_DIGIT) => { + let str = ''; + const {length} = alphabet; + const randomValues = new Uint32Array(size); + crypto.randomFillSync(randomValues); + for (let i = 0; i < size; i++) { + str += alphabet[randomValues[i] % length]; + } + + return str; +} + + export default { isNode: true, classes: { @@ -8,5 +32,7 @@ export default { FormData, Blob: typeof Blob !== 'undefined' && Blob || null }, + ALPHABET, + generateString, protocols: [ 'http', 'https', 'file', 'data' ] }; diff --git a/lib/utils.js b/lib/utils.js index 45529b4..c48eb1e 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -1,7 +1,6 @@ 'use strict'; import bind from './helpers/bind.js'; -import crypto from 'crypto'; // utils is a library of generic helper functions non-specific to axios @@ -603,28 +602,6 @@ const toFiniteNumber = (value, defaultValue) => { return value != null && Number.isFinite(value = +value) ? value : defaultValue; } -const ALPHA = 'abcdefghijklmnopqrstuvwxyz' - -const DIGIT = '0123456789'; - -const ALPHABET = { - DIGIT, - ALPHA, - ALPHA_DIGIT: ALPHA + ALPHA.toUpperCase() + DIGIT -} - -const generateString = (size = 16, alphabet = ALPHABET.ALPHA_DIGIT) => { - let str = ''; - const {length} = alphabet; - const randomValues = new Uint32Array(size); - crypto.randomFillSync(randomValues); - for (let i = 0; i < size; i++) { - str += alphabet[randomValues[i] % length]; - } - - return str; -} - /** * If the thing is a FormData object, return true, otherwise return false. * @@ -752,8 +729,6 @@ export default { findKey, global: _global, isContextDefined, - ALPHABET, - generateString, isSpecCompliantForm, toJSONObject, isAsyncFn, diff --git a/test/unit/platform/index.js b/test/unit/platform/index.js new file mode 100644 index 0000000..ddef421 --- /dev/null +++ b/test/unit/platform/index.js @@ -0,0 +1,22 @@ +import platform from "../../../lib/platform/index.js"; +import assert from "assert"; + +describe('generateString', function () { + it('should generate a string of the specified length using the default alphabet', function () { + const size = 10; + const str = platform.generateString(size); + + assert.strictEqual(str.length, size); + }); + + it('should generate a string using only characters from the default alphabet', function () { + const size = 10; + const alphabet = platform.ALPHABET.ALPHA_DIGIT; + + const str = platform.generateString(size, alphabet); + + for (let char of str) { + assert.ok(alphabet.includes(char), `Character ${char} is not in the alphabet`); + } + }); +}); diff --git a/test/unit/utils/utils.js b/test/unit/utils/utils.js index 8ce76e4..8d40250 100644 --- a/test/unit/utils/utils.js +++ b/test/unit/utils/utils.js @@ -80,24 +80,4 @@ describe('utils', function (){ assert.strictEqual(JSON.stringify(jsonObject), JSON.stringify({x: 1, y:2, obj: {ok: 1}})) }); }); - - describe('generateString', function () { - it('should generate a string of the specified length using the default alphabet', function () { - const size = 10; - const str = utils.generateString(size); - - assert.strictEqual(str.length, size); - }); - - it('should generate a string using only characters from the default alphabet', function () { - const size = 10; - const alphabet = utils.ALPHABET.ALPHA_DIGIT; - - const str = utils.generateString(size, alphabet); - - for (let char of str) { - assert.ok(alphabet.includes(char), `Character ${char} is not in the alphabet`); - } - }); - }); });