Gitlab@Informatics

Skip to content
Snippets Groups Projects
Select Git revision
  • 3bfe0608c0e6db315b4ae6b83afb2078749fe3f7
  • main default protected
  • revert-a98119d8
3 results

s3_setup.js

Blame
  • s3_setup.js 4.47 KiB
    'use strict';
    
    module.exports = exports;
    
    const url = require('url');
    const fs = require('fs');
    const path = require('path');
    
    module.exports.detect = function(opts, config) {
      const to = opts.hosted_path;
      const uri = url.parse(to);
      config.prefix = (!uri.pathname || uri.pathname === '/') ? '' : uri.pathname.replace('/', '');
      if (opts.bucket && opts.region) {
        config.bucket = opts.bucket;
        config.region = opts.region;
        config.endpoint = opts.host;
        config.s3ForcePathStyle = opts.s3ForcePathStyle;
      } else {
        const parts = uri.hostname.split('.s3');
        const bucket = parts[0];
        if (!bucket) {
          return;
        }
        if (!config.bucket) {
          config.bucket = bucket;
        }
        if (!config.region) {
          const region = parts[1].slice(1).split('.')[0];
          if (region === 'amazonaws') {
            config.region = 'us-east-1';
          } else {
            config.region = region;
          }
        }
      }
    };
    
    module.exports.get_s3 = function(config) {
    
      if (process.env.node_pre_gyp_mock_s3) {
        // here we're mocking. node_pre_gyp_mock_s3 is the scratch directory
        // for the mock code.
        const AWSMock = require('mock-aws-s3');
        const os = require('os');
    
        AWSMock.config.basePath = `${os.tmpdir()}/mock`;
    
        const s3 = AWSMock.S3();
    
        // wrapped callback maker. fs calls return code of ENOENT but AWS.S3 returns
        // NotFound.
        const wcb = (fn) => (err, ...args) => {
          if (err && err.code === 'ENOENT') {
            err.code = 'NotFound';
          }
          return fn(err, ...args);
        };
    
        return {
          listObjects(params, callback) {
            return s3.listObjects(params, wcb(callback));
          },
          headObject(params, callback) {
            return s3.headObject(params, wcb(callback));
          },
          deleteObject(params, callback) {
            return s3.deleteObject(params, wcb(callback));
          },
          putObject(params, callback) {
            return s3.putObject(params, wcb(callback));