Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

修复从 token 解析 bucket 的 bug & 完善下类型 & fix issue #456

Merged
merged 5 commits into from
Jun 30, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
@@ -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
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
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
lzfee0227 marked this conversation as resolved.
Show resolved Hide resolved
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
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)
}
Comment on lines +131 to +133
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个异常应该不重新 throw 出去吗? @nighca

}

public async putFile(): Promise<utils.ResponseSuccess<UploadCompleteData>> {
Expand Down
2 changes: 1 addition & 1 deletion src/utils.ts
Expand Up @@ -273,7 +273,7 @@ interface PutPolicy {
export function getPutPolicy(token: string) {
const segments = token.split(':')
const ak = segments[0]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

check

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

要不要写一下文档链接或格式注释

const putPolicy: PutPolicy = JSON.parse(urlSafeBase64Decode(segments[2]))
const putPolicy: PutPolicy = JSON.parse(urlSafeBase64Decode(segments[segments.length - 1]))

return {
ak,
Expand Down