Skip to content

Commit

Permalink
feat: add debug flag
Browse files Browse the repository at this point in the history
  • Loading branch information
brillout committed Feb 13, 2022
1 parent c324a18 commit c4ed79d
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 18 deletions.
5 changes: 5 additions & 0 deletions telefunc/node/server/runTelefunc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ async function runTelefunc_(httpRequest: { url: string; method: string; body: un
runContext.viteDevServer = null
objectAssign(runContext, globalContext)

{
const logInvalidRequests = !runContext.isProduction || runContext.debug
objectAssign(runContext, {logInvalidRequests })
}

objectAssign(runContext, {
providedContext: getContextOptional() || null,
})
Expand Down
4 changes: 2 additions & 2 deletions telefunc/node/server/runTelefunc/applyShield.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ function applyShield(runContext: {
telefunction: Telefunction
telefunctionName: string
telefunctionArgs: unknown[]
isProduction: boolean
logInvalidRequests: boolean
}) {
const { telefunction } = runContext
const hasShield = !shieldIsMissing(telefunction)
Expand All @@ -20,7 +20,7 @@ function applyShield(runContext: {
if (hasShield) {
const applyResult = shieldApply(telefunction, runContext.telefunctionArgs)
if (applyResult !== true) {
if (!runContext.isProduction) {
if (runContext.logInvalidRequests) {
const errMsg = [
`\`shield()\`: invalid arguments passed to telefunction ${runContext.telefunctionName}.`,
`Arguments: \`${JSON.stringify(runContext.telefunctionArgs)}\`.`,
Expand Down
4 changes: 2 additions & 2 deletions telefunc/node/server/runTelefunc/findTelefunction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ function findTelefunction(runContext: {
telefuncFiles: Record<string, Record<string, unknown>>
telefunctionFileExport: string
telefunctions: Record<string, Telefunction>
isProduction: boolean
logInvalidRequests: boolean
}) {
const telefunctionsFound = Object.keys(runContext.telefunctions)
assertUsage(
Expand All @@ -23,7 +23,7 @@ function findTelefunction(runContext: {

const telefunction = runContext.telefunctions[runContext.telefunctionKey]
if (!telefunction) {
if (!runContext.isProduction) {
if (runContext.logInvalidRequests) {
const errMsg = getNotFoundErrMsg()
console.error(getPluginError(errMsg))
}
Expand Down
28 changes: 14 additions & 14 deletions telefunc/node/server/runTelefunc/parseHttpRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { getTelefunctionName } from './getTelefunctionName'

function parseHttpRequest(runContext: {
httpRequest: { body: unknown; url: string; method: string }
logInvalidRequests: boolean
isProduction: boolean
telefuncUrl: string
}):
Expand All @@ -27,7 +28,7 @@ function parseHttpRequest(runContext: {

const { body } = runContext.httpRequest
if (typeof body !== 'string') {
if (!runContext.isProduction) {
if (runContext.logInvalidRequests) {
assertBody(body, runContext)
} else {
// In production `body` can be any value really.
Expand All @@ -43,12 +44,10 @@ function parseHttpRequest(runContext: {
} catch (err: unknown) {
logParseError(
[
errMsgPrefix,
'The argument `body` passed to `telefunc({ body })`',
'could not be parsed',
`(\`body === '${bodyString}'\`).`,
!hasProp(err, 'message') ? null : `Parse error: ${err.message}.`,
errMsgSuffix,
]
.filter(Boolean)
.join(' '),
Expand All @@ -64,11 +63,9 @@ function parseHttpRequest(runContext: {
) {
logParseError(
[
errMsgPrefix,
'The argument `body` passed to `telefunc({ body })`',
'can be parsed but its content is invalid',
`(\`body === '${bodyString}'\`).`,
errMsgSuffix,
].join(' '),
runContext,
)
Expand Down Expand Up @@ -115,18 +112,20 @@ function assertBody(body: unknown, runContext: { telefuncUrl: string }) {
)
}

function isWrongMethod(runContext: { httpRequest: { method: string }; isProduction: boolean }) {
function isWrongMethod(runContext: {
httpRequest: { method: string }
logInvalidRequests: boolean
isProduction: boolean
}) {
if (['POST', 'post'].includes(runContext.httpRequest.method)) {
return false
}
assert(typeof runContext.httpRequest.method === 'string')
logParseError(
[
errMsgPrefix,
'The argument `method` passed to `telefunc({ method })`',
'should be `POST` (or `post`) but',
`\`method === '${runContext.httpRequest.method}'\`.`,
errMsgSuffix,
].join(' '),
runContext,
)
Expand All @@ -141,15 +140,16 @@ function assertUrl(runContext: { httpRequest: { url: string }; telefuncUrl: stri
)
}

const errMsgPrefix = 'Malformed request in development.'
const errMsgSuffix =
'This is unexpected since, in development, all requests are expected to originate from the Telefunc Client and should therefore be valid. If this error is happening in production, then either the environment variable `NODE_ENV="production"` or `telefunc({ isProduction: true })` is missing.'
function logParseError(errMsg: string, runContext: { isProduction: boolean }) {
function logParseError(errMsg: string, runContext: { isProduction: boolean; logInvalidRequests: boolean }) {
const errMsgPrefix = 'Malformed request in development.'
const errMsgSuffix =
'This is unexpected since, in development, all requests are expected to originate from the Telefunc Client and should therefore be valid. If this error is happening in production, then either the environment variable `NODE_ENV="production"` or `telefunc({ isProduction: true })` is missing.'
assert(errMsg.startsWith(errMsgPrefix))
assert(errMsg.endsWith(errMsgSuffix))
if (!runContext.isProduction) {
errMsg = `${errMsgPrefix} ${errMsg} ${errMsgSuffix}`
}
if (runContext.logInvalidRequests) {
console.error(getPluginError(errMsg))
} else {
// In production any kind of malformed request are expected, when a third party doesn't use the Telefunc Client but uses an HTTP client and makes malformed requests to `_telefunc`, e.g. a HTTP GET request to `_telefunc`.
}
}
12 changes: 12 additions & 0 deletions telefunc/node/server/telefuncConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type TelefuncServerConfig = {
viteDevServer: ViteDevServer | null
disableEtag: boolean
telefuncFiles: Record<string, Record<string, Telefunction>> | null
debug: boolean
}

const configSpec = {
Expand Down Expand Up @@ -81,6 +82,15 @@ const configSpec = {
return false
},
},
debug: {
validate(val: unknown) {
assertUsage(typeof val === 'boolean', '`telefuncConfig.debug` should be a boolean')
},
getDefault() {
if (typeof process == 'undefined' || !hasProp(process, 'env')) return false
return !!process.env.DEBUG
},
},
}

function getTelefuncConfigObject(): TelefuncServerConfig {
Expand All @@ -99,6 +109,8 @@ function getTelefuncConfigObject(): TelefuncServerConfig {
get telefuncUrl() { return configProvidedByUser['telefuncUrl'] ?? configSpec['telefuncUrl'].getDefault() },
// prettier-ignore
get disableEtag() { return configProvidedByUser['disableEtag'] ?? configSpec['disableEtag'].getDefault() },
// prettier-ignore
get debug() { return configProvidedByUser['debug'] ?? configSpec['debug'].getDefault() },
},
{ set },
)
Expand Down

0 comments on commit c4ed79d

Please sign in to comment.