-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from iwatakeshi/develop
v1.1.0-0
- Loading branch information
Showing
6 changed files
with
117 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { GitlyDownloadError, GitlyErrorType, GitlyExtractError, GitlyFetchError, GitlyUknownError } from '../error' | ||
|
||
describe('utils/error', () => { | ||
describe('GitlyFetchError', () => { | ||
it('should return an instance', () => { | ||
const error = new GitlyFetchError('message') | ||
expect(error.message).toBe('[gitly:fetch]: message') | ||
expect(error.code).toBe(-1) | ||
expect(error.type).toBe(GitlyErrorType.Fetch) | ||
}) | ||
}) | ||
|
||
describe('GitlyExtractError', () => { | ||
it('should return an instance', () => { | ||
const error = new GitlyExtractError('message') | ||
expect(error.message).toBe('[gitly:extract]: message') | ||
expect(error.code).toBe(-1) | ||
expect(error.type).toBe(GitlyErrorType.Extract) | ||
}) | ||
}) | ||
|
||
describe('GitlyDownloadError', () => { | ||
it('should return an instance', () => { | ||
const error = new GitlyDownloadError('message', 402) | ||
expect(error.message).toBe('[gitly:download]: message') | ||
expect(error.code).toBe(402) | ||
expect(error.type).toBe(GitlyErrorType.Download) | ||
}) | ||
}) | ||
|
||
describe('GitlyUknownError', () => { | ||
it('should return an instance', () => { | ||
const error = new GitlyUknownError('message', 402) | ||
expect(error.message).toBe('[gitly:uknown]: message') | ||
expect(error.code).toBe(402) | ||
expect(error.type).toBe(GitlyErrorType.Unknown) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,21 @@ | ||
import axios from 'axios' | ||
import * as stream from 'stream' | ||
import { promisify } from 'util' | ||
|
||
import { GitlyDownloadError } from './error' | ||
import write from './write' | ||
|
||
const pipeline = promisify(stream.pipeline) | ||
|
||
export default async function download(url: string, file: string): Promise<string> { | ||
return new Promise(async (resolve, reject) => { | ||
const response = await axios.get(url, { | ||
responseType: 'stream', | ||
validateStatus(status) { | ||
return status >= 200 && status < 500 | ||
} | ||
}) | ||
const status = response.status | ||
if (status >= 400) reject({ status, message: response.statusText }) | ||
else if (status >= 300 && status < 400) { | ||
return download(response.headers.location, file).then(resolve) | ||
} else response.data.pipe(await write(file)).on('close', () => resolve(file)) | ||
const response = await axios.get(url, { | ||
responseType: 'stream', validateStatus: status => status >= 200 && status < 500 | ||
}) | ||
|
||
const { statusText: message, status: code } = response | ||
if (code >= 400) throw new GitlyDownloadError(message, code) | ||
else if (code >= 300 && code < 400) { | ||
return download(response.headers.location, file) | ||
} else await pipeline(response.data, await write(file)) | ||
return file | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
export enum GitlyErrorType { | ||
Fetch = 'fetch', | ||
Extract = 'extract', | ||
Download = 'download', | ||
Unknown = 'unknown' | ||
} | ||
|
||
export default abstract class GitlyAbstractError extends Error { | ||
static type: GitlyErrorType | ||
type: GitlyErrorType | ||
rawMessage: string | ||
constructor(readonly message: string, readonly code: number = -1) { | ||
super(message) | ||
this.rawMessage = message | ||
const type = this.type = this.ctor.type | ||
this.message = `[${type ? `gitly:${type}` : 'gitly'}]: ${message}` | ||
Object.setPrototypeOf(this, new.target.prototype) | ||
} | ||
|
||
get ctor(): typeof GitlyAbstractError { | ||
return (this.constructor) as typeof GitlyAbstractError | ||
} | ||
} | ||
|
||
export const GitlyUknownError = class extends GitlyAbstractError { | ||
static type = GitlyErrorType.Unknown | ||
} | ||
|
||
export const GitlyFetchError = class extends GitlyAbstractError { | ||
static type = GitlyErrorType.Fetch | ||
} | ||
|
||
export const GitlyExtractError = class extends GitlyAbstractError { | ||
static type = GitlyErrorType.Extract | ||
} | ||
|
||
export const GitlyDownloadError = class extends GitlyAbstractError { | ||
static type = GitlyErrorType.Download | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters