@absolutejs/search 0.2.0 → 0.2.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +18 -0
- package/changelog.json +33 -0
- package/dist/brave.js +23 -13
- package/dist/index.js +3 -3
- package/dist/types.d.ts +4 -0
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -6,6 +6,24 @@ This file is generated by `absolute-changelog` from the entries in
|
|
|
6
6
|
`changelog/`. Edit an entry, not this file — and add new ones under
|
|
7
7
|
`changelog/unreleased/`.
|
|
8
8
|
|
|
9
|
+
## 0.2.3 — 2026-10-22
|
|
10
|
+
|
|
11
|
+
### Changed
|
|
12
|
+
|
|
13
|
+
- **SearchAttempt includes optional errorCode and errorMessage fields for sanitized provider diagnostics.** (`SearchAttempt`)
|
|
14
|
+
|
|
15
|
+
## 0.2.2 — 2026-10-22
|
|
16
|
+
|
|
17
|
+
### Fixed
|
|
18
|
+
|
|
19
|
+
- **Preserve sanitized Brave error codes, explanations and request IDs; classify invalid subscription tokens as unavailable rather than generic HTTP 422 errors.**
|
|
20
|
+
|
|
21
|
+
## 0.2.1 — 2026-09-28
|
|
22
|
+
|
|
23
|
+
### Fixed
|
|
24
|
+
|
|
25
|
+
- **Brave web searches no longer fail with HTTP 404: the Api-Version header is sent only when a caller pins apiVersion, and a web search with no results is reported as empty instead of malformed.**
|
|
26
|
+
|
|
9
27
|
## 0.2.0 — 2026-09-24
|
|
10
28
|
|
|
11
29
|
### Added
|
package/changelog.json
CHANGED
|
@@ -2,6 +2,39 @@
|
|
|
2
2
|
"contract": 1,
|
|
3
3
|
"name": "@absolutejs/search",
|
|
4
4
|
"releases": [
|
|
5
|
+
{
|
|
6
|
+
"changes": [
|
|
7
|
+
{
|
|
8
|
+
"kind": "changed",
|
|
9
|
+
"summary": "SearchAttempt includes optional errorCode and errorMessage fields for sanitized provider diagnostics.",
|
|
10
|
+
"symbols": [
|
|
11
|
+
"SearchAttempt"
|
|
12
|
+
]
|
|
13
|
+
}
|
|
14
|
+
],
|
|
15
|
+
"date": "2026-10-22",
|
|
16
|
+
"version": "0.2.3"
|
|
17
|
+
},
|
|
18
|
+
{
|
|
19
|
+
"changes": [
|
|
20
|
+
{
|
|
21
|
+
"kind": "fixed",
|
|
22
|
+
"summary": "Preserve sanitized Brave error codes, explanations and request IDs; classify invalid subscription tokens as unavailable rather than generic HTTP 422 errors."
|
|
23
|
+
}
|
|
24
|
+
],
|
|
25
|
+
"date": "2026-10-22",
|
|
26
|
+
"version": "0.2.2"
|
|
27
|
+
},
|
|
28
|
+
{
|
|
29
|
+
"changes": [
|
|
30
|
+
{
|
|
31
|
+
"kind": "fixed",
|
|
32
|
+
"summary": "Brave web searches no longer fail with HTTP 404: the Api-Version header is sent only when a caller pins apiVersion, and a web search with no results is reported as empty instead of malformed."
|
|
33
|
+
}
|
|
34
|
+
],
|
|
35
|
+
"date": "2026-09-28",
|
|
36
|
+
"version": "0.2.1"
|
|
37
|
+
},
|
|
5
38
|
{
|
|
6
39
|
"changes": [
|
|
7
40
|
{
|
package/dist/brave.js
CHANGED
|
@@ -95,7 +95,7 @@ var braveSearchCapabilities = {
|
|
|
95
95
|
};
|
|
96
96
|
var createBraveSearch = (options) => {
|
|
97
97
|
const now = options.now ?? Date.now;
|
|
98
|
-
const version = options.apiVersion ?? "
|
|
98
|
+
const version = options.apiVersion ?? "default";
|
|
99
99
|
return {
|
|
100
100
|
name: "brave",
|
|
101
101
|
capabilities: braveSearchCapabilities,
|
|
@@ -156,7 +156,7 @@ var createBraveSearch = (options) => {
|
|
|
156
156
|
const headers = {
|
|
157
157
|
accept: "application/json",
|
|
158
158
|
"X-Subscription-Token": options.apiKey,
|
|
159
|
-
"Api-Version":
|
|
159
|
+
...options.apiVersion ? { "Api-Version": options.apiVersion } : {},
|
|
160
160
|
"Content-Type": "application/json"
|
|
161
161
|
};
|
|
162
162
|
const url = new URL(`https://api.search.brave.com${endpoint}`);
|
|
@@ -194,17 +194,27 @@ var createBraveSearch = (options) => {
|
|
|
194
194
|
if (!response.ok) {
|
|
195
195
|
result.status = response.status === 429 ? "rate_limited" : response.status === 401 || response.status === 403 ? "unavailable" : "error";
|
|
196
196
|
const error = await response.json().catch(() => null);
|
|
197
|
-
if (record(error) && record(error.error)
|
|
198
|
-
|
|
199
|
-
|
|
197
|
+
if (record(error) && record(error.error)) {
|
|
198
|
+
const sanitize = (value) => text(value)?.split(options.apiKey).join("[redacted]").replace(/Bearer\s+\S+/giu, "Bearer [redacted]").replace(/[\r\n]+/gu, " ").slice(0, 500);
|
|
199
|
+
attempt.errorCode = sanitize(error.error.code);
|
|
200
|
+
attempt.errorMessage = sanitize(error.error.detail);
|
|
201
|
+
attempt.providerRequestId ??= sanitize(error.error.id);
|
|
202
|
+
if (attempt.errorCode === "QUOTA_LIMITED")
|
|
203
|
+
result.status = "quota_exceeded";
|
|
204
|
+
if (attempt.errorCode === "SUBSCRIPTION_TOKEN_INVALID")
|
|
205
|
+
result.status = "unavailable";
|
|
206
|
+
}
|
|
207
|
+
result.limitations.push(`Brave request failed with HTTP ${response.status}${attempt.errorCode ? ` (${attempt.errorCode})` : ""}${attempt.errorMessage ? `: ${attempt.errorMessage}` : ""}`);
|
|
200
208
|
} else {
|
|
201
209
|
const body = await response.json();
|
|
202
210
|
if (!record(body))
|
|
203
211
|
throw new Error("Invalid Brave response");
|
|
204
212
|
const container = mode === "context" ? body.grounding : body.web;
|
|
205
|
-
|
|
213
|
+
const listed = record(container) ? container[mode === "context" ? "generic" : "results"] : undefined;
|
|
214
|
+
const noWebResults = mode === "web" && body.type === "search" && body.web === undefined;
|
|
215
|
+
if (!noWebResults && !Array.isArray(listed))
|
|
206
216
|
throw new Error("Brave response is missing its result array");
|
|
207
|
-
const rows =
|
|
217
|
+
const rows = Array.isArray(listed) ? listed : [];
|
|
208
218
|
let invalid = 0;
|
|
209
219
|
const byUrl = new Map;
|
|
210
220
|
for (const row of rows) {
|
|
@@ -212,27 +222,27 @@ var createBraveSearch = (options) => {
|
|
|
212
222
|
invalid++;
|
|
213
223
|
continue;
|
|
214
224
|
}
|
|
215
|
-
const
|
|
225
|
+
const url = publicUrl(row.url);
|
|
216
226
|
const excerpts = mode === "context" ? strings(row.snippets) : [
|
|
217
227
|
text(row.description),
|
|
218
228
|
...strings(row.extra_snippets)
|
|
219
229
|
].filter((item) => Boolean(item));
|
|
220
|
-
if (!
|
|
230
|
+
if (!url || !text(row.title) || mode === "context" && !excerpts.length) {
|
|
221
231
|
invalid++;
|
|
222
232
|
continue;
|
|
223
233
|
}
|
|
224
234
|
const metadata = record(body.sources) && record(body.sources[String(row.url)]) ? body.sources[String(row.url)] : {};
|
|
225
235
|
const age = Array.isArray(metadata.age) ? text(metadata.age[3]) : undefined;
|
|
226
236
|
const publishedAt = text(row.page_age) ?? age;
|
|
227
|
-
const previous = byUrl.get(
|
|
237
|
+
const previous = byUrl.get(url);
|
|
228
238
|
if (previous)
|
|
229
239
|
previous.excerpts = [
|
|
230
240
|
...new Set([...previous.excerpts, ...excerpts])
|
|
231
241
|
];
|
|
232
242
|
else
|
|
233
|
-
byUrl.set(
|
|
234
|
-
id:
|
|
235
|
-
url
|
|
243
|
+
byUrl.set(url, {
|
|
244
|
+
id: url,
|
|
245
|
+
url,
|
|
236
246
|
title: String(row.title),
|
|
237
247
|
excerpts,
|
|
238
248
|
retrievedAt: new Date(now()).toISOString(),
|
package/dist/index.js
CHANGED
|
@@ -91,14 +91,14 @@ var withSearchCache = (provider, options) => {
|
|
|
91
91
|
if (searchCompleted(result)) {
|
|
92
92
|
const ttl = result.status === "empty" ? options.emptyTtlMs ?? 60000 : options.ttlMs ?? 300000;
|
|
93
93
|
if (ttl > 0) {
|
|
94
|
-
const
|
|
94
|
+
const entry = {
|
|
95
95
|
result: structuredClone(result),
|
|
96
96
|
expiresAt: now() + ttl
|
|
97
97
|
};
|
|
98
98
|
while (cache.size >= capacity)
|
|
99
99
|
cache.delete(cache.keys().next().value);
|
|
100
|
-
cache.set(key,
|
|
101
|
-
await options.store?.set(key,
|
|
100
|
+
cache.set(key, entry);
|
|
101
|
+
await options.store?.set(key, entry);
|
|
102
102
|
}
|
|
103
103
|
}
|
|
104
104
|
return result;
|
package/dist/types.d.ts
CHANGED
|
@@ -34,6 +34,10 @@ export type SearchAttempt = {
|
|
|
34
34
|
durationMs: number;
|
|
35
35
|
status: SearchStatus;
|
|
36
36
|
httpStatus?: number;
|
|
37
|
+
/** Provider diagnostic code, retained independently of HTTP status. */
|
|
38
|
+
errorCode?: string;
|
|
39
|
+
/** Bounded provider explanation with the configured credential removed. */
|
|
40
|
+
errorMessage?: string;
|
|
37
41
|
retryAfterMs?: number;
|
|
38
42
|
/** A successful HTTP response is billable even if parsing fails. */
|
|
39
43
|
billing: "fulfilled" | "rejected" | "unknown" | "not_sent";
|