Musique/node_modules/@sapphire/snowflake/dist/index.mjs.map

1 line
9.9 KiB
Text
Raw Normal View History

{"version":3,"file":"index.mjs","sources":["../src/lib/Snowflake.ts","../src/lib/DiscordSnowflake.ts","../src/lib/TwitterSnowflake.ts"],"sourcesContent":["/* eslint-disable @typescript-eslint/explicit-member-accessibility */\n\n/**\n * A class for parsing snowflake ids\n */\nexport class Snowflake {\n\t/**\n\t * Internal incrementor for generating snowflakes\n\t * @internal\n\t */\n\t#increment = 0n;\n\n\t/**\n\t * Internal reference of the epoch passed in the constructor\n\t * @internal\n\t */\n\t#epoch: bigint;\n\n\t/**\n\t * Alias for {@link deconstruct}\n\t */\n\t// eslint-disable-next-line @typescript-eslint/unbound-method, @typescript-eslint/no-invalid-this\n\tpublic decode = this.deconstruct;\n\n\t/**\n\t * @param epoch the epoch to use\n\t */\n\tpublic constructor(epoch: number | bigint | Date) {\n\t\tthis.#epoch = BigInt(epoch instanceof Date ? epoch.getTime() : epoch);\n\t}\n\n\t/**\n\t * Generates a snowflake given an epoch and optionally a timestamp\n\t * @param options options to pass into the generator, see {@link SnowflakeGenerateOptions}\n\t *\n\t * **note** when increment is not provided it defaults to the private increment of the instance\n\t * @example\n\t * ```ts\n\t * const epoch = new Date('2000-01-01T00:00:00.000Z');\n\t * const snowflake = new Snowflake(epoch).generate();\n\t * ```\n\t * @returns A unique snowflake\n\t */\n\tpublic generate(\n\t\t{ increment = this.#increment, timestamp = Date.now(), workerID = 1n, processID = 1n }: SnowflakeGenerateOptions = {\n\t\t\tincrement: this.#increment,\n\t\t\ttimestamp: Date.now(),\n\t\t\tworkerID: 1n,\n\t\t\tprocessID: 1n\n\t\t}\n\t) {\n\t\tif (timestamp instanceof Date) timestamp = BigInt(timestamp.getTime());\n\t\tif (typeof timestamp === 'number' && !Number.isNaN(timestamp)) timestamp = BigInt(timestamp);\n\n\t\tif (typeof timestamp !== 'bigint') {\n\t\t\tthrow new TypeError(\n\t\t\t\t`\"timestamp\" argument must be a number, BigInt or Date (received ${Number.isNaN(timestamp) ? 'NaN' : typeof timestamp})`\n\t\t\t);\n\t\t}\n\n\t\tif (increment >= 4095n) increment = 0n;\n\n\t\t// timestamp, workerID, processID, increment\n\t\treturn ((timestamp - this.#epoch) << 22n) | (workerID << 17n) | (processID << 12n) | increment++;\n\t}\n\n\t/**\n\t * Deconstructs a snowflake given a snowflake ID\n\t * @param id the snowflake to deconstruct\n\t * @returns a deconstructed snowflake\n\t * @example\n\t * ```ts\n\t * const epoch = new Date('2000-01-01T00:00:00.000Z');\n\t * const snowflake = new Snowflake(epoch).deconstruct('3971046231244935168');\n\t * ```\n\t */\n\tpublic deconstruct(id: string | bigint): DeconstructedSnowflake {\n\t\tconst bigIntId = BigInt(id);\n\t\treturn {\n\t\t\tid: bigIntId,\n\t\t\ttimestamp: (bigIntId >> 22n) + this.#epoch,\n\t\t\tworkerID: (bigIntId >> 17n) & 0b11111n,\n\t\t\tprocessID: (bigIntId >> 12n) & 0b11111n,\n\t\t\tincrement: bigIntId & 0b111111111111n,\n\t\t\tepoch: this.#epoch\n\t\t};\n\t}\n}\n\n/**\n * Options for Snowflake#generate\n */\nexport interface SnowflakeGenerateOptions {\n\t/**\n\t * Timestamp or date of the snowflake to generate\n\t * @default Date.now()\n\t */\n\ttimestamp?: number | bigint | Date;\n\n\t/**\n\t * The increment to use\n\t * @default 0n\n\t * @remark keep in mind that this bigint is auto-incremented between generate calls\n\t */\n\tincrement?: bigint;\n\n\t/**\n\t * The worker ID to use\n\t * @default 1n\n\t */\n\tworkerID?: bigint;\n\n\t/**\n\t * The process ID to use\n\t * @default 1n\n\t */\n\tprocessID?: bigint;\n}\n\n/**\n * Object returned by Snowflake#deconstruct\n */\nexport interface DeconstructedSnowflake {\n\t/**\n\t * The id in BigInt form\n\t */\n\tid: bigint;\n\n\t/**\n\t * The timestamp stored in the snowflake\n\t */\n\ttimestamp: bigint;\n\n\t/**\n\t * The worker id stored in the snowflake\n\t */\n\tworkerID: bigint;\n\n\t/**\n\t * The process id stored in the snowflake\n\t */\n\tprocessID: bigint;\n\n\t/**\n\t * The increment stored in the snowflake\n\t */\n\tincrement: bigint;\n\n\t/**\n\t * The epoch to use in the snowflake\n\t */\n\tepoch: bigint;\n}\n","import { DeconstructedSnow