Skip to content

Commit

Permalink
fix: add fallback, if fsPath results in undefined/ error (AnWeber/htt…
Browse files Browse the repository at this point in the history
  • Loading branch information
AnWeber committed Jan 30, 2022
1 parent 9bbd08e commit 63241b5
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 26 deletions.
11 changes: 4 additions & 7 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
## 4.10.0 (2022-01-27)

#### Features
## Unreleased

- use Host Header as Url Prefix (#189)
- add cookie to userSessionStore instead cookieStore
- `# @loop` allows actions before execution of the loop (e.g. ' # @ref ...`)
- use all dotenv files between httpfile directory and rootDir (#174)
#### Fix

- add fallback, if fsPath results in undefined/ error (AnWeber/httpbook#43)
- Error parsing grpc URL starting with grpc fixed (mistaken for protocol)

## 4.10.0 (2022-01-27)

Expand Down
12 changes: 9 additions & 3 deletions src/cli/initCliProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,18 @@ function initFileProvider(): void {
};
fileProvider.readFile = async (fileName: models.PathLike, encoding: models.FileEncoding): Promise<string> => {
const file = fileProvider.fsPath(fileName);
return fs.readFile(file, encoding);
if (file) {
return fs.readFile(file, encoding);
}
throw new Error('No valid path for cli');
};
fileProvider.readBuffer = async (fileName: models.PathLike) => {
const file = fileProvider.fsPath(fileName);
const stream = createReadStream(file);
return toBuffer(stream);
if (file) {
const stream = createReadStream(file);
return toBuffer(stream);
}
throw new Error('No valid path for cli');
};
fileProvider.writeBuffer = (fileName: models.PathLike, buffer: Buffer) =>
fs.writeFile(fileProvider.toString(fileName), buffer);
Expand Down
2 changes: 1 addition & 1 deletion src/models/fileProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ export interface FileProvider {
writeBuffer(fileName: PathLike, buffer: Buffer): Promise<void>;
readdir: (dirname: PathLike) => Promise<string[]>;
hasExtension(fileName: PathLike, extension: string): boolean;
fsPath(fileName: PathLike): string;
fsPath(fileName: PathLike): string | undefined;
toString(fileName: PathLike): string;
}
18 changes: 14 additions & 4 deletions src/parser/protoHttpRegionParser.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { fileProvider, log, userInteractionProvider } from '../io';
import * as io from '../io';
import * as models from '../models';
import * as utils from '../utils';
import { ParserRegex } from './parserRegex';
Expand Down Expand Up @@ -76,7 +76,17 @@ export class ProtoImportAction implements models.HttpRegionAction {
definition.packageDefinition = await utils.replaceFilePath(
this.protoDefinition.fileName,
context,
(path: models.PathLike) => load(fileProvider.fsPath(path), options)
async (path: models.PathLike) => {
const fsPath = io.fileProvider.fsPath(path);
if (fsPath) {
return await load(fsPath, options);
}
const message = `file ${path} has not scheme file`;
io.userInteractionProvider.showWarnMessage?.(message);
io.log.warn(message);

return undefined;
}
);
if (definition.packageDefinition) {
definition.grpcObject = loadPackageDefinition(definition.packageDefinition);
Expand All @@ -97,8 +107,8 @@ export class ProtoImportAction implements models.HttpRegionAction {
Object.assign(options, await utils.evalExpression(`{${optionsScript}}`, context));
} catch (err) {
const message = `proto-loader options convert failed: ${optionsScript}`;
userInteractionProvider.showWarnMessage?.(message);
log.warn(message, err);
io.userInteractionProvider.showWarnMessage?.(message);
io.log.warn(message, err);
}
return options;
}
Expand Down
15 changes: 8 additions & 7 deletions src/utils/configUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,23 @@ export const defaultConfigFiles = [
];

async function loadFileConfig(rootDir: PathLike): Promise<EnvironmentConfig | undefined> {
let fileConfigPath: PathLike | undefined;
let fileConfigPath: string | undefined;
for (const fileName of defaultConfigFiles) {
const resolvedPath = fileName && fileProvider.joinPath(rootDir, fileName);
if (resolvedPath && (await fileProvider.exists(resolvedPath))) {
fileConfigPath = resolvedPath;
fileConfigPath = fileProvider.fsPath(resolvedPath);
break;
}
}
if (fileConfigPath) {
const fsConfigPath = fileProvider.fsPath(fileConfigPath);
const fsRoot = fileProvider.fsPath(rootDir);
const fileConfig = loadModule<EnvironmentConfig | (() => EnvironmentConfig)>(fsConfigPath, fsRoot, true);
if (typeof fileConfig === 'function') {
return fileConfig();
if (fsRoot) {
const fileConfig = loadModule<EnvironmentConfig | (() => EnvironmentConfig)>(fileConfigPath, fsRoot, true);
if (typeof fileConfig === 'function') {
return fileConfig();
}
return fileConfig;
}
return fileConfig;
}
return undefined;
}
Expand Down
5 changes: 3 additions & 2 deletions src/utils/fsUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,9 @@ export async function findRootDirOfFile(
...files: Array<string>
): Promise<PathLike | undefined> {
let file = filename;
if (!(await fileProvider.isAbsolute(filename)) && workingDir) {
file = fileProvider.joinPath(workingDir, fileProvider.fsPath(filename));
const fsPath = fileProvider.fsPath(filename);
if (!(await fileProvider.isAbsolute(filename)) && workingDir && fsPath) {
file = fileProvider.joinPath(workingDir, fsPath);
}
const dirName = fileProvider.dirname(file);
const dir = await findRootDir(dirName, ...files);
Expand Down
7 changes: 5 additions & 2 deletions src/utils/moduleUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export async function runScript(
deleteVariable?: (key: string) => void;
}
): Promise<Record<string, unknown>> {
const filename = fileProvider.fsPath(options.fileName);
const filename = toModuleFilename(options.fileName);

const mod = createModule(filename);

Expand All @@ -102,7 +102,7 @@ export async function runScript(
log.warn(`requireUncached is deprecated. It can no longer be supported due to esm conversion.`);
const dirName = fileProvider.dirname(filename);
if (dirName) {
clearModule(id, fileProvider.fsPath(dirName));
clearModule(id, toModuleFilename(dirName));
}
return mod.require(id);
},
Expand Down Expand Up @@ -131,6 +131,9 @@ export async function runScript(
}
return result;
}
function toModuleFilename(fileName: PathLike) {
return fileProvider.fsPath(fileName) || fileProvider.toString(fileName);
}

function deleteVariables(contextKeys: string[], context: vm.Context, deleteVariable?: (key: string) => void) {
if (deleteVariable) {
Expand Down

0 comments on commit 63241b5

Please sign in to comment.