@grafctory/mime-json 0.0.0-stage → 0.6.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/LICENSE +201 -0
- package/README.md +53 -2
- package/dist/index.d.ts +79 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +113 -0
- package/dist/index.js.map +1 -0
- package/dist/jpath.core.d.ts +8 -0
- package/dist/jpath.core.d.ts.map +1 -0
- package/dist/jpath.core.js +60 -0
- package/dist/jpath.core.js.map +1 -0
- package/dist/jpath.d.ts +120 -0
- package/dist/jpath.d.ts.map +1 -0
- package/dist/jpath.js +33 -0
- package/dist/jpath.js.map +1 -0
- package/dist/json.core.d.ts +8 -0
- package/dist/json.core.d.ts.map +1 -0
- package/dist/json.core.js +67 -0
- package/dist/json.core.js.map +1 -0
- package/dist/json.d.ts +45 -0
- package/dist/json.d.ts.map +1 -0
- package/dist/json.js +70 -0
- package/dist/json.js.map +1 -0
- package/dist/validate.d.ts +35 -0
- package/dist/validate.d.ts.map +1 -0
- package/dist/validate.js +63 -0
- package/dist/validate.js.map +1 -0
- package/package.json +58 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
CHANGED
|
@@ -1,3 +1,54 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @grafctory/mime-json
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[![npm][npm-badge]][npm] [![Grafctory][grafctory-badge]][grafctory]
|
|
4
|
+
|
|
5
|
+
[npm-badge]: https://img.shields.io/npm/v/@grafctory/mime-json
|
|
6
|
+
[npm]: https://www.npmjs.com/package/@grafctory/mime-json
|
|
7
|
+
[grafctory-badge]: https://img.shields.io/badge/part%20of-Grafctory-blue
|
|
8
|
+
[grafctory]: https://github.com/grafctory
|
|
9
|
+
|
|
10
|
+
JSON processing tasks for [@grafctory/mime].
|
|
11
|
+
|
|
12
|
+
[@grafctory/mime]: https://github.com/grafctory/mime
|
|
13
|
+
|
|
14
|
+
# Installation
|
|
15
|
+
|
|
16
|
+
```shell
|
|
17
|
+
npm install @grafctory/gear # the job executor
|
|
18
|
+
npm install @grafctory/mime-json # this package
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
> [!IMPORTANT]
|
|
22
|
+
>
|
|
23
|
+
> Node.js 22 or later is required.
|
|
24
|
+
|
|
25
|
+
> [!WARNING]
|
|
26
|
+
>
|
|
27
|
+
> TypeScript consumers must use `"moduleResolution": "nodenext"/"node16"/"bundler"` in `tsconfig.json`.
|
|
28
|
+
> The legacy `"node"` resolver is not supported.
|
|
29
|
+
|
|
30
|
+
# Usage
|
|
31
|
+
|
|
32
|
+
| Task | Description |
|
|
33
|
+
|--------------------------|----------------------|
|
|
34
|
+
| [`json()`][json] | JSON parser |
|
|
35
|
+
| [`jpath()`][jpath] | JSON path selector |
|
|
36
|
+
| [`validate()`][validate] | JSON value validator |
|
|
37
|
+
|
|
38
|
+
[json]: https://grafctory.github.io/mime/functions/_grafctory_mime-json.json.html
|
|
39
|
+
|
|
40
|
+
[jpath]: https://grafctory.github.io/mime/functions/_grafctory_mime-json.jpath.html
|
|
41
|
+
|
|
42
|
+
[validate]: https://grafctory.github.io/mime/functions/_grafctory_mime-json.validate.html
|
|
43
|
+
|
|
44
|
+
# Support
|
|
45
|
+
|
|
46
|
+
- open an [issue](https://github.com/grafctory/mime/issues) to report a problem or to suggest a new feature
|
|
47
|
+
- start a [discussion](https://github.com/grafctory/mime/discussions) to ask a how-to question or to share an idea
|
|
48
|
+
|
|
49
|
+
# License
|
|
50
|
+
|
|
51
|
+
This project is licensed under the Apache 2.0 License –
|
|
52
|
+
see [LICENSE](https://github.com/grafctory/mime?tab=Apache-2.0-1-ov-file) file for details.
|
|
53
|
+
|
|
54
|
+
<sub>Grafctory™ is a trademark of Metreeca srl.</sub>
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* JSON processing tasks.
|
|
3
|
+
*
|
|
4
|
+
* Provides ready-made {@link https://github.com/grafctory/flow @grafctory/flow} tasks for processing JSON content in
|
|
5
|
+
* pipelines run by the {@link https://github.com/grafctory/gear @grafctory/gear} executor.
|
|
6
|
+
*
|
|
7
|
+
* Helpers alongside the tasks narrow extracted values to the types a pipeline expects and fail fast on unexpected
|
|
8
|
+
* content. Content digests let a pipeline skip duplicate values and detect changes across successive runs.
|
|
9
|
+
*
|
|
10
|
+
* @module index
|
|
11
|
+
*
|
|
12
|
+
* @see {@link https://www.rfc-editor.org/rfc/rfc8259 RFC 8259 JSON Data Interchange Format}
|
|
13
|
+
* @see {@link https://www.rfc-editor.org/rfc/rfc9535 RFC 9535 JSONPath Query Expressions for JSON}
|
|
14
|
+
*/
|
|
15
|
+
import { type Value } from "@grafctory/core";
|
|
16
|
+
import { type IRI } from "@grafctory/core/resource";
|
|
17
|
+
export * from "./json.js";
|
|
18
|
+
export * from "./jpath.js";
|
|
19
|
+
export * from "./validate.js";
|
|
20
|
+
/**
|
|
21
|
+
* Narrows a JSON value to a boolean.
|
|
22
|
+
*
|
|
23
|
+
* @param value The value to narrow
|
|
24
|
+
*
|
|
25
|
+
* @returns `value`, unchanged, typed as a boolean
|
|
26
|
+
*
|
|
27
|
+
* @throws {@link !TypeError TypeError} If `value` is not a boolean
|
|
28
|
+
*/
|
|
29
|
+
export declare function boolean(value: Value): boolean;
|
|
30
|
+
/**
|
|
31
|
+
* Narrows a JSON value to a number.
|
|
32
|
+
*
|
|
33
|
+
* @param value The value to narrow
|
|
34
|
+
*
|
|
35
|
+
* @returns `value`, unchanged, typed as a number
|
|
36
|
+
*
|
|
37
|
+
* @throws {@link !TypeError TypeError} If `value` is not a number
|
|
38
|
+
*/
|
|
39
|
+
export declare function number(value: Value): number;
|
|
40
|
+
/**
|
|
41
|
+
* Narrows a JSON value to a string.
|
|
42
|
+
*
|
|
43
|
+
* @param value The value to narrow
|
|
44
|
+
*
|
|
45
|
+
* @returns `value`, unchanged, typed as a string
|
|
46
|
+
*
|
|
47
|
+
* @throws {@link !TypeError TypeError} If `value` is not a string
|
|
48
|
+
*/
|
|
49
|
+
export declare function string(value: Value): string;
|
|
50
|
+
/**
|
|
51
|
+
* Narrows a JSON value to a link.
|
|
52
|
+
*
|
|
53
|
+
* Accepts a relative reference as well as an absolute IRI, leaving resolution against a base to the consumer.
|
|
54
|
+
*
|
|
55
|
+
* @param value The value to narrow
|
|
56
|
+
*
|
|
57
|
+
* @returns `value`, unchanged, typed as an IRI
|
|
58
|
+
*
|
|
59
|
+
* @throws {@link !TypeError TypeError} If `value` is not a string holding an IRI or a relative reference
|
|
60
|
+
*
|
|
61
|
+
* @see {@link https://www.rfc-editor.org/rfc/rfc3987 RFC 3987 Internationalized Resource Identifiers}
|
|
62
|
+
*/
|
|
63
|
+
export declare function link(value: Value): IRI;
|
|
64
|
+
/**
|
|
65
|
+
* Computes a content digest of a JSON value.
|
|
66
|
+
*
|
|
67
|
+
* Values with equal content get the same digest, whatever the order of their object entries. A pipeline can
|
|
68
|
+
* therefore skip values it has already seen, and detect when a value it already processed changes between runs.
|
|
69
|
+
* Arrays are ordered, though: the same items in a different order produce a different digest.
|
|
70
|
+
*
|
|
71
|
+
* Only content that survives JSON serialisation is digested, so values differing only in what serialisation drops
|
|
72
|
+
* share a digest. Non-finite numbers count as `null` and negative zero as `0`, just as they would once stored.
|
|
73
|
+
*
|
|
74
|
+
* @param value The JSON value to be digested
|
|
75
|
+
*
|
|
76
|
+
* @returns The lowercase hex-encoded SHA-256 digest of the content of `value`
|
|
77
|
+
*/
|
|
78
|
+
export declare function digest(value: Value): string;
|
|
79
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAgBA;;;;;;;;;;;;;GAaG;AAEH,OAAO,EAAmD,KAAK,KAAK,EAAE,MAAM,iBAAiB,CAAC;AAE9F,OAAO,EAAE,KAAK,GAAG,EAAS,MAAM,0BAA0B,CAAC;AAG3D,cAAc,WAAW,CAAC;AAC1B,cAAc,YAAY,CAAC;AAC3B,cAAc,eAAe,CAAC;AAG9B;;;;;;;;GAQG;AACH,wBAAgB,OAAO,CAAC,KAAK,EAAE,KAAK,GAAG,OAAO,CAE7C;AAED;;;;;;;;GAQG;AACH,wBAAgB,MAAM,CAAC,KAAK,EAAE,KAAK,GAAG,MAAM,CAE3C;AAED;;;;;;;;GAQG;AACH,wBAAgB,MAAM,CAAC,KAAK,EAAE,KAAK,GAAG,MAAM,CAE3C;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,IAAI,CAAC,KAAK,EAAE,KAAK,GAAG,GAAG,CAEtC;AAGD;;;;;;;;;;;;;GAaG;AACH,wBAAgB,MAAM,CAAC,KAAK,EAAE,KAAK,GAAG,MAAM,CAa3C"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright © 2026 Metreeca srl
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
/**
|
|
17
|
+
* JSON processing tasks.
|
|
18
|
+
*
|
|
19
|
+
* Provides ready-made {@link https://github.com/grafctory/flow @grafctory/flow} tasks for processing JSON content in
|
|
20
|
+
* pipelines run by the {@link https://github.com/grafctory/gear @grafctory/gear} executor.
|
|
21
|
+
*
|
|
22
|
+
* Helpers alongside the tasks narrow extracted values to the types a pipeline expects and fail fast on unexpected
|
|
23
|
+
* content. Content digests let a pipeline skip duplicate values and detect changes across successive runs.
|
|
24
|
+
*
|
|
25
|
+
* @module index
|
|
26
|
+
*
|
|
27
|
+
* @see {@link https://www.rfc-editor.org/rfc/rfc8259 RFC 8259 JSON Data Interchange Format}
|
|
28
|
+
* @see {@link https://www.rfc-editor.org/rfc/rfc9535 RFC 9535 JSONPath Query Expressions for JSON}
|
|
29
|
+
*/
|
|
30
|
+
import { assert, isBoolean, isNumber, isObject, isString } from "@grafctory/core";
|
|
31
|
+
import { by } from "@grafctory/core/order";
|
|
32
|
+
import { isIRI } from "@grafctory/core/resource";
|
|
33
|
+
import { createHash } from "node:crypto";
|
|
34
|
+
export * from "./json.js";
|
|
35
|
+
export * from "./jpath.js";
|
|
36
|
+
export * from "./validate.js";
|
|
37
|
+
/**
|
|
38
|
+
* Narrows a JSON value to a boolean.
|
|
39
|
+
*
|
|
40
|
+
* @param value The value to narrow
|
|
41
|
+
*
|
|
42
|
+
* @returns `value`, unchanged, typed as a boolean
|
|
43
|
+
*
|
|
44
|
+
* @throws {@link !TypeError TypeError} If `value` is not a boolean
|
|
45
|
+
*/
|
|
46
|
+
export function boolean(value) {
|
|
47
|
+
return assert(value, isBoolean);
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Narrows a JSON value to a number.
|
|
51
|
+
*
|
|
52
|
+
* @param value The value to narrow
|
|
53
|
+
*
|
|
54
|
+
* @returns `value`, unchanged, typed as a number
|
|
55
|
+
*
|
|
56
|
+
* @throws {@link !TypeError TypeError} If `value` is not a number
|
|
57
|
+
*/
|
|
58
|
+
export function number(value) {
|
|
59
|
+
return assert(value, isNumber);
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Narrows a JSON value to a string.
|
|
63
|
+
*
|
|
64
|
+
* @param value The value to narrow
|
|
65
|
+
*
|
|
66
|
+
* @returns `value`, unchanged, typed as a string
|
|
67
|
+
*
|
|
68
|
+
* @throws {@link !TypeError TypeError} If `value` is not a string
|
|
69
|
+
*/
|
|
70
|
+
export function string(value) {
|
|
71
|
+
return assert(value, isString);
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Narrows a JSON value to a link.
|
|
75
|
+
*
|
|
76
|
+
* Accepts a relative reference as well as an absolute IRI, leaving resolution against a base to the consumer.
|
|
77
|
+
*
|
|
78
|
+
* @param value The value to narrow
|
|
79
|
+
*
|
|
80
|
+
* @returns `value`, unchanged, typed as an IRI
|
|
81
|
+
*
|
|
82
|
+
* @throws {@link !TypeError TypeError} If `value` is not a string holding an IRI or a relative reference
|
|
83
|
+
*
|
|
84
|
+
* @see {@link https://www.rfc-editor.org/rfc/rfc3987 RFC 3987 Internationalized Resource Identifiers}
|
|
85
|
+
*/
|
|
86
|
+
export function link(value) {
|
|
87
|
+
return assert(value, isIRI);
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Computes a content digest of a JSON value.
|
|
91
|
+
*
|
|
92
|
+
* Values with equal content get the same digest, whatever the order of their object entries. A pipeline can
|
|
93
|
+
* therefore skip values it has already seen, and detect when a value it already processed changes between runs.
|
|
94
|
+
* Arrays are ordered, though: the same items in a different order produce a different digest.
|
|
95
|
+
*
|
|
96
|
+
* Only content that survives JSON serialisation is digested, so values differing only in what serialisation drops
|
|
97
|
+
* share a digest. Non-finite numbers count as `null` and negative zero as `0`, just as they would once stored.
|
|
98
|
+
*
|
|
99
|
+
* @param value The JSON value to be digested
|
|
100
|
+
*
|
|
101
|
+
* @returns The lowercase hex-encoded SHA-256 digest of the content of `value`
|
|
102
|
+
*/
|
|
103
|
+
export function digest(value) {
|
|
104
|
+
return createHash("sha256")
|
|
105
|
+
.update(JSON.stringify(value, (_, value) => normalise(value)))
|
|
106
|
+
.digest("hex");
|
|
107
|
+
function normalise(value) {
|
|
108
|
+
return isObject(value)
|
|
109
|
+
? Object.fromEntries(Object.entries(value).sort(by(([name]) => name)))
|
|
110
|
+
: value;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH;;;;;;;;;;;;;GAaG;AAEH,OAAO,EAAE,MAAM,EAAE,SAAS,EAAE,QAAQ,EAAE,QAAQ,EAAE,QAAQ,EAAc,MAAM,iBAAiB,CAAC;AAC9F,OAAO,EAAE,EAAE,EAAE,MAAM,uBAAuB,CAAC;AAC3C,OAAO,EAAY,KAAK,EAAE,MAAM,0BAA0B,CAAC;AAC3D,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAEzC,cAAc,WAAW,CAAC;AAC1B,cAAc,YAAY,CAAC;AAC3B,cAAc,eAAe,CAAC;AAG9B;;;;;;;;GAQG;AACH,MAAM,UAAU,OAAO,CAAC,KAAY;IACnC,OAAO,MAAM,CAAC,KAAK,EAAE,SAAS,CAAC,CAAC;AACjC,CAAC;AAED;;;;;;;;GAQG;AACH,MAAM,UAAU,MAAM,CAAC,KAAY;IAClC,OAAO,MAAM,CAAC,KAAK,EAAE,QAAQ,CAAC,CAAC;AAChC,CAAC;AAED;;;;;;;;GAQG;AACH,MAAM,UAAU,MAAM,CAAC,KAAY;IAClC,OAAO,MAAM,CAAC,KAAK,EAAE,QAAQ,CAAC,CAAC;AAChC,CAAC;AAED;;;;;;;;;;;;GAYG;AACH,MAAM,UAAU,IAAI,CAAC,KAAY;IAChC,OAAO,MAAM,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC;AAC7B,CAAC;AAGD;;;;;;;;;;;;;GAaG;AACH,MAAM,UAAU,MAAM,CAAC,KAAY;IAElC,OAAO,UAAU,CAAC,QAAQ,CAAC;SACzB,MAAM,CAAC,IAAI,CAAC,SAAS,CAAC,KAAK,EAAE,CAAC,CAAC,EAAE,KAAK,EAAE,EAAE,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC,CAAC;SAC7D,MAAM,CAAC,KAAK,CAAC,CAAC;IAGhB,SAAS,SAAS,CAAC,KAAY;QAC9B,OAAO,QAAQ,CAAC,KAAK,CAAC;YACrB,CAAC,CAAC,MAAM,CAAC,WAAW,CAAC,MAAM,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,EAAE,EAAE,CAAC,IAAI,CAAC,CAAC,CAAC;YACtE,CAAC,CAAC,KAAK,CAAC;IACV,CAAC;AAEF,CAAC"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { type Value } from "@grafctory/core";
|
|
2
|
+
/**
|
|
3
|
+
* Selects values from a JSON value.
|
|
4
|
+
*
|
|
5
|
+
* Helper backing the `JPath` selector, which states the selection contract.
|
|
6
|
+
*/
|
|
7
|
+
export declare function select(value: Value, path: string): readonly Value[];
|
|
8
|
+
//# sourceMappingURL=jpath.core.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"jpath.core.d.ts","sourceRoot":"","sources":["../src/jpath.core.ts"],"names":[],"mappings":"AAgBA,OAAO,EAAqB,KAAK,KAAK,EAAE,MAAM,iBAAiB,CAAC;AAchE;;;;GAIG;AACH,wBAAgB,MAAM,CAAC,KAAK,EAAE,KAAK,EAAE,IAAI,EAAE,MAAM,GAAG,SAAS,KAAK,EAAE,CAyDnE"}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright © 2026 Metreeca srl
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
import { isArray, isObject } from "@grafctory/core"; // aliased, as the global is used
|
|
17
|
+
import { unescape } from "@grafctory/core/strings";
|
|
18
|
+
const DotPattern = "(?:^|\\.)(?<dot>\\w+)";
|
|
19
|
+
const NamePattern = "\\['(?<name>(?:[^']|\\\\.)*)']";
|
|
20
|
+
const IndexPattern = "\\[(?<index>\\d+)]";
|
|
21
|
+
const WildcardPattern = "(?:^|\\.)\\*|\\[\\*]";
|
|
22
|
+
const StepPattern = `(?:^\\$)?(?:(?:${DotPattern})|(?:${NamePattern})|(?:${IndexPattern})|(?:${WildcardPattern}))`;
|
|
23
|
+
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
24
|
+
/**
|
|
25
|
+
* Selects values from a JSON value.
|
|
26
|
+
*
|
|
27
|
+
* Helper backing the `JPath` selector, which states the selection contract.
|
|
28
|
+
*/
|
|
29
|
+
export function select(value, path) {
|
|
30
|
+
return !path || path === "$" ? [value]
|
|
31
|
+
: parse(path).reduce(step, [value]);
|
|
32
|
+
function parse(path) {
|
|
33
|
+
// sticky, so that steps match contiguously; global, as required by `matchAll`
|
|
34
|
+
const steps = [...path.matchAll(new RegExp(StepPattern, "gy"))];
|
|
35
|
+
// the scan stops at the first unmatched position, leaving the tail of the path uncovered
|
|
36
|
+
const scanned = steps.reduce((length, step) => length + step[0].length, 0);
|
|
37
|
+
if (scanned !== path.length) {
|
|
38
|
+
throw new SyntaxError(`malformed path <${path}>`);
|
|
39
|
+
}
|
|
40
|
+
return steps;
|
|
41
|
+
}
|
|
42
|
+
function step(selection, step) {
|
|
43
|
+
const { dot, name, index } = step.groups ?? {};
|
|
44
|
+
const property = dot ?? name;
|
|
45
|
+
// `undefined` isn't a JSON value: a value assembled in code carries it where a parsed one cannot
|
|
46
|
+
return (property !== undefined ? fields(selection, unescape(property))
|
|
47
|
+
: index !== undefined ? items(selection, Number(index))
|
|
48
|
+
: members(selection)).filter(value => value !== undefined);
|
|
49
|
+
}
|
|
50
|
+
function fields(selection, key) {
|
|
51
|
+
return selection.flatMap(value => isObject(value) && key in value ? [value[key]] : []);
|
|
52
|
+
}
|
|
53
|
+
function items(selection, position) {
|
|
54
|
+
return selection.flatMap(value => isArray(value) && position < value.length ? [value[position]] : []);
|
|
55
|
+
}
|
|
56
|
+
function members(selection) {
|
|
57
|
+
return selection.flatMap(value => isArray(value) || isObject(value) ? Object.values(value) : []);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
//# sourceMappingURL=jpath.core.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"jpath.core.js","sourceRoot":"","sources":["../src/jpath.core.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAc,MAAM,iBAAiB,CAAC,CAAC,iCAAiC;AAClG,OAAO,EAAE,QAAQ,EAAE,MAAM,yBAAyB,CAAC;AAGnD,MAAM,UAAU,GAAG,uBAAuB,CAAC;AAC3C,MAAM,WAAW,GAAG,gCAAgC,CAAC;AACrD,MAAM,YAAY,GAAG,oBAAoB,CAAC;AAC1C,MAAM,eAAe,GAAG,sBAAsB,CAAC;AAE/C,MAAM,WAAW,GAAG,kBAAkB,UAAU,QAAQ,WAAW,QAAQ,YAAY,QAAQ,eAAe,IAAI,CAAC;AAGnH,wHAAwH;AAExH;;;;GAIG;AACH,MAAM,UAAU,MAAM,CAAC,KAAY,EAAE,IAAY;IAEhD,OAAO,CAAC,IAAI,IAAI,IAAI,KAAK,GAAG,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC;QACrC,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,MAAM,CAAmB,IAAI,EAAE,CAAC,KAAK,CAAC,CAAC,CAAC;IAGvD,SAAS,KAAK,CAAC,IAAY;QAE1B,8EAA8E;QAE9E,MAAM,KAAK,GAAG,CAAC,GAAG,IAAI,CAAC,QAAQ,CAAC,IAAI,MAAM,CAAC,WAAW,EAAE,IAAI,CAAC,CAAC,CAAC,CAAC;QAEhE,yFAAyF;QAEzF,MAAM,OAAO,GAAG,KAAK,CAAC,MAAM,CAAC,CAAC,MAAM,EAAE,IAAI,EAAE,EAAE,CAAC,MAAM,GAAC,IAAI,CAAC,CAAC,CAAC,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC;QAEzE,IAAK,OAAO,KAAK,IAAI,CAAC,MAAM,EAAG,CAAC;YAC/B,MAAM,IAAI,WAAW,CAAC,mBAAmB,IAAI,GAAG,CAAC,CAAC;QACnD,CAAC;QAED,OAAO,KAAK,CAAC;IAEd,CAAC;IAED,SAAS,IAAI,CAAC,SAA2B,EAAE,IAAqB;QAE/D,MAAM,EAAE,GAAG,EAAE,IAAI,EAAE,KAAK,EAAE,GAAG,IAAI,CAAC,MAAM,IAAI,EAAE,CAAC;QAE/C,MAAM,QAAQ,GAAG,GAAG,IAAI,IAAI,CAAC;QAE7B,iGAAiG;QAEjG,OAAO,CAAC,QAAQ,KAAK,SAAS,CAAC,CAAC,CAAC,MAAM,CAAC,SAAS,EAAE,QAAQ,CAAC,QAAQ,CAAC,CAAC;YACrE,CAAC,CAAC,KAAK,KAAK,SAAS,CAAC,CAAC,CAAC,KAAK,CAAC,SAAS,EAAE,MAAM,CAAC,KAAK,CAAC,CAAC;gBACtD,CAAC,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE,CAAC,KAAK,KAAK,SAAS,CAAC,CAAC;IAE9D,CAAC;IAED,SAAS,MAAM,CAAC,SAA2B,EAAE,GAAW;QAEvD,OAAO,SAAS,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE,CAAC,QAAQ,CAAC,KAAK,CAAC,IAAI,GAAG,IAAI,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC;IAExF,CAAC;IAED,SAAS,KAAK,CAAC,SAA2B,EAAE,QAAgB;QAE3D,OAAO,SAAS,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE,CAAC,OAAO,CAAQ,KAAK,CAAC,IAAI,QAAQ,GAAG,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC;IAE9G,CAAC;IAED,SAAS,OAAO,CAAC,SAA2B;QAE3C,OAAO,SAAS,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE,CAAC,OAAO,CAAQ,KAAK,CAAC,IAAI,QAAQ,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC;IAEzG,CAAC;AAGF,CAAC"}
|
package/dist/jpath.d.ts
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import { type Value } from "@grafctory/core";
|
|
2
|
+
import { Task } from "@grafctory/flow";
|
|
3
|
+
/**
|
|
4
|
+
* JSONPath-like selector over JSON values.
|
|
5
|
+
*
|
|
6
|
+
* Selects nested values from a set of target JSON values, using a JSONPath-like syntax. The target set is fixed when
|
|
7
|
+
* the selector is created.
|
|
8
|
+
*
|
|
9
|
+
* @see {@link https://www.rfc-editor.org/rfc/rfc9535 RFC 9535 JSONPath Query Expressions for JSON}
|
|
10
|
+
*/
|
|
11
|
+
export type JPath = {
|
|
12
|
+
/**
|
|
13
|
+
* Selects values.
|
|
14
|
+
*
|
|
15
|
+
* Each selection spans the whole target set: the values selected from each target are merged into a single list.
|
|
16
|
+
*
|
|
17
|
+
* A path is a sequence of steps, each selecting from the values selected by the previous one. Steps are written
|
|
18
|
+
* one after the other, separated only by the leading `.` of the steps that carry one:
|
|
19
|
+
*
|
|
20
|
+
* - `$` — the target value itself; allowed only as the leading step, where it may be omitted
|
|
21
|
+
* - `.property` / `property` — object property
|
|
22
|
+
* - `['property']` — object property, with lenient JSON string escapes: an unknown escape stands for the character
|
|
23
|
+
* it introduces, so `\'` names an apostrophe
|
|
24
|
+
* - `[0]` — array element by index
|
|
25
|
+
* - `.*` / `[*]` — every element of an array or every property value of an object
|
|
26
|
+
*
|
|
27
|
+
* Property steps don't reach into arrays: selecting the properties of objects inside an array requires an explicit
|
|
28
|
+
* `[*]` or `.*` step.
|
|
29
|
+
*
|
|
30
|
+
* Only JSON values are selected: `null` is selected like any other value, while `undefined` (which can only occur
|
|
31
|
+
* in values built in code) is skipped like an absent property.
|
|
32
|
+
*
|
|
33
|
+
* @param path The selection path; an empty path or `$` selects the target values unchanged
|
|
34
|
+
*
|
|
35
|
+
* @returns An immutable list of the values selected by `path`, ordered by target and, within each target, in
|
|
36
|
+
* document order; empty if `path` selects no value
|
|
37
|
+
*
|
|
38
|
+
* @throws {@link !SyntaxError SyntaxError} If `path` is malformed
|
|
39
|
+
*/
|
|
40
|
+
(path: string): readonly Value[];
|
|
41
|
+
};
|
|
42
|
+
/**
|
|
43
|
+
* Creates a JSON path selector task.
|
|
44
|
+
*
|
|
45
|
+
* The task converts a feed of values into a feed of {@link JPath} selectors, one selector per value, so that downstream
|
|
46
|
+
* tasks select nested values by path rather than walking the structure.
|
|
47
|
+
*
|
|
48
|
+
* > [!NOTE]
|
|
49
|
+
* >
|
|
50
|
+
* > - **Incremental**: each selector is emitted as soon as its value is drawn, so endless sources are processed for as
|
|
51
|
+
* > long as the feed is consumed.
|
|
52
|
+
* > - **Streaming**: values are processed one at a time and none is retained, so memory use doesn't grow with the
|
|
53
|
+
* > length of the feed; each selector keeps its target value alive for as long as it is referenced.
|
|
54
|
+
* > - **Stateless**: each value is processed independently, so the result doesn't depend on how the feed is split
|
|
55
|
+
* > across nested feeds or runs.
|
|
56
|
+
*
|
|
57
|
+
* @returns A task converting a feed of values into a feed of path selectors
|
|
58
|
+
*
|
|
59
|
+
* @throws {@link !Error Error} While the feed is consumed, if the source feed fails
|
|
60
|
+
*
|
|
61
|
+
* @group Factories
|
|
62
|
+
*/
|
|
63
|
+
export declare function jpath(): Task<Value, JPath>;
|
|
64
|
+
/**
|
|
65
|
+
* Creates a JSON path mapping task.
|
|
66
|
+
*
|
|
67
|
+
* The task converts a feed of values into a feed of mapped results, one result per value, so that downstream tasks
|
|
68
|
+
* work on the shape they need rather than on the one stated by the source.
|
|
69
|
+
*
|
|
70
|
+
* > [!NOTE]
|
|
71
|
+
* >
|
|
72
|
+
* > - **Incremental**: each result is emitted as soon as its value is drawn, so endless sources are processed for as
|
|
73
|
+
* > long as the feed is consumed.
|
|
74
|
+
* > - **Streaming**: values are processed one at a time and released once mapped, so memory use doesn't grow with the
|
|
75
|
+
* > length of the feed.
|
|
76
|
+
* > - **Stateless**: each value is mapped independently, so the result doesn't depend on how the feed is split across
|
|
77
|
+
* > nested feeds or runs.
|
|
78
|
+
*
|
|
79
|
+
* @typeParam V The type of the results mapped from incoming values
|
|
80
|
+
*
|
|
81
|
+
* @param mapper The mapping function, applied to a {@link JPath} selector targeting the value being processed
|
|
82
|
+
*
|
|
83
|
+
* @returns A task converting a feed of values into a feed of mapped results
|
|
84
|
+
*
|
|
85
|
+
* @throws {@link !Error Error} While the feed is consumed, if the source feed fails or `mapper` throws, including a
|
|
86
|
+
* {@link !SyntaxError SyntaxError} for a malformed path
|
|
87
|
+
*
|
|
88
|
+
* @example
|
|
89
|
+
*
|
|
90
|
+
* ```typescript
|
|
91
|
+
* const events = jpath(path => ({
|
|
92
|
+
*
|
|
93
|
+
* id: path("$.id"),
|
|
94
|
+
* tags: path("$.tags[*]")
|
|
95
|
+
*
|
|
96
|
+
* }));
|
|
97
|
+
* ```
|
|
98
|
+
*
|
|
99
|
+
* @group Factories
|
|
100
|
+
*/
|
|
101
|
+
export declare function jpath<V>(mapper: (path: JPath) => V): Task<Value, V>;
|
|
102
|
+
/**
|
|
103
|
+
* Creates a JSON path selector over given values.
|
|
104
|
+
*
|
|
105
|
+
* Targets values already at hand, outside a feed, with the same selection semantics as the selectors emitted by the
|
|
106
|
+
* feed tasks.
|
|
107
|
+
*
|
|
108
|
+
* > [!IMPORTANT]
|
|
109
|
+
* >
|
|
110
|
+
* > A call without values, including one spreading an empty list, creates a selector task over a feed rather than a
|
|
111
|
+
* > selector with an empty target set.
|
|
112
|
+
*
|
|
113
|
+
* @param values The target values, in selection order
|
|
114
|
+
*
|
|
115
|
+
* @returns An immutable selector targeting `values`
|
|
116
|
+
*
|
|
117
|
+
* @group Factories
|
|
118
|
+
*/
|
|
119
|
+
export declare function jpath(...values: readonly Value[]): JPath;
|
|
120
|
+
//# sourceMappingURL=jpath.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"jpath.d.ts","sourceRoot":"","sources":["../src/jpath.ts"],"names":[],"mappings":"AAgBA,OAAO,EAAc,KAAK,KAAK,EAAE,MAAM,iBAAiB,CAAC;AACzD,OAAO,EAAE,IAAI,EAAE,MAAM,iBAAiB,CAAC;AAKvC;;;;;;;GAOG;AACH,MAAM,MAAM,KAAK,GAAG;IAEnB;;;;;;;;;;;;;;;;;;;;;;;;;;;OA2BG;IACH,CAAC,IAAI,EAAE,MAAM,GAAG,SAAS,KAAK,EAAE,CAAC;CAEjC,CAAA;AAKD;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAAgB,KAAK,IAAI,IAAI,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC;AAE5C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoCG;AACH,wBAAgB,KAAK,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,IAAI,EAAE,KAAK,KAAK,CAAC,GAAG,IAAI,CAAC,KAAK,EAAE,CAAC,CAAC,CAAC;AAErE;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,KAAK,CAAC,GAAG,MAAM,EAAE,SAAS,KAAK,EAAE,GAAG,KAAK,CAAC"}
|
package/dist/jpath.js
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright © 2026 Metreeca srl
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
import { isFunction } from "@grafctory/core";
|
|
17
|
+
import { map } from "@grafctory/flow/tasks";
|
|
18
|
+
import { select } from "./jpath.core.js";
|
|
19
|
+
/**
|
|
20
|
+
* Creates a JSON path selector.
|
|
21
|
+
*/
|
|
22
|
+
export function jpath(...args) {
|
|
23
|
+
return isMapper(args) ? map(value => args[0](selector([value])))
|
|
24
|
+
: args.length === 0 ? map(value => selector([value]))
|
|
25
|
+
: selector(args);
|
|
26
|
+
function isMapper(args) {
|
|
27
|
+
return isFunction(args[0]);
|
|
28
|
+
}
|
|
29
|
+
function selector(values) {
|
|
30
|
+
return Object.freeze((path) => values.flatMap(value => select(value, path)));
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
//# sourceMappingURL=jpath.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"jpath.js","sourceRoot":"","sources":["../src/jpath.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,OAAO,EAAE,UAAU,EAAc,MAAM,iBAAiB,CAAC;AAEzD,OAAO,EAAE,GAAG,EAAE,MAAM,uBAAuB,CAAC;AAC5C,OAAO,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAC;AAiIzC;;GAEG;AACH,MAAM,UAAU,KAAK,CAAC,GAAG,IAAoE;IAE5F,OAAO,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,GAAG,CAAiB,KAAK,CAAC,EAAE,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;QAC/E,CAAC,CAAC,IAAI,CAAC,MAAM,KAAK,CAAC,CAAC,CAAC,CAAC,GAAG,CAAe,KAAK,CAAC,EAAE,CAAC,QAAQ,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC;YAClE,CAAC,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC;IAGnB,SAAS,QAAQ,CAAC,IAAwB;QACzC,OAAO,UAAU,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC;IAC5B,CAAC;IAED,SAAS,QAAQ,CAAC,MAAwB;QACzC,OAAO,MAAM,CAAC,MAAM,CAAC,CAAC,IAAY,EAAE,EAAE,CAAC,MAAM,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE,CAAC,MAAM,CAAC,KAAK,EAAE,IAAI,CAAC,CAAC,CAAC,CAAC;IACtF,CAAC;AAEF,CAAC"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { Object, Value } from "@grafctory/core";
|
|
2
|
+
/**
|
|
3
|
+
* Parses a JSON document.
|
|
4
|
+
*
|
|
5
|
+
* Helper backing the `json()` task, which states the parsing contract.
|
|
6
|
+
*/
|
|
7
|
+
export declare function process<V extends Value = Object>(document: string | Response): Promise<undefined | V>;
|
|
8
|
+
//# sourceMappingURL=json.core.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"json.core.d.ts","sourceRoot":"","sources":["../src/json.core.ts"],"names":[],"mappings":"AAgBA,OAAO,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,iBAAiB,CAAC;AA+BrD;;;;GAIG;AACH,wBAAsB,OAAO,CAAC,CAAC,SAAS,KAAK,GAAG,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,QAAQ,GAAG,OAAO,CAAC,SAAS,GAAG,CAAC,CAAC,CAwC3G"}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright © 2026 Metreeca srl
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
import { parseItem } from "@grafctory/http";
|
|
17
|
+
import { log, report } from "@grafctory/tape";
|
|
18
|
+
/**
|
|
19
|
+
* The media types a JSON document is served under.
|
|
20
|
+
*
|
|
21
|
+
* Matches any type whose subtype is `json` or carries the `+json` structured syntax suffix, so that JSON-based
|
|
22
|
+
* formats, `application/ld+json` and `application/problem+json` among them, are recognised alongside plain
|
|
23
|
+
* `application/json`.
|
|
24
|
+
*
|
|
25
|
+
* @see {@link https://www.rfc-editor.org/rfc/rfc6838#section-4.2.8 RFC 6838 § 4.2.8 - Structured Syntax Suffixes}
|
|
26
|
+
*/
|
|
27
|
+
const JSONType = /^[-\w.]+\/(?:[-\w.]+\+)?json$/i;
|
|
28
|
+
/**
|
|
29
|
+
* The charsets a JSON document is encoded in.
|
|
30
|
+
*
|
|
31
|
+
* Matches the labels of UTF-8, the only encoding allowed for JSON exchanged between systems.
|
|
32
|
+
*
|
|
33
|
+
* @see {@link https://www.rfc-editor.org/rfc/rfc8259#section-8.1 RFC 8259 § 8.1 - Character Encoding}
|
|
34
|
+
*/
|
|
35
|
+
const UTF8Charset = /^utf-?8$/i;
|
|
36
|
+
const logger = log(import.meta.url);
|
|
37
|
+
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
38
|
+
/**
|
|
39
|
+
* Parses a JSON document.
|
|
40
|
+
*
|
|
41
|
+
* Helper backing the `json()` task, which states the parsing contract.
|
|
42
|
+
*/
|
|
43
|
+
export async function process(document) {
|
|
44
|
+
const text = (document instanceof Response ? await read(document) : document).trim();
|
|
45
|
+
return text ? parse(text) : undefined; // a document holding nothing but whitespace holds no value
|
|
46
|
+
async function read(response) {
|
|
47
|
+
const [type, parameters] = parseItem(response.headers.get("Content-Type"));
|
|
48
|
+
const charset = parameters.get("charset") ?? "";
|
|
49
|
+
if (type && !JSONType.test(type)) {
|
|
50
|
+
logger.warn `unexpected <${type}> content type`;
|
|
51
|
+
}
|
|
52
|
+
if (charset && !UTF8Charset.test(charset)) {
|
|
53
|
+
logger.warn `unsupported <${charset}> charset`;
|
|
54
|
+
}
|
|
55
|
+
return response.text();
|
|
56
|
+
}
|
|
57
|
+
function parse(text) {
|
|
58
|
+
try {
|
|
59
|
+
return JSON.parse(text);
|
|
60
|
+
}
|
|
61
|
+
catch (error) {
|
|
62
|
+
logger.warn `malformed JSON document (${report(error)})`;
|
|
63
|
+
return undefined;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
//# sourceMappingURL=json.core.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"json.core.js","sourceRoot":"","sources":["../src/json.core.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAGH,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAC5C,OAAO,EAAE,GAAG,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAC;AAG9C;;;;;;;;GAQG;AACH,MAAM,QAAQ,GAAG,gCAAgC,CAAC;AAElD;;;;;;GAMG;AACH,MAAM,WAAW,GAAG,WAAW,CAAC;AAGhC,MAAM,MAAM,GAAG,GAAG,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;AAGpC,wHAAwH;AAExH;;;;GAIG;AACH,MAAM,CAAC,KAAK,UAAU,OAAO,CAA2B,QAA2B;IAElF,MAAM,IAAI,GAAG,CAAC,QAAQ,YAAY,QAAQ,CAAC,CAAC,CAAC,MAAM,IAAI,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,IAAI,EAAE,CAAC;IAErF,OAAO,IAAI,CAAC,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,2DAA2D;IAGlG,KAAK,UAAU,IAAI,CAAC,QAAkB;QAErC,MAAM,CAAC,IAAI,EAAE,UAAU,CAAC,GAAG,SAAS,CAAC,QAAQ,CAAC,OAAO,CAAC,GAAG,CAAC,cAAc,CAAC,CAAC,CAAC;QAC3E,MAAM,OAAO,GAAG,UAAU,CAAC,GAAG,CAAC,SAAS,CAAC,IAAI,EAAE,CAAC;QAEhD,IAAK,IAAI,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,EAAG,CAAC;YACpC,MAAM,CAAC,IAAI,CAAA,eAAe,IAAI,gBAAgB,CAAC;QAChD,CAAC;QAED,IAAK,OAAO,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC,OAAO,CAAC,EAAG,CAAC;YAC7C,MAAM,CAAC,IAAI,CAAA,gBAAgB,OAAO,WAAW,CAAC;QAC/C,CAAC;QAED,OAAO,QAAQ,CAAC,IAAI,EAAE,CAAC;IAExB,CAAC;IAED,SAAS,KAAK,CAAC,IAAY;QAE1B,IAAI,CAAC;YAEJ,OAAO,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;QAEzB,CAAC;QAAC,OAAQ,KAAK,EAAG,CAAC;YAElB,MAAM,CAAC,IAAI,CAAA,4BAA4B,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC;YAExD,OAAO,SAAS,CAAC;QAElB,CAAC;IAEF,CAAC;AAEF,CAAC"}
|
package/dist/json.d.ts
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import type { Object, Value } from "@grafctory/core";
|
|
2
|
+
import type { Task } from "@grafctory/flow";
|
|
3
|
+
/**
|
|
4
|
+
* Creates a JSON parser.
|
|
5
|
+
*
|
|
6
|
+
* The task converts a feed of JSON documents into a feed of values, one value per document, so that downstream tasks
|
|
7
|
+
* work on structured data rather than on text.
|
|
8
|
+
*
|
|
9
|
+
* Each document is given either as text or as a response carrying it in its body. Empty or whitespace-only documents
|
|
10
|
+
* produce no value, and neither do responses without a body.
|
|
11
|
+
*
|
|
12
|
+
* Response bodies are always decoded as UTF-8, the only encoding JSON is exchanged in. Invalid UTF-8 bytes are read as
|
|
13
|
+
* replacement characters.
|
|
14
|
+
*
|
|
15
|
+
* A response is logged as a warning, and parsed anyway, if its content type is not a JSON one or its charset is not
|
|
16
|
+
* UTF-8. JSON content types are `application/json` and `+json` formats such as `application/ld+json`.
|
|
17
|
+
*
|
|
18
|
+
* > [!NOTE]
|
|
19
|
+
* >
|
|
20
|
+
* > - **Incremental**: each value is emitted as soon as its document is parsed, so endless sources are processed for
|
|
21
|
+
* > as long as the feed is consumed.
|
|
22
|
+
* > - **Materialising**: each document is held in memory as a whole while it is parsed, so peak memory use is about
|
|
23
|
+
* > twice the size of the largest document, regardless of the length of the feed.
|
|
24
|
+
* > - **Stateless**: each document is parsed independently, so the result doesn't depend on how the feed is split
|
|
25
|
+
* > across nested feeds or runs.
|
|
26
|
+
*
|
|
27
|
+
* > [!WARNING]
|
|
28
|
+
* >
|
|
29
|
+
* > Malformed documents are skipped and logged as warnings, and the feed runs to completion.
|
|
30
|
+
*
|
|
31
|
+
* @typeParam V The type of the values produced; parsed documents are emitted as is and are not validated against it;
|
|
32
|
+
* defaults to a JSON {@link Object}
|
|
33
|
+
*
|
|
34
|
+
* @returns A task converting a feed of JSON documents, given as text or as responses, into a feed of values
|
|
35
|
+
*
|
|
36
|
+
* @throws {@link !Error Error} While the feed is consumed, if the source feed fails or a response body can't be
|
|
37
|
+
* read
|
|
38
|
+
*
|
|
39
|
+
* @see {@link https://www.rfc-editor.org/rfc/rfc8259 RFC 8259 JSON Data Interchange Format}
|
|
40
|
+
* @see {@link https://www.rfc-editor.org/rfc/rfc9110#section-8.3 RFC 9110 § 8.3 - Content-Type}
|
|
41
|
+
*
|
|
42
|
+
* @group Factories
|
|
43
|
+
*/
|
|
44
|
+
export declare function json<V extends Value = Object>(): Task<string | Response, V>;
|
|
45
|
+
//# sourceMappingURL=json.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"json.d.ts","sourceRoot":"","sources":["../src/json.ts"],"names":[],"mappings":"AAgBA,OAAO,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,iBAAiB,CAAC;AACrD,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,iBAAiB,CAAC;AAO5C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAwCG;AACH,wBAAgB,IAAI,CAAC,CAAC,SAAS,KAAK,GAAG,MAAM,KAAK,IAAI,CAAC,MAAM,GAAG,QAAQ,EAAE,CAAC,CAAC,CAkB3E"}
|
package/dist/json.js
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright © 2026 Metreeca srl
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
import { items } from "@grafctory/flow/feeds";
|
|
17
|
+
import { process } from "./json.core.js";
|
|
18
|
+
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
19
|
+
/**
|
|
20
|
+
* Creates a JSON parser.
|
|
21
|
+
*
|
|
22
|
+
* The task converts a feed of JSON documents into a feed of values, one value per document, so that downstream tasks
|
|
23
|
+
* work on structured data rather than on text.
|
|
24
|
+
*
|
|
25
|
+
* Each document is given either as text or as a response carrying it in its body. Empty or whitespace-only documents
|
|
26
|
+
* produce no value, and neither do responses without a body.
|
|
27
|
+
*
|
|
28
|
+
* Response bodies are always decoded as UTF-8, the only encoding JSON is exchanged in. Invalid UTF-8 bytes are read as
|
|
29
|
+
* replacement characters.
|
|
30
|
+
*
|
|
31
|
+
* A response is logged as a warning, and parsed anyway, if its content type is not a JSON one or its charset is not
|
|
32
|
+
* UTF-8. JSON content types are `application/json` and `+json` formats such as `application/ld+json`.
|
|
33
|
+
*
|
|
34
|
+
* > [!NOTE]
|
|
35
|
+
* >
|
|
36
|
+
* > - **Incremental**: each value is emitted as soon as its document is parsed, so endless sources are processed for
|
|
37
|
+
* > as long as the feed is consumed.
|
|
38
|
+
* > - **Materialising**: each document is held in memory as a whole while it is parsed, so peak memory use is about
|
|
39
|
+
* > twice the size of the largest document, regardless of the length of the feed.
|
|
40
|
+
* > - **Stateless**: each document is parsed independently, so the result doesn't depend on how the feed is split
|
|
41
|
+
* > across nested feeds or runs.
|
|
42
|
+
*
|
|
43
|
+
* > [!WARNING]
|
|
44
|
+
* >
|
|
45
|
+
* > Malformed documents are skipped and logged as warnings, and the feed runs to completion.
|
|
46
|
+
*
|
|
47
|
+
* @typeParam V The type of the values produced; parsed documents are emitted as is and are not validated against it;
|
|
48
|
+
* defaults to a JSON {@link Object}
|
|
49
|
+
*
|
|
50
|
+
* @returns A task converting a feed of JSON documents, given as text or as responses, into a feed of values
|
|
51
|
+
*
|
|
52
|
+
* @throws {@link !Error Error} While the feed is consumed, if the source feed fails or a response body can't be
|
|
53
|
+
* read
|
|
54
|
+
*
|
|
55
|
+
* @see {@link https://www.rfc-editor.org/rfc/rfc8259 RFC 8259 JSON Data Interchange Format}
|
|
56
|
+
* @see {@link https://www.rfc-editor.org/rfc/rfc9110#section-8.3 RFC 9110 § 8.3 - Content-Type}
|
|
57
|
+
*
|
|
58
|
+
* @group Factories
|
|
59
|
+
*/
|
|
60
|
+
export function json() {
|
|
61
|
+
return documents => items((async function* () {
|
|
62
|
+
for await (const document of documents) {
|
|
63
|
+
const value = await process(document);
|
|
64
|
+
if (value !== undefined) { // a document holding no parsable value contributes no value
|
|
65
|
+
yield value;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
})());
|
|
69
|
+
}
|
|
70
|
+
//# sourceMappingURL=json.js.map
|
package/dist/json.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"json.js","sourceRoot":"","sources":["../src/json.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAIH,OAAO,EAAE,KAAK,EAAE,MAAM,uBAAuB,CAAC;AAC9C,OAAO,EAAE,OAAO,EAAE,MAAM,gBAAgB,CAAC;AAGzC,wHAAwH;AAExH;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAwCG;AACH,MAAM,UAAU,IAAI;IAEnB,OAAO,SAAS,CAAC,EAAE,CAAC,KAAK,CAAC,CAAC,KAAK,SAAS,CAAC;QAEzC,IAAI,KAAK,EAAE,MAAM,QAAQ,IAAI,SAAS,EAAE,CAAC;YAExC,MAAM,KAAK,GAAG,MAAM,OAAO,CAAI,QAAQ,CAAC,CAAC;YAEzC,IAAK,KAAK,KAAK,SAAS,EAAG,CAAC,CAAC,4DAA4D;gBAExF,MAAM,KAAK,CAAC;YAEb,CAAC;QAEF,CAAC;IAEF,CAAC,CAAC,EAAE,CAAC,CAAC;AAEP,CAAC"}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { Validator } from "@grafctory/core/trace";
|
|
2
|
+
import type { Task } from "@grafctory/flow";
|
|
3
|
+
/**
|
|
4
|
+
* Creates a value validation task.
|
|
5
|
+
*
|
|
6
|
+
* The task keeps the values a validator accepts and drops the ones it rejects, so that downstream tasks work only on
|
|
7
|
+
* values known to meet the expected constraints.
|
|
8
|
+
*
|
|
9
|
+
* > [!NOTE]
|
|
10
|
+
* >
|
|
11
|
+
* > - **Incremental**: each accepted value is emitted as soon as it is drawn, so endless sources are processed for as
|
|
12
|
+
* > long as the feed is consumed.
|
|
13
|
+
* > - **Streaming**: values are validated one at a time and none is retained, so memory use doesn't grow with the
|
|
14
|
+
* > length of the feed.
|
|
15
|
+
* > - **Stateless**: each value is validated independently, so the result doesn't depend on how the feed is split
|
|
16
|
+
* > across nested feeds or runs.
|
|
17
|
+
*
|
|
18
|
+
* > [!WARNING]
|
|
19
|
+
* >
|
|
20
|
+
* > Rejected values are dropped and logged as warnings, together with the trace of their violations, and the feed runs
|
|
21
|
+
* > to completion.
|
|
22
|
+
*
|
|
23
|
+
* @typeParam V The type of the validated values
|
|
24
|
+
*
|
|
25
|
+
* @param validator The validator applied to each value, reporting the trace of the violations it finds, or nothing if
|
|
26
|
+
* the value meets every constraint
|
|
27
|
+
*
|
|
28
|
+
* @returns A task keeping the values accepted by `validator`
|
|
29
|
+
*
|
|
30
|
+
* @throws {@link !Error Error} While the feed is consumed, if the source feed fails or `validator` throws
|
|
31
|
+
*
|
|
32
|
+
* @group Factories
|
|
33
|
+
*/
|
|
34
|
+
export declare function validate<V>(validator: Validator<V>): Task<V>;
|
|
35
|
+
//# sourceMappingURL=validate.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"validate.d.ts","sourceRoot":"","sources":["../src/validate.ts"],"names":[],"mappings":"AAgBA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,uBAAuB,CAAC;AACvD,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,iBAAiB,CAAC;AAU5C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AACH,wBAAgB,QAAQ,CAAC,CAAC,EAAE,SAAS,EAAE,SAAS,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC,CAAC,CAAC,CAoB5D"}
|
package/dist/validate.js
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright © 2026 Metreeca srl
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
import { filter } from "@grafctory/flow/tasks";
|
|
17
|
+
import { log } from "@grafctory/tape";
|
|
18
|
+
const logger = log(import.meta.url);
|
|
19
|
+
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
20
|
+
/**
|
|
21
|
+
* Creates a value validation task.
|
|
22
|
+
*
|
|
23
|
+
* The task keeps the values a validator accepts and drops the ones it rejects, so that downstream tasks work only on
|
|
24
|
+
* values known to meet the expected constraints.
|
|
25
|
+
*
|
|
26
|
+
* > [!NOTE]
|
|
27
|
+
* >
|
|
28
|
+
* > - **Incremental**: each accepted value is emitted as soon as it is drawn, so endless sources are processed for as
|
|
29
|
+
* > long as the feed is consumed.
|
|
30
|
+
* > - **Streaming**: values are validated one at a time and none is retained, so memory use doesn't grow with the
|
|
31
|
+
* > length of the feed.
|
|
32
|
+
* > - **Stateless**: each value is validated independently, so the result doesn't depend on how the feed is split
|
|
33
|
+
* > across nested feeds or runs.
|
|
34
|
+
*
|
|
35
|
+
* > [!WARNING]
|
|
36
|
+
* >
|
|
37
|
+
* > Rejected values are dropped and logged as warnings, together with the trace of their violations, and the feed runs
|
|
38
|
+
* > to completion.
|
|
39
|
+
*
|
|
40
|
+
* @typeParam V The type of the validated values
|
|
41
|
+
*
|
|
42
|
+
* @param validator The validator applied to each value, reporting the trace of the violations it finds, or nothing if
|
|
43
|
+
* the value meets every constraint
|
|
44
|
+
*
|
|
45
|
+
* @returns A task keeping the values accepted by `validator`
|
|
46
|
+
*
|
|
47
|
+
* @throws {@link !Error Error} While the feed is consumed, if the source feed fails or `validator` throws
|
|
48
|
+
*
|
|
49
|
+
* @group Factories
|
|
50
|
+
*/
|
|
51
|
+
export function validate(validator) {
|
|
52
|
+
return filter(value => {
|
|
53
|
+
const trace = validator(value);
|
|
54
|
+
if (trace === undefined) {
|
|
55
|
+
return true;
|
|
56
|
+
}
|
|
57
|
+
else {
|
|
58
|
+
logger.warn `invalid value (${JSON.stringify(trace)})`;
|
|
59
|
+
return false;
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
//# sourceMappingURL=validate.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"validate.js","sourceRoot":"","sources":["../src/validate.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAIH,OAAO,EAAE,MAAM,EAAE,MAAM,uBAAuB,CAAC;AAC/C,OAAO,EAAE,GAAG,EAAE,MAAM,iBAAiB,CAAC;AAGtC,MAAM,MAAM,GAAG,GAAG,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;AAGpC,wHAAwH;AAExH;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AACH,MAAM,UAAU,QAAQ,CAAI,SAAuB;IAElD,OAAO,MAAM,CAAC,KAAK,CAAC,EAAE;QAErB,MAAM,KAAK,GAAG,SAAS,CAAC,KAAK,CAAC,CAAC;QAE/B,IAAK,KAAK,KAAK,SAAS,EAAG,CAAC;YAE3B,OAAO,IAAI,CAAC;QAEb,CAAC;aAAM,CAAC;YAEP,MAAM,CAAC,IAAI,CAAA,kBAAkB,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,GAAG,CAAC;YAEtD,OAAO,KAAK,CAAC;QAEd,CAAC;IAEF,CAAC,CAAC,CAAC;AAEJ,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,60 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@grafctory/mime-json",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.6.0",
|
|
4
|
+
"description": "JSON processing tasks for @grafctory/mime.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"typescript",
|
|
7
|
+
"pipeline",
|
|
8
|
+
"etl",
|
|
9
|
+
"extraction",
|
|
10
|
+
"json"
|
|
11
|
+
],
|
|
12
|
+
"homepage": "https://grafctory.github.io/mime/",
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/grafctory/mime/issues",
|
|
15
|
+
"email": "info@grafctory.dev"
|
|
16
|
+
},
|
|
17
|
+
"license": "Apache-2.0",
|
|
18
|
+
"author": {
|
|
19
|
+
"name": "Metreeca",
|
|
20
|
+
"email": "info@metreeca.com",
|
|
21
|
+
"url": "https://www.metreeca.com/"
|
|
22
|
+
},
|
|
23
|
+
"repository": {
|
|
24
|
+
"type": "git",
|
|
25
|
+
"url": "git+https://github.com/grafctory/mime.git",
|
|
26
|
+
"directory": "packages/mime-json"
|
|
27
|
+
},
|
|
28
|
+
"type": "module",
|
|
29
|
+
"sideEffects": false,
|
|
30
|
+
"exports": {
|
|
31
|
+
".": {
|
|
32
|
+
"@grafctory/source": "./src/index.ts",
|
|
33
|
+
"default": "./dist/index.js"
|
|
34
|
+
}
|
|
35
|
+
},
|
|
36
|
+
"files": [
|
|
37
|
+
"README.md",
|
|
38
|
+
"LICENSE",
|
|
39
|
+
"dist"
|
|
40
|
+
],
|
|
41
|
+
"engines": {
|
|
42
|
+
"node": ">=22.22.0"
|
|
43
|
+
},
|
|
44
|
+
"scripts": {
|
|
45
|
+
"clean": "ush rm dist docs node_modules",
|
|
46
|
+
"build": "ush rm dist && tsc -p tsconfig.build.json",
|
|
47
|
+
"check": "npx vitest --typecheck run"
|
|
48
|
+
},
|
|
49
|
+
"dependencies": {
|
|
50
|
+
"@grafctory/core": "^0.14.0",
|
|
51
|
+
"@grafctory/flow": "^0.13.0",
|
|
52
|
+
"@grafctory/http": "^0.6.0",
|
|
53
|
+
"@grafctory/tape": "^0.13.0"
|
|
54
|
+
},
|
|
55
|
+
"devDependencies": {
|
|
56
|
+
"@types/node": "^25.9.3",
|
|
57
|
+
"typescript": "^6.0.3",
|
|
58
|
+
"vitest": "^4.1.12"
|
|
59
|
+
}
|
|
60
|
+
}
|