-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9d124e7
commit d61ea86
Showing
19 changed files
with
397 additions
and
11 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,2 @@ | ||
service_name: travis-pro | ||
repo_token: FbUTUBuan5AgSUOEJU4YoEAlUyhW4orTR |
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,27 @@ | ||
sudo: required | ||
language: node_js | ||
cache: | ||
yarn: true | ||
notifications: | ||
email: false | ||
addons: | ||
apt: | ||
sources: | ||
- ubuntu-toolchain-r-test | ||
packages: | ||
- g++-4.8 | ||
branches: | ||
only: | ||
- master | ||
env: | ||
global: | ||
- CXX=g++-4.8 | ||
- NODE_ENV="test" | ||
node_js: | ||
- "8" | ||
- "10" | ||
- "12" | ||
- "14" | ||
script: | ||
- yarn test | ||
- cat ./coverage/lcov.info | ./node_modules/.bin/coveralls --verbose |
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,7 @@ | ||
Copyright 2021 ccharlieli@live.com | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
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,62 @@ | ||
# alicloud-apsara | ||
|
||
This is a library for integrating with AliCloud live-streaming platform - [Apsara](https://www.alibabacloud.com/help/doc-detail/29951.htm?spm=a2c63.p38356.b99.2.2c0d56a2C7EHql). | ||
|
||
Integration progress: | ||
- [x] [get domains](https://www.alibabacloud.com/help/doc-detail/88332.htm?spm=a2c63.p38356.b99.143.17872c80zDTOBs) | ||
- [ ] TBD | ||
|
||
### How to use | ||
```js | ||
import { Apsara, ApsaraDomainsData } from 'alicloud-apsara' | ||
|
||
const apsara = new Apsara({ | ||
accessKeyId: 'keyId', | ||
accessKeySecret: 'keySecret' | ||
}) | ||
|
||
const domainsData: ApsaraDomainsData = await apsara.getDomains() | ||
// Domains: { | ||
// PageData: [ | ||
// { | ||
// Description: '', | ||
// LiveDomainStatus: 'online', | ||
// DomainName: 'live-ingest.upstra-china.cc', | ||
// LiveDomainType: 'liveEdge', | ||
// RegionName: 'ap-southeast-1', | ||
// GmtModified: '2020-09-14T14:31:23Z', | ||
// GmtCreated: '2020-09-14T14:27:06Z', | ||
// Cname: 'live-ingest.upstra-china.cc.w.alikunlun.com' | ||
// } | ||
// ] | ||
// }, | ||
// TotalCount: 8, | ||
// RequestId: '0500D05A-505D-49B1-B989-BDC9FDAB9BAD', | ||
// PageSize: 20, | ||
// PageNumber: 1 | ||
``` | ||
|
||
### Specs | ||
|
||
#### Apsara(options [,logger]) | ||
- **options**, required | ||
- `accessKeyId`: string, required | ||
- `accessKeySecret`: string, required | ||
- `baseUrl`?: string, by default `'https://live.aliyuncs.com'` | ||
- `timeout`?: number, by default `3000` | ||
- `version`?: string, by default `'2016-11-01'` | ||
- `signatureMethod`?: string, by default `'HMAC-SHA1'` | ||
- `signatureVersion`?: string, by default `'1.0'` | ||
- `format`?: string, by default `'json'` | ||
- **logger**, optional, WinstonLogger/BunyanLogger/Console | ||
|
||
#### [Apsara.getDomains()](https://www.alibabacloud.com/help/doc-detail/88332.htm?spm=a2c63.p38356.b99.143.17872c80zDTOBs) | ||
|
||
### How to test | ||
|
||
```sh | ||
yarn | ||
yarn test | ||
``` | ||
|
||
### [MIT License](https://github.com/CCharlieLi/alicloud-apsara/blob/master/License) |
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
Empty file.
Empty 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,118 @@ | ||
import axios, { AxiosInstance, AxiosResponse } from 'axios' | ||
import { createHmac } from 'crypto' | ||
import createHttpError from 'http-errors' | ||
import { escape } from 'querystring' | ||
import { v4 as uuidv4 } from 'uuid' | ||
|
||
import { API_VERSION, BASE_URL, FORMAT, SIGNATURE_METHOD, SIGNATURE_VERSION, TIMEOUT } from './constants' | ||
import { ApsaraDomainsData } from './data/ApsaraDomainsData' | ||
import { ApsaraErrorData } from './data/ApsaraErrorData' | ||
import { AsparaOptions } from './data/ApsaraOptions' | ||
import { ApsaraParams } from './data/ApsaraParams' | ||
import { Logger } from './data/Logger' | ||
|
||
export class Apsara { | ||
private options: AsparaOptions | ||
private axios: AxiosInstance | ||
private logger?: Logger | ||
|
||
constructor(options: AsparaOptions, logger?: Logger) { | ||
this.options = { | ||
version: API_VERSION, | ||
signatureMethod: SIGNATURE_METHOD, | ||
signatureVersion: SIGNATURE_VERSION, | ||
format: FORMAT, | ||
...options | ||
} | ||
this.logger = logger | ||
this.axios = axios.create({ | ||
baseURL: options.baseUrl || BASE_URL, | ||
timeout: options.timeout || TIMEOUT | ||
}) | ||
} | ||
|
||
/** | ||
* Fetch all domains from current Apsara account | ||
* See https://www.alibabacloud.com/help/zh/doc-detail/88332.htm?spm=a2c63.p38356.b99.153.6e601545HUfO2U | ||
*/ | ||
async getDomains(): Promise<ApsaraDomainsData> { | ||
this.logger?.info(`Get domains of current Apsara account`) | ||
return this.request<ApsaraDomainsData>({ Action: 'DescribeLiveUserDomains' }) | ||
} | ||
|
||
// Private | ||
|
||
/** | ||
* Make HTTP request to Apsara open API | ||
* @param {object} params | ||
*/ | ||
private async request<T>(params: Record<string, string>): Promise<T> { | ||
this.logger?.info(`Send request to Apsara with payload ${JSON.stringify(params)}`) | ||
|
||
// create payload | ||
const method = 'get' | ||
const commonParams: ApsaraParams = { | ||
Format: this.options.format as string, | ||
Version: this.options.version as string, | ||
SignatureMethod: this.options.signatureMethod as string, | ||
SignatureVersion: this.options.signatureVersion as string, | ||
AccessKeyId: this.options.accessKeyId, | ||
SignatureNonce: uuidv4(), | ||
Timestamp: new Date().toISOString() | ||
} | ||
const signature: string = this.generateSignature({ ...commonParams, ...params }, method) | ||
|
||
// init request | ||
let response: AxiosResponse<T> | ||
try { | ||
response = await this.axios({ | ||
method, | ||
params: { | ||
...commonParams, | ||
...params, | ||
Signature: signature | ||
} | ||
}) | ||
|
||
const { status, data } = response | ||
this.logger?.info(`Request to Apsara succeed with status ${status} and data ${JSON.stringify(data)}`) | ||
return data as T | ||
} catch (err) { | ||
this.logger?.error(`Request to Apsara failed with response ${JSON.stringify(err.message)}`) | ||
|
||
// Apsara business error | ||
if (err?.response?.data?.RequestId) { | ||
const data: ApsaraErrorData = err.response.data | ||
const status = err.response.status | ||
throw createHttpError(status, data.Message, data) | ||
} | ||
|
||
// Service error | ||
throw createHttpError(err) | ||
} | ||
} | ||
|
||
/** | ||
* Generate signature for Apsara open API | ||
* See https://www.alibabacloud.com/help/zh/doc-detail/50286.htm?spm=a2c63.p38356.b99.145.2b434281pZhqqP | ||
* @param {object} payload | ||
* @param {string} method | ||
*/ | ||
private generateSignature(payload: any, method: string): string { | ||
// sort params | ||
const paramsStr = Object.keys(payload) | ||
.map(key => (key === 'Timestamp' ? `${key}=${encodeURIComponent(payload[key])}` : `${key}=${payload[key]}`)) | ||
.sort() | ||
.join('&') | ||
|
||
// sign | ||
return createHmac('sha1', `${this.options.accessKeySecret}&`) | ||
.update( | ||
`${method.toUpperCase()}&${encodeURIComponent('/')}&${escape(paramsStr) | ||
.replace('+', '%20') | ||
.replace('*', '%2A') | ||
.replace('%7E', '~')}` | ||
) | ||
.digest('base64') | ||
} | ||
} |
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,8 @@ | ||
// https://www.alibabacloud.com/help/zh/doc-detail/50284.htm?spm=a2c63.p38356.b99.144.1cc659c6QMTj5N | ||
export const BASE_URL = 'https://live.aliyuncs.com' | ||
export const TIMEOUT = 3000 // 3s | ||
export const API_VERSION = '2016-11-01' | ||
export const SIGNATURE_METHOD = 'HMAC-SHA1' | ||
export const SIGNATURE_VERSION = '1.0' | ||
export const FORMAT = 'json' | ||
// ResourceOwnerAccount? |
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,22 @@ | ||
export interface ApsaraDomainsData { | ||
Domains: PageData | ||
TotalCount: number | ||
RequestId: string | ||
PageSize: number | ||
PageNumber: number | ||
} | ||
|
||
interface PageData { | ||
PageData: Domain[] | ||
} | ||
|
||
interface Domain { | ||
Description: string | ||
LiveDomainStatus: string | ||
DomainName: string | ||
LiveDomainType: string | ||
RegionName: string | ||
GmtModified: string | ||
GmtCreated: string | ||
Cname: string | ||
} |
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,7 @@ | ||
export interface ApsaraErrorData { | ||
RequestId: string | ||
Message: string | ||
Recommend: string | ||
HostId: string | ||
Code: string | ||
} |
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,10 @@ | ||
export interface AsparaOptions { | ||
baseUrl?: string | ||
timeout?: number | ||
version?: string | ||
signatureMethod?: string | ||
signatureVersion?: string | ||
accessKeyId: string | ||
accessKeySecret: string | ||
format?: string | ||
} |
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,10 @@ | ||
export interface ApsaraParams { | ||
Format: string | ||
Version: string | ||
SignatureMethod: string | ||
SignatureVersion: string | ||
AccessKeyId: string | ||
Signature?: string | ||
SignatureNonce: string | ||
Timestamp: string | ||
} |
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,5 @@ | ||
export declare class Logger { | ||
info(...meta: any[]): void | ||
warn(...meta: any[]): void | ||
error(...meta: any[]): void | ||
} |
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,5 @@ | ||
export * from './ApsaraDomainsData' | ||
export * from './ApsaraErrorData' | ||
export * from './ApsaraOptions' | ||
export * from './ApsaraParams' | ||
export * from './Logger' |
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,3 @@ | ||
export * from './Apsara' | ||
export * from './constants' | ||
export * from './data' |
Oops, something went wrong.