Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,13 @@ jobs:
node-version: latest
- run: npm ci
- run: npm run format:check

lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v7
- uses: actions/setup-node@v7
with:
node-version: latest
- run: npm ci
- run: npm run lint
43 changes: 43 additions & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
'use strict';

// The http and http2 diagnostics channels publish headers after they are
// parsed, so a `.headers` object is always present by the time any handler
// runs. Optional chaining on it is dead code and hides real mistakes.
const banOptionalChainingOnHeaders = () => ({
selector: "MemberExpression[optional=true][object.property.name='headers']",
message:
'Optional chaining on .headers is unnecessary: the diagnostics ' +
'channels publish headers after parsing, so they are always an object.',
});

export default [
{
files: ['lib/**/*.js', 'test/**/*.js', 'benchmark/**/*.js', 'index.js'],
languageOptions: {
ecmaVersion: 'latest',
sourceType: 'commonjs',
globals: {
console: 'readonly',
process: 'readonly',
Buffer: 'readonly',
URL: 'readonly',
setTimeout: 'readonly',
clearTimeout: 'readonly',
setInterval: 'readonly',
clearInterval: 'readonly',
setImmediate: 'readonly',
fetch: 'readonly',
},
},
rules: {
'no-unused-vars': [
'error',
{ argsIgnorePattern: '^_', varsIgnorePattern: '^_' },
],
'no-restricted-syntax': ['error', banOptionalChainingOnHeaders()],
},
},
{
ignores: ['node_modules/', 'test/fixtures/'],
},
];
2 changes: 1 addition & 1 deletion lib/id.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const kBufferSize = 4096;
const randomBuffer = new Uint8Array(kBufferSize);
let randomOffset = kBufferSize; // Start at end to trigger first fill

const hexTable = new Array(256);
const hexTable = Array.from({ length: 256 });
for (let i = 0; i < 256; i++) hexTable[i] = i.toString(16).padStart(2, '0');

function ensureRandomBytes(needed) {
Expand Down
14 changes: 7 additions & 7 deletions lib/instrumentations.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,16 +229,16 @@ function onHttp2ServerStreamStart({ stream, headers }) {
// Exports travel over HTTP/1.1, so an export request can never arrive as
// an HTTP/2 stream and the self-trace suppression the http handlers need
// does not apply here.
const traceparent = headers?.traceparent;
const traceparent = headers.traceparent;
const parent =
traceparent != null ? Span.fromTraceparent(traceparent) : undefined;
// Per the W3C Trace Context spec, tracestate is only meaningful
// alongside a valid traceparent. It is discarded when the traceparent
// is absent or rejected, same as for HTTP/1.1 requests.
const tracestate = parent != null ? headers?.tracestate : undefined;
const tracestate = parent != null ? headers.tracestate : undefined;

const method = headers?.[':method'] || 'GET';
const rawUrl = headers?.[':path'] || '/';
const method = headers[':method'] || 'GET';
const rawUrl = headers[':path'] || '/';
const qIdx = rawUrl.indexOf('?');
const path = qIdx === -1 ? rawUrl : rawUrl.slice(0, qIdx);

Expand All @@ -257,11 +257,11 @@ function onHttp2ServerStreamStart({ stream, headers }) {
// socket check for HTTP/1.1.
span.setAttribute(
'url.scheme',
headers?.[':scheme'] || (stream?.session?.encrypted ? 'https' : 'http'),
headers[':scheme'] || (stream?.session?.encrypted ? 'https' : 'http'),
);
span.setAttribute('network.protocol.version', '2.0');

const authority = headers?.[':authority'];
const authority = headers[':authority'];
if (authority) span.setAttribute('server.address', authority);

// The span is stashed on the stream so the finish and close handlers do
Expand All @@ -281,7 +281,7 @@ function onHttp2ServerStreamStart({ stream, headers }) {
// key/value pairs, and the channel publishes whichever form was used.
function getH2Header(headers, name) {
if (Array.isArray(headers)) return headers[headers.indexOf(name) + 1];
return headers?.[name];
return headers[name];
}

function onHttp2ServerStreamFinish({ stream, headers }) {
Expand Down
Loading
Loading