@superdurable/connector-bocha 0.1.0 → 0.1.1
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/dex-connector.json +2 -2
- package/dist/index.d.ts +1 -0
- package/dist/index.js +73 -30
- package/package.json +2 -2
package/dex-connector.json
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"connectorId": "bocha",
|
|
3
|
-
"releaseTag": "connectors/bocha/go/v0.1.
|
|
3
|
+
"releaseTag": "connectors/bocha/go/v0.1.2",
|
|
4
4
|
"package": {
|
|
5
5
|
"ecosystem": "npm",
|
|
6
6
|
"name": "@superdurable/connector-bocha",
|
|
7
|
-
"version": "0.1.
|
|
7
|
+
"version": "0.1.1"
|
|
8
8
|
},
|
|
9
9
|
"factories": [
|
|
10
10
|
{
|
package/dist/index.d.ts
CHANGED
|
@@ -73,6 +73,7 @@ declare class BochaClient {
|
|
|
73
73
|
/** Send one bounded web search request. */
|
|
74
74
|
getSearchResults(call: Call, query: string): Promise<{
|
|
75
75
|
response?: ProviderResponse;
|
|
76
|
+
attempt?: QueryAttempt<SearchWebOutput>;
|
|
76
77
|
error?: 'credential' | 'transport' | 'tooLarge';
|
|
77
78
|
retryAfterMs: number;
|
|
78
79
|
}>;
|
package/dist/index.js
CHANGED
|
@@ -73,7 +73,12 @@ var BochaClient = class {
|
|
|
73
73
|
if (error instanceof BodyTooLargeError && error.status !== void 0) return { response: { status: error.status, headers: error.headers ?? {}, body: new Uint8Array() }, error: "tooLarge", retryAfterMs: 0 };
|
|
74
74
|
return { error: "transport", retryAfterMs: 0 };
|
|
75
75
|
}
|
|
76
|
-
|
|
76
|
+
const retryAfterMs = parseRetryAfter(header(response.headers, "retry-after"));
|
|
77
|
+
if (response.body.length > this.maxResponseBytes) return { response, error: "tooLarge", retryAfterMs };
|
|
78
|
+
if (response.status !== 200) return { response, retryAfterMs };
|
|
79
|
+
const attempt = decodeSearchResponse(response.body, apiKey);
|
|
80
|
+
if (attempt.kind === "branch" && attempt.branch === "defect") return { error: "credential", retryAfterMs };
|
|
81
|
+
return { response, attempt, retryAfterMs };
|
|
77
82
|
}
|
|
78
83
|
};
|
|
79
84
|
var SearchWebOperation = class {
|
|
@@ -88,42 +93,16 @@ var SearchWebOperation = class {
|
|
|
88
93
|
}
|
|
89
94
|
/** Classify one provider response using the Go connector contract. */
|
|
90
95
|
async invoke(call, input) {
|
|
91
|
-
if (!input || typeof input.query !== "string" ||
|
|
96
|
+
if (!input || typeof input.query !== "string" || /[\uD800-\uDFFF]/u.test(input.query) || /^\p{White_Space}*$/u.test(input.query) || [...input.query].length > 256) return branch("defect", "VALIDATION");
|
|
92
97
|
const exchange = await this.client.getSearchResults(call, input.query);
|
|
93
98
|
if (exchange.error === "credential") return branch("defect", "AUTHENTICATION");
|
|
94
99
|
if (exchange.error === "transport" || !exchange.response) return retry("AVAILABILITY");
|
|
95
|
-
const { status
|
|
100
|
+
const { status } = exchange.response;
|
|
96
101
|
if (status === 429) return retry("RATE_LIMIT", exchange.retryAfterMs);
|
|
97
102
|
if (status === 408 || status >= 500) return retry("AVAILABILITY", exchange.retryAfterMs);
|
|
98
103
|
if (status !== 200) return branch("providerRejected", status === 401 ? "AUTHENTICATION" : status === 403 ? "AUTHORIZATION" : "PROVIDER_REJECTION");
|
|
99
104
|
if (exchange.error === "tooLarge") return branch("invalidResponse", "RESPONSE_TOO_LARGE");
|
|
100
|
-
|
|
101
|
-
try {
|
|
102
|
-
wire = JSON.parse(new TextDecoder("utf-8", { fatal: true }).decode(body));
|
|
103
|
-
} catch {
|
|
104
|
-
return branch("invalidResponse", "PROTOCOL");
|
|
105
|
-
}
|
|
106
|
-
if (!wire || typeof wire !== "object" || Array.isArray(wire)) return branch("invalidResponse", "PROTOCOL");
|
|
107
|
-
const result = wire;
|
|
108
|
-
if (typeof result.code !== "number" || !Number.isInteger(result.code)) return branch("invalidResponse", "PROTOCOL");
|
|
109
|
-
if (result.code !== 200) return branch("providerRejected", "PROVIDER_REJECTION");
|
|
110
|
-
const pages = result.data?.webPages?.value;
|
|
111
|
-
if (!Array.isArray(pages) || pages.length > 50) return branch("invalidResponse", "PROTOCOL");
|
|
112
|
-
const results = [];
|
|
113
|
-
for (const page of pages.slice(0, 5)) {
|
|
114
|
-
if (!page || typeof page !== "object" || Array.isArray(page)) return branch("invalidResponse", "PROTOCOL");
|
|
115
|
-
const candidate = page;
|
|
116
|
-
if (typeof candidate.name !== "string" || !candidate.name || Buffer.byteLength(candidate.name) > 500 || typeof candidate.url !== "string" || typeof candidate.snippet !== "string" || Buffer.byteLength(candidate.snippet) > 2e3) return branch("invalidResponse", "PROTOCOL");
|
|
117
|
-
let parsed;
|
|
118
|
-
try {
|
|
119
|
-
parsed = new URL(candidate.url);
|
|
120
|
-
} catch {
|
|
121
|
-
return branch("invalidResponse", "PROTOCOL");
|
|
122
|
-
}
|
|
123
|
-
if (parsed.protocol !== "https:" || !parsed.hostname || parsed.username || parsed.password) return branch("invalidResponse", "PROTOCOL");
|
|
124
|
-
results.push({ title: candidate.name, url: candidate.url, snippet: candidate.snippet });
|
|
125
|
-
}
|
|
126
|
-
return newQueryBranch("found", { results });
|
|
105
|
+
return exchange.attempt ?? decodeSearchResponse(exchange.response.body);
|
|
127
106
|
}
|
|
128
107
|
};
|
|
129
108
|
function header(headers, name) {
|
|
@@ -139,6 +118,70 @@ function branch(name, kind) {
|
|
|
139
118
|
function retry(kind, delay = 0) {
|
|
140
119
|
return newQueryRetry(failure(kind), delay);
|
|
141
120
|
}
|
|
121
|
+
function decodeSearchResponse(body, apiKey) {
|
|
122
|
+
let wire;
|
|
123
|
+
try {
|
|
124
|
+
wire = JSON.parse(
|
|
125
|
+
new TextDecoder().decode(body),
|
|
126
|
+
(key, value, context) => {
|
|
127
|
+
if (key.toLowerCase() === "code" && typeof value === "number") {
|
|
128
|
+
const source = context?.source ?? "";
|
|
129
|
+
return source.length <= 20 && /^-?(?:0|[1-9][0-9]*)$/.test(source) ? BigInt(source) : void 0;
|
|
130
|
+
}
|
|
131
|
+
if (value !== null && typeof value === "object" && !Array.isArray(value)) {
|
|
132
|
+
return Object.fromEntries(
|
|
133
|
+
Object.entries(value).map(([name, field]) => [name.toLowerCase(), field])
|
|
134
|
+
);
|
|
135
|
+
}
|
|
136
|
+
return value;
|
|
137
|
+
}
|
|
138
|
+
);
|
|
139
|
+
} catch {
|
|
140
|
+
return branch("invalidResponse", "PROTOCOL");
|
|
141
|
+
}
|
|
142
|
+
if (!isRecord(wire) || typeof wire.code !== "bigint" || BigInt.asIntN(64, wire.code) !== wire.code)
|
|
143
|
+
return branch("invalidResponse", "PROTOCOL");
|
|
144
|
+
const data = wire.data;
|
|
145
|
+
if (data != null && !isRecord(data)) return branch("invalidResponse", "PROTOCOL");
|
|
146
|
+
const collection = data?.webpages;
|
|
147
|
+
if (collection != null && !isRecord(collection)) return branch("invalidResponse", "PROTOCOL");
|
|
148
|
+
const pages = collection?.value;
|
|
149
|
+
if (pages != null && (!Array.isArray(pages) || pages.some(
|
|
150
|
+
(page) => page != null && (!isRecord(page) || ["name", "url", "snippet"].some((key) => page[key] != null && typeof page[key] !== "string"))
|
|
151
|
+
)))
|
|
152
|
+
return branch("invalidResponse", "PROTOCOL");
|
|
153
|
+
if (wire.code !== 200n) return branch("providerRejected", "PROVIDER_REJECTION");
|
|
154
|
+
if (!Array.isArray(pages) || pages.length > 50) return branch("invalidResponse", "PROTOCOL");
|
|
155
|
+
const results = [];
|
|
156
|
+
for (const page of pages.slice(0, 5)) {
|
|
157
|
+
if (!isRecord(page) || typeof page.name !== "string" || !page.name || Buffer.byteLength(page.name) > 500 || typeof page.url !== "string")
|
|
158
|
+
return branch("invalidResponse", "PROTOCOL");
|
|
159
|
+
const snippet = page.snippet ?? "";
|
|
160
|
+
if (typeof snippet !== "string" || Buffer.byteLength(snippet) > 2e3)
|
|
161
|
+
return branch("invalidResponse", "PROTOCOL");
|
|
162
|
+
if (!validResultURL(page.url)) return branch("invalidResponse", "PROTOCOL");
|
|
163
|
+
if (apiKey && [page.name, page.url, snippet].some((field) => field.includes(apiKey)))
|
|
164
|
+
return branch("defect", "AUTHENTICATION");
|
|
165
|
+
results.push({ title: page.name, url: page.url, snippet });
|
|
166
|
+
}
|
|
167
|
+
return newQueryBranch("found", { results });
|
|
168
|
+
}
|
|
169
|
+
function isRecord(value) {
|
|
170
|
+
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
171
|
+
}
|
|
172
|
+
function validResultURL(value) {
|
|
173
|
+
const parts = /^https:\/\/([^/?#]+)([^?#]*)(?:\?[^#]*)?(?:#(.*))?$/iu.exec(value);
|
|
174
|
+
if (!parts || [...value].some((c) => c.charCodeAt(0) < 32 || c.charCodeAt(0) === 127)) return false;
|
|
175
|
+
const [, authority, path, fragment] = parts;
|
|
176
|
+
if (!authority || authority.includes("@")) return false;
|
|
177
|
+
if ([authority, path, fragment].some((part) => part && /%(?![\da-f]{2})/iu.test(part))) return false;
|
|
178
|
+
const host = authority.replace(/:\d*$/u, "");
|
|
179
|
+
try {
|
|
180
|
+
return Boolean(new URL("https://" + host).hostname);
|
|
181
|
+
} catch {
|
|
182
|
+
return false;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
142
185
|
|
|
143
186
|
// src/project.ts
|
|
144
187
|
function newProjectConnection2(project, connectionName) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@superdurable/connector-bocha",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"description": "bocha connector for Dex TypeScript applications",
|
|
6
6
|
"repository": {
|
|
@@ -36,7 +36,7 @@
|
|
|
36
36
|
"build": "tsup src/index.ts src/mock.ts --format esm --dts --clean",
|
|
37
37
|
"typecheck": "tsc --noEmit",
|
|
38
38
|
"lint": "eslint src test examples",
|
|
39
|
-
"test": "vitest run --exclude test/integration/**",
|
|
39
|
+
"test": "vitest run --exclude 'test/integration/**'",
|
|
40
40
|
"test:integration": "vitest run test/integration/*.integration.test.ts",
|
|
41
41
|
"example:worker": "tsx examples/basic/worker.ts",
|
|
42
42
|
"example:start": "tsx examples/basic/start.ts",
|