Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: update ts #137

Merged
merged 2 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ See also [example](./test/compress_using_dict.test.ts)
Initialize module.
Please specify path to `zstd.wasm` without bunders on browser.

### compress(buffer: ArrayBuffer, compressionLevel?: number): ArrayBuffer
### compress(buffer: Uint8Array, compressionLevel?: number): Uint8Array

- buffer: data to compress.
- compressionLevel: (optional) compression level, default value is 3
Expand All @@ -189,7 +189,7 @@ Please specify path to `zstd.wasm` without bunders on browser.
const compressed = compress(buffer, 10);
```

### decompress(buffer: ArrayBuffer): ArrayBuffer
### decompress(buffer: Uint8Array): Uint8Array

- buffer: data to decompress.

Expand All @@ -199,7 +199,7 @@ const compressed = compress(buffer, 10);
const decompressed = decompress(buffer);
```

### compressUsingDict(cctx: number, buffer: ArrayBuffer, dict: ArrayBuffer, compressionLevel?: number): ArrayBuffer
### compressUsingDict(cctx: number, buffer: Uint8Array, dict: Uint8Array, compressionLevel?: number): Uint8Array

- cctx: a pointer to compress context. please create cctx with `createCCtx`.
- buffer: data to compress.
Expand All @@ -217,7 +217,7 @@ const compressed = compressUsingDict(createCCtx(), buffer, dict, 10);

- create a pointer to compress context.

### decompressUsingDict(dctx: number, dict: ArrayBuffer): ArrayBuffer
### decompressUsingDict(dctx: number, dict: Uint8Array): Uint8Array

- dctx: a pointer to decompress context. please create cctx with `createDCtx`.
- buffer: data to decompress.
Expand Down
124 changes: 37 additions & 87 deletions deno/zstd.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@

import { decodeBase64 } from "https://deno.land/std/encoding/base64.ts"
import { wasm } from "./zstd.encoded.wasm.ts"
import { decodeBase64 } from 'https://deno.land/std/encoding/base64.ts';
import { wasm } from './zstd.encoded.wasm.ts';
import Module from './zstd.deno.js';

const initialized = (() =>
Expand All @@ -14,9 +13,8 @@ export const init = async () => {
await initialized;
};


export const isError = (code: number): number => {
const _isError = Module["_ZSTD_isError"];
const _isError = Module['_ZSTD_isError'];
return _isError(code);
};

Expand All @@ -26,35 +24,29 @@ export const isError = (code: number): number => {
// return _getErrorName(code);
// };


const compressBound = (size: number): number => {
const bound = Module["_ZSTD_compressBound"];
const bound = Module['_ZSTD_compressBound'];
return bound(size);
};

export const createCCtx = (): number => {
return Module["_ZSTD_createCCtx"]();
return Module['_ZSTD_createCCtx']();
};

export const freeCCtx = (cctx: number) => {
return Module["_ZSTD_freeCCtx"](cctx);
return Module['_ZSTD_freeCCtx'](cctx);
};

export const compressUsingDict = (
cctx: number,
buf: ArrayBuffer,
dict: ArrayBuffer,
level?: number
) => {
export const compressUsingDict = (cctx: number, buf: Uint8Array, dict: Uint8Array, level?: number) => {
const bound = compressBound(buf.byteLength);
const malloc = Module["_malloc"];
const malloc = Module['_malloc'];
const compressed = malloc(bound);
const src = malloc(buf.byteLength);
Module.HEAP8.set(buf, src);
// Setup dict
const pdict = malloc(dict.byteLength);
Module.HEAP8.set(dict, pdict);
const free = Module["_free"];
const free = Module['_free'];
try {
/*
@See https://zstd.docsforge.com/dev/api/ZSTD_compress_usingDict/
Expand All @@ -64,27 +56,14 @@ export const compressUsingDict = (
const void* dict, size_t dictSize,
int compressionLevel)
*/
const _compress = Module["_ZSTD_compress_usingDict"];
const sizeOrError = _compress(
cctx,
compressed,
bound,
src,
buf.byteLength,
pdict,
dict.byteLength,
level ?? 3
);
const _compress = Module['_ZSTD_compress_usingDict'];
const sizeOrError = _compress(cctx, compressed, bound, src, buf.byteLength, pdict, dict.byteLength, level ?? 3);
if (isError(sizeOrError)) {
throw new Error(`Failed to compress with code ${sizeOrError}`);
}
// // Copy buffer
// // Uint8Array.prototype.slice() return copied buffer.
const data = new Uint8Array(
Module.HEAPU8.buffer,
compressed,
sizeOrError
).slice();
const data = new Uint8Array(Module.HEAPU8.buffer, compressed, sizeOrError).slice();
free(compressed, bound);
free(src, buf.byteLength);
free(pdict, dict.byteLength);
Expand All @@ -97,14 +76,13 @@ export const compressUsingDict = (
}
};


export const compress = (buf: ArrayBuffer, level?: number) => {
export const compress = (buf: Uint8Array, level?: number) => {
const bound = compressBound(buf.byteLength);
const malloc = Module["_malloc"];
const malloc = Module['_malloc'];
const compressed = malloc(bound);
const src = malloc(buf.byteLength);
Module.HEAP8.set(buf, src);
const free = Module["_free"];
const free = Module['_free'];
try {
/*
@See https://zstd.docsforge.com/dev/api/ZSTD_compress/
Expand All @@ -114,24 +92,14 @@ export const compress = (buf: ArrayBuffer, level?: number) => {
@return : compressed size written into `dst` (<= `dstCapacity),
or an error code if it fails (which can be tested using ZSTD_isError()).
*/
const _compress = Module["_ZSTD_compress"];
const sizeOrError = _compress(
compressed,
bound,
src,
buf.byteLength,
level ?? 3
);
const _compress = Module['_ZSTD_compress'];
const sizeOrError = _compress(compressed, bound, src, buf.byteLength, level ?? 3);
if (isError(sizeOrError)) {
throw new Error(`Failed to compress with code ${sizeOrError}`);
}
// // Copy buffer
// // Uint8Array.prototype.slice() return copied buffer.
const data = new Uint8Array(
Module.HEAPU8.buffer,
compressed,
sizeOrError
).slice();
const data = new Uint8Array(Module.HEAPU8.buffer, compressed, sizeOrError).slice();
free(compressed, bound);
free(src, buf.byteLength);
return data;
Expand All @@ -142,56 +110,43 @@ export const compress = (buf: ArrayBuffer, level?: number) => {
}
};


const getFrameContentSize = (src: number, size: number): number => {
const getSize = Module["_ZSTD_getFrameContentSize"];
const getSize = Module['_ZSTD_getFrameContentSize'];
return getSize(src, size);
};

export const createDCtx = (): number => {
return Module["_ZSTD_createDCtx"]();
return Module['_ZSTD_createDCtx']();
};

export const freeDCtx = (dctx: number) => {
return Module["_ZSTD_freeDCtx"](dctx);
return Module['_ZSTD_freeDCtx'](dctx);
};

export const decompressUsingDict = (
dctx: number,
buf: ArrayBuffer,
dict: ArrayBuffer,
opts: DecompressOption = { defaultHeapSize: 1024 * 1024 } // Use 1MB on default if it is failed to get content size.
): ArrayBuffer => {
const malloc = Module["_malloc"];
buf: Uint8Array,
dict: Uint8Array,
opts: DecompressOption = { defaultHeapSize: 1024 * 1024 }, // Use 1MB on default if it is failed to get content size.
): Uint8Array => {
const malloc = Module['_malloc'];
const src = malloc(buf.byteLength);
Module.HEAP8.set(buf, src);
const pdict = malloc(dict.byteLength);
Module.HEAP8.set(dict, pdict);
const contentSize = getFrameContentSize(src, buf.byteLength);
const size = contentSize === -1 ? opts.defaultHeapSize : contentSize;
const free = Module["_free"];
const free = Module['_free'];
const heap = malloc(size);
try {
const _decompress = Module["_ZSTD_decompress_usingDict"];
const sizeOrError = _decompress(
dctx,
heap,
size,
src,
buf.byteLength,
pdict,
dict.byteLength
);
const _decompress = Module['_ZSTD_decompress_usingDict'];
const sizeOrError = _decompress(dctx, heap, size, src, buf.byteLength, pdict, dict.byteLength);
if (isError(sizeOrError)) {
throw new Error(`Failed to compress with code ${sizeOrError}`);
}
// Copy buffer
// Uint8Array.prototype.slice() return copied buffer.
const data = new Uint8Array(
Module.HEAPU8.buffer,
heap,
sizeOrError
).slice();
const data = new Uint8Array(Module.HEAPU8.buffer, heap, sizeOrError).slice();
free(heap, size);
free(src, buf.byteLength);
free(pdict, dict.byteLength);
Expand All @@ -204,21 +159,20 @@ export const decompressUsingDict = (
}
};


export type DecompressOption = {
defaultHeapSize?: number;
};

export const decompress = (
buf: ArrayBuffer,
opts: DecompressOption = { defaultHeapSize: 1024 * 1024 } // Use 1MB on default if it is failed to get content size.
): ArrayBuffer => {
const malloc = Module["_malloc"];
buf: Uint8Array,
opts: DecompressOption = { defaultHeapSize: 1024 * 1024 }, // Use 1MB on default if it is failed to get content size.
): Uint8Array => {
const malloc = Module['_malloc'];
const src = malloc(buf.byteLength);
Module.HEAP8.set(buf, src);
const contentSize = getFrameContentSize(src, buf.byteLength);
const size = contentSize === -1 ? opts.defaultHeapSize : contentSize;
const free = Module["_free"];
const free = Module['_free'];
const heap = malloc(size);
try {
/*
Expand All @@ -228,18 +182,14 @@ export const decompress = (
If user cannot imply a maximum upper bound, it's better to use streaming mode to decompress data.
@return: the number of bytes decompressed into dst (<= dstCapacity), or an errorCode if it fails (which can be tested using ZSTD_isError()).
*/
const _decompress = Module["_ZSTD_decompress"];
const _decompress = Module['_ZSTD_decompress'];
const sizeOrError = _decompress(heap, size, src, buf.byteLength);
if (isError(sizeOrError)) {
throw new Error(`Failed to compress with code ${sizeOrError}`);
}
// Copy buffer
// Uint8Array.prototype.slice() return copied buffer.
const data = new Uint8Array(
Module.HEAPU8.buffer,
heap,
sizeOrError
).slice();
const data = new Uint8Array(Module.HEAPU8.buffer, heap, sizeOrError).slice();
free(heap, size);
free(src, buf.byteLength);
return data;
Expand Down
2 changes: 1 addition & 1 deletion examples/vite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"devDependencies": {
"@rollup/pluginutils": "^5.1.0",
"buffer": "^6.0.3",
"typescript": "^5.5.3",
"typescript": "^5.7",
"vite": "^5.4.1",
"vite-plugin-wasm": "^3.3.0",
"@bokuweb/zstd-wasm": "0.0.21-alpha.5"
Expand Down
2 changes: 1 addition & 1 deletion lib/simple/compress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const compressBound = (size: number): number => {
return bound(size);
};

export const compress = (buf: ArrayBuffer, level?: number) => {
export const compress = (buf: Uint8Array, level?: number): Uint8Array => {
const bound = compressBound(buf.byteLength);
const malloc = Module['_malloc'];
const compressed = malloc(bound);
Expand Down
2 changes: 1 addition & 1 deletion lib/simple/compress_using_dict.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export const freeCCtx = (cctx: number) => {
return Module['_ZSTD_freeCCtx'](cctx);
};

export const compressUsingDict = (cctx: number, buf: ArrayBuffer, dict: ArrayBuffer, level?: number) => {
export const compressUsingDict = (cctx: number, buf: Uint8Array, dict: Uint8Array, level?: number): Uint8Array => {
const bound = compressBound(buf.byteLength);
const malloc = Module['_malloc'];
const compressed = malloc(bound);
Expand Down
4 changes: 2 additions & 2 deletions lib/simple/decompress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ export type DecompressOption = {
};

export const decompress = (
buf: ArrayBuffer,
buf: Uint8Array,
opts: DecompressOption = { defaultHeapSize: 1024 * 1024 }, // Use 1MB on default if it is failed to get content size.
): ArrayBuffer => {
): Uint8Array => {
const malloc = Module['_malloc'];
const src = malloc(buf.byteLength);
Module.HEAP8.set(buf, src);
Expand Down
6 changes: 3 additions & 3 deletions lib/simple/decompress_using_dict.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ export const freeDCtx = (dctx: number) => {

export const decompressUsingDict = (
dctx: number,
buf: ArrayBuffer,
dict: ArrayBuffer,
buf: Uint8Array,
dict: Uint8Array,
opts: DecompressOption = { defaultHeapSize: 1024 * 1024 }, // Use 1MB on default if it is failed to get content size.
): ArrayBuffer => {
): Uint8Array => {
const malloc = Module['_malloc'];
const src = malloc(buf.byteLength);
Module.HEAP8.set(buf, src);
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@bokuweb/zstd-wasm",
"version": "0.0.21-alpha.5",
"version": "0.0.22",
"repository": "ssh://[email protected]/bokuweb/zstd-wasm.git",
"author": "<[email protected]>",
"license": "MIT",
Expand Down Expand Up @@ -44,7 +44,7 @@
"prettier": "3.2.5",
"text-encoding": "0.7.0",
"ts-jest": "28.0.8",
"typescript": "5.4.5"
"typescript": "5.7"
},
"dependencies": {},
"exports": {
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2505,10 +2505,10 @@ type-fest@^0.21.3:
resolved "https://registry.yarnpkg.com/type-fest/-/type-fest-0.21.3.tgz#d260a24b0198436e133fa26a524a6d65fa3b2e37"
integrity sha512-t0rzBq87m3fVcduHDUFhKmyyX+9eo6WQjZvf51Ea/M0Q7+T374Jp1aUiyUl0GKxp8M/OETVHSDvmkyPgvX+X2w==

typescript@5.4.5:
version "5.4.5"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-5.4.5.tgz#42ccef2c571fdbd0f6718b1d1f5e6e5ef006f611"
integrity sha512-vcI4UpRgg81oIRUFwR0WSIHKt11nJ7SAVlYNIu+QpqeyXP+gpQJy/Z4+F0aGxSE4MqwjyXvW/TzgkLAx2AGHwQ==
typescript@5.7:
version "5.7.2"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-5.7.2.tgz#3169cf8c4c8a828cde53ba9ecb3d2b1d5dd67be6"
integrity sha512-i5t66RHxDvVN40HfDd1PsEThGNnlMCMT3jMUuoh9/0TaqWevNontacunWyN02LA9/fIbEWlcHZcgTKb9QoaLfg==

undici-types@~5.26.4:
version "5.26.5"
Expand Down