@readme/httpsnippet 8.1.4 → 9.0.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.
@@ -4,4 +4,4 @@ declare const reducer: <T extends {
4
4
  value: string;
5
5
  }>(accumulator: ReducedHelperObject, pair: T) => ReducedHelperObject;
6
6
 
7
- export { ReducedHelperObject, reducer };
7
+ export { type ReducedHelperObject, reducer };
@@ -117,8 +117,8 @@ declare class HTTPSnippet {
117
117
  requests: Request[];
118
118
  options: HTTPSnippetOptions;
119
119
  constructor(input: HarEntry | HarRequest, opts?: HTTPSnippetOptions);
120
- init(): Promise<this>;
121
- prepare(harRequest: HarRequest, options: HTTPSnippetOptions): Promise<{
120
+ init(): this;
121
+ prepare(harRequest: HarRequest, options: HTTPSnippetOptions): {
122
122
  allHeaders: {
123
123
  [x: string]: string | string[];
124
124
  };
@@ -157,8 +157,8 @@ declare class HTTPSnippet {
157
157
  cookiesObj: ReducedHelperObject;
158
158
  headersObj: ReducedHelperObject;
159
159
  queryObj: ReducedHelperObject;
160
- }>;
161
- convert(targetId: TargetId, clientId?: ClientId, options?: any): Promise<string | false | string[]>;
160
+ };
161
+ convert(targetId: TargetId, clientId?: ClientId, options?: any): string | false | string[];
162
162
  }
163
163
 
164
- export { ClientId as C, Extension as E, HarRequest as H, RequestExtras as R, TargetId as T, ClientInfo as a, Converter as b, Client as c, ClientPlugin as d, TargetInfo as e, Target as f, addTarget as g, isClient as h, isTarget as i, addClientPlugin as j, addTargetClient as k, Request as l, HTTPSnippetOptions as m, HTTPSnippet as n, availableTargets as o, extname as p, targets as t };
164
+ export { type ClientId as C, type Extension as E, type HarRequest as H, type RequestExtras as R, type TargetId as T, type ClientInfo as a, type Converter as b, type Client as c, type ClientPlugin as d, type TargetInfo as e, type Target as f, addTarget as g, isClient as h, isTarget as i, addClientPlugin as j, addTargetClient as k, type Request as l, type HTTPSnippetOptions as m, HTTPSnippet as n, availableTargets as o, extname as p, targets as t };
@@ -117,8 +117,8 @@ declare class HTTPSnippet {
117
117
  requests: Request[];
118
118
  options: HTTPSnippetOptions;
119
119
  constructor(input: HarEntry | HarRequest, opts?: HTTPSnippetOptions);
120
- init(): Promise<this>;
121
- prepare(harRequest: HarRequest, options: HTTPSnippetOptions): Promise<{
120
+ init(): this;
121
+ prepare(harRequest: HarRequest, options: HTTPSnippetOptions): {
122
122
  allHeaders: {
123
123
  [x: string]: string | string[];
124
124
  };
@@ -157,8 +157,8 @@ declare class HTTPSnippet {
157
157
  cookiesObj: ReducedHelperObject;
158
158
  headersObj: ReducedHelperObject;
159
159
  queryObj: ReducedHelperObject;
160
- }>;
161
- convert(targetId: TargetId, clientId?: ClientId, options?: any): Promise<string | false | string[]>;
160
+ };
161
+ convert(targetId: TargetId, clientId?: ClientId, options?: any): string | false | string[];
162
162
  }
163
163
 
164
- export { ClientId as C, Extension as E, HarRequest as H, RequestExtras as R, TargetId as T, ClientInfo as a, Converter as b, Client as c, ClientPlugin as d, TargetInfo as e, Target as f, addTarget as g, isClient as h, isTarget as i, addClientPlugin as j, addTargetClient as k, Request as l, HTTPSnippetOptions as m, HTTPSnippet as n, availableTargets as o, extname as p, targets as t };
164
+ export { type ClientId as C, type Extension as E, type HarRequest as H, type RequestExtras as R, type TargetId as T, type ClientInfo as a, type Converter as b, type Client as c, type ClientPlugin as d, type TargetInfo as e, type Target as f, addTarget as g, isClient as h, isTarget as i, addClientPlugin as j, addTargetClient as k, type Request as l, type HTTPSnippetOptions as m, HTTPSnippet as n, availableTargets as o, extname as p, targets as t };
package/dist/index.cjs CHANGED
@@ -1,13 +1,11 @@
1
1
  'use strict';
2
2
 
3
3
  var url = require('url');
4
- var formDataToString = require('formdata-to-string');
5
4
  var qs = require('qs');
6
5
  var stringifyObject9 = require('stringify-object');
7
6
 
8
7
  function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
9
8
 
10
- var formDataToString__default = /*#__PURE__*/_interopDefault(formDataToString);
11
9
  var stringifyObject9__default = /*#__PURE__*/_interopDefault(stringifyObject9);
12
10
 
13
11
  // src/index.ts
@@ -3034,11 +3032,11 @@ ${indent}` : " "
3034
3032
  postData.params?.forEach((param) => {
3035
3033
  let post = "";
3036
3034
  if (param.fileName) {
3037
- post = `${param.name}=@${param.fileName}`;
3035
+ post = `${param.name}='@${param.fileName}'`;
3038
3036
  } else {
3039
- post = `${param.name}=${param.value}`;
3037
+ post = quote(`${param.name}=${param.value}`);
3040
3038
  }
3041
- push(`${arg("form")} ${quote(post)}`);
3039
+ push(`${arg("form")} ${post}`);
3042
3040
  });
3043
3041
  break;
3044
3042
  case "application/x-www-form-urlencoded":
@@ -3048,7 +3046,7 @@ ${indent}` : " "
3048
3046
  const encoded = encodeURIComponent(param.name);
3049
3047
  const needsEncoding = encoded !== unencoded;
3050
3048
  const name = needsEncoding ? encoded : unencoded;
3051
- const flag = binary ? "--data-binary" : `--data${needsEncoding ? "-urlencode" : ""}`;
3049
+ const flag = binary ? "--data-binary" : needsEncoding ? "--data-urlencode" : arg("data");
3052
3050
  push(`${flag} ${quote(`${name}=${param.value}`)}`);
3053
3051
  });
3054
3052
  } else {
@@ -3574,10 +3572,9 @@ var HTTPSnippet = class {
3574
3572
  ];
3575
3573
  }
3576
3574
  }
3577
- async init() {
3575
+ init() {
3578
3576
  this.initCalled = true;
3579
- const promises = [];
3580
- this.entries.forEach(({ request: request2 }) => {
3577
+ this.requests = this.entries.map(({ request: request2 }) => {
3581
3578
  const req = {
3582
3579
  bodySize: 0,
3583
3580
  headersSize: 0,
@@ -3593,12 +3590,11 @@ var HTTPSnippet = class {
3593
3590
  if (req.postData && !req.postData.mimeType) {
3594
3591
  req.postData.mimeType = "application/octet-stream";
3595
3592
  }
3596
- promises.push(this.prepare(req, this.options));
3593
+ return this.prepare(req, this.options);
3597
3594
  });
3598
- this.requests = await Promise.all(promises);
3599
3595
  return this;
3600
3596
  }
3601
- async prepare(harRequest, options) {
3597
+ prepare(harRequest, options) {
3602
3598
  const request2 = {
3603
3599
  ...harRequest,
3604
3600
  fullUrl: "",
@@ -3647,21 +3643,34 @@ var HTTPSnippet = class {
3647
3643
  request2.postData.text = "";
3648
3644
  request2.postData.mimeType = "multipart/form-data";
3649
3645
  if (request2.postData?.params) {
3650
- const form = new FormData();
3651
- request2.postData?.params.forEach((param) => {
3646
+ const boundary = "---011000010111000001101001";
3647
+ const carriage = `${boundary}--`;
3648
+ const rn = "\r\n";
3649
+ const escape2 = (str) => str.replace(/\n/g, "%0A").replace(/\r/g, "%0D").replace(/"/g, "%22");
3650
+ const normalizeLinefeeds = (value) => value.replace(/\r?\n|\r/g, "\r\n");
3651
+ const payload = [`--${boundary}`];
3652
+ request2.postData?.params.forEach((param, i) => {
3652
3653
  const name = param.name;
3653
3654
  const value = param.value || "";
3654
3655
  const filename = param.fileName || null;
3655
- const contentType = param.contentType || "";
3656
+ const contentType = param.contentType || "application/octet-stream";
3656
3657
  if (filename) {
3657
- form.append(name, new Blob([value], { type: contentType }), filename);
3658
+ payload.push(
3659
+ `Content-Disposition: form-data; name="${escape2(normalizeLinefeeds(name))}"; filename="${filename}"`
3660
+ );
3661
+ payload.push(`Content-Type: ${contentType}`);
3658
3662
  } else {
3659
- form.append(name, value);
3663
+ payload.push(`Content-Disposition: form-data; name="${escape2(normalizeLinefeeds(name))}"`);
3664
+ }
3665
+ payload.push("");
3666
+ payload.push(normalizeLinefeeds(value));
3667
+ if (i !== request2.postData.params.length - 1) {
3668
+ payload.push(`--${boundary}`);
3660
3669
  }
3661
3670
  });
3662
- const boundary = "---011000010111000001101001";
3671
+ payload.push(`--${carriage}`);
3663
3672
  request2.postData.boundary = boundary;
3664
- request2.postData.text = await formDataToString__default.default(form, { boundary });
3673
+ request2.postData.text = payload.join(rn);
3665
3674
  const contentTypeHeader = getHeaderName(request2.headersObj, "content-type") || "content-type";
3666
3675
  request2.headersObj[contentTypeHeader] = `multipart/form-data; boundary=${boundary}`;
3667
3676
  }
@@ -3731,9 +3740,9 @@ var HTTPSnippet = class {
3731
3740
  uriObj
3732
3741
  };
3733
3742
  }
3734
- async convert(targetId, clientId, options) {
3743
+ convert(targetId, clientId, options) {
3735
3744
  if (!this.initCalled) {
3736
- await this.init();
3745
+ this.init();
3737
3746
  }
3738
3747
  if (!options && clientId) {
3739
3748
  options = clientId;
@@ -3747,6 +3756,7 @@ var HTTPSnippet = class {
3747
3756
  return results.length === 1 ? results[0] : results;
3748
3757
  }
3749
3758
  };
3759
+ /*! formdata-polyfill. MIT License. Jimmy Wärting <https://jimmy.warting.se/opensource> */
3750
3760
 
3751
3761
  exports.HTTPSnippet = HTTPSnippet;
3752
3762
  exports.addClientPlugin = addClientPlugin;