Skip to content

Commit e95e726

Browse files
rectalogicdkundel
andauthored
fix: fileExists should not require write access by default (#407)
* chore(release): publish %s - [email protected] - @twilio-labs/[email protected] - @twilio-labs/[email protected] - @twilio/[email protected] - @twilio-labs/[email protected] - @twilio-labs/[email protected] - [email protected] * fileExists should not require write access Fixes #406 * chore: override @types/prettier to fix build * chore(runtime-handler): update default twilio to 3.80.0 This removes the vulnerability alerts for @twilio/runtime-handler but might cause some side-effects for the edge case where a customer has specified this version of runtime-handler but has not specified a version of twilio. In this case the local and deployed behavior might vary depending on the APIs used by the customer in Functions. * chore(twilio-run): update got version * chore(serverless-api): update got & file-type * fix(serverless-api): change file-type package usage * fix(twilio-run): replace listr and fix got usage * chore(serverless-api): upgrade typescript version * test(serverless-api): temporarily turn off tests that use file-type * chore(serverless-api): turn off typedoc for now * chore: remove Node 12 from Actions flow * chore(release): publish %s - [email protected] - @twilio-labs/[email protected] - @twilio-labs/[email protected] - @twilio/[email protected] - @twilio-labs/[email protected] - [email protected] * fileExists should not require write access Fixes #406 * fix: add option for write access check Co-authored-by: Dominik Kundel <[email protected]>
1 parent dba7ddb commit e95e726

File tree

5 files changed

+28
-13
lines changed

5 files changed

+28
-13
lines changed

packages/serverless-api/src/utils/fs.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,22 @@ export const writeFile = promisify(fs.writeFile);
2222
export const readDir = promisify(recursiveReadDir);
2323
export const stat = promisify(fs.stat);
2424

25+
const READ_ONLY = fs.constants.R_OK;
26+
const READ_WRITE = fs.constants.R_OK | fs.constants.W_OK;
27+
2528
/**
2629
* Checks if a given file exists by checking if we have read & write access
2730
*
2831
* @export
2932
* @param {string} filePath full path of the file to check
3033
* @returns
3134
*/
32-
export async function fileExists(filePath: string) {
35+
export async function fileExists(
36+
filePath: string,
37+
hasWriteAccess: boolean = false
38+
) {
3339
try {
34-
await access(filePath, fs.constants.R_OK | fs.constants.W_OK);
40+
await access(filePath, hasWriteAccess ? READ_WRITE : READ_ONLY);
3541
return true;
3642
} catch (err) {
3743
return false;
@@ -168,7 +174,7 @@ export async function getDirContent(
168174
);
169175

170176
return unfilteredFiles.filter(
171-
entry => typeof entry !== 'undefined'
177+
(entry) => typeof entry !== 'undefined'
172178
) as FileInfo[];
173179
}
174180

@@ -283,7 +289,7 @@ async function getServerlessConfigs(
283289
ignoreExtension?: string
284290
): Promise<ServerlessResourceConfigWithFilePath[]> {
285291
return Promise.all(
286-
dirContent.map(async file => {
292+
dirContent.map(async (file) => {
287293
const { path, access } = getPathAndAccessFromFileInfo(
288294
file,
289295
ignoreExtension

packages/twilio-run/src/templating/defaultConfig.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ export async function writeDefaultConfigFile(
6363
): Promise<boolean> {
6464
const fullConfigFilePath = path.resolve(baseDir, fileName);
6565

66-
const configFileExists = await fileExists(fullConfigFilePath);
66+
const configFileExists = await fileExists(fullConfigFilePath, true);
6767

6868
if (configFileExists && !overrideExisting) {
6969
return false;

packages/twilio-run/src/templating/filesystem.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ async function writeEnvFile(
2525
newEnvironmentVariableKeys: string[];
2626
}> {
2727
const envFilePath = path.join(targetDir, '.env');
28-
const envFileExists = await fileExists(envFilePath);
28+
const envFileExists = await fileExists(envFilePath, true);
2929
if (!envFileExists) {
3030
await downloadFile(contentUrl, envFilePath);
3131
return { newEnvironmentVariableKeys: [] };

packages/twilio-run/src/utils/deployInfoCache.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export function getDeployInfoCache(
3838
deployInfoCacheFileName: string = '.twiliodeployinfo'
3939
): DeployInfoCache {
4040
const fullPath = path.resolve(baseDir, deployInfoCacheFileName);
41-
const deployCacheInfoExists = fileExistsSync(fullPath);
41+
const deployCacheInfoExists = fileExistsSync(fullPath, true);
4242

4343
if (deployCacheInfoExists) {
4444
debug('Found deploy info cache at "%s"', fullPath);

packages/twilio-run/src/utils/fs.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,27 @@ export const mkdir = promisify(fs.mkdir);
1111
const stat = promisify(fs.stat);
1212
const open = promisify(fs.open);
1313

14-
export async function fileExists(filePath: string): Promise<boolean> {
14+
const READ_ONLY = fs.constants.R_OK;
15+
const READ_WRITE = fs.constants.R_OK | fs.constants.W_OK;
16+
17+
export async function fileExists(
18+
filePath: string,
19+
hasWriteAccess: boolean = false
20+
): Promise<boolean> {
1521
try {
16-
await access(filePath, fs.constants.R_OK | fs.constants.W_OK);
22+
await access(filePath, hasWriteAccess ? READ_WRITE : READ_ONLY);
1723
return true;
1824
} catch (err) {
1925
return false;
2026
}
2127
}
2228

23-
export function fileExistsSync(filePath: string): boolean {
29+
export function fileExistsSync(
30+
filePath: string,
31+
hasWriteAccess: boolean = false
32+
): boolean {
2433
try {
25-
fs.accessSync(filePath, fs.constants.R_OK | fs.constants.W_OK);
34+
fs.accessSync(filePath, hasWriteAccess ? READ_WRITE : READ_ONLY);
2635
return true;
2736
} catch (err) {
2837
return false;
@@ -36,15 +45,15 @@ export function downloadFile(
3645
return new Promise((resolve, reject) => {
3746
return mkdir(path.dirname(targetPath), { recursive: true })
3847
.then(() => open(targetPath, 'wx'))
39-
.then(fd => {
48+
.then((fd) => {
4049
const writeStream = fs.createWriteStream('', { fd });
4150
got
4251
.stream(contentUrl)
4352
.on('response', resolve)
4453
.on('error', reject)
4554
.pipe(writeStream);
4655
})
47-
.catch(err => reject(err));
56+
.catch((err) => reject(err));
4857
});
4958
}
5059

0 commit comments

Comments
 (0)