
Security News
/Research
Wallet-Draining npm Package Impersonates Nodemailer to Hijack Crypto Transactions
Malicious npm package impersonates Nodemailer and drains wallets by hijacking crypto transactions across multiple blockchains.
@sapphire/snowflake
Advanced tools
@sapphire/snowflake is a utility library for generating and parsing unique snowflake IDs, which are often used in distributed systems to ensure unique identifiers. It is particularly useful in applications that require high-scale, unique ID generation.
Generate Snowflake ID
This feature allows you to generate a unique snowflake ID. The generated ID is a 64-bit integer that is unique and can be used as an identifier in distributed systems.
const { Snowflake } = require('@sapphire/snowflake');
const snowflake = new Snowflake();
const id = snowflake.generate();
console.log(id);
Parse Snowflake ID
This feature allows you to parse a snowflake ID to extract its components, such as the timestamp, worker ID, and process ID. This can be useful for debugging or analyzing the IDs.
const { Snowflake } = require('@sapphire/snowflake');
const snowflake = new Snowflake();
const id = snowflake.generate();
const parsed = snowflake.deconstruct(id);
console.log(parsed);
Custom Epoch
This feature allows you to set a custom epoch for the snowflake ID generation. The epoch is the starting point for the timestamp component of the ID, and setting a custom epoch can be useful for aligning IDs with specific events or timeframes.
const { Snowflake } = require('@sapphire/snowflake');
const customEpoch = 1609459200000; // Custom epoch (e.g., January 1, 2021)
const snowflake = new Snowflake(customEpoch);
const id = snowflake.generate();
console.log(id);
The 'snowflake-id' package is another library for generating unique snowflake IDs. It offers similar functionality to @sapphire/snowflake, including ID generation and parsing. However, it may have different performance characteristics and configuration options.
The 'flake-idgen' package is a lightweight library for generating unique IDs using the snowflake algorithm. It is designed to be fast and efficient, making it a good alternative to @sapphire/snowflake for applications that require high-throughput ID generation.
The 'node-snowflake' package is a simple implementation of the snowflake ID generation algorithm. It provides basic functionality for generating and parsing snowflake IDs, similar to @sapphire/snowflake, but may lack some of the advanced features and customization options.
Table of Contents
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.
You can use the following command to install this package, or replace npm install
with your package manager of choice.
npm install @sapphire/snowflake
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'
.
// 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();
// Import the Snowflake class
const { DiscordSnowflake } = require('@sapphire/snowflake');
// Generate a snowflake with Discord's epoch
const uniqueId = DiscordSnowflake.generate();
// Alternatively, you can use the method directly
const uniqueId = DiscordSnowflake.generate();
// Import the Snowflake class
const { TwitterSnowflake } = require('@sapphire/snowflake');
// Generate a snowflake with Twitter's epoch
const uniqueId = TwitterSnowflake.generate();
// Alternatively, you can use the method directly
const uniqueId = TwitterSnowflake.generate();
// 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');
// Import the Snowflake class
const { DiscordSnowflake } = require('@sapphire/snowflake');
// Deconstruct a snowflake with Discord's epoch
const uniqueId = DiscordSnowflake.deconstruct('3971046231244935168');
// Alternatively, you can use the method directly
const uniqueId = DiscordSnowflake.deconstruct('3971046231244935168');
// Import the Snowflake class
const { TwitterSnowflake } = require('@sapphire/snowflake');
// Deconstruct a snowflake with Twitter's epoch
const uniqueId = TwitterSnowflake.deconstruct('3971046231244935168');
// Alternatively, you can use the method directly
const uniqueId = TwitterSnowflake.deconstruct('3971046231244935168');
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 |
Ko-fi | Click Here |
Patreon | Click Here |
PayPal | Click Here |
Please make sure to read the Contributing Guide before making a pull request.
Thank you to all the people who already contributed to Sapphire!
FAQs
Deconstructs and generates snowflake IDs using BigInts
The npm package @sapphire/snowflake receives a total of 188,377 weekly downloads. As such, @sapphire/snowflake popularity was classified as popular.
We found that @sapphire/snowflake demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 3 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
/Research
Malicious npm package impersonates Nodemailer and drains wallets by hijacking crypto transactions across multiple blockchains.
Security News
This episode explores the hard problem of reachability analysis, from static analysis limits to handling dynamic languages and massive dependency trees.
Security News
/Research
Malicious Nx npm versions stole secrets and wallet info using AI CLI tools; Socket’s AI scanner detected the supply chain attack and flagged the malware.