v35.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.URL = exports.DNS = exports.stringToBytes = void 0;
  4. const parse_js_1 = require("./parse.js");
  5. const stringify_js_1 = require("./stringify.js");
  6. function stringToBytes(str) {
  7. str = unescape(encodeURIComponent(str));
  8. const bytes = new Uint8Array(str.length);
  9. for (let i = 0; i < str.length; ++i) {
  10. bytes[i] = str.charCodeAt(i);
  11. }
  12. return bytes;
  13. }
  14. exports.stringToBytes = stringToBytes;
  15. exports.DNS = '6ba7b810-9dad-11d1-80b4-00c04fd430c8';
  16. exports.URL = '6ba7b811-9dad-11d1-80b4-00c04fd430c8';
  17. function v35(version, hash, value, namespace, buf, offset) {
  18. const valueBytes = typeof value === 'string' ? stringToBytes(value) : value;
  19. const namespaceBytes = typeof namespace === 'string' ? (0, parse_js_1.default)(namespace) : namespace;
  20. if (typeof namespace === 'string') {
  21. namespace = (0, parse_js_1.default)(namespace);
  22. }
  23. if (namespace?.length !== 16) {
  24. throw TypeError('Namespace must be array-like (16 iterable integer values, 0-255)');
  25. }
  26. let bytes = new Uint8Array(16 + valueBytes.length);
  27. bytes.set(namespaceBytes);
  28. bytes.set(valueBytes, namespaceBytes.length);
  29. bytes = hash(bytes);
  30. bytes[6] = (bytes[6] & 0x0f) | version;
  31. bytes[8] = (bytes[8] & 0x3f) | 0x80;
  32. if (buf) {
  33. offset = offset || 0;
  34. for (let i = 0; i < 16; ++i) {
  35. buf[offset + i] = bytes[i];
  36. }
  37. return buf;
  38. }
  39. return (0, stringify_js_1.unsafeStringify)(bytes);
  40. }
  41. exports.default = v35;