3

我无法在meteorjs 中使用imagemagick。我正在开发一个小型 svg->png 转换器,其中包含一个用于提供转换后的图像的 rest api。我用meteor-router实现了其余的api。imagemagick 转换有效。但是,我无法将转换结果写入 http 响应。我试图通过使用光纤来消除异步性来解决这个问题。但这仍然行不通。基本上,所有 request.write 调用在 yield 执行后都会被忽略。这是我的代码:

Meteor.Router.add({
  '/image/:hash' : function(hash) {

    var svg = Images.findOne({'hash' : hash}).svg;

    var request = this.request;
    var response = this.response;

    Fiber(function() {
      var fiber = Fiber.current;

      response.writeHead(200, {'Content-Type':'image/png'});

      var convert = imagemagick.convert(['svg:-', 'png:-']);

      convert.on('data', function(data) {
        response.write("doesn't work");
        //response.write(data);
      });

      convert.on('end', function() {
        response.write("doesn't work");
        //response.end();
        fiber.run({});
      });

      convert.stdin.write(svg);
      convert.stdin.end();

      response.write("works");
      Fiber.yield();
      response.write("doesn't work");
    }).run();

  }
});

我对meteorjs很陌生。因此,我可能会完全错误地使用 Fiber。或者我根本不应该使用纤维。有人可以帮忙吗?

4

2 回答 2

4

感谢meteor-router的作者,我能够解决这个问题。我以错误的方式使用光纤。如https://github.com/laverdet/node-fibers#futures所述,不建议在代码和原始 API 之间没有抽象的情况下使用 Fiber。

幸运的是,光纤提供了一种称为未来的抽象,可以用于我的用例!这是工作代码:

var require = __meteor_bootstrap__.require,
Future  = require('fibers/future');

Meteor.startup(function() {
  Meteor.Router.add('/image/:hash', function(hash) {
    var response = this.response;
    var fut = new Future();

    response.writeHead(200, {'Content-Type':'text/plain'});

    setTimeout(function(){ 
      response.write("hello hello");
      fut.ret();
    }, 1); 

    fut.wait();
  });
});
于 2012-12-17T07:30:54.553 回答
0

我又做了一些调查。这个问题与 imagemagick 是正交的。例如:以下代码片段在流星路由器中也不起作用:

示例 1:

Meteor.startup(function() {                                                        
  Meteor.Router.add({                                                              
    '/image/:hash' : function(hash) 

      var request = this.request;                                                  
      var response = this.response;                                                

      response.write("outside");                                                   

      setTimeout(function(){                                                       
        response.write("inside");                                                  
        response.end();                                                            
      }, 1); 
    }                                                                           
  }); 

示例 2:

Meteor.startup(function() {                                                        
  Meteor.Router.add({                                                              
    '/image/:hash' : function(hash) 
      var request = this.request;                                               
      var response = this.response;                                             

      response.write("outside");                                                

      Fiber(function() {                                                        
        var fiber = Fiber.current;                                              

        setTimeout(function(){                                                  
          response.write("inside");                                             
          response.end();                                                       
        }, 1);                                                                  
        Fiber.yield();                                                          
      }).run();   
    }                                                                           
  }); 

我认为这是流星路由器的一般问题。因为这两个示例都适用于纯 nodejs。

于 2012-12-15T12:34:29.373 回答