@superdurable/connector-baishancloud 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.
@@ -4,7 +4,7 @@
4
4
  "package": {
5
5
  "ecosystem": "npm",
6
6
  "name": "@superdurable/connector-baishancloud",
7
- "version": "0.1.0"
7
+ "version": "0.1.1"
8
8
  },
9
9
  "factories": [
10
10
  {
package/dist/index.js CHANGED
@@ -74,10 +74,13 @@ var BaishanCloudClient = class {
74
74
  try {
75
75
  response = await this.send(request);
76
76
  } catch (error) {
77
- if (error instanceof BodyTooLargeError && error.status !== void 0) return { response: { status: error.status, headers: error.headers ?? {}, body: new Uint8Array() }, error: "tooLarge", retryAfterMs: 0 };
77
+ if (error instanceof BodyTooLargeError && error.status !== void 0) {
78
+ const headers = error.headers ?? {};
79
+ return { response: { status: error.status, headers, body: new Uint8Array() }, error: "tooLarge", retryAfterMs: retryDelay(headers) };
80
+ }
78
81
  return { error: "transport", retryAfterMs: 0 };
79
82
  }
80
- return { response, error: response.body.length > this.maxResponseBytes ? "tooLarge" : void 0, retryAfterMs: parseRetryAfter(header(response.headers, "retry-after")) || parseRetryAfter(header(response.headers, "x-retry-after")) };
83
+ return { response, error: response.body.length > this.maxResponseBytes ? "tooLarge" : void 0, retryAfterMs: retryDelay(response.headers) };
81
84
  }
82
85
  };
83
86
  var DescribeDomainOperation = class {
@@ -93,7 +96,7 @@ var DescribeDomainOperation = class {
93
96
  /** Classify one provider response using the Go connector contract. */
94
97
  async invoke(call, input) {
95
98
  if (!input || typeof input.domain !== "string") return branch("defect", "VALIDATION");
96
- const domain = input.domain.trim().toLowerCase();
99
+ const domain = input.domain.replace(/^\p{White_Space}+|\p{White_Space}+$/gu, "").toLowerCase();
97
100
  if (domain.length > 253 || !/^[a-z0-9](?:[a-z0-9-]*[a-z0-9])?(?:\.[a-z0-9](?:[a-z0-9-]*[a-z0-9])?)+$/.test(domain)) return branch("defect", "VALIDATION");
98
101
  const exchange = await this.client.getDomain(call, domain);
99
102
  if (exchange.error === "credential") return branch("defect", "AUTHENTICATION");
@@ -103,23 +106,50 @@ var DescribeDomainOperation = class {
103
106
  if (status === 408 || status >= 500) return retry("AVAILABILITY", exchange.retryAfterMs);
104
107
  if (exchange.error === "tooLarge") return branch("invalidResponse", "RESPONSE_TOO_LARGE");
105
108
  if (status !== 200) return branch("providerRejected", status === 401 ? "AUTHENTICATION" : status === 403 ? "AUTHORIZATION" : status === 400 || status === 404 ? "VALIDATION" : "PROVIDER_REJECTION");
106
- let wire;
107
109
  try {
108
- wire = JSON.parse(new TextDecoder("utf-8", { fatal: true }).decode(body));
110
+ return decodeDomain(body, domain);
109
111
  } catch {
110
112
  return branch("invalidResponse", "PROTOCOL");
111
113
  }
112
- if (!wire || typeof wire !== "object" || Array.isArray(wire)) return branch("invalidResponse", "PROTOCOL");
113
- const result = wire;
114
- if (typeof result.code !== "number" || !Number.isInteger(result.code)) return branch("invalidResponse", "PROTOCOL");
115
- if (result.code !== 0) return branch("providerRejected", "PROVIDER_REJECTION");
116
- if (!Array.isArray(result.data) || result.data.length > 1) return branch("invalidResponse", "PROTOCOL");
117
- if (result.data.length === 0) return branch("notFound", "NOT_FOUND");
118
- const detail = result.data[0];
119
- if (!detail || typeof detail.id !== "string" || !detail.id || typeof detail.domain !== "string" || detail.domain.toLowerCase() !== domain || typeof detail.type !== "string" || !detail.type || typeof detail.status !== "string" || !detail.status || typeof detail.cname !== "string" || !detail.cname) return branch("invalidResponse", "PROTOCOL");
120
- return newQueryBranch("found", { id: detail.id, domain: detail.domain, type: detail.type, status: detail.status, cname: detail.cname });
121
114
  }
122
115
  };
116
+ function isRecord(value) {
117
+ return value !== null && typeof value === "object" && !Array.isArray(value);
118
+ }
119
+ function decodeDomain(body, domain) {
120
+ const wire = JSON.parse(
121
+ new TextDecoder("utf-8", { ignoreBOM: true }).decode(body),
122
+ (_key, value, context) => {
123
+ if (isRecord(value)) return Object.fromEntries(Object.entries(value).map(([name, item]) => [name.toLowerCase().replace(/ſ/g, "s"), item]));
124
+ if (typeof value !== "number" || !context?.source) return value;
125
+ if (!/^-?\d+$/.test(context.source)) return NaN;
126
+ const exact = BigInt(context.source);
127
+ return exact < -(1n << 63n) || exact >= 1n << 63n ? NaN : value;
128
+ }
129
+ );
130
+ if (!isRecord(wire) || typeof wire.code !== "number" || !Number.isInteger(wire.code)) throw new TypeError("Invalid domain response code");
131
+ if (wire.data != null && !Array.isArray(wire.data)) throw new TypeError("Invalid domain list");
132
+ const details = (wire.data ?? []).map(readDetail);
133
+ if (wire.code !== 0) return branch("providerRejected", "PROVIDER_REJECTION");
134
+ if (wire.data == null || details.length > 1) throw new TypeError("Invalid domain list");
135
+ if (details.length === 0) return branch("notFound", "NOT_FOUND");
136
+ const detail = details[0];
137
+ if (!detail.id || detail.domain.toLowerCase().replace(/ſ/g, "s") !== domain || !detail.type || !detail.status || !detail.cname) throw new TypeError("Invalid domain detail");
138
+ return newQueryBranch("found", detail);
139
+ }
140
+ function readDetail(row) {
141
+ const item = row ?? {};
142
+ if (!isRecord(item)) throw new TypeError("Invalid domain detail");
143
+ function field(name) {
144
+ const value = item[name] ?? "";
145
+ if (typeof value !== "string") throw new TypeError("Invalid domain field");
146
+ return value;
147
+ }
148
+ return { id: field("id"), domain: field("domain"), type: field("type"), status: field("status"), cname: field("cname") };
149
+ }
150
+ function retryDelay(headers) {
151
+ return parseRetryAfter(header(headers, "retry-after")) || parseRetryAfter(header(headers, "x-retry-after"));
152
+ }
123
153
  function header(headers, name) {
124
154
  const value = Object.entries(headers).find(([key]) => key.toLowerCase() === name)?.[1];
125
155
  return Array.isArray(value) ? value[0] ?? "" : value ?? "";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@superdurable/connector-baishancloud",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "license": "MIT",
5
5
  "description": "baishancloud connector for Dex TypeScript applications",
6
6
  "repository": {