From d7b8b57e9c0e84bee0cb9e8498d31fdcb50618e5 Mon Sep 17 00:00:00 2001 From: lonelyhentxi Date: Fri, 21 Mar 2025 13:54:39 +0800 Subject: [PATCH] feat: temp save --- .vscode/settings.json | 3 +- apps/playground/package.json | 2 +- apps/playground/scripts/codegen.ts | 0 apps/playground/src/media/mkv/model.ts | 121 +++++------------------- apps/playground/src/media/mkv/schema.ts | 0 apps/playground/src/media/mkv/util.ts | 45 ++++----- biome.jsonc | 4 + pnpm-lock.yaml | 17 +++- 8 files changed, 62 insertions(+), 130 deletions(-) create mode 100644 apps/playground/scripts/codegen.ts create mode 100644 apps/playground/src/media/mkv/schema.ts diff --git a/.vscode/settings.json b/.vscode/settings.json index b2539e9..f6611fc 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -6,5 +6,6 @@ // prioritize ArkType's "type" for autoimports "typescript.preferences.autoImportSpecifierExcludeRegexes": [ "^(node:)?os$" - ] + ], + "typescript.tsdk": "node_modules/typescript/lib" } \ No newline at end of file diff --git a/apps/playground/package.json b/apps/playground/package.json index 9200978..777d0b3 100644 --- a/apps/playground/package.json +++ b/apps/playground/package.json @@ -9,7 +9,7 @@ "preview": "rsbuild preview" }, "dependencies": { - "konoebml": "0.1.0-rc.8", + "konoebml": "0.1.0", "lit": "^3.2.1" }, "devDependencies": { diff --git a/apps/playground/scripts/codegen.ts b/apps/playground/scripts/codegen.ts new file mode 100644 index 0000000..e69de29 diff --git a/apps/playground/src/media/mkv/model.ts b/apps/playground/src/media/mkv/model.ts index 417ad8d..3d6db16 100644 --- a/apps/playground/src/media/mkv/model.ts +++ b/apps/playground/src/media/mkv/model.ts @@ -2,7 +2,6 @@ import { type EbmlTagType, EbmlTagIdEnum, EbmlTagPosition, - type EbmlCuePointTagType, type EbmlTracksTagType, type EbmlInfoTagType, type EbmlCuesTagType, @@ -127,13 +126,11 @@ export class TrackEntry extends TagWithArktype({ }), }) {} -const TracksSchema = type({ - tracks: type.instanceOf(TrackEntry).array(), -}); - export class Tracks extends TagWithArktype({ id: EbmlTagIdEnum.Tracks, - schema: TracksSchema, + schema: type({ + tracks: type.instanceOf(TrackEntry).array(), + }), extract: simpleMasterExtractor({ [EbmlTagIdEnum.TrackEntry]: { key: 'tracks', @@ -148,13 +145,11 @@ export interface EbmlSeekEntry { seekPosition: number; } -export class EbmlHead { - head: EbmlTagType; - - constructor(head: EbmlTagType) { - this.head = head; - } -} +export class MHead extends TagWithArktype({ + id: EbmlTagIdEnum.EBML, + schema: type({}), + extract: () => ({}), +}) {} export class SimpleBlock extends TagWithArktype({ id: EbmlTagIdEnum.SimpleBlock, @@ -166,6 +161,12 @@ export class SimpleBlock extends TagWithArktype({ }), }) {} +export class Block extends TagWithArktype({ + id: EbmlTagIdEnum.Block, + schema: type({}), + extract: () => ({}), +}) {} + export class Cluster extends TagWithArktype({ id: EbmlTagIdEnum.Cluster, schema: type({ @@ -191,88 +192,16 @@ export class Cluster extends TagWithArktype({ }), }) {} -export interface TrackPositions { - track: number; - clusterPosition: number; - relativePosition?: number; - duration?: number; -} +export type CuePointType = typeof CuePoint.infer; -export class CuePoint { - node: EbmlCuePointTagType; - _timestamp: number; - trackPositions: TrackPositions[]; +export class Cues { + cues: CuePointType[]; - get timestamp(): number { - return this._timestamp; - } + constructor( + public readonly tag: EbmlCuesTagType, + cues: CuePointType[] + ) {} - get position(): number { - return Math.max(...this.trackPositions.map((t) => t.clusterPosition)); - } - - constructor(node: EbmlCuePointTagType) { - this.node = node; - this._timestamp = node.children.find((c) => c.id === EbmlTagIdEnum.CueTime) - ?.data as number; - this.trackPositions = node.children - // biome-ignore lint/complexity/noExcessiveCognitiveComplexity: - .map((t) => { - if ( - t.id === EbmlTagIdEnum.CueTrackPositions && - t.position === EbmlTagPosition.End - ) { - let track!: number; - let clusterPosition!: number; - let relativePosition: number | undefined; - let duration: number | undefined; - - for (const c of t.children) { - if (c.id === EbmlTagIdEnum.CueTrack) { - track = c.data as number; - } - if (c.id === EbmlTagIdEnum.CueClusterPosition) { - clusterPosition = c.data as number; - } - if (c.id === EbmlTagIdEnum.CueRelativePosition) { - relativePosition = c.data as number; - } - if (c.id === EbmlTagIdEnum.CueDuration) { - duration = c.data as number; - } - } - - if (track! >= 0 && clusterPosition! >= 0) { - return { - track: track!, - clusterPosition: clusterPosition!, - relativePosition, - duration, - } as TrackPositions; - } - throw new Error( - `Tracking positions missing track of cluster position at ${t.startOffset}` - ); - } - return null; - }) - .filter((a): a is TrackPositions => !!a); - } -} - -export class Cues extends TagWithArktype({ - id: EbmlTagIdEnum.Cues, - schema: type({ - cues: type.instanceOf(CuePoint).array(), - }), - extract: simpleMasterExtractor({ - [EbmlTagIdEnum.CuePoint]: { - key: 'cues', - multi: true, - extract: (t) => new CuePoint(t), - }, - }), -}) { findClosestCue(seekTime: number): CuePoint | null { const cues = this.cues; if (!cues || cues.length === 0) { @@ -282,22 +211,22 @@ export class Cues extends TagWithArktype({ let left = 0; let right = cues.length - 1; - if (seekTime <= cues[0].timestamp) { + if (seekTime <= cues[0].CueTime) { return cues[0]; } - if (seekTime >= cues[right].timestamp) { + if (seekTime >= cues[right].CueTime) { return cues[right]; } while (left <= right) { const mid = Math.floor((left + right) / 2); - if (cues[mid].timestamp === seekTime) { + if (cues[mid].CueTime === seekTime) { return cues[mid]; } - if (cues[mid].timestamp < seekTime) { + if (cues[mid].CueTime < seekTime) { left = mid + 1; } else { right = mid - 1; diff --git a/apps/playground/src/media/mkv/schema.ts b/apps/playground/src/media/mkv/schema.ts new file mode 100644 index 0000000..e69de29 diff --git a/apps/playground/src/media/mkv/util.ts b/apps/playground/src/media/mkv/util.ts index 6a53acf..5757d39 100644 --- a/apps/playground/src/media/mkv/util.ts +++ b/apps/playground/src/media/mkv/util.ts @@ -4,65 +4,56 @@ import type { EbmlMasterTagType, EbmlTagIdEnum, EbmlTagType } from 'konoebml'; export type InferType = T extends Type ? U : never; export interface TagWithArktypeOptions< - I extends EbmlTagType['id'], + T extends EbmlTagType, S extends Type, > { - id: I; + id: T['id']; schema: S; - extract: (tag: Extract, schema: S) => InferType; + extract: (tag: T, schema: S) => InferType; } export type TagWithArktypeClassInstance< - I extends EbmlTagType['id'], + T extends EbmlTagType, S extends Type, > = InferType & { - tag: Extract; + tag: T; }; export interface TagWithArktypeClass< - I extends EbmlTagType['id'], + T extends EbmlTagType, S extends Type, > { - new ( - tag: Extract, - validatedTag: InferType - ): TagWithArktypeClassInstance; + new (tag: T, validatedTag: InferType): TagWithArktypeClassInstance; - fromTag>( + fromTag>( this: new ( - tag: Extract, + tag: T, validatedTag: InferType - ) => TagWithArktypeClassInstance, - tag: Extract + ) => TagWithArktypeClassInstance, + tag: T ): R; - id: I; + id: T['id']; schema: S; } -export function TagWithArktype< - I extends EbmlTagType['id'], - S extends Type, ->({ +export function TagWithArktype>({ id, schema, extract, -}: TagWithArktypeOptions): TagWithArktypeClass { +}: TagWithArktypeOptions): TagWithArktypeClass { const tagWithArktypeImpl = class TagWithArktypeImpl { static id = id; static schema = schema; - tag: Extract; + tag: T; - constructor( - tag: Extract, - validatedTag: InferType - ) { + constructor(tag: T, validatedTag: InferType) { Object.assign(this, validatedTag); this.tag = tag; } - static fromTag(tag: Extract) { + static fromTag(tag: T) { const extractedData = extract(tag, schema); const validatedExtractedData = schema(extractedData); // biome-ignore lint/complexity/noThisInStatic: @@ -70,7 +61,7 @@ export function TagWithArktype< } }; - return tagWithArktypeImpl as unknown as TagWithArktypeClass; + return tagWithArktypeImpl as unknown as TagWithArktypeClass; } export type PredicateIdExtract = Extract; diff --git a/biome.jsonc b/biome.jsonc index b2553b5..cd5aecc 100644 --- a/biome.jsonc +++ b/biome.jsonc @@ -6,6 +6,7 @@ "linter": { "rules": { "style": { + "noParameterProperties": "off", "noNonNullAssertion": "off" }, "suspicious": { @@ -16,6 +17,9 @@ }, "complexity": { "noBannedTypes": "off" + }, + "nursery": { + "useConsistentMemberAccessibility": "off" } } }, diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 089e7f8..bdb998c 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -80,8 +80,8 @@ importers: apps/playground: dependencies: konoebml: - specifier: 0.1.0-rc.8 - version: 0.1.0-rc.8 + specifier: 0.1.0 + version: 0.1.0(arktype@2.1.10) lit: specifier: ^3.2.1 version: 3.2.1 @@ -1853,9 +1853,14 @@ packages: resolution: {integrity: sha512-dcS1ul+9tmeD95T+x28/ehLgd9mENa3LsvDTtzm3vyBEO7RPptvAD+t44WVXaUjTBRcrpFeFlC8WCruUR456hw==} engines: {node: '>=0.10.0'} - konoebml@0.1.0-rc.8: - resolution: {integrity: sha512-fR4DZqCskLKxGBMc58gpOOzajFrfu9hQC7WZd8yGiIxLVhDkzBnihXqlsWJU6Qw77ukMOGGkbeM2uqQyv5dO3w==} + konoebml@0.1.0: + resolution: {integrity: sha512-Fp4nJBr9E82sr2Ap0JwHZFoeyNH7Cy0NdVXRcJ54wxCNfs3MHfo9IXnSLph/zvkGT1b5qw7JSs4nwoygZF4F7g==} engines: {node: '>= 18.0.0'} + peerDependencies: + arktype: ^2.0.0 + peerDependenciesMeta: + arktype: + optional: true lines-and-columns@1.2.4: resolution: {integrity: sha512-7ylylesZQ/PV29jhEDl3Ufjo6ZX7gCqJr5F7PKrqc93v7fzSymt1BpwEU8nAUXs8qzzvqhbjhK5QZg6Mt/HkBg==} @@ -4462,10 +4467,12 @@ snapshots: kind-of@6.0.3: {} - konoebml@0.1.0-rc.8: + konoebml@0.1.0(arktype@2.1.10): dependencies: mnemonist: 0.40.3 type-fest: 4.37.0 + optionalDependencies: + arktype: 2.1.10 lines-and-columns@1.2.4: {}