@discord/intl-message-database 0.0.1-rc.0

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.
Files changed (50) hide show
  1. package/.cargo/config.toml +12 -0
  2. package/Cargo.toml +38 -0
  3. package/LICENSE +21 -0
  4. package/README.md +7 -0
  5. package/bench/keyless-json.js +91 -0
  6. package/bench/native.js +114 -0
  7. package/bench/util.js +55 -0
  8. package/build.rs +5 -0
  9. package/index.d.ts +52 -0
  10. package/index.js +86 -0
  11. package/npm/darwin-arm64/README.md +3 -0
  12. package/npm/darwin-arm64/package.json +18 -0
  13. package/npm/darwin-x64/README.md +3 -0
  14. package/npm/darwin-x64/package.json +18 -0
  15. package/npm/linux-arm64-gnu/README.md +3 -0
  16. package/npm/linux-arm64-gnu/package.json +21 -0
  17. package/npm/linux-arm64-musl/README.md +3 -0
  18. package/npm/linux-arm64-musl/package.json +21 -0
  19. package/npm/linux-x64-gnu/README.md +3 -0
  20. package/npm/linux-x64-gnu/package.json +21 -0
  21. package/npm/linux-x64-musl/README.md +3 -0
  22. package/npm/linux-x64-musl/package.json +21 -0
  23. package/npm/win32-arm64-msvc/README.md +3 -0
  24. package/npm/win32-arm64-msvc/package.json +18 -0
  25. package/npm/win32-ia32-msvc/README.md +3 -0
  26. package/npm/win32-ia32-msvc/package.json +18 -0
  27. package/npm/win32-x64-msvc/README.md +3 -0
  28. package/npm/win32-x64-msvc/package.json +18 -0
  29. package/package.json +44 -0
  30. package/src/lib.rs +14 -0
  31. package/src/messages/database.rs +343 -0
  32. package/src/messages/error.rs +43 -0
  33. package/src/messages/message_definition.rs +169 -0
  34. package/src/messages/message_variables_visitor.rs +299 -0
  35. package/src/messages/meta.rs +38 -0
  36. package/src/messages/mod.rs +27 -0
  37. package/src/messages/source_file.rs +75 -0
  38. package/src/messages/symbols.rs +164 -0
  39. package/src/messages/translation.rs +64 -0
  40. package/src/messages/value.rs +51 -0
  41. package/src/napi.rs +300 -0
  42. package/src/services/mod.rs +9 -0
  43. package/src/services/precompile.rs +91 -0
  44. package/src/services/types.rs +237 -0
  45. package/src/services/validator.rs +104 -0
  46. package/src/sources/definitions_parser.rs +347 -0
  47. package/src/sources/mod.rs +87 -0
  48. package/src/sources/translations.rs +75 -0
  49. package/src/test.rs +67 -0
  50. package/src/threading.rs +61 -0
@@ -0,0 +1,61 @@
1
+ use std::sync::mpsc::channel;
2
+ use threadpool::ThreadPool;
3
+
4
+ /// Returns a reasonable number of threads to utilize for processing on the
5
+ /// running system. This examines whether logical cpus and physical cpus are
6
+ /// different and decides to use a _majority_ of available resources, but
7
+ /// without taking over the whole system. That's important because this library
8
+ /// is almost always run in the context of other work being done (i.e., as a
9
+ /// Node addon, as part of a bundler process, etc.), and that shouldn't have the
10
+ /// world stopped because of this (albeit fast) process consuming everything.
11
+ pub(crate) fn get_reasonable_thread_count() -> usize {
12
+ let physical = num_cpus::get_physical();
13
+ let logical = num_cpus::get();
14
+ // Use half of the cores on small machines
15
+ if logical < 8 {
16
+ return logical / 2;
17
+ }
18
+ // If hyperthreading is enabled on medium machines, use the physical count.
19
+ if logical > physical && physical <= 12 {
20
+ return physical;
21
+ }
22
+
23
+ // Otherwise use 2/3 of available resources.
24
+ return logical * 2 / 3;
25
+ }
26
+
27
+ /// For each element of `data`, run `thread_func` in a separate thread using a thread pool with a
28
+ /// pre-determined size (i.e., some threads may be reused if there are more items than threads
29
+ /// available). The result for each element is sent back to the main thread, where `processor` is
30
+ /// called with it as the argument.
31
+ pub(crate) fn run_in_thread_pool<
32
+ Data: IntoIterator<Item = T> + ExactSizeIterator,
33
+ T: Send + Sync + 'static,
34
+ R: Send + 'static,
35
+ P: Fn(T) -> anyhow::Result<R> + Copy + Send + Sync + 'static,
36
+ F: FnMut(R) -> anyhow::Result<()>,
37
+ >(
38
+ data: Data,
39
+ thread_func: P,
40
+ mut processor: F,
41
+ ) -> napi::Result<()> {
42
+ let num_jobs = data.len();
43
+ let pool = ThreadPool::new(get_reasonable_thread_count());
44
+ let (tx, rx) = channel();
45
+ for datum in data {
46
+ let tx = tx.clone();
47
+
48
+ pool.execute(move || {
49
+ let result = thread_func(datum);
50
+ tx.send(result)
51
+ .expect("Failed to send processing result from thread pool back to supervisor");
52
+ });
53
+ }
54
+
55
+ for result in rx.iter().take(num_jobs) {
56
+ let result = result?;
57
+ processor(result)?;
58
+ }
59
+
60
+ Ok(())
61
+ }