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

feat: refactor with typescript #12

Merged
merged 22 commits into from
Oct 5, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
impl Object.head()
fengmk2 committed Oct 5, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit aa93afbed34d85578ebae66ce51294ca78b4232b
23 changes: 0 additions & 23 deletions lib/common/object/getObjectMeta.js

This file was deleted.

15 changes: 8 additions & 7 deletions src/OSSBaseClient.ts
Original file line number Diff line number Diff line change
@@ -271,14 +271,15 @@ export abstract class OSSBaseClient {
let err: OSSClientError;
let requestId = result.headers['x-oss-request-id'] as string ?? '';
let hostId = '';
const status = result.status;
if (!result.data || !result.data.length) {
// HEAD not exists resource
if (result.status === 404) {
err = new OSSClientError('NoSuchKey', 'Object not exists', requestId, hostId);
} else if (result.status === 412) {
err = new OSSClientError('PreconditionFailed', 'Pre condition failed', requestId, hostId);
if (status === 404) {
err = new OSSClientError(status, 'NoSuchKey', 'Object not exists', requestId, hostId);
} else if (status === 412) {
err = new OSSClientError(status, 'PreconditionFailed', 'Pre condition failed', requestId, hostId);
} else {
err = new OSSClientError('Unknown', `Unknown error, status=${result.status}, raw error=${result}`,
err = new OSSClientError(status, 'Unknown', `Unknown error, status=${status}, raw error=${result}`,
requestId, hostId);
}
} else {
@@ -289,7 +290,7 @@ export abstract class OSSBaseClient {
try {
info = await this.#xml2json(xml);
} catch (e: any) {
err = new OSSClientError('PreconditionFailed', `${e.message} (raw xml=${JSON.stringify(xml)})`, requestId, hostId);
err = new OSSClientError(status, 'PreconditionFailed', `${e.message} (raw xml=${JSON.stringify(xml)})`, requestId, hostId);
return err;
}

@@ -303,7 +304,7 @@ export abstract class OSSBaseClient {
if (info?.HostId) {
hostId = info.HostId;
}
err = new OSSClientError(info?.Code ?? 'Unknown', message, requestId, hostId);
err = new OSSClientError(status, info?.Code ?? 'Unknown', message, requestId, hostId);
}

debug('generate error %o', err);
68 changes: 68 additions & 0 deletions src/OSSObject.ts
Original file line number Diff line number Diff line change
@@ -30,6 +30,8 @@ import type {
GetObjectResult,
// ObjectCallback,
SignatureUrlOptions,
HeadObjectOptions,
HeadObjectResult,
} from 'oss-interface';
import {
OSSBaseClientInitOptions,
@@ -447,6 +449,65 @@ export class OSSObject extends OSSBaseClient {
} satisfies DeleteMultipleObjectResponse;
}

/**
* HeadObject
* @see https://help.aliyun.com/zh/oss/developer-reference/headobject
*/
async head(name: string, options?: HeadObjectOptions): Promise<HeadObjectResult> {
options = options ?? {};
if (options.subres && !options.subResource) {
options.subResource = options.subres;
}
if (options.versionId) {
if (!options.subResource) {
options.subResource = {};
}
options.subResource.versionId = options.versionId;
}
const params = this.#objectRequestParams('HEAD', name, options);
params.successStatuses = [ 200, 304 ];
const { res } = await this.request(params);
const meta: UserMeta = {};
const result = {
meta,
res,
status: res.status,
} satisfies HeadObjectResult;
for (const k in res.headers) {
if (k.startsWith('x-oss-meta-')) {
const key = k.substring(11);
meta[key] = res.headers[k] as string;
}
}
return result;
}

/**
* GetObjectMeta
* @see https://help.aliyun.com/zh/oss/developer-reference/getobjectmeta
*/
async getObjectMeta(name: string, options?: HeadObjectOptions) {
options = options ?? {};
name = this.#objectName(name);
if (options.subres && !options.subResource) {
options.subResource = options.subres;
}
if (!options.subResource) {
options.subResource = {};
}
if (options.versionId) {
options.subResource.versionId = options.versionId;
}
options.subResource.objectMeta = '';
const params = this.#objectRequestParams('HEAD', name, options);
params.successStatuses = [ 200 ];
const { res } = await this.request(params);
return {
status: res.status,
res,
};
}

/**
* signatureUrl URL签名
* @see https://help.aliyun.com/zh/oss/developer-reference/signed-urls
@@ -476,6 +537,9 @@ export class OSSObject extends OSSBaseClient {
return url;
}

async asyncSignatureUrl(name: string, options?: SignatureUrlOptions) {
return this.signatureUrl(name, options);
}

/** protected methods */

@@ -487,6 +551,10 @@ export class OSSObject extends OSSBaseClient {

async #sendPutRequest(name: string, options: PutObjectOptions, contentOrStream: Buffer | Readable) {
options.headers = options.headers ?? {};
if (options.headers['Content-Type'] && !options.headers['content-type']) {
options.headers['content-type'] = options.headers['Content-Type'] as string;
delete options.headers['Content-Type'];
}
name = this.#objectName(name);
this.#convertMetaToHeaders(options.meta, options.headers);
// don't override exists headers
4 changes: 3 additions & 1 deletion src/error/OSSClientError.ts
Original file line number Diff line number Diff line change
@@ -4,11 +4,13 @@ const RESPONSE_HOST_KEY = 'response-host';

export class OSSClientError extends Error {
code: string;
status: number;
requestId?: string;
hostId?: string;

constructor(code: string, message: string, requestId?: string, hostId?: string) {
constructor(status: number, code: string, message: string, requestId?: string, hostId?: string) {
super(`[${REQUEST_ID_KEY}=${requestId}, ${RESPONSE_CODE_KEY}=${code}, ${RESPONSE_HOST_KEY}=${hostId}] ${message}`);
this.status = status;
this.code = code;
this.name = 'OSSClientError';
this.requestId = requestId;
3 changes: 3 additions & 0 deletions src/util/sign.ts
Original file line number Diff line number Diff line change
@@ -112,6 +112,9 @@ export function signatureForURL(accessKeySecret: string, options: SignatureUrlOp
if (options['Content-MD5'] && !options['content-md5']) {
options['content-md5'] = options['Content-MD5'];
}
if (options['Content-Md5'] && !options['content-md5']) {
options['content-md5'] = options['Content-Md5'];
}
if (options['content-md5']) {
headers['content-md5'] = options['content-md5'];
}
959 changes: 784 additions & 175 deletions test/OSSObject.test.ts

Large diffs are not rendered by default.

637 changes: 0 additions & 637 deletions test/object.test.js

Large diffs are not rendered by default.