1 | # good-squeeze
|
2 |
|
3 | Simple transform streams useful in creating [good](https://github.com/hapijs/good) data pipelines.
|
4 |
|
5 | [![Build Status](https://travis-ci.org/hapijs/good-squeeze.svg?branch=master&style=flat)](https://travis-ci.org/hapijs/good-squeeze)
|
6 | ![Current Version](https://img.shields.io/npm/v/good-squeeze.svg?style=flat)
|
7 |
|
8 | Lead Maintainer: [Adam Bretz](https://github.com/arb)
|
9 |
|
10 | ## Usage
|
11 |
|
12 | good-squeeze is a collection of small transform streams. The `Squeeze` stream is useful for filtering events based on the good event options. The `SafeJson` stream is useful for stringifying objects to prevent circular object errors.
|
13 |
|
14 | ## Methods
|
15 |
|
16 | ### `Squeeze(events, [options])`
|
17 |
|
18 | Creates a new Squeeze transform stream where:
|
19 |
|
20 | - `events` an object where each key is a valid good event and the value is one of the following:
|
21 | - `string` - a tag to include when filtering. '*' indicates no filtering.
|
22 | - `array` - array of tags to filter. `[]` indicates no filtering.
|
23 | - `object` - an object with the following values
|
24 | - `include` - string or array representing tag(s) to *include* when filtering
|
25 | - `exclude` - string or array representing tag(s) to *exclude* when filtering. `exclude` takes precedence over any `include` tags.
|
26 | - `[options]` configuration object that gets passed to the Node [`Stream.Transform`](http://nodejs.org/api/stream.html#stream_class_stream_transform) constructor. **Note** `objectMode` is always `true` for all `Squeeze` objects.
|
27 |
|
28 | The transform stream only passes on events that satisfy the event filtering based on event name and tags. If the upstream event doesn't satisfy the filter, it is not continued down the pipe line.
|
29 |
|
30 | ### `Squeeze.subscription(events)`
|
31 |
|
32 | A static method on `Squeeze` that creates a new event subscription map where:
|
33 |
|
34 | - `events` the same arguments used in the `Squeeze` constructor.
|
35 |
|
36 | ```js
|
37 | const Squeeze = require('good-squeeze');
|
38 |
|
39 | Squeeze.subscription({ log: 'user', ops: '*', request: ['hapi', 'foo'] });
|
40 |
|
41 | // Results in
|
42 | // {
|
43 | // log: { include: [ 'user' ], exclude: [] },
|
44 | // ops: { include: [], exclude: [] },
|
45 | // request: { include: [ 'hapi', 'foo' ], exclude: [] }
|
46 | // }
|
47 |
|
48 | Squeeze.subscription({ log: 'user', ops: { exclude: 'debug' }, request: { include: ['hapi', 'foo'], exclude: 'sensitive' } });
|
49 |
|
50 | // Results in
|
51 | // {
|
52 | // log: { include: [ 'user' ], exclude: [] },
|
53 | // ops: { include: [], exclude: [ 'debug' ] },
|
54 | // request: { include: [ 'hapi', 'foo' ], exclude: [ 'sensitive' ] }
|
55 | // }
|
56 | ```
|
57 |
|
58 | Useful for creating an event subscription to be used with `Squeeze.filter` if you do not plan on creating a pipeline coming from good and instead want to manage event filtering manually.
|
59 |
|
60 |
|
61 | ### `Squeeze.filter(subscription, data)`
|
62 |
|
63 | Returns `true` if the supplied `data.event` + `data.tags` should be reported based on `subscription` where:
|
64 |
|
65 | - `subscription` - a subscription map created by `Squeeze.subscription()`.
|
66 | - `data` - event object emitted from good/hapi which should contain the following keys:
|
67 | - `event` - a string representing the event name of `data`
|
68 | - `tags` - an array of strings representing tags associated with this event.
|
69 |
|
70 | ### `SafeJson([options], [stringify])`
|
71 |
|
72 | Creates a new SafeJson transform stream where:
|
73 |
|
74 | - `[options]` configuration object that gets passed to the Node [`Stream.Transform`](http://nodejs.org/api/stream.html#stream_class_stream_transform) constructor. **Note** `objectMode` is always `true` for all `Squeeze` objects.
|
75 | - `[stringify]` configuration object for controlling how stringify is handled.
|
76 | - `separator` - string to append to each message. Defaults to "\n".
|
77 | - `space` - number of spaces in pretty printed JSON. Doesn't pretty print when set to 0. Defaults to 0.
|
78 |
|
79 | The transform stream stringifys the incoming data and pipes it forward. It will not crash in the cases of circular references and will instead include a "~Circular" string in the result.
|