Skip to content

Commit

Permalink
remove request dependency (#179149)
Browse files Browse the repository at this point in the history
* remove request dependency

* replace gulp-remote-retry-src in build scripts

* fix retry delay & polish
  • Loading branch information
aeschli committed Apr 5, 2023
1 parent 35d591d commit 899cdd5
Show file tree
Hide file tree
Showing 13 changed files with 172 additions and 330 deletions.
2 changes: 1 addition & 1 deletion build/gulpfile.reh.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ if (defaultNodeTask) {
}

function nodejs(platform, arch) {
const remote = require('gulp-remote-retry-src');
const { remote } = require('./lib/gulpRemoteSource');
const untar = require('gulp-untar');

if (arch === 'ia32') {
Expand Down
9 changes: 4 additions & 5 deletions build/lib/extensions.js

Large diffs are not rendered by default.

7 changes: 3 additions & 4 deletions build/lib/extensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import webpack = require('webpack');
import { getProductionDependencies } from './dependencies';
import { getExtensionStream } from './builtInExtensions';
import { getVersion } from './getVersion';
import { remote, IOptions as IRemoteSrcOptions } from './gulpRemoteSource';
import { assetFromGithub } from './github';

const root = path.dirname(path.dirname(__dirname));
Expand Down Expand Up @@ -209,18 +210,16 @@ const baseHeaders = {
};

export function fromMarketplace(serviceUrl: string, { name: extensionName, version, metadata }: IBuiltInExtension): Stream {
const remote = require('gulp-remote-retry-src');
const json = require('gulp-json-editor') as typeof import('gulp-json-editor');

const [publisher, name] = extensionName.split('.');
const url = `${serviceUrl}/publishers/${publisher}/vsextensions/${name}/${version}/vspackage`;

fancyLog('Downloading extension:', ansiColors.yellow(`${extensionName}@${version}`), '...');

const options = {
const options: IRemoteSrcOptions = {
base: url,
requestOptions: {
gzip: true,
fetchOptions: {
headers: baseHeaders
}
};
Expand Down
8 changes: 4 additions & 4 deletions build/lib/github.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions build/lib/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import { Stream } from 'stream';
import got from 'got';
import * as remote from 'gulp-remote-retry-src';
import { remote } from './gulpRemoteSource';
import * as through2 from 'through2';

const ghApiHeaders: Record<string, string> = {
Expand All @@ -29,7 +29,7 @@ const ghDownloadHeaders = {
export function assetFromGithub(repo: string, version: string, assetFilter: (name: string) => boolean): Stream {
return remote(`/repos/${repo.replace(/^\/|\/$/g, '')}/releases/tags/v${version}`, {
base: 'https://api.github.com',
requestOptions: { headers: ghApiHeaders }
fetchOptions: { headers: ghApiHeaders }
}).pipe(through2.obj(function (file, _enc, callback) {
const asset = JSON.parse(file.contents.toString()).assets.find((a: { name: string }) => assetFilter(a.name));
if (!asset) {
Expand Down
58 changes: 58 additions & 0 deletions build/lib/gulpRemoteSource.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

69 changes: 69 additions & 0 deletions build/lib/gulpRemoteSource.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import * as es from 'event-stream';
import fetch, { RequestInit } from 'node-fetch';
import * as VinylFile from 'vinyl';
import * as through2 from 'through2';

export interface IOptions {
base?: string;
buffer?: boolean;
fetchOptions?: RequestInit;
}

export function remote(urls: string[] | string, options: IOptions): es.ThroughStream {
if (options === undefined) {
options = {};
}

if (typeof options.base !== 'string' && options.base !== null) {
options.base = '/';
}

if (typeof options.buffer !== 'boolean') {
options.buffer = true;
}

if (!Array.isArray(urls)) {
urls = [urls];
}

return es.readArray(urls).pipe(es.map<string, VinylFile | void>((data: string, cb) => {
const url = [options.base, data].join('');
fetchWithRetry(url, options).then(file => {
cb(undefined, file);
}, error => {
cb(error);
});
}));
}

async function fetchWithRetry(url: string, options: IOptions, retries = 3, retryDelay = 1000): Promise<VinylFile> {
try {
const response = await fetch(url, options.fetchOptions);
if (response.ok && (response.status >= 200 && response.status < 300)) {
// request must be piped out once created, or we'll get this error: "You cannot pipe after data has been emitted from the response."
const contents = options.buffer ? await response.buffer() : response.body.pipe(through2());
return new VinylFile({
cwd: '/',
base: options.base,
path: url,
contents
});
}
throw new Error(`Request ${url} failed with status code: ${response.status}`);
} catch (e) {
if (retries > 0) {
await new Promise(c => setTimeout(c, retryDelay));
return fetchWithRetry(url, options, retries - 1, retryDelay * 3);
}
throw e;
}
}




23 changes: 0 additions & 23 deletions build/lib/typings/gulp-remote-src.d.ts

This file was deleted.

1 change: 0 additions & 1 deletion build/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
"@types/node": "16.x",
"@types/p-limit": "^2.2.0",
"@types/pump": "^1.0.1",
"@types/request": "^2.47.0",
"@types/rimraf": "^2.0.4",
"@types/through": "^0.0.29",
"@types/through2": "^2.0.36",
Expand Down
27 changes: 0 additions & 27 deletions build/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -402,11 +402,6 @@
"@types/node" "*"
"@types/responselike" "*"

"@types/caseless@*":
version "0.12.1"
resolved "https://registry.yarnpkg.com/@types/caseless/-/caseless-0.12.1.tgz#9794c69c8385d0192acc471a540d1f8e0d16218a"
integrity sha512-FhlMa34NHp9K5MY1Uz8yb+ZvuX0pnvn3jScRSNAb75KHGB8d3rEU6hqMs3Z2vjuytcMfRg6c5CHMc3wtYyD2/A==

"@types/chokidar@*":
version "1.7.5"
resolved "https://registry.yarnpkg.com/@types/chokidar/-/chokidar-1.7.5.tgz#1fa78c8803e035bed6d98e6949e514b133b0c9b6"
Expand Down Expand Up @@ -442,13 +437,6 @@
resolved "https://registry.yarnpkg.com/@types/fancy-log/-/fancy-log-1.3.0.tgz#a61ab476e5e628cd07a846330df53b85e05c8ce0"
integrity sha512-mQjDxyOM1Cpocd+vm1kZBP7smwKZ4TNokFeds9LV7OZibmPJFEzY3+xZMrKfUdNT71lv8GoCPD6upKwHxubClw==

"@types/form-data@*":
version "2.2.1"
resolved "https://registry.yarnpkg.com/@types/form-data/-/form-data-2.2.1.tgz#ee2b3b8eaa11c0938289953606b745b738c54b1e"
integrity sha512-JAMFhOaHIciYVh8fb5/83nmuO/AHwmto+Hq7a9y8FzLDcC1KCU344XDOMEmahnrTFlHjgh4L0WJFczNIX2GxnQ==
dependencies:
"@types/node" "*"

"@types/fs-extra@^9.0.12":
version "9.0.12"
resolved "https://registry.yarnpkg.com/@types/fs-extra/-/fs-extra-9.0.12.tgz#9b8f27973df8a7a3920e8461517ebf8a7d4fdfaf"
Expand Down Expand Up @@ -611,16 +599,6 @@
dependencies:
"@types/node" "*"

"@types/request@^2.47.0":
version "2.47.0"
resolved "https://registry.yarnpkg.com/@types/request/-/request-2.47.0.tgz#76a666cee4cb85dcffea6cd4645227926d9e114e"
integrity sha512-/KXM5oev+nNCLIgBjkwbk8VqxmzI56woD4VUxn95O+YeQ8hJzcSmIZ1IN3WexiqBb6srzDo2bdMbsXxgXNkz5Q==
dependencies:
"@types/caseless" "*"
"@types/form-data" "*"
"@types/node" "*"
"@types/tough-cookie" "*"

"@types/responselike@*", "@types/responselike@^1.0.0":
version "1.0.0"
resolved "https://registry.yarnpkg.com/@types/responselike/-/responselike-1.0.0.tgz#251f4fe7d154d2bad125abe1b429b23afd262e29"
Expand Down Expand Up @@ -655,11 +633,6 @@
resolved "https://registry.yarnpkg.com/@types/tmp/-/tmp-0.2.1.tgz#83ecf4ec22a8c218c71db25f316619fe5b986011"
integrity sha512-7cTXwKP/HLOPVgjg+YhBdQ7bMiobGMuoBmrGmqwIWJv8elC6t1DfVc/mn4fD9UE1IjhwmhaQ5pGVXkmXbH0rhg==

"@types/tough-cookie@*":
version "2.3.2"
resolved "https://registry.yarnpkg.com/@types/tough-cookie/-/tough-cookie-2.3.2.tgz#e0d481d8bb282ad8a8c9e100ceb72c995fb5e709"
integrity sha512-vOVmaruQG5EatOU/jM6yU2uCp3Lz6mK1P5Ztu4iJjfM4SVHU9XYktPUQtKlIXuahqXHdEyUarMrBEwg5Cwu+bA==

"@types/tunnel@^0.0.3":
version "0.0.3"
resolved "https://registry.yarnpkg.com/@types/tunnel/-/tunnel-0.0.3.tgz#f109e730b072b3136347561fc558c9358bb8c6e9"
Expand Down
5 changes: 0 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -165,13 +165,11 @@
"gulp-json-editor": "^2.5.0",
"gulp-plumber": "^1.2.0",
"gulp-postcss": "^9.0.0",
"gulp-remote-retry-src": "^0.8.0",
"gulp-rename": "^1.2.0",
"gulp-replace": "^0.5.4",
"gulp-sourcemaps": "^3.0.0",
"gulp-svgmin": "^4.1.0",
"gulp-untar": "^0.0.7",
"gulp-vinyl-zip": "^2.1.2",
"http-server": "^14.1.1",
"husky": "^0.13.1",
"innosetup": "6.0.5",
Expand All @@ -198,7 +196,6 @@
"pump": "^1.0.1",
"queue": "3.0.6",
"rcedit": "^1.1.0",
"request": "^2.85.0",
"rimraf": "^2.2.8",
"sinon": "^12.0.1",
"sinon-test": "^3.1.3",
Expand All @@ -212,8 +209,6 @@
"typescript-formatter": "7.1.0",
"underscore": "^1.12.1",
"util": "^0.12.4",
"vinyl": "^2.0.0",
"vinyl-fs": "^3.0.0",
"vscode-nls-dev": "^3.3.1",
"webpack": "^5.77.0",
"webpack-cli": "^5.0.1",
Expand Down
Loading

0 comments on commit 899cdd5

Please sign in to comment.