@openfn/language-common 2.3.1 → 2.3.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
@@ -162,6 +162,31 @@ var throw_error_default = (code, { description, fix, ...extras } = {}) => {
162
162
  throw e;
163
163
  };
164
164
 
165
+ // src/util/base64.js
166
+ var import_lodash = __toESM(require("lodash"), 1);
167
+ var encode = (data, options2 = { parseJson: true }) => {
168
+ let str = data;
169
+ if (typeof data !== "string" && options2.parseJson) {
170
+ try {
171
+ str = JSON.stringify(str);
172
+ } catch (e) {
173
+ console.log(e.message);
174
+ }
175
+ }
176
+ return Buffer.from(str, "utf-8").toString("base64");
177
+ };
178
+ var decode = (base64Data, options2 = { parseJson: true }) => {
179
+ let decodedValue = Buffer.from(base64Data, "base64").toString("utf-8");
180
+ if ((import_lodash.default.startsWith(decodedValue, "[") || import_lodash.default.startsWith(decodedValue, "{")) && options2.parseJson) {
181
+ try {
182
+ decodedValue = JSON.parse(decodedValue);
183
+ } catch (e) {
184
+ console.log(e.message);
185
+ }
186
+ }
187
+ return decodedValue;
188
+ };
189
+
165
190
  // src/util/http.js
166
191
  var clients = /* @__PURE__ */ new Map();
167
192
  var makeBasicAuthHeader = (username, password) => {
@@ -327,9 +352,11 @@ function encodeRequestBody(body) {
327
352
  throw new Error("Unsupported body type");
328
353
  }
329
354
  async function readResponseBody(response, parseAs) {
355
+ var _a;
356
+ const contentLength = ((_a = response.body) == null ? void 0 : _a.readableLength) ?? 0;
357
+ const contentType = response.headers["content-type"];
330
358
  try {
331
- const contentType = response.headers["content-type"];
332
- if (+response.headers["content-length"] === 0) {
359
+ if (Number.isNaN(contentLength) || contentLength === 0) {
333
360
  return void 0;
334
361
  }
335
362
  switch (parseAs) {
@@ -339,6 +366,9 @@ async function readResponseBody(response, parseAs) {
339
366
  return response.body.text();
340
367
  case "stream":
341
368
  return response.body;
369
+ case "base64":
370
+ const arrayBuffer = await response.body.arrayBuffer();
371
+ return encode(arrayBuffer, { parseJson: false });
342
372
  default:
343
373
  return contentType && contentType.includes("application/json") ? await response.body.json() : response.body.text();
344
374
  }
@@ -347,7 +377,7 @@ async function readResponseBody(response, parseAs) {
347
377
  description: "Error parsing the response body",
348
378
  parseAs,
349
379
  contentType: response.headers["content-type"],
350
- bodyLength: +response.headers["content-length"],
380
+ bodyLength: contentLength,
351
381
  error: error.message
352
382
  });
353
383
  }
@@ -434,31 +464,6 @@ var parse_date_default = (d, startDate) => {
434
464
  return d;
435
465
  };
436
466
 
437
- // src/util/base64.js
438
- var import_lodash = __toESM(require("lodash"), 1);
439
- var encode = (data, options2 = { parseJson: true }) => {
440
- let str = data;
441
- if (typeof data !== "string" && options2.parseJson) {
442
- try {
443
- str = JSON.stringify(str);
444
- } catch (e) {
445
- console.log(e.message);
446
- }
447
- }
448
- return Buffer.from(str, "utf-8").toString("base64");
449
- };
450
- var decode = (base64Data, options2 = { parseJson: true }) => {
451
- let decodedValue = Buffer.from(base64Data, "base64").toString("utf-8");
452
- if ((import_lodash.default.startsWith(decodedValue, "[") || import_lodash.default.startsWith(decodedValue, "{")) && options2.parseJson) {
453
- try {
454
- decodedValue = JSON.parse(decodedValue);
455
- } catch (e) {
456
- console.log(e.message);
457
- }
458
- }
459
- return decodedValue;
460
- };
461
-
462
467
  // src/util/uuid.js
463
468
  var import_node_crypto = require("crypto");
464
469
  var uuid = () => (0, import_node_crypto.randomUUID)();
package/dist/index.js CHANGED
@@ -100,6 +100,31 @@ var throw_error_default = (code, { description, fix, ...extras } = {}) => {
100
100
  throw e;
101
101
  };
102
102
 
103
+ // src/util/base64.js
104
+ import _ from "lodash";
105
+ var encode = (data, options2 = { parseJson: true }) => {
106
+ let str = data;
107
+ if (typeof data !== "string" && options2.parseJson) {
108
+ try {
109
+ str = JSON.stringify(str);
110
+ } catch (e) {
111
+ console.log(e.message);
112
+ }
113
+ }
114
+ return Buffer.from(str, "utf-8").toString("base64");
115
+ };
116
+ var decode = (base64Data, options2 = { parseJson: true }) => {
117
+ let decodedValue = Buffer.from(base64Data, "base64").toString("utf-8");
118
+ if ((_.startsWith(decodedValue, "[") || _.startsWith(decodedValue, "{")) && options2.parseJson) {
119
+ try {
120
+ decodedValue = JSON.parse(decodedValue);
121
+ } catch (e) {
122
+ console.log(e.message);
123
+ }
124
+ }
125
+ return decodedValue;
126
+ };
127
+
103
128
  // src/util/http.js
104
129
  var clients = /* @__PURE__ */ new Map();
105
130
  var makeBasicAuthHeader = (username, password) => {
@@ -265,9 +290,11 @@ function encodeRequestBody(body) {
265
290
  throw new Error("Unsupported body type");
266
291
  }
267
292
  async function readResponseBody(response, parseAs) {
293
+ var _a;
294
+ const contentLength = ((_a = response.body) == null ? void 0 : _a.readableLength) ?? 0;
295
+ const contentType = response.headers["content-type"];
268
296
  try {
269
- const contentType = response.headers["content-type"];
270
- if (+response.headers["content-length"] === 0) {
297
+ if (Number.isNaN(contentLength) || contentLength === 0) {
271
298
  return void 0;
272
299
  }
273
300
  switch (parseAs) {
@@ -277,6 +304,9 @@ async function readResponseBody(response, parseAs) {
277
304
  return response.body.text();
278
305
  case "stream":
279
306
  return response.body;
307
+ case "base64":
308
+ const arrayBuffer = await response.body.arrayBuffer();
309
+ return encode(arrayBuffer, { parseJson: false });
280
310
  default:
281
311
  return contentType && contentType.includes("application/json") ? await response.body.json() : response.body.text();
282
312
  }
@@ -285,7 +315,7 @@ async function readResponseBody(response, parseAs) {
285
315
  description: "Error parsing the response body",
286
316
  parseAs,
287
317
  contentType: response.headers["content-type"],
288
- bodyLength: +response.headers["content-length"],
318
+ bodyLength: contentLength,
289
319
  error: error.message
290
320
  });
291
321
  }
@@ -372,31 +402,6 @@ var parse_date_default = (d, startDate) => {
372
402
  return d;
373
403
  };
374
404
 
375
- // src/util/base64.js
376
- import _ from "lodash";
377
- var encode = (data, options2 = { parseJson: true }) => {
378
- let str = data;
379
- if (typeof data !== "string" && options2.parseJson) {
380
- try {
381
- str = JSON.stringify(str);
382
- } catch (e) {
383
- console.log(e.message);
384
- }
385
- }
386
- return Buffer.from(str, "utf-8").toString("base64");
387
- };
388
- var decode = (base64Data, options2 = { parseJson: true }) => {
389
- let decodedValue = Buffer.from(base64Data, "base64").toString("utf-8");
390
- if ((_.startsWith(decodedValue, "[") || _.startsWith(decodedValue, "{")) && options2.parseJson) {
391
- try {
392
- decodedValue = JSON.parse(decodedValue);
393
- } catch (e) {
394
- console.log(e.message);
395
- }
396
- }
397
- return decodedValue;
398
- };
399
-
400
405
  // src/util/uuid.js
401
406
  import { randomUUID } from "crypto";
402
407
  var uuid = () => randomUUID();
package/dist/util.cjs CHANGED
@@ -71,6 +71,31 @@ var throw_error_default = (code, { description, fix, ...extras } = {}) => {
71
71
  throw e;
72
72
  };
73
73
 
74
+ // src/util/base64.js
75
+ var import_lodash = __toESM(require("lodash"), 1);
76
+ var encode = (data, options = { parseJson: true }) => {
77
+ let str = data;
78
+ if (typeof data !== "string" && options.parseJson) {
79
+ try {
80
+ str = JSON.stringify(str);
81
+ } catch (e) {
82
+ console.log(e.message);
83
+ }
84
+ }
85
+ return Buffer.from(str, "utf-8").toString("base64");
86
+ };
87
+ var decode = (base64Data, options = { parseJson: true }) => {
88
+ let decodedValue = Buffer.from(base64Data, "base64").toString("utf-8");
89
+ if ((import_lodash.default.startsWith(decodedValue, "[") || import_lodash.default.startsWith(decodedValue, "{")) && options.parseJson) {
90
+ try {
91
+ decodedValue = JSON.parse(decodedValue);
92
+ } catch (e) {
93
+ console.log(e.message);
94
+ }
95
+ }
96
+ return decodedValue;
97
+ };
98
+
74
99
  // src/util/http.js
75
100
  var clients = /* @__PURE__ */ new Map();
76
101
  var makeBasicAuthHeader = (username, password) => {
@@ -236,9 +261,11 @@ function encodeRequestBody(body) {
236
261
  throw new Error("Unsupported body type");
237
262
  }
238
263
  async function readResponseBody(response, parseAs) {
264
+ var _a;
265
+ const contentLength = ((_a = response.body) == null ? void 0 : _a.readableLength) ?? 0;
266
+ const contentType = response.headers["content-type"];
239
267
  try {
240
- const contentType = response.headers["content-type"];
241
- if (+response.headers["content-length"] === 0) {
268
+ if (Number.isNaN(contentLength) || contentLength === 0) {
242
269
  return void 0;
243
270
  }
244
271
  switch (parseAs) {
@@ -248,6 +275,9 @@ async function readResponseBody(response, parseAs) {
248
275
  return response.body.text();
249
276
  case "stream":
250
277
  return response.body;
278
+ case "base64":
279
+ const arrayBuffer = await response.body.arrayBuffer();
280
+ return encode(arrayBuffer, { parseJson: false });
251
281
  default:
252
282
  return contentType && contentType.includes("application/json") ? await response.body.json() : response.body.text();
253
283
  }
@@ -256,7 +286,7 @@ async function readResponseBody(response, parseAs) {
256
286
  description: "Error parsing the response body",
257
287
  parseAs,
258
288
  contentType: response.headers["content-type"],
259
- bodyLength: +response.headers["content-length"],
289
+ bodyLength: contentLength,
260
290
  error: error.message
261
291
  });
262
292
  }
@@ -343,31 +373,6 @@ var parse_date_default = (d, startDate) => {
343
373
  return d;
344
374
  };
345
375
 
346
- // src/util/base64.js
347
- var import_lodash = __toESM(require("lodash"), 1);
348
- var encode = (data, options = { parseJson: true }) => {
349
- let str = data;
350
- if (typeof data !== "string" && options.parseJson) {
351
- try {
352
- str = JSON.stringify(str);
353
- } catch (e) {
354
- console.log(e.message);
355
- }
356
- }
357
- return Buffer.from(str, "utf-8").toString("base64");
358
- };
359
- var decode = (base64Data, options = { parseJson: true }) => {
360
- let decodedValue = Buffer.from(base64Data, "base64").toString("utf-8");
361
- if ((import_lodash.default.startsWith(decodedValue, "[") || import_lodash.default.startsWith(decodedValue, "{")) && options.parseJson) {
362
- try {
363
- decodedValue = JSON.parse(decodedValue);
364
- } catch (e) {
365
- console.log(e.message);
366
- }
367
- }
368
- return decodedValue;
369
- };
370
-
371
376
  // src/util/uuid.js
372
377
  var import_node_crypto = require("crypto");
373
378
  var uuid = () => (0, import_node_crypto.randomUUID)();
package/dist/util.js CHANGED
@@ -22,6 +22,31 @@ var throw_error_default = (code, { description, fix, ...extras } = {}) => {
22
22
  throw e;
23
23
  };
24
24
 
25
+ // src/util/base64.js
26
+ import _ from "lodash";
27
+ var encode = (data, options = { parseJson: true }) => {
28
+ let str = data;
29
+ if (typeof data !== "string" && options.parseJson) {
30
+ try {
31
+ str = JSON.stringify(str);
32
+ } catch (e) {
33
+ console.log(e.message);
34
+ }
35
+ }
36
+ return Buffer.from(str, "utf-8").toString("base64");
37
+ };
38
+ var decode = (base64Data, options = { parseJson: true }) => {
39
+ let decodedValue = Buffer.from(base64Data, "base64").toString("utf-8");
40
+ if ((_.startsWith(decodedValue, "[") || _.startsWith(decodedValue, "{")) && options.parseJson) {
41
+ try {
42
+ decodedValue = JSON.parse(decodedValue);
43
+ } catch (e) {
44
+ console.log(e.message);
45
+ }
46
+ }
47
+ return decodedValue;
48
+ };
49
+
25
50
  // src/util/http.js
26
51
  var clients = /* @__PURE__ */ new Map();
27
52
  var makeBasicAuthHeader = (username, password) => {
@@ -187,9 +212,11 @@ function encodeRequestBody(body) {
187
212
  throw new Error("Unsupported body type");
188
213
  }
189
214
  async function readResponseBody(response, parseAs) {
215
+ var _a;
216
+ const contentLength = ((_a = response.body) == null ? void 0 : _a.readableLength) ?? 0;
217
+ const contentType = response.headers["content-type"];
190
218
  try {
191
- const contentType = response.headers["content-type"];
192
- if (+response.headers["content-length"] === 0) {
219
+ if (Number.isNaN(contentLength) || contentLength === 0) {
193
220
  return void 0;
194
221
  }
195
222
  switch (parseAs) {
@@ -199,6 +226,9 @@ async function readResponseBody(response, parseAs) {
199
226
  return response.body.text();
200
227
  case "stream":
201
228
  return response.body;
229
+ case "base64":
230
+ const arrayBuffer = await response.body.arrayBuffer();
231
+ return encode(arrayBuffer, { parseJson: false });
202
232
  default:
203
233
  return contentType && contentType.includes("application/json") ? await response.body.json() : response.body.text();
204
234
  }
@@ -207,7 +237,7 @@ async function readResponseBody(response, parseAs) {
207
237
  description: "Error parsing the response body",
208
238
  parseAs,
209
239
  contentType: response.headers["content-type"],
210
- bodyLength: +response.headers["content-length"],
240
+ bodyLength: contentLength,
211
241
  error: error.message
212
242
  });
213
243
  }
@@ -294,31 +324,6 @@ var parse_date_default = (d, startDate) => {
294
324
  return d;
295
325
  };
296
326
 
297
- // src/util/base64.js
298
- import _ from "lodash";
299
- var encode = (data, options = { parseJson: true }) => {
300
- let str = data;
301
- if (typeof data !== "string" && options.parseJson) {
302
- try {
303
- str = JSON.stringify(str);
304
- } catch (e) {
305
- console.log(e.message);
306
- }
307
- }
308
- return Buffer.from(str, "utf-8").toString("base64");
309
- };
310
- var decode = (base64Data, options = { parseJson: true }) => {
311
- let decodedValue = Buffer.from(base64Data, "base64").toString("utf-8");
312
- if ((_.startsWith(decodedValue, "[") || _.startsWith(decodedValue, "{")) && options.parseJson) {
313
- try {
314
- decodedValue = JSON.parse(decodedValue);
315
- } catch (e) {
316
- console.log(e.message);
317
- }
318
- }
319
- return decodedValue;
320
- };
321
-
322
327
  // src/util/uuid.js
323
328
  import { randomUUID } from "crypto";
324
329
  var uuid = () => randomUUID();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@openfn/language-common",
3
- "version": "2.3.1",
3
+ "version": "2.3.3",
4
4
  "description": "Common Expressions for OpenFn",
5
5
  "homepage": "https://docs.openfn.org",
6
6
  "repository": {
@@ -39,7 +39,7 @@
39
39
  "csvtojson": "^2.0.10",
40
40
  "date-fns": "^2.25.0",
41
41
  "http-status-codes": "^2.3.0",
42
- "jsonpath-plus": "^10.2.0",
42
+ "jsonpath-plus": "^10.3.0",
43
43
  "lodash": "^4.17.21",
44
44
  "undici": "^5.28.5"
45
45
  },