mirror of
https://github.com/tenrok/axios.git
synced 2026-06-23 20:40:40 +03:00
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 <jasonsaayman@gmail.com>
This commit is contained in:
@@ -2,8 +2,9 @@ import util from 'util';
|
|||||||
import {Readable} from 'stream';
|
import {Readable} from 'stream';
|
||||||
import utils from "../utils.js";
|
import utils from "../utils.js";
|
||||||
import readBlob from "./readBlob.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();
|
const textEncoder = typeof TextEncoder === 'function' ? new TextEncoder() : new util.TextEncoder();
|
||||||
|
|
||||||
@@ -63,7 +64,7 @@ const formDataToStream = (form, headersHandler, options) => {
|
|||||||
const {
|
const {
|
||||||
tag = 'form-data-boundary',
|
tag = 'form-data-boundary',
|
||||||
size = 25,
|
size = 25,
|
||||||
boundary = tag + '-' + utils.generateString(size, BOUNDARY_ALPHABET)
|
boundary = tag + '-' + platform.generateString(size, BOUNDARY_ALPHABET)
|
||||||
} = options || {};
|
} = options || {};
|
||||||
|
|
||||||
if(!utils.isFormData(form)) {
|
if(!utils.isFormData(form)) {
|
||||||
|
|||||||
@@ -1,6 +1,30 @@
|
|||||||
|
import crypto from 'crypto';
|
||||||
import URLSearchParams from './classes/URLSearchParams.js'
|
import URLSearchParams from './classes/URLSearchParams.js'
|
||||||
import FormData from './classes/FormData.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 {
|
export default {
|
||||||
isNode: true,
|
isNode: true,
|
||||||
classes: {
|
classes: {
|
||||||
@@ -8,5 +32,7 @@ export default {
|
|||||||
FormData,
|
FormData,
|
||||||
Blob: typeof Blob !== 'undefined' && Blob || null
|
Blob: typeof Blob !== 'undefined' && Blob || null
|
||||||
},
|
},
|
||||||
|
ALPHABET,
|
||||||
|
generateString,
|
||||||
protocols: [ 'http', 'https', 'file', 'data' ]
|
protocols: [ 'http', 'https', 'file', 'data' ]
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
import bind from './helpers/bind.js';
|
import bind from './helpers/bind.js';
|
||||||
import crypto from 'crypto';
|
|
||||||
|
|
||||||
// utils is a library of generic helper functions non-specific to axios
|
// 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;
|
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.
|
* If the thing is a FormData object, return true, otherwise return false.
|
||||||
*
|
*
|
||||||
@@ -752,8 +729,6 @@ export default {
|
|||||||
findKey,
|
findKey,
|
||||||
global: _global,
|
global: _global,
|
||||||
isContextDefined,
|
isContextDefined,
|
||||||
ALPHABET,
|
|
||||||
generateString,
|
|
||||||
isSpecCompliantForm,
|
isSpecCompliantForm,
|
||||||
toJSONObject,
|
toJSONObject,
|
||||||
isAsyncFn,
|
isAsyncFn,
|
||||||
|
|||||||
@@ -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`);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -80,24 +80,4 @@ describe('utils', function (){
|
|||||||
assert.strictEqual(JSON.stringify(jsonObject), JSON.stringify({x: 1, y:2, obj: {ok: 1}}))
|
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`);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user