@miso.ai/server-commons 0.6.0-beta.1 → 0.6.2-beta.0
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 +1 -1
- package/src/stream/diff.js +96 -0
- package/src/stream/index.js +1 -0
- package/src/stream/misc.js +16 -0
- package/src/string.js +8 -0
package/package.json
CHANGED
|
@@ -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
|
+
}
|
package/src/stream/index.js
CHANGED
|
@@ -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';
|
package/src/stream/misc.js
CHANGED
|
@@ -60,3 +60,19 @@ export function transform(fn, { transform: _, ...options } = {}) {
|
|
|
60
60
|
},
|
|
61
61
|
});
|
|
62
62
|
}
|
|
63
|
+
|
|
64
|
+
export function take(n, { transform: _, ...options } = {}) {
|
|
65
|
+
let count = 0;
|
|
66
|
+
return new Transform({
|
|
67
|
+
...options,
|
|
68
|
+
transform (chunk, _, next) {
|
|
69
|
+
if (count > n) {
|
|
70
|
+
next(undefined);
|
|
71
|
+
this.end();
|
|
72
|
+
} else {
|
|
73
|
+
next(undefined, chunk);
|
|
74
|
+
}
|
|
75
|
+
count++;
|
|
76
|
+
}
|
|
77
|
+
});
|
|
78
|
+
}
|
package/src/string.js
CHANGED
|
@@ -30,3 +30,11 @@ export function padLeft(val, num, str) {
|
|
|
30
30
|
export function padRight(val, num, str) {
|
|
31
31
|
return pad(false, val, num, str);
|
|
32
32
|
}
|
|
33
|
+
|
|
34
|
+
export function encodeURIIfNecessary(str) {
|
|
35
|
+
return typeof str === 'string' && !str.includes('%') ? encodeURI(str) : str;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export function encodeURIComponentIfNecessary(str) {
|
|
39
|
+
return typeof str === 'string' && !str.includes('%') ? encodeURIComponent(str) : str;
|
|
40
|
+
}
|