https#get TypeScript Examples

The following examples show how to use https#get. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: util.ts    From angular-git-info with MIT License 7 votes vote down vote up
/**
 * Attempt to retrieve the latest package version from NPM
 * Return an optional "latest" version in case of error
 * @param packageName
 */
export function getLatestNodeVersion(packageName: string): Promise<NodePackage> {
    const DEFAULT_VERSION = 'latest';

    return new Promise((resolve) => {
        return get(`https://registry.npmjs.org/${packageName}`, (res) => {
            let rawData = '';
            res.on('data', (chunk) => (rawData += chunk));
            res.on('end', () => {
                try {
                    const response = JSON.parse(rawData);
                    const version = (response && response['dist-tags']) || {};

                    resolve(buildPackage(packageName, version.latest));
                } catch (e) {
                    resolve(buildPackage(packageName));
                }
            });
        }).on('error', () => resolve(buildPackage(packageName)));
    });

    function buildPackage(name: string, version: string = DEFAULT_VERSION): NodePackage {
        return {name, version};
    }
}
Example #2
Source File: download.ts    From devoirs with MIT License 6 votes vote down vote up
download = (from: string, to: string) =>
  new Promise<void>((resolve) => {
    const stream = createWriteStream(to);

    get(from, (response) => {
      response.pipe(stream);
      response.on('end', () => {
        stream.end();
        resolve();
      });
    });
  })
Example #3
Source File: install.ts    From blake3 with MIT License 6 votes vote down vote up
async function download(url: string): Promise<boolean> {
  return new Promise<boolean>(resolve => {
    const onError = (err: Error) => {
      console.error(`Could not download binding from ${url}: ${err.stack || err.message}`);
      resolve(false);
    };

    const req = get(url, res => {
      if (res.headers.location) {
        resolve(download(res.headers.location));
        return;
      }

      if (!res.statusCode || res.statusCode >= 300) {
        console.error(`Unexpected ${res.statusCode} from ${url}`);
        resolve(false);
        return;
      }

      pipeline(res, createWriteStream(bindingPath), err => (err ? onError(err) : resolve(true)));
    });

    req.on('error', onError);
  });
}