-
Notifications
You must be signed in to change notification settings - Fork 1
/
adapter.ts
280 lines (240 loc) · 8.24 KB
/
adapter.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
import { Region } from './region';
import { URL } from 'url';
import { Readable } from 'stream';
import { OutgoingHttpHeaders } from 'http';
import { NatureLanguage } from './uplog';
import { KodoHttpClient } from './kodo-http-client';
export abstract class Adapter {
abstract readonly client: KodoHttpClient;
abstract storageClasses: StorageClass[];
abstract enter<T>(sdkApiName: string, f: (scope: Adapter) => Promise<T>, sdkUplogOption?: EnterUplogOption): Promise<T>;
abstract createBucket(region: string, bucket: string): Promise<void>;
abstract deleteBucket(region: string, bucket: string): Promise<void>;
abstract getBucketLocation(bucket: string): Promise<string>;
abstract listBuckets(): Promise<Bucket[]>;
abstract listDomains(region: string, bucket: string): Promise<Domain[]>;
abstract updateBucketRemark(bucket: string, remark: string): Promise<void>;
abstract isExists(region: string, object: StorageObject): Promise<boolean>;
abstract getFrozenInfo(region: string, object: StorageObject): Promise<FrozenInfo>;
abstract restoreObject(region: string, object: StorageObject, days: number): Promise<void>;
abstract restoreObjects(s3RegionId: string, bucket: string, keys: string[], days: number, callback?: BatchCallback): Promise<PartialObjectError[]>;
abstract setObjectStorageClass(region: string, object: StorageObject, storageClass: StorageClass['kodoName']): Promise<void>;
abstract setObjectsStorageClass(s3RegionId: string, bucket: string, keys: string[], storageClass: StorageClass['kodoName'], callback?: BatchCallback): Promise<PartialObjectError[]>;
abstract moveObject(region: string, transferObject: TransferObject): Promise<void>;
abstract moveObjects(region: string, transferObjects: TransferObject[], callback?: BatchCallback): Promise<PartialObjectError[]>;
abstract copyObject(region: string, transferObject: TransferObject): Promise<void>;
abstract copyObjects(region: string, transferObjects: TransferObject[], callback?: BatchCallback): Promise<PartialObjectError[]>;
abstract deleteObject(region: string, object: StorageObject): Promise<void>;
abstract deleteObjects(region: string, bucket: string, keys: string[], callback?: BatchCallback): Promise<PartialObjectError[]>;
abstract getObjectInfo(region: string, object: StorageObject): Promise<ObjectInfo>;
abstract getObjectHeader(region: string, object: StorageObject, domain?: Domain): Promise<ObjectHeader>;
abstract getObject(
region: string,
object: StorageObject,
domain?: Domain,
style?: UrlStyle,
): Promise<ObjectGetResult>;
abstract getObjectURL(
region: string,
object: StorageObject,
domain?: Domain,
deadline?: Date,
style?: UrlStyle,
): Promise<URL>;
abstract getObjectStream(s3RegionId: string, object: StorageObject, domain?: Domain, option?: GetObjectStreamOption): Promise<Readable>;
abstract putObject(
region: string,
object: StorageObject,
data: Buffer | Readable | (() => Buffer | Readable),
originalFileName: string,
header?: SetObjectHeader,
option?: PutObjectOption,
): Promise<void>;
abstract createMultipartUpload(
region: string,
object: StorageObject,
originalFileName: string,
header?: SetObjectHeader,
abortSignal?: AbortSignal,
accelerateUploading?: boolean,
): Promise<InitPartsOutput>;
abstract uploadPart(
region: string,
object: StorageObject,
uploadId: string,
partNumber: number,
data: Buffer | Readable | (() => Buffer | Readable),
option?: PutObjectOption,
): Promise<UploadPartOutput>;
abstract completeMultipartUpload(
region: string,
object: StorageObject,
uploadId: string,
parts: Part[],
originalFileName: string,
header?: SetObjectHeader,
abortSignal?: AbortSignal,
accelerateUploading?: boolean,
): Promise<void>;
abstract listObjects(region: string, bucket: string, prefix: string, option?: ListObjectsOption): Promise<ListedObjects>;
abstract clearCache(): void;
}
export type BatchCallback = (index: number, error?: Error) => any;
export type ProgressCallback = (uploaded: number, total: number) => any;
export interface SdkUplogOption {
language: NatureLanguage,
targetBucket?: string,
targetKey?: string,
}
export type EnterUplogOption = Omit<SdkUplogOption, 'language'>;
export interface ListObjectsOption {
delimiter?: string;
minKeys?: number;
maxKeys?: number;
nextContinuationToken?: string;
}
export interface ListedObjects {
objects: ObjectInfo[];
commonPrefixes?: StorageObject[];
nextContinuationToken?: string,
}
export interface AdapterOption {
accessKey: string;
secretKey: string;
sessionToken?: string;
regions: Region[];
ucUrl?: string;
appendedUserAgent?: string;
appName: string;
appVersion: string;
appNatureLanguage: NatureLanguage,
uplogBufferSize?: number;
requestCallback?: (request: RequestInfo) => void;
responseCallback?: (response: ResponseInfo) => void;
}
export interface RequestInfo {
url: string;
method: string;
headers: OutgoingHttpHeaders;
data?: any;
}
export interface ResponseInfo {
request: RequestInfo;
interval: number;
statusCode?: number;
headers?: OutgoingHttpHeaders;
data?: any;
error?: any;
}
export interface Bucket {
id: string;
name: string;
createDate: Date;
regionId?: string;
grantedPermission?: 'readonly' | 'readwrite';
remark?: string;
}
export interface BucketDetails {
perm: number,
private: number,
protected: number,
// still have others fields but not list in here
}
export interface Domain {
name: string;
protocol: string;
private: boolean;
protected: boolean;
type: 'cdn' | 'origin' | 'others';
apiScope: 'kodo' | 's3';
}
export type DomainWithoutShouldSign = Omit<Domain, 'private' | 'protected'>;
export interface FrozenInfo {
status: FrozenStatus;
expiryDate?: Date;
}
export type StorageClass = {
fileType: number,
kodoName: string,
s3Name: string,
};
export const DEFAULT_STORAGE_CLASS: StorageClass = {
fileType: 0,
kodoName: 'Standard',
s3Name: 'STANDARD',
};
export type FrozenStatus = 'Normal' | 'Frozen' | 'Unfreezing' | 'Unfrozen';
export interface TransferObject {
from: StorageObject;
to: StorageObject;
}
export interface StorageObject {
bucket: string;
key: string;
storageClassName?: StorageClass['kodoName'];
}
export interface PartialObjectError extends StorageObject {
error?: Error;
}
export interface ObjectGetResult {
data: Buffer;
header: ObjectHeader;
}
export interface SetObjectHeader {
metadata?: { [key: string]: string; };
contentType?: string;
}
export interface ObjectHeader extends SetObjectHeader {
size: number;
lastModified: Date;
metadata: { [key: string]: string; };
objectLockMode?: string;
objectLockRetainUntilDate?: Date;
}
export interface ObjectInfo extends StorageObject {
size: number;
lastModified: Date;
storageClass: StorageClass['kodoName'];
}
export interface InitPartsOutput {
uploadId: string
}
export interface UploadPartOutput {
etag: string
}
export interface Part {
partNumber: number;
etag: string;
}
export interface FileStreamSetting {
path: string,
start: number,
end: number,
}
export interface PutObjectOption {
/**
* control to abort upload request
*/
abortSignal?: AbortSignal;
/**
* For both kodo and s3, they need to calculate md5 when uploading.
*
* Specially s3, it not supports non-file stream when use http protocol.
* use this options to hack it like a file stream.
* ref: https://github.com/aws/aws-sdk-js/blob/v2.1015.0/lib/util.js#L730-L755
*/
fileStreamSetting?: FileStreamSetting;
progressCallback?: ProgressCallback;
accelerateUploading?: boolean;
}
export enum UrlStyle {
Path = 'path',
VirtualHost = 'virtualHost',
BucketEndpoint = 'bucketEndpoint',
}
export interface GetObjectStreamOption {
rangeStart?: number;
rangeEnd?: number;
abortSignal?: AbortSignal;
urlStyle?: UrlStyle,
}