@openfn/language-common 1.6.2 → 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
@@ -993,6 +993,59 @@
993
993
  },
994
994
  "valid": true
995
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
+ },
996
1049
  {
997
1050
  "name": "map",
998
1051
  "params": [
package/lib/Adaptor.js CHANGED
@@ -27,6 +27,7 @@ exports.composeNextState = composeNextState;
27
27
  exports.humanProper = humanProper;
28
28
  exports.splitKeys = splitKeys;
29
29
  exports.scrubEmojis = scrubEmojis;
30
+ exports.chunk = chunk;
30
31
  exports.dateFns = exports.http = exports.beta = exports.map = void 0;
31
32
 
32
33
  var _fp = require("lodash/fp");
@@ -593,4 +594,23 @@ function scrubEmojis(text, replacementChars) {
593
594
 
594
595
  const emojisPattern = /(\uFE0F|\u00a9|\u00ae|[\u2000-\u3300]|\ud83c[\ud000-\udfff]|\ud83d[\ud000-\udfff]|\ud83e[\ud000-\udfff])/g;
595
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;
596
616
  }
@@ -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.2",
3
+ "version": "1.7.0",
4
4
  "description": "Common Expressions for OpenFn",
5
5
  "main": "lib/Adaptor.js",
6
6
  "scripts": {