Skip to content

Commit

Permalink
Merge d2c1467 into 12a48c5
Browse files Browse the repository at this point in the history
  • Loading branch information
SnakeA committed May 7, 2019
2 parents 12a48c5 + d2c1467 commit f05753b
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 3 deletions.
13 changes: 10 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,14 @@ function morgan (format, options) {
var skip = opts.skip || false

// format function
var formatLine = typeof fmt !== 'function'
var isFormatFunction = typeof fmt === 'function'
var formatLine = !isFormatFunction
? getFormatFunction(fmt)
: fmt

// stream
var buffer = opts.buffer
var isStreamDefined = !!opts.stream
var stream = opts.stream || process.stdout

// buffering support
Expand Down Expand Up @@ -126,8 +128,13 @@ function morgan (format, options) {
return
}

debug('log request')
stream.write(line + '\n')
if (opts.enableObjectStream && isStreamDefined && isFormatFunction) {
debug('log request without newline character')
stream.write(line)
} else {
debug('log request')
stream.write(line + '\n')
}
};

if (immediate) {
Expand Down
70 changes: 70 additions & 0 deletions test/morgan.js
Original file line number Diff line number Diff line change
Expand Up @@ -1376,6 +1376,76 @@ describe('morgan()', function () {
.expect(200, done)
})
})

describe('with enableObjectStream option', function () {
it('should write an Object to stream based on the custom format function', function (done) {
var cb = after(2, function (err, res, streamOutput) {
if (err) return done(err)
assert.deepStrictEqual(streamOutput, { get: 'GET' })
done()
})

var customStream = {
write: function (streamOutput) {
cb(null, null, streamOutput)
}
}

var customFormatFunction = function (tokens, req, res) {
return { get: tokens.method(req, res) }
}

request(createServer(customFormatFunction, { enableObjectStream: true, stream: customStream }))
.get('/fakeEndpoint')
.expect(200, cb)
})

it('should write a string to stream when format is not a custom function', function (done) {
var cb = after(2, function (err, res, streamOutput) {
if (err) return done(err)
var masked = streamOutput.replace(/\w+, \d+ \w+ \d+ \d+:\d+:\d+ \w+/, '_timestamp_')
assert.strictEqual(masked, res.text + ' - tj [_timestamp_] "GET /fakeEndpoint HTTP/1.1" 200 - "http://localhost/" "my-ua"\n')
done()
})

var customStream = {
write: function (streamOutput) {
cb(null, null, streamOutput)
}
}

request(createServer('default', { enableObjectStream: true, stream: customStream }))
.get('/fakeEndpoint')
.set('Authorization', 'Basic dGo6')
.set('Referer', 'http://localhost/')
.set('User-Agent', 'my-ua')
.expect(200, cb)
})

it('should default to process.stdout when stream is not defined', function (done) {
var cb = after(2, function (err, res, line) {
if (err) return done(err)
assert(res.text.length > 0)
assert(typeof line === 'string')
done()
})

var stream = createLineStream(function (line) {
cb(null, null, line)
})

Object.defineProperty(process, 'stdout', {
value: stream
})

var customFormatFunction = function (tokens, req, res) {
return { get: tokens.method(req, res) }
}
request(createServer(customFormatFunction, { enableObjectStream: true, stream: undefined }))
.get('/')
.expect(200, cb)
})
})
})

describe('morgan.compile(format)', function () {
Expand Down

0 comments on commit f05753b

Please sign in to comment.