Skip to content
This repository has been archived by the owner on May 22, 2024. It is now read-only.

feat: detect invalid binaries and show warning #1124

Merged
merged 4 commits into from
Jul 4, 2022
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
22 changes: 11 additions & 11 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 @@ -53,13 +53,13 @@
},
"dependencies": {
"@babel/parser": "7.16.8",
"@netlify/binary-info": "^1.0.0",
"@netlify/esbuild": "0.14.25",
"@vercel/nft": "^0.20.0",
"archiver": "^5.3.0",
"common-path-prefix": "^3.0.0",
"cp-file": "^9.0.0",
"del": "^6.0.0",
"elf-cam": "^0.1.1",
"end-of-stream": "^1.4.4",
"es-module-lexer": "^0.10.0",
"execa": "^5.0.0",
Expand Down
6 changes: 6 additions & 0 deletions src/bin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,19 @@ import { hideBin } from 'yargs/helpers'
import type { ArchiveFormat } from './archive.js'
import { zipFunctions } from './main.js'

declare global {
// eslint-disable-next-line no-var
var ZISI_CLI: boolean
}

// CLI entry point
const runCli = async function () {
// @ts-expect-error TODO: `destFolder` and `srcFolder` are not being passed
// back from `parseArgs()`.
const { destFolder, srcFolder, ...options } = parseArgs()

try {
global.ZISI_CLI = true
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does this need to be a global?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not necessarily, but the alternative would be to pass the flag through the complete function hierarchy. And as the binary of zisi seems it might be deprecated and removed at some point, this was by far the more straightforward solution.

// @ts-expect-error TODO: `options` is not getting the right types.
const zipped = await zipFunctions(srcFolder, destFolder, options)

Expand Down
28 changes: 24 additions & 4 deletions src/runtimes/detect_runtime.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,25 @@
import type { Buffer } from 'buffer'

import { detect, Runtime } from 'elf-cam'
import { detect, Runtime, Arch, Platform, BinaryInfo } from '@netlify/binary-info'

import { cachedReadFile, FsCache } from '../utils/fs.js'

import type { RuntimeName } from './runtime.js'

const isValidFunctionBinary = (info: BinaryInfo) => info.arch === Arch.Amd64 && info.platform === Platform.Linux

const warnIncompatibleBinary = function (path: string, binaryInfo: BinaryInfo): undefined {
if (!global.ZISI_CLI) {
console.warn(`
Found incompatible prebuilt function binary in ${path}.
The binary needs to be built for Linux/Amd64, but it was built for ${Platform[binaryInfo.platform]}/${
Arch[binaryInfo.arch]
}`)
}

return undefined
}

// Try to guess the runtime by inspecting the binary file.
export const detectBinaryRuntime = async function ({
fsCache,
Expand All @@ -20,15 +34,21 @@ export const detectBinaryRuntime = async function ({
// We're using the Type Assertion because the `cachedReadFile` abstraction
// loses part of the return type information. We can safely say it's a
// Buffer in this case because we're not specifying an encoding.
const binaryType = detect(buffer as Buffer)
const binaryInfo = detect(buffer as Buffer)

if (!isValidFunctionBinary(binaryInfo)) {
return warnIncompatibleBinary(path, binaryInfo)
}

switch (binaryType) {
switch (binaryInfo.runtime) {
case Runtime.Go:
return 'go'
case Runtime.Rust:
return 'rs'
default:
return undefined
}
} catch {}
} catch {
// Possible errors are: non binary files, arch/platforms not supported by binary-info, path is directory
}
}
Binary file not shown.
11 changes: 11 additions & 0 deletions tests/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -2708,3 +2708,14 @@ test('listFunctionsFiles includes in-source config declarations', async (t) => {
t.is(func.schedule, '@daily')
})
})

test('listFunctionsFiles does not include wrong arch functions and warns', async (t) => {
sinon.spy(console, 'warn')
const functions = await listFunctionsFiles(join(FIXTURES_DIR, 'wrong-prebuilt-architecture'))

t.is(functions.length, 0)
t.is(console.warn.called, true)
t.is(console.warn.calledWith(sinon.match(/Darwin\/Arm64/)), true)

console.warn.restore()
})