@orpc/shared 2.0.0-beta.49 → 2.0.0-beta.50
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/dist/args.d.ts +1 -1
- package/dist/args.js +4 -2
- package/dist/array.d.ts +4 -1
- package/dist/array.js +11 -5
- package/dist/bigint.d.ts +1 -1
- package/dist/bigint.js +12 -16
- package/dist/buffer.d.ts +4 -4
- package/dist/buffer.js +44 -67
- package/dist/compare.d.ts +1 -1
- package/dist/compare.js +23 -42
- package/dist/consts.d.ts +1 -1
- package/dist/consts.js +3 -1
- package/dist/error.d.ts +1 -1
- package/dist/error.js +4 -2
- package/dist/function.d.ts +1 -1
- package/dist/function.js +27 -32
- package/dist/http.d.ts +29 -0
- package/dist/http.js +128 -94
- package/dist/id.d.ts +1 -1
- package/dist/id.js +11 -11
- package/dist/index.d.ts +26 -34
- package/dist/index.js +23 -32
- package/dist/interceptor.d.ts +17 -17
- package/dist/interceptor.js +106 -126
- package/dist/iterator.d.ts +41 -41
- package/dist/iterator.js +136 -152
- package/dist/object.d.ts +10 -8
- package/dist/object.js +176 -227
- package/dist/plugin.d.ts +7 -7
- package/dist/plugin.js +41 -57
- package/dist/promise.d.ts +4 -4
- package/dist/promise.js +28 -28
- package/dist/proxy.d.ts +2 -2
- package/dist/proxy.js +23 -22
- package/dist/queue.d.ts +13 -13
- package/dist/queue.js +64 -79
- package/dist/regex.d.ts +9 -0
- package/dist/regex.js +11 -0
- package/dist/signal.d.ts +7 -2
- package/dist/signal.js +83 -80
- package/dist/stream.d.ts +23 -23
- package/dist/stream.js +118 -144
- package/dist/tracing.d.ts +50 -50
- package/dist/tracing.js +60 -85
- package/dist/types.d.ts +7 -10
- package/dist/value.d.ts +1 -1
- package/dist/value.js +5 -5
- package/package.json +2 -2
- package/dist/types.js +0 -1
package/dist/object.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { AnyFunction } from "./function.js";
|
|
2
2
|
export type Segment = string | number;
|
|
3
3
|
export declare function findDeepMatches(check: (value: unknown) => boolean, payload: unknown, segments?: Segment[], maps?: Segment[][], values?: unknown[]): {
|
|
4
|
-
|
|
5
|
-
|
|
4
|
+
maps: Segment[][];
|
|
5
|
+
values: unknown[];
|
|
6
6
|
};
|
|
7
7
|
/**
|
|
8
8
|
* Get constructor of the value
|
|
@@ -15,6 +15,10 @@ export declare function getConstructors(value: unknown): Generator<Function>;
|
|
|
15
15
|
* `Object.create(null)`.
|
|
16
16
|
*/
|
|
17
17
|
export declare function isPlainObject(value: unknown): value is Record<PropertyKey, unknown>;
|
|
18
|
+
/**
|
|
19
|
+
* Checks whether a value is an array or a plain object, i.e. something whose keys can be walked.
|
|
20
|
+
*/
|
|
21
|
+
export declare function isPlainObjectOrArray(value: unknown): value is unknown[] | Record<PropertyKey, unknown>;
|
|
18
22
|
/**
|
|
19
23
|
* Returns `object[key]` only when it is an own property, so keys like
|
|
20
24
|
* `toString` do not resolve through the prototype chain.
|
|
@@ -44,7 +48,7 @@ export declare function clone<T>(value: T): T;
|
|
|
44
48
|
export declare function copyOnWrite(parent: object, key: PropertyKey, original: unknown): unknown;
|
|
45
49
|
export declare function isPropertyKey(value: unknown): value is PropertyKey;
|
|
46
50
|
export declare const NullProtoObj: {
|
|
47
|
-
|
|
51
|
+
new <T extends Record<PropertyKey, unknown>>(): T;
|
|
48
52
|
};
|
|
49
53
|
/**
|
|
50
54
|
* Returns an object containing all methods of the given object, with each
|
|
@@ -54,7 +58,5 @@ export declare const NullProtoObj: {
|
|
|
54
58
|
* (excluding `Object.prototype` and the `constructor` property).
|
|
55
59
|
*/
|
|
56
60
|
export declare function bindMethods<T extends object>(obj: T, options?: {
|
|
57
|
-
|
|
58
|
-
}): Pick<T, {
|
|
59
|
-
[K in keyof T]: T[K] extends AnyFunction ? K : never;
|
|
60
|
-
}[keyof T]>;
|
|
61
|
+
unbound?: (keyof T)[];
|
|
62
|
+
}): Pick<T, { [K in keyof T]: T[K] extends AnyFunction ? K : never; }[keyof T]>;
|
package/dist/object.js
CHANGED
|
@@ -1,245 +1,194 @@
|
|
|
1
|
-
import { getOrBind, isNonPrimitive } from
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
return { maps, values };
|
|
1
|
+
import { getOrBind, isNonPrimitive } from "@standard-server/shared";
|
|
2
|
+
// src/object.ts
|
|
3
|
+
function findDeepMatches(check, payload, segments = [], maps = [], values = []) {
|
|
4
|
+
if (check(payload)) {
|
|
5
|
+
maps.push(segments);
|
|
6
|
+
values.push(payload);
|
|
7
|
+
} else if (Array.isArray(payload)) payload.forEach((v, i) => {
|
|
8
|
+
findDeepMatches(check, v, [...segments, i], maps, values);
|
|
9
|
+
});
|
|
10
|
+
else if (isPlainObject(payload)) for (const key of Object.keys(payload)) findDeepMatches(check, payload[key], [...segments, key], maps, values);
|
|
11
|
+
return {
|
|
12
|
+
maps,
|
|
13
|
+
values
|
|
14
|
+
};
|
|
18
15
|
}
|
|
19
16
|
/**
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
let proto = Object.getPrototypeOf(value);
|
|
35
|
-
while (proto !== null) {
|
|
36
|
-
if (proto.constructor) {
|
|
37
|
-
yield proto.constructor;
|
|
38
|
-
}
|
|
39
|
-
proto = Object.getPrototypeOf(proto);
|
|
40
|
-
}
|
|
17
|
+
* Get constructor of the value
|
|
18
|
+
*
|
|
19
|
+
*/
|
|
20
|
+
function getConstructor(value) {
|
|
21
|
+
if (!isNonPrimitive(value)) return null;
|
|
22
|
+
return Object.getPrototypeOf(value)?.constructor;
|
|
23
|
+
}
|
|
24
|
+
function* getConstructors(value) {
|
|
25
|
+
if (!isNonPrimitive(value)) return;
|
|
26
|
+
let proto = Object.getPrototypeOf(value);
|
|
27
|
+
while (proto !== null) {
|
|
28
|
+
if (proto.constructor) yield proto.constructor;
|
|
29
|
+
proto = Object.getPrototypeOf(proto);
|
|
30
|
+
}
|
|
41
31
|
}
|
|
42
32
|
/**
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
const proto = Object.getPrototypeOf(value);
|
|
51
|
-
return proto === Object.prototype || !proto || !proto.constructor;
|
|
33
|
+
* Checks whether a value is a plain object, including objects created with
|
|
34
|
+
* `Object.create(null)`.
|
|
35
|
+
*/
|
|
36
|
+
function isPlainObject(value) {
|
|
37
|
+
if (!value || typeof value !== "object") return false;
|
|
38
|
+
const proto = Object.getPrototypeOf(value);
|
|
39
|
+
return proto === Object.prototype || !proto || !proto.constructor;
|
|
52
40
|
}
|
|
53
41
|
/**
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
return Object.hasOwn(object, key) ? object[key] : undefined;
|
|
59
|
-
}
|
|
60
|
-
export function get(object, path) {
|
|
61
|
-
let current = object;
|
|
62
|
-
for (const key of path) {
|
|
63
|
-
if (!isNonPrimitive(current)) {
|
|
64
|
-
return undefined;
|
|
65
|
-
}
|
|
66
|
-
current = getOwn(current, key);
|
|
67
|
-
}
|
|
68
|
-
return current;
|
|
42
|
+
* Checks whether a value is an array or a plain object, i.e. something whose keys can be walked.
|
|
43
|
+
*/
|
|
44
|
+
function isPlainObjectOrArray(value) {
|
|
45
|
+
return Array.isArray(value) || isPlainObject(value);
|
|
69
46
|
}
|
|
70
47
|
/**
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
current = next;
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
setOwn(current, path.at(-1), value);
|
|
48
|
+
* Returns `object[key]` only when it is an own property, so keys like
|
|
49
|
+
* `toString` do not resolve through the prototype chain.
|
|
50
|
+
*/
|
|
51
|
+
function getOwn(object, key) {
|
|
52
|
+
return Object.hasOwn(object, key) ? object[key] : void 0;
|
|
53
|
+
}
|
|
54
|
+
function get(object, path) {
|
|
55
|
+
let current = object;
|
|
56
|
+
for (const key of path) {
|
|
57
|
+
if (!isNonPrimitive(current)) return;
|
|
58
|
+
current = getOwn(current, key);
|
|
59
|
+
}
|
|
60
|
+
return current;
|
|
88
61
|
}
|
|
89
62
|
/**
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
}
|
|
63
|
+
* Sets a value at the given path, creating plain objects for intermediate keys as needed.
|
|
64
|
+
*/
|
|
65
|
+
function set(root, path, value) {
|
|
66
|
+
let current = root;
|
|
67
|
+
for (let i = 0; i < path.length - 1; i++) {
|
|
68
|
+
const key = path[i];
|
|
69
|
+
const next = getOwn(current, key);
|
|
70
|
+
if (!isNonPrimitive(next)) {
|
|
71
|
+
const child = {};
|
|
72
|
+
setOwn(current, key, child);
|
|
73
|
+
current = child;
|
|
74
|
+
} else current = next;
|
|
75
|
+
}
|
|
76
|
+
setOwn(current, path.at(-1), value);
|
|
105
77
|
}
|
|
106
78
|
/**
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
if (!Object.hasOwn(first, key)) {
|
|
118
|
-
continue;
|
|
119
|
-
}
|
|
120
|
-
const firstValue = first[key];
|
|
121
|
-
const secondValue = second[key];
|
|
122
|
-
if (isPlainObject(firstValue) && isPlainObject(secondValue)) {
|
|
123
|
-
result[key] = { ...firstValue, ...secondValue };
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
return result;
|
|
127
|
-
}
|
|
128
|
-
export function omit(obj, keys) {
|
|
129
|
-
const result = { ...obj };
|
|
130
|
-
for (const key of keys) {
|
|
131
|
-
delete result[key];
|
|
132
|
-
}
|
|
133
|
-
return result;
|
|
134
|
-
}
|
|
135
|
-
export function pick(obj, keys) {
|
|
136
|
-
const result = {};
|
|
137
|
-
for (const key of keys) {
|
|
138
|
-
if (Object.hasOwn(obj, key)) {
|
|
139
|
-
result[key] = obj[key];
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
return result;
|
|
79
|
+
* Sets `object[key]`, defining `__proto__` as an own property instead of re-parenting the object.
|
|
80
|
+
*/
|
|
81
|
+
function setOwn(object, key, value) {
|
|
82
|
+
if (key === "__proto__") Object.defineProperty(object, key, {
|
|
83
|
+
value,
|
|
84
|
+
writable: true,
|
|
85
|
+
enumerable: true,
|
|
86
|
+
configurable: true
|
|
87
|
+
});
|
|
88
|
+
else object[key] = value;
|
|
143
89
|
}
|
|
144
90
|
/**
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
91
|
+
* Merges two values by copying the second over the first, repeating the merge one level deeper for
|
|
92
|
+
* nested plain objects. Anything that is not a pair of plain objects is replaced by the second one.
|
|
93
|
+
*/
|
|
94
|
+
function mergeTwoLevels(first, second) {
|
|
95
|
+
if (!isPlainObject(first) || !isPlainObject(second)) return second;
|
|
96
|
+
const result = {
|
|
97
|
+
...first,
|
|
98
|
+
...second
|
|
99
|
+
};
|
|
100
|
+
for (const key of Object.keys(second)) {
|
|
101
|
+
if (!Object.hasOwn(first, key)) continue;
|
|
102
|
+
const firstValue = first[key];
|
|
103
|
+
const secondValue = second[key];
|
|
104
|
+
if (isPlainObject(firstValue) && isPlainObject(secondValue)) result[key] = {
|
|
105
|
+
...firstValue,
|
|
106
|
+
...secondValue
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
return result;
|
|
110
|
+
}
|
|
111
|
+
function omit(obj, keys) {
|
|
112
|
+
const result = { ...obj };
|
|
113
|
+
for (const key of keys) delete result[key];
|
|
114
|
+
return result;
|
|
115
|
+
}
|
|
116
|
+
function pick(obj, keys) {
|
|
117
|
+
const result = {};
|
|
118
|
+
for (const key of keys) if (Object.hasOwn(obj, key)) result[key] = obj[key];
|
|
119
|
+
return result;
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Deep clones arrays and plain objects, leaving every other value as is.
|
|
123
|
+
* Circular and shared references are preserved in the copy.
|
|
124
|
+
*/
|
|
125
|
+
function clone(value) {
|
|
126
|
+
return cloneWithVisited(value, /* @__PURE__ */ new WeakMap());
|
|
150
127
|
}
|
|
151
128
|
function cloneWithVisited(value, visited) {
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
}
|
|
191
|
-
else if (isPlainObject(value)) {
|
|
192
|
-
copy = { ...value };
|
|
193
|
-
}
|
|
194
|
-
else {
|
|
195
|
-
return value;
|
|
196
|
-
}
|
|
197
|
-
setOwn(parent, key, copy);
|
|
198
|
-
return copy;
|
|
199
|
-
}
|
|
200
|
-
export function isPropertyKey(value) {
|
|
201
|
-
const type = typeof value;
|
|
202
|
-
return type === 'string' || type === 'number' || type === 'symbol';
|
|
203
|
-
}
|
|
204
|
-
export const NullProtoObj = /* @__PURE__ */ (() => {
|
|
205
|
-
// oxlint-disable-next-line unicorn/consistent-function-scoping
|
|
206
|
-
const e = function () { };
|
|
207
|
-
e.prototype = Object.create(null);
|
|
208
|
-
Object.freeze(e.prototype);
|
|
209
|
-
return e;
|
|
129
|
+
if (Array.isArray(value)) {
|
|
130
|
+
const existing = visited.get(value);
|
|
131
|
+
if (existing) return existing;
|
|
132
|
+
const result = [];
|
|
133
|
+
visited.set(value, result);
|
|
134
|
+
for (const item of value) result.push(cloneWithVisited(item, visited));
|
|
135
|
+
return result;
|
|
136
|
+
}
|
|
137
|
+
if (isPlainObject(value)) {
|
|
138
|
+
const existing = visited.get(value);
|
|
139
|
+
if (existing) return existing;
|
|
140
|
+
const result = {};
|
|
141
|
+
visited.set(value, result);
|
|
142
|
+
for (const key of Object.keys(value)) setOwn(result, key, cloneWithVisited(value[key], visited));
|
|
143
|
+
for (const sym of Object.getOwnPropertySymbols(value)) setOwn(result, sym, cloneWithVisited(value[sym], visited));
|
|
144
|
+
return result;
|
|
145
|
+
}
|
|
146
|
+
return value;
|
|
147
|
+
}
|
|
148
|
+
function copyOnWrite(parent, key, original) {
|
|
149
|
+
const value = parent[key];
|
|
150
|
+
if (value !== original) return value;
|
|
151
|
+
let copy;
|
|
152
|
+
if (Array.isArray(value)) copy = value.slice();
|
|
153
|
+
else if (isPlainObject(value)) copy = { ...value };
|
|
154
|
+
else return value;
|
|
155
|
+
setOwn(parent, key, copy);
|
|
156
|
+
return copy;
|
|
157
|
+
}
|
|
158
|
+
function isPropertyKey(value) {
|
|
159
|
+
const type = typeof value;
|
|
160
|
+
return type === "string" || type === "number" || type === "symbol";
|
|
161
|
+
}
|
|
162
|
+
const NullProtoObj = /* @__PURE__ */ (() => {
|
|
163
|
+
const e = function() {};
|
|
164
|
+
e.prototype = Object.create(null);
|
|
165
|
+
Object.freeze(e.prototype);
|
|
166
|
+
return e;
|
|
210
167
|
})();
|
|
211
168
|
/**
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
if (typeof val === 'function') {
|
|
239
|
-
methods[sym] = val;
|
|
240
|
-
}
|
|
241
|
-
}
|
|
242
|
-
current = Object.getPrototypeOf(current);
|
|
243
|
-
}
|
|
244
|
-
return methods;
|
|
245
|
-
}
|
|
169
|
+
* Returns an object containing all methods of the given object, with each
|
|
170
|
+
* method bound to the original object instance.
|
|
171
|
+
*
|
|
172
|
+
* Methods are collected from both the object itself and its prototype chain
|
|
173
|
+
* (excluding `Object.prototype` and the `constructor` property).
|
|
174
|
+
*/
|
|
175
|
+
function bindMethods(obj, options = {}) {
|
|
176
|
+
const unbound = new Set(options.unbound);
|
|
177
|
+
const methods = new NullProtoObj();
|
|
178
|
+
let current = obj;
|
|
179
|
+
while (current && current !== Object.prototype) {
|
|
180
|
+
for (const key of Object.getOwnPropertyNames(current)) {
|
|
181
|
+
if (key === "constructor" || key in methods) continue;
|
|
182
|
+
const val = getOrBind(obj, key, { bind: !unbound.has(key) });
|
|
183
|
+
if (typeof val === "function") methods[key] = val;
|
|
184
|
+
}
|
|
185
|
+
for (const sym of Object.getOwnPropertySymbols(current)) {
|
|
186
|
+
if (sym in methods) continue;
|
|
187
|
+
const val = getOrBind(obj, sym, { bind: !unbound.has(sym) });
|
|
188
|
+
if (typeof val === "function") methods[sym] = val;
|
|
189
|
+
}
|
|
190
|
+
current = Object.getPrototypeOf(current);
|
|
191
|
+
}
|
|
192
|
+
return methods;
|
|
193
|
+
}
|
|
194
|
+
export { NullProtoObj, bindMethods, clone, copyOnWrite, findDeepMatches, get, getConstructor, getConstructors, getOwn, isPlainObject, isPlainObjectOrArray, isPropertyKey, mergeTwoLevels, omit, pick, set, setOwn };
|
package/dist/plugin.d.ts
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
export interface OrderablePlugin {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
2
|
+
/** Unique name of the plugin, used for ordering and identification. */
|
|
3
|
+
name: string;
|
|
4
|
+
/** Plugins this plugin should execute before. */
|
|
5
|
+
before?: string[] | undefined;
|
|
6
|
+
/** Plugins this plugin should execute after. */
|
|
7
|
+
after?: string[] | undefined;
|
|
8
8
|
}
|
|
9
9
|
/**
|
|
10
10
|
* Sorts plugins based on their `before` and `after` dependencies.
|
|
11
11
|
*/
|
|
12
|
-
export declare function sortPlugins<T extends OrderablePlugin>(plugins: T[]): T[];
|
|
12
|
+
export declare function sortPlugins<T extends OrderablePlugin>(plugins: T[]): T[];
|
package/dist/plugin.js
CHANGED
|
@@ -1,61 +1,45 @@
|
|
|
1
|
+
// src/plugin.ts
|
|
1
2
|
/**
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
for (const afterIndex of afterIndices) {
|
|
35
|
-
dependencies[i].push(afterIndex);
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
const sorted = [];
|
|
42
|
-
const placed = new Set();
|
|
43
|
-
while (sorted.length < pluginCount) {
|
|
44
|
-
const next = plugins.findIndex((_, i) => !placed.has(i) && dependencies[i].every((dependency) => placed.has(dependency)));
|
|
45
|
-
if (next === -1) {
|
|
46
|
-
throw new Error(`Circular dependency detected involving plugin "${findCyclicPlugin(plugins, dependencies, placed).name}"`);
|
|
47
|
-
}
|
|
48
|
-
placed.add(next);
|
|
49
|
-
sorted.push(plugins[next]);
|
|
50
|
-
}
|
|
51
|
-
return sorted;
|
|
3
|
+
* Sorts plugins based on their `before` and `after` dependencies.
|
|
4
|
+
*/
|
|
5
|
+
function sortPlugins(plugins) {
|
|
6
|
+
const pluginCount = plugins.length;
|
|
7
|
+
const pluginIdToIndices = /* @__PURE__ */ new Map();
|
|
8
|
+
for (let i = 0; i < pluginCount; i++) {
|
|
9
|
+
const plugin = plugins[i];
|
|
10
|
+
const indices = pluginIdToIndices.get(plugin.name);
|
|
11
|
+
if (indices === void 0) pluginIdToIndices.set(plugin.name, [i]);
|
|
12
|
+
else indices.push(i);
|
|
13
|
+
}
|
|
14
|
+
const dependencies = Array.from({ length: pluginCount }, () => []);
|
|
15
|
+
for (let i = 0; i < pluginCount; i++) {
|
|
16
|
+
const plugin = plugins[i];
|
|
17
|
+
if (plugin.before !== void 0) for (const beforeId of plugin.before) {
|
|
18
|
+
const beforeIndices = pluginIdToIndices.get(beforeId);
|
|
19
|
+
if (beforeIndices !== void 0) for (const beforeIndex of beforeIndices) dependencies[beforeIndex].push(i);
|
|
20
|
+
}
|
|
21
|
+
if (plugin.after !== void 0) for (const afterId of plugin.after) {
|
|
22
|
+
const afterIndices = pluginIdToIndices.get(afterId);
|
|
23
|
+
if (afterIndices !== void 0) for (const afterIndex of afterIndices) dependencies[i].push(afterIndex);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
const sorted = [];
|
|
27
|
+
const placed = /* @__PURE__ */ new Set();
|
|
28
|
+
while (sorted.length < pluginCount) {
|
|
29
|
+
const next = plugins.findIndex((_, i) => !placed.has(i) && dependencies[i].every((dependency) => placed.has(dependency)));
|
|
30
|
+
if (next === -1) throw new Error(`Circular dependency detected involving plugin "${findCyclicPlugin(plugins, dependencies, placed).name}"`);
|
|
31
|
+
placed.add(next);
|
|
32
|
+
sorted.push(plugins[next]);
|
|
33
|
+
}
|
|
34
|
+
return sorted;
|
|
52
35
|
}
|
|
53
36
|
function findCyclicPlugin(plugins, dependencies, placed) {
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
37
|
+
const seen = /* @__PURE__ */ new Set();
|
|
38
|
+
let current = plugins.findIndex((_, i) => !placed.has(i));
|
|
39
|
+
while (!seen.has(current)) {
|
|
40
|
+
seen.add(current);
|
|
41
|
+
current = dependencies[current].find((dependency) => !placed.has(dependency));
|
|
42
|
+
}
|
|
43
|
+
return plugins[current];
|
|
61
44
|
}
|
|
45
|
+
export { sortPlugins };
|
package/dist/promise.d.ts
CHANGED
|
@@ -6,12 +6,12 @@
|
|
|
6
6
|
* where that API is not yet available.
|
|
7
7
|
*/
|
|
8
8
|
export declare function promiseWithResolvers<T>(): {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
9
|
+
promise: Promise<T>;
|
|
10
|
+
resolve: (v: T) => void;
|
|
11
|
+
reject: (reason: unknown) => void;
|
|
12
12
|
};
|
|
13
13
|
/**
|
|
14
14
|
* Captures the current async context (such as AsyncLocalStorage) through a promise reaction, so no runtime API is needed.
|
|
15
15
|
* The returned function runs a callback inside it once; later calls reject.
|
|
16
16
|
*/
|
|
17
|
-
export declare function captureAsyncContext(): <T>(callback: () => Promise<T>) => Promise<T>;
|
|
17
|
+
export declare function captureAsyncContext(): <T>(callback: () => Promise<T>) => Promise<T>;
|