You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

38 lines
828 B

  1. import crypto from 'crypto';
  2. import URLSearchParams from './classes/URLSearchParams.js'
  3. import FormData from './classes/FormData.js'
  4. const ALPHA = 'abcdefghijklmnopqrstuvwxyz'
  5. const DIGIT = '0123456789';
  6. const ALPHABET = {
  7. DIGIT,
  8. ALPHA,
  9. ALPHA_DIGIT: ALPHA + ALPHA.toUpperCase() + DIGIT
  10. }
  11. const generateString = (size = 16, alphabet = ALPHABET.ALPHA_DIGIT) => {
  12. let str = '';
  13. const {length} = alphabet;
  14. const randomValues = new Uint32Array(size);
  15. crypto.randomFillSync(randomValues);
  16. for (let i = 0; i < size; i++) {
  17. str += alphabet[randomValues[i] % length];
  18. }
  19. return str;
  20. }
  21. export default {
  22. isNode: true,
  23. classes: {
  24. URLSearchParams,
  25. FormData,
  26. Blob: typeof Blob !== 'undefined' && Blob || null
  27. },
  28. ALPHABET,
  29. generateString,
  30. protocols: [ 'http', 'https', 'file', 'data' ]
  31. };