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

Commit

Permalink
feat: support go provided.al2 runtime (#1559)
Browse files Browse the repository at this point in the history
* feat: support go provided.al2 runtime

* refactor: write into runtimeVersion
  • Loading branch information
Skn0tt authored Sep 6, 2023
1 parent 8788624 commit 2715587
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
3 changes: 3 additions & 0 deletions src/feature_flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ export const defaultFlags = {

// If multiple glob stars are in includedFiles, fail the build instead of warning.
zisi_esbuild_fail_double_glob: false,

// Bundle for the provided.AL2 runtime for Go functions.
zisi_golang_use_al2: false,
} as const

export type FeatureFlags = Partial<Record<keyof typeof defaultFlags, boolean>>
Expand Down
10 changes: 8 additions & 2 deletions src/runtimes/go/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ const zipFunction: ZipFunction = async function ({
srcPath,
stat,
isInternal,
featureFlags,
}) {
const destPath = join(destFolder, filename)
const isSource = extname(mainFile) === '.go'
Expand All @@ -139,13 +140,18 @@ const zipFunction: ZipFunction = async function ({
const zipPath = `${destPath}.zip`
const zipOptions = {
destPath: zipPath,
filename: basename(destPath),
filename: featureFlags.zisi_golang_use_al2 ? 'bootstrap' : basename(destPath),
runtime,
}

await zipBinary({ ...zipOptions, srcPath: binary.path, stat: binary.stat })

return { config, path: zipPath, entryFilename: zipOptions.filename }
return {
config,
path: zipPath,
entryFilename: zipOptions.filename,
runtimeVersion: featureFlags.zisi_golang_use_al2 ? 'provided.al2' : undefined,
}
}

// We don't need to zip the binary, so we can just copy it to the right path.
Expand Down
26 changes: 24 additions & 2 deletions tests/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1740,14 +1740,21 @@ describe('zip-it-and-ship-it', () => {
})

test('Zips Go function binaries if the `zipGo` config property is set', async () => {
const { path: tmpDir } = await getTmpDir({ prefix: 'zip-it-test' })
const manifestPath = join(tmpDir, 'manifest.json')

const fixtureName = 'go-simple'
const { files } = await zipFixture(fixtureName, {
opts: {
manifest: manifestPath,
config: {
'*': {
zipGo: true,
},
},
featureFlags: {
zisi_golang_use_al2: true,
},
},
})
const binaryPath = join(FIXTURES_DIR, fixtureName, 'test')
Expand All @@ -1758,8 +1765,12 @@ describe('zip-it-and-ship-it', () => {
expect(func.runtime).toBe('go')
expect(func.path.endsWith('.zip')).toBe(true)

const manifest = JSON.parse(await readFile(manifestPath, 'utf-8'))

expect(manifest.functions[0].runtimeVersion).toEqual('provided.al2')

const unzippedFunctions = await unzipFiles([func])
const unzippedBinaryPath = join(unzippedFunctions[0].unzipPath, 'test')
const unzippedBinaryPath = join(unzippedFunctions[0].unzipPath, 'bootstrap')

await expect(unzippedBinaryPath).toPathExist()

Expand All @@ -1776,27 +1787,38 @@ describe('zip-it-and-ship-it', () => {
return {} as any
})

const { path: manifestTmpDir } = await getTmpDir({ prefix: 'zip-it-test' })
const manifestPath = join(manifestTmpDir, 'manifest.json')

const fixtureName = 'go-source'
const { files, tmpDir } = await zipFixture(fixtureName, {
opts: {
manifest: manifestPath,
config: {
'*': {
zipGo: true,
},
},
featureFlags: {
zisi_golang_use_al2: true,
},
},
})
const [func] = files

expect(func.runtime).toBe('go')
expect(func.path.endsWith('.zip')).toBe(true)

const manifest = JSON.parse(await readFile(manifestPath, 'utf-8'))

expect(manifest.functions[0].runtimeVersion).toEqual('provided.al2')

// remove the binary before unzipping
await rm(join(tmpDir, 'go-func-1'), { maxRetries: 10 })

const unzippedFunctions = await unzipFiles([func])

const unzippedBinaryPath = join(unzippedFunctions[0].unzipPath, 'go-func-1')
const unzippedBinaryPath = join(unzippedFunctions[0].unzipPath, 'bootstrap')
const unzippedBinaryContents = await readFile(unzippedBinaryPath, 'utf8')

expect(mockSource).toBe(unzippedBinaryContents)
Expand Down

1 comment on commit 2715587

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

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

⏱ Benchmark results

  • largeDepsEsbuild: 2.8s
  • largeDepsNft: 8.7s
  • largeDepsZisi: 16.9s

Please sign in to comment.