barnard59-base 3.0.1 → 3.1.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 +12 -0
- package/json.js +12 -2
- package/manifest.ttl +7 -0
- package/package.json +2 -2
- package/skills/barnard59-base/SKILL.md +90 -0
- package/test/json.test.js +25 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,17 @@
|
|
|
1
1
|
# barnard59-base
|
|
2
2
|
|
|
3
|
+
## 3.1.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- [#416](https://github.com/zazuko/barnard59/pull/416) [`e12fa2b`](https://github.com/zazuko/barnard59/commit/e12fa2bba0d1c7385473b2a33a54ff1590bdf85a) - Add the `json/parse/document` operation and the `parseDocument` JavaScript export for parsing a complete JSON document from a stream, including documents split across chunks.
|
|
8
|
+
|
|
9
|
+
### Patch Changes
|
|
10
|
+
|
|
11
|
+
- [#415](https://github.com/zazuko/barnard59/pull/415) [`2228ea1`](https://github.com/zazuko/barnard59/commit/2228ea1790ac6b988c6dbcfba4fd4f38e1c6e486) - build(deps): bump glob from 13.0.6 to 14.0.2
|
|
12
|
+
|
|
13
|
+
- [#416](https://github.com/zazuko/barnard59/pull/416) [`f9b9f9e`](https://github.com/zazuko/barnard59/commit/f9b9f9e23b70aa4af2254f2f308246cee0639904) - Add the `barnard59-base` skill for composing and transforming pipeline streams with mapping, filtering, batching, and other base operations.
|
|
14
|
+
|
|
3
15
|
## 3.0.1
|
|
4
16
|
|
|
5
17
|
### Patch Changes
|
package/json.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { Transform } from 'node:stream'
|
|
1
|
+
import { Duplex, Transform } from 'node:stream'
|
|
2
|
+
import { json } from 'node:stream/consumers'
|
|
2
3
|
|
|
3
4
|
class JsonParse extends Transform {
|
|
4
5
|
constructor() {
|
|
@@ -43,6 +44,15 @@ function parse() {
|
|
|
43
44
|
return new JsonParse()
|
|
44
45
|
}
|
|
45
46
|
|
|
47
|
+
function parseDocument() {
|
|
48
|
+
return Duplex.from(async function* (chunks) {
|
|
49
|
+
const value = await json(chunks)
|
|
50
|
+
if (value !== null) {
|
|
51
|
+
yield value
|
|
52
|
+
}
|
|
53
|
+
})
|
|
54
|
+
}
|
|
55
|
+
|
|
46
56
|
/**
|
|
47
57
|
* @return {Transform}
|
|
48
58
|
*/
|
|
@@ -50,4 +60,4 @@ function stringify() {
|
|
|
50
60
|
return new JsonStringify()
|
|
51
61
|
}
|
|
52
62
|
|
|
53
|
-
export { parse, stringify }
|
|
63
|
+
export { parse, parseDocument, stringify }
|
package/manifest.ttl
CHANGED
|
@@ -73,6 +73,13 @@
|
|
|
73
73
|
code:link <node:barnard59-base/json.js#stringify>
|
|
74
74
|
].
|
|
75
75
|
|
|
76
|
+
<json/parse/document> a p:Operation, p:Writable, p:ReadableObjectMode;
|
|
77
|
+
rdfs:label "Parse JSON document";
|
|
78
|
+
rdfs:comment "Reads the input stream as one JSON document and emits the parsed value.";
|
|
79
|
+
code:implementedBy [ a code:EcmaScriptModule;
|
|
80
|
+
code:link <node:barnard59-base/json.js#parseDocument>
|
|
81
|
+
].
|
|
82
|
+
|
|
76
83
|
<limit> a p:Operation, p:WritableObjectMode, p:ReadableObjectMode;
|
|
77
84
|
rdfs:label "Limit";
|
|
78
85
|
rdfs:comment "Limits the amount of forwarded chunks and discards any chunks after reaching the limit.";
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "barnard59-base",
|
|
3
|
-
"version": "3.0
|
|
3
|
+
"version": "3.1.0",
|
|
4
4
|
"description": "Linked Data pipelines",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"type": "module",
|
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
"dependencies": {
|
|
26
26
|
"@opentelemetry/api": "^1.9.1",
|
|
27
27
|
"duplexify": "^4.1.1",
|
|
28
|
-
"glob": "^
|
|
28
|
+
"glob": "^14.0.2",
|
|
29
29
|
"onetime": "^6.0.0",
|
|
30
30
|
"parallel-transform": "^1.2.0",
|
|
31
31
|
"readable-to-readable": "^0.1.3",
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: barnard59-base
|
|
3
|
+
description: Use when composing or transforming Barnard59 streams with mapping, filtering, batching, flattening, JSON chunks, standard I/O, globbing, or nested pipelines.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Base stream steps
|
|
7
|
+
|
|
8
|
+
Declare `@prefix op: <https://barnard59.zazuko.com/operations/> .` at the top of your Turtle file. In `p:stepList`, write `[ op:base\/batch (10) ]` to group incoming chunks into batches of ten.
|
|
9
|
+
|
|
10
|
+
For an explicit `p:Step`, link to the JavaScript export with `code:link`. For example, the link for `batch` is `<node:barnard59-base/batch.js#default>`. The other module paths below follow the same pattern.
|
|
11
|
+
|
|
12
|
+
## `streamValues`
|
|
13
|
+
|
|
14
|
+
`Readable.js#from` takes each positional argument as a separate value and emits it as one object-mode chunk. Use `[ op:base\/streamValues ("first" "second") ]` for a fixed list of work items or a small fixture. Passing a list as a single argument emits that list as one chunk.
|
|
15
|
+
|
|
16
|
+
## `glob`
|
|
17
|
+
|
|
18
|
+
`glob.js#default` takes named `pattern` and optional glob settings such as `cwd` and `nodir`. It emits one matched path string per object-mode chunk. Relative patterns start at the working directory unless you set `cwd`. Use `nodir true` for file readers and pass the results to `barnard59-rdf`'s `fs.js#parse` or a `forEach` subpipeline to open the matches.
|
|
19
|
+
|
|
20
|
+
## `stdin`
|
|
21
|
+
|
|
22
|
+
`process.js#stdin` takes no arguments and returns the byte stream from standard input. Put it first when the CLI should accept redirected files or input from a shell pipe. Add a parser when downstream steps expect objects or quads.
|
|
23
|
+
|
|
24
|
+
## `stdout`
|
|
25
|
+
|
|
26
|
+
`process.js#stdout` takes no arguments. It writes incoming bytes to stdout and forwards those bytes downstream. Use it when a pipeline explicitly needs that side effect. A readable CLI pipeline already prints its output by default, so including this step in that path can print the same bytes twice.
|
|
27
|
+
|
|
28
|
+
## `map`
|
|
29
|
+
|
|
30
|
+
`map.js#default` takes a function as the first positional argument or named options with a `map` function. The callback receives `(chunk, ...args)` and may return a value or a promise. Use a regular function to access the pipeline context through `this`.
|
|
31
|
+
|
|
32
|
+
The default is object mode, concurrency `1`, and ordered output. Raise `concurrency` for independent asynchronous work. Set `ordered false` only when downstream steps can accept completion order. In programmatic calls, additional arguments follow the callback or options object.
|
|
33
|
+
|
|
34
|
+
## `filter`
|
|
35
|
+
|
|
36
|
+
`filter.js#default` takes a predicate followed by any additional positional arguments. It calls `predicate(chunk, encoding, ...args)` in the pipeline context and awaits the result. A truthy result forwards the original object-mode chunk. Use `map` when the operation needs to change the chunk as well.
|
|
37
|
+
|
|
38
|
+
## `limit`
|
|
39
|
+
|
|
40
|
+
`limit.js#default` takes a positional chunk count and emits up to the first N chunks. Place it before expensive work to sample a stream or after `filter` to keep the first N matches. It counts the chunks at that point in the pipeline, so a batch counts as one chunk.
|
|
41
|
+
|
|
42
|
+
## `offset`
|
|
43
|
+
|
|
44
|
+
`offset.js#default` takes a positional chunk count and discards the first N chunks before forwarding the remainder. Pair it with `limit` after a source with a stable order for a repeatable slice.
|
|
45
|
+
|
|
46
|
+
## `batch`
|
|
47
|
+
|
|
48
|
+
`batch.js#default` takes a positional size and groups object-mode input into arrays of that size. It emits the final partial array at end of input. A size of `0`, the default, collects all input into one array, so use it for bounded input. Choose boundaries that keep related quads together when batch contents affect query or validation results.
|
|
49
|
+
|
|
50
|
+
## `flatten`
|
|
51
|
+
|
|
52
|
+
`flatten.js#default` takes no arguments. Each chunk must be iterable or implement `forEach`. The step emits its members individually in object mode and expands one level. Place it between dataset or array output and a step that expects one RDF/JS quad per chunk.
|
|
53
|
+
|
|
54
|
+
## `concat`
|
|
55
|
+
|
|
56
|
+
`concat.js#default` takes readable streams as separate positional arguments. It drains them in argument order and exposes one byte stream without inserting separators. For example, `[ op:base\/concat (ex:header ex:body) ]` can join two byte-producing subpipelines.
|
|
57
|
+
|
|
58
|
+
## `concat/object`
|
|
59
|
+
|
|
60
|
+
`concat.js#object` takes the same positional stream arguments and emits their chunks sequentially in object mode. Use it to join quad streams, records, or dataset chunks. Each input chunk is forwarded unchanged, including a grouped dataset.
|
|
61
|
+
|
|
62
|
+
## `combine`
|
|
63
|
+
|
|
64
|
+
`combine.js#default` takes a nonempty array of streams and an optional duplex-options object. It pipes each stream into the next and exposes the first stream's writable side and the last stream's readable side as one duplex. Supply `objectMode` or side-specific stream options when needed. Use `concat` to drain independent sources in sequence.
|
|
65
|
+
|
|
66
|
+
## `forEach`
|
|
67
|
+
|
|
68
|
+
`forEach.js#default` takes a nested pipeline and an optional variable name as positional arguments. It creates a new pipeline for each object-mode input chunk. The nested pipeline inherits the caller's variables, and the named variable receives the current chunk. If the nested pipeline is writable, the chunk is also sent to its input. Its readable output is forwarded before work starts on the next chunk.
|
|
69
|
+
|
|
70
|
+
Define `ex:readOne` as a `p:Pipeline`, then use `[ op:base\/forEach (ex:readOne "filename") ]` after `glob` to open each path through a fresh nested pipeline.
|
|
71
|
+
|
|
72
|
+
## `json/parse`
|
|
73
|
+
|
|
74
|
+
`json.js#parse` takes no arguments. It parses each byte chunk as one complete JSON value and emits the result in object mode.
|
|
75
|
+
|
|
76
|
+
## `json/parse/document`
|
|
77
|
+
|
|
78
|
+
`json.js#parseDocument` takes no arguments. Use `[ op:base\/json\/parse\/document () ]` after a file or HTTP source that yields one bounded JSON document across arbitrary byte chunks. It buffers the stream, then parses the document and emits its value in object mode. A top-level JSON null emits no chunk.
|
|
79
|
+
|
|
80
|
+
## `json/stringify`
|
|
81
|
+
|
|
82
|
+
`json.js#stringify` takes no arguments and emits the JSON serialization of each object-mode chunk as bytes. It adds neither newlines nor array framing. Supply framing separately for JSON Lines or a single JSON document with several values.
|
|
83
|
+
|
|
84
|
+
## `toString`
|
|
85
|
+
|
|
86
|
+
`toString.js#default` takes no arguments. It calls `toString()` on each object-mode input chunk and exposes a byte-mode readable side. Use this for text representations the object already defines. Use `json/stringify` or an RDF serializer when the downstream format requires structured encoding.
|
|
87
|
+
|
|
88
|
+
## `nul`
|
|
89
|
+
|
|
90
|
+
`dev-nul.js#default` takes no arguments and consumes object-mode chunks without producing output. Use it as a terminal sink when upstream work should run to completion and its output can be discarded.
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { Readable } from 'node:stream'
|
|
2
|
+
import { pipeline } from 'node:stream/promises'
|
|
3
|
+
import { deepStrictEqual, rejects } from 'node:assert/strict'
|
|
4
|
+
import getStream from 'get-stream'
|
|
5
|
+
import { parseDocument } from '../json.js'
|
|
6
|
+
|
|
7
|
+
describe('json/parse/document', function () {
|
|
8
|
+
it('parses one document across chunk and UTF-8 character boundaries', async function () {
|
|
9
|
+
const input = Buffer.from('{"city":"Łódź"}')
|
|
10
|
+
const split = input.indexOf(Buffer.from('Ł')) + 1
|
|
11
|
+
const values = await pipeline(Readable.from([input.subarray(0, split), input.subarray(split)]), parseDocument(), stream => getStream.array(stream))
|
|
12
|
+
|
|
13
|
+
deepStrictEqual(values, [{ city: 'Łódź' }])
|
|
14
|
+
})
|
|
15
|
+
|
|
16
|
+
it('rejects a truncated document', async function () {
|
|
17
|
+
await rejects(pipeline(Readable.from(['{"city":']), parseDocument(), stream => getStream.array(stream)), SyntaxError)
|
|
18
|
+
})
|
|
19
|
+
|
|
20
|
+
it('treats a top-level null as no output, like the chunk parser', async function () {
|
|
21
|
+
const values = await pipeline(Readable.from(['null']), parseDocument(), stream => getStream.array(stream))
|
|
22
|
+
|
|
23
|
+
deepStrictEqual(values, [])
|
|
24
|
+
})
|
|
25
|
+
})
|