isomorphic-git 1.44.16 → 1.44.17

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.
@@ -207,37 +207,113 @@ async function request({
207
207
  // @ts-expect-error
208
208
  body = asyncIteratorToStream(body);
209
209
  }
210
+ const { followRedirects = true, maxRedirects = 10, ...options } = fetchOptions;
211
+ /** @type {RedirectableRequest} */
212
+ let next = { url, method: method.toUpperCase(), headers, body };
213
+ for (let redirects = 0; ; redirects++) {
214
+ const res = await send({
215
+ ...options,
216
+ ...next,
217
+ agent,
218
+ signal: signal ?? options.signal,
219
+ });
220
+ const location = res.headers.location;
221
+ if (
222
+ !followRedirects ||
223
+ res.statusCode < 300 ||
224
+ res.statusCode >= 400 ||
225
+ !location
226
+ ) {
227
+ return toResponse(res)
228
+ }
229
+ // Discard the redirect body. simple-get has already wrapped it in a
230
+ // decompressor, so a malformed body must not become an unhandled error.
231
+ res.on('error', () => {});
232
+ res.resume();
233
+ if (redirects >= maxRedirects) throw new Error('too many redirects')
234
+ next = redirectRequest(next, res.statusCode, location);
235
+ // simple-get builds a body from `form` on every call; like its own
236
+ // redirect handling, send it only with the first request.
237
+ delete options.form;
238
+ }
239
+ }
240
+
241
+ /**
242
+ * @typedef {Object} RedirectableRequest
243
+ * @property {string} url
244
+ * @property {string} method
245
+ * @property {Object<string, string>} headers
246
+ * @property {any} body
247
+ */
248
+
249
+ /**
250
+ * Follows a redirect the way simple-get did, except that credentials are scoped
251
+ * to an origin (scheme, host and port) rather than to a hostname, like curl,
252
+ * which canonical git uses. simple-get kept the Authorization header when a
253
+ * redirect changed only the port or the scheme.
254
+ *
255
+ * @param {RedirectableRequest} previous
256
+ * @param {number} statusCode
257
+ * @param {string} location
258
+ * @returns {RedirectableRequest}
259
+ */
260
+ function redirectRequest(previous, statusCode, location) {
261
+ const url = new URL(location, previous.url);
262
+ const sameOrigin = url.origin === new URL(previous.url).origin;
263
+ let { method, body } = previous;
264
+ const dropped = ['host'];
265
+ if (!sameOrigin) dropped.push('authorization', 'cookie');
266
+ // Like curl and RFC 7231 §6.4.4, a 303 always turns into a GET; a 301 or 302
267
+ // turns only a POST into one.
268
+ if (
269
+ (statusCode === 303 && method !== 'GET' && method !== 'HEAD') ||
270
+ (method === 'POST' && (statusCode === 301 || statusCode === 302))
271
+ ) {
272
+ method = 'GET';
273
+ body = undefined;
274
+ dropped.push('content-length', 'content-type');
275
+ } else if (body && !Buffer.isBuffer(body)) {
276
+ // A streamed body has been read by the first request and would be resent
277
+ // empty.
278
+ throw new Error(
279
+ `cannot follow a ${statusCode} redirect: the request body is a stream and cannot be replayed`
280
+ )
281
+ }
282
+ /** @type {Object<string, string>} */
283
+ const headers = {};
284
+ for (const [name, value] of Object.entries(previous.headers)) {
285
+ if (!dropped.includes(name.toLowerCase())) headers[name] = value;
286
+ }
287
+ return { url: url.href, method, headers, body }
288
+ }
289
+
290
+ /**
291
+ * @param {object} options
292
+ * @returns {Promise<any>}
293
+ */
294
+ function send(options) {
210
295
  return new Promise((resolve, reject) => {
211
- get(
212
- {
213
- ...fetchOptions,
214
- url,
215
- method,
216
- headers,
217
- agent,
218
- body,
219
- signal: signal ?? fetchOptions.signal,
220
- },
221
- (err, res) => {
222
- if (err) return reject(err)
223
- try {
224
- const iter = fromNodeStream(res);
225
- resolve({
226
- url: res.url,
227
- method: res.method,
228
- statusCode: res.statusCode,
229
- statusMessage: res.statusMessage,
230
- body: iter,
231
- headers: res.headers,
232
- });
233
- } catch (e) {
234
- reject(e);
235
- }
236
- }
296
+ get({ ...options, followRedirects: false }, (err, res) =>
297
+ err ? reject(err) : resolve(res)
237
298
  );
238
299
  })
239
300
  }
240
301
 
302
+ /**
303
+ * @param {any} res
304
+ * @returns {GitHttpResponse}
305
+ */
306
+ function toResponse(res) {
307
+ return {
308
+ url: res.url,
309
+ method: res.method,
310
+ statusCode: res.statusCode,
311
+ statusMessage: res.statusMessage,
312
+ body: fromNodeStream(res),
313
+ headers: res.headers,
314
+ }
315
+ }
316
+
241
317
  var index = { request };
242
318
 
243
319
  exports.default = index;
@@ -74,6 +74,14 @@ export type HttpFetch = (request: GitHttpRequest) => Promise<GitHttpResponse>;
74
74
  export type HttpClient = {
75
75
  request: HttpFetch;
76
76
  };
77
+ export type RedirectableRequest = {
78
+ url: string;
79
+ method: string;
80
+ headers: {
81
+ [x: string]: string;
82
+ };
83
+ body: any;
84
+ };
77
85
  declare namespace index {
78
86
  export { request };
79
87
  }
@@ -73,6 +73,14 @@ export type HttpFetch = (request: GitHttpRequest) => Promise<GitHttpResponse>;
73
73
  export type HttpClient = {
74
74
  request: HttpFetch;
75
75
  };
76
+ export type RedirectableRequest = {
77
+ url: string;
78
+ method: string;
79
+ headers: {
80
+ [x: string]: string;
81
+ };
82
+ body: any;
83
+ };
76
84
  declare namespace index {
77
85
  export { request };
78
86
  }
@@ -201,37 +201,113 @@ async function request({
201
201
  // @ts-expect-error
202
202
  body = asyncIteratorToStream(body);
203
203
  }
204
+ const { followRedirects = true, maxRedirects = 10, ...options } = fetchOptions;
205
+ /** @type {RedirectableRequest} */
206
+ let next = { url, method: method.toUpperCase(), headers, body };
207
+ for (let redirects = 0; ; redirects++) {
208
+ const res = await send({
209
+ ...options,
210
+ ...next,
211
+ agent,
212
+ signal: signal ?? options.signal,
213
+ });
214
+ const location = res.headers.location;
215
+ if (
216
+ !followRedirects ||
217
+ res.statusCode < 300 ||
218
+ res.statusCode >= 400 ||
219
+ !location
220
+ ) {
221
+ return toResponse(res)
222
+ }
223
+ // Discard the redirect body. simple-get has already wrapped it in a
224
+ // decompressor, so a malformed body must not become an unhandled error.
225
+ res.on('error', () => {});
226
+ res.resume();
227
+ if (redirects >= maxRedirects) throw new Error('too many redirects')
228
+ next = redirectRequest(next, res.statusCode, location);
229
+ // simple-get builds a body from `form` on every call; like its own
230
+ // redirect handling, send it only with the first request.
231
+ delete options.form;
232
+ }
233
+ }
234
+
235
+ /**
236
+ * @typedef {Object} RedirectableRequest
237
+ * @property {string} url
238
+ * @property {string} method
239
+ * @property {Object<string, string>} headers
240
+ * @property {any} body
241
+ */
242
+
243
+ /**
244
+ * Follows a redirect the way simple-get did, except that credentials are scoped
245
+ * to an origin (scheme, host and port) rather than to a hostname, like curl,
246
+ * which canonical git uses. simple-get kept the Authorization header when a
247
+ * redirect changed only the port or the scheme.
248
+ *
249
+ * @param {RedirectableRequest} previous
250
+ * @param {number} statusCode
251
+ * @param {string} location
252
+ * @returns {RedirectableRequest}
253
+ */
254
+ function redirectRequest(previous, statusCode, location) {
255
+ const url = new URL(location, previous.url);
256
+ const sameOrigin = url.origin === new URL(previous.url).origin;
257
+ let { method, body } = previous;
258
+ const dropped = ['host'];
259
+ if (!sameOrigin) dropped.push('authorization', 'cookie');
260
+ // Like curl and RFC 7231 §6.4.4, a 303 always turns into a GET; a 301 or 302
261
+ // turns only a POST into one.
262
+ if (
263
+ (statusCode === 303 && method !== 'GET' && method !== 'HEAD') ||
264
+ (method === 'POST' && (statusCode === 301 || statusCode === 302))
265
+ ) {
266
+ method = 'GET';
267
+ body = undefined;
268
+ dropped.push('content-length', 'content-type');
269
+ } else if (body && !Buffer.isBuffer(body)) {
270
+ // A streamed body has been read by the first request and would be resent
271
+ // empty.
272
+ throw new Error(
273
+ `cannot follow a ${statusCode} redirect: the request body is a stream and cannot be replayed`
274
+ )
275
+ }
276
+ /** @type {Object<string, string>} */
277
+ const headers = {};
278
+ for (const [name, value] of Object.entries(previous.headers)) {
279
+ if (!dropped.includes(name.toLowerCase())) headers[name] = value;
280
+ }
281
+ return { url: url.href, method, headers, body }
282
+ }
283
+
284
+ /**
285
+ * @param {object} options
286
+ * @returns {Promise<any>}
287
+ */
288
+ function send(options) {
204
289
  return new Promise((resolve, reject) => {
205
- get(
206
- {
207
- ...fetchOptions,
208
- url,
209
- method,
210
- headers,
211
- agent,
212
- body,
213
- signal: signal ?? fetchOptions.signal,
214
- },
215
- (err, res) => {
216
- if (err) return reject(err)
217
- try {
218
- const iter = fromNodeStream(res);
219
- resolve({
220
- url: res.url,
221
- method: res.method,
222
- statusCode: res.statusCode,
223
- statusMessage: res.statusMessage,
224
- body: iter,
225
- headers: res.headers,
226
- });
227
- } catch (e) {
228
- reject(e);
229
- }
230
- }
290
+ get({ ...options, followRedirects: false }, (err, res) =>
291
+ err ? reject(err) : resolve(res)
231
292
  );
232
293
  })
233
294
  }
234
295
 
296
+ /**
297
+ * @param {any} res
298
+ * @returns {GitHttpResponse}
299
+ */
300
+ function toResponse(res) {
301
+ return {
302
+ url: res.url,
303
+ method: res.method,
304
+ statusCode: res.statusCode,
305
+ statusMessage: res.statusMessage,
306
+ body: fromNodeStream(res),
307
+ headers: res.headers,
308
+ }
309
+ }
310
+
235
311
  var index = { request };
236
312
 
237
313
  export default index;
package/index.cjs CHANGED
@@ -10441,8 +10441,8 @@ function filterCapabilities(server, client) {
10441
10441
 
10442
10442
  const pkg = {
10443
10443
  name: 'isomorphic-git',
10444
- version: '1.44.16',
10445
- agent: 'git/isomorphic-git@1.44.16',
10444
+ version: '1.44.17',
10445
+ agent: 'git/isomorphic-git@1.44.17',
10446
10446
  };
10447
10447
 
10448
10448
  class FIFO {
package/index.js CHANGED
@@ -10429,8 +10429,8 @@ function filterCapabilities(server, client) {
10429
10429
 
10430
10430
  const pkg = {
10431
10431
  name: 'isomorphic-git',
10432
- version: '1.44.16',
10433
- agent: 'git/isomorphic-git@1.44.16',
10432
+ version: '1.44.17',
10433
+ agent: 'git/isomorphic-git@1.44.17',
10434
10434
  };
10435
10435
 
10436
10436
  class FIFO {