Skip to content

Commit

Permalink
chore(deps): switched to @theia/request
Browse files Browse the repository at this point in the history
Closes eclipse-theia#12401

Signed-off-by: Akos Kitta <a.kitta@arduino.cc>
  • Loading branch information
Akos Kitta committed Apr 13, 2023
1 parent 506f277 commit ccf7e3a
Show file tree
Hide file tree
Showing 13 changed files with 100 additions and 319 deletions.
3 changes: 1 addition & 2 deletions dev-packages/application-package/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,14 @@
"watch": "theiaext watch"
},
"dependencies": {
"@theia/request": "1.36.0",
"@types/fs-extra": "^4.0.2",
"@types/request": "^2.0.3",
"@types/semver": "^5.4.0",
"@types/write-json-file": "^2.2.1",
"deepmerge": "^4.2.2",
"fs-extra": "^4.0.2",
"is-electron": "^2.1.0",
"nano": "^9.0.5",
"request": "^2.82.0",
"resolve-package-path": "^4.0.3",
"semver": "^5.4.1",
"write-json-file": "^2.2.0"
Expand Down
27 changes: 15 additions & 12 deletions dev-packages/application-package/src/npm-registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
// *****************************************************************************

/* eslint-disable @typescript-eslint/no-explicit-any */
import * as request from 'request';
import * as nano from 'nano';
import { RequestContext } from '@theia/request';
import { NodeRequestService } from '@theia/request/lib/node-request-service';
import { NpmRegistryProps } from './application-props';

export interface IChangeStream {
Expand Down Expand Up @@ -96,12 +97,15 @@ export class NpmRegistry {
protected changes?: nano.ChangesReaderScope;
protected readonly index = new Map<string, Promise<ViewResult>>();

protected request: NodeRequestService;

constructor(options?: Partial<NpmRegistryOptions>) {
this.options = {
watchChanges: false,
...options
};
this.resetIndex();
this.request = new NodeRequestService();
}

updateProps(props?: Partial<NpmRegistryProps>): void {
Expand Down Expand Up @@ -150,19 +154,18 @@ export class NpmRegistry {
const headers: {
[header: string]: string
} = {};
return new Promise((resolve, reject) => {
request({
url, headers
}, (err, response, body) => {
if (err) {
reject(err);
} else if (response.statusCode !== 200) {
reject(new Error(`${response.statusCode}: ${response.statusMessage} for ${url}`));
return new Promise(async (resolve, reject) => {
try {
const response = await this.request.request({ url, headers });
if (response.res.statusCode !== 200) {
reject(new Error(`HTTP ${response.res.statusCode}: for ${url}`));
} else {
const data = JSON.parse(body);
resolve(data);
const data = RequestContext.asJson(response);
resolve(data as ViewResult);
}
});
} catch (err) {
reject(err);
}
});
}

Expand Down
6 changes: 5 additions & 1 deletion dev-packages/application-package/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,9 @@
"include": [
"src"
],
"references": []
"references": [
{
"path": "../request"
}
]
}
5 changes: 2 additions & 3 deletions packages/plugin-dev/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@
"@theia/filesystem": "1.36.0",
"@theia/output": "1.36.0",
"@theia/plugin-ext": "1.36.0",
"@theia/request": "1.36.0",
"@theia/workspace": "1.36.0",
"@types/request": "^2.0.3",
"ps-tree": "^1.2.0",
"request": "^2.82.0"
"ps-tree": "^1.2.0"
},
"publishConfig": {
"access": "public"
Expand Down
23 changes: 13 additions & 10 deletions packages/plugin-dev/src/node/hosted-instance-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,12 @@
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
// *****************************************************************************

import { RequestOptions, RequestService } from '@theia/core/shared/@theia/request';
import { inject, injectable, named } from '@theia/core/shared/inversify';
import * as cp from 'child_process';
import * as fs from '@theia/core/shared/fs-extra';
import * as net from 'net';
import * as path from 'path';
import * as request from 'request';

import URI from '@theia/core/lib/common/uri';
import { ContributionProvider } from '@theia/core/lib/common/contribution-provider';
import { HostedPluginUriPostProcessor, HostedPluginUriPostProcessorSymbolName } from './hosted-plugin-uri-postprocessor';
Expand Down Expand Up @@ -101,7 +100,7 @@ export abstract class AbstractHostedInstanceManager implements HostedInstanceMan
protected isPluginRunning: boolean = false;
protected instanceUri: URI;
protected pluginUri: URI;
protected instanceOptions: object;
protected instanceOptions: Omit<RequestOptions, 'url'>;

@inject(HostedPluginSupport)
protected readonly hostedPluginSupport: HostedPluginSupport;
Expand All @@ -112,6 +111,9 @@ export abstract class AbstractHostedInstanceManager implements HostedInstanceMan
@inject(HostedPluginProcess)
protected readonly hostedPluginProcess: HostedPluginProcess;

@inject(RequestService)
protected readonly request: RequestService;

isRunning(): boolean {
return this.isPluginRunning;
}
Expand Down Expand Up @@ -148,7 +150,7 @@ export abstract class AbstractHostedInstanceManager implements HostedInstanceMan
this.pluginUri = pluginUri;
// disable redirect to grab the release
this.instanceOptions = {
followRedirect: false
followRedirects: 0
};
this.instanceOptions = await this.postProcessInstanceOptions(this.instanceOptions);
await this.checkInstanceUriReady();
Expand Down Expand Up @@ -212,14 +214,15 @@ export abstract class AbstractHostedInstanceManager implements HostedInstanceMan
* Ping the plugin URI (checking status of the head)
*/
private async ping(): Promise<boolean> {
return new Promise<boolean>((resolve, reject) => {
return new Promise<boolean>(async (resolve, reject) => {
const url = this.instanceUri.toString();
request.head(url, this.instanceOptions).on('response', res => {
try {
const response = await this.request.request({ url, type: 'HEAD', ...this.instanceOptions });
// Wait that the status is OK
resolve(res.statusCode === 200);
}).on('error', error => {
resolve(response.res.statusCode === 200);
} catch {
resolve(false);
});
}
});
}

Expand Down Expand Up @@ -274,7 +277,7 @@ export abstract class AbstractHostedInstanceManager implements HostedInstanceMan
return uri;
}

protected async postProcessInstanceOptions(options: object): Promise<object> {
protected async postProcessInstanceOptions(options: Omit<RequestOptions, 'url'>): Promise<Omit<RequestOptions, 'url'>> {
return options;
}

Expand Down
3 changes: 3 additions & 0 deletions packages/plugin-dev/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
"src"
],
"references": [
{
"path": "../../dev-packages/request"
},
{
"path": "../core"
},
Expand Down
5 changes: 2 additions & 3 deletions packages/plugin-ext-vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,12 @@
"@theia/navigator": "1.36.0",
"@theia/plugin": "1.36.0",
"@theia/plugin-ext": "1.36.0",
"@theia/request": "1.36.0",
"@theia/terminal": "1.36.0",
"@theia/typehierarchy": "1.36.0",
"@theia/userstorage": "1.36.0",
"@theia/workspace": "1.36.0",
"@types/request": "^2.0.3",
"filenamify": "^4.1.0",
"request": "^2.82.0"
"filenamify": "^4.1.0"
},
"publishConfig": {
"access": "public"
Expand Down
3 changes: 3 additions & 0 deletions packages/plugin-ext-vscode/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
"src"
],
"references": [
{
"path": "../../dev-packages/request"
},
{
"path": "../callhierarchy"
},
Expand Down
5 changes: 2 additions & 3 deletions packages/plugin-ext/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"@theia/output": "1.36.0",
"@theia/plugin": "1.36.0",
"@theia/preferences": "1.36.0",
"@theia/request": "1.36.0",
"@theia/scm": "1.36.0",
"@theia/search-in-workspace": "1.36.0",
"@theia/task": "1.36.0",
Expand All @@ -41,7 +42,6 @@
"macaddress": "^0.2.9",
"mime": "^2.4.4",
"ps-tree": "^1.2.0",
"request": "^2.82.0",
"semver": "^5.4.1",
"uuid": "^8.0.0",
"vhost": "^3.0.2",
Expand Down Expand Up @@ -90,8 +90,7 @@
"@types/decompress": "^4.2.2",
"@types/escape-html": "^0.0.20",
"@types/lodash.clonedeep": "^4.5.3",
"@types/ps-tree": "^1.1.0",
"@types/request": "^2.0.3"
"@types/ps-tree": "^1.1.0"
},
"nyc": {
"extends": "../../configs/nyc.json"
Expand Down
63 changes: 32 additions & 31 deletions packages/plugin-ext/src/main/node/plugin-github-resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
// *****************************************************************************

import { injectable } from '@theia/core/shared/inversify';
import { RequestService } from '@theia/core/shared/@theia/request';
import { inject, injectable } from '@theia/core/shared/inversify';
import * as fs from 'fs';
import * as os from 'os';
import * as path from 'path';
import * as request from 'request';
import { Duplex } from 'stream';
import { PluginDeployerResolver, PluginDeployerResolverContext } from '../../common';

/**
Expand All @@ -35,6 +36,9 @@ export class GithubPluginDeployerResolver implements PluginDeployerResolver {

private unpackedFolder: string;

@inject(RequestService)
protected readonly request: RequestService;

constructor() {
this.unpackedFolder = path.resolve(os.tmpdir(), 'github-remote');
if (!fs.existsSync(this.unpackedFolder)) {
Expand All @@ -48,7 +52,7 @@ export class GithubPluginDeployerResolver implements PluginDeployerResolver {
async resolve(pluginResolverContext: PluginDeployerResolverContext): Promise<void> {

// download the file
return new Promise<void>((resolve, reject) => {
return new Promise<void>(async (resolve, reject) => {
// extract data
const extracted = /^github:(.*)\/(.*)\/(.*)$/gm.exec(pluginResolverContext.getOriginId());
if (!extracted || extracted === null || extracted.length !== 4) {
Expand All @@ -74,34 +78,30 @@ export class GithubPluginDeployerResolver implements PluginDeployerResolver {
// latest version, need to get the redirect
const url = GithubPluginDeployerResolver.GITHUB_ENDPOINT + orgName + '/' + repoName + '/releases/latest';

// disable redirect to grab the release
const options = {
followRedirect: false
};
// if latest, resolve first the real version
if (version === 'latest') {
request.get(url, options).on('response', response => {

// should have a redirect
if (response.statusCode === 302) {
const redirectLocation = response.headers.location;
if (!redirectLocation) {
reject(new Error('Invalid github link with latest not being found'));
return;
}

// parse redirect link
const taggedValueArray = /^https:\/\/.*tag\/(.*)/gm.exec(redirectLocation);
if (!taggedValueArray || taggedValueArray.length !== 2) {
reject(new Error('The redirect link for latest is invalid ' + redirectLocation));
return;
}

// grab version of tag
this.grabGithubFile(pluginResolverContext, orgName, repoName, filename, taggedValueArray[1], resolve, reject);
// disable redirect to grab the release
const followRedirects = 0;
const response = await this.request.request({ url, followRedirects });
// should have a redirect
if (response.res.statusCode === 302) {
const redirectLocation = response.res.headers.location;
if (!redirectLocation) {
reject(new Error('Invalid github link with latest not being found'));
return;
}

// parse redirect link
const taggedValueArray = /^https:\/\/.*tag\/(.*)/gm.exec(redirectLocation);
if (!taggedValueArray || taggedValueArray.length !== 2) {
reject(new Error('The redirect link for latest is invalid ' + redirectLocation));
return;
}
});

// grab version of tag
this.grabGithubFile(pluginResolverContext, orgName, repoName, filename, taggedValueArray[1], resolve, reject);

}
} else {
this.grabGithubFile(pluginResolverContext, orgName, repoName, filename, version, resolve, reject);
}
Expand Down Expand Up @@ -132,10 +132,11 @@ export class GithubPluginDeployerResolver implements PluginDeployerResolver {

dest.addListener('finish', finish);
const url = GithubPluginDeployerResolver.GITHUB_ENDPOINT + orgName + '/' + repoName + '/releases/download/' + version + '/' + filename;
request.get(url)
.on('error', err => {
reject(err);
}).pipe(dest);
const stream = new Duplex();
this.request.request({ url }).then(({ buffer }) => {
stream.push(buffer);
stream.push(null);
}, reject);

}

Expand Down
18 changes: 11 additions & 7 deletions packages/plugin-ext/src/main/node/plugin-http-resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
// *****************************************************************************

import { injectable } from '@theia/core/shared/inversify';
import { RequestService } from '@theia/core/shared/@theia/request';
import { inject, injectable } from '@theia/core/shared/inversify';
import * as fs from 'fs';
import * as os from 'os';
import * as path from 'path';
import { Duplex } from 'stream';
import * as url from 'url';
import * as request from 'request';

import { PluginDeployerResolver, PluginDeployerResolverContext } from '../../common';

/**
Expand All @@ -33,6 +33,9 @@ export class HttpPluginDeployerResolver implements PluginDeployerResolver {

private unpackedFolder: string;

@inject(RequestService)
protected readonly request: RequestService;

constructor() {
this.unpackedFolder = path.resolve(os.tmpdir(), 'http-remote');
if (!fs.existsSync(this.unpackedFolder)) {
Expand Down Expand Up @@ -74,10 +77,11 @@ export class HttpPluginDeployerResolver implements PluginDeployerResolver {
const dest = fs.createWriteStream(unpackedPath);

dest.addListener('finish', finish);
request.get(pluginResolverContext.getOriginId())
.on('error', err => {
reject(err);
}).pipe(dest);
const stream = new Duplex();
this.request.request({ url: pluginResolverContext.getOriginId() }).then(({ buffer }) => {
stream.push(buffer);
stream.push(null);
}, reject);
});

}
Expand Down
3 changes: 3 additions & 0 deletions packages/plugin-ext/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
"src"
],
"references": [
{
"path": "../../dev-packages/request"
},
{
"path": "../bulk-edit"
},
Expand Down

0 comments on commit ccf7e3a

Please sign in to comment.