This repository has been archived by the owner on Dec 16, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update build scripts and add new package
- Loading branch information
1 parent
533e1b1
commit 5867754
Showing
19 changed files
with
247 additions
and
50 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,8 +14,8 @@ | |
"url": "https://github.com/Sirherobrine23/coreUtils/issues" | ||
}, | ||
"scripts": { | ||
"clean": "tsc --build --clean", | ||
"build": "tsc" | ||
"prepack": "tsc --build --clean && tsc --build", | ||
"postpack": "tsc --build --clean" | ||
}, | ||
"keywords": [], | ||
"author": "Matheus Sampaio Queiroga <[email protected]> (https://sirherobrine23.org/)", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,8 +14,8 @@ | |
"url": "https://github.com/Sirherobrine23/coreUtils/issues" | ||
}, | ||
"scripts": { | ||
"clean": "tsc --build --clean", | ||
"build": "tsc" | ||
"prepack": "tsc --build --clean && tsc --build", | ||
"postpack": "tsc --build --clean" | ||
}, | ||
"keywords": [], | ||
"author": "Matheus Sampaio Queiroga <[email protected]> (https://sirherobrine23.org/)", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,8 +15,8 @@ | |
"url": "https://github.com/Sirherobrine23/coreUtils/issues" | ||
}, | ||
"scripts": { | ||
"clean": "tsc --build --clean", | ||
"build": "tsc" | ||
"prepack": "tsc --build --clean && tsc --build", | ||
"postpack": "tsc --build --clean" | ||
}, | ||
"keywords": [], | ||
"author": "Matheus Sampaio Queiroga <[email protected]> (https://sirherobrine23.org/)", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,8 +14,8 @@ | |
"url": "https://github.com/Sirherobrine23/coreUtils/issues" | ||
}, | ||
"scripts": { | ||
"clean": "tsc --build --clean", | ||
"build": "tsc" | ||
"prepack": "tsc --build --clean && tsc --build", | ||
"postpack": "tsc --build --clean" | ||
}, | ||
"keywords": [], | ||
"author": "Matheus Sampaio Queiroga <[email protected]> (https://sirherobrine23.org/)", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
{ | ||
"name": "@sirherobrine23/decompress", | ||
"version": "1.0.0", | ||
"description": "", | ||
"type": "module", | ||
"main": "src/index.js", | ||
"types": "src/index.d.ts", | ||
"scripts": { | ||
"prepack": "tsc --build --clean && tsc --build", | ||
"postpack": "tsc --build --clean" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/Sirherobrine23/coreUtils.git" | ||
}, | ||
"keywords": [ | ||
"auto", | ||
"decompress" | ||
], | ||
"author": "Matheus Sampaio Queirora <[email protected]>", | ||
"license": "GPL-2.0-only", | ||
"bugs": { | ||
"url": "https://github.com/Sirherobrine23/coreUtils/issues" | ||
}, | ||
"homepage": "https://github.com/Sirherobrine23/coreUtils#readme", | ||
"dependencies": { | ||
"duplexify": "^4.1.2" | ||
}, | ||
"devDependencies": { | ||
"@types/duplexify": "^3.6.1" | ||
}, | ||
"optionalDependencies": { | ||
"lzma-native": "^8.0.6" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { peek } from "./peek.js"; | ||
import stream from "stream"; | ||
|
||
function isDeflate(buf: Buffer) { | ||
if (!buf || buf.length < 2) return false; | ||
return buf[0] === 0x78 && (buf[1] === 1 || buf[1] === 0x9c || buf[1] === 0xda); | ||
} | ||
|
||
function isGzip(buf: Buffer) { | ||
if (!buf || buf.length < 3) return false; | ||
return buf[0] === 0x1F && buf[1] === 0x8B && buf[2] === 0x08; | ||
} | ||
|
||
function isXz(buf: Buffer) { | ||
if (!buf || buf.length < 5) return false; | ||
return (buf[0] === 0xFD) && (buf[1] === 0x37) && (buf[2] === 0x7A) && (buf[3] === 0x58) && (buf[4] === 0x5A); | ||
} | ||
|
||
export default decompress; | ||
/** | ||
* auto detect compress if is match to Xz/Lzma, gzip or deflate pipe and decompress else echo Buffer | ||
*/ | ||
export function decompress() { | ||
return peek({newLine: false, maxBuffer: 10}, async (data, swap) => { | ||
if (isDeflate(data)) import("zlib").then(({createInflate}) => swap(null, createInflate())).catch(err => swap(err)); | ||
else if (isGzip(data)) import("zlib").then(({createGunzip}) => swap(null, createGunzip())).catch(err => swap(err)); | ||
else if (isXz(data)) import("lzma-native").then(({createDecompressor}) => swap(null, createDecompressor())).catch(err => swap(err)); | ||
else swap(null, new stream.PassThrough()); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { PassThrough, Transform } from "stream"; | ||
import duplexify from "duplexify"; | ||
|
||
function isObject(data: Buffer) { | ||
return !Buffer.isBuffer(data) && typeof data !== 'string'; | ||
} | ||
|
||
export interface options { | ||
maxBuffer?: number; | ||
newLine?: boolean; | ||
strict?: any | ||
} | ||
|
||
export type onpeek = (data: Buffer, swap: (err?: any, str?: Transform) => void) => void | ||
|
||
export function peek(opts: number): duplexify.Duplexify; | ||
export function peek(onpeek: onpeek): duplexify.Duplexify; | ||
export function peek(): duplexify.Duplexify; | ||
export function peek(opts: options, onpeek: onpeek): duplexify.Duplexify; | ||
export function peek(opts?: options|number|onpeek, onpeek?: onpeek): duplexify.Duplexify { | ||
if (typeof opts === "number") opts = {maxBuffer: opts}; | ||
if (typeof opts === "function") return peek(null, opts) | ||
if (!opts) opts = {}; | ||
const maxBuffer = typeof opts.maxBuffer === "number" ? opts.maxBuffer : 65535; | ||
const newline = opts.newLine !== false; | ||
const strict = opts.strict; | ||
const dup = duplexify.obj(); | ||
let buffer = [], bufferSize = 0; | ||
|
||
function onpreend() { | ||
if (strict) return dup.destroy(new Error('No newline found')); | ||
dup.cork(); | ||
return ready(Buffer.concat(buffer), null, (err) => err ? dup.destroy(err) : dup.uncork()); | ||
} | ||
|
||
function ready(data: Buffer, overflow, cb) { | ||
dup.removeListener("preend", onpreend) | ||
onpeek(data, function(err, parser) { | ||
if (err) return cb(err) | ||
dup.setWritable(parser); | ||
dup.setReadable(parser); | ||
if (data) parser.write(data); | ||
if (overflow) parser.write(overflow); | ||
overflow = buffer = peeker = null; // free the data | ||
return cb(); | ||
}); | ||
}; | ||
|
||
var peeker = new PassThrough({ | ||
highWaterMark: 1, | ||
transform(chunk, encoding, callback) { | ||
if (isObject(chunk)) return ready(chunk, null, callback) | ||
if (!Buffer.isBuffer(chunk)) chunk = Buffer.from(chunk, encoding); | ||
|
||
if (newline) { | ||
var nl = Array.prototype.indexOf.call(chunk, 10); | ||
if (nl > 0 && chunk[nl-1] === 13) nl--; | ||
if (nl > -1) { | ||
buffer.push(chunk.slice(0, nl)) | ||
return ready(Buffer.concat(buffer), chunk.slice(nl), callback) | ||
} | ||
} | ||
|
||
buffer.push(chunk) | ||
bufferSize += chunk.length | ||
|
||
if (bufferSize < maxBuffer) return callback(); | ||
if (strict) return callback(new Error("No newline found")); | ||
ready(Buffer.concat(buffer), null, callback) | ||
}, | ||
}); | ||
|
||
dup.on("preend", onpreend); | ||
dup.setWritable(peeker); | ||
return dup; | ||
} |
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { fileURLToPath } from "url"; | ||
import { decompress } from "../src/index.js"; | ||
import path from "path"; | ||
import fs from "fs"; | ||
const __dirname = path.dirname(fileURLToPath(import.meta.url)); | ||
|
||
fs.createReadStream(path.join(__dirname, "hello.gz")).pipe(decompress()).on("data", data => console.log(data.toString())); | ||
fs.createReadStream(path.join(__dirname, "hello.xz")).pipe(decompress()).on("data", data => console.log(data.toString())); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"compilerOptions": { | ||
"composite": true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,8 +14,8 @@ | |
"url": "https://github.com/Sirherobrine23/coreUtils/issues" | ||
}, | ||
"scripts": { | ||
"clean": "tsc --build --clean", | ||
"build": "npm run clean && tsc" | ||
"prepack": "tsc --build --clean && tsc --build", | ||
"postpack": "tsc --build --clean" | ||
}, | ||
"keywords": [], | ||
"author": "Matheus Sampaio Queiroga <[email protected]> (https://sirherobrine23.org/)", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,8 +14,8 @@ | |
"url": "https://github.com/Sirherobrine23/coreUtils/issues" | ||
}, | ||
"scripts": { | ||
"clean": "tsc --build --clean", | ||
"build": "tsc" | ||
"prepack": "tsc --build --clean && tsc --build", | ||
"postpack": "tsc --build --clean" | ||
}, | ||
"keywords": [], | ||
"author": "Matheus Sampaio Queiroga <[email protected]> (https://sirherobrine23.org/)", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,8 +14,8 @@ | |
"url": "https://github.com/Sirherobrine23/coreUtils/issues" | ||
}, | ||
"scripts": { | ||
"clean": "tsc --build --clean", | ||
"build": "tsc" | ||
"prepack": "tsc --build --clean && tsc --build", | ||
"postpack": "tsc --build --clean" | ||
}, | ||
"keywords": [], | ||
"author": "Matheus Sampaio Queiroga <[email protected]> (https://sirherobrine23.org/)", | ||
|
Oops, something went wrong.