Skip to content

Commit

Permalink
Rename constants & do not export them
Browse files Browse the repository at this point in the history
  • Loading branch information
Murderlon committed Sep 15, 2023
1 parent c63e780 commit ebf4d78
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 18 deletions.
1 change: 0 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export {Server} from './lib/server.js'
export * from './lib/constants.js'
10 changes: 5 additions & 5 deletions lib/constants.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/** @constant */
export const DEFAULT_HEADERS = {
/** @type {Readonly<Record<string, string>>} */
export const securityHeaders = {
/**
* Ensures that the page can’t be displayed in a frame,
* regardless of where the request came from.
Expand Down Expand Up @@ -51,7 +51,7 @@ export const DEFAULT_HEADERS = {
*
* @type {Readonly<Map<string, boolean>>}
*/
export const REQUEST_HEADERS = new Map([
export const defaultRequestHeaders = new Map([
['Accept', true],
['Accept-Charset', true],
// No `Accept-Encoding` because images (other than from XML or SVG), don’t
Expand All @@ -74,7 +74,7 @@ export const REQUEST_HEADERS = new Map([
*
* @type {Readonly<Map<string, boolean>>}
*/
export const RESPONSE_HEADERS = new Map([
export const defaultResponseHeaders = new Map([
// `Accept-Ranges` is used by Safari for byte range requests on video:
['Accept-Ranges', true],
['Cache-Control', true],
Expand All @@ -95,7 +95,7 @@ export const RESPONSE_HEADERS = new Map([
*
* @type {ReadonlyArray<string>}
*/
export const IMAGE_MIME_TYPES = [
export const defaultMimeTypes = [
'image/bmp',
'image/cgm',
'image/g3fax',
Expand Down
4 changes: 2 additions & 2 deletions lib/safe-http-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import dns from 'node:dns/promises'
import {fetch} from 'undici'
import ipaddr from 'ipaddr.js'

import {IMAGE_MIME_TYPES} from './constants.js'
import {defaultMimeTypes} from './constants.js'

export class HttpError extends Error {
/**
Expand Down Expand Up @@ -97,7 +97,7 @@ export class SafeHttpClient {
throw new HttpError(400, 'Empty content-type header')
}

if (!IMAGE_MIME_TYPES.includes(contentType)) {
if (!defaultMimeTypes.includes(contentType)) {
throw new HttpError(400, 'Unsupported content-type returned')
}

Expand Down
14 changes: 7 additions & 7 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import {Headers} from 'undici'

import {SafeHttpClient, HttpError} from './safe-http-client.js'
import {
DEFAULT_HEADERS,
REQUEST_HEADERS,
RESPONSE_HEADERS
securityHeaders,
defaultRequestHeaders,
defaultResponseHeaders
} from './constants.js'

/**
Expand Down Expand Up @@ -121,8 +121,8 @@ export class Server extends EventEmitter {

try {
// TODO: respect forwarded headers (check if not private IP)
const filterRequestHeaders = filterHeaders(REQUEST_HEADERS)
const filterResponseHeaders = filterHeaders(RESPONSE_HEADERS)
const filterRequestHeaders = filterHeaders(defaultRequestHeaders)
const filterResponseHeaders = filterHeaders(defaultResponseHeaders)
const client = new SafeHttpClient(this.config.maxSize)
const {buffer, headers: resHeaders} = await client.safeFetch(validUrl, {
// @ts-ignore we can convert req.headers to Headers
Expand All @@ -136,7 +136,7 @@ export class Server extends EventEmitter {
})

const headers = {
...DEFAULT_HEADERS,
...securityHeaders,
...filterResponseHeaders(resHeaders),
Via: this.config.serverName
}
Expand Down Expand Up @@ -193,7 +193,7 @@ export class Server extends EventEmitter {
* @param {Record<string, string | undefined>} [headers]
* @private
*/
write(res, status, body, headers = DEFAULT_HEADERS) {
write(res, status, body, headers = securityHeaders) {
if (status !== 204) {
// @ts-expect-error not explicitly typed but possible
headers['Content-Length'] = Buffer.byteLength(body, 'utf8')
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
"index.js"
],
"scripts": {
"build": "tsc --build --clean && tsc --build",
"build": "npm run build:clean && tsc --build",
"build-clean": "tsc --build --clean",
"format": "remark . -qfo && prettier . -w --log-level warn",
"test": "npm run build && npm run format && npm run test-api",
"test-api": "node --test test.js"
Expand Down
4 changes: 2 additions & 2 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {MockAgent, setGlobalDispatcher, fetch} from 'undici'
// TODO: use published version of this plugin
import {camo} from './rehype-github-image/camo.js'
import {Server} from './index.js'
import {DEFAULT_HEADERS} from './lib/constants.js'
import {securityHeaders} from './lib/constants.js'

const host = '127.0.0.1'
const port = 1080
Expand Down Expand Up @@ -44,7 +44,7 @@ function closeTestServer(server) {

/** @param {import('undici').Response} res */
function testDefaultHeaders(res) {
Object.keys(DEFAULT_HEADERS).forEach((key) => {
Object.keys(securityHeaders).forEach((key) => {
assert.ok(res.headers.has(key), key)
})
}
Expand Down

0 comments on commit ebf4d78

Please sign in to comment.