Skip to content

Commit

Permalink
refactor: provide via "systemLog" function
Browse files Browse the repository at this point in the history
  • Loading branch information
Skn0tt committed Nov 17, 2023
1 parent 1610ab4 commit bbcad09
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 20 deletions.
13 changes: 12 additions & 1 deletion packages/build/src/plugins/child/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { getNewEnvChanges, setEnvChanges } from '../../env/changes.js'
import { logPluginMethodStart, logPluginMethodEnd } from '../../log/messages/ipc.js'

import { cloneNetlifyConfig, getConfigMutations } from './diff.js'
import { getSystemLog } from './systemLog.js'
import { getUtils } from './utils.js'

// Run a specific plugin event handler
Expand All @@ -11,9 +12,19 @@ export const run = async function (
) {
const method = methods[event]
const runState = {}
const systemLog = getSystemLog()
const utils = getUtils({ event, constants, runState })
const netlifyConfigCopy = cloneNetlifyConfig(netlifyConfig)
const runOptions = { utils, constants, inputs, netlifyConfig: netlifyConfigCopy, packageJson, error, featureFlags }
const runOptions = {
utils,
constants,
inputs,
netlifyConfig: netlifyConfigCopy,
packageJson,
error,
featureFlags,
systemLog,
}

const envBefore = setEnvChanges(envChanges)

Expand Down
15 changes: 15 additions & 0 deletions packages/build/src/plugins/child/systemLog.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { appendFileSync, openSync } from 'fs'

const systemLogLocation = '/dev/fd/4'

export const getSystemLog = () => {
try {
// throws if system log wasn't hooked up
const fd = openSync(systemLogLocation, 'a')
return (message) => {
appendFileSync(fd, `${message}\n`)
}
} catch {
return
}
}
8 changes: 2 additions & 6 deletions packages/build/tests/plugins/fixtures/feature_flags/plugin.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import { appendFileSync } from "fs"

const systemLog = "/dev/fd/4" // 🪄 magic ✨

export const onBuild = function ({ featureFlags }) {
appendFileSync(systemLog, "some system-facing logs\n")
export const onBuild = function ({ featureFlags, systemLog }) {
systemLog("some system-facing logs")
console.log(JSON.stringify(featureFlags))
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,5 @@
import { appendFileSync } from "fs"

const isSystemLogAccessible = () => {
try {
appendFileSync("/dev/fd/4", "some system-facing logs")
return true
} catch (error) {
return false
}
}

export const onBuild = function ({ featureFlags }) {
if (isSystemLogAccessible()) {
export const onBuild = function ({ featureFlags, systemLog }) {
if (systemLog) {
throw new Error("System log shouldn't be acessible from untrusted plugins")
}
console.log("typeof featureflags:", typeof featureFlags)
Expand Down

0 comments on commit bbcad09

Please sign in to comment.