Skip to content

Commit

Permalink
Changing own request.js to async interface
Browse files Browse the repository at this point in the history
This is the version that passes all the existing tests.
The HTTP requests used to be handled by a callback that had three arguments: one for the errors if there are any, one for the response and one for the payload within the response.
Now, the error has been turned into rejected promises (exceptions when awaited) and the payload has simply been dropped. It's extracted from the response via the .body attribute.
  • Loading branch information
2colours committed Aug 31, 2024
1 parent 7a0c3fa commit a06a641
Show file tree
Hide file tree
Showing 13 changed files with 235 additions and 364 deletions.
42 changes: 18 additions & 24 deletions src/develop.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,31 +43,25 @@ cmd-shift-o to run the package out of the newly cloned repository.\
return options.alias('h', 'help').describe('help', 'Print this usage message');
}

getRepositoryUrl(packageName) {
return new Promise((resolve, reject) => {
const requestSettings = {
url: `${config.getAtomPackagesUrl()}/${packageName}`,
json: true
};
return request.get(requestSettings, (error, response, body) => {
body ??= {};
if (error != null) {
return void reject(`Request for package information failed: ${error.message}`);
}

if (response.statusCode === 200) {
const repositoryUrl = body.repository.url;
if (repositoryUrl) {
return void resolve(repositoryUrl);
}

return void reject(`No repository URL found for package: ${packageName}`);
}
async getRepositoryUrl(packageName) {
const requestSettings = {
url: `${config.getAtomPackagesUrl()}/${packageName}`,
json: true
};
const response = await request.get(requestSettings).catch(error => Promise.reject(`Request for package information failed: ${error.message}`));
const body = response.body ?? {};

if (response.statusCode === 200) {
const repositoryUrl = body.repository.url;
if (repositoryUrl) {
return repositoryUrl;
}

throw `No repository URL found for package: ${packageName}`;
}

const message = request.getErrorMessage(body, error);
return void reject(`Request for package information failed: ${message}`);
});
});
const message = request.getErrorMessage(body, error);
throw `Request for package information failed: ${message}`;
}

async cloneRepository(repoUrl, packageDirectory, options) {
Expand Down
24 changes: 10 additions & 14 deletions src/featured.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,29 +28,25 @@ List the Pulsar packages and themes that are currently featured.\
return options.boolean('json').describe('json', 'Output featured packages as JSON array');
}

getFeaturedPackagesByType(atomVersion, packageType) {
async getFeaturedPackagesByType(atomVersion, packageType) {

const requestSettings = {
url: `${config.getAtomApiUrl()}/${packageType}/featured`,
json: true
};
if (atomVersion) { requestSettings.qs = {engine: atomVersion}; }

return new Promise((resolve, reject) => void request.get(requestSettings, function(error, response, body) {
body ??= [];
if (error != null) {
return void reject(error);
}
if (response.statusCode === 200) {
let packages = body.filter(pack => (pack != null ? pack.releases : undefined) != null);
const response = await request.get(requestSettings);
const body = response.body ?? [];
if (response.statusCode === 200) {
let packages = body.filter(pack => pack?.releases != null);
packages = packages.map(({readme, metadata, downloads, stargazers_count}) => _.extend({}, metadata, {readme, downloads, stargazers_count}));
packages = _.sortBy(packages, 'name');
return void resolve(packages);
}

const message = request.getErrorMessage(body, error);
reject(`Requesting packages failed: ${message}`);
}));
return packages;
}

const message = request.getErrorMessage(body, error);
throw `Requesting packages failed: ${message}`;
}

async getAllFeaturedPackages(atomVersion) {
Expand Down
34 changes: 14 additions & 20 deletions src/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,32 +209,26 @@ Run ppm -v after installing Git to see what version has been detected.\
// packageName - The string name of the package to request.
//
// return value - A Promise that rejects with an appropriate error or resolves to the response body
requestPackage(packageName) {
async requestPackage(packageName) {
const requestSettings = {
url: `${config.getAtomPackagesUrl()}/${packageName}`,
json: true,
retries: 4
};
return new Promise((resolve, reject) => {
request.get(requestSettings, (error, response, body) => {
let message;
body ??= {};
if (error != null) {
message = `Request for package information failed: ${error.message}`;
if (error.status) { message += ` (${error.status})`; }
return void reject(message);
}
if (response.statusCode !== 200) {
message = request.getErrorMessage(body, error);
return void reject(`Request for package information failed: ${message}`);
}
if (!body.releases.latest) {
return void reject(`No releases available for ${packageName}`);
}

resolve(body);
});
const response = await request.get(requestSettings).catch(error => {
const message = request.getErrorMessage(body, error);
throw `Request for package information failed: ${message}`;
});
const body = response.body ?? {};
if (response.statusCode !== 200) {
const message = request.getErrorMessage(body, null);
throw `Request for package information failed: ${message}`;
}
if (!body.releases.latest) {
throw `No releases available for ${packageName}`;
}

return body;
}

// Is the package at the specified version already installed?
Expand Down
91 changes: 30 additions & 61 deletions src/publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,35 +105,24 @@ have published it.\
// return value - A Promise that resolves (without a value) when either the
// number of max retries have been reached or the tag could
// actually be retrieved.
waitForTagToBeAvailable(pack, tag) {
let retryCount = 5;
async waitForTagToBeAvailable(pack, tag) {
const retryCount = 5;
const interval = 1000;
const requestSettings = {
url: `https://api.github.com/repos/${Packages.getRepository(pack)}/tags`,
json: true
};

return new Promise((resolve, _reject) => {
const requestTags = () => {
request.get(
requestSettings,
(_error, response, tags) => {
tags ??= [];
if (response?.statusCode === 200) {
if (tags.some(t => t.name === tag)) {
resolve();
return;
}
}
if (--retryCount <= 0) {
return void resolve();
}
setTimeout(requestTags, interval);
}
);
};
requestTags();
});
for (let i = 0; i < retryCount; i++) {
const response = await request.get(requestSettings).catch();
const tags = response.body ?? [];
if (response?.statusCode === 200) {
if (tags.some(t => t.name === tag)) {
return;
}
}
await new Promise(resolve => setTimeout(resolve, interval)); //not strictly necessary on the last iteration
}
}

// Does the given package already exist in the registry?
Expand All @@ -151,15 +140,9 @@ have published it.\
authorization: token
}
};
return new Promise((resolve, reject) => {
request.get(requestSettings, (error, response, body) => {
body ??= {};
if (error != null) {
return void reject(error);
}
resolve(response.statusCode === 200);
});
});
const response = await request.get(requestSettings);
const body = response.body ?? {};
return response.statusCode === 200;
}

// Register the current repository with the package registry.
Expand Down Expand Up @@ -197,22 +180,16 @@ have published it.\
authorization: token
}
};
return new Promise((resolve, reject) => {
request.post(requestSettings, (error, response, body) => {
body ??= {};
if (error != null) {
return void reject(error);
}
if (response.statusCode !== 201) {
const message = request.getErrorMessage(body, error);
this.logFailure();
return void reject(`Registering package in ${repository} repository failed: ${message}`);
}
const response = await request.post(requestSettings);
const body = response.body ?? {};
if (response.statusCode !== 201) {
const message = request.getErrorMessage(body, null);
this.logFailure();
throw `Registering package in ${repository} repository failed: ${message}`; //again, why the double logging?
}

this.logSuccess();
return resolve(true);
});
});
this.logSuccess();
return true;
} catch (error) {
this.logFailure();
throw error;
Expand All @@ -238,20 +215,12 @@ have published it.\
authorization: token
}
};
return new Promise((resolve, reject) => {
request.post(requestSettings, (error, response, body) => {
body ??= {};
if (error != null) {
return void reject(error);
}
if (response.statusCode !== 201) {
const message = request.getErrorMessage(body, error);
return void reject(`Creating new version failed: ${message}`);
}

resolve();
});
});
const response = await request.post(requestSettings);
const body = response.body ?? {};
if (response.statusCode !== 201) {
const message = request.getErrorMessage(body, null);
throw `Creating new version failed: ${message}`;
}
}

// Publish the version of the package associated with the given tag.
Expand Down
Loading

0 comments on commit a06a641

Please sign in to comment.