barnard59-base 2.2.0 → 2.3.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/CHANGELOG.md CHANGED
@@ -1,5 +1,16 @@
1
1
  # barnard59-base
2
2
 
3
+ ## 2.3.0
4
+
5
+ ### Minor Changes
6
+
7
+ - 0c0245d: Bundle TypeScript type declarations
8
+ - 464b09e: Added an operation which creates a readable from given values (closes #199)
9
+
10
+ ### Patch Changes
11
+
12
+ - ba328de: Simplify `base:limit` and `base:offset` by using async generators
13
+
3
14
  ## 2.2.0
4
15
 
5
16
  ### Minor Changes
package/Readable.js ADDED
@@ -0,0 +1,5 @@
1
+ import Readable from 'readable-stream'
2
+
3
+ export function from(...array) {
4
+ return Readable.from(array)
5
+ }
package/combine.d.ts ADDED
@@ -0,0 +1,8 @@
1
+ export default combine;
2
+ /**
3
+ * Limit the amount of chunks in a pipe.
4
+ * @returns {import('stream').Duplex} A transform stream.
5
+ * @param {(import('stream').Duplex)[]} streams
6
+ * @param {*} options
7
+ */
8
+ declare function combine(streams: (import('stream').Duplex)[], options: any): import('stream').Duplex;
package/combine.js CHANGED
@@ -1,5 +1,11 @@
1
1
  import duplexify from 'duplexify'
2
2
 
3
+ /**
4
+ * Limit the amount of chunks in a pipe.
5
+ * @returns {import('stream').Duplex} A transform stream.
6
+ * @param {(import('stream').Duplex)[]} streams
7
+ * @param {*} options
8
+ */
3
9
  function combine(streams, options) {
4
10
  if (streams.length === 0) {
5
11
  throw new Error('no streams to combine')
package/concat.d.ts ADDED
@@ -0,0 +1,15 @@
1
+ export default factory;
2
+ /**
3
+ * @param {(import('stream').Duplex)[]} streams
4
+ * @return {Readable}
5
+ */
6
+ declare function factory(...streams: (import('stream').Duplex)[]): Readable;
7
+ declare namespace factory {
8
+ export { object };
9
+ }
10
+ /**
11
+ * @param {(import('stream').Duplex)[]} streams
12
+ * @return {Readable}
13
+ */
14
+ export function object(...streams: (import('stream').Duplex)[]): Readable;
15
+ import { Readable } from 'readable-stream';
package/concat.js CHANGED
@@ -1,6 +1,12 @@
1
1
  import { finished, Readable } from 'readable-stream'
2
2
 
3
3
  class ConcatStream extends Readable {
4
+ /**
5
+ * @param {(import('stream').Duplex)[]} streams
6
+ * @param {{
7
+ * objectMode?: boolean
8
+ * }} [options]
9
+ */
4
10
  constructor(streams, { objectMode = false } = {}) {
5
11
  super({ objectMode })
6
12
 
@@ -10,6 +16,9 @@ class ConcatStream extends Readable {
10
16
  this.next()
11
17
  }
12
18
 
19
+ /**
20
+ * @return {void|boolean|unknown}
21
+ */
13
22
  _read() {
14
23
  if (!this.current) {
15
24
  return this.push(null)
@@ -37,10 +46,18 @@ class ConcatStream extends Readable {
37
46
  }
38
47
  }
39
48
 
49
+ /**
50
+ * @param {(import('stream').Duplex)[]} streams
51
+ * @return {Readable}
52
+ */
40
53
  function factory(...streams) {
41
54
  return new ConcatStream(streams)
42
55
  }
43
56
 
57
+ /**
58
+ * @param {(import('stream').Duplex)[]} streams
59
+ * @return {Readable}
60
+ */
44
61
  const object = (...streams) => {
45
62
  return new ConcatStream(streams, { objectMode: true })
46
63
  }
package/filter.d.ts ADDED
@@ -0,0 +1,16 @@
1
+ export default filter;
2
+ /**
3
+ * <T>
4
+ */
5
+ export type Filter<T> = (this: import('barnard59-core').Context, chunk: T, encoding: string) => boolean | Promise<boolean>;
6
+ /**
7
+ * @typedef {(this: import('barnard59-core').Context, chunk: T, encoding: string) => boolean | Promise<boolean>} Filter<T>
8
+ * @template T
9
+ */
10
+ /**
11
+ * @template T
12
+ * @this {import('barnard59-core').Context}
13
+ * @param {Filter<T>} func
14
+ * @return {import('stream').Transform}
15
+ */
16
+ declare function filter<T>(this: import("barnard59-core").Context, func: Filter<T>): import('stream').Transform;
package/filter.js CHANGED
@@ -1,5 +1,16 @@
1
1
  import { obj } from 'through2'
2
2
 
3
+ /**
4
+ * @typedef {(this: import('barnard59-core').Context, chunk: T, encoding: string) => boolean | Promise<boolean>} Filter<T>
5
+ * @template T
6
+ */
7
+
8
+ /**
9
+ * @template T
10
+ * @this {import('barnard59-core').Context}
11
+ * @param {Filter<T>} func
12
+ * @return {import('stream').Transform}
13
+ */
3
14
  function filter(func) {
4
15
  return obj((chunk, encoding, callback) => {
5
16
  Promise.resolve().then(() => {
package/flatten.d.ts ADDED
@@ -0,0 +1,17 @@
1
+ export default flatten;
2
+ export type ForEachCallback = (item: unknown) => void;
3
+ export type IterableLike = Iterable<unknown> | {
4
+ forEach(cb: ForEachCallback): void;
5
+ };
6
+ /**
7
+ * @callback ForEachCallback
8
+ * @param {unknown} item
9
+ * @return {void}
10
+ */
11
+ /**
12
+ * @typedef {Iterable<unknown> | { forEach(cb: ForEachCallback): void }} IterableLike
13
+ */
14
+ /**
15
+ * @return {import('stream').Transform} Transform
16
+ */
17
+ declare function flatten(): import('stream').Transform;
package/flatten.js CHANGED
@@ -1,7 +1,20 @@
1
1
  import { obj } from 'through2'
2
2
 
3
+ /**
4
+ * @callback ForEachCallback
5
+ * @param {unknown} item
6
+ * @return {void}
7
+ */
8
+
9
+ /**
10
+ * @typedef {Iterable<unknown> | { forEach(cb: ForEachCallback): void }} IterableLike
11
+ */
12
+
13
+ /**
14
+ * @return {import('stream').Transform} Transform
15
+ */
3
16
  function flatten() {
4
- return obj(function (chunk, encoding, callback) {
17
+ return obj(function (/** IterableLike */ chunk, encoding, callback) {
5
18
  if (typeof chunk[Symbol.iterator] === 'function') {
6
19
  for (const item of chunk) {
7
20
  this.push(item)
@@ -11,7 +24,7 @@ function flatten() {
11
24
  }
12
25
 
13
26
  if (typeof chunk.forEach === 'function') {
14
- chunk.forEach(item => this.push(item))
27
+ chunk.forEach((/** @type {unknown} */ item) => this.push(item))
15
28
 
16
29
  return callback()
17
30
  }
package/forEach.d.ts ADDED
@@ -0,0 +1,40 @@
1
+ export default factory;
2
+ export type ForEachOptions = Pick<import('barnard59-core').Context, 'createPipeline' | 'variables'> & {
3
+ pipeline: PipelineStream;
4
+ variable: string;
5
+ };
6
+ export type PipelineStream = import('stream').Duplex & {
7
+ pipeline: import('barnard59-core').Pipeline;
8
+ };
9
+ /**
10
+ * @this {import('barnard59-core').Context}
11
+ * @param {PipelineStream} pipeline
12
+ * @param {string} variable
13
+ * @return {ForEach}
14
+ */
15
+ declare function factory(this: import("barnard59-core").Context, pipeline: PipelineStream, variable: string): ForEach;
16
+ declare class ForEach extends stream.Duplex {
17
+ /**
18
+ * @param {ForEachOptions} context
19
+ */
20
+ constructor({ createPipeline, pipeline, variable, variables }: ForEachOptions);
21
+ _read(): Promise<boolean | undefined>;
22
+ /**
23
+ * @param {*} chunk
24
+ * @param {string} encoding
25
+ * @param {(error?: (Error | null)) => void} callback
26
+ */
27
+ _write(chunk: any, encoding: string, callback: (error?: (Error | null)) => void): Promise<void>;
28
+ createPipeline: (ptr: import("clownface").GraphPointer, arg?: Partial<import("barnard59-core").PipelineOptions> | undefined) => import("barnard59-core/lib/Pipeline").default;
29
+ ptr: import("clownface").GraphPointer;
30
+ variable: string;
31
+ variables: Map<string, unknown>;
32
+ pull: ReadableToReadable.ReadFunction | null;
33
+ set subPipeline(subPipeline: import("barnard59-core/lib/Pipeline").default);
34
+ /**
35
+ * @type import('barnard59-core').Pipeline
36
+ */
37
+ get subPipeline(): import("barnard59-core/lib/Pipeline").default;
38
+ }
39
+ import stream from 'readable-stream';
40
+ import ReadableToReadable from 'readable-to-readable';
package/forEach.js CHANGED
@@ -1,16 +1,30 @@
1
- import { promisify } from 'util'
1
+ import { promisify } from 'node:util'
2
2
  import { context } from '@opentelemetry/api'
3
3
  import stream from 'readable-stream'
4
4
  import ReadableToReadable from 'readable-to-readable'
5
5
 
6
6
  const { finished, Duplex } = stream
7
7
 
8
+ /**
9
+ * @typedef {Pick<import('barnard59-core').Context, 'createPipeline' | 'variables'> & {
10
+ * pipeline: PipelineStream
11
+ * variable: string
12
+ * }} ForEachOptions
13
+ *
14
+ * @typedef {import('stream').Duplex & {
15
+ * pipeline: import('barnard59-core').Pipeline
16
+ * }} PipelineStream
17
+ */
18
+
8
19
  async function nextLoop() {
9
20
  return new Promise(resolve => setTimeout(resolve, 0))
10
21
  }
11
22
 
12
23
  class ForEach extends Duplex {
13
- constructor({ createPipeline, pipeline, step, variable, variables }) {
24
+ /**
25
+ * @param {ForEachOptions} context
26
+ */
27
+ constructor({ createPipeline, pipeline, variable, variables }) {
14
28
  super({ objectMode: true })
15
29
 
16
30
  // Bind the read and write function to the current context so the trace ID
@@ -18,7 +32,6 @@ class ForEach extends Duplex {
18
32
  this._read = context.bind(context.active(), this._read)
19
33
  this._write = context.bind(context.active(), this._write)
20
34
  this.createPipeline = createPipeline
21
- this.step = step
22
35
 
23
36
  // we only need the ptr of the pipeline to create new copies...
24
37
  this.ptr = pipeline.pipeline.ptr
@@ -31,15 +44,26 @@ class ForEach extends Duplex {
31
44
  this.pull = null
32
45
  }
33
46
 
47
+ /**
48
+ * @type import('barnard59-core').Pipeline
49
+ */
34
50
  get subPipeline() {
51
+ // @ts-ignore
35
52
  return this.step.children[0]
36
53
  }
37
54
 
38
55
  set subPipeline(subPipeline) {
56
+ // @ts-ignore
39
57
  this.step.children[0] = subPipeline
40
58
  }
41
59
 
60
+ /**
61
+ * @param {*} chunk
62
+ * @param {string} encoding
63
+ * @param {(error?: (Error | null)) => void} callback
64
+ */
42
65
  async _write(chunk, encoding, callback) {
66
+ // @ts-ignore
43
67
  try {
44
68
  const variables = new Map(this.variables)
45
69
 
@@ -49,9 +73,11 @@ class ForEach extends Duplex {
49
73
 
50
74
  this.subPipeline = this.createPipeline(this.ptr, { variables })
51
75
 
76
+ // @ts-ignore
52
77
  this.pull = ReadableToReadable.readFrom(this.subPipeline.stream, { end: false })
53
78
 
54
79
  if (this.subPipeline.writable) {
80
+ // @ts-ignore
55
81
  this.subPipeline.stream.end(chunk)
56
82
  }
57
83
 
@@ -60,7 +86,7 @@ class ForEach extends Duplex {
60
86
  this.pull = null
61
87
 
62
88
  return callback()
63
- } catch (cause) {
89
+ } catch (/** @type {any} */ cause) {
64
90
  const err = new Error(`error in forEach sub-pipeline ${this.ptr.value}`)
65
91
 
66
92
  err.stack += `\nCaused by: ${cause.stack}`
@@ -84,11 +110,16 @@ class ForEach extends Duplex {
84
110
  }
85
111
  }
86
112
 
113
+ /**
114
+ * @this {import('barnard59-core').Context}
115
+ * @param {PipelineStream} pipeline
116
+ * @param {string} variable
117
+ * @return {ForEach}
118
+ */
87
119
  function factory(pipeline, variable) {
88
120
  return new ForEach({
89
121
  pipeline,
90
122
  createPipeline: this.createPipeline,
91
- step: this.step,
92
123
  variable,
93
124
  variables: this.variables,
94
125
  })
package/glob.d.ts ADDED
@@ -0,0 +1,10 @@
1
+ export default glob;
2
+ /**
3
+ * @this {import('barnard59-core').Context}
4
+ * @param {{ pattern: string } & import('glob').IOptions} options
5
+ * @return {Readable}
6
+ */
7
+ declare function glob(this: import("barnard59-core").Context, { pattern, ...options }: {
8
+ pattern: string;
9
+ } & import('glob').IOptions): Readable;
10
+ import { Readable } from 'readable-stream';
package/glob.js CHANGED
@@ -1,13 +1,19 @@
1
- import { promisify } from 'util'
1
+ import { promisify } from 'node:util'
2
2
  import { SpanStatusCode } from '@opentelemetry/api'
3
3
  import globFn from 'glob'
4
4
  import onetime from 'onetime'
5
5
  import { Readable } from 'readable-stream'
6
6
  import tracer from './lib/tracer.js'
7
7
 
8
+ /**
9
+ * @this {import('barnard59-core').Context}
10
+ * @param {{ pattern: string } & import('glob').IOptions} options
11
+ * @return {Readable}
12
+ */
8
13
  function glob({ pattern, ...options }) {
9
14
  const { logger } = this
10
- let filenames = null
15
+ /** @type {string[]} */
16
+ let filenames = []
11
17
 
12
18
  const span = tracer.startSpan('glob')
13
19
 
@@ -17,6 +23,7 @@ function glob({ pattern, ...options }) {
17
23
  return filenames.length === 0
18
24
  })
19
25
 
26
+ /** @type {Readable} */
20
27
  const stream = new Readable({
21
28
  objectMode: true,
22
29
  read: async () => {
@@ -34,8 +41,9 @@ function glob({ pattern, ...options }) {
34
41
  return
35
42
  }
36
43
 
44
+ // @ts-ignore
37
45
  stream._read()
38
- } catch (err) {
46
+ } catch (/** @type {any} */ err) {
39
47
  span.recordException(err)
40
48
  span.setStatus({ code: SpanStatusCode.ERROR, message: err.message })
41
49
  stream.destroy(err)
package/index.d.ts ADDED
@@ -0,0 +1,13 @@
1
+ export { default as combine } from "./combine.js";
2
+ export { default as filter } from "./filter.js";
3
+ export { default as flatten } from "./flatten.js";
4
+ export { default as forEach } from "./forEach.js";
5
+ export { default as glob } from "./glob.js";
6
+ export { default as limit } from "./limit.js";
7
+ export { default as map } from "./map.js";
8
+ export { default as nul } from "./nul.js";
9
+ export { default as offset } from "./offset.js";
10
+ export * from "./process.js";
11
+ export { default as toString } from "./toString.js";
12
+ export { default as concat, object as concatObject } from "./concat.js";
13
+ export { parse as jsonParse, stringify as jsonStringify } from "./json.js";
package/index.js CHANGED
@@ -2,6 +2,7 @@ export { default as combine } from './combine.js'
2
2
  export { default as concat, object as concatObject } from './concat.js'
3
3
  export { default as filter } from './filter.js'
4
4
  export { default as flatten } from './flatten.js'
5
+ export { default as forEach } from './forEach.js'
5
6
  export { default as glob } from './glob.js'
6
7
  export { parse as jsonParse, stringify as jsonStringify } from './json.js'
7
8
  export { default as limit } from './limit.js'
package/json.d.ts ADDED
@@ -0,0 +1,9 @@
1
+ /**
2
+ * @return {Transform}
3
+ */
4
+ export function parse(): Transform;
5
+ /**
6
+ * @return {Transform}
7
+ */
8
+ export function stringify(): Transform;
9
+ import { Transform } from 'readable-stream';
package/json.js CHANGED
@@ -8,6 +8,11 @@ class JsonParse extends Transform {
8
8
  })
9
9
  }
10
10
 
11
+ /**
12
+ * @param {*} chunk
13
+ * @param {string} encoding
14
+ * @param {(error?: Error | null, data?: any) => void} callback
15
+ */
11
16
  _transform(chunk, encoding, callback) {
12
17
  callback(null, JSON.parse(chunk.toString()))
13
18
  }
@@ -21,15 +26,26 @@ class JsonStringify extends Transform {
21
26
  })
22
27
  }
23
28
 
29
+ /**
30
+ * @param {*} chunk
31
+ * @param {string} encoding
32
+ * @param {(error?: Error | null, data?: any) => void} callback
33
+ */
24
34
  _transform(chunk, encoding, callback) {
25
35
  callback(null, JSON.stringify(chunk))
26
36
  }
27
37
  }
28
38
 
39
+ /**
40
+ * @return {Transform}
41
+ */
29
42
  function parse() {
30
43
  return new JsonParse()
31
44
  }
32
45
 
46
+ /**
47
+ * @return {Transform}
48
+ */
33
49
  function stringify() {
34
50
  return new JsonStringify()
35
51
  }
@@ -0,0 +1,2 @@
1
+ export default tracer;
2
+ declare const tracer: import("@opentelemetry/api").Tracer;
package/limit.d.ts ADDED
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Limit the amount of chunks in a pipe.
3
+ * @param {number} limit Limit the amount of chunks passed through the pipe.
4
+ */
5
+ export default function limit(limit: number): (stream: AsyncIterable<any>) => AsyncGenerator<any, void, unknown>;
package/limit.js CHANGED
@@ -1,25 +1,18 @@
1
- import { obj } from 'through2'
2
-
3
1
  /**
4
2
  * Limit the amount of chunks in a pipe.
5
3
  * @param {number} limit Limit the amount of chunks passed through the pipe.
6
- * @memberof module:barnard59
7
4
  */
8
- function limit(limit) {
9
- const t = obj(function (chunk, encoding, callback) {
10
- t.count++
11
-
12
- if (t.count <= t.limit) {
13
- this.push(chunk)
5
+ export default function limit(limit) {
6
+ /**
7
+ * @param {AsyncIterable<*>} stream
8
+ */
9
+ return async function * (stream) {
10
+ let count = 0
11
+ for await (const chunk of stream) {
12
+ count++
13
+ if (count <= limit) {
14
+ yield chunk
15
+ }
14
16
  }
15
-
16
- callback()
17
- })
18
-
19
- t.limit = limit
20
- t.count = 0
21
-
22
- return t
17
+ }
23
18
  }
24
-
25
- export default limit
package/manifest.ttl CHANGED
@@ -114,3 +114,10 @@
114
114
  code:implementedBy [ a code:EcmaScriptModule;
115
115
  code:link <node:barnard59-base/toString.js#default>
116
116
  ].
117
+
118
+ <streamValues> a p:Operation, p:ReadableObjectMode;
119
+ rdfs:label "Stream values";
120
+ rdfs:comment "Creates a readable stream from the given array.";
121
+ code:implementedBy [ a code:EcmaScriptModule;
122
+ code:link <node:barnard59-base/Readable.js#from>
123
+ ].
package/map.d.ts ADDED
@@ -0,0 +1,29 @@
1
+ /**
2
+ * @typedef {(this: import('barnard59-core').Context, chunk: From) => Promise<To> | To} MapCallback
3
+ * @template From, To
4
+ */
5
+ /**
6
+ * @typedef {{
7
+ * map: MapCallback<From, To>
8
+ * concurrency?: number
9
+ * ordered?: boolean
10
+ * objectMode?: boolean
11
+ * }|MapCallback<From, To>} MapOptions
12
+ * @template From, To
13
+ */
14
+ /**
15
+ * Processes chunks with a transform function
16
+ *
17
+ * @this {import('barnard59-core').Context}
18
+ * @param {MapOptions<From, To>} options Transform function or complex options
19
+ * @return {import('stream').Transform}
20
+ * @template From, To
21
+ */
22
+ export default function map<From, To>(this: import("barnard59-core").Context, options: MapOptions<From, To>): import('stream').Transform;
23
+ export type MapCallback<From, To> = (this: import('barnard59-core').Context, chunk: From) => Promise<To> | To;
24
+ export type MapOptions<From, To> = {
25
+ map: MapCallback<From, To>;
26
+ concurrency?: number;
27
+ ordered?: boolean;
28
+ objectMode?: boolean;
29
+ } | MapCallback<From, To>;
package/map.js CHANGED
@@ -1,16 +1,32 @@
1
1
  import transform from 'parallel-transform'
2
2
 
3
+ /**
4
+ * @typedef {(this: import('barnard59-core').Context, chunk: From) => Promise<To> | To} MapCallback
5
+ * @template From, To
6
+ */
7
+
8
+ /**
9
+ * @typedef {{
10
+ * map: MapCallback<From, To>
11
+ * concurrency?: number
12
+ * ordered?: boolean
13
+ * objectMode?: boolean
14
+ * }|MapCallback<From, To>} MapOptions
15
+ * @template From, To
16
+ */
17
+
3
18
  /**
4
19
  * Processes chunks with a transform function
5
20
  *
6
- * @param {Object|Function} options Transform function or complex options
7
- * @params {Function} options.map Transform function
8
- * @param {Number} [options.concurrency=1] The max number of concurrent chunks being transformed
9
- * @param {Boolean} [options.ordered=true] Option to keep order of asynchronously transformed chunks
10
- * @param {Boolean} [options.objectMode=true] Option to transform chunks in object mode
11
- * @return {ReadableStream}
21
+ * @this {import('barnard59-core').Context}
22
+ * @param {MapOptions<From, To>} options Transform function or complex options
23
+ * @return {import('stream').Transform}
24
+ * @template From, To
12
25
  */
13
26
  export default function map(options) {
27
+ /**
28
+ * @type {MapCallback<*, *>}
29
+ */
14
30
  let func
15
31
  let concurrency = 1
16
32
  let ordered = true
package/nul.d.ts ADDED
@@ -0,0 +1,6 @@
1
+ export default factory;
2
+ /**
3
+ * @return {Writable}
4
+ */
5
+ declare function factory(): Writable;
6
+ import { Writable } from 'readable-stream';
package/nul.js CHANGED
@@ -5,11 +5,19 @@ class Nul extends Writable {
5
5
  super({ objectMode: true })
6
6
  }
7
7
 
8
+ /**
9
+ * @param {any} chunk
10
+ * @param {string} encoding
11
+ * @param {() => void} callback
12
+ */
8
13
  _write(chunk, encoding, callback) {
9
14
  callback()
10
15
  }
11
16
  }
12
17
 
18
+ /**
19
+ * @return {Writable}
20
+ */
13
21
  function factory() {
14
22
  return new Nul()
15
23
  }
package/offset.d.ts ADDED
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Limit the amount of chunks in a pipe.
3
+ * @param {number} offset
4
+ */
5
+ export default function _default(offset: number): (stream: AsyncIterable<any>) => AsyncGenerator<any, void, unknown>;
package/offset.js CHANGED
@@ -1,20 +1,19 @@
1
- import { obj } from 'through2'
1
+ /**
2
+ * Limit the amount of chunks in a pipe.
3
+ * @param {number} offset
4
+ */
5
+ export default function (offset) {
6
+ /**
7
+ * @param {AsyncIterable<*>} stream
8
+ */
9
+ return async function * (stream) {
10
+ let count = 0
2
11
 
3
- function offset(offset) {
4
- const t = obj(function (chunk, encoding, callback) {
5
- t.count++
6
-
7
- if (t.count > t.offset) {
8
- this.push(chunk)
12
+ for await (const chunk of stream) {
13
+ count++
14
+ if (count > offset) {
15
+ yield chunk
16
+ }
9
17
  }
10
-
11
- callback()
12
- })
13
-
14
- t.offset = offset
15
- t.count = 0
16
-
17
- return t
18
+ }
18
19
  }
19
-
20
- export default offset
package/package.json CHANGED
@@ -1,11 +1,14 @@
1
1
  {
2
2
  "name": "barnard59-base",
3
- "version": "2.2.0",
3
+ "version": "2.3.0",
4
4
  "description": "Linked Data pipelines",
5
5
  "main": "index.js",
6
6
  "type": "module",
7
7
  "scripts": {
8
- "test": "mocha"
8
+ "test": "mocha",
9
+ "prebuild": "rimraf *.d.ts",
10
+ "build": "tsc",
11
+ "prepack": "npm run build"
9
12
  },
10
13
  "repository": {
11
14
  "type": "git",
@@ -13,7 +16,7 @@
13
16
  "directory": "packages/base"
14
17
  },
15
18
  "keywords": [],
16
- "author": "Thomas Bergwinkl <bergi@axolotlfarm.org> (https://www.bergnet.org/people/bergi/card#me)",
19
+ "author": "Zazuko GmbH",
17
20
  "license": "MIT",
18
21
  "bugs": {
19
22
  "url": "https://github.com/zazuko/barnard59/issues"
@@ -30,18 +33,25 @@
30
33
  "through2": "^4.0.2"
31
34
  },
32
35
  "devDependencies": {
33
- "barnard59-core": "^5.0.0",
36
+ "@types/duplexify": "^3.6.4",
37
+ "@types/glob": "^7.2.0",
38
+ "@types/parallel-transform": "^1.1.4",
39
+ "@types/readable-stream": "^4.0.10",
40
+ "@types/readable-to-readable": "^0.1.0",
41
+ "@types/through2": "^2.0.41",
42
+ "barnard59-core": "^5.3.0",
34
43
  "chai": "^4.3.10",
35
44
  "get-stream": "^6.0.1",
36
45
  "into-stream": "^7.0.0",
37
- "isstream": "^0.1.2",
38
- "mocha": "^9.0.1",
46
+ "is-stream": "^3.0.0",
47
+ "rimraf": "^3.0.2",
39
48
  "sinon": "^17.0.0"
40
49
  },
41
50
  "engines": {
42
51
  "node": ">= 14.0.0"
43
52
  },
44
53
  "mocha": {
45
- "require": "../../test/mocha-setup.cjs"
54
+ "require": "../../test/mocha-setup.cjs",
55
+ "loader": "tsm"
46
56
  }
47
57
  }
package/process.d.ts ADDED
@@ -0,0 +1,9 @@
1
+ /**
2
+ * @return {Transform}
3
+ */
4
+ export function stdout(): Transform;
5
+ /**
6
+ * @return {import('stream').Readable}
7
+ */
8
+ export function stdin(): import('stream').Readable;
9
+ import { Transform } from 'readable-stream';
package/process.js CHANGED
@@ -1,6 +1,11 @@
1
1
  import { Transform } from 'readable-stream'
2
2
 
3
3
  class StdOut extends Transform {
4
+ /**
5
+ * @param {Uint8Array | string} chunk
6
+ * @param {BufferEncoding} encoding
7
+ * @param {(error?: Error | null, data?: any) => void} callback
8
+ */
4
9
  _transform(chunk, encoding, callback) {
5
10
  process.stdout.write(chunk, encoding)
6
11
 
@@ -8,10 +13,16 @@ class StdOut extends Transform {
8
13
  }
9
14
  }
10
15
 
16
+ /**
17
+ * @return {Transform}
18
+ */
11
19
  export function stdout() {
12
20
  return new StdOut()
13
21
  }
14
22
 
23
+ /**
24
+ * @return {import('stream').Readable}
25
+ */
15
26
  export function stdin() {
16
27
  return process.stdin
17
28
  }
@@ -0,0 +1,17 @@
1
+ import assert from 'node:assert'
2
+ import { isReadableStream } from 'is-stream'
3
+ import getStream from 'get-stream'
4
+ import { expect } from 'chai'
5
+ import * as Readable from '../Readable.js'
6
+
7
+ describe('Readable', () => {
8
+ it('should return a readable stream', async () => {
9
+ // given
10
+ const stream = Readable.from(0, 1, 2, 3, 4)
11
+
12
+ // then
13
+ assert(isReadableStream(stream))
14
+ const array = await getStream.array(stream)
15
+ expect(array).to.deep.equal([0, 1, 2, 3, 4])
16
+ })
17
+ })
@@ -1,6 +1,6 @@
1
1
  import { deepStrictEqual, strictEqual, rejects } from 'assert'
2
2
  import getStream, { array } from 'get-stream'
3
- import { isReadable, isWritable } from 'isstream'
3
+ import { isReadableStream as isReadable, isWritableStream as isWritable } from 'is-stream'
4
4
  import { Readable } from 'readable-stream'
5
5
  import concat, { object } from '../concat.js'
6
6
 
@@ -1,6 +1,6 @@
1
1
  import { deepStrictEqual, strictEqual } from 'assert'
2
2
  import { array } from 'get-stream'
3
- import { isReadable, isWritable } from 'isstream'
3
+ import { isReadableStream as isReadable, isWritableStream as isWritable } from 'is-stream'
4
4
  import { Readable } from 'readable-stream'
5
5
  import flatten from '../flatten.js'
6
6
 
package/test/glob.test.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { deepStrictEqual, strictEqual } from 'assert'
2
2
  import { array } from 'get-stream'
3
- import { isReadable, isWritable } from 'isstream'
3
+ import { isReadableStream as isReadable, isWritableStream as isWritable } from 'is-stream'
4
4
  import sinon from 'sinon'
5
5
  import { expect } from 'chai'
6
6
  import glob from '../glob.js'
@@ -0,0 +1,23 @@
1
+ import { Readable, Duplex } from 'node:stream'
2
+ import { expect } from 'chai'
3
+ import getStream from 'get-stream'
4
+ import limit from '../limit.js'
5
+
6
+ describe('limit', () => {
7
+ it('should return generator', () => {
8
+ const step = limit(1)
9
+
10
+ expect(step).to.be.instanceOf(Function)
11
+ })
12
+
13
+ it('stop processing chunks when limit is reached', async () => {
14
+ // given
15
+ const step = limit(1)
16
+
17
+ // when
18
+ const stream = Readable.from([0, 1, 2, 3, 4]).pipe(Duplex.from(step))
19
+ const limited = await getStream.array(stream)
20
+
21
+ expect(limited).to.deep.equal([0])
22
+ })
23
+ })
@@ -0,0 +1,23 @@
1
+ import { Readable, Duplex } from 'node:stream'
2
+ import { expect } from 'chai'
3
+ import getStream from 'get-stream'
4
+ import offset from '../offset.js'
5
+
6
+ describe('offset', () => {
7
+ it('should return generator', () => {
8
+ const step = offset(1)
9
+
10
+ expect(step).to.be.instanceOf(Function)
11
+ })
12
+
13
+ it('stop processing chunks when limit is reached', async () => {
14
+ // given
15
+ const step = offset(2)
16
+
17
+ // when
18
+ const stream = Readable.from([0, 1, 2, 3, 4]).pipe(Duplex.from(step))
19
+ const skipped = await getStream.array(stream)
20
+
21
+ expect(skipped).to.deep.equal([2, 3, 4])
22
+ })
23
+ })
package/toReadable.js CHANGED
@@ -12,10 +12,16 @@ class ToReadable extends Readable {
12
12
  }
13
13
  }
14
14
 
15
+ /**
16
+ * @deprecated Use `base:streamValues` instead.
17
+ */
15
18
  function stringToReadable(str) {
16
19
  return new ToReadable(str)
17
20
  }
18
21
 
22
+ /**
23
+ * @deprecated Use `base:streamValues` instead.
24
+ */
19
25
  function objectToReadable(obj) {
20
26
  return new ToReadable(obj, { objectMode: true })
21
27
  }
package/toString.d.ts ADDED
@@ -0,0 +1,6 @@
1
+ export default factory;
2
+ /**
3
+ * @return {Transform}
4
+ */
5
+ declare function factory(): Transform;
6
+ import { Transform } from 'readable-stream';
package/toString.js CHANGED
@@ -8,11 +8,19 @@ class ToString extends Transform {
8
8
  })
9
9
  }
10
10
 
11
+ /**
12
+ * @param {Uint8Array | string} chunk
13
+ * @param {BufferEncoding} encoding
14
+ * @param {import('stream').TransformCallback} callback
15
+ */
11
16
  _transform(chunk, encoding, callback) {
12
17
  callback(null, chunk.toString())
13
18
  }
14
19
  }
15
20
 
21
+ /**
22
+ * @return {Transform}
23
+ */
16
24
  function factory() {
17
25
  return new ToString()
18
26
  }
package/tsconfig.json ADDED
@@ -0,0 +1,11 @@
1
+ {
2
+ "extends": "../../tsconfig.json",
3
+ "compilerOptions": {
4
+ "allowJs": true,
5
+ "checkJs": true,
6
+ "emitDeclarationOnly": true
7
+ },
8
+ "files": [
9
+ "./index.js"
10
+ ]
11
+ }