@turntrout/subfont 1.9.12 → 1.9.13
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/lib/fontConverter.d.ts.map +1 -1
- package/lib/fontConverter.js +57 -68
- package/lib/fontConverter.js.map +1 -1
- package/package.json +1 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fontConverter.d.ts","sourceRoot":"","sources":["../src/fontConverter.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"fontConverter.d.ts","sourceRoot":"","sources":["../src/fontConverter.ts"],"names":[],"mappings":"AAuIA,wBAAsB,OAAO,CAC3B,MAAM,EAAE,MAAM,GAAG,UAAU,EAC3B,YAAY,EAAE,MAAM,EACpB,YAAY,CAAC,EAAE,MAAM,GACpB,OAAO,CAAC,MAAM,CAAC,CAQjB;AAED,wBAAsB,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC,CAM7C"}
|
package/lib/fontConverter.js
CHANGED
|
@@ -8,72 +8,59 @@ const worker_threads_1 = require("worker_threads");
|
|
|
8
8
|
const workerPath = pathModule.join(__dirname, 'fontConverterWorker.js');
|
|
9
9
|
const CONVERT_TIMEOUT_MS = 120_000;
|
|
10
10
|
const POOL_SIZE = Math.max(1, Math.min(os.cpus().length, 8));
|
|
11
|
-
|
|
12
|
-
const
|
|
13
|
-
|
|
11
|
+
let _activeCount = 0;
|
|
12
|
+
const _queue = [];
|
|
13
|
+
const _idleWorkers = [];
|
|
14
|
+
const _allWorkers = new Set();
|
|
15
|
+
function acquireSlot() {
|
|
16
|
+
if (_activeCount < POOL_SIZE) {
|
|
17
|
+
_activeCount++;
|
|
18
|
+
return Promise.resolve();
|
|
19
|
+
}
|
|
20
|
+
return new Promise((resolve) => {
|
|
21
|
+
_queue.push(() => {
|
|
22
|
+
_activeCount++;
|
|
23
|
+
resolve();
|
|
24
|
+
});
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
function releaseSlot() {
|
|
28
|
+
_activeCount--;
|
|
29
|
+
const next = _queue.shift();
|
|
30
|
+
if (next)
|
|
31
|
+
next();
|
|
32
|
+
}
|
|
33
|
+
function createWorker() {
|
|
14
34
|
const worker = new worker_threads_1.Worker(workerPath);
|
|
15
35
|
worker.unref();
|
|
16
|
-
|
|
36
|
+
_allWorkers.add(worker);
|
|
37
|
+
// Drop crashed idle workers from the cache so we never hand a zombie
|
|
38
|
+
// to a future caller. doConvert attaches its own short-lived exit
|
|
39
|
+
// handler for the in-flight case; both can fire harmlessly.
|
|
17
40
|
worker.on('exit', () => {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
41
|
+
_allWorkers.delete(worker);
|
|
42
|
+
const idx = _idleWorkers.indexOf(worker);
|
|
43
|
+
if (idx !== -1)
|
|
44
|
+
_idleWorkers.splice(idx, 1);
|
|
21
45
|
});
|
|
22
|
-
return
|
|
23
|
-
}
|
|
24
|
-
function acquire() {
|
|
25
|
-
const idle = _pool.find((e) => !e.busy);
|
|
26
|
-
if (idle) {
|
|
27
|
-
idle.busy = true;
|
|
28
|
-
return idle;
|
|
29
|
-
}
|
|
30
|
-
if (_pool.length < POOL_SIZE) {
|
|
31
|
-
const entry = createEntry();
|
|
32
|
-
entry.busy = true;
|
|
33
|
-
_pool.push(entry);
|
|
34
|
-
return entry;
|
|
35
|
-
}
|
|
36
|
-
return null;
|
|
46
|
+
return worker;
|
|
37
47
|
}
|
|
38
|
-
function
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
}
|
|
48
|
+
function discardWorker(worker) {
|
|
49
|
+
_allWorkers.delete(worker);
|
|
50
|
+
const idx = _idleWorkers.indexOf(worker);
|
|
51
|
+
if (idx !== -1)
|
|
52
|
+
_idleWorkers.splice(idx, 1);
|
|
53
|
+
worker.terminate().catch(() => { });
|
|
45
54
|
}
|
|
46
|
-
function
|
|
47
|
-
const idx = _pool.indexOf(entry);
|
|
48
|
-
if (idx === -1)
|
|
49
|
-
return;
|
|
50
|
-
try {
|
|
51
|
-
const replacement = createEntry();
|
|
52
|
-
_pool[idx] = replacement;
|
|
53
|
-
if (_waiters.length > 0) {
|
|
54
|
-
replacement.busy = true;
|
|
55
|
-
const waiter = _waiters.shift();
|
|
56
|
-
waiter(replacement);
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
catch {
|
|
60
|
-
// Worker creation failed — shrink the pool. In the degenerate case
|
|
61
|
-
// where all workers are unspawnable, the pool empties and pending
|
|
62
|
-
// waiters will hang until the caller's own timeout fires.
|
|
63
|
-
_pool.splice(idx, 1);
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
function doConvert(entry, buffer, targetFormat, sourceFormat) {
|
|
55
|
+
function doConvert(worker, buffer, targetFormat, sourceFormat) {
|
|
67
56
|
return new Promise((resolve, reject) => {
|
|
68
57
|
let settled = false;
|
|
69
|
-
const { worker } = entry;
|
|
70
58
|
const timer = setTimeout(() => {
|
|
71
59
|
if (settled)
|
|
72
60
|
return;
|
|
73
61
|
settled = true;
|
|
74
62
|
cleanup();
|
|
75
|
-
worker
|
|
76
|
-
replaceEntry(entry);
|
|
63
|
+
discardWorker(worker);
|
|
77
64
|
reject(new Error(`Font conversion to ${targetFormat} timed out after ${CONVERT_TIMEOUT_MS}ms`));
|
|
78
65
|
}, CONVERT_TIMEOUT_MS);
|
|
79
66
|
timer.unref();
|
|
@@ -88,7 +75,7 @@ function doConvert(entry, buffer, targetFormat, sourceFormat) {
|
|
|
88
75
|
return;
|
|
89
76
|
settled = true;
|
|
90
77
|
cleanup();
|
|
91
|
-
|
|
78
|
+
_idleWorkers.push(worker);
|
|
92
79
|
if (msg.type === 'result' && msg.buffer) {
|
|
93
80
|
resolve(Buffer.from(msg.buffer));
|
|
94
81
|
}
|
|
@@ -101,8 +88,7 @@ function doConvert(entry, buffer, targetFormat, sourceFormat) {
|
|
|
101
88
|
return;
|
|
102
89
|
settled = true;
|
|
103
90
|
cleanup();
|
|
104
|
-
worker
|
|
105
|
-
replaceEntry(entry);
|
|
91
|
+
discardWorker(worker);
|
|
106
92
|
reject(err);
|
|
107
93
|
};
|
|
108
94
|
const onExit = (code) => {
|
|
@@ -110,7 +96,7 @@ function doConvert(entry, buffer, targetFormat, sourceFormat) {
|
|
|
110
96
|
return;
|
|
111
97
|
settled = true;
|
|
112
98
|
cleanup();
|
|
113
|
-
|
|
99
|
+
discardWorker(worker);
|
|
114
100
|
reject(new Error(`Font converter worker exited unexpectedly with code ${code}`));
|
|
115
101
|
};
|
|
116
102
|
worker.on('message', onMessage);
|
|
@@ -122,23 +108,26 @@ function doConvert(entry, buffer, targetFormat, sourceFormat) {
|
|
|
122
108
|
catch (err) {
|
|
123
109
|
settled = true;
|
|
124
110
|
cleanup();
|
|
125
|
-
|
|
111
|
+
discardWorker(worker);
|
|
126
112
|
reject(err);
|
|
127
113
|
}
|
|
128
114
|
});
|
|
129
115
|
}
|
|
130
|
-
function convert(buffer, targetFormat, sourceFormat) {
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
116
|
+
async function convert(buffer, targetFormat, sourceFormat) {
|
|
117
|
+
await acquireSlot();
|
|
118
|
+
try {
|
|
119
|
+
const worker = _idleWorkers.pop() ?? createWorker();
|
|
120
|
+
return await doConvert(worker, buffer, targetFormat, sourceFormat);
|
|
121
|
+
}
|
|
122
|
+
finally {
|
|
123
|
+
releaseSlot();
|
|
134
124
|
}
|
|
135
|
-
return new Promise((resolve) => {
|
|
136
|
-
_waiters.push(resolve);
|
|
137
|
-
}).then((e) => doConvert(e, buffer, targetFormat, sourceFormat));
|
|
138
125
|
}
|
|
139
126
|
async function destroy() {
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
127
|
+
_queue.length = 0;
|
|
128
|
+
_idleWorkers.length = 0;
|
|
129
|
+
const workers = Array.from(_allWorkers);
|
|
130
|
+
_allWorkers.clear();
|
|
131
|
+
await Promise.all(workers.map((w) => w.terminate()));
|
|
143
132
|
}
|
|
144
133
|
//# sourceMappingURL=fontConverter.js.map
|
package/lib/fontConverter.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fontConverter.js","sourceRoot":"","sources":["../src/fontConverter.ts"],"names":[],"mappings":";;
|
|
1
|
+
{"version":3,"file":"fontConverter.js","sourceRoot":"","sources":["../src/fontConverter.ts"],"names":[],"mappings":";;AAuIA,0BAYC;AAED,0BAMC;AA3JD,mCAAoC;AACpC,yBAA0B;AAC1B,mDAAwC;AAExC,MAAM,UAAU,GAAG,UAAU,CAAC,IAAI,CAAC,SAAS,EAAE,wBAAwB,CAAC,CAAC;AACxE,MAAM,kBAAkB,GAAG,OAAO,CAAC;AACnC,MAAM,SAAS,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,IAAI,EAAE,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC,CAAC;AAQ7D,IAAI,YAAY,GAAG,CAAC,CAAC;AACrB,MAAM,MAAM,GAAsB,EAAE,CAAC;AACrC,MAAM,YAAY,GAAa,EAAE,CAAC;AAClC,MAAM,WAAW,GAAG,IAAI,GAAG,EAAU,CAAC;AAEtC,SAAS,WAAW;IAClB,IAAI,YAAY,GAAG,SAAS,EAAE,CAAC;QAC7B,YAAY,EAAE,CAAC;QACf,OAAO,OAAO,CAAC,OAAO,EAAE,CAAC;IAC3B,CAAC;IACD,OAAO,IAAI,OAAO,CAAO,CAAC,OAAO,EAAE,EAAE;QACnC,MAAM,CAAC,IAAI,CAAC,GAAG,EAAE;YACf,YAAY,EAAE,CAAC;YACf,OAAO,EAAE,CAAC;QACZ,CAAC,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;AACL,CAAC;AAED,SAAS,WAAW;IAClB,YAAY,EAAE,CAAC;IACf,MAAM,IAAI,GAAG,MAAM,CAAC,KAAK,EAAE,CAAC;IAC5B,IAAI,IAAI;QAAE,IAAI,EAAE,CAAC;AACnB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