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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow proxy rules logs to be hidden when inserted by plugin #6453

Merged
merged 6 commits into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 1 addition & 1 deletion src/commands/dev/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ export const dev = async (options: OptionValues, command: BaseCommand) => {
},
})

const mutatedConfig = applyMutations(config, configMutations)
const mutatedConfig: typeof config = applyMutations(config, configMutations)

const functionsRegistry = await startFunctionsServer({
blobsContext,
Expand Down
1 change: 0 additions & 1 deletion src/commands/serve/serve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,6 @@ export const serve = async (options: OptionValues, command: BaseCommand) => {
}

const inspectSettings = generateInspectSettings(options.edgeInspect, options.edgeInspectBrk)
// @ts-expect-error TS(2345) FIXME: Argument of type '{ addonsUrls: { [k: string]: any... Remove this comment to see the full error message
const url = await startProxyServer({
addonsUrls,
command,
Expand Down
6 changes: 4 additions & 2 deletions src/lib/images/proxy.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { IncomingMessage } from 'http'

import { NetlifyConfig } from '@netlify/build'
import express from 'express'
import { createIPX, ipxFSStorage, ipxHttpStorage, createIPXNodeServer } from 'ipx'
Expand Down Expand Up @@ -83,8 +85,8 @@ export const parseRemoteImages = async function ({ config }) {
return remotePatterns
}

export const isImageRequest = function (req: Request): boolean {
return req.url.startsWith(IMAGE_URL_PATTERN)
export const isImageRequest = function (req: IncomingMessage): boolean {
return req.url?.startsWith(IMAGE_URL_PATTERN) ?? false
}

export const transformImageParams = function (query: QueryParams): string {
Expand Down
22 changes: 8 additions & 14 deletions src/utils/create-stream-promise.ts
Original file line number Diff line number Diff line change
@@ -1,56 +1,50 @@
import { Buffer } from 'buffer'
import { IncomingMessage } from 'http'

const SEC_TO_MILLISEC = 1e3

// 6 MiB
const DEFAULT_BYTES_LIMIT = 6e6

// @ts-expect-error TS(7006) FIXME: Parameter 'stream' implicitly has an 'any' type.
const createStreamPromise = function (stream, timeoutSeconds, bytesLimit = DEFAULT_BYTES_LIMIT) {
const createStreamPromise = function (
stream: IncomingMessage,
timeoutSeconds: number,
bytesLimit = DEFAULT_BYTES_LIMIT,
): Promise<Buffer> {
return new Promise(function streamPromiseFunc(resolve, reject) {
// @ts-expect-error TS(7034) FIXME: Variable 'data' implicitly has type 'any[]' in som... Remove this comment to see the full error message
let data = []
let data: unknown[] | null = []
let dataLength = 0

// @ts-expect-error TS(7034) FIXME: Variable 'timeoutId' implicitly has type 'any' in ... Remove this comment to see the full error message
let timeoutId = null
let timeoutId: NodeJS.Timeout = null
if (timeoutSeconds != null && Number.isFinite(timeoutSeconds)) {
timeoutId = setTimeout(() => {
// @ts-expect-error TS(2322) FIXME: Type 'null' is not assignable to type 'any[]'.
data = null
reject(new Error('Request timed out waiting for body'))
}, timeoutSeconds * SEC_TO_MILLISEC)
}

// @ts-expect-error TS(7006) FIXME: Parameter 'chunk' implicitly has an 'any' type.
stream.on('data', function onData(chunk) {
// @ts-expect-error TS(7005) FIXME: Variable 'data' implicitly has an 'any[]' type.
if (!Array.isArray(data)) {
// Stream harvesting closed
return
}
dataLength += chunk.length
if (dataLength > bytesLimit) {
// @ts-expect-error TS(2322) FIXME: Type 'null' is not assignable to type 'any[]'.
data = null
reject(new Error('Stream body too big'))
} else {
data.push(chunk)
}
})

// @ts-expect-error TS(7006) FIXME: Parameter 'error' implicitly has an 'any' type.
stream.on('error', function onError(error) {
// @ts-expect-error TS(2322) FIXME: Type 'null' is not assignable to type 'any[]'.
data = null
reject(error)
// @ts-expect-error TS(7005) FIXME: Variable 'timeoutId' implicitly has an 'any' type.
clearTimeout(timeoutId)
})
stream.on('end', function onEnd() {
// @ts-expect-error TS(7005) FIXME: Variable 'timeoutId' implicitly has an 'any' type.
clearTimeout(timeoutId)
// @ts-expect-error TS(7005) FIXME: Variable 'data' implicitly has an 'any[]' type.
if (data) {
// @ts-expect-error TS(7005) FIXME: Variable 'data' implicitly has an 'any[]' type.
resolve(Buffer.concat(data))
Expand Down
98 changes: 41 additions & 57 deletions src/utils/proxy-server.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
import BaseCommand from '../commands/base-command.js'
import { $TSFixMe, NetlifyOptions } from '../commands/types.js'
import { BlobsContext } from '../lib/blobs/blobs.js'
import { FunctionsRegistry } from '../lib/functions/registry.js'

import { exit, log, NETLIFYDEVERR } from './command-helpers.js'
import { startProxy } from './proxy.js'
import type StateConfig from './state-config.js'
import { ServerSettings } from './types.js'

/**
* @typedef {Object} InspectSettings
* @property {boolean} enabled - Inspect enabled
* @property {boolean} pause - Pause on breakpoints
* @property {string|undefined} address - Host/port override (optional)
*/
interface InspectSettings {
// Inspect enabled
enabled: boolean
// Pause on breakpoints
pause: boolean
// Host/port override (optional)
address?: string
}

/**
* @param {boolean|string} edgeInspect
* @param {boolean|string} edgeInspectBrk
* @returns {InspectSettings}
*/
// @ts-expect-error TS(7006) FIXME: Parameter 'edgeInspect' implicitly has an 'any' ty... Remove this comment to see the full error message
export const generateInspectSettings = (edgeInspect, edgeInspectBrk) => {
export const generateInspectSettings = (
edgeInspect: boolean | string,
edgeInspectBrk: boolean | string,
): InspectSettings => {
const enabled = Boolean(edgeInspect) || Boolean(edgeInspectBrk)
const pause = Boolean(edgeInspectBrk)
const getAddress = () => {
Expand All @@ -33,71 +39,49 @@ export const generateInspectSettings = (edgeInspect, edgeInspectBrk) => {
}
}

/**
*
* @param {object} params
* @param {string=} params.accountId
* @param {*} params.addonsUrls
* @param {import("../lib/blobs/blobs.js").BlobsContext} blobsContext
* @param {import('../commands/types.js').NetlifyOptions["config"]} params.config
* @param {string} [params.configPath] An override for the Netlify config path
* @param {boolean} params.debug
* @param {import('../commands/types.js').NetlifyOptions["cachedConfig"]['env']} params.env
* @param {InspectSettings} params.inspectSettings
* @param {() => Promise<object>} params.getUpdatedConfig
* @param {string} params.geolocationMode
* @param {string} params.geoCountry
* @param {*} params.settings
* @param {boolean} params.offline
* @param {object} params.site
* @param {*} params.siteInfo
* @param {string} params.projectDir
* @param {string} params.repositoryRoot
* @param {import('./state-config.js').default} params.state
* @param {import('../lib/functions/registry.js').FunctionsRegistry=} params.functionsRegistry
* @returns
*/
export const startProxyServer = async ({
// @ts-expect-error TS(7031) FIXME: Binding element 'accountId' implicitly has an 'any... Remove this comment to see the full error message
accountId,
// @ts-expect-error TS(7031) FIXME: Binding element 'addonsUrls' implicitly has an 'an... Remove this comment to see the full error message
addonsUrls,
// @ts-expect-error TS(7031) FIXME: Binding element 'blobsContext' implicitly has an '... Remove this comment to see the full error message
blobsContext,
// @ts-expect-error TS(7031) FIXME: Binding element 'accountId' implicitly has an 'any... Remove this comment to see the full error message
command,
// @ts-expect-error TS(7031) FIXME: Binding element 'config' implicitly has an 'any' t... Remove this comment to see the full error message
config,
// @ts-expect-error TS(7031) FIXME: Binding element 'configPath' implicitly has an 'an... Remove this comment to see the full error message
configPath,
// @ts-expect-error TS(7031) FIXME: Binding element 'debug' implicitly has an 'any' ty... Remove this comment to see the full error message
debug,
// @ts-expect-error TS(7031) FIXME: Binding element 'env' implicitly has an 'any' type... Remove this comment to see the full error message
env,
// @ts-expect-error TS(7031) FIXME: Binding element 'functionsRegistry' implicitly has... Remove this comment to see the full error message
functionsRegistry,
// @ts-expect-error TS(7031) FIXME: Binding element 'geoCountry' implicitly has an 'an... Remove this comment to see the full error message
geoCountry,
// @ts-expect-error TS(7031) FIXME: Binding element 'geolocationMode' implicitly has a... Remove this comment to see the full error message
geolocationMode,
// @ts-expect-error TS(7031) FIXME: Binding element 'getUpdatedConfig' implicitly has ... Remove this comment to see the full error message
getUpdatedConfig,
// @ts-expect-error TS(7031) FIXME: Binding element 'inspectSettings' implicitly has a... Remove this comment to see the full error message
inspectSettings,
// @ts-expect-error TS(7031) FIXME: Binding element 'offline' implicitly has an 'any' ... Remove this comment to see the full error message
offline,
// @ts-expect-error TS(7031) FIXME: Binding element 'projectDir' implicitly has an 'an... Remove this comment to see the full error message
projectDir,
// @ts-expect-error TS(7031) FIXME: Binding element 'repositoryRoot' implicitly has an... Remove this comment to see the full error message
repositoryRoot,
// @ts-expect-error TS(7031) FIXME: Binding element 'settings' implicitly has an 'any'... Remove this comment to see the full error message
settings,
// @ts-expect-error TS(7031) FIXME: Binding element 'site' implicitly has an 'any' typ... Remove this comment to see the full error message
site,
// @ts-expect-error TS(7031) FIXME: Binding element 'siteInfo' implicitly has an 'any'... Remove this comment to see the full error message
siteInfo,
// @ts-expect-error TS(7031) FIXME: Binding element 'state' implicitly has an 'any' ty... Remove this comment to see the full error message
state,
}: {
accountId: string
addonsUrls: $TSFixMe
blobsContext?: BlobsContext
command: BaseCommand
config: NetlifyOptions['config']
// An override for the Netlify config path
configPath?: string
debug: boolean
env: NetlifyOptions['cachedConfig']['env']
inspectSettings: InspectSettings
getUpdatedConfig: () => Promise<object>
geolocationMode: string
geoCountry: string
settings: ServerSettings
offline: boolean
site: $TSFixMe
siteInfo: $TSFixMe
projectDir: string
repositoryRoot?: string
state: StateConfig
functionsRegistry?: FunctionsRegistry
}) => {
const url = await startProxy({
addonsUrls,
Expand Down