Skip to content

Commit

Permalink
refactor: Use @std/tar instead of @std/archive due to deprecation
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangfuxing committed Nov 5, 2024
1 parent 9e85873 commit e8c5529
Show file tree
Hide file tree
Showing 15 changed files with 470 additions and 139 deletions.
16 changes: 10 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,13 @@ If you want to read and write files, you need the following [permissions](https:

> --allow-read --allow-write
## `tar`

For tar (un)compression, Deno v1.2.2+ is required. The reason can be seen here:

> <https://github.com/denoland/deno/pull/6905>
### Definition

v0.5.0
Use `@std/tar` instead of `@std/archive` due to deprecation
If you want to use the old API, you can do it
> import { tar } from "jsr:@deno-library/compress/tar_archive/mod.ts";
```ts
// JSR
import { tar } from "jsr:@deno-library/compress";
Expand Down Expand Up @@ -168,6 +167,11 @@ const decompressed = gunzip(compressed);

## `tgz`

v0.5.0
Use `@std/tar` instead of `@std/archive` due to deprecation
If you want to use the old API, you can do it
> import { tgz } from "jsr:@deno-library/compress/tgz_archive/mod.ts";
### Definition

```ts
Expand Down
9 changes: 9 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
## Changelog

### v0.5.0 - 2024.11.5

- refactor: Use `@std/tar` instead of `@std/archive` due to deprecation
- refactor: use Deno API for gzip file handling

### v0.4.9 - 2024.11.5

- Add symbol documentation

### v0.4.8 - 2024.11.5

- Added support for zip[#14]
Expand Down
7 changes: 7 additions & 0 deletions deflate/deflate_raw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1711,6 +1711,13 @@ function qoutbuf() {
}
}

/**
* Compresses the input data using the DEFLATE algorithm.
*
* @param bytes - The input data as a Uint8Array to be compressed.
* @param level - The compression level (default is DEFAULT_LEVEL).
* @returns A Uint8Array containing the compressed data.
*/
export function deflateRaw(
bytes: Uint8Array,
level: number = DEFAULT_LEVEL,
Expand Down
5 changes: 5 additions & 0 deletions deflate/inflate_raw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -927,6 +927,11 @@ function inflate_internal(buff: number[], off: number, size: number) {
return n;
}

/**
* Decompresses a raw Uint8Array data.
* @param arr - The Uint8Array data to decompress.
* @returns The decompressed Uint8Array data.
*/
export function inflateRaw(arr: Uint8Array): Uint8Array {
let i;
const buff: number[] = [];
Expand Down
2 changes: 1 addition & 1 deletion deno.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "@deno-library/compress",
"version": "0.4.9",
"version": "0.5.0",
"exports": "./mod.ts"
}
16 changes: 16 additions & 0 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions deps.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
export { EventEmitter } from "node:events";
export { Tar } from "jsr:@std/[email protected]/tar";
export { Untar } from "jsr:@std/[email protected]/untar";
export { ensureDir, ensureFile } from "jsr:@std/[email protected]";
export * as path from "jsr:@std/[email protected]";
export { Buffer, copy, readAll, writeAll } from "jsr:@std/[email protected]";
export { crc32, Crc32Stream } from "jsr:@deno-library/[email protected]";
export type { Reader, Writer } from "jsr:@std/[email protected]/types";
export type { Reader, Writer } from "jsr:@std/[email protected]/types";
export { UntarStream, TarStream, type TarStreamInput } from "jsr:@std/[email protected]";
31 changes: 31 additions & 0 deletions gzip/gzip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,11 @@ interface Options {
name?: string;
}

/**
* Generates the header for a GZIP file.
* @param {Options} [options={}] - Optional parameters.
* @returns {Uint8Array} - The byte array of the GZIP file header.
*/
export function getHeader(
options: Options = {},
): Uint8Array {
Expand Down Expand Up @@ -167,6 +172,17 @@ export function getHeader(
return new Uint8Array(out);
}

/**
* @module gzip
* @description This module provides functions to compress and decompress data using the GZIP format.
* @example
* const compressed = gzip(data);
* const decompressed = gunzip(compressed);
* @typedef {Object} Options
* @property {number} [level] - Compression level (default is 6).
* @property {number} [timestamp] - Timestamp for the header.
* @property {string} [name] - Original name of the file.
*/
export function gzip(
bytes: Uint8Array,
options: Options = {},
Expand Down Expand Up @@ -223,6 +239,11 @@ export function gzip(
return new Uint8Array(out);
}

/**
* Decompresses a GZIP formatted byte array.
* @param {Uint8Array} bytes - The byte array to decompress.
* @returns {Uint8Array} - The decompressed byte array.
*/
export function gunzip(bytes: Uint8Array): Uint8Array {
const arr = Array.from(bytes);

Expand Down Expand Up @@ -253,6 +274,11 @@ export function gunzip(bytes: Uint8Array): Uint8Array {
return res;
}

/**
* Checks the validity of the GZIP file header.
* @param {number[]} arr - The array containing GZIP data.
* @throws {string} - Throws an error if the header is invalid.
*/
export function checkHeader(arr: number[]) {
// check the first two bytes for the magic numbers
if (readByte(arr) !== ID1 || readByte(arr) !== ID2) {
Expand Down Expand Up @@ -289,6 +315,11 @@ export function checkHeader(arr: number[]) {
}
}

/**
* Checks the GZIP file's tail for CRC32 checksum and file size.
* @param {number[]} arr - The array containing GZIP data.
* @returns {{ crc32: number, size: number }} - An object containing CRC32 and file size.
*/
export function checkTail(arr: number[]) {
const tail = arr.splice(arr.length - 8);

Expand Down
4 changes: 4 additions & 0 deletions gzip/gzip_stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ import { copy, EventEmitter } from "../deps.ts";
import GzipWriter from "./writer_gzip.ts";
import GunzipWriter from "./writer_gunzip.ts";

/**
* @symbol GzipStream
* @description A class for compressing and uncompressing files using Gzip.
*/
export class GzipStream extends EventEmitter {
constructor() {
super();
Expand Down
9 changes: 9 additions & 0 deletions gzip/writer_gunzip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@ interface Options {
onceSize?: number;
}

/**
* @module writer_gunzip
* @description This module provides a Writer class for handling GZIP decompression and writing to a file.
* @example
* const writer = new Writer('output.txt', { onceSize: 2048 });
* await writer.setup();
* await writer.write(data);
* writer.close();
*/
export default class Writer extends EventEmitter implements StdWriter {
protected writer!: File;
protected bytesWritten = 0; // readed size of reader
Expand Down
12 changes: 12 additions & 0 deletions gzip/writer_gzip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,18 @@ interface Options {
onceSize?: number;
}

/**
* @class Writer
* @extends EventEmitter
* @description A class for writing GZIP compressed data to a file.
* @param {string} path - The file path where the data will be written.
* @param {Options} [options] - Optional settings for the writer.
* @property {number} onceSize - The size threshold for writing chunks.
* @property {number} bytesWritten - The total number of bytes written.
* @method setup - Prepares the writer by opening the file and writing headers.
* @method write - Writes a chunk of data to the file, compressing it if necessary.
* @method close - Closes the writer and emits the total bytes written.
*/
export default class Writer extends EventEmitter implements StdWriter {
private writer!: File;
private bytesWritten = 0;
Expand Down
Loading

0 comments on commit e8c5529

Please sign in to comment.