@miso.ai/server-commons 0.6.0 → 0.6.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -20,5 +20,5 @@
20
20
  "uuid": "^9.0.0",
21
21
  "yargs": "^17.5.1"
22
22
  },
23
- "version": "0.6.0"
23
+ "version": "0.6.2"
24
24
  }
@@ -0,0 +1,96 @@
1
+ import { Transform } from 'stream';
2
+
3
+ const OUTPUT = {
4
+ BOTH: 0,
5
+ PLUS: 1,
6
+ MINUS: 2,
7
+ };
8
+
9
+ export default class DiffStream extends Transform {
10
+
11
+ constructor(data, {
12
+ objectMode,
13
+ readableObjectMode,
14
+ writableObjectMode,
15
+ ...options
16
+ } = {}) {
17
+ super({
18
+ readableObjectMode: !!writableObjectMode || (readableObjectMode !== undefined ? !!readableObjectMode : objectMode !== undefined ? !!objectMode : false),
19
+ writableObjectMode: writableObjectMode !== undefined ? !!writableObjectMode : objectMode !== undefined ? !!objectMode : false,
20
+ });
21
+ if (writableObjectMode && readableObjectMode !== undefined && !readableObjectMode) {
22
+ throw new Error(`Cannot have writableObjectMode = true and readableObjectMode = false.`);
23
+ }
24
+ this._options = this._normalizeOptions(options);
25
+ this._baseDataSet = new Set(data);
26
+ this._inputDataSet = new Set();
27
+ }
28
+
29
+ _normalizeOptions({
30
+ output,
31
+ ...options
32
+ }) {
33
+ return {
34
+ output: this._normalizeOutputOptions(output),
35
+ ...options,
36
+ };
37
+ }
38
+
39
+ _normalizeOutputOptions(output = 'default') {
40
+ switch (output) {
41
+ case 'plus':
42
+ return OUTPUT.PLUS;
43
+ case 'minus':
44
+ return OUTPUT.MINUS;
45
+ case 'default':
46
+ return OUTPUT.BOTH;
47
+ default:
48
+ throw new Error(`Unrecognized output mode: ${output}`);
49
+ }
50
+ }
51
+
52
+ async _transform(value, _) {
53
+ if (value instanceof Buffer) {
54
+ value = value.toString();
55
+ }
56
+ // dedupe
57
+ const input = this._inputDataSet;
58
+ if (input.has(value)) {
59
+ return;
60
+ }
61
+ input.add(value);
62
+
63
+ const base = this._baseDataSet;
64
+ if (base.has(value)) {
65
+ base.delete(value);
66
+ } else {
67
+ this._output(true, value);
68
+ }
69
+ }
70
+
71
+ async _flush(done) {
72
+ for (const value of this._baseDataSet) {
73
+ this._output(false, value);
74
+ }
75
+ done();
76
+ }
77
+
78
+ _output(plus, value) {
79
+ switch (this._options.output) {
80
+ case OUTPUT.BOTH:
81
+ this.push(this.readableObjectMode ? [plus, value] : `${plus ? '+' : '-'} ${value}`);
82
+ break;
83
+ case OUTPUT.PLUS:
84
+ if (plus) {
85
+ this.push(value);
86
+ }
87
+ break;
88
+ case OUTPUT.MINUS:
89
+ if (!plus) {
90
+ this.push(value);
91
+ }
92
+ break;
93
+ }
94
+ }
95
+
96
+ }
@@ -3,3 +3,4 @@ export { default as BufferedReadStream } from './buffered-read.js';
3
3
  export { default as BufferedWriteStream } from './buffered-write.js';
4
4
  export { default as OutputStream } from './output.js';
5
5
  export { default as LogUpdateStream } from './log-update.js';
6
+ export { default as DiffStream } from './diff.js';