whet 0.0.18 → 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();
@@ -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) {
package/bin/whet/Whet.js CHANGED
@@ -2,6 +2,7 @@ import {Project} from "./Project.js"
2
2
  import {LogLevel, Log} from "./Log.js"
3
3
  import * as Url from "url"
4
4
  import * as PinoPretty from "pino-pretty"
5
+ import {Exception} from "../haxe/Exception.js"
5
6
  import {Register} from "../genes/Register.js"
6
7
  import {Command, CommanderError} from "commander"
7
8
  import {Std} from "../Std.js"
@@ -11,8 +12,17 @@ const $global = Register.$global
11
12
  export const Whet_Fields_ = Register.global("$hxClasses")["whet._Whet.Whet_Fields_"] =
12
13
  class Whet_Fields_ {
13
14
  static main() {
14
- Whet_Fields_.program.enablePositionalOptions().passThroughOptions().description("Project tooling.").usage("[options] [command] [+ [command]...]").version("0.0.18", "-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();
15
- 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
+ };
16
26
  var options = Whet_Fields_.program.opts();
17
27
  if (options.logLevel != null) {
18
28
  var n = Std.parseInt(options.logLevel);
@@ -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 {
@@ -57,26 +57,7 @@ class Server extends Register.inherits(Stone) {
57
57
  var _gthis = this;
58
58
  Log.log(30, ...["Handling request.", {"url": req.url, "method": req.method}]);
59
59
  var searchIndex = req.url.indexOf("?");
60
- var id;
61
- if (searchIndex > 0) {
62
- var s = req.url.substring(0, searchIndex);
63
- var str = (s.length > 1 && HxOverrides.cca(s, 0) == 47) ? s.substring(1) : s;
64
- if (str.length > 0) {
65
- str = Path.posix.normalize(str);
66
- str = StringTools.replace(str, "\\", "/");
67
- };
68
- s = str;
69
- id = (HxOverrides.cca(s, 0) == 47) ? s : "/" + s;
70
- } else {
71
- var s = req.url;
72
- var str = (s.length > 1 && HxOverrides.cca(s, 0) == 47) ? s.substring(1) : s;
73
- if (str.length > 0) {
74
- str = Path.posix.normalize(str);
75
- str = StringTools.replace(str, "\\", "/");
76
- };
77
- s = str;
78
- id = (HxOverrides.cca(s, 0) == 47) ? s : "/" + s;
79
- };
60
+ var id = decodeURI((searchIndex > 0 ? req.url.substring(0,searchIndex) : req.url));
80
61
  switch (req.method) {
81
62
  case "GET":
82
63
  if (HxOverrides.cca(id, id.length - 1) == 47) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "whet",
3
- "version": "0.0.18",
3
+ "version": "0.0.19",
4
4
  "description": "NodeJS based assets management and project tooling library.",
5
5
  "scripts": {
6
6
  "devinit": "npx dts2hx commander pino-pretty --modular --noLibWrap",