@rescale/slim
v1.0.1
Published
`Slim` is a compression suite for javascript objects. It is designed to be used in the browser and in Node.js envs.
Downloads
26
Maintainers
Readme
@rescale/slim
Slim
is a compression suite for javascript objects. It is designed to be used in the browser and in Node.js envs.
Installation
npm install @rescale/slim
pnpm add @rescale/slim
bun add @rescale/slim
Docs
Demo
Usage
React
import { inflate, deflate } from '@rescale/slim';
const data = { a: 1, b: 2, c: 3 };
const compressed = deflate(data);
const decompressed = inflate(compressed);
export function App() {
return (
<div>
<h1>Compressed data</h1>
<pre>{compressed}</pre>
<h1>Decompressed data</h1>
<pre>{JSON.stringify(decompressed, null, 2)}</pre>
</div>
);
}
Node.js
import { inflate, deflate } from '@rescale/slim';
const data = { a: 1, b: 2, c: 3 };
const compressed = deflate(data);
const decompressed = inflate(compressed);
console.log('Compressed data:', compressed);
console.log('Decompressed data:', decompressed);
Next.js
/app/page.tsx
import { inflate, deflate } from '@rescale/slim';
const data = { a: 1, b: 2, c: 3 };
const compressed = deflate(data);
const _decompressed = inflate(compressed);
export default async function Page() {
const { decompressed } = await fetch(
`/api/data?compressed=${compressed}`,
).then((res) => res.json());
return (
<div>
<h1>Compressed data</h1>
<pre>{compressed}</pre>
<h1>compatibility</h1>
<pre>
{String(JSON.stringify(_decompressed) === JSON.stringify(decompressed))}
</pre>
</div>
);
}
/app/api/data/route.ts
import { type NextRequest, NextResponse } from 'next/server';
import { inflate, deflate } from '@rescale/slim';
export function GET(request: NextRequest) {
const compressed = request.nextUrl.searchParams.get('compressed');
return NextResponse.json({
decompressed: inflate(compressed),
});
}
Motivation
We've encountered many problems with URL link length when passing filters in params and the size of the data we send to the server. We've tried many libraries and none of them were good enough for our needs. So we decided to create our own library based on PAKO zlib port.