Skip to content

Commit

Permalink
Cache S3_VERSION_ID_ENCODING_TYPE env variable
Browse files Browse the repository at this point in the history
Reading env variables is costly, so do it only once on startup.

Issue: ARSN-442
  • Loading branch information
francoisferrand committed Jan 11, 2025
1 parent 435dc7f commit 81b26ea
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
4 changes: 3 additions & 1 deletion lib/versioning/VersionID.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ const TEMPLATE_TS = new Array(LENGTH_TS + 1).join('0');
const TEMPLATE_SEQ = new Array(LENGTH_SEQ + 1).join('0');
const TEMPLATE_RG = new Array(LENGTH_RG + 1).join(' ');

export let S3_VERSION_ID_ENCODING_TYPE = process.env.S3_VERSION_ID_ENCODING_TYPE;

/**
* Left-pad a string representation of a value with a given template.
* For example: pad('foo', '00000') gives '00foo'.
Expand Down Expand Up @@ -121,7 +123,7 @@ export function generateVersionId(info: string, replicationGroupId: string): str
lastTimestamp = ts;

// if S3_VERSION_ID_ENCODING_TYPE is "hex", info is used.
if (process.env.S3_VERSION_ID_ENCODING_TYPE === 'hex' || !process.env.S3_VERSION_ID_ENCODING_TYPE) {
if (S3_VERSION_ID_HEX_ENCODING === 'hex' || !S3_VERSION_ID_ENCODING_TYPE) {
// info field stays as is
} else {
info = ''; // eslint-disable-line
Expand Down
5 changes: 2 additions & 3 deletions tests/unit/versioning/VersionID.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const VID = require('../../../lib/versioning/VersionID');
const assert = require('assert');
const { env } = require('process');

function randkey(length) {
let key = '';
Expand Down Expand Up @@ -34,7 +33,7 @@ describe('test generating versionIds', () => {
});
});
describe('legaxy hex encoding', () => {
env.S3_VERSION_ID_ENCODING_TYPE = 'hex';
VID.S3_VERSION_ID_ENCODING_TYPE = 'hex';
const vids = generateRandomVIDs(count);

it('sorted in reversed chronological and alphabetical order', () => {
Expand Down Expand Up @@ -69,7 +68,7 @@ describe('test generating versionIds', () => {


describe('Short IDs', () => {
env.S3_VERSION_ID_ENCODING_TYPE = 'base62';
VID.S3_VERSION_ID_ENCODING_TYPE = 'base62';
const vids = generateRandomVIDs(count);

it('sorted in reversed chronological and alphabetical order', () => {
Expand Down

0 comments on commit 81b26ea

Please sign in to comment.