presigner.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. var AWS = require('../core');
  2. var rest = AWS.Protocol.Rest;
  3. /**
  4. * A presigner object can be used to generate presigned urls for the Polly service.
  5. */
  6. AWS.Polly.Presigner = AWS.util.inherit({
  7. /**
  8. * Creates a presigner object with a set of configuration options.
  9. *
  10. * @option options params [map] An optional map of parameters to bind to every
  11. * request sent by this service object.
  12. * @option options service [AWS.Polly] An optional pre-configured instance
  13. * of the AWS.Polly service object to use for requests. The object may
  14. * bound parameters used by the presigner.
  15. * @see AWS.Polly.constructor
  16. */
  17. constructor: function Signer(options) {
  18. options = options || {};
  19. this.options = options;
  20. this.service = options.service;
  21. this.bindServiceObject(options);
  22. this._operations = {};
  23. },
  24. /**
  25. * @api private
  26. */
  27. bindServiceObject: function bindServiceObject(options) {
  28. options = options || {};
  29. if (!this.service) {
  30. this.service = new AWS.Polly(options);
  31. } else {
  32. var config = AWS.util.copy(this.service.config);
  33. this.service = new this.service.constructor.__super__(config);
  34. this.service.config.params = AWS.util.merge(this.service.config.params || {}, options.params);
  35. }
  36. },
  37. /**
  38. * @api private
  39. */
  40. modifyInputMembers: function modifyInputMembers(input) {
  41. // make copies of the input so we don't overwrite the api
  42. // need to be careful to copy anything we access/modify
  43. var modifiedInput = AWS.util.copy(input);
  44. modifiedInput.members = AWS.util.copy(input.members);
  45. AWS.util.each(input.members, function(name, member) {
  46. modifiedInput.members[name] = AWS.util.copy(member);
  47. // update location and locationName
  48. if (!member.location || member.location === 'body') {
  49. modifiedInput.members[name].location = 'querystring';
  50. modifiedInput.members[name].locationName = name;
  51. }
  52. });
  53. return modifiedInput;
  54. },
  55. /**
  56. * @api private
  57. */
  58. convertPostToGet: function convertPostToGet(req) {
  59. // convert method
  60. req.httpRequest.method = 'GET';
  61. var operation = req.service.api.operations[req.operation];
  62. // get cached operation input first
  63. var input = this._operations[req.operation];
  64. if (!input) {
  65. // modify the original input
  66. this._operations[req.operation] = input = this.modifyInputMembers(operation.input);
  67. }
  68. var uri = rest.generateURI(req.httpRequest.endpoint.path, operation.httpPath, input, req.params);
  69. req.httpRequest.path = uri;
  70. req.httpRequest.body = '';
  71. // don't need these headers on a GET request
  72. delete req.httpRequest.headers['Content-Length'];
  73. delete req.httpRequest.headers['Content-Type'];
  74. },
  75. /**
  76. * @overload getSynthesizeSpeechUrl(params = {}, [expires = 3600], [callback])
  77. * Generate a presigned url for {AWS.Polly.synthesizeSpeech}.
  78. * @note You must ensure that you have static or previously resolved
  79. * credentials if you call this method synchronously (with no callback),
  80. * otherwise it may not properly sign the request. If you cannot guarantee
  81. * this (you are using an asynchronous credential provider, i.e., EC2
  82. * IAM roles), you should always call this method with an asynchronous
  83. * callback.
  84. * @param params [map] parameters to pass to the operation. See the {AWS.Polly.synthesizeSpeech}
  85. * operation for the expected operation parameters.
  86. * @param expires [Integer] (3600) the number of seconds to expire the pre-signed URL operation in.
  87. * Defaults to 1 hour.
  88. * @return [string] if called synchronously (with no callback), returns the signed URL.
  89. * @return [null] nothing is returned if a callback is provided.
  90. * @callback callback function (err, url)
  91. * If a callback is supplied, it is called when a signed URL has been generated.
  92. * @param err [Error] the error object returned from the presigner.
  93. * @param url [String] the signed URL.
  94. * @see AWS.Polly.synthesizeSpeech
  95. */
  96. getSynthesizeSpeechUrl: function getSynthesizeSpeechUrl(params, expires, callback) {
  97. var self = this;
  98. var request = this.service.makeRequest('synthesizeSpeech', params);
  99. // remove existing build listeners
  100. request.removeAllListeners('build');
  101. request.on('build', function(req) {
  102. self.convertPostToGet(req);
  103. });
  104. return request.presign(expires, callback);
  105. }
  106. });