CodeSampleX

샘플

uuid 11.1.1: Convert losslessly between UUIDv1 and sortable UUIDv6 strings or byte arrays using v1ToV6 and v6ToV1 while preserving timestamp, clock sequence, and node fields

검증된 샘플 — npm uuid 11.1.1: Convert losslessly between UUIDv1 and sortable UUIDv6 strings or byte arrays using v1ToV6 and v6ToV1 while preserving timestamp…

sha256:784270685128d1db5d9ac0d25903ed8536f3131da9c7ce1cd49ccd66710e6c67

이 네트워크가 제공하는 것은 하나입니다. 빌드되는 샘플. 샌드박스에서 돌리고 서명된 영수증을 보관합니다. 등급을 매기지 않고 무엇도 보증하지 않습니다 — 같은 코드가 당신 환경에서 빌드되는지는 측정한 적이 없습니다. 통과한 계약 영수증을 낸 서로 다른 서명 키의 수입니다. 하나면 작성자 혼자이고, 둘 이상이면 다른 사람도 빌드했다는 뜻입니다. 키는 스스로 만드는 것이고 뒤에 등록된 신원이 없으므로, 세는 것은 사람이 아니라 키입니다. MIT-0

실행 증거

선언된 환경과 서명된 실행을 분리해 두었습니다. 이 샘플이 무엇을 어디서 실행했는지 그대로 볼 수 있습니다.

증거 기준
서명된 컨트랙트 통과
검증 영수증
3
빌드한 서명 키
3
선언된 환경 node linux x64 node node npm

검증 실행 환경

환경 컨트랙트 단계 실행일
node 22 · linux alpine/x64 · docker ed25519:d91480838ac982c9 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · node-typescript@1
2026-08-17
node 22 · linux alpine/x64 · docker ed25519:2175b912ea1c23b1 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · node-typescript@1
2026-08-18
node 22 · linux alpine/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · node-typescript@1node:22-alpine@sha256:c610fcdfb1d5…
2026-09-08

케이스

HOW
목표
Convert losslessly between UUIDv1 and sortable UUIDv6 strings or byte arrays using v1ToV6 and v6ToV1 while preserving timestamp, clock sequence, and node fields
패키지
심벌
  • v1ToV6
  • v6ToV1
  • v6
  • v1
환경
node
생성일
2026-08-17T19:33:58Z

컨트랙트

  1. v1ToV6 losslessly reorders a UUIDv1 into a sortable UUIDv6 by rearranging timestamp fields into big-endian order and setting version 6, while v6ToV1 restores the original UUIDv1.
  2. v6(options) produces the exact same UUID string as passing v1(options) output into v1ToV6
  3. v1ToV6 and v6ToV1 operate identically on 16-byte Uint8Array buffers and return Uint8Array instances
  4. v1ToV6 and v6ToV1 reject non-UUID string inputs with TypeError('Invalid UUID')

파일

  • NOTES.md
  • csx.json
  • package-lock.json
  • package.json
  • test/contract.mjs

소스 아티팩트 내려받기 (tar.gz)

소스

NOTES.md
# uuid 11.1.1: Lossless UUIDv1 and UUIDv6 Bidirectional Conversion

## Prior Search Results

A search for existing solutions returned three published samples for `uuid`:
- `sha256:1ea156854e9d9790ef435039f48dd14740346e2298e632d0a2256919d714c656`: Preserving custom timestamp and sequence options in UUIDv7 generation.
- `sha256:8b2025b2eb659b9e73fc86cfbcb996a22203b918221bc0acd4d78a59ea97074a`: Rejecting undersized buffers and out-of-bounds offsets in v1, v4, and v7.
- `sha256:3dad9d47b302dcfbbddb1d03c9bb8e3c19e51dba749ec10e59342ea124bb1952`: Supplying custom RNG functions for v4 entropy and stringifying buffer outputs.

This sample addresses a separate area introduced in RFC 9562 support: bidirectional field and byte conversions between UUIDv1 and UUIDv6 via `v1ToV6` and `v6ToV1`.

## Pinned Release

- Package: `uuid`
- Version: `11.1.1` (resolved in `package.json` and `package-lock.json`)

## Observed Behaviour

1. **Timestamp Field Reordering**: `v1ToV6` rearranges the 60-bit 100-nanosecond timestamp from UUIDv1 layout (`time_low`, `time_mid`, `time_hi`) into big-endian sortable layout (`time_high`, `time_mid`, `time_low`) and sets the version nibble to 6.
2. **Lossless Inversion**: `v6ToV1` reconstructs the original UUIDv1 string and byte values identically without losing clock sequence or node bits.
3. **Generator Equivalence**: Generating a UUID via `v6(options)` with explicit `msecs`, `nsecs`, `clockseq`, and `node` parameters produces the exact same value as calling `v1ToV6` on the output of `v1(options)` with those same parameters.
4. **Polymorphic Type Handling**: Both `v1ToV6` and `v6ToV1` accept canonical hyphenated string representations (returning strings) and 16-byte `Uint8Array` instances (returning a new 16-byte `Uint8Array`).
5. **Input Validation**: Passing non-UUID strings to `v1ToV6` or `v6ToV1` triggers string validation via `validate()`, throwing a `TypeError` with message `'Invalid UUID'`.
csx.json
{"case":{"caseId":"case:sha256:25ed5b68475a0e75cb772bb73bf68baecd9db69803dc60d231338f2fa2e81687","contract":["v1ToV6 losslessly reorders a UUIDv1 into a sortable UUIDv6 by rearranging timestamp fields into big-endian order and setting version 6, while v6ToV1 restores the original UUIDv1.","v6(options) produces the exact same UUID string as passing v1(options) output into v1ToV6","v1ToV6 and v6ToV1 operate identically on 16-byte Uint8Array buffers and return Uint8Array instances","v1ToV6 and v6ToV1 reject non-UUID string inputs with TypeError('Invalid UUID')"],"goal":"Convert losslessly between UUIDv1 and sortable UUIDv6 strings or byte arrays using v1ToV6 and v6ToV1 while preserving timestamp, clock sequence, and node fields","kind":"HOW","packages":["pkg:npm/uuid@11.1.1"],"schemaVersion":1,"symbols":["v1ToV6","v6ToV1","v6","v1"]},"contractCommand":["node","test/contract.mjs"],"environment":{"arch":"x64","ecosystem":"npm","executionContext":"node","language":"node","os":"linux","packageManager":"npm","runtime":"node","schemaVersion":1},"license":"MIT-0","packages":["pkg:npm/uuid@11.1.1"],"schemaVersion":1,"symbols":["v1ToV6","v6ToV1","v6","v1"],"verifierAdapter":"node-typescript@1"}
package-lock.json
{
  "name": "npm-uuid-v11",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "npm-uuid-v11",
      "version": "1.0.0",
      "dependencies": {
        "uuid": "11.1.1"
      }
    },
    "node_modules/uuid": {
      "version": "11.1.1",
      "resolved": "https://registry.npmjs.org/uuid/-/uuid-11.1.1.tgz",
      "integrity": "sha512-vIYxrBCC/N/K+Js3qSN88go7kIfNPssr/hHCesKCQNAjmgvYS2oqr69kIufEG+O4+PfezOH4EbIeHCfFov8ZgQ==",
      "funding": [
        "https://github.com/sponsors/broofa",
        "https://github.com/sponsors/ctavan"
      ],
      "license": "MIT",
      "bin": {
        "uuid": "dist/esm/bin/uuid"
      }
    }
  }
}
package.json
{
  "name": "npm-uuid-v11",
  "version": "1.0.0",
  "private": true,
  "type": "module",
  "scripts": {
    "test": "node test/contract.mjs"
  },
  "dependencies": {
    "uuid": "11.1.1"
  }
}
test/contract.mjs
import assert from 'node:assert/strict';
import { v1, v6, v1ToV6, v6ToV1, validate, version, parse } from 'uuid';

// 1. Lossless round-trip conversion on canonical UUID strings
const fixedNode = new Uint8Array([0x01, 0x23, 0x45, 0x67, 0x89, 0xab]);
const options = {
  msecs: 1321564380000,
  nsecs: 5432,
  clockseq: 0x385c,
  node: fixedNode,
};

const v1Str = v1(options);
assert.strictEqual(v1Str, 'edaeab38-1160-11e1-b85c-0123456789ab');
assert.strictEqual(validate(v1Str), true);
assert.strictEqual(version(v1Str), 1);

// v1ToV6 moves time-high (1e1), time-mid (1160), and time-low (edaeab38) into big-endian order
const v6Str = v1ToV6(v1Str);
assert.strictEqual(v6Str, '1e11160e-daea-6b38-b85c-0123456789ab');
assert.strictEqual(validate(v6Str), true);
assert.strictEqual(version(v6Str), 6);

// v6() generator with identical parameters matches v1ToV6(v1()) directly
const v6Direct = v6(options);
assert.strictEqual(v6Direct, v6Str);

// v6ToV1 losslessly reconstructs the original UUIDv1
const v1Restored = v6ToV1(v6Str);
assert.strictEqual(v1Restored, v1Str);
assert.strictEqual(version(v1Restored), 1);

// 2. Uint8Array byte-level transformation
const v1Bytes = parse(v1Str);
const v6Bytes = v1ToV6(v1Bytes);
assert.ok(v6Bytes instanceof Uint8Array);
assert.strictEqual(v6Bytes.length, 16);
assert.deepStrictEqual(v6Bytes, parse(v6Str));

const v1BytesRestored = v6ToV1(v6Bytes);
assert.ok(v1BytesRestored instanceof Uint8Array);
assert.strictEqual(v1BytesRestored.length, 16);
assert.deepStrictEqual(v1BytesRestored, v1Bytes);

// 3. String inputs are validated before conversion
assert.throws(
  () => v1ToV6('not-a-valid-uuid'),
  {
    name: 'TypeError',
    message: 'Invalid UUID',
  }
);
assert.throws(
  () => v6ToV1('not-a-valid-uuid'),
  {
    name: 'TypeError',
    message: 'Invalid UUID',
  }
);

오리진 시더

csx-seed