From aff1ca8f9858288d1df2410d68d84e56aba2b7e5 Mon Sep 17 00:00:00 2001 From: Formsmith746 <214276465+Formsmith746@users.noreply.github.com> Date: Sun, 23 Aug 2026 13:45:27 +0300 Subject: [PATCH] fix: import dense STL meshes --- .github/SETUP.md | 2 +- .github/workflows/desktop-release.yml | 2 +- README.md | 2 +- apps/web/src/lib/editorHistory.ts | 130 +++++++++++++++++++++++++- apps/web/src/lib/skfProject.ts | 2 +- docs/CHANGELOG.md | 5 + package-lock.json | 4 +- package.json | 2 +- tests/unit/editorHistory.test.ts | 33 ++++++- 9 files changed, 173 insertions(+), 9 deletions(-) diff --git a/.github/SETUP.md b/.github/SETUP.md index 0818de7..0f5d3df 100644 --- a/.github/SETUP.md +++ b/.github/SETUP.md @@ -32,7 +32,7 @@ permissions: ## Create a release -Push a tag such as `v1.0.5`, or run `Desktop Release` with `workflow_dispatch` and enter `1.0.5`. +Push a tag such as `v1.0.6`, or run `Desktop Release` with `workflow_dispatch` and enter `1.0.6`. The workflow builds these packages: diff --git a/.github/workflows/desktop-release.yml b/.github/workflows/desktop-release.yml index 8d6736f..f6d3b63 100644 --- a/.github/workflows/desktop-release.yml +++ b/.github/workflows/desktop-release.yml @@ -8,7 +8,7 @@ on: workflow_dispatch: inputs: version: - description: "Version to release (for example 1.0.5)" + description: "Version to release (for example 1.0.6)" required: true type: string diff --git a/README.md b/README.md index 50dc8f4..2b1c5a1 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ Star SketchForge on GitHub Sponsor SketchForge on GitHub Local first - Version v1.0.5 + Version v1.0.6

diff --git a/apps/web/src/lib/editorHistory.ts b/apps/web/src/lib/editorHistory.ts index 85f12d6..fcb1449 100644 --- a/apps/web/src/lib/editorHistory.ts +++ b/apps/web/src/lib/editorHistory.ts @@ -24,6 +24,8 @@ type ResourceSignature = { const resourceSignatureCache = new WeakMap(); const stringSignatureCache = new Map(); const MAX_CACHED_STRING_SIGNATURES = 64; +const STREAMED_NUMERIC_ARRAY_LENGTH = 10_000; +const STREAMED_NUMERIC_ARRAY_MARKER = "$sketchforgeFloat64Array"; const COMPACT_RESOURCE_KEYS = new Set([ "cadDisplayEdges", "edgeTreatmentHistory", @@ -47,10 +49,136 @@ function signatureFromSerialized(serialized: string): ResourceSignature { }; } +type StreamingHashState = { + hashA: number; + hashB: number; + units: number; + estimatedBytes: number; +}; + +function streamingHashState(): StreamingHashState { + return { hashA: 2166136261, hashB: 5381, units: 0, estimatedBytes: 0 }; +} + +function appendHashUnit(state: StreamingHashState, unit: number) { + state.hashA = Math.imul(state.hashA ^ unit, 16777619); + state.hashB = Math.imul(state.hashB, 33) ^ unit; + state.units += 1; +} + +function appendHashText(state: StreamingHashState, value: string) { + for (let index = 0; index < value.length; index += 1) { + appendHashUnit(state, value.charCodeAt(index)); + } + state.estimatedBytes += value.length * 2; +} + +function appendHashNumber(state: StreamingHashState, value: number, view: DataView) { + view.setFloat64(0, value, true); + appendHashUnit(state, view.getUint32(0, true)); + appendHashUnit(state, view.getUint32(4, true)); + state.estimatedBytes += 8; +} + +function signatureFromStreamingState(state: StreamingHashState, prefix: string): ResourceSignature { + return { + fingerprint: `${prefix}:${state.units}:${state.hashA >>> 0}:${state.hashB >>> 0}`, + estimatedBytes: state.estimatedBytes, + }; +} + +function largeNumericArraySignature(value: unknown) { + if (!Array.isArray(value) || value.length <= STREAMED_NUMERIC_ARRAY_LENGTH) return null; + const state = streamingHashState(); + const view = new DataView(new ArrayBuffer(8)); + appendHashText(state, `float64-array:${value.length}:`); + for (let index = 0; index < value.length; index += 1) { + const number = value[index]; + if (typeof number !== "number") return null; + appendHashNumber(state, number, view); + } + return signatureFromStreamingState(state, "float64-v1"); +} + +function signatureFromStreamedValue(resource: object) { + const state = streamingHashState(); + const view = new DataView(new ArrayBuffer(8)); + const active = new WeakSet(); + + const appendValue = (value: unknown, arrayValue = false): void => { + if (value === null || (arrayValue && (value === undefined || typeof value === "function" || typeof value === "symbol"))) { + appendHashText(state, "null;"); + return; + } + if (typeof value === "string") { + appendHashText(state, `string:${value.length}:`); + appendHashText(state, value); + return; + } + if (typeof value === "number") { + appendHashText(state, "number:"); + appendHashNumber(state, value, view); + return; + } + if (typeof value === "boolean") { + appendHashText(state, value ? "true;" : "false;"); + return; + } + if (typeof value === "bigint") { + throw new TypeError("Cannot serialize a BigInt resource"); + } + if (value === undefined || typeof value === "function" || typeof value === "symbol") { + appendHashText(state, "undefined;"); + return; + } + + if (active.has(value)) throw new TypeError("Cannot fingerprint a circular resource"); + active.add(value); + if (Array.isArray(value)) { + appendHashText(state, `array:${value.length}:`); + for (let index = 0; index < value.length; index += 1) appendValue(value[index], true); + } else { + const record = value as Record; + const keys = Object.keys(record).filter((key) => { + const item = record[key]; + return item !== undefined && typeof item !== "function" && typeof item !== "symbol"; + }); + appendHashText(state, `object:${keys.length}:`); + for (const key of keys) { + appendHashText(state, `key:${key.length}:`); + appendHashText(state, key); + appendValue(record[key]); + } + } + active.delete(value); + }; + + appendValue(resource); + return signatureFromStreamingState(state, "stream-v1"); +} + +function signatureWithStreamedNumericArrays(resource: object) { + let streamedBytes = 0; + const serialized = JSON.stringify(resource, (_key, value: unknown) => { + const signature = largeNumericArraySignature(value); + if (!signature) return value; + streamedBytes += signature.estimatedBytes; + return { [STREAMED_NUMERIC_ARRAY_MARKER]: signature.fingerprint }; + }); + const signature = signatureFromSerialized(serialized); + return { ...signature, estimatedBytes: signature.estimatedBytes + streamedBytes }; +} + function objectResourceSignature(resource: object) { const cached = resourceSignatureCache.get(resource); if (cached) return cached; - const signature = signatureFromSerialized(JSON.stringify(resource)); + let signature: ResourceSignature; + try { + signature = signatureWithStreamedNumericArrays(resource); + } catch (error) { + if (!(error instanceof RangeError) || !/string length/i.test(error.message)) throw error; + signature = signatureFromStreamedValue(resource); + } resourceSignatureCache.set(resource, signature); return signature; } diff --git a/apps/web/src/lib/skfProject.ts b/apps/web/src/lib/skfProject.ts index 386a535..5d7fe0a 100644 --- a/apps/web/src/lib/skfProject.ts +++ b/apps/web/src/lib/skfProject.ts @@ -12,7 +12,7 @@ import type { GridSize, ProjectAsset, ProjectAssetSourceFormat, SketchOperation, export const SKF_SCHEMA_ID = "com.sketchforge.project"; export const SKF_FORMAT_VERSION = 1; export const SKF_MINIMUM_READER_VERSION = 1; -export const SKF_CREATED_WITH_VERSION = "1.0.5"; +export const SKF_CREATED_WITH_VERSION = "1.0.6"; export const SKF_MEDIA_TYPE = "application/vnd.sketchforge.project+zip"; export const SKF_LIMITS = { diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index f91a174..e5c9bdc 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -1,5 +1,10 @@ # Changelog +## 1.0.6 + +- Fixed dense STL imports failing with `Invalid string length` while creating their initial undo-history fingerprint. +- Streamed large numeric mesh arrays into deterministic hashes instead of converting millions of coordinates to one oversized JSON string. + ## 1.0.5 - Made the rotation handles larger and aligned their arrow glyphs with the model faces as the camera moves, including stable behavior on long objects. diff --git a/package-lock.json b/package-lock.json index a876674..94013eb 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "sketchforge", - "version": "1.0.5", + "version": "1.0.6", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "sketchforge", - "version": "1.0.5", + "version": "1.0.6", "license": "AGPL-3.0-only", "dependencies": { "@tailwindcss/postcss": "^4.1.8", diff --git a/package.json b/package.json index 29188ee..f199ab0 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "sketchforge", - "version": "1.0.5", + "version": "1.0.6", "private": true, "description": "A local-first 3D design editor.", "author": { diff --git a/tests/unit/editorHistory.test.ts b/tests/unit/editorHistory.test.ts index e8a99d6..096e553 100644 --- a/tests/unit/editorHistory.test.ts +++ b/tests/unit/editorHistory.test.ts @@ -1,5 +1,5 @@ import { describe, expect, it } from "vitest"; -import { appendEditorHistorySnapshot, boundedEditorHistory, editorHistoryEntry, editorHistoryForExport, hydrateEditorHistoryState, projectShapesFingerprint } from "@/lib/editorHistory"; +import { appendEditorHistorySnapshot, boundedEditorHistory, editorHistoryEntry, editorHistoryForExport, hydrateEditorHistoryState, immutableResourceFingerprint, projectShapesFingerprint } from "@/lib/editorHistory"; import type { WorkplaneShape } from "@/types/sketchforge"; function box(overrides: Partial = {}): WorkplaneShape { @@ -93,6 +93,37 @@ describe("editor history snapshots", () => { }])).toBe(baseline); }); + it("stream-hashes large mesh arrays and detects changes at the end", () => { + const positions = Array.from({ length: 10_001 }, (_, index) => Math.fround(Math.sin(index) * 100)); + const resource = { + positions, + normals: positions.map((value) => Math.fround(value / 100)), + baseWidth: 200, + baseDepth: 150, + baseHeight: 80, + triangleCount: Math.floor(positions.length / 9), + sourceFormat: "stl", + }; + const changedPositions = [...positions]; + changedPositions[changedPositions.length - 1] += 1; + + expect(immutableResourceFingerprint({ ...resource, positions: [...positions], normals: [...resource.normals] })) + .toBe(immutableResourceFingerprint(resource)); + expect(immutableResourceFingerprint({ ...resource, positions: changedPositions })) + .not.toBe(immutableResourceFingerprint(resource)); + }); + + it("falls back to direct field hashing when serialization exceeds the string limit", () => { + const resource = { + positions: [0, 0, 0, 1, 0, 0, 0, 1, 0], + toJSON() { + throw new RangeError("Invalid string length"); + }, + }; + + expect(immutableResourceFingerprint(resource)).toMatch(/^stream-v1:/); + }); + it("keeps unlimited history and applies preset or custom action limits", () => { const entries = Array.from({ length: 140 }, (_, index) => ({ ...editorHistoryEntry([box({ id: `box-${index}`, x: index })], []),