1

I have a node.js project that uses express. Within this project I have a models folder that contains different models used in the project.

Model list

In my MosaicParTileStreamerS3Only model I have the following:

'use strict'; 
var GlobalMercator = require('./GlobalMercator.js');
var S3 = require('./S3.js'); 
var binary = require('binary'); 
var bufferpack = require('bufferpack'); 

function MosaicParTileStreamerS3Only() {

}; 

Further into this model, I use S3 without any issues:

MosaicParTileStreamerS3Only.prototype.Init = function(filepath, index, s3config){
   var retval = false; 

   this.s3 = new S3(s3config.access_key, s3config.secret_key, s3config.host); 
   var host = s3config.host; 
   var bucket = s3config.bucket; 
   filepath = s3config.tile_directory + filepath; 

   var request = this.s3.getObject(bucket, filepath, false, 0, 
   this.HEADER_SIZE + 5 * this.RESOLUTION_ENTRY_SIZE + this.TILE_COUNT_SIZE    
   + 256 * this.TILE_ENTRY_SIZE); 

   ...

Placing a breakpoint on the second line (this.s3 = new S3(s3config.access_key, s3config.secret_key, s3config.host);) shows that S3 is being brought in as a function.

S3 As Function

I have the following in my S3Request.js:

'use strict';

var S3 = require('./S3.js'); 
var STDClass = require('stdclass'); 
var Curl = require('node-libcurl').Curl; 
var parseString = require('xml2js').parseString; 



function S3Request(verb, bucket, uri, endpoint){
    this.headers = ''; 
    this.endpoint = endpoint; 
    this.verb = verb; 
    this.bucket = bucket;
    this.uri = (uri !== '' ? '/' + '%2F'.replace('/', encodeURIComponent(uri)) : '/'); 

    ...

}

Later on in this model I try using S3:

    var test = new S3(); 
    headers['Authorization'] = S3.getSignature(this.verb + '\n' + 
    this.headers['Content-MD5'] + "\n" + this.headers['Content-Type']
    + "\n" + this.headers["Date"] + amz + "\n" + this.resource); 

I get the following error:

TypeError: S3 is not a function

Putting a breakpoint on var test = new S3(); shows that in this file S3 is considered an object: S3 as object

Any clue as to what's going wrong?

Sara Fuerst
  • 5,688
  • 8
  • 43
  • 86

1 Answers1

1

The issue is that there is a cyclic dependency between S3 and S3Request.

See How to deal with cyclic dependencies in Node.js

Community
  • 1
  • 1
Sara Fuerst
  • 5,688
  • 8
  • 43
  • 86