Skip to content

Commit 74bef69

Browse files
committed
refactor: consolidate workaround logic in a function
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: na - task: lint_package_json status: na - task: lint_repl_help status: na - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: na - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: na - task: lint_typescript_tests status: na - task: lint_license_headers status: passed ---
1 parent 10d8600 commit 74bef69

File tree

1 file changed

+20
-7
lines changed
  • lib/node_modules/@stdlib/net/disposable-http-server/lib

1 file changed

+20
-7
lines changed

lib/node_modules/@stdlib/net/disposable-http-server/lib/main.js

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,10 @@ var isString = require( '@stdlib/assert/is-string' ).isPrimitive;
2929
var isFunction = require( '@stdlib/assert/is-function' );
3030
var openURL = require( '@stdlib/utils/open-url' );
3131
var noop = require( '@stdlib/utils/noop' );
32-
var Buffer = require( '@stdlib/buffer/ctor' );
32+
var buffer2bytelength = require( '@stdlib/buffer/ctor' ).byteLength;
3333
var string2buffer = require( '@stdlib/buffer/from-string' );
3434
var nextTick = require( '@stdlib/utils/next-tick' );
35+
var NODE_VERSION = require( '@stdlib/process/node-version' );
3536
var format = require( '@stdlib/string/format' );
3637
var validate = require( './validate.js' );
3738
var serverOpts = require( './opts.js' );
@@ -41,6 +42,7 @@ var createStore = require( './connections_store.js' );
4142
// VARIABLES //
4243

4344
var debug = logger( 'disposable-http-server' );
45+
var SUPPORTS_BUFFER = ( parseInt( NODE_VERSION.split( '.' )[ 0 ], 10 ) < 6 );
4446

4547

4648
// FUNCTIONS //
@@ -80,6 +82,21 @@ function onClose() {
8082
debug( 'Server closed.' );
8183
}
8284

85+
/**
86+
* Returns the byte length of an encoded string.
87+
*
88+
* @private
89+
* @param {(string|Buffer)} str - input string
90+
* @returns {NonNegativeInteger} byte length
91+
*/
92+
function byteLength( str ) { // TODO: consider making a robust utility in `@stdlib/buffer/byte-length`
93+
if ( SUPPORTS_BUFFER ) {
94+
return buffer2bytelength( str );
95+
}
96+
// Earlier versions of Node.js do not support Buffers, ArrayBuffers, TypedArrays, DataViews, or SharedArrayBuffers, so we need to explicitly call `#.toString()`...
97+
return buffer2bytelength( str.toString() );
98+
}
99+
83100

84101
// MAIN //
85102

@@ -270,9 +287,7 @@ function httpServer( options ) {
270287
debug( 'Sending HTML...' );
271288
response.statusCode = 200;
272289
response.setHeader( 'Content-Type', 'text/html' );
273-
274-
// TODO: we have to convert to a `string` because Node v0.10 requires a `string`. Subsequent versions support providing a `Buffer` object. Ideally, we would sniff `Buffer` support and only convert to a `string` if necessary.
275-
response.setHeader( 'Content-Length', Buffer.byteLength( opts.html.toString() ) );
290+
response.setHeader( 'Content-Length', byteLength( opts.html ) );
276291
response.end( opts.html );
277292
}
278293

@@ -287,9 +302,7 @@ function httpServer( options ) {
287302
debug( 'Sending JavaScript...' );
288303
response.statusCode = 200;
289304
response.setHeader( 'Content-Type', 'text/javascript' );
290-
291-
// TODO: we have to convert to a `string` because Node v0.10 requires a `string`. Subsequent versions support providing a `Buffer` object. Ideally, we would sniff `Buffer` support and only convert to a `string` if necessary.
292-
response.setHeader( 'Content-Length', Buffer.byteLength( opts.javascript.toString() ) );
305+
response.setHeader( 'Content-Length', byteLength( opts.javascript ) );
293306
response.end( opts.javascript );
294307
}
295308

0 commit comments

Comments
 (0)