Skip to content

Commit

Permalink
修复从 token 解析 bucket 的 bug & 完善下类型 & fix issue (#456)
Browse files Browse the repository at this point in the history
* 修复从 token 解析 bucket 的 bug

* 完善observer类型

* 增加 base64 的导出

* 增加对 ak 的处理

* 添加注释
  • Loading branch information
winddies authored Jun 30, 2020
1 parent 1b81947 commit 54557c0
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 13 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "qiniu-js",
"jsName": "qiniu",
"version": "3.0.2",
"version": "3.0.3",
"private": false,
"description": "Javascript SDK for Qiniu Resource (Cloud) Storage AP",
"main": "lib/index.js",
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ export {
getHeadersForChunkUpload
} from './utils'

export { urlSafeBase64Encode, urlSafeBase64Decode } from './base64'

export { CompressResult } from './compress'

export { deleteUploadedChunks, getUploadUrl } from './api'
Expand Down
29 changes: 22 additions & 7 deletions src/observable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,20 @@ export interface NextObserver<T, E, C> {
complete?: (res: C) => void
}

export interface ErrorObserver<T, E, C> {
next?: (value: T) => void
error: (err: E) => void
complete?: (res: C) => void
}

export interface CompletionObserver<T, E, C> {
next?: (value: T) => void
error?: (err: E) => void
complete: (res: C) => void
}

export type PartialObserver<T, E, C> = NextObserver<T, E, C> | ErrorObserver<T, E, C> | CompletionObserver<T, E, C>

export interface IUnsubscribable {
/** 取消 observer 的订阅 */
unsubscribe(): void
Expand All @@ -27,10 +41,11 @@ export interface ISubscriptionLike extends IUnsubscribable {
export type TeardownLogic = () => void

export interface ISubscribable<T, E, C> {
subscribe(observer?: NextObserver<T, E, C>): IUnsubscribable
subscribe(next: null | undefined, error: null | undefined, complete: (res: C) => void): IUnsubscribable
subscribe(next: null | undefined, error: (error: E) => void, complete?: (res: C) => void): IUnsubscribable
subscribe(next: (value: T) => void, error: null | undefined, complete: (res: C) => void): IUnsubscribable
subscribe(
observer?: PartialObserver<T, E, C> | ((value: T) => void),
error?: (error: any) => void,
complete?: () => void
): IUnsubscribable
}

/** 表示可清理的资源,比如 Observable 的执行 */
Expand Down Expand Up @@ -68,7 +83,7 @@ export class Subscriber<T, E, C> extends Subscription implements IObserver<T, E,
protected destination: Partial<IObserver<T, E, C>>

constructor(
observerOrNext?: NextObserver<T, E, C> | ((value: T) => void) | null,
observerOrNext?: PartialObserver<T, E, C> | ((value: T) => void) | null,
error?: ((err: E) => void) | null,
complete?: ((res: C) => void) | null
) {
Expand Down Expand Up @@ -120,12 +135,12 @@ export class Observable<T, E, C> implements ISubscribable<T, E, C> {

constructor(private _subscribe: (subscriber: Subscriber<T, E, C>) => TeardownLogic) {}

subscribe(observer: NextObserver<T, E, C>): Subscription
subscribe(observer: PartialObserver<T, E, C>): Subscription
subscribe(next: null | undefined, error: null | undefined, complete: (res: C) => void): Subscription
subscribe(next: null | undefined, error: (error: E) => void, complete?: (res: C) => void): Subscription
subscribe(next: (value: T) => void, error: null | undefined, complete: (res: C) => void): Subscription
subscribe(
observerOrNext?: NextObserver<T, E, C> | ((value: T) => void) | null,
observerOrNext?: PartialObserver<T, E, C> | ((value: T) => void) | null,
error?: ((err: E) => void) | null,
complete?: ((res: C) => void) | null
): Subscription {
Expand Down
7 changes: 5 additions & 2 deletions src/upload/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,11 @@ export default abstract class Base {
this.onData = handlers.onData
this.onError = handlers.onError
this.onComplete = handlers.onComplete

this.bucket = utils.getPutPolicy(this.token).bucket
try {
this.bucket = utils.getPutPolicy(this.token).bucket
} catch (e) {
this.onError(e)
}
}

public async putFile(): Promise<utils.ResponseSuccess<UploadCompleteData>> {
Expand Down
5 changes: 3 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,8 +272,9 @@ interface PutPolicy {

export function getPutPolicy(token: string) {
const segments = token.split(':')
const ak = segments[0]
const putPolicy: PutPolicy = JSON.parse(urlSafeBase64Decode(segments[2]))
// token 构造的差异参考:https://github.com/qbox/product/blob/master/kodo/auths/UpToken.md#admin-uptoken-authorization
const ak = segments.length > 3 ? segments[1] : segments[0]
const putPolicy: PutPolicy = JSON.parse(urlSafeBase64Decode(segments[segments.length - 1]))

return {
ak,
Expand Down

0 comments on commit 54557c0

Please sign in to comment.