@superdurable/connector-alapi 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-alapi",
7
- "version": "0.1.0"
7
+ "version": "0.1.1"
8
8
  },
9
9
  "factories": [
10
10
  {
package/dist/index.js CHANGED
@@ -38,6 +38,7 @@ import { SecretString as SecretString2 } from "@superdurable/connectors-sdk";
38
38
  import { z } from "zod";
39
39
 
40
40
  // src/client.ts
41
+ import { isIP } from "net";
41
42
  import { SecretString, newQueryBranch, newQueryRetry } from "@superdurable/connectors-sdk";
42
43
  import { BodyTooLargeError, isHeaderSafeCredential, parseRetryAfter, sendProviderRequest } from "@superdurable/connectors-sdk/providerhttp";
43
44
  var endpoint = "https://v3.alapi.cn/api/ip";
@@ -75,7 +76,8 @@ var LookupIPLocationOperation = class {
75
76
  }
76
77
  /** Send one provider request and classify its result. */
77
78
  async invoke(call, input) {
78
- if (!isPublicIPv4(input.ipAddress)) return branch("defect", "VALIDATION", "ALAPI requires one public IPv4 address");
79
+ const address = publicIPv4(input.ipAddress);
80
+ if (!address) return branch("defect", "VALIDATION", "ALAPI requires one public IPv4 address");
79
81
  let token;
80
82
  try {
81
83
  const resolved = await this.client.credentials.resolve(call, call.context.cancellationSignal);
@@ -92,7 +94,7 @@ var LookupIPLocationOperation = class {
92
94
  url: endpoint,
93
95
  method: "POST",
94
96
  headers: { Accept: "application/json", "Content-Type": "application/json", token },
95
- body: Buffer.from(JSON.stringify({ ip: input.ipAddress }), "utf8"),
97
+ body: Buffer.from(JSON.stringify({ ip: address }), "utf8"),
96
98
  maxResponseBytes: this.client.maxResponseBytes,
97
99
  maxErrorResponseBytes: 4096,
98
100
  timeoutMs: this.client.timeoutMs,
@@ -110,34 +112,48 @@ var LookupIPLocationOperation = class {
110
112
  if (response.status !== 200) return branch("providerRejected", response.status === 401 ? "AUTHENTICATION" : response.status === 403 ? "AUTHORIZATION" : "PROVIDER_REJECTION", `ALAPI rejected lookup with HTTP ${response.status}`);
111
113
  let document;
112
114
  try {
113
- document = JSON.parse(Buffer.from(response.body).toString("utf8"));
115
+ document = lowercaseFields(JSON.parse(Buffer.from(response.body).toString("utf8")));
114
116
  } catch {
115
117
  return branch("invalidResponse", "PROTOCOL", "ALAPI returned invalid lookup data");
116
118
  }
117
119
  if (!isRecord(document) || !Number.isInteger(document.code) || typeof document.success !== "boolean") return branch("invalidResponse", "PROTOCOL", "ALAPI returned invalid lookup data");
120
+ const data = lowercaseFields(document.data);
121
+ if (data != null && (!isRecord(data) || ["country", "province", "city", "district", "isp"].some((field) => data[field] != null && typeof data[field] !== "string"))) {
122
+ return branch("invalidResponse", "PROTOCOL", "ALAPI returned invalid lookup data");
123
+ }
118
124
  const code = document.code;
119
125
  if (code === 429) return retry("RATE_LIMIT", "ALAPI rate limited the lookup", retryAfterMs);
120
126
  if (code !== 200 || !document.success) {
121
127
  const kind = [10001, 10002, 10008].includes(code) ? "AUTHENTICATION" : [10004, 10009].includes(code) ? "AUTHORIZATION" : [10005, 10010].includes(code) ? "QUOTA_EXHAUSTED" : "PROVIDER_REJECTION";
122
128
  return branch("providerRejected", kind, `ALAPI rejected lookup with code ${code}`);
123
129
  }
124
- const data = document.data;
125
- if (!isRecord(data) || !["country", "province", "city", "district", "isp"].every((field) => typeof data[field] === "string") || !data.country.trim() || !data.city.trim()) {
130
+ if (!isRecord(data) || typeof data.country !== "string" || !data.country.trim() || typeof data.city !== "string" || !data.city.trim()) {
126
131
  return branch("invalidResponse", "PROTOCOL", "ALAPI returned incomplete location data");
127
132
  }
128
- return newQueryBranch("found", { country: data.country, province: data.province, city: data.city, district: data.district, operator: data.isp });
133
+ return newQueryBranch("found", { country: data.country, province: String(data.province ?? ""), city: data.city, district: String(data.district ?? ""), operator: String(data.isp ?? "") });
129
134
  }
130
135
  };
131
136
  function isRecord(value) {
132
137
  return typeof value === "object" && value !== null && !Array.isArray(value);
133
138
  }
134
- function isPublicIPv4(value) {
135
- if (typeof value !== "string") return false;
136
- const parts = value.split(".");
137
- if (parts.length !== 4 || parts.some((part) => !/^(0|[1-9]\d{0,2})$/.test(part) || Number(part) > 255)) return false;
139
+ function lowercaseFields(value) {
140
+ return isRecord(value) ? Object.fromEntries(Object.entries(value).map(([key, field]) => [key.toLowerCase(), field])) : value;
141
+ }
142
+ function publicIPv4(value) {
143
+ if (typeof value !== "string" || value.includes("%")) return void 0;
144
+ const version = isIP(value);
145
+ let address = value;
146
+ if (version === 6) {
147
+ const mapped = /^\[::ffff:([a-f0-9]+):([a-f0-9]+)\]$/.exec(new URL("http://[" + value + "]").hostname);
148
+ if (!mapped) return void 0;
149
+ const high = parseInt(mapped[1], 16);
150
+ const low = parseInt(mapped[2], 16);
151
+ address = [high >>> 8, high & 255, low >>> 8, low & 255].join(".");
152
+ } else if (version !== 4) return void 0;
153
+ const parts = address.split(".");
138
154
  const first = Number(parts[0]);
139
155
  const second = Number(parts[1]);
140
- return !parts.every((part) => part === "0") && first !== 10 && first !== 127 && !(first >= 224 && first <= 239) && !(first === 169 && second === 254) && !(first === 172 && second >= 16 && second <= 31) && !(first === 192 && second === 168);
156
+ return !parts.every((part) => part === "0") && first !== 10 && first !== 127 && !(first >= 224 && first <= 239) && !(first === 169 && second === 254) && !(first === 172 && second >= 16 && second <= 31) && !(first === 192 && second === 168) ? address : void 0;
141
157
  }
142
158
  function failure(kind, message) {
143
159
  return { provider: "alapi", operation: "lookupIPLocation", kind, message };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@superdurable/connector-alapi",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "license": "MIT",
5
5
  "description": "Alapi connector for Dex TypeScript applications",
6
6
  "repository": {