|
1 | 1 | const nodeFetch = require('node-fetch'); |
2 | | -const AWS = require('aws-sdk'); |
| 2 | +const { HttpRequest } = require('@aws-sdk/protocol-http'); |
| 3 | +const { SignatureV4 } = require('@aws-sdk/signature-v4'); |
| 4 | +const { Sha256 } = require('@aws-crypto/sha256-js'); |
3 | 5 | const xml2js = require('xml2js'); |
4 | 6 |
|
5 | | -const sendRequest = async (method, host, path, body = '', config = null) => { |
| 7 | +const sendRequest = async (method, host, path, body = '', config = null, signingDate = new Date()) => { |
6 | 8 | const service = 's3'; |
7 | | - const endpoint = new AWS.Endpoint(host); |
8 | | - |
9 | | - const request = new AWS.HttpRequest(endpoint); |
10 | | - request.method = method.toUpperCase(); |
11 | | - request.path = path; |
12 | | - request.body = body; |
13 | | - request.headers.Host = host; |
14 | | - request.headers['X-Amz-Date'] = new Date().toISOString().replace(/[:\-]|\.\d{3}/g, ''); |
15 | | - const sha256hash = AWS.util.crypto.sha256(request.body || '', 'hex'); |
16 | | - request.headers['X-Amz-Content-SHA256'] = sha256hash; |
17 | | - request.region = 'us-east-1'; |
18 | | - |
19 | | - const signer = new AWS.Signers.V4(request, service); |
20 | | - const accessKeyId = config?.accessKey || AWS.config.credentials?.accessKeyId; |
21 | | - const secretAccessKey = config?.secretKey || AWS.config.credentials?.secretAccessKey; |
22 | | - const credentials = new AWS.Credentials(accessKeyId, secretAccessKey); |
23 | | - signer.addAuthorization(credentials, new Date()); |
24 | | - |
25 | | - const url = `http://${host}${path}`; |
| 9 | + const region = 'us-east-1'; |
| 10 | + |
| 11 | + // Ensure host includes port for canonical request |
| 12 | + const hostname = host.split(':')[0]; // Extract 127.0.0.1 |
| 13 | + const port = parseInt(host.split(':')[1] || '8000', 10); // Default to 8000 |
| 14 | + const [pathBase, queryString] = path.split('?'); |
| 15 | + const query = queryString ? Object.fromEntries(new URLSearchParams(queryString)) : {}; |
| 16 | + |
| 17 | + // Create HTTP request (mimics AWS.HttpRequest with v2-like endpoint structure) |
| 18 | + const request = new HttpRequest({ |
| 19 | + protocol: 'http:', // Match Scality CloudServer |
| 20 | + hostname, // 127.0.0.1 |
| 21 | + port, // 8000 |
| 22 | + method: method.toUpperCase(), |
| 23 | + path: pathBase, |
| 24 | + query, |
| 25 | + body, |
| 26 | + headers: { |
| 27 | + Host: host, // Explicitly set Host: 127.0.0.1:8000 |
| 28 | + 'X-Amz-Date': signingDate.toISOString().replace(/[:\-]|\.\d{3}/g, ''), |
| 29 | + }, |
| 30 | + }); |
| 31 | + |
| 32 | + // Compute SHA256 hash for body |
| 33 | + const sha256 = new Sha256(); |
| 34 | + sha256.update(request.body || ''); |
| 35 | + const hash = await sha256.digest(); |
| 36 | + request.headers['X-Amz-Content-SHA256'] = Buffer.from(hash).toString('hex'); |
| 37 | + request.region = region; |
| 38 | + |
| 39 | + // Get credentials |
| 40 | + const accessKeyId = config?.accessKey || config?.accessKeyId || 'accessKey1'; |
| 41 | + const secretAccessKey = config?.secretKey || config?.secretAccessKey || 'verySecretKey1'; |
| 42 | + if (!accessKeyId || !secretAccessKey) { |
| 43 | + throw new Error('Missing accessKeyId or secretAccessKey in config'); |
| 44 | + } |
| 45 | + const credentials = { accessKeyId, secretAccessKey }; |
| 46 | + |
| 47 | + // Create signer |
| 48 | + const signer = new SignatureV4({ |
| 49 | + credentials, |
| 50 | + region, |
| 51 | + service, |
| 52 | + sha256: Sha256, |
| 53 | + uriEscapePath: true, |
| 54 | + applyChecksum: true, |
| 55 | + }); |
| 56 | + |
| 57 | + // Sign request |
| 58 | + const signedRequest = await signer.sign(request, { signingDate }); |
| 59 | + |
| 60 | + // Rename 'authorization' to 'Authorization' |
| 61 | + if (signedRequest.headers.authorization) { |
| 62 | + signedRequest.headers.Authorization = signedRequest.headers.authorization; |
| 63 | + delete signedRequest.headers.authorization; |
| 64 | + } |
| 65 | + |
| 66 | + // Send HTTP request |
| 67 | + const url = `http://${host}${path}`; // Match Scality CloudServer |
26 | 68 | const options = { |
27 | | - method: request.method, |
28 | | - headers: request.headers, |
| 69 | + method: signedRequest.method, |
| 70 | + headers: signedRequest.headers, |
29 | 71 | }; |
30 | 72 |
|
31 | | - if (method !== 'GET') { |
32 | | - options.body = request.body; |
| 73 | + if (method.toUpperCase() !== 'GET') { |
| 74 | + options.body = signedRequest.body; |
33 | 75 | } |
34 | 76 |
|
35 | | - const response = await nodeFetch(url, options); |
| 77 | + let response; |
| 78 | + try { |
| 79 | + response = await (nodeFetch.default || nodeFetch)(url, options); |
| 80 | + } catch (error) { |
| 81 | + throw new Error(`HTTP request failed: ${error.message}`); |
| 82 | + } |
36 | 83 | const text = await response.text(); |
37 | | - const result = await xml2js.parseStringPromise(text); |
| 84 | + |
| 85 | + let result; |
| 86 | + try { |
| 87 | + result = await xml2js.parseStringPromise(text); |
| 88 | + } catch { |
| 89 | + result = { Error: { Message: text } }; |
| 90 | + } |
38 | 91 | if (result && result.Error) { |
39 | 92 | throw result; |
40 | 93 | } |
41 | 94 |
|
42 | | - return result; |
| 95 | + return { |
| 96 | + result, |
| 97 | + status: response.status, |
| 98 | + ok: response.ok, |
| 99 | + error: result?.Error ? text : null, |
| 100 | + request: signedRequest, |
| 101 | + }; |
43 | 102 | }; |
44 | 103 |
|
45 | 104 | module.exports = { |
|
0 commit comments