feat: ship Wave 5-6 multi-surface hygiene, optimization, and bug fixes
This commit is contained in:
parent
01ef3293cf
commit
66b6b45ba9
399 changed files with 17828 additions and 11330 deletions
80
packages/core/src/ports.ts
Normal file
80
packages/core/src/ports.ts
Normal file
|
|
@ -0,0 +1,80 @@
|
|||
// packages/core/src/ports.ts
|
||||
// 결정성(Determinism) 포트 — 시간(Clock)과 난수/UUID(Random)를 명시적으로 주입하기 위한 공용 계약.
|
||||
// 프로덕션 기본값은 systemClock / systemRandom 이며, 단위·통합 테스트는 createFakeClock / createSeededRandom 을 주입한다.
|
||||
|
||||
import { randomUUID } from 'node:crypto'
|
||||
|
||||
export interface Clock {
|
||||
now(): number
|
||||
}
|
||||
|
||||
export interface FakeClock extends Clock {
|
||||
advance(ms: number): number
|
||||
set(ms: number): void
|
||||
}
|
||||
|
||||
export interface Random {
|
||||
/** [0, 1) 범위 부동소수점 난수 */
|
||||
next(): number
|
||||
/** RFC 4122 v4 형식 UUID */
|
||||
uuid(): string
|
||||
}
|
||||
|
||||
export const systemClock: Clock = {
|
||||
now: () => Date.now()
|
||||
}
|
||||
|
||||
export const systemRandom: Random = {
|
||||
next: () => Math.random(),
|
||||
uuid: () => randomUUID()
|
||||
}
|
||||
|
||||
export function createFakeClock(initialMs = 1_700_000_000_000): FakeClock {
|
||||
let currentMs = initialMs
|
||||
return {
|
||||
now: () => currentMs,
|
||||
advance(ms: number): number {
|
||||
currentMs += ms
|
||||
return currentMs
|
||||
},
|
||||
set(ms: number): void {
|
||||
currentMs = ms
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 결정적 Mulberry32 기반 PRNG + RFC 4122 v4 형식 UUID 생성기.
|
||||
* 동일 seed 입력 시 플랫폼·시각과 무관하게 동일한 수열을 보장한다.
|
||||
*/
|
||||
export function createSeededRandom(seed = 1): Random {
|
||||
let state = seed >>> 0
|
||||
|
||||
const nextUint32 = (): number => {
|
||||
state = (state + 0x6d2b79f5) >>> 0
|
||||
let t = Math.imul(state ^ (state >>> 15), 1 | state)
|
||||
t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t
|
||||
return (t ^ (t >>> 14)) >>> 0
|
||||
}
|
||||
|
||||
return {
|
||||
next(): number {
|
||||
return nextUint32() / 0x1_0000_0000
|
||||
},
|
||||
uuid(): string {
|
||||
const bytes = new Uint8Array(16)
|
||||
for (let offset = 0; offset < 16; offset += 4) {
|
||||
const word = nextUint32()
|
||||
bytes[offset] = (word >>> 24) & 0xff
|
||||
bytes[offset + 1] = (word >>> 16) & 0xff
|
||||
bytes[offset + 2] = (word >>> 8) & 0xff
|
||||
bytes[offset + 3] = word & 0xff
|
||||
}
|
||||
bytes[6] = (bytes[6] & 0x0f) | 0x40
|
||||
bytes[8] = (bytes[8] & 0x3f) | 0x80
|
||||
|
||||
const hex = Array.from(bytes, (b) => b.toString(16).padStart(2, '0')).join('')
|
||||
return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20, 32)}`
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue