@docmana/sdk 0.2.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/dist/index.js ADDED
@@ -0,0 +1,449 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ Docmana: () => Docmana,
24
+ DocmanaAbortError: () => DocmanaAbortError,
25
+ DocmanaAuthError: () => DocmanaAuthError,
26
+ DocmanaError: () => DocmanaError,
27
+ DocmanaExecutionError: () => DocmanaExecutionError,
28
+ DocmanaNotFoundError: () => DocmanaNotFoundError,
29
+ DocmanaPermissionError: () => DocmanaPermissionError,
30
+ DocmanaRequestError: () => DocmanaRequestError,
31
+ DocmanaTimeoutError: () => DocmanaTimeoutError
32
+ });
33
+ module.exports = __toCommonJS(index_exports);
34
+
35
+ // src/config.ts
36
+ var DEFAULTS = {
37
+ apiBaseUrl: "https://api.docmana.ai",
38
+ tokenEndpoint: "https://4fe70f5b-e013-4f65-9fa7-3109a33beba5.ciamlogin.com/4fe70f5b-e013-4f65-9fa7-3109a33beba5/oauth2/v2.0/token",
39
+ scope: "api://d781e6ba-cc08-4618-8099-ad968abd2b9e/.default",
40
+ timeoutMs: 3e5,
41
+ pollIntervalMs: 2e3
42
+ };
43
+ function resolveConfig(config) {
44
+ if (!config.clientId) throw new Error("DocmanaConfig.clientId is required");
45
+ if (!config.clientSecret) throw new Error("DocmanaConfig.clientSecret is required");
46
+ const apiBaseUrl = (config.apiBaseUrl ?? DEFAULTS.apiBaseUrl).replace(/\/+$/, "");
47
+ return {
48
+ clientId: config.clientId,
49
+ clientSecret: config.clientSecret,
50
+ apiBaseUrl,
51
+ tokenEndpoint: config.tokenEndpoint ?? DEFAULTS.tokenEndpoint,
52
+ scope: config.scope ?? DEFAULTS.scope,
53
+ timeoutMs: config.timeoutMs ?? DEFAULTS.timeoutMs,
54
+ pollIntervalMs: config.pollIntervalMs ?? DEFAULTS.pollIntervalMs,
55
+ fetchImpl: config.fetch ?? fetch,
56
+ headers: config.headers ?? {},
57
+ tokenCache: config.tokenCache
58
+ };
59
+ }
60
+
61
+ // src/errors.ts
62
+ var DocmanaError = class extends Error {
63
+ status;
64
+ requestId;
65
+ code;
66
+ constructor(message, opts = {}) {
67
+ super(message);
68
+ this.name = new.target.name;
69
+ this.status = opts.status;
70
+ this.requestId = opts.requestId;
71
+ this.code = opts.code ?? "docmana_error";
72
+ Object.setPrototypeOf(this, new.target.prototype);
73
+ }
74
+ };
75
+ var DocmanaAuthError = class extends DocmanaError {
76
+ constructor(message, opts = {}) {
77
+ super(message, { ...opts, code: "auth_error" });
78
+ }
79
+ };
80
+ var DocmanaPermissionError = class extends DocmanaError {
81
+ constructor(message, opts = {}) {
82
+ super(message, { ...opts, code: "permission_error" });
83
+ }
84
+ };
85
+ var DocmanaNotFoundError = class extends DocmanaError {
86
+ constructor(message, opts = {}) {
87
+ super(message, { ...opts, code: "not_found" });
88
+ }
89
+ };
90
+ var DocmanaRequestError = class extends DocmanaError {
91
+ constructor(message, opts = {}) {
92
+ super(message, { ...opts, code: "bad_request" });
93
+ }
94
+ };
95
+ var DocmanaExecutionError = class extends DocmanaError {
96
+ errors;
97
+ constructor(message, errors = []) {
98
+ super(message, { code: "execution_failed" });
99
+ this.errors = errors;
100
+ }
101
+ };
102
+ var DocmanaTimeoutError = class extends DocmanaError {
103
+ constructor(message) {
104
+ super(message, { code: "timeout" });
105
+ }
106
+ };
107
+ var DocmanaAbortError = class extends DocmanaError {
108
+ constructor(message) {
109
+ super(message, { code: "aborted" });
110
+ }
111
+ };
112
+ function errorFromResponse(status, message, requestId) {
113
+ const opts = { status, requestId };
114
+ switch (status) {
115
+ case 400:
116
+ return new DocmanaRequestError(message, opts);
117
+ case 401:
118
+ return new DocmanaAuthError(message, opts);
119
+ case 403:
120
+ return new DocmanaPermissionError(message, opts);
121
+ case 404:
122
+ return new DocmanaNotFoundError(message, opts);
123
+ default:
124
+ return new DocmanaError(message, opts);
125
+ }
126
+ }
127
+
128
+ // src/auth/token-manager.ts
129
+ var SAFETY_MARGIN_MS = 6e4;
130
+ var TokenManager = class {
131
+ constructor(opts) {
132
+ this.opts = opts;
133
+ }
134
+ opts;
135
+ token = null;
136
+ expiresAt = 0;
137
+ inflight = null;
138
+ invalidate() {
139
+ this.token = null;
140
+ this.expiresAt = 0;
141
+ void this.opts.tokenCache?.clear().catch(() => void 0);
142
+ }
143
+ async getToken() {
144
+ if (this.token && Date.now() < this.expiresAt) return this.token;
145
+ const cached = await this.readCachedToken();
146
+ if (cached) return cached;
147
+ if (this.inflight) return this.inflight;
148
+ this.inflight = this.acquire().finally(() => {
149
+ this.inflight = null;
150
+ });
151
+ return this.inflight;
152
+ }
153
+ async acquire() {
154
+ const body = new URLSearchParams({
155
+ grant_type: "client_credentials",
156
+ client_id: this.opts.clientId,
157
+ client_secret: this.opts.clientSecret,
158
+ scope: this.opts.scope
159
+ });
160
+ const res = await this.opts.fetchImpl(this.opts.tokenEndpoint, {
161
+ method: "POST",
162
+ headers: { "content-type": "application/x-www-form-urlencoded" },
163
+ body: body.toString()
164
+ });
165
+ if (!res.ok) {
166
+ throw new DocmanaAuthError("Failed to acquire Docmana access token", { status: res.status });
167
+ }
168
+ const json = await res.json();
169
+ if (typeof json.access_token !== "string" || json.access_token.length === 0 || typeof json.expires_in !== "number" || !Number.isFinite(json.expires_in)) {
170
+ throw new DocmanaAuthError("Malformed token response from Docmana CIAM", {
171
+ status: res.status
172
+ });
173
+ }
174
+ this.token = json.access_token;
175
+ this.expiresAt = Date.now() + json.expires_in * 1e3 - SAFETY_MARGIN_MS;
176
+ await this.writeCachedToken().catch(() => void 0);
177
+ return this.token;
178
+ }
179
+ async readCachedToken() {
180
+ if (!this.opts.tokenCache) return null;
181
+ let entry;
182
+ try {
183
+ entry = await this.opts.tokenCache.read();
184
+ } catch {
185
+ return null;
186
+ }
187
+ if (!entry) return null;
188
+ if (entry.clientId !== this.opts.clientId || entry.tokenEndpoint !== this.opts.tokenEndpoint || entry.scope !== this.opts.scope || Date.now() >= entry.expiresAt) {
189
+ await this.opts.tokenCache.clear().catch(() => void 0);
190
+ return null;
191
+ }
192
+ this.token = entry.accessToken;
193
+ this.expiresAt = entry.expiresAt;
194
+ return this.token;
195
+ }
196
+ async writeCachedToken() {
197
+ if (!this.opts.tokenCache || !this.token) return;
198
+ await this.opts.tokenCache.write({
199
+ accessToken: this.token,
200
+ expiresAt: this.expiresAt,
201
+ clientId: this.opts.clientId,
202
+ tokenEndpoint: this.opts.tokenEndpoint,
203
+ scope: this.opts.scope
204
+ });
205
+ }
206
+ };
207
+
208
+ // src/http/http-client.ts
209
+ var HttpClient = class {
210
+ constructor(opts) {
211
+ this.opts = opts;
212
+ }
213
+ opts;
214
+ async requestJson(method, path, options = {}) {
215
+ const res = await this.requestRaw(method, path, options);
216
+ const text = await res.text();
217
+ if (!text) return {};
218
+ try {
219
+ return JSON.parse(text);
220
+ } catch {
221
+ throw new DocmanaError("Invalid JSON response from Docmana", { status: res.status });
222
+ }
223
+ }
224
+ async requestRaw(method, path, options = {}) {
225
+ let res = await this.send(method, path, options);
226
+ if (res.status === 401) {
227
+ this.opts.tokenManager.invalidate();
228
+ res = await this.send(method, path, options);
229
+ }
230
+ if (!res.ok) {
231
+ const requestId = res.headers.get("x-request-id") ?? void 0;
232
+ const message = await this.extractMessage(res);
233
+ throw errorFromResponse(res.status, message, requestId);
234
+ }
235
+ return res;
236
+ }
237
+ async send(method, path, options) {
238
+ const token = await this.opts.tokenManager.getToken();
239
+ const url = new URL(this.opts.apiBaseUrl + path);
240
+ for (const [k, v] of Object.entries(options.query ?? {})) url.searchParams.set(k, v);
241
+ const headers = {
242
+ ...this.opts.headers ?? {},
243
+ authorization: `Bearer ${token}`,
244
+ ...options.headers ?? {}
245
+ };
246
+ try {
247
+ return await this.opts.fetchImpl(url, {
248
+ method,
249
+ headers,
250
+ body: options.body,
251
+ signal: options.signal
252
+ });
253
+ } catch (err) {
254
+ if (options.signal?.aborted || err instanceof Error && err.name === "AbortError") {
255
+ throw new DocmanaAbortError("Request aborted");
256
+ }
257
+ throw err;
258
+ }
259
+ }
260
+ async extractMessage(res) {
261
+ try {
262
+ const data = await res.clone().json();
263
+ return data.message ?? data.error ?? res.statusText ?? `HTTP ${res.status}`;
264
+ } catch {
265
+ return res.statusText ?? `HTTP ${res.status}`;
266
+ }
267
+ }
268
+ };
269
+
270
+ // src/files/resolve-input.ts
271
+ var import_promises = require("fs/promises");
272
+ var import_node_path = require("path");
273
+ var CONTENT_TYPES = {
274
+ ".pdf": "application/pdf",
275
+ ".png": "image/png",
276
+ ".jpg": "image/jpeg",
277
+ ".jpeg": "image/jpeg",
278
+ ".tif": "image/tiff",
279
+ ".tiff": "image/tiff",
280
+ ".txt": "text/plain",
281
+ ".json": "application/json"
282
+ };
283
+ function contentTypeFor(name) {
284
+ const dot = name.lastIndexOf(".");
285
+ if (dot < 0) return "application/octet-stream";
286
+ const ext = name.slice(dot).toLowerCase();
287
+ return CONTENT_TYPES[ext] ?? "application/octet-stream";
288
+ }
289
+ function isReadable(x) {
290
+ return typeof x === "object" && x !== null && typeof x.pipe === "function";
291
+ }
292
+ function isPathInput(x) {
293
+ return typeof x === "object" && x !== null && typeof x.path === "string";
294
+ }
295
+ async function streamToBuffer(stream) {
296
+ const chunks = [];
297
+ for await (const chunk of stream) chunks.push(Buffer.from(chunk));
298
+ return Buffer.concat(chunks);
299
+ }
300
+ async function resolveOne(input, fallbackName) {
301
+ if (typeof input === "string" || isPathInput(input)) {
302
+ const path = typeof input === "string" ? input : input.path;
303
+ const data = await (0, import_promises.readFile)(path);
304
+ const filename = (0, import_node_path.basename)(path);
305
+ return { data: new Blob([data]), filename, contentType: contentTypeFor(filename) };
306
+ }
307
+ if (input instanceof Uint8Array) {
308
+ return {
309
+ data: new Blob([input]),
310
+ filename: fallbackName,
311
+ contentType: contentTypeFor(fallbackName)
312
+ };
313
+ }
314
+ if (isReadable(input)) {
315
+ const buf = await streamToBuffer(input);
316
+ return {
317
+ data: new Blob([buf]),
318
+ filename: fallbackName,
319
+ contentType: contentTypeFor(fallbackName)
320
+ };
321
+ }
322
+ throw new Error("Unsupported file input type");
323
+ }
324
+ async function resolveInputs(input) {
325
+ const list = input.files ?? (input.file !== void 0 ? [input.file] : []);
326
+ if (list.length === 0) throw new Error("runFlow requires `file` or `files`");
327
+ const fallback = input.fileName ?? "upload.bin";
328
+ return Promise.all(list.map((item) => resolveOne(item, fallback)));
329
+ }
330
+
331
+ // src/api/upload.ts
332
+ async function uploadFiles(http, parts, signal) {
333
+ const ids = [];
334
+ for (const part of parts) {
335
+ const form = new FormData();
336
+ form.append("files", new File([part.data], part.filename, { type: part.contentType }));
337
+ const res = await http.requestJson("POST", "/upload", { body: form, signal });
338
+ ids.push(res.id);
339
+ }
340
+ return ids;
341
+ }
342
+
343
+ // src/api/run-flow.ts
344
+ async function runFlow(http, flowId, uuidFiles, signal, once = false) {
345
+ const path = once ? `/flows/run_once_flow/${flowId}` : `/flows/run_flow/${flowId}`;
346
+ const res = await http.requestJson("POST", path, {
347
+ query: { async_mode: "true" },
348
+ headers: { "content-type": "application/json" },
349
+ body: JSON.stringify({ uuid_files: uuidFiles }),
350
+ signal
351
+ });
352
+ return res.execution_result_id;
353
+ }
354
+
355
+ // src/api/execution-status.ts
356
+ async function getStatus(http, executionResultId, signal) {
357
+ return http.requestJson("GET", `/flows/execution-status/${executionResultId}`, { signal });
358
+ }
359
+
360
+ // src/api/execution-result.ts
361
+ async function getResult(http, executionResultId, signal) {
362
+ return http.requestJson("GET", `/flows/execution-result/${executionResultId}`, { signal });
363
+ }
364
+
365
+ // src/polling/poll.ts
366
+ var TERMINAL = /* @__PURE__ */ new Set(["Completed", "Failed"]);
367
+ var defaultSleep = (ms) => new Promise((r) => setTimeout(r, ms));
368
+ async function pollUntilTerminal(opts) {
369
+ const sleep = opts.sleep ?? defaultSleep;
370
+ const start = Date.now();
371
+ const elapsed = opts.elapsed ?? (() => Date.now() - start);
372
+ for (; ; ) {
373
+ if (opts.signal?.aborted) throw new DocmanaAbortError("Polling aborted");
374
+ const status = await opts.check();
375
+ if (TERMINAL.has(status)) return status;
376
+ if (elapsed() >= opts.timeoutMs) {
377
+ throw new DocmanaTimeoutError(`Execution did not finish within ${opts.timeoutMs}ms`);
378
+ }
379
+ await sleep(opts.intervalMs);
380
+ }
381
+ }
382
+
383
+ // src/client.ts
384
+ var Docmana = class {
385
+ config;
386
+ http;
387
+ constructor(config) {
388
+ this.config = resolveConfig(config);
389
+ const tokenManager = new TokenManager({
390
+ clientId: this.config.clientId,
391
+ clientSecret: this.config.clientSecret,
392
+ tokenEndpoint: this.config.tokenEndpoint,
393
+ scope: this.config.scope,
394
+ fetchImpl: this.config.fetchImpl,
395
+ tokenCache: this.config.tokenCache
396
+ });
397
+ this.http = new HttpClient({
398
+ apiBaseUrl: this.config.apiBaseUrl,
399
+ fetchImpl: this.config.fetchImpl,
400
+ tokenManager,
401
+ headers: this.config.headers
402
+ });
403
+ }
404
+ async runFlowAsync(flowId, input) {
405
+ const parts = await resolveInputs(input);
406
+ const fileIds = await uploadFiles(this.http, parts, input.signal);
407
+ const executionResultId = await runFlow(
408
+ this.http,
409
+ flowId,
410
+ fileIds,
411
+ input.signal,
412
+ input.once ?? false
413
+ );
414
+ return { executionResultId, fileIds };
415
+ }
416
+ async getStatus(executionResultId, signal) {
417
+ return getStatus(this.http, executionResultId, signal);
418
+ }
419
+ async getResult(executionResultId, signal) {
420
+ return getResult(this.http, executionResultId, signal);
421
+ }
422
+ async runFlow(flowId, input) {
423
+ const { executionResultId } = await this.runFlowAsync(flowId, input);
424
+ await pollUntilTerminal({
425
+ check: async () => (await this.getStatus(executionResultId, input.signal)).status,
426
+ intervalMs: input.pollIntervalMs ?? this.config.pollIntervalMs,
427
+ timeoutMs: input.timeoutMs ?? this.config.timeoutMs,
428
+ signal: input.signal
429
+ });
430
+ const result = await this.getResult(executionResultId, input.signal);
431
+ if (result.status === "Failed") {
432
+ throw new DocmanaExecutionError(`Flow ${flowId} execution failed`, result.errors ?? []);
433
+ }
434
+ return result;
435
+ }
436
+ };
437
+ // Annotate the CommonJS export names for ESM import in node:
438
+ 0 && (module.exports = {
439
+ Docmana,
440
+ DocmanaAbortError,
441
+ DocmanaAuthError,
442
+ DocmanaError,
443
+ DocmanaExecutionError,
444
+ DocmanaNotFoundError,
445
+ DocmanaPermissionError,
446
+ DocmanaRequestError,
447
+ DocmanaTimeoutError
448
+ });
449
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts","../src/config.ts","../src/errors.ts","../src/auth/token-manager.ts","../src/http/http-client.ts","../src/files/resolve-input.ts","../src/api/upload.ts","../src/api/run-flow.ts","../src/api/execution-status.ts","../src/api/execution-result.ts","../src/polling/poll.ts","../src/client.ts"],"sourcesContent":["export { Docmana } from \"./client.js\";\nexport {\n DocmanaError,\n DocmanaAuthError,\n DocmanaRequestError,\n DocmanaPermissionError,\n DocmanaNotFoundError,\n DocmanaExecutionError,\n DocmanaTimeoutError,\n DocmanaAbortError,\n} from \"./errors.js\";\nexport type {\n DocmanaConfig,\n RunFlowInput,\n FileInput,\n ExecutionResult,\n ExecutionStatus,\n} from \"./types.js\";\n","import type { DocmanaConfig } from \"./types.js\";\nimport type { TokenCache } from \"./types.js\";\n\nexport const DEFAULTS = {\n apiBaseUrl: \"https://api.docmana.ai\",\n tokenEndpoint:\n \"https://4fe70f5b-e013-4f65-9fa7-3109a33beba5.ciamlogin.com/4fe70f5b-e013-4f65-9fa7-3109a33beba5/oauth2/v2.0/token\",\n scope: \"api://d781e6ba-cc08-4618-8099-ad968abd2b9e/.default\",\n timeoutMs: 300_000,\n pollIntervalMs: 2_000,\n} as const;\n\nexport interface ResolvedConfig {\n clientId: string;\n clientSecret: string;\n apiBaseUrl: string;\n tokenEndpoint: string;\n scope: string;\n timeoutMs: number;\n pollIntervalMs: number;\n fetchImpl: typeof fetch;\n headers: Record<string, string>;\n tokenCache?: TokenCache;\n}\n\nexport function resolveConfig(config: DocmanaConfig): ResolvedConfig {\n if (!config.clientId) throw new Error(\"DocmanaConfig.clientId is required\");\n if (!config.clientSecret) throw new Error(\"DocmanaConfig.clientSecret is required\");\n const apiBaseUrl = (config.apiBaseUrl ?? DEFAULTS.apiBaseUrl).replace(/\\/+$/, \"\");\n return {\n clientId: config.clientId,\n clientSecret: config.clientSecret,\n apiBaseUrl,\n tokenEndpoint: config.tokenEndpoint ?? DEFAULTS.tokenEndpoint,\n scope: config.scope ?? DEFAULTS.scope,\n timeoutMs: config.timeoutMs ?? DEFAULTS.timeoutMs,\n pollIntervalMs: config.pollIntervalMs ?? DEFAULTS.pollIntervalMs,\n fetchImpl: config.fetch ?? fetch,\n headers: config.headers ?? {},\n tokenCache: config.tokenCache,\n };\n}\n","export class DocmanaError extends Error {\n readonly status?: number;\n readonly requestId?: string;\n readonly code: string;\n constructor(message: string, opts: { status?: number; requestId?: string; code?: string } = {}) {\n super(message);\n this.name = new.target.name;\n this.status = opts.status;\n this.requestId = opts.requestId;\n this.code = opts.code ?? \"docmana_error\";\n Object.setPrototypeOf(this, new.target.prototype);\n }\n}\n\nexport class DocmanaAuthError extends DocmanaError {\n constructor(message: string, opts: { status?: number; requestId?: string } = {}) {\n super(message, { ...opts, code: \"auth_error\" });\n }\n}\n\nexport class DocmanaPermissionError extends DocmanaError {\n constructor(message: string, opts: { status?: number; requestId?: string } = {}) {\n super(message, { ...opts, code: \"permission_error\" });\n }\n}\n\nexport class DocmanaNotFoundError extends DocmanaError {\n constructor(message: string, opts: { status?: number; requestId?: string } = {}) {\n super(message, { ...opts, code: \"not_found\" });\n }\n}\n\nexport class DocmanaRequestError extends DocmanaError {\n constructor(message: string, opts: { status?: number; requestId?: string } = {}) {\n super(message, { ...opts, code: \"bad_request\" });\n }\n}\n\nexport class DocmanaExecutionError extends DocmanaError {\n readonly errors: unknown[];\n constructor(message: string, errors: unknown[] = []) {\n super(message, { code: \"execution_failed\" });\n this.errors = errors;\n }\n}\n\nexport class DocmanaTimeoutError extends DocmanaError {\n constructor(message: string) {\n super(message, { code: \"timeout\" });\n }\n}\n\nexport class DocmanaAbortError extends DocmanaError {\n constructor(message: string) {\n super(message, { code: \"aborted\" });\n }\n}\n\nexport function errorFromResponse(\n status: number,\n message: string,\n requestId?: string,\n): DocmanaError {\n const opts = { status, requestId };\n switch (status) {\n case 400:\n return new DocmanaRequestError(message, opts);\n case 401:\n return new DocmanaAuthError(message, opts);\n case 403:\n return new DocmanaPermissionError(message, opts);\n case 404:\n return new DocmanaNotFoundError(message, opts);\n default:\n return new DocmanaError(message, opts);\n }\n}\n","import { DocmanaAuthError } from \"../errors.js\";\nimport type { TokenCache, TokenCacheEntry } from \"../types.js\";\n\nconst SAFETY_MARGIN_MS = 60_000;\n\nexport interface TokenManagerOptions {\n clientId: string;\n clientSecret: string;\n tokenEndpoint: string;\n scope: string;\n fetchImpl: typeof fetch;\n tokenCache?: TokenCache;\n}\n\nexport class TokenManager {\n private token: string | null = null;\n private expiresAt = 0;\n private inflight: Promise<string> | null = null;\n\n constructor(private readonly opts: TokenManagerOptions) {}\n\n invalidate(): void {\n this.token = null;\n this.expiresAt = 0;\n void this.opts.tokenCache?.clear().catch(() => undefined);\n }\n\n async getToken(): Promise<string> {\n if (this.token && Date.now() < this.expiresAt) return this.token;\n const cached = await this.readCachedToken();\n if (cached) return cached;\n if (this.inflight) return this.inflight;\n this.inflight = this.acquire().finally(() => {\n this.inflight = null;\n });\n return this.inflight;\n }\n\n private async acquire(): Promise<string> {\n const body = new URLSearchParams({\n grant_type: \"client_credentials\",\n client_id: this.opts.clientId,\n client_secret: this.opts.clientSecret,\n scope: this.opts.scope,\n });\n const res = await this.opts.fetchImpl(this.opts.tokenEndpoint, {\n method: \"POST\",\n headers: { \"content-type\": \"application/x-www-form-urlencoded\" },\n body: body.toString(),\n });\n if (!res.ok) {\n // Do NOT include the response body verbatim — avoid leaking secrets/echoes.\n throw new DocmanaAuthError(\"Failed to acquire Docmana access token\", { status: res.status });\n }\n const json = (await res.json()) as Partial<{ access_token: string; expires_in: number }>;\n if (\n typeof json.access_token !== \"string\" ||\n json.access_token.length === 0 ||\n typeof json.expires_in !== \"number\" ||\n !Number.isFinite(json.expires_in)\n ) {\n throw new DocmanaAuthError(\"Malformed token response from Docmana CIAM\", {\n status: res.status,\n });\n }\n this.token = json.access_token;\n this.expiresAt = Date.now() + json.expires_in * 1000 - SAFETY_MARGIN_MS;\n await this.writeCachedToken().catch(() => undefined);\n return this.token;\n }\n\n private async readCachedToken(): Promise<string | null> {\n if (!this.opts.tokenCache) return null;\n let entry: TokenCacheEntry | null;\n try {\n entry = await this.opts.tokenCache.read();\n } catch {\n return null;\n }\n if (!entry) return null;\n if (\n entry.clientId !== this.opts.clientId ||\n entry.tokenEndpoint !== this.opts.tokenEndpoint ||\n entry.scope !== this.opts.scope ||\n Date.now() >= entry.expiresAt\n ) {\n await this.opts.tokenCache.clear().catch(() => undefined);\n return null;\n }\n this.token = entry.accessToken;\n this.expiresAt = entry.expiresAt;\n return this.token;\n }\n\n private async writeCachedToken(): Promise<void> {\n if (!this.opts.tokenCache || !this.token) return;\n await this.opts.tokenCache.write({\n accessToken: this.token,\n expiresAt: this.expiresAt,\n clientId: this.opts.clientId,\n tokenEndpoint: this.opts.tokenEndpoint,\n scope: this.opts.scope,\n });\n }\n}\n","import type { TokenManager } from \"../auth/token-manager.js\";\nimport { DocmanaError, DocmanaAbortError, errorFromResponse } from \"../errors.js\";\n\nexport interface HttpClientOptions {\n apiBaseUrl: string;\n fetchImpl: typeof fetch;\n tokenManager: TokenManager;\n headers?: Record<string, string>;\n}\n\ntype FetchBody = NonNullable<Parameters<typeof fetch>[1]>[\"body\"];\n\nexport interface RequestOptions {\n query?: Record<string, string>;\n body?: FetchBody;\n headers?: Record<string, string>;\n signal?: AbortSignal;\n}\n\nexport class HttpClient {\n constructor(private readonly opts: HttpClientOptions) {}\n\n async requestJson<T>(method: string, path: string, options: RequestOptions = {}): Promise<T> {\n const res = await this.requestRaw(method, path, options);\n const text = await res.text();\n if (!text) return {} as T;\n try {\n return JSON.parse(text) as T;\n } catch {\n throw new DocmanaError(\"Invalid JSON response from Docmana\", { status: res.status });\n }\n }\n\n async requestRaw(method: string, path: string, options: RequestOptions = {}): Promise<Response> {\n let res = await this.send(method, path, options);\n if (res.status === 401) {\n this.opts.tokenManager.invalidate();\n res = await this.send(method, path, options);\n }\n if (!res.ok) {\n const requestId = res.headers.get(\"x-request-id\") ?? undefined;\n const message = await this.extractMessage(res);\n throw errorFromResponse(res.status, message, requestId);\n }\n return res;\n }\n\n private async send(method: string, path: string, options: RequestOptions): Promise<Response> {\n const token = await this.opts.tokenManager.getToken();\n const url = new URL(this.opts.apiBaseUrl + path);\n for (const [k, v] of Object.entries(options.query ?? {})) url.searchParams.set(k, v);\n const headers: Record<string, string> = {\n ...(this.opts.headers ?? {}),\n authorization: `Bearer ${token}`,\n ...(options.headers ?? {}),\n };\n try {\n return await this.opts.fetchImpl(url, {\n method,\n headers,\n body: options.body,\n signal: options.signal,\n });\n } catch (err) {\n if (options.signal?.aborted || (err instanceof Error && err.name === \"AbortError\")) {\n throw new DocmanaAbortError(\"Request aborted\");\n }\n throw err;\n }\n }\n\n private async extractMessage(res: Response): Promise<string> {\n try {\n const data = (await res.clone().json()) as { message?: string; error?: string };\n return data.message ?? data.error ?? res.statusText ?? `HTTP ${res.status}`;\n } catch {\n return res.statusText ?? `HTTP ${res.status}`;\n }\n }\n}\n","import { readFile } from \"node:fs/promises\";\nimport { basename } from \"node:path\";\nimport type { Readable } from \"node:stream\";\nimport type { FileInput, RunFlowInput } from \"../types.js\";\n\nexport interface ResolvedPart {\n data: Blob;\n filename: string;\n contentType: string;\n}\n\nconst CONTENT_TYPES: Record<string, string> = {\n \".pdf\": \"application/pdf\",\n \".png\": \"image/png\",\n \".jpg\": \"image/jpeg\",\n \".jpeg\": \"image/jpeg\",\n \".tif\": \"image/tiff\",\n \".tiff\": \"image/tiff\",\n \".txt\": \"text/plain\",\n \".json\": \"application/json\",\n};\n\nfunction contentTypeFor(name: string): string {\n const dot = name.lastIndexOf(\".\");\n if (dot < 0) return \"application/octet-stream\";\n const ext = name.slice(dot).toLowerCase();\n return CONTENT_TYPES[ext] ?? \"application/octet-stream\";\n}\n\nfunction isReadable(x: unknown): x is Readable {\n return typeof x === \"object\" && x !== null && typeof (x as Readable).pipe === \"function\";\n}\n\nfunction isPathInput(x: unknown): x is { path: string } {\n return typeof x === \"object\" && x !== null && typeof (x as { path?: unknown }).path === \"string\";\n}\n\nasync function streamToBuffer(stream: Readable): Promise<Buffer> {\n const chunks: Buffer[] = [];\n for await (const chunk of stream) chunks.push(Buffer.from(chunk));\n return Buffer.concat(chunks);\n}\n\nasync function resolveOne(input: FileInput, fallbackName: string): Promise<ResolvedPart> {\n if (typeof input === \"string\" || isPathInput(input)) {\n const path = typeof input === \"string\" ? input : input.path;\n const data = await readFile(path);\n const filename = basename(path);\n return { data: new Blob([data]), filename, contentType: contentTypeFor(filename) };\n }\n if (input instanceof Uint8Array) {\n return {\n data: new Blob([input]),\n filename: fallbackName,\n contentType: contentTypeFor(fallbackName),\n };\n }\n if (isReadable(input)) {\n const buf = await streamToBuffer(input);\n return {\n data: new Blob([buf]),\n filename: fallbackName,\n contentType: contentTypeFor(fallbackName),\n };\n }\n throw new Error(\"Unsupported file input type\");\n}\n\nexport async function resolveInputs(input: RunFlowInput): Promise<ResolvedPart[]> {\n const list: FileInput[] = input.files ?? (input.file !== undefined ? [input.file] : []);\n if (list.length === 0) throw new Error(\"runFlow requires `file` or `files`\");\n const fallback = input.fileName ?? \"upload.bin\";\n return Promise.all(list.map((item) => resolveOne(item, fallback)));\n}\n","import type { HttpClient } from \"../http/http-client.js\";\nimport type { ResolvedPart } from \"../files/resolve-input.js\";\n\nexport async function uploadFiles(\n http: HttpClient,\n parts: ResolvedPart[],\n signal?: AbortSignal,\n): Promise<string[]> {\n const ids: string[] = [];\n for (const part of parts) {\n const form = new FormData();\n form.append(\"files\", new File([part.data], part.filename, { type: part.contentType }));\n const res = await http.requestJson<{ id: string }>(\"POST\", \"/upload\", { body: form, signal });\n ids.push(res.id);\n }\n return ids;\n}\n","import type { HttpClient } from \"../http/http-client.js\";\n\nexport async function runFlow(\n http: HttpClient,\n flowId: string,\n uuidFiles: string[],\n signal?: AbortSignal,\n once = false,\n): Promise<string> {\n const path = once ? `/flows/run_once_flow/${flowId}` : `/flows/run_flow/${flowId}`;\n const res = await http.requestJson<{ execution_result_id: string }>(\"POST\", path, {\n query: { async_mode: \"true\" },\n headers: { \"content-type\": \"application/json\" },\n body: JSON.stringify({ uuid_files: uuidFiles }),\n signal,\n });\n return res.execution_result_id;\n}\n","import type { HttpClient } from \"../http/http-client.js\";\nimport type { ExecutionStatus } from \"../types.js\";\n\nexport async function getStatus(\n http: HttpClient,\n executionResultId: string,\n signal?: AbortSignal,\n): Promise<{ status: ExecutionStatus | string }> {\n return http.requestJson(\"GET\", `/flows/execution-status/${executionResultId}`, { signal });\n}\n","import type { HttpClient } from \"../http/http-client.js\";\nimport type { ExecutionResult } from \"../types.js\";\n\nexport async function getResult(\n http: HttpClient,\n executionResultId: string,\n signal?: AbortSignal,\n): Promise<ExecutionResult> {\n return http.requestJson(\"GET\", `/flows/execution-result/${executionResultId}`, { signal });\n}\n","import { DocmanaTimeoutError, DocmanaAbortError } from \"../errors.js\";\n\nconst TERMINAL = new Set([\"Completed\", \"Failed\"]);\n\nexport interface PollOptions {\n check: () => Promise<string>;\n intervalMs: number;\n timeoutMs: number;\n sleep?: (ms: number) => Promise<void>;\n elapsed?: () => number;\n signal?: AbortSignal;\n}\n\nconst defaultSleep = (ms: number) => new Promise<void>((r) => setTimeout(r, ms));\n\nexport async function pollUntilTerminal(opts: PollOptions): Promise<string> {\n const sleep = opts.sleep ?? defaultSleep;\n const start = Date.now();\n const elapsed = opts.elapsed ?? (() => Date.now() - start);\n for (;;) {\n if (opts.signal?.aborted) throw new DocmanaAbortError(\"Polling aborted\");\n const status = await opts.check();\n if (TERMINAL.has(status)) return status;\n if (elapsed() >= opts.timeoutMs) {\n throw new DocmanaTimeoutError(`Execution did not finish within ${opts.timeoutMs}ms`);\n }\n await sleep(opts.intervalMs);\n }\n}\n","import { resolveConfig } from \"./config.js\";\nimport type { ResolvedConfig } from \"./config.js\";\nimport type { DocmanaConfig, ExecutionResult, ExecutionStatus, RunFlowInput } from \"./types.js\";\nimport { TokenManager } from \"./auth/token-manager.js\";\nimport { HttpClient } from \"./http/http-client.js\";\nimport { resolveInputs } from \"./files/resolve-input.js\";\nimport { uploadFiles } from \"./api/upload.js\";\nimport { runFlow as runFlowApi } from \"./api/run-flow.js\";\nimport { getStatus as getStatusApi } from \"./api/execution-status.js\";\nimport { getResult as getResultApi } from \"./api/execution-result.js\";\nimport { pollUntilTerminal } from \"./polling/poll.js\";\nimport { DocmanaExecutionError } from \"./errors.js\";\n\nexport class Docmana {\n private readonly config: ResolvedConfig;\n private readonly http: HttpClient;\n\n constructor(config: DocmanaConfig) {\n this.config = resolveConfig(config);\n const tokenManager = new TokenManager({\n clientId: this.config.clientId,\n clientSecret: this.config.clientSecret,\n tokenEndpoint: this.config.tokenEndpoint,\n scope: this.config.scope,\n fetchImpl: this.config.fetchImpl,\n tokenCache: this.config.tokenCache,\n });\n this.http = new HttpClient({\n apiBaseUrl: this.config.apiBaseUrl,\n fetchImpl: this.config.fetchImpl,\n tokenManager,\n headers: this.config.headers,\n });\n }\n\n async runFlowAsync(\n flowId: string,\n input: RunFlowInput,\n ): Promise<{ executionResultId: string; fileIds: string[] }> {\n const parts = await resolveInputs(input);\n const fileIds = await uploadFiles(this.http, parts, input.signal);\n const executionResultId = await runFlowApi(\n this.http,\n flowId,\n fileIds,\n input.signal,\n input.once ?? false,\n );\n return { executionResultId, fileIds };\n }\n\n async getStatus(\n executionResultId: string,\n signal?: AbortSignal,\n ): Promise<{ status: ExecutionStatus | string }> {\n return getStatusApi(this.http, executionResultId, signal);\n }\n\n async getResult(executionResultId: string, signal?: AbortSignal): Promise<ExecutionResult> {\n return getResultApi(this.http, executionResultId, signal);\n }\n\n async runFlow(flowId: string, input: RunFlowInput): Promise<ExecutionResult> {\n const { executionResultId } = await this.runFlowAsync(flowId, input);\n await pollUntilTerminal({\n check: async () => (await this.getStatus(executionResultId, input.signal)).status,\n intervalMs: input.pollIntervalMs ?? this.config.pollIntervalMs,\n timeoutMs: input.timeoutMs ?? this.config.timeoutMs,\n signal: input.signal,\n });\n const result = await this.getResult(executionResultId, input.signal);\n if (result.status === \"Failed\") {\n throw new DocmanaExecutionError(`Flow ${flowId} execution failed`, result.errors ?? []);\n }\n return result;\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACGO,IAAM,WAAW;AAAA,EACtB,YAAY;AAAA,EACZ,eACE;AAAA,EACF,OAAO;AAAA,EACP,WAAW;AAAA,EACX,gBAAgB;AAClB;AAeO,SAAS,cAAc,QAAuC;AACnE,MAAI,CAAC,OAAO,SAAU,OAAM,IAAI,MAAM,oCAAoC;AAC1E,MAAI,CAAC,OAAO,aAAc,OAAM,IAAI,MA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