Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

nanoids

Manav Rathice421eded48e
diff --git a/web/docs/dependencies.md b/web/docs/dependencies.md
index c28824b288..efdbf617ce 100644
--- a/web/docs/dependencies.md
+++ b/web/docs/dependencies.md
@@ -193,6 +193,9 @@ For more details, see [translations.md](translations.md).
- [zod](https://github.com/colinhacks/zod) is used for runtime typechecking
(e.g. verifying that API responses match the expected TypeScript shape).
+- [nanoid](https://github.com/ai/nanoid) is used for generating unique
+ identifiers.
+
- [debounce](https://github.com/sindresorhus/debounce) and its
promise-supporting sibling
[pDebounce](https://github.com/sindresorhus/p-debounce) are used for
diff --git a/web/packages/base/id-worker.ts b/web/packages/base/id-worker.ts
new file mode 100644
index 0000000000..593e2d236e
--- /dev/null
+++ b/web/packages/base/id-worker.ts
@@ -0,0 +1,17 @@
+import { customAlphabet } from "nanoid/non-secure";
+import { alphabet } from "./id";
+
+const nanoid = customAlphabet(alphabet, 22);
+
+/**
+ * This is a variant of the regular {@link newID} that can be used in web
+ * workers.
+ *
+ * Web workers don't have access to a secure random generator, so we need to use
+ * the non-secure variant.
+ * https://github.com/ai/nanoid?tab=readme-ov-file#web-workers
+ *
+ * For many of our use cases, where we're not using these IDs for cryptographic
+ * operations, this is okay. We also have an increased alphabet length.
+ */
+export const newNonSecureID = (prefix: string) => prefix + nanoid();
diff --git a/web/packages/base/id.ts b/web/packages/base/id.ts
new file mode 100644
index 0000000000..5b9cebbbaa
--- /dev/null
+++ b/web/packages/base/id.ts
@@ -0,0 +1,24 @@
+import { customAlphabet } from "nanoid";
+
+/**
+ * Remove _ and - from the default set to have better looking IDs that can also
+ * be selected in the editor quickly ("-" prevents this), and which we can
+ * prefix unambigously ("_" is used for that).
+ *
+ * To compensate, increase length from the default of 21 to 22.
+ *
+ * To play around with these, use https://zelark.github.io/nano-id-cc/
+ */
+export const alphabet =
+ "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
+
+const nanoid = customAlphabet(alphabet, 22);
+
+/**
+ * Generate a new random identifier with the given prefix.
+ *
+ * Internally this uses [nanoids](https://github.com/ai/nanoid).
+ *
+ * See {@link newNonSecureID} for a variant that can be used in web workers.
+ */
+export const newID = (prefix: string) => prefix + nanoid();
diff --git a/web/packages/base/package.json b/web/packages/base/package.json
index 86f21d8fdf..36bcf203f9 100644
--- a/web/packages/base/package.json
+++ b/web/packages/base/package.json
@@ -12,6 +12,7 @@
"i18next": "^23.11",
"i18next-resources-to-backend": "^1.2",
"is-electron": "^2.2",
+ "nanoid": "^5.0.7",
"next": "^14.2",
"react": "^18",
"react-dom": "^18",
diff --git a/web/packages/new/photos/services/ml/cluster-new.ts b/web/packages/new/photos/services/ml/cluster-new.ts
index 0f1abd153e..bad2652f7d 100644
--- a/web/packages/new/photos/services/ml/cluster-new.ts
+++ b/web/packages/new/photos/services/ml/cluster-new.ts
@@ -7,7 +7,9 @@ import type { FaceIndex } from "./face";
* Each cluster has an id so that a Person (a set of clusters) can refer to it.
*/
export interface Cluster {
- /** A unique nanoid to identify this cluster. */
+ /**
+ * A randomly generated ID to uniquely identify this cluster.
+ */
id: string;
/**
* An unordered set of ids of the faces that belong to the cluster.
@@ -31,7 +33,9 @@ export interface Cluster {
* That is, it has the clusters embedded within itself.
*/
export interface Person {
- /** A unique nanoid to identify this person. */
+ /**
+ * A randomly generated ID to uniquely identify this person.
+ */
id: string;
/**
* An optional name assigned by the user to this person.
@@ -67,5 +71,6 @@ export interface Person {
*/
export const clusterFaces = (faceIndices: FaceIndex[]) => {
log.debug(() => ["Clustering", faceIndices]);
+ const clusters: Cluster = []
return undefined;
};
diff --git a/web/yarn.lock b/web/yarn.lock
index caf41595f2..04405053f3 100644
--- a/web/yarn.lock
+++ b/web/yarn.lock
@@ -3374,7 +3374,7 @@ [email protected]:
dependencies:
libsodium "^0.7.0"
[email protected], libsodium@^0.7.0:
+libsodium@^0.7.0:
version "0.7.9"
resolved "https://registry.yarnpkg.com/libsodium/-/libsodium-0.7.9.tgz#4bb7bcbf662ddd920d8795c227ae25bbbfa3821b"
integrity sha512-gfeADtR4D/CM0oRUviKBViMGXZDgnFdMKMzHsvBdqLBHd9ySi6EtYnmuhHVDDYgYpAO8eU8hEY+F8vIUAPh08A==
@@ -3590,6 +3590,11 @@ nanoid@^3.3.6, nanoid@^3.3.7:
resolved "https://registry.yarnpkg.com/nanoid/-/nanoid-3.3.7.tgz#d0c301a691bc8d54efa0a2226ccf3fe2fd656bd8"
integrity sha512-eSRppjcPIatRIMC1U6UngP8XFcz8MQWGQdt1MTBQ7NaAmvXDfvNxbvWV3x2y6CdEUciCSsDHDQZbhYaB8QEo2g==
+nanoid@^5.0.7:
+ version "5.0.7"
+ resolved "https://registry.yarnpkg.com/nanoid/-/nanoid-5.0.7.tgz#6452e8c5a816861fd9d2b898399f7e5fd6944cc6"
+ integrity sha512-oLxFY2gd2IqnjcYyOXD8XGCftpGtZP2AbHbOkthDkvRywH5ayNtPVy9YlOPcHckXzbLTCHpkb7FB+yuxKV13pQ==
+
natural-compare@^1.4.0:
version "1.4.0"
resolved "https://registry.yarnpkg.com/natural-compare/-/natural-compare-1.4.0.tgz#4abebfeed7541f2c27acfb29bdbbd15c8d5ba4f7"