You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: examples/gzip-stream/README.md
+15-54Lines changed: 15 additions & 54 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -25,82 +25,43 @@ The `flush` method allows any internally buffered data to be processed before co
25
25
26
26
```js
27
27
functioncompress() {
28
-
constcompressor=compressNew();
28
+
conststream=newCompressStream();
29
29
30
30
returnnewTransform({
31
31
transform(chunk, encoding, callback) {
32
-
compressChunk(compressor, encoding, chunk)
32
+
stream
33
+
.compress(encoding, chunk)
33
34
.then(data=>callback(null, data))
34
35
.catch(callback);
35
36
},
36
37
37
38
flush(callback) {
38
-
compressFinish(compressor)
39
+
stream
40
+
.finish()
39
41
.then(data=>callback(null, data))
40
42
.catch(callback);
41
43
}
42
44
});
43
45
}
44
46
```
45
47
46
-
The glue code exports a single function `compress`that creates a `Transform` stream delegating the implementation to Neon functions. Since these functions return promises, they are adapted to the `callback` style continuation that `Transform` expects.
48
+
The glue code exports a single function `compress`responsible for creating a `Transform` stream, delegating to the `CompressStream` class and adapting [`Promise`s](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) to callbacks.
47
49
48
50
## Neon
49
51
50
-
The Neon module exports three functions:
52
+
The Neon module exports a `CompressSteam` class with two methods:
Creates a new instance of the `CompressStream` class with an optional gzip level.
61
60
62
-
Ok(cx.boxed(stream))
63
-
}
64
-
```
65
-
66
-
`compressNew` creates an instance of the stateful Rust struct, `CompressStream`, and returns it wrapped in a [`JsBox`](https://docs.rs/neon/latest/neon/types/struct.JsBox.html). Each of the other two methods expects `CompressStream` as the first argument. This pattern is similar to using [`Function.prototype.call`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/call) on a class method to manually bind `this`.
`compressChunk` accepts the instance of the `CompressStream` struct and the other arguments to the [`transform`](#transformchunk-encoding-callback) function. The chunk is cloned to a `Vec<u8>` and passed to a task to execute on the Node worker pool. The asynchronous task compresses the data and passes the compressed data to the `.promise(|cx, result| { ... })` callback. The callback to `promise` is executed on the JavaScript main thread and converts the compressed `Vec<u8>` to a `JsBuffer` and resolves the promise.
86
-
87
-
`CompressChunk::and_buffer` is used to create a `Buffer`. `ArrayBuffer` cannot be used because stream chunks are required to be an instance of `Uint8Array`. `Buffer` is a subclass of `Uint8Array`.
`compressFinish` works very similar to [`compressChunkl`](#compresschunkcompressstream-chunk-encoding-callback), except it is provided the arguments to [`flush`](#flushcallback) which does not include any data. Instead, the remaining buffered data is compressed, a CRC is calculated, and the compressed gzip data is completed.
67
+
`finish` works very similar to [`compress`](#compresschunk-encoding), except it is provided the arguments to [`flush`](#flushcallback) which does not include any data. Instead, the remaining buffered data is compressed, a CRC is calculated, and the compressed gzip data is completed.
0 commit comments