Comparing version 0.1.0 to 0.2.0
@@ -11,2 +11,12 @@ # Changelog | ||
## [0.2.0] - 2020-09-13 | ||
### Added | ||
- A `decompressBuffer` function. | ||
### Changed | ||
- Removed the `options` param from `decompressFile`. | ||
## [0.1.0] - 2020-09-13 | ||
@@ -18,3 +28,4 @@ | ||
[unreleased]: https://github.com/jordanbtucker/oead/compare/v0.1.0...HEAD | ||
[unreleased]: https://github.com/jordanbtucker/oead/compare/v0.2.0...HEAD | ||
[0.2.0]: https://github.com/jordanbtucker/oead/compare/v0.1.0...v0.2.0 | ||
[0.1.0]: https://github.com/jordanbtucker/oead/releases/tag/v0.1.0 |
/// <reference types="node" /> | ||
import { OpenMode, PathLike } from 'fs'; | ||
import { PathLike } from 'fs'; | ||
import { Transform } from 'stream'; | ||
@@ -39,32 +39,34 @@ /** | ||
/** | ||
* Asynchronously decompresses the entire contents of a file. | ||
* Asynchronously decompresses the contents of a Buffer. | ||
* | ||
* @param path A path to a file. | ||
* @param buffer A buffer to decompress. | ||
* | ||
* @returns A Promise that resolves with a Buffer containing the decompressed | ||
* contents of the file. | ||
* data. | ||
* | ||
* @example | ||
* ```js | ||
* const buffer = await decompressFile('ActorInfo.product.sbyml') | ||
* await writeFile('ActorInfo.product.byml', buffer) | ||
* const compressedBuffer = await readFile('ActorInfo.product.sbyml') | ||
* const decompressedBuffer = await decompressBuffer(compressedBuffer) | ||
* await writeFile('ActorInfo.product.byml', decompressedBuffer) | ||
* ``` | ||
*/ | ||
export declare function decompressFile(path: PathLike): Promise<Buffer>; | ||
export declare function decompressBuffer(buffer: Buffer): Promise<Buffer>; | ||
/** | ||
* Asynchronously decompresses the entire contents of a file. | ||
* Asynchronously decompresses the contents of a Buffer. | ||
* | ||
* @param path A path to a file. | ||
* @param encoding The encoding for the file. | ||
* @param buffer A buffer to decompress. | ||
* @param encoding The encoding of the data. | ||
* | ||
* @returns A Promise that resolves with a string containing the decompressed | ||
* contents of the file. | ||
* data. | ||
* | ||
* @example | ||
* ```js | ||
* const text = await decompressFile('ActorInfo.product.sbyml', 'utf8') | ||
* await writeFile('ActorInfo.product.byml', text) | ||
* const compressedBuffer = await readFile('ActorInfo.product.sbyml') | ||
* const decompressedText = await decompressBuffer(compressedBuffer, 'utf8') | ||
* await writeFile('ActorInfo.product.byml', decompressedText) | ||
* ``` | ||
*/ | ||
export declare function decompressFile(path: PathLike, encoding: BufferEncoding): Promise<string>; | ||
export declare function decompressBuffer(buffer: Buffer, encoding: BufferEncoding): Promise<string>; | ||
/** | ||
@@ -74,7 +76,4 @@ * Asynchronously decompresses the entire contents of a file. | ||
* @param path A path to a file. | ||
* @param options An object optionally specifying the encoding and flag. | ||
* @param options.encoding The encoding for the file. | ||
* @param options.flags The flags for the file. | ||
* | ||
* @returns A Promise that resolves with a string containing the decompressed | ||
* @returns A Promise that resolves with a Buffer containing the decompressed | ||
* contents of the file. | ||
@@ -84,10 +83,7 @@ * | ||
* ```js | ||
* const text = await decompressFile('ActorInfo.product.sbyml', {encoding: 'utf8'}) | ||
* await writeFile('ActorInfo.product.byml', text) | ||
* const buffer = await decompressFile('ActorInfo.product.sbyml') | ||
* await writeFile('ActorInfo.product.byml', buffer) | ||
* ``` | ||
*/ | ||
export declare function decompressFile(path: PathLike, options: { | ||
encoding: BufferEncoding; | ||
flag?: OpenMode; | ||
}): Promise<string>; | ||
export declare function decompressFile(path: PathLike): Promise<Buffer>; | ||
/** | ||
@@ -97,7 +93,5 @@ * Asynchronously decompresses the entire contents of a file. | ||
* @param path A path to a file. | ||
* @param options An object optionally specifying the encoding and flag. | ||
* @param options.encoding The encoding for the file. | ||
* @param options.flags The flags for the file. | ||
* | ||
* @returns A Promise that resolves with a Buffer containing the decompressed | ||
* @param encoding The encoding for the file. | ||
* @returns A Promise that resolves with a string containing the decompressed | ||
* contents of the file. | ||
@@ -107,9 +101,6 @@ * | ||
* ```js | ||
* const buffer = await decompressFile('ActorInfo.product.sbyml', {falgs: 'r'}) | ||
* await writeFile('ActorInfo.product.byml', buffer) | ||
* const text = await decompressFile('ActorInfo.product.sbyml', 'utf8') | ||
* await writeFile('ActorInfo.product.byml', text) | ||
* ``` | ||
*/ | ||
export declare function decompressFile(path: PathLike, options: { | ||
encoding?: null; | ||
flag?: OpenMode; | ||
}): Promise<Buffer>; | ||
export declare function decompressFile(path: PathLike, encoding: BufferEncoding): Promise<string>; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.decompressFile = exports.decompress = void 0; | ||
exports.decompressFile = exports.decompressBuffer = exports.decompress = void 0; | ||
const fs_1 = require("fs"); | ||
@@ -218,21 +218,28 @@ const stream_1 = require("stream"); | ||
exports.decompress = decompress; | ||
async function decompressFile(path, encodingOrOptions) { | ||
async function decompressBuffer(buffer, encoding) { | ||
return new Promise((resolve, reject) => { | ||
try { | ||
const { encoding, flag } = encodingOrOptions == null | ||
? {} | ||
: typeof encodingOrOptions === 'string' | ||
? { encoding: encodingOrOptions } | ||
: encodingOrOptions; | ||
// const options = | ||
// encodingOrOptions == null | ||
// ? undefined | ||
// : typeof encodingOrOptions === 'string' | ||
// ? encodingOrOptions | ||
// : { | ||
// encoding: encodingOrOptions.encoding || undefined, | ||
// flags: encodingOrOptions.flag as string | undefined, | ||
// } | ||
const buffers = []; | ||
fs_1.createReadStream(path, { flags: flag }) | ||
decompress() | ||
.on('data', (data) => { | ||
buffers.push(data); | ||
}) | ||
.on('end', () => { | ||
const buffer = Buffer.concat(buffers); | ||
resolve(encoding == null ? buffer : buffer.toString(encoding)); | ||
}) | ||
.on('error', reject) | ||
.end(buffer); | ||
} | ||
catch (err) { | ||
reject(err); | ||
} | ||
}); | ||
} | ||
exports.decompressBuffer = decompressBuffer; | ||
async function decompressFile(path, encoding) { | ||
return new Promise((resolve, reject) => { | ||
try { | ||
const buffers = []; | ||
fs_1.createReadStream(path) | ||
.pipe(decompress()) | ||
@@ -239,0 +246,0 @@ .on('data', (data) => { |
{ | ||
"name": "oead", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"description": "A library for recent Nintendo EAD formats in first-party games.", | ||
@@ -50,3 +50,3 @@ "keywords": [ | ||
"start": "node lib", | ||
"tap": "tap test -R spec -t 0", | ||
"tap": "tap -R spec -t 0 --test-ignore /util/", | ||
"tap-coverage": "tap --coverage-report html", | ||
@@ -53,0 +53,0 @@ "test": "run-s build lint tap" |
@@ -1,2 +0,2 @@ | ||
import {createReadStream, OpenMode, PathLike} from 'fs' | ||
import {createReadStream, PathLike} from 'fs' | ||
import {Transform, TransformCallback} from 'stream' | ||
@@ -328,37 +328,62 @@ import { | ||
/** | ||
* Asynchronously decompresses the entire contents of a file. | ||
* Asynchronously decompresses the contents of a Buffer. | ||
* | ||
* @param path A path to a file. | ||
* @param buffer A buffer to decompress. | ||
* | ||
* @returns A Promise that resolves with a Buffer containing the decompressed | ||
* contents of the file. | ||
* data. | ||
* | ||
* @example | ||
* ```js | ||
* const buffer = await decompressFile('ActorInfo.product.sbyml') | ||
* await writeFile('ActorInfo.product.byml', buffer) | ||
* const compressedBuffer = await readFile('ActorInfo.product.sbyml') | ||
* const decompressedBuffer = await decompressBuffer(compressedBuffer) | ||
* await writeFile('ActorInfo.product.byml', decompressedBuffer) | ||
* ``` | ||
*/ | ||
export async function decompressFile(path: PathLike): Promise<Buffer> | ||
export async function decompressBuffer(buffer: Buffer): Promise<Buffer> | ||
/** | ||
* Asynchronously decompresses the entire contents of a file. | ||
* Asynchronously decompresses the contents of a Buffer. | ||
* | ||
* @param path A path to a file. | ||
* @param encoding The encoding for the file. | ||
* @param buffer A buffer to decompress. | ||
* @param encoding The encoding of the data. | ||
* | ||
* @returns A Promise that resolves with a string containing the decompressed | ||
* contents of the file. | ||
* data. | ||
* | ||
* @example | ||
* ```js | ||
* const text = await decompressFile('ActorInfo.product.sbyml', 'utf8') | ||
* await writeFile('ActorInfo.product.byml', text) | ||
* const compressedBuffer = await readFile('ActorInfo.product.sbyml') | ||
* const decompressedText = await decompressBuffer(compressedBuffer, 'utf8') | ||
* await writeFile('ActorInfo.product.byml', decompressedText) | ||
* ``` | ||
*/ | ||
export async function decompressFile( | ||
path: PathLike, | ||
export async function decompressBuffer( | ||
buffer: Buffer, | ||
encoding: BufferEncoding, | ||
): Promise<string> | ||
export async function decompressBuffer( | ||
buffer: Buffer, | ||
encoding?: BufferEncoding, | ||
): Promise<Buffer | string> { | ||
return new Promise((resolve, reject) => { | ||
try { | ||
const buffers: Buffer[] = [] | ||
decompress() | ||
.on('data', (data: Buffer) => { | ||
buffers.push(data) | ||
}) | ||
.on('end', () => { | ||
const buffer = Buffer.concat(buffers) | ||
resolve(encoding == null ? buffer : buffer.toString(encoding)) | ||
}) | ||
.on('error', reject) | ||
.end(buffer) | ||
} catch (err) { | ||
reject(err) | ||
} | ||
}) | ||
} | ||
/** | ||
@@ -368,7 +393,4 @@ * Asynchronously decompresses the entire contents of a file. | ||
* @param path A path to a file. | ||
* @param options An object optionally specifying the encoding and flag. | ||
* @param options.encoding The encoding for the file. | ||
* @param options.flags The flags for the file. | ||
* | ||
* @returns A Promise that resolves with a string containing the decompressed | ||
* @returns A Promise that resolves with a Buffer containing the decompressed | ||
* contents of the file. | ||
@@ -378,10 +400,7 @@ * | ||
* ```js | ||
* const text = await decompressFile('ActorInfo.product.sbyml', {encoding: 'utf8'}) | ||
* await writeFile('ActorInfo.product.byml', text) | ||
* const buffer = await decompressFile('ActorInfo.product.sbyml') | ||
* await writeFile('ActorInfo.product.byml', buffer) | ||
* ``` | ||
*/ | ||
export async function decompressFile( | ||
path: PathLike, | ||
options: {encoding: BufferEncoding; flag?: OpenMode}, | ||
): Promise<string> | ||
export async function decompressFile(path: PathLike): Promise<Buffer> | ||
@@ -392,7 +411,5 @@ /** | ||
* @param path A path to a file. | ||
* @param options An object optionally specifying the encoding and flag. | ||
* @param options.encoding The encoding for the file. | ||
* @param options.flags The flags for the file. | ||
* | ||
* @returns A Promise that resolves with a Buffer containing the decompressed | ||
* @param encoding The encoding for the file. | ||
* @returns A Promise that resolves with a string containing the decompressed | ||
* contents of the file. | ||
@@ -402,4 +419,4 @@ * | ||
* ```js | ||
* const buffer = await decompressFile('ActorInfo.product.sbyml', {falgs: 'r'}) | ||
* await writeFile('ActorInfo.product.byml', buffer) | ||
* const text = await decompressFile('ActorInfo.product.sbyml', 'utf8') | ||
* await writeFile('ActorInfo.product.byml', text) | ||
* ``` | ||
@@ -409,32 +426,13 @@ */ | ||
path: PathLike, | ||
options: {encoding?: null; flag?: OpenMode}, | ||
): Promise<Buffer> | ||
encoding: BufferEncoding, | ||
): Promise<string> | ||
export async function decompressFile( | ||
path: PathLike, | ||
encodingOrOptions?: | ||
| null | ||
| BufferEncoding | ||
| {encoding?: BufferEncoding | null; flag?: OpenMode}, | ||
encoding?: BufferEncoding, | ||
): Promise<Buffer | string> { | ||
return new Promise((resolve, reject) => { | ||
try { | ||
const {encoding, flag}: {encoding?: BufferEncoding; flag?: string} = | ||
encodingOrOptions == null | ||
? {} | ||
: typeof encodingOrOptions === 'string' | ||
? {encoding: encodingOrOptions} | ||
: (encodingOrOptions as {encoding?: BufferEncoding; flag?: string}) | ||
// const options = | ||
// encodingOrOptions == null | ||
// ? undefined | ||
// : typeof encodingOrOptions === 'string' | ||
// ? encodingOrOptions | ||
// : { | ||
// encoding: encodingOrOptions.encoding || undefined, | ||
// flags: encodingOrOptions.flag as string | undefined, | ||
// } | ||
const buffers: Buffer[] = [] | ||
createReadStream(path, {flags: flag}) | ||
createReadStream(path) | ||
.pipe(decompress()) | ||
@@ -441,0 +439,0 @@ .on('data', (data: Buffer) => { |
Sorry, the diff of this file is not supported yet
45159
848