Skip to content

Add github actions and Typescript declarations #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: Build test
on:
push:
branches:
- master

jobs:
nodejs:
strategy:
matrix:
nodejs:
- lts/*
- latest
os:
- ubuntu
- windows
- macos
runs-on: ${{ matrix.os }}-latest
steps:
- uses: actions/checkout@v3
name: Get source
with:
submodules: true

- uses: actions/setup-node@v3
name: Setup Node.js ${{ matrix.nodejs }}
with:
node-version: ${{ matrix.nodejs }}

- name: Install depencies
run: npm install

- run: npm run rebuild
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,16 @@
# Editors
.vscode

# Build
/deps
!/deps/zstd
!/deps/zstd.gyp

# Node Related Folders
build
/*.tgz
node_modules
/*lock*
.yarn
coverage
.nyc_output
11 changes: 0 additions & 11 deletions .travis.yml

This file was deleted.

1 change: 0 additions & 1 deletion .yarnrc.yml

This file was deleted.

233 changes: 233 additions & 0 deletions index.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,233 @@
const { promisify } = require('util');
const bindings = require('bindings');
const stream = require('stream');
const util = require('util');
const compressor = bindings('compressor.node');
const decompressor = bindings('decompressor.node');
const Transform = stream.Transform;

module.exports.compress = promisify(function(input, params, cb) {
if (arguments.length === 2) {
cb = params;
params = {};
}
if (!Buffer.isBuffer(input)) {
process.nextTick(cb, new Error('Input is not a buffer.'));
return;
}
if (typeof cb !== 'function') {
process.nextTick(cb, new Error('Second argument is not a function.'));
return;
}
var stream = new TransformStreamCompressor(params);
var chunks = [];
var length = 0;
stream.on('error', cb);
stream.on('data', function(c) {
chunks.push(c);
length += c.length;
});
stream.on('end', function() {
cb(null, Buffer.concat(chunks, length));
});
stream.end(input);
});

module.exports.decompress = promisify(function (input, params, cb) {
if (arguments.length === 2) {
cb = params;
params = {};
}
if (!Buffer.isBuffer(input)) {
process.nextTick(cb, new Error('Input is not a buffer.'));
return;
}
if (typeof cb !== 'function') {
process.nextTick(cb, new Error('Second argument is not a function.'));
return;
}
var stream = new TransformStreamDecompressor(params);
var chunks = [];
var length = 0;
stream.on('error', cb);
stream.on('data', function(c) {
chunks.push(c);
length += c.length;
});
stream.on('end', function() {
cb(null, Buffer.concat(chunks, length));
});
stream.end(input);
});

module.exports.compressSync = compressSync;
function compressSync(input, params) {
if (!Buffer.isBuffer(input)) {
throw new Error('Input is not a buffer.');
}
var stream = new TransformStreamCompressor(params || {}, true);
var chunks = [];
var length = 0;
stream.on('error', function(e) {
throw e;
});
stream.on('data', function(c) {
chunks.push(c);
length += c.length;
});
stream.end(input);
return Buffer.concat(chunks, length);
}

module.exports.decompressSync = decompressSync;
function decompressSync(input, params) {
if (!Buffer.isBuffer(input)) {
throw new Error('Input is not a buffer.');
}
var stream = new TransformStreamDecompressor(params || {}, true);
var chunks = [];
var length = 0;
stream.on('error', function(e) {
throw e;
});
stream.on('data', function(c) {
chunks.push(c);
length += c.length;
});
stream.end(input);
return Buffer.concat(chunks, length);
}

module.exports.TransformStreamCompressor = TransformStreamCompressor;
function TransformStreamCompressor(params, sync) {
Transform.call(this, params);

this.compressor = new compressor.StreamCompressor(params || {});
this.sync = sync || false;
var blockSize = this.compressor.getBlockSize();
this.status = {
blockSize: blockSize,
remaining: blockSize
};
}
util.inherits(TransformStreamCompressor, Transform);

TransformStreamCompressor.prototype._transform = function(chunk, encoding, next) {
compressStreamChunk(this, chunk, this.compressor, this.status, this.sync, next);
};

TransformStreamCompressor.prototype._flush = function(done) {
var that = this;
this.compressor.compress(true, function(err, output) {
if (err) {
return done(err);
}
if (output) {
for (var i = 0; i < output.length; i++) {
that.push(output[i]);
}
}
return done();
}, !this.sync);
};

// We need to fill the blockSize for better compression results
module.exports.compressStreamChunk = compressStreamChunk;
function compressStreamChunk(stream, chunk, compressor, status, sync, done) {
var length = chunk.length;

if (length > status.remaining) {
var slicedChunk = chunk.slice(0, status.remaining);
chunk = chunk.slice(status.remaining);
status.remaining = status.blockSize;

compressor.copy(slicedChunk);
compressor.compress(false, function(err, output) {
if (err) {
return done(err);
}
if (output) {
for (var i = 0; i < output.length; i++) {
stream.push(output[i]);
}
}
return compressStreamChunk(stream, chunk, compressor, status, sync, done);
}, !sync);
} else if (length <= status.remaining) {
status.remaining -= length;
compressor.copy(chunk);
return done();
}
}

module.exports.compressStream = compressStream;
function compressStream(params) {
return new TransformStreamCompressor(params);
}

module.exports.TransformStreamDecompressor = TransformStreamDecompressor;
function TransformStreamDecompressor(params, sync) {
Transform.call(this, params);

this.decompressor = new decompressor.StreamDecompressor(params || {});
this.sync = sync || false;
var blockSize = this.decompressor.getBlockSize();
this.status = {
blockSize: blockSize,
remaining: blockSize
};
}
util.inherits(TransformStreamDecompressor, Transform);

TransformStreamDecompressor.prototype._transform = function(chunk, encoding, next) {
decompressStreamChunk(this, chunk, this.decompressor, this.status, this.sync, next);
};

TransformStreamDecompressor.prototype._flush = function(done) {
var that = this;
this.decompressor.decompress(function(err, output) {
if (err) {
return done(err);
}
if (output) {
for (var i = 0; i < output.length; i++) {
that.push(output[i]);
}
}
return done();
}, !this.sync);
};

// We need to fill the blockSize for better compression results
module.exports.decompressStreamChunk = decompressStreamChunk;
function decompressStreamChunk(stream, chunk, decompressor, status, sync, done) {
var length = chunk.length;

if (length > status.remaining) {
var slicedChunk = chunk.slice(0, status.remaining);
chunk = chunk.slice(status.remaining);
status.remaining = status.blockSize;

decompressor.copy(slicedChunk);
decompressor.decompress(function(err, output) {
if (err) {
return done(err);
}
if (output) {
for (var i = 0; i < output.length; i++) {
stream.push(output[i]);
}
}
return decompressStreamChunk(stream, chunk, decompressor, status, sync, done);
}, !sync);
} else if (length <= status.remaining) {
status.remaining -= length;
decompressor.copy(chunk);
return done();
}
}

module.exports.decompressStream = decompressStream;
function decompressStream(params) {
return new TransformStreamDecompressor(params);
};
33 changes: 33 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { Transform } from "stream";
export interface ZstdOptions {
level?: 1|2|3|4|5|6|7|8|9;
dict?: string;
}

export function decompressStream(params?: ZstdOptions): Transform;
export function compressStream(params?: ZstdOptions): Transform;

/**
* Descompress zst file blocking loop event
*
* @param input - Zst Buffer file
* @param params - Zstd options
*/
export function decompressSync(input: Buffer, params?: ZstdOptions): Buffer;

/**
* Compress zst file blocking loop event
*
* @param input - Raw file
* @param params - Zstd options
*/
export function compressSync(input: Buffer, params?: ZstdOptions): Buffer;

/**
* Compress file to zst asyn
*
*
* @param input - Raw file
* @param params - Zstd options
*/
export function compress(input: Buffer, params?: ZstdOptions): Promise<Buffer>;
Loading