Skip to content

Commit

Permalink
fix: fix exists usage error
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangfuxing committed Nov 20, 2024
1 parent 8b8ed06 commit e55b874
Show file tree
Hide file tree
Showing 22 changed files with 594 additions and 117 deletions.
40 changes: 20 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ If you want to read and write files, you need the following [permissions](https:
## `tar`

v0.5.4
v0.5.5
Use `@std/tar` instead of `@std/archive` due to deprecation
If you want to use the old API, you can do it

Expand All @@ -34,11 +34,11 @@ If you want to use the old API, you can do it
// JSR
import { tar } from "jsr:@deno-library/compress";
// or
// import { tar } from "jsr:@deno-library/[email protected].4";
// import { tar } from "jsr:@deno-library/[email protected].5";
// or
// import { tar } from "https://deno.land/x/[email protected].4/mod.ts";
// import { tar } from "https://deno.land/x/[email protected].5/mod.ts";
// or only import tar
// import { tar } from "https://deno.land/x/[email protected].4/tar/mod.ts";
// import { tar } from "https://deno.land/x/[email protected].5/tar/mod.ts";
export interface compressInterface {
excludeSrc?: boolean; // does not contain the src directory
debug?: boolean; // list the files and folders
Expand All @@ -56,7 +56,7 @@ tar.uncompress(src, dest, options?: uncompressInterface): Promise<void>;
// JSR
import { tar } from "jsr:@deno-library/compress";
// or
// import { tar } from "https://deno.land/x/[email protected].4/mod.ts";
// import { tar } from "https://deno.land/x/[email protected].5/mod.ts";
// compress folder
await tar.compress("./test", "./test.tar");
// compress folder, exclude src directory
Expand All @@ -83,9 +83,9 @@ import {
inflateRaw,
} from "jsr:@deno-library/compress";
// or
// import { deflate, inflate, deflateRaw, inflateRaw } from "https://deno.land/x/[email protected].4/mod.ts";
// import { deflate, inflate, deflateRaw, inflateRaw } from "https://deno.land/x/[email protected].5/mod.ts";
// or only import deflate, inflate, deflateRaw, inflateRaw
// import { deflate, inflate, deflateRaw, inflateRaw } from "https://deno.land/x/[email protected].4/zlib/mod.ts";
// import { deflate, inflate, deflateRaw, inflateRaw } from "https://deno.land/x/[email protected].5/zlib/mod.ts";
const str = "hello world!";
const bytes = new TextEncoder().encode(str);
// with zlib header
Expand Down Expand Up @@ -128,9 +128,9 @@ Useful for reading and writing large files.
```ts
import { GzipStream } from "jsr:@deno-library/compress";
// or
// import { GzipStream } from "https://deno.land/x/[email protected].4/mod.ts";
// import { GzipStream } from "https://deno.land/x/[email protected].5/mod.ts";
// or only import GzipStream
// import { GzipStream } from "https://deno.land/x/[email protected].4/gzip/mod.ts";
// import { GzipStream } from "https://deno.land/x/[email protected].5/gzip/mod.ts";
const gzip = new GzipStream();
gzip.on("progress", (progress: string) => {
console.log(progress); // 0.00% => 100.00%
Expand All @@ -145,9 +145,9 @@ await gzip.uncompress("./big.mkv.gz", "./big.mkv");
```ts
import { gunzipFile, gzipFile } from "jsr:@deno-library/compress";
// or
// import { gunzipFile, gzipFile } from "https://deno.land/x/[email protected].4/mod.ts";
// import { gunzipFile, gzipFile } from "https://deno.land/x/[email protected].5/mod.ts";
// or only import gzipFile, gunzipFile
// import { gzipFile, gunzipFile } from "https://deno.land/x/[email protected].4/gzip/mod.ts";
// import { gzipFile, gunzipFile } from "https://deno.land/x/[email protected].5/gzip/mod.ts";
await gzipFile("./deno.txt", "./deno.txt.gz");
await gunzipFile("./deno.txt.gz", "./deno.txt");
```
Expand All @@ -160,9 +160,9 @@ await gunzipFile("./deno.txt.gz", "./deno.txt");
```ts
import { gunzip, gzip } from "jsr:@deno-library/compress";
// or
// import { gunzip, gzip } from "https://deno.land/x/[email protected].4/mod.ts";
// import { gunzip, gzip } from "https://deno.land/x/[email protected].5/mod.ts";
// or only import gzip, gunzip
// import { gzip, gunzip } from "https://deno.land/x/[email protected].4/zlib/mod.ts";
// import { gzip, gunzip } from "https://deno.land/x/[email protected].5/zlib/mod.ts";
// gzip
const bytes = new TextEncoder().encode("hello");
const compressed = gzip(bytes);
Expand All @@ -172,7 +172,7 @@ const decompressed = gunzip(compressed);

## `tgz`

v0.5.4
v0.5.5
Use `@std/tar` instead of `@std/archive` due to deprecation
If you want to use the old API, you can do it

Expand All @@ -183,9 +183,9 @@ If you want to use the old API, you can do it
```ts
import { tgz } from "jsr:@deno-library/compress";
// or
// import { tgz } from "https://deno.land/x/[email protected].4/mod.ts";
// import { tgz } from "https://deno.land/x/[email protected].5/mod.ts";
// or only import tgz
// import { tgz } from "https://deno.land/x/[email protected].4/tgz/mod.ts";
// import { tgz } from "https://deno.land/x/[email protected].5/tgz/mod.ts";
export interface compressInterface {
excludeSrc?: boolean; // does not contain the src directory
debug?: boolean; // list the files and folders
Expand All @@ -202,7 +202,7 @@ tgz.uncompress(src, dest, options?: uncompressInterface): Promise<void>;
```ts
import { tgz } from "jsr:@deno-library/compress";
// or
// import { tgz } from "https://deno.land/x/[email protected].4/mod.ts";
// import { tgz } from "https://deno.land/x/[email protected].5/mod.ts";
// compress folder
await tgz.compress("./test", "./test.tar.gz");
// compress folder, exclude src directory
Expand All @@ -220,9 +220,9 @@ await tgz.uncompress("./test.tar.gz", "./dest");
```ts
import { zip } from "jsr:@deno-library/compress";
// or
// import { zip } from "https://deno.land/x/[email protected].4/mod.ts";
// import { zip } from "https://deno.land/x/[email protected].5/mod.ts";
// or only import zip
// import { zip } from "https://deno.land/x/[email protected].4/zip/mod.ts";
// import { zip } from "https://deno.land/x/[email protected].5/zip/mod.ts";

export interface compressInterface {
excludeSrc?: boolean; // does not contain the src directory
Expand All @@ -240,7 +240,7 @@ zip.uncompress(src, dest, options?: uncompressInterface): Promise<void>;
```ts
import { zip } from "jsr:@deno-library/compress";
// or
// import { zip } from "https://deno.land/x/[email protected].4/mod.ts";
// import { zip } from "https://deno.land/x/[email protected].5/mod.ts";
// compress folder
await zip.compress("./test", "./test.tar.gz");
// compress folder, exclude src directory
Expand Down
4 changes: 4 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## Changelog

### v0.5.5 - 2024.11.20

- fix: fix exists usage error

### v0.5.4 - 2024.11.20

- fix: zip.compress does not close the dest file if src does not exist[#19]
Expand Down
12 changes: 12 additions & 0 deletions deflate/mod.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,14 @@
/**
* 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 { deflateRaw } from "./deflate_raw.ts";
/**
* Decompresses a raw Uint8Array data.
* @param arr - The Uint8Array data to decompress.
* @returns The decompressed Uint8Array data.
*/
export { inflateRaw } from "./inflate_raw.ts";
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.5.4",
"version": "0.5.5",
"exports": "./mod.ts"
}
2 changes: 1 addition & 1 deletion deps.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export { EventEmitter } from "node:events";
export { ensureDir, exists } from "jsr:@std/[email protected]";
export { ensureDir } 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]";
Expand Down
12 changes: 8 additions & 4 deletions tar/mod.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { compressInterface, uncompressInterface } from "../interface.ts";
import { path, exists } from "../deps.ts";
import { path } from "../deps.ts";
import { UntarStream } from "../deps.ts";
import { TarStream, type TarStreamInput } from "../deps.ts";

Expand All @@ -14,9 +14,13 @@ export async function uncompress(
dest: string,
options?: uncompressInterface,
): Promise<void> {
await exists(src, { isFile: true });
const stat = await Deno.stat(src);
if(stat.isDirectory) {
throw new Error("The source path is a directory, not a file: ${src}")
}
using srcFile = await Deno.open(src);
for await (
const entry of (await Deno.open(src)).readable.pipeThrough(new UntarStream())
const entry of srcFile.readable.pipeThrough(new UntarStream())
) {
const filePath = path.resolve(dest, entry.path);
if (options?.debug) console.log(filePath);
Expand All @@ -36,8 +40,8 @@ export async function compress(
dest: string,
options?: compressInterface,
): Promise<void> {
const stat = await Deno.stat(src);
const inputs: TarStreamInput[] = [];
const stat = await Deno.lstat(src);
if (stat.isFile) {
inputs.push({
type: "file",
Expand Down
11 changes: 7 additions & 4 deletions tar_archive/mod.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Buffer, copy, ensureDir, exists, path } from "../deps.ts";
import { Buffer, copy, ensureDir, path } from "../deps.ts";
import type { compressInterface, uncompressInterface } from "../interface.ts";
import { Tar } from "jsr:@std/[email protected]/tar";
import { Untar } from "jsr:@std/[email protected]/untar";
Expand All @@ -14,7 +14,10 @@ export async function uncompress(
dest: string,
options?: uncompressInterface,
): Promise<void> {
await exists(src, { isFile: true });
const stat = await Deno.stat(src);
if(stat.isDirectory) {
throw new Error("The source path is a directory, not a file: ${src}")
}
using reader = await Deno.open(src, { read: true });
const untar = new Untar(reader);
for await (const entry of untar) {
Expand Down Expand Up @@ -44,7 +47,7 @@ export async function compress(
options?: compressInterface,
): Promise<void> {
const tar = new Tar();
const stat = await Deno.lstat(src);
const stat = await Deno.stat(src);
if (stat.isFile) {
await tar.append(path.basename(src), {
filePath: src,
Expand Down Expand Up @@ -118,7 +121,7 @@ export async function compress(
// options?: compressInterface,
// ): Promise<void> {
// const tar = new Tar();
// const stat = await Deno.lstat(src);
// const stat = await Deno.stat(src);
// if (stat.isFile) {
// await tar.append(path.basename(src), {
// filePath: src,
Expand Down
6 changes: 3 additions & 3 deletions test/tar.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Deno.test("tar.compress file", async () => {
const dest = "./test.tar";
try {
await tar.compress(src, dest, { debug: true });
const stat = await Deno.lstat(dest);
const stat = await Deno.stat(dest);
/**
* 2048 = 512 (header) + 512 (content) + 1024 (footer)
*/
Expand All @@ -23,7 +23,7 @@ Deno.test("tar.compress folder", async () => {
const dest = "./test.tar";
try {
await tar.compress(src, dest, { debug: true });
const stat = await Deno.lstat(dest);
const stat = await Deno.stat(dest);
/**
* 4096 = 512 (header) + 0 (content) + // tar folder
* 512 (header) + 512 (content) + // tar.txt
Expand All @@ -47,7 +47,7 @@ Deno.test("tar.uncompress", async () => {
const landTxtContent = "land\n";
try {
await tar.uncompress(src, dest, { debug: true });
const stat = await Deno.lstat(landTxtPath);
const stat = await Deno.stat(landTxtPath);
assertEquals(stat.size, landTxtSize);
const buf = await Deno.readFile(landTxtPath);
const content = new TextDecoder().decode(buf);
Expand Down
4 changes: 2 additions & 2 deletions test/zip.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Deno.test("zip.compress file", async () => {
const dest = "./test.zip";
try {
await zip.compress(src, dest, { debug: true });
const stat = await Deno.lstat(dest);
const stat = await Deno.stat(dest);
assertEquals(stat.size, 255);
await Deno.remove(dest);
} catch (error) {
Expand All @@ -20,7 +20,7 @@ Deno.test("zip.compress folder", async () => {
const dest = "./deno.zip";
try {
await zip.compress(src, dest, { debug: true });
const stat = await Deno.lstat(dest);
const stat = await Deno.stat(dest);
assertEquals(stat.size, 943);
await Deno.remove(dest);
} catch (error) {
Expand Down
12 changes: 8 additions & 4 deletions tgz/mod.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { compressInterface, uncompressInterface } from "../interface.ts";
import { exists, path } from "../deps.ts";
import { path } from "../deps.ts";
import { UntarStream } from "../deps.ts";
import { TarStream, type TarStreamInput } from "../deps.ts";

Expand All @@ -22,9 +22,13 @@ export async function uncompress(
dest: string,
options?: uncompressInterface,
): Promise<void> {
await exists(src, { isFile: true });
const stat = await Deno.stat(src);
if(stat.isDirectory) {
throw new Error("The source path is a directory, not a file: ${src}")
}
using srcFile = await Deno.open(src);
for await (
const entry of (await Deno.open(src)).readable.pipeThrough(
const entry of srcFile.readable.pipeThrough(
new DecompressionStream("gzip"),
).pipeThrough(new UntarStream())
) {
Expand All @@ -47,8 +51,8 @@ export async function compress(
dest: string,
options?: compressInterface,
): Promise<void> {
const stat = await Deno.stat(src);
const inputs: TarStreamInput[] = [];
const stat = await Deno.lstat(src);
if (stat.isFile) {
inputs.push({
type: "file",
Expand Down
16 changes: 9 additions & 7 deletions zip/mod.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { compressInterface, uncompressInterface } from "../interface.ts";
import { path, exists } from "../deps.ts";
import { path } from "../deps.ts";
import {
type EntryMetaData,
terminateWorkers,
Expand All @@ -18,11 +18,13 @@ export async function uncompress(
dest: string,
options?: uncompressInterface,
): Promise<void> {
await exists(src, { isFile: true });
const stat = await Deno.stat(src);
if(stat.isDirectory) {
throw new Error("The source path is a directory, not a file: ${src}")
}
using srcFile = await Deno.open(src);
for await (
const entry of (await Deno.open(src))
.readable
.pipeThrough(new ZipReaderStream())
const entry of srcFile.readable.pipeThrough(new ZipReaderStream())
) {
const filePath = path.resolve(dest, entry.filename);
if (options?.debug) console.log(filePath);
Expand All @@ -44,10 +46,10 @@ export async function compress(
dest: string,
options?: compressInterface,
): Promise<void> {
await exists(src, { isFile: true });
const stat = await Deno.stat(src);
const zipWriter = new ZipWriter((await Deno.create(dest)).writable);
const inputs: Promise<EntryMetaData>[] = [];
const stat = await Deno.lstat(src);

if (stat.isFile) {
inputs.push(
zipWriter.add(path.basename(src), (await Deno.open(src)).readable, {
Expand Down
10 changes: 6 additions & 4 deletions zip_archive/archive.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { compressInterface, uncompressInterface } from "../interface.ts";
import { path, exists } from "../deps.ts";
import { path } from "../deps.ts";
import {
terminateWorkers,
ZipReader,
Expand All @@ -17,7 +17,10 @@ export async function uncompress(
dest: string,
options?: uncompressInterface,
): Promise<void> {
await exists(src, { isFile: true });
const stat = await Deno.stat(src);
if(stat.isDirectory) {
throw new Error("The source path is a directory, not a file: ${src}")
}
using srcFile = await Deno.open(src);
const zipReader = new ZipReader(srcFile);
try {
Expand Down Expand Up @@ -46,11 +49,10 @@ export async function compress(
dest: string,
options?: compressInterface,
): Promise<void> {
await exists(src, { isFile: true });
const stat = await Deno.stat(src);
const zipper = new ZipWriterStream();
zipper.readable.pipeTo((await Deno.create(dest)).writable);

const stat = await Deno.lstat(src);
if (stat.isFile) {
(await Deno.open(src)).readable.pipeTo(
zipper.writable(path.basename(src)),
Expand Down
Loading

0 comments on commit e55b874

Please sign in to comment.