@metreeca/pipe-url 0.10.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 +42 -0
- package/dist/crawl.d.ts +140 -0
- package/dist/crawl.d.ts.map +1 -0
- package/dist/crawl.js +85 -0
- package/dist/crawl.js.map +1 -0
- package/dist/fetch.d.ts +60 -0
- package/dist/fetch.d.ts.map +1 -0
- package/dist/fetch.js +120 -0
- package/dist/fetch.js.map +1 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +29 -0
- package/dist/index.js.map +1 -0
- package/package.json +61 -0
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
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
# @metreeca/pipe-url
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/@metreeca/pipe-url)
|
|
4
|
+
|
|
5
|
+
URL processing tasks for [@metreeca/pipe](https://github.com/metreeca/pipe).
|
|
6
|
+
|
|
7
|
+
# Installation
|
|
8
|
+
|
|
9
|
+
```shell
|
|
10
|
+
npm install @metreeca/gear # the job executor
|
|
11
|
+
npm install @metreeca/pipe-url # this package
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
> [!IMPORTANT]
|
|
15
|
+
>
|
|
16
|
+
> Node.js 22 or later is required.
|
|
17
|
+
|
|
18
|
+
> [!WARNING]
|
|
19
|
+
>
|
|
20
|
+
> TypeScript consumers must use `"moduleResolution": "nodenext"/"node16"/"bundler"` in `tsconfig.json`.
|
|
21
|
+
> The legacy `"node"` resolver is not supported.
|
|
22
|
+
|
|
23
|
+
# Usage
|
|
24
|
+
|
|
25
|
+
| Task | Description |
|
|
26
|
+
|--------------------|--------------------------------|
|
|
27
|
+
| [`fetch()`][fetch] | Resource fetcher |
|
|
28
|
+
| [`crawl()`][crawl] | URL graph walker and harvester |
|
|
29
|
+
|
|
30
|
+
[fetch]: https://metreeca.github.io/pipe/functions/_metreeca_pipe-url.fetch.html
|
|
31
|
+
|
|
32
|
+
[crawl]: https://metreeca.github.io/pipe/functions/_metreeca_pipe-url.crawl.html
|
|
33
|
+
|
|
34
|
+
# Support
|
|
35
|
+
|
|
36
|
+
- open an [issue](https://github.com/metreeca/pipe/issues) to report a problem or to suggest a new feature
|
|
37
|
+
- start a [discussion](https://github.com/metreeca/pipe/discussions) to ask a how-to question or to share an idea
|
|
38
|
+
|
|
39
|
+
# License
|
|
40
|
+
|
|
41
|
+
This project is licensed under the Apache 2.0 License –
|
|
42
|
+
see [LICENSE](https://github.com/metreeca/pipe?tab=Apache-2.0-1-ov-file) file for details.
|
package/dist/crawl.d.ts
ADDED
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import type { Optional, URLLike } from "@metreeca/core";
|
|
2
|
+
import type { Awaitable, Awaitables } from "@metreeca/core/async";
|
|
3
|
+
import type { Task } from "@metreeca/flow";
|
|
4
|
+
/**
|
|
5
|
+
* Possibly asynchronous, possibly absent value.
|
|
6
|
+
*
|
|
7
|
+
* Absence and asynchrony are taken uniformly, so that a provider hands over whatever it already holds, a value, a
|
|
8
|
+
* promise or nothing at all.
|
|
9
|
+
*
|
|
10
|
+
* @typeParam T The type of the supplied value
|
|
11
|
+
*/
|
|
12
|
+
export type Source<T> = Awaitable<Optional<T>>;
|
|
13
|
+
/**
|
|
14
|
+
* Creates a URL graph walker.
|
|
15
|
+
*
|
|
16
|
+
* The generated task converts a feed of seed URLs into a feed of the URLs reachable from them, so that a consumer
|
|
17
|
+
* works on a whole graph of URLs while stating no more than the step from one URL to the next. URLs are emitted
|
|
18
|
+
* breadth-first in level order, every seed first, then every URL one step away from a seed, and so on, so that the
|
|
19
|
+
* first arrival at a URL is also its shallowest one.
|
|
20
|
+
*
|
|
21
|
+
* The walker navigates a graph of URLs without retrieving the resources they identify. Any retrieval needed to find
|
|
22
|
+
* the links of a URL is up to `walker`, and deriving results from the crawled URLs is up to the tasks downstream; the
|
|
23
|
+
* harvester form takes retrieval and derivation over when a crawl must read each resource anyway. Seeds and links are
|
|
24
|
+
* stated as {@link URLLike} values, strings and {@link !URL URL} objects alike, but reach `walker` and the feed as
|
|
25
|
+
* parsed objects. Each object is a fresh copy, safe to be altered.
|
|
26
|
+
*
|
|
27
|
+
* > [!NOTE]
|
|
28
|
+
* >
|
|
29
|
+
* > - **Incremental**: seeds are emitted as they are pulled, and reachable URLs level by level, so the feed produced
|
|
30
|
+
* > runs dry when the source and `walker` do. No URL reachable from a seed is emitted until the source runs dry,
|
|
31
|
+
* > so the feed never completes on an endless source.
|
|
32
|
+
* > - **Materialising**: every crawled URL is retained for the whole lifetime of the feed, as are the seeds and the
|
|
33
|
+
* > level being crawled, so an unbounded or widely branching graph may exhaust memory.
|
|
34
|
+
* > - **Stateful**: the URLs already crawled decide the ones that follow, so a task invoked per nested feed or per
|
|
35
|
+
* > run crawls each independently, reaching a URL once per invocation rather than once for the feed as a whole.
|
|
36
|
+
*
|
|
37
|
+
* > [!IMPORTANT]
|
|
38
|
+
* >
|
|
39
|
+
* > URLs are crawled at most once across the whole feed, whatever seed they are reached from, so cyclic and
|
|
40
|
+
* > converging graphs are crawled without duplicates and without looping. URLs are matched in canonical form: an
|
|
41
|
+
* > omitted path or an uppercase host doesn't make a URL distinct, while a trailing slash or a fragment does.
|
|
42
|
+
*
|
|
43
|
+
* @param walker The function stating the URLs linked from a URL, none if it is a leaf
|
|
44
|
+
*
|
|
45
|
+
* @returns A task converting a feed of seed URLs into a feed of the seeds and the URLs reachable from them, each as a
|
|
46
|
+
* parsed object
|
|
47
|
+
*
|
|
48
|
+
* @throws {@link !Error Error} While the feed is consumed, whatever the source reports while producing seeds, or
|
|
49
|
+
* whatever `walker` reports while stating the URLs linked from a URL
|
|
50
|
+
*
|
|
51
|
+
* @throws {@link !TypeError TypeError} While the feed is consumed, if a seed or a link cannot be parsed on its
|
|
52
|
+
* own, a relative reference among them
|
|
53
|
+
*
|
|
54
|
+
* @example
|
|
55
|
+
*
|
|
56
|
+
* ```typescript
|
|
57
|
+
* const pages: Record<string, string[]> = { "/a": ["/b", "/c"], "/b": ["/d"], "/c": ["/d"], "/d": [] };
|
|
58
|
+
*
|
|
59
|
+
* await pipe(
|
|
60
|
+
* (items(["https://example.com/a"]))
|
|
61
|
+
* (crawl(url => pages[url.pathname]?.map(path => new URL(path, url))))
|
|
62
|
+
* (toArray())
|
|
63
|
+
* ); // the URLs of /a, /b, /c and /d, in that order
|
|
64
|
+
* ```
|
|
65
|
+
*
|
|
66
|
+
* @group Factories
|
|
67
|
+
*/
|
|
68
|
+
export declare function crawl(walker: (url: URL) => Source<Awaitables<URLLike>>): Task<URLLike, URL>;
|
|
69
|
+
/**
|
|
70
|
+
* Creates a URL graph harvester.
|
|
71
|
+
*
|
|
72
|
+
* The generated task converts a feed of seed URLs into a feed of results derived from the resources the crawled URLs
|
|
73
|
+
* identify, so that a consumer harvests a whole graph of URLs while stating the retrieval of a URL as a task of its
|
|
74
|
+
* own. Each crawled URL is retrieved once, however many links converge on it. The value retrieved for it serves both
|
|
75
|
+
* to find its links and to derive its results, so no resource is read twice. Results are emitted in level order: the
|
|
76
|
+
* results of every seed first, then those of every URL one step away from a seed, and so on.
|
|
77
|
+
*
|
|
78
|
+
* Retrieval is stated as a task over a whole level rather than as a step per URL, so the consumer controls how many
|
|
79
|
+
* URLs are retrieved at a time with the tasks already at hand: a forked `feeder` retrieves several at once, an
|
|
80
|
+
* unforked one retrieves them in turn. A URL is left out of the harvest by emitting nothing for it. Seeds and links
|
|
81
|
+
* are stated as {@link URLLike} values, strings and {@link !URL URL} objects alike, but reach `feeder` as parsed
|
|
82
|
+
* objects. Each object is a fresh copy, safe to be altered.
|
|
83
|
+
*
|
|
84
|
+
* > [!NOTE]
|
|
85
|
+
* >
|
|
86
|
+
* > - **Incremental**: the results of the seeds are emitted as `feeder` draws them, and those of reachable URLs level
|
|
87
|
+
* > by level, so the feed produced runs dry when the source, `feeder`, `walker` and `mapper` do. No URL reachable
|
|
88
|
+
* > from a seed is fed until the source runs dry, so the feed never completes on an endless source.
|
|
89
|
+
* > - **Materialising**: every crawled URL is retained for the whole lifetime of the feed, as for the walker form.
|
|
90
|
+
* > The value retrieved for a URL is released as soon as it is walked and mapped, so it is never retained across
|
|
91
|
+
* > levels.
|
|
92
|
+
* > - **Stateful**: the URLs already crawled decide the ones that follow, as for the walker form.
|
|
93
|
+
*
|
|
94
|
+
* > [!IMPORTANT]
|
|
95
|
+
* >
|
|
96
|
+
* > URLs are crawled at most once across the whole feed, whatever seed they are reached from, and matched in
|
|
97
|
+
* > canonical form, as for the walker form.
|
|
98
|
+
*
|
|
99
|
+
* > [!IMPORTANT]
|
|
100
|
+
* >
|
|
101
|
+
* > `feeder` is invoked once per level, so any state it initialises on invocation lasts for that level only. State
|
|
102
|
+
* > spanning the whole crawl belongs to the enclosing closure. Levels never mix, but order within a level is up to
|
|
103
|
+
* > `feeder`: a feeder retrieving several URLs at a time harvests a level in completion order.
|
|
104
|
+
*
|
|
105
|
+
* @typeParam V The type of the value retrieved for a crawled URL
|
|
106
|
+
* @typeParam R The type of the results derived from a crawled URL
|
|
107
|
+
*
|
|
108
|
+
* @param feeder The task retrieving a value for each URL of a level, emitting nothing for a URL to be crawled no
|
|
109
|
+
* further and to contribute no result
|
|
110
|
+
* @param walker The function stating the URLs linked from the value retrieved for a URL, none if it is a leaf
|
|
111
|
+
* @param mapper The function stating the results derived from the value retrieved for a URL, either a single result
|
|
112
|
+
* or a sequence of them, none if it contributes no result
|
|
113
|
+
*
|
|
114
|
+
* @returns A task converting a feed of seed URLs into a feed of the results derived from every crawled URL
|
|
115
|
+
*
|
|
116
|
+
* @throws {@link !Error Error} While the feed is consumed, whatever the source reports while producing seeds, or
|
|
117
|
+
* whatever `feeder`, `walker` and `mapper` report while retrieving, walking and mapping
|
|
118
|
+
* a URL
|
|
119
|
+
*
|
|
120
|
+
* @throws {@link !TypeError TypeError} While the feed is consumed, if a seed or a link cannot be parsed on its
|
|
121
|
+
* own, a relative reference among them
|
|
122
|
+
*
|
|
123
|
+
* @example
|
|
124
|
+
*
|
|
125
|
+
* ```typescript
|
|
126
|
+
* await pipe(
|
|
127
|
+
* (items(["https://example.com/products/"]))
|
|
128
|
+
* (crawl(
|
|
129
|
+
* fork(4, urls => urls(fetch())(html())(xpath())), // the index pages, four retrievals at a time
|
|
130
|
+
* page => page("//nav//a/@href").map(link), // the index pages it paginates to
|
|
131
|
+
* page => page("//article//a/@href").map(link) // the item links it lists
|
|
132
|
+
* ))
|
|
133
|
+
* (toArray())
|
|
134
|
+
* ); // the item links of every index page
|
|
135
|
+
* ```
|
|
136
|
+
*
|
|
137
|
+
* @group Factories
|
|
138
|
+
*/
|
|
139
|
+
export declare function crawl<V, R>(feeder: Task<URL, V>, walker: (data: V) => Source<Awaitables<URLLike>>, mapper: (data: V) => Source<R | Awaitables<R>>): Task<URLLike, R>;
|
|
140
|
+
//# sourceMappingURL=crawl.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"crawl.d.ts","sourceRoot":"","sources":["../src/crawl.ts"],"names":[],"mappings":"AAgBA,OAAO,KAAK,EAAE,QAAQ,EAAE,OAAO,EAAE,MAAM,gBAAgB,CAAC;AACxD,OAAO,KAAK,EAAE,SAAS,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAC;AAClE,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,gBAAgB,CAAC;AAI3C;;;;;;;GAOG;AACH,MAAM,MAAM,MAAM,CAAC,CAAC,IACnB,SAAS,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC;AAMxB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAsDG;AACH,wBAAgB,KAAK,CACpB,MAAM,EAAE,CAAC,GAAG,EAAE,GAAG,KAAK,MAAM,CAAC,UAAU,CAAC,OAAO,CAAC,CAAC,GAC/C,IAAI,CAAC,OAAO,EAAE,GAAG,CAAC,CAAC;AAEtB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqEG;AACH,wBAAgB,KAAK,CAAC,CAAC,EAAE,CAAC,EACzB,MAAM,EAAE,IAAI,CAAC,GAAG,EAAE,CAAC,CAAC,EACpB,MAAM,EAAE,CAAC,IAAI,EAAE,CAAC,KAAK,MAAM,CAAC,UAAU,CAAC,OAAO,CAAC,CAAC,EAChD,MAAM,EAAE,CAAC,IAAI,EAAE,CAAC,KAAK,MAAM,CAAC,CAAC,GAAG,UAAU,CAAC,CAAC,CAAC,CAAC,GAC5C,IAAI,CAAC,OAAO,EAAE,CAAC,CAAC,CAAC"}
|
package/dist/crawl.js
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
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 "@metreeca/flow/feeds";
|
|
17
|
+
/**
|
|
18
|
+
* Creates a URL graph walker or harvester.
|
|
19
|
+
*/
|
|
20
|
+
export function crawl(...steps) {
|
|
21
|
+
return steps.length === 1
|
|
22
|
+
? roam(steps[0])
|
|
23
|
+
: reap(steps[0], steps[1], steps[2]);
|
|
24
|
+
function roam(walker) {
|
|
25
|
+
return source => items((async function* () {
|
|
26
|
+
const admitted = admitting();
|
|
27
|
+
// the seed level, drained before descending so that the first arrival at a URL is its shallowest;
|
|
28
|
+
// seeds are emitted as they are pulled, so a slow source doesn't withhold the ones already in
|
|
29
|
+
const seeds = [];
|
|
30
|
+
for await (const url of admitted(source)) {
|
|
31
|
+
seeds.push(url);
|
|
32
|
+
yield url;
|
|
33
|
+
}
|
|
34
|
+
yield* descending(seeds, reach);
|
|
35
|
+
async function* reach(frontier, reached) {
|
|
36
|
+
for (const url of frontier) {
|
|
37
|
+
for await (const next of admitted(await walker(url))) {
|
|
38
|
+
reached.push(next);
|
|
39
|
+
yield next;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
})());
|
|
44
|
+
}
|
|
45
|
+
function reap(feeder, walker, mapper) {
|
|
46
|
+
return source => items((async function* () {
|
|
47
|
+
const admitted = admitting();
|
|
48
|
+
// the URLs linked from the seed level, buffered until the source runs dry so that the first arrival at a
|
|
49
|
+
// URL is its shallowest; seeds are fed as `feeder` draws them, so a slow source doesn't withhold the
|
|
50
|
+
// ones already in
|
|
51
|
+
const linked = [];
|
|
52
|
+
yield* reach(admitted(source), linked);
|
|
53
|
+
yield* descending(linked, reach);
|
|
54
|
+
async function* reach(frontier, reached) {
|
|
55
|
+
for await (const data of feeder(items(frontier))) {
|
|
56
|
+
yield* items(await mapper(data) ?? []);
|
|
57
|
+
for await (const next of admitted(await walker(data))) {
|
|
58
|
+
reached.push(next);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
})());
|
|
63
|
+
}
|
|
64
|
+
function admitting() {
|
|
65
|
+
const crawled = new Set();
|
|
66
|
+
return async function* (links) {
|
|
67
|
+
for await (const link of items(links ?? [])) {
|
|
68
|
+
const url = new URL(link);
|
|
69
|
+
if (!crawled.has(url.href)) {
|
|
70
|
+
crawled.add(url.href);
|
|
71
|
+
yield url;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
async function* descending(seeds, reach) {
|
|
77
|
+
let frontier = seeds;
|
|
78
|
+
while (frontier.length > 0) {
|
|
79
|
+
const reached = [];
|
|
80
|
+
yield* reach(frontier, reached);
|
|
81
|
+
frontier = reached;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
//# sourceMappingURL=crawl.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"crawl.js","sourceRoot":"","sources":["../src/crawl.ts"],"names":[],"mappings":"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"}
|
package/dist/fetch.d.ts
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import type { Task } from "@metreeca/flow";
|
|
2
|
+
import { type Middleware } from "@metreeca/http";
|
|
3
|
+
/**
|
|
4
|
+
* Creates a resource fetcher.
|
|
5
|
+
*
|
|
6
|
+
* The generated task converts a feed of requests into a feed of
|
|
7
|
+
* {@link https://developer.mozilla.org/docs/Web/API/Response Response} objects, so that a consumer draws on remote and
|
|
8
|
+
* local resources through the client the execution supplies rather than through one of its own.
|
|
9
|
+
*
|
|
10
|
+
* A request is given as the standard `fetch()` function accepts it, that is as a URL string, a
|
|
11
|
+
* {@link https://developer.mozilla.org/docs/Web/API/URL URL} object or a
|
|
12
|
+
* {@link https://developer.mozilla.org/docs/Web/API/Request Request} object. A response is emitted as soon as its head
|
|
13
|
+
* is received, with the body left unread for the consumer to draw or discard; a response carrying no body is emitted
|
|
14
|
+
* all the same.
|
|
15
|
+
*
|
|
16
|
+
* Exchanges are routed through the fetch client of the {@link @metreeca/gear!index.executor execution} enclosing the
|
|
17
|
+
* call to this factory, so the transport is chosen by the job rather than by the task. Binding {@link createFetch}
|
|
18
|
+
* routes every exchange through a throttled, cached or stubbed client without altering the task. An execution with no
|
|
19
|
+
* binding falls back on the standard `fetch` function.
|
|
20
|
+
*
|
|
21
|
+
* Exchanges are sent with desktop browser `User-Agent` and `Accept` fields. Sites that serve unattended clients
|
|
22
|
+
* differently, or refuse them altogether, are thus scraped as a browser would be. A request that already states one of
|
|
23
|
+
* these fields keeps its own value, as do the values stated by `middlewares`.
|
|
24
|
+
*
|
|
25
|
+
* Content coding is negotiated by the fetch client, which decodes response bodies before they reach the consumer. The
|
|
26
|
+
* task states no `Accept-Encoding` field of its own.
|
|
27
|
+
*
|
|
28
|
+
* Every exchange is logged as it is performed, so a run leaves a trace of the resources it drew on and of the ones it
|
|
29
|
+
* was denied. Requests are logged as stated, before `middlewares` can alter them.
|
|
30
|
+
*
|
|
31
|
+
* > [!NOTE]
|
|
32
|
+
* >
|
|
33
|
+
* > - **Incremental**: each response is emitted as soon as its head is received, so the feed produced runs dry when
|
|
34
|
+
* > the source does, and an endless source is read for as long as the feed is consumed.
|
|
35
|
+
* > - **Streaming**: responses are drawn one at a time and handed over with the body unread, none retained, so
|
|
36
|
+
* > resources of any size are handled without holding them in memory, as long as each body is read or cancelled
|
|
37
|
+
* > before the next response is drawn.
|
|
38
|
+
* > - **Stateless**: the task keeps no state across requests, so the outcome is unaffected by how the feed is split
|
|
39
|
+
* > across nested feeds or runs; any state carried across exchanges belongs to `middlewares` and the fetch client.
|
|
40
|
+
*
|
|
41
|
+
* > [!WARNING]
|
|
42
|
+
* >
|
|
43
|
+
* > A request stating a URL that is not absolute, or malformed in any other way, is dropped before it is sent, as is
|
|
44
|
+
* > a response stating an unsuccessful status; both are reported to the log, leaving the feed to run to completion.
|
|
45
|
+
*
|
|
46
|
+
* @param middlewares The middlewares to be layered over the fetch client, in request processing order
|
|
47
|
+
*
|
|
48
|
+
* @returns A task converting a feed of requests into a feed of responses
|
|
49
|
+
*
|
|
50
|
+
* @throws {@link !Error Error} If no execution is running, as the fetch client is resolved when the task is created
|
|
51
|
+
*
|
|
52
|
+
* @throws {@link !Error Error} While the feed is consumed, whatever the source reports while producing requests, or
|
|
53
|
+
* whatever the exchange reports while connecting to a resource or receiving its response
|
|
54
|
+
*
|
|
55
|
+
* @see {@link https://developer.mozilla.org/docs/Web/API/Window/fetch `fetch()`}
|
|
56
|
+
*
|
|
57
|
+
* @group Factories
|
|
58
|
+
*/
|
|
59
|
+
export declare function fetch(...middlewares: readonly Middleware[]): Task<string | URL | Request, Response>;
|
|
60
|
+
//# sourceMappingURL=fetch.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fetch.d.ts","sourceRoot":"","sources":["../src/fetch.ts"],"names":[],"mappings":"AAgBA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,gBAAgB,CAAC;AAG3C,OAAO,EAAe,KAAK,UAAU,EAAE,MAAM,gBAAgB,CAAC;AAyC9D;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuDG;AACH,wBAAgB,KAAK,CAAC,GAAG,WAAW,EAAE,SAAS,UAAU,EAAE,GAAG,IAAI,CAAC,MAAM,GAAG,GAAG,GAAG,OAAO,EAAE,QAAQ,CAAC,CA4BnG"}
|
package/dist/fetch.js
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
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 "@metreeca/flow/feeds";
|
|
17
|
+
import { service } from "@metreeca/gear";
|
|
18
|
+
import { createFetch } from "@metreeca/http";
|
|
19
|
+
import { headers } from "@metreeca/http/headers";
|
|
20
|
+
import { monitor } from "@metreeca/http/monitor";
|
|
21
|
+
import { transport } from "@metreeca/http/transport";
|
|
22
|
+
import { log } from "@metreeca/tape";
|
|
23
|
+
/**
|
|
24
|
+
* The `User-Agent` field stated on every exchange.
|
|
25
|
+
*
|
|
26
|
+
* States a current desktop Chrome, so that sites serving unattended clients differently, or refusing them altogether,
|
|
27
|
+
* are scraped as a browser would be. The platform token and the `AppleWebKit` and `Safari` tokens are frozen by the
|
|
28
|
+
* reduced user agent Chrome sends, leaving the Chrome version as the only one to be kept in step.
|
|
29
|
+
*
|
|
30
|
+
* @see {@link https://www.rfc-editor.org/rfc/rfc9110#section-10.1.5 RFC 9110 § 10.1.5 - User-Agent}
|
|
31
|
+
* @see {@link https://developer.chrome.com/docs/privacy-security/user-agent-client-hints User-Agent Client Hints}
|
|
32
|
+
*/
|
|
33
|
+
const UserAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7)"
|
|
34
|
+
+ " AppleWebKit/537.36 (KHTML, like Gecko)"
|
|
35
|
+
+ " Chrome/151.0.0.0"
|
|
36
|
+
+ " Safari/537.36";
|
|
37
|
+
/**
|
|
38
|
+
* The `Accept` field stated on every exchange.
|
|
39
|
+
*
|
|
40
|
+
* Ranks media types as Chrome ranks them, down to the `q=0.8` weight carried by the catch-all entry, so that a site
|
|
41
|
+
* reading the field against the browser claimed by {@link UserAgent} is not handed a mismatched pair to fingerprint.
|
|
42
|
+
*
|
|
43
|
+
* @see {@link https://www.rfc-editor.org/rfc/rfc9110#section-12.5.1 RFC 9110 § 12.5.1 - Accept}
|
|
44
|
+
*/
|
|
45
|
+
const Accept = "text/html,"
|
|
46
|
+
+ "application/xhtml+xml,"
|
|
47
|
+
+ "application/xml;q=0.9,"
|
|
48
|
+
+ "*/*;q=0.8";
|
|
49
|
+
const logger = log(import.meta.url);
|
|
50
|
+
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
51
|
+
/**
|
|
52
|
+
* Creates a resource fetcher.
|
|
53
|
+
*
|
|
54
|
+
* The generated task converts a feed of requests into a feed of
|
|
55
|
+
* {@link https://developer.mozilla.org/docs/Web/API/Response Response} objects, so that a consumer draws on remote and
|
|
56
|
+
* local resources through the client the execution supplies rather than through one of its own.
|
|
57
|
+
*
|
|
58
|
+
* A request is given as the standard `fetch()` function accepts it, that is as a URL string, a
|
|
59
|
+
* {@link https://developer.mozilla.org/docs/Web/API/URL URL} object or a
|
|
60
|
+
* {@link https://developer.mozilla.org/docs/Web/API/Request Request} object. A response is emitted as soon as its head
|
|
61
|
+
* is received, with the body left unread for the consumer to draw or discard; a response carrying no body is emitted
|
|
62
|
+
* all the same.
|
|
63
|
+
*
|
|
64
|
+
* Exchanges are routed through the fetch client of the {@link @metreeca/gear!index.executor execution} enclosing the
|
|
65
|
+
* call to this factory, so the transport is chosen by the job rather than by the task. Binding {@link createFetch}
|
|
66
|
+
* routes every exchange through a throttled, cached or stubbed client without altering the task. An execution with no
|
|
67
|
+
* binding falls back on the standard `fetch` function.
|
|
68
|
+
*
|
|
69
|
+
* Exchanges are sent with desktop browser `User-Agent` and `Accept` fields. Sites that serve unattended clients
|
|
70
|
+
* differently, or refuse them altogether, are thus scraped as a browser would be. A request that already states one of
|
|
71
|
+
* these fields keeps its own value, as do the values stated by `middlewares`.
|
|
72
|
+
*
|
|
73
|
+
* Content coding is negotiated by the fetch client, which decodes response bodies before they reach the consumer. The
|
|
74
|
+
* task states no `Accept-Encoding` field of its own.
|
|
75
|
+
*
|
|
76
|
+
* Every exchange is logged as it is performed, so a run leaves a trace of the resources it drew on and of the ones it
|
|
77
|
+
* was denied. Requests are logged as stated, before `middlewares` can alter them.
|
|
78
|
+
*
|
|
79
|
+
* > [!NOTE]
|
|
80
|
+
* >
|
|
81
|
+
* > - **Incremental**: each response is emitted as soon as its head is received, so the feed produced runs dry when
|
|
82
|
+
* > the source does, and an endless source is read for as long as the feed is consumed.
|
|
83
|
+
* > - **Streaming**: responses are drawn one at a time and handed over with the body unread, none retained, so
|
|
84
|
+
* > resources of any size are handled without holding them in memory, as long as each body is read or cancelled
|
|
85
|
+
* > before the next response is drawn.
|
|
86
|
+
* > - **Stateless**: the task keeps no state across requests, so the outcome is unaffected by how the feed is split
|
|
87
|
+
* > across nested feeds or runs; any state carried across exchanges belongs to `middlewares` and the fetch client.
|
|
88
|
+
*
|
|
89
|
+
* > [!WARNING]
|
|
90
|
+
* >
|
|
91
|
+
* > A request stating a URL that is not absolute, or malformed in any other way, is dropped before it is sent, as is
|
|
92
|
+
* > a response stating an unsuccessful status; both are reported to the log, leaving the feed to run to completion.
|
|
93
|
+
*
|
|
94
|
+
* @param middlewares The middlewares to be layered over the fetch client, in request processing order
|
|
95
|
+
*
|
|
96
|
+
* @returns A task converting a feed of requests into a feed of responses
|
|
97
|
+
*
|
|
98
|
+
* @throws {@link !Error Error} If no execution is running, as the fetch client is resolved when the task is created
|
|
99
|
+
*
|
|
100
|
+
* @throws {@link !Error Error} While the feed is consumed, whatever the source reports while producing requests, or
|
|
101
|
+
* whatever the exchange reports while connecting to a resource or receiving its response
|
|
102
|
+
*
|
|
103
|
+
* @see {@link https://developer.mozilla.org/docs/Web/API/Window/fetch `fetch()`}
|
|
104
|
+
*
|
|
105
|
+
* @group Factories
|
|
106
|
+
*/
|
|
107
|
+
export function fetch(...middlewares) {
|
|
108
|
+
const fetch = service(createFetch);
|
|
109
|
+
return requests => items((async function* () {
|
|
110
|
+
const send = createFetch(monitor(logger), ...middlewares, headers({
|
|
111
|
+
"Accept": Accept,
|
|
112
|
+
"User-Agent": UserAgent
|
|
113
|
+
}), transport(fetch));
|
|
114
|
+
for await (const request of requests) {
|
|
115
|
+
const response = await send(request);
|
|
116
|
+
yield* response.ok ? [response] : []; // unsuccessful responses are reported by the monitor and skipped
|
|
117
|
+
}
|
|
118
|
+
})());
|
|
119
|
+
}
|
|
120
|
+
//# sourceMappingURL=fetch.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fetch.js","sourceRoot":"","sources":["../src/fetch.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAGH,OAAO,EAAE,KAAK,EAAE,MAAM,sBAAsB,CAAC;AAC7C,OAAO,EAAE,OAAO,EAAE,MAAM,gBAAgB,CAAC;AACzC,OAAO,EAAE,WAAW,EAAmB,MAAM,gBAAgB,CAAC;AAC9D,OAAO,EAAE,OAAO,EAAE,MAAM,wBAAwB,CAAC;AACjD,OAAO,EAAE,OAAO,EAAE,MAAM,wBAAwB,CAAC;AACjD,OAAO,EAAE,SAAS,EAAE,MAAM,0BAA0B,CAAC;AACrD,OAAO,EAAE,GAAG,EAAE,MAAM,gBAAgB,CAAC;AAGrC;;;;;;;;;GASG;AACH,MAAM,SAAS,GAAG,iDAAiD;MACjE,yCAAyC;MACzC,mBAAmB;MACnB,gBAAgB,CAAC;AAEnB;;;;;;;GAOG;AACH,MAAM,MAAM,GAAG,YAAY;MACzB,wBAAwB;MACxB,wBAAwB;MACxB,WAAW,CAAC;AAGd,MAAM,MAAM,GAAG,GAAG,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;AAGpC,wHAAwH;AAExH;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuDG;AACH,MAAM,UAAU,KAAK,CAAC,GAAG,WAAkC;IAE1D,MAAM,KAAK,GAAG,OAAO,CAAC,WAAW,CAAC,CAAC;IAEnC,OAAO,QAAQ,CAAC,EAAE,CAAC,KAAK,CAAC,CAAC,KAAK,SAAS,CAAC;QAExC,MAAM,IAAI,GAAG,WAAW,CACvB,OAAO,CAAC,MAAM,CAAC,EACf,GAAG,WAAW,EACd,OAAO,CAAC;YAEP,QAAQ,EAAE,MAAM;YAChB,YAAY,EAAE,SAAS;SAEvB,CAAC,EACF,SAAS,CAAC,KAAK,CAAC,CAChB,CAAC;QAEF,IAAI,KAAK,EAAE,MAAM,OAAO,IAAI,QAAQ,EAAE,CAAC;YAEtC,MAAM,QAAQ,GAAG,MAAM,IAAI,CAAC,OAAO,CAAC,CAAC;YAErC,KAAK,CAAC,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,iEAAiE;QAExG,CAAC;IAEF,CAAC,CAAC,EAAE,CAAC,CAAC;AAEP,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* URL processing tasks.
|
|
3
|
+
*
|
|
4
|
+
* Retrieves the resources that URLs identify and crawls the graphs their links form, so a job can draw on remote and
|
|
5
|
+
* local content as a feed. Exchanges run through the fetch client the enclosing execution supplies, so a job can swap
|
|
6
|
+
* in a throttled, cached or stubbed client without altering its tasks.
|
|
7
|
+
*
|
|
8
|
+
* @module index
|
|
9
|
+
*
|
|
10
|
+
* @see {@link https://www.rfc-editor.org/rfc/rfc3986 RFC 3986 Uniform Resource Identifier (URI): Generic Syntax}
|
|
11
|
+
*/
|
|
12
|
+
export * from "./fetch.js";
|
|
13
|
+
export * from "./crawl.js";
|
|
14
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAgBA;;;;;;;;;;GAUG;AAEH,cAAc,YAAY,CAAC;AAC3B,cAAc,YAAY,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
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
|
+
* URL processing tasks.
|
|
18
|
+
*
|
|
19
|
+
* Retrieves the resources that URLs identify and crawls the graphs their links form, so a job can draw on remote and
|
|
20
|
+
* local content as a feed. Exchanges run through the fetch client the enclosing execution supplies, so a job can swap
|
|
21
|
+
* in a throttled, cached or stubbed client without altering its tasks.
|
|
22
|
+
*
|
|
23
|
+
* @module index
|
|
24
|
+
*
|
|
25
|
+
* @see {@link https://www.rfc-editor.org/rfc/rfc3986 RFC 3986 Uniform Resource Identifier (URI): Generic Syntax}
|
|
26
|
+
*/
|
|
27
|
+
export * from "./fetch.js";
|
|
28
|
+
export * from "./crawl.js";
|
|
29
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH;;;;;;;;;;GAUG;AAEH,cAAc,YAAY,CAAC;AAC3B,cAAc,YAAY,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@metreeca/pipe-url",
|
|
3
|
+
"version": "0.10.0",
|
|
4
|
+
"description": "URL processing tasks for @metreeca/pipe.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"typescript",
|
|
7
|
+
"pipeline",
|
|
8
|
+
"etl",
|
|
9
|
+
"data-integration",
|
|
10
|
+
"url"
|
|
11
|
+
],
|
|
12
|
+
"homepage": "https://metreeca.github.io/pipe/",
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/metreeca/pipe/issues",
|
|
15
|
+
"email": "info@metreeca.com"
|
|
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/metreeca/pipe.git",
|
|
26
|
+
"directory": "packages/pipe-url"
|
|
27
|
+
},
|
|
28
|
+
"type": "module",
|
|
29
|
+
"sideEffects": false,
|
|
30
|
+
"exports": {
|
|
31
|
+
".": {
|
|
32
|
+
"@metreeca/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": "rm -rf dist docs node_modules",
|
|
46
|
+
"build": "rm -rf dist && tsc -p tsconfig.build.json",
|
|
47
|
+
"check": "npx vitest --typecheck run"
|
|
48
|
+
},
|
|
49
|
+
"dependencies": {
|
|
50
|
+
"@metreeca/core": "^0.12.0",
|
|
51
|
+
"@metreeca/flow": "^0.11.0",
|
|
52
|
+
"@metreeca/gear": "^0.3.0",
|
|
53
|
+
"@metreeca/http": "^0.4.0",
|
|
54
|
+
"@metreeca/tape": "^0.11.0"
|
|
55
|
+
},
|
|
56
|
+
"devDependencies": {
|
|
57
|
+
"@types/node": "^25.9.3",
|
|
58
|
+
"typescript": "^5.9.3",
|
|
59
|
+
"vitest": "^4.0.13"
|
|
60
|
+
}
|
|
61
|
+
}
|