@openfn/language-common 3.1.0 → 3.1.2

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
@@ -25,7 +25,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
25
25
  // src/index.js
26
26
  var src_exports = {};
27
27
  __export(src_exports, {
28
- _: () => import_lodash2.default,
28
+ _: () => import_lodash3.default,
29
29
  alterState: () => alterState,
30
30
  arrayToString: () => arrayToString,
31
31
  as: () => as,
@@ -72,7 +72,7 @@ module.exports = __toCommonJS(src_exports);
72
72
  // src/Adaptor.js
73
73
  var Adaptor_exports = {};
74
74
  __export(Adaptor_exports, {
75
- _: () => import_lodash2.default,
75
+ _: () => import_lodash3.default,
76
76
  alterState: () => alterState,
77
77
  arrayToString: () => arrayToString,
78
78
  as: () => as,
@@ -114,7 +114,7 @@ var import_csv_parse = require("csv-parse");
114
114
  var import_node_stream3 = require("stream");
115
115
  var import_undici2 = require("undici");
116
116
  var import_date_fns2 = __toESM(require("date-fns"), 1);
117
- var import_lodash2 = __toESM(require("lodash"), 1);
117
+ var import_lodash3 = __toESM(require("lodash"), 1);
118
118
 
119
119
  // src/util/index.js
120
120
  var util_exports = {};
@@ -127,6 +127,7 @@ __export(util_exports, {
127
127
  enableMockClient: () => enableMockClient,
128
128
  encode: () => encode,
129
129
  expandReferences: () => expandReferences,
130
+ generateAgentKey: () => generateAgentKey,
130
131
  get: () => get,
131
132
  logResponse: () => logResponse,
132
133
  makeBasicAuthHeader: () => makeBasicAuthHeader,
@@ -141,7 +142,6 @@ __export(util_exports, {
141
142
  });
142
143
 
143
144
  // src/util/http.js
144
- var import_undici = require("undici");
145
145
  var import_http_status_codes = require("http-status-codes");
146
146
  var import_node_stream = require("stream");
147
147
  var import_node_querystring = __toESM(require("querystring"), 1);
@@ -190,6 +190,8 @@ var decode = (base64Data, options2 = { parseJson: true }) => {
190
190
  };
191
191
 
192
192
  // src/util/http.js
193
+ var import_undici = require("undici");
194
+ var import_lodash2 = __toESM(require("lodash"), 1);
193
195
  var agents = /* @__PURE__ */ new Map();
194
196
  var makeBasicAuthHeader = (username, password) => {
195
197
  const buff = Buffer.from(`${username}:${password}`);
@@ -211,59 +213,93 @@ var logResponse = (response) => {
211
213
  }
212
214
  return response;
213
215
  };
214
- var getAgent = (origin, { tls = {}, ...agentOpts } = {}) => {
215
- if (!agents.has(origin)) {
216
+ var sortObject = (obj) => (0, import_lodash2.default)(obj).toPairs().filter(([_k, v]) => v !== void 0).sortBy(0).map(([k, v]) => {
217
+ if (v && typeof v === "object") {
218
+ if (!Object.keys(v).length) {
219
+ return "";
220
+ }
221
+ v = `${"{"}${sortObject(v)}${"}"}`;
222
+ }
223
+ return [k, v].join(":");
224
+ }).join("|");
225
+ var generateAgentKey = (baseUrl, agentOpts = {}) => {
226
+ if (Object.keys(agentOpts).length) {
227
+ const sortedSerializedOptions = sortObject(agentOpts);
228
+ if (sortedSerializedOptions.length) {
229
+ return `${baseUrl}+${sortedSerializedOptions}`;
230
+ }
231
+ }
232
+ return baseUrl;
233
+ };
234
+ var getDispatcher = (origin, options2 = {}) => {
235
+ const { tls = {}, defaultContentType, ...agentOpts } = options2;
236
+ const key = generateAgentKey(origin, options2);
237
+ if (!agents.has(key)) {
216
238
  const agent = new import_undici.Agent({
217
239
  connect: tls,
218
240
  ...agentOpts
219
- });
220
- agents.set(origin, agent);
241
+ }).compose(
242
+ import_undici.interceptors.redirect({
243
+ maxRedirections: agentOpts.maxRedirections
244
+ }),
245
+ import_undici.interceptors.decompress()
246
+ );
247
+ agents.set(key, agent);
221
248
  }
222
- return agents.get(origin);
249
+ return agents.get(key);
223
250
  };
224
251
  var enableMockClient = (baseUrl, options2 = {}) => {
225
- const { defaultContentType = "application/json" } = options2;
252
+ const {
253
+ defaultContentType = "application/json",
254
+ tls = {},
255
+ ...agentOpts
256
+ } = options2;
226
257
  const mockAgent = new import_undici.MockAgent({ connections: 1 });
227
258
  mockAgent.disableNetConnect();
228
- const client = mockAgent.get(baseUrl);
229
- if (!agents.has(baseUrl)) {
230
- if (defaultContentType) {
231
- const _intercept = client.intercept;
232
- client.intercept = (...args) => {
233
- const interceptor = _intercept.apply(client, args);
234
- const _reply = interceptor.reply;
235
- const ensureJsonHeader = (headers = {}) => {
236
- const hasJsonHeader = Object.keys(headers).find(
237
- (k) => /content-type/i.test(k)
238
- );
239
- if (!hasJsonHeader) {
240
- headers["content-type"] = defaultContentType;
259
+ const key = generateAgentKey(baseUrl, {
260
+ ...agentOpts,
261
+ tls
262
+ });
263
+ console.log("Creating mock client for key:", key);
264
+ const dispatcher = mockAgent.get(baseUrl);
265
+ if (defaultContentType) {
266
+ const _intercept = dispatcher.intercept;
267
+ dispatcher.intercept = (...args) => {
268
+ const interceptor = _intercept.apply(dispatcher, args);
269
+ const _reply = interceptor.reply;
270
+ const ensureJsonHeader = (headers = {}) => {
271
+ const hasJsonHeader = Object.keys(headers).find(
272
+ (k) => /content-type/i.test(k)
273
+ );
274
+ if (!hasJsonHeader) {
275
+ headers["content-type"] = defaultContentType;
276
+ }
277
+ };
278
+ const reply = (...args2) => {
279
+ if (typeof args2[0] === "function") {
280
+ const response = _reply.apply(interceptor, args2);
281
+ if (response.body) {
282
+ response.headers ?? (response.headers = {});
283
+ ensureJsonHeader(response.headers);
241
284
  }
242
- };
243
- const reply = (...args2) => {
244
- if (typeof args2[0] === "function") {
245
- const response = _reply.apply(interceptor, args2);
246
- if (response.body) {
247
- response.headers ?? (response.headers = {});
248
- ensureJsonHeader(response.headers);
249
- }
250
- return response;
251
- } else {
252
- const [code, data, options3 = {}] = args2;
253
- if (data) {
254
- options3.headers ?? (options3.headers = {});
255
- ensureJsonHeader(options3.headers);
256
- }
257
- return _reply.call(interceptor, code, data, options3);
285
+ return response;
286
+ } else {
287
+ const [code, data, options3 = {}] = args2;
288
+ if (data) {
289
+ options3.headers ?? (options3.headers = {});
290
+ ensureJsonHeader(options3.headers);
258
291
  }
259
- };
260
- interceptor.reply = reply;
261
- return interceptor;
292
+ return _reply.call(interceptor, code, data, options3);
293
+ }
262
294
  };
263
- }
264
- agents.set(baseUrl, client);
295
+ interceptor.reply = reply;
296
+ return interceptor;
297
+ };
298
+ }
299
+ if (!agents.has(key)) {
300
+ agents.set(key, mockAgent);
265
301
  }
266
- return client;
302
+ return dispatcher;
267
303
  };
268
304
  var assertOK = async (response, errorMap, fullUrl, method, startTime) => {
269
305
  if (errorMap === false) {
@@ -346,7 +382,7 @@ async function request(method, fullUrlOrPath, options2 = {}) {
346
382
  parseAs = "auto",
347
383
  maxRedirections
348
384
  } = options2;
349
- const dispatcher = getAgent(baseUrl, { tls });
385
+ const dispatcher = getDispatcher(baseUrl, { tls, maxRedirections });
350
386
  const queryParams = {
351
387
  ...optionQuery,
352
388
  ...urlQuery
@@ -357,8 +393,6 @@ async function request(method, fullUrlOrPath, options2 = {}) {
357
393
  method,
358
394
  headers,
359
395
  body: encodeRequestBody(body),
360
- throwOnError: false,
361
- maxRedirections,
362
396
  bodyTimeout: timeout,
363
397
  headersTimeout: timeout,
364
398
  origin: baseUrl
@@ -630,7 +664,7 @@ function field(key, value) {
630
664
  return [key, value];
631
665
  }
632
666
  function fields(...fields2) {
633
- return import_lodash2.default.fromPairs(fields2);
667
+ return import_lodash3.default.fromPairs(fields2);
634
668
  }
635
669
  function merge(dataSource, fields2) {
636
670
  return (state) => {
@@ -648,11 +682,11 @@ function group(arrayOfObjects, keyPath, callback = (s) => s) {
648
682
  arrayOfObjects,
649
683
  keyPath
650
684
  );
651
- const results = import_lodash2.default.groupBy(
685
+ const results = import_lodash3.default.groupBy(
652
686
  resolvedArray,
653
- (item) => import_lodash2.default.get(item, resolvedKeyPath)
687
+ (item) => import_lodash3.default.get(item, resolvedKeyPath)
654
688
  );
655
- return callback({ ...state, data: import_lodash2.default.omit(results, [void 0]) });
689
+ return callback({ ...state, data: import_lodash3.default.omit(results, [void 0]) });
656
690
  };
657
691
  }
658
692
  function index() {
@@ -961,8 +995,9 @@ __export(http_exports, {
961
995
  post: () => post2,
962
996
  request: () => req
963
997
  });
964
- var import_lodash3 = __toESM(require("lodash"), 1);
965
- var { set } = import_lodash3.default;
998
+ var import_lodash4 = __toESM(require("lodash"), 1);
999
+ var { request: request3, expandReferences: expandReferences2 } = util_exports;
1000
+ var { set } = import_lodash4.default;
966
1001
  var helpers = {
967
1002
  json: function() {
968
1003
  set(this, "headers.Content-Type", "application/json");
@@ -994,13 +1029,13 @@ function options(opts = {}) {
994
1029
  }
995
1030
  var req = function(method, url, options2) {
996
1031
  return async (state) => {
997
- const [resolvedMethod, resolvedUrl, resolvedOptions] = expandReferences(
1032
+ const [resolvedMethod, resolvedUrl, resolvedOptions] = expandReferences2(
998
1033
  state,
999
1034
  method,
1000
1035
  url,
1001
1036
  options2
1002
1037
  );
1003
- const { body, ...responseWithoutBody } = await request(
1038
+ const { body, ...responseWithoutBody } = await request3(
1004
1039
  resolvedMethod,
1005
1040
  resolvedUrl,
1006
1041
  resolvedOptions
package/dist/index.js CHANGED
@@ -7,7 +7,7 @@ var __export = (target, all) => {
7
7
  // src/Adaptor.js
8
8
  var Adaptor_exports = {};
9
9
  __export(Adaptor_exports, {
10
- _: () => _2,
10
+ _: () => _3,
11
11
  alterState: () => alterState,
12
12
  arrayToString: () => arrayToString,
13
13
  as: () => as,
@@ -49,7 +49,7 @@ import { parse } from "csv-parse";
49
49
  import { Readable as Readable3 } from "stream";
50
50
  import { request as request2 } from "undici";
51
51
  import dateFns from "date-fns";
52
- import _2 from "lodash";
52
+ import _3 from "lodash";
53
53
 
54
54
  // src/util/index.js
55
55
  var util_exports = {};
@@ -62,6 +62,7 @@ __export(util_exports, {
62
62
  enableMockClient: () => enableMockClient,
63
63
  encode: () => encode,
64
64
  expandReferences: () => expandReferences,
65
+ generateAgentKey: () => generateAgentKey,
65
66
  get: () => get,
66
67
  logResponse: () => logResponse,
67
68
  makeBasicAuthHeader: () => makeBasicAuthHeader,
@@ -76,7 +77,6 @@ __export(util_exports, {
76
77
  });
77
78
 
78
79
  // src/util/http.js
79
- import { MockAgent, Agent } from "undici";
80
80
  import { getReasonPhrase } from "http-status-codes";
81
81
  import { Readable } from "stream";
82
82
  import querystring from "querystring";
@@ -125,6 +125,8 @@ var decode = (base64Data, options2 = { parseJson: true }) => {
125
125
  };
126
126
 
127
127
  // src/util/http.js
128
+ import { MockAgent, Agent, interceptors } from "undici";
129
+ import _2 from "lodash";
128
130
  var agents = /* @__PURE__ */ new Map();
129
131
  var makeBasicAuthHeader = (username, password) => {
130
132
  const buff = Buffer.from(`${username}:${password}`);
@@ -146,59 +148,93 @@ var logResponse = (response) => {
146
148
  }
147
149
  return response;
148
150
  };
149
- var getAgent = (origin, { tls = {}, ...agentOpts } = {}) => {
150
- if (!agents.has(origin)) {
151
+ var sortObject = (obj) => _2(obj).toPairs().filter(([_k, v]) => v !== void 0).sortBy(0).map(([k, v]) => {
152
+ if (v && typeof v === "object") {
153
+ if (!Object.keys(v).length) {
154
+ return "";
155
+ }
156
+ v = `${"{"}${sortObject(v)}${"}"}`;
157
+ }
158
+ return [k, v].join(":");
159
+ }).join("|");
160
+ var generateAgentKey = (baseUrl, agentOpts = {}) => {
161
+ if (Object.keys(agentOpts).length) {
162
+ const sortedSerializedOptions = sortObject(agentOpts);
163
+ if (sortedSerializedOptions.length) {
164
+ return `${baseUrl}+${sortedSerializedOptions}`;
165
+ }
166
+ }
167
+ return baseUrl;
168
+ };
169
+ var getDispatcher = (origin, options2 = {}) => {
170
+ const { tls = {}, defaultContentType, ...agentOpts } = options2;
171
+ const key = generateAgentKey(origin, options2);
172
+ if (!agents.has(key)) {
151
173
  const agent = new Agent({
152
174
  connect: tls,
153
175
  ...agentOpts
154
- });
155
- agents.set(origin, agent);
176
+ }).compose(
177
+ interceptors.redirect({
178
+ maxRedirections: agentOpts.maxRedirections
179
+ }),
180
+ interceptors.decompress()
181
+ );
182
+ agents.set(key, agent);
156
183
  }
157
- return agents.get(origin);
184
+ return agents.get(key);
158
185
  };
159
186
  var enableMockClient = (baseUrl, options2 = {}) => {
160
- const { defaultContentType = "application/json" } = options2;
187
+ const {
188
+ defaultContentType = "application/json",
189
+ tls = {},
190
+ ...agentOpts
191
+ } = options2;
161
192
  const mockAgent = new MockAgent({ connections: 1 });
162
193
  mockAgent.disableNetConnect();
163
- const client = mockAgent.get(baseUrl);
164
- if (!agents.has(baseUrl)) {
165
- if (defaultContentType) {
166
- const _intercept = client.intercept;
167
- client.intercept = (...args) => {
168
- const interceptor = _intercept.apply(client, args);
169
- const _reply = interceptor.reply;
170
- const ensureJsonHeader = (headers = {}) => {
171
- const hasJsonHeader = Object.keys(headers).find(
172
- (k) => /content-type/i.test(k)
173
- );
174
- if (!hasJsonHeader) {
175
- headers["content-type"] = defaultContentType;
194
+ const key = generateAgentKey(baseUrl, {
195
+ ...agentOpts,
196
+ tls
197
+ });
198
+ console.log("Creating mock client for key:", key);
199
+ const dispatcher = mockAgent.get(baseUrl);
200
+ if (defaultContentType) {
201
+ const _intercept = dispatcher.intercept;
202
+ dispatcher.intercept = (...args) => {
203
+ const interceptor = _intercept.apply(dispatcher, args);
204
+ const _reply = interceptor.reply;
205
+ const ensureJsonHeader = (headers = {}) => {
206
+ const hasJsonHeader = Object.keys(headers).find(
207
+ (k) => /content-type/i.test(k)
208
+ );
209
+ if (!hasJsonHeader) {
210
+ headers["content-type"] = defaultContentType;
211
+ }
212
+ };
213
+ const reply = (...args2) => {
214
+ if (typeof args2[0] === "function") {
215
+ const response = _reply.apply(interceptor, args2);
216
+ if (response.body) {
217
+ response.headers ?? (response.headers = {});
218
+ ensureJsonHeader(response.headers);
176
219
  }
177
- };
178
- const reply = (...args2) => {
179
- if (typeof args2[0] === "function") {
180
- const response = _reply.apply(interceptor, args2);
181
- if (response.body) {
182
- response.headers ?? (response.headers = {});
183
- ensureJsonHeader(response.headers);
184
- }
185
- return response;
186
- } else {
187
- const [code, data, options3 = {}] = args2;
188
- if (data) {
189
- options3.headers ?? (options3.headers = {});
190
- ensureJsonHeader(options3.headers);
191
- }
192
- return _reply.call(interceptor, code, data, options3);
220
+ return response;
221
+ } else {
222
+ const [code, data, options3 = {}] = args2;
223
+ if (data) {
224
+ options3.headers ?? (options3.headers = {});
225
+ ensureJsonHeader(options3.headers);
193
226
  }
194
- };
195
- interceptor.reply = reply;
196
- return interceptor;
227
+ return _reply.call(interceptor, code, data, options3);
228
+ }
197
229
  };
198
- }
199
- agents.set(baseUrl, client);
230
+ interceptor.reply = reply;
231
+ return interceptor;
232
+ };
200
233
  }
201
- return client;
234
+ if (!agents.has(key)) {
235
+ agents.set(key, mockAgent);
236
+ }
237
+ return dispatcher;
202
238
  };
203
239
  var assertOK = async (response, errorMap, fullUrl, method, startTime) => {
204
240
  if (errorMap === false) {
@@ -281,7 +317,7 @@ async function request(method, fullUrlOrPath, options2 = {}) {
281
317
  parseAs = "auto",
282
318
  maxRedirections
283
319
  } = options2;
284
- const dispatcher = getAgent(baseUrl, { tls });
320
+ const dispatcher = getDispatcher(baseUrl, { tls, maxRedirections });
285
321
  const queryParams = {
286
322
  ...optionQuery,
287
323
  ...urlQuery
@@ -292,8 +328,6 @@ async function request(method, fullUrlOrPath, options2 = {}) {
292
328
  method,
293
329
  headers,
294
330
  body: encodeRequestBody(body),
295
- throwOnError: false,
296
- maxRedirections,
297
331
  bodyTimeout: timeout,
298
332
  headersTimeout: timeout,
299
333
  origin: baseUrl
@@ -565,7 +599,7 @@ function field(key, value) {
565
599
  return [key, value];
566
600
  }
567
601
  function fields(...fields2) {
568
- return _2.fromPairs(fields2);
602
+ return _3.fromPairs(fields2);
569
603
  }
570
604
  function merge(dataSource, fields2) {
571
605
  return (state) => {
@@ -583,11 +617,11 @@ function group(arrayOfObjects, keyPath, callback = (s) => s) {
583
617
  arrayOfObjects,
584
618
  keyPath
585
619
  );
586
- const results = _2.groupBy(
620
+ const results = _3.groupBy(
587
621
  resolvedArray,
588
- (item) => _2.get(item, resolvedKeyPath)
622
+ (item) => _3.get(item, resolvedKeyPath)
589
623
  );
590
- return callback({ ...state, data: _2.omit(results, [void 0]) });
624
+ return callback({ ...state, data: _3.omit(results, [void 0]) });
591
625
  };
592
626
  }
593
627
  function index() {
@@ -896,8 +930,9 @@ __export(http_exports, {
896
930
  post: () => post2,
897
931
  request: () => req
898
932
  });
899
- import _3 from "lodash";
900
- var { set } = _3;
933
+ import _4 from "lodash";
934
+ var { request: request3, expandReferences: expandReferences2 } = util_exports;
935
+ var { set } = _4;
901
936
  var helpers = {
902
937
  json: function() {
903
938
  set(this, "headers.Content-Type", "application/json");
@@ -929,13 +964,13 @@ function options(opts = {}) {
929
964
  }
930
965
  var req = function(method, url, options2) {
931
966
  return async (state) => {
932
- const [resolvedMethod, resolvedUrl, resolvedOptions] = expandReferences(
967
+ const [resolvedMethod, resolvedUrl, resolvedOptions] = expandReferences2(
933
968
  state,
934
969
  method,
935
970
  url,
936
971
  options2
937
972
  );
938
- const { body, ...responseWithoutBody } = await request(
973
+ const { body, ...responseWithoutBody } = await request3(
939
974
  resolvedMethod,
940
975
  resolvedUrl,
941
976
  resolvedOptions
@@ -968,7 +1003,7 @@ import {
968
1003
  // src/index.js
969
1004
  var src_default = Adaptor_exports;
970
1005
  export {
971
- _2 as _,
1006
+ _3 as _,
972
1007
  alterState,
973
1008
  arrayToString,
974
1009
  as,
package/dist/util.cjs CHANGED
@@ -33,6 +33,7 @@ __export(util_exports, {
33
33
  enableMockClient: () => enableMockClient,
34
34
  encode: () => encode,
35
35
  expandReferences: () => expandReferences,
36
+ generateAgentKey: () => generateAgentKey,
36
37
  get: () => get,
37
38
  logResponse: () => logResponse,
38
39
  makeBasicAuthHeader: () => makeBasicAuthHeader,
@@ -48,7 +49,6 @@ __export(util_exports, {
48
49
  module.exports = __toCommonJS(util_exports);
49
50
 
50
51
  // src/util/http.js
51
- var import_undici = require("undici");
52
52
  var import_http_status_codes = require("http-status-codes");
53
53
  var import_node_stream = require("stream");
54
54
  var import_node_querystring = __toESM(require("querystring"), 1);
@@ -97,6 +97,8 @@ var decode = (base64Data, options = { parseJson: true }) => {
97
97
  };
98
98
 
99
99
  // src/util/http.js
100
+ var import_undici = require("undici");
101
+ var import_lodash2 = __toESM(require("lodash"), 1);
100
102
  var agents = /* @__PURE__ */ new Map();
101
103
  var makeBasicAuthHeader = (username, password) => {
102
104
  const buff = Buffer.from(`${username}:${password}`);
@@ -118,59 +120,93 @@ var logResponse = (response) => {
118
120
  }
119
121
  return response;
120
122
  };
121
- var getAgent = (origin, { tls = {}, ...agentOpts } = {}) => {
122
- if (!agents.has(origin)) {
123
+ var sortObject = (obj) => (0, import_lodash2.default)(obj).toPairs().filter(([_k, v]) => v !== void 0).sortBy(0).map(([k, v]) => {
124
+ if (v && typeof v === "object") {
125
+ if (!Object.keys(v).length) {
126
+ return "";
127
+ }
128
+ v = `${"{"}${sortObject(v)}${"}"}`;
129
+ }
130
+ return [k, v].join(":");
131
+ }).join("|");
132
+ var generateAgentKey = (baseUrl, agentOpts = {}) => {
133
+ if (Object.keys(agentOpts).length) {
134
+ const sortedSerializedOptions = sortObject(agentOpts);
135
+ if (sortedSerializedOptions.length) {
136
+ return `${baseUrl}+${sortedSerializedOptions}`;
137
+ }
138
+ }
139
+ return baseUrl;
140
+ };
141
+ var getDispatcher = (origin, options = {}) => {
142
+ const { tls = {}, defaultContentType, ...agentOpts } = options;
143
+ const key = generateAgentKey(origin, options);
144
+ if (!agents.has(key)) {
123
145
  const agent = new import_undici.Agent({
124
146
  connect: tls,
125
147
  ...agentOpts
126
- });
127
- agents.set(origin, agent);
148
+ }).compose(
149
+ import_undici.interceptors.redirect({
150
+ maxRedirections: agentOpts.maxRedirections
151
+ }),
152
+ import_undici.interceptors.decompress()
153
+ );
154
+ agents.set(key, agent);
128
155
  }
129
- return agents.get(origin);
156
+ return agents.get(key);
130
157
  };
131
158
  var enableMockClient = (baseUrl, options = {}) => {
132
- const { defaultContentType = "application/json" } = options;
159
+ const {
160
+ defaultContentType = "application/json",
161
+ tls = {},
162
+ ...agentOpts
163
+ } = options;
133
164
  const mockAgent = new import_undici.MockAgent({ connections: 1 });
134
165
  mockAgent.disableNetConnect();
135
- const client = mockAgent.get(baseUrl);
136
- if (!agents.has(baseUrl)) {
137
- if (defaultContentType) {
138
- const _intercept = client.intercept;
139
- client.intercept = (...args) => {
140
- const interceptor = _intercept.apply(client, args);
141
- const _reply = interceptor.reply;
142
- const ensureJsonHeader = (headers = {}) => {
143
- const hasJsonHeader = Object.keys(headers).find(
144
- (k) => /content-type/i.test(k)
145
- );
146
- if (!hasJsonHeader) {
147
- headers["content-type"] = defaultContentType;
166
+ const key = generateAgentKey(baseUrl, {
167
+ ...agentOpts,
168
+ tls
169
+ });
170
+ console.log("Creating mock client for key:", key);
171
+ const dispatcher = mockAgent.get(baseUrl);
172
+ if (defaultContentType) {
173
+ const _intercept = dispatcher.intercept;
174
+ dispatcher.intercept = (...args) => {
175
+ const interceptor = _intercept.apply(dispatcher, args);
176
+ const _reply = interceptor.reply;
177
+ const ensureJsonHeader = (headers = {}) => {
178
+ const hasJsonHeader = Object.keys(headers).find(
179
+ (k) => /content-type/i.test(k)
180
+ );
181
+ if (!hasJsonHeader) {
182
+ headers["content-type"] = defaultContentType;
183
+ }
184
+ };
185
+ const reply = (...args2) => {
186
+ if (typeof args2[0] === "function") {
187
+ const response = _reply.apply(interceptor, args2);
188
+ if (response.body) {
189
+ response.headers ?? (response.headers = {});
190
+ ensureJsonHeader(response.headers);
148
191
  }
149
- };
150
- const reply = (...args2) => {
151
- if (typeof args2[0] === "function") {
152
- const response = _reply.apply(interceptor, args2);
153
- if (response.body) {
154
- response.headers ?? (response.headers = {});
155
- ensureJsonHeader(response.headers);
156
- }
157
- return response;
158
- } else {
159
- const [code, data, options2 = {}] = args2;
160
- if (data) {
161
- options2.headers ?? (options2.headers = {});
162
- ensureJsonHeader(options2.headers);
163
- }
164
- return _reply.call(interceptor, code, data, options2);
192
+ return response;
193
+ } else {
194
+ const [code, data, options2 = {}] = args2;
195
+ if (data) {
196
+ options2.headers ?? (options2.headers = {});
197
+ ensureJsonHeader(options2.headers);
165
198
  }
166
- };
167
- interceptor.reply = reply;
168
- return interceptor;
199
+ return _reply.call(interceptor, code, data, options2);
200
+ }
169
201
  };
170
- }
171
- agents.set(baseUrl, client);
202
+ interceptor.reply = reply;
203
+ return interceptor;
204
+ };
205
+ }
206
+ if (!agents.has(key)) {
207
+ agents.set(key, mockAgent);
172
208
  }
173
- return client;
209
+ return dispatcher;
174
210
  };
175
211
  var assertOK = async (response, errorMap, fullUrl, method, startTime) => {
176
212
  if (errorMap === false) {
@@ -253,7 +289,7 @@ async function request(method, fullUrlOrPath, options = {}) {
253
289
  parseAs = "auto",
254
290
  maxRedirections
255
291
  } = options;
256
- const dispatcher = getAgent(baseUrl, { tls });
292
+ const dispatcher = getDispatcher(baseUrl, { tls, maxRedirections });
257
293
  const queryParams = {
258
294
  ...optionQuery,
259
295
  ...urlQuery
@@ -264,8 +300,6 @@ async function request(method, fullUrlOrPath, options = {}) {
264
300
  method,
265
301
  headers,
266
302
  body: encodeRequestBody(body),
267
- throwOnError: false,
268
- maxRedirections,
269
303
  bodyTimeout: timeout,
270
304
  headersTimeout: timeout,
271
305
  origin: baseUrl
@@ -432,6 +466,7 @@ var uuid = () => (0, import_node_crypto.randomUUID)();
432
466
  enableMockClient,
433
467
  encode,
434
468
  expandReferences,
469
+ generateAgentKey,
435
470
  get,
436
471
  logResponse,
437
472
  makeBasicAuthHeader,
package/dist/util.js CHANGED
@@ -1,5 +1,4 @@
1
1
  // src/util/http.js
2
- import { MockAgent, Agent } from "undici";
3
2
  import { getReasonPhrase } from "http-status-codes";
4
3
  import { Readable } from "stream";
5
4
  import querystring from "querystring";
@@ -48,6 +47,8 @@ var decode = (base64Data, options = { parseJson: true }) => {
48
47
  };
49
48
 
50
49
  // src/util/http.js
50
+ import { MockAgent, Agent, interceptors } from "undici";
51
+ import _2 from "lodash";
51
52
  var agents = /* @__PURE__ */ new Map();
52
53
  var makeBasicAuthHeader = (username, password) => {
53
54
  const buff = Buffer.from(`${username}:${password}`);
@@ -69,59 +70,93 @@ var logResponse = (response) => {
69
70
  }
70
71
  return response;
71
72
  };
72
- var getAgent = (origin, { tls = {}, ...agentOpts } = {}) => {
73
- if (!agents.has(origin)) {
73
+ var sortObject = (obj) => _2(obj).toPairs().filter(([_k, v]) => v !== void 0).sortBy(0).map(([k, v]) => {
74
+ if (v && typeof v === "object") {
75
+ if (!Object.keys(v).length) {
76
+ return "";
77
+ }
78
+ v = `${"{"}${sortObject(v)}${"}"}`;
79
+ }
80
+ return [k, v].join(":");
81
+ }).join("|");
82
+ var generateAgentKey = (baseUrl, agentOpts = {}) => {
83
+ if (Object.keys(agentOpts).length) {
84
+ const sortedSerializedOptions = sortObject(agentOpts);
85
+ if (sortedSerializedOptions.length) {
86
+ return `${baseUrl}+${sortedSerializedOptions}`;
87
+ }
88
+ }
89
+ return baseUrl;
90
+ };
91
+ var getDispatcher = (origin, options = {}) => {
92
+ const { tls = {}, defaultContentType, ...agentOpts } = options;
93
+ const key = generateAgentKey(origin, options);
94
+ if (!agents.has(key)) {
74
95
  const agent = new Agent({
75
96
  connect: tls,
76
97
  ...agentOpts
77
- });
78
- agents.set(origin, agent);
98
+ }).compose(
99
+ interceptors.redirect({
100
+ maxRedirections: agentOpts.maxRedirections
101
+ }),
102
+ interceptors.decompress()
103
+ );
104
+ agents.set(key, agent);
79
105
  }
80
- return agents.get(origin);
106
+ return agents.get(key);
81
107
  };
82
108
  var enableMockClient = (baseUrl, options = {}) => {
83
- const { defaultContentType = "application/json" } = options;
109
+ const {
110
+ defaultContentType = "application/json",
111
+ tls = {},
112
+ ...agentOpts
113
+ } = options;
84
114
  const mockAgent = new MockAgent({ connections: 1 });
85
115
  mockAgent.disableNetConnect();
86
- const client = mockAgent.get(baseUrl);
87
- if (!agents.has(baseUrl)) {
88
- if (defaultContentType) {
89
- const _intercept = client.intercept;
90
- client.intercept = (...args) => {
91
- const interceptor = _intercept.apply(client, args);
92
- const _reply = interceptor.reply;
93
- const ensureJsonHeader = (headers = {}) => {
94
- const hasJsonHeader = Object.keys(headers).find(
95
- (k) => /content-type/i.test(k)
96
- );
97
- if (!hasJsonHeader) {
98
- headers["content-type"] = defaultContentType;
116
+ const key = generateAgentKey(baseUrl, {
117
+ ...agentOpts,
118
+ tls
119
+ });
120
+ console.log("Creating mock client for key:", key);
121
+ const dispatcher = mockAgent.get(baseUrl);
122
+ if (defaultContentType) {
123
+ const _intercept = dispatcher.intercept;
124
+ dispatcher.intercept = (...args) => {
125
+ const interceptor = _intercept.apply(dispatcher, args);
126
+ const _reply = interceptor.reply;
127
+ const ensureJsonHeader = (headers = {}) => {
128
+ const hasJsonHeader = Object.keys(headers).find(
129
+ (k) => /content-type/i.test(k)
130
+ );
131
+ if (!hasJsonHeader) {
132
+ headers["content-type"] = defaultContentType;
133
+ }
134
+ };
135
+ const reply = (...args2) => {
136
+ if (typeof args2[0] === "function") {
137
+ const response = _reply.apply(interceptor, args2);
138
+ if (response.body) {
139
+ response.headers ?? (response.headers = {});
140
+ ensureJsonHeader(response.headers);
99
141
  }
100
- };
101
- const reply = (...args2) => {
102
- if (typeof args2[0] === "function") {
103
- const response = _reply.apply(interceptor, args2);
104
- if (response.body) {
105
- response.headers ?? (response.headers = {});
106
- ensureJsonHeader(response.headers);
107
- }
108
- return response;
109
- } else {
110
- const [code, data, options2 = {}] = args2;
111
- if (data) {
112
- options2.headers ?? (options2.headers = {});
113
- ensureJsonHeader(options2.headers);
114
- }
115
- return _reply.call(interceptor, code, data, options2);
142
+ return response;
143
+ } else {
144
+ const [code, data, options2 = {}] = args2;
145
+ if (data) {
146
+ options2.headers ?? (options2.headers = {});
147
+ ensureJsonHeader(options2.headers);
116
148
  }
117
- };
118
- interceptor.reply = reply;
119
- return interceptor;
149
+ return _reply.call(interceptor, code, data, options2);
150
+ }
120
151
  };
121
- }
122
- agents.set(baseUrl, client);
152
+ interceptor.reply = reply;
153
+ return interceptor;
154
+ };
155
+ }
156
+ if (!agents.has(key)) {
157
+ agents.set(key, mockAgent);
123
158
  }
124
- return client;
159
+ return dispatcher;
125
160
  };
126
161
  var assertOK = async (response, errorMap, fullUrl, method, startTime) => {
127
162
  if (errorMap === false) {
@@ -204,7 +239,7 @@ async function request(method, fullUrlOrPath, options = {}) {
204
239
  parseAs = "auto",
205
240
  maxRedirections
206
241
  } = options;
207
- const dispatcher = getAgent(baseUrl, { tls });
242
+ const dispatcher = getDispatcher(baseUrl, { tls, maxRedirections });
208
243
  const queryParams = {
209
244
  ...optionQuery,
210
245
  ...urlQuery
@@ -215,8 +250,6 @@ async function request(method, fullUrlOrPath, options = {}) {
215
250
  method,
216
251
  headers,
217
252
  body: encodeRequestBody(body),
218
- throwOnError: false,
219
- maxRedirections,
220
253
  bodyTimeout: timeout,
221
254
  headersTimeout: timeout,
222
255
  origin: baseUrl
@@ -382,6 +415,7 @@ export {
382
415
  enableMockClient,
383
416
  encode,
384
417
  expandReferences,
418
+ generateAgentKey,
385
419
  get,
386
420
  logResponse,
387
421
  makeBasicAuthHeader,
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@openfn/language-common",
3
3
  "label": "Common",
4
- "version": "3.1.0",
4
+ "version": "3.1.2",
5
5
  "description": "Common Expressions for OpenFn",
6
6
  "homepage": "https://docs.openfn.org",
7
7
  "repository": {
@@ -42,13 +42,16 @@
42
42
  "http-status-codes": "^2.3.0",
43
43
  "jsonpath-plus": "^10.3.0",
44
44
  "lodash": "^4.17.21",
45
- "undici": "^5.29.0"
45
+ "undici": "^7.15.0"
46
46
  },
47
47
  "devDependencies": {
48
48
  "chai": "4.3.6",
49
49
  "deep-eql": "4.1.1",
50
+ "koa": "^3.0.3",
51
+ "koa-compress": "^5.1.1",
50
52
  "nock": "13.2.9",
51
- "rimraf": "^3.0.2"
53
+ "rimraf": "^3.0.2",
54
+ "zlib": "^1.0.5"
52
55
  },
53
56
  "main": "dist/index.cjs",
54
57
  "scripts": {
@@ -28,7 +28,8 @@ export function makeBasicAuthHeader(username: any, password: any): {
28
28
  Authorization: string;
29
29
  };
30
30
  export function logResponse(response: any): any;
31
- export function enableMockClient(baseUrl: any, options?: {}): import("undici/types/mock-interceptor").Interceptable;
31
+ export function generateAgentKey(baseUrl: any, agentOpts?: {}): any;
32
+ export function enableMockClient(baseUrl: any, options?: {}): import("undici/types/mock-interceptor.js").Interceptable;
32
33
  export const ERROR_ABSOLUTE_URL: "Absolute URLs not supported";
33
34
  export function assertRelativeUrl(path: any): void;
34
35
  export const ERROR_URL_MISMATCH: "Target origin does not match baseUrl origin";
@@ -1,6 +1,6 @@
1
- export * from "./http";
2
- export * from "./references";
3
- export { default as parseDate } from "./parse-date";
4
- export { default as throwError } from "./throw-error";
5
- export { uuid } from "./uuid";
6
- export { encode, decode } from "./base64";
1
+ export * from "./http.js";
2
+ export * from "./references.js";
3
+ export { default as parseDate } from "./parse-date.js";
4
+ export { default as throwError } from "./throw-error.js";
5
+ export { uuid } from "./uuid.js";
6
+ export { encode, decode } from "./base64.js";