UNPKG

@langchain/core

Version:
1 lines 2.46 kB
{"version":3,"file":"v4.cjs","names":["rng","unsafeStringify"],"sources":["../../../src/utils/uuid/v4.ts"],"sourcesContent":["import rng from \"./rng.js\";\nimport { unsafeStringify } from \"./stringify.js\";\nimport type { UUIDTypes, Version4Options } from \"./types.js\";\n\nfunction v4(\n options?: Version4Options,\n buf?: undefined,\n offset?: number\n): string;\nfunction v4<TBuf extends Uint8Array = Uint8Array>(\n options: Version4Options | undefined,\n buf: TBuf,\n offset?: number\n): TBuf;\nfunction v4<TBuf extends Uint8Array = Uint8Array>(\n options?: Version4Options,\n buf?: TBuf,\n offset?: number\n): UUIDTypes<TBuf> {\n if (!buf && !options && crypto.randomUUID) {\n return crypto.randomUUID();\n }\n\n // Putting tail-code that could just go inline here in a separate function to\n // enable compiler optimizations that dramatically improve performance.\n //\n // REF: https://github.com/uuidjs/uuid/issues/892\n return _v4(options, buf, offset);\n}\n\nfunction _v4<TBuf extends Uint8Array = Uint8Array>(\n options?: Version4Options,\n buf?: TBuf,\n offset?: number\n): UUIDTypes<TBuf> {\n options = options || {};\n\n const rnds = options.random ?? options.rng?.() ?? rng();\n if (rnds.length < 16) {\n throw new Error(\"Random bytes length must be >= 16\");\n }\n\n // Per 4.4, set bits for version and `clock_seq_hi_and_reserved`\n rnds[6] = (rnds[6] & 0x0f) | 0x40;\n rnds[8] = (rnds[8] & 0x3f) | 0x80;\n\n // Copy bytes to buffer, if provided\n if (buf) {\n offset = offset || 0;\n if (offset < 0 || offset + 16 > buf.length) {\n throw new RangeError(\n `UUID byte range ${offset}:${offset + 15} is out of buffer bounds`\n );\n }\n\n for (let i = 0; i < 16; ++i) {\n buf[offset + i] = rnds[i];\n }\n\n return buf;\n }\n\n return unsafeStringify(rnds);\n}\nexport default v4;\n"],"mappings":";;;AAcA,SAAS,GACP,SACA,KACA,QACiB;AACjB,KAAI,CAAC,OAAO,CAAC,WAAW,OAAO,WAC7B,QAAO,OAAO,YAAY;AAO5B,QAAO,IAAI,SAAS,KAAK,OAAO;;AAGlC,SAAS,IACP,SACA,KACA,QACiB;AACjB,WAAU,WAAW,EAAE;CAEvB,MAAM,OAAO,QAAQ,UAAU,QAAQ,OAAO,IAAIA,YAAAA,SAAK;AACvD,KAAI,KAAK,SAAS,GAChB,OAAM,IAAI,MAAM,oCAAoC;AAItD,MAAK,KAAM,KAAK,KAAK,KAAQ;AAC7B,MAAK,KAAM,KAAK,KAAK,KAAQ;AAG7B,KAAI,KAAK;AACP,WAAS,UAAU;AACnB,MAAI,SAAS,KAAK,SAAS,KAAK,IAAI,OAClC,OAAM,IAAI,WACR,mBAAmB,OAAO,GAAG,SAAS,GAAG,0BAC1C;AAGH,OAAK,IAAI,IAAI,GAAG,IAAI,IAAI,EAAE,EACxB,KAAI,SAAS,KAAK,KAAK;AAGzB,SAAO;;AAGT,QAAOC,kBAAAA,gBAAgB,KAAK"}