Skip to content

Commit

Permalink
📦 NEW: Try to use ctx.httpclient first (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 committed Dec 4, 2022
1 parent d0b2e15 commit a9ad395
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 7 deletions.
2 changes: 1 addition & 1 deletion SECURITY.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

## Reporting a Vulnerability

Please report security issues to <email>
Please report security issues to <fengmk2+security@gmail.com>
8 changes: 5 additions & 3 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,10 @@ async function request(params) {
const reqParams = createRequest.call(this, params);
let result;
let reqErr;
// try ctx.httpclient first
const urllib = reqParams.params.ctx?.httpclient ?? reqParams.params.ctx?.urllib ?? this.urllib;
try {
result = await this.urllib.request(reqParams.url, reqParams.params);
result = await urllib.request(reqParams.url, reqParams.params);
debug('response %s %s, got %s, headers: %j', params.method, reqParams.url, result.status, result.headers);
} catch (err) {
reqErr = err;
Expand Down Expand Up @@ -306,11 +308,11 @@ proto.requestError = async function requestError(result) {
err.status = 412;
err.code = 'PreconditionFailed';
} else {
err = new Error(`Unknow error, status: ${result.status}`);
err = new Error(`Unknow error, status: ${result.status}, raw error: ${result}`);
err.name = 'UnknownError';
err.status = result.status;
}
err.requestId = result.headers['x-oss-request-id'];
err.requestId = result.headers?.['x-oss-request-id'];
err.host = '';
}
} else {
Expand Down
1 change: 0 additions & 1 deletion lib/image.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ module.exports = function(OssClient) {
options.endpoint = options.imageHost;
this.ossClient = new OssClient(options);
this.ossClient.options.imageHost = options.imageHost;
// this.ossClient._objectRequestParams = objectRequestParams;
}

/**
Expand Down
4 changes: 3 additions & 1 deletion lib/sts.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,9 @@ proto.assumeRole = async function assumeRole(role, policy, expiration, session,
ctx: options && options.ctx,
};

const result = await this.urllib.request(reqUrl, reqParams);
// try ctx.httpclient first
const urllib = reqParams.ctx?.httpclient ?? reqParams.ctx?.urllib ?? this.urllib;
const result = await urllib.request(reqUrl, reqParams);
debug(
'response %s %s, got %s, headers: %j',
reqParams.method, reqUrl, result.status, result.headers
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"scripts": {
"lint": "eslint lib test",
"test": "egg-bin test --full-trace --parallel",
"test-local": "egg-bin test --full-trace",
"tsd": "tsd",
"cov": "egg-bin cov --full-trace --parallel",
"ci": "npm run lint && npm run tsd && npm run cov"
Expand Down Expand Up @@ -51,7 +52,7 @@
},
"devDependencies": {
"@eggjs/tsconfig": "^1.1.0",
"@types/node": "^18.11.6",
"@types/node": "^14.18.34",
"egg-bin": "^5.3.1",
"eslint": "^8.25.0",
"eslint-config-egg": "^12.1.0",
Expand Down
30 changes: 30 additions & 0 deletions test/object.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,36 @@ describe('test/object.test.js', () => {
assert.equal(object.name, name);
});

it('should with options.ctx', async () => {
const name = `${prefix}oss-client/oss/put-localfile-options-ctx.js`;
let ctx = {
httpclient: {},
};
await assert.rejects(async () => {
await store.put(name, __filename, { ctx });
}, err => {
assert(err.message.includes('raw error: TypeError: urllib.request is not a function'));
return true;
});
ctx = {
httpclient: urllib,
};
let object = await store.put(name, __filename, { ctx });
assert.equal(typeof object.res.headers['x-oss-request-id'], 'string');
assert.equal(typeof object.res.rt, 'number');
assert.equal(object.res.size, 0);
assert.equal(object.name, name);

ctx = {
urllib,
};
object = await store.put(name, __filename, { ctx });
assert.equal(typeof object.res.headers['x-oss-request-id'], 'string');
assert.equal(typeof object.res.rt, 'number');
assert.equal(object.res.size, 0);
assert.equal(object.name, name);
});

it('should add object with content buffer', async () => {
const name = `${prefix}oss-client/oss/put-buffer`;
const object = await store.put(`/${name}`, Buffer.from('foo content'));
Expand Down

0 comments on commit a9ad395

Please sign in to comment.