push.mjs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import { createHash, createHmac, randomUUID } from 'node:crypto';
  2. import { readFileSync } from 'node:fs';
  3. import { request } from 'node:https';
  4. const config = Object.fromEntries(readFileSync('client.env', 'utf8').trimEnd().split('\n').map(line => line.split('=')));
  5. const env = name => {
  6. if (!process.env[name]) throw new Error('Set ' + name);
  7. return process.env[name];
  8. };
  9. const base = new URL(process.env.INGRESS_BASE || config.INGRESS_BASE);
  10. if (base.protocol !== 'https:' || base.username || base.password || base.pathname !== '/' || base.search || base.hash)
  11. throw new Error('INGRESS_BASE must be an HTTPS origin');
  12. const [, , action, value] = process.argv;
  13. if (!value || !['send', 'status'].includes(action))
  14. throw new Error('Usage: push.mjs send <file> | status <batchId>');
  15. const send = action === 'send';
  16. if (!send && !/^[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12}$/i.test(value)) throw new Error('Invalid batchId');
  17. const endpoint = config.DATASET_ENDPOINT + (send ? '' : '/batches/' + value.toLowerCase());
  18. const method = send ? 'POST' : 'GET';
  19. const body = send ? readFileSync(value) : Buffer.alloc(0);
  20. const media = send ? (value.toLowerCase().endsWith('.ndjson') ? 'application/x-ndjson' : 'application/json') : '';
  21. const idem = send ? env('IDEMPOTENCY_KEY') : '';
  22. const schema = send ? config.SCHEMA_VERSION : '';
  23. const factory = media === 'application/json' ? '' : config.FACTORY_ID;
  24. const accessKey = env('ACCESS_KEY');
  25. const timestamp = Math.floor(Date.now() / 1000).toString();
  26. const nonce = randomUUID().replaceAll('-', '');
  27. const digest = createHash('sha256').update(body).digest('hex');
  28. const version = 'mdp-ingress-hmac-sha256-v2';
  29. const wire = [version, method, endpoint, accessKey, timestamp, nonce, digest, idem, media, factory, schema].join('&');
  30. const signature = createHmac('sha256', Buffer.from(env('SIGNING_KEY_BASE64'), 'base64')).update(wire).digest('base64');
  31. const headers = { 'X-Access-Key': accessKey, 'X-Timestamp': timestamp, 'X-Nonce': nonce, 'X-Content-SHA256': digest,
  32. 'X-Signature': signature, 'X-Signature-Version': version };
  33. if (factory) headers['X-Factory-Id'] = factory;
  34. if (send) Object.assign(headers, { 'Content-Type': media, 'Content-Length': body.length,
  35. 'Idempotency-Key': idem, 'X-Schema-Version': schema });
  36. const ca = process.env.INGRESS_CA_FILE ? readFileSync(process.env.INGRESS_CA_FILE) : undefined;
  37. const pass = request(new URL(endpoint, base), { method, headers, ca, timeout: 30_000 }, response => {
  38. console.log('HTTP ' + response.statusCode);
  39. if (response.headers['retry-after']) console.log('Retry-After: ' + response.headers['retry-after']);
  40. response.pipe(process.stdout);
  41. if (response.statusCode >= 300) process.exitCode = 1;
  42. });
  43. pass.on('timeout', () => pass.destroy(new Error('Request timed out')));
  44. pass.end(send ? body : undefined);