import * as crypto from "@deepkit/crypto" ; const { randomBytes } = crypto ; let PROCESS_UNIQUE = undefined ; function getUnique ( ) { if ( PROCESS_UNIQUE ) return PROCESS_UNIQUE ; PROCESS_UNIQUE = randomBytes ( 5 ) ; return PROCESS_UNIQUE ; } export const hexTable = [ ] ; for ( let i = 0 ; i < 256 ; i ++ ) { hexTable [ i ] = ( i <= 15 ? '0' : '' ) + i . toString ( 16 ) ; } export const ObjectIdSymbol = Symbol . for ( 'deepkit/bson/objectid' ) ; export const UUIDSymbol = Symbol . for ( 'deepkit/bson/uuid' ) ; export function isUUID ( v ) { return ! ! v && v . hasOwnProperty ( UUIDSymbol ) ; } export function isObjectId ( v ) { return ! ! v && v . hasOwnProperty ( ObjectIdSymbol ) ; } /**
 * Thin wrapper around the native type to allow to serialize it correctly
 * in types like t.any.
*/ export class ObjectId { static index : number = Math . ceil ( Math . random ( ) & 0xffffff ) ; [ ObjectIdSymbol ] = true ; constructor ( public id ) { } static generate ( time ? ) { if ( ! time ) time = Math . ceil ( Date . now ( ) / 1000 ) ; const inc = ( ++ ObjectId . index ) % 0xffffff ; const processUnique = getUnique ( ) ; return '' + hexTable [ ( time >> 24 ) & 0xff ] + hexTable [ ( time >> 16 ) & 0xff ] + hexTable [ ( time >> 8 ) & 0xff ] + hexTable [ time & 0xff ] + hexTable [ processUnique [ 0 ] ] + hexTable [ processUnique [ 1 ] ] + hexTable [ processUnique [ 2 ] ] + hexTable [ processUnique [ 3 ] ] + hexTable [ processUnique [ 4 ] ] + hexTable [ ( inc >> 16 ) & 0xff ] + hexTable [ ( inc >> 8 ) & 0xff ] + hexTable [ inc & 0xff ] ; } } /**
 * Thin wrapper around the native type to allow to serialize it correctly
 * in types like t.any.
*/ export class UUID { [ UUIDSymbol ] = true ; constructor ( public id ) { } }