( options?: Version1Options, buf?: TBuf, offset?: number, )
| 35 | offset?: number, |
| 36 | ): Buf; |
| 37 | function v1<TBuf extends Uint8Array = Uint8Array>( |
| 38 | options?: Version1Options, |
| 39 | buf?: TBuf, |
| 40 | offset?: number, |
| 41 | ): UUIDTypes<TBuf> { |
| 42 | let bytes: Uint8Array; |
| 43 | |
| 44 | // Extract _v6 flag from options, clearing options if appropriate |
| 45 | const isV6 = options?._v6 ?? false; |
| 46 | if (options) { |
| 47 | const optionsKeys = Object.keys(options); |
| 48 | if (optionsKeys.length === 1 && optionsKeys[0] === '_v6') { |
| 49 | options = undefined; |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | if (options) { |
| 54 | // With options: Make UUID independent of internal state |
| 55 | bytes = v1Bytes( |
| 56 | options.random ?? options.rng?.() ?? rng(), |
| 57 | options.msecs, |
| 58 | options.nsecs, |
| 59 | options.clockseq, |
| 60 | options.node, |
| 61 | buf, |
| 62 | offset, |
| 63 | ); |
| 64 | } else { |
| 65 | // Without options: Make UUID from internal state |
| 66 | const now = Date.now(); |
| 67 | const rnds = rng(); |
| 68 | |
| 69 | updateV1State(_state, now, rnds); |
| 70 | |
| 71 | // Geenerate UUID. Note that v6 uses random values for `clockseq` and |
| 72 | // `node`. |
| 73 | // |
| 74 | // https://www.rfc-editor.org/rfc/rfc9562.html#section-5.6-4 |
| 75 | bytes = v1Bytes( |
| 76 | rnds, |
| 77 | _state.msecs, |
| 78 | _state.nsecs, |
| 79 | // v6 UUIDs get random `clockseq` and `node` for every UUID |
| 80 | // https://www.rfc-editor.org/rfc/rfc9562.html#section-5.6-4 |
| 81 | isV6 ? undefined : _state.clockseq, |
| 82 | isV6 ? undefined : _state.node, |
| 83 | buf, |
| 84 | offset, |
| 85 | ); |
| 86 | } |
| 87 | |
| 88 | return buf ?? unsafeStringify(bytes); |
| 89 | } |
| 90 | |
| 91 | // (Private!) Do not use. This method is only exported for testing purposes |
| 92 | // and may change without notice. |
no test coverage detected