@superdurable/connector-apispace 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-apispace",
7
- "version": "0.1.0"
7
+ "version": "0.1.1"
8
8
  },
9
9
  "factories": [
10
10
  {
package/dist/index.js CHANGED
@@ -107,35 +107,61 @@ var GetCurrentWeatherOperation = class {
107
107
  }
108
108
  const early = classifyEarlyStatus(response.status, response.headers);
109
109
  if (early) return early;
110
+ if (response.body.length > this.client.maxResponseBytes) return branch("invalidResponse", "RESPONSE_TOO_LARGE", "APISpace response exceeded the configured limit");
110
111
  if (response.status !== 200) {
111
112
  const kind = response.status === 401 ? "AUTHENTICATION" : response.status === 403 || response.status === 416 ? "AUTHORIZATION" : response.status === 400 || response.status === 404 ? "VALIDATION" : "PROVIDER_REJECTION";
112
113
  return branch("providerRejected", kind, `APISpace rejected weather query with HTTP ${response.status}`);
113
114
  }
114
- let document;
115
+ let wire;
115
116
  try {
116
- document = JSON.parse(new TextDecoder("utf-8", { fatal: true }).decode(response.body));
117
+ wire = fieldsByName(JSON.parse(new TextDecoder().decode(response.body), readProviderNumber));
117
118
  } catch {
118
- return branch("invalidResponse", "PROTOCOL", "APISpace returned an invalid weather response");
119
+ return invalidWeather();
119
120
  }
120
- if (!isRecord(document) || !Number.isInteger(document.status)) return branch("invalidResponse", "PROTOCOL", "APISpace returned an invalid weather response");
121
- if (document.status !== 0) return branch("providerRejected", "PROVIDER_REJECTION", `APISpace rejected weather query with status ${document.status}`);
122
- const result = document.result;
123
- if (!isRecord(result) || !isRecord(result.location) || !isRecord(result.realtime)) return branch("invalidResponse", "PROTOCOL", "APISpace returned an invalid weather response");
124
- const location = result.location;
125
- const realtime = result.realtime;
126
- if (typeof location.name !== "string" || !location.name || typeof realtime.text !== "string" || !realtime.text || typeof realtime.temp !== "number" || !Number.isFinite(realtime.temp) || !Number.isInteger(realtime.rh) || realtime.rh < 0 || realtime.rh > 100 || typeof result.last_update !== "string" || !result.last_update) return branch("invalidResponse", "PROTOCOL", "APISpace returned an invalid weather response");
127
- return newQueryBranch("found", { city: location.name, condition: realtime.text, temperatureCelsius: realtime.temp, humidityPercent: realtime.rh, updatedAtBeijing: result.last_update });
121
+ if (!wire || typeof wire.status !== "bigint") return invalidWeather();
122
+ const result = wire.result == null ? {} : fieldsByName(wire.result);
123
+ if (!result) return invalidWeather();
124
+ const location = result.location == null ? {} : fieldsByName(result.location);
125
+ const observation = result.realtime == null ? {} : fieldsByName(result.realtime);
126
+ if (!location || !observation) return invalidWeather();
127
+ const { name: city } = location;
128
+ const { text: condition, temp: temperature, rh: humidity } = observation;
129
+ const updated = result.last_update;
130
+ if ([city, condition, updated].some((value) => value != null && typeof value !== "string") || temperature != null && (typeof temperature !== "number" || !Number.isFinite(temperature)) || humidity != null && typeof humidity !== "bigint") return invalidWeather();
131
+ if (wire.status !== 0n) {
132
+ return branch("providerRejected", "PROVIDER_REJECTION", `APISpace rejected weather query with status ${wire.status}`);
133
+ }
134
+ if (typeof city !== "string" || !city || typeof condition !== "string" || !condition || typeof temperature !== "number" || typeof humidity !== "bigint" || humidity < 0n || humidity > 100n || typeof updated !== "string" || !updated) return invalidWeather();
135
+ return newQueryBranch("found", {
136
+ city,
137
+ condition,
138
+ temperatureCelsius: temperature,
139
+ humidityPercent: Number(humidity),
140
+ updatedAtBeijing: updated
141
+ });
128
142
  }
129
143
  };
130
144
  function classifyEarlyStatus(status, headers) {
131
- const retryAfterHeader = headers["retry-after"];
145
+ const retryAfterHeader = Object.entries(headers).find(([name]) => name.toLowerCase() === "retry-after")?.[1];
132
146
  const retryAfterMs = parseRetryAfter(Array.isArray(retryAfterHeader) ? retryAfterHeader[0] ?? "" : retryAfterHeader ?? "");
133
147
  if (status === 429 || status === 413) return retry("RATE_LIMIT", "APISpace rate limited weather requests", retryAfterMs);
134
148
  if (status === 408 || status >= 500) return retry("AVAILABILITY", "APISpace weather is temporarily unavailable", retryAfterMs);
135
149
  return void 0;
136
150
  }
137
- function isRecord(value) {
138
- return typeof value === "object" && value !== null && !Array.isArray(value);
151
+ function invalidWeather() {
152
+ return branch("invalidResponse", "PROTOCOL", "APISpace returned an invalid weather response");
153
+ }
154
+ function fieldsByName(value) {
155
+ if (value === null || typeof value !== "object" || Array.isArray(value)) return void 0;
156
+ return Object.fromEntries(Object.entries(value).map(([name, field]) => [name.toLowerCase(), field]));
157
+ }
158
+ function readProviderNumber(key, value, context) {
159
+ if (!["status", "rh"].includes(key.toLowerCase()) || typeof value !== "number") return value;
160
+ const source = context?.source;
161
+ if (source === void 0) return Number.isSafeInteger(value) ? BigInt(value) : false;
162
+ if (!/^-?\d+$/.test(source) || source.length > 20) return false;
163
+ const integer = BigInt(source);
164
+ return integer >= -(1n << 63n) && integer < 1n << 63n ? integer : false;
139
165
  }
140
166
  function isValidAPIToken(value) {
141
167
  return value.length > 0 && value.length <= 2048 && /^[!-~]+$/.test(value);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@superdurable/connector-apispace",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "license": "MIT",
5
5
  "description": "APISpace current weather connector for Dex TypeScript applications",
6
6
  "repository": {