feat: edge mode(エントロピーモード)を追加 (#6)#7
Merged
Merged
Conversation
タイムスタンプ以外の 22bit を crypto.getRandomValues で埋める mode: 'edge' を追加。worker-id の割り当てが不要で、確率的に一意性を確保する。既存のデフォルトモードは不変(後方互換)。parseSnowflakeId は edge ID を entropy + worker 系 null で返す。テストと README も更新。 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@biomejs/biome と esbuild のビルドスクリプトを承認し、pnpm install を警告・プロンプトなしで通せるようにする。 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
parseSnowflakeId のシグネチャを lineWidth(100) に合わせて複数行に整形。CI の format チェックを通すための修正。 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
概要
issue #6 を実装。serverless / edge 環境(Cloudflare Workers 等)向けに、worker-id の割り当てが不要な edge mode(エントロピーモード) を追加する。
timestamp 以外の 22bit(datacenter 5 + worker 5 + sequence 12)を 暗号学的乱数
crypto.getRandomValuesで埋めることで、「timestamp + 22bit のランダム性」により確率的に一意性を確保する。Closes #6
変更点
src/index.tsSnowflakeMode = "default" | "edge"型とSnowflakeConfig.modeを追加mode: 'edge':ステートレスにエントロピーで ID を生成(datacenterId/workerIdは無視・バリデーションもスキップ)parseSnowflakeId(id, epoch, mode):datacenterId/workerId/sequenceをnull、entropy: numberを返すentropy: nullgetSnowflakeTimestamp/snowflakeToDate/generateSnowflakeIdは変更なしで自動対応src/index.test.tsREADME.mdmode/ parse 戻り値)、ID Format のビット図、「DB UNIQUE 制約 + 衝突リトライ推奨」 の注記、Cloudflare Workers 例を edge ベースに更新pnpm-workspace.yaml@biomejs/biome/esbuild)を追加ビット割り当て(edge mode)
一意性の前提
22bit = 4,194,304/ms。同一 ms 内で k 個生成時の衝突確率は約
k² / (2·2²²)(例: 100 IDs/ms で約 0.12%)。確率的モードのため、利用側は DB の UNIQUE 制約 + 衝突時リトライ をバックストップとして併用することを推奨(README に明記)。テスト計画
pnpm test(85 件 pass)pnpm test:coverage(全ファイル lines/branches/functions/statements 100%)pnpm typecheckpnpm lintpnpm build🤖 Generated with Claude Code