@ptkl/sdk 1.16.0 → 1.17.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.
@@ -20323,6 +20323,70 @@ class IntegrationsBaseClient extends BaseClient {
20323
20323
  }
20324
20324
  }
20325
20325
 
20326
+ /**
20327
+ * Build an RFC 7233 Range header value from the SDK's range options.
20328
+ *
20329
+ * Validation happens here rather than at the server so a typo (a float offset, a
20330
+ * negative length) fails immediately and locally instead of arriving as an opaque
20331
+ * 400 after a round trip.
20332
+ */
20333
+ function buildRangeHeader(range) {
20334
+ const { offset, length, suffix } = range;
20335
+ if (suffix !== undefined) {
20336
+ if (offset !== undefined || length !== undefined) {
20337
+ throw new Error('readRange: `suffix` cannot be combined with `offset` or `length`');
20338
+ }
20339
+ if (!Number.isInteger(suffix) || suffix <= 0) {
20340
+ throw new Error('readRange: `suffix` must be a positive integer');
20341
+ }
20342
+ return `bytes=-${suffix}`;
20343
+ }
20344
+ const start = offset !== null && offset !== void 0 ? offset : 0;
20345
+ if (!Number.isInteger(start) || start < 0) {
20346
+ throw new Error('readRange: `offset` must be a non-negative integer');
20347
+ }
20348
+ if (length === undefined) {
20349
+ return `bytes=${start}-`;
20350
+ }
20351
+ if (!Number.isInteger(length) || length <= 0) {
20352
+ throw new Error('readRange: `length` must be a positive integer');
20353
+ }
20354
+ // Range end offsets are inclusive, so a length of N ends at start + N - 1.
20355
+ return `bytes=${start}-${start + length - 1}`;
20356
+ }
20357
+ /**
20358
+ * Turn a 206 response into a ByteRangeResult.
20359
+ *
20360
+ * The Content-Range check is the important part. Requests reach media-api through
20361
+ * two proxies, and if any hop ever dropped the Range header the response would be
20362
+ * a perfectly valid 200 carrying the *entire* file — a caller asking for 64 KiB
20363
+ * of a 2 GB object would quietly receive all 2 GB. Failing loudly here turns that
20364
+ * from a silent memory blow-up into an actionable error.
20365
+ */
20366
+ function toByteRangeResult(resp) {
20367
+ var _a, _b, _c, _d, _e, _f, _g;
20368
+ const contentRange = (_b = (_a = resp === null || resp === void 0 ? void 0 : resp.headers) === null || _a === void 0 ? void 0 : _a['content-range']) !== null && _b !== void 0 ? _b : (_c = resp === null || resp === void 0 ? void 0 : resp.headers) === null || _c === void 0 ? void 0 : _c['Content-Range'];
20369
+ const parsed = /^bytes\s+(\d+)-(\d+)\/(\d+)$/.exec(String(contentRange !== null && contentRange !== void 0 ? contentRange : '').trim());
20370
+ if (!parsed) {
20371
+ throw new Error('readRange: response did not include a Content-Range header, so the returned body ' +
20372
+ 'may be the entire object rather than the requested range. ' +
20373
+ 'Check that the media host supports ranged reads and that no proxy is stripping the Range header.');
20374
+ }
20375
+ const start = Number(parsed[1]);
20376
+ const end = Number(parsed[2]);
20377
+ const totalSize = Number(parsed[3]);
20378
+ const etag = (_e = (_d = resp === null || resp === void 0 ? void 0 : resp.headers) === null || _d === void 0 ? void 0 : _d.etag) !== null && _e !== void 0 ? _e : (_f = resp === null || resp === void 0 ? void 0 : resp.headers) === null || _f === void 0 ? void 0 : _f.ETag;
20379
+ return {
20380
+ data: resp.data,
20381
+ start,
20382
+ end,
20383
+ length: end - start + 1,
20384
+ totalSize,
20385
+ eof: end >= totalSize - 1,
20386
+ etag: typeof etag === 'string' ? etag.replace(/^"|"$/g, '') : undefined,
20387
+ contentType: (_g = resp === null || resp === void 0 ? void 0 : resp.headers) === null || _g === void 0 ? void 0 : _g['content-type'],
20388
+ };
20389
+ }
20326
20390
  /**
20327
20391
  * Document Management System (DMS) API client
20328
20392
  *
@@ -20456,6 +20520,95 @@ class DMS extends IntegrationsBaseClient {
20456
20520
  responseType: (!encoding) ? 'blob' : null
20457
20521
  });
20458
20522
  }
20523
+ /**
20524
+ * Read a slice of a stored file without transferring the whole object.
20525
+ *
20526
+ * Uses an HTTP Range request, so only the requested bytes cross the wire.
20527
+ * Slicing is positional and format-agnostic — it works on any file.
20528
+ *
20529
+ * ```ts
20530
+ * // First 64 KiB
20531
+ * const head = await dms.readRange(libraryUuid, 'reports/big.csv', { length: 65536 })
20532
+ *
20533
+ * // Continue from where that stopped
20534
+ * const next = await dms.readRange(libraryUuid, 'reports/big.csv', { offset: head.end + 1, length: 65536 })
20535
+ *
20536
+ * // Final 1 KiB
20537
+ * const tail = await dms.readRange(libraryUuid, 'reports/big.csv', { suffix: 1024 })
20538
+ * ```
20539
+ *
20540
+ * The server caps how much a single call may return, so `length` is an upper
20541
+ * bound rather than a guarantee: always advance using the returned `end`
20542
+ * rather than assuming the window you asked for. `eof` tells you when to stop.
20543
+ *
20544
+ * Byte ranges are not meaningful for .xlsx, .zip or .gz — their contents are
20545
+ * compressed as a unit, so no byte window corresponds to a range of rows.
20546
+ *
20547
+ * @param lib Library UUID
20548
+ * @param key Path of the file within the library
20549
+ * @param range Which bytes to read
20550
+ * @throws If the object is smaller than `offset` (nothing left to read), or if
20551
+ * the response came back without range metadata — see `readRange`'s
20552
+ * Content-Range check, which prevents silently receiving a whole file.
20553
+ */
20554
+ async readRange(lib, key, range = {}) {
20555
+ const { responseType = 'arraybuffer' } = range;
20556
+ const resp = await this.request('GET', `media/library/${lib}/get/${key}`, {
20557
+ headers: { Range: buildRangeHeader(range) },
20558
+ responseType,
20559
+ });
20560
+ return toByteRangeResult(resp);
20561
+ }
20562
+ /**
20563
+ * Read a file as a sequence of byte windows, newest request issued only when
20564
+ * the previous window has been consumed.
20565
+ *
20566
+ * This is the memory-bounded way to process a large file: the whole object is
20567
+ * never held at once, and a caller can stop early simply by breaking out.
20568
+ *
20569
+ * ```ts
20570
+ * for await (const chunk of dms.streamRanges(libraryUuid, 'logs/huge.ndjson', { chunkSize: 1 << 20 })) {
20571
+ * process(chunk.data) // one window at a time
20572
+ * if (foundWhatIWanted) break // no further requests are made
20573
+ * }
20574
+ * ```
20575
+ *
20576
+ * Windows are contiguous and non-overlapping, so concatenating every `data`
20577
+ * reproduces the file byte for byte. A record spanning a window boundary is
20578
+ * the caller's to reassemble — this yields bytes, not records.
20579
+ *
20580
+ * An empty object yields nothing rather than throwing.
20581
+ *
20582
+ * @param lib Library UUID
20583
+ * @param key Path of the file within the library
20584
+ * @param options Where to start and how large each window should be
20585
+ */
20586
+ async *streamRanges(lib, key, options = {}) {
20587
+ var _a, _b;
20588
+ const { chunkSize = 1024 * 1024, responseType = 'arraybuffer' } = options;
20589
+ let offset = (_a = options.offset) !== null && _a !== void 0 ? _a : 0;
20590
+ for (;;) {
20591
+ let chunk;
20592
+ try {
20593
+ chunk = await this.readRange(lib, key, { offset, length: chunkSize, responseType });
20594
+ }
20595
+ catch (err) {
20596
+ // 416 means the offset is at or past the end. For the first request
20597
+ // that is an empty object; afterwards it is a benign race with a
20598
+ // truncating writer. Either way the stream is simply over.
20599
+ if (((_b = err === null || err === void 0 ? void 0 : err.response) === null || _b === void 0 ? void 0 : _b.status) === 416)
20600
+ return;
20601
+ throw err;
20602
+ }
20603
+ yield chunk;
20604
+ if (chunk.eof)
20605
+ return;
20606
+ // Advance from what the server actually returned, never from chunkSize:
20607
+ // the per-request cap can make a window shorter than requested, and
20608
+ // assuming otherwise would skip bytes.
20609
+ offset = chunk.end + 1;
20610
+ }
20611
+ }
20459
20612
  async download(lib, key) {
20460
20613
  return this.request('POST', `media/library/${lib}/download`, {
20461
20614
  data: {
@@ -1354,6 +1354,70 @@ class IntegrationsBaseClient extends BaseClient {
1354
1354
  }
1355
1355
  }
1356
1356
 
1357
+ /**
1358
+ * Build an RFC 7233 Range header value from the SDK's range options.
1359
+ *
1360
+ * Validation happens here rather than at the server so a typo (a float offset, a
1361
+ * negative length) fails immediately and locally instead of arriving as an opaque
1362
+ * 400 after a round trip.
1363
+ */
1364
+ function buildRangeHeader(range) {
1365
+ const { offset, length, suffix } = range;
1366
+ if (suffix !== undefined) {
1367
+ if (offset !== undefined || length !== undefined) {
1368
+ throw new Error('readRange: `suffix` cannot be combined with `offset` or `length`');
1369
+ }
1370
+ if (!Number.isInteger(suffix) || suffix <= 0) {
1371
+ throw new Error('readRange: `suffix` must be a positive integer');
1372
+ }
1373
+ return `bytes=-${suffix}`;
1374
+ }
1375
+ const start = offset !== null && offset !== void 0 ? offset : 0;
1376
+ if (!Number.isInteger(start) || start < 0) {
1377
+ throw new Error('readRange: `offset` must be a non-negative integer');
1378
+ }
1379
+ if (length === undefined) {
1380
+ return `bytes=${start}-`;
1381
+ }
1382
+ if (!Number.isInteger(length) || length <= 0) {
1383
+ throw new Error('readRange: `length` must be a positive integer');
1384
+ }
1385
+ // Range end offsets are inclusive, so a length of N ends at start + N - 1.
1386
+ return `bytes=${start}-${start + length - 1}`;
1387
+ }
1388
+ /**
1389
+ * Turn a 206 response into a ByteRangeResult.
1390
+ *
1391
+ * The Content-Range check is the important part. Requests reach media-api through
1392
+ * two proxies, and if any hop ever dropped the Range header the response would be
1393
+ * a perfectly valid 200 carrying the *entire* file — a caller asking for 64 KiB
1394
+ * of a 2 GB object would quietly receive all 2 GB. Failing loudly here turns that
1395
+ * from a silent memory blow-up into an actionable error.
1396
+ */
1397
+ function toByteRangeResult(resp) {
1398
+ var _a, _b, _c, _d, _e, _f, _g;
1399
+ const contentRange = (_b = (_a = resp === null || resp === void 0 ? void 0 : resp.headers) === null || _a === void 0 ? void 0 : _a['content-range']) !== null && _b !== void 0 ? _b : (_c = resp === null || resp === void 0 ? void 0 : resp.headers) === null || _c === void 0 ? void 0 : _c['Content-Range'];
1400
+ const parsed = /^bytes\s+(\d+)-(\d+)\/(\d+)$/.exec(String(contentRange !== null && contentRange !== void 0 ? contentRange : '').trim());
1401
+ if (!parsed) {
1402
+ throw new Error('readRange: response did not include a Content-Range header, so the returned body ' +
1403
+ 'may be the entire object rather than the requested range. ' +
1404
+ 'Check that the media host supports ranged reads and that no proxy is stripping the Range header.');
1405
+ }
1406
+ const start = Number(parsed[1]);
1407
+ const end = Number(parsed[2]);
1408
+ const totalSize = Number(parsed[3]);
1409
+ const etag = (_e = (_d = resp === null || resp === void 0 ? void 0 : resp.headers) === null || _d === void 0 ? void 0 : _d.etag) !== null && _e !== void 0 ? _e : (_f = resp === null || resp === void 0 ? void 0 : resp.headers) === null || _f === void 0 ? void 0 : _f.ETag;
1410
+ return {
1411
+ data: resp.data,
1412
+ start,
1413
+ end,
1414
+ length: end - start + 1,
1415
+ totalSize,
1416
+ eof: end >= totalSize - 1,
1417
+ etag: typeof etag === 'string' ? etag.replace(/^"|"$/g, '') : undefined,
1418
+ contentType: (_g = resp === null || resp === void 0 ? void 0 : resp.headers) === null || _g === void 0 ? void 0 : _g['content-type'],
1419
+ };
1420
+ }
1357
1421
  /**
1358
1422
  * Document Management System (DMS) API client
1359
1423
  *
@@ -1487,6 +1551,95 @@ class DMS extends IntegrationsBaseClient {
1487
1551
  responseType: (!encoding) ? 'blob' : null
1488
1552
  });
1489
1553
  }
1554
+ /**
1555
+ * Read a slice of a stored file without transferring the whole object.
1556
+ *
1557
+ * Uses an HTTP Range request, so only the requested bytes cross the wire.
1558
+ * Slicing is positional and format-agnostic — it works on any file.
1559
+ *
1560
+ * ```ts
1561
+ * // First 64 KiB
1562
+ * const head = await dms.readRange(libraryUuid, 'reports/big.csv', { length: 65536 })
1563
+ *
1564
+ * // Continue from where that stopped
1565
+ * const next = await dms.readRange(libraryUuid, 'reports/big.csv', { offset: head.end + 1, length: 65536 })
1566
+ *
1567
+ * // Final 1 KiB
1568
+ * const tail = await dms.readRange(libraryUuid, 'reports/big.csv', { suffix: 1024 })
1569
+ * ```
1570
+ *
1571
+ * The server caps how much a single call may return, so `length` is an upper
1572
+ * bound rather than a guarantee: always advance using the returned `end`
1573
+ * rather than assuming the window you asked for. `eof` tells you when to stop.
1574
+ *
1575
+ * Byte ranges are not meaningful for .xlsx, .zip or .gz — their contents are
1576
+ * compressed as a unit, so no byte window corresponds to a range of rows.
1577
+ *
1578
+ * @param lib Library UUID
1579
+ * @param key Path of the file within the library
1580
+ * @param range Which bytes to read
1581
+ * @throws If the object is smaller than `offset` (nothing left to read), or if
1582
+ * the response came back without range metadata — see `readRange`'s
1583
+ * Content-Range check, which prevents silently receiving a whole file.
1584
+ */
1585
+ async readRange(lib, key, range = {}) {
1586
+ const { responseType = 'arraybuffer' } = range;
1587
+ const resp = await this.request('GET', `media/library/${lib}/get/${key}`, {
1588
+ headers: { Range: buildRangeHeader(range) },
1589
+ responseType,
1590
+ });
1591
+ return toByteRangeResult(resp);
1592
+ }
1593
+ /**
1594
+ * Read a file as a sequence of byte windows, newest request issued only when
1595
+ * the previous window has been consumed.
1596
+ *
1597
+ * This is the memory-bounded way to process a large file: the whole object is
1598
+ * never held at once, and a caller can stop early simply by breaking out.
1599
+ *
1600
+ * ```ts
1601
+ * for await (const chunk of dms.streamRanges(libraryUuid, 'logs/huge.ndjson', { chunkSize: 1 << 20 })) {
1602
+ * process(chunk.data) // one window at a time
1603
+ * if (foundWhatIWanted) break // no further requests are made
1604
+ * }
1605
+ * ```
1606
+ *
1607
+ * Windows are contiguous and non-overlapping, so concatenating every `data`
1608
+ * reproduces the file byte for byte. A record spanning a window boundary is
1609
+ * the caller's to reassemble — this yields bytes, not records.
1610
+ *
1611
+ * An empty object yields nothing rather than throwing.
1612
+ *
1613
+ * @param lib Library UUID
1614
+ * @param key Path of the file within the library
1615
+ * @param options Where to start and how large each window should be
1616
+ */
1617
+ async *streamRanges(lib, key, options = {}) {
1618
+ var _a, _b;
1619
+ const { chunkSize = 1024 * 1024, responseType = 'arraybuffer' } = options;
1620
+ let offset = (_a = options.offset) !== null && _a !== void 0 ? _a : 0;
1621
+ for (;;) {
1622
+ let chunk;
1623
+ try {
1624
+ chunk = await this.readRange(lib, key, { offset, length: chunkSize, responseType });
1625
+ }
1626
+ catch (err) {
1627
+ // 416 means the offset is at or past the end. For the first request
1628
+ // that is an empty object; afterwards it is a benign race with a
1629
+ // truncating writer. Either way the stream is simply over.
1630
+ if (((_b = err === null || err === void 0 ? void 0 : err.response) === null || _b === void 0 ? void 0 : _b.status) === 416)
1631
+ return;
1632
+ throw err;
1633
+ }
1634
+ yield chunk;
1635
+ if (chunk.eof)
1636
+ return;
1637
+ // Advance from what the server actually returned, never from chunkSize:
1638
+ // the per-request cap can make a window shorter than requested, and
1639
+ // assuming otherwise would skip bytes.
1640
+ offset = chunk.end + 1;
1641
+ }
1642
+ }
1490
1643
  async download(lib, key) {
1491
1644
  return this.request('POST', `media/library/${lib}/download`, {
1492
1645
  data: {
@@ -307,3 +307,47 @@ export type DocumentCommentPayload = {
307
307
  /** Comment end position */
308
308
  to?: number;
309
309
  };
310
+ /**
311
+ * Which bytes to read. Supply either `offset`/`length` or `suffix`, not both.
312
+ */
313
+ export type ByteRangeRequest = {
314
+ /** First byte to read, 0-based. Defaults to 0. */
315
+ offset?: number;
316
+ /** How many bytes to read. Omit to read to the end of the object, subject to the server's per-request cap. */
317
+ length?: number;
318
+ /** Read the final N bytes instead. Mutually exclusive with `offset`/`length`. */
319
+ suffix?: number;
320
+ /** Axios response type for the body. Defaults to `arraybuffer`. */
321
+ responseType?: 'arraybuffer' | 'blob' | 'text';
322
+ };
323
+ /**
324
+ * A single window of bytes plus the position metadata needed to request the next one.
325
+ */
326
+ export type ByteRangeResult<T = any> = {
327
+ /** The bytes, in the shape requested via `responseType`. */
328
+ data: T;
329
+ /** Offset of the first byte returned. */
330
+ start: number;
331
+ /** Offset of the last byte returned, inclusive. */
332
+ end: number;
333
+ /** Number of bytes returned. May be less than requested — see `ByteRangeRequest.length`. */
334
+ length: number;
335
+ /** Total size of the whole object. */
336
+ totalSize: number;
337
+ /** True when this window ends at the last byte of the object. */
338
+ eof: boolean;
339
+ /** Object validator. Changes if the object is replaced, so a multi-window scan can detect it. */
340
+ etag?: string;
341
+ contentType?: string;
342
+ };
343
+ /**
344
+ * Options for walking an object window by window.
345
+ */
346
+ export type ByteStreamOptions = {
347
+ /** Byte offset to start from. Defaults to 0. */
348
+ offset?: number;
349
+ /** Bytes per window. Defaults to 1 MiB. The server may return less. */
350
+ chunkSize?: number;
351
+ /** Axios response type for each window. Defaults to `arraybuffer`. */
352
+ responseType?: 'arraybuffer' | 'blob' | 'text';
353
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ptkl/sdk",
3
- "version": "1.16.0",
3
+ "version": "1.17.1",
4
4
  "scripts": {
5
5
  "build": "rollup -c",
6
6
  "build:monaco": "npm run build && node scripts/generate-monaco-types.cjs",