@superdurable/connector-doc2x 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/README.md CHANGED
@@ -5,3 +5,5 @@
5
5
  In Dex Web Connections, sign in at [Doc2X Open Platform](https://open.noedgeai.com), open **API Keys**, create or select a platform API Key and store it as the secret `apiKey`. A blank key prevents requests. The Query input `uid` comes from the PDF preupload API. The provider endpoint is fixed; leave timeout and maxResponseBytes blank for their displayed defaults unless an approved network or response requires adjustment.
6
6
 
7
7
  The [runnable Flow](examples/basic/) uses the static connection `doc2x`. `npm run check` replays all Go-recorded provider scenarios, including retry and invalid response behavior. Live authorization and task state remain unverified without a user-controlled connection.
8
+
9
+ The task uid accepts up to 256 UTF-8 bytes. The query returns only the task state and its integer progress percentage. If the provider omits progress, the result uses zero.
@@ -4,7 +4,7 @@
4
4
  "package": {
5
5
  "ecosystem": "npm",
6
6
  "name": "@superdurable/connector-doc2x",
7
- "version": "0.1.0"
7
+ "version": "0.1.1"
8
8
  },
9
9
  "factories": [
10
10
  {
package/dist/index.js CHANGED
@@ -34,8 +34,16 @@ function newGetParseStatusStep(configuration) {
34
34
  }
35
35
 
36
36
  // src/client.ts
37
- import { SecretString, newQueryBranch, newQueryRetry } from "@superdurable/connectors-sdk";
38
- import { BodyTooLargeError, parseRetryAfter, sendProviderRequest } from "@superdurable/connectors-sdk/providerhttp";
37
+ import {
38
+ SecretString,
39
+ newQueryBranch,
40
+ newQueryRetry
41
+ } from "@superdurable/connectors-sdk";
42
+ import {
43
+ BodyTooLargeError,
44
+ parseRetryAfter,
45
+ sendProviderRequest
46
+ } from "@superdurable/connectors-sdk/providerhttp";
39
47
  var endpoint = "https://v2.doc2x.noedgeai.com/api/v2/parse/status";
40
48
  var emptyOutput = { status: "", progress: 0 };
41
49
  var Doc2xClient = class {
@@ -45,7 +53,8 @@ var Doc2xClient = class {
45
53
  if (!config || !credentials) throw new TypeError("Doc2X configuration and credentials are required");
46
54
  this.timeoutMs = config.timeoutMs || 15e3;
47
55
  this.maxResponseBytes = config.maxResponseBytes || 1048576;
48
- if (!Number.isSafeInteger(this.timeoutMs) || this.timeoutMs <= 0 || !Number.isSafeInteger(this.maxResponseBytes) || this.maxResponseBytes <= 0 || this.maxResponseBytes > 16 << 20) throw new TypeError("Doc2X request limits are invalid");
56
+ if (!Number.isSafeInteger(this.timeoutMs) || this.timeoutMs <= 0 || !Number.isSafeInteger(this.maxResponseBytes) || this.maxResponseBytes <= 0 || this.maxResponseBytes > 16 << 20)
57
+ throw new TypeError("Doc2X request limits are invalid");
49
58
  this.send = options.send ?? sendProviderRequest;
50
59
  }
51
60
  credentials;
@@ -65,17 +74,35 @@ var Doc2xClient = class {
65
74
  } catch {
66
75
  return { error: "credential", retryAfterMs: 0 };
67
76
  }
68
- if (!apiKey || apiKey.length > 1024 || /[^\x21-\x7e]/.test(apiKey)) return { error: "credential", retryAfterMs: 0 };
69
- const url = endpoint + "?" + new URLSearchParams({ uid }).toString();
70
- const request = { url, method: "GET", headers: { Authorization: "Bearer " + apiKey, Accept: "application/json" }, timeoutMs: this.timeoutMs, maxResponseBytes: this.maxResponseBytes, addressPolicy: "publicHTTPS", signal: call.context.cancellationSignal };
77
+ if (!apiKey || apiKey.length > 1024 || /[^\x21-\x7e]/.test(apiKey))
78
+ return { error: "credential", retryAfterMs: 0 };
79
+ const url = endpoint + "?" + new URLSearchParams({ uid }).toString().replaceAll("*", "%2A").replaceAll("%7E", "~");
80
+ const request = {
81
+ url,
82
+ method: "GET",
83
+ headers: { Authorization: "Bearer " + apiKey, Accept: "application/json" },
84
+ timeoutMs: this.timeoutMs,
85
+ maxResponseBytes: this.maxResponseBytes,
86
+ addressPolicy: "publicHTTPS",
87
+ signal: call.context.cancellationSignal
88
+ };
71
89
  let response;
72
90
  try {
73
91
  response = await this.send(request);
74
92
  } catch (error) {
75
- if (error instanceof BodyTooLargeError && error.status !== void 0) return { response: { status: error.status, headers: error.headers ?? {}, body: new Uint8Array() }, error: "tooLarge", retryAfterMs: 0 };
93
+ if (error instanceof BodyTooLargeError && error.status !== void 0)
94
+ return {
95
+ response: { status: error.status, headers: error.headers ?? {}, body: new Uint8Array() },
96
+ error: "tooLarge",
97
+ retryAfterMs: parseRetryAfter(header(error.headers ?? {}, "retry-after"))
98
+ };
76
99
  return { error: "transport", retryAfterMs: 0 };
77
100
  }
78
- return { response, error: response.body.length > this.maxResponseBytes ? "tooLarge" : void 0, retryAfterMs: parseRetryAfter(header(response.headers, "retry-after")) };
101
+ return {
102
+ response,
103
+ error: response.body.length > this.maxResponseBytes ? "tooLarge" : void 0,
104
+ retryAfterMs: parseRetryAfter(header(response.headers, "retry-after"))
105
+ };
79
106
  }
80
107
  };
81
108
  var GetParseStatusOperation = class {
@@ -91,29 +118,49 @@ var GetParseStatusOperation = class {
91
118
  /** Classify one status request according to the Go connector contract. */
92
119
  async invoke(call, input) {
93
120
  const uid = input?.uid;
94
- if (typeof uid !== "string" || !uid || uid.length > 256 || uid.trim() !== uid || Array.from(uid).some((character) => character.charCodeAt(0) === 0 || character === "\r" || character === "\n")) return branch("defect", "VALIDATION");
121
+ if (typeof uid !== "string" || !uid || Buffer.byteLength(uid) > 256 || /^\p{White_Space}|\p{White_Space}$/u.test(uid) || Array.from(uid).some(
122
+ (character) => character.charCodeAt(0) === 0 || character === "\r" || character === "\n"
123
+ ))
124
+ return branch("defect", "VALIDATION");
95
125
  const exchange = await this.client.readStatus(call, uid);
96
126
  if (exchange.error === "credential") return branch("defect", "AUTHENTICATION");
97
127
  if (exchange.error === "transport" || !exchange.response) return retry("AVAILABILITY");
98
128
  const { status, body } = exchange.response;
99
129
  if (status === 429) return retry("RATE_LIMIT", exchange.retryAfterMs);
100
130
  if (status === 408 || status >= 500) return retry("AVAILABILITY", exchange.retryAfterMs);
101
- if (status !== 200) return branch("providerRejected", status === 401 ? "AUTHENTICATION" : status === 403 ? "AUTHORIZATION" : "PROVIDER_REJECTION");
131
+ if (status !== 200)
132
+ return branch(
133
+ "providerRejected",
134
+ status === 401 ? "AUTHENTICATION" : status === 403 ? "AUTHORIZATION" : "PROVIDER_REJECTION"
135
+ );
102
136
  if (exchange.error === "tooLarge") return branch("invalidResponse", "RESPONSE_TOO_LARGE");
103
137
  let wire;
104
138
  try {
105
- wire = JSON.parse(new TextDecoder("utf-8", { fatal: true }).decode(body));
139
+ wire = JSON.parse(new TextDecoder("utf-8", { ignoreBOM: true }).decode(body), readProviderField) ?? {};
106
140
  } catch {
107
141
  return branch("invalidResponse", "PROTOCOL");
108
142
  }
109
- if (!isRecord(wire)) return branch("invalidResponse", "PROTOCOL");
143
+ if (!isRecord(wire) || wire.code != null && typeof wire.code !== "string")
144
+ return branch("invalidResponse", "PROTOCOL");
145
+ const data = wire.data ?? {};
146
+ if (!isRecord(data) || data.status != null && typeof data.status !== "string")
147
+ return branch("invalidResponse", "PROTOCOL");
148
+ const progress = data.progress ?? 0n;
149
+ if (typeof progress !== "bigint" || progress < -(1n << 63n) || progress >= 1n << 63n)
150
+ return branch("invalidResponse", "PROTOCOL");
110
151
  if (wire.code !== "success") return branch("providerRejected", "PROVIDER_REJECTION");
111
- if (!isRecord(wire.data) || !["processing", "failed", "success"].includes(String(wire.data.status))) return branch("invalidResponse", "PROTOCOL");
112
- const progress = wire.data.progress ?? 0;
113
- if (!Number.isInteger(progress) || progress < 0 || progress > 100) return branch("invalidResponse", "PROTOCOL");
114
- return newQueryBranch("found", { status: wire.data.status, progress }, {});
152
+ if (typeof data.status !== "string" || !["processing", "failed", "success"].includes(data.status) || progress < 0n || progress > 100n)
153
+ return branch("invalidResponse", "PROTOCOL");
154
+ return newQueryBranch("found", { status: data.status, progress: Number(progress) }, {});
115
155
  }
116
156
  };
157
+ function readProviderField(key, value, context) {
158
+ if (isRecord(value))
159
+ return Object.fromEntries(Object.entries(value).map(([name, field]) => [name.toLowerCase(), field]));
160
+ if (key.toLowerCase() === "progress" && typeof value === "number" && context?.source && /^-?\d{1,19}$/.test(context.source))
161
+ return BigInt(context.source);
162
+ return value;
163
+ }
117
164
  function isRecord(value) {
118
165
  return value !== null && typeof value === "object" && !Array.isArray(value);
119
166
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@superdurable/connector-doc2x",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "license": "MIT",
5
5
  "description": "Doc2X PDF parse status connector for Dex TypeScript applications",
6
6
  "repository": {