Skip to content

Commit

Permalink
Merge pull request #32856 from github/repo-sync
Browse files Browse the repository at this point in the history
Repo sync
  • Loading branch information
docs-bot authored May 8, 2024
2 parents d35d0e4 + 654c13a commit 2cb46e3
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 10 deletions.
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@
"hast-util-from-parse5": "^8.0.1",
"hast-util-to-string": "^2.0.0",
"hastscript": "^9.0.0",
"helmet": "^7.0.0",
"helmet": "^7.1.0",
"highlight.js": "11.9.0",
"highlightjs-curl": "^1.3.0",
"hot-shots": "^10.0.0",
Expand Down
19 changes: 15 additions & 4 deletions src/frame/middleware/abort.js → src/frame/middleware/abort.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
import type { NextFunction, Response } from 'express'

import statsd from '#src/observability/lib/statsd.js'
import { ExtendedRequest } from '@/types'

class AbortError extends Error {
statusCode: number
code: string
constructor(message: string, statusCode: number, code: string) {
super(message)
this.statusCode = statusCode
this.code = code
}
}

export default function abort(req, res, next) {
export default function abort(req: ExtendedRequest, res: Response, next: NextFunction) {
// If the client aborts the connection, send an error
req.once('aborted', () => {
// ignore aborts from next, usually has to do with webpack-hmr
Expand All @@ -21,9 +34,7 @@ export default function abort(req, res, next) {
}
statsd.increment('middleware.abort', 1, incrementTags)

const abortError = new Error('Client closed request')
abortError.statusCode = 499
abortError.code = 'ECONNRESET'
const abortError = new AbortError('Client closed request', 499, 'ECONNRESET')

// Pass the error to the Express error handler
return next(abortError)
Expand Down
2 changes: 1 addition & 1 deletion src/frame/middleware/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import express from 'express'
import type { NextFunction, Request, Response, Express } from 'express'

import haltOnDroppedConnection from './halt-on-dropped-connection.js'
import abort from './abort.js'
import abort from './abort'
import timeout from './timeout.js'
import morgan from 'morgan'
import datadog from '@/observability/middleware/connect-datadog.js'
Expand Down
13 changes: 13 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import type { Request } from 'express'

// Throughout our codebase we "extend" the Request object by attaching
// things to it. For example `req.context = { currentCategory: 'foo' }`.
// This type aims to match all the custom things we do to requests
// througout the codebase.
export type ExtendedRequest = Request & {
pagePath?: string
context?: {
currentCategory?: string
}
// Add more properties here as needed
}

0 comments on commit 2cb46e3

Please sign in to comment.