/** * Copyright 2026 Angus.Fenying * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * https://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import * as eL from './Errors.js'; /** * Transform a binary UUID (16-byte buffer) into its standard string * representation. * * > Not for Snowflake (series) IDs, which are not UUID family and have different formats. * * @param buffer A 16-byte buffer representing the binary form of a UUID. * @returns A string representation of the UUID. * * @throws {E_INVALID_UUID} If the input buffer is not 16 bytes long. */ export function uuidToString(buffer: Buffer): string { if (buffer.length !== 16) { throw new eL.E_INVALID_UUID(); } const hex = buffer.toString('hex'); return `${ hex.slice(0, 8) }-${ hex.slice(8, 12) }-${ hex.slice(12, 16) }-${ hex.slice(16, 20) }-${ hex.slice(20, 32) }`; } /** * Transform a UUID string into its binary form (16-byte buffer). * * > Not for Snowflake (series) IDs, which are not UUID family and have different formats. * * @param uuid A string representation of a UUID. * @returns A 16-byte buffer representing the binary form of the UUID. * * @throws {E_INVALID_UUID} If the input string is not a valid UUID format. */ export function uuidToBuffer(uuid: string): Buffer { if (uuid.length !== 36) { throw new eL.E_INVALID_UUID(); } const hex = uuid.replace(/-/g, ''); const buf = Buffer.from(hex, 'hex'); if (buf.length !== 16) { throw new eL.E_INVALID_UUID(); } return buf; }