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