Skip to content

Commit 1b7e9be

Browse files
committed
switch to localforage
1 parent c9241fa commit 1b7e9be

File tree

6 files changed

+66
-30
lines changed

6 files changed

+66
-30
lines changed

package-lock.json

Lines changed: 22 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/frame/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"@tiptap/react": "^2.0.4",
1313
"@floating-ui/react": "^0.25.1",
1414
"@syncedstore/yjs-reactive-bindings": "^0.5.1",
15+
"localforage":"^1.10.0",
1516
"lz-string": "^1.4.4",
1617
"markdown-it": "^12.0.2",
1718
"monaco-editor": "^0.35.0",
Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,63 @@
1+
import * as localForage from "localforage";
12
import lzstring from "lz-string";
23
import { compress, decompress } from "./compress";
34

45
const SUPPORTS_COMPRESSIONSTREAM = "CompressionStream" in window;
5-
const SEPARATOR = "-=-^-=-";
66

77
export class CompressedCache {
88
constructor(private readonly cacheTimeout: number = 604800000 /* 1 week*/) {}
99

1010
async getItem(key: string): Promise<string | undefined> {
11-
const cached = localStorage.getItem(key);
11+
const cached = await localForage.getItem<{
12+
date: string;
13+
format: string;
14+
data: string | ArrayBuffer
15+
}>(key);
1216
if (!cached) {
1317
return undefined;
1418
}
1519

16-
const [dateString, format, text] = cached.split("-=-^-=-");
17-
const cachedDate = new Date(dateString);
20+
// const [dateString, format, text] = cached.split("-=-^-=-");
21+
const cachedDate = new Date(cached.date);
1822
const now = new Date();
1923

2024
if (now.getTime() - cachedDate.getTime() > this.cacheTimeout) {
2125
// timed out
2226
return undefined;
2327
}
24-
if (format === "lz") {
25-
return lzstring.decompressFromUTF16(text);
28+
if (cached.format === "lz") {
29+
return lzstring.decompressFromUTF16(cached.data as string);
2630
}
2731

28-
if (format === "csgzip" && SUPPORTS_COMPRESSIONSTREAM) {
29-
return (
30-
await decompress(new Blob([text], { type: "text/plain" }))
31-
).text();
32+
if (cached.format === "csgzip" && SUPPORTS_COMPRESSIONSTREAM) {
33+
const dc = await decompress(cached.data as ArrayBuffer)
34+
return dc;
3235
}
3336
return undefined;
3437
}
3538

3639
async setItem(key: string, value: string) {
3740
const now = new Date();
3841

39-
let compressed: string;
42+
let compressed: string | ArrayBuffer;
4043
let format: string;
4144

4245
if (SUPPORTS_COMPRESSIONSTREAM) {
4346
format = "csgzip";
44-
compressed = await (
45-
await compress(new Blob([value], { type: "text/plain" }))
46-
).text();
47+
compressed =
48+
await compress(value)
49+
4750
} else {
4851
format = "lz";
4952
compressed = lzstring.compressToUTF16(value);
5053
}
5154

52-
const cacheContent = `${now.toISOString()}${SEPARATOR}${format}${SEPARATOR}${compressed}`;
53-
localStorage.setItem(key, cacheContent);
55+
const cacheContent = {
56+
date: now.toISOString(),
57+
format,
58+
data: compressed
59+
}
60+
61+
localForage.setItem(key, cacheContent);
5462
}
5563
}

packages/frame/src/runtime/editor/languages/typescript/compress.browsertest.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { compress, decompress } from "./compress";
33

44
it("compresses / decompressess", async () => {
55
const str = "hello world ấɖḯƥĭṩčįɳġ ḝłįʈ 🥳";
6-
const compressed = await compress(new Blob([str], { type: "text/plain" }));
7-
const decompressed = await (await decompress(compressed)).text();
6+
const compressed = await compress(str);
7+
const decompressed = await (await decompress(compressed));
88
expect(decompressed).to.eq(str);
99
});
Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
/* eslint-disable @typescript-eslint/ban-ts-comment */
2-
export function compress(blob: Blob) {
3-
const compressedReadableStream = blob
4-
.stream()
5-
// @ts-ignore
6-
.pipeThrough(new CompressionStream("deflate"));
7-
return new Response(compressedReadableStream).blob();
2+
export async function compress(text: string) {
3+
const byteArray = new TextEncoder().encode(text)
4+
const cs = new CompressionStream("deflate")
5+
const writer = cs.writable.getWriter()
6+
writer.write(byteArray)
7+
writer.close()
8+
const ret = await new Response(cs.readable).arrayBuffer()
9+
return ret;
810
}
911

10-
export async function decompress(blob: Blob) {
11-
// @ts-ignore
12-
const ds = new DecompressionStream("deflate");
13-
const decompressedStream = blob.stream().pipeThrough(ds);
14-
return new Response(decompressedStream).blob();
12+
export async function decompress(bytes: ArrayBuffer) {
13+
const cs = new DecompressionStream("deflate")
14+
const writer = cs.writable.getWriter()
15+
writer.write(bytes)
16+
writer.close()
17+
const arrayBuffer = await new Response(cs.readable).arrayBuffer()
18+
return new TextDecoder().decode(arrayBuffer)
1519
}

packages/frame/src/runtime/editor/languages/typescript/typeAcquisition.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,11 +410,12 @@ const getModuleAndRootDefTypePath = async (
410410
};
411411

412412
const getCachedDTSString = async (config: ATAConfig, url: string) => {
413+
413414
const cached = await cache.getItem(url);
414415
if (cached) {
415416
return cached;
416417
}
417-
418+
418419
const response = await config.fetcher(url);
419420

420421
if (!response.ok) {

0 commit comments

Comments
 (0)