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 Dec 1, 2024
1 parent 28a3f0f commit d5cfd28
Show file tree
Hide file tree
Showing 13 changed files with 364 additions and 235 deletions.
42 changes: 24 additions & 18 deletions src/develop.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,25 +43,31 @@ cmd-shift-o to run the package out of the newly cloned repository.\
return options.alias('h', 'help').describe('help', 'Print this usage message');
}

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}`;
}
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);
}

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

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

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

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

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

const response = await request.get(requestSettings);
const body = response.body ?? [];
if (response.statusCode === 200) {
let packages = body.filter(pack => pack?.releases != null);
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);
packages = packages.map(({readme, metadata, downloads, stargazers_count}) => _.extend({}, metadata, {readme, downloads, stargazers_count}));
packages = _.sortBy(packages, 'name');
return packages;
}

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

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

async getAllFeaturedPackages(atomVersion) {
Expand Down
34 changes: 20 additions & 14 deletions src/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,26 +209,32 @@ 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
async requestPackage(packageName) {
requestPackage(packageName) {
const requestSettings = {
url: `${config.getAtomPackagesUrl()}/${packageName}`,
json: true,
retries: 4
};
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 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}`);
}

return body;
resolve(body);
});
});
}

// Is the package at the specified version already installed?
Expand Down
91 changes: 61 additions & 30 deletions src/publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,24 +105,35 @@ 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.
async waitForTagToBeAvailable(pack, tag) {
const retryCount = 5;
waitForTagToBeAvailable(pack, tag) {
let retryCount = 5;
const interval = 1000;
const requestSettings = {
url: `https://api.github.com/repos/${Packages.getRepository(pack)}/tags`,
json: true
};

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
}
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();
});
}

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

// Register the current repository with the package registry.
Expand Down Expand Up @@ -180,16 +197,22 @@ have published it.\
authorization: token
}
};
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?
}
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}`);
}

this.logSuccess();
return true;
this.logSuccess();
return resolve(true);
});
});
} catch (error) {
this.logFailure();
throw error;
Expand All @@ -215,12 +238,20 @@ have published it.\
authorization: token
}
};
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}`;
}
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();
});
});
}

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

0 comments on commit d5cfd28

Please sign in to comment.