lambder 1.0.3 → 1.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -61,6 +61,7 @@ export default class Resolver {
61
61
  reject: (err: Error) => void;
62
62
  });
63
63
  private readFileSync;
64
+ private checkFileExist;
64
65
  raw(param: ResolverResponse): ResolverResponse;
65
66
  json(data: {
66
67
  [key: string]: any;
@@ -83,7 +84,7 @@ export default class Resolver {
83
84
  }): ResolverResponse;
84
85
  file(filePath: string, headers?: {
85
86
  [key: string]: string | string[];
86
- }): ResolverResponse;
87
+ }, fallbackFilePath?: string): ResolverResponse;
87
88
  api({ payload, error, ...other }: {
88
89
  payload?: any;
89
90
  error?: any;
package/dist/Resolver.js CHANGED
@@ -34,13 +34,21 @@ export default class Resolver {
34
34
  });
35
35
  }
36
36
  readFileSync(filePath) {
37
- const __dirname = pathLibrary.resolve(pathLibrary.dirname(""));
38
- const absolutePath = pathLibrary.resolve(filePath);
39
37
  const publicPath = pathLibrary.resolve(this.publicPath);
38
+ const absolutePath = pathLibrary.resolve(publicPath, filePath);
40
39
  if (!absolutePath.includes(publicPath)) {
41
40
  return "forbiddenpath";
42
41
  }
43
- return fs.readFileSync(filePath);
42
+ return fs.readFileSync(absolutePath);
43
+ }
44
+ ;
45
+ checkFileExist(filePath) {
46
+ const absolutePath = pathLibrary.resolve(filePath);
47
+ const publicPath = pathLibrary.resolve(this.publicPath);
48
+ if (!absolutePath.includes(publicPath)) {
49
+ return false;
50
+ }
51
+ return fs.existsSync(absolutePath);
44
52
  }
45
53
  ;
46
54
  raw(param) {
@@ -110,9 +118,13 @@ export default class Resolver {
110
118
  });
111
119
  }
112
120
  ;
113
- file(filePath, headers) {
114
- if (!fs.existsSync(filePath))
121
+ file(filePath, headers, fallbackFilePath) {
122
+ if (!this.checkFileExist(filePath)) {
123
+ if (fallbackFilePath && this.checkFileExist(fallbackFilePath)) {
124
+ return this.file(fallbackFilePath, headers);
125
+ }
115
126
  return this.json({ error: "File not found: " + filePath });
127
+ }
116
128
  const mimeType = mimeTypeResolver.lookup(filePath);
117
129
  const body = this.readFileSync(filePath);
118
130
  const bodyBase64 = Buffer.from(body).toString("base64");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lambder",
3
- "version": "1.0.3",
3
+ "version": "1.0.4",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
package/src/Resolver.ts CHANGED
@@ -80,11 +80,16 @@ export default class Resolver {
80
80
  }
81
81
 
82
82
  private readFileSync(filePath: string){
83
- const __dirname = pathLibrary.resolve(pathLibrary.dirname(""));
84
- const absolutePath = pathLibrary.resolve(filePath);
85
83
  const publicPath = pathLibrary.resolve(this.publicPath);
84
+ const absolutePath = pathLibrary.resolve(publicPath, filePath);
86
85
  if(!absolutePath.includes(publicPath)){ return "forbiddenpath"; }
87
- return fs.readFileSync(filePath);
86
+ return fs.readFileSync(absolutePath);
87
+ };
88
+ private checkFileExist(filePath: string){
89
+ const absolutePath = pathLibrary.resolve(filePath);
90
+ const publicPath = pathLibrary.resolve(this.publicPath);
91
+ if(!absolutePath.includes(publicPath)){ return false; }
92
+ return fs.existsSync(absolutePath);
88
93
  };
89
94
 
90
95
  raw(param: ResolverResponse){
@@ -174,8 +179,14 @@ export default class Resolver {
174
179
  file(
175
180
  filePath: string,
176
181
  headers?: { [key:string]: string|string[] },
182
+ fallbackFilePath?: string,
177
183
  ):ResolverResponse{
178
- if(!fs.existsSync(filePath)) return this.json({ error: "File not found: " + filePath });
184
+ if(!this.checkFileExist(filePath)){
185
+ if(fallbackFilePath && this.checkFileExist(fallbackFilePath)){
186
+ return this.file(fallbackFilePath, headers);
187
+ }
188
+ return this.json({ error: "File not found: " + filePath })
189
+ }
179
190
  const mimeType = mimeTypeResolver.lookup(filePath);
180
191
  const body = this.readFileSync(filePath);
181
192
  const bodyBase64 = Buffer.from(body).toString("base64");