SupportBot 7.3

This commit is contained in:
ItzEmerald committed 2021-10-04 13:54:30 +01:00
1 parent 19a12f6c8f
commit 09f0873f27
4762 files changed
+504608 -52

No files matched your search

+24
View File
@@ -0,0 +1,24 @@
# The MIT License (MIT)
Copyright © `2020` `The Sapphire Community and its contributors`
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the “Software”), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
+95
View File
@@ -0,0 +1,95 @@
<div align="center">
![Sapphire Logo](https://cdn.skyra.pw/gh-assets/sapphire-banner.png)
# @sapphire/async-queue
**Sequential asynchronous lock-based queue for promises.**
[![GitHub](https://img.shields.io/github/license/sapphiredev/utilities)](https://github.com/sapphiredev/utilities/blob/main/LICENSE.md)
[![codecov](https://codecov.io/gh/sapphiredev/utilities/branch/main/graph/badge.svg?token=OEGIV6RFDO)](https://codecov.io/gh/sapphiredev/utilities)
[![npm bundle size](https://img.shields.io/bundlephobia/min/@sapphire/async-queue?logo=webpack&style=flat-square)](https://bundlephobia.com/result?p=@sapphire/async-queue)
[![npm](https://img.shields.io/npm/v/@sapphire/async-queue?color=crimson&logo=npm&style=flat-square)](https://www.npmjs.com/package/@sapphire/async-queue)
[![Depfu](https://badges.depfu.com/badges/ec42ff3d6bae55eee1de4749960852b3/count.svg)](https://depfu.com/github/sapphiredev/utilities?project_id=15195)
</div>
## Description
Ever needed a queue for a set of promises? This is the package for you.
## Features
- Written in TypeScript
- Bundled with Rollup so it can be used in NodeJS and browsers
- Offers CommonJS, ESM and UMD bundles
- Fully tested
## Installation
You can use the following command to install this package, or replace `npm install` with your package manager of choice.
```sh
npm install @sapphire/async-queue
```
---
## API Documentation
For the full API documentation please refer to the TypeDoc generated [documentation](https://sapphiredev.github.io/utilities/modules/_sapphire_async_queue.html).
## Buy us some doughnuts
Sapphire Community is and always will be open source, even if we don't get donations. That being said, we know there are amazing people who may still want to donate just to show their appreciation. Thank you very much in advance!
We accept donations through Open Collective, Ko-fi, PayPal, Patreon and GitHub Sponsorships. You can use the buttons below to donate through your method of choice.
| Donate With | Address |
| :-------------: | :-------------------------------------------------: |
| Open Collective | [Click Here](https://sapphirejs.dev/opencollective) |
| Ko-fi | [Click Here](https://sapphirejs.dev/kofi) |
| Patreon | [Click Here](https://sapphirejs.dev/patreon) |
| PayPal | [Click Here](https://sapphirejs.dev/paypal) |
## Contributors ✨
Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/docs/en/emoji-key)):
<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
<!-- prettier-ignore-start -->
<!-- markdownlint-disable -->
<table>
<tr>
<td align="center"><a href="https://favware.tech/"><img src="https://avatars3.githubusercontent.com/u/4019718?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Jeroen Claassens</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=Favna" title="Code">💻</a> <a href="#infra-Favna" title="Infrastructure (Hosting, Build-Tools, etc)">🚇</a> <a href="#projectManagement-Favna" title="Project Management">📆</a> <a href="https://github.com/sapphiredev/utilities/commits?author=Favna" title="Documentation">📖</a> <a href="https://github.com/sapphiredev/utilities/commits?author=Favna" title="Tests">⚠️</a></td>
<td align="center"><a href="https://github.com/kyranet"><img src="https://avatars0.githubusercontent.com/u/24852502?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Antonio Román</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=kyranet" title="Code">💻</a> <a href="#projectManagement-kyranet" title="Project Management">📆</a> <a href="https://github.com/sapphiredev/utilities/pulls?q=is%3Apr+reviewed-by%3Akyranet" title="Reviewed Pull Requests">👀</a> <a href="https://github.com/sapphiredev/utilities/commits?author=kyranet" title="Tests">⚠️</a></td>
<td align="center"><a href="https://github.com/PyroTechniac"><img src="https://avatars2.githubusercontent.com/u/39341355?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Gryffon Bellish</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=PyroTechniac" title="Code">💻</a> <a href="https://github.com/sapphiredev/utilities/pulls?q=is%3Apr+reviewed-by%3APyroTechniac" title="Reviewed Pull Requests">👀</a> <a href="https://github.com/sapphiredev/utilities/commits?author=PyroTechniac" title="Tests">⚠️</a></td>
<td align="center"><a href="https://github.com/vladfrangu"><img src="https://avatars3.githubusercontent.com/u/17960496?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Vlad Frangu</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=vladfrangu" title="Code">💻</a> <a href="https://github.com/sapphiredev/utilities/issues?q=author%3Avladfrangu" title="Bug reports">🐛</a> <a href="https://github.com/sapphiredev/utilities/pulls?q=is%3Apr+reviewed-by%3Avladfrangu" title="Reviewed Pull Requests">👀</a> <a href="#userTesting-vladfrangu" title="User Testing">📓</a> <a href="https://github.com/sapphiredev/utilities/commits?author=vladfrangu" title="Tests">⚠️</a></td>
<td align="center"><a href="https://github.com/Soumil07"><img src="https://avatars0.githubusercontent.com/u/29275227?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Soumil07</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=Soumil07" title="Code">💻</a> <a href="#projectManagement-Soumil07" title="Project Management">📆</a> <a href="https://github.com/sapphiredev/utilities/commits?author=Soumil07" title="Tests">⚠️</a></td>
<td align="center"><a href="https://github.com/apps/depfu"><img src="https://avatars3.githubusercontent.com/in/715?v=4?s=100" width="100px;" alt=""/><br /><sub><b>depfu[bot]</b></sub></a><br /><a href="#maintenance-depfu[bot]" title="Maintenance">🚧</a></td>
<td align="center"><a href="https://github.com/apps/allcontributors"><img src="https://avatars0.githubusercontent.com/in/23186?v=4?s=100" width="100px;" alt=""/><br /><sub><b>allcontributors[bot]</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=allcontributors[bot]" title="Documentation">📖</a></td>
</tr>
<tr>
<td align="center"><a href="https://github.com/Nytelife26"><img src="https://avatars1.githubusercontent.com/u/22531310?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Tyler J Russell</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=Nytelife26" title="Documentation">📖</a></td>
<td align="center"><a href="https://github.com/Alcremie"><img src="https://avatars0.githubusercontent.com/u/54785334?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Ivan Lieder</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=Alcremie" title="Code">💻</a> <a href="https://github.com/sapphiredev/utilities/issues?q=author%3AAlcremie" title="Bug reports">🐛</a></td>
<td align="center"><a href="https://github.com/RealShadowNova"><img src="https://avatars3.githubusercontent.com/u/46537907?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Hezekiah Hendry</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=RealShadowNova" title="Code">💻</a></td>
<td align="center"><a href="https://github.com/Stitch07"><img src="https://avatars.githubusercontent.com/u/29275227?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Stitch07</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=Stitch07" title="Code">💻</a> <a href="https://github.com/sapphiredev/utilities/commits?author=Stitch07" title="Tests">⚠️</a></td>
<td align="center"><a href="https://github.com/Vetlix"><img src="https://avatars.githubusercontent.com/u/31412314?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Vetlix</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=Vetlix" title="Code">💻</a></td>
<td align="center"><a href="https://github.com/ethamitc"><img src="https://avatars.githubusercontent.com/u/27776796?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Ethan Mitchell</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=ethamitc" title="Documentation">📖</a></td>
<td align="center"><a href="https://github.com/noftaly"><img src="https://avatars.githubusercontent.com/u/34779161?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Elliot</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=noftaly" title="Code">💻</a></td>
</tr>
<tr>
<td align="center"><a href="https://jurien.dev"><img src="https://avatars.githubusercontent.com/u/5418114?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Jurien Hamaker</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=jurienhamaker" title="Code">💻</a></td>
<td align="center"><a href="https://fanoulis.dev/"><img src="https://avatars.githubusercontent.com/u/38255093?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Charalampos Fanoulis</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=cfanoulis" title="Documentation">📖</a></td>
<td align="center"><a href="https://github.com/apps/dependabot"><img src="https://avatars.githubusercontent.com/in/29110?v=4?s=100" width="100px;" alt=""/><br /><sub><b>dependabot[bot]</b></sub></a><br /><a href="#maintenance-dependabot[bot]" title="Maintenance">🚧</a></td>
<td align="center"><a href="https://kaname.netlify.app/"><img src="https://avatars.githubusercontent.com/u/56084970?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Kaname</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=kaname-png" title="Code">💻</a></td>
<td align="center"><a href="https://github.com/nandhagk"><img src="https://avatars.githubusercontent.com/u/62976649?v=4?s=100" width="100px;" alt=""/><br /><sub><b>nandhagk</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/issues?q=author%3Anandhagk" title="Bug reports">🐛</a></td>
</tr>
</table>
<!-- markdownlint-restore -->
<!-- prettier-ignore-end -->
<!-- ALL-CONTRIBUTORS-LIST:END -->
This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome!
+2
View File
@@ -0,0 +1,2 @@
export * from './lib/AsyncQueue';
//# sourceMappingURL=index.d.ts.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,kBAAkB,CAAC"}
+70
View File
@@ -0,0 +1,70 @@
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
/**
* The AsyncQueue class used to sequentialize burst requests
*/
class AsyncQueue {
constructor() {
/**
* The promises array
*/
Object.defineProperty(this, "promises", {
enumerable: true,
configurable: true,
writable: true,
value: []
});
}
/**
* The remaining amount of queued promises
*/
get remaining() {
return this.promises.length;
}
/**
* Waits for last promise and queues a new one
* @example
* ```
* const queue = new AsyncQueue();
* async function request(url, options) {
* await queue.wait();
* try {
* const result = await fetch(url, options);
* // Do some operations with 'result'
* } finally {
* // Remove first entry from the queue and resolve for the next entry
* queue.shift();
* }
* }
*
* request(someUrl1, someOptions1); // Will call fetch() immediately
* request(someUrl2, someOptions2); // Will call fetch() after the first finished
* request(someUrl3, someOptions3); // Will call fetch() after the second finished
* ```
*/
wait() {
const next = this.promises.length ? this.promises[this.promises.length - 1].promise : Promise.resolve();
let resolve;
const promise = new Promise((res) => {
resolve = res;
});
this.promises.push({
resolve: resolve,
promise
});
return next;
}
/**
* Frees the queue's lock for the next item to process
*/
shift() {
const deferred = this.promises.shift();
if (typeof deferred !== 'undefined')
deferred.resolve();
}
}
exports.AsyncQueue = AsyncQueue;
//# sourceMappingURL=index.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"index.js","sources":["../src/lib/AsyncQueue.ts"],"sourcesContent":["/**\n * The AsyncQueue class used to sequentialize burst requests\n */\nexport class AsyncQueue {\n\t/**\n\t * The remaining amount of queued promises\n\t */\n\tpublic get remaining(): number {\n\t\treturn this.promises.length;\n\t}\n\n\t/**\n\t * The promises array\n\t */\n\tprivate promises: InternalAsyncQueueDeferredPromise[] = [];\n\n\t/**\n\t * Waits for last promise and queues a new one\n\t * @example\n\t * ```\n\t * const queue = new AsyncQueue();\n\t * async function request(url, options) {\n\t * await queue.wait();\n\t * try {\n\t * const result = await fetch(url, options);\n\t * // Do some operations with 'result'\n\t * } finally {\n\t * // Remove first entry from the queue and resolve for the next entry\n\t * queue.shift();\n\t * }\n\t * }\n\t *\n\t * request(someUrl1, someOptions1); // Will call fetch() immediately\n\t * request(someUrl2, someOptions2); // Will call fetch() after the first finished\n\t * request(someUrl3, someOptions3); // Will call fetch() after the second finished\n\t * ```\n\t */\n\tpublic wait(): Promise<void> {\n\t\tconst next = this.promises.length ? this.promises[this.promises.length - 1].promise : Promise.resolve();\n\t\tlet resolve: () => void;\n\t\tconst promise = new Promise<void>((res) => {\n\t\t\tresolve = res;\n\t\t});\n\n\t\tthis.promises.push({\n\t\t\tresolve: resolve!,\n\t\t\tpromise\n\t\t});\n\n\t\treturn next;\n\t}\n\n\t/**\n\t * Frees the queue's lock for the next item to process\n\t */\n\tpublic shift(): void {\n\t\tconst deferred = this.promises.shift();\n\t\tif (typeof deferred !== 'undefined') deferred.resolve();\n\t}\n}\n\n/**\n * @internal\n */\ninterface InternalAsyncQueueDeferredPromise {\n\tresolve(): void;\n\tpromise: Promise<void>;\n}\n"],"names":[],"mappings":";;;;AAAA;;;MAGa,UAAU;IAAvB;;;;QAWC;;;;mBAAwD,EAAE;WAAC;KA6C3D;;;;IApDA,IAAW,SAAS;QACnB,OAAO,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC;KAC5B;;;;;;;;;;;;;;;;;;;;;;IA4BM,IAAI;QACV,MAAM,IAAI,GAAG,IAAI,CAAC,QAAQ,CAAC,MAAM,GAAG,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,QAAQ,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,OAAO,GAAG,OAAO,CAAC,OAAO,EAAE,CAAC;QACxG,IAAI,OAAmB,CAAC;QACxB,MAAM,OAAO,GAAG,IAAI,OAAO,CAAO,CAAC,GAAG;YACrC,OAAO,GAAG,GAAG,CAAC;SACd,CAAC,CAAC;QAEH,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC;YAClB,OAAO,EAAE,OAAQ;YACjB,OAAO;SACP,CAAC,CAAC;QAEH,OAAO,IAAI,CAAC;KACZ;;;;IAKM,KAAK;QACX,MAAM,QAAQ,GAAG,IAAI,CAAC,QAAQ,CAAC,KAAK,EAAE,CAAC;QACvC,IAAI,OAAO,QAAQ,KAAK,WAAW;YAAE,QAAQ,CAAC,OAAO,EAAE,CAAC;KACxD;;;;;"}
+66
View File
@@ -0,0 +1,66 @@
/**
* The AsyncQueue class used to sequentialize burst requests
*/
class AsyncQueue {
constructor() {
/**
* The promises array
*/
Object.defineProperty(this, "promises", {
enumerable: true,
configurable: true,
writable: true,
value: []
});
}
/**
* The remaining amount of queued promises
*/
get remaining() {
return this.promises.length;
}
/**
* Waits for last promise and queues a new one
* @example
* ```
* const queue = new AsyncQueue();
* async function request(url, options) {
* await queue.wait();
* try {
* const result = await fetch(url, options);
* // Do some operations with 'result'
* } finally {
* // Remove first entry from the queue and resolve for the next entry
* queue.shift();
* }
* }
*
* request(someUrl1, someOptions1); // Will call fetch() immediately
* request(someUrl2, someOptions2); // Will call fetch() after the first finished
* request(someUrl3, someOptions3); // Will call fetch() after the second finished
* ```
*/
wait() {
const next = this.promises.length ? this.promises[this.promises.length - 1].promise : Promise.resolve();
let resolve;
const promise = new Promise((res) => {
resolve = res;
});
this.promises.push({
resolve: resolve,
promise
});
return next;
}
/**
* Frees the queue's lock for the next item to process
*/
shift() {
const deferred = this.promises.shift();
if (typeof deferred !== 'undefined')
deferred.resolve();
}
}
export { AsyncQueue };
//# sourceMappingURL=index.mjs.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"index.mjs","sources":["../src/lib/AsyncQueue.ts"],"sourcesContent":["/**\n * The AsyncQueue class used to sequentialize burst requests\n */\nexport class AsyncQueue {\n\t/**\n\t * The remaining amount of queued promises\n\t */\n\tpublic get remaining(): number {\n\t\treturn this.promises.length;\n\t}\n\n\t/**\n\t * The promises array\n\t */\n\tprivate promises: InternalAsyncQueueDeferredPromise[] = [];\n\n\t/**\n\t * Waits for last promise and queues a new one\n\t * @example\n\t * ```\n\t * const queue = new AsyncQueue();\n\t * async function request(url, options) {\n\t * await queue.wait();\n\t * try {\n\t * const result = await fetch(url, options);\n\t * // Do some operations with 'result'\n\t * } finally {\n\t * // Remove first entry from the queue and resolve for the next entry\n\t * queue.shift();\n\t * }\n\t * }\n\t *\n\t * request(someUrl1, someOptions1); // Will call fetch() immediately\n\t * request(someUrl2, someOptions2); // Will call fetch() after the first finished\n\t * request(someUrl3, someOptions3); // Will call fetch() after the second finished\n\t * ```\n\t */\n\tpublic wait(): Promise<void> {\n\t\tconst next = this.promises.length ? this.promises[this.promises.length - 1].promise : Promise.resolve();\n\t\tlet resolve: () => void;\n\t\tconst promise = new Promise<void>((res) => {\n\t\t\tresolve = res;\n\t\t});\n\n\t\tthis.promises.push({\n\t\t\tresolve: resolve!,\n\t\t\tpromise\n\t\t});\n\n\t\treturn next;\n\t}\n\n\t/**\n\t * Frees the queue's lock for the next item to process\n\t */\n\tpublic shift(): void {\n\t\tconst deferred = this.promises.shift();\n\t\tif (typeof deferred !== 'undefined') deferred.resolve();\n\t}\n}\n\n/**\n * @internal\n */\ninterface InternalAsyncQueueDeferredPromise {\n\tresolve(): void;\n\tpromise: Promise<void>;\n}\n"],"names":[],"mappings":"AAAA;;;MAGa,UAAU;IAAvB;;;;QAWC;;;;mBAAwD,EAAE;WAAC;KA6C3D;;;;IApDA,IAAW,SAAS;QACnB,OAAO,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC;KAC5B;;;;;;;;;;;;;;;;;;;;;;IA4BM,IAAI;QACV,MAAM,IAAI,GAAG,IAAI,CAAC,QAAQ,CAAC,MAAM,GAAG,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,QAAQ,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,OAAO,GAAG,OAAO,CAAC,OAAO,EAAE,CAAC;QACxG,IAAI,OAAmB,CAAC;QACxB,MAAM,OAAO,GAAG,IAAI,OAAO,CAAO,CAAC,GAAG;YACrC,OAAO,GAAG,GAAG,CAAC;SACd,CAAC,CAAC;QAEH,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC;YAClB,OAAO,EAAE,OAAQ;YACjB,OAAO;SACP,CAAC,CAAC;QAEH,OAAO,IAAI,CAAC;KACZ;;;;IAKM,KAAK;QACX,MAAM,QAAQ,GAAG,IAAI,CAAC,QAAQ,CAAC,KAAK,EAAE,CAAC;QACvC,IAAI,OAAO,QAAQ,KAAK,WAAW;YAAE,QAAQ,CAAC,OAAO,EAAE,CAAC;KACxD;;;;;"}
+76
View File
@@ -0,0 +1,76 @@
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
typeof define === 'function' && define.amd ? define(['exports'], factory) :
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.SapphireAsyncQueue = {}));
}(this, (function (exports) { 'use strict';
/**
* The AsyncQueue class used to sequentialize burst requests
*/
class AsyncQueue {
constructor() {
/**
* The promises array
*/
Object.defineProperty(this, "promises", {
enumerable: true,
configurable: true,
writable: true,
value: []
});
}
/**
* The remaining amount of queued promises
*/
get remaining() {
return this.promises.length;
}
/**
* Waits for last promise and queues a new one
* @example
* ```
* const queue = new AsyncQueue();
* async function request(url, options) {
* await queue.wait();
* try {
* const result = await fetch(url, options);
* // Do some operations with 'result'
* } finally {
* // Remove first entry from the queue and resolve for the next entry
* queue.shift();
* }
* }
*
* request(someUrl1, someOptions1); // Will call fetch() immediately
* request(someUrl2, someOptions2); // Will call fetch() after the first finished
* request(someUrl3, someOptions3); // Will call fetch() after the second finished
* ```
*/
wait() {
const next = this.promises.length ? this.promises[this.promises.length - 1].promise : Promise.resolve();
let resolve;
const promise = new Promise((res) => {
resolve = res;
});
this.promises.push({
resolve: resolve,
promise
});
return next;
}
/**
* Frees the queue's lock for the next item to process
*/
shift() {
const deferred = this.promises.shift();
if (typeof deferred !== 'undefined')
deferred.resolve();
}
}
exports.AsyncQueue = AsyncQueue;
Object.defineProperty(exports, '__esModule', { value: true });
})));
//# sourceMappingURL=index.umd.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"index.umd.js","sources":["../src/lib/AsyncQueue.ts"],"sourcesContent":["/**\n * The AsyncQueue class used to sequentialize burst requests\n */\nexport class AsyncQueue {\n\t/**\n\t * The remaining amount of queued promises\n\t */\n\tpublic get remaining(): number {\n\t\treturn this.promises.length;\n\t}\n\n\t/**\n\t * The promises array\n\t */\n\tprivate promises: InternalAsyncQueueDeferredPromise[] = [];\n\n\t/**\n\t * Waits for last promise and queues a new one\n\t * @example\n\t * ```\n\t * const queue = new AsyncQueue();\n\t * async function request(url, options) {\n\t * await queue.wait();\n\t * try {\n\t * const result = await fetch(url, options);\n\t * // Do some operations with 'result'\n\t * } finally {\n\t * // Remove first entry from the queue and resolve for the next entry\n\t * queue.shift();\n\t * }\n\t * }\n\t *\n\t * request(someUrl1, someOptions1); // Will call fetch() immediately\n\t * request(someUrl2, someOptions2); // Will call fetch() after the first finished\n\t * request(someUrl3, someOptions3); // Will call fetch() after the second finished\n\t * ```\n\t */\n\tpublic wait(): Promise<void> {\n\t\tconst next = this.promises.length ? this.promises[this.promises.length - 1].promise : Promise.resolve();\n\t\tlet resolve: () => void;\n\t\tconst promise = new Promise<void>((res) => {\n\t\t\tresolve = res;\n\t\t});\n\n\t\tthis.promises.push({\n\t\t\tresolve: resolve!,\n\t\t\tpromise\n\t\t});\n\n\t\treturn next;\n\t}\n\n\t/**\n\t * Frees the queue's lock for the next item to process\n\t */\n\tpublic shift(): void {\n\t\tconst deferred = this.promises.shift();\n\t\tif (typeof deferred !== 'undefined') deferred.resolve();\n\t}\n}\n\n/**\n * @internal\n */\ninterface InternalAsyncQueueDeferredPromise {\n\tresolve(): void;\n\tpromise: Promise<void>;\n}\n"],"names":[],"mappings":";;;;;;CAAA;;;OAGa,UAAU;KAAvB;;;;SAWC;;;;oBAAwD,EAAE;YAAC;MA6C3D;;;;KApDA,IAAW,SAAS;SACnB,OAAO,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC;MAC5B;;;;;;;;;;;;;;;;;;;;;;KA4BM,IAAI;SACV,MAAM,IAAI,GAAG,IAAI,CAAC,QAAQ,CAAC,MAAM,GAAG,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,QAAQ,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,OAAO,GAAG,OAAO,CAAC,OAAO,EAAE,CAAC;SACxG,IAAI,OAAmB,CAAC;SACxB,MAAM,OAAO,GAAG,IAAI,OAAO,CAAO,CAAC,GAAG;aACrC,OAAO,GAAG,GAAG,CAAC;UACd,CAAC,CAAC;SAEH,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC;aAClB,OAAO,EAAE,OAAQ;aACjB,OAAO;UACP,CAAC,CAAC;SAEH,OAAO,IAAI,CAAC;MACZ;;;;KAKM,KAAK;SACX,MAAM,QAAQ,GAAG,IAAI,CAAC,QAAQ,CAAC,KAAK,EAAE,CAAC;SACvC,IAAI,OAAO,QAAQ,KAAK,WAAW;aAAE,QAAQ,CAAC,OAAO,EAAE,CAAC;MACxD;;;;;;;;;;;"}
+40
View File
@@ -0,0 +1,40 @@
/**
* The AsyncQueue class used to sequentialize burst requests
*/
export declare class AsyncQueue {
/**
* The remaining amount of queued promises
*/
get remaining(): number;
/**
* The promises array
*/
private promises;
/**
* Waits for last promise and queues a new one
* @example
* ```
* const queue = new AsyncQueue();
* async function request(url, options) {
* await queue.wait();
* try {
* const result = await fetch(url, options);
* // Do some operations with 'result'
* } finally {
* // Remove first entry from the queue and resolve for the next entry
* queue.shift();
* }
* }
*
* request(someUrl1, someOptions1); // Will call fetch() immediately
* request(someUrl2, someOptions2); // Will call fetch() after the first finished
* request(someUrl3, someOptions3); // Will call fetch() after the second finished
* ```
*/
wait(): Promise<void>;
/**
* Frees the queue's lock for the next item to process
*/
shift(): void;
}
//# sourceMappingURL=AsyncQueue.d.ts.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"AsyncQueue.d.ts","sourceRoot":"","sources":["../../src/lib/AsyncQueue.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,qBAAa,UAAU;IACtB;;OAEG;IACH,IAAW,SAAS,IAAI,MAAM,CAE7B;IAED;;OAEG;IACH,OAAO,CAAC,QAAQ,CAA2C;IAE3D;;;;;;;;;;;;;;;;;;;;OAoBG;IACI,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC;IAe5B;;OAEG;IACI,KAAK,IAAI,IAAI;CAIpB"}
+55
View File
@@ -0,0 +1,55 @@
{
"name": "@sapphire/async-queue",
"version": "1.1.4",
"description": "Sequential asynchronous lock-based queue for promises",
"author": "@sapphire",
"license": "MIT",
"main": "dist/index.js",
"module": "dist/index.mjs",
"browser": "dist/index.umd.js",
"unpkg": "dist/index.umd.js",
"types": "dist/index.d.ts",
"exports": {
"import": "./dist/index.mjs",
"require": "./dist/index.js"
},
"sideEffects": false,
"homepage": "https://github.com/sapphiredev/utilities/tree/main/packages/async-queue",
"scripts": {
"test": "jest",
"lint": "eslint src tests --ext ts --fix -c ../../.eslintrc",
"build": "rollup -c",
"start": "yarn build -w",
"prepublishOnly": "yarn build"
},
"repository": {
"type": "git",
"url": "git+https://github.com/sapphiredev/utilities.git",
"directory": "packages/async-queue"
},
"files": [
"dist",
"!dist/*.tsbuildinfo"
],
"engines": {
"node": ">=14",
"npm": ">=6"
},
"keywords": [
"@sapphire/async-queue",
"bot",
"typescript",
"ts",
"yarn",
"discord",
"sapphire",
"standalone"
],
"bugs": {
"url": "https://github.com/sapphiredev/utilities/issues"
},
"publishConfig": {
"access": "public"
},
"gitHead": "632c305cb9666dcff8c0ee71167570b8df46ccab"
}
+24
View File
@@ -0,0 +1,24 @@
# The MIT License (MIT)
Copyright © `2020` `The Sapphire Community and its contributors`
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the “Software”), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
+218
View File
@@ -0,0 +1,218 @@
<div align="center">
![Sapphire Logo](https://cdn.skyra.pw/gh-assets/sapphire-banner.png)
# @sapphire/snowflake
**Deconstruct and generate snowflake IDs using BigInts.**
[![GitHub](https://img.shields.io/github/license/sapphiredev/utilities)](https://github.com/sapphiredev/utilities/blob/main/LICENSE.md)
[![codecov](https://codecov.io/gh/sapphiredev/utilities/branch/main/graph/badge.svg?token=OEGIV6RFDO)](https://codecov.io/gh/sapphiredev/utilities)
[![npm bundle size](https://img.shields.io/bundlephobia/min/@sapphire/snowflake?logo=webpack&style=flat-square)](https://bundlephobia.com/result?p=@sapphire/snowflake)
[![npm](https://img.shields.io/npm/v/@sapphire/snowflake?color=crimson&logo=npm&style=flat-square)](https://www.npmjs.com/package/@sapphire/snowflake)
[![Depfu](https://badges.depfu.com/badges/ec42ff3d6bae55eee1de4749960852b3/count.svg)](https://depfu.com/github/sapphiredev/utilities?project_id=15195)
</div>
**Table of Contents**
- [Description](#description)
- [Features](#features)
- [Installation](#installation)
- [Usage](#usage)
- [Constructing snowflakes](#constructing-snowflakes)
- [Snowflakes with custom epoch](#snowflakes-with-custom-epoch)
- [Snowflake with Discord epoch constant](#snowflake-with-discord-epoch-constant)
- [Snowflake with Twitter epoch constant](#snowflake-with-twitter-epoch-constant)
- [Deconstructing snowflakes](#deconstructing-snowflakes)
- [Snowflakes with custom epoch](#snowflakes-with-custom-epoch-1)
- [Snowflake with Discord epoch constant](#snowflake-with-discord-epoch-constant-1)
- [Snowflake with Twitter epoch constant](#snowflake-with-twitter-epoch-constant-1)
- [API Documentation](#api-documentation)
- [Buy us some doughnuts](#buy-us-some-doughnuts)
- [Contributors ✨](#contributors-%E2%9C%A8)
## Description
There is often a need to get a unique ID for entities, be that for Discord messages/channels/servers, keys in a database or many other similar examples. There are many ways to get such a unique ID, and one of those is using a so-called "snowflake". You can read more about snowflake IDs in [this Medium article](https://medium.com/better-programming/uuid-generation-snowflake-identifiers-unique-2aed8b1771bc).
## Features
- Written in TypeScript
- Bundled with Rollup so it can be used in NodeJS and browsers
- Offers CommonJS, ESM and UMD bundles
- Offers predefined epochs for Discord and Twitter
- Fully tested
## Installation
You can use the following command to install this package, or replace `npm install` with your package manager of choice.
```sh
npm install @sapphire/snowflake
```
## Usage
**Note:** While this section uses `require`, the imports match 1:1 with ESM imports. For example `const { Snowflake } = require('@sapphire/snowflake')` equals `import { Snowflake } from '@sapphire/snowflake'`.
### Constructing snowflakes
#### Snowflakes with custom epoch
```ts
// Import the Snowflake class
const { Snowflake } = require('@sapphire/snowflake');
// Define a custom epoch
const epoch = new Date('2000-01-01T00:00:00.000Z');
// Create an instance of Snowflake
const snowflake = new Snowflake(epoch);
// Generate a snowflake with the given epoch
const uniqueId = snowflake.generate();
```
#### Snowflake with Discord epoch constant
```ts
// Import the Snowflake class
const { DiscordSnowflake } = require('@sapphire/snowflake');
// Create an instance of Snowflake
const discordSnowflake = new DiscordSnowflake();
// Generate a snowflake with Discord's epoch
const uniqueId = discordSnowflake.generate();
// Alternatively, you can use the method directly
const uniqueId = DiscordSnowflake.generate();
```
#### Snowflake with Twitter epoch constant
```ts
// Import the Snowflake class
const { TwitterSnowflake } = require('@sapphire/snowflake');
// Create an instance of Snowflake
const twitterSnowflake = new TwitterSnowflake();
// Generate a snowflake with Twitter's epoch
const uniqueId = twitterSnowflake.generate();
// Alternatively, you can use the method directly
const uniqueId = TwitterSnowflake.generate();
```
### Deconstructing snowflakes
#### Snowflakes with custom epoch
```ts
// Import the Snowflake class
const { Snowflake } = require('@sapphire/snowflake');
// Define a custom epoch
const epoch = new Date('2000-01-01T00:00:00.000Z');
// Create an instance of Snowflake
const snowflake = new Snowflake(epoch);
// Deconstruct a snowflake with the given epoch
const uniqueId = snowflake.deconstruct('3971046231244935168');
```
#### Snowflake with Discord epoch constant
```ts
// Import the Snowflake class
const { DiscordSnowflake } = require('@sapphire/snowflake');
// Create an instance of Snowflake
const discordSnowflake = new DiscordSnowflake();
// Deconstruct a snowflake with Discord's epoch
const uniqueId = discordSnowflake.deconstruct('3971046231244935168');
// Alternatively, you can use the method directly
const uniqueId = DiscordSnowflake.deconstruct('3971046231244935168');
```
#### Snowflake with Twitter epoch constant
```ts
// Import the Snowflake class
const { TwitterSnowflake } = require('@sapphire/snowflake');
// Create an instance of Snowflake
const twitterSnowflake = new TwitterSnowflake();
// Deconstruct a snowflake with Twitter's epoch
const uniqueId = twitterSnowflake.deconstruct('3971046231244935168');
// Alternatively, you can use the method directly
const uniqueId = TwitterSnowflake.deconstruct('3971046231244935168');
```
---
## API Documentation
For the full API documentation please refer to the TypeDoc generated [documentation](https://sapphiredev.github.io/utilities/modules/_sapphire_snowflake.html).
## Buy us some doughnuts
Sapphire Community is and always will be open source, even if we don't get donations. That being said, we know there are amazing people who may still want to donate just to show their appreciation. Thank you very much in advance!
We accept donations through Open Collective, Ko-fi, PayPal, Patreon and GitHub Sponsorships. You can use the buttons below to donate through your method of choice.
| Donate With | Address |
| :-------------: | :-------------------------------------------------: |
| Open Collective | [Click Here](https://sapphirejs.dev/opencollective) |
| Ko-fi | [Click Here](https://sapphirejs.dev/kofi) |
| Patreon | [Click Here](https://sapphirejs.dev/patreon) |
| PayPal | [Click Here](https://sapphirejs.dev/paypal) |
## Contributors ✨
Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/docs/en/emoji-key)):
<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
<!-- prettier-ignore-start -->
<!-- markdownlint-disable -->
<table>
<tr>
<td align="center"><a href="https://favware.tech/"><img src="https://avatars3.githubusercontent.com/u/4019718?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Jeroen Claassens</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=Favna" title="Code">💻</a> <a href="#infra-Favna" title="Infrastructure (Hosting, Build-Tools, etc)">🚇</a> <a href="#projectManagement-Favna" title="Project Management">📆</a> <a href="https://github.com/sapphiredev/utilities/commits?author=Favna" title="Documentation">📖</a> <a href="https://github.com/sapphiredev/utilities/commits?author=Favna" title="Tests">⚠️</a></td>
<td align="center"><a href="https://github.com/kyranet"><img src="https://avatars0.githubusercontent.com/u/24852502?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Antonio Román</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=kyranet" title="Code">💻</a> <a href="#projectManagement-kyranet" title="Project Management">📆</a> <a href="https://github.com/sapphiredev/utilities/pulls?q=is%3Apr+reviewed-by%3Akyranet" title="Reviewed Pull Requests">👀</a> <a href="https://github.com/sapphiredev/utilities/commits?author=kyranet" title="Tests">⚠️</a></td>
<td align="center"><a href="https://github.com/PyroTechniac"><img src="https://avatars2.githubusercontent.com/u/39341355?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Gryffon Bellish</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=PyroTechniac" title="Code">💻</a> <a href="https://github.com/sapphiredev/utilities/pulls?q=is%3Apr+reviewed-by%3APyroTechniac" title="Reviewed Pull Requests">👀</a> <a href="https://github.com/sapphiredev/utilities/commits?author=PyroTechniac" title="Tests">⚠️</a></td>
<td align="center"><a href="https://github.com/vladfrangu"><img src="https://avatars3.githubusercontent.com/u/17960496?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Vlad Frangu</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=vladfrangu" title="Code">💻</a> <a href="https://github.com/sapphiredev/utilities/issues?q=author%3Avladfrangu" title="Bug reports">🐛</a> <a href="https://github.com/sapphiredev/utilities/pulls?q=is%3Apr+reviewed-by%3Avladfrangu" title="Reviewed Pull Requests">👀</a> <a href="#userTesting-vladfrangu" title="User Testing">📓</a> <a href="https://github.com/sapphiredev/utilities/commits?author=vladfrangu" title="Tests">⚠️</a></td>
<td align="center"><a href="https://github.com/Stitch07"><img src="https://avatars0.githubusercontent.com/u/29275227?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Stitch07</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=Stitch07" title="Code">💻</a> <a href="#projectManagement-Stitch07" title="Project Management">📆</a> <a href="https://github.com/sapphiredev/utilities/commits?author=Stitch07" title="Tests">⚠️</a></td>
<td align="center"><a href="https://github.com/apps/depfu"><img src="https://avatars3.githubusercontent.com/in/715?v=4?s=100" width="100px;" alt=""/><br /><sub><b>depfu[bot]</b></sub></a><br /><a href="#maintenance-depfu[bot]" title="Maintenance">🚧</a></td>
<td align="center"><a href="https://github.com/apps/allcontributors"><img src="https://avatars0.githubusercontent.com/in/23186?v=4?s=100" width="100px;" alt=""/><br /><sub><b>allcontributors[bot]</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=allcontributors[bot]" title="Documentation">📖</a></td>
</tr>
<tr>
<td align="center"><a href="https://github.com/Nytelife26"><img src="https://avatars1.githubusercontent.com/u/22531310?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Tyler J Russell</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=Nytelife26" title="Documentation">📖</a></td>
<td align="center"><a href="https://github.com/Alcremie"><img src="https://avatars0.githubusercontent.com/u/54785334?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Ivan Lieder</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=Alcremie" title="Code">💻</a> <a href="https://github.com/sapphiredev/utilities/issues?q=author%3AAlcremie" title="Bug reports">🐛</a></td>
<td align="center"><a href="https://github.com/RealShadowNova"><img src="https://avatars3.githubusercontent.com/u/46537907?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Hezekiah Hendry</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=RealShadowNova" title="Code">💻</a></td>
<td align="center"><a href="https://github.com/Stitch07"><img src="https://avatars.githubusercontent.com/u/29275227?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Stitch07</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=Stitch07" title="Code">💻</a> <a href="https://github.com/sapphiredev/utilities/commits?author=Stitch07" title="Tests">⚠️</a></td>
<td align="center"><a href="https://github.com/Vetlix"><img src="https://avatars.githubusercontent.com/u/31412314?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Vetlix</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=Vetlix" title="Code">💻</a></td>
<td align="center"><a href="https://github.com/ethamitc"><img src="https://avatars.githubusercontent.com/u/27776796?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Ethan Mitchell</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=ethamitc" title="Documentation">📖</a></td>
<td align="center"><a href="https://github.com/noftaly"><img src="https://avatars.githubusercontent.com/u/34779161?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Elliot</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=noftaly" title="Code">💻</a></td>
</tr>
<tr>
<td align="center"><a href="https://jurien.dev"><img src="https://avatars.githubusercontent.com/u/5418114?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Jurien Hamaker</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=jurienhamaker" title="Code">💻</a></td>
<td align="center"><a href="https://fanoulis.dev/"><img src="https://avatars.githubusercontent.com/u/38255093?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Charalampos Fanoulis</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=cfanoulis" title="Documentation">📖</a></td>
<td align="center"><a href="https://github.com/apps/dependabot"><img src="https://avatars.githubusercontent.com/in/29110?v=4?s=100" width="100px;" alt=""/><br /><sub><b>dependabot[bot]</b></sub></a><br /><a href="#maintenance-dependabot[bot]" title="Maintenance">🚧</a></td>
<td align="center"><a href="https://kaname.netlify.app/"><img src="https://avatars.githubusercontent.com/u/56084970?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Kaname</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=kaname-png" title="Code">💻</a></td>
<td align="center"><a href="https://github.com/nandhagk"><img src="https://avatars.githubusercontent.com/u/62976649?v=4?s=100" width="100px;" alt=""/><br /><sub><b>nandhagk</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/issues?q=author%3Anandhagk" title="Bug reports">🐛</a></td>
</tr>
</table>
<!-- markdownlint-restore -->
<!-- prettier-ignore-end -->
<!-- ALL-CONTRIBUTORS-LIST:END -->
This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome!
+4
View File
@@ -0,0 +1,4 @@
export * from './lib/DiscordSnowflake';
export * from './lib/Snowflake';
export * from './lib/TwitterSnowflake';
//# sourceMappingURL=index.d.ts.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,wBAAwB,CAAC;AACvC,cAAc,iBAAiB,CAAC;AAChC,cAAc,wBAAwB,CAAC"}
+250
View File
@@ -0,0 +1,250 @@
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
/*! *****************************************************************************
Copyright (c) Microsoft Corporation.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
***************************************************************************** */
function __classPrivateFieldGet(receiver, privateMap) {
if (!privateMap.has(receiver)) {
throw new TypeError("attempted to get private field on non-instance");
}
return privateMap.get(receiver);
}
function __classPrivateFieldSet(receiver, privateMap, value) {
if (!privateMap.has(receiver)) {
throw new TypeError("attempted to set private field on non-instance");
}
privateMap.set(receiver, value);
return value;
}
/* eslint-disable @typescript-eslint/explicit-member-accessibility */
var _Snowflake_increment, _Snowflake_epoch;
/**
* A class for parsing snowflake ids
*/
class Snowflake {
/**
* @param epoch the epoch to use
*/
constructor(epoch) {
/**
* Internal incrementor for generating snowflakes
* @internal
*/
_Snowflake_increment.set(this, 0n);
/**
* Internal reference of the epoch passed in the constructor
* @internal
*/
_Snowflake_epoch.set(this, void 0);
/**
* Alias for {@link deconstruct}
*/
// eslint-disable-next-line @typescript-eslint/unbound-method, @typescript-eslint/no-invalid-this
Object.defineProperty(this, "decode", {
enumerable: true,
configurable: true,
writable: true,
value: this.deconstruct
});
__classPrivateFieldSet(this, _Snowflake_epoch, BigInt(epoch instanceof Date ? epoch.getTime() : epoch));
}
/**
* Generates a snowflake given an epoch and optionally a timestamp
* @param options options to pass into the generator, see {@link SnowflakeGenerateOptions}
*
* **note** when increment is not provided it defaults to the private increment of the instance
* @example
* ```ts
* const epoch = new Date('2000-01-01T00:00:00.000Z');
* const snowflake = new Snowflake(epoch).generate();
* ```
* @returns A unique snowflake
*/
generate({ increment = __classPrivateFieldGet(this, _Snowflake_increment), timestamp = Date.now(), workerID = 1n, processID = 1n } = {
increment: __classPrivateFieldGet(this, _Snowflake_increment),
timestamp: Date.now(),
workerID: 1n,
processID: 1n
}) {
if (timestamp instanceof Date)
timestamp = BigInt(timestamp.getTime());
if (typeof timestamp === 'number' && !Number.isNaN(timestamp))
timestamp = BigInt(timestamp);
if (typeof timestamp !== 'bigint') {
throw new TypeError(`"timestamp" argument must be a number, BigInt or Date (received ${Number.isNaN(timestamp) ? 'NaN' : typeof timestamp})`);
}
if (increment >= 4095n)
increment = 0n;
// timestamp, workerID, processID, increment
return ((timestamp - __classPrivateFieldGet(this, _Snowflake_epoch)) << 22n) | (workerID << 17n) | (processID << 12n) | increment++;
}
/**
* Deconstructs a snowflake given a snowflake ID
* @param id the snowflake to deconstruct
* @returns a deconstructed snowflake
* @example
* ```ts
* const epoch = new Date('2000-01-01T00:00:00.000Z');
* const snowflake = new Snowflake(epoch).deconstruct('3971046231244935168');
* ```
*/
deconstruct(id) {
const bigIntId = BigInt(id);
return {
id: bigIntId,
timestamp: (bigIntId >> 22n) + __classPrivateFieldGet(this, _Snowflake_epoch),
workerID: (bigIntId >> 17n) & 31n,
processID: (bigIntId >> 12n) & 31n,
increment: bigIntId & 4095n,
epoch: __classPrivateFieldGet(this, _Snowflake_epoch)
};
}
}
_Snowflake_increment = new WeakMap(), _Snowflake_epoch = new WeakMap();
/**
* A class for parsing snowflake ids using Discord's snowflake epoch
*
* Which is 2015-01-01 at 00:00:00.000 UTC+0, {@linkplain https://discord.com/developers/docs/reference#snowflakes}
*/
class DiscordSnowflake extends Snowflake {
constructor() {
super(DiscordSnowflake.Epoch);
}
/**
* Deconstructs a snowflake given a snowflake ID
* @param id the snowflake to deconstruct
* @returns a deconstructed snowflake
* @example
* ```ts
* const snowflake = DiscordSnowflake.deconstruct('3971046231244935168');
* ```
*/
static deconstruct(id) {
return new DiscordSnowflake().deconstruct(id);
}
/**
* Generates a snowflake given an epoch and optionally a timestamp
* @param options {@link SnowflakeGenerateOptions} to pass into the generator
*
* **note** when increment is not provided it defaults to `0n`
* @example
* ```ts
* const snowflake = DiscordSnowflake.generate();
* ```
* @returns A unique snowflake
*/
static generate(options = { timestamp: Date.now() }) {
return new DiscordSnowflake().generate(options);
}
}
/**
* Discord epoch (`2015-01-01T00:00:00.000Z`)
* @see {@linkplain https://discord.com/developers/docs/reference#snowflakes}
*/
Object.defineProperty(DiscordSnowflake, "Epoch", {
enumerable: true,
configurable: true,
writable: true,
value: 1420070400000n
});
/**
* Deconstructs a snowflake given a snowflake ID
* @param id the snowflake to deconstruct
* @returns a deconstructed snowflake
* @example
* ```ts
* const snowflake = DiscordSnowflake.decode('3971046231244935168');
* ```
*/
// eslint-disable-next-line @typescript-eslint/unbound-method
Object.defineProperty(DiscordSnowflake, "decode", {
enumerable: true,
configurable: true,
writable: true,
value: DiscordSnowflake.deconstruct
});
/**
* A class for parsing snowflake ids using Twitter's snowflake epoch
*
* Which is 2006-03-21 at 20:50:14.000 UTC+0, the time and date of the first tweet ever made {@linkplain https://twitter.com/jack/status/20}
*/
class TwitterSnowflake extends Snowflake {
constructor() {
super(TwitterSnowflake.Epoch);
}
/**
* Deconstructs a snowflake given a snowflake ID
* @param id the snowflake to deconstruct
* @returns a deconstructed snowflake
* @example
* ```ts
* const snowflake = TwitterSnowflake.deconstruct('3971046231244935168');
* ```
*/
static deconstruct(id) {
return new TwitterSnowflake().deconstruct(id);
}
/**
* Generates a snowflake given an epoch and optionally a timestamp
* @param options options to pass into the generator, see {@link SnowflakeGenerateOptions}
*
* **note** when increment is not provided it defaults to `0n`
* @example
* ```ts
* const snowflake = TwitterSnowflake.generate();
* ```
* @returns A unique snowflake
*/
static generate(options = { timestamp: Date.now() }) {
return new TwitterSnowflake().generate(options);
}
}
/**
* Twitter epoch (`2006-03-21T20:50:14.000Z`)
* @see {@linkplain https://twitter.com/jack/status/20}, first tweet ever made
*/
Object.defineProperty(TwitterSnowflake, "Epoch", {
enumerable: true,
configurable: true,
writable: true,
value: 1142974214000n
});
/**
* Deconstructs a snowflake given a snowflake ID
* @param id the snowflake to deconstruct
* @returns a deconstructed snowflake
* @example
* ```ts
* const snowflake = TwitterSnowflake.decode('3971046231244935168');
* ```
*/
// eslint-disable-next-line @typescript-eslint/unbound-method
Object.defineProperty(TwitterSnowflake, "decode", {
enumerable: true,
configurable: true,
writable: true,
value: TwitterSnowflake.deconstruct
});
exports.DiscordSnowflake = DiscordSnowflake;
exports.Snowflake = Snowflake;
exports.TwitterSnowflake = TwitterSnowflake;
//# sourceMappingURL=index.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"index.js","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 { DeconstructedSnowflake, Snowflake, SnowflakeGenerateOptions } from './Snowflake';\n\n/**\n * A class for parsing snowflake ids using Discord's snowflake epoch\n *\n * Which is 2015-01-01 at 00:00:00.000 UTC+0, {@linkplain https://discord.com/developers/docs/reference#snowflakes}\n */\nexport class DiscordSnowflake extends Snowflake {\n\tpublic constructor() {\n\t\tsuper(DiscordSnowflake.Epoch);\n\t}\n\n\t/**\n\t * Discord epoch (`2015-01-01T00:00:00.000Z`)\n\t * @see {@linkplain https://discord.com/developers/docs/reference#snowflakes}\n\t */\n\tpublic static readonly Epoch = 1420070400000n;\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 snowflake = DiscordSnowflake.decode('3971046231244935168');\n\t * ```\n\t */\n\t// eslint-disable-next-line @typescript-eslint/unbound-meLine truncated
+244
View File
@@ -0,0 +1,244 @@
/*! *****************************************************************************
Copyright (c) Microsoft Corporation.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
***************************************************************************** */
function __classPrivateFieldGet(receiver, privateMap) {
if (!privateMap.has(receiver)) {
throw new TypeError("attempted to get private field on non-instance");
}
return privateMap.get(receiver);
}
function __classPrivateFieldSet(receiver, privateMap, value) {
if (!privateMap.has(receiver)) {
throw new TypeError("attempted to set private field on non-instance");
}
privateMap.set(receiver, value);
return value;
}
/* eslint-disable @typescript-eslint/explicit-member-accessibility */
var _Snowflake_increment, _Snowflake_epoch;
/**
* A class for parsing snowflake ids
*/
class Snowflake {
/**
* @param epoch the epoch to use
*/
constructor(epoch) {
/**
* Internal incrementor for generating snowflakes
* @internal
*/
_Snowflake_increment.set(this, 0n);
/**
* Internal reference of the epoch passed in the constructor
* @internal
*/
_Snowflake_epoch.set(this, void 0);
/**
* Alias for {@link deconstruct}
*/
// eslint-disable-next-line @typescript-eslint/unbound-method, @typescript-eslint/no-invalid-this
Object.defineProperty(this, "decode", {
enumerable: true,
configurable: true,
writable: true,
value: this.deconstruct
});
__classPrivateFieldSet(this, _Snowflake_epoch, BigInt(epoch instanceof Date ? epoch.getTime() : epoch));
}
/**
* Generates a snowflake given an epoch and optionally a timestamp
* @param options options to pass into the generator, see {@link SnowflakeGenerateOptions}
*
* **note** when increment is not provided it defaults to the private increment of the instance
* @example
* ```ts
* const epoch = new Date('2000-01-01T00:00:00.000Z');
* const snowflake = new Snowflake(epoch).generate();
* ```
* @returns A unique snowflake
*/
generate({ increment = __classPrivateFieldGet(this, _Snowflake_increment), timestamp = Date.now(), workerID = 1n, processID = 1n } = {
increment: __classPrivateFieldGet(this, _Snowflake_increment),
timestamp: Date.now(),
workerID: 1n,
processID: 1n
}) {
if (timestamp instanceof Date)
timestamp = BigInt(timestamp.getTime());
if (typeof timestamp === 'number' && !Number.isNaN(timestamp))
timestamp = BigInt(timestamp);
if (typeof timestamp !== 'bigint') {
throw new TypeError(`"timestamp" argument must be a number, BigInt or Date (received ${Number.isNaN(timestamp) ? 'NaN' : typeof timestamp})`);
}
if (increment >= 4095n)
increment = 0n;
// timestamp, workerID, processID, increment
return ((timestamp - __classPrivateFieldGet(this, _Snowflake_epoch)) << 22n) | (workerID << 17n) | (processID << 12n) | increment++;
}
/**
* Deconstructs a snowflake given a snowflake ID
* @param id the snowflake to deconstruct
* @returns a deconstructed snowflake
* @example
* ```ts
* const epoch = new Date('2000-01-01T00:00:00.000Z');
* const snowflake = new Snowflake(epoch).deconstruct('3971046231244935168');
* ```
*/
deconstruct(id) {
const bigIntId = BigInt(id);
return {
id: bigIntId,
timestamp: (bigIntId >> 22n) + __classPrivateFieldGet(this, _Snowflake_epoch),
workerID: (bigIntId >> 17n) & 31n,
processID: (bigIntId >> 12n) & 31n,
increment: bigIntId & 4095n,
epoch: __classPrivateFieldGet(this, _Snowflake_epoch)
};
}
}
_Snowflake_increment = new WeakMap(), _Snowflake_epoch = new WeakMap();
/**
* A class for parsing snowflake ids using Discord's snowflake epoch
*
* Which is 2015-01-01 at 00:00:00.000 UTC+0, {@linkplain https://discord.com/developers/docs/reference#snowflakes}
*/
class DiscordSnowflake extends Snowflake {
constructor() {
super(DiscordSnowflake.Epoch);
}
/**
* Deconstructs a snowflake given a snowflake ID
* @param id the snowflake to deconstruct
* @returns a deconstructed snowflake
* @example
* ```ts
* const snowflake = DiscordSnowflake.deconstruct('3971046231244935168');
* ```
*/
static deconstruct(id) {
return new DiscordSnowflake().deconstruct(id);
}
/**
* Generates a snowflake given an epoch and optionally a timestamp
* @param options {@link SnowflakeGenerateOptions} to pass into the generator
*
* **note** when increment is not provided it defaults to `0n`
* @example
* ```ts
* const snowflake = DiscordSnowflake.generate();
* ```
* @returns A unique snowflake
*/
static generate(options = { timestamp: Date.now() }) {
return new DiscordSnowflake().generate(options);
}
}
/**
* Discord epoch (`2015-01-01T00:00:00.000Z`)
* @see {@linkplain https://discord.com/developers/docs/reference#snowflakes}
*/
Object.defineProperty(DiscordSnowflake, "Epoch", {
enumerable: true,
configurable: true,
writable: true,
value: 1420070400000n
});
/**
* Deconstructs a snowflake given a snowflake ID
* @param id the snowflake to deconstruct
* @returns a deconstructed snowflake
* @example
* ```ts
* const snowflake = DiscordSnowflake.decode('3971046231244935168');
* ```
*/
// eslint-disable-next-line @typescript-eslint/unbound-method
Object.defineProperty(DiscordSnowflake, "decode", {
enumerable: true,
configurable: true,
writable: true,
value: DiscordSnowflake.deconstruct
});
/**
* A class for parsing snowflake ids using Twitter's snowflake epoch
*
* Which is 2006-03-21 at 20:50:14.000 UTC+0, the time and date of the first tweet ever made {@linkplain https://twitter.com/jack/status/20}
*/
class TwitterSnowflake extends Snowflake {
constructor() {
super(TwitterSnowflake.Epoch);
}
/**
* Deconstructs a snowflake given a snowflake ID
* @param id the snowflake to deconstruct
* @returns a deconstructed snowflake
* @example
* ```ts
* const snowflake = TwitterSnowflake.deconstruct('3971046231244935168');
* ```
*/
static deconstruct(id) {
return new TwitterSnowflake().deconstruct(id);
}
/**
* Generates a snowflake given an epoch and optionally a timestamp
* @param options options to pass into the generator, see {@link SnowflakeGenerateOptions}
*
* **note** when increment is not provided it defaults to `0n`
* @example
* ```ts
* const snowflake = TwitterSnowflake.generate();
* ```
* @returns A unique snowflake
*/
static generate(options = { timestamp: Date.now() }) {
return new TwitterSnowflake().generate(options);
}
}
/**
* Twitter epoch (`2006-03-21T20:50:14.000Z`)
* @see {@linkplain https://twitter.com/jack/status/20}, first tweet ever made
*/
Object.defineProperty(TwitterSnowflake, "Epoch", {
enumerable: true,
configurable: true,
writable: true,
value: 1142974214000n
});
/**
* Deconstructs a snowflake given a snowflake ID
* @param id the snowflake to deconstruct
* @returns a deconstructed snowflake
* @example
* ```ts
* const snowflake = TwitterSnowflake.decode('3971046231244935168');
* ```
*/
// eslint-disable-next-line @typescript-eslint/unbound-method
Object.defineProperty(TwitterSnowflake, "decode", {
enumerable: true,
configurable: true,
writable: true,
value: TwitterSnowflake.deconstruct
});
export { DiscordSnowflake, Snowflake, TwitterSnowflake };
//# sourceMappingURL=index.mjs.map
+1
View File
@@ -0,0 +1 @@
{"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 { DeconstructedSnowflake, Snowflake, SnowflakeGenerateOptions } from './Snowflake';\n\n/**\n * A class for parsing snowflake ids using Discord's snowflake epoch\n *\n * Which is 2015-01-01 at 00:00:00.000 UTC+0, {@linkplain https://discord.com/developers/docs/reference#snowflakes}\n */\nexport class DiscordSnowflake extends Snowflake {\n\tpublic constructor() {\n\t\tsuper(DiscordSnowflake.Epoch);\n\t}\n\n\t/**\n\t * Discord epoch (`2015-01-01T00:00:00.000Z`)\n\t * @see {@linkplain https://discord.com/developers/docs/reference#snowflakes}\n\t */\n\tpublic static readonly Epoch = 1420070400000n;\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 snowflake = DiscordSnowflake.decode('3971046231244935168');\n\t * ```\n\t */\n\t// eslint-disable-next-line @typescript-eslint/unbound-mLine truncated
+256
View File
@@ -0,0 +1,256 @@
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
typeof define === 'function' && define.amd ? define(['exports'], factory) :
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.SapphireSnowflake = {}));
}(this, (function (exports) { 'use strict';
/*! *****************************************************************************
Copyright (c) Microsoft Corporation.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
***************************************************************************** */
function __classPrivateFieldGet(receiver, privateMap) {
if (!privateMap.has(receiver)) {
throw new TypeError("attempted to get private field on non-instance");
}
return privateMap.get(receiver);
}
function __classPrivateFieldSet(receiver, privateMap, value) {
if (!privateMap.has(receiver)) {
throw new TypeError("attempted to set private field on non-instance");
}
privateMap.set(receiver, value);
return value;
}
/* eslint-disable @typescript-eslint/explicit-member-accessibility */
var _Snowflake_increment, _Snowflake_epoch;
/**
* A class for parsing snowflake ids
*/
class Snowflake {
/**
* @param epoch the epoch to use
*/
constructor(epoch) {
/**
* Internal incrementor for generating snowflakes
* @internal
*/
_Snowflake_increment.set(this, 0n);
/**
* Internal reference of the epoch passed in the constructor
* @internal
*/
_Snowflake_epoch.set(this, void 0);
/**
* Alias for {@link deconstruct}
*/
// eslint-disable-next-line @typescript-eslint/unbound-method, @typescript-eslint/no-invalid-this
Object.defineProperty(this, "decode", {
enumerable: true,
configurable: true,
writable: true,
value: this.deconstruct
});
__classPrivateFieldSet(this, _Snowflake_epoch, BigInt(epoch instanceof Date ? epoch.getTime() : epoch));
}
/**
* Generates a snowflake given an epoch and optionally a timestamp
* @param options options to pass into the generator, see {@link SnowflakeGenerateOptions}
*
* **note** when increment is not provided it defaults to the private increment of the instance
* @example
* ```ts
* const epoch = new Date('2000-01-01T00:00:00.000Z');
* const snowflake = new Snowflake(epoch).generate();
* ```
* @returns A unique snowflake
*/
generate({ increment = __classPrivateFieldGet(this, _Snowflake_increment), timestamp = Date.now(), workerID = 1n, processID = 1n } = {
increment: __classPrivateFieldGet(this, _Snowflake_increment),
timestamp: Date.now(),
workerID: 1n,
processID: 1n
}) {
if (timestamp instanceof Date)
timestamp = BigInt(timestamp.getTime());
if (typeof timestamp === 'number' && !Number.isNaN(timestamp))
timestamp = BigInt(timestamp);
if (typeof timestamp !== 'bigint') {
throw new TypeError(`"timestamp" argument must be a number, BigInt or Date (received ${Number.isNaN(timestamp) ? 'NaN' : typeof timestamp})`);
}
if (increment >= 4095n)
increment = 0n;
// timestamp, workerID, processID, increment
return ((timestamp - __classPrivateFieldGet(this, _Snowflake_epoch)) << 22n) | (workerID << 17n) | (processID << 12n) | increment++;
}
/**
* Deconstructs a snowflake given a snowflake ID
* @param id the snowflake to deconstruct
* @returns a deconstructed snowflake
* @example
* ```ts
* const epoch = new Date('2000-01-01T00:00:00.000Z');
* const snowflake = new Snowflake(epoch).deconstruct('3971046231244935168');
* ```
*/
deconstruct(id) {
const bigIntId = BigInt(id);
return {
id: bigIntId,
timestamp: (bigIntId >> 22n) + __classPrivateFieldGet(this, _Snowflake_epoch),
workerID: (bigIntId >> 17n) & 31n,
processID: (bigIntId >> 12n) & 31n,
increment: bigIntId & 4095n,
epoch: __classPrivateFieldGet(this, _Snowflake_epoch)
};
}
}
_Snowflake_increment = new WeakMap(), _Snowflake_epoch = new WeakMap();
/**
* A class for parsing snowflake ids using Discord's snowflake epoch
*
* Which is 2015-01-01 at 00:00:00.000 UTC+0, {@linkplain https://discord.com/developers/docs/reference#snowflakes}
*/
class DiscordSnowflake extends Snowflake {
constructor() {
super(DiscordSnowflake.Epoch);
}
/**
* Deconstructs a snowflake given a snowflake ID
* @param id the snowflake to deconstruct
* @returns a deconstructed snowflake
* @example
* ```ts
* const snowflake = DiscordSnowflake.deconstruct('3971046231244935168');
* ```
*/
static deconstruct(id) {
return new DiscordSnowflake().deconstruct(id);
}
/**
* Generates a snowflake given an epoch and optionally a timestamp
* @param options {@link SnowflakeGenerateOptions} to pass into the generator
*
* **note** when increment is not provided it defaults to `0n`
* @example
* ```ts
* const snowflake = DiscordSnowflake.generate();
* ```
* @returns A unique snowflake
*/
static generate(options = { timestamp: Date.now() }) {
return new DiscordSnowflake().generate(options);
}
}
/**
* Discord epoch (`2015-01-01T00:00:00.000Z`)
* @see {@linkplain https://discord.com/developers/docs/reference#snowflakes}
*/
Object.defineProperty(DiscordSnowflake, "Epoch", {
enumerable: true,
configurable: true,
writable: true,
value: 1420070400000n
});
/**
* Deconstructs a snowflake given a snowflake ID
* @param id the snowflake to deconstruct
* @returns a deconstructed snowflake
* @example
* ```ts
* const snowflake = DiscordSnowflake.decode('3971046231244935168');
* ```
*/
// eslint-disable-next-line @typescript-eslint/unbound-method
Object.defineProperty(DiscordSnowflake, "decode", {
enumerable: true,
configurable: true,
writable: true,
value: DiscordSnowflake.deconstruct
});
/**
* A class for parsing snowflake ids using Twitter's snowflake epoch
*
* Which is 2006-03-21 at 20:50:14.000 UTC+0, the time and date of the first tweet ever made {@linkplain https://twitter.com/jack/status/20}
*/
class TwitterSnowflake extends Snowflake {
constructor() {
super(TwitterSnowflake.Epoch);
}
/**
* Deconstructs a snowflake given a snowflake ID
* @param id the snowflake to deconstruct
* @returns a deconstructed snowflake
* @example
* ```ts
* const snowflake = TwitterSnowflake.deconstruct('3971046231244935168');
* ```
*/
static deconstruct(id) {
return new TwitterSnowflake().deconstruct(id);
}
/**
* Generates a snowflake given an epoch and optionally a timestamp
* @param options options to pass into the generator, see {@link SnowflakeGenerateOptions}
*
* **note** when increment is not provided it defaults to `0n`
* @example
* ```ts
* const snowflake = TwitterSnowflake.generate();
* ```
* @returns A unique snowflake
*/
static generate(options = { timestamp: Date.now() }) {
return new TwitterSnowflake().generate(options);
}
}
/**
* Twitter epoch (`2006-03-21T20:50:14.000Z`)
* @see {@linkplain https://twitter.com/jack/status/20}, first tweet ever made
*/
Object.defineProperty(TwitterSnowflake, "Epoch", {
enumerable: true,
configurable: true,
writable: true,
value: 1142974214000n
});
/**
* Deconstructs a snowflake given a snowflake ID
* @param id the snowflake to deconstruct
* @returns a deconstructed snowflake
* @example
* ```ts
* const snowflake = TwitterSnowflake.decode('3971046231244935168');
* ```
*/
// eslint-disable-next-line @typescript-eslint/unbound-method
Object.defineProperty(TwitterSnowflake, "decode", {
enumerable: true,
configurable: true,
writable: true,
value: TwitterSnowflake.deconstruct
});
exports.DiscordSnowflake = DiscordSnowflake;
exports.Snowflake = Snowflake;
exports.TwitterSnowflake = TwitterSnowflake;
Object.defineProperty(exports, '__esModule', { value: true });
})));
//# sourceMappingURL=index.umd.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"index.umd.js","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 { DeconstructedSnowflake, Snowflake, SnowflakeGenerateOptions } from './Snowflake';\n\n/**\n * A class for parsing snowflake ids using Discord's snowflake epoch\n *\n * Which is 2015-01-01 at 00:00:00.000 UTC+0, {@linkplain https://discord.com/developers/docs/reference#snowflakes}\n */\nexport class DiscordSnowflake extends Snowflake {\n\tpublic constructor() {\n\t\tsuper(DiscordSnowflake.Epoch);\n\t}\n\n\t/**\n\t * Discord epoch (`2015-01-01T00:00:00.000Z`)\n\t * @see {@linkplain https://discord.com/developers/docs/reference#snowflakes}\n\t */\n\tpublic static readonly Epoch = 1420070400000n;\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 snowflake = DiscordSnowflake.decode('3971046231244935168');\n\t * ```\n\t */\n\t// eslint-disable-next-line @typescript-eslint/unbounLine truncated
+47
View File
@@ -0,0 +1,47 @@
import { DeconstructedSnowflake, Snowflake, SnowflakeGenerateOptions } from './Snowflake';
/**
* A class for parsing snowflake ids using Discord's snowflake epoch
*
* Which is 2015-01-01 at 00:00:00.000 UTC+0, {@linkplain https://discord.com/developers/docs/reference#snowflakes}
*/
export declare class DiscordSnowflake extends Snowflake {
constructor();
/**
* Discord epoch (`2015-01-01T00:00:00.000Z`)
* @see {@linkplain https://discord.com/developers/docs/reference#snowflakes}
*/
static readonly Epoch = 1420070400000n;
/**
* Deconstructs a snowflake given a snowflake ID
* @param id the snowflake to deconstruct
* @returns a deconstructed snowflake
* @example
* ```ts
* const snowflake = DiscordSnowflake.decode('3971046231244935168');
* ```
*/
static decode: typeof DiscordSnowflake.deconstruct;
/**
* Deconstructs a snowflake given a snowflake ID
* @param id the snowflake to deconstruct
* @returns a deconstructed snowflake
* @example
* ```ts
* const snowflake = DiscordSnowflake.deconstruct('3971046231244935168');
* ```
*/
static deconstruct(id: string | bigint): DeconstructedSnowflake;
/**
* Generates a snowflake given an epoch and optionally a timestamp
* @param options {@link SnowflakeGenerateOptions} to pass into the generator
*
* **note** when increment is not provided it defaults to `0n`
* @example
* ```ts
* const snowflake = DiscordSnowflake.generate();
* ```
* @returns A unique snowflake
*/
static generate(options?: SnowflakeGenerateOptions): bigint;
}
//# sourceMappingURL=DiscordSnowflake.d.ts.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"DiscordSnowflake.d.ts","sourceRoot":"","sources":["../../src/lib/DiscordSnowflake.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,sBAAsB,EAAE,SAAS,EAAE,wBAAwB,EAAE,MAAM,aAAa,CAAC;AAE1F;;;;GAIG;AACH,qBAAa,gBAAiB,SAAQ,SAAS;;IAK9C;;;OAGG;IACH,gBAAuB,KAAK,kBAAkB;IAE9C;;;;;;;;OAQG;IAEH,OAAc,MAAM,sCAAgC;IAEpD;;;;;;;;OAQG;WACW,WAAW,CAAC,EAAE,EAAE,MAAM,GAAG,MAAM,GAAG,sBAAsB;IAItE;;;;;;;;;;OAUG;WACW,QAAQ,CAAC,OAAO,GAAE,wBAAoD;CAGpF"}
+94
View File
@@ -0,0 +1,94 @@
/**
* A class for parsing snowflake ids
*/
export declare class Snowflake {
#private;
/**
* Alias for {@link deconstruct}
*/
decode: (id: string | bigint) => DeconstructedSnowflake;
/**
* @param epoch the epoch to use
*/
constructor(epoch: number | bigint | Date);
/**
* Generates a snowflake given an epoch and optionally a timestamp
* @param options options to pass into the generator, see {@link SnowflakeGenerateOptions}
*
* **note** when increment is not provided it defaults to the private increment of the instance
* @example
* ```ts
* const epoch = new Date('2000-01-01T00:00:00.000Z');
* const snowflake = new Snowflake(epoch).generate();
* ```
* @returns A unique snowflake
*/
generate({ increment, timestamp, workerID, processID }?: SnowflakeGenerateOptions): bigint;
/**
* Deconstructs a snowflake given a snowflake ID
* @param id the snowflake to deconstruct
* @returns a deconstructed snowflake
* @example
* ```ts
* const epoch = new Date('2000-01-01T00:00:00.000Z');
* const snowflake = new Snowflake(epoch).deconstruct('3971046231244935168');
* ```
*/
deconstruct(id: string | bigint): DeconstructedSnowflake;
}
/**
* Options for Snowflake#generate
*/
export interface SnowflakeGenerateOptions {
/**
* Timestamp or date of the snowflake to generate
* @default Date.now()
*/
timestamp?: number | bigint | Date;
/**
* The increment to use
* @default 0n
* @remark keep in mind that this bigint is auto-incremented between generate calls
*/
increment?: bigint;
/**
* The worker ID to use
* @default 1n
*/
workerID?: bigint;
/**
* The process ID to use
* @default 1n
*/
processID?: bigint;
}
/**
* Object returned by Snowflake#deconstruct
*/
export interface DeconstructedSnowflake {
/**
* The id in BigInt form
*/
id: bigint;
/**
* The timestamp stored in the snowflake
*/
timestamp: bigint;
/**
* The worker id stored in the snowflake
*/
workerID: bigint;
/**
* The process id stored in the snowflake
*/
processID: bigint;
/**
* The increment stored in the snowflake
*/
increment: bigint;
/**
* The epoch to use in the snowflake
*/
epoch: bigint;
}
//# sourceMappingURL=Snowflake.d.ts.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"Snowflake.d.ts","sourceRoot":"","sources":["../../src/lib/Snowflake.ts"],"names":[],"mappings":"AAEA;;GAEG;AACH,qBAAa,SAAS;;IAarB;;OAEG;IAEI,MAAM,OAsDU,MAAM,GAAG,MAAM,KAAG,sBAAsB,CAtD9B;IAEjC;;OAEG;gBACgB,KAAK,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI;IAIhD;;;;;;;;;;;OAWG;IACI,QAAQ,CACd,EAAE,SAA2B,EAAE,SAAsB,EAAE,QAAa,EAAE,SAAc,EAAE,GAAE,wBAKvF;IAiBF;;;;;;;;;OASG;IACI,WAAW,CAAC,EAAE,EAAE,MAAM,GAAG,MAAM,GAAG,sBAAsB;CAW/D;AAED;;GAEG;AACH,MAAM,WAAW,wBAAwB;IACxC;;;OAGG;IACH,SAAS,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAAC;IAEnC;;;;OAIG;IACH,SAAS,CAAC,EAAE,MAAM,CAAC;IAEnB;;;OAGG;IACH,QAAQ,CAAC,EAAE,MAAM,CAAC;IAElB;;;OAGG;IACH,SAAS,CAAC,EAAE,MAAM,CAAC;CACnB;AAED;;GAEG;AACH,MAAM,WAAW,sBAAsB;IACtC;;OAEG;IACH,EAAE,EAAE,MAAM,CAAC;IAEX;;OAEG;IACH,SAAS,EAAE,MAAM,CAAC;IAElB;;OAEG;IACH,QAAQ,EAAE,MAAM,CAAC;IAEjB;;OAEG;IACH,SAAS,EAAE,MAAM,CAAC;IAElB;;OAEG;IACH,SAAS,EAAE,MAAM,CAAC;IAElB;;OAEG;IACH,KAAK,EAAE,MAAM,CAAC;CACd"}
+47
View File
@@ -0,0 +1,47 @@
import { DeconstructedSnowflake, Snowflake, SnowflakeGenerateOptions } from './Snowflake';
/**
* A class for parsing snowflake ids using Twitter's snowflake epoch
*
* Which is 2006-03-21 at 20:50:14.000 UTC+0, the time and date of the first tweet ever made {@linkplain https://twitter.com/jack/status/20}
*/
export declare class TwitterSnowflake extends Snowflake {
constructor();
/**
* Twitter epoch (`2006-03-21T20:50:14.000Z`)
* @see {@linkplain https://twitter.com/jack/status/20}, first tweet ever made
*/
static readonly Epoch = 1142974214000n;
/**
* Deconstructs a snowflake given a snowflake ID
* @param id the snowflake to deconstruct
* @returns a deconstructed snowflake
* @example
* ```ts
* const snowflake = TwitterSnowflake.decode('3971046231244935168');
* ```
*/
static decode: typeof TwitterSnowflake.deconstruct;
/**
* Deconstructs a snowflake given a snowflake ID
* @param id the snowflake to deconstruct
* @returns a deconstructed snowflake
* @example
* ```ts
* const snowflake = TwitterSnowflake.deconstruct('3971046231244935168');
* ```
*/
static deconstruct(id: string | bigint): DeconstructedSnowflake;
/**
* Generates a snowflake given an epoch and optionally a timestamp
* @param options options to pass into the generator, see {@link SnowflakeGenerateOptions}
*
* **note** when increment is not provided it defaults to `0n`
* @example
* ```ts
* const snowflake = TwitterSnowflake.generate();
* ```
* @returns A unique snowflake
*/
static generate(options?: SnowflakeGenerateOptions): bigint;
}
//# sourceMappingURL=TwitterSnowflake.d.ts.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"TwitterSnowflake.d.ts","sourceRoot":"","sources":["../../src/lib/TwitterSnowflake.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,sBAAsB,EAAE,SAAS,EAAE,wBAAwB,EAAE,MAAM,aAAa,CAAC;AAE1F;;;;GAIG;AACH,qBAAa,gBAAiB,SAAQ,SAAS;;IAK9C;;;OAGG;IACH,gBAAuB,KAAK,kBAAkB;IAE9C;;;;;;;;OAQG;IAEH,OAAc,MAAM,sCAAgC;IAEpD;;;;;;;;OAQG;WACW,WAAW,CAAC,EAAE,EAAE,MAAM,GAAG,MAAM,GAAG,sBAAsB;IAItE;;;;;;;;;;OAUG;WACW,QAAQ,CAAC,OAAO,GAAE,wBAAoD;CAGpF"}
+55
View File
@@ -0,0 +1,55 @@
{
"name": "@sapphire/snowflake",
"version": "1.3.6",
"description": "Deconstructs and generates snoflake IDs using BigInts",
"author": "@sapphire",
"license": "MIT",
"main": "dist/index.js",
"module": "dist/index.mjs",
"browser": "dist/index.umd.js",
"unpkg": "dist/index.umd.js",
"types": "dist/index.d.ts",
"exports": {
"import": "./dist/index.mjs",
"require": "./dist/index.js"
},
"sideEffects": false,
"homepage": "https://github.com/sapphiredev/utilities/tree/main/packages/snowflake",
"scripts": {
"test": "jest",
"lint": "eslint src tests --ext ts --fix -c ../../.eslintrc",
"build": "rollup -c",
"start": "yarn build -w",
"prepublishOnly": "yarn build"
},
"repository": {
"type": "git",
"url": "git+https://github.com/sapphiredev/utilities.git",
"directory": "packages/snowflake"
},
"files": [
"dist",
"!dist/*.tsbuildinfo"
],
"engines": {
"node": ">=12",
"npm": ">=6"
},
"keywords": [
"@sapphire/snowflake",
"bot",
"typescript",
"ts",
"yarn",
"discord",
"sapphire",
"standalone"
],
"bugs": {
"url": "https://github.com/sapphiredev/utilities/issues"
},
"publishConfig": {
"access": "public"
},
"gitHead": "8b13ce9be946fe45ef40c84e47597d488c924921"
}