Skip to content

Commit 54557c0

Browse files
author
winddies
authored
修复从 token 解析 bucket 的 bug & 完善下类型 & fix issue (#456)
* 修复从 token 解析 bucket 的 bug * 完善observer类型 * 增加 base64 的导出 * 增加对 ak 的处理 * 添加注释
1 parent 1b81947 commit 54557c0

File tree

6 files changed

+34
-13
lines changed

6 files changed

+34
-13
lines changed

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "qiniu-js",
33
"jsName": "qiniu",
4-
"version": "3.0.2",
4+
"version": "3.0.3",
55
"private": false,
66
"description": "Javascript SDK for Qiniu Resource (Cloud) Storage AP",
77
"main": "lib/index.js",

src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ export {
4747
getHeadersForChunkUpload
4848
} from './utils'
4949

50+
export { urlSafeBase64Encode, urlSafeBase64Decode } from './base64'
51+
5052
export { CompressResult } from './compress'
5153

5254
export { deleteUploadedChunks, getUploadUrl } from './api'

src/observable.ts

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,20 @@ export interface NextObserver<T, E, C> {
1414
complete?: (res: C) => void
1515
}
1616

17+
export interface ErrorObserver<T, E, C> {
18+
next?: (value: T) => void
19+
error: (err: E) => void
20+
complete?: (res: C) => void
21+
}
22+
23+
export interface CompletionObserver<T, E, C> {
24+
next?: (value: T) => void
25+
error?: (err: E) => void
26+
complete: (res: C) => void
27+
}
28+
29+
export type PartialObserver<T, E, C> = NextObserver<T, E, C> | ErrorObserver<T, E, C> | CompletionObserver<T, E, C>
30+
1731
export interface IUnsubscribable {
1832
/** 取消 observer 的订阅 */
1933
unsubscribe(): void
@@ -27,10 +41,11 @@ export interface ISubscriptionLike extends IUnsubscribable {
2741
export type TeardownLogic = () => void
2842

2943
export interface ISubscribable<T, E, C> {
30-
subscribe(observer?: NextObserver<T, E, C>): IUnsubscribable
31-
subscribe(next: null | undefined, error: null | undefined, complete: (res: C) => void): IUnsubscribable
32-
subscribe(next: null | undefined, error: (error: E) => void, complete?: (res: C) => void): IUnsubscribable
33-
subscribe(next: (value: T) => void, error: null | undefined, complete: (res: C) => void): IUnsubscribable
44+
subscribe(
45+
observer?: PartialObserver<T, E, C> | ((value: T) => void),
46+
error?: (error: any) => void,
47+
complete?: () => void
48+
): IUnsubscribable
3449
}
3550

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

7085
constructor(
71-
observerOrNext?: NextObserver<T, E, C> | ((value: T) => void) | null,
86+
observerOrNext?: PartialObserver<T, E, C> | ((value: T) => void) | null,
7287
error?: ((err: E) => void) | null,
7388
complete?: ((res: C) => void) | null
7489
) {
@@ -120,12 +135,12 @@ export class Observable<T, E, C> implements ISubscribable<T, E, C> {
120135

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

123-
subscribe(observer: NextObserver<T, E, C>): Subscription
138+
subscribe(observer: PartialObserver<T, E, C>): Subscription
124139
subscribe(next: null | undefined, error: null | undefined, complete: (res: C) => void): Subscription
125140
subscribe(next: null | undefined, error: (error: E) => void, complete?: (res: C) => void): Subscription
126141
subscribe(next: (value: T) => void, error: null | undefined, complete: (res: C) => void): Subscription
127142
subscribe(
128-
observerOrNext?: NextObserver<T, E, C> | ((value: T) => void) | null,
143+
observerOrNext?: PartialObserver<T, E, C> | ((value: T) => void) | null,
129144
error?: ((err: E) => void) | null,
130145
complete?: ((res: C) => void) | null
131146
): Subscription {

src/upload/base.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,11 @@ export default abstract class Base {
126126
this.onData = handlers.onData
127127
this.onError = handlers.onError
128128
this.onComplete = handlers.onComplete
129-
130-
this.bucket = utils.getPutPolicy(this.token).bucket
129+
try {
130+
this.bucket = utils.getPutPolicy(this.token).bucket
131+
} catch (e) {
132+
this.onError(e)
133+
}
131134
}
132135

133136
public async putFile(): Promise<utils.ResponseSuccess<UploadCompleteData>> {

src/utils.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,8 +272,9 @@ interface PutPolicy {
272272

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

278279
return {
279280
ak,

0 commit comments

Comments
 (0)