diff --git a/packages/logic/package.json b/packages/logic/package.json index 5a782b4..a14d11f 100644 --- a/packages/logic/package.json +++ b/packages/logic/package.json @@ -2,8 +2,15 @@ "name": "@sammo-ts/logic", "private": true, "version": "0.0.0", + "type": "module", "main": "dist/index.js", "types": "dist/index.d.ts", + "exports": { + ".": { + "types": "./dist/index.d.ts", + "default": "./dist/index.js" + } + }, "scripts": { "build": "tsc -p tsconfig.json", "dev": "node -e \"console.log('dev not configured')\"", diff --git a/packages/logic/src/index.ts b/packages/logic/src/index.ts index 99fd3aa..5371785 100644 --- a/packages/logic/src/index.ts +++ b/packages/logic/src/index.ts @@ -1,10 +1,10 @@ -export * from './domain/entities'; -export * from './ports/rng'; -export * from './ports/world'; -export * from './triggers'; -export * from './util/BytesLike'; -export * from './util/convertBytesLikeToArrayBuffer'; -export * from './util/convertBytesLikeToUint8Array'; -export * from './util/LiteHashDRBG'; -export * from './util/RNG'; -export * from './util/RandUtil'; +export * from './domain/entities.js'; +export * from './ports/rng.js'; +export * from './ports/world.js'; +export * from './triggers/index.js'; +export * from './util/BytesLike.js'; +export * from './util/convertBytesLikeToArrayBuffer.js'; +export * from './util/convertBytesLikeToUint8Array.js'; +export * from './util/LiteHashDRBG.js'; +export * from './util/RNG.js'; +export * from './util/RandUtil.js'; diff --git a/packages/logic/src/ports/world.ts b/packages/logic/src/ports/world.ts index bb2c6d5..5e2be65 100644 --- a/packages/logic/src/ports/world.ts +++ b/packages/logic/src/ports/world.ts @@ -7,7 +7,7 @@ import type { GeneralId, Nation, NationId, -} from '../domain/entities'; +} from '../domain/entities.js'; export interface GeneralRepository { getById(id: GeneralId): Promise; diff --git a/packages/logic/src/triggers/general-action.ts b/packages/logic/src/triggers/general-action.ts index 63466f6..414329c 100644 --- a/packages/logic/src/triggers/general-action.ts +++ b/packages/logic/src/triggers/general-action.ts @@ -1,5 +1,5 @@ -import type { GeneralTriggerState } from '../domain/entities'; -import { type GeneralActionContext, GeneralTriggerCaller } from './general'; +import type { GeneralTriggerState } from '../domain/entities.js'; +import { type GeneralActionContext, GeneralTriggerCaller } from './general.js'; export interface GeneralActionModule { getName?(): string; diff --git a/packages/logic/src/triggers/general.ts b/packages/logic/src/triggers/general.ts index 224c537..32704b9 100644 --- a/packages/logic/src/triggers/general.ts +++ b/packages/logic/src/triggers/general.ts @@ -1,7 +1,7 @@ -import type { General, GeneralTriggerState } from '../domain/entities'; -import type { RandomGenerator } from '../ports/rng'; -import type { WorldStateRepository } from '../ports/world'; -import { TriggerCaller, type Trigger } from './core'; +import type { General, GeneralTriggerState } from '../domain/entities.js'; +import type { RandomGenerator } from '../ports/rng.js'; +import type { WorldStateRepository } from '../ports/world.js'; +import { TriggerCaller, type Trigger } from './core.js'; export interface GeneralActionLogSink { push(message: string): void; diff --git a/packages/logic/src/triggers/index.ts b/packages/logic/src/triggers/index.ts index 5b8f1f2..03cfb21 100644 --- a/packages/logic/src/triggers/index.ts +++ b/packages/logic/src/triggers/index.ts @@ -1,3 +1,3 @@ -export * from './core'; -export * from './general'; -export * from './general-action'; +export * from './core.js'; +export * from './general.js'; +export * from './general-action.js'; diff --git a/packages/logic/src/util/LiteHashDRBG.ts b/packages/logic/src/util/LiteHashDRBG.ts index f511ecd..ca8bb03 100644 --- a/packages/logic/src/util/LiteHashDRBG.ts +++ b/packages/logic/src/util/LiteHashDRBG.ts @@ -1,9 +1,9 @@ -import type { RNG } from './RNG'; +import type { RNG } from './RNG.js'; import { sha512 } from 'js-sha512'; -import { convertBytesLikeToUint8Array } from './convertBytesLikeToUint8Array'; -import type { BytesLike } from './BytesLike'; +import { convertBytesLikeToUint8Array } from './convertBytesLikeToUint8Array.js'; +import type { BytesLike } from './BytesLike.js'; const maxRngSupportBit = 53; const maxInt = 0x1f_ffff_ffff_ffff; // NOTE: b 0, 10000110011, 11...11 @@ -111,7 +111,7 @@ export class LiteHashDRBG implements RNG { protected genNextBlock(): void { this.hq.setUint32(this.hqIdxPos, this.stateIdx, true); - const digest = sha512.arrayBuffer(this.hq.buffer); + const digest = sha512.arrayBuffer(this.hq.buffer as ArrayBuffer); this.buffer = digest; this.bufferIdx = 0; this.stateIdx += 1; @@ -181,7 +181,7 @@ export class LiteHashDRBG implements RNG { return result; } - result[bytes - 1] &= 0xff >> (8 - headBits); + result[bytes - 1]! &= 0xff >> (8 - headBits); return result; } diff --git a/packages/logic/src/util/RandUtil.ts b/packages/logic/src/util/RandUtil.ts index 34db5a4..6ed934c 100644 --- a/packages/logic/src/util/RandUtil.ts +++ b/packages/logic/src/util/RandUtil.ts @@ -1,4 +1,4 @@ -import type { RNG } from './RNG'; +import type { RNG } from './RNG.js'; // RNG 유틸리티 모음 export class RandUtil { @@ -25,8 +25,8 @@ export class RandUtil { } public nextBit(): boolean { - const view = new DataView(this.rng.nextBits(1) as ArrayBufferLike); - return view.getUint8(0) != 0; + const bits = this.rng.nextBits(1); + return bits[0]! != 0; } public nextBool(prob = 0.5): boolean { @@ -57,7 +57,10 @@ export class RandUtil { if (srcIdx === destIdx) { continue; } - [result[srcIdx], result[destIdx]] = [result[destIdx], result[srcIdx]]; + const srcValue = result[srcIdx]!; + const destValue = result[destIdx]!; + result[srcIdx] = destValue; + result[destIdx] = srcValue; } return result; @@ -71,14 +74,15 @@ export class RandUtil { throw new Error('Empty items'); } const idx = this.rng.nextInt(items.length - 1); - return items[idx]; + return items[idx]!; } if (items instanceof Set) { return this.choice(Array.from(items.values())); } - return items[this.choice(Array.from(Object.keys(items)))]; + const key = this.choice(Array.from(Object.keys(items))) as keyof typeof items; + return items[key]!; } public choiceUsingWeight(items: Record): string | number { diff --git a/packages/logic/src/util/convertBytesLikeToArrayBuffer.ts b/packages/logic/src/util/convertBytesLikeToArrayBuffer.ts index f7326f7..eef3bc9 100644 --- a/packages/logic/src/util/convertBytesLikeToArrayBuffer.ts +++ b/packages/logic/src/util/convertBytesLikeToArrayBuffer.ts @@ -1,11 +1,22 @@ -import type { BytesLike } from './BytesLike'; +import type { BytesLike } from './BytesLike.js'; export function convertBytesLikeToArrayBuffer(data: BytesLike, encodeUTF8 = true): ArrayBuffer { if (data instanceof ArrayBuffer) { return data; } if (data instanceof Uint8Array) { - return data.buffer; + if ( + data.byteOffset === 0 + && data.byteLength === data.buffer.byteLength + && data.buffer instanceof ArrayBuffer + ) { + return data.buffer; + } + return data.slice().buffer; + } + if (data instanceof DataView) { + const view = new Uint8Array(data.buffer, data.byteOffset, data.byteLength); + return view.slice().buffer; } if (typeof (data) === 'string') { if (encodeUTF8) { @@ -13,5 +24,5 @@ export function convertBytesLikeToArrayBuffer(data: BytesLike, encodeUTF8 = true } return new Uint8Array(data.split('').map((s) => s.codePointAt(0) as number)).buffer; } - return data.buffer; + throw new Error('Unsupported BytesLike'); } diff --git a/packages/logic/src/util/convertBytesLikeToUint8Array.ts b/packages/logic/src/util/convertBytesLikeToUint8Array.ts index c3c27ca..5caf8fc 100644 --- a/packages/logic/src/util/convertBytesLikeToUint8Array.ts +++ b/packages/logic/src/util/convertBytesLikeToUint8Array.ts @@ -1,4 +1,4 @@ -import type { BytesLike } from './BytesLike'; +import type { BytesLike } from './BytesLike.js'; export function convertBytesLikeToUint8Array(data: BytesLike, encodeUTF8 = true): Uint8Array { if (data instanceof Uint8Array) { diff --git a/packages/logic/test/rng.test.ts b/packages/logic/test/rng.test.ts index ad28acd..4778459 100644 --- a/packages/logic/test/rng.test.ts +++ b/packages/logic/test/rng.test.ts @@ -1,9 +1,9 @@ import chai, { assert } from 'chai'; import chaiBytes from 'chai-bytes'; -import { bufferByteSize, LiteHashDRBG } from '../src/util/LiteHashDRBG'; -import { RandUtil } from '../src/util/RandUtil'; -import { convertBytesLikeToArrayBuffer } from '../src/util/convertBytesLikeToArrayBuffer'; -import { convertBytesLikeToUint8Array as toBytes } from '../src/util/convertBytesLikeToUint8Array'; +import { bufferByteSize, LiteHashDRBG } from '../src/util/LiteHashDRBG.js'; +import { RandUtil } from '../src/util/RandUtil.js'; +import { convertBytesLikeToArrayBuffer } from '../src/util/convertBytesLikeToArrayBuffer.js'; +import { convertBytesLikeToUint8Array as toBytes } from '../src/util/convertBytesLikeToUint8Array.js'; import _ from 'lodash-es'; chai.use(chaiBytes); diff --git a/packages/logic/tsconfig.json b/packages/logic/tsconfig.json index 2c11d91..00ee3aa 100644 --- a/packages/logic/tsconfig.json +++ b/packages/logic/tsconfig.json @@ -3,7 +3,20 @@ "compilerOptions": { "outDir": "dist", "rootDir": "src", - "composite": true + "composite": true, + "module": "NodeNext", + "moduleResolution": "NodeNext", + "target": "ES2023", + "lib": ["ES2024"], + "verbatimModuleSyntax": true, + "noUncheckedIndexedAccess": true, + "exactOptionalPropertyTypes": true, + "noImplicitOverride": true, + "noImplicitReturns": true, + "noFallthroughCasesInSwitch": true, + "useUnknownInCatchVariables": true, + "noUnusedLocals": true, + "noUnusedParameters": true }, "include": ["src"] }