Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
}
while (true) {
if (this.readState === ReadState.BODY) {
this.readState = ReadState.INIT
}
else {
this.partIndex++
let taskIndex = this.partIndexToTaskIndex.get(this.partIndex)
if (taskIndex == null) {
if (this.isFinished) {
taskIndex = this.options.end
}
else {
throw newError("taskIndex is null", "ERR_DATA_SPLITTER_TASK_INDEX_IS_NULL")
}
}
const prevTaskIndex = this.partIndex === 0 ? this.options.start : (this.partIndexToTaskIndex.get(this.partIndex - 1)!! + 1 /* prev part is download, next maybe copy */)
if (prevTaskIndex < taskIndex) {
await this.copyExistingData(prevTaskIndex, taskIndex)
}
else if (prevTaskIndex > taskIndex) {
throw newError("prevTaskIndex must be < taskIndex", "ERR_DATA_SPLITTER_TASK_INDEX_ASSERT_FAILED")
}
if (this.isFinished) {
this.onPartEnd()
this.finishHandler()
return
}
async getLatestVersion(): Promise {
const cancellationToken = new CancellationToken()
const channelFile = getChannelFilename(this.getDefaultChannelName())
const releaseInfo = await this.getLatestVersionInfo(cancellationToken)
const asset = releaseInfo.assets.find(it => it.name === channelFile)
if (asset == null) {
// html_url must be always, but just to be sure
throw newError(`Cannot find ${channelFile} in the release ${releaseInfo.html_url || releaseInfo.name}`, "ERR_UPDATER_CHANNEL_FILE_NOT_FOUND")
}
const url = new URL(asset.url)
let result: any
try {
result = safeLoad((await this.httpRequest(url, this.configureHeaders("application/octet-stream"), cancellationToken))!!)
}
catch (e) {
if (e instanceof HttpError && e.statusCode === 404) {
throw newError(`Cannot find ${channelFile} in the latest release artifacts (${url}): ${e.stack || e.message}`, "ERR_UPDATER_CHANNEL_FILE_NOT_FOUND")
}
throw e
}
(result as PrivateGitHubUpdateInfo).assets = releaseInfo.assets
return result
let taskIndex = this.partIndexToTaskIndex.get(this.partIndex)
if (taskIndex == null) {
if (this.isFinished) {
taskIndex = this.options.end
}
else {
throw newError("taskIndex is null", "ERR_DATA_SPLITTER_TASK_INDEX_IS_NULL")
}
}
const prevTaskIndex = this.partIndex === 0 ? this.options.start : (this.partIndexToTaskIndex.get(this.partIndex - 1)!! + 1 /* prev part is download, next maybe copy */)
if (prevTaskIndex < taskIndex) {
await this.copyExistingData(prevTaskIndex, taskIndex)
}
else if (prevTaskIndex > taskIndex) {
throw newError("prevTaskIndex must be < taskIndex", "ERR_DATA_SPLITTER_TASK_INDEX_ASSERT_FAILED")
}
if (this.isFinished) {
this.onPartEnd()
this.finishHandler()
return
}
start = this.searchHeaderListEnd(chunk, start)
if (start === -1) {
this.readState = ReadState.HEADER
return
}
}
private async handleData(chunk: Buffer): Promise {
let start = 0
if (this.ignoreByteCount !== 0 && this.remainingPartDataCount !== 0) {
throw newError("Internal error", "ERR_DATA_SPLITTER_BYTE_COUNT_MISMATCH")
}
if (this.ignoreByteCount > 0) {
const toIgnore = Math.min(this.ignoreByteCount, chunk.length)
this.ignoreByteCount -= toIgnore
start = toIgnore
}
else if (this.remainingPartDataCount > 0) {
const toRead = Math.min(this.remainingPartDataCount, chunk.length)
this.remainingPartDataCount -= toRead
await this.processPartData(chunk, 0, toRead)
start = toRead
}
if (start === chunk.length) {
return
version = await this.getLatestVersionString(cancellationToken)
for (const element of feed.getElements("entry")) {
if (element.element("link").attribute("href").match(hrefRegExp)!![1] === version) {
latestRelease = element
break
}
}
}
}
catch (e) {
throw newError(`Cannot parse releases feed: ${e.stack || e.message},\nXML:\n${feedXml}`, "ERR_UPDATER_INVALID_RELEASE_FEED")
}
if (version == null) {
throw newError(`No published versions on GitHub`, "ERR_UPDATER_NO_PUBLISHED_VERSIONS")
}
const channelFile = getChannelFilename(this.getDefaultChannelName())
const channelFileUrl = newUrlFromBase(this.getBaseDownloadPath(version, channelFile), this.baseUrl)
const requestOptions = this.createRequestOptions(channelFileUrl)
let rawData: string
try {
rawData = (await this.executor.request(requestOptions, cancellationToken))!!
}
catch (e) {
if (!this.updater.allowPrerelease && e instanceof HttpError && e.statusCode === 404) {
throw newError(`Cannot find ${channelFile} in the latest release artifacts (${channelFileUrl}): ${e.stack || e.message}`, "ERR_UPDATER_CHANNEL_FILE_NOT_FOUND")
}
throw e
}
private onPartEnd() {
const expectedLength = this.partIndexToLength[this.partIndex - 1]
if (this.actualPartLength !== expectedLength) {
throw newError(`Expected length: ${expectedLength} differs from actual: ${this.actualPartLength}`, "ERR_DATA_SPLITTER_LENGTH_MISMATCH")
}
this.actualPartLength = 0
}
}
if (version == null) {
throw newError(`No published versions on GitHub`, "ERR_UPDATER_NO_PUBLISHED_VERSIONS")
}
const channelFile = getChannelFilename(this.getDefaultChannelName())
const channelFileUrl = newUrlFromBase(this.getBaseDownloadPath(version, channelFile), this.baseUrl)
const requestOptions = this.createRequestOptions(channelFileUrl)
let rawData: string
try {
rawData = (await this.executor.request(requestOptions, cancellationToken))!!
}
catch (e) {
if (!this.updater.allowPrerelease && e instanceof HttpError && e.statusCode === 404) {
throw newError(`Cannot find ${channelFile} in the latest release artifacts (${channelFileUrl}): ${e.stack || e.message}`, "ERR_UPDATER_CHANNEL_FILE_NOT_FOUND")
}
throw e
}
const result = parseUpdateInfo(rawData, channelFile, channelFileUrl)
if (result.releaseName == null) {
result.releaseName = latestRelease.elementValueOrEmpty("title")
}
if (result.releaseNotes == null) {
result.releaseNotes = computeReleaseNotes(this.updater.currentVersion, this.updater.fullChangelog, feed, latestRelease)
}
return result
}
if (!allowPrerelease) {
basePath = `${basePath}/latest`
}
const url = newUrlFromBase(basePath, this.baseUrl)
try {
const version = (JSON.parse((await this.httpRequest(url, this.configureHeaders("application/vnd.github.v3+json"), cancellationToken))!!))
if (allowPrerelease) {
return version.find((v: any) => v.prerelease) || version[0]
}
else {
return version
}
}
catch (e) {
throw newError(`Unable to find latest version on GitHub (${url}), please ensure a production release exists: ${e.stack || e.message}`, "ERR_UPDATER_LATEST_VERSION_NOT_FOUND")
}
}