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