mirror of
https://github.com/NotAShelf/nix-evaluator-stats.git
synced 2026-05-02 14:13:17 +00:00
packages: move sharing logic from web to core; reuse in web
Signed-off-by: NotAShelf <raf@notashelf.dev> Change-Id: I26cb6848615a5788f2196fc1675bc22d6a6a6964
This commit is contained in:
parent
8d7bd7bb05
commit
5482cd9df1
7 changed files with 481 additions and 1142 deletions
|
|
@ -16,5 +16,8 @@
|
|||
},
|
||||
"devDependencies": {
|
||||
"typescript": "^6.0.2"
|
||||
},
|
||||
"dependencies": {
|
||||
"lzutf8": "^0.6.3"
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,2 +1,3 @@
|
|||
export * from './types';
|
||||
export * from './parser';
|
||||
export * from './share';
|
||||
|
|
|
|||
47
packages/core/src/share.ts
Normal file
47
packages/core/src/share.ts
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
import LZUTF8 from 'lzutf8';
|
||||
|
||||
export type ShareState =
|
||||
| { type: 'analysis'; data: Record<string, unknown>; name: string }
|
||||
| {
|
||||
type: 'compare';
|
||||
left: Record<string, unknown>;
|
||||
right: Record<string, unknown>;
|
||||
leftName: string;
|
||||
rightName: string;
|
||||
};
|
||||
|
||||
export function encodeShareUrl(state: ShareState): string {
|
||||
const json = JSON.stringify(state);
|
||||
const encoded = LZUTF8.compress(json, { outputEncoding: 'Base64' }) as string;
|
||||
return encoded.replace(/\+/g, '-').replace(/\//g, '_').replace(/=/g, '');
|
||||
}
|
||||
|
||||
export function decodeShareUrl(encoded: string): ShareState | null {
|
||||
try {
|
||||
let base64 = encoded.replace(/-/g, '+').replace(/_/g, '/');
|
||||
const padLength = (4 - (base64.length % 4)) % 4;
|
||||
base64 += '='.repeat(padLength);
|
||||
const json = LZUTF8.decompress(base64, {
|
||||
inputEncoding: 'Base64',
|
||||
outputEncoding: 'String',
|
||||
}) as string | null;
|
||||
if (!json) {
|
||||
return null;
|
||||
}
|
||||
const state = JSON.parse(json) as ShareState;
|
||||
if (state.type === 'analysis') {
|
||||
if (!state.data || !state.name) {
|
||||
return null;
|
||||
}
|
||||
} else if (state.type === 'compare') {
|
||||
if (!state.left || !state.right || !state.leftName || !state.rightName) {
|
||||
return null;
|
||||
}
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
return state;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue