本文整理汇总了TypeScript中follow-redirects.https.get方法的典型用法代码示例。如果您正苦于以下问题:TypeScript https.get方法的具体用法?TypeScript https.get怎么用?TypeScript https.get使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类follow-redirects.https
的用法示例。
在下文中一共展示了https.get方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: getCoreDownloadURL
export function getCoreDownloadURL(callback: (err: Error, downloadURL?: string) => any) {
const registryUrl = "https://raw.githubusercontent.com/superpowers/superpowers-registry/master/registry.json";
const request = https.get(registryUrl, (res) => {
if (res.statusCode !== 200) {
callback(new Error(`Unexpected status code: ${res.statusCode}`));
return;
}
let content = "";
res.on("data", (chunk: string) => { content += chunk; });
res.on("end", () => {
let registry: any;
try { registry = JSON.parse(content); }
catch (err) {
callback(new Error(`Could not parse registry as JSON`));
return;
}
callback(null, registry.core.downloadURL);
});
});
request.on("error", (err: Error) => {
callback(err);
});
}
示例2: getLatestRelease
export function getLatestRelease(repositoryURL: string, callback: (version: string, downloadURL: string) => void) {
const repositoryPath = `${repositoryURL.replace("https://github.com", "/repos")}/releases/latest`;
const request = https.get({
hostname: "api.github.com",
path: repositoryPath,
headers: { "user-agent": "Superpowers" }
}, (res) => {
if (res.statusCode !== 200) {
console.error(`Couldn't get latest release from repository at ${repositoryURL}:`);
const err = new Error(`Unexpected status code: ${res.statusCode}`);
console.error(err.stack);
process.exit(1);
}
let content = "";
res.on("data", (chunk: string) => { content += chunk; });
res.on("end", () => {
const repositoryInfo = JSON.parse(content);
callback(repositoryInfo.tag_name.slice(1), repositoryInfo.assets[0].browser_download_url);
});
});
request.on("error", (err: Error) => {
console.error(`Couldn't get latest release from repository at ${repositoryURL}:`);
console.error(err.stack);
process.exit(1);
});
}
示例3: downloadRelease
function downloadRelease(downloadURL: string, downloadPath: string, callback: () => void) {
mkdirp.sync(downloadPath);
https.get({
hostname: "github.com",
path: downloadURL,
headers: { "user-agent": "Superpowers" }
}, (res) => {
if (res.statusCode !== 200) {
console.error("Couldn't download the release:");
const err = new Error(`Unexpected status code: ${res.statusCode}`);
console.error(err.stack);
process.exit(1);
}
let rootFolderName: string;
res.pipe(unzip.Parse())
.on("entry", (entry: any) => {
if (rootFolderName == null) {
rootFolderName = entry.path;
return;
}
const entryPath = `${downloadPath}/${entry.path.replace(rootFolderName, "")}`;
if (entry.type === "Directory") mkdirp.sync(entryPath);
else entry.pipe(fs.createWriteStream(entryPath));
})
.on("close", () => { callback(); });
});
}
示例4: getRegistry
function getRegistry(callback: (err: Error, registry: Registry) => any) {
// FIXME: Use registry.json instead once the next release is out
const registryUrl = "https://raw.githubusercontent.com/superpowers/superpowers-core/master/registryNext.json";
const request = https.get(registryUrl, (res) => {
if (res.statusCode !== 200) {
callback(new Error(`Unexpected status code: ${res.statusCode}`), null);
return;
}
let content = "";
res.on("data", (chunk: string) => { content += chunk; });
res.on("end", () => {
let registry: Registry;
try { registry = JSON.parse(content); }
catch (err) {
callback(new Error(`Could not parse registry as JSON`), null);
return;
}
if (registry.version !== currentRegistryVersion) callback(new Error("The registry format has changed. Please update Superpowers."), null);
else callback(null, registry);
});
});
request.on("error", (err: Error) => {
callback(err, null);
});
}
示例5: getRegistry
export function getRegistry(callback: (err: Error, registry: Registry) => any) {
// FIXME: Use registry.json instead once the next release is out
const registryUrl = "https://raw.githubusercontent.com/superpowers/superpowers-core/master/registryNext.json";
const request = https.get(registryUrl, (res) => {
if (res.statusCode !== 200) {
callback(new Error(`Unexpected status code: ${res.statusCode}`), null);
return;
}
let content = "";
res.on("data", (chunk: string) => { content += chunk; });
res.on("end", () => {
let registry: Registry;
try { registry = JSON.parse(content); }
catch (err) {
callback(new Error(`Could not parse registry as JSON`), null);
return;
}
if (registry.version !== currentRegistryVersion) {
callback(new Error("The registry format has changed. Please update Superpowers."), null);
} else {
getLatestRelease("https://github.com/superpowers/superpowers-core", (version, downloadURL) => {
const packageData = fs.readFileSync(`${__dirname}/../../package.json`, { encoding: "utf8" });
const { version: localVersion } = JSON.parse(packageData);
let isLocalDev = true;
try { if (!fs.lstatSync(`${__dirname}/../../.git`).isDirectory()) isLocalDev = false; }
catch (err) { isLocalDev = false; }
registry.core = { version, downloadURL, localVersion, isLocalDev };
async.each(Object.keys(registry.systems), (systemId, cb) => {
const system = registry.systems[systemId];
getLatestRelease(system.repository, (version, downloadURL) => {
system.version = version;
system.downloadURL = downloadURL;
if (systemsById[systemId] != null) {
system.localVersion = systemsById[systemId].version;
system.isLocalDev = systemsById[systemId].isDev;
} else {
system.isLocalDev = false;
}
cb();
});
}, (err) => { callback(err, registry); });
});
}
});
});
request.on("error", (err: Error) => {
callback(err, null);
});
}
示例6: downloadFile
export function downloadFile(url: string, dest: string): q.Promise<any> {
let deferal = q.defer<any>();
let file = fs.createWriteStream(dest);
let request = https.get(url, (response: IncomingMessage) => {
response.pipe(file);
file.on("finish", () => {
deferal.resolve();
});
}).on("error", (err: any) => {
deferal.reject(err);
});
return deferal.promise;
}
示例7: getRegistry
export function getRegistry(callback: (err: Error, registry: Registry) => any) {
// FIXME: Use registry.json instead once the next release is out
const registryUrl = "https://raw.githubusercontent.com/superpowers/superpowers-core/master/registryNext.json";
const request = https.get(registryUrl, (res) => {
if (res.statusCode !== 200) {
callback(new Error(`Unexpected status code: ${res.statusCode}`), null);
return;
}
let content = "";
res.on("data", (chunk: string) => { content += chunk; });
res.on("end", () => {
let registry: Registry;
try { registry = JSON.parse(content); }
catch (err) {
callback(new Error(`Could not parse registry as JSON`), null);
return;
}
if (registry.version !== currentRegistryVersion) {
callback(new Error("The registry format has changed. Please update Superpowers."), null);
} else {
getLatestRelease("https://github.com/superpowers/superpowers-core", (version, downloadURL) => {
registry.core = { version, downloadURL };
async.each(Object.keys(registry.systems), (systemId, cb) => {
const system = registry.systems[systemId];
getLatestRelease(system.repository, (version, downloadURL) => {
system.version = version;
system.downloadURL = downloadURL;
cb();
});
}, (err) => { callback(err, registry); });
});
}
});
});
request.on("error", (err: Error) => {
callback(err, null);
});
}
示例8: downloadRelease
export function downloadRelease(downloadURL: string, downloadPath: string, callback: (err: string) => void) {
console.log("0%");
https.get({
hostname: "github.com",
path: downloadURL,
headers: { "user-agent": "Superpowers" }
}, (res) => {
if (res.statusCode !== 200) {
callback(`Unexpected status code: ${res.statusCode}`);
return;
}
let progress = 0;
let progressMax = parseInt(res.headers["content-length"], 10) * 2;
const buffers: Buffer[] = [];
res.on("data", (data: Buffer) => { buffers.push(data); progress += data.length; onProgress(progress / progressMax); });
res.on("end", () => {
const zipBuffer = Buffer.concat(buffers);
yauzl.fromBuffer(zipBuffer, { lazyEntries: true }, (err: Error, zipFile: any) => {
if (err != null) throw err;
progress = zipFile.entryCount;
progressMax = zipFile.entryCount * 2;
const rootFolderName = path.parse(downloadURL).name;
zipFile.readEntry();
zipFile.on("entry", (entry: any) => {
if (entry.fileName.indexOf(rootFolderName) !== 0) throw new Error(`Found file outside of root folder: ${entry.fileName} (${rootFolderName})`);
const filename = path.join(downloadPath, entry.fileName.replace(rootFolderName, ""));
if (/\/$/.test(entry.fileName)) {
mkdirp(filename, (err) => {
if (err != null) throw err;
progress++;
onProgress(progress / progressMax);
zipFile.readEntry();
});
} else {
zipFile.openReadStream(entry, (err: Error, readStream: NodeJS.ReadableStream) => {
if (err) throw err;
mkdirp(path.dirname(filename), (err: Error) => {
if (err) throw err;
readStream.pipe(fs.createWriteStream(filename));
readStream.on("end", () => {
progress++;
onProgress(progress / progressMax);
zipFile.readEntry();
});
});
});
}
});
zipFile.on("end", () => {
callback(null);
});
});
});
});
}
示例9:
http.request({
host: 'localhost',
path: '/a/b',
port: 8000,
maxRedirects: 12,
}, (response) => {
console.log(response.responseUrl, response.redirects);
response.on('data', (chunk) => {
console.log(chunk);
});
}).on('error', (err) => {
console.error(err);
});
http.get('http://bit.ly/900913', (response) => {
response.on('data', (chunk) => {
console.log(chunk);
});
}).on('error', (err) => {
console.error(err);
});
https.get('http://bit.ly/900913', (response) => {
console.log(response.responseUrl, response.redirects);
response.on('data', (chunk) => {
console.log(chunk);
});
}).on('error', (err: Error) => {
console.error(err);
});
示例10: getRegistry
export function getRegistry(callback: (err: Error, registry: Registry) => any) {
const registryUrl = "https://raw.githubusercontent.com/superpowers/superpowers-registry/master/registry.json";
const request = https.get(registryUrl, (res) => {
if (res.statusCode !== 200) {
callback(new Error(`Unexpected status code: ${res.statusCode}`), null);
return;
}
let content = "";
res.on("data", (chunk: string) => { content += chunk; });
res.on("end", () => {
let registry: Registry;
try { registry = JSON.parse(content); }
catch (err) {
callback(new Error(`Could not parse registry as JSON`), null);
return;
}
if (registry.version !== currentRegistryVersion) {
callback(new Error("The registry format has changed. Please update Superpowers."), null);
} else {
const packageData = fs.readFileSync(`${__dirname}/../../package.json`, { encoding: "utf8" });
const { version: localCoreVersion } = JSON.parse(packageData);
registry.core.localVersion = localCoreVersion;
let isLocalCoreDev = true;
try { if (!fs.lstatSync(`${__dirname}/../../.git`).isDirectory()) isLocalCoreDev = false; }
catch (err) { isLocalCoreDev = false; }
registry.core.isLocalDev = isLocalCoreDev;
async.each(Object.keys(registry.systems), (systemId, cb) => {
const registrySystem = registry.systems[systemId];
const localSystem = systemsById[systemId];
if (localSystem != null) {
registrySystem.localVersion = localSystem.version;
registrySystem.isLocalDev = localSystem.isDev;
} else {
registrySystem.isLocalDev = false;
}
async.each(Object.keys(registrySystem.plugins), (authorName, cb) => {
async.each(Object.keys(registrySystem.plugins[authorName]), (pluginName, cb) => {
const registryPlugin = registrySystem.plugins[authorName][pluginName];
const localPlugin = localSystem != null && localSystem.plugins[authorName] != null ? localSystem.plugins[authorName][pluginName] : null;
if (localPlugin != null) {
registryPlugin.localVersion = localPlugin.version;
registryPlugin.isLocalDev = localPlugin.isDev;
} else {
registryPlugin.isLocalDev = false;
}
cb();
}, cb);
}, cb);
}, (err) => { callback(err, registry); });
}
});
});
request.on("error", (err: Error) => {
callback(err, null);
});
}