@openfn/language-common 1.6.0 → 1.7.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.
package/ast.json CHANGED
@@ -903,7 +903,7 @@
903
903
  "str"
904
904
  ],
905
905
  "docs": {
906
- "description": "Subsitutes underscores for spaces and proper-cases a string",
906
+ "description": "Substitutes underscores for spaces and proper-cases a string",
907
907
  "tags": [
908
908
  {
909
909
  "title": "public",
@@ -941,12 +941,13 @@
941
941
  "valid": true
942
942
  },
943
943
  {
944
- "name": "removeEmojis",
944
+ "name": "scrubEmojis",
945
945
  "params": [
946
- "text"
946
+ "text",
947
+ "replacementChars"
947
948
  ],
948
949
  "docs": {
949
- "description": "Removes emojis from a string of text",
950
+ "description": "Replaces emojis in a string.",
950
951
  "tags": [
951
952
  {
952
953
  "title": "public",
@@ -955,7 +956,7 @@
955
956
  },
956
957
  {
957
958
  "title": "example",
958
- "description": "removeEmojis('Dove🕊️⭐ 29')"
959
+ "description": "scrubEmojis('Dove🕊️⭐ 29')"
959
960
  },
960
961
  {
961
962
  "title": "function",
@@ -971,6 +972,15 @@
971
972
  },
972
973
  "name": "text"
973
974
  },
975
+ {
976
+ "title": "param",
977
+ "description": "Characters that replace the emojis",
978
+ "type": {
979
+ "type": "NameExpression",
980
+ "name": "string"
981
+ },
982
+ "name": "replacementChars"
983
+ },
974
984
  {
975
985
  "title": "returns",
976
986
  "description": null,
@@ -983,6 +993,59 @@
983
993
  },
984
994
  "valid": true
985
995
  },
996
+ {
997
+ "name": "chunk",
998
+ "params": [
999
+ "array",
1000
+ "chunkSize"
1001
+ ],
1002
+ "docs": {
1003
+ "description": "Chunks an array into an array of arrays, each with no more than a certain size.",
1004
+ "tags": [
1005
+ {
1006
+ "title": "public",
1007
+ "description": null,
1008
+ "type": null
1009
+ },
1010
+ {
1011
+ "title": "example",
1012
+ "description": "chunk([1,2,3,4,5], 2)"
1013
+ },
1014
+ {
1015
+ "title": "function",
1016
+ "description": null,
1017
+ "name": null
1018
+ },
1019
+ {
1020
+ "title": "param",
1021
+ "description": "Array to be chunked",
1022
+ "type": {
1023
+ "type": "NameExpression",
1024
+ "name": "Object"
1025
+ },
1026
+ "name": "array"
1027
+ },
1028
+ {
1029
+ "title": "param",
1030
+ "description": "The maxiumum size of each chunks",
1031
+ "type": {
1032
+ "type": "NameExpression",
1033
+ "name": "Integer"
1034
+ },
1035
+ "name": "chunkSize"
1036
+ },
1037
+ {
1038
+ "title": "returns",
1039
+ "description": null,
1040
+ "type": {
1041
+ "type": "NameExpression",
1042
+ "name": "Object"
1043
+ }
1044
+ }
1045
+ ]
1046
+ },
1047
+ "valid": true
1048
+ },
986
1049
  {
987
1050
  "name": "map",
988
1051
  "params": [
package/lib/Adaptor.js CHANGED
@@ -26,8 +26,9 @@ exports.toArray = toArray;
26
26
  exports.composeNextState = composeNextState;
27
27
  exports.humanProper = humanProper;
28
28
  exports.splitKeys = splitKeys;
29
- exports.removeEmojis = removeEmojis;
30
- exports.http = exports.beta = exports.map = void 0;
29
+ exports.scrubEmojis = scrubEmojis;
30
+ exports.chunk = chunk;
31
+ exports.dateFns = exports.http = exports.beta = exports.map = void 0;
31
32
 
32
33
  var _fp = require("lodash/fp");
33
34
 
@@ -41,6 +42,10 @@ var _http = _interopRequireWildcard(require("./http"));
41
42
 
42
43
  exports.http = _http;
43
44
 
45
+ var _dateFns = _interopRequireWildcard(require("./dateFns"));
46
+
47
+ exports.dateFns = _dateFns;
48
+
44
49
  function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
45
50
 
46
51
  function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
@@ -533,7 +538,7 @@ function composeNextState(state, response) {
533
538
  };
534
539
  }
535
540
  /**
536
- * Subsitutes underscores for spaces and proper-cases a string
541
+ * Substitutes underscores for spaces and proper-cases a string
537
542
  * @public
538
543
  * @example
539
544
  * field("destination_string__c", humanProper(state.data.path_to_string))
@@ -569,18 +574,43 @@ function splitKeys(obj, keys) {
569
574
  }, [{}, {}]);
570
575
  }
571
576
  /**
572
- * Removes emojis from a string of text
577
+ * Replaces emojis in a string.
573
578
  * @public
574
579
  * @example
575
- * removeEmojis('Dove🕊️⭐ 29')
580
+ * scrubEmojis('Dove🕊️⭐ 29')
576
581
  * @function
577
582
  * @param {string} text - String that needs to be cleaned
583
+ * @param {string} replacementChars - Characters that replace the emojis
578
584
  * @returns {string}
579
585
  */
580
586
 
581
587
 
582
- function removeEmojis(text) {
588
+ function scrubEmojis(text, replacementChars) {
583
589
  if (!text) return text;
590
+
591
+ if (replacementChars == '') {
592
+ console.warn('Removing characters from a string may create injection vulnerabilities;', "It's better to replace than remove.", 'See https://www.unicode.org/reports/tr36/#Deletion_of_Noncharacters');
593
+ } else if (!replacementChars) replacementChars = '\uFFFD';
594
+
584
595
  const emojisPattern = /(\uFE0F|\u00a9|\u00ae|[\u2000-\u3300]|\ud83c[\ud000-\udfff]|\ud83d[\ud000-\udfff]|\ud83e[\ud000-\udfff])/g;
585
- return text.replace(emojisPattern, '');
596
+ return text.replace(emojisPattern, replacementChars);
597
+ }
598
+ /**
599
+ * Chunks an array into an array of arrays, each with no more than a certain size.
600
+ * @public
601
+ * @example
602
+ * chunk([1,2,3,4,5], 2)
603
+ * @function
604
+ * @param {Object} array - Array to be chunked
605
+ * @param {Integer} chunkSize - The maxiumum size of each chunks
606
+ * @returns {Object}
607
+ */
608
+
609
+
610
+ function chunk(array, chunkSize) {
611
+ const output = [];
612
+
613
+ for (var i = 0, len = array.length; i < len; i += chunkSize) output.push(array.slice(i, i + chunkSize));
614
+
615
+ return output;
586
616
  }
package/lib/dateFns.js ADDED
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ Object.defineProperty(exports, "parse", {
7
+ enumerable: true,
8
+ get: function () {
9
+ return _dateFns.parse;
10
+ }
11
+ });
12
+ Object.defineProperty(exports, "format", {
13
+ enumerable: true,
14
+ get: function () {
15
+ return _dateFns.format;
16
+ }
17
+ });
18
+
19
+ var _dateFns = require("date-fns");
@@ -18,7 +18,7 @@ events.forEach(function (event) {
18
18
  // Error types with codes
19
19
  var RedirectionError = createErrorType(
20
20
  "ERR_FR_REDIRECTION_FAILURE",
21
- ""
21
+ "Redirected request failed"
22
22
  );
23
23
  var TooManyRedirectsError = createErrorType(
24
24
  "ERR_FR_TOO_MANY_REDIRECTS",
@@ -147,10 +147,8 @@ RedirectableRequest.prototype.removeHeader = function (name) {
147
147
  // Global timeout for all underlying requests
148
148
  RedirectableRequest.prototype.setTimeout = function (msecs, callback) {
149
149
  var self = this;
150
- if (callback) {
151
- this.on("timeout", callback);
152
- }
153
150
 
151
+ // Destroys the socket on timeout
154
152
  function destroyOnTimeout(socket) {
155
153
  socket.setTimeout(msecs);
156
154
  socket.removeListener("timeout", socket.destroy);
@@ -169,18 +167,32 @@ RedirectableRequest.prototype.setTimeout = function (msecs, callback) {
169
167
  destroyOnTimeout(socket);
170
168
  }
171
169
 
172
- // Prevent a timeout from triggering
170
+ // Stops a timeout from triggering
173
171
  function clearTimer() {
174
- clearTimeout(self._timeout);
172
+ // Clear the timeout
173
+ if (self._timeout) {
174
+ clearTimeout(self._timeout);
175
+ self._timeout = null;
176
+ }
177
+
178
+ // Clean up all attached listeners
179
+ self.removeListener("abort", clearTimer);
180
+ self.removeListener("error", clearTimer);
181
+ self.removeListener("response", clearTimer);
175
182
  if (callback) {
176
183
  self.removeListener("timeout", callback);
177
184
  }
178
- if (!this.socket) {
185
+ if (!self.socket) {
179
186
  self._currentRequest.removeListener("socket", startTimer);
180
187
  }
181
188
  }
182
189
 
183
- // Start the timer when the socket is opened
190
+ // Attach callback if passed
191
+ if (callback) {
192
+ this.on("timeout", callback);
193
+ }
194
+
195
+ // Start the timer if or when the socket is opened
184
196
  if (this.socket) {
185
197
  startTimer(this.socket);
186
198
  }
@@ -188,9 +200,11 @@ RedirectableRequest.prototype.setTimeout = function (msecs, callback) {
188
200
  this._currentRequest.once("socket", startTimer);
189
201
  }
190
202
 
203
+ // Clean up on events
191
204
  this.on("socket", destroyOnTimeout);
192
- this.once("response", clearTimer);
193
- this.once("error", clearTimer);
205
+ this.on("abort", clearTimer);
206
+ this.on("error", clearTimer);
207
+ this.on("response", clearTimer);
194
208
 
195
209
  return this;
196
210
  };
@@ -354,19 +368,34 @@ RedirectableRequest.prototype._processResponse = function (response) {
354
368
  }
355
369
 
356
370
  // Drop the Host header, as the redirect might lead to a different host
357
- var previousHostName = removeMatchingHeaders(/^host$/i, this._options.headers) ||
358
- url.parse(this._currentUrl).hostname;
371
+ var currentHostHeader = removeMatchingHeaders(/^host$/i, this._options.headers);
372
+
373
+ // If the redirect is relative, carry over the host of the last request
374
+ var currentUrlParts = url.parse(this._currentUrl);
375
+ var currentHost = currentHostHeader || currentUrlParts.host;
376
+ var currentUrl = /^\w+:/.test(location) ? this._currentUrl :
377
+ url.format(Object.assign(currentUrlParts, { host: currentHost }));
378
+
379
+ // Determine the URL of the redirection
380
+ var redirectUrl;
381
+ try {
382
+ redirectUrl = url.resolve(currentUrl, location);
383
+ }
384
+ catch (cause) {
385
+ this.emit("error", new RedirectionError(cause));
386
+ return;
387
+ }
359
388
 
360
389
  // Create the redirected request
361
- var redirectUrl = url.resolve(this._currentUrl, location);
362
390
  debug("redirecting to", redirectUrl);
363
391
  this._isRedirect = true;
364
392
  var redirectUrlParts = url.parse(redirectUrl);
365
393
  Object.assign(this._options, redirectUrlParts);
366
394
 
367
- // Drop the Authorization header if redirecting to another host
368
- if (redirectUrlParts.hostname !== previousHostName) {
369
- removeMatchingHeaders(/^authorization$/i, this._options.headers);
395
+ // Drop confidential headers when redirecting to another scheme:domain
396
+ if (redirectUrlParts.protocol !== currentUrlParts.protocol ||
397
+ !isSameOrSubdomain(redirectUrlParts.host, currentHost)) {
398
+ removeMatchingHeaders(/^(?:authorization|cookie)$/i, this._options.headers);
370
399
  }
371
400
 
372
401
  // Evaluate the beforeRedirect callback
@@ -387,9 +416,7 @@ RedirectableRequest.prototype._processResponse = function (response) {
387
416
  this._performRequest();
388
417
  }
389
418
  catch (cause) {
390
- var error = new RedirectionError("Redirected request failed: " + cause.message);
391
- error.cause = cause;
392
- this.emit("error", error);
419
+ this.emit("error", new RedirectionError(cause));
393
420
  }
394
421
  }
395
422
  else {
@@ -503,13 +530,20 @@ function removeMatchingHeaders(regex, headers) {
503
530
  delete headers[header];
504
531
  }
505
532
  }
506
- return lastValue;
533
+ return (lastValue === null || typeof lastValue === "undefined") ?
534
+ undefined : String(lastValue).trim();
507
535
  }
508
536
 
509
537
  function createErrorType(code, defaultMessage) {
510
- function CustomError(message) {
538
+ function CustomError(cause) {
511
539
  Error.captureStackTrace(this, this.constructor);
512
- this.message = message || defaultMessage;
540
+ if (!cause) {
541
+ this.message = defaultMessage;
542
+ }
543
+ else {
544
+ this.message = defaultMessage + ": " + cause.message;
545
+ this.cause = cause;
546
+ }
513
547
  }
514
548
  CustomError.prototype = new Error();
515
549
  CustomError.prototype.constructor = CustomError;
@@ -526,6 +560,14 @@ function abortRequest(request) {
526
560
  request.abort();
527
561
  }
528
562
 
563
+ function isSameOrSubdomain(subdomain, domain) {
564
+ if (subdomain === domain) {
565
+ return true;
566
+ }
567
+ const dot = subdomain.length - domain.length - 1;
568
+ return dot > 0 && subdomain[dot] === "." && subdomain.endsWith(domain);
569
+ }
570
+
529
571
  // Exports
530
572
  module.exports = wrap({ http: http, https: https });
531
573
  module.exports.wrap = wrap;
@@ -1,27 +1,32 @@
1
1
  {
2
- "_from": "follow-redirects@^1.14.0",
3
- "_id": "follow-redirects@1.14.3",
2
+ "_args": [
3
+ [
4
+ "follow-redirects@1.14.8",
5
+ "/workspaces/language-common"
6
+ ]
7
+ ],
8
+ "_from": "follow-redirects@1.14.8",
9
+ "_id": "follow-redirects@1.14.8",
4
10
  "_inBundle": false,
5
- "_integrity": "sha512-3MkHxknWMUtb23apkgz/83fDoe+y+qr0TdgacGIA7bew+QLBo3vdgEN2xEsuXNivpFy4CyDhBBZnNZOtalmenw==",
11
+ "_integrity": "sha512-1x0S9UVJHsQprFcEC/qnNzBLcIxsjAV905f/UkQxbclCsoTWlacCNOpQa/anodLl2uaEKFhfWOvM2Qg77+15zA==",
6
12
  "_location": "/follow-redirects",
7
13
  "_phantomChildren": {},
8
14
  "_requested": {
9
- "type": "range",
15
+ "type": "version",
10
16
  "registry": true,
11
- "raw": "follow-redirects@^1.14.0",
17
+ "raw": "follow-redirects@1.14.8",
12
18
  "name": "follow-redirects",
13
19
  "escapedName": "follow-redirects",
14
- "rawSpec": "^1.14.0",
20
+ "rawSpec": "1.14.8",
15
21
  "saveSpec": null,
16
- "fetchSpec": "^1.14.0"
22
+ "fetchSpec": "1.14.8"
17
23
  },
18
24
  "_requiredBy": [
19
25
  "/axios"
20
26
  ],
21
- "_resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.14.3.tgz",
22
- "_shasum": "6ada78118d8d24caee595595accdc0ac6abd022e",
23
- "_spec": "follow-redirects@^1.14.0",
24
- "_where": "/workspaces/language-common/node_modules/axios",
27
+ "_resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.14.8.tgz",
28
+ "_spec": "1.14.8",
29
+ "_where": "/workspaces/language-common",
25
30
  "author": {
26
31
  "name": "Ruben Verborgh",
27
32
  "email": "ruben@verborgh.org",
@@ -30,7 +35,6 @@
30
35
  "bugs": {
31
36
  "url": "https://github.com/follow-redirects/follow-redirects/issues"
32
37
  },
33
- "bundleDependencies": false,
34
38
  "contributors": [
35
39
  {
36
40
  "name": "Olivier Lalonde",
@@ -42,7 +46,6 @@
42
46
  "email": "james@talmage.io"
43
47
  }
44
48
  ],
45
- "deprecated": false,
46
49
  "description": "HTTP and HTTPS modules that follow redirects.",
47
50
  "devDependencies": {
48
51
  "concat-stream": "^2.0.0",
@@ -91,5 +94,5 @@
91
94
  "mocha": "nyc mocha",
92
95
  "test": "npm run lint && npm run mocha"
93
96
  },
94
- "version": "1.14.3"
97
+ "version": "1.14.8"
95
98
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@openfn/language-common",
3
- "version": "1.6.0",
3
+ "version": "1.7.0",
4
4
  "description": "Common Expressions for OpenFn",
5
5
  "main": "lib/Adaptor.js",
6
6
  "scripts": {
@@ -19,6 +19,7 @@
19
19
  ],
20
20
  "dependencies": {
21
21
  "axios": "^0.21.4",
22
+ "date-fns": "^2.25.0",
22
23
  "jsonpath-plus": "^4.0.0",
23
24
  "lodash": "^4.17.19"
24
25
  },