@whatwg-node/node-fetch 0.4.6-alpha-20230613182236-ce4ebc7 → 0.4.6

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/cjs/Body.js CHANGED
@@ -232,21 +232,14 @@ function processBodyInit(bodyInit) {
232
232
  };
233
233
  }
234
234
  if (typeof bodyInit === 'string') {
235
- let contentLength;
235
+ const buffer = Buffer.from(bodyInit);
236
+ const contentLength = buffer.byteLength;
236
237
  return {
237
238
  bodyType: BodyInitType.String,
238
239
  contentType: 'text/plain;charset=UTF-8',
239
- get contentLength() {
240
- if (contentLength == null) {
241
- contentLength = Buffer.byteLength(bodyInit);
242
- }
243
- return contentLength;
244
- },
245
- set contentLength(value) {
246
- contentLength = value;
247
- },
240
+ contentLength,
248
241
  bodyFactory() {
249
- const readable = stream_1.Readable.from(bodyInit);
242
+ const readable = stream_1.Readable.from(buffer);
250
243
  return new ReadableStream_js_1.PonyfillReadableStream(readable);
251
244
  },
252
245
  };
package/cjs/Headers.js CHANGED
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.PonyfillHeaders = exports.isHeadersLike = void 0;
3
+ exports.splitSetCookieHeader = exports.PonyfillHeaders = exports.isHeadersLike = void 0;
4
+ const node_util_1 = require("node:util");
4
5
  function isHeadersLike(headers) {
5
6
  return headers?.get && headers?.forEach;
6
7
  }
@@ -169,8 +170,57 @@ class PonyfillHeaders {
169
170
  }
170
171
  return this.getMap().entries();
171
172
  }
173
+ getSetCookie() {
174
+ const setCookieHeader = this.get('set-cookie');
175
+ if (!setCookieHeader) {
176
+ return [];
177
+ }
178
+ return splitSetCookieHeader(setCookieHeader);
179
+ }
172
180
  [Symbol.iterator]() {
173
181
  return this.entries();
174
182
  }
183
+ [Symbol.for('nodejs.util.inspect.custom')]() {
184
+ const record = {};
185
+ this.forEach((value, key) => {
186
+ if (key === 'set-cookie') {
187
+ record['set-cookie'] = this.getSetCookie();
188
+ }
189
+ else {
190
+ record[key] = value.includes(',') ? value.split(',').map(el => el.trim()) : value;
191
+ }
192
+ });
193
+ return `Headers ${(0, node_util_1.inspect)(record)}`;
194
+ }
175
195
  }
176
196
  exports.PonyfillHeaders = PonyfillHeaders;
197
+ function splitSetCookieHeader(setCookieHeader) {
198
+ const setCookieHeaders = [];
199
+ let currentStr = '';
200
+ let ignoreComma = false;
201
+ for (const ch of setCookieHeader) {
202
+ if (currentStr.endsWith('Expires=')) {
203
+ ignoreComma = true;
204
+ }
205
+ if (ignoreComma) {
206
+ if (ch === ';') {
207
+ ignoreComma = false;
208
+ }
209
+ if (ch === ',' && currentStr.split('Expires=')[1].length > 3) {
210
+ ignoreComma = false;
211
+ }
212
+ }
213
+ if (ch === ',' && !ignoreComma) {
214
+ setCookieHeaders.push(currentStr.trim());
215
+ currentStr = '';
216
+ }
217
+ else {
218
+ currentStr += ch;
219
+ }
220
+ }
221
+ if (currentStr) {
222
+ setCookieHeaders.push(currentStr.trim());
223
+ }
224
+ return setCookieHeaders;
225
+ }
226
+ exports.splitSetCookieHeader = splitSetCookieHeader;
package/esm/Body.js CHANGED
@@ -227,21 +227,14 @@ function processBodyInit(bodyInit) {
227
227
  };
228
228
  }
229
229
  if (typeof bodyInit === 'string') {
230
- let contentLength;
230
+ const buffer = Buffer.from(bodyInit);
231
+ const contentLength = buffer.byteLength;
231
232
  return {
232
233
  bodyType: BodyInitType.String,
233
234
  contentType: 'text/plain;charset=UTF-8',
234
- get contentLength() {
235
- if (contentLength == null) {
236
- contentLength = Buffer.byteLength(bodyInit);
237
- }
238
- return contentLength;
239
- },
240
- set contentLength(value) {
241
- contentLength = value;
242
- },
235
+ contentLength,
243
236
  bodyFactory() {
244
- const readable = Readable.from(bodyInit);
237
+ const readable = Readable.from(buffer);
245
238
  return new PonyfillReadableStream(readable);
246
239
  },
247
240
  };
package/esm/Headers.js CHANGED
@@ -1,3 +1,4 @@
1
+ import { inspect } from 'node:util';
1
2
  export function isHeadersLike(headers) {
2
3
  return headers?.get && headers?.forEach;
3
4
  }
@@ -165,7 +166,55 @@ export class PonyfillHeaders {
165
166
  }
166
167
  return this.getMap().entries();
167
168
  }
169
+ getSetCookie() {
170
+ const setCookieHeader = this.get('set-cookie');
171
+ if (!setCookieHeader) {
172
+ return [];
173
+ }
174
+ return splitSetCookieHeader(setCookieHeader);
175
+ }
168
176
  [Symbol.iterator]() {
169
177
  return this.entries();
170
178
  }
179
+ [Symbol.for('nodejs.util.inspect.custom')]() {
180
+ const record = {};
181
+ this.forEach((value, key) => {
182
+ if (key === 'set-cookie') {
183
+ record['set-cookie'] = this.getSetCookie();
184
+ }
185
+ else {
186
+ record[key] = value.includes(',') ? value.split(',').map(el => el.trim()) : value;
187
+ }
188
+ });
189
+ return `Headers ${inspect(record)}`;
190
+ }
191
+ }
192
+ export function splitSetCookieHeader(setCookieHeader) {
193
+ const setCookieHeaders = [];
194
+ let currentStr = '';
195
+ let ignoreComma = false;
196
+ for (const ch of setCookieHeader) {
197
+ if (currentStr.endsWith('Expires=')) {
198
+ ignoreComma = true;
199
+ }
200
+ if (ignoreComma) {
201
+ if (ch === ';') {
202
+ ignoreComma = false;
203
+ }
204
+ if (ch === ',' && currentStr.split('Expires=')[1].length > 3) {
205
+ ignoreComma = false;
206
+ }
207
+ }
208
+ if (ch === ',' && !ignoreComma) {
209
+ setCookieHeaders.push(currentStr.trim());
210
+ currentStr = '';
211
+ }
212
+ else {
213
+ currentStr += ch;
214
+ }
215
+ }
216
+ if (currentStr) {
217
+ setCookieHeaders.push(currentStr.trim());
218
+ }
219
+ return setCookieHeaders;
171
220
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@whatwg-node/node-fetch",
3
- "version": "0.4.6-alpha-20230613182236-ce4ebc7",
3
+ "version": "0.4.6",
4
4
  "description": "Fetch API implementation for Node",
5
5
  "sideEffects": false,
6
6
  "dependencies": {
@@ -17,5 +17,7 @@ export declare class PonyfillHeaders implements Headers {
17
17
  keys(): IterableIterator<string>;
18
18
  values(): IterableIterator<string>;
19
19
  entries(): IterableIterator<[string, string]>;
20
+ getSetCookie(): string[];
20
21
  [Symbol.iterator](): IterableIterator<[string, string]>;
21
22
  }
23
+ export declare function splitSetCookieHeader(setCookieHeader: string): string[];
@@ -17,5 +17,7 @@ export declare class PonyfillHeaders implements Headers {
17
17
  keys(): IterableIterator<string>;
18
18
  values(): IterableIterator<string>;
19
19
  entries(): IterableIterator<[string, string]>;
20
+ getSetCookie(): string[];
20
21
  [Symbol.iterator](): IterableIterator<[string, string]>;
21
22
  }
23
+ export declare function splitSetCookieHeader(setCookieHeader: string): string[];