sliftutils 1.1.42 → 1.1.44

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,65 @@
1
+ import { isNode } from "typesafecss";
2
+
3
+ // SharedWorker that synchronizes BulkDatabase2 writes between tabs of the same origin. It does NOT
4
+ // persist anything (each tab still writes to disk itself); it just relays writes to other tabs and
5
+ // buffers the recent ones so a freshly-opened tab can catch writes that haven't been flushed to disk
6
+ // yet. Per collection it keeps the latest write per key, pruned to a short recency window (older
7
+ // writes are already on disk, so a new tab gets them from there).
8
+
9
+ type RemoteWrite = { key: string; time: number; deleted?: boolean; value?: unknown };
10
+ type Collection = { ports: Set<MessagePort>; recent: Map<string, RemoteWrite> };
11
+
12
+ const RECENT_WINDOW_MS = 60_000;
13
+ const collections = new Map<string, Collection>();
14
+
15
+ function getCollection(name: string): Collection {
16
+ let c = collections.get(name);
17
+ if (!c) {
18
+ c = { ports: new Set(), recent: new Map() };
19
+ collections.set(name, c);
20
+ }
21
+ return c;
22
+ }
23
+
24
+ function prune(c: Collection) {
25
+ let cutoff = Date.now() - RECENT_WINDOW_MS;
26
+ for (let [key, write] of c.recent) {
27
+ if (write.time < cutoff) c.recent.delete(key);
28
+ }
29
+ }
30
+
31
+ function post(port: MessagePort, message: unknown) {
32
+ try {
33
+ port.postMessage(message);
34
+ } catch {
35
+ // Port closed (tab gone); it'll be dropped on the next failed send.
36
+ }
37
+ }
38
+
39
+ function main() {
40
+ if (isNode()) return;
41
+ (self as unknown as { onconnect: (e: MessageEvent) => void }).onconnect = (event: MessageEvent) => {
42
+ let port = (event as unknown as { ports: MessagePort[] }).ports[0];
43
+ port.onmessage = (ev: MessageEvent) => {
44
+ let msg = ev.data as { type: string; collection: string } & RemoteWrite;
45
+ let c = getCollection(msg.collection);
46
+ if (msg.type === "hello") {
47
+ c.ports.add(port);
48
+ prune(c);
49
+ port.postMessage({ type: "recent", collection: msg.collection, writes: [...c.recent.values()] });
50
+ } else if (msg.type === "write") {
51
+ prune(c);
52
+ let existing = c.recent.get(msg.key);
53
+ if (!existing || msg.time > existing.time) {
54
+ c.recent.set(msg.key, { key: msg.key, time: msg.time, deleted: msg.deleted, value: msg.value });
55
+ }
56
+ for (let other of c.ports) {
57
+ if (other !== port) post(other, msg);
58
+ }
59
+ }
60
+ };
61
+ port.start();
62
+ };
63
+ }
64
+
65
+ main();
@@ -129,6 +129,14 @@ export declare const getFileStorageNested: {
129
129
  getAllKeys(): string[];
130
130
  get(key: string): Promise<FileStorage> | undefined;
131
131
  };
132
+ export declare const getFileStorageNested2: {
133
+ (key: string): Promise<FileStorage>;
134
+ clear(key: string): void;
135
+ clearAll(): void;
136
+ forceSet(key: string, value: Promise<FileStorage>): void;
137
+ getAllKeys(): string[];
138
+ get(key: string): Promise<FileStorage> | undefined;
139
+ };
132
140
  export declare const getFileStorage: {
133
141
  (): Promise<FileStorage>;
134
142
  reset(): void;
@@ -379,6 +379,33 @@ export const getFileStorageNested = cache(async function getFileStorage(path: st
379
379
  }
380
380
  return wrapHandle(base);
381
381
  });
382
+ // Supports if the user selects the folder that contains the data folder or the data folder directly. If pathStr is an absolute path (and we're in nodejs) we use it directly.
383
+ export const getFileStorageNested2 = cache(async function getFileStorage(pathStr: string): Promise<FileStorage> {
384
+ let base: DirectoryWrapper;
385
+ pathStr = pathStr.replaceAll("\\", "/");
386
+ if (isNode()) {
387
+ if (path.isAbsolute(pathStr)) {
388
+ return wrapHandle(new NodeJSDirectoryHandleWrapper(pathStr));
389
+ }
390
+ base = new NodeJSDirectoryHandleWrapper(path.resolve("./data/"));
391
+ } else {
392
+ base = await getDirectoryHandle();
393
+ let dirs: string[] = [];
394
+ for await (const [name, entry] of base) {
395
+ if (entry.kind === "directory") {
396
+ dirs.push(name);
397
+ }
398
+ }
399
+ if (dirs.includes(".git") || dirs.includes("data")) {
400
+ base = await base.getDirectoryHandle("data", { create: true });
401
+ }
402
+ }
403
+ for (let part of pathStr.split("/")) {
404
+ if (!part) continue;
405
+ base = await base.getDirectoryHandle(part, { create: true });
406
+ }
407
+ return wrapHandle(base);
408
+ });
382
409
  export const getFileStorage = lazy(async function getFileStorage(): Promise<FileStorage> {
383
410
  if (USE_INDEXED_DB) {
384
411
  return await getFileStorageIndexDB();
package/yarn.lock CHANGED
@@ -1940,10 +1940,10 @@ slash@^3.0.0:
1940
1940
  resolved "https://registry.yarnpkg.com/slash/-/slash-3.0.0.tgz#6539be870c165adbd5240220dbe361f1bc4d4634"
1941
1941
  integrity sha512-g9Q1haeby36OSStwb4ntCGGGaKsaVSjQ68fBxoQcutl5fS1vuY18H3wSt3jFyFtrkx+Kz0V1G85A4MyAdDMi2Q==
1942
1942
 
1943
- socket-function@^1.1.30:
1944
- version "1.1.30"
1945
- resolved "https://registry.yarnpkg.com/socket-function/-/socket-function-1.1.30.tgz#adeb9614996e727518e2abdc7a1aa90544d669cd"
1946
- integrity sha512-EcqNpM8i0icBr2iU80aovXI05aUBrB4P4e9U7EQyju68TGAMagdA4OQXi+V1MHUipH/3nIdw3UAVYv+kFwZwnQ==
1943
+ socket-function@^1.1.38:
1944
+ version "1.1.38"
1945
+ resolved "https://registry.yarnpkg.com/socket-function/-/socket-function-1.1.38.tgz#8923a1db19480d5828d9bde9975eabac505698ce"
1946
+ integrity sha512-LJFeVUaXT8D8egyKr7fZajq7ctmLUN58Uih6GHx3/pfXV3pDFfJS0+XJRqsk0udn0II1ZqozEz242khs4fLCKQ==
1947
1947
  dependencies:
1948
1948
  "@types/pako" "^2.0.3"
1949
1949
  "@types/ws" "^8.5.3"