Skip to content

Commit ed8d61a

Browse files
authored
add createPngUrl and createBrowserPreviewUrl, make gzip deterministic by fixing time (#3)
* add createPngUrl * add createPngUrl and createBrowserPreviewUrl * format * update snapshots * add format check * update snapshots * remove gzip time component variable
1 parent 4cc87b7 commit ed8d61a

File tree

5 files changed

+33
-7
lines changed

5 files changed

+33
-7
lines changed

lib/index.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ import { base64ToBytes, bytesToBase64 } from "./bytesToBase64"
33

44
export function getCompressedBase64SnippetString(text: string) {
55
// Compress the input string
6-
const compressedData = gzipSync(strToU8(text))
6+
const compressedData = gzipSync(strToU8(text.trim()), {
7+
mtime: 1739063707691, // Date.now() when i wrote this line, ensures consistent output
8+
})
79

810
// Convert to base64
911
const base64Data = bytesToBase64(compressedData)
@@ -12,7 +14,7 @@ export function getCompressedBase64SnippetString(text: string) {
1214
}
1315

1416
export function getBase64PoundSnippetString(text: string) {
15-
const base64Data = getCompressedBase64SnippetString(text)
17+
const base64Data = getCompressedBase64SnippetString(text.trim())
1618
return `#data:application/gzip;base64,${base64Data}`
1719
}
1820

@@ -33,6 +35,22 @@ export function createSvgUrl(
3335
return `https://svg.tscircuit.com/?svg_type=${svgType}&code=${encodeURIComponent(base64Data)}`
3436
}
3537

38+
export function createBrowserPreviewUrl(
39+
tscircuitCode: string,
40+
view?: "pcb" | "schematic" | "3d",
41+
) {
42+
const base64Data = getCompressedBase64SnippetString(tscircuitCode)
43+
return `https://browser-preview.tscircuit.com/?view=${view}&code=${encodeURIComponent(base64Data)}`
44+
}
45+
46+
export function createPngUrl(
47+
tscircuitCode: string,
48+
view: "pcb" | "schematic" | "3d",
49+
) {
50+
const base64Data = getCompressedBase64SnippetString(tscircuitCode)
51+
return `https://png.tscircuit.com/?view=${view}&code=${encodeURIComponent(base64Data)}`
52+
}
53+
3654
export function createSnippetUrl(text: string, snippet_type?: string): string {
3755
// Construct the URL
3856
const typeParam = snippet_type ? `&snippet_type=${snippet_type}` : ""

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
"version": "0.0.7",
55
"type": "module",
66
"scripts": {
7-
"build": "tsup-node lib/index.ts --dts --format esm"
7+
"build": "tsup-node lib/index.ts --dts --format esm",
8+
"format": "biome format --write .",
9+
"format:check": "biome format ."
810
},
911
"devDependencies": {
1012
"@biomejs/biome": "^1.9.4",

tests/test1-basic-url.test.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import { test, expect } from "bun:test"
2-
import { createSnippetUrl, getUncompressedSnippetString, getBase64PoundSnippetString } from "lib"
2+
import {
3+
createSnippetUrl,
4+
getUncompressedSnippetString,
5+
getBase64PoundSnippetString,
6+
} from "lib"
37

48
declare module "bun:test" {
59
interface Matchers<T = unknown> {
@@ -16,5 +20,7 @@ export default () => (
1620
)
1721
`)
1822

19-
expect(url).toMatchInlineSnapshot(`"https://tscircuit.com/editor?#data:application/gzip;base64,H4sIAMaEkmcAAy2NTQ7CIBhE95xiwqpdtVWXhUO4ckuBClF+Ap/RxHh30Xb3JvMyw+wrp0IwdlWPO6HrISQ6BsxLUsXg6Q05wacxBA5n/dXRnmSTmlZs9ZVSwQYqatuEG8eaEuXiY/PH03jgiCq06jxxVO0u4n38IOtlg+G3Ng//T8l69gUyPWjAmAAAAA=="`)
23+
expect(url).toMatchInlineSnapshot(
24+
`"https://tscircuit.com/editor?#data:application/gzip;base64,H4sIAJsBqGcAAy2NTQ7CIBhE9z3FhFW7KlWXhUO4ckuBClF+Ap/RxHh30Xb3JvMyY185FYKxq3rcCf0AIdF3wLwkVQye3pATbOIhMDjrr472JJvUtGKrr5QKNlBR2ybcGNaUKBcfm89P/MAQVWjVeWKo2l3E+/hB1ssG429tHv+fshu+lSYxzJYAAAA="`,
25+
)
2026
})

tests/test2-decompression.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,5 @@ pcbX={3} />
1717

1818
const compressed = getBase64PoundSnippetString(originalText)
1919
const uncompressed = getUncompressedSnippetString(compressed)
20-
expect(uncompressed).toEqual(originalText)
20+
expect(uncompressed).toEqual(originalText.trim())
2121
})

tests/test3-svg-url.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@ export default () => (
1414
)
1515

1616
expect(url).toMatchInlineSnapshot(
17-
`"https://svg.tscircuit.com/?svg_type=pcb&code=H4sIAN2jkmcAAy2NTQ7CIBhE95xiwqpdtVWXhUO4ckuBClF%2BAp%2FRxHh30Xb3JvMyw%2Bwrp0IwdlWPO6HrISQ6BsxLUsXg6Q05wacxBA5n%2FdXRnmSTmlZs9ZVSwQYqatuEG8eaEuXiY%2FPH03jgiCq06jxxVO0u4n38IOtlg%2BG3Ng%2F%2FT8l69gUyPWjAmAAAAA%3D%3D"`,
17+
`"https://svg.tscircuit.com/?svg_type=pcb&code=H4sIAJsBqGcAAy2NTQ7CIBhE9z3FhFW7KlWXhUO4ckuBClF%2BAp%2FRxHh30Xb3JvMyY185FYKxq3rcCf0AIdF3wLwkVQye3pATbOIhMDjrr472JJvUtGKrr5QKNlBR2ybcGNaUKBcfm89P%2FMAQVWjVeWKo2l3E%2B%2FhB1ssG429tHv%2Bfshu%2BlSYxzJYAAAA%3D"`,
1818
)
1919
})

0 commit comments

Comments
 (0)