dataiku-sdk 3.8.0 → 4.0.0

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.
Files changed (68) hide show
  1. package/README.md +7 -5
  2. package/dist/build-metadata.json +1 -1
  3. package/dist/packages/types/src/index.d.ts +4 -0
  4. package/dist/packages/types/src/index.js +4 -0
  5. package/dist/src/cli/coerce.d.ts +6 -0
  6. package/dist/src/cli/coerce.js +9 -0
  7. package/dist/src/cli/command-syntax.json +36 -0
  8. package/dist/src/cli/commands/app.js +2 -2
  9. package/dist/src/cli/commands/code-env.js +5 -6
  10. package/dist/src/cli/commands/code.js +16 -2
  11. package/dist/src/cli/commands/dashboard.js +1 -2
  12. package/dist/src/cli/commands/dataset.js +2 -2
  13. package/dist/src/cli/commands/flow-zone.js +1 -1
  14. package/dist/src/cli/commands/job.js +101 -22
  15. package/dist/src/cli/commands/plugin.js +7 -7
  16. package/dist/src/cli/commands/project-library.js +2 -2
  17. package/dist/src/cli/commands/project.js +25 -7
  18. package/dist/src/cli/commands/recipe.js +11 -19
  19. package/dist/src/cli/commands/scenario.js +2 -4
  20. package/dist/src/cli/contract.js +29 -10
  21. package/dist/src/cli/env.js +2 -3
  22. package/dist/src/cli/helpers/app-permissions.d.ts +1 -1
  23. package/dist/src/cli/helpers/app-permissions.js +2 -2
  24. package/dist/src/cli/helpers/cleanup.d.ts +2 -0
  25. package/dist/src/cli/helpers/cleanup.js +122 -8
  26. package/dist/src/cli/helpers/flow-zone.d.ts +1 -1
  27. package/dist/src/cli/helpers/flow-zone.js +3 -4
  28. package/dist/src/cli/list-output.d.ts +5 -0
  29. package/dist/src/cli/list-output.js +11 -2
  30. package/dist/src/cli/list-shapes.d.ts +6 -0
  31. package/dist/src/cli/list-shapes.js +7 -1
  32. package/dist/src/cli/output-schemas.d.ts +1 -1
  33. package/dist/src/cli/output-schemas.js +28 -4
  34. package/dist/src/cli/output.js +7 -0
  35. package/dist/src/cli/project-map-output.d.ts +24 -0
  36. package/dist/src/cli/project-map-output.js +21 -0
  37. package/dist/src/cli/read-shapes.d.ts +14 -0
  38. package/dist/src/cli/read-shapes.js +114 -0
  39. package/dist/src/cli/result-shapes.d.ts +6 -0
  40. package/dist/src/cli/result-shapes.js +235 -0
  41. package/dist/src/cli/version.d.ts +2 -2
  42. package/dist/src/cli/version.js +4 -5
  43. package/dist/src/cli.js +77 -33
  44. package/dist/src/client.d.ts +14 -2
  45. package/dist/src/client.js +18 -10
  46. package/dist/src/generated/action-output-schemas.json +242 -35
  47. package/dist/src/index.d.ts +1 -1
  48. package/dist/src/resources/datasets.d.ts +11 -1
  49. package/dist/src/resources/datasets.js +91 -30
  50. package/dist/src/resources/jobs.d.ts +37 -6
  51. package/dist/src/resources/jobs.js +110 -44
  52. package/dist/src/resources/projects.js +2 -2
  53. package/dist/src/resources/recipes.d.ts +12 -1
  54. package/dist/src/resources/recipes.js +39 -12
  55. package/dist/src/resources/scenarios.d.ts +8 -1
  56. package/dist/src/resources/scenarios.js +51 -12
  57. package/dist/src/resources/wiki.js +3 -7
  58. package/dist/src/skill.js +1 -2
  59. package/dist/src/utils/project-archive.d.ts +25 -0
  60. package/dist/src/utils/project-archive.js +216 -5
  61. package/dist/src/utils/response-file.js +15 -8
  62. package/dist/src/utils/secret-sanitize.d.ts +11 -2
  63. package/dist/src/utils/secret-sanitize.js +23 -6
  64. package/package.json +13 -14
  65. package/plugin.json +1 -1
  66. package/skills/dataiku-dss/SKILL.md +1 -1
  67. package/skills/dataiku-dss/references/discovery.md +2 -2
  68. package/skills/dataiku-dss/references/troubleshooting.md +2 -2
@@ -363,6 +363,107 @@
363
363
  }
364
364
  }
365
365
  },
366
+ "dataset.info": {
367
+ "type": "object",
368
+ "additionalProperties": false,
369
+ "required": [
370
+ "name"
371
+ ],
372
+ "properties": {
373
+ "name": {
374
+ "type": "string"
375
+ },
376
+ "type": {
377
+ "type": "string"
378
+ },
379
+ "managed": {
380
+ "type": "boolean"
381
+ },
382
+ "connection": {
383
+ "type": "string"
384
+ },
385
+ "formatType": {
386
+ "type": "string"
387
+ },
388
+ "partitioned": {
389
+ "type": "boolean"
390
+ },
391
+ "schema": {
392
+ "type": "array",
393
+ "items": {
394
+ "type": "object",
395
+ "additionalProperties": false,
396
+ "required": [
397
+ "name",
398
+ "type"
399
+ ],
400
+ "properties": {
401
+ "name": {
402
+ "type": "string"
403
+ },
404
+ "type": {
405
+ "type": "string"
406
+ },
407
+ "meaning": {
408
+ "type": "string"
409
+ },
410
+ "comment": {
411
+ "type": "string"
412
+ }
413
+ }
414
+ }
415
+ },
416
+ "recipes": {
417
+ "type": "array",
418
+ "items": {
419
+ "type": "string"
420
+ }
421
+ },
422
+ "buildable": {
423
+ "type": "boolean"
424
+ },
425
+ "upstreamBuildable": {
426
+ "type": "boolean"
427
+ },
428
+ "downstreamBuildable": {
429
+ "type": "boolean"
430
+ },
431
+ "tags": {
432
+ "type": "array",
433
+ "items": {
434
+ "type": "string"
435
+ }
436
+ },
437
+ "dataQuality": {
438
+ "type": "object",
439
+ "additionalProperties": true
440
+ },
441
+ "lastBuild": {
442
+ "type": "object",
443
+ "additionalProperties": false,
444
+ "properties": {
445
+ "jobId": {
446
+ "type": "string"
447
+ },
448
+ "success": {
449
+ "type": "boolean"
450
+ },
451
+ "startTime": {
452
+ "type": "number"
453
+ },
454
+ "endTime": {
455
+ "type": "number"
456
+ }
457
+ }
458
+ },
459
+ "lastModifiedOn": {
460
+ "type": "number"
461
+ },
462
+ "lastModifiedBy": {
463
+ "type": "string"
464
+ }
465
+ }
466
+ },
366
467
  "dataset.list": {
367
468
  "type": "array",
368
469
  "items": {
@@ -825,6 +926,121 @@
825
926
  }
826
927
  }
827
928
  },
929
+ "job.get": {
930
+ "type": "object",
931
+ "additionalProperties": false,
932
+ "required": [
933
+ "id",
934
+ "state"
935
+ ],
936
+ "properties": {
937
+ "id": {
938
+ "type": "string"
939
+ },
940
+ "name": {
941
+ "type": "string"
942
+ },
943
+ "type": {
944
+ "type": "string"
945
+ },
946
+ "state": {
947
+ "type": "string"
948
+ },
949
+ "initiator": {
950
+ "type": "string"
951
+ },
952
+ "triggeredFrom": {
953
+ "type": "string"
954
+ },
955
+ "startTime": {
956
+ "type": "number"
957
+ },
958
+ "endTime": {
959
+ "type": "number"
960
+ },
961
+ "outputs": {
962
+ "type": "array",
963
+ "items": {
964
+ "type": "object",
965
+ "additionalProperties": false,
966
+ "properties": {
967
+ "type": {
968
+ "type": "string"
969
+ },
970
+ "id": {
971
+ "type": "string"
972
+ },
973
+ "projectKey": {
974
+ "type": "string"
975
+ },
976
+ "partition": {
977
+ "type": "string"
978
+ }
979
+ }
980
+ }
981
+ },
982
+ "progress": {
983
+ "type": "object",
984
+ "additionalProperties": {
985
+ "type": "number"
986
+ }
987
+ },
988
+ "error": {
989
+ "type": "object",
990
+ "additionalProperties": false,
991
+ "properties": {
992
+ "type": {
993
+ "type": "string"
994
+ },
995
+ "message": {
996
+ "type": "string"
997
+ }
998
+ }
999
+ },
1000
+ "activities": {
1001
+ "type": "array",
1002
+ "items": {
1003
+ "type": "object",
1004
+ "additionalProperties": false,
1005
+ "properties": {
1006
+ "id": {
1007
+ "type": "string"
1008
+ },
1009
+ "recipe": {
1010
+ "type": "string"
1011
+ },
1012
+ "recipeType": {
1013
+ "type": "string"
1014
+ },
1015
+ "state": {
1016
+ "type": "string"
1017
+ },
1018
+ "totalTime": {
1019
+ "type": "number"
1020
+ },
1021
+ "warnings": {
1022
+ "type": "number"
1023
+ },
1024
+ "error": {
1025
+ "type": "object",
1026
+ "additionalProperties": false,
1027
+ "properties": {
1028
+ "type": {
1029
+ "type": "string"
1030
+ },
1031
+ "message": {
1032
+ "type": "string"
1033
+ }
1034
+ }
1035
+ },
1036
+ "message": {
1037
+ "type": "string"
1038
+ }
1039
+ }
1040
+ }
1041
+ }
1042
+ }
1043
+ },
828
1044
  "job.list": {
829
1045
  "type": "array",
830
1046
  "items": {
@@ -903,6 +1119,12 @@
903
1119
  "timedOut": {
904
1120
  "type": "boolean"
905
1121
  },
1122
+ "cancelled": {
1123
+ "type": "boolean"
1124
+ },
1125
+ "resume": {
1126
+ "type": "string"
1127
+ },
906
1128
  "progress": {
907
1129
  "type": "object",
908
1130
  "required": [
@@ -971,6 +1193,12 @@
971
1193
  "timedOut": {
972
1194
  "type": "boolean"
973
1195
  },
1196
+ "cancelled": {
1197
+ "type": "boolean"
1198
+ },
1199
+ "resume": {
1200
+ "type": "string"
1201
+ },
974
1202
  "progress": {
975
1203
  "type": "object",
976
1204
  "required": [
@@ -1361,6 +1589,12 @@
1361
1589
  "string",
1362
1590
  "null"
1363
1591
  ]
1592
+ },
1593
+ "isDev": {
1594
+ "type": [
1595
+ "boolean",
1596
+ "null"
1597
+ ]
1364
1598
  }
1365
1599
  }
1366
1600
  }
@@ -1519,7 +1753,8 @@
1519
1753
  "type": "object",
1520
1754
  "required": [
1521
1755
  "projectKey",
1522
- "name"
1756
+ "name",
1757
+ "deniedPermissions"
1523
1758
  ],
1524
1759
  "properties": {
1525
1760
  "projectKey": {
@@ -1537,25 +1772,6 @@
1537
1772
  "projectAppType": {
1538
1773
  "type": "string"
1539
1774
  },
1540
- "creationTag": {
1541
- "additionalProperties": true,
1542
- "type": "object",
1543
- "properties": {
1544
- "versionNumber": {
1545
- "type": "number"
1546
- },
1547
- "lastModifiedOn": {
1548
- "anyOf": [
1549
- {
1550
- "type": "number"
1551
- },
1552
- {
1553
- "type": "string"
1554
- }
1555
- ]
1556
- }
1557
- }
1558
- },
1559
1775
  "ownerLogin": {
1560
1776
  "type": "string"
1561
1777
  },
@@ -1565,21 +1781,12 @@
1565
1781
  "type": "string"
1566
1782
  }
1567
1783
  },
1568
- "versionTag": {
1569
- "additionalProperties": true,
1570
- "type": "object",
1571
- "required": [
1572
- "versionNumber",
1573
- "lastModifiedOn"
1574
- ],
1575
- "properties": {
1576
- "versionNumber": {
1577
- "type": "number"
1578
- },
1579
- "lastModifiedOn": {
1580
- "type": "number"
1581
- }
1582
- }
1784
+ "deniedPermissions": {
1785
+ "type": "array",
1786
+ "items": {
1787
+ "type": "string"
1788
+ },
1789
+ "description": "Permission flags DSS reports as false; absent flags are granted."
1583
1790
  }
1584
1791
  }
1585
1792
  },
@@ -22,7 +22,7 @@ export { FoldersResource, } from "./resources/folders.js";
22
22
  export { type DssTask, FuturesResource, type FutureWaitOptions, } from "./resources/futures.js";
23
23
  export { type DssGroup, type GroupCreateRequest, GroupsResource, } from "./resources/groups.js";
24
24
  export { InsightsResource, } from "./resources/insights.js";
25
- export { type JobBuildAndWaitOptions, type JobBuildOptions, type JobBuildTarget, type JobBuildTargetType, type JobLogFilter, type JobLogProgress, type JobLogSummary, type JobLogUnavailableReason, JobsResource, type JobWaitOutcome, parseJobLogProgress, } from "./resources/jobs.js";
25
+ export { type JobBuildAndWaitOptions, type JobBuildOptions, type JobBuildTarget, type JobBuildTargetType, type JobLogDetails, type JobLogFilter, type JobLogProgress, type JobLogSummary, type JobLogUnavailableReason, JobsResource, type JobWaitOutcome, parseJobLogProgress, } from "./resources/jobs.js";
26
26
  export * from "./resources/knowledge-banks.js";
27
27
  export * from "./resources/llms.js";
28
28
  export { type MacroResultOptions, type MacroRunFailure, type MacroRunHandle, type MacroRunOptions, type MacroRunWaitedResult, MacrosResource, type MacroWaitOptions, } from "./resources/macros.js";
@@ -135,9 +135,19 @@ export declare class DatasetsResource extends BaseResource {
135
135
  /**
136
136
  * TSV data stream of at most `limit` rows. A column or partition selection
137
137
  * uses the documented POST variant of the data endpoint (body `columns`,
138
- * `partitions`, `format`, `sampling`); otherwise the GET endpoint.
138
+ * `partitions`, `format`, `sampling`); otherwise the GET endpoint. Both carry
139
+ * a client-generated `readSessionId` for {@link verifyStreamComplete}.
139
140
  */
140
141
  private dataStream;
142
+ /**
143
+ * A data stream that ended normally may still be incomplete: DSS sends 200
144
+ * headers before reading the data, so a server-side read failure only cuts
145
+ * the body short. As dataikuapi does, ask DSS whether the read session
146
+ * finished; it answers non-2xx (DataikuError) when it did not. Only called
147
+ * after the body was read to its end — a stream the client cancelled at
148
+ * its row cap is not complete by design.
149
+ */
150
+ private verifyStreamComplete;
141
151
  /**
142
152
  * Preview dataset rows as structured data: column names plus row arrays,
143
153
  * mirroring the sql query shape ({ columns, rows, rowCount }). Streams TSV
@@ -1,6 +1,5 @@
1
1
  import * as fs from "node:fs";
2
2
  import * as nodePath from "node:path";
3
- import { StringDecoder, } from "node:string_decoder";
4
3
  import { ClientValidationError, DataikuError, } from "../errors.js";
5
4
  import { DatasetDetailsSchema, DatasetSchemaSchema, DatasetSummaryArraySchema, } from "../schemas.js";
6
5
  import { deepMerge, } from "../utils/deep-merge.js";
@@ -28,8 +27,44 @@ function formatLimitBytes(bytes) {
28
27
  return `${bytes / (1024 * 1024)} MiB`;
29
28
  return `${bytes} B`;
30
29
  }
31
- function tsvLimitError(what, limit) {
32
- return new DataikuError(0, "Dataset TSV Limit", `Dataset TSV ${what} exceeded ${formatLimitBytes(limit)}; streaming was aborted to guard against unbounded memory use.`);
30
+ const TSV_ERROR_COLUMN_NAME_MAX = 40;
31
+ /**
32
+ * Describe the column whose cell is being read when a field/row budget trips:
33
+ * the header name once the header row has been parsed, otherwise the 0-based
34
+ * column index. Error summaries are bounded (~200 chars), so the name is
35
+ * shortened to keep the `--columns` hint inside the visible message.
36
+ */
37
+ function describeTsvColumn(state) {
38
+ const index = state.currentRow.length;
39
+ const name = state.header?.[index];
40
+ if (name === undefined)
41
+ return `column index ${index}`;
42
+ const shown = name.length > TSV_ERROR_COLUMN_NAME_MAX
43
+ ? `${name.slice(0, TSV_ERROR_COLUMN_NAME_MAX)}...`
44
+ : name;
45
+ return `column "${shown}"`;
46
+ }
47
+ /**
48
+ * A client-side size budget, not a DSS or transport failure: retrying the same
49
+ * read cannot succeed, so this is a non-retryable input error whose fix is a
50
+ * narrower selection (--columns).
51
+ */
52
+ function tsvLimitError(what, limit, state) {
53
+ const exceeded = `Dataset TSV ${what} exceeded ${formatLimitBytes(limit)}`;
54
+ const cellLimit = formatLimitBytes(state.limits.maxFieldBytes);
55
+ let message;
56
+ if (what === "field") {
57
+ message = `${exceeded} in ${describeTsvColumn(state)} (a single cell is limited to ${cellLimit}); exclude it with --columns.`;
58
+ }
59
+ else if (what === "row") {
60
+ message = `${exceeded} (a single cell is limited to ${cellLimit}); reached in ${describeTsvColumn(state)}. Exclude large columns with --columns.`;
61
+ }
62
+ else {
63
+ message = `${exceeded}; streaming was aborted to guard against unbounded memory use.`;
64
+ }
65
+ return new ClientValidationError(message, "validation_failed", what === "response"
66
+ ? "Narrow the read with --columns, --partitions, or a lower row limit."
67
+ : "Select fewer columns with --columns so the oversized cell is not read.");
33
68
  }
34
69
  /** Characters that evaluate a spreadsheet cell as a formula. */
35
70
  const FORMULA_SIGILS = "=+-@";
@@ -75,6 +110,7 @@ function createTsvStreamState(limits = {
75
110
  return {
76
111
  currentField: "",
77
112
  currentRow: [],
113
+ header: undefined,
78
114
  inQuotes: false,
79
115
  pendingQuoteInQuotes: false,
80
116
  fieldBytes: 0,
@@ -91,10 +127,10 @@ function appendFieldText(state, text) {
91
127
  state.fieldBytes += bytes;
92
128
  state.rowBytes += bytes;
93
129
  if (state.fieldBytes > state.limits.maxFieldBytes) {
94
- throw tsvLimitError("field", state.limits.maxFieldBytes);
130
+ throw tsvLimitError("field", state.limits.maxFieldBytes, state);
95
131
  }
96
132
  if (state.rowBytes > state.limits.maxRowBytes) {
97
- throw tsvLimitError("row", state.limits.maxRowBytes);
133
+ throw tsvLimitError("row", state.limits.maxRowBytes, state);
98
134
  }
99
135
  state.currentField += text;
100
136
  }
@@ -105,11 +141,20 @@ function endField(state) {
105
141
  state.fieldBytes = 0;
106
142
  state.rowBytes += 1; // count the delimiter byte
107
143
  }
144
+ /** Hand a completed row to `onRow`, remembering the first non-blank row as the header. */
145
+ function emitRow(state, onRow) {
146
+ const row = state.currentRow;
147
+ state.currentRow = [];
148
+ state.rowBytes = 0;
149
+ if (state.header === undefined && !isBlankRow(row))
150
+ state.header = row;
151
+ onRow(row);
152
+ }
108
153
  function consumeTsvChunk(text, state, onRow) {
109
154
  // Response budget: bound total decoded bytes regardless of row structure.
110
155
  state.responseBytes += Buffer.byteLength(text, "utf-8");
111
156
  if (state.responseBytes > state.limits.maxResponseBytes) {
112
- throw tsvLimitError("response", state.limits.maxResponseBytes);
157
+ throw tsvLimitError("response", state.limits.maxResponseBytes, state);
113
158
  }
114
159
  let i = 0;
115
160
  if (state.pendingQuoteInQuotes) {
@@ -193,10 +238,7 @@ function consumeTsvChunk(text, state, onRow) {
193
238
  if (ch === "\n") {
194
239
  endField(state);
195
240
  i = special + 1;
196
- const row = state.currentRow;
197
- state.currentRow = [];
198
- state.rowBytes = 0;
199
- onRow(row);
241
+ emitRow(state, onRow);
200
242
  continue;
201
243
  }
202
244
  // "\r" — carriage return: skipped, as in the original parser.
@@ -213,10 +255,7 @@ function flushTsvStream(state, onRow) {
213
255
  state.currentRow.push(state.currentField);
214
256
  state.currentField = "";
215
257
  state.fieldBytes = 0;
216
- const row = state.currentRow;
217
- state.currentRow = [];
218
- state.rowBytes = 0;
219
- onRow(row);
258
+ emitRow(state, onRow);
220
259
  }
221
260
  function rowToCsv(row, spreadsheetSafe) {
222
261
  return row.map((field) => csvEscape(field, spreadsheetSafe)).join(",");
@@ -264,7 +303,7 @@ async function readChunkWithTimeout(reader, remainingMs, timeoutMs, action) {
264
303
  }
265
304
  async function collectPreviewRows(body, maxDataRows, timeoutMs, onHeader) {
266
305
  const state = createTsvStreamState();
267
- const decoder = new StringDecoder("utf-8");
306
+ const decoder = new TextDecoder("utf-8", { ignoreBOM: true, });
268
307
  let columns;
269
308
  const rows = [];
270
309
  let done = false;
@@ -304,10 +343,10 @@ async function collectPreviewRows(body, maxDataRows, timeoutMs, onHeader) {
304
343
  const result = await readChunkWithTimeout(reader, remainingMs, timeoutMs, "preview");
305
344
  if (result.done)
306
345
  break;
307
- consumeTsvChunk(decoder.write(result.value), state, handleRow);
346
+ consumeTsvChunk(decoder.decode(result.value, { stream: true, }), state, handleRow);
308
347
  }
309
348
  if (!done) {
310
- const tail = decoder.end();
349
+ const tail = decoder.decode();
311
350
  if (tail !== "")
312
351
  consumeTsvChunk(tail, state, handleRow);
313
352
  flushTsvStream(state, handleRow);
@@ -332,7 +371,7 @@ async function collectPreviewRows(body, maxDataRows, timeoutMs, onHeader) {
332
371
  */
333
372
  async function collectTsvRowCount(body, stopAfter, timeoutMs) {
334
373
  const state = createTsvStreamState();
335
- const decoder = new StringDecoder("utf-8");
374
+ const decoder = new TextDecoder("utf-8", { ignoreBOM: true, });
336
375
  let headerSeen = false;
337
376
  let count = 0;
338
377
  let bounded = false;
@@ -373,10 +412,10 @@ async function collectTsvRowCount(body, stopAfter, timeoutMs) {
373
412
  const result = await readChunkWithTimeout(reader, remainingMs, timeoutMs, "row count assertion");
374
413
  if (result.done)
375
414
  break;
376
- consumeTsvChunk(decoder.write(result.value), state, handleRow);
415
+ consumeTsvChunk(decoder.decode(result.value, { stream: true, }), state, handleRow);
377
416
  }
378
417
  if (!bounded) {
379
- const tail = decoder.end();
418
+ const tail = decoder.decode();
380
419
  if (tail !== "")
381
420
  consumeTsvChunk(tail, state, handleRow);
382
421
  flushTsvStream(state, handleRow);
@@ -402,7 +441,7 @@ async function collectTsvRowCount(body, stopAfter, timeoutMs) {
402
441
  const CSV_BATCH_BYTES = 64 * 1024;
403
442
  function tsvToCsvTransform(maxDataRows, stats, onHeader, spreadsheetSafe = true) {
404
443
  const state = createTsvStreamState();
405
- const decoder = new StringDecoder("utf-8");
444
+ const decoder = new TextDecoder("utf-8", { ignoreBOM: true, });
406
445
  const encoder = new TextEncoder();
407
446
  const maxRows = Math.max(1, maxDataRows);
408
447
  let headerSeen = false;
@@ -454,7 +493,7 @@ function tsvToCsvTransform(maxDataRows, stats, onHeader, spreadsheetSafe = true)
454
493
  // which cancels the response and makes download() remove the partial file.
455
494
  return new TransformStream({
456
495
  transform(chunk, controller) {
457
- consumeTsvChunk(decoder.write(chunk), state, (row) => handleRow(row, (line) => queueLine(controller, line)));
496
+ consumeTsvChunk(decoder.decode(chunk, { stream: true, }), state, (row) => handleRow(row, (line) => queueLine(controller, line)));
458
497
  if (done) {
459
498
  // Truncation reached: emit the batch queued so far, end the output,
460
499
  // and cancel the rest of the response.
@@ -463,7 +502,7 @@ function tsvToCsvTransform(maxDataRows, stats, onHeader, spreadsheetSafe = true)
463
502
  }
464
503
  },
465
504
  flush(controller) {
466
- const tail = decoder.end();
505
+ const tail = decoder.decode();
467
506
  if (tail !== "") {
468
507
  consumeTsvChunk(tail, state, (row) => handleRow(row, (line) => queueLine(controller, line)));
469
508
  }
@@ -660,19 +699,35 @@ export class DatasetsResource extends BaseResource {
660
699
  /**
661
700
  * TSV data stream of at most `limit` rows. A column or partition selection
662
701
  * uses the documented POST variant of the data endpoint (body `columns`,
663
- * `partitions`, `format`, `sampling`); otherwise the GET endpoint.
702
+ * `partitions`, `format`, `sampling`); otherwise the GET endpoint. Both carry
703
+ * a client-generated `readSessionId` for {@link verifyStreamComplete}.
664
704
  */
665
705
  async dataStream(datasetName, limit, projectKey, selection) {
706
+ const readSessionId = crypto.randomUUID();
666
707
  const path = `/public/api/projects/${this.enc(projectKey)}/datasets/${encodeURIComponent(datasetName)}/data/`;
667
708
  if (!selection?.columns?.length && !selection?.partitions) {
668
- return this.client.stream(`${path}?format=tsv-excel-header&limit=${limit}`);
709
+ const res = await this.client.stream(`${path}?format=tsv-excel-header&limit=${limit}&readSessionId=${readSessionId}`);
710
+ return { res, readSessionId, };
669
711
  }
670
- return this.client.postStream(path, {
712
+ const res = await this.client.postStream(path, {
671
713
  format: "tsv-excel-header",
672
714
  ...(selection.columns?.length ? { columns: selection.columns, } : {}),
673
715
  ...(selection.partitions ? { partitions: selection.partitions, } : {}),
674
716
  sampling: { samplingMethod: "HEAD_SEQUENTIAL", maxRecords: limit, },
717
+ readSessionId,
675
718
  });
719
+ return { res, readSessionId, };
720
+ }
721
+ /**
722
+ * A data stream that ended normally may still be incomplete: DSS sends 200
723
+ * headers before reading the data, so a server-side read failure only cuts
724
+ * the body short. As dataikuapi does, ask DSS whether the read session
725
+ * finished; it answers non-2xx (DataikuError) when it did not. Only called
726
+ * after the body was read to its end — a stream the client cancelled at
727
+ * its row cap is not complete by design.
728
+ */
729
+ async verifyStreamComplete(datasetName, projectKey, readSessionId) {
730
+ await this.client.getTextLimited(`/public/api/projects/${this.enc(projectKey)}/datasets/${encodeURIComponent(datasetName)}/finish-streaming/?readSessionId=${readSessionId}`, 64 * 1024);
676
731
  }
677
732
  /**
678
733
  * Preview dataset rows as structured data: column names plus row arrays,
@@ -691,7 +746,7 @@ export class DatasetsResource extends BaseResource {
691
746
  const timeoutMs = Math.max(1, opts?.timeoutMs ?? this.client.getRequestTimeoutMs());
692
747
  // Probe one row past the cap so callers learn whether the dataset holds
693
748
  // more rows than requested without materializing the full dataset.
694
- const res = await this.dataStream(datasetName, maxRows + 1, opts?.projectKey, opts);
749
+ const { res, readSessionId, } = await this.dataStream(datasetName, maxRows + 1, opts?.projectKey, opts);
695
750
  const onHeader = opts?.validateColumns
696
751
  ? (headerRow) => {
697
752
  const warnings = validateStreamColumns(headerRow, opts.validateColumns);
@@ -704,6 +759,8 @@ export class DatasetsResource extends BaseResource {
704
759
  return { columns: [], rows: [], rowCount: 0, truncated: false, limit: maxRows, };
705
760
  }
706
761
  const { columns, rows, truncated, } = await collectPreviewRows(res.body, maxRows, timeoutMs, onHeader);
762
+ if (!truncated)
763
+ await this.verifyStreamComplete(datasetName, opts?.projectKey, readSessionId);
707
764
  return {
708
765
  columns: columns.map((name) => ({ name, })),
709
766
  rows,
@@ -730,12 +787,13 @@ export class DatasetsResource extends BaseResource {
730
787
  const target = expected;
731
788
  const probe = target + 1;
732
789
  const timeoutMs = Math.max(1, this.client.getRequestTimeoutMs());
733
- const dsEnc = encodeURIComponent(datasetName);
734
- const res = await this.client.stream(`/public/api/projects/${this.enc(projectKey)}/datasets/${dsEnc}/data/?format=tsv-excel-header&limit=${probe}`);
790
+ const { res, readSessionId, } = await this.dataStream(datasetName, probe, projectKey, undefined);
735
791
  if (!res.body) {
736
792
  return { expected: target, count: 0, exact: true, satisfied: target === 0, };
737
793
  }
738
794
  const { count, bounded, } = await collectTsvRowCount(res.body, probe, timeoutMs);
795
+ if (!bounded)
796
+ await this.verifyStreamComplete(datasetName, projectKey, readSessionId);
739
797
  return { expected: target, count, exact: !bounded, satisfied: !bounded && count === target, };
740
798
  }
741
799
  /** Get dataset metadata (tags, custom fields, checklists). */
@@ -873,7 +931,7 @@ export class DatasetsResource extends BaseResource {
873
931
  */
874
932
  async download(datasetName, opts) {
875
933
  const limit = Math.max(1, opts?.limit ?? 100_000);
876
- const res = await this.dataStream(datasetName, limit + 1, opts?.projectKey, opts);
934
+ const { res, readSessionId, } = await this.dataStream(datasetName, limit + 1, opts?.projectKey, opts);
877
935
  const safeDatasetName = sanitizeFileName(datasetName, "dataset");
878
936
  const filePath = opts?.outputPath?.endsWith(".gz") || opts?.outputPath?.endsWith(".csv")
879
937
  ? nodePath.resolve(opts.outputPath)
@@ -895,6 +953,9 @@ export class DatasetsResource extends BaseResource {
895
953
  try {
896
954
  // Native streaming write with backpressure; no Node stream layer.
897
955
  await Bun.write(filePath, new Response(csv));
956
+ if (!stats.truncated) {
957
+ await this.verifyStreamComplete(datasetName, opts?.projectKey, readSessionId);
958
+ }
898
959
  }
899
960
  catch (error) {
900
961
  // A failed export (byte-limit overflow, network error) must never