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

Commit

Permalink
feat: add size property to output (#672)
Browse files Browse the repository at this point in the history
* feat: add `size` property to output

* chore: add `size` to README

* refactor: replace lstat with stat

* chore: add test

* refactor: move formatZipResult to a separate file

* refactor: move `addArchiveSize` to separate file
  • Loading branch information
eduardoboucas committed Oct 14, 2021
1 parent 89d11ca commit c4b41ab
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 36 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,10 @@ properties.

Either `"js"`, `"go"`, or `"rs"`.

- `size`: `number`

The size of the generated archive, in bytes.

Additionally, the following properties also exist for Node.js functions:

- `bundler`: `string`
Expand Down
19 changes: 19 additions & 0 deletions src/utils/archive_size.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const { extname } = require('path')

const { stat } = require('./fs')

// Returns the input object with an additional `size` property containing the
// size of the file at `path` when it is a ZIP archive.
const addArchiveSize = async (result) => {
const { path } = result

if (extname(path) !== '.zip') {
return result
}

const { size } = await stat(path)

return { ...result, size }
}

module.exports = { addArchiveSize }
36 changes: 36 additions & 0 deletions src/utils/format_result.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const { removeFalsy } = require('./remove_falsy')

// Takes the result of zipping a function and formats it for output.
const formatZipResult = (result) => {
const {
bundler,
bundlerErrors,
bundlerWarnings,
config = {},
inputs,
mainFile,
name,
nativeNodeModules,
nodeModulesWithDynamicImports,
path,
runtime,
size,
} = result

return removeFalsy({
bundler,
bundlerErrors,
bundlerWarnings,
config,
inputs,
mainFile,
name,
nativeNodeModules,
nodeModulesWithDynamicImports,
path,
runtime: runtime.name,
size,
})
}

module.exports = { formatZipResult }
4 changes: 3 additions & 1 deletion src/utils/fs.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
const { lstat, readdir, readFile, unlink, writeFile } = require('fs')
const { lstat, readdir, readFile, stat, unlink, writeFile } = require('fs')
const { format, join, parse, resolve } = require('path')
const { promisify } = require('util')

const pLstat = promisify(lstat)
const pReaddir = promisify(readdir)
const pReadFile = promisify(readFile)
const pStat = promisify(stat)
const pUnlink = promisify(unlink)
const pWriteFile = promisify(writeFile)

Expand Down Expand Up @@ -84,5 +85,6 @@ module.exports = {
listFunctionsDirectory,
resolveFunctionsDirectories,
safeUnlink,
stat: pStat,
writeFile: pWriteFile,
}
46 changes: 11 additions & 35 deletions src/zip.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ const { getFlags } = require('./feature_flags')
const { createManifest } = require('./manifest')
const { getFunctionsFromPaths } = require('./runtimes')
const { getPluginsModulesPath } = require('./runtimes/node/utils/plugin_modules_path')
const { addArchiveSize } = require('./utils/archive_size')
const { ARCHIVE_FORMAT_NONE, ARCHIVE_FORMAT_ZIP } = require('./utils/consts')
const { formatZipResult } = require('./utils/format_result')
const { listFunctionsDirectories, resolveFunctionsDirectories } = require('./utils/fs')
const { removeFalsy } = require('./utils/remove_falsy')

const DEFAULT_PARALLEL_LIMIT = 5

Expand All @@ -19,37 +20,6 @@ const validateArchiveFormat = (archiveFormat) => {
}
}

// Takes the result of zipping a function and formats it for output.
const formatZipResult = (result) => {
const {
bundler,
bundlerErrors,
bundlerWarnings,
config = {},
inputs,
mainFile,
name,
nativeNodeModules,
nodeModulesWithDynamicImports,
path,
runtime,
} = result

return removeFalsy({
bundler,
bundlerErrors,
bundlerWarnings,
config,
inputs,
mainFile,
name,
nativeNodeModules,
nodeModulesWithDynamicImports,
path,
runtime: runtime.name,
})
}

// Zip `srcFolder/*` (Node.js or Go files) to `destFolder/*.zip` so it can be
// used by AWS Lambda
const zipFunctions = async function (
Expand Down Expand Up @@ -105,7 +75,13 @@ const zipFunctions = async function (
concurrency: parallelLimit,
},
)
const formattedResults = results.filter(Boolean).map(formatZipResult)
const formattedResults = await Promise.all(
results.filter(Boolean).map(async (result) => {
const resultWithSize = await addArchiveSize(result)

return formatZipResult(resultWithSize)
}),
)

if (manifest !== undefined) {
await createManifest({ functions: formattedResults, path: resolve(manifest) })
Expand Down Expand Up @@ -136,7 +112,7 @@ const zipFunction = async function (
return
}

const { config, extension, filename, mainFile, name, runtime, srcDir, stat } = functions.values().next().value
const { config, extension, filename, mainFile, name, runtime, srcDir, stat: stats } = functions.values().next().value
const pluginsModulesPath =
defaultModulesPath === undefined ? await getPluginsModulesPath(srcPath) : defaultModulesPath

Expand All @@ -156,7 +132,7 @@ const zipFunction = async function (
runtime,
srcDir,
srcPath,
stat,
stat: stats,
})

return formatZipResult({ ...zipResult, mainFile, name, runtime })
Expand Down
14 changes: 14 additions & 0 deletions tests/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -2186,3 +2186,17 @@ testMany(
t.true(mock)
},
)

testMany(
'Returns a `size` property with the size of each generated archive',
['bundler_default', 'bundler_esbuild', 'bundler_nft'],
async (options, t) => {
const FUNCTIONS_COUNT = 6
const { files } = await zipNode(t, 'many-functions', {
length: FUNCTIONS_COUNT,
opts: options,
})

files.every(({ size }) => Number.isInteger(size) && size > 0)
},
)

1 comment on commit c4b41ab

@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: 11s

largeDepsNft: 43.8s

largeDepsZisi: 1m 3.3s

Please sign in to comment.