util.js 788 B

12345678910111213141516171819202122232425262728293031323334353637
  1. 'use strict'
  2. /**
  3. * Checks if the given value is a valid LastEventId.
  4. * @param {string} value
  5. * @returns {boolean}
  6. */
  7. function isValidLastEventId (value) {
  8. // LastEventId should not contain U+0000 NULL
  9. return value.indexOf('\u0000') === -1
  10. }
  11. /**
  12. * Checks if the given value is a base 10 digit.
  13. * @param {string} value
  14. * @returns {boolean}
  15. */
  16. function isASCIINumber (value) {
  17. if (value.length === 0) return false
  18. for (let i = 0; i < value.length; i++) {
  19. if (value.charCodeAt(i) < 0x30 || value.charCodeAt(i) > 0x39) return false
  20. }
  21. return true
  22. }
  23. // https://github.com/nodejs/undici/issues/2664
  24. function delay (ms) {
  25. return new Promise((resolve) => {
  26. setTimeout(resolve, ms).unref()
  27. })
  28. }
  29. module.exports = {
  30. isValidLastEventId,
  31. isASCIINumber,
  32. delay
  33. }