(method, host, path, body = '', config = null, signingDate = new Date(), headers = {})
| 6 | const { getCredentials } = require('../support/credentials'); |
| 7 | |
| 8 | const sendRequest = async (method, host, path, body = '', config = null, signingDate = new Date(), headers = {}) => { |
| 9 | const service = 's3'; |
| 10 | const region = 'us-east-1'; |
| 11 | |
| 12 | // Ensure host includes port for canonical request |
| 13 | const hostname = host.split(':')[0]; // Extract 127.0.0.1 |
| 14 | const port = parseInt(host.split(':')[1] || '8000', 10); // Default to 8000 |
| 15 | const [pathBase, queryString] = path.split('?'); |
| 16 | const query = queryString ? Object.fromEntries(new URLSearchParams(queryString)) : {}; |
| 17 | |
| 18 | // Create HTTP request (mimics AWS.HttpRequest with v2-like endpoint structure) |
| 19 | const request = new HttpRequest({ |
| 20 | protocol: 'http:', // Match Scality CloudServer |
| 21 | hostname, // 127.0.0.1 |
| 22 | port, // 8000 |
| 23 | method: method.toUpperCase(), |
| 24 | path: pathBase, |
| 25 | query, |
| 26 | body, |
| 27 | headers: { |
| 28 | Host: host, // Explicitly set Host: 127.0.0.1:8000 |
| 29 | 'X-Amz-Date': signingDate.toISOString().replace(/[:\-]|\.\d{3}/g, ''), |
| 30 | ...headers, |
| 31 | }, |
| 32 | }); |
| 33 | |
| 34 | // Compute SHA256 hash for body |
| 35 | const sha256 = new Sha256(); |
| 36 | sha256.update(request.body || ''); |
| 37 | const hash = await sha256.digest(); |
| 38 | request.headers['X-Amz-Content-SHA256'] = Buffer.from(hash).toString('hex'); |
| 39 | request.region = region; |
| 40 | |
| 41 | // Get credentials - use same source as S3 client configuration |
| 42 | let accessKeyId = config?.accessKey || config?.accessKeyId; |
| 43 | let secretAccessKey = config?.secretKey || config?.secretAccessKey; |
| 44 | |
| 45 | // If not provided in config, use getCredentials (matches S3 client credential source) |
| 46 | if (!accessKeyId || !secretAccessKey) { |
| 47 | const defaultCreds = getCredentials('default'); |
| 48 | accessKeyId = accessKeyId || defaultCreds.accessKeyId; |
| 49 | secretAccessKey = secretAccessKey || defaultCreds.secretAccessKey; |
| 50 | } |
| 51 | |
| 52 | if (!accessKeyId || !secretAccessKey) { |
| 53 | throw new Error('Missing accessKeyId or secretAccessKey in config'); |
| 54 | } |
| 55 | const credentials = { accessKeyId, secretAccessKey }; |
| 56 | |
| 57 | // Create signer |
| 58 | const signer = new SignatureV4({ |
| 59 | credentials, |
| 60 | region, |
| 61 | service, |
| 62 | sha256: Sha256, |
| 63 | uriEscapePath: true, |
| 64 | applyChecksum: true, |
| 65 | }); |
no test coverage detected