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

Commit 5867754

Browse files
Update build scripts and add new package
1 parent 533e1b1 commit 5867754

File tree

19 files changed

+247
-50
lines changed

19 files changed

+247
-50
lines changed

package-lock.json

Lines changed: 62 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,20 @@
1212
"url": "https://github.com/Sirherobrine23/coreUtils/issues"
1313
},
1414
"workspaces": [
15-
"packages/*"
15+
"packages/ar",
16+
"packages/page_index",
17+
"packages/utils",
18+
"packages/descompress",
19+
"packages/extends",
20+
"packages/http",
21+
"packages/cloud",
22+
"packages/debian",
23+
"packages/docker",
24+
"packages/core"
1625
],
1726
"scripts": {
18-
"build": "node scripts/transpiler.mjs",
19-
"clean": "npm run --workspaces clean --if-present",
27+
"build": "npm run prepack --workspaces",
28+
"clean": "npm run --workspaces postpack --if-present",
2029
"update_version": "node scripts/updateVersion.mjs"
2130
},
2231
"devDependencies": {

packages/ar/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
"url": "https://github.com/Sirherobrine23/coreUtils/issues"
1515
},
1616
"scripts": {
17-
"clean": "tsc --build --clean",
18-
"build": "tsc"
17+
"prepack": "tsc --build --clean && tsc --build",
18+
"postpack": "tsc --build --clean"
1919
},
2020
"keywords": [],
2121
"author": "Matheus Sampaio Queiroga <[email protected]> (https://sirherobrine23.org/)",

packages/cloud/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
"url": "https://github.com/Sirherobrine23/coreUtils/issues"
1515
},
1616
"scripts": {
17-
"clean": "tsc --build --clean",
18-
"build": "tsc"
17+
"prepack": "tsc --build --clean && tsc --build",
18+
"postpack": "tsc --build --clean"
1919
},
2020
"keywords": [],
2121
"author": "Matheus Sampaio Queiroga <[email protected]> (https://sirherobrine23.org/)",

packages/core/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
"url": "https://github.com/Sirherobrine23/coreUtils/issues"
1616
},
1717
"scripts": {
18-
"clean": "tsc --build --clean",
19-
"build": "tsc"
18+
"prepack": "tsc --build --clean && tsc --build",
19+
"postpack": "tsc --build --clean"
2020
},
2121
"keywords": [],
2222
"author": "Matheus Sampaio Queiroga <[email protected]> (https://sirherobrine23.org/)",

packages/debian/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
"url": "https://github.com/Sirherobrine23/coreUtils/issues"
1515
},
1616
"scripts": {
17-
"clean": "tsc --build --clean",
18-
"build": "tsc"
17+
"prepack": "tsc --build --clean && tsc --build",
18+
"postpack": "tsc --build --clean"
1919
},
2020
"keywords": [],
2121
"author": "Matheus Sampaio Queiroga <[email protected]> (https://sirherobrine23.org/)",

packages/descompress/package.json

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"name": "@sirherobrine23/decompress",
3+
"version": "1.0.0",
4+
"description": "",
5+
"type": "module",
6+
"main": "src/index.js",
7+
"types": "src/index.d.ts",
8+
"scripts": {
9+
"prepack": "tsc --build --clean && tsc --build",
10+
"postpack": "tsc --build --clean"
11+
},
12+
"repository": {
13+
"type": "git",
14+
"url": "git+https://github.com/Sirherobrine23/coreUtils.git"
15+
},
16+
"keywords": [
17+
"auto",
18+
"decompress"
19+
],
20+
"author": "Matheus Sampaio Queirora <[email protected]>",
21+
"license": "GPL-2.0-only",
22+
"bugs": {
23+
"url": "https://github.com/Sirherobrine23/coreUtils/issues"
24+
},
25+
"homepage": "https://github.com/Sirherobrine23/coreUtils#readme",
26+
"dependencies": {
27+
"duplexify": "^4.1.2"
28+
},
29+
"devDependencies": {
30+
"@types/duplexify": "^3.6.1"
31+
},
32+
"optionalDependencies": {
33+
"lzma-native": "^8.0.6"
34+
}
35+
}

packages/descompress/src/index.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { peek } from "./peek.js";
2+
import stream from "stream";
3+
4+
function isDeflate(buf: Buffer) {
5+
if (!buf || buf.length < 2) return false;
6+
return buf[0] === 0x78 && (buf[1] === 1 || buf[1] === 0x9c || buf[1] === 0xda);
7+
}
8+
9+
function isGzip(buf: Buffer) {
10+
if (!buf || buf.length < 3) return false;
11+
return buf[0] === 0x1F && buf[1] === 0x8B && buf[2] === 0x08;
12+
}
13+
14+
function isXz(buf: Buffer) {
15+
if (!buf || buf.length < 5) return false;
16+
return (buf[0] === 0xFD) && (buf[1] === 0x37) && (buf[2] === 0x7A) && (buf[3] === 0x58) && (buf[4] === 0x5A);
17+
}
18+
19+
export default decompress;
20+
/**
21+
* auto detect compress if is match to Xz/Lzma, gzip or deflate pipe and decompress else echo Buffer
22+
*/
23+
export function decompress() {
24+
return peek({newLine: false, maxBuffer: 10}, async (data, swap) => {
25+
if (isDeflate(data)) import("zlib").then(({createInflate}) => swap(null, createInflate())).catch(err => swap(err));
26+
else if (isGzip(data)) import("zlib").then(({createGunzip}) => swap(null, createGunzip())).catch(err => swap(err));
27+
else if (isXz(data)) import("lzma-native").then(({createDecompressor}) => swap(null, createDecompressor())).catch(err => swap(err));
28+
else swap(null, new stream.PassThrough());
29+
});
30+
}

packages/descompress/src/peek.ts

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import { PassThrough, Transform } from "stream";
2+
import duplexify from "duplexify";
3+
4+
function isObject(data: Buffer) {
5+
return !Buffer.isBuffer(data) && typeof data !== 'string';
6+
}
7+
8+
export interface options {
9+
maxBuffer?: number;
10+
newLine?: boolean;
11+
strict?: any
12+
}
13+
14+
export type onpeek = (data: Buffer, swap: (err?: any, str?: Transform) => void) => void
15+
16+
export function peek(opts: number): duplexify.Duplexify;
17+
export function peek(onpeek: onpeek): duplexify.Duplexify;
18+
export function peek(): duplexify.Duplexify;
19+
export function peek(opts: options, onpeek: onpeek): duplexify.Duplexify;
20+
export function peek(opts?: options|number|onpeek, onpeek?: onpeek): duplexify.Duplexify {
21+
if (typeof opts === "number") opts = {maxBuffer: opts};
22+
if (typeof opts === "function") return peek(null, opts)
23+
if (!opts) opts = {};
24+
const maxBuffer = typeof opts.maxBuffer === "number" ? opts.maxBuffer : 65535;
25+
const newline = opts.newLine !== false;
26+
const strict = opts.strict;
27+
const dup = duplexify.obj();
28+
let buffer = [], bufferSize = 0;
29+
30+
function onpreend() {
31+
if (strict) return dup.destroy(new Error('No newline found'));
32+
dup.cork();
33+
return ready(Buffer.concat(buffer), null, (err) => err ? dup.destroy(err) : dup.uncork());
34+
}
35+
36+
function ready(data: Buffer, overflow, cb) {
37+
dup.removeListener("preend", onpreend)
38+
onpeek(data, function(err, parser) {
39+
if (err) return cb(err)
40+
dup.setWritable(parser);
41+
dup.setReadable(parser);
42+
if (data) parser.write(data);
43+
if (overflow) parser.write(overflow);
44+
overflow = buffer = peeker = null; // free the data
45+
return cb();
46+
});
47+
};
48+
49+
var peeker = new PassThrough({
50+
highWaterMark: 1,
51+
transform(chunk, encoding, callback) {
52+
if (isObject(chunk)) return ready(chunk, null, callback)
53+
if (!Buffer.isBuffer(chunk)) chunk = Buffer.from(chunk, encoding);
54+
55+
if (newline) {
56+
var nl = Array.prototype.indexOf.call(chunk, 10);
57+
if (nl > 0 && chunk[nl-1] === 13) nl--;
58+
if (nl > -1) {
59+
buffer.push(chunk.slice(0, nl))
60+
return ready(Buffer.concat(buffer), chunk.slice(nl), callback)
61+
}
62+
}
63+
64+
buffer.push(chunk)
65+
bufferSize += chunk.length
66+
67+
if (bufferSize < maxBuffer) return callback();
68+
if (strict) return callback(new Error("No newline found"));
69+
ready(Buffer.concat(buffer), null, callback)
70+
},
71+
});
72+
73+
dup.on("preend", onpreend);
74+
dup.setWritable(peeker);
75+
return dup;
76+
}

packages/descompress/test/hello.gz

36 Bytes
Binary file not shown.

packages/descompress/test/hello.xz

72 Bytes
Binary file not shown.

packages/descompress/test/index.mjs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { fileURLToPath } from "url";
2+
import { decompress } from "../src/index.js";
3+
import path from "path";
4+
import fs from "fs";
5+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
6+
7+
fs.createReadStream(path.join(__dirname, "hello.gz")).pipe(decompress()).on("data", data => console.log(data.toString()));
8+
fs.createReadStream(path.join(__dirname, "hello.xz")).pipe(decompress()).on("data", data => console.log(data.toString()));

packages/descompress/tsconfig.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"extends": "../../tsconfig.json",
3+
"compilerOptions": {
4+
"composite": true
5+
}
6+
}

packages/docker/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
"url": "https://github.com/Sirherobrine23/coreUtils/issues"
2121
},
2222
"scripts": {
23-
"clean": "tsc --build --clean",
24-
"build": "tsc --build"
23+
"prepack": "tsc --build --clean && tsc --build",
24+
"postpack": "tsc --build --clean"
2525
},
2626
"dependencies": {
2727
"@sirherobrine23/http": "3.5.2",

packages/extends/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
"url": "https://github.com/Sirherobrine23/coreUtils/issues"
1515
},
1616
"scripts": {
17-
"clean": "tsc --build --clean",
18-
"build": "npm run clean && tsc"
17+
"prepack": "tsc --build --clean && tsc --build",
18+
"postpack": "tsc --build --clean"
1919
},
2020
"keywords": [],
2121
"author": "Matheus Sampaio Queiroga <[email protected]> (https://sirherobrine23.org/)",

packages/http/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
"url": "https://github.com/Sirherobrine23/coreUtils/issues"
1515
},
1616
"scripts": {
17-
"clean": "tsc --build --clean",
18-
"build": "tsc"
17+
"prepack": "tsc --build --clean && tsc --build",
18+
"postpack": "tsc --build --clean"
1919
},
2020
"keywords": [],
2121
"author": "Matheus Sampaio Queiroga <[email protected]> (https://sirherobrine23.org/)",

packages/page_index/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
"pageindex": "./src/cli.js"
2121
},
2222
"scripts": {
23-
"clean": "tsc --build --clean",
24-
"build": "tsc"
23+
"prepack": "tsc --build --clean && tsc --build",
24+
"postpack": "tsc --build --clean"
2525
},
2626
"keywords": [],
2727
"dependencies": {

packages/utils/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
"url": "https://github.com/Sirherobrine23/coreUtils/issues"
1515
},
1616
"scripts": {
17-
"clean": "tsc --build --clean",
18-
"build": "tsc"
17+
"prepack": "tsc --build --clean && tsc --build",
18+
"postpack": "tsc --build --clean"
1919
},
2020
"keywords": [],
2121
"author": "Matheus Sampaio Queiroga <[email protected]> (https://sirherobrine23.org/)",

0 commit comments

Comments
 (0)