@superdurable/connector-chinaz 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 +51 -22
- package/package.json +1 -1
package/dex-connector.json
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"connectorId": "chinaz",
|
|
3
|
-
"releaseTag": "connectors/chinaz/go/v0.1.
|
|
3
|
+
"releaseTag": "connectors/chinaz/go/v0.1.1",
|
|
4
4
|
"package": {
|
|
5
5
|
"ecosystem": "npm",
|
|
6
6
|
"name": "@superdurable/connector-chinaz",
|
|
7
|
-
"version": "0.1.
|
|
7
|
+
"version": "0.1.1"
|
|
8
8
|
},
|
|
9
9
|
"factories": [
|
|
10
10
|
{
|
package/dist/index.d.ts
CHANGED
|
@@ -81,6 +81,7 @@ declare class ChinazClient {
|
|
|
81
81
|
response?: ProviderResponse;
|
|
82
82
|
error?: 'credential' | 'transport' | 'tooLarge';
|
|
83
83
|
retryAfterMs: number;
|
|
84
|
+
siteAttempt?: QueryAttempt<SiteTDK>;
|
|
84
85
|
}>;
|
|
85
86
|
}
|
|
86
87
|
/** Read public site title, keywords, description, and IP address. */
|
package/dist/index.js
CHANGED
|
@@ -76,10 +76,19 @@ var ChinazClient = class {
|
|
|
76
76
|
try {
|
|
77
77
|
response = await this.send(request);
|
|
78
78
|
} catch (error) {
|
|
79
|
-
if (error instanceof BodyTooLargeError && error.status !== void 0) return {
|
|
79
|
+
if (error instanceof BodyTooLargeError && error.status !== void 0) return {
|
|
80
|
+
response: { status: error.status, headers: error.headers ?? {}, body: new Uint8Array() },
|
|
81
|
+
error: "tooLarge",
|
|
82
|
+
retryAfterMs: parseRetryAfter(header(error.headers ?? {}, "retry-after"))
|
|
83
|
+
};
|
|
80
84
|
return { error: "transport", retryAfterMs: 0 };
|
|
81
85
|
}
|
|
82
|
-
|
|
86
|
+
const retryAfterMs = parseRetryAfter(header(response.headers, "retry-after"));
|
|
87
|
+
if (response.body.length > this.maxResponseBytes) return { response, error: "tooLarge", retryAfterMs };
|
|
88
|
+
if (response.status !== 200) return { response, retryAfterMs };
|
|
89
|
+
const siteAttempt = decodeSiteTDK(domain, response.body, retryAfterMs, apiKey);
|
|
90
|
+
if (siteAttempt.kind === "branch" && siteAttempt.branch === "defect") return { error: "credential", retryAfterMs };
|
|
91
|
+
return { response, siteAttempt, retryAfterMs };
|
|
83
92
|
}
|
|
84
93
|
};
|
|
85
94
|
var GetSiteTDKOperation = class {
|
|
@@ -103,28 +112,48 @@ var GetSiteTDKOperation = class {
|
|
|
103
112
|
if (status === 408 || status >= 500) return retry("AVAILABILITY", exchange.retryAfterMs);
|
|
104
113
|
if (exchange.error === "tooLarge") return branch("invalidResponse", "RESPONSE_TOO_LARGE");
|
|
105
114
|
if (status !== 200) return branch("providerRejected", status === 401 ? "AUTHENTICATION" : status === 403 ? "AUTHORIZATION" : status === 400 || status === 404 ? "VALIDATION" : "PROVIDER_REJECTION");
|
|
106
|
-
|
|
107
|
-
try {
|
|
108
|
-
wire = JSON.parse(new TextDecoder("utf-8", { fatal: true }).decode(body));
|
|
109
|
-
} catch {
|
|
110
|
-
return branch("invalidResponse", "PROTOCOL");
|
|
111
|
-
}
|
|
112
|
-
if (!wire || typeof wire !== "object" || Array.isArray(wire)) return branch("invalidResponse", "PROTOCOL");
|
|
113
|
-
const response = wire;
|
|
114
|
-
if (typeof response.StateCode !== "number" || !Number.isInteger(response.StateCode)) return branch("invalidResponse", "PROTOCOL");
|
|
115
|
-
const state = response.StateCode;
|
|
116
|
-
if (state === 10025) return branch("notFound", "PROVIDER_REJECTION");
|
|
117
|
-
if ([10011, 10012, 10013, 10026].includes(state)) return retry("RATE_LIMIT", exchange.retryAfterMs);
|
|
118
|
-
if ([-1, 10020, 10024, 10029, 10034].includes(state)) return retry("AVAILABILITY", exchange.retryAfterMs);
|
|
119
|
-
if ([10001, 10003, 10004, 10009].includes(state)) return branch("providerRejected", "AUTHENTICATION");
|
|
120
|
-
if ([10002, 10005, 10008, 10022, 10027, 10032].includes(state)) return branch("providerRejected", "AUTHORIZATION");
|
|
121
|
-
if (state !== 1) return branch("providerRejected", "PROVIDER_REJECTION");
|
|
122
|
-
if (!response.Result || typeof response.Result !== "object" || Array.isArray(response.Result)) return branch("invalidResponse", "PROTOCOL");
|
|
123
|
-
const result = response.Result;
|
|
124
|
-
if ([result.Title, result.Keywords, result.Description, result.IP].some((value) => value !== void 0 && typeof value !== "string")) return branch("invalidResponse", "PROTOCOL");
|
|
125
|
-
return newQueryBranch("found", { domain: input.domain, title: result.Title ?? "", keywords: result.Keywords ?? "", description: result.Description ?? "", ipAddress: result.IP ?? "" });
|
|
115
|
+
return exchange.siteAttempt ?? decodeSiteTDK(input.domain, body, exchange.retryAfterMs);
|
|
126
116
|
}
|
|
127
117
|
};
|
|
118
|
+
function decodeSiteTDK(domain, body, retryAfterMs, apiKey) {
|
|
119
|
+
let response;
|
|
120
|
+
try {
|
|
121
|
+
response = providerFields(JSON.parse(new TextDecoder().decode(body), (name, value, context) => {
|
|
122
|
+
if (name.toLowerCase() === "statecode" && typeof value === "number") {
|
|
123
|
+
if (!context?.source || context.source.length > 20 || !/^-?(?:0|[1-9][0-9]*)$/.test(context.source)) return null;
|
|
124
|
+
const code = BigInt(context.source);
|
|
125
|
+
return BigInt.asIntN(64, code) === code ? Number(code) : null;
|
|
126
|
+
}
|
|
127
|
+
return value;
|
|
128
|
+
}));
|
|
129
|
+
} catch {
|
|
130
|
+
return branch("invalidResponse", "PROTOCOL");
|
|
131
|
+
}
|
|
132
|
+
const state = response?.statecode;
|
|
133
|
+
if (typeof state !== "number") return branch("invalidResponse", "PROTOCOL");
|
|
134
|
+
const result = providerFields(response?.result);
|
|
135
|
+
if (response?.result != null && (!result || ["title", "keywords", "description", "ip"].some((key) => result[key] != null && typeof result[key] !== "string"))) return branch("invalidResponse", "PROTOCOL");
|
|
136
|
+
if (state === 10025) return branch("notFound", "PROVIDER_REJECTION");
|
|
137
|
+
if ([10011, 10012, 10013, 10026].includes(state)) return retry("RATE_LIMIT", retryAfterMs);
|
|
138
|
+
if ([-1, 10020, 10024, 10029, 10034].includes(state)) return retry("AVAILABILITY", retryAfterMs);
|
|
139
|
+
if ([10001, 10003, 10004, 10009].includes(state)) return branch("providerRejected", "AUTHENTICATION");
|
|
140
|
+
if ([10002, 10005, 10008, 10022, 10027, 10032].includes(state)) return branch("providerRejected", "AUTHORIZATION");
|
|
141
|
+
if (state !== 1) return branch("providerRejected", "PROVIDER_REJECTION");
|
|
142
|
+
if (!result) return branch("invalidResponse", "PROTOCOL");
|
|
143
|
+
const site = {
|
|
144
|
+
domain,
|
|
145
|
+
title: String(result.title ?? ""),
|
|
146
|
+
keywords: String(result.keywords ?? ""),
|
|
147
|
+
description: String(result.description ?? ""),
|
|
148
|
+
ipAddress: String(result.ip ?? "")
|
|
149
|
+
};
|
|
150
|
+
if (apiKey && Object.values(site).some((value) => value.includes(apiKey))) return branch("defect", "AUTHENTICATION");
|
|
151
|
+
return newQueryBranch("found", site);
|
|
152
|
+
}
|
|
153
|
+
function providerFields(value) {
|
|
154
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) return void 0;
|
|
155
|
+
return Object.fromEntries(Object.entries(value).map(([name, field]) => [name.toLowerCase(), field]));
|
|
156
|
+
}
|
|
128
157
|
function validDomain(domain) {
|
|
129
158
|
return domain.length >= 3 && domain.length <= 253 && domain === domain.toLowerCase() && domain.split(".").length >= 2 && domain.split(".").every((label) => label.length >= 1 && label.length <= 63 && /^[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/.test(label));
|
|
130
159
|
}
|