.compression()
Access:
public

This is the compression class. It'll help you with compressing data sync or async. More information about this compression can be found in the tutorials.

Summary

Name
Description
Asynchronously compresses the inserted data.
Returns the compression.constants class.
Forces the compression stream to write all data to the memory.
Destroys the compression stream.
end
Ends the compression stream after writing data.
Returns a promise that's called for every event.
Opens the compression stream.
Returns the settings class.
Returns the compression stream.
Stops forcing the compression stream to write all data to the memory.
Writes data to the compression stream.

Functions

.compress(data, options)
Access:
public
Summary:
Asynchronously compresses the inserted data.

With this function you can asynchronously compress data. This means this function will immediately compress the data instead of having to use a stream. The data you want to compress is specified with the data parameter. You can also override the settings using the opts parameter. This function will return a promise that calls the "data" event when the data has been compressed, with the data as first argument, and the "error" event when there was an error while compressing the data.

Parameters

Name
Type
Description
data
param
The data that has to be compressed.
options
Object
The settings you want to override. (optional)
options.encoding
Object
The encoding you've used for the buffer. (optional) (child)

Examples

var compression = cerus.compression();
compression.compress("example string");
// -> this function will compress the string "example string"

Returns

Type
Promise
Description
This function will return a promise.
.constants()
Access:
public
Summary:
Returns the compression.constants class.

This function returns the compression.constants class. If the class hasn't been created yet, it will create it.

Returns

Type
Class
Description
Returns the constants class.
.cork()
Access:
public
Summary:
Forces the compression stream to write all data to the memory.

With this function you can force the compression stream to buffer all the data to the memory. By doing this a situation can be avoided where a backup would be created when a lot of small chunks of data are added to the internal buffer. When this happens performance will be heavily impacted. This behaviour is stopped using the .uncork() function or when the stream is ended. This function will throw an error when the stream hasn't been started yet.

.destroy(error)
Access:
public
Summary:
Destroys the compression stream.

With this function you can destroy the compression stream. Destroying it is basically ending it with a possible error. This error can be inserted using the error parameter. You can get this error from the "error" event. This function will throw an error when the stream hasn't started yet.

Parameters

Name
Type
Description
error
Error
The error that will be used in the "error" event. (optional)

Examples

var compression = cerus.compression();
compression.open();
compression.destroy(new Error("destroyed"));
// -> this function will destroy the compression stream and throw the error "destroyed"
.end(chunk, encoding)
Access:
public
Summary:
Ends the compression stream after writing data.

This function will end the compression stream. While ending the stream you can also supply the last data that will be written to the stream. The data you want to write to the stream is specified with the chunk parameter. You can also set the encoding it will be written in using the encoding parameter. This function will throw an error when the stream hasn't been opened yet when this function is called. This function will also return a promise that calls the "ended" event when the stream has succesfully ended and all the data has been flushed.

Parameters

Name
Type
Description
chunk
param
The last data to write to the stream.
encoding
String
The encoding the data will be written in. (optional)

Examples

var compression = cerus.compression();
compression.open();
compression.end("data");
// -> this function will write the string "data" to the compression stream end then end the stream

Returns

Type
Promise
Description
This function will return a promise.
.events()
Access:
public
Summary:
Returns a promise that's called for every event.

This function returns a promise that will call a number of events. The "drain" event is called when something has been written to the stream and the stream is ready to be written to again. The "error" event is called when there was an error somewhere in the stream. The "finish" event is called when the compression.end() function has been called. The "pipe" event is called when the .pipe() function is used on a readable stream and the "unpipe" event when the .unpipe() function is used. This function will throw an error when when the stream has not been opened yet.

Returns

Type
Promise
Description
Returns a promise.
.open(options)
Access:
public
Summary:
Opens the compression stream.

With this function you can open the compression stream. Opening it means you can start writing data, which will be compressed. You can also add the options you want. They will be overwritten by the settings if you don't add them.

Parameters

Name
Type
Description
options
Object
The options for opening the stream. (optional) (default: {})
.settings()
Access:
public
Summary:
Returns the settings class.

This function returns the settings class. With this class you can change the settings for how the data will be compressed. You can also change the compression type with this class.

.stream()
Access:
public
Summary:
Returns the compression stream.

This function will return the compression stream. It will be undefined until you've opened it. This stream is used for async compression. Use .compress() when you want to compress it synchronously.

Returns

Type
Stream
Description
The compression stream.
.uncork()
Access:
public
Summary:
Stops forcing the compression stream to write all data to the memory.

Using this function you can stop the behaviour that was started using the .cork() method. This can also be done by ending the compression stream. This function will throw an error when the stream hasn't been started yet.

.write(chunk, encoding)
Access:
public
Summary:
Writes data to the compression stream.

With this function you can write to the compression stream. What you write to the compression stream is defined with the chunk parameter. You can also change the encoding with the encoding parameter. There will be an error of the stream has not been opened yet. This function will return a promise that calls the "written" event when the data has been written to the compression stream.

Parameters

Name
Type
Description
chunk
param
The data to write to the stream.
encoding
String
The encoding the data will be written in. (optional)

Examples

var compression = cerus.compression();
compression.open();
compression.write("data");
// -> this function will write the string "data" to the compression stream

Returns

Type
Promise
Description
This function will return a promise.