Skip to content

Commit aa3c3f1

Browse files
committed
wipp
1 parent 709fc54 commit aa3c3f1

File tree

3 files changed

+93
-53
lines changed

3 files changed

+93
-53
lines changed

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,12 @@
2323
"@aws-crypto/sha256-js": "^5.2.0",
2424
"@aws-sdk/client-s3": "^3.705.0",
2525
"@aws-sdk/credential-provider-node": "^3.705.0",
26-
"@aws-sdk/credential-providers": "^3.705.0",
26+
"@aws-sdk/credential-providers": "^3.864.0",
2727
"@aws-sdk/lib-storage": "^3.864.0",
2828
"@aws-sdk/protocol-http": "^3.374.0",
2929
"@aws-sdk/s3-request-presigner": "^3.705.0",
30-
"@aws-sdk/signature-v4": "3.374.0",
30+
"@aws-sdk/signature-v4": "^3.374.0",
31+
"@aws-sdk/types": "^3.862.0",
3132
"@azure/storage-blob": "^12.25.0",
3233
"@hapi/joi": "^17.1.1",
3334
"@smithy/node-http-handler": "^3.0.0",
@@ -45,7 +46,7 @@
4546
"level-mem": "^5.0.1",
4647
"moment": "^2.30.1",
4748
"mongodb": "^6.11.0",
48-
"node-fetch": "^2.6.0",
49+
"node-fetch": "^3.3.2",
4950
"node-forge": "^1.3.1",
5051
"npm-run-all": "^4.1.5",
5152
"prom-client": "^15.1.3",
Lines changed: 46 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,68 @@
11
const nodeFetch = require('node-fetch');
2-
const { SignatureV4 } = require('@smithy/signature-v4');
3-
const { HttpRequest } = require('@smithy/protocol-http');
2+
const { SignatureV4 } = require('@aws-sdk/signature-v4');
3+
const { HttpRequest } = require('@aws-sdk/types');
44
const { Sha256 } = require('@aws-crypto/sha256-js');
5-
// const { defaultProvider } = require('@aws-sdk/credential-provider-node');
5+
const { fromNodeProviderChain } = require('@aws-sdk/credential-providers');
66
const xml2js = require('xml2js');
77

88
const sendRequest = async (method, host, path, body = '', config = null) => {
99
const service = 's3';
10-
11-
// Create HTTP request (AWS SDK v3 equivalent of new AWS.HttpRequest(endpoint))
10+
11+
const region = 'us-east-1';
12+
// Create HttpRequest object (v3 equivalent of AWS.HttpRequest)
13+
1214
const request = new HttpRequest({
13-
protocol: 'http:',
14-
hostname: host,
15+
1516
method: method.toUpperCase(),
17+
protocol: 'http:', // Match v2's http:// URL
18+
hostname: host,
1619
path,
17-
body,
18-
headers: {},
20+
headers: {
21+
Host: host,
22+
'X-Amz-Date': new Date().toISOString().replace(/[:\-]|\.\d{3}/g, ''),
23+
'X-Amz-Content-Sha256': computeSha256(body),
24+
},
25+
body: body || undefined,
1926
});
20-
21-
// Set headers exactly like AWS SDK v2
22-
request.headers.Host = host;
23-
request.headers['X-Amz-Date'] = new Date().toISOString().replace(/[:\-]|\.\d{3}/g, '');
24-
25-
// Calculate SHA256 hash exactly like AWS.util.crypto.sha256(request.body || '', 'hex')
26-
const sha256 = new Sha256();
27-
sha256.update(request.body || '');
28-
const hash = await sha256.digest();
29-
const sha256hash = Buffer.from(hash).toString('hex');
30-
request.headers['X-Amz-Content-SHA256'] = sha256hash;
31-
32-
// Set region exactly like AWS SDK v2
33-
request.region = 'us-east-1';
34-
35-
// Get credentials exactly like AWS SDK v2
36-
const accessKeyId = config?.accessKey || config?.accessKeyId || 'accessKey1';
37-
const secretAccessKey = config?.secretKey || config?.secretAccessKey || 'verySecretKey1';
38-
39-
// Create signer (AWS SDK v3 equivalent of new AWS.Signers.V4(request, service))
27+
28+
let credentials;
29+
const accessKeyId = config?.accessKey || config?.accessKeyId;
30+
const secretAccessKey = config?.secretKey || config?.secretAccessKey;
31+
32+
if (accessKeyId && secretAccessKey) {
33+
34+
credentials = { accessKeyId, secretAccessKey };
35+
36+
} else {
37+
credentials = await fromNodeProviderChain()();
38+
}
39+
// Create signer (v3 equivalent of AWS.Signers.V4)
4040
const signer = new SignatureV4({
41-
credentials: {
42-
accessKeyId,
43-
secretAccessKey,
44-
},
45-
region: request.region,
41+
42+
credentials,
43+
region,
4644
service,
4745
sha256: Sha256,
4846
});
49-
50-
// Sign the request (equivalent of signer.addAuthorization(credentials, new Date()))
47+
48+
// Sign the request
5149
const signedRequest = await signer.sign(request, {
5250
signingDate: new Date(),
5351
});
5452

55-
// Make HTTP request exactly like original
53+
// Prepare node-fetch options
5654
const url = `http://${host}${path}`;
5755
const options = {
5856
method: signedRequest.method,
5957
headers: signedRequest.headers,
6058
};
6159

62-
if (method !== 'GET') {
60+
if (method.toUpperCase() !== 'GET') {
6361
options.body = signedRequest.body;
6462
}
6563

64+
// Send request with node-fetch
65+
6666
const response = await nodeFetch(url, options);
6767
const text = await response.text();
6868
const result = await xml2js.parseStringPromise(text);
@@ -73,6 +73,14 @@ const sendRequest = async (method, host, path, body = '', config = null) => {
7373
return result;
7474
};
7575

76+
function computeSha256(body) {
77+
const sha256 = new Sha256();
78+
if (body) {
79+
sha256.update(body);
80+
}
81+
return sha256.digestSync().toString('hex');
82+
}
83+
7684
module.exports = {
7785
sendRequest,
7886
};

yarn.lock

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@
438438
"@smithy/types" "^4.3.2"
439439
tslib "^2.6.2"
440440

441-
"@aws-sdk/credential-providers@^3.540.0", "@aws-sdk/credential-providers@^3.705.0":
441+
"@aws-sdk/credential-providers@^3.540.0", "@aws-sdk/credential-providers@^3.864.0":
442442
version "3.864.0"
443443
resolved "https://registry.yarnpkg.com/@aws-sdk/credential-providers/-/credential-providers-3.864.0.tgz#80d43a2c684da92aff4317e2b68c3c6488253728"
444444
integrity sha512-k4K7PzvHpdHQLczgWT26Yk6t+VBwZ35jkIQ3dKODvBjfzlYHTX0y+VgemmDWrat1ahKfYb/OAw/gdwmnyxsAsw==
@@ -696,7 +696,7 @@
696696
"@smithy/types" "^4.3.2"
697697
tslib "^2.6.2"
698698

699-
"@aws-sdk/signature-v4@3.374.0":
699+
"@aws-sdk/signature-v4@^3.374.0":
700700
version "3.374.0"
701701
resolved "https://registry.yarnpkg.com/@aws-sdk/signature-v4/-/signature-v4-3.374.0.tgz#bd727f4c392acb81bc667aa4cfceeba608250771"
702702
integrity sha512-2xLJvSdzcZZAg0lsDLUAuSQuihzK0dcxIK7WmfuJeF7DGKJFmp9czQmz5f3qiDz6IDQzvgK1M9vtJSVCslJbyQ==
@@ -717,7 +717,7 @@
717717
"@smithy/types" "^4.3.2"
718718
tslib "^2.6.2"
719719

720-
"@aws-sdk/types@3.862.0":
720+
"@aws-sdk/types@3.862.0", "@aws-sdk/types@^3.862.0":
721721
version "3.862.0"
722722
resolved "https://registry.yarnpkg.com/@aws-sdk/types/-/types-3.862.0.tgz#2f5622e1aa3a5281d4f419f5d2c90f87dd5ff0cf"
723723
integrity sha512-Bei+RL0cDxxV+lW2UezLbCYYNeJm6Nzee0TpW0FfyTRBhH9C1XQh4+x+IClriXvgBnRquTMMYsmJfvx8iyLKrg==
@@ -1803,14 +1803,6 @@
18031803
"@smithy/types" "^1.2.0"
18041804
tslib "^2.5.0"
18051805

1806-
"@smithy/protocol-http@^3.0.0":
1807-
version "3.3.0"
1808-
resolved "https://registry.yarnpkg.com/@smithy/protocol-http/-/protocol-http-3.3.0.tgz#a37df7b4bb4960cdda560ce49acfd64c455e4090"
1809-
integrity sha512-Xy5XK1AFWW2nlY/biWZXu6/krgbaf2dg0q492D8M5qthsnU2H+UgFeZLbM76FnH7s6RO/xhQRkj+T6KBO3JzgQ==
1810-
dependencies:
1811-
"@smithy/types" "^2.12.0"
1812-
tslib "^2.6.2"
1813-
18141806
"@smithy/protocol-http@^4.1.8":
18151807
version "4.1.8"
18161808
resolved "https://registry.yarnpkg.com/@smithy/protocol-http/-/protocol-http-4.1.8.tgz#0461758671335f65e8ff3fc0885ab7ed253819c9"
@@ -3367,6 +3359,11 @@ dashdash@^1.12.0:
33673359
dependencies:
33683360
assert-plus "^1.0.0"
33693361

3362+
data-uri-to-buffer@^4.0.0:
3363+
version "4.0.1"
3364+
resolved "https://registry.yarnpkg.com/data-uri-to-buffer/-/data-uri-to-buffer-4.0.1.tgz#d8feb2b2881e6a4f58c2e08acfd0e2834e26222e"
3365+
integrity sha512-0R9ikRb668HB7QDxT1vkpuUBtqc53YyAwMwGeUFKRojY/NWKvdZ+9UYtRfGmhqNbRkTSVpMbmyhXipFFv2cb/A==
3366+
33703367
data-view-buffer@^1.0.2:
33713368
version "1.0.2"
33723369
resolved "https://registry.yarnpkg.com/data-view-buffer/-/data-view-buffer-1.0.2.tgz#211a03ba95ecaf7798a8c7198d79536211f88570"
@@ -4196,6 +4193,14 @@ fecha@^4.2.0:
41964193
resolved "https://registry.yarnpkg.com/fecha/-/fecha-4.2.3.tgz#4d9ccdbc61e8629b259fdca67e65891448d569fd"
41974194
integrity sha512-OP2IUU6HeYKJi3i0z4A19kHMQoLVs4Hc+DPqqxI2h/DPZHTm/vjsfC6P0b4jCMy14XizLBqvndQ+UilD7707Jw==
41984195

4196+
fetch-blob@^3.1.2, fetch-blob@^3.1.4:
4197+
version "3.2.0"
4198+
resolved "https://registry.yarnpkg.com/fetch-blob/-/fetch-blob-3.2.0.tgz#f09b8d4bbd45adc6f0c20b7e787e793e309dcce9"
4199+
integrity sha512-7yAQpD2UMJzLi1Dqv7qFYnPbaPx7ZfFK6PiIxQ4PfkGPyNyl2Ugx+a/umUonmKqjhM4DnfbMvdX6otXq83soQQ==
4200+
dependencies:
4201+
node-domexception "^1.0.0"
4202+
web-streams-polyfill "^3.0.3"
4203+
41994204
file-entry-cache@^8.0.0:
42004205
version "8.0.0"
42014206
resolved "https://registry.yarnpkg.com/file-entry-cache/-/file-entry-cache-8.0.0.tgz#7787bddcf1131bffb92636c69457bbc0edd6d81f"
@@ -4343,6 +4348,13 @@ form-data@~2.3.2:
43434348
combined-stream "^1.0.6"
43444349
mime-types "^2.1.12"
43454350

4351+
formdata-polyfill@^4.0.10:
4352+
version "4.0.10"
4353+
resolved "https://registry.yarnpkg.com/formdata-polyfill/-/formdata-polyfill-4.0.10.tgz#24807c31c9d402e002ab3d8c720144ceb8848423"
4354+
integrity sha512-buewHzMvYL29jdeQTVILecSaZKnt/RJWjoZCF5OW60Z67/GmSLBkOFM7qh1PI3zFNtJbaZL5eQu1vLfazOwj4g==
4355+
dependencies:
4356+
fetch-blob "^3.1.2"
4357+
43464358
forwarded@0.2.0:
43474359
version "0.2.0"
43484360
resolved "https://registry.yarnpkg.com/forwarded/-/forwarded-0.2.0.tgz#2269936428aad4c15c7ebe9779a84bf0b2a81811"
@@ -6349,13 +6361,27 @@ node-addon-api@^8.3.0:
63496361
resolved "https://registry.yarnpkg.com/node-addon-api/-/node-addon-api-8.3.1.tgz#53bc8a4f8dbde3de787b9828059da94ba9fd4eed"
63506362
integrity sha512-lytcDEdxKjGJPTLEfW4mYMigRezMlyJY8W4wxJK8zE533Jlb8L8dRuObJFWg2P+AuOIxoCgKF+2Oq4d4Zd0OUA==
63516363

6352-
node-fetch@^2.3.0, node-fetch@^2.6.0:
6364+
node-domexception@^1.0.0:
6365+
version "1.0.0"
6366+
resolved "https://registry.yarnpkg.com/node-domexception/-/node-domexception-1.0.0.tgz#6888db46a1f71c0b76b3f7555016b63fe64766e5"
6367+
integrity sha512-/jKZoMpw0F8GRwl4/eLROPA3cfcXtLApP0QzLmUT/HuPCZWyB7IY9ZrMeKw2O/nFIqPQB3PVM9aYm0F312AXDQ==
6368+
6369+
node-fetch@^2.3.0:
63536370
version "2.7.0"
63546371
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.7.0.tgz#d0f0fa6e3e2dc1d27efcd8ad99d550bda94d187d"
63556372
integrity sha512-c4FRfUm/dbcWZ7U+1Wq0AwCyFL+3nt2bEw05wfxSz+DWpWsitgmSgYmy2dQdWyKC1694ELPqMs/YzUSNozLt8A==
63566373
dependencies:
63576374
whatwg-url "^5.0.0"
63586375

6376+
node-fetch@^3.3.2:
6377+
version "3.3.2"
6378+
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-3.3.2.tgz#d1e889bacdf733b4ff3b2b243eb7a12866a0b78b"
6379+
integrity sha512-dRB78srN/l6gqWulah9SrxeYnxeddIG30+GOqK/9OlLVyLg3HPnr6SqOWTWOXKRwC2eGYCkZ59NNuSgvSrpgOA==
6380+
dependencies:
6381+
data-uri-to-buffer "^4.0.0"
6382+
fetch-blob "^3.1.4"
6383+
formdata-polyfill "^4.0.10"
6384+
63596385
node-forge@^0.10.0:
63606386
version "0.10.0"
63616387
resolved "https://registry.yarnpkg.com/node-forge/-/node-forge-0.10.0.tgz#32dea2afb3e9926f02ee5ce8794902691a676bf3"
@@ -8299,6 +8325,11 @@ verror@1.10.0:
82998325
core-util-is "1.0.2"
83008326
extsprintf "^1.2.0"
83018327

8328+
web-streams-polyfill@^3.0.3:
8329+
version "3.3.3"
8330+
resolved "https://registry.yarnpkg.com/web-streams-polyfill/-/web-streams-polyfill-3.3.3.tgz#2073b91a2fdb1fbfbd401e7de0ac9f8214cecb4b"
8331+
integrity sha512-d2JWLCivmZYTSIoge9MsgFCZrt571BikcWGYkjC1khllbTeDlGqZ2D8vD8E/lJa8WGWbb7Plm8/XJYV7IJHZZw==
8332+
83028333
webidl-conversions@^3.0.0:
83038334
version "3.0.1"
83048335
resolved "https://registry.yarnpkg.com/webidl-conversions/-/webidl-conversions-3.0.1.tgz#24534275e2a7bc6be7bc86611cc16ae0a5654871"

0 commit comments

Comments
 (0)