@dasmig/persona 0.0.0-stage → 1.0.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.
- package/DATA_LICENSES.txt +170 -0
- package/LICENSE +21 -0
- package/README.md +116 -2
- package/batch.mjs +70 -0
- package/generator.mjs +109 -0
- package/index.d.ts +313 -0
- package/index.mjs +26 -0
- package/node.mjs +31 -0
- package/package.json +55 -4
- package/persona.mjs +16 -0
- package/persona.wasm +0 -0
- package/pool-worker-core.mjs +13 -0
- package/pool-worker-node.mjs +8 -0
- package/pool-worker.mjs +7 -0
- package/pool.mjs +95 -0
- package/versions.mjs +13 -0
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
The persona module compiles in the lite data of eight dasmig libraries.
|
|
2
|
+
Their data sources and licences follow, as each library states them.
|
|
3
|
+
|
|
4
|
+
== country-generator ==
|
|
5
|
+
|
|
6
|
+
The country data in the resources/ directory, and its compiled-in copy in
|
|
7
|
+
dasmig/countrygen_data.hpp, is a database built from:
|
|
8
|
+
|
|
9
|
+
1. mledoze/countries (https://github.com/mledoze/countries)
|
|
10
|
+
Licensed under the Open Database License (ODbL) v1.0.
|
|
11
|
+
https://opendatacommons.org/licenses/odbl/1.0/
|
|
12
|
+
|
|
13
|
+
2. REST Countries (https://gitlab.com/restcountries/restcountries)
|
|
14
|
+
Licensed under the Mozilla Public License 2.0.
|
|
15
|
+
https://www.mozilla.org/MPL/2.0/
|
|
16
|
+
|
|
17
|
+
3. World Bank Open Data (https://data.worldbank.org/)
|
|
18
|
+
Licensed under Creative Commons Attribution 4.0 International (CC BY 4.0).
|
|
19
|
+
https://creativecommons.org/licenses/by/4.0/
|
|
20
|
+
|
|
21
|
+
Because the database is adapted from an ODbL source, the ODbL's terms apply
|
|
22
|
+
to it. In short, and not as legal advice (the licence texts govern):
|
|
23
|
+
|
|
24
|
+
- Output generated from the data, such as countries returned by the library,
|
|
25
|
+
is a "Produced Work" (ODbL section 4.3). Anything you publish that is made
|
|
26
|
+
from it must carry a notice saying it uses data from mledoze/countries
|
|
27
|
+
under the ODbL. There is no share-alike requirement for Produced Works.
|
|
28
|
+
|
|
29
|
+
- If you publicly use or distribute the database itself, or a database
|
|
30
|
+
adapted from it (for example these files with your changes, or an
|
|
31
|
+
extension layer built from them), you must credit the sources above,
|
|
32
|
+
keep this notice, and offer that database under the ODbL v1.0
|
|
33
|
+
(share-alike, section 4.4). You may not restrict it with technical
|
|
34
|
+
measures unless you also offer it without them (section 4.7).
|
|
35
|
+
|
|
36
|
+
- The World Bank income classification also requires credit (CC BY 4.0).
|
|
37
|
+
|
|
38
|
+
== city-generator ==
|
|
39
|
+
|
|
40
|
+
The city data in the resources/ directory is derived from the GeoNames
|
|
41
|
+
geographical database (https://www.geonames.org/).
|
|
42
|
+
|
|
43
|
+
GeoNames data is licensed under the Creative Commons Attribution 4.0
|
|
44
|
+
International License (CC BY 4.0).
|
|
45
|
+
|
|
46
|
+
https://creativecommons.org/licenses/by/4.0/
|
|
47
|
+
|
|
48
|
+
You are free to share and adapt the data for any purpose, including
|
|
49
|
+
commercial use, provided you give appropriate credit to GeoNames.
|
|
50
|
+
|
|
51
|
+
== birth-generator ==
|
|
52
|
+
|
|
53
|
+
The demographic data in the resources/ directory is derived from:
|
|
54
|
+
|
|
55
|
+
1. United Nations World Population Prospects 2024 Revision
|
|
56
|
+
(https://population.un.org/wpp/)
|
|
57
|
+
Used under United Nations terms of use.
|
|
58
|
+
|
|
59
|
+
2. United Nations Demographic Yearbook — Statistics Division
|
|
60
|
+
(https://unstats.un.org/unsd/demographic-social/products/dyb/)
|
|
61
|
+
Used under United Nations terms of use.
|
|
62
|
+
|
|
63
|
+
3. World Bank Open Data (https://data.worldbank.org/)
|
|
64
|
+
Licensed under Creative Commons Attribution 4.0 International (CC BY 4.0).
|
|
65
|
+
https://creativecommons.org/licenses/by/4.0/
|
|
66
|
+
|
|
67
|
+
4. World Health Organization Global Health Observatory
|
|
68
|
+
(https://www.who.int/data/gho)
|
|
69
|
+
Used under WHO terms of use.
|
|
70
|
+
|
|
71
|
+
You are free to share and adapt the data for any purpose, including
|
|
72
|
+
commercial use, provided you give appropriate credit to the original sources.
|
|
73
|
+
|
|
74
|
+
== biodata-generator ==
|
|
75
|
+
|
|
76
|
+
The demographic data in the resources/ directory is derived from:
|
|
77
|
+
|
|
78
|
+
1. NCD Risk Factor Collaboration (NCD-RisC)
|
|
79
|
+
(https://ncdrisc.org/)
|
|
80
|
+
via Our World in Data (https://ourworldindata.org/)
|
|
81
|
+
Height means and standard deviations by country and sex.
|
|
82
|
+
Licensed under Creative Commons Attribution 4.0 International (CC BY 4.0).
|
|
83
|
+
https://creativecommons.org/licenses/by/4.0/
|
|
84
|
+
|
|
85
|
+
2. World Health Organization Global Health Observatory
|
|
86
|
+
(https://www.who.int/data/gho)
|
|
87
|
+
via Our World in Data (https://ourworldindata.org/)
|
|
88
|
+
BMI means by country and sex.
|
|
89
|
+
Used under WHO terms of use.
|
|
90
|
+
|
|
91
|
+
3. Published population studies (Wikipedia compilation)
|
|
92
|
+
Blood type frequency distributions by country.
|
|
93
|
+
Source data is in the public domain.
|
|
94
|
+
|
|
95
|
+
4. Katsara & Nothnagel 2019 + regional estimates
|
|
96
|
+
Eye colour frequency distributions.
|
|
97
|
+
|
|
98
|
+
5. VISAGE consortium + regional estimates
|
|
99
|
+
Hair colour frequency distributions.
|
|
100
|
+
|
|
101
|
+
6. WHO UV guidance + ethnic composition estimates
|
|
102
|
+
Skin type (Fitzpatrick scale) distributions.
|
|
103
|
+
|
|
104
|
+
7. Papadatou-Pastou et al. 2020 meta-analysis
|
|
105
|
+
Handedness frequency distributions.
|
|
106
|
+
|
|
107
|
+
You are free to share and adapt the data for any purpose, including
|
|
108
|
+
commercial use, provided you give appropriate credit to the original sources.
|
|
109
|
+
|
|
110
|
+
== name-generator ==
|
|
111
|
+
|
|
112
|
+
See https://github.com/dasmig/name-generator/blob/HEAD/doc/data.md (Source).
|
|
113
|
+
|
|
114
|
+
== interest-generator ==
|
|
115
|
+
|
|
116
|
+
Religion shares (resources/*/religion.tsv) come from the CIA World Factbook
|
|
117
|
+
("People and Society: Religions"), a US government work in the public domain,
|
|
118
|
+
as archived in factbook/factbook.json (CC0 1.0). About 25 countries have rows
|
|
119
|
+
written by hand for this library (resources/raw/religion_curated.tsv).
|
|
120
|
+
|
|
121
|
+
The sex, age and country factors of the activity classes (rows with basis
|
|
122
|
+
"eurostat HETUS 2000/2010" in resources/*/affinity.tsv) are derived from
|
|
123
|
+
Eurostat's Harmonised European Time Use Surveys, dataset tus_00age
|
|
124
|
+
(https://ec.europa.eu/eurostat/web/time-use-surveys). Source: Eurostat. Reuse
|
|
125
|
+
is authorised under Eurostat's reuse policy (Commission Decision
|
|
126
|
+
2011/833/EU) with acknowledgement of the source.
|
|
127
|
+
|
|
128
|
+
The hobby and genre catalogue (interests.tsv), the age bands, and all other
|
|
129
|
+
factor rows (basis "curated") were written for this library and are released
|
|
130
|
+
with it under the MIT License (see LICENSE.MIT), as are the grouping of
|
|
131
|
+
religions and the mapping from activity classes to hobbies. The Pew Research
|
|
132
|
+
Center's estimates, as republished by Our World in Data, were used only to
|
|
133
|
+
check the religion shares and are not part of this repository.
|
|
134
|
+
|
|
135
|
+
See doc/data.md.
|
|
136
|
+
|
|
137
|
+
== occupation-generator ==
|
|
138
|
+
|
|
139
|
+
Employment status, education, the share of employment by ISCO-08 group, and
|
|
140
|
+
the age and education factors (resources/*/status.tsv, education.tsv,
|
|
141
|
+
shares.tsv and factors.tsv) are derived from the International Labour
|
|
142
|
+
Organization's labour force statistics, ILOSTAT (https://ilostat.ilo.org),
|
|
143
|
+
indicators EMP_TEMP_SEX_OC2_NB_A, EMP_TEMP_SEX_AGE_OCU_NB_A,
|
|
144
|
+
EMP_TEMP_SEX_OCU_EDU_NB_A, POP_XWAP_SEX_AGE_EDU_NB_A, POP_XWAP_SEX_AGE_NB_A,
|
|
145
|
+
EMP_TEMP_SEX_AGE_NB_A and UNE_TUNE_SEX_AGE_NB_A, downloaded on 2026-10-14.
|
|
146
|
+
Source: ILOSTAT, ILO. ILOSTAT data is published under the Creative Commons
|
|
147
|
+
Attribution 4.0 International licence (CC BY 4.0); the values here are
|
|
148
|
+
percentages and factors computed from the ILO's counts, not the ILO's own
|
|
149
|
+
figures, and the ILO has not endorsed them.
|
|
150
|
+
|
|
151
|
+
The titles of the 43 ISCO-08 level-2 groups (resources/*/isco.tsv) follow the
|
|
152
|
+
International Standard Classification of Occupations 2008 (ILO).
|
|
153
|
+
|
|
154
|
+
The job catalogue (occupations.tsv: jobs, weights, age limits, tags, skills,
|
|
155
|
+
gear), the split of the inactive by age (inactivity.tsv), the education age
|
|
156
|
+
limits, and the mapping of jobs to ISCO-08 groups were written for this
|
|
157
|
+
library and are released with it under the MIT License (see LICENSE.MIT).
|
|
158
|
+
O*NET and ESCO were used as a guide to what skills a job has; no text from
|
|
159
|
+
either was copied.
|
|
160
|
+
|
|
161
|
+
Licences have not been reviewed (owner's decision, 2026-10-12): see
|
|
162
|
+
doc/data.md.
|
|
163
|
+
|
|
164
|
+
== trait-generator ==
|
|
165
|
+
|
|
166
|
+
The trait catalogue in the resources/ directory (traits, axes, loadings and
|
|
167
|
+
relations) was written for this library and is released with it under the
|
|
168
|
+
MIT License (see LICENSE.MIT). The directions of some Big Five loadings follow
|
|
169
|
+
published adjective markers (Goldberg 1992; Saucier 1994); no data from those
|
|
170
|
+
works is reproduced. See doc/data.md.
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Diego Dasso Migotto
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,117 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @dasmig/persona
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Complete fictional characters from one seeded call: name, country, city, birth date and age, body, personality, religion, hobbies, favourite media and job. It is [persona-generator](https://github.com/dasmig/persona-generator) (C++23) compiled to WebAssembly, with the `lite` data of the eight libraries it composes built in, so nothing is fetched at run time but the module's own `persona.wasm`.
|
|
4
|
+
|
|
5
|
+
The output is fictional sample data, not a description or prediction of a real person.
|
|
6
|
+
|
|
7
|
+
```js
|
|
8
|
+
import { createPersonaGenerator } from '@dasmig/persona';
|
|
9
|
+
|
|
10
|
+
createPersonaGenerator().then(personas => {
|
|
11
|
+
const p = personas.generate({ country: 'BR', sex: 'female', ages: { min: 25, max: 40 } });
|
|
12
|
+
console.log(p.name, p.age, p.city.name, p.occupation.job?.label);
|
|
13
|
+
|
|
14
|
+
// The same seed, request and data give the same persona, on every platform.
|
|
15
|
+
const again = personas.generate({ country: 'BR', sex: 'female', ages: { min: 25, max: 40 }, seed: p.signature.seed });
|
|
16
|
+
});
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
The package is an ES module only (no CommonJS build), for browsers, Web Workers and Node 20+.
|
|
20
|
+
|
|
21
|
+
## Loading
|
|
22
|
+
|
|
23
|
+
`createPersonaGenerator()` instantiates the module and loads the data of all eight libraries. That takes **a second or two** (about 1.4 s on a desktop, more on a phone) and blocks the thread it runs on, and a generator uses about 90 MB of memory. So:
|
|
24
|
+
|
|
25
|
+
- **Create one generator and reuse it.** Each persona then takes about a third of a millisecond.
|
|
26
|
+
- **In a browser, create it in a Web Worker** so the page stays responsive, and post the personas (plain JSON) back:
|
|
27
|
+
|
|
28
|
+
```js
|
|
29
|
+
// persona-worker.js, started with new Worker(new URL('./persona-worker.js', import.meta.url), { type: 'module' })
|
|
30
|
+
import { createPersonaGenerator } from '@dasmig/persona';
|
|
31
|
+
|
|
32
|
+
const ready = createPersonaGenerator();
|
|
33
|
+
onmessage = async ({ data: request }) => {
|
|
34
|
+
const personas = await ready;
|
|
35
|
+
try {
|
|
36
|
+
postMessage({ persona: personas.generate(request) });
|
|
37
|
+
} catch (error) {
|
|
38
|
+
postMessage({ error: error.message });
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
In a browser the module finds `persona.wasm` next to itself (`new URL('persona.wasm', import.meta.url)`). Vite and webpack 5 follow that and emit the file as an asset; webpack warns that it is over its recommended asset size, which is expected. To serve the file from elsewhere, pass Emscripten's `locateFile`, or pass its bytes as `wasmBinary`:
|
|
44
|
+
|
|
45
|
+
```js
|
|
46
|
+
const personas = await createPersonaGenerator({ locateFile: file => '/assets/' + file });
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
In Node, the package's `node` export reads `persona.wasm` from disk.
|
|
50
|
+
|
|
51
|
+
`persona.wasm` is 8.3 MB, most of it data (city and name lists): 2.8 MB with gzip, 2.2 MB with brotli. Serve it compressed.
|
|
52
|
+
|
|
53
|
+
## Request
|
|
54
|
+
|
|
55
|
+
Every field is optional. What is left out is drawn; what is given conditions the draws that depend on it. The country sets the city, the name culture, the body and the job; the age and sex set the rest.
|
|
56
|
+
|
|
57
|
+
| Field | Type | Default |
|
|
58
|
+
| --- | --- | --- |
|
|
59
|
+
| `country` | An ISO 3166-1 alpha-2 code from `personas.countries` (79 countries), any case | One of `personas.countries`, weighted by population |
|
|
60
|
+
| `sex` | `'male'` or `'female'` | Drawn with the birth |
|
|
61
|
+
| `ages` | `{ min, max }`, completed years, 0-120 | The country's age pyramid |
|
|
62
|
+
| `traits` | Number of personality traits, 0 to `personas.maxTraits` | 3 |
|
|
63
|
+
| `referenceDate` | `'YYYY-MM-DD'` or a `Date` (its local date), 1900-2100 | `'2023-07-01'` |
|
|
64
|
+
| `seed` | A `bigint`, a non-negative safe integer, or a **hexadecimal** string | Random |
|
|
65
|
+
|
|
66
|
+
- **Countries.** Only the countries in `personas.countries` have data in all eight libraries, so another code is an error rather than a persona with a name from the wrong culture.
|
|
67
|
+
- **Traits.** Fewer traits than asked are returned when no other trait is compatible with those already chosen (traits in one group exclude each other).
|
|
68
|
+
- **Dates.** The generator never reads the clock. Pass `referenceDate: new Date()` for ages as of today, and keep the date with the seed if the persona must replay.
|
|
69
|
+
- **Seeds.** A seed is a 64-bit unsigned integer. A string is read as hexadecimal, optionally after `0x`, which is the form of `signature.seed`: `seed: '42'` is 0x42 (66), not 42. Use a `bigint` or a hexadecimal string for seeds above `Number.MAX_SAFE_INTEGER`.
|
|
70
|
+
|
|
71
|
+
An invalid request throws before anything is generated: a `TypeError` for a field of the wrong type or an unknown field (a misspelt `contry` is an error, not a random country), and a `RangeError` for a value out of range. The message says which field and why. A failed call leaves the generator usable.
|
|
72
|
+
|
|
73
|
+
## Result
|
|
74
|
+
|
|
75
|
+
`generate()` returns a `Persona` (see `index.d.ts`). It has `name`, `sex`, `age`, `admin1` (the state or province) and `signature`, and each library's own result under `country`, `city`, `birth`, `biodata`, `personality`, `interest` and `occupation`. `generateJson()` returns the same as JSON text, exactly as the C++ library writes it.
|
|
76
|
+
|
|
77
|
+
Each part draws from its own random stream, split from the persona's seed. Forcing one choice does not shift the unrelated parts: the same seed with the other sex keeps the city and the personality.
|
|
78
|
+
|
|
79
|
+
## Batches
|
|
80
|
+
|
|
81
|
+
For test data and populations, generate many personas from one batch seed:
|
|
82
|
+
|
|
83
|
+
```js
|
|
84
|
+
// 10,000 personas, as objects or as JSON Lines (one persona per line).
|
|
85
|
+
const people = personas.generateBatch({ request: { country: 'BR' }, count: 10000, seed: 42 });
|
|
86
|
+
const lines = personas.generateBatchJsonl({ request: { country: 'BR' }, count: 10000, seed: 42 });
|
|
87
|
+
|
|
88
|
+
// The same personas, split across workers (Web Workers in a browser, worker threads in Node).
|
|
89
|
+
import { createPersonaPool } from '@dasmig/persona';
|
|
90
|
+
const pool = await createPersonaPool({ workers: 4 });
|
|
91
|
+
const same = await pool.generateBatchJsonl({ request: { country: 'BR' }, count: 10000, seed: 42 });
|
|
92
|
+
pool.close();
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
- **Persona `i` of a batch** is exactly `generate({ ...request, seed })` for the seed derived from the batch seed and `i` (`core::item_seed` in the C++ libraries). So any persona replays on its own from its `signature.seed`, a range can be generated alone (`first: 1000` starts at persona 1000), two batches that follow each other equal one, and the output is the same for any pool size.
|
|
96
|
+
- **Options.** `request` (the fields above except `seed`), `count` (0 to 2^31 - 1), `seed` (a random one if left out, which makes the batch unrepeatable) and `first` (default 0).
|
|
97
|
+
- **Speed.** About 14,000 personas per second on one generator in Node 24, 20,000 with a pool of 2 and 38,000 with a pool of 4, once the pool has started (about half a second). The JSON Lines form skips building the objects.
|
|
98
|
+
- **Memory.** Every worker loads its own module, about 60 MB, and a pool defaults to the number of cores, at most 4. The pool needs no `SharedArrayBuffer`, so it works on any page: pieces travel as strings.
|
|
99
|
+
- In a browser the pool starts module Web Workers from `pool-worker.mjs` next to the package's entry; bundlers that follow `new URL(..., import.meta.url)` pick it up.
|
|
100
|
+
|
|
101
|
+
## Replaying personas
|
|
102
|
+
|
|
103
|
+
To recreate a persona later (for example, from a saved game), store its `signature.seed` and the request it was made with, including `referenceDate`.
|
|
104
|
+
|
|
105
|
+
The same seed and request give the same persona **only with the same data**. `personas.dataHash` identifies the data of all eight libraries, and `personas.versions` lists the library versions the module was built from. Store `dataHash` with the seed: if a package update changes it, the saved seed gives a different persona.
|
|
106
|
+
|
|
107
|
+
Each part of a persona also has a `signature`, with its own seed and its library's data hash.
|
|
108
|
+
|
|
109
|
+
## Data
|
|
110
|
+
|
|
111
|
+
The data comes from open datasets (GeoNames, mledoze/countries, UN World Population Prospects, NCD-RisC, the CIA World Factbook, ILOSTAT and others) and from catalogues written for the libraries. `DATA_LICENSES.txt` lists each source and its licence. The names come from [name-dataset](https://github.com/philipperemy/name-dataset); see name-generator's [data notes](https://github.com/dasmig/name-generator/blob/HEAD/doc/data.md) for its provenance.
|
|
112
|
+
|
|
113
|
+
Each library documents its data's limits in its own `doc/data.md`.
|
|
114
|
+
|
|
115
|
+
## Licence
|
|
116
|
+
|
|
117
|
+
MIT for the code; see `DATA_LICENSES.txt` for the data.
|
package/batch.mjs
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
// Batch options, checked the same way by the generator and by the worker pool.
|
|
2
|
+
// No WebAssembly here: the pool uses it to split a batch before any worker
|
|
3
|
+
// has loaded the module.
|
|
4
|
+
//
|
|
5
|
+
// A batch is `count` personas from one batch seed; persona `i` is exactly the
|
|
6
|
+
// single call with the seed core::item_seed(batchSeed, i), so a range of a
|
|
7
|
+
// batch can be generated alone and the pieces joined.
|
|
8
|
+
|
|
9
|
+
const batchKeys = new Set(['request', 'count', 'seed', 'first']);
|
|
10
|
+
const maxSeed = (1n << 64n) - 1n;
|
|
11
|
+
|
|
12
|
+
export function malformed(message) {
|
|
13
|
+
throw new TypeError('persona request: ' + message);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function outOfRange(message) {
|
|
17
|
+
throw new RangeError('persona request: ' + message);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function integer(value, name, min, max) {
|
|
21
|
+
if (typeof value !== 'number' || !Number.isInteger(value)) malformed(`${name} must be an integer`);
|
|
22
|
+
if (value < min || value > max) outOfRange(`${name} must be from ${min} to ${max}, not ${value}`);
|
|
23
|
+
return value;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// The seed as the module takes it: 1 to 16 lowercase hexadecimal digits.
|
|
27
|
+
export function seedText(seed) {
|
|
28
|
+
if (seed === undefined) return '';
|
|
29
|
+
if (typeof seed === 'string') {
|
|
30
|
+
const match = /^(?:0x)?([0-9a-f]{1,16})$/i.exec(seed);
|
|
31
|
+
if (!match) outOfRange(`seed string must be 1 to 16 hexadecimal digits, optionally after 0x, not "${seed}"`);
|
|
32
|
+
return match[1].toLowerCase();
|
|
33
|
+
}
|
|
34
|
+
if (typeof seed === 'bigint') {
|
|
35
|
+
if (seed < 0n || seed > maxSeed) outOfRange('seed must be an unsigned 64-bit integer');
|
|
36
|
+
return seed.toString(16);
|
|
37
|
+
}
|
|
38
|
+
if (typeof seed === 'number') {
|
|
39
|
+
if (!Number.isSafeInteger(seed) || seed < 0)
|
|
40
|
+
outOfRange('seed number must be a non-negative safe integer; use a bigint or a hexadecimal string for larger seeds');
|
|
41
|
+
return seed.toString(16);
|
|
42
|
+
}
|
|
43
|
+
return malformed('seed must be a hexadecimal string, a bigint or a number');
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function randomSeed() {
|
|
47
|
+
const words = globalThis.crypto.getRandomValues(new Uint32Array(2));
|
|
48
|
+
return words[0].toString(16).padStart(8, '0') + words[1].toString(16).padStart(8, '0');
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Check batch options and fill in the defaults.
|
|
53
|
+
* @returns {{ request: object, count: number, first: number, seed: string }} The seed is always set
|
|
54
|
+
* (drawn when the caller gave none), so every piece of a split batch uses the same one.
|
|
55
|
+
*/
|
|
56
|
+
export function batchPlan(options) {
|
|
57
|
+
if (options === null || typeof options !== 'object' || Array.isArray(options)) malformed('batch options must be an object');
|
|
58
|
+
for (const key of Object.keys(options))
|
|
59
|
+
if (!batchKeys.has(key)) malformed(`unknown batch field "${key}"; the fields are ${[...batchKeys].join(', ')}`);
|
|
60
|
+
const { request = {}, count, seed, first = 0 } = options;
|
|
61
|
+
if (count === undefined) malformed('count is required');
|
|
62
|
+
if (request === null || typeof request !== 'object' || Array.isArray(request)) malformed('request must be an object');
|
|
63
|
+
if ('seed' in request) malformed('the batch has its own seed; set it on the batch options, not on the request');
|
|
64
|
+
return {
|
|
65
|
+
request,
|
|
66
|
+
count: integer(count, 'count', 0, 2 ** 31 - 1),
|
|
67
|
+
first: integer(first, 'first', 0, Number.MAX_SAFE_INTEGER),
|
|
68
|
+
seed: seed === undefined ? randomSeed() : seedText(seed)
|
|
69
|
+
};
|
|
70
|
+
}
|
package/generator.mjs
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
// The generator over the WebAssembly module's C API (src/persona_api.cpp),
|
|
2
|
+
// shared by the browser entry (index.mjs) and the Node entry (node.mjs). It
|
|
3
|
+
// checks the request, passes it on and parses the JSON that comes back.
|
|
4
|
+
import { versions } from './versions.mjs';
|
|
5
|
+
import { batchPlan, integer, malformed, outOfRange, seedText } from './batch.mjs';
|
|
6
|
+
|
|
7
|
+
const keys = new Set(['country', 'sex', 'ages', 'traits', 'referenceDate', 'seed']);
|
|
8
|
+
const sexes = new Set(['male', 'female']);
|
|
9
|
+
|
|
10
|
+
function dateText(date) {
|
|
11
|
+
if (date === undefined) return '';
|
|
12
|
+
let year, month, day;
|
|
13
|
+
if (date instanceof Date) {
|
|
14
|
+
if (Number.isNaN(date.getTime())) outOfRange('referenceDate is an invalid Date');
|
|
15
|
+
[year, month, day] = [date.getFullYear(), date.getMonth() + 1, date.getDate()];
|
|
16
|
+
} else if (typeof date === 'string') {
|
|
17
|
+
const match = /^(\d{4})-(\d{2})-(\d{2})$/.exec(date);
|
|
18
|
+
if (!match) outOfRange(`referenceDate must be YYYY-MM-DD, not "${date}"`);
|
|
19
|
+
[year, month, day] = match.slice(1).map(Number);
|
|
20
|
+
const check = new Date(Date.UTC(year, month - 1, day));
|
|
21
|
+
if (check.getUTCMonth() !== month - 1 || check.getUTCDate() !== day) outOfRange(`referenceDate ${date} is not a calendar date`);
|
|
22
|
+
} else {
|
|
23
|
+
return malformed('referenceDate must be a YYYY-MM-DD string or a Date');
|
|
24
|
+
}
|
|
25
|
+
if (year < 1900 || year > 2100) outOfRange('referenceDate must be between 1900 and 2100');
|
|
26
|
+
const pad = n => String(n).padStart(2, '0');
|
|
27
|
+
return `${year}-${pad(month)}-${pad(day)}`;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function parse(json) {
|
|
31
|
+
const result = JSON.parse(json);
|
|
32
|
+
if (result.error) throw new Error(result.error);
|
|
33
|
+
return result;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Wrap an instantiated module.
|
|
38
|
+
* @param {Function} createModule The Emscripten factory (persona.mjs).
|
|
39
|
+
* @param {object} moduleOptions Emscripten module options.
|
|
40
|
+
*/
|
|
41
|
+
export async function createGenerator(createModule, moduleOptions) {
|
|
42
|
+
const module = await createModule(moduleOptions);
|
|
43
|
+
const generate = module.cwrap('dasmig_persona_generate', 'string',
|
|
44
|
+
['string', 'string', 'number', 'number', 'number', 'string', 'string']);
|
|
45
|
+
const generateBatchRaw = module.cwrap('dasmig_persona_generate_batch', 'string',
|
|
46
|
+
['string', 'string', 'number', 'number', 'number', 'string', 'string', 'number', 'number']);
|
|
47
|
+
const requestKeys = new Set([...keys].filter(key => key !== 'seed'));
|
|
48
|
+
const info = parse(module.cwrap('dasmig_persona_info', 'string', [])());
|
|
49
|
+
const countries = Object.freeze([...info.countries]);
|
|
50
|
+
const supported = new Set(countries);
|
|
51
|
+
|
|
52
|
+
// The request's fields as the module's arguments (everything but the seed).
|
|
53
|
+
function requestArgs(request, allowed) {
|
|
54
|
+
if (request === null || typeof request !== 'object' || Array.isArray(request)) malformed('must be an object');
|
|
55
|
+
for (const key of Object.keys(request))
|
|
56
|
+
if (!allowed.has(key)) malformed(`unknown field "${key}"; the fields are ${[...allowed].join(', ')}`);
|
|
57
|
+
const { country, sex, ages, traits, referenceDate } = request;
|
|
58
|
+
|
|
59
|
+
let code = '';
|
|
60
|
+
if (country !== undefined) {
|
|
61
|
+
if (typeof country !== 'string') malformed('country must be a string');
|
|
62
|
+
code = country.toUpperCase();
|
|
63
|
+
if (!supported.has(code))
|
|
64
|
+
outOfRange(`country "${country}" is not supported; the supported countries are the generator's countries list`);
|
|
65
|
+
}
|
|
66
|
+
if (sex !== undefined && !sexes.has(sex))
|
|
67
|
+
(typeof sex === 'string' ? outOfRange : malformed)(`sex must be "male" or "female", not ${JSON.stringify(sex)}`);
|
|
68
|
+
let min = -1, max = -1;
|
|
69
|
+
if (ages !== undefined) {
|
|
70
|
+
if (ages === null || typeof ages !== 'object') malformed('ages must be { min, max }');
|
|
71
|
+
min = integer(ages.min, 'ages.min', 0, 120);
|
|
72
|
+
max = integer(ages.max, 'ages.max', 0, 120);
|
|
73
|
+
if (min > max) outOfRange(`ages.min (${min}) must not be greater than ages.max (${max})`);
|
|
74
|
+
}
|
|
75
|
+
const count = traits === undefined ? -1 : integer(traits, 'traits', 0, info.max_traits);
|
|
76
|
+
return [code, sex ?? '', min, max, count, dateText(referenceDate)];
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function generateJson(request = {}) {
|
|
80
|
+
return generate(...requestArgs(request, keys), seedText(request.seed));
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function generateBatchJsonl(options) {
|
|
84
|
+
const plan = batchPlan(options);
|
|
85
|
+
const text = generateBatchRaw(...requestArgs(plan.request, requestKeys), plan.seed, plan.first, plan.count);
|
|
86
|
+
if (text.startsWith('{"error"')) throw new Error(JSON.parse(text).error);
|
|
87
|
+
return text;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
return Object.freeze({
|
|
91
|
+
countries,
|
|
92
|
+
maxTraits: info.max_traits,
|
|
93
|
+
dataHash: info.data,
|
|
94
|
+
versions,
|
|
95
|
+
generateJson(request) {
|
|
96
|
+
const json = generateJson(request);
|
|
97
|
+
parse(json);
|
|
98
|
+
return json;
|
|
99
|
+
},
|
|
100
|
+
generate(request) {
|
|
101
|
+
return parse(generateJson(request));
|
|
102
|
+
},
|
|
103
|
+
generateBatchJsonl,
|
|
104
|
+
generateBatch(options) {
|
|
105
|
+
const text = generateBatchJsonl(options);
|
|
106
|
+
return text === '' ? [] : text.slice(0, -1).split('\n').map(line => JSON.parse(line));
|
|
107
|
+
}
|
|
108
|
+
});
|
|
109
|
+
}
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,313 @@
|
|
|
1
|
+
// Types for the dasmig persona generator. The JSON shapes are those of the
|
|
2
|
+
// C++ libraries' to_json(); see each library's doc/usage.md.
|
|
3
|
+
|
|
4
|
+
/** What a persona call asks for. Every field is optional: what is left out is drawn. */
|
|
5
|
+
export interface PersonaRequest {
|
|
6
|
+
/**
|
|
7
|
+
* ISO 3166-1 alpha-2 code, any case, from the generator's `countries`; another code is a RangeError.
|
|
8
|
+
* Unset draws one of `countries`, weighted by population.
|
|
9
|
+
*/
|
|
10
|
+
country?: string;
|
|
11
|
+
/** Unset draws it with the birth. */
|
|
12
|
+
sex?: Sex;
|
|
13
|
+
/** Inclusive age range in completed years (0-120). Unset draws from the country's age pyramid. */
|
|
14
|
+
ages?: { min: number; max: number };
|
|
15
|
+
/**
|
|
16
|
+
* Number of personality traits, 0 to the generator's `maxTraits`; default 3. Fewer are returned
|
|
17
|
+
* when no other trait is compatible with those already chosen (traits in one group exclude each other).
|
|
18
|
+
*/
|
|
19
|
+
traits?: number;
|
|
20
|
+
/**
|
|
21
|
+
* The date ages are computed at: `YYYY-MM-DD`, or a Date (its local calendar date).
|
|
22
|
+
* Default 2023-07-01. The generator never reads the clock, so pass today's date for ages "as of now".
|
|
23
|
+
*/
|
|
24
|
+
referenceDate?: string | Date;
|
|
25
|
+
/**
|
|
26
|
+
* The call seed, a 64-bit unsigned integer: a bigint, a non-negative safe integer, or a string
|
|
27
|
+
* of 1-16 **hexadecimal** digits with an optional `0x` (so `'42'` is 0x42, not 42), the form of
|
|
28
|
+
* `signature.seed`. Unset draws a random one. The same seed, request and data give the same persona.
|
|
29
|
+
*/
|
|
30
|
+
seed?: string | bigint | number;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export type Sex = 'male' | 'female';
|
|
34
|
+
|
|
35
|
+
/** The seed a result was generated from and a hash of the data it used (16 hex digits each). */
|
|
36
|
+
export interface Signature {
|
|
37
|
+
seed: string;
|
|
38
|
+
data: string;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export interface Country {
|
|
42
|
+
cca2: string;
|
|
43
|
+
cca3: string;
|
|
44
|
+
ccn3: string;
|
|
45
|
+
name_common: string;
|
|
46
|
+
name_official: string;
|
|
47
|
+
capital: string[];
|
|
48
|
+
region: string;
|
|
49
|
+
subregion: string;
|
|
50
|
+
continent: string[];
|
|
51
|
+
latitude: number;
|
|
52
|
+
longitude: number;
|
|
53
|
+
area: number;
|
|
54
|
+
population: number;
|
|
55
|
+
landlocked: boolean;
|
|
56
|
+
independent: boolean;
|
|
57
|
+
un_member: boolean;
|
|
58
|
+
languages: string[];
|
|
59
|
+
currency_code: string;
|
|
60
|
+
currency_name: string;
|
|
61
|
+
currency_symbol: string;
|
|
62
|
+
borders: string[];
|
|
63
|
+
timezones: string[];
|
|
64
|
+
driving_side: string;
|
|
65
|
+
tld: string;
|
|
66
|
+
idd_root: string;
|
|
67
|
+
idd_suffix: string;
|
|
68
|
+
demonym_m: string;
|
|
69
|
+
demonym_f: string;
|
|
70
|
+
flag_emoji: string;
|
|
71
|
+
income_level: string;
|
|
72
|
+
start_of_week: string;
|
|
73
|
+
signature: Signature;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export interface City {
|
|
77
|
+
geonameid: number;
|
|
78
|
+
name: string;
|
|
79
|
+
asciiname: string;
|
|
80
|
+
latitude: number;
|
|
81
|
+
longitude: number;
|
|
82
|
+
feature_code: string;
|
|
83
|
+
country_code: string;
|
|
84
|
+
cc2: string;
|
|
85
|
+
admin1_code: string;
|
|
86
|
+
admin2_code: string;
|
|
87
|
+
admin3_code: string;
|
|
88
|
+
admin4_code: string;
|
|
89
|
+
population: number;
|
|
90
|
+
/** Metres; null when unknown. */
|
|
91
|
+
elevation: number | null;
|
|
92
|
+
/** Digital elevation model, metres. */
|
|
93
|
+
dem: number;
|
|
94
|
+
timezone: string;
|
|
95
|
+
signature: Signature;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export interface Birth {
|
|
99
|
+
country_code: string;
|
|
100
|
+
/** YYYY-MM-DD. */
|
|
101
|
+
date: string;
|
|
102
|
+
year: number;
|
|
103
|
+
month: number;
|
|
104
|
+
day: number;
|
|
105
|
+
/** Completed years at the reference date. */
|
|
106
|
+
age: number;
|
|
107
|
+
sex: Sex;
|
|
108
|
+
/** 0 = Sunday. */
|
|
109
|
+
weekday: number;
|
|
110
|
+
/** Remaining life expectancy at that age, years (a population model, not a forecast). */
|
|
111
|
+
le_remaining: number;
|
|
112
|
+
cohort: string;
|
|
113
|
+
signature: Signature;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/** `intermediate` covers green, hazel and amber. */
|
|
117
|
+
export type EyeColor = 'blue' | 'intermediate' | 'brown';
|
|
118
|
+
/** `grey` only at older ages. */
|
|
119
|
+
export type HairColor = 'black' | 'brown' | 'blond' | 'red' | 'grey';
|
|
120
|
+
/** Fitzpatrick skin phototype. */
|
|
121
|
+
export type SkinType = 'I' | 'II' | 'III' | 'IV' | 'V' | 'VI';
|
|
122
|
+
export type BloodType = 'O+' | 'O-' | 'A+' | 'A-' | 'B+' | 'B-' | 'AB+' | 'AB-';
|
|
123
|
+
export type Handedness = 'right' | 'left';
|
|
124
|
+
|
|
125
|
+
export interface Biodata {
|
|
126
|
+
country: string;
|
|
127
|
+
sex: Sex;
|
|
128
|
+
height_cm: number;
|
|
129
|
+
weight_kg: number;
|
|
130
|
+
bmi: number;
|
|
131
|
+
eyes: EyeColor;
|
|
132
|
+
hair: HairColor;
|
|
133
|
+
skin: SkinType;
|
|
134
|
+
blood: BloodType;
|
|
135
|
+
hand: Handedness;
|
|
136
|
+
age: number;
|
|
137
|
+
signature: Signature;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
export interface Trait {
|
|
141
|
+
key: string;
|
|
142
|
+
label: string;
|
|
143
|
+
/** The exclusive group the trait belongs to, if any. */
|
|
144
|
+
group: string | null;
|
|
145
|
+
tags: string[];
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
export interface Personality {
|
|
149
|
+
traits: Trait[];
|
|
150
|
+
/** Big Five scores: centered on zero, unbounded model values, not percentiles. */
|
|
151
|
+
axes: {
|
|
152
|
+
openness: number;
|
|
153
|
+
conscientiousness: number;
|
|
154
|
+
extraversion: number;
|
|
155
|
+
agreeableness: number;
|
|
156
|
+
neuroticism: number;
|
|
157
|
+
};
|
|
158
|
+
signature: Signature;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
export interface InterestItem {
|
|
162
|
+
key: string;
|
|
163
|
+
label: string;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
export interface Interest {
|
|
167
|
+
country: string;
|
|
168
|
+
religion: string;
|
|
169
|
+
religion_group: string;
|
|
170
|
+
hobbies: InterestItem[];
|
|
171
|
+
films: InterestItem[];
|
|
172
|
+
games: InterestItem[];
|
|
173
|
+
books: InterestItem[];
|
|
174
|
+
music: InterestItem[];
|
|
175
|
+
signature: Signature;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
export type EmploymentStatus = 'employed' | 'unemployed' | 'student' | 'retired' | 'inactive';
|
|
179
|
+
export type EducationLevel = 'less_than_basic' | 'basic' | 'intermediate' | 'advanced';
|
|
180
|
+
|
|
181
|
+
export interface Job {
|
|
182
|
+
key: string;
|
|
183
|
+
label: string;
|
|
184
|
+
/** ISCO-08 major or sub-major group code. */
|
|
185
|
+
isco: string;
|
|
186
|
+
isco_label: string;
|
|
187
|
+
/** ISCO-08 unit group code. */
|
|
188
|
+
unit: string;
|
|
189
|
+
/** ISCO-08 skill level, "1" to "4". */
|
|
190
|
+
skill: string;
|
|
191
|
+
skills: string[];
|
|
192
|
+
gear: string[];
|
|
193
|
+
tags: string[];
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
export interface Occupation {
|
|
197
|
+
country: string;
|
|
198
|
+
sex: Sex;
|
|
199
|
+
age: number;
|
|
200
|
+
status: EmploymentStatus;
|
|
201
|
+
education: EducationLevel | null;
|
|
202
|
+
/** Null unless employed. */
|
|
203
|
+
job: Job | null;
|
|
204
|
+
signature: Signature;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/** One persona: each library's result, as that library writes it. */
|
|
208
|
+
export interface Persona {
|
|
209
|
+
name: string;
|
|
210
|
+
sex: Sex;
|
|
211
|
+
age: number;
|
|
212
|
+
/** The city's state or province. */
|
|
213
|
+
admin1: string;
|
|
214
|
+
country: Country;
|
|
215
|
+
city: City;
|
|
216
|
+
birth: Birth;
|
|
217
|
+
biodata: Biodata;
|
|
218
|
+
personality: Personality;
|
|
219
|
+
interest: Interest;
|
|
220
|
+
occupation: Occupation;
|
|
221
|
+
/** `generate({ ...request, seed: persona.signature.seed })` replays this persona. */
|
|
222
|
+
signature: Signature;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
export interface PersonaGenerator {
|
|
226
|
+
/** The countries a persona can be generated for (ISO 3166-1 alpha-2, uppercase, sorted). */
|
|
227
|
+
readonly countries: readonly string[];
|
|
228
|
+
/** The most personality traits a request can ask for: the trait catalogue's size. */
|
|
229
|
+
readonly maxTraits: number;
|
|
230
|
+
/**
|
|
231
|
+
* Hash of the data of all eight libraries (16 hex digits). A saved seed replays the same persona
|
|
232
|
+
* only while this is unchanged; it changes when a package update changes any library's data.
|
|
233
|
+
*/
|
|
234
|
+
readonly dataHash: string;
|
|
235
|
+
/** The library versions (git tags) this module was built from. */
|
|
236
|
+
readonly versions: Readonly<Record<string, string>>;
|
|
237
|
+
/**
|
|
238
|
+
* Generate a persona. Takes about a third of a millisecond.
|
|
239
|
+
* @throws {TypeError} A request field of the wrong type, or an unknown field.
|
|
240
|
+
* @throws {RangeError} A field outside its range: an unsupported country, ages, traits, date or seed.
|
|
241
|
+
*/
|
|
242
|
+
generate(request?: PersonaRequest): Persona;
|
|
243
|
+
/** Generate a persona as the JSON text the module returns, exactly as the C++ library writes it. */
|
|
244
|
+
generateJson(request?: PersonaRequest): string;
|
|
245
|
+
/**
|
|
246
|
+
* Generate a batch on this thread. Persona `i` is `generate({ ...request, seed })` for the seed
|
|
247
|
+
* derived from the batch seed and `first + i`, so any persona replays on its own, a range can be
|
|
248
|
+
* generated alone, and two batches that follow each other equal one. For large batches use a
|
|
249
|
+
* pool (`createPersonaPool`), which splits the work across workers with the same result.
|
|
250
|
+
* @throws {TypeError} A field of the wrong type, an unknown field, or a seed inside the request.
|
|
251
|
+
* @throws {RangeError} A count, first index, seed, country or other field out of range.
|
|
252
|
+
*/
|
|
253
|
+
generateBatch(options: BatchOptions): Persona[];
|
|
254
|
+
/** The same batch as JSON Lines: one persona per line, each line ending in `\n`. Empty for a count of 0. */
|
|
255
|
+
generateBatchJsonl(options: BatchOptions): string;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
/** What a batch asks for. */
|
|
259
|
+
export interface BatchOptions {
|
|
260
|
+
/** What every persona is generated from (the same fields as `generate`, but no `seed`). */
|
|
261
|
+
request?: Omit<PersonaRequest, 'seed'>;
|
|
262
|
+
/** Number of personas, 0 to 2^31 - 1. */
|
|
263
|
+
count: number;
|
|
264
|
+
/**
|
|
265
|
+
* The batch seed, in the forms `PersonaRequest.seed` takes. Unset draws a random one, so the batch
|
|
266
|
+
* cannot be repeated; each persona's `signature.seed` still replays that persona alone.
|
|
267
|
+
*/
|
|
268
|
+
seed?: string | bigint | number;
|
|
269
|
+
/** Index of the first persona, default 0: `first: 1000` starts at persona 1000 of the same batch. */
|
|
270
|
+
first?: number;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
/** A pool of workers that generates batches in pieces and joins them in order. */
|
|
274
|
+
export interface PersonaPool {
|
|
275
|
+
/** Number of workers. */
|
|
276
|
+
readonly size: number;
|
|
277
|
+
/** Same personas as `PersonaGenerator.generateBatch` with the same options, for any pool size. */
|
|
278
|
+
generateBatch(options: BatchOptions): Promise<Persona[]>;
|
|
279
|
+
/** Same text as `PersonaGenerator.generateBatchJsonl`. */
|
|
280
|
+
generateBatchJsonl(options: BatchOptions): Promise<string>;
|
|
281
|
+
/** Stop the workers. The pool cannot be used afterwards. */
|
|
282
|
+
close(): void;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
export interface PoolOptions {
|
|
286
|
+
/**
|
|
287
|
+
* Number of workers, 1 to 64. Each loads its own copy of the module, about 60 MB, and takes a
|
|
288
|
+
* second or two to start. Default: the number of cores, at most 4.
|
|
289
|
+
*/
|
|
290
|
+
workers?: number;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
/** Emscripten module options this package passes on. */
|
|
294
|
+
export interface ModuleOptions {
|
|
295
|
+
/** Map a file the module loads (`persona.wasm`) to its URL. */
|
|
296
|
+
locateFile?: (path: string, prefix: string) => string;
|
|
297
|
+
/** The contents of persona.wasm, to skip fetching it. */
|
|
298
|
+
wasmBinary?: ArrayBuffer | Uint8Array;
|
|
299
|
+
[option: string]: unknown;
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
/**
|
|
303
|
+
* Load the WebAssembly module and its data, and return a generator. Loading takes a second or
|
|
304
|
+
* two and blocks the thread it runs on, so create one generator, reuse it, and in a browser
|
|
305
|
+
* prefer a Web Worker.
|
|
306
|
+
*/
|
|
307
|
+
export function createPersonaGenerator(moduleOptions?: ModuleOptions): Promise<PersonaGenerator>;
|
|
308
|
+
|
|
309
|
+
/**
|
|
310
|
+
* Start a pool of Web Workers (worker threads in Node), each with its own module. Resolves when all
|
|
311
|
+
* are ready. Call `close()` when done.
|
|
312
|
+
*/
|
|
313
|
+
export function createPersonaPool(options?: PoolOptions): Promise<PersonaPool>;
|
package/index.mjs
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
// dasmig persona generator, browser and worker entry. The module finds
|
|
2
|
+
// persona.wasm next to itself (new URL('persona.wasm', import.meta.url)),
|
|
3
|
+
// which bundlers follow; pass locateFile or wasmBinary to load it otherwise.
|
|
4
|
+
import createPersonaModule from './persona.mjs';
|
|
5
|
+
import { createGenerator } from './generator.mjs';
|
|
6
|
+
import { createPool, poolSize } from './pool.mjs';
|
|
7
|
+
|
|
8
|
+
export function createPersonaGenerator(moduleOptions = {}) {
|
|
9
|
+
return createGenerator(createPersonaModule, moduleOptions);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
// A pool of module Web Workers (pool-worker.mjs, next to this file) that
|
|
13
|
+
// generates batches in pieces and joins them in order. Each worker loads its
|
|
14
|
+
// own copy of the module, about 60 MB, so the default is at most 4.
|
|
15
|
+
export async function createPersonaPool(options = {}) {
|
|
16
|
+
const size = poolSize(options, Math.min(4, globalThis.navigator?.hardwareConcurrency ?? 2));
|
|
17
|
+
return createPool(() => {
|
|
18
|
+
const worker = new Worker(new URL('./pool-worker.mjs', import.meta.url), { type: 'module' });
|
|
19
|
+
return {
|
|
20
|
+
send: message => worker.postMessage(message),
|
|
21
|
+
onMessage: handler => { worker.onmessage = event => handler(event.data); },
|
|
22
|
+
onError: handler => { worker.onerror = event => handler(new Error(event.message || 'pool worker failed')); },
|
|
23
|
+
terminate: () => worker.terminate()
|
|
24
|
+
};
|
|
25
|
+
}, size);
|
|
26
|
+
}
|
package/node.mjs
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
// dasmig persona generator, Node entry (the package's "node" export
|
|
2
|
+
// condition). The module is built for browsers and workers only, so it has no
|
|
3
|
+
// Node code for bundlers to trip on; here the .wasm file is read from disk and
|
|
4
|
+
// handed to it.
|
|
5
|
+
import { readFile } from 'node:fs/promises';
|
|
6
|
+
import { availableParallelism } from 'node:os';
|
|
7
|
+
import { Worker } from 'node:worker_threads';
|
|
8
|
+
import createPersonaModule from './persona.mjs';
|
|
9
|
+
import { createGenerator } from './generator.mjs';
|
|
10
|
+
import { createPool, poolSize } from './pool.mjs';
|
|
11
|
+
|
|
12
|
+
export async function createPersonaGenerator(moduleOptions = {}) {
|
|
13
|
+
const wasmBinary = moduleOptions.wasmBinary ?? await readFile(new URL('./persona.wasm', import.meta.url));
|
|
14
|
+
return createGenerator(createPersonaModule, { ...moduleOptions, wasmBinary });
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
// A pool of worker threads (pool-worker-node.mjs, next to this file) that
|
|
18
|
+
// generates batches in pieces and joins them in order. Each worker loads its
|
|
19
|
+
// own copy of the module, about 60 MB, so the default is at most 4.
|
|
20
|
+
export async function createPersonaPool(options = {}) {
|
|
21
|
+
const size = poolSize(options, Math.min(4, availableParallelism()));
|
|
22
|
+
return createPool(() => {
|
|
23
|
+
const worker = new Worker(new URL('./pool-worker-node.mjs', import.meta.url));
|
|
24
|
+
return {
|
|
25
|
+
send: message => worker.postMessage(message),
|
|
26
|
+
onMessage: handler => { worker.on('message', handler); },
|
|
27
|
+
onError: handler => { worker.on('error', handler); },
|
|
28
|
+
terminate: () => { worker.terminate(); }
|
|
29
|
+
};
|
|
30
|
+
}, size);
|
|
31
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,57 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dasmig/persona",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Complete fictional characters (name, country, city, birth, body, personality, interests, job) from one seeded call, in WebAssembly.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./index.mjs",
|
|
7
|
+
"types": "./index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./index.d.ts",
|
|
11
|
+
"node": "./node.mjs",
|
|
12
|
+
"default": "./index.mjs"
|
|
13
|
+
},
|
|
14
|
+
"./persona.wasm": "./persona.wasm",
|
|
15
|
+
"./package.json": "./package.json"
|
|
16
|
+
},
|
|
17
|
+
"files": [
|
|
18
|
+
"index.mjs",
|
|
19
|
+
"node.mjs",
|
|
20
|
+
"generator.mjs",
|
|
21
|
+
"pool-worker-core.mjs",
|
|
22
|
+
"pool-worker-node.mjs",
|
|
23
|
+
"pool-worker.mjs",
|
|
24
|
+
"pool.mjs",
|
|
25
|
+
"batch.mjs",
|
|
26
|
+
"versions.mjs",
|
|
27
|
+
"index.d.ts",
|
|
28
|
+
"persona.mjs",
|
|
29
|
+
"persona.wasm",
|
|
30
|
+
"README.md",
|
|
31
|
+
"LICENSE",
|
|
32
|
+
"DATA_LICENSES.txt"
|
|
33
|
+
],
|
|
34
|
+
"sideEffects": false,
|
|
35
|
+
"engines": {
|
|
36
|
+
"node": ">=20"
|
|
37
|
+
},
|
|
38
|
+
"keywords": [
|
|
39
|
+
"procedural-generation",
|
|
40
|
+
"character-generator",
|
|
41
|
+
"persona",
|
|
42
|
+
"names",
|
|
43
|
+
"webassembly",
|
|
44
|
+
"seeded"
|
|
45
|
+
],
|
|
46
|
+
"repository": {
|
|
47
|
+
"type": "git",
|
|
48
|
+
"url": "git+https://github.com/dasmig/generator-tech-demo.git",
|
|
49
|
+
"directory": "package"
|
|
50
|
+
},
|
|
51
|
+
"homepage": "https://github.com/dasmig/persona-generator",
|
|
52
|
+
"bugs": {
|
|
53
|
+
"url": "https://github.com/dasmig/generator-tech-demo/issues"
|
|
54
|
+
},
|
|
55
|
+
"author": "Diego Dasso Migotto",
|
|
56
|
+
"license": "MIT"
|
|
57
|
+
}
|
package/persona.mjs
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
|
|
2
|
+
var createPersonaModule = (() => {
|
|
3
|
+
var _scriptName = import.meta.url;
|
|
4
|
+
|
|
5
|
+
return (
|
|
6
|
+
function(moduleArg = {}) {
|
|
7
|
+
var moduleRtn;
|
|
8
|
+
|
|
9
|
+
var Module=moduleArg;var readyPromiseResolve,readyPromiseReject;var readyPromise=new Promise((resolve,reject)=>{readyPromiseResolve=resolve;readyPromiseReject=reject});var ENVIRONMENT_IS_WEB=typeof window=="object";var ENVIRONMENT_IS_WORKER=typeof WorkerGlobalScope!="undefined";var ENVIRONMENT_IS_NODE=typeof process=="object"&&typeof process.versions=="object"&&typeof process.versions.node=="string"&&process.type!="renderer";var moduleOverrides=Object.assign({},Module);var arguments_=[];var thisProgram="./this.program";var scriptDirectory="";function locateFile(path){if(Module["locateFile"]){return Module["locateFile"](path,scriptDirectory)}return scriptDirectory+path}var readAsync,readBinary;if(ENVIRONMENT_IS_WEB||ENVIRONMENT_IS_WORKER){if(ENVIRONMENT_IS_WORKER){scriptDirectory=self.location.href}else if(typeof document!="undefined"&&document.currentScript){scriptDirectory=document.currentScript.src}if(_scriptName){scriptDirectory=_scriptName}if(scriptDirectory.startsWith("blob:")){scriptDirectory=""}else{scriptDirectory=scriptDirectory.substr(0,scriptDirectory.replace(/[?#].*/,"").lastIndexOf("/")+1)}{if(ENVIRONMENT_IS_WORKER){readBinary=url=>{var xhr=new XMLHttpRequest;xhr.open("GET",url,false);xhr.responseType="arraybuffer";xhr.send(null);return new Uint8Array(xhr.response)}}readAsync=async url=>{var response=await fetch(url,{credentials:"same-origin"});if(response.ok){return response.arrayBuffer()}throw new Error(response.status+" : "+response.url)}}}else{}var out=Module["print"]||console.log.bind(console);var err=Module["printErr"]||console.error.bind(console);Object.assign(Module,moduleOverrides);moduleOverrides=null;if(Module["arguments"])arguments_=Module["arguments"];if(Module["thisProgram"])thisProgram=Module["thisProgram"];var wasmBinary=Module["wasmBinary"];var wasmMemory;var ABORT=false;var HEAP8,HEAPU8,HEAP16,HEAPU16,HEAP32,HEAPU32,HEAPF32,HEAPF64;function updateMemoryViews(){var b=wasmMemory.buffer;Module["HEAP8"]=HEAP8=new Int8Array(b);Module["HEAP16"]=HEAP16=new Int16Array(b);Module["HEAPU8"]=HEAPU8=new Uint8Array(b);Module["HEAPU16"]=HEAPU16=new Uint16Array(b);Module["HEAP32"]=HEAP32=new Int32Array(b);Module["HEAPU32"]=HEAPU32=new Uint32Array(b);Module["HEAPF32"]=HEAPF32=new Float32Array(b);Module["HEAPF64"]=HEAPF64=new Float64Array(b)}var __ATPRERUN__=[];var __ATINIT__=[];var __ATPOSTRUN__=[];var runtimeInitialized=false;function preRun(){if(Module["preRun"]){if(typeof Module["preRun"]=="function")Module["preRun"]=[Module["preRun"]];while(Module["preRun"].length){addOnPreRun(Module["preRun"].shift())}}callRuntimeCallbacks(__ATPRERUN__)}function initRuntime(){runtimeInitialized=true;if(!Module["noFSInit"]&&!FS.initialized)FS.init();FS.ignorePermissions=false;TTY.init();callRuntimeCallbacks(__ATINIT__)}function postRun(){if(Module["postRun"]){if(typeof Module["postRun"]=="function")Module["postRun"]=[Module["postRun"]];while(Module["postRun"].length){addOnPostRun(Module["postRun"].shift())}}callRuntimeCallbacks(__ATPOSTRUN__)}function addOnPreRun(cb){__ATPRERUN__.unshift(cb)}function addOnInit(cb){__ATINIT__.unshift(cb)}function addOnPostRun(cb){__ATPOSTRUN__.unshift(cb)}var runDependencies=0;var dependenciesFulfilled=null;function getUniqueRunDependency(id){return id}function addRunDependency(id){runDependencies++;Module["monitorRunDependencies"]?.(runDependencies)}function removeRunDependency(id){runDependencies--;Module["monitorRunDependencies"]?.(runDependencies);if(runDependencies==0){if(dependenciesFulfilled){var callback=dependenciesFulfilled;dependenciesFulfilled=null;callback()}}}function abort(what){Module["onAbort"]?.(what);what="Aborted("+what+")";err(what);ABORT=true;what+=". Build with -sASSERTIONS for more info.";if(runtimeInitialized){___trap()}var e=new WebAssembly.RuntimeError(what);readyPromiseReject(e);throw e}var dataURIPrefix="data:application/octet-stream;base64,";var isDataURI=filename=>filename.startsWith(dataURIPrefix);function findWasmBinary(){if(Module["locateFile"]){var f="persona.wasm";if(!isDataURI(f)){return locateFile(f)}return f}return new URL("persona.wasm",import.meta.url).href}var wasmBinaryFile;function getBinarySync(file){if(file==wasmBinaryFile&&wasmBinary){return new Uint8Array(wasmBinary)}if(readBinary){return readBinary(file)}throw"both async and sync fetching of the wasm failed"}async function getWasmBinary(binaryFile){if(!wasmBinary){try{var response=await readAsync(binaryFile);return new Uint8Array(response)}catch{}}return getBinarySync(binaryFile)}async function instantiateArrayBuffer(binaryFile,imports){try{var binary=await getWasmBinary(binaryFile);var instance=await WebAssembly.instantiate(binary,imports);return instance}catch(reason){err(`failed to asynchronously prepare wasm: ${reason}`);abort(reason)}}async function instantiateAsync(binary,binaryFile,imports){if(!binary&&typeof WebAssembly.instantiateStreaming=="function"&&!isDataURI(binaryFile)&&typeof fetch=="function"){try{var response=fetch(binaryFile,{credentials:"same-origin"});var instantiationResult=await WebAssembly.instantiateStreaming(response,imports);return instantiationResult}catch(reason){err(`wasm streaming compile failed: ${reason}`);err("falling back to ArrayBuffer instantiation")}}return instantiateArrayBuffer(binaryFile,imports)}function getWasmImports(){return{a:wasmImports}}async function createWasm(){function receiveInstance(instance,module){wasmExports=instance.exports;wasmMemory=wasmExports["u"];updateMemoryViews();addOnInit(wasmExports["v"]);removeRunDependency("wasm-instantiate");return wasmExports}addRunDependency("wasm-instantiate");function receiveInstantiationResult(result){receiveInstance(result["instance"])}var info=getWasmImports();if(Module["instantiateWasm"]){try{return Module["instantiateWasm"](info,receiveInstance)}catch(e){err(`Module.instantiateWasm callback failed with error: ${e}`);readyPromiseReject(e)}}wasmBinaryFile??=findWasmBinary();try{var result=await instantiateAsync(wasmBinary,wasmBinaryFile,info);receiveInstantiationResult(result);return result}catch(e){readyPromiseReject(e);return}}var tempDouble;var tempI64;class ExitStatus{name="ExitStatus";constructor(status){this.message=`Program terminated with exit(${status})`;this.status=status}}var callRuntimeCallbacks=callbacks=>{while(callbacks.length>0){callbacks.shift()(Module)}};var noExitRuntime=Module["noExitRuntime"]||true;var syscallGetVarargI=()=>{var ret=HEAP32[+SYSCALLS.varargs>>2];SYSCALLS.varargs+=4;return ret};var syscallGetVarargP=syscallGetVarargI;var PATH={isAbs:path=>path.charAt(0)==="/",splitPath:filename=>{var splitPathRe=/^(\/?|)([\s\S]*?)((?:\.{1,2}|[^\/]+?|)(\.[^.\/]*|))(?:[\/]*)$/;return splitPathRe.exec(filename).slice(1)},normalizeArray:(parts,allowAboveRoot)=>{var up=0;for(var i=parts.length-1;i>=0;i--){var last=parts[i];if(last==="."){parts.splice(i,1)}else if(last===".."){parts.splice(i,1);up++}else if(up){parts.splice(i,1);up--}}if(allowAboveRoot){for(;up;up--){parts.unshift("..")}}return parts},normalize:path=>{var isAbsolute=PATH.isAbs(path),trailingSlash=path.substr(-1)==="/";path=PATH.normalizeArray(path.split("/").filter(p=>!!p),!isAbsolute).join("/");if(!path&&!isAbsolute){path="."}if(path&&trailingSlash){path+="/"}return(isAbsolute?"/":"")+path},dirname:path=>{var result=PATH.splitPath(path),root=result[0],dir=result[1];if(!root&&!dir){return"."}if(dir){dir=dir.substr(0,dir.length-1)}return root+dir},basename:path=>{if(path==="/")return"/";path=PATH.normalize(path);path=path.replace(/\/$/,"");var lastSlash=path.lastIndexOf("/");if(lastSlash===-1)return path;return path.substr(lastSlash+1)},join:(...paths)=>PATH.normalize(paths.join("/")),join2:(l,r)=>PATH.normalize(l+"/"+r)};var initRandomFill=()=>{if(typeof crypto=="object"&&typeof crypto["getRandomValues"]=="function"){return view=>crypto.getRandomValues(view)}else abort("initRandomDevice")};var randomFill=view=>(randomFill=initRandomFill())(view);var PATH_FS={resolve:(...args)=>{var resolvedPath="",resolvedAbsolute=false;for(var i=args.length-1;i>=-1&&!resolvedAbsolute;i--){var path=i>=0?args[i]:FS.cwd();if(typeof path!="string"){throw new TypeError("Arguments to path.resolve must be strings")}else if(!path){return""}resolvedPath=path+"/"+resolvedPath;resolvedAbsolute=PATH.isAbs(path)}resolvedPath=PATH.normalizeArray(resolvedPath.split("/").filter(p=>!!p),!resolvedAbsolute).join("/");return(resolvedAbsolute?"/":"")+resolvedPath||"."},relative:(from,to)=>{from=PATH_FS.resolve(from).substr(1);to=PATH_FS.resolve(to).substr(1);function trim(arr){var start=0;for(;start<arr.length;start++){if(arr[start]!=="")break}var end=arr.length-1;for(;end>=0;end--){if(arr[end]!=="")break}if(start>end)return[];return arr.slice(start,end-start+1)}var fromParts=trim(from.split("/"));var toParts=trim(to.split("/"));var length=Math.min(fromParts.length,toParts.length);var samePartsLength=length;for(var i=0;i<length;i++){if(fromParts[i]!==toParts[i]){samePartsLength=i;break}}var outputParts=[];for(var i=samePartsLength;i<fromParts.length;i++){outputParts.push("..")}outputParts=outputParts.concat(toParts.slice(samePartsLength));return outputParts.join("/")}};var UTF8Decoder=typeof TextDecoder!="undefined"?new TextDecoder:undefined;var UTF8ArrayToString=(heapOrArray,idx=0,maxBytesToRead=NaN)=>{var endIdx=idx+maxBytesToRead;var endPtr=idx;while(heapOrArray[endPtr]&&!(endPtr>=endIdx))++endPtr;if(endPtr-idx>16&&heapOrArray.buffer&&UTF8Decoder){return UTF8Decoder.decode(heapOrArray.subarray(idx,endPtr))}var str="";while(idx<endPtr){var u0=heapOrArray[idx++];if(!(u0&128)){str+=String.fromCharCode(u0);continue}var u1=heapOrArray[idx++]&63;if((u0&224)==192){str+=String.fromCharCode((u0&31)<<6|u1);continue}var u2=heapOrArray[idx++]&63;if((u0&240)==224){u0=(u0&15)<<12|u1<<6|u2}else{u0=(u0&7)<<18|u1<<12|u2<<6|heapOrArray[idx++]&63}if(u0<65536){str+=String.fromCharCode(u0)}else{var ch=u0-65536;str+=String.fromCharCode(55296|ch>>10,56320|ch&1023)}}return str};var FS_stdin_getChar_buffer=[];var lengthBytesUTF8=str=>{var len=0;for(var i=0;i<str.length;++i){var c=str.charCodeAt(i);if(c<=127){len++}else if(c<=2047){len+=2}else if(c>=55296&&c<=57343){len+=4;++i}else{len+=3}}return len};var stringToUTF8Array=(str,heap,outIdx,maxBytesToWrite)=>{if(!(maxBytesToWrite>0))return 0;var startIdx=outIdx;var endIdx=outIdx+maxBytesToWrite-1;for(var i=0;i<str.length;++i){var u=str.charCodeAt(i);if(u>=55296&&u<=57343){var u1=str.charCodeAt(++i);u=65536+((u&1023)<<10)|u1&1023}if(u<=127){if(outIdx>=endIdx)break;heap[outIdx++]=u}else if(u<=2047){if(outIdx+1>=endIdx)break;heap[outIdx++]=192|u>>6;heap[outIdx++]=128|u&63}else if(u<=65535){if(outIdx+2>=endIdx)break;heap[outIdx++]=224|u>>12;heap[outIdx++]=128|u>>6&63;heap[outIdx++]=128|u&63}else{if(outIdx+3>=endIdx)break;heap[outIdx++]=240|u>>18;heap[outIdx++]=128|u>>12&63;heap[outIdx++]=128|u>>6&63;heap[outIdx++]=128|u&63}}heap[outIdx]=0;return outIdx-startIdx};function intArrayFromString(stringy,dontAddNull,length){var len=length>0?length:lengthBytesUTF8(stringy)+1;var u8array=new Array(len);var numBytesWritten=stringToUTF8Array(stringy,u8array,0,u8array.length);if(dontAddNull)u8array.length=numBytesWritten;return u8array}var FS_stdin_getChar=()=>{if(!FS_stdin_getChar_buffer.length){var result=null;if(typeof window!="undefined"&&typeof window.prompt=="function"){result=window.prompt("Input: ");if(result!==null){result+="\n"}}else{}if(!result){return null}FS_stdin_getChar_buffer=intArrayFromString(result,true)}return FS_stdin_getChar_buffer.shift()};var TTY={ttys:[],init(){},shutdown(){},register(dev,ops){TTY.ttys[dev]={input:[],output:[],ops};FS.registerDevice(dev,TTY.stream_ops)},stream_ops:{open(stream){var tty=TTY.ttys[stream.node.rdev];if(!tty){throw new FS.ErrnoError(43)}stream.tty=tty;stream.seekable=false},close(stream){stream.tty.ops.fsync(stream.tty)},fsync(stream){stream.tty.ops.fsync(stream.tty)},read(stream,buffer,offset,length,pos){if(!stream.tty||!stream.tty.ops.get_char){throw new FS.ErrnoError(60)}var bytesRead=0;for(var i=0;i<length;i++){var result;try{result=stream.tty.ops.get_char(stream.tty)}catch(e){throw new FS.ErrnoError(29)}if(result===undefined&&bytesRead===0){throw new FS.ErrnoError(6)}if(result===null||result===undefined)break;bytesRead++;buffer[offset+i]=result}if(bytesRead){stream.node.atime=Date.now()}return bytesRead},write(stream,buffer,offset,length,pos){if(!stream.tty||!stream.tty.ops.put_char){throw new FS.ErrnoError(60)}try{for(var i=0;i<length;i++){stream.tty.ops.put_char(stream.tty,buffer[offset+i])}}catch(e){throw new FS.ErrnoError(29)}if(length){stream.node.mtime=stream.node.ctime=Date.now()}return i}},default_tty_ops:{get_char(tty){return FS_stdin_getChar()},put_char(tty,val){if(val===null||val===10){out(UTF8ArrayToString(tty.output));tty.output=[]}else{if(val!=0)tty.output.push(val)}},fsync(tty){if(tty.output&&tty.output.length>0){out(UTF8ArrayToString(tty.output));tty.output=[]}},ioctl_tcgets(tty){return{c_iflag:25856,c_oflag:5,c_cflag:191,c_lflag:35387,c_cc:[3,28,127,21,4,0,1,0,17,19,26,0,18,15,23,22,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]}},ioctl_tcsets(tty,optional_actions,data){return 0},ioctl_tiocgwinsz(tty){return[24,80]}},default_tty1_ops:{put_char(tty,val){if(val===null||val===10){err(UTF8ArrayToString(tty.output));tty.output=[]}else{if(val!=0)tty.output.push(val)}},fsync(tty){if(tty.output&&tty.output.length>0){err(UTF8ArrayToString(tty.output));tty.output=[]}}}};var alignMemory=(size,alignment)=>Math.ceil(size/alignment)*alignment;var mmapAlloc=size=>{abort()};var MEMFS={ops_table:null,mount(mount){return MEMFS.createNode(null,"/",16895,0)},createNode(parent,name,mode,dev){if(FS.isBlkdev(mode)||FS.isFIFO(mode)){throw new FS.ErrnoError(63)}MEMFS.ops_table||={dir:{node:{getattr:MEMFS.node_ops.getattr,setattr:MEMFS.node_ops.setattr,lookup:MEMFS.node_ops.lookup,mknod:MEMFS.node_ops.mknod,rename:MEMFS.node_ops.rename,unlink:MEMFS.node_ops.unlink,rmdir:MEMFS.node_ops.rmdir,readdir:MEMFS.node_ops.readdir,symlink:MEMFS.node_ops.symlink},stream:{llseek:MEMFS.stream_ops.llseek}},file:{node:{getattr:MEMFS.node_ops.getattr,setattr:MEMFS.node_ops.setattr},stream:{llseek:MEMFS.stream_ops.llseek,read:MEMFS.stream_ops.read,write:MEMFS.stream_ops.write,allocate:MEMFS.stream_ops.allocate,mmap:MEMFS.stream_ops.mmap,msync:MEMFS.stream_ops.msync}},link:{node:{getattr:MEMFS.node_ops.getattr,setattr:MEMFS.node_ops.setattr,readlink:MEMFS.node_ops.readlink},stream:{}},chrdev:{node:{getattr:MEMFS.node_ops.getattr,setattr:MEMFS.node_ops.setattr},stream:FS.chrdev_stream_ops}};var node=FS.createNode(parent,name,mode,dev);if(FS.isDir(node.mode)){node.node_ops=MEMFS.ops_table.dir.node;node.stream_ops=MEMFS.ops_table.dir.stream;node.contents={}}else if(FS.isFile(node.mode)){node.node_ops=MEMFS.ops_table.file.node;node.stream_ops=MEMFS.ops_table.file.stream;node.usedBytes=0;node.contents=null}else if(FS.isLink(node.mode)){node.node_ops=MEMFS.ops_table.link.node;node.stream_ops=MEMFS.ops_table.link.stream}else if(FS.isChrdev(node.mode)){node.node_ops=MEMFS.ops_table.chrdev.node;node.stream_ops=MEMFS.ops_table.chrdev.stream}node.atime=node.mtime=node.ctime=Date.now();if(parent){parent.contents[name]=node;parent.atime=parent.mtime=parent.ctime=node.atime}return node},getFileDataAsTypedArray(node){if(!node.contents)return new Uint8Array(0);if(node.contents.subarray)return node.contents.subarray(0,node.usedBytes);return new Uint8Array(node.contents)},expandFileStorage(node,newCapacity){var prevCapacity=node.contents?node.contents.length:0;if(prevCapacity>=newCapacity)return;var CAPACITY_DOUBLING_MAX=1024*1024;newCapacity=Math.max(newCapacity,prevCapacity*(prevCapacity<CAPACITY_DOUBLING_MAX?2:1.125)>>>0);if(prevCapacity!=0)newCapacity=Math.max(newCapacity,256);var oldContents=node.contents;node.contents=new Uint8Array(newCapacity);if(node.usedBytes>0)node.contents.set(oldContents.subarray(0,node.usedBytes),0)},resizeFileStorage(node,newSize){if(node.usedBytes==newSize)return;if(newSize==0){node.contents=null;node.usedBytes=0}else{var oldContents=node.contents;node.contents=new Uint8Array(newSize);if(oldContents){node.contents.set(oldContents.subarray(0,Math.min(newSize,node.usedBytes)))}node.usedBytes=newSize}},node_ops:{getattr(node){var attr={};attr.dev=FS.isChrdev(node.mode)?node.id:1;attr.ino=node.id;attr.mode=node.mode;attr.nlink=1;attr.uid=0;attr.gid=0;attr.rdev=node.rdev;if(FS.isDir(node.mode)){attr.size=4096}else if(FS.isFile(node.mode)){attr.size=node.usedBytes}else if(FS.isLink(node.mode)){attr.size=node.link.length}else{attr.size=0}attr.atime=new Date(node.atime);attr.mtime=new Date(node.mtime);attr.ctime=new Date(node.ctime);attr.blksize=4096;attr.blocks=Math.ceil(attr.size/attr.blksize);return attr},setattr(node,attr){for(const key of["mode","atime","mtime","ctime"]){if(attr[key]){node[key]=attr[key]}}if(attr.size!==undefined){MEMFS.resizeFileStorage(node,attr.size)}},lookup(parent,name){throw MEMFS.doesNotExistError},mknod(parent,name,mode,dev){return MEMFS.createNode(parent,name,mode,dev)},rename(old_node,new_dir,new_name){var new_node;try{new_node=FS.lookupNode(new_dir,new_name)}catch(e){}if(new_node){if(FS.isDir(old_node.mode)){for(var i in new_node.contents){throw new FS.ErrnoError(55)}}FS.hashRemoveNode(new_node)}delete old_node.parent.contents[old_node.name];new_dir.contents[new_name]=old_node;old_node.name=new_name;new_dir.ctime=new_dir.mtime=old_node.parent.ctime=old_node.parent.mtime=Date.now()},unlink(parent,name){delete parent.contents[name];parent.ctime=parent.mtime=Date.now()},rmdir(parent,name){var node=FS.lookupNode(parent,name);for(var i in node.contents){throw new FS.ErrnoError(55)}delete parent.contents[name];parent.ctime=parent.mtime=Date.now()},readdir(node){return[".","..",...Object.keys(node.contents)]},symlink(parent,newname,oldpath){var node=MEMFS.createNode(parent,newname,511|40960,0);node.link=oldpath;return node},readlink(node){if(!FS.isLink(node.mode)){throw new FS.ErrnoError(28)}return node.link}},stream_ops:{read(stream,buffer,offset,length,position){var contents=stream.node.contents;if(position>=stream.node.usedBytes)return 0;var size=Math.min(stream.node.usedBytes-position,length);if(size>8&&contents.subarray){buffer.set(contents.subarray(position,position+size),offset)}else{for(var i=0;i<size;i++)buffer[offset+i]=contents[position+i]}return size},write(stream,buffer,offset,length,position,canOwn){if(buffer.buffer===HEAP8.buffer){canOwn=false}if(!length)return 0;var node=stream.node;node.mtime=node.ctime=Date.now();if(buffer.subarray&&(!node.contents||node.contents.subarray)){if(canOwn){node.contents=buffer.subarray(offset,offset+length);node.usedBytes=length;return length}else if(node.usedBytes===0&&position===0){node.contents=buffer.slice(offset,offset+length);node.usedBytes=length;return length}else if(position+length<=node.usedBytes){node.contents.set(buffer.subarray(offset,offset+length),position);return length}}MEMFS.expandFileStorage(node,position+length);if(node.contents.subarray&&buffer.subarray){node.contents.set(buffer.subarray(offset,offset+length),position)}else{for(var i=0;i<length;i++){node.contents[position+i]=buffer[offset+i]}}node.usedBytes=Math.max(node.usedBytes,position+length);return length},llseek(stream,offset,whence){var position=offset;if(whence===1){position+=stream.position}else if(whence===2){if(FS.isFile(stream.node.mode)){position+=stream.node.usedBytes}}if(position<0){throw new FS.ErrnoError(28)}return position},allocate(stream,offset,length){MEMFS.expandFileStorage(stream.node,offset+length);stream.node.usedBytes=Math.max(stream.node.usedBytes,offset+length)},mmap(stream,length,position,prot,flags){if(!FS.isFile(stream.node.mode)){throw new FS.ErrnoError(43)}var ptr;var allocated;var contents=stream.node.contents;if(!(flags&2)&&contents&&contents.buffer===HEAP8.buffer){allocated=false;ptr=contents.byteOffset}else{allocated=true;ptr=mmapAlloc(length);if(!ptr){throw new FS.ErrnoError(48)}if(contents){if(position>0||position+length<contents.length){if(contents.subarray){contents=contents.subarray(position,position+length)}else{contents=Array.prototype.slice.call(contents,position,position+length)}}HEAP8.set(contents,ptr)}}return{ptr,allocated}},msync(stream,buffer,offset,length,mmapFlags){MEMFS.stream_ops.write(stream,buffer,0,length,offset,false);return 0}}};var asyncLoad=async url=>{var arrayBuffer=await readAsync(url);return new Uint8Array(arrayBuffer)};var FS_createDataFile=(parent,name,fileData,canRead,canWrite,canOwn)=>{FS.createDataFile(parent,name,fileData,canRead,canWrite,canOwn)};var preloadPlugins=Module["preloadPlugins"]||[];var FS_handledByPreloadPlugin=(byteArray,fullname,finish,onerror)=>{if(typeof Browser!="undefined")Browser.init();var handled=false;preloadPlugins.forEach(plugin=>{if(handled)return;if(plugin["canHandle"](fullname)){plugin["handle"](byteArray,fullname,finish,onerror);handled=true}});return handled};var FS_createPreloadedFile=(parent,name,url,canRead,canWrite,onload,onerror,dontCreateFile,canOwn,preFinish)=>{var fullname=name?PATH_FS.resolve(PATH.join2(parent,name)):parent;var dep=getUniqueRunDependency(`cp ${fullname}`);function processData(byteArray){function finish(byteArray){preFinish?.();if(!dontCreateFile){FS_createDataFile(parent,name,byteArray,canRead,canWrite,canOwn)}onload?.();removeRunDependency(dep)}if(FS_handledByPreloadPlugin(byteArray,fullname,finish,()=>{onerror?.();removeRunDependency(dep)})){return}finish(byteArray)}addRunDependency(dep);if(typeof url=="string"){asyncLoad(url).then(processData,onerror)}else{processData(url)}};var FS_modeStringToFlags=str=>{var flagModes={r:0,"r+":2,w:512|64|1,"w+":512|64|2,a:1024|64|1,"a+":1024|64|2};var flags=flagModes[str];if(typeof flags=="undefined"){throw new Error(`Unknown file open mode: ${str}`)}return flags};var FS_getMode=(canRead,canWrite)=>{var mode=0;if(canRead)mode|=292|73;if(canWrite)mode|=146;return mode};var FS={root:null,mounts:[],devices:{},streams:[],nextInode:1,nameTable:null,currentPath:"/",initialized:false,ignorePermissions:true,ErrnoError:class{name="ErrnoError";constructor(errno){this.errno=errno}},filesystems:null,syncFSRequests:0,readFiles:{},FSStream:class{shared={};get object(){return this.node}set object(val){this.node=val}get isRead(){return(this.flags&2097155)!==1}get isWrite(){return(this.flags&2097155)!==0}get isAppend(){return this.flags&1024}get flags(){return this.shared.flags}set flags(val){this.shared.flags=val}get position(){return this.shared.position}set position(val){this.shared.position=val}},FSNode:class{node_ops={};stream_ops={};readMode=292|73;writeMode=146;mounted=null;constructor(parent,name,mode,rdev){if(!parent){parent=this}this.parent=parent;this.mount=parent.mount;this.id=FS.nextInode++;this.name=name;this.mode=mode;this.rdev=rdev;this.atime=this.mtime=this.ctime=Date.now()}get read(){return(this.mode&this.readMode)===this.readMode}set read(val){val?this.mode|=this.readMode:this.mode&=~this.readMode}get write(){return(this.mode&this.writeMode)===this.writeMode}set write(val){val?this.mode|=this.writeMode:this.mode&=~this.writeMode}get isFolder(){return FS.isDir(this.mode)}get isDevice(){return FS.isChrdev(this.mode)}},lookupPath(path,opts={}){if(!path)return{path:"",node:null};opts.follow_mount??=true;if(!PATH.isAbs(path)){path=FS.cwd()+"/"+path}linkloop:for(var nlinks=0;nlinks<40;nlinks++){var parts=path.split("/").filter(p=>!!p&&p!==".");var current=FS.root;var current_path="/";for(var i=0;i<parts.length;i++){var islast=i===parts.length-1;if(islast&&opts.parent){break}if(parts[i]===".."){current_path=PATH.dirname(current_path);current=current.parent;continue}current_path=PATH.join2(current_path,parts[i]);try{current=FS.lookupNode(current,parts[i])}catch(e){if(e?.errno===44&&islast&&opts.noent_okay){return{path:current_path}}throw e}if(FS.isMountpoint(current)&&(!islast||opts.follow_mount)){current=current.mounted.root}if(FS.isLink(current.mode)&&(!islast||opts.follow)){if(!current.node_ops.readlink){throw new FS.ErrnoError(52)}var link=current.node_ops.readlink(current);if(!PATH.isAbs(link)){link=PATH.dirname(current_path)+"/"+link}path=link+"/"+parts.slice(i+1).join("/");continue linkloop}}return{path:current_path,node:current}}throw new FS.ErrnoError(32)},getPath(node){var path;while(true){if(FS.isRoot(node)){var mount=node.mount.mountpoint;if(!path)return mount;return mount[mount.length-1]!=="/"?`${mount}/${path}`:mount+path}path=path?`${node.name}/${path}`:node.name;node=node.parent}},hashName(parentid,name){var hash=0;for(var i=0;i<name.length;i++){hash=(hash<<5)-hash+name.charCodeAt(i)|0}return(parentid+hash>>>0)%FS.nameTable.length},hashAddNode(node){var hash=FS.hashName(node.parent.id,node.name);node.name_next=FS.nameTable[hash];FS.nameTable[hash]=node},hashRemoveNode(node){var hash=FS.hashName(node.parent.id,node.name);if(FS.nameTable[hash]===node){FS.nameTable[hash]=node.name_next}else{var current=FS.nameTable[hash];while(current){if(current.name_next===node){current.name_next=node.name_next;break}current=current.name_next}}},lookupNode(parent,name){var errCode=FS.mayLookup(parent);if(errCode){throw new FS.ErrnoError(errCode)}var hash=FS.hashName(parent.id,name);for(var node=FS.nameTable[hash];node;node=node.name_next){var nodeName=node.name;if(node.parent.id===parent.id&&nodeName===name){return node}}return FS.lookup(parent,name)},createNode(parent,name,mode,rdev){var node=new FS.FSNode(parent,name,mode,rdev);FS.hashAddNode(node);return node},destroyNode(node){FS.hashRemoveNode(node)},isRoot(node){return node===node.parent},isMountpoint(node){return!!node.mounted},isFile(mode){return(mode&61440)===32768},isDir(mode){return(mode&61440)===16384},isLink(mode){return(mode&61440)===40960},isChrdev(mode){return(mode&61440)===8192},isBlkdev(mode){return(mode&61440)===24576},isFIFO(mode){return(mode&61440)===4096},isSocket(mode){return(mode&49152)===49152},flagsToPermissionString(flag){var perms=["r","w","rw"][flag&3];if(flag&512){perms+="w"}return perms},nodePermissions(node,perms){if(FS.ignorePermissions){return 0}if(perms.includes("r")&&!(node.mode&292)){return 2}else if(perms.includes("w")&&!(node.mode&146)){return 2}else if(perms.includes("x")&&!(node.mode&73)){return 2}return 0},mayLookup(dir){if(!FS.isDir(dir.mode))return 54;var errCode=FS.nodePermissions(dir,"x");if(errCode)return errCode;if(!dir.node_ops.lookup)return 2;return 0},mayCreate(dir,name){if(!FS.isDir(dir.mode)){return 54}try{var node=FS.lookupNode(dir,name);return 20}catch(e){}return FS.nodePermissions(dir,"wx")},mayDelete(dir,name,isdir){var node;try{node=FS.lookupNode(dir,name)}catch(e){return e.errno}var errCode=FS.nodePermissions(dir,"wx");if(errCode){return errCode}if(isdir){if(!FS.isDir(node.mode)){return 54}if(FS.isRoot(node)||FS.getPath(node)===FS.cwd()){return 10}}else{if(FS.isDir(node.mode)){return 31}}return 0},mayOpen(node,flags){if(!node){return 44}if(FS.isLink(node.mode)){return 32}else if(FS.isDir(node.mode)){if(FS.flagsToPermissionString(flags)!=="r"||flags&512){return 31}}return FS.nodePermissions(node,FS.flagsToPermissionString(flags))},MAX_OPEN_FDS:4096,nextfd(){for(var fd=0;fd<=FS.MAX_OPEN_FDS;fd++){if(!FS.streams[fd]){return fd}}throw new FS.ErrnoError(33)},getStreamChecked(fd){var stream=FS.getStream(fd);if(!stream){throw new FS.ErrnoError(8)}return stream},getStream:fd=>FS.streams[fd],createStream(stream,fd=-1){stream=Object.assign(new FS.FSStream,stream);if(fd==-1){fd=FS.nextfd()}stream.fd=fd;FS.streams[fd]=stream;return stream},closeStream(fd){FS.streams[fd]=null},dupStream(origStream,fd=-1){var stream=FS.createStream(origStream,fd);stream.stream_ops?.dup?.(stream);return stream},chrdev_stream_ops:{open(stream){var device=FS.getDevice(stream.node.rdev);stream.stream_ops=device.stream_ops;stream.stream_ops.open?.(stream)},llseek(){throw new FS.ErrnoError(70)}},major:dev=>dev>>8,minor:dev=>dev&255,makedev:(ma,mi)=>ma<<8|mi,registerDevice(dev,ops){FS.devices[dev]={stream_ops:ops}},getDevice:dev=>FS.devices[dev],getMounts(mount){var mounts=[];var check=[mount];while(check.length){var m=check.pop();mounts.push(m);check.push(...m.mounts)}return mounts},syncfs(populate,callback){if(typeof populate=="function"){callback=populate;populate=false}FS.syncFSRequests++;if(FS.syncFSRequests>1){err(`warning: ${FS.syncFSRequests} FS.syncfs operations in flight at once, probably just doing extra work`)}var mounts=FS.getMounts(FS.root.mount);var completed=0;function doCallback(errCode){FS.syncFSRequests--;return callback(errCode)}function done(errCode){if(errCode){if(!done.errored){done.errored=true;return doCallback(errCode)}return}if(++completed>=mounts.length){doCallback(null)}}mounts.forEach(mount=>{if(!mount.type.syncfs){return done(null)}mount.type.syncfs(mount,populate,done)})},mount(type,opts,mountpoint){var root=mountpoint==="/";var pseudo=!mountpoint;var node;if(root&&FS.root){throw new FS.ErrnoError(10)}else if(!root&&!pseudo){var lookup=FS.lookupPath(mountpoint,{follow_mount:false});mountpoint=lookup.path;node=lookup.node;if(FS.isMountpoint(node)){throw new FS.ErrnoError(10)}if(!FS.isDir(node.mode)){throw new FS.ErrnoError(54)}}var mount={type,opts,mountpoint,mounts:[]};var mountRoot=type.mount(mount);mountRoot.mount=mount;mount.root=mountRoot;if(root){FS.root=mountRoot}else if(node){node.mounted=mount;if(node.mount){node.mount.mounts.push(mount)}}return mountRoot},unmount(mountpoint){var lookup=FS.lookupPath(mountpoint,{follow_mount:false});if(!FS.isMountpoint(lookup.node)){throw new FS.ErrnoError(28)}var node=lookup.node;var mount=node.mounted;var mounts=FS.getMounts(mount);Object.keys(FS.nameTable).forEach(hash=>{var current=FS.nameTable[hash];while(current){var next=current.name_next;if(mounts.includes(current.mount)){FS.destroyNode(current)}current=next}});node.mounted=null;var idx=node.mount.mounts.indexOf(mount);node.mount.mounts.splice(idx,1)},lookup(parent,name){return parent.node_ops.lookup(parent,name)},mknod(path,mode,dev){var lookup=FS.lookupPath(path,{parent:true});var parent=lookup.node;var name=PATH.basename(path);if(!name||name==="."||name===".."){throw new FS.ErrnoError(28)}var errCode=FS.mayCreate(parent,name);if(errCode){throw new FS.ErrnoError(errCode)}if(!parent.node_ops.mknod){throw new FS.ErrnoError(63)}return parent.node_ops.mknod(parent,name,mode,dev)},statfs(path){var rtn={bsize:4096,frsize:4096,blocks:1e6,bfree:5e5,bavail:5e5,files:FS.nextInode,ffree:FS.nextInode-1,fsid:42,flags:2,namelen:255};var parent=FS.lookupPath(path,{follow:true}).node;if(parent?.node_ops.statfs){Object.assign(rtn,parent.node_ops.statfs(parent.mount.opts.root))}return rtn},create(path,mode=438){mode&=4095;mode|=32768;return FS.mknod(path,mode,0)},mkdir(path,mode=511){mode&=511|512;mode|=16384;return FS.mknod(path,mode,0)},mkdirTree(path,mode){var dirs=path.split("/");var d="";for(var i=0;i<dirs.length;++i){if(!dirs[i])continue;d+="/"+dirs[i];try{FS.mkdir(d,mode)}catch(e){if(e.errno!=20)throw e}}},mkdev(path,mode,dev){if(typeof dev=="undefined"){dev=mode;mode=438}mode|=8192;return FS.mknod(path,mode,dev)},symlink(oldpath,newpath){if(!PATH_FS.resolve(oldpath)){throw new FS.ErrnoError(44)}var lookup=FS.lookupPath(newpath,{parent:true});var parent=lookup.node;if(!parent){throw new FS.ErrnoError(44)}var newname=PATH.basename(newpath);var errCode=FS.mayCreate(parent,newname);if(errCode){throw new FS.ErrnoError(errCode)}if(!parent.node_ops.symlink){throw new FS.ErrnoError(63)}return parent.node_ops.symlink(parent,newname,oldpath)},rename(old_path,new_path){var old_dirname=PATH.dirname(old_path);var new_dirname=PATH.dirname(new_path);var old_name=PATH.basename(old_path);var new_name=PATH.basename(new_path);var lookup,old_dir,new_dir;lookup=FS.lookupPath(old_path,{parent:true});old_dir=lookup.node;lookup=FS.lookupPath(new_path,{parent:true});new_dir=lookup.node;if(!old_dir||!new_dir)throw new FS.ErrnoError(44);if(old_dir.mount!==new_dir.mount){throw new FS.ErrnoError(75)}var old_node=FS.lookupNode(old_dir,old_name);var relative=PATH_FS.relative(old_path,new_dirname);if(relative.charAt(0)!=="."){throw new FS.ErrnoError(28)}relative=PATH_FS.relative(new_path,old_dirname);if(relative.charAt(0)!=="."){throw new FS.ErrnoError(55)}var new_node;try{new_node=FS.lookupNode(new_dir,new_name)}catch(e){}if(old_node===new_node){return}var isdir=FS.isDir(old_node.mode);var errCode=FS.mayDelete(old_dir,old_name,isdir);if(errCode){throw new FS.ErrnoError(errCode)}errCode=new_node?FS.mayDelete(new_dir,new_name,isdir):FS.mayCreate(new_dir,new_name);if(errCode){throw new FS.ErrnoError(errCode)}if(!old_dir.node_ops.rename){throw new FS.ErrnoError(63)}if(FS.isMountpoint(old_node)||new_node&&FS.isMountpoint(new_node)){throw new FS.ErrnoError(10)}if(new_dir!==old_dir){errCode=FS.nodePermissions(old_dir,"w");if(errCode){throw new FS.ErrnoError(errCode)}}FS.hashRemoveNode(old_node);try{old_dir.node_ops.rename(old_node,new_dir,new_name);old_node.parent=new_dir}catch(e){throw e}finally{FS.hashAddNode(old_node)}},rmdir(path){var lookup=FS.lookupPath(path,{parent:true});var parent=lookup.node;var name=PATH.basename(path);var node=FS.lookupNode(parent,name);var errCode=FS.mayDelete(parent,name,true);if(errCode){throw new FS.ErrnoError(errCode)}if(!parent.node_ops.rmdir){throw new FS.ErrnoError(63)}if(FS.isMountpoint(node)){throw new FS.ErrnoError(10)}parent.node_ops.rmdir(parent,name);FS.destroyNode(node)},readdir(path){var lookup=FS.lookupPath(path,{follow:true});var node=lookup.node;if(!node.node_ops.readdir){throw new FS.ErrnoError(54)}return node.node_ops.readdir(node)},unlink(path){var lookup=FS.lookupPath(path,{parent:true});var parent=lookup.node;if(!parent){throw new FS.ErrnoError(44)}var name=PATH.basename(path);var node=FS.lookupNode(parent,name);var errCode=FS.mayDelete(parent,name,false);if(errCode){throw new FS.ErrnoError(errCode)}if(!parent.node_ops.unlink){throw new FS.ErrnoError(63)}if(FS.isMountpoint(node)){throw new FS.ErrnoError(10)}parent.node_ops.unlink(parent,name);FS.destroyNode(node)},readlink(path){var lookup=FS.lookupPath(path);var link=lookup.node;if(!link){throw new FS.ErrnoError(44)}if(!link.node_ops.readlink){throw new FS.ErrnoError(28)}return link.node_ops.readlink(link)},stat(path,dontFollow){var lookup=FS.lookupPath(path,{follow:!dontFollow});var node=lookup.node;if(!node){throw new FS.ErrnoError(44)}if(!node.node_ops.getattr){throw new FS.ErrnoError(63)}return node.node_ops.getattr(node)},lstat(path){return FS.stat(path,true)},chmod(path,mode,dontFollow){var node;if(typeof path=="string"){var lookup=FS.lookupPath(path,{follow:!dontFollow});node=lookup.node}else{node=path}if(!node.node_ops.setattr){throw new FS.ErrnoError(63)}node.node_ops.setattr(node,{mode:mode&4095|node.mode&~4095,ctime:Date.now()})},lchmod(path,mode){FS.chmod(path,mode,true)},fchmod(fd,mode){var stream=FS.getStreamChecked(fd);FS.chmod(stream.node,mode)},chown(path,uid,gid,dontFollow){var node;if(typeof path=="string"){var lookup=FS.lookupPath(path,{follow:!dontFollow});node=lookup.node}else{node=path}if(!node.node_ops.setattr){throw new FS.ErrnoError(63)}node.node_ops.setattr(node,{timestamp:Date.now()})},lchown(path,uid,gid){FS.chown(path,uid,gid,true)},fchown(fd,uid,gid){var stream=FS.getStreamChecked(fd);FS.chown(stream.node,uid,gid)},truncate(path,len){if(len<0){throw new FS.ErrnoError(28)}var node;if(typeof path=="string"){var lookup=FS.lookupPath(path,{follow:true});node=lookup.node}else{node=path}if(!node.node_ops.setattr){throw new FS.ErrnoError(63)}if(FS.isDir(node.mode)){throw new FS.ErrnoError(31)}if(!FS.isFile(node.mode)){throw new FS.ErrnoError(28)}var errCode=FS.nodePermissions(node,"w");if(errCode){throw new FS.ErrnoError(errCode)}node.node_ops.setattr(node,{size:len,timestamp:Date.now()})},ftruncate(fd,len){var stream=FS.getStreamChecked(fd);if((stream.flags&2097155)===0){throw new FS.ErrnoError(28)}FS.truncate(stream.node,len)},utime(path,atime,mtime){var lookup=FS.lookupPath(path,{follow:true});var node=lookup.node;node.node_ops.setattr(node,{atime,mtime})},open(path,flags,mode=438){if(path===""){throw new FS.ErrnoError(44)}flags=typeof flags=="string"?FS_modeStringToFlags(flags):flags;if(flags&64){mode=mode&4095|32768}else{mode=0}var node;if(typeof path=="object"){node=path}else{var lookup=FS.lookupPath(path,{follow:!(flags&131072),noent_okay:true});node=lookup.node;path=lookup.path}var created=false;if(flags&64){if(node){if(flags&128){throw new FS.ErrnoError(20)}}else{node=FS.mknod(path,mode,0);created=true}}if(!node){throw new FS.ErrnoError(44)}if(FS.isChrdev(node.mode)){flags&=~512}if(flags&65536&&!FS.isDir(node.mode)){throw new FS.ErrnoError(54)}if(!created){var errCode=FS.mayOpen(node,flags);if(errCode){throw new FS.ErrnoError(errCode)}}if(flags&512&&!created){FS.truncate(node,0)}flags&=~(128|512|131072);var stream=FS.createStream({node,path:FS.getPath(node),flags,seekable:true,position:0,stream_ops:node.stream_ops,ungotten:[],error:false});if(stream.stream_ops.open){stream.stream_ops.open(stream)}if(Module["logReadFiles"]&&!(flags&1)){if(!(path in FS.readFiles)){FS.readFiles[path]=1}}return stream},close(stream){if(FS.isClosed(stream)){throw new FS.ErrnoError(8)}if(stream.getdents)stream.getdents=null;try{if(stream.stream_ops.close){stream.stream_ops.close(stream)}}catch(e){throw e}finally{FS.closeStream(stream.fd)}stream.fd=null},isClosed(stream){return stream.fd===null},llseek(stream,offset,whence){if(FS.isClosed(stream)){throw new FS.ErrnoError(8)}if(!stream.seekable||!stream.stream_ops.llseek){throw new FS.ErrnoError(70)}if(whence!=0&&whence!=1&&whence!=2){throw new FS.ErrnoError(28)}stream.position=stream.stream_ops.llseek(stream,offset,whence);stream.ungotten=[];return stream.position},read(stream,buffer,offset,length,position){if(length<0||position<0){throw new FS.ErrnoError(28)}if(FS.isClosed(stream)){throw new FS.ErrnoError(8)}if((stream.flags&2097155)===1){throw new FS.ErrnoError(8)}if(FS.isDir(stream.node.mode)){throw new FS.ErrnoError(31)}if(!stream.stream_ops.read){throw new FS.ErrnoError(28)}var seeking=typeof position!="undefined";if(!seeking){position=stream.position}else if(!stream.seekable){throw new FS.ErrnoError(70)}var bytesRead=stream.stream_ops.read(stream,buffer,offset,length,position);if(!seeking)stream.position+=bytesRead;return bytesRead},write(stream,buffer,offset,length,position,canOwn){if(length<0||position<0){throw new FS.ErrnoError(28)}if(FS.isClosed(stream)){throw new FS.ErrnoError(8)}if((stream.flags&2097155)===0){throw new FS.ErrnoError(8)}if(FS.isDir(stream.node.mode)){throw new FS.ErrnoError(31)}if(!stream.stream_ops.write){throw new FS.ErrnoError(28)}if(stream.seekable&&stream.flags&1024){FS.llseek(stream,0,2)}var seeking=typeof position!="undefined";if(!seeking){position=stream.position}else if(!stream.seekable){throw new FS.ErrnoError(70)}var bytesWritten=stream.stream_ops.write(stream,buffer,offset,length,position,canOwn);if(!seeking)stream.position+=bytesWritten;return bytesWritten},allocate(stream,offset,length){if(FS.isClosed(stream)){throw new FS.ErrnoError(8)}if(offset<0||length<=0){throw new FS.ErrnoError(28)}if((stream.flags&2097155)===0){throw new FS.ErrnoError(8)}if(!FS.isFile(stream.node.mode)&&!FS.isDir(stream.node.mode)){throw new FS.ErrnoError(43)}if(!stream.stream_ops.allocate){throw new FS.ErrnoError(138)}stream.stream_ops.allocate(stream,offset,length)},mmap(stream,length,position,prot,flags){if((prot&2)!==0&&(flags&2)===0&&(stream.flags&2097155)!==2){throw new FS.ErrnoError(2)}if((stream.flags&2097155)===1){throw new FS.ErrnoError(2)}if(!stream.stream_ops.mmap){throw new FS.ErrnoError(43)}if(!length){throw new FS.ErrnoError(28)}return stream.stream_ops.mmap(stream,length,position,prot,flags)},msync(stream,buffer,offset,length,mmapFlags){if(!stream.stream_ops.msync){return 0}return stream.stream_ops.msync(stream,buffer,offset,length,mmapFlags)},ioctl(stream,cmd,arg){if(!stream.stream_ops.ioctl){throw new FS.ErrnoError(59)}return stream.stream_ops.ioctl(stream,cmd,arg)},readFile(path,opts={}){opts.flags=opts.flags||0;opts.encoding=opts.encoding||"binary";if(opts.encoding!=="utf8"&&opts.encoding!=="binary"){throw new Error(`Invalid encoding type "${opts.encoding}"`)}var ret;var stream=FS.open(path,opts.flags);var stat=FS.stat(path);var length=stat.size;var buf=new Uint8Array(length);FS.read(stream,buf,0,length,0);if(opts.encoding==="utf8"){ret=UTF8ArrayToString(buf)}else if(opts.encoding==="binary"){ret=buf}FS.close(stream);return ret},writeFile(path,data,opts={}){opts.flags=opts.flags||577;var stream=FS.open(path,opts.flags,opts.mode);if(typeof data=="string"){var buf=new Uint8Array(lengthBytesUTF8(data)+1);var actualNumBytes=stringToUTF8Array(data,buf,0,buf.length);FS.write(stream,buf,0,actualNumBytes,undefined,opts.canOwn)}else if(ArrayBuffer.isView(data)){FS.write(stream,data,0,data.byteLength,undefined,opts.canOwn)}else{throw new Error("Unsupported data type")}FS.close(stream)},cwd:()=>FS.currentPath,chdir(path){var lookup=FS.lookupPath(path,{follow:true});if(lookup.node===null){throw new FS.ErrnoError(44)}if(!FS.isDir(lookup.node.mode)){throw new FS.ErrnoError(54)}var errCode=FS.nodePermissions(lookup.node,"x");if(errCode){throw new FS.ErrnoError(errCode)}FS.currentPath=lookup.path},createDefaultDirectories(){FS.mkdir("/tmp");FS.mkdir("/home");FS.mkdir("/home/web_user")},createDefaultDevices(){FS.mkdir("/dev");FS.registerDevice(FS.makedev(1,3),{read:()=>0,write:(stream,buffer,offset,length,pos)=>length,llseek:()=>0});FS.mkdev("/dev/null",FS.makedev(1,3));TTY.register(FS.makedev(5,0),TTY.default_tty_ops);TTY.register(FS.makedev(6,0),TTY.default_tty1_ops);FS.mkdev("/dev/tty",FS.makedev(5,0));FS.mkdev("/dev/tty1",FS.makedev(6,0));var randomBuffer=new Uint8Array(1024),randomLeft=0;var randomByte=()=>{if(randomLeft===0){randomLeft=randomFill(randomBuffer).byteLength}return randomBuffer[--randomLeft]};FS.createDevice("/dev","random",randomByte);FS.createDevice("/dev","urandom",randomByte);FS.mkdir("/dev/shm");FS.mkdir("/dev/shm/tmp")},createSpecialDirectories(){FS.mkdir("/proc");var proc_self=FS.mkdir("/proc/self");FS.mkdir("/proc/self/fd");FS.mount({mount(){var node=FS.createNode(proc_self,"fd",16895,73);node.stream_ops={llseek:MEMFS.stream_ops.llseek};node.node_ops={lookup(parent,name){var fd=+name;var stream=FS.getStreamChecked(fd);var ret={parent:null,mount:{mountpoint:"fake"},node_ops:{readlink:()=>stream.path},id:fd+1};ret.parent=ret;return ret},readdir(){return Array.from(FS.streams.entries()).filter(([k,v])=>v).map(([k,v])=>k.toString())}};return node}},{},"/proc/self/fd")},createStandardStreams(input,output,error){if(input){FS.createDevice("/dev","stdin",input)}else{FS.symlink("/dev/tty","/dev/stdin")}if(output){FS.createDevice("/dev","stdout",null,output)}else{FS.symlink("/dev/tty","/dev/stdout")}if(error){FS.createDevice("/dev","stderr",null,error)}else{FS.symlink("/dev/tty1","/dev/stderr")}var stdin=FS.open("/dev/stdin",0);var stdout=FS.open("/dev/stdout",1);var stderr=FS.open("/dev/stderr",1)},staticInit(){FS.nameTable=new Array(4096);FS.mount(MEMFS,{},"/");FS.createDefaultDirectories();FS.createDefaultDevices();FS.createSpecialDirectories();FS.filesystems={MEMFS}},init(input,output,error){FS.initialized=true;input??=Module["stdin"];output??=Module["stdout"];error??=Module["stderr"];FS.createStandardStreams(input,output,error)},quit(){FS.initialized=false;for(var i=0;i<FS.streams.length;i++){var stream=FS.streams[i];if(!stream){continue}FS.close(stream)}},findObject(path,dontResolveLastLink){var ret=FS.analyzePath(path,dontResolveLastLink);if(!ret.exists){return null}return ret.object},analyzePath(path,dontResolveLastLink){try{var lookup=FS.lookupPath(path,{follow:!dontResolveLastLink});path=lookup.path}catch(e){}var ret={isRoot:false,exists:false,error:0,name:null,path:null,object:null,parentExists:false,parentPath:null,parentObject:null};try{var lookup=FS.lookupPath(path,{parent:true});ret.parentExists=true;ret.parentPath=lookup.path;ret.parentObject=lookup.node;ret.name=PATH.basename(path);lookup=FS.lookupPath(path,{follow:!dontResolveLastLink});ret.exists=true;ret.path=lookup.path;ret.object=lookup.node;ret.name=lookup.node.name;ret.isRoot=lookup.path==="/"}catch(e){ret.error=e.errno}return ret},createPath(parent,path,canRead,canWrite){parent=typeof parent=="string"?parent:FS.getPath(parent);var parts=path.split("/").reverse();while(parts.length){var part=parts.pop();if(!part)continue;var current=PATH.join2(parent,part);try{FS.mkdir(current)}catch(e){}parent=current}return current},createFile(parent,name,properties,canRead,canWrite){var path=PATH.join2(typeof parent=="string"?parent:FS.getPath(parent),name);var mode=FS_getMode(canRead,canWrite);return FS.create(path,mode)},createDataFile(parent,name,data,canRead,canWrite,canOwn){var path=name;if(parent){parent=typeof parent=="string"?parent:FS.getPath(parent);path=name?PATH.join2(parent,name):parent}var mode=FS_getMode(canRead,canWrite);var node=FS.create(path,mode);if(data){if(typeof data=="string"){var arr=new Array(data.length);for(var i=0,len=data.length;i<len;++i)arr[i]=data.charCodeAt(i);data=arr}FS.chmod(node,mode|146);var stream=FS.open(node,577);FS.write(stream,data,0,data.length,0,canOwn);FS.close(stream);FS.chmod(node,mode)}},createDevice(parent,name,input,output){var path=PATH.join2(typeof parent=="string"?parent:FS.getPath(parent),name);var mode=FS_getMode(!!input,!!output);FS.createDevice.major??=64;var dev=FS.makedev(FS.createDevice.major++,0);FS.registerDevice(dev,{open(stream){stream.seekable=false},close(stream){if(output?.buffer?.length){output(10)}},read(stream,buffer,offset,length,pos){var bytesRead=0;for(var i=0;i<length;i++){var result;try{result=input()}catch(e){throw new FS.ErrnoError(29)}if(result===undefined&&bytesRead===0){throw new FS.ErrnoError(6)}if(result===null||result===undefined)break;bytesRead++;buffer[offset+i]=result}if(bytesRead){stream.node.atime=Date.now()}return bytesRead},write(stream,buffer,offset,length,pos){for(var i=0;i<length;i++){try{output(buffer[offset+i])}catch(e){throw new FS.ErrnoError(29)}}if(length){stream.node.mtime=stream.node.ctime=Date.now()}return i}});return FS.mkdev(path,mode,dev)},forceLoadFile(obj){if(obj.isDevice||obj.isFolder||obj.link||obj.contents)return true;if(typeof XMLHttpRequest!="undefined"){throw new Error("Lazy loading should have been performed (contents set) in createLazyFile, but it was not. Lazy loading only works in web workers. Use --embed-file or --preload-file in emcc on the main thread.")}else{try{obj.contents=readBinary(obj.url);obj.usedBytes=obj.contents.length}catch(e){throw new FS.ErrnoError(29)}}},createLazyFile(parent,name,url,canRead,canWrite){class LazyUint8Array{lengthKnown=false;chunks=[];get(idx){if(idx>this.length-1||idx<0){return undefined}var chunkOffset=idx%this.chunkSize;var chunkNum=idx/this.chunkSize|0;return this.getter(chunkNum)[chunkOffset]}setDataGetter(getter){this.getter=getter}cacheLength(){var xhr=new XMLHttpRequest;xhr.open("HEAD",url,false);xhr.send(null);if(!(xhr.status>=200&&xhr.status<300||xhr.status===304))throw new Error("Couldn't load "+url+". Status: "+xhr.status);var datalength=Number(xhr.getResponseHeader("Content-length"));var header;var hasByteServing=(header=xhr.getResponseHeader("Accept-Ranges"))&&header==="bytes";var usesGzip=(header=xhr.getResponseHeader("Content-Encoding"))&&header==="gzip";var chunkSize=1024*1024;if(!hasByteServing)chunkSize=datalength;var doXHR=(from,to)=>{if(from>to)throw new Error("invalid range ("+from+", "+to+") or no bytes requested!");if(to>datalength-1)throw new Error("only "+datalength+" bytes available! programmer error!");var xhr=new XMLHttpRequest;xhr.open("GET",url,false);if(datalength!==chunkSize)xhr.setRequestHeader("Range","bytes="+from+"-"+to);xhr.responseType="arraybuffer";if(xhr.overrideMimeType){xhr.overrideMimeType("text/plain; charset=x-user-defined")}xhr.send(null);if(!(xhr.status>=200&&xhr.status<300||xhr.status===304))throw new Error("Couldn't load "+url+". Status: "+xhr.status);if(xhr.response!==undefined){return new Uint8Array(xhr.response||[])}return intArrayFromString(xhr.responseText||"",true)};var lazyArray=this;lazyArray.setDataGetter(chunkNum=>{var start=chunkNum*chunkSize;var end=(chunkNum+1)*chunkSize-1;end=Math.min(end,datalength-1);if(typeof lazyArray.chunks[chunkNum]=="undefined"){lazyArray.chunks[chunkNum]=doXHR(start,end)}if(typeof lazyArray.chunks[chunkNum]=="undefined")throw new Error("doXHR failed!");return lazyArray.chunks[chunkNum]});if(usesGzip||!datalength){chunkSize=datalength=1;datalength=this.getter(0).length;chunkSize=datalength;out("LazyFiles on gzip forces download of the whole file when length is accessed")}this._length=datalength;this._chunkSize=chunkSize;this.lengthKnown=true}get length(){if(!this.lengthKnown){this.cacheLength()}return this._length}get chunkSize(){if(!this.lengthKnown){this.cacheLength()}return this._chunkSize}}if(typeof XMLHttpRequest!="undefined"){if(!ENVIRONMENT_IS_WORKER)throw"Cannot do synchronous binary XHRs outside webworkers in modern browsers. Use --embed-file or --preload-file in emcc";var lazyArray=new LazyUint8Array;var properties={isDevice:false,contents:lazyArray}}else{var properties={isDevice:false,url}}var node=FS.createFile(parent,name,properties,canRead,canWrite);if(properties.contents){node.contents=properties.contents}else if(properties.url){node.contents=null;node.url=properties.url}Object.defineProperties(node,{usedBytes:{get:function(){return this.contents.length}}});var stream_ops={};var keys=Object.keys(node.stream_ops);keys.forEach(key=>{var fn=node.stream_ops[key];stream_ops[key]=(...args)=>{FS.forceLoadFile(node);return fn(...args)}});function writeChunks(stream,buffer,offset,length,position){var contents=stream.node.contents;if(position>=contents.length)return 0;var size=Math.min(contents.length-position,length);if(contents.slice){for(var i=0;i<size;i++){buffer[offset+i]=contents[position+i]}}else{for(var i=0;i<size;i++){buffer[offset+i]=contents.get(position+i)}}return size}stream_ops.read=(stream,buffer,offset,length,position)=>{FS.forceLoadFile(node);return writeChunks(stream,buffer,offset,length,position)};stream_ops.mmap=(stream,length,position,prot,flags)=>{FS.forceLoadFile(node);var ptr=mmapAlloc(length);if(!ptr){throw new FS.ErrnoError(48)}writeChunks(stream,HEAP8,ptr,length,position);return{ptr,allocated:true}};node.stream_ops=stream_ops;return node}};var UTF8ToString=(ptr,maxBytesToRead)=>ptr?UTF8ArrayToString(HEAPU8,ptr,maxBytesToRead):"";var SYSCALLS={DEFAULT_POLLMASK:5,calculateAt(dirfd,path,allowEmpty){if(PATH.isAbs(path)){return path}var dir;if(dirfd===-100){dir=FS.cwd()}else{var dirstream=SYSCALLS.getStreamFromFD(dirfd);dir=dirstream.path}if(path.length==0){if(!allowEmpty){throw new FS.ErrnoError(44)}return dir}return dir+"/"+path},doStat(func,path,buf){var stat=func(path);HEAP32[buf>>2]=stat.dev;HEAP32[buf+4>>2]=stat.mode;HEAPU32[buf+8>>2]=stat.nlink;HEAP32[buf+12>>2]=stat.uid;HEAP32[buf+16>>2]=stat.gid;HEAP32[buf+20>>2]=stat.rdev;tempI64=[stat.size>>>0,(tempDouble=stat.size,+Math.abs(tempDouble)>=1?tempDouble>0?+Math.floor(tempDouble/4294967296)>>>0:~~+Math.ceil((tempDouble-+(~~tempDouble>>>0))/4294967296)>>>0:0)],HEAP32[buf+24>>2]=tempI64[0],HEAP32[buf+28>>2]=tempI64[1];HEAP32[buf+32>>2]=4096;HEAP32[buf+36>>2]=stat.blocks;var atime=stat.atime.getTime();var mtime=stat.mtime.getTime();var ctime=stat.ctime.getTime();tempI64=[Math.floor(atime/1e3)>>>0,(tempDouble=Math.floor(atime/1e3),+Math.abs(tempDouble)>=1?tempDouble>0?+Math.floor(tempDouble/4294967296)>>>0:~~+Math.ceil((tempDouble-+(~~tempDouble>>>0))/4294967296)>>>0:0)],HEAP32[buf+40>>2]=tempI64[0],HEAP32[buf+44>>2]=tempI64[1];HEAPU32[buf+48>>2]=atime%1e3*1e3*1e3;tempI64=[Math.floor(mtime/1e3)>>>0,(tempDouble=Math.floor(mtime/1e3),+Math.abs(tempDouble)>=1?tempDouble>0?+Math.floor(tempDouble/4294967296)>>>0:~~+Math.ceil((tempDouble-+(~~tempDouble>>>0))/4294967296)>>>0:0)],HEAP32[buf+56>>2]=tempI64[0],HEAP32[buf+60>>2]=tempI64[1];HEAPU32[buf+64>>2]=mtime%1e3*1e3*1e3;tempI64=[Math.floor(ctime/1e3)>>>0,(tempDouble=Math.floor(ctime/1e3),+Math.abs(tempDouble)>=1?tempDouble>0?+Math.floor(tempDouble/4294967296)>>>0:~~+Math.ceil((tempDouble-+(~~tempDouble>>>0))/4294967296)>>>0:0)],HEAP32[buf+72>>2]=tempI64[0],HEAP32[buf+76>>2]=tempI64[1];HEAPU32[buf+80>>2]=ctime%1e3*1e3*1e3;tempI64=[stat.ino>>>0,(tempDouble=stat.ino,+Math.abs(tempDouble)>=1?tempDouble>0?+Math.floor(tempDouble/4294967296)>>>0:~~+Math.ceil((tempDouble-+(~~tempDouble>>>0))/4294967296)>>>0:0)],HEAP32[buf+88>>2]=tempI64[0],HEAP32[buf+92>>2]=tempI64[1];return 0},doMsync(addr,stream,len,flags,offset){if(!FS.isFile(stream.node.mode)){throw new FS.ErrnoError(43)}if(flags&2){return 0}var buffer=HEAPU8.slice(addr,addr+len);FS.msync(stream,buffer,offset,len,flags)},getStreamFromFD(fd){var stream=FS.getStreamChecked(fd);return stream},varargs:undefined,getStr(ptr){var ret=UTF8ToString(ptr);return ret}};function ___syscall_fcntl64(fd,cmd,varargs){SYSCALLS.varargs=varargs;try{var stream=SYSCALLS.getStreamFromFD(fd);switch(cmd){case 0:{var arg=syscallGetVarargI();if(arg<0){return-28}while(FS.streams[arg]){arg++}var newStream;newStream=FS.dupStream(stream,arg);return newStream.fd}case 1:case 2:return 0;case 3:return stream.flags;case 4:{var arg=syscallGetVarargI();stream.flags|=arg;return 0}case 12:{var arg=syscallGetVarargP();var offset=0;HEAP16[arg+offset>>1]=2;return 0}case 13:case 14:return 0}return-28}catch(e){if(typeof FS=="undefined"||!(e.name==="ErrnoError"))throw e;return-e.errno}}var stringToUTF8=(str,outPtr,maxBytesToWrite)=>stringToUTF8Array(str,HEAPU8,outPtr,maxBytesToWrite);function ___syscall_getcwd(buf,size){try{if(size===0)return-28;var cwd=FS.cwd();var cwdLengthInBytes=lengthBytesUTF8(cwd)+1;if(size<cwdLengthInBytes)return-68;stringToUTF8(cwd,buf,size);return cwdLengthInBytes}catch(e){if(typeof FS=="undefined"||!(e.name==="ErrnoError"))throw e;return-e.errno}}function ___syscall_getdents64(fd,dirp,count){try{var stream=SYSCALLS.getStreamFromFD(fd);stream.getdents||=FS.readdir(stream.path);var struct_size=280;var pos=0;var off=FS.llseek(stream,0,1);var startIdx=Math.floor(off/struct_size);var endIdx=Math.min(stream.getdents.length,startIdx+Math.floor(count/struct_size));for(var idx=startIdx;idx<endIdx;idx++){var id;var type;var name=stream.getdents[idx];if(name==="."){id=stream.node.id;type=4}else if(name===".."){var lookup=FS.lookupPath(stream.path,{parent:true});id=lookup.node.id;type=4}else{var child;try{child=FS.lookupNode(stream.node,name)}catch(e){if(e?.errno===28){continue}throw e}id=child.id;type=FS.isChrdev(child.mode)?2:FS.isDir(child.mode)?4:FS.isLink(child.mode)?10:8}tempI64=[id>>>0,(tempDouble=id,+Math.abs(tempDouble)>=1?tempDouble>0?+Math.floor(tempDouble/4294967296)>>>0:~~+Math.ceil((tempDouble-+(~~tempDouble>>>0))/4294967296)>>>0:0)],HEAP32[dirp+pos>>2]=tempI64[0],HEAP32[dirp+pos+4>>2]=tempI64[1];tempI64=[(idx+1)*struct_size>>>0,(tempDouble=(idx+1)*struct_size,+Math.abs(tempDouble)>=1?tempDouble>0?+Math.floor(tempDouble/4294967296)>>>0:~~+Math.ceil((tempDouble-+(~~tempDouble>>>0))/4294967296)>>>0:0)],HEAP32[dirp+pos+8>>2]=tempI64[0],HEAP32[dirp+pos+12>>2]=tempI64[1];HEAP16[dirp+pos+16>>1]=280;HEAP8[dirp+pos+18]=type;stringToUTF8(name,dirp+pos+19,256);pos+=struct_size}FS.llseek(stream,idx*struct_size,0);return pos}catch(e){if(typeof FS=="undefined"||!(e.name==="ErrnoError"))throw e;return-e.errno}}function ___syscall_ioctl(fd,op,varargs){SYSCALLS.varargs=varargs;try{var stream=SYSCALLS.getStreamFromFD(fd);switch(op){case 21509:{if(!stream.tty)return-59;return 0}case 21505:{if(!stream.tty)return-59;if(stream.tty.ops.ioctl_tcgets){var termios=stream.tty.ops.ioctl_tcgets(stream);var argp=syscallGetVarargP();HEAP32[argp>>2]=termios.c_iflag||0;HEAP32[argp+4>>2]=termios.c_oflag||0;HEAP32[argp+8>>2]=termios.c_cflag||0;HEAP32[argp+12>>2]=termios.c_lflag||0;for(var i=0;i<32;i++){HEAP8[argp+i+17]=termios.c_cc[i]||0}return 0}return 0}case 21510:case 21511:case 21512:{if(!stream.tty)return-59;return 0}case 21506:case 21507:case 21508:{if(!stream.tty)return-59;if(stream.tty.ops.ioctl_tcsets){var argp=syscallGetVarargP();var c_iflag=HEAP32[argp>>2];var c_oflag=HEAP32[argp+4>>2];var c_cflag=HEAP32[argp+8>>2];var c_lflag=HEAP32[argp+12>>2];var c_cc=[];for(var i=0;i<32;i++){c_cc.push(HEAP8[argp+i+17])}return stream.tty.ops.ioctl_tcsets(stream.tty,op,{c_iflag,c_oflag,c_cflag,c_lflag,c_cc})}return 0}case 21519:{if(!stream.tty)return-59;var argp=syscallGetVarargP();HEAP32[argp>>2]=0;return 0}case 21520:{if(!stream.tty)return-59;return-28}case 21531:{var argp=syscallGetVarargP();return FS.ioctl(stream,op,argp)}case 21523:{if(!stream.tty)return-59;if(stream.tty.ops.ioctl_tiocgwinsz){var winsize=stream.tty.ops.ioctl_tiocgwinsz(stream.tty);var argp=syscallGetVarargP();HEAP16[argp>>1]=winsize[0];HEAP16[argp+2>>1]=winsize[1]}return 0}case 21524:{if(!stream.tty)return-59;return 0}case 21515:{if(!stream.tty)return-59;return 0}default:return-28}}catch(e){if(typeof FS=="undefined"||!(e.name==="ErrnoError"))throw e;return-e.errno}}function ___syscall_lstat64(path,buf){try{path=SYSCALLS.getStr(path);return SYSCALLS.doStat(FS.lstat,path,buf)}catch(e){if(typeof FS=="undefined"||!(e.name==="ErrnoError"))throw e;return-e.errno}}function ___syscall_newfstatat(dirfd,path,buf,flags){try{path=SYSCALLS.getStr(path);var nofollow=flags&256;var allowEmpty=flags&4096;flags=flags&~6400;path=SYSCALLS.calculateAt(dirfd,path,allowEmpty);return SYSCALLS.doStat(nofollow?FS.lstat:FS.stat,path,buf)}catch(e){if(typeof FS=="undefined"||!(e.name==="ErrnoError"))throw e;return-e.errno}}function ___syscall_openat(dirfd,path,flags,varargs){SYSCALLS.varargs=varargs;try{path=SYSCALLS.getStr(path);path=SYSCALLS.calculateAt(dirfd,path);var mode=varargs?syscallGetVarargI():0;return FS.open(path,flags,mode).fd}catch(e){if(typeof FS=="undefined"||!(e.name==="ErrnoError"))throw e;return-e.errno}}function ___syscall_stat64(path,buf){try{path=SYSCALLS.getStr(path);return SYSCALLS.doStat(FS.stat,path,buf)}catch(e){if(typeof FS=="undefined"||!(e.name==="ErrnoError"))throw e;return-e.errno}}var __abort_js=()=>abort("");var __emscripten_fs_load_embedded_files=ptr=>{do{var name_addr=HEAPU32[ptr>>2];ptr+=4;var len=HEAPU32[ptr>>2];ptr+=4;var content=HEAPU32[ptr>>2];ptr+=4;var name=UTF8ToString(name_addr);FS.createPath("/",PATH.dirname(name),true,true);FS.createDataFile(name,null,HEAP8.subarray(content,content+len),true,true,true)}while(HEAPU32[ptr>>2])};var __emscripten_memcpy_js=(dest,src,num)=>HEAPU8.copyWithin(dest,src,src+num);var __tzset_js=(timezone,daylight,std_name,dst_name)=>{var currentYear=(new Date).getFullYear();var winter=new Date(currentYear,0,1);var summer=new Date(currentYear,6,1);var winterOffset=winter.getTimezoneOffset();var summerOffset=summer.getTimezoneOffset();var stdTimezoneOffset=Math.max(winterOffset,summerOffset);HEAPU32[timezone>>2]=stdTimezoneOffset*60;HEAP32[daylight>>2]=Number(winterOffset!=summerOffset);var extractZone=timezoneOffset=>{var sign=timezoneOffset>=0?"-":"+";var absOffset=Math.abs(timezoneOffset);var hours=String(Math.floor(absOffset/60)).padStart(2,"0");var minutes=String(absOffset%60).padStart(2,"0");return`UTC${sign}${hours}${minutes}`};var winterName=extractZone(winterOffset);var summerName=extractZone(summerOffset);if(summerOffset<winterOffset){stringToUTF8(winterName,std_name,17);stringToUTF8(summerName,dst_name,17)}else{stringToUTF8(winterName,dst_name,17);stringToUTF8(summerName,std_name,17)}};var getHeapMax=()=>2147483648;var growMemory=size=>{var b=wasmMemory.buffer;var pages=(size-b.byteLength+65535)/65536|0;try{wasmMemory.grow(pages);updateMemoryViews();return 1}catch(e){}};var _emscripten_resize_heap=requestedSize=>{var oldSize=HEAPU8.length;requestedSize>>>=0;var maxHeapSize=getHeapMax();if(requestedSize>maxHeapSize){return false}for(var cutDown=1;cutDown<=4;cutDown*=2){var overGrownHeapSize=oldSize*(1+.2/cutDown);overGrownHeapSize=Math.min(overGrownHeapSize,requestedSize+100663296);var newSize=Math.min(maxHeapSize,alignMemory(Math.max(requestedSize,overGrownHeapSize),65536));var replacement=growMemory(newSize);if(replacement){return true}}return false};var ENV={};var getExecutableName=()=>thisProgram||"./this.program";var getEnvStrings=()=>{if(!getEnvStrings.strings){var lang=(typeof navigator=="object"&&navigator.languages&&navigator.languages[0]||"C").replace("-","_")+".UTF-8";var env={USER:"web_user",LOGNAME:"web_user",PATH:"/",PWD:"/",HOME:"/home/web_user",LANG:lang,_:getExecutableName()};for(var x in ENV){if(ENV[x]===undefined)delete env[x];else env[x]=ENV[x]}var strings=[];for(var x in env){strings.push(`${x}=${env[x]}`)}getEnvStrings.strings=strings}return getEnvStrings.strings};var stringToAscii=(str,buffer)=>{for(var i=0;i<str.length;++i){HEAP8[buffer++]=str.charCodeAt(i)}HEAP8[buffer]=0};var _environ_get=(__environ,environ_buf)=>{var bufSize=0;getEnvStrings().forEach((string,i)=>{var ptr=environ_buf+bufSize;HEAPU32[__environ+i*4>>2]=ptr;stringToAscii(string,ptr);bufSize+=string.length+1});return 0};var _environ_sizes_get=(penviron_count,penviron_buf_size)=>{var strings=getEnvStrings();HEAPU32[penviron_count>>2]=strings.length;var bufSize=0;strings.forEach(string=>bufSize+=string.length+1);HEAPU32[penviron_buf_size>>2]=bufSize;return 0};function _fd_close(fd){try{var stream=SYSCALLS.getStreamFromFD(fd);FS.close(stream);return 0}catch(e){if(typeof FS=="undefined"||!(e.name==="ErrnoError"))throw e;return e.errno}}var doReadv=(stream,iov,iovcnt,offset)=>{var ret=0;for(var i=0;i<iovcnt;i++){var ptr=HEAPU32[iov>>2];var len=HEAPU32[iov+4>>2];iov+=8;var curr=FS.read(stream,HEAP8,ptr,len,offset);if(curr<0)return-1;ret+=curr;if(curr<len)break;if(typeof offset!="undefined"){offset+=curr}}return ret};function _fd_read(fd,iov,iovcnt,pnum){try{var stream=SYSCALLS.getStreamFromFD(fd);var num=doReadv(stream,iov,iovcnt);HEAPU32[pnum>>2]=num;return 0}catch(e){if(typeof FS=="undefined"||!(e.name==="ErrnoError"))throw e;return e.errno}}var convertI32PairToI53Checked=(lo,hi)=>hi+2097152>>>0<4194305-!!lo?(lo>>>0)+hi*4294967296:NaN;function _fd_seek(fd,offset_low,offset_high,whence,newOffset){var offset=convertI32PairToI53Checked(offset_low,offset_high);try{if(isNaN(offset))return 61;var stream=SYSCALLS.getStreamFromFD(fd);FS.llseek(stream,offset,whence);tempI64=[stream.position>>>0,(tempDouble=stream.position,+Math.abs(tempDouble)>=1?tempDouble>0?+Math.floor(tempDouble/4294967296)>>>0:~~+Math.ceil((tempDouble-+(~~tempDouble>>>0))/4294967296)>>>0:0)],HEAP32[newOffset>>2]=tempI64[0],HEAP32[newOffset+4>>2]=tempI64[1];if(stream.getdents&&offset===0&&whence===0)stream.getdents=null;return 0}catch(e){if(typeof FS=="undefined"||!(e.name==="ErrnoError"))throw e;return e.errno}}var doWritev=(stream,iov,iovcnt,offset)=>{var ret=0;for(var i=0;i<iovcnt;i++){var ptr=HEAPU32[iov>>2];var len=HEAPU32[iov+4>>2];iov+=8;var curr=FS.write(stream,HEAP8,ptr,len,offset);if(curr<0)return-1;ret+=curr;if(curr<len){break}if(typeof offset!="undefined"){offset+=curr}}return ret};function _fd_write(fd,iov,iovcnt,pnum){try{var stream=SYSCALLS.getStreamFromFD(fd);var num=doWritev(stream,iov,iovcnt);HEAPU32[pnum>>2]=num;return 0}catch(e){if(typeof FS=="undefined"||!(e.name==="ErrnoError"))throw e;return e.errno}}function _random_get(buffer,size){try{randomFill(HEAPU8.subarray(buffer,buffer+size));return 0}catch(e){if(typeof FS=="undefined"||!(e.name==="ErrnoError"))throw e;return e.errno}}var getCFunc=ident=>{var func=Module["_"+ident];return func};var writeArrayToMemory=(array,buffer)=>{HEAP8.set(array,buffer)};var stackAlloc=sz=>__emscripten_stack_alloc(sz);var stringToUTF8OnStack=str=>{var size=lengthBytesUTF8(str)+1;var ret=stackAlloc(size);stringToUTF8(str,ret,size);return ret};var stackSave=()=>_emscripten_stack_get_current();var stackRestore=val=>__emscripten_stack_restore(val);var ccall=(ident,returnType,argTypes,args,opts)=>{var toC={string:str=>{var ret=0;if(str!==null&&str!==undefined&&str!==0){ret=stringToUTF8OnStack(str)}return ret},array:arr=>{var ret=stackAlloc(arr.length);writeArrayToMemory(arr,ret);return ret}};function convertReturnValue(ret){if(returnType==="string"){return UTF8ToString(ret)}if(returnType==="boolean")return Boolean(ret);return ret}var func=getCFunc(ident);var cArgs=[];var stack=0;if(args){for(var i=0;i<args.length;i++){var converter=toC[argTypes[i]];if(converter){if(stack===0)stack=stackSave();cArgs[i]=converter(args[i])}else{cArgs[i]=args[i]}}}var ret=func(...cArgs);function onDone(ret){if(stack!==0)stackRestore(stack);return convertReturnValue(ret)}ret=onDone(ret);return ret};var cwrap=(ident,returnType,argTypes,opts)=>{var numericArgs=!argTypes||argTypes.every(type=>type==="number"||type==="boolean");var numericRet=returnType!=="string";if(numericRet&&numericArgs&&!opts){return getCFunc(ident)}return(...args)=>ccall(ident,returnType,argTypes,args,opts)};var FS_createPath=FS.createPath;var FS_unlink=path=>FS.unlink(path);var FS_createLazyFile=FS.createLazyFile;var FS_createDevice=FS.createDevice;FS.createPreloadedFile=FS_createPreloadedFile;FS.staticInit();Module["FS_createPath"]=FS.createPath;Module["FS_createDataFile"]=FS.createDataFile;Module["FS_createPreloadedFile"]=FS.createPreloadedFile;Module["FS_unlink"]=FS.unlink;Module["FS_createLazyFile"]=FS.createLazyFile;Module["FS_createDevice"]=FS.createDevice;MEMFS.doesNotExistError=new FS.ErrnoError(44);MEMFS.doesNotExistError.stack="<generic error, no stack>";var wasmImports={c:___syscall_fcntl64,o:___syscall_getcwd,g:___syscall_getdents64,f:___syscall_ioctl,l:___syscall_lstat64,m:___syscall_newfstatat,d:___syscall_openat,n:___syscall_stat64,j:__abort_js,t:__emscripten_fs_load_embedded_files,s:__emscripten_memcpy_js,p:__tzset_js,h:_emscripten_resize_heap,q:_environ_get,r:_environ_sizes_get,a:_fd_close,e:_fd_read,i:_fd_seek,b:_fd_write,k:_random_get};var wasmExports;createWasm();var ___wasm_call_ctors=()=>(___wasm_call_ctors=wasmExports["v"])();var _dasmig_persona_generate=Module["_dasmig_persona_generate"]=(a0,a1,a2,a3,a4,a5,a6)=>(_dasmig_persona_generate=Module["_dasmig_persona_generate"]=wasmExports["w"])(a0,a1,a2,a3,a4,a5,a6);var _dasmig_persona_generate_batch=Module["_dasmig_persona_generate_batch"]=(a0,a1,a2,a3,a4,a5,a6,a7,a8)=>(_dasmig_persona_generate_batch=Module["_dasmig_persona_generate_batch"]=wasmExports["y"])(a0,a1,a2,a3,a4,a5,a6,a7,a8);var _dasmig_persona_info=Module["_dasmig_persona_info"]=()=>(_dasmig_persona_info=Module["_dasmig_persona_info"]=wasmExports["z"])();var ___trap=()=>(___trap=wasmExports["A"])();var __emscripten_stack_restore=a0=>(__emscripten_stack_restore=wasmExports["B"])(a0);var __emscripten_stack_alloc=a0=>(__emscripten_stack_alloc=wasmExports["C"])(a0);var _emscripten_stack_get_current=()=>(_emscripten_stack_get_current=wasmExports["D"])();var ___emscripten_embedded_file_data=Module["___emscripten_embedded_file_data"]=7499860;Module["addRunDependency"]=addRunDependency;Module["removeRunDependency"]=removeRunDependency;Module["cwrap"]=cwrap;Module["FS_createPreloadedFile"]=FS_createPreloadedFile;Module["FS_unlink"]=FS_unlink;Module["FS_createPath"]=FS_createPath;Module["FS_createDevice"]=FS_createDevice;Module["FS_createDataFile"]=FS_createDataFile;Module["FS_createLazyFile"]=FS_createLazyFile;var calledRun;dependenciesFulfilled=function runCaller(){if(!calledRun)run();if(!calledRun)dependenciesFulfilled=runCaller};function run(){if(runDependencies>0){return}preRun();if(runDependencies>0){return}function doRun(){if(calledRun)return;calledRun=true;Module["calledRun"]=true;if(ABORT)return;initRuntime();readyPromiseResolve(Module);Module["onRuntimeInitialized"]?.();postRun()}if(Module["setStatus"]){Module["setStatus"]("Running...");setTimeout(()=>{setTimeout(()=>Module["setStatus"](""),1);doRun()},1)}else{doRun()}}if(Module["preInit"]){if(typeof Module["preInit"]=="function")Module["preInit"]=[Module["preInit"]];while(Module["preInit"].length>0){Module["preInit"].pop()()}}run();moduleRtn=readyPromise;
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
return moduleRtn;
|
|
13
|
+
}
|
|
14
|
+
);
|
|
15
|
+
})();
|
|
16
|
+
export default createPersonaModule;
|
package/persona.wasm
ADDED
|
Binary file
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
// What a pool worker does once it has a generator: answer batch pieces. The
|
|
2
|
+
// two worker entry points (pool-worker.mjs for browsers, pool-worker-node.mjs
|
|
3
|
+
// for Node) supply the generator and the messaging.
|
|
4
|
+
export function serve(generator, send, subscribe) {
|
|
5
|
+
subscribe(message => {
|
|
6
|
+
try {
|
|
7
|
+
send({ id: message.id, jsonl: generator.generateBatchJsonl(message.options) });
|
|
8
|
+
} catch (error) {
|
|
9
|
+
send({ id: message.id, error: { name: error.name, message: error.message } });
|
|
10
|
+
}
|
|
11
|
+
});
|
|
12
|
+
send({ ready: true });
|
|
13
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
// A pool worker for Node (a worker thread started by createPersonaPool).
|
|
2
|
+
import { parentPort } from 'node:worker_threads';
|
|
3
|
+
import { createPersonaGenerator } from './node.mjs';
|
|
4
|
+
import { serve } from './pool-worker-core.mjs';
|
|
5
|
+
|
|
6
|
+
serve(await createPersonaGenerator(), message => parentPort.postMessage(message), handler => {
|
|
7
|
+
parentPort.on('message', handler);
|
|
8
|
+
});
|
package/pool-worker.mjs
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
// A pool worker for browsers (a module Web Worker started by createPersonaPool).
|
|
2
|
+
import { createPersonaGenerator } from './index.mjs';
|
|
3
|
+
import { serve } from './pool-worker-core.mjs';
|
|
4
|
+
|
|
5
|
+
serve(await createPersonaGenerator(), message => self.postMessage(message), handler => {
|
|
6
|
+
self.onmessage = event => handler(event.data);
|
|
7
|
+
});
|
package/pool.mjs
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
// A pool of workers, each with its own copy of the module, that generates a
|
|
2
|
+
// batch in pieces and joins them in order. The pool is independent of where the
|
|
3
|
+
// workers come from: the browser entry spawns Web Workers, the Node entry
|
|
4
|
+
// worker threads, and each passes `spawn`, which returns
|
|
5
|
+
//
|
|
6
|
+
// { send(message), onMessage(handler), onError(handler), terminate() }
|
|
7
|
+
//
|
|
8
|
+
// No SharedArrayBuffer is needed: a piece is a string of JSON Lines.
|
|
9
|
+
import { batchPlan, outOfRange } from './batch.mjs';
|
|
10
|
+
|
|
11
|
+
function rebuild(error) {
|
|
12
|
+
const type = { TypeError, RangeError }[error.name] ?? Error;
|
|
13
|
+
return new type(error.message);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function poolSize(options, fallback) {
|
|
17
|
+
if (options === null || typeof options !== 'object') throw new TypeError('pool options must be an object');
|
|
18
|
+
for (const key of Object.keys(options))
|
|
19
|
+
if (key !== 'workers') throw new TypeError(`unknown pool option "${key}"; the option is workers`);
|
|
20
|
+
const { workers = fallback } = options;
|
|
21
|
+
if (!Number.isInteger(workers)) throw new TypeError('workers must be an integer');
|
|
22
|
+
if (workers < 1 || workers > 64) outOfRange(`workers must be from 1 to 64, not ${workers}`);
|
|
23
|
+
return workers;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export async function createPool(spawn, size) {
|
|
27
|
+
let sequence = 0;
|
|
28
|
+
let closed = false;
|
|
29
|
+
const pending = new Map();
|
|
30
|
+
const workers = [];
|
|
31
|
+
const started = [];
|
|
32
|
+
|
|
33
|
+
for (let i = 0; i < size; ++i) {
|
|
34
|
+
const worker = spawn();
|
|
35
|
+
workers.push(worker);
|
|
36
|
+
started.push(new Promise((resolve, reject) => {
|
|
37
|
+
worker.onMessage(message => {
|
|
38
|
+
if (message.ready) return resolve();
|
|
39
|
+
const entry = pending.get(message.id);
|
|
40
|
+
if (!entry) return;
|
|
41
|
+
pending.delete(message.id);
|
|
42
|
+
if (message.error) entry.reject(rebuild(message.error));
|
|
43
|
+
else entry.resolve(message.jsonl);
|
|
44
|
+
});
|
|
45
|
+
worker.onError(error => {
|
|
46
|
+
reject(error);
|
|
47
|
+
for (const [id, entry] of pending) {
|
|
48
|
+
if (entry.worker !== worker) continue;
|
|
49
|
+
pending.delete(id);
|
|
50
|
+
entry.reject(error);
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
}));
|
|
54
|
+
}
|
|
55
|
+
try {
|
|
56
|
+
await Promise.all(started);
|
|
57
|
+
} catch (error) {
|
|
58
|
+
for (const worker of workers) worker.terminate();
|
|
59
|
+
throw error;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function run(worker, options) {
|
|
63
|
+
return new Promise((resolve, reject) => {
|
|
64
|
+
const id = ++sequence;
|
|
65
|
+
pending.set(id, { resolve, reject, worker });
|
|
66
|
+
worker.send({ id, options });
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
async function generateBatchJsonl(options) {
|
|
71
|
+
if (closed) throw new Error('the pool is closed');
|
|
72
|
+
const plan = batchPlan(options);
|
|
73
|
+
const pieces = [];
|
|
74
|
+
for (let c = 0; c < size; ++c) {
|
|
75
|
+
const lo = Math.floor((plan.count * c) / size);
|
|
76
|
+
const hi = Math.floor((plan.count * (c + 1)) / size);
|
|
77
|
+
if (hi === lo) continue;
|
|
78
|
+
pieces.push(run(workers[c], { request: plan.request, seed: plan.seed, first: plan.first + lo, count: hi - lo }));
|
|
79
|
+
}
|
|
80
|
+
return (await Promise.all(pieces)).join('');
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
return Object.freeze({
|
|
84
|
+
size,
|
|
85
|
+
generateBatchJsonl,
|
|
86
|
+
async generateBatch(options) {
|
|
87
|
+
const text = await generateBatchJsonl(options);
|
|
88
|
+
return text === '' ? [] : text.slice(0, -1).split('\n').map(line => JSON.parse(line));
|
|
89
|
+
},
|
|
90
|
+
close() {
|
|
91
|
+
closed = true;
|
|
92
|
+
for (const worker of workers) worker.terminate();
|
|
93
|
+
}
|
|
94
|
+
});
|
|
95
|
+
}
|
package/versions.mjs
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
// Generated by CMakeLists.txt: the library versions this module was built from.
|
|
2
|
+
export const versions = Object.freeze({
|
|
3
|
+
'persona-generator': 'v3.0.0',
|
|
4
|
+
'entity-generator': 'v4.2.0',
|
|
5
|
+
'country-generator': 'v3.1.2',
|
|
6
|
+
'city-generator': 'v4.2.0',
|
|
7
|
+
'birth-generator': 'v3.1.2',
|
|
8
|
+
'biodata-generator': 'v5.0.0',
|
|
9
|
+
'name-generator': 'v4.2.2',
|
|
10
|
+
'interest-generator': 'v3.0.1',
|
|
11
|
+
'occupation-generator': 'v3.0.1',
|
|
12
|
+
'trait-generator': 'v2.1.2',
|
|
13
|
+
});
|