Skip to content

The VS Code of video editing, built for agents.

TypeScript

Remove `sqids` dependency and refactor ID allocation in asset management

konstantin-paulusc630a75f5d2d
diff --git a/apps/desktop/package.json b/apps/desktop/package.json
index 4fcd10dd..cb3ead68 100644
--- a/apps/desktop/package.json
+++ b/apps/desktop/package.json
@@ -39,7 +39,6 @@
"@diffusionstudio/jsx": "*",
"babel-preset-solid": "^1.9.12",
"esbuild": "^0.28.1",
- "sqids": "^0.3.0",
"ts-morph": "^28.0.0",
"update-electron-app": "^3.0.0",
"yaml": "^2.9.0"
diff --git a/apps/desktop/src/edit.ts b/apps/desktop/src/edit.ts
index 05b55f6e..a07e16aa 100644
--- a/apps/desktop/src/edit.ts
+++ b/apps/desktop/src/edit.ts
@@ -3,10 +3,10 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+import { randomInt } from "node:crypto";
import { readdir, readFile, writeFile } from "node:fs/promises";
import { join } from "node:path";
-import Sqids from "sqids";
import { IndentationText, Project, SyntaxKind } from "ts-morph";
import { ID_ATTR, formatSource, isCompositionTag, isLoopTag, parseSource } from "@diffusionstudio/jsx";
@@ -133,8 +133,6 @@ type JsxTag = JsxOpeningElement | JsxSelfClosingElement;
const SOURCE_FILE = /\.[jt]sx?$/;
-const sqids = new Sqids({ minLength: 2 });
-
const absolute = (dir: string, file: string): string => join(dir, ...file.split("/"));
// ---------------------------------------------------------------------------
@@ -185,21 +183,17 @@ function findTag(sourceFile: SourceFile, locator: number | string): JsxTag | und
const idsIn = (sourceFile: SourceFile): Set<string> =>
new Set(tags(sourceFile).flatMap((tag) => idOf(tag) ?? []));
-function counterOf(id: string): number {
- const decoded = sqids.decode(id);
- return decoded.length === 1 && Number.isInteger(decoded[0]) ? decoded[0]! : 0;
-}
+const ID_LENGTH = 6;
+const ID_SPACE = 36 ** ID_LENGTH;
+/**
+ * Ids only need to be unique within their file, so a fresh one is simply
+ * drawn until it misses everything already taken.
+ */
function idAllocator(taken: Set<string>): () => string {
- let counter = 0;
-
- for (const id of taken) {
- counter = Math.max(counter, counterOf(id));
- }
-
return () => {
while (true) {
- const id = sqids.encode([++counter]);
+ const id = randomInt(ID_SPACE).toString(36).padStart(ID_LENGTH, "0");
if (taken.has(id)) continue;
taken.add(id);
return id;
diff --git a/apps/web/package.json b/apps/web/package.json
index 2529e327..73a9da2c 100644
--- a/apps/web/package.json
+++ b/apps/web/package.json
@@ -39,7 +39,6 @@
"mediabunny": "^1.32.2",
"solid-js": "^1.9.10",
"somoto": "^0.0.2",
- "sqids": "^0.3.0",
"stats.js": "^0.17.0",
"tailwind-merge": "^3.4.0",
"typegpu": "^0.11.9",
diff --git a/apps/web/src/components/engine/api/assets.ts b/apps/web/src/components/engine/api/assets.ts
index b901cbdb..e4a7ec56 100644
--- a/apps/web/src/components/engine/api/assets.ts
+++ b/apps/web/src/components/engine/api/assets.ts
@@ -9,7 +9,6 @@ import { ALL_FORMATS, InputTrack, UrlSource } from 'mediabunny';
import { BlobSource } from 'mediabunny';
import { Input } from 'mediabunny';
import { nanoid } from "nanoid";
-import Sqids from "sqids";
import { saveDirectoryHandle, retrieveDirectoryHandle } from "../db";
import { GeometryType, PaintType } from "../components";
import { secondsToFrames } from "../utils/time";
@@ -32,7 +31,6 @@ import type { Transcript } from "@diffusionstudio/api-contract";
const URL_REGEX = /\b(?:https?|ftp|blob):\/\/[^\s]+/;
const DEFAULT_SEQUENCE_FPS = 30;
-const sqids = new Sqids({ minLength: 4 });
type MediaInput =
| string
@@ -100,22 +98,20 @@ export function invalidateAssets(world: EngineWorld): void {
export function allocateId(world: EngineWorld): string {
const state = assetState(world);
state.counter += 1;
- return sqids.encode([state.counter]);
+ return state.counter.toString(36).padStart(4, "0");
}
/**
- * Raises the id counter past every sqid in `ids`. Non-sqid ids (e.g. legacy
- * "fld-…" folder ids) are ignored. Restores may run in parallel, so this only
- * ever raises the counter.
+ * Raises the id counter past every counter id in `ids`. Ids that are not
+ * base36 numbers (e.g. legacy "fld-…" folder ids) are ignored. Restores may
+ * run in parallel, so this only ever raises the counter.
*/
export function raiseIdCounter(world: EngineWorld, ids: Iterable<string>): void {
const state = assetState(world);
for (const id of ids) {
- const decoded = sqids.decode(id);
- const num = decoded[0];
- if (decoded.length === 1 && Number.isFinite(num) && num > state.counter) {
- state.counter = num;
- }
+ if (!/^[0-9a-z]+$/i.test(id)) continue;
+ const num = parseInt(id, 36);
+ if (Number.isSafeInteger(num) && num > state.counter) state.counter = num;
}
}
diff --git a/apps/web/src/components/ui/asset-thumbnail.tsx b/apps/web/src/components/ui/asset-thumbnail.tsx
index 545d1ae5..7fd8eada 100644
--- a/apps/web/src/components/ui/asset-thumbnail.tsx
+++ b/apps/web/src/components/ui/asset-thumbnail.tsx
@@ -143,7 +143,7 @@ export function AssetThumbnail(props: AssetThumbnailProps) {
const width = () => props.size?.width ?? DEFAULT_THUMBNAIL_WIDTH;
return (
- <div class={cx(props.class, 'relative')} draggable={props.draggable}>
+ <div class={cx('relative', props.class)} draggable={props.draggable}>
<Show when={props.asset.mimeType.startsWith('image') || props.asset.mimeType.startsWith('video')}>
<MediaThumbnail asset={props.asset} width={width()} cache={props.cache} />
</Show>
diff --git a/package-lock.json b/package-lock.json
index eed01533..f9d25675 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -52,7 +52,6 @@
"@diffusionstudio/jsx": "*",
"babel-preset-solid": "^1.9.12",
"esbuild": "^0.28.1",
- "sqids": "^0.3.0",
"ts-morph": "^28.0.0",
"update-electron-app": "^3.0.0",
"yaml": "^2.9.0"
@@ -74,6 +73,7 @@
"license": "MPL-2.0",
"dependencies": {
"@diffusionstudio/api-contract": "^0.1.0",
+ "@diffusionstudio/assets": "*",
"@diffusionstudio/cli": "*",
"@diffusionstudio/jsx": "*",
"@diffusionstudio/koota-solid": "*",
@@ -98,7 +98,6 @@
"mediabunny": "^1.32.2",
"solid-js": "^1.9.10",
"somoto": "^0.0.2",
- "sqids": "^0.3.0",
"stats.js": "^0.17.0",
"tailwind-merge": "^3.4.0",
"typegpu": "^0.11.9",
@@ -10978,12 +10977,6 @@
"license": "BSD-3-Clause",
"optional": true
},
- "node_modules/sqids": {
- "version": "0.3.0",
- "resolved": "https://registry.npmjs.org/sqids/-/sqids-0.3.0.tgz",
- "integrity": "sha512-lOQK1ucVg+W6n3FhRwwSeUijxe93b51Bfz5PMRMihVf1iVkl82ePQG7V5vwrhzB11v0NtsR25PSZRGiSomJaJw==",
- "license": "MIT"
- },
"node_modules/ssri": {
"version": "9.0.1",
"resolved": "https://registry.npmjs.org/ssri/-/ssri-9.0.1.tgz",
@@ -12446,6 +12439,7 @@
"version": "0.1.0",
"license": "MPL-2.0",
"dependencies": {
+ "@diffusionstudio/assets": "*",
"@diffusionstudio/jsx": "*",
"@diffusionstudio/runtime": "*",
"koota": "^0.6.6"