forked from mermaid-js/mermaid
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request mermaid-js#604 from mermaid-js/sidv/compress
Compressed URL
- Loading branch information
Showing
11 changed files
with
131 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"editor.formatOnSave": true, | ||
"cSpell.words": ["pako", "Serde", "serdes"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
export const rendererUrl = import.meta.env.MERMAID_RENDERER_URL ?? 'https://mermaid.ink'; | ||
export const krokiRendererUrl = import.meta.env.MERMAID_KROKI_RENDERER_URL ?? 'https://kroki.io'; | ||
export const rendererUrl: string = | ||
(import.meta.env.MERMAID_RENDERER_URL as string) ?? 'https://mermaid.ink'; | ||
export const krokiRendererUrl: string = | ||
(import.meta.env.MERMAID_KROKI_RENDERER_URL as string) ?? 'https://kroki.io'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { deflate, inflate } from 'pako'; | ||
import { toUint8Array, fromUint8Array, toBase64, fromBase64 } from 'js-base64'; | ||
import type { State } from '$lib/types'; | ||
|
||
interface Serde { | ||
serialize: (state: string) => string; | ||
deserialize: (state: string) => string; | ||
} | ||
|
||
const base64Serde: Serde = { | ||
serialize: (state: string): string => { | ||
return toBase64(state, true); | ||
}, | ||
deserialize: (state: string): string => { | ||
return fromBase64(state); | ||
} | ||
}; | ||
|
||
export const pakoSerde: Serde = { | ||
serialize: (state: string): string => { | ||
const data = new TextEncoder().encode(state); | ||
const compressed = deflate(data, { level: 9 }); | ||
return fromUint8Array(compressed, true); | ||
}, | ||
deserialize: (state: string): string => { | ||
const data = toUint8Array(state); | ||
return inflate(data, { to: 'string' }); | ||
} | ||
}; | ||
|
||
const serdes: { [key: string]: Serde } = { | ||
base64: base64Serde, | ||
pako: pakoSerde | ||
}; | ||
|
||
type SerdeType = keyof typeof serdes; | ||
|
||
export const serializeState = (state: State): string => { | ||
const json = JSON.stringify(state); | ||
const defaultSerde: SerdeType = 'pako'; | ||
const serialized = serdes[defaultSerde].serialize(json); | ||
return `${defaultSerde}:${serialized}`; | ||
}; | ||
|
||
export const deserializeState = (state: string): State => { | ||
let type: SerdeType, serialized: string; | ||
if (state.includes(':')) { | ||
let tempType: string; | ||
[tempType, serialized] = state.split(':'); | ||
if (tempType in serdes) { | ||
type = tempType; | ||
} else { | ||
throw new Error(`Unknown serde type: ${tempType}`); | ||
} | ||
} else { | ||
type = 'base64'; | ||
serialized = state; | ||
} | ||
const json = serdes[type].deserialize(serialized); | ||
return JSON.parse(json) as State; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters