@haatlabs-core/provider-amazon 0.0.0-stage → 0.11.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 +63 -2
- package/dist/amazon-source.d.ts +37 -0
- package/dist/amazon-source.js +240 -0
- package/dist/amazon-source.js.map +1 -0
- package/dist/cjs/amazon-source.d.ts +37 -0
- package/dist/cjs/amazon-source.js +249 -0
- package/dist/cjs/errors.d.ts +28 -0
- package/dist/cjs/errors.js +44 -0
- package/dist/cjs/fixtures/autocomplete.json +47 -0
- package/dist/cjs/fixtures/index.d.ts +6 -0
- package/dist/cjs/fixtures/index.js +44 -0
- package/dist/cjs/fixtures/product.json +702 -0
- package/dist/cjs/fixtures/search.json +1864 -0
- package/dist/cjs/haatlabs.d.ts +15 -0
- package/dist/cjs/haatlabs.js +136 -0
- package/dist/cjs/index.d.ts +8 -0
- package/dist/cjs/index.js +46 -0
- package/dist/cjs/links.d.ts +5 -0
- package/dist/cjs/links.js +51 -0
- package/dist/cjs/map.d.ts +5 -0
- package/dist/cjs/map.js +154 -0
- package/dist/cjs/markets.d.ts +11 -0
- package/dist/cjs/markets.js +66 -0
- package/dist/cjs/package.json +3 -0
- package/dist/cjs/raw.d.ts +86 -0
- package/dist/cjs/raw.js +90 -0
- package/dist/cjs/schemas.d.ts +286 -0
- package/dist/cjs/schemas.js +213 -0
- package/dist/cjs/source.d.ts +36 -0
- package/dist/cjs/source.js +15 -0
- package/dist/cjs/testing.d.ts +27 -0
- package/dist/cjs/testing.js +122 -0
- package/dist/cjs/text.d.ts +4 -0
- package/dist/cjs/text.js +62 -0
- package/dist/errors.d.ts +28 -0
- package/dist/errors.js +16 -0
- package/dist/errors.js.map +1 -0
- package/dist/fixtures/autocomplete.json +47 -0
- package/dist/fixtures/index.d.ts +6 -0
- package/dist/fixtures/index.js +10 -0
- package/dist/fixtures/index.js.map +1 -0
- package/dist/fixtures/product.json +702 -0
- package/dist/fixtures/search.json +1864 -0
- package/dist/haatlabs.d.ts +15 -0
- package/dist/haatlabs.js +118 -0
- package/dist/haatlabs.js.map +1 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +8 -0
- package/dist/index.js.map +1 -0
- package/dist/links.d.ts +5 -0
- package/dist/links.js +27 -0
- package/dist/links.js.map +1 -0
- package/dist/map.d.ts +5 -0
- package/dist/map.js +164 -0
- package/dist/map.js.map +1 -0
- package/dist/markets.d.ts +11 -0
- package/dist/markets.js +40 -0
- package/dist/markets.js.map +1 -0
- package/dist/raw.d.ts +86 -0
- package/dist/raw.js +69 -0
- package/dist/raw.js.map +1 -0
- package/dist/schemas.d.ts +286 -0
- package/dist/schemas.js +181 -0
- package/dist/schemas.js.map +1 -0
- package/dist/source.d.ts +36 -0
- package/dist/source.js +2 -0
- package/dist/source.js.map +1 -0
- package/dist/testing.d.ts +27 -0
- package/dist/testing.js +106 -0
- package/dist/testing.js.map +1 -0
- package/dist/text.d.ts +4 -0
- package/dist/text.js +39 -0
- package/dist/text.js.map +1 -0
- package/package.json +79 -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 2026 Haatlabs
|
|
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,64 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @haatlabs-core/provider-amazon
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
## Install
|
|
4
|
+
|
|
5
|
+
pnpm add @haatlabs-core/provider-amazon
|
|
6
|
+
|
|
7
|
+
It leaves one package to your app, so the app holds one copy: `zod`.
|
|
8
|
+
|
|
9
|
+
## What it does
|
|
10
|
+
|
|
11
|
+
Amazon search, products and suggestions through an Amazon scraper, called directly or through a
|
|
12
|
+
services gateway: `createAmazonSource(options)` gives a `ProductSource` (`search`,
|
|
13
|
+
`product`, `autocomplete`, `parseLink`, `health`). It caches nothing: wrap a call in
|
|
14
|
+
`cachedCall` (@haatlabs-core/providers) for `AMAZON_CACHE_SECONDS` (search and product an hour,
|
|
15
|
+
suggestions a day). `AMAZON_IMAGE_HOSTS` are the hosts a content security policy names.
|
|
16
|
+
|
|
17
|
+
## Options
|
|
18
|
+
|
|
19
|
+
`baseUrl` (the scraper, the gateway's `/amazon`, or the hosted API), `timeoutMs`, `apiKey`
|
|
20
|
+
(the hosted API's) or `gatewayKey` (the gateway's; setting both is a `RangeError`),
|
|
21
|
+
`proxyEnabled` (Japan and Australia need a proxy that exits there), `fetch`,
|
|
22
|
+
`maxConcurrent` (12 calls in flight), `maxWaitMs` (10 s for a place before the call is
|
|
23
|
+
refused) and `maxResponseBytes` (2 MiB).
|
|
24
|
+
|
|
25
|
+
## Errors
|
|
26
|
+
|
|
27
|
+
`SOURCE_BAD_REQUEST` (400), `SOURCE_PRODUCT_NOT_FOUND` (404, for a product only),
|
|
28
|
+
`SOURCE_UNAVAILABLE` (503, transient: away, slow, busy, or no place within `maxWaitMs`) and
|
|
29
|
+
`SOURCE_ERROR` (503: an answer that cannot be used). A project spreads
|
|
30
|
+
`PROVIDER_AMAZON_ERRORS` into `defineErrors`. A 429 or 5xx carries `retryAfterMs` from its
|
|
31
|
+
Retry-After (seconds or a date, at most five minutes). Called directly, a refusal's body is
|
|
32
|
+
never read: the connection is let go.
|
|
33
|
+
|
|
34
|
+
## Gateway and deadlines
|
|
35
|
+
|
|
36
|
+
Each call takes a `CallContext` (tier, priority, `deadlineMs`); with `gatewayKey` it sends
|
|
37
|
+
the gateway's headers, tier 4 in the background when there is none. The call's time limit is
|
|
38
|
+
`timeoutMs`, or `deadlineMs` when that is sooner (`localTimeoutMs`), and it starts before the
|
|
39
|
+
wait for one of the `maxConcurrent` places: the wait counts, and a call whose time ran out
|
|
40
|
+
while it waited is never made. The gateway is given what is left of that limit, never longer.
|
|
41
|
+
The deadline is each call's: a caller's retry starts it again, so a caller that retries
|
|
42
|
+
gives each try what is left of its own budget. Through the gateway a refusal carries an `UpstreamStatusError` as its
|
|
43
|
+
cause: `isGatewayRefusal(error)` is true for the gateway's 401 and 403 (both
|
|
44
|
+
`SOURCE_UNAVAILABLE`), and only the gateway's own `bad_request` 400 is a contract error.
|
|
45
|
+
|
|
46
|
+
## Fixtures
|
|
47
|
+
|
|
48
|
+
`@haatlabs-core/provider-amazon/fixtures`: `amazonFixtures.product`, `.search` and
|
|
49
|
+
`.autocomplete`, answers recorded from the real scraper.
|
|
50
|
+
|
|
51
|
+
## Testing
|
|
52
|
+
|
|
53
|
+
`@haatlabs-core/provider-amazon/testing`: `createFakeSource(options)`, a second `ProductSource`
|
|
54
|
+
with one market, for tests that must not assume Amazon. The package's own tests are unit
|
|
55
|
+
tests on the fixtures: `pnpm --filter @haatlabs-core/provider-amazon test`.
|
|
56
|
+
|
|
57
|
+
## Hosted services
|
|
58
|
+
|
|
59
|
+
Without a scraper of your own, read Amazon through the Haatlabs API on RapidAPI: set
|
|
60
|
+
`haatlabsApiKey` (your RapidAPI key) and `baseUrl: 'https://haatlabs-api.p.rapidapi.com'`
|
|
61
|
+
(`apiKey` and `gatewayKey` stay null). Every marketplace is available; there are no
|
|
62
|
+
suggestions (`autocomplete` is absent); a refusal keeps its message, and a used-up quota or a
|
|
63
|
+
refused key is `SOURCE_UNAVAILABLE` that is not transient. See `@haatlabs-core/services` and
|
|
64
|
+
https://haatlabs.shop/docs/services/
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type { ProductSource } from './source.js';
|
|
2
|
+
/** Real products are under 100 KB, so 2 MiB is plenty. */
|
|
3
|
+
export declare const DEFAULT_AMAZON_MAX_RESPONSE_BYTES: number;
|
|
4
|
+
/** How long each kind of answer may be kept, in seconds. */
|
|
5
|
+
export declare const AMAZON_CACHE_SECONDS: {
|
|
6
|
+
readonly search: 3600;
|
|
7
|
+
readonly product: 3600;
|
|
8
|
+
readonly autocomplete: 86400;
|
|
9
|
+
};
|
|
10
|
+
/** Where Amazon serves product images from: the dashboards' content security policy names each. */
|
|
11
|
+
export declare const AMAZON_IMAGE_HOSTS: readonly ["https://m.media-amazon.com", "https://images-na.ssl-images-amazon.com", "https://images-eu.ssl-images-amazon.com", "https://images-fe.ssl-images-amazon.com"];
|
|
12
|
+
export interface AmazonSourceOptions {
|
|
13
|
+
/** The scraper container, or the author's hosted API (which needs `apiKey`). */
|
|
14
|
+
baseUrl: string;
|
|
15
|
+
timeoutMs: number;
|
|
16
|
+
/** The hosted API's key. Mutually exclusive with `gatewayKey`: setting both is refused. */
|
|
17
|
+
apiKey: string | null;
|
|
18
|
+
/**
|
|
19
|
+
* haatlabs-shared's gateway key; null reaches a scraper (or the hosted API) directly.
|
|
20
|
+
* Mutually exclusive with `apiKey`.
|
|
21
|
+
*/
|
|
22
|
+
gatewayKey: string | null;
|
|
23
|
+
/**
|
|
24
|
+
* Your RapidAPI key for the Haatlabs API (`@haatlabs-core/services`), with `baseUrl` its address
|
|
25
|
+
* (`https://haatlabs-api.p.rapidapi.com`). Mutually exclusive with `apiKey` and `gatewayKey`.
|
|
26
|
+
*/
|
|
27
|
+
haatlabsApiKey?: string | null;
|
|
28
|
+
/** True when the scraper has a proxy that exits in Japan and Australia. */
|
|
29
|
+
proxyEnabled: boolean;
|
|
30
|
+
fetch?: typeof fetch;
|
|
31
|
+
maxConcurrent?: number;
|
|
32
|
+
/** How long a call may wait for one of the `maxConcurrent` places before it is refused. */
|
|
33
|
+
maxWaitMs?: number;
|
|
34
|
+
/** The largest answer read; default 2 MiB. */
|
|
35
|
+
maxResponseBytes?: number;
|
|
36
|
+
}
|
|
37
|
+
export declare function createAmazonSource(options: AmazonSourceOptions): ProductSource;
|
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
import { createConcurrencyLimit, GATEWAY_HEADER, gatewayAuthorization, gatewayErrorOf, gatewayHeadersFor, localTimeoutMs, readAtMost, retryAfterMsOf, UpstreamStatusError, sharedCall, } from '@haatlabs-core/providers';
|
|
2
|
+
import { AmazonErrorCode, createAmazonError } from './errors.js';
|
|
3
|
+
import { ASIN, parseAmazonLink } from './links.js';
|
|
4
|
+
import { mapProduct, mapSearch } from './map.js';
|
|
5
|
+
import { AMAZON_MARKETS, marketByCode } from './markets.js';
|
|
6
|
+
import { RawAutocompleteSchema, RawProductSchema, RawSearchSchema } from './raw.js';
|
|
7
|
+
import { SearchFilter, SortOrder } from './schemas.js';
|
|
8
|
+
import { createHaatlabsAmazonSource } from './haatlabs.js';
|
|
9
|
+
import { cleanText } from './text.js';
|
|
10
|
+
/** Real products are under 100 KB, so 2 MiB is plenty. */
|
|
11
|
+
export const DEFAULT_AMAZON_MAX_RESPONSE_BYTES = 2 * 1024 * 1024;
|
|
12
|
+
const HEALTH_TIMEOUT_MS = 5000;
|
|
13
|
+
const DEFAULT_MAX_CONCURRENT = 12;
|
|
14
|
+
// A burst queues briefly; past this the scraper is overwhelmed, and the person is told so.
|
|
15
|
+
const DEFAULT_MAX_WAIT_MS = 10_000;
|
|
16
|
+
const MAX_SUGGESTIONS = 10;
|
|
17
|
+
/** How long each kind of answer may be kept, in seconds. */
|
|
18
|
+
export const AMAZON_CACHE_SECONDS = { search: 3600, product: 3600, autocomplete: 86_400 };
|
|
19
|
+
/** Where Amazon serves product images from: the dashboards' content security policy names each. */
|
|
20
|
+
export const AMAZON_IMAGE_HOSTS = [
|
|
21
|
+
'https://m.media-amazon.com',
|
|
22
|
+
'https://images-na.ssl-images-amazon.com',
|
|
23
|
+
'https://images-eu.ssl-images-amazon.com',
|
|
24
|
+
'https://images-fe.ssl-images-amazon.com',
|
|
25
|
+
];
|
|
26
|
+
const PRODUCT_PATH = '/products/details';
|
|
27
|
+
const NOT_RESPONDING = 'Amazon is not responding. Try again shortly.';
|
|
28
|
+
/** Settles as `work` does, or rejects with the signal's reason once it aborts first. */
|
|
29
|
+
async function untilAborted(work, signal) {
|
|
30
|
+
// `work` still settles after an abort; nothing is left waiting on it.
|
|
31
|
+
work.catch(() => undefined);
|
|
32
|
+
let onAbort = () => undefined;
|
|
33
|
+
const aborted = new Promise((_resolve, reject) => {
|
|
34
|
+
onAbort = () => reject(signal.reason);
|
|
35
|
+
if (signal.aborted)
|
|
36
|
+
onAbort();
|
|
37
|
+
else
|
|
38
|
+
signal.addEventListener('abort', onAbort, { once: true });
|
|
39
|
+
});
|
|
40
|
+
try {
|
|
41
|
+
return await Promise.race([work, aborted]);
|
|
42
|
+
}
|
|
43
|
+
finally {
|
|
44
|
+
signal.removeEventListener('abort', onAbort);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
const fail = (code, message, cause, retryAfterMs) => createAmazonError(code, message, { cause, retryAfterMs });
|
|
48
|
+
export function createAmazonSource(options) {
|
|
49
|
+
if (options.apiKey && options.gatewayKey) {
|
|
50
|
+
throw new RangeError('Amazon source: apiKey and gatewayKey are mutually exclusive; set one');
|
|
51
|
+
}
|
|
52
|
+
if (options.haatlabsApiKey && (options.apiKey || options.gatewayKey)) {
|
|
53
|
+
throw new RangeError('Amazon source: haatlabsApiKey cannot be set with apiKey or gatewayKey; set one');
|
|
54
|
+
}
|
|
55
|
+
const fetchImpl = options.fetch ?? fetch;
|
|
56
|
+
const base = options.baseUrl.replace(/\/$/, '');
|
|
57
|
+
const maxResponseBytes = options.maxResponseBytes ?? DEFAULT_AMAZON_MAX_RESPONSE_BYTES;
|
|
58
|
+
const limit = createConcurrencyLimit(options.maxConcurrent ?? DEFAULT_MAX_CONCURRENT, options.maxWaitMs ?? DEFAULT_MAX_WAIT_MS);
|
|
59
|
+
const headers = {
|
|
60
|
+
accept: 'application/json',
|
|
61
|
+
...gatewayAuthorization(options.gatewayKey),
|
|
62
|
+
};
|
|
63
|
+
if (options.apiKey) {
|
|
64
|
+
headers['x-rapidapi-key'] = options.apiKey;
|
|
65
|
+
headers['x-rapidapi-host'] = new URL(base).hostname;
|
|
66
|
+
}
|
|
67
|
+
// The gateway is given no longer than this call still waits for it.
|
|
68
|
+
const forCall = (call, leftMs) => ({
|
|
69
|
+
...headers,
|
|
70
|
+
...gatewayHeadersFor(options.gatewayKey, call && { ...call, deadlineMs: leftMs }, leftMs),
|
|
71
|
+
});
|
|
72
|
+
const available = (needsProxy) => !needsProxy || options.proxyEnabled;
|
|
73
|
+
const info = {
|
|
74
|
+
id: 'amazon',
|
|
75
|
+
name: 'Amazon',
|
|
76
|
+
defaultMarket: 'US',
|
|
77
|
+
markets: AMAZON_MARKETS.map((market) => ({
|
|
78
|
+
code: market.code,
|
|
79
|
+
name: market.name,
|
|
80
|
+
domain: market.domain,
|
|
81
|
+
currency: market.currency,
|
|
82
|
+
available: available(market.needsProxy),
|
|
83
|
+
})),
|
|
84
|
+
capabilities: {
|
|
85
|
+
autocomplete: true,
|
|
86
|
+
sorts: Object.values(SortOrder),
|
|
87
|
+
filters: Object.values(SearchFilter),
|
|
88
|
+
productIdPattern: '^[A-Za-z0-9]{10}$',
|
|
89
|
+
},
|
|
90
|
+
imageHosts: [...AMAZON_IMAGE_HOSTS],
|
|
91
|
+
};
|
|
92
|
+
if (options.haatlabsApiKey) {
|
|
93
|
+
return createHaatlabsAmazonSource({
|
|
94
|
+
baseUrl: options.baseUrl,
|
|
95
|
+
apiKey: options.haatlabsApiKey,
|
|
96
|
+
timeoutMs: options.timeoutMs,
|
|
97
|
+
info,
|
|
98
|
+
...(options.fetch ? { fetch: options.fetch } : {}),
|
|
99
|
+
maxConcurrent: options.maxConcurrent ?? DEFAULT_MAX_CONCURRENT,
|
|
100
|
+
maxWaitMs: options.maxWaitMs ?? DEFAULT_MAX_WAIT_MS,
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
const marketFor = (code) => {
|
|
104
|
+
const market = marketByCode(code);
|
|
105
|
+
if (!market || !available(market.needsProxy)) {
|
|
106
|
+
throw fail(AmazonErrorCode.SOURCE_BAD_REQUEST, `Amazon has no marketplace ${code} that can be read here`);
|
|
107
|
+
}
|
|
108
|
+
return market;
|
|
109
|
+
};
|
|
110
|
+
async function get(path, params, schema, call) {
|
|
111
|
+
const url = `${base}${path}?${new URLSearchParams(params).toString()}`;
|
|
112
|
+
// The timer starts before the wait for a place, so the wait counts against it.
|
|
113
|
+
const budgetMs = localTimeoutMs(options.timeoutMs, call);
|
|
114
|
+
const signal = AbortSignal.timeout(budgetMs);
|
|
115
|
+
const startedAt = performance.now();
|
|
116
|
+
let response;
|
|
117
|
+
try {
|
|
118
|
+
response = await untilAborted(limit(() => {
|
|
119
|
+
signal.throwIfAborted();
|
|
120
|
+
const leftMs = budgetMs - (performance.now() - startedAt);
|
|
121
|
+
return sharedCall('amazon', () => fetchImpl(url, { headers: forCall(call, leftMs), signal }), (answer) => answer.status);
|
|
122
|
+
}), signal);
|
|
123
|
+
}
|
|
124
|
+
catch (error) {
|
|
125
|
+
throw fail('SOURCE_UNAVAILABLE', NOT_RESPONDING, error);
|
|
126
|
+
}
|
|
127
|
+
const status = response.status;
|
|
128
|
+
const busy = [429, 502, 503, 504].includes(status);
|
|
129
|
+
if (!response.ok && options.gatewayKey) {
|
|
130
|
+
const retryAfterMs = retryAfterMsOf(response.headers);
|
|
131
|
+
const gatewayError = await gatewayErrorOf(response);
|
|
132
|
+
const refused = status === 401 || status === 403;
|
|
133
|
+
// Only the gateway's own 400 says `bad_request`; a relayed scraper 400 is about the query.
|
|
134
|
+
const contract = status === 400 && gatewayError === 'bad_request';
|
|
135
|
+
const detail = new UpstreamStatusError(status, gatewayError, refused, contract);
|
|
136
|
+
if (refused)
|
|
137
|
+
throw fail('SOURCE_UNAVAILABLE', NOT_RESPONDING, detail);
|
|
138
|
+
if (status === 400)
|
|
139
|
+
throw fail('SOURCE_BAD_REQUEST', 'Amazon did not accept this search or product reference.', detail);
|
|
140
|
+
if (status === 404 && path === PRODUCT_PATH)
|
|
141
|
+
throw fail('SOURCE_PRODUCT_NOT_FOUND', 'Amazon has no such product.', detail);
|
|
142
|
+
if (busy)
|
|
143
|
+
throw fail('SOURCE_UNAVAILABLE', NOT_RESPONDING, detail, retryAfterMs);
|
|
144
|
+
throw fail('SOURCE_ERROR', `Something went wrong reading Amazon (HTTP ${status}).`, detail);
|
|
145
|
+
}
|
|
146
|
+
if (!response.ok) {
|
|
147
|
+
// A refusal's body is never read: let the connection go.
|
|
148
|
+
await response.body?.cancel().catch(() => undefined);
|
|
149
|
+
if (status === 400)
|
|
150
|
+
throw fail('SOURCE_BAD_REQUEST', 'Amazon did not accept this search or product reference.');
|
|
151
|
+
if (status === 404 && path === PRODUCT_PATH)
|
|
152
|
+
throw fail('SOURCE_PRODUCT_NOT_FOUND', 'Amazon has no such product.');
|
|
153
|
+
if (busy)
|
|
154
|
+
throw fail('SOURCE_UNAVAILABLE', NOT_RESPONDING, undefined, retryAfterMsOf(response.headers));
|
|
155
|
+
throw fail('SOURCE_ERROR', `Something went wrong reading Amazon (HTTP ${status}).`);
|
|
156
|
+
}
|
|
157
|
+
let body;
|
|
158
|
+
try {
|
|
159
|
+
body = await readAtMost(response, maxResponseBytes);
|
|
160
|
+
}
|
|
161
|
+
catch (error) {
|
|
162
|
+
throw fail('SOURCE_UNAVAILABLE', NOT_RESPONDING, error);
|
|
163
|
+
}
|
|
164
|
+
if (body === null)
|
|
165
|
+
throw fail('SOURCE_ERROR', 'Amazon sent more than can be read.');
|
|
166
|
+
let json;
|
|
167
|
+
try {
|
|
168
|
+
json = JSON.parse(body.toString('utf8'));
|
|
169
|
+
}
|
|
170
|
+
catch (error) {
|
|
171
|
+
throw fail('SOURCE_ERROR', 'Something went wrong reading Amazon.', error);
|
|
172
|
+
}
|
|
173
|
+
const parsed = schema.safeParse(json);
|
|
174
|
+
if (!parsed.success)
|
|
175
|
+
throw fail('SOURCE_ERROR', 'Something went wrong reading Amazon.', parsed.error);
|
|
176
|
+
return parsed.data;
|
|
177
|
+
}
|
|
178
|
+
return {
|
|
179
|
+
info,
|
|
180
|
+
cacheSeconds: AMAZON_CACHE_SECONDS,
|
|
181
|
+
async search(query, call) {
|
|
182
|
+
const market = marketFor(query.market);
|
|
183
|
+
const params = {
|
|
184
|
+
query: query.q,
|
|
185
|
+
country: market.code,
|
|
186
|
+
page: String(query.page),
|
|
187
|
+
sort: query.sort,
|
|
188
|
+
};
|
|
189
|
+
if (query.minPrice !== undefined)
|
|
190
|
+
params.min_price = String(query.minPrice);
|
|
191
|
+
if (query.maxPrice !== undefined)
|
|
192
|
+
params.max_price = String(query.maxPrice);
|
|
193
|
+
if (query.brand !== undefined)
|
|
194
|
+
params.brand = query.brand;
|
|
195
|
+
if (query.primeOnly)
|
|
196
|
+
params.is_prime = 'true';
|
|
197
|
+
if (query.fourStarsAndUp)
|
|
198
|
+
params.four_stars_and_up = 'true';
|
|
199
|
+
if (query.dealsOnly)
|
|
200
|
+
params.deals = 'all_discounts';
|
|
201
|
+
return mapSearch(await get('/search', params, RawSearchSchema, call), query);
|
|
202
|
+
},
|
|
203
|
+
async product(input, call) {
|
|
204
|
+
const market = marketFor(input.market);
|
|
205
|
+
const asin = input.id.toUpperCase();
|
|
206
|
+
if (!ASIN.test(asin))
|
|
207
|
+
throw fail(AmazonErrorCode.SOURCE_BAD_REQUEST, 'An Amazon product id is ten letters and digits.');
|
|
208
|
+
return mapProduct(await get(PRODUCT_PATH, { product: asin, country: market.code }, RawProductSchema, call), market);
|
|
209
|
+
},
|
|
210
|
+
async autocomplete(input, call) {
|
|
211
|
+
const market = marketFor(input.market);
|
|
212
|
+
const raw = await get('/search/autocomplete', { query: input.q, country: market.code }, RawAutocompleteSchema, call);
|
|
213
|
+
return (raw.suggestions ?? [])
|
|
214
|
+
.map((suggestion) => cleanText(suggestion?.value, 200))
|
|
215
|
+
.filter((value) => value !== null)
|
|
216
|
+
.slice(0, MAX_SUGGESTIONS);
|
|
217
|
+
},
|
|
218
|
+
parseLink: parseAmazonLink,
|
|
219
|
+
async health() {
|
|
220
|
+
try {
|
|
221
|
+
const response = await fetchImpl(`${base}/health`, {
|
|
222
|
+
headers: options.gatewayKey
|
|
223
|
+
? {
|
|
224
|
+
...headers,
|
|
225
|
+
[GATEWAY_HEADER.deadlineMs]: String(Math.min(options.timeoutMs, HEALTH_TIMEOUT_MS)),
|
|
226
|
+
}
|
|
227
|
+
: headers,
|
|
228
|
+
signal: AbortSignal.timeout(Math.min(options.timeoutMs, HEALTH_TIMEOUT_MS)),
|
|
229
|
+
});
|
|
230
|
+
return response.ok
|
|
231
|
+
? { healthy: true }
|
|
232
|
+
: { healthy: false, detail: `HTTP ${response.status}` };
|
|
233
|
+
}
|
|
234
|
+
catch {
|
|
235
|
+
return { healthy: false, detail: 'no answer' };
|
|
236
|
+
}
|
|
237
|
+
},
|
|
238
|
+
};
|
|
239
|
+
}
|
|
240
|
+
//# sourceMappingURL=amazon-source.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"amazon-source.js","sourceRoot":"","sources":["../src/amazon-source.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,sBAAsB,EACtB,cAAc,EACd,oBAAoB,EACpB,cAAc,EACd,iBAAiB,EACjB,cAAc,EACd,UAAU,EACV,cAAc,EACd,mBAAmB,EACnB,UAAU,GACX,MAAM,qBAAqB,CAAC;AAG7B,OAAO,EAAE,eAAe,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AACjE,OAAO,EAAE,IAAI,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC;AACnD,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,UAAU,CAAC;AACjD,OAAO,EAAE,cAAc,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAC5D,OAAO,EAAE,qBAAqB,EAAE,gBAAgB,EAAE,eAAe,EAAE,MAAM,UAAU,CAAC;AACpF,OAAO,EAAE,YAAY,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAGvD,OAAO,EAAE,0BAA0B,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AU,EAAE,KAAK,EAAE,GAAG,CAAC,CAAC;iBACtD,MAAM,CAAC,CAAC,KAAK,EAAmB,EAAE,CAAC,KAAK,KAAK,IAAI,CAAC;iBAClD,KAAK,CAAC,CAAC,EAAE,eAAe,CAAC,CAAC;QAC/B,CAAC;QACD,SAAS,EAAE,eAAe;QAC1B,KAAK,CAAC,MAAM;YACV,IAAI,CAAC;gBACH,MAAM,QAAQ,GAAG,MAAM,SAAS,CAAC,GAAG,IAAI,SAAS,EAAE;oBACjD,OAAO,EAAE,OAAO,CAAC,UAAU;wBACzB,CAAC,CAAC;4BACE,GAAG,OAAO;4BACV,CAAC,cAAc,CAAC,UAAU,CAAC,EAAE,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,OAAO,CAAC,SAAS,EAAE,iBAAiB,CAAC,CAAC;yBACpF;wBACH,CAAC,CAAC,OAAO;oBACX,MAAM,EAAE,WAAW,CAAC,OAAO,CAAC,IAAI,CAAC,GAAG,CAAC,OAAO,CAAC,SAAS,EAAE,iBAAiB,CAAC,CAAC;iBAC5E,CAAC,CAAC;gBACH,OAAO,QAAQ,CAAC,EAAE;oBAChB,CAAC,CAAC,EAAE,OAAO,EAAE,IAAI,EAAE;oBACnB,CAAC,CAAC,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,QAAQ,QAAQ,CAAC,MAAM,EAAE,EAAE,CAAC;YAC5D,CAAC;YAAC,MAAM,CAAC;gBACP,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,WAAW,EAAE,CAAC;YACjD,CAAC;QACH,CAAC;KACF,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type { ProductSource } from './source.js';
|
|
2
|
+
/** Real products are under 100 KB, so 2 MiB is plenty. */
|
|
3
|
+
export declare const DEFAULT_AMAZON_MAX_RESPONSE_BYTES: number;
|
|
4
|
+
/** How long each kind of answer may be kept, in seconds. */
|
|
5
|
+
export declare const AMAZON_CACHE_SECONDS: {
|
|
6
|
+
readonly search: 3600;
|
|
7
|
+
readonly product: 3600;
|
|
8
|
+
readonly autocomplete: 86400;
|
|
9
|
+
};
|
|
10
|
+
/** Where Amazon serves product images from: the dashboards' content security policy names each. */
|
|
11
|
+
export declare const AMAZON_IMAGE_HOSTS: readonly ["https://m.media-amazon.com", "https://images-na.ssl-images-amazon.com", "https://images-eu.ssl-images-amazon.com", "https://images-fe.ssl-images-amazon.com"];
|
|
12
|
+
export interface AmazonSourceOptions {
|
|
13
|
+
/** The scraper container, or the author's hosted API (which needs `apiKey`). */
|
|
14
|
+
baseUrl: string;
|
|
15
|
+
timeoutMs: number;
|
|
16
|
+
/** The hosted API's key. Mutually exclusive with `gatewayKey`: setting both is refused. */
|
|
17
|
+
apiKey: string | null;
|
|
18
|
+
/**
|
|
19
|
+
* haatlabs-shared's gateway key; null reaches a scraper (or the hosted API) directly.
|
|
20
|
+
* Mutually exclusive with `apiKey`.
|
|
21
|
+
*/
|
|
22
|
+
gatewayKey: string | null;
|
|
23
|
+
/**
|
|
24
|
+
* Your RapidAPI key for the Haatlabs API (`@haatlabs-core/services`), with `baseUrl` its address
|
|
25
|
+
* (`https://haatlabs-api.p.rapidapi.com`). Mutually exclusive with `apiKey` and `gatewayKey`.
|
|
26
|
+
*/
|
|
27
|
+
haatlabsApiKey?: string | null;
|
|
28
|
+
/** True when the scraper has a proxy that exits in Japan and Australia. */
|
|
29
|
+
proxyEnabled: boolean;
|
|
30
|
+
fetch?: typeof fetch;
|
|
31
|
+
maxConcurrent?: number;
|
|
32
|
+
/** How long a call may wait for one of the `maxConcurrent` places before it is refused. */
|
|
33
|
+
maxWaitMs?: number;
|
|
34
|
+
/** The largest answer read; default 2 MiB. */
|
|
35
|
+
maxResponseBytes?: number;
|
|
36
|
+
}
|
|
37
|
+
export declare function createAmazonSource(options: AmazonSourceOptions): ProductSource;
|