@openfn/language-common 1.10.1 → 1.10.3

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.cjs CHANGED
@@ -38,7 +38,7 @@ __export(src_exports, {
38
38
  default: () => src_default,
39
39
  each: () => each2,
40
40
  execute: () => execute,
41
- expandReferences: () => expandReferences,
41
+ expandReferences: () => expandReferences2,
42
42
  field: () => field,
43
43
  fields: () => fields,
44
44
  fn: () => fn,
@@ -75,7 +75,7 @@ __export(Adaptor_exports, {
75
75
  dateFns: () => dateFns_exports,
76
76
  each: () => each2,
77
77
  execute: () => execute,
78
- expandReferences: () => expandReferences,
78
+ expandReferences: () => expandReferences2,
79
79
  field: () => field,
80
80
  fields: () => fields,
81
81
  fn: () => fn,
@@ -99,6 +99,38 @@ var import_curry = __toESM(require("lodash/fp/curry.js"), 1);
99
99
  var import_fromPairs = __toESM(require("lodash/fp/fromPairs.js"), 1);
100
100
  var import_jsonpath_plus = require("jsonpath-plus");
101
101
  var import_csv_parse = require("csv-parse");
102
+ var import_node_stream2 = require("stream");
103
+
104
+ // src/util.js
105
+ var import_node_stream = require("stream");
106
+ function expandReferences(state, ...args) {
107
+ return args.map((value) => expandReference(state, value));
108
+ }
109
+ var isStream = (value) => {
110
+ if (value && typeof value == "object") {
111
+ if (value instanceof import_node_stream.Readable || value instanceof import_node_stream.Writable) {
112
+ return true;
113
+ }
114
+ if (value.pipeTo || value.pipe) {
115
+ return true;
116
+ }
117
+ }
118
+ return false;
119
+ };
120
+ function expandReference(state, value) {
121
+ if (Array.isArray(value)) {
122
+ return value.map((v) => expandReference(state, v));
123
+ }
124
+ if (typeof value == "object" && !!value && !isStream(value)) {
125
+ return Object.keys(value).reduce((acc, key) => {
126
+ return { ...acc, [key]: expandReference(state, value[key]) };
127
+ }, {});
128
+ }
129
+ if (typeof value == "function") {
130
+ return expandReference(state, value(state));
131
+ }
132
+ return value;
133
+ }
102
134
 
103
135
  // src/beta.js
104
136
  var beta_exports = {};
@@ -165,7 +197,7 @@ function expandRequestReferences(params) {
165
197
  return true;
166
198
  };
167
199
  return (state) => {
168
- const expandedParams = expandReferences(toExpand, skipFormData)(state);
200
+ const expandedParams = expandReferences2(toExpand, skipFormData)(state);
169
201
  const safelyExpandedParams = { ...toKeep, ...expandedParams };
170
202
  return safelyExpandedParams;
171
203
  };
@@ -333,20 +365,20 @@ function join(targetPath, sourcePath, targetKey) {
333
365
  });
334
366
  };
335
367
  }
336
- function expandReferences(value, skipFilter) {
368
+ function expandReferences2(value, skipFilter) {
337
369
  return (state) => {
338
370
  if (skipFilter && skipFilter(value))
339
371
  return value;
340
372
  if (Array.isArray(value)) {
341
- return value.map((v) => expandReferences(v)(state));
373
+ return value.map((v) => expandReferences2(v)(state));
342
374
  }
343
375
  if (typeof value == "object" && !!value) {
344
376
  return Object.keys(value).reduce((acc, key) => {
345
- return { ...acc, [key]: expandReferences(value[key])(state) };
377
+ return { ...acc, [key]: expandReferences2(value[key])(state) };
346
378
  }, {});
347
379
  }
348
380
  if (typeof value == "function") {
349
- return expandReferences(value(state))(state);
381
+ return expandReferences2(value(state))(state);
350
382
  }
351
383
  return value;
352
384
  };
@@ -360,7 +392,7 @@ function fields(...fields2) {
360
392
  function merge(dataSource, fields2) {
361
393
  return (state) => {
362
394
  const initialData = source(dataSource)(state);
363
- const additionalData = expandReferences(fields2)(state);
395
+ const additionalData = expandReferences2(fields2)(state);
364
396
  return initialData.reduce((acc, dataItem) => {
365
397
  return [...acc, { ...dataItem, ...additionalData }];
366
398
  }, []);
@@ -425,6 +457,16 @@ function chunk(array, chunkSize) {
425
457
  output.push(array.slice(i, i + chunkSize));
426
458
  return output;
427
459
  }
460
+ var getParser = (csvData, options2) => {
461
+ if (typeof csvData === "string") {
462
+ return (0, import_csv_parse.parse)(csvData, options2);
463
+ }
464
+ let stream = csvData;
465
+ if (csvData instanceof ReadableStream) {
466
+ stream = import_node_stream2.Readable.from(csvData);
467
+ }
468
+ return stream.pipe((0, import_csv_parse.parse)(options2));
469
+ };
428
470
  function parseCsv(csvData, parsingOptions = {}, callback) {
429
471
  const defaultOptions = {
430
472
  delimiter: ",",
@@ -438,31 +480,41 @@ function parseCsv(csvData, parsingOptions = {}, callback) {
438
480
  chunkSize: Infinity,
439
481
  skip_empty_lines: true
440
482
  };
441
- const filteredOptions = Object.fromEntries(
442
- Object.entries(parsingOptions).filter(([key]) => key in defaultOptions)
443
- );
444
- const options2 = { ...defaultOptions, ...filteredOptions };
445
- if (options2.chunkSize < 1) {
446
- throw new Error("chunkSize must be at least 1");
447
- }
448
483
  return async (state) => {
484
+ const [resolvedCsvData, resolvedParsingOptions] = expandReferences(
485
+ state,
486
+ csvData,
487
+ parsingOptions
488
+ );
489
+ const filteredOptions = Object.fromEntries(
490
+ Object.entries(resolvedParsingOptions).filter(
491
+ ([key]) => key in defaultOptions
492
+ )
493
+ );
494
+ const options2 = { ...defaultOptions, ...filteredOptions };
495
+ if (options2.chunkSize < 1) {
496
+ throw new Error("chunkSize must be at least 1");
497
+ }
449
498
  let buffer = [];
450
- const parser = typeof csvData === "string" ? (0, import_csv_parse.parse)(csvData, options2) : csvData.pipe((0, import_csv_parse.parse)(options2));
499
+ const parser = getParser(resolvedCsvData, options2);
451
500
  const flushBuffer = async (currentState) => {
452
501
  const nextState = callback ? await callback(currentState, buffer) : composeNextState(currentState, buffer);
453
502
  buffer = [];
454
503
  return [nextState, buffer];
455
504
  };
505
+ let result = state;
456
506
  for await (const record of parser) {
457
507
  buffer.push(record);
458
508
  if (buffer.length === options2.chunkSize) {
459
- const [nextState, nextBuffer] = await flushBuffer(state);
460
- state = nextState;
509
+ const [nextState, nextBuffer] = await flushBuffer(result);
510
+ result = nextState;
461
511
  buffer = nextBuffer;
462
512
  }
463
513
  }
464
- const [finalState] = await flushBuffer(state);
465
- return finalState;
514
+ if (buffer.length) {
515
+ [result] = await flushBuffer(result);
516
+ }
517
+ return result;
466
518
  };
467
519
  }
468
520
 
package/dist/index.js CHANGED
@@ -19,7 +19,7 @@ __export(Adaptor_exports, {
19
19
  dateFns: () => dateFns_exports,
20
20
  each: () => each2,
21
21
  execute: () => execute,
22
- expandReferences: () => expandReferences,
22
+ expandReferences: () => expandReferences2,
23
23
  field: () => field,
24
24
  fields: () => fields,
25
25
  fn: () => fn,
@@ -43,6 +43,38 @@ import curry from "lodash/fp/curry.js";
43
43
  import fromPairs from "lodash/fp/fromPairs.js";
44
44
  import { JSONPath } from "jsonpath-plus";
45
45
  import { parse as parse2 } from "csv-parse";
46
+ import { Readable as Readable2 } from "stream";
47
+
48
+ // src/util.js
49
+ import { Readable, Writable } from "stream";
50
+ function expandReferences(state, ...args) {
51
+ return args.map((value) => expandReference(state, value));
52
+ }
53
+ var isStream = (value) => {
54
+ if (value && typeof value == "object") {
55
+ if (value instanceof Readable || value instanceof Writable) {
56
+ return true;
57
+ }
58
+ if (value.pipeTo || value.pipe) {
59
+ return true;
60
+ }
61
+ }
62
+ return false;
63
+ };
64
+ function expandReference(state, value) {
65
+ if (Array.isArray(value)) {
66
+ return value.map((v) => expandReference(state, v));
67
+ }
68
+ if (typeof value == "object" && !!value && !isStream(value)) {
69
+ return Object.keys(value).reduce((acc, key) => {
70
+ return { ...acc, [key]: expandReference(state, value[key]) };
71
+ }, {});
72
+ }
73
+ if (typeof value == "function") {
74
+ return expandReference(state, value(state));
75
+ }
76
+ return value;
77
+ }
46
78
 
47
79
  // src/beta.js
48
80
  var beta_exports = {};
@@ -109,7 +141,7 @@ function expandRequestReferences(params) {
109
141
  return true;
110
142
  };
111
143
  return (state) => {
112
- const expandedParams = expandReferences(toExpand, skipFormData)(state);
144
+ const expandedParams = expandReferences2(toExpand, skipFormData)(state);
113
145
  const safelyExpandedParams = { ...toKeep, ...expandedParams };
114
146
  return safelyExpandedParams;
115
147
  };
@@ -277,20 +309,20 @@ function join(targetPath, sourcePath, targetKey) {
277
309
  });
278
310
  };
279
311
  }
280
- function expandReferences(value, skipFilter) {
312
+ function expandReferences2(value, skipFilter) {
281
313
  return (state) => {
282
314
  if (skipFilter && skipFilter(value))
283
315
  return value;
284
316
  if (Array.isArray(value)) {
285
- return value.map((v) => expandReferences(v)(state));
317
+ return value.map((v) => expandReferences2(v)(state));
286
318
  }
287
319
  if (typeof value == "object" && !!value) {
288
320
  return Object.keys(value).reduce((acc, key) => {
289
- return { ...acc, [key]: expandReferences(value[key])(state) };
321
+ return { ...acc, [key]: expandReferences2(value[key])(state) };
290
322
  }, {});
291
323
  }
292
324
  if (typeof value == "function") {
293
- return expandReferences(value(state))(state);
325
+ return expandReferences2(value(state))(state);
294
326
  }
295
327
  return value;
296
328
  };
@@ -304,7 +336,7 @@ function fields(...fields2) {
304
336
  function merge(dataSource, fields2) {
305
337
  return (state) => {
306
338
  const initialData = source(dataSource)(state);
307
- const additionalData = expandReferences(fields2)(state);
339
+ const additionalData = expandReferences2(fields2)(state);
308
340
  return initialData.reduce((acc, dataItem) => {
309
341
  return [...acc, { ...dataItem, ...additionalData }];
310
342
  }, []);
@@ -369,6 +401,16 @@ function chunk(array, chunkSize) {
369
401
  output.push(array.slice(i, i + chunkSize));
370
402
  return output;
371
403
  }
404
+ var getParser = (csvData, options2) => {
405
+ if (typeof csvData === "string") {
406
+ return parse2(csvData, options2);
407
+ }
408
+ let stream = csvData;
409
+ if (csvData instanceof ReadableStream) {
410
+ stream = Readable2.from(csvData);
411
+ }
412
+ return stream.pipe(parse2(options2));
413
+ };
372
414
  function parseCsv(csvData, parsingOptions = {}, callback) {
373
415
  const defaultOptions = {
374
416
  delimiter: ",",
@@ -382,31 +424,41 @@ function parseCsv(csvData, parsingOptions = {}, callback) {
382
424
  chunkSize: Infinity,
383
425
  skip_empty_lines: true
384
426
  };
385
- const filteredOptions = Object.fromEntries(
386
- Object.entries(parsingOptions).filter(([key]) => key in defaultOptions)
387
- );
388
- const options2 = { ...defaultOptions, ...filteredOptions };
389
- if (options2.chunkSize < 1) {
390
- throw new Error("chunkSize must be at least 1");
391
- }
392
427
  return async (state) => {
428
+ const [resolvedCsvData, resolvedParsingOptions] = expandReferences(
429
+ state,
430
+ csvData,
431
+ parsingOptions
432
+ );
433
+ const filteredOptions = Object.fromEntries(
434
+ Object.entries(resolvedParsingOptions).filter(
435
+ ([key]) => key in defaultOptions
436
+ )
437
+ );
438
+ const options2 = { ...defaultOptions, ...filteredOptions };
439
+ if (options2.chunkSize < 1) {
440
+ throw new Error("chunkSize must be at least 1");
441
+ }
393
442
  let buffer = [];
394
- const parser = typeof csvData === "string" ? parse2(csvData, options2) : csvData.pipe(parse2(options2));
443
+ const parser = getParser(resolvedCsvData, options2);
395
444
  const flushBuffer = async (currentState) => {
396
445
  const nextState = callback ? await callback(currentState, buffer) : composeNextState(currentState, buffer);
397
446
  buffer = [];
398
447
  return [nextState, buffer];
399
448
  };
449
+ let result = state;
400
450
  for await (const record of parser) {
401
451
  buffer.push(record);
402
452
  if (buffer.length === options2.chunkSize) {
403
- const [nextState, nextBuffer] = await flushBuffer(state);
404
- state = nextState;
453
+ const [nextState, nextBuffer] = await flushBuffer(result);
454
+ result = nextState;
405
455
  buffer = nextBuffer;
406
456
  }
407
457
  }
408
- const [finalState] = await flushBuffer(state);
409
- return finalState;
458
+ if (buffer.length) {
459
+ [result] = await flushBuffer(result);
460
+ }
461
+ return result;
410
462
  };
411
463
  }
412
464
 
@@ -426,7 +478,7 @@ export {
426
478
  src_default as default,
427
479
  each2 as each,
428
480
  execute,
429
- expandReferences,
481
+ expandReferences2 as expandReferences,
430
482
  field,
431
483
  fields,
432
484
  fn,
package/dist/util.cjs CHANGED
@@ -23,14 +23,26 @@ __export(util_exports, {
23
23
  normalizeOauthConfig: () => normalizeOauthConfig
24
24
  });
25
25
  module.exports = __toCommonJS(util_exports);
26
+ var import_node_stream = require("stream");
26
27
  function expandReferences(state, ...args) {
27
28
  return args.map((value) => expandReference(state, value));
28
29
  }
30
+ var isStream = (value) => {
31
+ if (value && typeof value == "object") {
32
+ if (value instanceof import_node_stream.Readable || value instanceof import_node_stream.Writable) {
33
+ return true;
34
+ }
35
+ if (value.pipeTo || value.pipe) {
36
+ return true;
37
+ }
38
+ }
39
+ return false;
40
+ };
29
41
  function expandReference(state, value) {
30
42
  if (Array.isArray(value)) {
31
43
  return value.map((v) => expandReference(state, v));
32
44
  }
33
- if (typeof value == "object" && !!value) {
45
+ if (typeof value == "object" && !!value && !isStream(value)) {
34
46
  return Object.keys(value).reduce((acc, key) => {
35
47
  return { ...acc, [key]: expandReference(state, value[key]) };
36
48
  }, {});
package/dist/util.js CHANGED
@@ -1,12 +1,24 @@
1
1
  // src/util.js
2
+ import { Readable, Writable } from "stream";
2
3
  function expandReferences(state, ...args) {
3
4
  return args.map((value) => expandReference(state, value));
4
5
  }
6
+ var isStream = (value) => {
7
+ if (value && typeof value == "object") {
8
+ if (value instanceof Readable || value instanceof Writable) {
9
+ return true;
10
+ }
11
+ if (value.pipeTo || value.pipe) {
12
+ return true;
13
+ }
14
+ }
15
+ return false;
16
+ };
5
17
  function expandReference(state, value) {
6
18
  if (Array.isArray(value)) {
7
19
  return value.map((v) => expandReference(state, v));
8
20
  }
9
- if (typeof value == "object" && !!value) {
21
+ if (typeof value == "object" && !!value && !isStream(value)) {
10
22
  return Object.keys(value).reduce((acc, key) => {
11
23
  return { ...acc, [key]: expandReference(state, value[key]) };
12
24
  }, {});
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@openfn/language-common",
3
- "version": "1.10.1",
3
+ "version": "1.10.3",
4
4
  "description": "Common Expressions for OpenFn",
5
5
  "homepage": "https://docs.openfn.org",
6
6
  "repository": {
@@ -48,7 +48,8 @@
48
48
  "esno": "^0.16.3",
49
49
  "mocha": "9.2.2",
50
50
  "nock": "13.2.9",
51
- "rimraf": "^3.0.2"
51
+ "rimraf": "^3.0.2",
52
+ "undici": "^5.22.1"
52
53
  },
53
54
  "main": "dist/index.cjs",
54
55
  "scripts": {
@@ -0,0 +1,10 @@
1
+ /**
2
+ * General-purpose utility functions
3
+ *
4
+ * These are designed more for use in adaptor code than job code
5
+ * (but we could choose to export util from common)
6
+ *
7
+ * None of these functions are operation factories
8
+ */
9
+ export function expandReferences(state: any, ...args: any[]): any[];
10
+ export function normalizeOauthConfig(configuration: any): any;