@weborigami/async-tree 0.3.3-jse.2 → 0.3.3
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/package.json +2 -2
- package/shared.js +1 -1
- package/src/drivers/FileTree.js +8 -1
- package/src/drivers/limitConcurrency.js +62 -0
- package/src/operations/concat.js +29 -0
- package/src/operations/deepText.js +19 -23
- package/test/drivers/limitConcurrency.test.js +41 -0
- package/test/operations/concat.test.js +34 -0
- package/test/operations/deepText.test.js +5 -27
- package/src/operations/text.js +0 -25
- package/test/operations/text.test.js +0 -12
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@weborigami/async-tree",
|
|
3
|
-
"version": "0.3.3
|
|
3
|
+
"version": "0.3.3",
|
|
4
4
|
"description": "Asynchronous tree drivers based on standard JavaScript classes",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./main.js",
|
|
7
7
|
"browser": "./browser.js",
|
|
8
8
|
"types": "./index.ts",
|
|
9
9
|
"dependencies": {
|
|
10
|
-
"@weborigami/types": "0.3.3
|
|
10
|
+
"@weborigami/types": "0.3.3"
|
|
11
11
|
},
|
|
12
12
|
"devDependencies": {
|
|
13
13
|
"@types/node": "22.13.13",
|
package/shared.js
CHANGED
|
@@ -14,6 +14,7 @@ export * as jsonKeys from "./src/jsonKeys.js";
|
|
|
14
14
|
export { default as addNextPrevious } from "./src/operations/addNextPrevious.js";
|
|
15
15
|
export { default as cache } from "./src/operations/cache.js";
|
|
16
16
|
export { default as cachedKeyFunctions } from "./src/operations/cachedKeyFunctions.js";
|
|
17
|
+
export { default as concat } from "./src/operations/concat.js";
|
|
17
18
|
export { default as deepMerge } from "./src/operations/deepMerge.js";
|
|
18
19
|
export { default as deepReverse } from "./src/operations/deepReverse.js";
|
|
19
20
|
export { default as deepTake } from "./src/operations/deepTake.js";
|
|
@@ -33,7 +34,6 @@ export { default as reverse } from "./src/operations/reverse.js";
|
|
|
33
34
|
export { default as scope } from "./src/operations/scope.js";
|
|
34
35
|
export { default as sort } from "./src/operations/sort.js";
|
|
35
36
|
export { default as take } from "./src/operations/take.js";
|
|
36
|
-
export { default as text } from "./src/operations/text.js";
|
|
37
37
|
export * as symbols from "./src/symbols.js";
|
|
38
38
|
export * as trailingSlash from "./src/trailingSlash.js";
|
|
39
39
|
export { default as TraverseError } from "./src/TraverseError.js";
|
package/src/drivers/FileTree.js
CHANGED
|
@@ -11,6 +11,13 @@ import {
|
|
|
11
11
|
naturalOrder,
|
|
12
12
|
setParent,
|
|
13
13
|
} from "../utilities.js";
|
|
14
|
+
import limitConcurrency from "./limitConcurrency.js";
|
|
15
|
+
|
|
16
|
+
// As of July 2025, Node doesn't provide any way to limit the number of
|
|
17
|
+
// concurrent calls to readFile, so we wrap readFile in a function that
|
|
18
|
+
// arbitrarily limits the number of concurrent calls to it.
|
|
19
|
+
const MAX_CONCURRENT_READS = 256;
|
|
20
|
+
const limitReadFile = limitConcurrency(fs.readFile, MAX_CONCURRENT_READS);
|
|
14
21
|
|
|
15
22
|
/**
|
|
16
23
|
* A file system tree via the Node file system API.
|
|
@@ -81,7 +88,7 @@ export default class FileTree {
|
|
|
81
88
|
value = Reflect.construct(this.constructor, [filePath]);
|
|
82
89
|
} else {
|
|
83
90
|
// Return file contents as a standard Uint8Array
|
|
84
|
-
const buffer = await
|
|
91
|
+
const buffer = await limitReadFile(filePath);
|
|
85
92
|
value = Uint8Array.from(buffer);
|
|
86
93
|
}
|
|
87
94
|
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Wrap an async function with a function that limits the number of concurrent
|
|
3
|
+
* calls to that function.
|
|
4
|
+
*/
|
|
5
|
+
export default function limitConcurrency(fn, maxConcurrency) {
|
|
6
|
+
const queue = [];
|
|
7
|
+
const activeCallPool = new Set();
|
|
8
|
+
|
|
9
|
+
return async function limitedFunction(...args) {
|
|
10
|
+
// Our turn is represented by a promise that can be externally resolved
|
|
11
|
+
const turnWithResolvers = withResolvers();
|
|
12
|
+
|
|
13
|
+
// Construct a promise for the result of the function call
|
|
14
|
+
const resultPromise =
|
|
15
|
+
// Block until its our turn
|
|
16
|
+
turnWithResolvers.promise
|
|
17
|
+
.then(() => fn(...args)) // Call the function and return its result
|
|
18
|
+
.finally(() => {
|
|
19
|
+
// Remove the promise from the active pool
|
|
20
|
+
activeCallPool.delete(resultPromise);
|
|
21
|
+
// Tell the next call in the queue it can proceed
|
|
22
|
+
next();
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
// Join the queue
|
|
26
|
+
queue.push({
|
|
27
|
+
promise: resultPromise,
|
|
28
|
+
resolve: turnWithResolvers.resolve,
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
if (activeCallPool.size >= maxConcurrency) {
|
|
32
|
+
// The pool is full; wait for the next active call to complete. The call
|
|
33
|
+
// will remove its own completed promise from the active pool.
|
|
34
|
+
await Promise.any(activeCallPool);
|
|
35
|
+
} else {
|
|
36
|
+
next();
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
return resultPromise;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
// If there are calls in the queue and the active pool is not full, resolve
|
|
43
|
+
// the next call in the queue and add it to the active pool.
|
|
44
|
+
function next() {
|
|
45
|
+
if (queue.length > 0 && activeCallPool.size < maxConcurrency) {
|
|
46
|
+
const { promise, resolve } = queue.shift();
|
|
47
|
+
activeCallPool.add(promise);
|
|
48
|
+
resolve();
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// Polyfill Promise.withResolvers until Node LTS supports it
|
|
54
|
+
function withResolvers() {
|
|
55
|
+
let resolve;
|
|
56
|
+
let reject;
|
|
57
|
+
const promise = new Promise((res, rej) => {
|
|
58
|
+
resolve = res;
|
|
59
|
+
reject = rej;
|
|
60
|
+
});
|
|
61
|
+
return { promise, resolve, reject };
|
|
62
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { assertIsTreelike, toString } from "../utilities.js";
|
|
2
|
+
import deepValuesIterator from "./deepValuesIterator.js";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Concatenate the deep text values in a tree.
|
|
6
|
+
*
|
|
7
|
+
* @param {import("../../index.ts").Treelike} treelike
|
|
8
|
+
*/
|
|
9
|
+
export default async function concat(treelike) {
|
|
10
|
+
assertIsTreelike(treelike, "concat");
|
|
11
|
+
|
|
12
|
+
const strings = [];
|
|
13
|
+
for await (const value of deepValuesIterator(treelike, { expand: true })) {
|
|
14
|
+
let string;
|
|
15
|
+
if (value === null) {
|
|
16
|
+
string = "null";
|
|
17
|
+
} else if (value === undefined) {
|
|
18
|
+
string = "undefined";
|
|
19
|
+
} else {
|
|
20
|
+
string = toString(value);
|
|
21
|
+
}
|
|
22
|
+
if (value === null || value === undefined) {
|
|
23
|
+
const message = `Warning: Origami template encountered a ${string} value. To locate where this happened, build your project and search your build output for the text "${string}".`;
|
|
24
|
+
console.warn(message);
|
|
25
|
+
}
|
|
26
|
+
strings.push(string);
|
|
27
|
+
}
|
|
28
|
+
return strings.join("");
|
|
29
|
+
}
|
|
@@ -1,29 +1,25 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import
|
|
1
|
+
import { Tree } from "../internal.js";
|
|
2
|
+
import { toString } from "../utilities.js";
|
|
3
|
+
import concat from "./concat.js";
|
|
3
4
|
|
|
4
5
|
/**
|
|
5
|
-
*
|
|
6
|
+
* A tagged template literal function that concatenate the deep text values in a
|
|
7
|
+
* tree. Any treelike values will be concatenated using `concat`.
|
|
6
8
|
*
|
|
7
|
-
* @param {
|
|
9
|
+
* @param {TemplateStringsArray} strings
|
|
10
|
+
* @param {...any} values
|
|
8
11
|
*/
|
|
9
|
-
export default async function deepText(
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
string = toString(value);
|
|
21
|
-
}
|
|
22
|
-
if (value === null || value === undefined) {
|
|
23
|
-
const message = `Warning: a template encountered a ${string} value. To locate where this happened, build your project and search your build output for the text "${string}".`;
|
|
24
|
-
console.warn(message);
|
|
25
|
-
}
|
|
26
|
-
strings.push(string);
|
|
12
|
+
export default async function deepText(strings, ...values) {
|
|
13
|
+
// Convert all the values to strings
|
|
14
|
+
const valueTexts = await Promise.all(
|
|
15
|
+
values.map((value) =>
|
|
16
|
+
Tree.isTreelike(value) ? concat(value) : toString(value)
|
|
17
|
+
)
|
|
18
|
+
);
|
|
19
|
+
// Splice all the strings together
|
|
20
|
+
let result = strings[0];
|
|
21
|
+
for (let i = 0; i < valueTexts.length; i++) {
|
|
22
|
+
result += valueTexts[i] + strings[i + 1];
|
|
27
23
|
}
|
|
28
|
-
return
|
|
24
|
+
return result;
|
|
29
25
|
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import assert from "node:assert";
|
|
2
|
+
import { before, describe, test } from "node:test";
|
|
3
|
+
import limitConcurrency from "../../src/drivers/limitConcurrency.js";
|
|
4
|
+
|
|
5
|
+
describe("limitConcurrency", async () => {
|
|
6
|
+
before(async () => {
|
|
7
|
+
// Confirm our limited functions throws on too many calls
|
|
8
|
+
const fn = createFixture();
|
|
9
|
+
try {
|
|
10
|
+
const array = Array.from({ length: 10 }, (_, index) => index);
|
|
11
|
+
await Promise.all(array.map((index) => fn(index)));
|
|
12
|
+
} catch (/** @type {any} */ error) {
|
|
13
|
+
assert.equal(error.message, "Too many calls");
|
|
14
|
+
}
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
test("limits the number of concurrent calls", async () => {
|
|
18
|
+
const fn = createFixture();
|
|
19
|
+
const limitedFn = limitConcurrency(fn, 3);
|
|
20
|
+
const array = Array.from({ length: 10 }, (_, index) => index);
|
|
21
|
+
const result = await Promise.all(array.map((index) => limitedFn(index)));
|
|
22
|
+
assert.deepEqual(result, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
|
|
23
|
+
});
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
// Return a function that only permits a limited number of concurrent calls and
|
|
27
|
+
// simulates a delay for each request.
|
|
28
|
+
function createFixture() {
|
|
29
|
+
let activeCalls = 0;
|
|
30
|
+
const maxActiveCalls = 3;
|
|
31
|
+
|
|
32
|
+
return async function (n) {
|
|
33
|
+
if (activeCalls >= maxActiveCalls) {
|
|
34
|
+
throw new Error("Too many calls");
|
|
35
|
+
}
|
|
36
|
+
activeCalls++;
|
|
37
|
+
await new Promise((resolve) => setTimeout(resolve, 10));
|
|
38
|
+
activeCalls--;
|
|
39
|
+
return n;
|
|
40
|
+
};
|
|
41
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import assert from "node:assert";
|
|
2
|
+
import { describe, test } from "node:test";
|
|
3
|
+
import FunctionTree from "../../src/drivers/FunctionTree.js";
|
|
4
|
+
import { Tree } from "../../src/internal.js";
|
|
5
|
+
import concat from "../../src/operations/concat.js";
|
|
6
|
+
|
|
7
|
+
describe("concat", () => {
|
|
8
|
+
test("concatenates deep tree values", async () => {
|
|
9
|
+
const tree = Tree.from({
|
|
10
|
+
a: "A",
|
|
11
|
+
b: "B",
|
|
12
|
+
c: "C",
|
|
13
|
+
more: {
|
|
14
|
+
d: "D",
|
|
15
|
+
e: "E",
|
|
16
|
+
},
|
|
17
|
+
});
|
|
18
|
+
const result = await concat.call(null, tree);
|
|
19
|
+
assert.equal(result, "ABCDE");
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
test("concatenates deep tree-like values", async () => {
|
|
23
|
+
const letters = ["a", "b", "c"];
|
|
24
|
+
const specimens = new FunctionTree(
|
|
25
|
+
(letter) => ({
|
|
26
|
+
lowercase: letter,
|
|
27
|
+
uppercase: letter.toUpperCase(),
|
|
28
|
+
}),
|
|
29
|
+
letters
|
|
30
|
+
);
|
|
31
|
+
const result = await concat.call(null, specimens);
|
|
32
|
+
assert.equal(result, "aAbBcC");
|
|
33
|
+
});
|
|
34
|
+
});
|
|
@@ -1,34 +1,12 @@
|
|
|
1
1
|
import assert from "node:assert";
|
|
2
2
|
import { describe, test } from "node:test";
|
|
3
|
-
import FunctionTree from "../../src/drivers/FunctionTree.js";
|
|
4
|
-
import { Tree } from "../../src/internal.js";
|
|
5
3
|
import deepText from "../../src/operations/deepText.js";
|
|
6
4
|
|
|
7
5
|
describe("deepText", () => {
|
|
8
|
-
test("
|
|
9
|
-
const
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
more: {
|
|
14
|
-
d: "D",
|
|
15
|
-
e: "E",
|
|
16
|
-
},
|
|
17
|
-
});
|
|
18
|
-
const result = await deepText(tree);
|
|
19
|
-
assert.equal(result, "ABCDE");
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
test("concatenates deep tree-like values", async () => {
|
|
23
|
-
const letters = ["a", "b", "c"];
|
|
24
|
-
const specimens = new FunctionTree(
|
|
25
|
-
(letter) => ({
|
|
26
|
-
lowercase: letter,
|
|
27
|
-
uppercase: letter.toUpperCase(),
|
|
28
|
-
}),
|
|
29
|
-
letters
|
|
30
|
-
);
|
|
31
|
-
const result = await deepText(specimens);
|
|
32
|
-
assert.equal(result, "aAbBcC");
|
|
6
|
+
test("joins strings and values together", async () => {
|
|
7
|
+
const array = [1, 2, 3];
|
|
8
|
+
const object = { person1: "Alice", person2: "Bob" };
|
|
9
|
+
const result = await deepText`a ${array} b ${object} c`;
|
|
10
|
+
assert.equal(result, "a 123 b AliceBob c");
|
|
33
11
|
});
|
|
34
12
|
});
|
package/src/operations/text.js
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import { Tree } from "../internal.js";
|
|
2
|
-
import { toString } from "../utilities.js";
|
|
3
|
-
import deepText from "./deepText.js";
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* A tagged template literal function that concatenate the deep text values in a
|
|
7
|
-
* tree. Any treelike values will be concatenated using `deepText`.
|
|
8
|
-
*
|
|
9
|
-
* @param {TemplateStringsArray} strings
|
|
10
|
-
* @param {...any} values
|
|
11
|
-
*/
|
|
12
|
-
export default async function text(strings, ...values) {
|
|
13
|
-
// Convert all the values to strings
|
|
14
|
-
const valueTexts = await Promise.all(
|
|
15
|
-
values.map((value) =>
|
|
16
|
-
Tree.isTreelike(value) ? deepText(value) : toString(value)
|
|
17
|
-
)
|
|
18
|
-
);
|
|
19
|
-
// Splice all the strings together
|
|
20
|
-
let result = strings[0];
|
|
21
|
-
for (let i = 0; i < valueTexts.length; i++) {
|
|
22
|
-
result += valueTexts[i] + strings[i + 1];
|
|
23
|
-
}
|
|
24
|
-
return result;
|
|
25
|
-
}
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import assert from "node:assert";
|
|
2
|
-
import { describe, test } from "node:test";
|
|
3
|
-
import text from "../../src/operations/text.js";
|
|
4
|
-
|
|
5
|
-
describe("text template literal function", () => {
|
|
6
|
-
test("joins strings and values together", async () => {
|
|
7
|
-
const array = [1, 2, 3];
|
|
8
|
-
const object = { person1: "Alice", person2: "Bob" };
|
|
9
|
-
const result = await text`a ${array} b ${object} c`;
|
|
10
|
-
assert.equal(result, "a 123 b AliceBob c");
|
|
11
|
-
});
|
|
12
|
-
});
|