whet 0.0.17 → 0.0.19

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.
@@ -62,5 +62,6 @@ export declare class Exception extends Error {
62
62
  toString(): string
63
63
  protected get_message(): string
64
64
  protected get_native(): any
65
+ protected static caught(value: any): Exception
65
66
  protected static thrown(value: any): any
66
67
  }
@@ -65,6 +65,15 @@ class Exception extends Register.inherits(() => Error, true) {
65
65
  get_native() {
66
66
  return this.__nativeException;
67
67
  }
68
+ static caught(value) {
69
+ if (((value) instanceof Exception)) {
70
+ return value;
71
+ } else if (((value) instanceof Error)) {
72
+ return new Exception(value.message, null, value);
73
+ } else {
74
+ return new ValueException(value, null, value);
75
+ };
76
+ }
68
77
  static thrown(value) {
69
78
  if (((value) instanceof Exception)) {
70
79
  return value.get_native();
@@ -0,0 +1,98 @@
1
+
2
+ export type PrettyOptions = {
3
+ /**
4
+ Opens the file with the 'a' flag.
5
+ */
6
+ append?: null | boolean,
7
+ /**
8
+ If set to true, will add color information to the formatted output message.
9
+ */
10
+ colorize?: null | boolean,
11
+ /**
12
+ Appends carriage return and line feed, instead of just a line feed, to the formatted log line.
13
+ */
14
+ crlf?: null | boolean,
15
+ /**
16
+ Provides the ability to add a custom prettify function for specific log properties.
17
+ `customPrettifiers` is an object, where keys are log properties that will be prettified
18
+ and value is the prettify function itself.
19
+ For example, if a log line contains a query property, you can specify a prettifier for it:
20
+ */
21
+ customPrettifiers?: null | {
22
+ },
23
+ /**
24
+ The file, file descriptor, or stream to write to. Defaults to 1 (stdout).
25
+ */
26
+ destination?: null | any,
27
+ /**
28
+ Define the log keys that are associated with error like objects.
29
+ */
30
+ errorLikeObjectKeys?: null | string[],
31
+ /**
32
+ When formatting an error object, display this list of properties.
33
+ The list should be a comma separated list of properties.
34
+ */
35
+ errorProps?: null | string,
36
+ /**
37
+ Hide objects from output (but not error object).
38
+ */
39
+ hideObject?: null | boolean,
40
+ /**
41
+ Ignore one or several keys.
42
+ Will be overridden by the option include if include is presented.
43
+ */
44
+ ignore?: null | string,
45
+ /**
46
+ Include one or several keys.
47
+ */
48
+ include?: null | string,
49
+ /**
50
+ If set to true, it will print the name of the log level as the first field in the log line.
51
+ */
52
+ levelFirst?: null | boolean,
53
+ /**
54
+ Define the key that contains the level of the log.
55
+ */
56
+ levelKey?: null | string,
57
+ /**
58
+ Output the log level using the specified label.
59
+ */
60
+ levelLabel?: null | string,
61
+ /**
62
+ Format output of message, e.g. {level} - {pid} will output message: INFO - 1123
63
+ */
64
+ messageFormat?: null | any,
65
+ /**
66
+ The key in the JSON object to use as the highlighted message.
67
+ */
68
+ messageKey?: null | string,
69
+ /**
70
+ The minimum log level to include in the output.
71
+ */
72
+ minimumLevel?: null | any,
73
+ /**
74
+ Ensure directory for destination file exists.
75
+ */
76
+ mkdir?: null | boolean,
77
+ /**
78
+ Print each log message on a single line (errors will still be multi-line).
79
+ */
80
+ singleLine?: null | boolean,
81
+ /**
82
+ Makes messaging synchronous.
83
+ */
84
+ sync?: null | boolean,
85
+ /**
86
+ The key in the JSON object to use for timestamp display.
87
+ */
88
+ timestampKey?: null | string,
89
+ /**
90
+ Translate the epoch time value into a human readable date and time string. This flag also can set the format
91
+ string to apply when translating the date to human readable format. For a list of available pattern letters
92
+ see the {@link https://www.npmjs.com/package/dateformat|dateformat documentation}.
93
+ - The default format is `yyyy-mm-dd HH:MM:ss.l o` in UTC.
94
+ - Requires a `SYS:` prefix to translate time to the local system's timezone. Use the shortcut `SYS:standard`
95
+ to translate time to `yyyy-mm-dd HH:MM:ss.l o` in system timezone.
96
+ */
97
+ translateTime?: null | any
98
+ }
@@ -0,0 +1,3 @@
1
+
2
+ export type MessageFormatFunc = ((log: {
3
+ }, messageKey: string, levelLabel: string) => string)
package/bin/whet/Log.d.ts CHANGED
@@ -7,6 +7,7 @@ export declare class Log {
7
7
  static error(...args: any[]): void
8
8
  static fatal(...args: any[]): void
9
9
  static logLevel: number
10
+ static stream: IWritable
10
11
  protected static log(level: number, ...args: any[]): void
11
12
  protected static replacer(key: any, val: any): any
12
13
  }
package/bin/whet/Log.js CHANGED
@@ -50,12 +50,12 @@ class Log {
50
50
  };
51
51
  };
52
52
  };
53
- process.stdout.write(JSON.stringify(out, Log.replacer) + "\n");
53
+ Log.stream.write(JSON.stringify(out, Log.replacer) + "\n");
54
54
  };
55
55
  }
56
56
  static replacer(key, val) {
57
57
  if (((val) instanceof Error)) {
58
- return val.stack;
58
+ return {"type": val.name, "message": val.message, "stack": val.stack};
59
59
  };
60
60
  if (val != null && typeof(val.toString) == "function" && val.toString != Object.prototype.toString) {
61
61
  return val.toString();
@@ -72,6 +72,7 @@ class Log {
72
72
 
73
73
 
74
74
  Log.logLevel = 30
75
+ Log.stream = process.stdout
75
76
  export const LogLevel = Register.global("$hxClasses")["whet._Log.LogLevel"] =
76
77
  class LogLevel {
77
78
  static fromString(s) {
@@ -129,7 +129,7 @@ class SourceData extends Register.inherits() {
129
129
  Fs.readFile(path, function (err, buffer) {
130
130
  if (err != null) {
131
131
  Log.log(50, ...["File does not exist.", {"id": id, "path": path}]);
132
- res(null);
132
+ rej(err);
133
133
  } else {
134
134
  var source = SourceData.fromBytes(id, buffer);
135
135
  source.filePath = path;
@@ -18,6 +18,12 @@ export declare class Stone<T extends StoneConfig> {
18
18
  cacheStrategy: CacheStrategy
19
19
  readonly cache: CacheManager
20
20
  project: Project
21
+ protected lockQueue: {
22
+ rej: (arg0: any) => void,
23
+ res: (arg0: any) => void,
24
+ run: () => Promise<any>
25
+ }[]
26
+ protected locked: boolean
21
27
 
22
28
  /**
23
29
  Override this to set config defaults.
@@ -29,6 +35,11 @@ export declare class Stone<T extends StoneConfig> {
29
35
  */
30
36
  protected addCommands(): void
31
37
 
38
+ /**
39
+ * Locks this stone for generating. Used to prevent parallel generation of the same sources.
40
+ */
41
+ protected acquire<T>(run: (() => Promise<T>)): Promise<T>
42
+
32
43
  /**
33
44
  * **Do not override.**
34
45
  * Get Source for this stone. Goes through the cache.
package/bin/whet/Stone.js CHANGED
@@ -17,6 +17,8 @@ const $global = Register.$global
17
17
  export const Stone = Register.global("$hxClasses")["whet.Stone"] =
18
18
  class Stone extends Register.inherits() {
19
19
  new(config) {
20
+ this.locked = false;
21
+ this.lockQueue = [];
20
22
  this.ignoreFileHash = false;
21
23
  Log.log(10, ...["Instantiating new Stone.", {"type": StoneId_Fields_.getTypeName(this)}]);
22
24
  if (config == null) {
@@ -49,6 +51,39 @@ class Stone extends Register.inherits() {
49
51
  addCommands() {
50
52
  }
51
53
 
54
+ /**
55
+ * Locks this stone for generating. Used to prevent parallel generation of the same sources.
56
+ */
57
+ acquire(run) {
58
+ var _gthis = this;
59
+ Log.log(10, ...["Acquiring lock on a stone.", {"stone": this}]);
60
+ if (this.locked) {
61
+ Log.log(20, ...["Stone is locked, waiting.", {"stone": this}]);
62
+ var deferredRes;
63
+ var deferredRej;
64
+ var deferred = new Promise(function (res, rej) {
65
+ deferredRes = res;
66
+ deferredRej = rej;
67
+ });
68
+ this.lockQueue.push({"run": run, "res": deferredRes, "rej": deferredRej});
69
+ return deferred;
70
+ } else {
71
+ this.locked = true;
72
+ var runNext = null;
73
+ runNext = function () {
74
+ if (_gthis.lockQueue.length > 0) {
75
+ Log.log(20, ...["Running next queued-up lock acquire function.", {"stone": _gthis}]);
76
+ var queued = _gthis.lockQueue.shift();
77
+ queued.run().then(queued.res)["catch"](queued.rej)["finally"](runNext);
78
+ } else {
79
+ _gthis.locked = false;
80
+ Log.log(10, ...["No function in lock queue. Stone is now unlocked.", {"stone": _gthis}]);
81
+ };
82
+ };
83
+ return run()["finally"](runNext);
84
+ };
85
+ }
86
+
52
87
  /**
53
88
  * **Do not override.**
54
89
  * Get Source for this stone. Goes through the cache.
@@ -104,6 +139,9 @@ class Stone extends Register.inherits() {
104
139
  */
105
140
  generateSource(hash) {
106
141
  var _gthis = this;
142
+ if (!this.locked) {
143
+ throw new Error("Acquire a lock before generating.");
144
+ };
107
145
  Log.log(20, ...["Generating source.", {"stone": this, "hash": hash}]);
108
146
  var init;
109
147
  if (this.config.dependencies != null) {
@@ -161,6 +199,7 @@ class Stone extends Register.inherits() {
161
199
  * @param err Any error that might have happened during `generateSource`.
162
200
  */
163
201
  handleError(err) {
202
+ Log.log(50, ...["Error while generating.", {"stone": this, "err": err}]);
164
203
  return Promise.reject(err);
165
204
  }
166
205
 
@@ -2,3 +2,4 @@ import {Command} from "commander"
2
2
 
3
3
  export const program: Command
4
4
  export const main: () => void
5
+ export const executeCommand: (cmd: string[]) => Promise<Command>
package/bin/whet/Whet.js CHANGED
@@ -1,6 +1,8 @@
1
1
  import {Project} from "./Project.js"
2
2
  import {LogLevel, Log} from "./Log.js"
3
3
  import * as Url from "url"
4
+ import * as PinoPretty from "pino-pretty"
5
+ import {Exception} from "../haxe/Exception.js"
4
6
  import {Register} from "../genes/Register.js"
5
7
  import {Command, CommanderError} from "commander"
6
8
  import {Std} from "../Std.js"
@@ -10,8 +12,17 @@ const $global = Register.$global
10
12
  export const Whet_Fields_ = Register.global("$hxClasses")["whet._Whet.Whet_Fields_"] =
11
13
  class Whet_Fields_ {
12
14
  static main() {
13
- Whet_Fields_.program.enablePositionalOptions().passThroughOptions().description("Project tooling.").usage("[options] [command] [+ [command]...]").version("0.0.17", "-v, --version").allowUnknownOption(true).showSuggestionAfterError(true).option("-p, --project <file>", "project to run", "Project.mjs").option("-l, --log-level <level>", "log level, a string/number", "info").exitOverride();
14
- Whet_Fields_.program.parse();
15
+ Whet_Fields_.program.enablePositionalOptions().passThroughOptions().description("Project tooling.").usage("[options] [command] [+ [command]...]").version("0.0.19", "-v, --version").allowUnknownOption(true).showSuggestionAfterError(true).option("-p, --project <file>", "project to run", "Project.mjs").option("-l, --log-level <level>", "log level, a string/number", "info").option("--no-pretty", "disable pretty logging").exitOverride();
16
+ try {
17
+ Whet_Fields_.program.parse();
18
+ }catch (_g) {
19
+ var _g1 = Exception.caught(_g);
20
+ if (((_g1.get_native()) instanceof CommanderError) && _g1.get_native().code == "commander.version") {
21
+ process.exit();
22
+ } else {
23
+ throw Exception.thrown(_g1);
24
+ };
25
+ };
15
26
  var options = Whet_Fields_.program.opts();
16
27
  if (options.logLevel != null) {
17
28
  var n = Std.parseInt(options.logLevel);
@@ -24,6 +35,9 @@ class Whet_Fields_ {
24
35
  Log.logLevel = n;
25
36
  };
26
37
  };
38
+ if (options.pretty) {
39
+ Log.stream = PinoPretty["default"]();
40
+ };
27
41
  global.setImmediate(Whet_Fields_.init, options);
28
42
  }
29
43
  static init(options) {
@@ -41,7 +55,10 @@ class Whet_Fields_ {
41
55
  if (((e) instanceof Error)) {
42
56
  Log.log(50, ...[e.stack]);
43
57
  };
44
- return Whet_Fields_.program.help();
58
+ try {
59
+ Whet_Fields_.program.help();
60
+ }catch (_g) {
61
+ };
45
62
  });
46
63
  };
47
64
  }
@@ -98,6 +115,10 @@ class Whet_Fields_ {
98
115
  nextCommand();
99
116
  });
100
117
  }
118
+ static executeCommand(cmd) {
119
+ Log.log(10, ...["Executing command.", {"command": cmd}]);
120
+ return Whet_Fields_.program.parseAsync(cmd, {"from": "user"});
121
+ }
101
122
  static getCommands(args) {
102
123
  var commands = [];
103
124
  var from = 0;
@@ -124,3 +145,4 @@ class Whet_Fields_ {
124
145
  Whet_Fields_.program = new Command("whet")
125
146
  export const program = Whet_Fields_.program
126
147
  export const main = Whet_Fields_.main
148
+ export const executeCommand = Whet_Fields_.executeCommand
@@ -23,74 +23,76 @@ class BaseCache extends Register.inherits() {
23
23
  }
24
24
  get(stone, durability, check) {
25
25
  var _gthis = this;
26
- return stone.finalMaybeHash().then(function (hash) {
27
- if (hash == null) {
28
- Log.log(20, ...["Generating source, because it does not supply a hash.", {"stone": stone, "cache": _gthis}]);
29
- };
30
- if (hash == null) {
31
- return stone.generateSource(null).then(function (generatedSource) {
32
- return {"generatedSource": generatedSource, "hash": generatedSource.hash};
33
- });
34
- } else {
35
- Log.log(20, ...["Stone provided hash.", {"stone": stone, "hash": SourceHash.toHex(hash)}]);
36
- return Promise.resolve({"generatedSource": null, "hash": hash});
37
- };
38
- }).then(function (data) {
39
- var generatedSource = data.generatedSource;
40
- var hash = data.hash;
41
- var values = _gthis.cache.get(_gthis.key(stone));
42
- var ageCount = function (val) {
43
- return Lambda.count(values, function (v) {
44
- if (v != val) {
45
- return v.ctime > val.ctime;
46
- } else {
47
- return false;
48
- };
49
- });
50
- };
51
- var value = null;
52
- if (values != null && values.length > 0) {
53
- value = Lambda.find(values, function (v) {
54
- return SourceHash.equals(v.hash, hash);
55
- });
56
- if (value != null && check._hx_index == 2 && !_gthis.shouldKeep(stone, value, durability, function (v) {
57
- return 0;
58
- }, ageCount)) {
59
- _gthis.remove(stone, value);
60
- value = null;
26
+ return stone.acquire(function () {
27
+ return stone.finalMaybeHash().then(function (hash) {
28
+ if (hash == null) {
29
+ Log.log(20, ...["Generating source, because it does not supply a hash.", {"stone": stone, "cache": _gthis}]);
61
30
  };
62
- if (value != null) {
63
- _gthis.setRecentUseOrder(values, value);
31
+ if (hash == null) {
32
+ return stone.generateSource(null).then(function (generatedSource) {
33
+ return {"generatedSource": generatedSource, "hash": generatedSource.hash};
34
+ });
35
+ } else {
36
+ Log.log(20, ...["Stone provided hash.", {"stone": stone, "hash": SourceHash.toHex(hash)}]);
37
+ return Promise.resolve({"generatedSource": null, "hash": hash});
64
38
  };
65
- };
66
- return ((value != null) ? _gthis.source(stone, value) : Promise.resolve(null)).then(function (src) {
67
- if (src == null) {
68
- Log.log(10, ...["Not cached.", {"stone": stone, "cache": _gthis}]);
69
- return ((value != null) ? _gthis.remove(stone, value) : Promise.resolve(null)).then(function (_) {
70
- if (check._hx_index == 1) {
71
- _gthis.checkDurability(stone, values, durability, function (v) {
72
- return values.indexOf(v) + 1;
73
- }, function (v) {
74
- return ageCount(v) + 1;
75
- });
39
+ }).then(function (data) {
40
+ var generatedSource = data.generatedSource;
41
+ var hash = data.hash;
42
+ var values = _gthis.cache.get(_gthis.key(stone));
43
+ var ageCount = function (val) {
44
+ return Lambda.count(values, function (v) {
45
+ if (v != val) {
46
+ return v.ctime > val.ctime;
47
+ } else {
48
+ return false;
76
49
  };
77
- return ((generatedSource != null) ? Promise.resolve(generatedSource) : stone.generateSource(hash)).then(function (src) {
78
- return _gthis.set(src);
79
- }).then(function (val) {
80
- return _gthis.source(stone, val);
81
- });
82
50
  });
83
- } else {
84
- Log.log(10, ...["Found in cache", {"stone": stone, "cache": _gthis}]);
85
- return Promise.resolve(src);
86
51
  };
87
- }).then(function (src) {
88
- if ((check == null) ? true : check._hx_index == 0) {
89
- _gthis.checkDurability(stone, values, durability, function (v) {
90
- return values.indexOf(v);
91
- }, ageCount);
52
+ var value = null;
53
+ if (values != null && values.length > 0) {
54
+ value = Lambda.find(values, function (v) {
55
+ return SourceHash.equals(v.hash, hash);
56
+ });
57
+ if (value != null && check._hx_index == 2 && !_gthis.shouldKeep(stone, value, durability, function (v) {
58
+ return 0;
59
+ }, ageCount)) {
60
+ _gthis.remove(stone, value);
61
+ value = null;
62
+ };
63
+ if (value != null) {
64
+ _gthis.setRecentUseOrder(values, value);
65
+ };
92
66
  };
93
- return src;
67
+ return ((value != null) ? _gthis.source(stone, value) : Promise.resolve(null)).then(function (src) {
68
+ if (src == null) {
69
+ Log.log(10, ...["Not cached.", {"stone": stone, "cache": _gthis}]);
70
+ return ((value != null) ? _gthis.remove(stone, value) : Promise.resolve(null)).then(function (_) {
71
+ if (check._hx_index == 1) {
72
+ _gthis.checkDurability(stone, values, durability, function (v) {
73
+ return values.indexOf(v) + 1;
74
+ }, function (v) {
75
+ return ageCount(v) + 1;
76
+ });
77
+ };
78
+ return ((generatedSource != null) ? Promise.resolve(generatedSource) : stone.generateSource(hash)).then(function (src) {
79
+ return _gthis.set(src);
80
+ }).then(function (val) {
81
+ return _gthis.source(stone, val);
82
+ });
83
+ });
84
+ } else {
85
+ Log.log(10, ...["Found in cache", {"stone": stone, "cache": _gthis}]);
86
+ return Promise.resolve(src);
87
+ };
88
+ }).then(function (src) {
89
+ if ((check == null) ? true : check._hx_index == 0) {
90
+ _gthis.checkDurability(stone, values, durability, function (v) {
91
+ return values.indexOf(v);
92
+ }, ageCount);
93
+ };
94
+ return src;
95
+ });
94
96
  });
95
97
  });
96
98
  }
@@ -27,8 +27,10 @@ class CacheManager extends Register.inherits() {
27
27
  var _g = stone.cacheStrategy;
28
28
  switch (_g._hx_index) {
29
29
  case 0:
30
- return stone.finalMaybeHash().then(function (hash) {
31
- return stone.generateSource(hash);
30
+ return stone.acquire(function () {
31
+ return stone.finalMaybeHash().then(function (hash) {
32
+ return stone.generateSource(hash);
33
+ });
32
34
  });
33
35
  break
34
36
  case 1:
@@ -54,8 +56,10 @@ class CacheManager extends Register.inherits() {
54
56
  Log.log(10, ...["Re-generating cached stone.", {"stone": stone}]);
55
57
  switch (stone.cacheStrategy._hx_index) {
56
58
  case 0:
57
- return stone.finalMaybeHash().then(function (hash) {
58
- return stone.generateSource(hash);
59
+ return stone.acquire(function () {
60
+ return stone.finalMaybeHash().then(function (hash) {
61
+ return stone.generateSource(hash);
62
+ });
59
63
  });
60
64
  break
61
65
  case 1:
@@ -161,18 +161,26 @@ class FileCache extends Register.inherits(BaseCache) {
161
161
  SourceData.fromFile(file[0].id, path, file[0].filePath).then((function (file) {
162
162
  return function (sourceData) {
163
163
  if (sourceData == null || !stone.ignoreFileHash && !SourceHash.equals(sourceData.hash, file[0].fileHash)) {
164
- rej("Wrong hash.");
164
+ rej("Invalid.");
165
165
  } else {
166
166
  res(sourceData);
167
167
  };
168
168
  };
169
- })(file));
169
+ })(file), (function () {
170
+ return function (err) {
171
+ if (((err) instanceof Error) && err.code == "ENOENT") {
172
+ rej("Invalid.");
173
+ } else {
174
+ rej(err);
175
+ };
176
+ };
177
+ })());
170
178
  };
171
179
  })([_g2[_g1++]])));
172
180
  return Promise.all(_g).then(function (data) {
173
181
  return new Source(data, value.hash, stone, value.ctime);
174
182
  }, function (rejected) {
175
- if (rejected == "Wrong hash.") {
183
+ if (rejected == "Invalid.") {
176
184
  return null;
177
185
  } else {
178
186
  throw rejected;
@@ -1,5 +1,8 @@
1
+ import * as Path from "path"
1
2
  import Minimatch from "minimatch"
2
3
  import {Register} from "../../genes/Register.js"
4
+ import {StringTools} from "../../StringTools.js"
5
+ import {HxOverrides} from "../../HxOverrides.js"
3
6
 
4
7
  const $global = Register.$global
5
8
 
@@ -7,7 +10,14 @@ export const MinimatchType_Fields_ = Register.global("$hxClasses")["whet.magic._
7
10
  class MinimatchType_Fields_ {
8
11
  static makeMinimatch(src) {
9
12
  if (typeof(src) == "string") {
10
- return new Minimatch.Minimatch(src, null);
13
+ var s = src;
14
+ var str = (s.length > 1 && HxOverrides.cca(s, 0) == 47) ? s.substring(1) : s;
15
+ if (str.length > 0) {
16
+ str = Path.posix.normalize(str);
17
+ str = StringTools.replace(str, "\\", "/");
18
+ };
19
+ s = str;
20
+ return new Minimatch.Minimatch((HxOverrides.cca(s, 0) == 47) ? s : "/" + s, null);
11
21
  } else if (src instanceof Minimatch.Minimatch) {
12
22
  return src;
13
23
  } else {
@@ -42,6 +42,7 @@ export type ServerConfig = {
42
42
  * Do not create cyclic dependencies!
43
43
  */
44
44
  dependencies?: null | MaybeArray<AnyStone>,
45
+ headers?: null | {[key: string]: string},
45
46
  /**
46
47
  Defaults to the Stone's class name.
47
48
  */
@@ -1,3 +1,4 @@
1
+ import {Whet_Fields_} from "../Whet.js"
1
2
  import {Stone} from "../Stone.js"
2
3
  import {Log} from "../Log.js"
3
4
  import * as Path from "path"
@@ -6,6 +7,7 @@ import * as Http from "http"
6
7
  import {Register} from "../../genes/Register.js"
7
8
  import {StringTools} from "../../StringTools.js"
8
9
  import {Std} from "../../Std.js"
10
+ import {Reflect as Reflect__1} from "../../Reflect.js"
9
11
  import {HxOverrides} from "../../HxOverrides.js"
10
12
 
11
13
  const $global = Register.$global
@@ -55,61 +57,63 @@ class Server extends Register.inherits(Stone) {
55
57
  var _gthis = this;
56
58
  Log.log(30, ...["Handling request.", {"url": req.url, "method": req.method}]);
57
59
  var searchIndex = req.url.indexOf("?");
58
- var id;
59
- if (searchIndex > 0) {
60
- var s = req.url.substring(0, searchIndex);
61
- var str = (s.length > 1 && HxOverrides.cca(s, 0) == 47) ? s.substring(1) : s;
62
- if (str.length > 0) {
63
- str = Path.posix.normalize(str);
64
- str = StringTools.replace(str, "\\", "/");
65
- };
66
- s = str;
67
- id = (HxOverrides.cca(s, 0) == 47) ? s : "/" + s;
68
- } else {
69
- var s = req.url;
70
- var str = (s.length > 1 && HxOverrides.cca(s, 0) == 47) ? s.substring(1) : s;
71
- if (str.length > 0) {
72
- str = Path.posix.normalize(str);
73
- str = StringTools.replace(str, "\\", "/");
74
- };
75
- s = str;
76
- id = (HxOverrides.cca(s, 0) == 47) ? s : "/" + s;
77
- };
78
- if (req.method == "GET") {
79
- if (HxOverrides.cca(id, id.length - 1) == 47) {
80
- if ("index.html".length > 0) {
81
- var s = id.substring(0, id.lastIndexOf("/") + 1);
60
+ var id = decodeURI((searchIndex > 0 ? req.url.substring(0,searchIndex) : req.url));
61
+ switch (req.method) {
62
+ case "GET":
63
+ if (HxOverrides.cca(id, id.length - 1) == 47) {
64
+ if ("index.html".length > 0) {
65
+ var s = id.substring(0, id.lastIndexOf("/") + 1);
66
+ var str = (s.length > 1 && HxOverrides.cca(s, 0) == 47) ? s.substring(1) : s;
67
+ if (str.length > 0) {
68
+ str = Path.posix.normalize(str);
69
+ str = StringTools.replace(str, "\\", "/");
70
+ };
71
+ s = str;
72
+ id = ((HxOverrides.cca(s, 0) == 47) ? s : "/" + s) + "index.html";
73
+ };
74
+ } else if (Path.posix.extname(id) == "") {
75
+ var s = "" + id + "/index.html";
82
76
  var str = (s.length > 1 && HxOverrides.cca(s, 0) == 47) ? s.substring(1) : s;
83
77
  if (str.length > 0) {
84
78
  str = Path.posix.normalize(str);
85
79
  str = StringTools.replace(str, "\\", "/");
86
80
  };
87
81
  s = str;
88
- id = ((HxOverrides.cca(s, 0) == 47) ? s : "/" + s) + "index.html";
82
+ id = (HxOverrides.cca(s, 0) == 47) ? s : "/" + s;
89
83
  };
90
- } else if (Path.posix.extname(id) == "") {
91
- var s = "" + id + "/index.html";
92
- var str = (s.length > 1 && HxOverrides.cca(s, 0) == 47) ? s.substring(1) : s;
93
- if (str.length > 0) {
94
- str = Path.posix.normalize(str);
95
- str = StringTools.replace(str, "\\", "/");
96
- };
97
- s = str;
98
- id = (HxOverrides.cca(s, 0) == 47) ? s : "/" + s;
99
- };
100
- this.config.router.get(id).then(function (routeResult) {
101
- return ((routeResult.length > 0) ? routeResult[0].get() : (_gthis.routeDynamic != null) ? _gthis.routeDynamic(id) : Promise.resolve(null)).then(function (source) {
102
- if (source == null) {
103
- res.writeHead(404, "File not found.");
84
+ this.config.router.get(id).then(function (routeResult) {
85
+ return ((routeResult.length > 0) ? routeResult[0].get() : (_gthis.routeDynamic != null) ? _gthis.routeDynamic(id) : Promise.resolve(null)).then(function (source) {
86
+ if (source == null) {
87
+ res.writeHead(404, "File not found.");
88
+ res.end();
89
+ return;
90
+ };
91
+ var headers = {"Content-Type": Mime.getType(Path.posix.extname(id).toLowerCase()), "Last-Modified": new Date(source.source.ctime * 1000).toUTCString(), "Content-Length": Std.string(source.data.length), "Cache-Control": "no-store, no-cache"};
92
+ if (_gthis.config.headers != null) {
93
+ var access = _gthis.config.headers;
94
+ var _g_keys = Reflect__1.fields(access);
95
+ var _g_index = 0;
96
+ while (_g_index < _g_keys.length) {
97
+ var key = _g_keys[_g_index++];
98
+ headers[key] = access[key];
99
+ };
100
+ };
101
+ res.writeHead(200, headers);
102
+ res.write(source.data, "binary");
104
103
  res.end();
105
- return;
106
- };
107
- res.writeHead(200, {"Content-Type": Mime.getType(Path.posix.extname(id).toLowerCase()), "Last-Modified": new Date(source.source.ctime * 1000).toUTCString(), "Content-Length": Std.string(source.data.length), "Cache-Control": "no-store, no-cache"});
108
- res.write(source.data, "binary");
109
- res.end();
104
+ })["catch"](function (e) {
105
+ Log.log(40, ...["Server error.", {"error": e}]);
106
+ res.writeHead(500, "Error happened.", _gthis.config.headers);
107
+ if (((e) instanceof Error)) {
108
+ res.write(e.stack, "utf-8");
109
+ } else {
110
+ res.write(Std.string(e), "utf-8");
111
+ };
112
+ res.end();
113
+ });
110
114
  })["catch"](function (e) {
111
115
  Log.log(40, ...["Server error.", {"error": e}]);
112
- res.writeHead(500, "Error happened.");
116
+ res.writeHead(500, "Error happened.", _gthis.config.headers);
113
117
  if (((e) instanceof Error)) {
114
118
  res.write(e.stack, "utf-8");
115
119
  } else {
@@ -117,19 +121,52 @@ class Server extends Register.inherits(Stone) {
117
121
  };
118
122
  res.end();
119
123
  });
120
- })["catch"](function (e) {
121
- Log.log(40, ...["Server error.", {"error": e}]);
122
- res.writeHead(500, "Error happened.");
123
- if (((e) instanceof Error)) {
124
- res.write(e.stack, "utf-8");
125
- } else {
126
- res.write(Std.string(e), "utf-8");
127
- };
124
+ break
125
+ case "OPTIONS":
126
+ res.writeHead(200, this.config.headers);
128
127
  res.end();
129
- });
130
- } else {
128
+ break
129
+ case "PUT":
130
+ var s = "/";
131
+ var str = ("/".length > 1 && HxOverrides.cca("/", 0) == 47) ? "/".substring(1) : "/";
132
+ if (str.length > 0) {
133
+ str = Path.posix.normalize(str);
134
+ str = StringTools.replace(str, "\\", "/");
135
+ };
136
+ s = str;
137
+ var root = (HxOverrides.cca(s, 0) == 47) ? s : "/" + s;
138
+ if (id.charAt(0) != "/") {
139
+ throw new Error("Badly formed SourceId.");
140
+ };
141
+ var cmd = [Path.posix.join(".", root, ".", id)];
142
+ var body = "";
143
+ req.on("data", function (chunk) {
144
+ body += chunk;
145
+ return body;
146
+ });
147
+ req.on("end", function () {
148
+ if (body != "") {
149
+ cmd.push(body);
150
+ };
151
+ return Whet_Fields_.executeCommand(cmd).then(function (_) {
152
+ res.writeHead(200, _gthis.config.headers);
153
+ res.end();
154
+ })["catch"](function (e) {
155
+ Log.log(40, ...["Server error.", {"error": e}]);
156
+ res.writeHead(500, "Error happened.", _gthis.config.headers);
157
+ if (((e) instanceof Error)) {
158
+ res.write(e.stack, "utf-8");
159
+ } else {
160
+ res.write(Std.string(e), "utf-8");
161
+ };
162
+ res.end();
163
+ });
164
+ });
165
+ break
166
+ default:
131
167
  res.writeHead(400, "Unsupported method.");
132
168
  res.end();
169
+
133
170
  };
134
171
  }
135
172
  static get __name__() {
package/package.json CHANGED
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "name": "whet",
3
- "version": "0.0.17",
3
+ "version": "0.0.19",
4
4
  "description": "NodeJS based assets management and project tooling library.",
5
5
  "scripts": {
6
- "devinit": "npx dts2hx commander --modular --noLibWrap",
6
+ "devinit": "npx dts2hx commander pino-pretty --modular --noLibWrap",
7
7
  "build": "npx haxe build.hxml"
8
8
  },
9
9
  "repository": {
@@ -25,7 +25,8 @@
25
25
  "dependencies": {
26
26
  "commander": "^9.0.0",
27
27
  "mime": "^3.0.0",
28
- "minimatch": "^5.1.0"
28
+ "minimatch": "^5.1.0",
29
+ "pino-pretty": "^9.1.0"
29
30
  },
30
31
  "devDependencies": {
31
32
  "@types/minimatch": "^5.1.0",