All files / shared storage-names.js

100% Statements 8/8
100% Branches 7/7
100% Functions 1/1
100% Lines 7/7

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25                      6x     8x 8x 3x 311x 3x         6x  
/**
 * Expands the compacted storage names of a function's environment, before any
 * other module reads it.
 *
 * Lambda caps a function's environment at 4 KB, and every table name starts
 * with the storage stack's name (`Flows-Storage-Stack-Dev-UsersTable…`): a
 * function with many tables (users/delete-account.js) gets its names without
 * that prefix, marked with STORAGE_NAME_MARKER, and the prefix once in
 * STORAGE_NAME_PREFIX (see lambda-factory.ts's `compactStorageNames`). Every
 * other variable is left as is.
 */
const STORAGE_NAME_MARKER = '~'
 
function expandStorageNames(env = process.env) {
  const prefix = env.STORAGE_NAME_PREFIX
  if (!prefix) return
  for (const [key, value] of Object.entries(env)) {
    if (typeof value === 'string' && value.startsWith(STORAGE_NAME_MARKER)) {
      env[key] = prefix + value.slice(STORAGE_NAME_MARKER.length)
    }
  }
}
 
module.exports = { STORAGE_NAME_MARKER, expandStorageNames }