std/tar/untar_stream_test.ts

244 lines
5.9 KiB
TypeScript
Raw Normal View History

feat(archive): `UntarStream` and `TarStream` (#4548) * refactor(archive): An implementation of Tar as streams * fmt(archive): Ran `deno fmt` * fix(archive): fixed JSDoc examples in tar_streams.ts * fix(archive): fixed JSDoc examples so `deno task test` doesn't complain * fix(archive): lint license error * fix(archive): lint error files not exported * set(archive): Set current time as mtime for default * resolve(archive): resolves comments made * add(archive): `{ mode: 'byob' }` support for TarStream * add(archive): `{ mode: 'byob' }` support for UnTarStream * adjust(archive): The logical flow of a few if statements * tests(archive): Updated Tests for Un/TarStream * fix(archive): TarStream mtime wasn't an octal * fix(archive): TarStream tests * add(archive): Added parsePathname function Added parsePathname function abstracting the logic out of TarStream allowing the developer to validate pathnames before providing them to TarStream hoping it doesn't throw an error and require the archive creation to start all over again. * fix(archive): extra bytes incorrectly appending at the end of files When the appending file was exactly divisible by 512 bytes, an extra 512 bytes was being appending instead of zero to fill in the gap, causing the next header to be read at the wrong place. * adjust(archive): to always return the amount of bytes requested Instead of using enqueue, the leftover bytes are saved for later for the next buffer provided. * tweaks * fix * docs(archive): Link to the spec that they're following * docs(archive): fix spelling * add(archive): function validTarSteamOptions - To make sure, if TarStreamOptions are being provided, that they are in the correct format so as to not create bad tarballs. * add(archive): more tests * fix(archive): validTarStreamOptions * add(archive): tests for validTarStreamOptions * refactor(archive): code to copy the changes made in the @doctor/tar-stream version * test(archive): added from @doctor/tar-stream * chore: nit on anonymous function * refactor(archive): UnTarStream that fixes unexplainable memory leak - The second newest test introduced here '... with invalid ending' seems to detect a memory leak due to an invalid tarball. I couldn't figure out why the memory leak was happening but I know this restructure of the code doesn't have that same memory leak. * chore: fmt * tests(archive): added remaining tests to cover many lines as possible * adjust(archive): remove simplify pathname code * adjust(archive): remove checking for duplicate pathnames in taring process * adjust(archive): A readable will exist on TarEntry unless string values 1-6 * tests(archive): added more tests for higher coverage * adjust(archives): TarStream and UnTarStream to implement TransformStream * docs(archive): moved TarStreamOptions docs into properties. * adjust(archive): TarStreamFile to take a ReadableSteam instead of an Iterable | AsyncIterable * adjust(archive): to use FixedChunkStream instead of rolling it's own implementation * fix(archive): lint error * adjust(archive): Error types and messages * adjust(archive): more Error messages / improve tests * refactor(archive): UnTarStream to return TarStreamChunk instead of TarStreamEntry * fix(archive): JSDoc example * adjust(archive): mode, uid, gid options to be provided as numbers instead of strings. * adjust(archive): TarStream's pathname to be only of type string * fix(archive): prefix/name to ignore everything past the first NULL * adjust(archive): `checksum` and `pad` to not be exposed from UnTarStream * adjust(archive): checksum calculation * change(archive): `.slice` to `.subarray` * doc(archive): "octal number" to "octal literal" * adjust(archive): TarStreamOptions to be optional with defaults * doc(archive): added more docs for the interfaces * docs(archive): denoting defaults * docs(archive): updated for new lint rules * adjust(archive): Tests to use assertRejects where appropriate & add `validPathname` function - The `validPathname` is meant to be a nicer exposed function for users of this lib to validate that their pathnames are valid before pipping it through the TarStream, over exposing parsePathname where the user may be confused about what to do with the result. * adjust(archive): to use `Date.now()` instead of `new Date().getTime()` Co-authored-by: ud2 <sjx233@qq.com> * adjust(archive): mode, uid, and gid to be numbers instead of strings when Untaring * tests(archive): adjust two tests to also validate the contents of the files are valid * adjust(archive): linkname, uname, and gname to follow the same decoding rules as name and prefix * rename(archive): UnTarStream to UntarStream * fix(archive): type that was missed getting updated * tests(archive): adjust check headers test to validate all header properties instead of just pathnames * rename(archive): `pathname` properties to `path` * docs(archive): updated to be more descriptive * docs(archive): Updated error types * adjust(archive): `validPath` to `assertValidPath` * adjust(archive): `validTarStreamOptions` to `assertValidTarStreamOptions` * revert(archive): UntarStream to produce TarStreamEntry instead of TarStreamChunk * refactor: remove redundant `void` return types * docs: cleanup assertion function docs * docs: correct `TarStream` example * docs: minor docs cleanups * refactor: improve error class specificity * docs: add `@experimental` JSDoc tags * docs(archive): Updated examples for `assertValidPath` and `assertValidTarStreamOptions``` * fix(archive): problem with tests - I suspect the problem is that a file that was read by `Deno.readDir` changed size between being read at `Deno.stat` and when `Deno.open` finished pulling it all in. * update error messages * update error messages * fix typos * refactor: tweak error messages * refactor: tweaks and add type field --------- Co-authored-by: Asher Gomez <ashersaupingomez@gmail.com> Co-authored-by: ud2 <sjx233@qq.com> Co-authored-by: Yoshiya Hinosawa <stibium121@gmail.com>
2024-09-02 07:43:22 +00:00
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
import { assertEquals, assertRejects } from "@std/assert";
import { toBytes } from "@std/streams/unstable-to-bytes";
feat(archive): `UntarStream` and `TarStream` (#4548) * refactor(archive): An implementation of Tar as streams * fmt(archive): Ran `deno fmt` * fix(archive): fixed JSDoc examples in tar_streams.ts * fix(archive): fixed JSDoc examples so `deno task test` doesn't complain * fix(archive): lint license error * fix(archive): lint error files not exported * set(archive): Set current time as mtime for default * resolve(archive): resolves comments made * add(archive): `{ mode: 'byob' }` support for TarStream * add(archive): `{ mode: 'byob' }` support for UnTarStream * adjust(archive): The logical flow of a few if statements * tests(archive): Updated Tests for Un/TarStream * fix(archive): TarStream mtime wasn't an octal * fix(archive): TarStream tests * add(archive): Added parsePathname function Added parsePathname function abstracting the logic out of TarStream allowing the developer to validate pathnames before providing them to TarStream hoping it doesn't throw an error and require the archive creation to start all over again. * fix(archive): extra bytes incorrectly appending at the end of files When the appending file was exactly divisible by 512 bytes, an extra 512 bytes was being appending instead of zero to fill in the gap, causing the next header to be read at the wrong place. * adjust(archive): to always return the amount of bytes requested Instead of using enqueue, the leftover bytes are saved for later for the next buffer provided. * tweaks * fix * docs(archive): Link to the spec that they're following * docs(archive): fix spelling * add(archive): function validTarSteamOptions - To make sure, if TarStreamOptions are being provided, that they are in the correct format so as to not create bad tarballs. * add(archive): more tests * fix(archive): validTarStreamOptions * add(archive): tests for validTarStreamOptions * refactor(archive): code to copy the changes made in the @doctor/tar-stream version * test(archive): added from @doctor/tar-stream * chore: nit on anonymous function * refactor(archive): UnTarStream that fixes unexplainable memory leak - The second newest test introduced here '... with invalid ending' seems to detect a memory leak due to an invalid tarball. I couldn't figure out why the memory leak was happening but I know this restructure of the code doesn't have that same memory leak. * chore: fmt * tests(archive): added remaining tests to cover many lines as possible * adjust(archive): remove simplify pathname code * adjust(archive): remove checking for duplicate pathnames in taring process * adjust(archive): A readable will exist on TarEntry unless string values 1-6 * tests(archive): added more tests for higher coverage * adjust(archives): TarStream and UnTarStream to implement TransformStream * docs(archive): moved TarStreamOptions docs into properties. * adjust(archive): TarStreamFile to take a ReadableSteam instead of an Iterable | AsyncIterable * adjust(archive): to use FixedChunkStream instead of rolling it's own implementation * fix(archive): lint error * adjust(archive): Error types and messages * adjust(archive): more Error messages / improve tests * refactor(archive): UnTarStream to return TarStreamChunk instead of TarStreamEntry * fix(archive): JSDoc example * adjust(archive): mode, uid, gid options to be provided as numbers instead of strings. * adjust(archive): TarStream's pathname to be only of type string * fix(archive): prefix/name to ignore everything past the first NULL * adjust(archive): `checksum` and `pad` to not be exposed from UnTarStream * adjust(archive): checksum calculation * change(archive): `.slice` to `.subarray` * doc(archive): "octal number" to "octal literal" * adjust(archive): TarStreamOptions to be optional with defaults * doc(archive): added more docs for the interfaces * docs(archive): denoting defaults * docs(archive): updated for new lint rules * adjust(archive): Tests to use assertRejects where appropriate & add `validPathname` function - The `validPathname` is meant to be a nicer exposed function for users of this lib to validate that their pathnames are valid before pipping it through the TarStream, over exposing parsePathname where the user may be confused about what to do with the result. * adjust(archive): to use `Date.now()` instead of `new Date().getTime()` Co-authored-by: ud2 <sjx233@qq.com> * adjust(archive): mode, uid, and gid to be numbers instead of strings when Untaring * tests(archive): adjust two tests to also validate the contents of the files are valid * adjust(archive): linkname, uname, and gname to follow the same decoding rules as name and prefix * rename(archive): UnTarStream to UntarStream * fix(archive): type that was missed getting updated * tests(archive): adjust check headers test to validate all header properties instead of just pathnames * rename(archive): `pathname` properties to `path` * docs(archive): updated to be more descriptive * docs(archive): Updated error types * adjust(archive): `validPath` to `assertValidPath` * adjust(archive): `validTarStreamOptions` to `assertValidTarStreamOptions` * revert(archive): UntarStream to produce TarStreamEntry instead of TarStreamChunk * refactor: remove redundant `void` return types * docs: cleanup assertion function docs * docs: correct `TarStream` example * docs: minor docs cleanups * refactor: improve error class specificity * docs: add `@experimental` JSDoc tags * docs(archive): Updated examples for `assertValidPath` and `assertValidTarStreamOptions``` * fix(archive): problem with tests - I suspect the problem is that a file that was read by `Deno.readDir` changed size between being read at `Deno.stat` and when `Deno.open` finished pulling it all in. * update error messages * update error messages * fix typos * refactor: tweak error messages * refactor: tweaks and add type field --------- Co-authored-by: Asher Gomez <ashersaupingomez@gmail.com> Co-authored-by: ud2 <sjx233@qq.com> Co-authored-by: Yoshiya Hinosawa <stibium121@gmail.com>
2024-09-02 07:43:22 +00:00
import { TarStream, type TarStreamInput } from "./tar_stream.ts";
import {
type OldStyleFormat,
type PosixUstarFormat,
UntarStream,
} from "./untar_stream.ts";
Deno.test("expandTarArchiveCheckingHeaders", async () => {
const text = new TextEncoder().encode("Hello World!");
const seconds = Math.floor(Date.now() / 1000);
const readable = ReadableStream.from<TarStreamInput>([
{
type: "directory",
path: "./potato",
options: {
mode: 111111,
uid: 12,
gid: 21,
mtime: seconds,
uname: "potato",
gname: "cake",
devmajor: "ice",
devminor: "scream",
},
},
{
type: "file",
path: "./text.txt",
size: text.length,
readable: ReadableStream.from([text.slice()]),
options: { mtime: seconds },
},
])
.pipeThrough(new TarStream())
.pipeThrough(new UntarStream());
const headers: (OldStyleFormat | PosixUstarFormat)[] = [];
for await (const entry of readable) {
headers.push(entry.header);
await entry.readable?.cancel();
feat(archive): `UntarStream` and `TarStream` (#4548) * refactor(archive): An implementation of Tar as streams * fmt(archive): Ran `deno fmt` * fix(archive): fixed JSDoc examples in tar_streams.ts * fix(archive): fixed JSDoc examples so `deno task test` doesn't complain * fix(archive): lint license error * fix(archive): lint error files not exported * set(archive): Set current time as mtime for default * resolve(archive): resolves comments made * add(archive): `{ mode: 'byob' }` support for TarStream * add(archive): `{ mode: 'byob' }` support for UnTarStream * adjust(archive): The logical flow of a few if statements * tests(archive): Updated Tests for Un/TarStream * fix(archive): TarStream mtime wasn't an octal * fix(archive): TarStream tests * add(archive): Added parsePathname function Added parsePathname function abstracting the logic out of TarStream allowing the developer to validate pathnames before providing them to TarStream hoping it doesn't throw an error and require the archive creation to start all over again. * fix(archive): extra bytes incorrectly appending at the end of files When the appending file was exactly divisible by 512 bytes, an extra 512 bytes was being appending instead of zero to fill in the gap, causing the next header to be read at the wrong place. * adjust(archive): to always return the amount of bytes requested Instead of using enqueue, the leftover bytes are saved for later for the next buffer provided. * tweaks * fix * docs(archive): Link to the spec that they're following * docs(archive): fix spelling * add(archive): function validTarSteamOptions - To make sure, if TarStreamOptions are being provided, that they are in the correct format so as to not create bad tarballs. * add(archive): more tests * fix(archive): validTarStreamOptions * add(archive): tests for validTarStreamOptions * refactor(archive): code to copy the changes made in the @doctor/tar-stream version * test(archive): added from @doctor/tar-stream * chore: nit on anonymous function * refactor(archive): UnTarStream that fixes unexplainable memory leak - The second newest test introduced here '... with invalid ending' seems to detect a memory leak due to an invalid tarball. I couldn't figure out why the memory leak was happening but I know this restructure of the code doesn't have that same memory leak. * chore: fmt * tests(archive): added remaining tests to cover many lines as possible * adjust(archive): remove simplify pathname code * adjust(archive): remove checking for duplicate pathnames in taring process * adjust(archive): A readable will exist on TarEntry unless string values 1-6 * tests(archive): added more tests for higher coverage * adjust(archives): TarStream and UnTarStream to implement TransformStream * docs(archive): moved TarStreamOptions docs into properties. * adjust(archive): TarStreamFile to take a ReadableSteam instead of an Iterable | AsyncIterable * adjust(archive): to use FixedChunkStream instead of rolling it's own implementation * fix(archive): lint error * adjust(archive): Error types and messages * adjust(archive): more Error messages / improve tests * refactor(archive): UnTarStream to return TarStreamChunk instead of TarStreamEntry * fix(archive): JSDoc example * adjust(archive): mode, uid, gid options to be provided as numbers instead of strings. * adjust(archive): TarStream's pathname to be only of type string * fix(archive): prefix/name to ignore everything past the first NULL * adjust(archive): `checksum` and `pad` to not be exposed from UnTarStream * adjust(archive): checksum calculation * change(archive): `.slice` to `.subarray` * doc(archive): "octal number" to "octal literal" * adjust(archive): TarStreamOptions to be optional with defaults * doc(archive): added more docs for the interfaces * docs(archive): denoting defaults * docs(archive): updated for new lint rules * adjust(archive): Tests to use assertRejects where appropriate & add `validPathname` function - The `validPathname` is meant to be a nicer exposed function for users of this lib to validate that their pathnames are valid before pipping it through the TarStream, over exposing parsePathname where the user may be confused about what to do with the result. * adjust(archive): to use `Date.now()` instead of `new Date().getTime()` Co-authored-by: ud2 <sjx233@qq.com> * adjust(archive): mode, uid, and gid to be numbers instead of strings when Untaring * tests(archive): adjust two tests to also validate the contents of the files are valid * adjust(archive): linkname, uname, and gname to follow the same decoding rules as name and prefix * rename(archive): UnTarStream to UntarStream * fix(archive): type that was missed getting updated * tests(archive): adjust check headers test to validate all header properties instead of just pathnames * rename(archive): `pathname` properties to `path` * docs(archive): updated to be more descriptive * docs(archive): Updated error types * adjust(archive): `validPath` to `assertValidPath` * adjust(archive): `validTarStreamOptions` to `assertValidTarStreamOptions` * revert(archive): UntarStream to produce TarStreamEntry instead of TarStreamChunk * refactor: remove redundant `void` return types * docs: cleanup assertion function docs * docs: correct `TarStream` example * docs: minor docs cleanups * refactor: improve error class specificity * docs: add `@experimental` JSDoc tags * docs(archive): Updated examples for `assertValidPath` and `assertValidTarStreamOptions``` * fix(archive): problem with tests - I suspect the problem is that a file that was read by `Deno.readDir` changed size between being read at `Deno.stat` and when `Deno.open` finished pulling it all in. * update error messages * update error messages * fix typos * refactor: tweak error messages * refactor: tweaks and add type field --------- Co-authored-by: Asher Gomez <ashersaupingomez@gmail.com> Co-authored-by: ud2 <sjx233@qq.com> Co-authored-by: Yoshiya Hinosawa <stibium121@gmail.com>
2024-09-02 07:43:22 +00:00
}
assertEquals(headers, [{
name: "./potato",
mode: 111111,
uid: 12,
gid: 21,
mtime: seconds,
uname: "potato",
gname: "cake",
devmajor: "ice",
devminor: "scream",
size: 0,
typeflag: "5",
linkname: "",
magic: "ustar\0",
version: "00",
prefix: "",
}, {
name: "./text.txt",
mode: 644,
uid: 0,
gid: 0,
mtime: seconds,
uname: "",
gname: "",
devmajor: "",
devminor: "",
size: text.length,
typeflag: "0",
linkname: "",
magic: "ustar\0",
version: "00",
prefix: "",
}]);
});
Deno.test("expandTarArchiveCheckingBodies", async () => {
const text = new TextEncoder().encode("Hello World!");
const readable = ReadableStream.from<TarStreamInput>([
{
type: "directory",
path: "./potato",
},
{
type: "file",
path: "./text.txt",
size: text.length,
readable: ReadableStream.from([text.slice()]),
},
])
.pipeThrough(new TarStream())
.pipeThrough(new UntarStream());
let buffer = new Uint8Array();
for await (const item of readable) {
if (item.readable) buffer = await toBytes(item.readable);
feat(archive): `UntarStream` and `TarStream` (#4548) * refactor(archive): An implementation of Tar as streams * fmt(archive): Ran `deno fmt` * fix(archive): fixed JSDoc examples in tar_streams.ts * fix(archive): fixed JSDoc examples so `deno task test` doesn't complain * fix(archive): lint license error * fix(archive): lint error files not exported * set(archive): Set current time as mtime for default * resolve(archive): resolves comments made * add(archive): `{ mode: 'byob' }` support for TarStream * add(archive): `{ mode: 'byob' }` support for UnTarStream * adjust(archive): The logical flow of a few if statements * tests(archive): Updated Tests for Un/TarStream * fix(archive): TarStream mtime wasn't an octal * fix(archive): TarStream tests * add(archive): Added parsePathname function Added parsePathname function abstracting the logic out of TarStream allowing the developer to validate pathnames before providing them to TarStream hoping it doesn't throw an error and require the archive creation to start all over again. * fix(archive): extra bytes incorrectly appending at the end of files When the appending file was exactly divisible by 512 bytes, an extra 512 bytes was being appending instead of zero to fill in the gap, causing the next header to be read at the wrong place. * adjust(archive): to always return the amount of bytes requested Instead of using enqueue, the leftover bytes are saved for later for the next buffer provided. * tweaks * fix * docs(archive): Link to the spec that they're following * docs(archive): fix spelling * add(archive): function validTarSteamOptions - To make sure, if TarStreamOptions are being provided, that they are in the correct format so as to not create bad tarballs. * add(archive): more tests * fix(archive): validTarStreamOptions * add(archive): tests for validTarStreamOptions * refactor(archive): code to copy the changes made in the @doctor/tar-stream version * test(archive): added from @doctor/tar-stream * chore: nit on anonymous function * refactor(archive): UnTarStream that fixes unexplainable memory leak - The second newest test introduced here '... with invalid ending' seems to detect a memory leak due to an invalid tarball. I couldn't figure out why the memory leak was happening but I know this restructure of the code doesn't have that same memory leak. * chore: fmt * tests(archive): added remaining tests to cover many lines as possible * adjust(archive): remove simplify pathname code * adjust(archive): remove checking for duplicate pathnames in taring process * adjust(archive): A readable will exist on TarEntry unless string values 1-6 * tests(archive): added more tests for higher coverage * adjust(archives): TarStream and UnTarStream to implement TransformStream * docs(archive): moved TarStreamOptions docs into properties. * adjust(archive): TarStreamFile to take a ReadableSteam instead of an Iterable | AsyncIterable * adjust(archive): to use FixedChunkStream instead of rolling it's own implementation * fix(archive): lint error * adjust(archive): Error types and messages * adjust(archive): more Error messages / improve tests * refactor(archive): UnTarStream to return TarStreamChunk instead of TarStreamEntry * fix(archive): JSDoc example * adjust(archive): mode, uid, gid options to be provided as numbers instead of strings. * adjust(archive): TarStream's pathname to be only of type string * fix(archive): prefix/name to ignore everything past the first NULL * adjust(archive): `checksum` and `pad` to not be exposed from UnTarStream * adjust(archive): checksum calculation * change(archive): `.slice` to `.subarray` * doc(archive): "octal number" to "octal literal" * adjust(archive): TarStreamOptions to be optional with defaults * doc(archive): added more docs for the interfaces * docs(archive): denoting defaults * docs(archive): updated for new lint rules * adjust(archive): Tests to use assertRejects where appropriate & add `validPathname` function - The `validPathname` is meant to be a nicer exposed function for users of this lib to validate that their pathnames are valid before pipping it through the TarStream, over exposing parsePathname where the user may be confused about what to do with the result. * adjust(archive): to use `Date.now()` instead of `new Date().getTime()` Co-authored-by: ud2 <sjx233@qq.com> * adjust(archive): mode, uid, and gid to be numbers instead of strings when Untaring * tests(archive): adjust two tests to also validate the contents of the files are valid * adjust(archive): linkname, uname, and gname to follow the same decoding rules as name and prefix * rename(archive): UnTarStream to UntarStream * fix(archive): type that was missed getting updated * tests(archive): adjust check headers test to validate all header properties instead of just pathnames * rename(archive): `pathname` properties to `path` * docs(archive): updated to be more descriptive * docs(archive): Updated error types * adjust(archive): `validPath` to `assertValidPath` * adjust(archive): `validTarStreamOptions` to `assertValidTarStreamOptions` * revert(archive): UntarStream to produce TarStreamEntry instead of TarStreamChunk * refactor: remove redundant `void` return types * docs: cleanup assertion function docs * docs: correct `TarStream` example * docs: minor docs cleanups * refactor: improve error class specificity * docs: add `@experimental` JSDoc tags * docs(archive): Updated examples for `assertValidPath` and `assertValidTarStreamOptions``` * fix(archive): problem with tests - I suspect the problem is that a file that was read by `Deno.readDir` changed size between being read at `Deno.stat` and when `Deno.open` finished pulling it all in. * update error messages * update error messages * fix typos * refactor: tweak error messages * refactor: tweaks and add type field --------- Co-authored-by: Asher Gomez <ashersaupingomez@gmail.com> Co-authored-by: ud2 <sjx233@qq.com> Co-authored-by: Yoshiya Hinosawa <stibium121@gmail.com>
2024-09-02 07:43:22 +00:00
}
assertEquals(buffer, text);
});
Deno.test("UntarStream() with size equals to multiple of 512", async () => {
const size = 512 * 3;
const data = Uint8Array.from(
{ length: size },
() => Math.floor(Math.random() * 256),
);
const readable = ReadableStream.from<TarStreamInput>([
{
type: "file",
path: "name",
size,
readable: ReadableStream.from([data.slice()]),
},
])
.pipeThrough(new TarStream())
.pipeThrough(new UntarStream());
let buffer = new Uint8Array();
for await (const entry of readable) {
if (entry.readable) buffer = await toBytes(entry.readable);
feat(archive): `UntarStream` and `TarStream` (#4548) * refactor(archive): An implementation of Tar as streams * fmt(archive): Ran `deno fmt` * fix(archive): fixed JSDoc examples in tar_streams.ts * fix(archive): fixed JSDoc examples so `deno task test` doesn't complain * fix(archive): lint license error * fix(archive): lint error files not exported * set(archive): Set current time as mtime for default * resolve(archive): resolves comments made * add(archive): `{ mode: 'byob' }` support for TarStream * add(archive): `{ mode: 'byob' }` support for UnTarStream * adjust(archive): The logical flow of a few if statements * tests(archive): Updated Tests for Un/TarStream * fix(archive): TarStream mtime wasn't an octal * fix(archive): TarStream tests * add(archive): Added parsePathname function Added parsePathname function abstracting the logic out of TarStream allowing the developer to validate pathnames before providing them to TarStream hoping it doesn't throw an error and require the archive creation to start all over again. * fix(archive): extra bytes incorrectly appending at the end of files When the appending file was exactly divisible by 512 bytes, an extra 512 bytes was being appending instead of zero to fill in the gap, causing the next header to be read at the wrong place. * adjust(archive): to always return the amount of bytes requested Instead of using enqueue, the leftover bytes are saved for later for the next buffer provided. * tweaks * fix * docs(archive): Link to the spec that they're following * docs(archive): fix spelling * add(archive): function validTarSteamOptions - To make sure, if TarStreamOptions are being provided, that they are in the correct format so as to not create bad tarballs. * add(archive): more tests * fix(archive): validTarStreamOptions * add(archive): tests for validTarStreamOptions * refactor(archive): code to copy the changes made in the @doctor/tar-stream version * test(archive): added from @doctor/tar-stream * chore: nit on anonymous function * refactor(archive): UnTarStream that fixes unexplainable memory leak - The second newest test introduced here '... with invalid ending' seems to detect a memory leak due to an invalid tarball. I couldn't figure out why the memory leak was happening but I know this restructure of the code doesn't have that same memory leak. * chore: fmt * tests(archive): added remaining tests to cover many lines as possible * adjust(archive): remove simplify pathname code * adjust(archive): remove checking for duplicate pathnames in taring process * adjust(archive): A readable will exist on TarEntry unless string values 1-6 * tests(archive): added more tests for higher coverage * adjust(archives): TarStream and UnTarStream to implement TransformStream * docs(archive): moved TarStreamOptions docs into properties. * adjust(archive): TarStreamFile to take a ReadableSteam instead of an Iterable | AsyncIterable * adjust(archive): to use FixedChunkStream instead of rolling it's own implementation * fix(archive): lint error * adjust(archive): Error types and messages * adjust(archive): more Error messages / improve tests * refactor(archive): UnTarStream to return TarStreamChunk instead of TarStreamEntry * fix(archive): JSDoc example * adjust(archive): mode, uid, gid options to be provided as numbers instead of strings. * adjust(archive): TarStream's pathname to be only of type string * fix(archive): prefix/name to ignore everything past the first NULL * adjust(archive): `checksum` and `pad` to not be exposed from UnTarStream * adjust(archive): checksum calculation * change(archive): `.slice` to `.subarray` * doc(archive): "octal number" to "octal literal" * adjust(archive): TarStreamOptions to be optional with defaults * doc(archive): added more docs for the interfaces * docs(archive): denoting defaults * docs(archive): updated for new lint rules * adjust(archive): Tests to use assertRejects where appropriate & add `validPathname` function - The `validPathname` is meant to be a nicer exposed function for users of this lib to validate that their pathnames are valid before pipping it through the TarStream, over exposing parsePathname where the user may be confused about what to do with the result. * adjust(archive): to use `Date.now()` instead of `new Date().getTime()` Co-authored-by: ud2 <sjx233@qq.com> * adjust(archive): mode, uid, and gid to be numbers instead of strings when Untaring * tests(archive): adjust two tests to also validate the contents of the files are valid * adjust(archive): linkname, uname, and gname to follow the same decoding rules as name and prefix * rename(archive): UnTarStream to UntarStream * fix(archive): type that was missed getting updated * tests(archive): adjust check headers test to validate all header properties instead of just pathnames * rename(archive): `pathname` properties to `path` * docs(archive): updated to be more descriptive * docs(archive): Updated error types * adjust(archive): `validPath` to `assertValidPath` * adjust(archive): `validTarStreamOptions` to `assertValidTarStreamOptions` * revert(archive): UntarStream to produce TarStreamEntry instead of TarStreamChunk * refactor: remove redundant `void` return types * docs: cleanup assertion function docs * docs: correct `TarStream` example * docs: minor docs cleanups * refactor: improve error class specificity * docs: add `@experimental` JSDoc tags * docs(archive): Updated examples for `assertValidPath` and `assertValidTarStreamOptions``` * fix(archive): problem with tests - I suspect the problem is that a file that was read by `Deno.readDir` changed size between being read at `Deno.stat` and when `Deno.open` finished pulling it all in. * update error messages * update error messages * fix typos * refactor: tweak error messages * refactor: tweaks and add type field --------- Co-authored-by: Asher Gomez <ashersaupingomez@gmail.com> Co-authored-by: ud2 <sjx233@qq.com> Co-authored-by: Yoshiya Hinosawa <stibium121@gmail.com>
2024-09-02 07:43:22 +00:00
}
assertEquals(buffer, data);
});
Deno.test("UntarStream() with invalid size", async () => {
const bytes = (await toBytes(
ReadableStream.from<TarStreamInput>([
{
type: "file",
path: "newFile.txt",
size: 512,
readable: ReadableStream.from([new Uint8Array(512).fill(97)]),
},
])
.pipeThrough(new TarStream()),
)).slice(0, -100);
const readable = ReadableStream.from([bytes])
feat(archive): `UntarStream` and `TarStream` (#4548) * refactor(archive): An implementation of Tar as streams * fmt(archive): Ran `deno fmt` * fix(archive): fixed JSDoc examples in tar_streams.ts * fix(archive): fixed JSDoc examples so `deno task test` doesn't complain * fix(archive): lint license error * fix(archive): lint error files not exported * set(archive): Set current time as mtime for default * resolve(archive): resolves comments made * add(archive): `{ mode: 'byob' }` support for TarStream * add(archive): `{ mode: 'byob' }` support for UnTarStream * adjust(archive): The logical flow of a few if statements * tests(archive): Updated Tests for Un/TarStream * fix(archive): TarStream mtime wasn't an octal * fix(archive): TarStream tests * add(archive): Added parsePathname function Added parsePathname function abstracting the logic out of TarStream allowing the developer to validate pathnames before providing them to TarStream hoping it doesn't throw an error and require the archive creation to start all over again. * fix(archive): extra bytes incorrectly appending at the end of files When the appending file was exactly divisible by 512 bytes, an extra 512 bytes was being appending instead of zero to fill in the gap, causing the next header to be read at the wrong place. * adjust(archive): to always return the amount of bytes requested Instead of using enqueue, the leftover bytes are saved for later for the next buffer provided. * tweaks * fix * docs(archive): Link to the spec that they're following * docs(archive): fix spelling * add(archive): function validTarSteamOptions - To make sure, if TarStreamOptions are being provided, that they are in the correct format so as to not create bad tarballs. * add(archive): more tests * fix(archive): validTarStreamOptions * add(archive): tests for validTarStreamOptions * refactor(archive): code to copy the changes made in the @doctor/tar-stream version * test(archive): added from @doctor/tar-stream * chore: nit on anonymous function * refactor(archive): UnTarStream that fixes unexplainable memory leak - The second newest test introduced here '... with invalid ending' seems to detect a memory leak due to an invalid tarball. I couldn't figure out why the memory leak was happening but I know this restructure of the code doesn't have that same memory leak. * chore: fmt * tests(archive): added remaining tests to cover many lines as possible * adjust(archive): remove simplify pathname code * adjust(archive): remove checking for duplicate pathnames in taring process * adjust(archive): A readable will exist on TarEntry unless string values 1-6 * tests(archive): added more tests for higher coverage * adjust(archives): TarStream and UnTarStream to implement TransformStream * docs(archive): moved TarStreamOptions docs into properties. * adjust(archive): TarStreamFile to take a ReadableSteam instead of an Iterable | AsyncIterable * adjust(archive): to use FixedChunkStream instead of rolling it's own implementation * fix(archive): lint error * adjust(archive): Error types and messages * adjust(archive): more Error messages / improve tests * refactor(archive): UnTarStream to return TarStreamChunk instead of TarStreamEntry * fix(archive): JSDoc example * adjust(archive): mode, uid, gid options to be provided as numbers instead of strings. * adjust(archive): TarStream's pathname to be only of type string * fix(archive): prefix/name to ignore everything past the first NULL * adjust(archive): `checksum` and `pad` to not be exposed from UnTarStream * adjust(archive): checksum calculation * change(archive): `.slice` to `.subarray` * doc(archive): "octal number" to "octal literal" * adjust(archive): TarStreamOptions to be optional with defaults * doc(archive): added more docs for the interfaces * docs(archive): denoting defaults * docs(archive): updated for new lint rules * adjust(archive): Tests to use assertRejects where appropriate & add `validPathname` function - The `validPathname` is meant to be a nicer exposed function for users of this lib to validate that their pathnames are valid before pipping it through the TarStream, over exposing parsePathname where the user may be confused about what to do with the result. * adjust(archive): to use `Date.now()` instead of `new Date().getTime()` Co-authored-by: ud2 <sjx233@qq.com> * adjust(archive): mode, uid, and gid to be numbers instead of strings when Untaring * tests(archive): adjust two tests to also validate the contents of the files are valid * adjust(archive): linkname, uname, and gname to follow the same decoding rules as name and prefix * rename(archive): UnTarStream to UntarStream * fix(archive): type that was missed getting updated * tests(archive): adjust check headers test to validate all header properties instead of just pathnames * rename(archive): `pathname` properties to `path` * docs(archive): updated to be more descriptive * docs(archive): Updated error types * adjust(archive): `validPath` to `assertValidPath` * adjust(archive): `validTarStreamOptions` to `assertValidTarStreamOptions` * revert(archive): UntarStream to produce TarStreamEntry instead of TarStreamChunk * refactor: remove redundant `void` return types * docs: cleanup assertion function docs * docs: correct `TarStream` example * docs: minor docs cleanups * refactor: improve error class specificity * docs: add `@experimental` JSDoc tags * docs(archive): Updated examples for `assertValidPath` and `assertValidTarStreamOptions``` * fix(archive): problem with tests - I suspect the problem is that a file that was read by `Deno.readDir` changed size between being read at `Deno.stat` and when `Deno.open` finished pulling it all in. * update error messages * update error messages * fix typos * refactor: tweak error messages * refactor: tweaks and add type field --------- Co-authored-by: Asher Gomez <ashersaupingomez@gmail.com> Co-authored-by: ud2 <sjx233@qq.com> Co-authored-by: Yoshiya Hinosawa <stibium121@gmail.com>
2024-09-02 07:43:22 +00:00
.pipeThrough(new UntarStream());
await assertRejects(
async () => {
for await (const entry of readable) await entry.readable?.cancel();
feat(archive): `UntarStream` and `TarStream` (#4548) * refactor(archive): An implementation of Tar as streams * fmt(archive): Ran `deno fmt` * fix(archive): fixed JSDoc examples in tar_streams.ts * fix(archive): fixed JSDoc examples so `deno task test` doesn't complain * fix(archive): lint license error * fix(archive): lint error files not exported * set(archive): Set current time as mtime for default * resolve(archive): resolves comments made * add(archive): `{ mode: 'byob' }` support for TarStream * add(archive): `{ mode: 'byob' }` support for UnTarStream * adjust(archive): The logical flow of a few if statements * tests(archive): Updated Tests for Un/TarStream * fix(archive): TarStream mtime wasn't an octal * fix(archive): TarStream tests * add(archive): Added parsePathname function Added parsePathname function abstracting the logic out of TarStream allowing the developer to validate pathnames before providing them to TarStream hoping it doesn't throw an error and require the archive creation to start all over again. * fix(archive): extra bytes incorrectly appending at the end of files When the appending file was exactly divisible by 512 bytes, an extra 512 bytes was being appending instead of zero to fill in the gap, causing the next header to be read at the wrong place. * adjust(archive): to always return the amount of bytes requested Instead of using enqueue, the leftover bytes are saved for later for the next buffer provided. * tweaks * fix * docs(archive): Link to the spec that they're following * docs(archive): fix spelling * add(archive): function validTarSteamOptions - To make sure, if TarStreamOptions are being provided, that they are in the correct format so as to not create bad tarballs. * add(archive): more tests * fix(archive): validTarStreamOptions * add(archive): tests for validTarStreamOptions * refactor(archive): code to copy the changes made in the @doctor/tar-stream version * test(archive): added from @doctor/tar-stream * chore: nit on anonymous function * refactor(archive): UnTarStream that fixes unexplainable memory leak - The second newest test introduced here '... with invalid ending' seems to detect a memory leak due to an invalid tarball. I couldn't figure out why the memory leak was happening but I know this restructure of the code doesn't have that same memory leak. * chore: fmt * tests(archive): added remaining tests to cover many lines as possible * adjust(archive): remove simplify pathname code * adjust(archive): remove checking for duplicate pathnames in taring process * adjust(archive): A readable will exist on TarEntry unless string values 1-6 * tests(archive): added more tests for higher coverage * adjust(archives): TarStream and UnTarStream to implement TransformStream * docs(archive): moved TarStreamOptions docs into properties. * adjust(archive): TarStreamFile to take a ReadableSteam instead of an Iterable | AsyncIterable * adjust(archive): to use FixedChunkStream instead of rolling it's own implementation * fix(archive): lint error * adjust(archive): Error types and messages * adjust(archive): more Error messages / improve tests * refactor(archive): UnTarStream to return TarStreamChunk instead of TarStreamEntry * fix(archive): JSDoc example * adjust(archive): mode, uid, gid options to be provided as numbers instead of strings. * adjust(archive): TarStream's pathname to be only of type string * fix(archive): prefix/name to ignore everything past the first NULL * adjust(archive): `checksum` and `pad` to not be exposed from UnTarStream * adjust(archive): checksum calculation * change(archive): `.slice` to `.subarray` * doc(archive): "octal number" to "octal literal" * adjust(archive): TarStreamOptions to be optional with defaults * doc(archive): added more docs for the interfaces * docs(archive): denoting defaults * docs(archive): updated for new lint rules * adjust(archive): Tests to use assertRejects where appropriate & add `validPathname` function - The `validPathname` is meant to be a nicer exposed function for users of this lib to validate that their pathnames are valid before pipping it through the TarStream, over exposing parsePathname where the user may be confused about what to do with the result. * adjust(archive): to use `Date.now()` instead of `new Date().getTime()` Co-authored-by: ud2 <sjx233@qq.com> * adjust(archive): mode, uid, and gid to be numbers instead of strings when Untaring * tests(archive): adjust two tests to also validate the contents of the files are valid * adjust(archive): linkname, uname, and gname to follow the same decoding rules as name and prefix * rename(archive): UnTarStream to UntarStream * fix(archive): type that was missed getting updated * tests(archive): adjust check headers test to validate all header properties instead of just pathnames * rename(archive): `pathname` properties to `path` * docs(archive): updated to be more descriptive * docs(archive): Updated error types * adjust(archive): `validPath` to `assertValidPath` * adjust(archive): `validTarStreamOptions` to `assertValidTarStreamOptions` * revert(archive): UntarStream to produce TarStreamEntry instead of TarStreamChunk * refactor: remove redundant `void` return types * docs: cleanup assertion function docs * docs: correct `TarStream` example * docs: minor docs cleanups * refactor: improve error class specificity * docs: add `@experimental` JSDoc tags * docs(archive): Updated examples for `assertValidPath` and `assertValidTarStreamOptions``` * fix(archive): problem with tests - I suspect the problem is that a file that was read by `Deno.readDir` changed size between being read at `Deno.stat` and when `Deno.open` finished pulling it all in. * update error messages * update error messages * fix typos * refactor: tweak error messages * refactor: tweaks and add type field --------- Co-authored-by: Asher Gomez <ashersaupingomez@gmail.com> Co-authored-by: ud2 <sjx233@qq.com> Co-authored-by: Yoshiya Hinosawa <stibium121@gmail.com>
2024-09-02 07:43:22 +00:00
},
RangeError,
"Cannot extract the tar archive: The tarball chunk has an unexpected number of bytes (412)",
feat(archive): `UntarStream` and `TarStream` (#4548) * refactor(archive): An implementation of Tar as streams * fmt(archive): Ran `deno fmt` * fix(archive): fixed JSDoc examples in tar_streams.ts * fix(archive): fixed JSDoc examples so `deno task test` doesn't complain * fix(archive): lint license error * fix(archive): lint error files not exported * set(archive): Set current time as mtime for default * resolve(archive): resolves comments made * add(archive): `{ mode: 'byob' }` support for TarStream * add(archive): `{ mode: 'byob' }` support for UnTarStream * adjust(archive): The logical flow of a few if statements * tests(archive): Updated Tests for Un/TarStream * fix(archive): TarStream mtime wasn't an octal * fix(archive): TarStream tests * add(archive): Added parsePathname function Added parsePathname function abstracting the logic out of TarStream allowing the developer to validate pathnames before providing them to TarStream hoping it doesn't throw an error and require the archive creation to start all over again. * fix(archive): extra bytes incorrectly appending at the end of files When the appending file was exactly divisible by 512 bytes, an extra 512 bytes was being appending instead of zero to fill in the gap, causing the next header to be read at the wrong place. * adjust(archive): to always return the amount of bytes requested Instead of using enqueue, the leftover bytes are saved for later for the next buffer provided. * tweaks * fix * docs(archive): Link to the spec that they're following * docs(archive): fix spelling * add(archive): function validTarSteamOptions - To make sure, if TarStreamOptions are being provided, that they are in the correct format so as to not create bad tarballs. * add(archive): more tests * fix(archive): validTarStreamOptions * add(archive): tests for validTarStreamOptions * refactor(archive): code to copy the changes made in the @doctor/tar-stream version * test(archive): added from @doctor/tar-stream * chore: nit on anonymous function * refactor(archive): UnTarStream that fixes unexplainable memory leak - The second newest test introduced here '... with invalid ending' seems to detect a memory leak due to an invalid tarball. I couldn't figure out why the memory leak was happening but I know this restructure of the code doesn't have that same memory leak. * chore: fmt * tests(archive): added remaining tests to cover many lines as possible * adjust(archive): remove simplify pathname code * adjust(archive): remove checking for duplicate pathnames in taring process * adjust(archive): A readable will exist on TarEntry unless string values 1-6 * tests(archive): added more tests for higher coverage * adjust(archives): TarStream and UnTarStream to implement TransformStream * docs(archive): moved TarStreamOptions docs into properties. * adjust(archive): TarStreamFile to take a ReadableSteam instead of an Iterable | AsyncIterable * adjust(archive): to use FixedChunkStream instead of rolling it's own implementation * fix(archive): lint error * adjust(archive): Error types and messages * adjust(archive): more Error messages / improve tests * refactor(archive): UnTarStream to return TarStreamChunk instead of TarStreamEntry * fix(archive): JSDoc example * adjust(archive): mode, uid, gid options to be provided as numbers instead of strings. * adjust(archive): TarStream's pathname to be only of type string * fix(archive): prefix/name to ignore everything past the first NULL * adjust(archive): `checksum` and `pad` to not be exposed from UnTarStream * adjust(archive): checksum calculation * change(archive): `.slice` to `.subarray` * doc(archive): "octal number" to "octal literal" * adjust(archive): TarStreamOptions to be optional with defaults * doc(archive): added more docs for the interfaces * docs(archive): denoting defaults * docs(archive): updated for new lint rules * adjust(archive): Tests to use assertRejects where appropriate & add `validPathname` function - The `validPathname` is meant to be a nicer exposed function for users of this lib to validate that their pathnames are valid before pipping it through the TarStream, over exposing parsePathname where the user may be confused about what to do with the result. * adjust(archive): to use `Date.now()` instead of `new Date().getTime()` Co-authored-by: ud2 <sjx233@qq.com> * adjust(archive): mode, uid, and gid to be numbers instead of strings when Untaring * tests(archive): adjust two tests to also validate the contents of the files are valid * adjust(archive): linkname, uname, and gname to follow the same decoding rules as name and prefix * rename(archive): UnTarStream to UntarStream * fix(archive): type that was missed getting updated * tests(archive): adjust check headers test to validate all header properties instead of just pathnames * rename(archive): `pathname` properties to `path` * docs(archive): updated to be more descriptive * docs(archive): Updated error types * adjust(archive): `validPath` to `assertValidPath` * adjust(archive): `validTarStreamOptions` to `assertValidTarStreamOptions` * revert(archive): UntarStream to produce TarStreamEntry instead of TarStreamChunk * refactor: remove redundant `void` return types * docs: cleanup assertion function docs * docs: correct `TarStream` example * docs: minor docs cleanups * refactor: improve error class specificity * docs: add `@experimental` JSDoc tags * docs(archive): Updated examples for `assertValidPath` and `assertValidTarStreamOptions``` * fix(archive): problem with tests - I suspect the problem is that a file that was read by `Deno.readDir` changed size between being read at `Deno.stat` and when `Deno.open` finished pulling it all in. * update error messages * update error messages * fix typos * refactor: tweak error messages * refactor: tweaks and add type field --------- Co-authored-by: Asher Gomez <ashersaupingomez@gmail.com> Co-authored-by: ud2 <sjx233@qq.com> Co-authored-by: Yoshiya Hinosawa <stibium121@gmail.com>
2024-09-02 07:43:22 +00:00
);
});
Deno.test("UntarStream() with invalid ending", async () => {
const tarBytes = await toBytes(
ReadableStream.from<TarStreamInput>([
{
type: "file",
path: "newFile.txt",
size: 512,
readable: ReadableStream.from([new Uint8Array(512).fill(97)]),
},
])
.pipeThrough(new TarStream()),
feat(archive): `UntarStream` and `TarStream` (#4548) * refactor(archive): An implementation of Tar as streams * fmt(archive): Ran `deno fmt` * fix(archive): fixed JSDoc examples in tar_streams.ts * fix(archive): fixed JSDoc examples so `deno task test` doesn't complain * fix(archive): lint license error * fix(archive): lint error files not exported * set(archive): Set current time as mtime for default * resolve(archive): resolves comments made * add(archive): `{ mode: 'byob' }` support for TarStream * add(archive): `{ mode: 'byob' }` support for UnTarStream * adjust(archive): The logical flow of a few if statements * tests(archive): Updated Tests for Un/TarStream * fix(archive): TarStream mtime wasn't an octal * fix(archive): TarStream tests * add(archive): Added parsePathname function Added parsePathname function abstracting the logic out of TarStream allowing the developer to validate pathnames before providing them to TarStream hoping it doesn't throw an error and require the archive creation to start all over again. * fix(archive): extra bytes incorrectly appending at the end of files When the appending file was exactly divisible by 512 bytes, an extra 512 bytes was being appending instead of zero to fill in the gap, causing the next header to be read at the wrong place. * adjust(archive): to always return the amount of bytes requested Instead of using enqueue, the leftover bytes are saved for later for the next buffer provided. * tweaks * fix * docs(archive): Link to the spec that they're following * docs(archive): fix spelling * add(archive): function validTarSteamOptions - To make sure, if TarStreamOptions are being provided, that they are in the correct format so as to not create bad tarballs. * add(archive): more tests * fix(archive): validTarStreamOptions * add(archive): tests for validTarStreamOptions * refactor(archive): code to copy the changes made in the @doctor/tar-stream version * test(archive): added from @doctor/tar-stream * chore: nit on anonymous function * refactor(archive): UnTarStream that fixes unexplainable memory leak - The second newest test introduced here '... with invalid ending' seems to detect a memory leak due to an invalid tarball. I couldn't figure out why the memory leak was happening but I know this restructure of the code doesn't have that same memory leak. * chore: fmt * tests(archive): added remaining tests to cover many lines as possible * adjust(archive): remove simplify pathname code * adjust(archive): remove checking for duplicate pathnames in taring process * adjust(archive): A readable will exist on TarEntry unless string values 1-6 * tests(archive): added more tests for higher coverage * adjust(archives): TarStream and UnTarStream to implement TransformStream * docs(archive): moved TarStreamOptions docs into properties. * adjust(archive): TarStreamFile to take a ReadableSteam instead of an Iterable | AsyncIterable * adjust(archive): to use FixedChunkStream instead of rolling it's own implementation * fix(archive): lint error * adjust(archive): Error types and messages * adjust(archive): more Error messages / improve tests * refactor(archive): UnTarStream to return TarStreamChunk instead of TarStreamEntry * fix(archive): JSDoc example * adjust(archive): mode, uid, gid options to be provided as numbers instead of strings. * adjust(archive): TarStream's pathname to be only of type string * fix(archive): prefix/name to ignore everything past the first NULL * adjust(archive): `checksum` and `pad` to not be exposed from UnTarStream * adjust(archive): checksum calculation * change(archive): `.slice` to `.subarray` * doc(archive): "octal number" to "octal literal" * adjust(archive): TarStreamOptions to be optional with defaults * doc(archive): added more docs for the interfaces * docs(archive): denoting defaults * docs(archive): updated for new lint rules * adjust(archive): Tests to use assertRejects where appropriate & add `validPathname` function - The `validPathname` is meant to be a nicer exposed function for users of this lib to validate that their pathnames are valid before pipping it through the TarStream, over exposing parsePathname where the user may be confused about what to do with the result. * adjust(archive): to use `Date.now()` instead of `new Date().getTime()` Co-authored-by: ud2 <sjx233@qq.com> * adjust(archive): mode, uid, and gid to be numbers instead of strings when Untaring * tests(archive): adjust two tests to also validate the contents of the files are valid * adjust(archive): linkname, uname, and gname to follow the same decoding rules as name and prefix * rename(archive): UnTarStream to UntarStream * fix(archive): type that was missed getting updated * tests(archive): adjust check headers test to validate all header properties instead of just pathnames * rename(archive): `pathname` properties to `path` * docs(archive): updated to be more descriptive * docs(archive): Updated error types * adjust(archive): `validPath` to `assertValidPath` * adjust(archive): `validTarStreamOptions` to `assertValidTarStreamOptions` * revert(archive): UntarStream to produce TarStreamEntry instead of TarStreamChunk * refactor: remove redundant `void` return types * docs: cleanup assertion function docs * docs: correct `TarStream` example * docs: minor docs cleanups * refactor: improve error class specificity * docs: add `@experimental` JSDoc tags * docs(archive): Updated examples for `assertValidPath` and `assertValidTarStreamOptions``` * fix(archive): problem with tests - I suspect the problem is that a file that was read by `Deno.readDir` changed size between being read at `Deno.stat` and when `Deno.open` finished pulling it all in. * update error messages * update error messages * fix typos * refactor: tweak error messages * refactor: tweaks and add type field --------- Co-authored-by: Asher Gomez <ashersaupingomez@gmail.com> Co-authored-by: ud2 <sjx233@qq.com> Co-authored-by: Yoshiya Hinosawa <stibium121@gmail.com>
2024-09-02 07:43:22 +00:00
);
tarBytes[tarBytes.length - 1] = 1;
const readable = ReadableStream.from([tarBytes])
.pipeThrough(new UntarStream());
await assertRejects(
async () => {
for await (const entry of readable) await entry.readable?.cancel();
feat(archive): `UntarStream` and `TarStream` (#4548) * refactor(archive): An implementation of Tar as streams * fmt(archive): Ran `deno fmt` * fix(archive): fixed JSDoc examples in tar_streams.ts * fix(archive): fixed JSDoc examples so `deno task test` doesn't complain * fix(archive): lint license error * fix(archive): lint error files not exported * set(archive): Set current time as mtime for default * resolve(archive): resolves comments made * add(archive): `{ mode: 'byob' }` support for TarStream * add(archive): `{ mode: 'byob' }` support for UnTarStream * adjust(archive): The logical flow of a few if statements * tests(archive): Updated Tests for Un/TarStream * fix(archive): TarStream mtime wasn't an octal * fix(archive): TarStream tests * add(archive): Added parsePathname function Added parsePathname function abstracting the logic out of TarStream allowing the developer to validate pathnames before providing them to TarStream hoping it doesn't throw an error and require the archive creation to start all over again. * fix(archive): extra bytes incorrectly appending at the end of files When the appending file was exactly divisible by 512 bytes, an extra 512 bytes was being appending instead of zero to fill in the gap, causing the next header to be read at the wrong place. * adjust(archive): to always return the amount of bytes requested Instead of using enqueue, the leftover bytes are saved for later for the next buffer provided. * tweaks * fix * docs(archive): Link to the spec that they're following * docs(archive): fix spelling * add(archive): function validTarSteamOptions - To make sure, if TarStreamOptions are being provided, that they are in the correct format so as to not create bad tarballs. * add(archive): more tests * fix(archive): validTarStreamOptions * add(archive): tests for validTarStreamOptions * refactor(archive): code to copy the changes made in the @doctor/tar-stream version * test(archive): added from @doctor/tar-stream * chore: nit on anonymous function * refactor(archive): UnTarStream that fixes unexplainable memory leak - The second newest test introduced here '... with invalid ending' seems to detect a memory leak due to an invalid tarball. I couldn't figure out why the memory leak was happening but I know this restructure of the code doesn't have that same memory leak. * chore: fmt * tests(archive): added remaining tests to cover many lines as possible * adjust(archive): remove simplify pathname code * adjust(archive): remove checking for duplicate pathnames in taring process * adjust(archive): A readable will exist on TarEntry unless string values 1-6 * tests(archive): added more tests for higher coverage * adjust(archives): TarStream and UnTarStream to implement TransformStream * docs(archive): moved TarStreamOptions docs into properties. * adjust(archive): TarStreamFile to take a ReadableSteam instead of an Iterable | AsyncIterable * adjust(archive): to use FixedChunkStream instead of rolling it's own implementation * fix(archive): lint error * adjust(archive): Error types and messages * adjust(archive): more Error messages / improve tests * refactor(archive): UnTarStream to return TarStreamChunk instead of TarStreamEntry * fix(archive): JSDoc example * adjust(archive): mode, uid, gid options to be provided as numbers instead of strings. * adjust(archive): TarStream's pathname to be only of type string * fix(archive): prefix/name to ignore everything past the first NULL * adjust(archive): `checksum` and `pad` to not be exposed from UnTarStream * adjust(archive): checksum calculation * change(archive): `.slice` to `.subarray` * doc(archive): "octal number" to "octal literal" * adjust(archive): TarStreamOptions to be optional with defaults * doc(archive): added more docs for the interfaces * docs(archive): denoting defaults * docs(archive): updated for new lint rules * adjust(archive): Tests to use assertRejects where appropriate & add `validPathname` function - The `validPathname` is meant to be a nicer exposed function for users of this lib to validate that their pathnames are valid before pipping it through the TarStream, over exposing parsePathname where the user may be confused about what to do with the result. * adjust(archive): to use `Date.now()` instead of `new Date().getTime()` Co-authored-by: ud2 <sjx233@qq.com> * adjust(archive): mode, uid, and gid to be numbers instead of strings when Untaring * tests(archive): adjust two tests to also validate the contents of the files are valid * adjust(archive): linkname, uname, and gname to follow the same decoding rules as name and prefix * rename(archive): UnTarStream to UntarStream * fix(archive): type that was missed getting updated * tests(archive): adjust check headers test to validate all header properties instead of just pathnames * rename(archive): `pathname` properties to `path` * docs(archive): updated to be more descriptive * docs(archive): Updated error types * adjust(archive): `validPath` to `assertValidPath` * adjust(archive): `validTarStreamOptions` to `assertValidTarStreamOptions` * revert(archive): UntarStream to produce TarStreamEntry instead of TarStreamChunk * refactor: remove redundant `void` return types * docs: cleanup assertion function docs * docs: correct `TarStream` example * docs: minor docs cleanups * refactor: improve error class specificity * docs: add `@experimental` JSDoc tags * docs(archive): Updated examples for `assertValidPath` and `assertValidTarStreamOptions``` * fix(archive): problem with tests - I suspect the problem is that a file that was read by `Deno.readDir` changed size between being read at `Deno.stat` and when `Deno.open` finished pulling it all in. * update error messages * update error messages * fix typos * refactor: tweak error messages * refactor: tweaks and add type field --------- Co-authored-by: Asher Gomez <ashersaupingomez@gmail.com> Co-authored-by: ud2 <sjx233@qq.com> Co-authored-by: Yoshiya Hinosawa <stibium121@gmail.com>
2024-09-02 07:43:22 +00:00
},
TypeError,
"Cannot extract the tar archive: The tarball has invalid ending",
);
});
Deno.test("UntarStream() with too small size", async () => {
const readable = ReadableStream.from([new Uint8Array(512)])
.pipeThrough(new UntarStream());
await assertRejects(
async () => {
for await (const entry of readable) await entry.readable?.cancel();
feat(archive): `UntarStream` and `TarStream` (#4548) * refactor(archive): An implementation of Tar as streams * fmt(archive): Ran `deno fmt` * fix(archive): fixed JSDoc examples in tar_streams.ts * fix(archive): fixed JSDoc examples so `deno task test` doesn't complain * fix(archive): lint license error * fix(archive): lint error files not exported * set(archive): Set current time as mtime for default * resolve(archive): resolves comments made * add(archive): `{ mode: 'byob' }` support for TarStream * add(archive): `{ mode: 'byob' }` support for UnTarStream * adjust(archive): The logical flow of a few if statements * tests(archive): Updated Tests for Un/TarStream * fix(archive): TarStream mtime wasn't an octal * fix(archive): TarStream tests * add(archive): Added parsePathname function Added parsePathname function abstracting the logic out of TarStream allowing the developer to validate pathnames before providing them to TarStream hoping it doesn't throw an error and require the archive creation to start all over again. * fix(archive): extra bytes incorrectly appending at the end of files When the appending file was exactly divisible by 512 bytes, an extra 512 bytes was being appending instead of zero to fill in the gap, causing the next header to be read at the wrong place. * adjust(archive): to always return the amount of bytes requested Instead of using enqueue, the leftover bytes are saved for later for the next buffer provided. * tweaks * fix * docs(archive): Link to the spec that they're following * docs(archive): fix spelling * add(archive): function validTarSteamOptions - To make sure, if TarStreamOptions are being provided, that they are in the correct format so as to not create bad tarballs. * add(archive): more tests * fix(archive): validTarStreamOptions * add(archive): tests for validTarStreamOptions * refactor(archive): code to copy the changes made in the @doctor/tar-stream version * test(archive): added from @doctor/tar-stream * chore: nit on anonymous function * refactor(archive): UnTarStream that fixes unexplainable memory leak - The second newest test introduced here '... with invalid ending' seems to detect a memory leak due to an invalid tarball. I couldn't figure out why the memory leak was happening but I know this restructure of the code doesn't have that same memory leak. * chore: fmt * tests(archive): added remaining tests to cover many lines as possible * adjust(archive): remove simplify pathname code * adjust(archive): remove checking for duplicate pathnames in taring process * adjust(archive): A readable will exist on TarEntry unless string values 1-6 * tests(archive): added more tests for higher coverage * adjust(archives): TarStream and UnTarStream to implement TransformStream * docs(archive): moved TarStreamOptions docs into properties. * adjust(archive): TarStreamFile to take a ReadableSteam instead of an Iterable | AsyncIterable * adjust(archive): to use FixedChunkStream instead of rolling it's own implementation * fix(archive): lint error * adjust(archive): Error types and messages * adjust(archive): more Error messages / improve tests * refactor(archive): UnTarStream to return TarStreamChunk instead of TarStreamEntry * fix(archive): JSDoc example * adjust(archive): mode, uid, gid options to be provided as numbers instead of strings. * adjust(archive): TarStream's pathname to be only of type string * fix(archive): prefix/name to ignore everything past the first NULL * adjust(archive): `checksum` and `pad` to not be exposed from UnTarStream * adjust(archive): checksum calculation * change(archive): `.slice` to `.subarray` * doc(archive): "octal number" to "octal literal" * adjust(archive): TarStreamOptions to be optional with defaults * doc(archive): added more docs for the interfaces * docs(archive): denoting defaults * docs(archive): updated for new lint rules * adjust(archive): Tests to use assertRejects where appropriate & add `validPathname` function - The `validPathname` is meant to be a nicer exposed function for users of this lib to validate that their pathnames are valid before pipping it through the TarStream, over exposing parsePathname where the user may be confused about what to do with the result. * adjust(archive): to use `Date.now()` instead of `new Date().getTime()` Co-authored-by: ud2 <sjx233@qq.com> * adjust(archive): mode, uid, and gid to be numbers instead of strings when Untaring * tests(archive): adjust two tests to also validate the contents of the files are valid * adjust(archive): linkname, uname, and gname to follow the same decoding rules as name and prefix * rename(archive): UnTarStream to UntarStream * fix(archive): type that was missed getting updated * tests(archive): adjust check headers test to validate all header properties instead of just pathnames * rename(archive): `pathname` properties to `path` * docs(archive): updated to be more descriptive * docs(archive): Updated error types * adjust(archive): `validPath` to `assertValidPath` * adjust(archive): `validTarStreamOptions` to `assertValidTarStreamOptions` * revert(archive): UntarStream to produce TarStreamEntry instead of TarStreamChunk * refactor: remove redundant `void` return types * docs: cleanup assertion function docs * docs: correct `TarStream` example * docs: minor docs cleanups * refactor: improve error class specificity * docs: add `@experimental` JSDoc tags * docs(archive): Updated examples for `assertValidPath` and `assertValidTarStreamOptions``` * fix(archive): problem with tests - I suspect the problem is that a file that was read by `Deno.readDir` changed size between being read at `Deno.stat` and when `Deno.open` finished pulling it all in. * update error messages * update error messages * fix typos * refactor: tweak error messages * refactor: tweaks and add type field --------- Co-authored-by: Asher Gomez <ashersaupingomez@gmail.com> Co-authored-by: ud2 <sjx233@qq.com> Co-authored-by: Yoshiya Hinosawa <stibium121@gmail.com>
2024-09-02 07:43:22 +00:00
},
RangeError,
"Cannot extract the tar archive: The tarball is too small to be valid",
);
});
Deno.test("UntarStream() with invalid checksum", async () => {
const tarBytes = await toBytes(
ReadableStream.from<TarStreamInput>([
{
type: "file",
path: "newFile.txt",
size: 512,
readable: ReadableStream.from([new Uint8Array(512).fill(97)]),
},
])
.pipeThrough(new TarStream()),
feat(archive): `UntarStream` and `TarStream` (#4548) * refactor(archive): An implementation of Tar as streams * fmt(archive): Ran `deno fmt` * fix(archive): fixed JSDoc examples in tar_streams.ts * fix(archive): fixed JSDoc examples so `deno task test` doesn't complain * fix(archive): lint license error * fix(archive): lint error files not exported * set(archive): Set current time as mtime for default * resolve(archive): resolves comments made * add(archive): `{ mode: 'byob' }` support for TarStream * add(archive): `{ mode: 'byob' }` support for UnTarStream * adjust(archive): The logical flow of a few if statements * tests(archive): Updated Tests for Un/TarStream * fix(archive): TarStream mtime wasn't an octal * fix(archive): TarStream tests * add(archive): Added parsePathname function Added parsePathname function abstracting the logic out of TarStream allowing the developer to validate pathnames before providing them to TarStream hoping it doesn't throw an error and require the archive creation to start all over again. * fix(archive): extra bytes incorrectly appending at the end of files When the appending file was exactly divisible by 512 bytes, an extra 512 bytes was being appending instead of zero to fill in the gap, causing the next header to be read at the wrong place. * adjust(archive): to always return the amount of bytes requested Instead of using enqueue, the leftover bytes are saved for later for the next buffer provided. * tweaks * fix * docs(archive): Link to the spec that they're following * docs(archive): fix spelling * add(archive): function validTarSteamOptions - To make sure, if TarStreamOptions are being provided, that they are in the correct format so as to not create bad tarballs. * add(archive): more tests * fix(archive): validTarStreamOptions * add(archive): tests for validTarStreamOptions * refactor(archive): code to copy the changes made in the @doctor/tar-stream version * test(archive): added from @doctor/tar-stream * chore: nit on anonymous function * refactor(archive): UnTarStream that fixes unexplainable memory leak - The second newest test introduced here '... with invalid ending' seems to detect a memory leak due to an invalid tarball. I couldn't figure out why the memory leak was happening but I know this restructure of the code doesn't have that same memory leak. * chore: fmt * tests(archive): added remaining tests to cover many lines as possible * adjust(archive): remove simplify pathname code * adjust(archive): remove checking for duplicate pathnames in taring process * adjust(archive): A readable will exist on TarEntry unless string values 1-6 * tests(archive): added more tests for higher coverage * adjust(archives): TarStream and UnTarStream to implement TransformStream * docs(archive): moved TarStreamOptions docs into properties. * adjust(archive): TarStreamFile to take a ReadableSteam instead of an Iterable | AsyncIterable * adjust(archive): to use FixedChunkStream instead of rolling it's own implementation * fix(archive): lint error * adjust(archive): Error types and messages * adjust(archive): more Error messages / improve tests * refactor(archive): UnTarStream to return TarStreamChunk instead of TarStreamEntry * fix(archive): JSDoc example * adjust(archive): mode, uid, gid options to be provided as numbers instead of strings. * adjust(archive): TarStream's pathname to be only of type string * fix(archive): prefix/name to ignore everything past the first NULL * adjust(archive): `checksum` and `pad` to not be exposed from UnTarStream * adjust(archive): checksum calculation * change(archive): `.slice` to `.subarray` * doc(archive): "octal number" to "octal literal" * adjust(archive): TarStreamOptions to be optional with defaults * doc(archive): added more docs for the interfaces * docs(archive): denoting defaults * docs(archive): updated for new lint rules * adjust(archive): Tests to use assertRejects where appropriate & add `validPathname` function - The `validPathname` is meant to be a nicer exposed function for users of this lib to validate that their pathnames are valid before pipping it through the TarStream, over exposing parsePathname where the user may be confused about what to do with the result. * adjust(archive): to use `Date.now()` instead of `new Date().getTime()` Co-authored-by: ud2 <sjx233@qq.com> * adjust(archive): mode, uid, and gid to be numbers instead of strings when Untaring * tests(archive): adjust two tests to also validate the contents of the files are valid * adjust(archive): linkname, uname, and gname to follow the same decoding rules as name and prefix * rename(archive): UnTarStream to UntarStream * fix(archive): type that was missed getting updated * tests(archive): adjust check headers test to validate all header properties instead of just pathnames * rename(archive): `pathname` properties to `path` * docs(archive): updated to be more descriptive * docs(archive): Updated error types * adjust(archive): `validPath` to `assertValidPath` * adjust(archive): `validTarStreamOptions` to `assertValidTarStreamOptions` * revert(archive): UntarStream to produce TarStreamEntry instead of TarStreamChunk * refactor: remove redundant `void` return types * docs: cleanup assertion function docs * docs: correct `TarStream` example * docs: minor docs cleanups * refactor: improve error class specificity * docs: add `@experimental` JSDoc tags * docs(archive): Updated examples for `assertValidPath` and `assertValidTarStreamOptions``` * fix(archive): problem with tests - I suspect the problem is that a file that was read by `Deno.readDir` changed size between being read at `Deno.stat` and when `Deno.open` finished pulling it all in. * update error messages * update error messages * fix typos * refactor: tweak error messages * refactor: tweaks and add type field --------- Co-authored-by: Asher Gomez <ashersaupingomez@gmail.com> Co-authored-by: ud2 <sjx233@qq.com> Co-authored-by: Yoshiya Hinosawa <stibium121@gmail.com>
2024-09-02 07:43:22 +00:00
);
tarBytes[148] = 97;
const readable = ReadableStream.from([tarBytes])
.pipeThrough(new UntarStream());
await assertRejects(
async () => {
for await (const entry of readable) await entry.readable?.cancel();
feat(archive): `UntarStream` and `TarStream` (#4548) * refactor(archive): An implementation of Tar as streams * fmt(archive): Ran `deno fmt` * fix(archive): fixed JSDoc examples in tar_streams.ts * fix(archive): fixed JSDoc examples so `deno task test` doesn't complain * fix(archive): lint license error * fix(archive): lint error files not exported * set(archive): Set current time as mtime for default * resolve(archive): resolves comments made * add(archive): `{ mode: 'byob' }` support for TarStream * add(archive): `{ mode: 'byob' }` support for UnTarStream * adjust(archive): The logical flow of a few if statements * tests(archive): Updated Tests for Un/TarStream * fix(archive): TarStream mtime wasn't an octal * fix(archive): TarStream tests * add(archive): Added parsePathname function Added parsePathname function abstracting the logic out of TarStream allowing the developer to validate pathnames before providing them to TarStream hoping it doesn't throw an error and require the archive creation to start all over again. * fix(archive): extra bytes incorrectly appending at the end of files When the appending file was exactly divisible by 512 bytes, an extra 512 bytes was being appending instead of zero to fill in the gap, causing the next header to be read at the wrong place. * adjust(archive): to always return the amount of bytes requested Instead of using enqueue, the leftover bytes are saved for later for the next buffer provided. * tweaks * fix * docs(archive): Link to the spec that they're following * docs(archive): fix spelling * add(archive): function validTarSteamOptions - To make sure, if TarStreamOptions are being provided, that they are in the correct format so as to not create bad tarballs. * add(archive): more tests * fix(archive): validTarStreamOptions * add(archive): tests for validTarStreamOptions * refactor(archive): code to copy the changes made in the @doctor/tar-stream version * test(archive): added from @doctor/tar-stream * chore: nit on anonymous function * refactor(archive): UnTarStream that fixes unexplainable memory leak - The second newest test introduced here '... with invalid ending' seems to detect a memory leak due to an invalid tarball. I couldn't figure out why the memory leak was happening but I know this restructure of the code doesn't have that same memory leak. * chore: fmt * tests(archive): added remaining tests to cover many lines as possible * adjust(archive): remove simplify pathname code * adjust(archive): remove checking for duplicate pathnames in taring process * adjust(archive): A readable will exist on TarEntry unless string values 1-6 * tests(archive): added more tests for higher coverage * adjust(archives): TarStream and UnTarStream to implement TransformStream * docs(archive): moved TarStreamOptions docs into properties. * adjust(archive): TarStreamFile to take a ReadableSteam instead of an Iterable | AsyncIterable * adjust(archive): to use FixedChunkStream instead of rolling it's own implementation * fix(archive): lint error * adjust(archive): Error types and messages * adjust(archive): more Error messages / improve tests * refactor(archive): UnTarStream to return TarStreamChunk instead of TarStreamEntry * fix(archive): JSDoc example * adjust(archive): mode, uid, gid options to be provided as numbers instead of strings. * adjust(archive): TarStream's pathname to be only of type string * fix(archive): prefix/name to ignore everything past the first NULL * adjust(archive): `checksum` and `pad` to not be exposed from UnTarStream * adjust(archive): checksum calculation * change(archive): `.slice` to `.subarray` * doc(archive): "octal number" to "octal literal" * adjust(archive): TarStreamOptions to be optional with defaults * doc(archive): added more docs for the interfaces * docs(archive): denoting defaults * docs(archive): updated for new lint rules * adjust(archive): Tests to use assertRejects where appropriate & add `validPathname` function - The `validPathname` is meant to be a nicer exposed function for users of this lib to validate that their pathnames are valid before pipping it through the TarStream, over exposing parsePathname where the user may be confused about what to do with the result. * adjust(archive): to use `Date.now()` instead of `new Date().getTime()` Co-authored-by: ud2 <sjx233@qq.com> * adjust(archive): mode, uid, and gid to be numbers instead of strings when Untaring * tests(archive): adjust two tests to also validate the contents of the files are valid * adjust(archive): linkname, uname, and gname to follow the same decoding rules as name and prefix * rename(archive): UnTarStream to UntarStream * fix(archive): type that was missed getting updated * tests(archive): adjust check headers test to validate all header properties instead of just pathnames * rename(archive): `pathname` properties to `path` * docs(archive): updated to be more descriptive * docs(archive): Updated error types * adjust(archive): `validPath` to `assertValidPath` * adjust(archive): `validTarStreamOptions` to `assertValidTarStreamOptions` * revert(archive): UntarStream to produce TarStreamEntry instead of TarStreamChunk * refactor: remove redundant `void` return types * docs: cleanup assertion function docs * docs: correct `TarStream` example * docs: minor docs cleanups * refactor: improve error class specificity * docs: add `@experimental` JSDoc tags * docs(archive): Updated examples for `assertValidPath` and `assertValidTarStreamOptions``` * fix(archive): problem with tests - I suspect the problem is that a file that was read by `Deno.readDir` changed size between being read at `Deno.stat` and when `Deno.open` finished pulling it all in. * update error messages * update error messages * fix typos * refactor: tweak error messages * refactor: tweaks and add type field --------- Co-authored-by: Asher Gomez <ashersaupingomez@gmail.com> Co-authored-by: ud2 <sjx233@qq.com> Co-authored-by: Yoshiya Hinosawa <stibium121@gmail.com>
2024-09-02 07:43:22 +00:00
},
Error,
"Cannot extract the tar archive: An archive entry has invalid header checksum",
);
});
Deno.test("UntarStream() with extra bytes", async () => {
const readable = ReadableStream.from<TarStreamInput>([
{
type: "directory",
path: "a",
},
])
.pipeThrough(new TarStream())
.pipeThrough(
new TransformStream({
flush(controller) {
controller.enqueue(new Uint8Array(512 * 2).fill(1));
},
}),
)
.pipeThrough(new UntarStream());
for await (const entry of readable) {
assertEquals(entry.path, "a");
entry.readable?.cancel();
}
});