@oddington/sdk 0.1.26 → 0.1.27

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/browser.js CHANGED
@@ -1,3 +1,108 @@
1
+ var FileOperationError = class extends Error {
2
+ constructor(code, status, retryable = !1) {
3
+ super(code);
4
+ this.code = code;
5
+ this.status = status;
6
+ this.retryable = retryable;
7
+ }
8
+ code;
9
+ status;
10
+ retryable;
11
+ }, FILE_ID_PATTERN = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/, FILE_ORIGINAL_MAX_BYTES = 5 * 1024 ** 3;
12
+ function parseFileDescriptor(value) {
13
+ let invalid8 = () => {
14
+ throw new FileOperationError("unavailable", 503, !0);
15
+ };
16
+ if (!value || typeof value != "object" || Array.isArray(value)) return invalid8();
17
+ let f = value;
18
+ if (Object.keys(f).sort().join(",") !== "details,enrichments,id,mediaType,name,path,sha256,size" || typeof f.id != "string" || !FILE_ID_PATTERN.test(f.id) || !safeFileName(f.name) || !safeFilePath(f.path) || !Number.isSafeInteger(f.size) || f.size < 0 || f.size > FILE_ORIGINAL_MAX_BYTES || typeof f.sha256 != "string" || !/^[0-9a-f]{64}$/.test(f.sha256) || !f.enrichments || typeof f.enrichments != "object" || Array.isArray(f.enrichments) || !f.details || typeof f.details != "object" || Array.isArray(f.details)) return invalid8();
19
+ let d = f.details;
20
+ if (d.kind === "other") {
21
+ if (Object.keys(d).length !== 1 || ![
22
+ "application/octet-stream",
23
+ "text/plain",
24
+ "text/markdown",
25
+ "application/json",
26
+ "image/png",
27
+ "image/jpeg"
28
+ ].includes(f.mediaType)) return invalid8();
29
+ } else if (d.kind === "csv") {
30
+ if (Object.keys(d).sort().join(",") !== "dataRows,delimiter,encoding,headerRow,kind" || f.mediaType !== "text/csv" || !["utf-8", "windows-1252"].includes(d.encoding) || ![",", ";", " "].includes(d.delimiter) || !Number.isSafeInteger(d.headerRow) || d.headerRow < 1 || !Number.isSafeInteger(d.dataRows) || d.dataRows < 0) return invalid8();
31
+ } else if (d.kind === "pdf") {
32
+ if (f.mediaType !== "application/pdf" || Object.keys(d).some((key) => !["kind", "pages"].includes(key)) || "pages" in d && (!Number.isSafeInteger(d.pages) || d.pages < 1)) return invalid8();
33
+ } else return invalid8();
34
+ for (let [name, descriptor] of Object.entries(f.enrichments)) {
35
+ if (!safeEnrichmentName(name)) return invalid8();
36
+ parseEnrichmentDescriptor(descriptor);
37
+ }
38
+ return new TextEncoder().encode(JSON.stringify(f)).byteLength > 16 * 1024 ? invalid8() : f;
39
+ }
40
+ function parseEnrichmentDescriptor(value) {
41
+ let invalid8 = () => {
42
+ throw new FileOperationError("unavailable", 503, !0);
43
+ };
44
+ if (!value || typeof value != "object" || Array.isArray(value)) return invalid8();
45
+ let d = value;
46
+ return Object.keys(d).sort().join(",") !== "mediaType,sha256,size" || !["application/octet-stream", "text/plain", "text/markdown", "application/json"].includes(d.mediaType) || !Number.isSafeInteger(d.size) || d.size < 0 || d.size > 16 * 1024 * 1024 || typeof d.sha256 != "string" || !/^[0-9a-f]{64}$/.test(d.sha256) ? invalid8() : d;
47
+ }
48
+ function safeEnrichmentName(value) {
49
+ return safeFileName(value) && !/^original(?:\.|$)/i.test(value);
50
+ }
51
+ function parseFileEnrichMetadata(value, maxBytes) {
52
+ let invalid8 = () => {
53
+ throw new FileOperationError("invalid_input", 400);
54
+ };
55
+ if (!value || typeof value != "object" || Array.isArray(value)) return invalid8();
56
+ let d = value;
57
+ if (Object.keys(d).some((key) => !["name", "size", "mediaType", "replace"].includes(key)) || !safeEnrichmentName(d.name) || !Number.isSafeInteger(d.size) || d.size < 0 || d.mediaType !== void 0 && (typeof d.mediaType != "string" || d.mediaType.length > 255 || /[\u0000-\u001f\u007f]/.test(d.mediaType)) || d.replace !== void 0 && (typeof d.replace != "string" || !/^[0-9a-f]{64}$/.test(d.replace))) return invalid8();
58
+ if (d.size > maxBytes) throw new FileOperationError("request_too_large", 413);
59
+ return d;
60
+ }
61
+ function safeFileName(value) {
62
+ return typeof value == "string" && value.length > 0 && value.length <= 255 && value === value.trim() && value !== "." && value !== ".." && !/[\u0000-\u001f\u007f-\u009f/\\:*?"<>|]/u.test(value) && !/[\ud800-\udbff](?![\udc00-\udfff])|(?<![\ud800-\udbff])[\udc00-\udfff]/.test(value) && !/[. ]$/.test(value) && !/^(con|prn|aux|nul|com[1-9]|lpt[1-9])(?:\.|$)/i.test(value);
63
+ }
64
+ function safeFilePath(value) {
65
+ return typeof value == "string" && value.length <= 1024 && value.split("/").every(safeFileName);
66
+ }
67
+ function safeManagedFilePath(value) {
68
+ return safeFilePath(value) && value.split("/")[0].toLowerCase() !== ".os";
69
+ }
70
+ var ORIGINAL_MEDIA_TYPES = ["application/pdf", "text/plain", "text/markdown", "application/json", "image/png", "image/jpeg"];
71
+ function declaredOriginalMediaType(value) {
72
+ let normalized = value.split(";", 1)[0].trim().toLowerCase();
73
+ return ORIGINAL_MEDIA_TYPES.includes(normalized) ? normalized : void 0;
74
+ }
75
+ function parseFileCreateMetadata(value, maxBytes) {
76
+ let invalid8 = () => {
77
+ throw new FileOperationError("invalid_input", 400);
78
+ };
79
+ if (!value || typeof value != "object" || Array.isArray(value)) return invalid8();
80
+ let input = value;
81
+ if (Object.keys(input).some((key) => !["name", "path", "storage", "csv", "inputMediaType", "size", "requestKey"].includes(key))) return invalid8();
82
+ let path = input.path === void 0 ? "Files" : input.path;
83
+ if (!safeFileName(input.name) || !safeManagedFilePath(path) || input.storage !== void 0 && input.storage !== "remote" && input.storage !== "local" || input.inputMediaType !== void 0 && (typeof input.inputMediaType != "string" || !ORIGINAL_MEDIA_TYPES.includes(input.inputMediaType)) || !Number.isSafeInteger(input.size) || input.size < 0) return invalid8();
84
+ if (input.size > maxBytes) throw new FileOperationError("request_too_large", 413);
85
+ if (typeof input.requestKey != "string" || input.requestKey.length < 1 || input.requestKey.length > 256 || input.requestKey !== input.requestKey.trim() || /[\u0000-\u001f\u007f]/.test(input.requestKey)) return invalid8();
86
+ let csv;
87
+ if (input.csv !== void 0) {
88
+ if (!input.csv || typeof input.csv != "object" || Array.isArray(input.csv)) return invalid8();
89
+ let hints = input.csv;
90
+ if (Object.keys(hints).some((key) => !["encoding", "delimiter", "headerRow"].includes(key))) return invalid8();
91
+ let encoding = hints.encoding === void 0 ? "utf-8" : hints.encoding, delimiter = hints.delimiter === void 0 ? "," : hints.delimiter, headerRow = hints.headerRow === void 0 ? 1 : hints.headerRow;
92
+ if (encoding !== "utf-8" && encoding !== "windows-1252" || ![",", ";", " "].includes(delimiter) || !Number.isSafeInteger(headerRow) || headerRow < 1 || headerRow > 1e5) return invalid8();
93
+ csv = { encoding, delimiter, headerRow };
94
+ }
95
+ return {
96
+ name: input.name,
97
+ path,
98
+ ...input.storage ? { storage: input.storage } : {},
99
+ ...input.inputMediaType === void 0 ? {} : { inputMediaType: input.inputMediaType },
100
+ ...csv ? { csv } : {},
101
+ size: input.size,
102
+ requestKey: input.requestKey
103
+ };
104
+ }
105
+
1
106
  var InboxContractError = class extends Error {
2
107
  constructor(message2) {
3
108
  super(message2), this.name = "InboxContractError";
@@ -65,7 +170,7 @@ function validateInboxFile(value) {
65
170
  let fileId = id(v.id, "Inbox file ID"), name = id(v.name, "filename"), mediaType = id(v.mediaType, "media type");
66
171
  (!/^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/.test(fileId) || /[\x00-\x1f\x7f/\\]/.test(name) || !/^[a-z0-9!#$&^_.+-]+\/[a-z0-9!#$&^_.+-]+$/.test(mediaType) || typeof v.sha256 != "string" || !/^[0-9a-f]{64}$/.test(v.sha256)) && fail("Invalid Inbox file identity or metadata");
67
172
  let size = natural(v.size, "file size");
68
- size > 25 * 1024 * 1024 && fail("File exceeds supported size");
173
+ size > FILE_ORIGINAL_MAX_BYTES && fail("File exceeds supported size");
69
174
  let d = object(v.details, "file details"), details;
70
175
  switch (d.kind) {
71
176
  case "csv":
@@ -144,7 +249,19 @@ function recordsBlock(value) {
144
249
  return validateBlock({ ...value, kind: "records" });
145
250
  }
146
251
  function fileBlock(value) {
147
- return validateBlock({ ...value, kind: "files" });
252
+ let input = object(serializable(value), "Files Block"), files3 = array(input.files, "files").map((entry) => {
253
+ let descriptor = object(entry, "File");
254
+ if (!("path" in descriptor || "enrichments" in descriptor)) return validateInboxFile(descriptor);
255
+ let file2;
256
+ try {
257
+ file2 = parseFileDescriptor(descriptor);
258
+ } catch {
259
+ return fail("Invalid shared File descriptor");
260
+ }
261
+ let { id: id3, name, mediaType, size, sha256, details } = file2;
262
+ return { id: id3, name, mediaType, size, sha256, details };
263
+ });
264
+ return validateBlock({ ...input, kind: "files", files: files3 });
148
265
  }
149
266
  function asksBlock(value) {
150
267
  return validateBlock({ ...value, kind: "asks" });
@@ -393,111 +510,6 @@ function validateRecordRead(value) {
393
510
  };
394
511
  }
395
512
 
396
- var FileOperationError = class extends Error {
397
- constructor(code, status, retryable = !1) {
398
- super(code);
399
- this.code = code;
400
- this.status = status;
401
- this.retryable = retryable;
402
- }
403
- code;
404
- status;
405
- retryable;
406
- }, FILE_ID_PATTERN = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/, FILE_ORIGINAL_MAX_BYTES = 5 * 1024 ** 3;
407
- function parseFileDescriptor(value) {
408
- let invalid8 = () => {
409
- throw new FileOperationError("unavailable", 503, !0);
410
- };
411
- if (!value || typeof value != "object" || Array.isArray(value)) return invalid8();
412
- let f = value;
413
- if (Object.keys(f).sort().join(",") !== "details,enrichments,id,mediaType,name,path,sha256,size" || typeof f.id != "string" || !FILE_ID_PATTERN.test(f.id) || !safeFileName(f.name) || !safeFilePath(f.path) || !Number.isSafeInteger(f.size) || f.size < 0 || f.size > FILE_ORIGINAL_MAX_BYTES || typeof f.sha256 != "string" || !/^[0-9a-f]{64}$/.test(f.sha256) || !f.enrichments || typeof f.enrichments != "object" || Array.isArray(f.enrichments) || !f.details || typeof f.details != "object" || Array.isArray(f.details)) return invalid8();
414
- let d = f.details;
415
- if (d.kind === "other") {
416
- if (Object.keys(d).length !== 1 || ![
417
- "application/octet-stream",
418
- "text/plain",
419
- "text/markdown",
420
- "application/json",
421
- "image/png",
422
- "image/jpeg"
423
- ].includes(f.mediaType)) return invalid8();
424
- } else if (d.kind === "csv") {
425
- if (Object.keys(d).sort().join(",") !== "dataRows,delimiter,encoding,headerRow,kind" || f.mediaType !== "text/csv" || !["utf-8", "windows-1252"].includes(d.encoding) || ![",", ";", " "].includes(d.delimiter) || !Number.isSafeInteger(d.headerRow) || d.headerRow < 1 || !Number.isSafeInteger(d.dataRows) || d.dataRows < 0) return invalid8();
426
- } else if (d.kind === "pdf") {
427
- if (f.mediaType !== "application/pdf" || Object.keys(d).some((key) => !["kind", "pages"].includes(key)) || "pages" in d && (!Number.isSafeInteger(d.pages) || d.pages < 1)) return invalid8();
428
- } else return invalid8();
429
- for (let [name, descriptor] of Object.entries(f.enrichments)) {
430
- if (!safeEnrichmentName(name)) return invalid8();
431
- parseEnrichmentDescriptor(descriptor);
432
- }
433
- return new TextEncoder().encode(JSON.stringify(f)).byteLength > 16 * 1024 ? invalid8() : f;
434
- }
435
- function parseEnrichmentDescriptor(value) {
436
- let invalid8 = () => {
437
- throw new FileOperationError("unavailable", 503, !0);
438
- };
439
- if (!value || typeof value != "object" || Array.isArray(value)) return invalid8();
440
- let d = value;
441
- return Object.keys(d).sort().join(",") !== "mediaType,sha256,size" || !["application/octet-stream", "text/plain", "text/markdown", "application/json"].includes(d.mediaType) || !Number.isSafeInteger(d.size) || d.size < 0 || d.size > 16 * 1024 * 1024 || typeof d.sha256 != "string" || !/^[0-9a-f]{64}$/.test(d.sha256) ? invalid8() : d;
442
- }
443
- function safeEnrichmentName(value) {
444
- return safeFileName(value) && !/^original(?:\.|$)/i.test(value);
445
- }
446
- function parseFileEnrichMetadata(value, maxBytes) {
447
- let invalid8 = () => {
448
- throw new FileOperationError("invalid_input", 400);
449
- };
450
- if (!value || typeof value != "object" || Array.isArray(value)) return invalid8();
451
- let d = value;
452
- if (Object.keys(d).some((key) => !["name", "size", "mediaType", "replace"].includes(key)) || !safeEnrichmentName(d.name) || !Number.isSafeInteger(d.size) || d.size < 0 || d.mediaType !== void 0 && (typeof d.mediaType != "string" || d.mediaType.length > 255 || /[\u0000-\u001f\u007f]/.test(d.mediaType)) || d.replace !== void 0 && (typeof d.replace != "string" || !/^[0-9a-f]{64}$/.test(d.replace))) return invalid8();
453
- if (d.size > maxBytes) throw new FileOperationError("request_too_large", 413);
454
- return d;
455
- }
456
- function safeFileName(value) {
457
- return typeof value == "string" && value.length > 0 && value.length <= 255 && value === value.trim() && value !== "." && value !== ".." && !/[\u0000-\u001f\u007f-\u009f/\\:*?"<>|]/u.test(value) && !/[\ud800-\udbff](?![\udc00-\udfff])|(?<![\ud800-\udbff])[\udc00-\udfff]/.test(value) && !/[. ]$/.test(value) && !/^(con|prn|aux|nul|com[1-9]|lpt[1-9])(?:\.|$)/i.test(value);
458
- }
459
- function safeFilePath(value) {
460
- return typeof value == "string" && value.length <= 1024 && value.split("/").every(safeFileName);
461
- }
462
- function safeManagedFilePath(value) {
463
- return safeFilePath(value) && value.split("/")[0].toLowerCase() !== ".os";
464
- }
465
- var ORIGINAL_MEDIA_TYPES = ["application/pdf", "text/plain", "text/markdown", "application/json", "image/png", "image/jpeg"];
466
- function declaredOriginalMediaType(value) {
467
- let normalized = value.split(";", 1)[0].trim().toLowerCase();
468
- return ORIGINAL_MEDIA_TYPES.includes(normalized) ? normalized : void 0;
469
- }
470
- function parseFileCreateMetadata(value, maxBytes) {
471
- let invalid8 = () => {
472
- throw new FileOperationError("invalid_input", 400);
473
- };
474
- if (!value || typeof value != "object" || Array.isArray(value)) return invalid8();
475
- let input = value;
476
- if (Object.keys(input).some((key) => !["name", "path", "storage", "csv", "inputMediaType", "size", "requestKey"].includes(key))) return invalid8();
477
- let path = input.path === void 0 ? "Files" : input.path;
478
- if (!safeFileName(input.name) || !safeManagedFilePath(path) || input.storage !== void 0 && input.storage !== "remote" && input.storage !== "local" || input.inputMediaType !== void 0 && (typeof input.inputMediaType != "string" || !ORIGINAL_MEDIA_TYPES.includes(input.inputMediaType)) || !Number.isSafeInteger(input.size) || input.size < 0) return invalid8();
479
- if (input.size > maxBytes) throw new FileOperationError("request_too_large", 413);
480
- if (typeof input.requestKey != "string" || input.requestKey.length < 1 || input.requestKey.length > 256 || input.requestKey !== input.requestKey.trim() || /[\u0000-\u001f\u007f]/.test(input.requestKey)) return invalid8();
481
- let csv;
482
- if (input.csv !== void 0) {
483
- if (!input.csv || typeof input.csv != "object" || Array.isArray(input.csv)) return invalid8();
484
- let hints = input.csv;
485
- if (Object.keys(hints).some((key) => !["encoding", "delimiter", "headerRow"].includes(key))) return invalid8();
486
- let encoding = hints.encoding === void 0 ? "utf-8" : hints.encoding, delimiter = hints.delimiter === void 0 ? "," : hints.delimiter, headerRow = hints.headerRow === void 0 ? 1 : hints.headerRow;
487
- if (encoding !== "utf-8" && encoding !== "windows-1252" || ![",", ";", " "].includes(delimiter) || !Number.isSafeInteger(headerRow) || headerRow < 1 || headerRow > 1e5) return invalid8();
488
- csv = { encoding, delimiter, headerRow };
489
- }
490
- return {
491
- name: input.name,
492
- path,
493
- ...input.storage ? { storage: input.storage } : {},
494
- ...input.inputMediaType === void 0 ? {} : { inputMediaType: input.inputMediaType },
495
- ...csv ? { csv } : {},
496
- size: input.size,
497
- requestKey: input.requestKey
498
- };
499
- }
500
-
501
513
  var AppRoutineContractError = class extends Error {
502
514
  constructor(code) {
503
515
  super(code);