lambder 1.0.3 → 1.0.5

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/dist/index.js CHANGED
@@ -63,13 +63,13 @@ export default class Lambder {
63
63
  addApi(condition, actionFn) {
64
64
  let conditionFn = (renderContext) => false;
65
65
  if (typeof condition === "string") {
66
- conditionFn = (renderContext) => renderContext.url === this.apiPath && (renderContext.post?.api) === condition;
66
+ conditionFn = (renderContext) => renderContext.method === "POST" && renderContext.url === this.apiPath && (renderContext.post?.api) === condition;
67
67
  }
68
68
  else if (typeof condition === "function") {
69
- conditionFn = async (renderContext) => renderContext.url === this.apiPath && (await condition(renderContext));
69
+ conditionFn = async (renderContext) => renderContext.method === "POST" && renderContext.url === this.apiPath && (await condition(renderContext));
70
70
  }
71
71
  else if (condition?.constructor == RegExp) {
72
- conditionFn = (renderContext) => renderContext.url === this.apiPath && condition.test(renderContext.post?.api);
72
+ conditionFn = (renderContext) => renderContext.method === "POST" && renderContext.url === this.apiPath && condition.test(renderContext.post?.api);
73
73
  }
74
74
  else {
75
75
  throw "Unsupported API Condition";
@@ -82,13 +82,13 @@ export default class Lambder {
82
82
  addPath(condition, actionFn) {
83
83
  let conditionFn = (renderContext) => false;
84
84
  if (typeof condition === "string") {
85
- conditionFn = (renderContext) => renderContext.url === condition;
85
+ conditionFn = (renderContext) => renderContext.method === "GET" && renderContext.url === condition;
86
86
  }
87
87
  else if (typeof condition === "function") {
88
- conditionFn = async (renderContext) => (await condition(renderContext));
88
+ conditionFn = async (renderContext) => renderContext.method === "GET" && (await condition(renderContext));
89
89
  }
90
90
  else if (condition?.constructor == RegExp) {
91
- conditionFn = (renderContext) => condition.test(renderContext.url);
91
+ conditionFn = (renderContext) => renderContext.method === "GET" && condition.test(renderContext.url);
92
92
  }
93
93
  else {
94
94
  throw "Unsupported Path Condition";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lambder",
3
- "version": "1.0.3",
3
+ "version": "1.0.5",
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");
package/src/index.ts CHANGED
@@ -109,11 +109,14 @@ export default class Lambder {
109
109
  addApi(condition: string|ConditionFunction|RegExp, actionFn: ActionFunction):void{
110
110
  let conditionFn: ConditionFunction = (renderContext:RenderContext) => false;
111
111
  if(typeof condition === "string"){
112
- conditionFn = (renderContext:RenderContext) => renderContext.url === this.apiPath && (renderContext.post?.api) === condition;
112
+ conditionFn = (renderContext:RenderContext) =>
113
+ renderContext.method === "POST" && renderContext.url === this.apiPath && (renderContext.post?.api) === condition;
113
114
  }else if(typeof condition === "function"){
114
- conditionFn = async (renderContext:RenderContext) => renderContext.url === this.apiPath && (await condition(renderContext));
115
+ conditionFn = async (renderContext:RenderContext) =>
116
+ renderContext.method === "POST" && renderContext.url === this.apiPath && (await condition(renderContext));
115
117
  }else if(condition?.constructor == RegExp){
116
- conditionFn = (renderContext:RenderContext) => renderContext.url === this.apiPath && condition.test(renderContext.post?.api);
118
+ conditionFn = (renderContext:RenderContext) =>
119
+ renderContext.method === "POST" && renderContext.url === this.apiPath && condition.test(renderContext.post?.api);
117
120
  }else { throw "Unsupported API Condition"; }
118
121
  this._actionList.push({
119
122
  conditionFn,
@@ -124,11 +127,14 @@ export default class Lambder {
124
127
  addPath(condition: string|ConditionFunction|RegExp, actionFn: ActionFunction):void{
125
128
  let conditionFn: ConditionFunction = (renderContext:RenderContext) => false;
126
129
  if(typeof condition === "string"){
127
- conditionFn = (renderContext:RenderContext) => renderContext.url === condition;
130
+ conditionFn = (renderContext:RenderContext) =>
131
+ renderContext.method === "GET" && renderContext.url === condition;
128
132
  }else if(typeof condition === "function"){
129
- conditionFn = async (renderContext:RenderContext) => (await condition(renderContext));
133
+ conditionFn = async (renderContext:RenderContext) =>
134
+ renderContext.method === "GET" && (await condition(renderContext));
130
135
  }else if(condition?.constructor == RegExp){
131
- conditionFn = (renderContext:RenderContext) => condition.test(renderContext.url);
136
+ conditionFn = (renderContext:RenderContext) =>
137
+ renderContext.method === "GET" && condition.test(renderContext.url);
132
138
  }else { throw "Unsupported Path Condition"; }
133
139
  this._actionList.push({
134
140
  conditionFn,