@naturalcycles/nodejs-lib 13.3.0 → 13.4.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.
@@ -9,6 +9,14 @@ export interface PipelineOptions {
9
9
  * Required to support graceful close when using transformLimit
10
10
  */
11
11
  allowClose?: boolean;
12
+ /**
13
+ * Set to true to allow graceful abort (via AbortSignal).
14
+ * "Graceful" means it'll swallow the AbortError and let the pipeline resolve normally.
15
+ *
16
+ * Default is false - AbortError will be thrown.
17
+ */
18
+ allowGracefulAbort?: boolean;
19
+ signal?: AbortSignal;
12
20
  }
13
21
  /**
14
22
  * Promisified `stream.pipeline`.
@@ -45,7 +45,7 @@ async function _pipeline(streams, opt = {}) {
45
45
  };
46
46
  rest.forEach(s => {
47
47
  // console.log(s)
48
- if (s instanceof AbortableTransform || s.constructor.name === 'DestroyableTransform') {
48
+ if (s instanceof AbortableTransform || s.constructor.name === 'AbortableTransform') {
49
49
  // console.log(`found ${s.constructor.name}, setting props`)
50
50
  ;
51
51
  s.sourceReadable = sourceReadable;
@@ -54,13 +54,17 @@ async function _pipeline(streams, opt = {}) {
54
54
  });
55
55
  }
56
56
  try {
57
- return await (0, promises_1.pipeline)(first, ...rest);
57
+ return await (0, promises_1.pipeline)([first, ...rest], opt);
58
58
  }
59
59
  catch (err) {
60
60
  if (opt.allowClose && err?.code === 'ERR_STREAM_PREMATURE_CLOSE') {
61
61
  console.log('_pipeline closed (as expected)');
62
62
  return;
63
63
  }
64
+ if (opt.allowGracefulAbort && err?.name === 'AbortError') {
65
+ console.log('_pipeline closed via AbortSignal (as expected)');
66
+ return;
67
+ }
64
68
  // console.log(`_pipeline error`, err)
65
69
  throw err;
66
70
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@naturalcycles/nodejs-lib",
3
- "version": "13.3.0",
3
+ "version": "13.4.0",
4
4
  "scripts": {
5
5
  "prepare": "husky install",
6
6
  "docs-serve": "vuepress dev docs",
@@ -11,6 +11,16 @@ export interface PipelineOptions {
11
11
  * Required to support graceful close when using transformLimit
12
12
  */
13
13
  allowClose?: boolean
14
+
15
+ /**
16
+ * Set to true to allow graceful abort (via AbortSignal).
17
+ * "Graceful" means it'll swallow the AbortError and let the pipeline resolve normally.
18
+ *
19
+ * Default is false - AbortError will be thrown.
20
+ */
21
+ allowGracefulAbort?: boolean
22
+
23
+ signal?: AbortSignal
14
24
  }
15
25
 
16
26
  /**
@@ -55,7 +65,7 @@ export async function _pipeline(streams: AnyStream[], opt: PipelineOptions = {})
55
65
 
56
66
  rest.forEach(s => {
57
67
  // console.log(s)
58
- if (s instanceof AbortableTransform || s.constructor.name === 'DestroyableTransform') {
68
+ if (s instanceof AbortableTransform || s.constructor.name === 'AbortableTransform') {
59
69
  // console.log(`found ${s.constructor.name}, setting props`)
60
70
  ;(s as AbortableTransform).sourceReadable = sourceReadable
61
71
  ;(s as AbortableTransform).streamDone = streamDone
@@ -64,12 +74,18 @@ export async function _pipeline(streams: AnyStream[], opt: PipelineOptions = {})
64
74
  }
65
75
 
66
76
  try {
67
- return await pipeline(first, ...(rest as any[]))
77
+ return await pipeline([first, ...rest], opt)
68
78
  } catch (err) {
69
79
  if (opt.allowClose && (err as any)?.code === 'ERR_STREAM_PREMATURE_CLOSE') {
70
80
  console.log('_pipeline closed (as expected)')
71
81
  return
72
82
  }
83
+
84
+ if (opt.allowGracefulAbort && (err as any)?.name === 'AbortError') {
85
+ console.log('_pipeline closed via AbortSignal (as expected)')
86
+ return
87
+ }
88
+
73
89
  // console.log(`_pipeline error`, err)
74
90
  throw err
75
91
  }