Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added enableObjectStream option #194

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"on-headers": "~1.0.1"
},
"devDependencies": {
"eslint": "5.9.0",
"eslint": "5.10.0",
"eslint-config-standard": "12.0.0",
"eslint-plugin-import": "2.14.0",
"eslint-plugin-markdown": "1.0.0-rc.0",
Expand Down
71 changes: 71 additions & 0 deletions test/morgan.js
Original file line number Diff line number Diff line change
Expand Up @@ -1376,6 +1376,77 @@ 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(typeof streamOutput === 'object')
assert(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