pausing-transform 1.0.0 → 1.0.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.
@@ -0,0 +1,17 @@
1
+ {
2
+ // Use IntelliSense to learn about possible attributes.
3
+ // Hover to view descriptions of existing attributes.
4
+ // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5
+ "version": "0.2.0",
6
+ "configurations": [
7
+ {
8
+ "name": "Attach",
9
+ "port": 9229,
10
+ "request": "attach",
11
+ "skipFiles": [
12
+ "<node_internals>/**"
13
+ ],
14
+ "type": "node"
15
+ }
16
+ ]
17
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pausing-transform",
3
- "version": "1.0.0",
3
+ "version": "1.0.2",
4
4
  "description": "A transform stream that will buffer data written until flushed.",
5
5
  "main": "pausing-transform.js",
6
6
  "scripts": {
@@ -8,13 +8,21 @@ class PausingTransform extends Transform {
8
8
  this.transformer = transformer
9
9
  }
10
10
  _transform(chunk, encoding, callback) {
11
-
12
11
  return callback(null, this.transformer ? this.transformer(chunk.toString()) : chunk)
13
12
  }
14
13
  _flush(callback) {
15
- let ret = super.write(this.pausedData, null, callback)
16
- this.pausedData = ''
17
- return ret
14
+ if(this.pausedData) {
15
+ let p = this.pausedData
16
+ this.pausedData = ''
17
+ let ret = super.write(p, null, () => {
18
+ if(callback) {
19
+ return callback()
20
+ }
21
+ })
22
+ }
23
+ else if(callback) {
24
+ callback()
25
+ }
18
26
  }
19
27
  write(chunk, encoding, callback) {
20
28
  if(this.paused) {
@@ -34,6 +42,16 @@ class PausingTransform extends Transform {
34
42
  this.paused = false
35
43
  this._flush()
36
44
  }
45
+ end() {
46
+ if(this.pausedData) {
47
+ this._flush(() => {
48
+ super.end()
49
+ })
50
+ }
51
+ else {
52
+ super.end()
53
+ }
54
+ }
37
55
  }
38
56
 
39
- module.exports = PausingTransform
57
+ module.exports = PausingTransform
@@ -0,0 +1,70 @@
1
+ import test from 'node:test';
2
+ import assert from 'node:assert'
3
+ import wait from './wait.mjs';
4
+
5
+ import PausingTransform from '../pausing-transform.js'
6
+
7
+ test("basics", async (t) => {
8
+
9
+ await t.test('paused and run', async (t) => {
10
+ let final = new PausingTransform()
11
+ let head = new PausingTransform()
12
+
13
+ head.pipe(final)
14
+
15
+ head.write('hello')
16
+
17
+ assert.equal(final.pausedData, '', 'Paused data should be empty.')
18
+ assert.equal(head.pausedData, 'hello', 'Paused data should be empty.')
19
+
20
+ head.run()
21
+ await wait(300)
22
+ assert.equal(head.pausedData, '', 'Paused data should be empty.')
23
+ assert.equal(final.pausedData, 'hello', 'Paused data should be empty.')
24
+
25
+
26
+ })
27
+
28
+ await t.test('transform', async (t) => {
29
+ let final = new PausingTransform()
30
+ let head = new PausingTransform(msg => msg.toUpperCase())
31
+
32
+ head.pipe(final)
33
+
34
+ head.write('hello')
35
+
36
+ assert.equal(final.pausedData, '', 'Paused data should be empty.')
37
+ assert.equal(head.pausedData, 'hello', 'Paused data should be empty.')
38
+
39
+ head.run()
40
+ await wait(300)
41
+ assert.equal(head.pausedData, '', 'Paused data should be empty.')
42
+ assert.equal(final.pausedData, 'HELLO', 'Paused data should be empty.')
43
+ })
44
+
45
+ await t.test('unpipe', async (t) => {
46
+ let final = new PausingTransform()
47
+ let head = new PausingTransform(msg => msg.toUpperCase())
48
+
49
+ head.pipe(final)
50
+
51
+ head.write('hello')
52
+
53
+ assert.equal(final.pausedData, '', 'Paused data should be empty.')
54
+ assert.equal(head.pausedData, 'hello', 'Final data does not match.')
55
+
56
+ head.run()
57
+ await wait(300)
58
+ assert.equal(head.pausedData, '', 'Paused data should be empty.')
59
+ assert.equal(final.pausedData, 'HELLO', 'Final data does not match.')
60
+
61
+ head.unpipe()
62
+ await wait(300)
63
+ head.run()
64
+ head.pipe(final)
65
+
66
+ head.write('there')
67
+ await wait(300)
68
+ assert.equal(final.pausedData, 'HELLOTHERE', 'Final data does not match.')
69
+ })
70
+ })
package/test/wait.mjs ADDED
@@ -0,0 +1,6 @@
1
+ export default async function wait(millis) {
2
+ let pr = new Promise((resolve, reject) => {
3
+ setTimeout(resolve, millis)
4
+ })
5
+ return pr
6
+ }