2011-05-15 17 views
9

En Nodejs, cuando console.log un objeto de solicitud, ¿qué significa [Circular]?En Nodejs, cuando console.log un objeto de solicitud, ¿a qué se refiere [Circular]? Cómo determinar que

Aquí hay un ejemplo console.log (req) contra un ejemplo básico de nodejs. Observe que request.socket._readWatcher.socket es un [Circular]. ¿Eso significa que se refiere a sí mismo? ¿Cómo puedo desreferenciar eso?

{ socket: 
    { bufferSize: 0, 
    fd: 7, 
    type: 'tcp4', 
    allowHalfOpen: true, 
    _readWatcher: 
     { socket: [Circular], 
     callback: [Function: onReadable] }, 
    destroyed: false, 
    readable: true, 
    _writeQueue: [], 
    _writeQueueEncoding: [], 
    _writeQueueFD: [], 
    _writeQueueCallbacks: [], 
    _writeWatcher: 
     { socket: [Circular], 
     callback: [Function: onWritable] }, 
    writable: true, 
    _writeImpl: [Function], 
    _readImpl: [Function], 
    _shutdownImpl: [Function], 
    remoteAddress: '127.0.1.1', 
    remotePort: 40407, 
    server: 
     { connections: 1, 
     allowHalfOpen: true, 
     watcher: [Object], 
     _events: [Object], 
     httpAllowHalfOpen: false, 
     type: 'tcp4', 
     fd: 5 }, 
    ondrain: [Function], 
    _idleTimeout: 120000, 
    _idleNext: 
     { repeat: 120, 
     _idleNext: [Circular], 
     _idlePrev: [Circular], 
     callback: [Function] }, 
    _idlePrev: 
     { repeat: 120, 
     _idleNext: [Circular], 
     _idlePrev: [Circular], 
     callback: [Function] }, 
    _idleStart: Sun, 15 May 2011 01:18:50 GMT, 
    _events: 
     { timeout: [Function], 
     error: [Function], 
     close: [Function] }, 
    ondata: [Function], 
    onend: [Function], 
    _httpMessage: 
     { output: [], 
     outputEncodings: [], 
     writable: true, 
     _last: false, 
     chunkedEncoding: true, 
     shouldKeepAlive: true, 
     useChunkedEncodingByDefault: true, 
     _hasBody: true, 
     _trailer: '', 
     finished: false, 
     socket: [Circular], 
     connection: [Circular], 
     _events: [Object], 
     _header: 'HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nConnection: keep-alive\r\nTransfer-Encoding: chunked\r\n\r\n', 
     _headerSent: false } }, 
    connection: 
    { bufferSize: 0, 
    fd: 7, 
    type: 'tcp4', 
    allowHalfOpen: true, 
    _readWatcher: 
     { socket: [Circular], 
     callback: [Function: onReadable] }, 
    destroyed: false, 
    readable: true, 
    _writeQueue: [], 
    _writeQueueEncoding: [], 
    _writeQueueFD: [], 
    _writeQueueCallbacks: [], 
    _writeWatcher: 
     { socket: [Circular], 
     callback: [Function: onWritable] }, 
    writable: true, 
    _writeImpl: [Function], 
    _readImpl: [Function], 
    _shutdownImpl: [Function], 
    remoteAddress: '127.0.1.1', 
    remotePort: 40407, 
    server: 
     { connections: 1, 
     allowHalfOpen: true, 
     watcher: [Object], 
     _events: [Object], 
     httpAllowHalfOpen: false, 
     type: 'tcp4', 
     fd: 5 }, 
    ondrain: [Function], 
    _idleTimeout: 120000, 
    _idleNext: 
     { repeat: 120, 
     _idleNext: [Circular], 
     _idlePrev: [Circular], 
     callback: [Function] }, 
    _idlePrev: 
     { repeat: 120, 
     _idleNext: [Circular], 
     _idlePrev: [Circular], 
     callback: [Function] }, 
    _idleStart: Sun, 15 May 2011 01:18:50 GMT, 
    _events: 
     { timeout: [Function], 
     error: [Function], 
     close: [Function] }, 
    ondata: [Function], 
    onend: [Function], 
    _httpMessage: 
     { output: [], 
     outputEncodings: [], 
     writable: true, 
     _last: false, 
     chunkedEncoding: true, 
     shouldKeepAlive: true, 
     useChunkedEncodingByDefault: true, 
     _hasBody: true, 
     _trailer: '', 
     finished: false, 
     socket: [Circular], 
     connection: [Circular], 
     _events: [Object], 
     _header: 'HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nConnection: keep-alive\r\nTransfer-Encoding: chunked\r\n\r\n', 
     _headerSent: false } }, 
    httpVersion: '1.1', 
    complete: false, 
    headers: 
    { 'user-agent': 'curl/7.21.0 (x86_64-pc-linux-gnu) libcurl/7.21.0 OpenSSL/0.9.8o zlib/1.2.3.4 libidn/1.18', 
    host: 'uh-uh-uh-I-aint-telling', 
    accept: '*/*' }, 
    trailers: {}, 
    readable: true, 
    url: '/', 
    method: 'GET', 
    statusCode: null, 
    client: 
    { bufferSize: 0, 
    fd: 7, 
    type: 'tcp4', 
    allowHalfOpen: true, 
    _readWatcher: 
     { socket: [Circular], 
     callback: [Function: onReadable] }, 
    destroyed: false, 
    readable: true, 
    _writeQueue: [], 
    _writeQueueEncoding: [], 
    _writeQueueFD: [], 
    _writeQueueCallbacks: [], 
    _writeWatcher: 
     { socket: [Circular], 
     callback: [Function: onWritable] }, 
    writable: true, 
    _writeImpl: [Function], 
    _readImpl: [Function], 
    _shutdownImpl: [Function], 
    remoteAddress: '127.0.1.1', 
    remotePort: 40407, 
    server: 
     { connections: 1, 
     allowHalfOpen: true, 
     watcher: [Object], 
     _events: [Object], 
     httpAllowHalfOpen: false, 
     type: 'tcp4', 
     fd: 5 }, 
    ondrain: [Function], 
    _idleTimeout: 120000, 
    _idleNext: 
     { repeat: 120, 
     _idleNext: [Circular], 
     _idlePrev: [Circular], 
     callback: [Function] }, 
    _idlePrev: 
     { repeat: 120, 
     _idleNext: [Circular], 
     _idlePrev: [Circular], 
     callback: [Function] }, 
    _idleStart: Sun, 15 May 2011 01:18:50 GMT, 
    _events: 
     { timeout: [Function], 
     error: [Function], 
     close: [Function] }, 
    ondata: [Function], 
    onend: [Function], 
    _httpMessage: 
     { output: [], 
     outputEncodings: [], 
     writable: true, 
     _last: false, 
     chunkedEncoding: true, 
     shouldKeepAlive: true, 
     useChunkedEncodingByDefault: true, 
     _hasBody: true, 
     _trailer: '', 
     finished: false, 
     socket: [Circular], 
     connection: [Circular], 
     _events: [Object], 
     _header: 'HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nConnection: keep-alive\r\nTransfer-Encoding: chunked\r\n\r\n', 
     _headerSent: false } }, 
    httpVersionMajor: 1, 
    httpVersionMinor: 1, 
    upgrade: false } 

ACTUALIZACIÓN

Ya que al parecer no estaba tan claro como el que podría haber sido, ¿cómo puedo eliminar la referencia de lo que se está haciendo referencia elemento circular? ¿Es siempre el elemento padre?

También es de destacar que anoche aprendí sobre la clase Utils y el método de inspección. Lo que probablemente me dé más información sobre esto de lo que razonablemente quisiera saber;)

Respuesta

8

[Circular] simplemente significa referencia circular.

var o = { 
    "self": o 
} 

se muestra como

{ 
    "self": [Circular] 
} 

Se pudo demostrar que

{ 
    "self": { 
     "self": { 
       "self": { 
        ... 
       } 
     } 
    } 
} 

En su caso socket es el encaje exterior.

Y _idleNext y _idlePrev también apuntan a los exteriores.

actualización

_idleNext: // This one! 
    { repeat: 120, 
    _idleNext: [Circular], 



{ socket: // this one! 
    { bufferSize: 0, 
    fd: 7, 
    type: 'tcp4', 
    allowHalfOpen: true, 
    _readWatcher: 
     { socket: [Circular], 
     callback: [Function: onReadable] }, 
    destroyed: false, 
    readable: true, 
    _writeQueue: [], 
    _writeQueueEncoding: [], 
    _writeQueueFD: [], 
    _writeQueueCallbacks: [], 
    _writeWatcher: 
     { socket: [Circular], 
12

Es una referencia circular. Eso es todo.

Ejemplo:

a.b = a 

Ahora Log:

[Object a] { 
    b: [Object a] { 
     b: [Object a] { 
      b: [Object a] { 
       b: [Object a] { 
        b: [Object a] { 
         b: [Object a] { 
          ... welcome to recursion! 
         } 
        } 
       } 

      } 
     } 
    } 
} 

Será que nunca y, la llamada log producirá un desbordamiento de pila y eso es todo. Node detecta eso y da el texto de referencia circular en su lugar.

+16

+1 para ir 4 capas más profundo que yo. – Raynos

+0

@Raynos Originalmente, planeé escribir una macro VIM para producir la cosa, pero temía que podría bloquear mi maravilloso editor ... –

+0

No, ¿qué es una referencia circular TO (debería haber sido más específico) – jcolebrand

2

request.socket._readWatcher.socket == request.socket De forma similar para las otras referencias [circulares].

No debe desreferenciarlos, ya que dará como resultado un comportamiento indefinido (los objetos que tienen sockets necesitan esa referencia para funcionar correctamente).

+0

Sí, no No quiero desreferenciarlos en código, más bien en mi mente. – jcolebrand

Cuestiones relacionadas