@orpc/shared 2.0.0-beta.48 → 2.0.0-beta.49
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/README.md +4 -4
- package/dist/args.js +3 -0
- package/dist/array.js +7 -0
- package/dist/bigint.d.ts +9 -0
- package/dist/bigint.js +19 -0
- package/dist/buffer.d.ts +18 -0
- package/dist/buffer.js +77 -0
- package/dist/compare.js +45 -0
- package/dist/consts.js +1 -0
- package/dist/error.js +3 -0
- package/dist/{function.d.mts → function.d.ts} +5 -5
- package/dist/function.js +38 -0
- package/dist/{http.d.mts → http.d.ts} +16 -16
- package/dist/http.js +113 -0
- package/dist/id.d.ts +8 -0
- package/dist/id.js +13 -0
- package/dist/index.d.ts +34 -0
- package/dist/index.js +32 -0
- package/dist/interceptor.d.ts +60 -0
- package/dist/interceptor.js +139 -0
- package/dist/iterator.d.ts +56 -0
- package/dist/iterator.js +157 -0
- package/dist/object.d.ts +60 -0
- package/dist/object.js +245 -0
- package/dist/plugin.d.ts +12 -0
- package/dist/plugin.js +61 -0
- package/dist/promise.d.ts +17 -0
- package/dist/promise.js +32 -0
- package/dist/proxy.d.ts +12 -0
- package/dist/proxy.js +24 -0
- package/dist/queue.d.ts +16 -0
- package/dist/queue.js +79 -0
- package/dist/signal.d.ts +18 -0
- package/dist/signal.js +85 -0
- package/dist/stream.d.ts +48 -0
- package/dist/stream.js +155 -0
- package/dist/tracing.d.ts +73 -0
- package/dist/tracing.js +94 -0
- package/dist/types.d.ts +26 -0
- package/dist/value.d.ts +2 -0
- package/dist/value.js +6 -0
- package/package.json +16 -18
- package/dist/args.mjs +0 -3
- package/dist/array.mjs +0 -7
- package/dist/bigint.d.mts +0 -9
- package/dist/bigint.mjs +0 -12
- package/dist/buffer.d.mts +0 -18
- package/dist/buffer.mjs +0 -66
- package/dist/compare.mjs +0 -43
- package/dist/consts.mjs +0 -1
- package/dist/error.mjs +0 -3
- package/dist/function.mjs +0 -31
- package/dist/http.mjs +0 -83
- package/dist/id.d.mts +0 -8
- package/dist/id.mjs +0 -7
- package/dist/index.d.mts +0 -25
- package/dist/index.mjs +0 -23
- package/dist/interceptor.d.mts +0 -96
- package/dist/interceptor.mjs +0 -102
- package/dist/iterator.d.mts +0 -82
- package/dist/iterator.mjs +0 -151
- package/dist/object.d.mts +0 -64
- package/dist/object.mjs +0 -221
- package/dist/plugin.d.mts +0 -12
- package/dist/plugin.mjs +0 -58
- package/dist/promise.d.mts +0 -17
- package/dist/promise.mjs +0 -23
- package/dist/proxy.d.mts +0 -15
- package/dist/proxy.mjs +0 -15
- package/dist/queue.d.mts +0 -16
- package/dist/queue.mjs +0 -74
- package/dist/signal.d.mts +0 -13
- package/dist/signal.mjs +0 -74
- package/dist/stream.d.mts +0 -54
- package/dist/stream.mjs +0 -138
- package/dist/tracing.d.mts +0 -73
- package/dist/tracing.mjs +0 -89
- package/dist/types.d.mts +0 -32
- package/dist/value.d.mts +0 -8
- package/dist/value.mjs +0 -6
- /package/dist/{args.d.mts → args.d.ts} +0 -0
- /package/dist/{array.d.mts → array.d.ts} +0 -0
- /package/dist/{compare.d.mts → compare.d.ts} +0 -0
- /package/dist/{consts.d.mts → consts.d.ts} +0 -0
- /package/dist/{error.d.mts → error.d.ts} +0 -0
- /package/dist/{types.mjs → types.js} +0 -0
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import { isAsyncIteratorObject } from '@standard-server/shared';
|
|
2
|
+
import { wrapAsyncIterator } from './iterator.js';
|
|
3
|
+
import { override } from './proxy.js';
|
|
4
|
+
import { wrapReadableStream } from './stream.js';
|
|
5
|
+
/**
|
|
6
|
+
* Can used for interceptors or middlewares
|
|
7
|
+
*/
|
|
8
|
+
// oxlint-disable-next-line typescript/no-unnecessary-type-parameters
|
|
9
|
+
export function onStart(callback) {
|
|
10
|
+
return async (options, ...rest) => {
|
|
11
|
+
await callback(options, ...rest);
|
|
12
|
+
return await options.next();
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Can used for interceptors or middlewares
|
|
17
|
+
*/
|
|
18
|
+
// oxlint-disable-next-line typescript/no-unnecessary-type-parameters
|
|
19
|
+
export function onSuccess(callback) {
|
|
20
|
+
return async (options, ...rest) => {
|
|
21
|
+
const result = await options.next();
|
|
22
|
+
await callback(result, options, ...rest);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Creates an interceptor or middleware that invokes a callback whenever an error
|
|
28
|
+
* is thrown. The error is rethrown after the callback completes.
|
|
29
|
+
*
|
|
30
|
+
* @see {@link https://orpc.dev/docs/adapters/fetch-api | Fetch API Adapter}
|
|
31
|
+
*/
|
|
32
|
+
// oxlint-disable-next-line typescript/no-unnecessary-type-parameters
|
|
33
|
+
export function onError(callback) {
|
|
34
|
+
return async (options, ...rest) => {
|
|
35
|
+
try {
|
|
36
|
+
return await options.next();
|
|
37
|
+
}
|
|
38
|
+
catch (error) {
|
|
39
|
+
await callback(error, options, ...rest);
|
|
40
|
+
throw error;
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Can used for interceptors or middlewares
|
|
46
|
+
*/
|
|
47
|
+
// oxlint-disable-next-line typescript/no-unnecessary-type-parameters
|
|
48
|
+
export function onFinish(callback) {
|
|
49
|
+
return async (options, ...rest) => {
|
|
50
|
+
let state;
|
|
51
|
+
try {
|
|
52
|
+
const result = await options.next();
|
|
53
|
+
state = [null, result, true];
|
|
54
|
+
return result;
|
|
55
|
+
}
|
|
56
|
+
catch (error) {
|
|
57
|
+
state = [error, undefined, false];
|
|
58
|
+
throw error;
|
|
59
|
+
}
|
|
60
|
+
finally {
|
|
61
|
+
await callback(state, options, ...rest);
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Creates a middleware or interceptor that invokes a callback when the returned async
|
|
67
|
+
* iterator object throws an error while being consumed.
|
|
68
|
+
*
|
|
69
|
+
* This does not replace the `onError`. `onError` only fires on the
|
|
70
|
+
* initial call (before the interceptor returns the iterator), whereas this
|
|
71
|
+
* callback only fires while consuming the iterator. Use both together to
|
|
72
|
+
* catch all possible errors.
|
|
73
|
+
*/
|
|
74
|
+
export function onAsyncIteratorObjectError(callback) {
|
|
75
|
+
// The typed error should always be combined with `ThrowableError`
|
|
76
|
+
// because an AsyncIterator can throw any error during iteration,
|
|
77
|
+
// while TError only represents errors from the initial promise.
|
|
78
|
+
// In oRPC client/server usage, initial and iteration errors are usually the same,
|
|
79
|
+
// but this utility is shared, so it needs to support the general case.
|
|
80
|
+
return async (options, ...rest) => {
|
|
81
|
+
const output = await options.next();
|
|
82
|
+
if (!isAsyncIteratorObject(output)) {
|
|
83
|
+
return output;
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* @warning
|
|
87
|
+
* Remember use `override` for AsyncIteratorObject to remain other special properties
|
|
88
|
+
*/
|
|
89
|
+
return override(output, wrapAsyncIterator(output, {
|
|
90
|
+
onError: (error) => callback(error, options, ...rest),
|
|
91
|
+
}));
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Creates an interceptor that invokes a callback when the returned readable
|
|
96
|
+
* stream errors while being consumed.
|
|
97
|
+
*
|
|
98
|
+
* This does not replace the `onError`. `onError` only fires on the
|
|
99
|
+
* initial call (before the interceptor returns the stream), whereas this
|
|
100
|
+
* callback only fires while consuming the stream. Use both together to catch
|
|
101
|
+
* all possible errors.
|
|
102
|
+
*/
|
|
103
|
+
// oxlint-disable-next-line typescript/no-unnecessary-type-parameters
|
|
104
|
+
export function onReadableStreamError(callback) {
|
|
105
|
+
// The typed error should always be combined with `ThrowableError`
|
|
106
|
+
// because a ReadableStream can throw any error during iteration,
|
|
107
|
+
// while TError only represents errors from the initial promise.
|
|
108
|
+
// In oRPC client/server usage, initial and iteration errors are usually the same,
|
|
109
|
+
// but this utility is shared, so it needs to support the general case.
|
|
110
|
+
return async (options, ...rest) => {
|
|
111
|
+
const output = await options.next();
|
|
112
|
+
if (!(output instanceof ReadableStream)) {
|
|
113
|
+
return output;
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* @warning
|
|
117
|
+
* Remember use `override` for ReadableStream to remain other special properties
|
|
118
|
+
*/
|
|
119
|
+
return override(output, wrapReadableStream(output, {
|
|
120
|
+
onError: (error) => callback(error, options, ...rest),
|
|
121
|
+
}));
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
export function intercept(interceptors, options, main) {
|
|
125
|
+
if (!interceptors?.length) {
|
|
126
|
+
return main(options);
|
|
127
|
+
}
|
|
128
|
+
const next = (options, index) => {
|
|
129
|
+
const interceptor = interceptors[index];
|
|
130
|
+
if (!interceptor) {
|
|
131
|
+
return main(options);
|
|
132
|
+
}
|
|
133
|
+
return interceptor({
|
|
134
|
+
...options,
|
|
135
|
+
next: (newOptions = options) => next(newOptions, index + 1),
|
|
136
|
+
});
|
|
137
|
+
};
|
|
138
|
+
return next(options, 0);
|
|
139
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { AsyncIteratorClass } from '@standard-server/shared';
|
|
2
|
+
import type { Promisable } from 'type-fest';
|
|
3
|
+
import type { StartSpanOptions } from './tracing.ts';
|
|
4
|
+
import type { PromiseWithError, ThrowableError } from './types.ts';
|
|
5
|
+
export interface WrapAsyncIteratorOptions<TYield, TReturn, TMappedYield, TMappedReturn> {
|
|
6
|
+
/**
|
|
7
|
+
* Any call to the original iterator will be executed inside this function.
|
|
8
|
+
* Useful when you want execution to happen within a specific context,
|
|
9
|
+
* such as AsyncLocalStorage.
|
|
10
|
+
*/
|
|
11
|
+
runWith?: (<T>(run: () => Promise<T>) => Promisable<T>) | undefined;
|
|
12
|
+
mapResult?: ((result: IteratorResult<TYield, TReturn>) => Promisable<IteratorResult<TMappedYield, TMappedReturn>>) | undefined;
|
|
13
|
+
mapError?: ((error: ThrowableError) => Promisable<ThrowableError>) | undefined;
|
|
14
|
+
onError?: ((error: ThrowableError) => Promisable<void>) | undefined;
|
|
15
|
+
/**
|
|
16
|
+
* Execute after the stream finishes or is cancelled.
|
|
17
|
+
*/
|
|
18
|
+
onFinish?: (() => Promisable<void>) | undefined;
|
|
19
|
+
}
|
|
20
|
+
export declare function wrapAsyncIterator<TYield, TReturn, TMappedYield = TYield, TMappedReturn = TReturn>(iterator: AsyncIterator<TYield, TReturn>, { runWith, mapResult, mapError, onError, onFinish, }: WrapAsyncIteratorOptions<TYield, TReturn, TMappedYield, TMappedReturn>): NoInfer<AsyncIteratorClass<TMappedYield, TMappedReturn>>;
|
|
21
|
+
export declare function traceAsyncIterator<T, TReturn, TNext>(options: StartSpanOptions | string, iterator: AsyncIterator<T, TReturn, TNext>): AsyncIteratorClass<T, TReturn, TNext>;
|
|
22
|
+
export declare function replicateAsyncIterator<T, TReturn, TNext>(source: AsyncIterator<T, TReturn, TNext>, count: number): AsyncIteratorClass<T, TReturn, TNext>[];
|
|
23
|
+
export interface ConsumeAsyncIteratorOptions<T, TReturn, TError> {
|
|
24
|
+
/**
|
|
25
|
+
* Called on each event
|
|
26
|
+
*/
|
|
27
|
+
onEvent: (event: T) => void;
|
|
28
|
+
/**
|
|
29
|
+
* Called once error happens
|
|
30
|
+
*/
|
|
31
|
+
onError?: ((error: TError) => void) | undefined;
|
|
32
|
+
/**
|
|
33
|
+
* Called once AsyncIteratorObject is done
|
|
34
|
+
*
|
|
35
|
+
* @remarks
|
|
36
|
+
* **Note**: If iterator is canceled, `undefined` can be passed on success
|
|
37
|
+
*/
|
|
38
|
+
onSuccess?: ((value: TReturn | undefined) => void) | undefined;
|
|
39
|
+
/**
|
|
40
|
+
* Called once after onError or onSuccess
|
|
41
|
+
*
|
|
42
|
+
* @remarks
|
|
43
|
+
* **Note**: If iterator is canceled, `undefined` can be passed on success
|
|
44
|
+
*/
|
|
45
|
+
onFinish?: ((state: [error: TError, data: undefined, isSuccess: false] | [error: null, data: TReturn | undefined, isSuccess: true]) => void) | undefined;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Consumes an AsyncIteratorObject with lifecycle callbacks.
|
|
49
|
+
*
|
|
50
|
+
* @remarks
|
|
51
|
+
* **Warning**: If no `onError` or `onFinish` is provided, errors are thrown into the unhandled rejection channel.
|
|
52
|
+
*
|
|
53
|
+
* @returns An unsubscribe callback that stops consuming and cleans up the iterator.
|
|
54
|
+
* @see {@link https://orpc.dev/docs/client/async-iterator-object#using-consumeasynciterator | AsyncIteratorObject in Client - Using consumeAsyncIterator}
|
|
55
|
+
*/
|
|
56
|
+
export declare function consumeAsyncIterator<T, TReturn, TError = ThrowableError>(iterator: AsyncIterator<T, TReturn> | PromiseWithError<AsyncIterator<T, TReturn>, TError>, options: ConsumeAsyncIteratorOptions<T, TReturn, TError | ThrowableError>): () => Promise<void>;
|
package/dist/iterator.js
ADDED
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
import { AsyncIteratorClass } from '@standard-server/shared';
|
|
2
|
+
import { once } from './function.js';
|
|
3
|
+
import { AsyncIdQueue } from './queue.js';
|
|
4
|
+
import { recordSpanError, runInSpanContext, startSpan } from './tracing.js';
|
|
5
|
+
export function wrapAsyncIterator(iterator, { runWith, mapResult, mapError, onError, onFinish, }) {
|
|
6
|
+
runWith ??= (run) => run();
|
|
7
|
+
let isDone;
|
|
8
|
+
return new AsyncIteratorClass(async () => {
|
|
9
|
+
try {
|
|
10
|
+
let result;
|
|
11
|
+
try {
|
|
12
|
+
result = await runWith(() => iterator.next());
|
|
13
|
+
isDone = result.done;
|
|
14
|
+
}
|
|
15
|
+
catch (error) {
|
|
16
|
+
isDone = true;
|
|
17
|
+
throw error;
|
|
18
|
+
}
|
|
19
|
+
return mapResult ? await mapResult(result) : result;
|
|
20
|
+
}
|
|
21
|
+
catch (error) {
|
|
22
|
+
await onError?.(error);
|
|
23
|
+
throw mapError ? await mapError(error) : error;
|
|
24
|
+
}
|
|
25
|
+
}, async (_state) => {
|
|
26
|
+
try {
|
|
27
|
+
// Only cancel the original iterator if it has not finished yet.
|
|
28
|
+
// Do not rely on _state because the user's options may throw errors.
|
|
29
|
+
if (!isDone) {
|
|
30
|
+
try {
|
|
31
|
+
await runWith(async () => iterator.return?.());
|
|
32
|
+
}
|
|
33
|
+
catch (error) {
|
|
34
|
+
await onError?.(error);
|
|
35
|
+
throw error;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
finally {
|
|
40
|
+
await onFinish?.();
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
export function traceAsyncIterator(options, iterator) {
|
|
45
|
+
const getSpan = once(() => startSpan(options));
|
|
46
|
+
return wrapAsyncIterator(iterator, {
|
|
47
|
+
runWith: (run) => runInSpanContext(getSpan(), run),
|
|
48
|
+
mapResult(result) {
|
|
49
|
+
getSpan()?.addEvent(result.done ? 'completed' : 'yielded');
|
|
50
|
+
return result;
|
|
51
|
+
},
|
|
52
|
+
onError(error) {
|
|
53
|
+
recordSpanError(getSpan(), error);
|
|
54
|
+
},
|
|
55
|
+
onFinish() {
|
|
56
|
+
getSpan()?.end();
|
|
57
|
+
},
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
export function replicateAsyncIterator(source, count) {
|
|
61
|
+
const queue = new AsyncIdQueue();
|
|
62
|
+
const ids = Array.from({ length: count }, (_, i) => i.toString());
|
|
63
|
+
let isSourceFinished = false;
|
|
64
|
+
const start = once(async () => {
|
|
65
|
+
try {
|
|
66
|
+
while (true) {
|
|
67
|
+
const item = await source.next();
|
|
68
|
+
ids.forEach((id) => {
|
|
69
|
+
if (queue.isOpen(id)) {
|
|
70
|
+
queue.push(id, { next: item });
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
if (item.done) {
|
|
74
|
+
break;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
catch (error) {
|
|
79
|
+
ids.forEach((id) => {
|
|
80
|
+
if (queue.isOpen(id)) {
|
|
81
|
+
queue.push(id, { error });
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
finally {
|
|
86
|
+
isSourceFinished = true;
|
|
87
|
+
}
|
|
88
|
+
});
|
|
89
|
+
const replicated = ids.map((id) => {
|
|
90
|
+
queue.open(id);
|
|
91
|
+
return new AsyncIteratorClass(async () => {
|
|
92
|
+
void start();
|
|
93
|
+
const item = await queue.pull(id);
|
|
94
|
+
if (item.next) {
|
|
95
|
+
return item.next;
|
|
96
|
+
}
|
|
97
|
+
throw item.error;
|
|
98
|
+
}, async ({ kind, error }) => {
|
|
99
|
+
queue.close({ id, reason: error });
|
|
100
|
+
if (kind === 'cancelled' && !queue.size && !isSourceFinished) {
|
|
101
|
+
isSourceFinished = true;
|
|
102
|
+
await source?.return?.();
|
|
103
|
+
}
|
|
104
|
+
});
|
|
105
|
+
});
|
|
106
|
+
return replicated;
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Consumes an AsyncIteratorObject with lifecycle callbacks.
|
|
110
|
+
*
|
|
111
|
+
* @remarks
|
|
112
|
+
* **Warning**: If no `onError` or `onFinish` is provided, errors are thrown into the unhandled rejection channel.
|
|
113
|
+
*
|
|
114
|
+
* @returns An unsubscribe callback that stops consuming and cleans up the iterator.
|
|
115
|
+
* @see {@link https://orpc.dev/docs/client/async-iterator-object#using-consumeasynciterator | AsyncIteratorObject in Client - Using consumeAsyncIterator}
|
|
116
|
+
*/
|
|
117
|
+
export function consumeAsyncIterator(iterator, options) {
|
|
118
|
+
// The typed error should always be combined with `ThrowableError`
|
|
119
|
+
// because an AsyncIterator can throw any error during iteration,
|
|
120
|
+
// while TError only represents errors from the initial promise.
|
|
121
|
+
// In oRPC client/server usage, initial and iteration errors are usually the same,
|
|
122
|
+
// but this utility is shared, so it needs to support the general case.
|
|
123
|
+
void (async () => {
|
|
124
|
+
let onFinishState;
|
|
125
|
+
try {
|
|
126
|
+
const resolvedIterator = await iterator;
|
|
127
|
+
while (true) {
|
|
128
|
+
const { done, value } = await resolvedIterator.next();
|
|
129
|
+
if (done) {
|
|
130
|
+
// if iterator is canceled, value can be undefined
|
|
131
|
+
const realValue = value;
|
|
132
|
+
onFinishState = [null, realValue, true];
|
|
133
|
+
options.onSuccess?.(realValue);
|
|
134
|
+
break;
|
|
135
|
+
}
|
|
136
|
+
try {
|
|
137
|
+
options.onEvent(value);
|
|
138
|
+
}
|
|
139
|
+
catch (error) {
|
|
140
|
+
// Close the source before reporting the onEvent error
|
|
141
|
+
await resolvedIterator.return?.();
|
|
142
|
+
throw error;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
catch (error) {
|
|
147
|
+
onFinishState = [error, undefined, false];
|
|
148
|
+
options.onError?.(error);
|
|
149
|
+
}
|
|
150
|
+
finally {
|
|
151
|
+
options.onFinish?.(onFinishState);
|
|
152
|
+
}
|
|
153
|
+
})();
|
|
154
|
+
return async () => {
|
|
155
|
+
await (await iterator)?.return?.();
|
|
156
|
+
};
|
|
157
|
+
}
|
package/dist/object.d.ts
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import type { AnyFunction } from './function.ts';
|
|
2
|
+
export type Segment = string | number;
|
|
3
|
+
export declare function findDeepMatches(check: (value: unknown) => boolean, payload: unknown, segments?: Segment[], maps?: Segment[][], values?: unknown[]): {
|
|
4
|
+
maps: Segment[][];
|
|
5
|
+
values: unknown[];
|
|
6
|
+
};
|
|
7
|
+
/**
|
|
8
|
+
* Get constructor of the value
|
|
9
|
+
*
|
|
10
|
+
*/
|
|
11
|
+
export declare function getConstructor(value: unknown): Function | null | undefined;
|
|
12
|
+
export declare function getConstructors(value: unknown): Generator<Function>;
|
|
13
|
+
/**
|
|
14
|
+
* Checks whether a value is a plain object, including objects created with
|
|
15
|
+
* `Object.create(null)`.
|
|
16
|
+
*/
|
|
17
|
+
export declare function isPlainObject(value: unknown): value is Record<PropertyKey, unknown>;
|
|
18
|
+
/**
|
|
19
|
+
* Returns `object[key]` only when it is an own property, so keys like
|
|
20
|
+
* `toString` do not resolve through the prototype chain.
|
|
21
|
+
*/
|
|
22
|
+
export declare function getOwn<T extends object>(object: T, key: PropertyKey): T[keyof T] | undefined;
|
|
23
|
+
export declare function get(object: unknown, path: readonly PropertyKey[]): unknown;
|
|
24
|
+
/**
|
|
25
|
+
* Sets a value at the given path, creating plain objects for intermediate keys as needed.
|
|
26
|
+
*/
|
|
27
|
+
export declare function set(root: object, path: [PropertyKey, ...PropertyKey[]] | [...PropertyKey[], PropertyKey], value: unknown): void;
|
|
28
|
+
/**
|
|
29
|
+
* Sets `object[key]`, defining `__proto__` as an own property instead of re-parenting the object.
|
|
30
|
+
*/
|
|
31
|
+
export declare function setOwn(object: object, key: PropertyKey, value: unknown): void;
|
|
32
|
+
/**
|
|
33
|
+
* Merges two values by copying the second over the first, repeating the merge one level deeper for
|
|
34
|
+
* nested plain objects. Anything that is not a pair of plain objects is replaced by the second one.
|
|
35
|
+
*/
|
|
36
|
+
export declare function mergeTwoLevels(first: unknown, second: unknown): unknown;
|
|
37
|
+
export declare function omit<T extends object, K extends keyof T>(obj: T, keys: readonly K[]): Omit<T, K>;
|
|
38
|
+
export declare function pick<T extends object, K extends keyof T>(obj: T, keys: readonly K[]): Pick<T, K>;
|
|
39
|
+
/**
|
|
40
|
+
* Deep clones arrays and plain objects, leaving every other value as is.
|
|
41
|
+
* Circular and shared references are preserved in the copy.
|
|
42
|
+
*/
|
|
43
|
+
export declare function clone<T>(value: T): T;
|
|
44
|
+
export declare function copyOnWrite(parent: object, key: PropertyKey, original: unknown): unknown;
|
|
45
|
+
export declare function isPropertyKey(value: unknown): value is PropertyKey;
|
|
46
|
+
export declare const NullProtoObj: {
|
|
47
|
+
new <T extends Record<PropertyKey, unknown>>(): T;
|
|
48
|
+
};
|
|
49
|
+
/**
|
|
50
|
+
* Returns an object containing all methods of the given object, with each
|
|
51
|
+
* method bound to the original object instance.
|
|
52
|
+
*
|
|
53
|
+
* Methods are collected from both the object itself and its prototype chain
|
|
54
|
+
* (excluding `Object.prototype` and the `constructor` property).
|
|
55
|
+
*/
|
|
56
|
+
export declare function bindMethods<T extends object>(obj: T, options?: {
|
|
57
|
+
unbound?: (keyof T)[];
|
|
58
|
+
}): Pick<T, {
|
|
59
|
+
[K in keyof T]: T[K] extends AnyFunction ? K : never;
|
|
60
|
+
}[keyof T]>;
|
package/dist/object.js
ADDED
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
import { getOrBind, isNonPrimitive } from '@standard-server/shared';
|
|
2
|
+
export function findDeepMatches(check, payload, segments = [], maps = [], values = []) {
|
|
3
|
+
if (check(payload)) {
|
|
4
|
+
maps.push(segments);
|
|
5
|
+
values.push(payload);
|
|
6
|
+
}
|
|
7
|
+
else if (Array.isArray(payload)) {
|
|
8
|
+
payload.forEach((v, i) => {
|
|
9
|
+
findDeepMatches(check, v, [...segments, i], maps, values);
|
|
10
|
+
});
|
|
11
|
+
}
|
|
12
|
+
else if (isPlainObject(payload)) {
|
|
13
|
+
for (const key of Object.keys(payload)) {
|
|
14
|
+
findDeepMatches(check, payload[key], [...segments, key], maps, values);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
return { maps, values };
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Get constructor of the value
|
|
21
|
+
*
|
|
22
|
+
*/
|
|
23
|
+
export function getConstructor(value) {
|
|
24
|
+
// Object.getPrototypeOf require object in node.js
|
|
25
|
+
if (!isNonPrimitive(value)) {
|
|
26
|
+
return null;
|
|
27
|
+
}
|
|
28
|
+
return Object.getPrototypeOf(value)?.constructor;
|
|
29
|
+
}
|
|
30
|
+
export function* getConstructors(value) {
|
|
31
|
+
if (!isNonPrimitive(value)) {
|
|
32
|
+
return;
|
|
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
|
+
}
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Checks whether a value is a plain object, including objects created with
|
|
44
|
+
* `Object.create(null)`.
|
|
45
|
+
*/
|
|
46
|
+
export function isPlainObject(value) {
|
|
47
|
+
if (!value || typeof value !== 'object') {
|
|
48
|
+
return false;
|
|
49
|
+
}
|
|
50
|
+
const proto = Object.getPrototypeOf(value);
|
|
51
|
+
return proto === Object.prototype || !proto || !proto.constructor;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Returns `object[key]` only when it is an own property, so keys like
|
|
55
|
+
* `toString` do not resolve through the prototype chain.
|
|
56
|
+
*/
|
|
57
|
+
export function getOwn(object, key) {
|
|
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;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Sets a value at the given path, creating plain objects for intermediate keys as needed.
|
|
72
|
+
*/
|
|
73
|
+
export function set(root, path, value) {
|
|
74
|
+
let current = root;
|
|
75
|
+
for (let i = 0; i < path.length - 1; i++) {
|
|
76
|
+
const key = path[i];
|
|
77
|
+
const next = getOwn(current, key);
|
|
78
|
+
if (!isNonPrimitive(next)) {
|
|
79
|
+
const child = {};
|
|
80
|
+
setOwn(current, key, child);
|
|
81
|
+
current = child;
|
|
82
|
+
}
|
|
83
|
+
else {
|
|
84
|
+
current = next;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
setOwn(current, path.at(-1), value);
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Sets `object[key]`, defining `__proto__` as an own property instead of re-parenting the object.
|
|
91
|
+
*/
|
|
92
|
+
export function setOwn(object, key, value) {
|
|
93
|
+
if (key === '__proto__') {
|
|
94
|
+
Object.defineProperty(object, key, {
|
|
95
|
+
value,
|
|
96
|
+
writable: true,
|
|
97
|
+
enumerable: true,
|
|
98
|
+
configurable: true,
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
else {
|
|
102
|
+
;
|
|
103
|
+
object[key] = value;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Merges two values by copying the second over the first, repeating the merge one level deeper for
|
|
108
|
+
* nested plain objects. Anything that is not a pair of plain objects is replaced by the second one.
|
|
109
|
+
*/
|
|
110
|
+
export function mergeTwoLevels(first, second) {
|
|
111
|
+
if (!isPlainObject(first) || !isPlainObject(second)) {
|
|
112
|
+
return second;
|
|
113
|
+
}
|
|
114
|
+
// Spread keeps special keys like __proto__ as own properties instead of re-parenting the result.
|
|
115
|
+
const result = { ...first, ...second };
|
|
116
|
+
for (const key of Object.keys(second)) {
|
|
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;
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* Deep clones arrays and plain objects, leaving every other value as is.
|
|
146
|
+
* Circular and shared references are preserved in the copy.
|
|
147
|
+
*/
|
|
148
|
+
export function clone(value) {
|
|
149
|
+
return cloneWithVisited(value, new WeakMap());
|
|
150
|
+
}
|
|
151
|
+
function cloneWithVisited(value, visited) {
|
|
152
|
+
if (Array.isArray(value)) {
|
|
153
|
+
const existing = visited.get(value);
|
|
154
|
+
if (existing) {
|
|
155
|
+
return existing;
|
|
156
|
+
}
|
|
157
|
+
const result = [];
|
|
158
|
+
visited.set(value, result);
|
|
159
|
+
for (const item of value) {
|
|
160
|
+
result.push(cloneWithVisited(item, visited));
|
|
161
|
+
}
|
|
162
|
+
return result;
|
|
163
|
+
}
|
|
164
|
+
if (isPlainObject(value)) {
|
|
165
|
+
const existing = visited.get(value);
|
|
166
|
+
if (existing) {
|
|
167
|
+
return existing;
|
|
168
|
+
}
|
|
169
|
+
const result = {};
|
|
170
|
+
visited.set(value, result);
|
|
171
|
+
// Use setOwn so special keys like __proto__ don't re-parent the result.
|
|
172
|
+
for (const key of Object.keys(value)) {
|
|
173
|
+
setOwn(result, key, cloneWithVisited(value[key], visited));
|
|
174
|
+
}
|
|
175
|
+
for (const sym of Object.getOwnPropertySymbols(value)) {
|
|
176
|
+
setOwn(result, sym, cloneWithVisited(value[sym], visited));
|
|
177
|
+
}
|
|
178
|
+
return result;
|
|
179
|
+
}
|
|
180
|
+
return value;
|
|
181
|
+
}
|
|
182
|
+
export function copyOnWrite(parent, key, original) {
|
|
183
|
+
const value = parent[key];
|
|
184
|
+
if (value !== original) {
|
|
185
|
+
return value;
|
|
186
|
+
}
|
|
187
|
+
let copy;
|
|
188
|
+
if (Array.isArray(value)) {
|
|
189
|
+
copy = value.slice();
|
|
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;
|
|
210
|
+
})();
|
|
211
|
+
/**
|
|
212
|
+
* Returns an object containing all methods of the given object, with each
|
|
213
|
+
* method bound to the original object instance.
|
|
214
|
+
*
|
|
215
|
+
* Methods are collected from both the object itself and its prototype chain
|
|
216
|
+
* (excluding `Object.prototype` and the `constructor` property).
|
|
217
|
+
*/
|
|
218
|
+
export function bindMethods(obj, options = {}) {
|
|
219
|
+
const unbound = new Set(options.unbound);
|
|
220
|
+
// Use NullProtoObj so special methods like toString and __proto__ are supported.
|
|
221
|
+
const methods = new NullProtoObj();
|
|
222
|
+
let current = obj;
|
|
223
|
+
while (current && current !== Object.prototype) {
|
|
224
|
+
for (const key of Object.getOwnPropertyNames(current)) {
|
|
225
|
+
if (key === 'constructor' || key in methods) {
|
|
226
|
+
continue;
|
|
227
|
+
}
|
|
228
|
+
const val = getOrBind(obj, key, { bind: !unbound.has(key) });
|
|
229
|
+
if (typeof val === 'function') {
|
|
230
|
+
methods[key] = val;
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
for (const sym of Object.getOwnPropertySymbols(current)) {
|
|
234
|
+
if (sym in methods) {
|
|
235
|
+
continue;
|
|
236
|
+
}
|
|
237
|
+
const val = getOrBind(obj, sym, { bind: !unbound.has(sym) });
|
|
238
|
+
if (typeof val === 'function') {
|
|
239
|
+
methods[sym] = val;
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
current = Object.getPrototypeOf(current);
|
|
243
|
+
}
|
|
244
|
+
return methods;
|
|
245
|
+
}
|
package/dist/plugin.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export interface OrderablePlugin {
|
|
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
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Sorts plugins based on their `before` and `after` dependencies.
|
|
11
|
+
*/
|
|
12
|
+
export declare function sortPlugins<T extends OrderablePlugin>(plugins: T[]): T[];
|