Skip to content
This repository has been archived by the owner on Dec 16, 2023. It is now read-only.

Commit

Permalink
Update build scripts and add new package
Browse files Browse the repository at this point in the history
  • Loading branch information
Sirherobrine23 committed Mar 28, 2023
1 parent 533e1b1 commit 5867754
Show file tree
Hide file tree
Showing 19 changed files with 247 additions and 50 deletions.
62 changes: 62 additions & 0 deletions package-lock.json

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

15 changes: 12 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,20 @@
"url": "https://github.com/Sirherobrine23/coreUtils/issues"
},
"workspaces": [
"packages/*"
"packages/ar",
"packages/page_index",
"packages/utils",
"packages/descompress",
"packages/extends",
"packages/http",
"packages/cloud",
"packages/debian",
"packages/docker",
"packages/core"
],
"scripts": {
"build": "node scripts/transpiler.mjs",
"clean": "npm run --workspaces clean --if-present",
"build": "npm run prepack --workspaces",
"clean": "npm run --workspaces postpack --if-present",
"update_version": "node scripts/updateVersion.mjs"
},
"devDependencies": {
Expand Down
4 changes: 2 additions & 2 deletions packages/ar/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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/)",
Expand Down
4 changes: 2 additions & 2 deletions packages/cloud/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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/)",
Expand Down
4 changes: 2 additions & 2 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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/)",
Expand Down
4 changes: 2 additions & 2 deletions packages/debian/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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/)",
Expand Down
35 changes: 35 additions & 0 deletions packages/descompress/package.json
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"
}
}
30 changes: 30 additions & 0 deletions packages/descompress/src/index.ts
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());
});
}
76 changes: 76 additions & 0 deletions packages/descompress/src/peek.ts
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 added packages/descompress/test/hello.gz
Binary file not shown.
Binary file added packages/descompress/test/hello.xz
Binary file not shown.
8 changes: 8 additions & 0 deletions packages/descompress/test/index.mjs
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()));
6 changes: 6 additions & 0 deletions packages/descompress/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"composite": true
}
}
4 changes: 2 additions & 2 deletions packages/docker/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
"url": "https://github.com/Sirherobrine23/coreUtils/issues"
},
"scripts": {
"clean": "tsc --build --clean",
"build": "tsc --build"
"prepack": "tsc --build --clean && tsc --build",
"postpack": "tsc --build --clean"
},
"dependencies": {
"@sirherobrine23/http": "3.5.2",
Expand Down
4 changes: 2 additions & 2 deletions packages/extends/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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/)",
Expand Down
4 changes: 2 additions & 2 deletions packages/http/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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/)",
Expand Down
4 changes: 2 additions & 2 deletions packages/page_index/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
"pageindex": "./src/cli.js"
},
"scripts": {
"clean": "tsc --build --clean",
"build": "tsc"
"prepack": "tsc --build --clean && tsc --build",
"postpack": "tsc --build --clean"
},
"keywords": [],
"dependencies": {
Expand Down
4 changes: 2 additions & 2 deletions packages/utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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/)",
Expand Down
Loading

0 comments on commit 5867754

Please sign in to comment.