@trpc/server 11.0.0-alpha-tmp-subscription-connection-state.489 → 11.0.0-alpha-tmp-12-06-react.665
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/@trpc/server/http.d.ts +1 -1
- package/dist/@trpc/server/http.d.ts.map +1 -1
- package/dist/@trpc/server/index.d.ts +1 -1
- package/dist/@trpc/server/index.d.ts.map +1 -1
- package/dist/adapters/aws-lambda/getPlanner.d.ts.map +1 -1
- package/dist/adapters/aws-lambda/getPlanner.js +2 -2
- package/dist/adapters/aws-lambda/getPlanner.mjs +2 -2
- package/dist/adapters/aws-lambda/index.d.ts.map +1 -1
- package/dist/adapters/aws-lambda/index.js +2 -0
- package/dist/adapters/aws-lambda/index.mjs +2 -0
- package/dist/adapters/express.d.ts.map +1 -1
- package/dist/adapters/express.js +27 -7
- package/dist/adapters/express.mjs +27 -7
- package/dist/adapters/fastify/fastifyRequestHandler.js +3 -1
- package/dist/adapters/fastify/fastifyRequestHandler.mjs +3 -1
- package/dist/adapters/fastify/fastifyTRPCPlugin.d.ts.map +1 -1
- package/dist/adapters/fastify/fastifyTRPCPlugin.js +9 -2
- package/dist/adapters/fastify/fastifyTRPCPlugin.mjs +10 -3
- package/dist/adapters/fetch/fetchRequestHandler.js +9 -8
- package/dist/adapters/fetch/fetchRequestHandler.mjs +9 -8
- package/dist/adapters/next-app-dir/nextAppDirCaller.d.ts.map +1 -1
- package/dist/adapters/next-app-dir/nextAppDirCaller.js +10 -5
- package/dist/adapters/next-app-dir/nextAppDirCaller.mjs +10 -5
- package/dist/adapters/next-app-dir/notFound.js +2 -0
- package/dist/adapters/next-app-dir/notFound.mjs +2 -0
- package/dist/adapters/next-app-dir/redirect.js +16 -1
- package/dist/adapters/next-app-dir/redirect.mjs +16 -1
- package/dist/adapters/next-app-dir/rethrowNextErrors.d.ts.map +1 -1
- package/dist/adapters/next-app-dir/rethrowNextErrors.js +4 -3
- package/dist/adapters/next-app-dir/rethrowNextErrors.mjs +4 -3
- package/dist/adapters/next.d.ts.map +1 -1
- package/dist/adapters/next.js +23 -30
- package/dist/adapters/next.mjs +24 -31
- package/dist/adapters/node-http/incomingMessageToRequest.d.ts +2 -1
- package/dist/adapters/node-http/incomingMessageToRequest.d.ts.map +1 -1
- package/dist/adapters/node-http/incomingMessageToRequest.js +83 -40
- package/dist/adapters/node-http/incomingMessageToRequest.mjs +83 -41
- package/dist/adapters/node-http/index.js +2 -0
- package/dist/adapters/node-http/index.mjs +2 -2
- package/dist/adapters/node-http/nodeHTTPRequestHandler.d.ts +7 -0
- package/dist/adapters/node-http/nodeHTTPRequestHandler.d.ts.map +1 -1
- package/dist/adapters/node-http/nodeHTTPRequestHandler.js +70 -58
- package/dist/adapters/node-http/nodeHTTPRequestHandler.mjs +70 -59
- package/dist/adapters/node-http/writeResponse.d.ts +18 -0
- package/dist/adapters/node-http/writeResponse.d.ts.map +1 -0
- package/dist/adapters/node-http/writeResponse.js +82 -0
- package/dist/adapters/node-http/writeResponse.mjs +79 -0
- package/dist/adapters/standalone.d.ts +5 -2
- package/dist/adapters/standalone.d.ts.map +1 -1
- package/dist/adapters/standalone.js +25 -13
- package/dist/adapters/standalone.mjs +26 -14
- package/dist/adapters/ws.d.ts +14 -4
- package/dist/adapters/ws.d.ts.map +1 -1
- package/dist/adapters/ws.js +217 -101
- package/dist/adapters/ws.mjs +217 -102
- package/dist/bundle-analysis.json +412 -310
- package/dist/http.js +2 -4
- package/dist/http.mjs +2 -2
- package/dist/index.js +3 -0
- package/dist/index.mjs +3 -1
- package/dist/node_modules/.pnpm/@rollup_plugin-typescript@12.1.1_rollup@4.27.4_tslib@2.8.1_typescript@5.7.2/node_modules/tslib/tslib.es6.js +73 -0
- package/dist/observable/behaviorSubject.d.ts +15 -0
- package/dist/observable/behaviorSubject.d.ts.map +1 -0
- package/dist/observable/behaviorSubject.js +40 -0
- package/dist/observable/behaviorSubject.mjs +38 -0
- package/dist/observable/index.d.ts +3 -3
- package/dist/observable/index.d.ts.map +1 -1
- package/dist/observable/index.js +5 -0
- package/dist/observable/index.mjs +3 -2
- package/dist/observable/observable.d.ts +2 -1
- package/dist/observable/observable.d.ts.map +1 -1
- package/dist/observable/observable.js +31 -8
- package/dist/observable/observable.mjs +31 -8
- package/dist/observable/operators.d.ts +2 -0
- package/dist/observable/operators.d.ts.map +1 -1
- package/dist/observable/operators.js +35 -0
- package/dist/observable/operators.mjs +34 -1
- package/dist/rpc.js +2 -0
- package/dist/rpc.mjs +2 -0
- package/dist/shared.js +2 -0
- package/dist/shared.mjs +2 -0
- package/dist/unstable-core-do-not-import/clientish/inference.d.ts.map +1 -1
- package/dist/unstable-core-do-not-import/clientish/inferrable.d.ts.map +1 -1
- package/dist/unstable-core-do-not-import/clientish/serialize.d.ts +2 -2
- package/dist/unstable-core-do-not-import/clientish/serialize.d.ts.map +1 -1
- package/dist/unstable-core-do-not-import/createProxy.js +1 -1
- package/dist/unstable-core-do-not-import/createProxy.mjs +1 -1
- package/dist/unstable-core-do-not-import/error/TRPCError.d.ts +1 -1
- package/dist/unstable-core-do-not-import/error/TRPCError.js +16 -1
- package/dist/unstable-core-do-not-import/error/TRPCError.mjs +16 -1
- package/dist/unstable-core-do-not-import/error/formatter.js +1 -1
- package/dist/unstable-core-do-not-import/error/formatter.mjs +1 -1
- package/dist/unstable-core-do-not-import/error/getErrorShape.js +2 -2
- package/dist/unstable-core-do-not-import/error/getErrorShape.mjs +2 -2
- package/dist/unstable-core-do-not-import/http/contentType.js +3 -3
- package/dist/unstable-core-do-not-import/http/contentType.mjs +3 -3
- package/dist/unstable-core-do-not-import/http/getHTTPStatusCode.d.ts +6 -1
- package/dist/unstable-core-do-not-import/http/getHTTPStatusCode.d.ts.map +1 -1
- package/dist/unstable-core-do-not-import/http/getHTTPStatusCode.js +28 -0
- package/dist/unstable-core-do-not-import/http/getHTTPStatusCode.mjs +25 -1
- package/dist/unstable-core-do-not-import/http/isAbortError.d.ts +4 -0
- package/dist/unstable-core-do-not-import/http/isAbortError.d.ts.map +1 -0
- package/dist/unstable-core-do-not-import/http/isAbortError.js +9 -0
- package/dist/unstable-core-do-not-import/http/isAbortError.mjs +7 -0
- package/dist/unstable-core-do-not-import/http/resolveResponse.d.ts.map +1 -1
- package/dist/unstable-core-do-not-import/http/resolveResponse.js +180 -96
- package/dist/unstable-core-do-not-import/http/resolveResponse.mjs +182 -98
- package/dist/unstable-core-do-not-import/http/types.d.ts +1 -1
- package/dist/unstable-core-do-not-import/initTRPC.d.ts +11 -11
- package/dist/unstable-core-do-not-import/initTRPC.d.ts.map +1 -1
- package/dist/unstable-core-do-not-import/initTRPC.js +11 -11
- package/dist/unstable-core-do-not-import/initTRPC.mjs +11 -11
- package/dist/unstable-core-do-not-import/middleware.d.ts +2 -1
- package/dist/unstable-core-do-not-import/middleware.d.ts.map +1 -1
- package/dist/unstable-core-do-not-import/middleware.js +2 -2
- package/dist/unstable-core-do-not-import/middleware.mjs +2 -2
- package/dist/unstable-core-do-not-import/parser.d.ts +5 -1
- package/dist/unstable-core-do-not-import/parser.d.ts.map +1 -1
- package/dist/unstable-core-do-not-import/parser.js +4 -0
- package/dist/unstable-core-do-not-import/parser.mjs +4 -0
- package/dist/unstable-core-do-not-import/procedure.d.ts +8 -2
- package/dist/unstable-core-do-not-import/procedure.d.ts.map +1 -1
- package/dist/unstable-core-do-not-import/procedureBuilder.d.ts +31 -12
- package/dist/unstable-core-do-not-import/procedureBuilder.d.ts.map +1 -1
- package/dist/unstable-core-do-not-import/procedureBuilder.js +33 -39
- package/dist/unstable-core-do-not-import/procedureBuilder.mjs +33 -39
- package/dist/unstable-core-do-not-import/rootConfig.d.ts +17 -17
- package/dist/unstable-core-do-not-import/rootConfig.d.ts.map +1 -1
- package/dist/unstable-core-do-not-import/router.d.ts +4 -3
- package/dist/unstable-core-do-not-import/router.d.ts.map +1 -1
- package/dist/unstable-core-do-not-import/router.js +6 -5
- package/dist/unstable-core-do-not-import/router.mjs +6 -5
- package/dist/unstable-core-do-not-import/rpc/codes.d.ts +2 -9
- package/dist/unstable-core-do-not-import/rpc/codes.d.ts.map +1 -1
- package/dist/unstable-core-do-not-import/rpc/envelopes.d.ts +7 -10
- package/dist/unstable-core-do-not-import/rpc/envelopes.d.ts.map +1 -1
- package/dist/unstable-core-do-not-import/rpc/parseTRPCMessage.js +2 -2
- package/dist/unstable-core-do-not-import/rpc/parseTRPCMessage.mjs +2 -2
- package/dist/unstable-core-do-not-import/stream/jsonl.d.ts +25 -14
- package/dist/unstable-core-do-not-import/stream/jsonl.d.ts.map +1 -1
- package/dist/unstable-core-do-not-import/stream/jsonl.js +439 -318
- package/dist/unstable-core-do-not-import/stream/jsonl.mjs +439 -318
- package/dist/unstable-core-do-not-import/stream/sse.d.ts +58 -18
- package/dist/unstable-core-do-not-import/stream/sse.d.ts.map +1 -1
- package/dist/unstable-core-do-not-import/stream/sse.js +297 -122
- package/dist/unstable-core-do-not-import/stream/sse.mjs +298 -123
- package/dist/unstable-core-do-not-import/stream/sse.types.d.ts +31 -0
- package/dist/unstable-core-do-not-import/stream/sse.types.d.ts.map +1 -0
- package/dist/unstable-core-do-not-import/stream/tracked.d.ts +2 -2
- package/dist/unstable-core-do-not-import/stream/tracked.d.ts.map +1 -1
- package/dist/unstable-core-do-not-import/stream/tracked.js +1 -1
- package/dist/unstable-core-do-not-import/stream/tracked.mjs +1 -1
- package/dist/unstable-core-do-not-import/stream/utils/asyncIterable.d.ts +17 -0
- package/dist/unstable-core-do-not-import/stream/utils/asyncIterable.d.ts.map +1 -0
- package/dist/unstable-core-do-not-import/stream/utils/asyncIterable.js +171 -0
- package/dist/unstable-core-do-not-import/stream/utils/asyncIterable.mjs +167 -0
- package/dist/unstable-core-do-not-import/stream/utils/createDeferred.d.ts +0 -11
- package/dist/unstable-core-do-not-import/stream/utils/createDeferred.d.ts.map +1 -1
- package/dist/unstable-core-do-not-import/stream/utils/createDeferred.js +0 -29
- package/dist/unstable-core-do-not-import/stream/utils/createDeferred.mjs +1 -29
- package/dist/unstable-core-do-not-import/stream/utils/disposable.d.ts +17 -0
- package/dist/unstable-core-do-not-import/stream/utils/disposable.d.ts.map +1 -0
- package/dist/unstable-core-do-not-import/stream/utils/disposable.js +44 -0
- package/dist/unstable-core-do-not-import/stream/utils/disposable.mjs +41 -0
- package/dist/unstable-core-do-not-import/stream/utils/readableStreamFrom.d.ts +8 -0
- package/dist/unstable-core-do-not-import/stream/utils/readableStreamFrom.d.ts.map +1 -0
- package/dist/unstable-core-do-not-import/stream/utils/readableStreamFrom.js +25 -0
- package/dist/unstable-core-do-not-import/stream/utils/readableStreamFrom.mjs +23 -0
- package/dist/unstable-core-do-not-import/stream/utils/timerResource.d.ts +5 -0
- package/dist/unstable-core-do-not-import/stream/utils/timerResource.d.ts.map +1 -0
- package/dist/unstable-core-do-not-import/stream/utils/timerResource.js +26 -0
- package/dist/unstable-core-do-not-import/stream/utils/timerResource.mjs +23 -0
- package/dist/unstable-core-do-not-import/stream/utils/withPing.d.ts +7 -0
- package/dist/unstable-core-do-not-import/stream/utils/withPing.d.ts.map +1 -0
- package/dist/unstable-core-do-not-import/stream/utils/withPing.js +130 -0
- package/dist/unstable-core-do-not-import/stream/utils/withPing.mjs +127 -0
- package/dist/unstable-core-do-not-import/transformer.d.ts +2 -5
- package/dist/unstable-core-do-not-import/transformer.d.ts.map +1 -1
- package/dist/unstable-core-do-not-import/transformer.js +1 -1
- package/dist/unstable-core-do-not-import/transformer.mjs +1 -1
- package/dist/unstable-core-do-not-import/types.d.ts +26 -3
- package/dist/unstable-core-do-not-import/types.d.ts.map +1 -1
- package/dist/unstable-core-do-not-import/utils.d.ts +15 -0
- package/dist/unstable-core-do-not-import/utils.d.ts.map +1 -1
- package/dist/unstable-core-do-not-import/utils.js +49 -1
- package/dist/unstable-core-do-not-import/utils.mjs +45 -2
- package/dist/unstable-core-do-not-import.d.ts +3 -2
- package/dist/unstable-core-do-not-import.d.ts.map +1 -1
- package/dist/unstable-core-do-not-import.js +14 -7
- package/dist/unstable-core-do-not-import.mjs +5 -6
- package/dist/vendor/unpromise/index.d.ts +3 -0
- package/dist/vendor/unpromise/index.d.ts.map +1 -0
- package/dist/vendor/unpromise/types.d.ts +28 -0
- package/dist/vendor/unpromise/types.d.ts.map +1 -0
- package/dist/vendor/unpromise/unpromise.d.ts +121 -0
- package/dist/vendor/unpromise/unpromise.d.ts.map +1 -0
- package/dist/vendor/unpromise/unpromise.js +292 -0
- package/dist/vendor/unpromise/unpromise.mjs +289 -0
- package/package.json +20 -17
- package/src/@trpc/server/http.ts +0 -2
- package/src/@trpc/server/index.ts +2 -0
- package/src/adapters/aws-lambda/getPlanner.ts +2 -2
- package/src/adapters/aws-lambda/index.ts +5 -6
- package/src/adapters/express.ts +21 -9
- package/src/adapters/fastify/fastifyRequestHandler.ts +1 -1
- package/src/adapters/fastify/fastifyTRPCPlugin.ts +16 -5
- package/src/adapters/fetch/fetchRequestHandler.ts +2 -2
- package/src/adapters/next-app-dir/nextAppDirCaller.ts +3 -0
- package/src/adapters/next-app-dir/rethrowNextErrors.ts +3 -2
- package/src/adapters/next.ts +29 -34
- package/src/adapters/node-http/incomingMessageToRequest.ts +110 -40
- package/src/adapters/node-http/nodeHTTPRequestHandler.ts +85 -63
- package/src/adapters/node-http/writeResponse.ts +94 -0
- package/src/adapters/standalone.ts +35 -18
- package/src/adapters/ws.ts +111 -57
- package/src/observable/behaviorSubject.ts +55 -0
- package/src/observable/index.ts +22 -3
- package/src/observable/observable.ts +32 -14
- package/src/observable/operators.ts +47 -0
- package/src/unstable-core-do-not-import/clientish/inference.ts +4 -4
- package/src/unstable-core-do-not-import/clientish/inferrable.ts +6 -6
- package/src/unstable-core-do-not-import/clientish/serialize.ts +5 -5
- package/src/unstable-core-do-not-import/http/getHTTPStatusCode.ts +37 -4
- package/src/unstable-core-do-not-import/http/isAbortError.ts +7 -0
- package/src/unstable-core-do-not-import/http/resolveResponse.ts +185 -90
- package/src/unstable-core-do-not-import/http/types.ts +1 -1
- package/src/unstable-core-do-not-import/initTRPC.ts +14 -20
- package/src/unstable-core-do-not-import/middleware.ts +5 -4
- package/src/unstable-core-do-not-import/parser.ts +17 -6
- package/src/unstable-core-do-not-import/procedure.ts +17 -2
- package/src/unstable-core-do-not-import/procedureBuilder.ts +137 -122
- package/src/unstable-core-do-not-import/rootConfig.ts +20 -20
- package/src/unstable-core-do-not-import/router.ts +15 -16
- package/src/unstable-core-do-not-import/rpc/codes.ts +2 -10
- package/src/unstable-core-do-not-import/rpc/envelopes.ts +7 -12
- package/src/unstable-core-do-not-import/stream/jsonl.ts +287 -296
- package/src/unstable-core-do-not-import/stream/sse.ts +338 -171
- package/src/unstable-core-do-not-import/stream/sse.types.ts +44 -0
- package/src/unstable-core-do-not-import/stream/tracked.ts +5 -11
- package/src/unstable-core-do-not-import/stream/utils/asyncIterable.ts +89 -0
- package/src/unstable-core-do-not-import/stream/utils/createDeferred.ts +0 -36
- package/src/unstable-core-do-not-import/stream/utils/disposable.ts +52 -0
- package/src/unstable-core-do-not-import/stream/utils/readableStreamFrom.ts +28 -0
- package/src/unstable-core-do-not-import/stream/utils/timerResource.ts +29 -0
- package/src/unstable-core-do-not-import/stream/utils/withPing.ts +47 -0
- package/src/unstable-core-do-not-import/transformer.ts +1 -1
- package/src/unstable-core-do-not-import/types.ts +35 -10
- package/src/unstable-core-do-not-import/utils.ts +56 -1
- package/src/unstable-core-do-not-import.ts +3 -2
- package/src/vendor/unpromise/ATTRIBUTION.txt +1 -0
- package/src/vendor/unpromise/LICENSE +20 -0
- package/src/vendor/unpromise/index.ts +7 -0
- package/src/vendor/unpromise/types.ts +55 -0
- package/src/vendor/unpromise/unpromise.ts +380 -0
- package/dist/unstable-core-do-not-import/http/batchStreamFormatter.d.ts +0 -24
- package/dist/unstable-core-do-not-import/http/batchStreamFormatter.d.ts.map +0 -1
- package/dist/unstable-core-do-not-import/http/batchStreamFormatter.js +0 -32
- package/dist/unstable-core-do-not-import/http/batchStreamFormatter.mjs +0 -30
- package/dist/unstable-core-do-not-import/http/toURL.d.ts +0 -2
- package/dist/unstable-core-do-not-import/http/toURL.d.ts.map +0 -1
- package/dist/unstable-core-do-not-import/http/toURL.js +0 -8
- package/dist/unstable-core-do-not-import/http/toURL.mjs +0 -6
- package/dist/unstable-core-do-not-import/stream/utils/createReadableStream.d.ts +0 -10
- package/dist/unstable-core-do-not-import/stream/utils/createReadableStream.d.ts.map +0 -1
- package/dist/unstable-core-do-not-import/stream/utils/createReadableStream.js +0 -31
- package/dist/unstable-core-do-not-import/stream/utils/createReadableStream.mjs +0 -29
- package/dist/unstable-core-do-not-import/stream/utils/createServer.d.ts +0 -7
- package/dist/unstable-core-do-not-import/stream/utils/createServer.d.ts.map +0 -1
- package/dist/unstable-core-do-not-import/types.js +0 -11
- package/dist/unstable-core-do-not-import/types.mjs +0 -9
- package/src/unstable-core-do-not-import/http/batchStreamFormatter.ts +0 -29
- package/src/unstable-core-do-not-import/http/toURL.ts +0 -7
- package/src/unstable-core-do-not-import/stream/utils/createReadableStream.ts +0 -31
- package/src/unstable-core-do-not-import/stream/utils/createServer.ts +0 -44
|
@@ -0,0 +1,289 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/unbound-method */ function _define_property(obj, key, value) {
|
|
2
|
+
if (key in obj) {
|
|
3
|
+
Object.defineProperty(obj, key, {
|
|
4
|
+
value: value,
|
|
5
|
+
enumerable: true,
|
|
6
|
+
configurable: true,
|
|
7
|
+
writable: true
|
|
8
|
+
});
|
|
9
|
+
} else {
|
|
10
|
+
obj[key] = value;
|
|
11
|
+
}
|
|
12
|
+
return obj;
|
|
13
|
+
}
|
|
14
|
+
var _computedKey;
|
|
15
|
+
/** Memory safe (weakmapped) cache of the ProxyPromise for each Promise,
|
|
16
|
+
* which is retained for the lifetime of the original Promise.
|
|
17
|
+
*/ const subscribableCache = new WeakMap();
|
|
18
|
+
/** A NOOP function allowing a consistent interface for settled
|
|
19
|
+
* SubscribedPromises (settled promises are not subscribed - they resolve
|
|
20
|
+
* immediately). */ const NOOP = ()=>{
|
|
21
|
+
// noop
|
|
22
|
+
};
|
|
23
|
+
_computedKey = Symbol.toStringTag;
|
|
24
|
+
let _computedKey1 = _computedKey;
|
|
25
|
+
/**
|
|
26
|
+
* Every `Promise<T>` can be shadowed by a single `ProxyPromise<T>`. It is
|
|
27
|
+
* created once, cached and reused throughout the lifetime of the Promise. Get a
|
|
28
|
+
* Promise's ProxyPromise using `Unpromise.proxy(promise)`.
|
|
29
|
+
*
|
|
30
|
+
* The `ProxyPromise<T>` attaches handlers to the original `Promise<T>`
|
|
31
|
+
* `.then()` and `.catch()` just once. Promises derived from it use a
|
|
32
|
+
* subscription- (and unsubscription-) based mechanism that monitors these
|
|
33
|
+
* handlers.
|
|
34
|
+
*
|
|
35
|
+
* Every time you call `.subscribe()`, `.then()` `.catch()` or `.finally()` on a
|
|
36
|
+
* `ProxyPromise<T>` it returns a `SubscribedPromise<T>` having an additional
|
|
37
|
+
* `unsubscribe()` method. Calling `unsubscribe()` detaches reference chains
|
|
38
|
+
* from the original, potentially long-lived Promise, eliminating memory leaks.
|
|
39
|
+
*
|
|
40
|
+
* This approach can eliminate the memory leaks that otherwise come about from
|
|
41
|
+
* repeated `race()` or `any()` calls invoking `.then()` and `.catch()` multiple
|
|
42
|
+
* times on the same long-lived native Promise (subscriptions which can never be
|
|
43
|
+
* cleaned up).
|
|
44
|
+
*
|
|
45
|
+
* `Unpromise.race(promises)` is a reference implementation of `Promise.race`
|
|
46
|
+
* avoiding memory leaks when using long-lived unsettled Promises.
|
|
47
|
+
*
|
|
48
|
+
* `Unpromise.any(promises)` is a reference implementation of `Promise.any`
|
|
49
|
+
* avoiding memory leaks when using long-lived unsettled Promises.
|
|
50
|
+
*
|
|
51
|
+
* `Unpromise.resolve(promise)` returns an ephemeral `SubscribedPromise<T>` for
|
|
52
|
+
* any given `Promise<T>` facilitating arbitrary async/await patterns. Behind
|
|
53
|
+
* the scenes, `resolve` is implemented simply as
|
|
54
|
+
* `Unpromise.proxy(promise).subscribe()`. Don't forget to call `.unsubscribe()`
|
|
55
|
+
* to tidy up!
|
|
56
|
+
*
|
|
57
|
+
*/ class Unpromise {
|
|
58
|
+
/** Create a promise that mitigates uncontrolled subscription to a long-lived
|
|
59
|
+
* Promise via .then() and .catch() - otherwise a source of memory leaks.
|
|
60
|
+
*
|
|
61
|
+
* The returned promise has an `unsubscribe()` method which can be called when
|
|
62
|
+
* the Promise is no longer being tracked by application logic, and which
|
|
63
|
+
* ensures that there is no reference chain from the original promise to the
|
|
64
|
+
* new one, and therefore no memory leak.
|
|
65
|
+
*
|
|
66
|
+
* If original promise has not yet settled, this adds a new unique promise
|
|
67
|
+
* that listens to then/catch events, along with an `unsubscribe()` method to
|
|
68
|
+
* detach it.
|
|
69
|
+
*
|
|
70
|
+
* If original promise has settled, then creates a new Promise.resolve() or
|
|
71
|
+
* Promise.reject() and provided unsubscribe is a noop.
|
|
72
|
+
*
|
|
73
|
+
* If you call `unsubscribe()` before the returned Promise has settled, it
|
|
74
|
+
* will never settle.
|
|
75
|
+
*/ subscribe() {
|
|
76
|
+
// in all cases we will combine some promise with its unsubscribe function
|
|
77
|
+
let promise;
|
|
78
|
+
let unsubscribe;
|
|
79
|
+
const { settlement } = this;
|
|
80
|
+
if (settlement === null) {
|
|
81
|
+
// not yet settled - subscribe new promise. Expect eventual settlement
|
|
82
|
+
if (this.subscribers === null) {
|
|
83
|
+
// invariant - it is not settled, so it must have subscribers
|
|
84
|
+
throw new Error("Unpromise settled but still has subscribers");
|
|
85
|
+
}
|
|
86
|
+
const subscriber = withResolvers();
|
|
87
|
+
this.subscribers = listWithMember(this.subscribers, subscriber);
|
|
88
|
+
promise = subscriber.promise;
|
|
89
|
+
unsubscribe = ()=>{
|
|
90
|
+
if (this.subscribers !== null) {
|
|
91
|
+
this.subscribers = listWithoutMember(this.subscribers, subscriber);
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
} else {
|
|
95
|
+
// settled - don't create subscribed promise. Just resolve or reject
|
|
96
|
+
const { status } = settlement;
|
|
97
|
+
if (status === "fulfilled") {
|
|
98
|
+
promise = Promise.resolve(settlement.value);
|
|
99
|
+
} else {
|
|
100
|
+
promise = Promise.reject(settlement.reason);
|
|
101
|
+
}
|
|
102
|
+
unsubscribe = NOOP;
|
|
103
|
+
}
|
|
104
|
+
// extend promise signature with the extra method
|
|
105
|
+
return Object.assign(promise, {
|
|
106
|
+
unsubscribe
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
/** STANDARD PROMISE METHODS (but returning a SubscribedPromise) */ then(onfulfilled, onrejected) {
|
|
110
|
+
const subscribed = this.subscribe();
|
|
111
|
+
const { unsubscribe } = subscribed;
|
|
112
|
+
return Object.assign(subscribed.then(onfulfilled, onrejected), {
|
|
113
|
+
unsubscribe
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
catch(onrejected) {
|
|
117
|
+
const subscribed = this.subscribe();
|
|
118
|
+
const { unsubscribe } = subscribed;
|
|
119
|
+
return Object.assign(subscribed.catch(onrejected), {
|
|
120
|
+
unsubscribe
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
finally(onfinally) {
|
|
124
|
+
const subscribed = this.subscribe();
|
|
125
|
+
const { unsubscribe } = subscribed;
|
|
126
|
+
return Object.assign(subscribed.finally(onfinally), {
|
|
127
|
+
unsubscribe
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
/** Unpromise STATIC METHODS */ /** Create or Retrieve the proxy Unpromise (a re-used Unpromise for the VM lifetime
|
|
131
|
+
* of the provided Promise reference) */ static proxy(promise) {
|
|
132
|
+
const cached = Unpromise.getSubscribablePromise(promise);
|
|
133
|
+
return typeof cached !== "undefined" ? cached : Unpromise.createSubscribablePromise(promise);
|
|
134
|
+
}
|
|
135
|
+
/** Create and store an Unpromise keyed by an original Promise. */ static createSubscribablePromise(promise) {
|
|
136
|
+
const created = new Unpromise(promise);
|
|
137
|
+
subscribableCache.set(promise, created); // resolve promise to unpromise
|
|
138
|
+
subscribableCache.set(created, created); // resolve the unpromise to itself
|
|
139
|
+
return created;
|
|
140
|
+
}
|
|
141
|
+
/** Retrieve a previously-created Unpromise keyed by an original Promise. */ static getSubscribablePromise(promise) {
|
|
142
|
+
return subscribableCache.get(promise);
|
|
143
|
+
}
|
|
144
|
+
/** Promise STATIC METHODS */ /** Lookup the Unpromise for this promise, and derive a SubscribedPromise from
|
|
145
|
+
* it (that can be later unsubscribed to eliminate Memory leaks) */ static resolve(value) {
|
|
146
|
+
const promise = typeof value === "object" && value !== null && "then" in value && typeof value.then === "function" ? value : Promise.resolve(value);
|
|
147
|
+
return Unpromise.proxy(promise).subscribe();
|
|
148
|
+
}
|
|
149
|
+
static async any(values) {
|
|
150
|
+
const valuesArray = Array.isArray(values) ? values : [
|
|
151
|
+
...values
|
|
152
|
+
];
|
|
153
|
+
const subscribedPromises = valuesArray.map(Unpromise.resolve);
|
|
154
|
+
try {
|
|
155
|
+
return await Promise.any(subscribedPromises);
|
|
156
|
+
} finally{
|
|
157
|
+
subscribedPromises.forEach(({ unsubscribe })=>{
|
|
158
|
+
unsubscribe();
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
static async race(values) {
|
|
163
|
+
const valuesArray = Array.isArray(values) ? values : [
|
|
164
|
+
...values
|
|
165
|
+
];
|
|
166
|
+
const subscribedPromises = valuesArray.map(Unpromise.resolve);
|
|
167
|
+
try {
|
|
168
|
+
return await Promise.race(subscribedPromises);
|
|
169
|
+
} finally{
|
|
170
|
+
subscribedPromises.forEach(({ unsubscribe })=>{
|
|
171
|
+
unsubscribe();
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
/** Create a race of SubscribedPromises that will fulfil to a single winning
|
|
176
|
+
* Promise (in a 1-Tuple). Eliminates memory leaks from long-lived promises
|
|
177
|
+
* accumulating .then() and .catch() subscribers. Allows simple logic to
|
|
178
|
+
* consume the result, like...
|
|
179
|
+
* ```ts
|
|
180
|
+
* const [ winner ] = await Unpromise.race([ promiseA, promiseB ]);
|
|
181
|
+
* if(winner === promiseB){
|
|
182
|
+
* const result = await promiseB;
|
|
183
|
+
* // do the thing
|
|
184
|
+
* }
|
|
185
|
+
* ```
|
|
186
|
+
* */ static async raceReferences(promises) {
|
|
187
|
+
// map each promise to an eventual 1-tuple containing itself
|
|
188
|
+
const selfPromises = promises.map(resolveSelfTuple);
|
|
189
|
+
// now race them. They will fulfil to a readonly [P] or reject.
|
|
190
|
+
try {
|
|
191
|
+
return await Promise.race(selfPromises);
|
|
192
|
+
} finally{
|
|
193
|
+
for (const promise of selfPromises){
|
|
194
|
+
// unsubscribe proxy promises when the race is over to mitigate memory leaks
|
|
195
|
+
promise.unsubscribe();
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
constructor(arg){
|
|
200
|
+
/** INSTANCE IMPLEMENTATION */ /** The promise shadowed by this Unpromise<T> */ _define_property(this, "promise", void 0);
|
|
201
|
+
/** Promises expecting eventual settlement (unless unsubscribed first). This list is deleted
|
|
202
|
+
* after the original promise settles - no further notifications will be issued. */ _define_property(this, "subscribers", []);
|
|
203
|
+
/** The Promise's settlement (recorded when it fulfils or rejects). This is consulted when
|
|
204
|
+
* calling .subscribe() .then() .catch() .finally() to see if an immediately-resolving Promise
|
|
205
|
+
* can be returned, and therefore subscription can be bypassed. */ _define_property(this, "settlement", null);
|
|
206
|
+
/** TOSTRING SUPPORT */ _define_property(this, _computedKey1, "Unpromise");
|
|
207
|
+
// handle either a Promise or a Promise executor function
|
|
208
|
+
if (typeof arg === "function") {
|
|
209
|
+
this.promise = new Promise(arg);
|
|
210
|
+
} else {
|
|
211
|
+
this.promise = arg;
|
|
212
|
+
}
|
|
213
|
+
// subscribe for eventual fulfilment and rejection
|
|
214
|
+
// handle PromiseLike objects (that at least have .then)
|
|
215
|
+
const thenReturn = this.promise.then((value)=>{
|
|
216
|
+
// atomically record fulfilment and detach subscriber list
|
|
217
|
+
const { subscribers } = this;
|
|
218
|
+
this.subscribers = null;
|
|
219
|
+
this.settlement = {
|
|
220
|
+
status: "fulfilled",
|
|
221
|
+
value
|
|
222
|
+
};
|
|
223
|
+
// notify fulfilment to subscriber list
|
|
224
|
+
subscribers?.forEach(({ resolve })=>{
|
|
225
|
+
resolve(value);
|
|
226
|
+
});
|
|
227
|
+
});
|
|
228
|
+
// handle Promise (that also have a .catch behaviour)
|
|
229
|
+
if ("catch" in thenReturn) {
|
|
230
|
+
thenReturn.catch((reason)=>{
|
|
231
|
+
// atomically record rejection and detach subscriber list
|
|
232
|
+
const { subscribers } = this;
|
|
233
|
+
this.subscribers = null;
|
|
234
|
+
this.settlement = {
|
|
235
|
+
status: "rejected",
|
|
236
|
+
reason
|
|
237
|
+
};
|
|
238
|
+
// notify rejection to subscriber list
|
|
239
|
+
subscribers?.forEach(({ reject })=>{
|
|
240
|
+
reject(reason);
|
|
241
|
+
});
|
|
242
|
+
});
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
/** Promises a 1-tuple containing the original promise when it resolves. Allows
|
|
247
|
+
* awaiting the eventual Promise ***reference*** (easy to destructure and
|
|
248
|
+
* exactly compare with ===). Avoids resolving to the Promise ***value*** (which
|
|
249
|
+
* may be ambiguous and therefore hard to identify as the winner of a race).
|
|
250
|
+
* You can call unsubscribe on the Promise to mitigate memory leaks.
|
|
251
|
+
* */ function resolveSelfTuple(promise) {
|
|
252
|
+
return Unpromise.proxy(promise).then(()=>[
|
|
253
|
+
promise
|
|
254
|
+
]);
|
|
255
|
+
}
|
|
256
|
+
/** VENDORED (Future) PROMISE UTILITIES */ /** Reference implementation of https://github.com/tc39/proposal-promise-with-resolvers */ function withResolvers() {
|
|
257
|
+
let resolve;
|
|
258
|
+
let reject;
|
|
259
|
+
const promise = new Promise((_resolve, _reject)=>{
|
|
260
|
+
resolve = _resolve;
|
|
261
|
+
reject = _reject;
|
|
262
|
+
});
|
|
263
|
+
return {
|
|
264
|
+
promise,
|
|
265
|
+
resolve,
|
|
266
|
+
reject
|
|
267
|
+
};
|
|
268
|
+
}
|
|
269
|
+
/** IMMUTABLE LIST OPERATIONS */ function listWithMember(arr, member) {
|
|
270
|
+
return [
|
|
271
|
+
...arr,
|
|
272
|
+
member
|
|
273
|
+
];
|
|
274
|
+
}
|
|
275
|
+
function listWithoutIndex(arr, index) {
|
|
276
|
+
return [
|
|
277
|
+
...arr.slice(0, index),
|
|
278
|
+
...arr.slice(index + 1)
|
|
279
|
+
];
|
|
280
|
+
}
|
|
281
|
+
function listWithoutMember(arr, member) {
|
|
282
|
+
const index = arr.indexOf(member);
|
|
283
|
+
if (index !== -1) {
|
|
284
|
+
return listWithoutIndex(arr, index);
|
|
285
|
+
}
|
|
286
|
+
return arr;
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
export { Unpromise, resolveSelfTuple };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@trpc/server",
|
|
3
|
-
"version": "11.0.0-alpha-tmp-
|
|
3
|
+
"version": "11.0.0-alpha-tmp-12-06-react.665+f3677f632",
|
|
4
4
|
"description": "The tRPC server library",
|
|
5
5
|
"author": "KATT",
|
|
6
6
|
"license": "MIT",
|
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
"dev": "pnpm build --watch",
|
|
19
19
|
"codegen-entrypoints": "tsx entrypoints.script.ts",
|
|
20
20
|
"benchmark": "tsc --project tsconfig.benchmark.json",
|
|
21
|
-
"lint": "eslint --cache
|
|
21
|
+
"lint": "eslint --cache src",
|
|
22
22
|
"ts-watch": "tsc --watch"
|
|
23
23
|
},
|
|
24
24
|
"exports": {
|
|
@@ -110,38 +110,38 @@
|
|
|
110
110
|
"rpc",
|
|
111
111
|
"shared",
|
|
112
112
|
"unstable-core-do-not-import",
|
|
113
|
-
"!**/*.test.*"
|
|
113
|
+
"!**/*.test.*",
|
|
114
|
+
"!**/__tests__"
|
|
114
115
|
],
|
|
115
116
|
"publishConfig": {
|
|
116
117
|
"access": "public"
|
|
117
118
|
},
|
|
118
119
|
"devDependencies": {
|
|
119
120
|
"@fastify/websocket": "^10.0.1",
|
|
120
|
-
"@tanstack/react-query": "^5.
|
|
121
|
+
"@tanstack/react-query": "^5.59.15",
|
|
121
122
|
"@types/aws-lambda": "^8.10.137",
|
|
122
123
|
"@types/express": "^4.17.17",
|
|
123
124
|
"@types/hash-sum": "^1.0.0",
|
|
124
|
-
"@types/node": "^
|
|
125
|
-
"@types/react": "^
|
|
126
|
-
"@types/react-dom": "^
|
|
125
|
+
"@types/node": "^22.9.0",
|
|
126
|
+
"@types/react": "^19.0.0",
|
|
127
|
+
"@types/react-dom": "^19.0.0",
|
|
127
128
|
"@types/ws": "^8.2.0",
|
|
128
129
|
"devalue": "^5.0.0",
|
|
129
|
-
"eslint": "^
|
|
130
|
+
"eslint": "^9.13.0",
|
|
130
131
|
"express": "^4.17.1",
|
|
131
132
|
"fastify": "^4.16.0",
|
|
132
|
-
"fastify-plugin": "^
|
|
133
|
+
"fastify-plugin": "^5.0.0",
|
|
133
134
|
"hash-sum": "^2.0.0",
|
|
134
135
|
"myzod": "^1.3.1",
|
|
135
|
-
"next": "^
|
|
136
|
-
"react": "^
|
|
137
|
-
"react-dom": "^
|
|
138
|
-
"rollup": "^4.
|
|
136
|
+
"next": "^15.0.4",
|
|
137
|
+
"react": "^19.0.0",
|
|
138
|
+
"react-dom": "^19.0.0",
|
|
139
|
+
"rollup": "^4.24.4",
|
|
139
140
|
"superjson": "^1.12.4",
|
|
140
141
|
"superstruct": "^2.0.0",
|
|
141
|
-
"tslib": "^2.5.0",
|
|
142
142
|
"tsx": "^4.0.0",
|
|
143
|
-
"typescript": "^5.
|
|
144
|
-
"valibot": "^0.
|
|
143
|
+
"typescript": "^5.7.0",
|
|
144
|
+
"valibot": "^0.42.0",
|
|
145
145
|
"ws": "^8.0.0",
|
|
146
146
|
"yup": "^1.0.0",
|
|
147
147
|
"zod": "^3.0.0"
|
|
@@ -149,5 +149,8 @@
|
|
|
149
149
|
"funding": [
|
|
150
150
|
"https://trpc.io/sponsor"
|
|
151
151
|
],
|
|
152
|
-
"
|
|
152
|
+
"peerDependencies": {
|
|
153
|
+
"typescript": ">=5.6.2"
|
|
154
|
+
},
|
|
155
|
+
"gitHead": "f3677f6326d62400c29d1c358db9f3a68a0dd27d"
|
|
153
156
|
}
|
package/src/@trpc/server/http.ts
CHANGED
|
@@ -29,8 +29,8 @@ function determinePayloadFormat(event: LambdaEvent): string {
|
|
|
29
29
|
export type inferAPIGWReturn<TEvent> = TEvent extends APIGatewayProxyEvent
|
|
30
30
|
? APIGatewayProxyResult
|
|
31
31
|
: TEvent extends APIGatewayProxyEventV2
|
|
32
|
-
|
|
33
|
-
|
|
32
|
+
? APIGatewayProxyStructuredResultV2
|
|
33
|
+
: never;
|
|
34
34
|
|
|
35
35
|
interface Processor<TEvent extends LambdaEvent> {
|
|
36
36
|
getTRPCPath: (event: TEvent) => string;
|
|
@@ -33,12 +33,11 @@ export type CreateAWSLambdaContextOptions<TEvent extends LambdaEvent> = {
|
|
|
33
33
|
export type AWSLambdaOptions<
|
|
34
34
|
TRouter extends AnyRouter,
|
|
35
35
|
TEvent extends LambdaEvent,
|
|
36
|
-
> =
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
>;
|
|
36
|
+
> = HTTPBaseHandlerOptions<TRouter, TEvent> &
|
|
37
|
+
CreateContextCallback<
|
|
38
|
+
inferRouterContext<AnyRouter>,
|
|
39
|
+
AWSLambdaCreateContextFn<TRouter, TEvent>
|
|
40
|
+
>;
|
|
42
41
|
|
|
43
42
|
export type AWSLambdaCreateContextFn<
|
|
44
43
|
TRouter extends AnyRouter,
|
package/src/adapters/express.ts
CHANGED
|
@@ -9,11 +9,13 @@
|
|
|
9
9
|
*/
|
|
10
10
|
import type * as express from 'express';
|
|
11
11
|
import type { AnyRouter } from '../@trpc/server';
|
|
12
|
+
// eslint-disable-next-line no-restricted-imports
|
|
13
|
+
import { run } from '../unstable-core-do-not-import';
|
|
12
14
|
import type {
|
|
13
15
|
NodeHTTPCreateContextFnOptions,
|
|
14
16
|
NodeHTTPHandlerOptions,
|
|
15
17
|
} from './node-http';
|
|
16
|
-
import { nodeHTTPRequestHandler } from './node-http';
|
|
18
|
+
import { internal_exceptionHandler, nodeHTTPRequestHandler } from './node-http';
|
|
17
19
|
|
|
18
20
|
export type CreateExpressContextOptions = NodeHTTPCreateContextFnOptions<
|
|
19
21
|
express.Request,
|
|
@@ -23,14 +25,24 @@ export type CreateExpressContextOptions = NodeHTTPCreateContextFnOptions<
|
|
|
23
25
|
export function createExpressMiddleware<TRouter extends AnyRouter>(
|
|
24
26
|
opts: NodeHTTPHandlerOptions<TRouter, express.Request, express.Response>,
|
|
25
27
|
): express.Handler {
|
|
26
|
-
return
|
|
27
|
-
|
|
28
|
+
return (req, res) => {
|
|
29
|
+
let path = '';
|
|
30
|
+
run(async () => {
|
|
31
|
+
path = req.path.slice(1);
|
|
28
32
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
33
|
+
await nodeHTTPRequestHandler({
|
|
34
|
+
...(opts as any),
|
|
35
|
+
req,
|
|
36
|
+
res,
|
|
37
|
+
path,
|
|
38
|
+
});
|
|
39
|
+
}).catch(
|
|
40
|
+
internal_exceptionHandler({
|
|
41
|
+
req,
|
|
42
|
+
res,
|
|
43
|
+
path,
|
|
44
|
+
...opts,
|
|
45
|
+
}),
|
|
46
|
+
);
|
|
35
47
|
};
|
|
36
48
|
}
|
|
@@ -60,7 +60,7 @@ export async function fastifyRequestHandler<
|
|
|
60
60
|
if ('body' in opts.req) {
|
|
61
61
|
incomingMessage.body = opts.req.body;
|
|
62
62
|
}
|
|
63
|
-
const req = incomingMessageToRequest(incomingMessage, {
|
|
63
|
+
const req = incomingMessageToRequest(incomingMessage, opts.res.raw, {
|
|
64
64
|
maxBodySize: null,
|
|
65
65
|
});
|
|
66
66
|
|
|
@@ -14,7 +14,11 @@ import type { AnyRouter } from '../../@trpc/server';
|
|
|
14
14
|
// @trpc/server/http
|
|
15
15
|
import type { NodeHTTPCreateContextFnOptions } from '../node-http';
|
|
16
16
|
// @trpc/server/ws
|
|
17
|
-
import {
|
|
17
|
+
import {
|
|
18
|
+
getWSConnectionHandler,
|
|
19
|
+
handleKeepAlive,
|
|
20
|
+
type WSSHandlerOptions,
|
|
21
|
+
} from '../ws';
|
|
18
22
|
import type { FastifyHandlerOptions } from './fastifyRequestHandler';
|
|
19
23
|
import { fastifyRequestHandler } from './fastifyRequestHandler';
|
|
20
24
|
|
|
@@ -57,13 +61,20 @@ export function fastifyTRPCPlugin<TRouter extends AnyRouter>(
|
|
|
57
61
|
});
|
|
58
62
|
|
|
59
63
|
if (opts.useWSS) {
|
|
64
|
+
const trpcOptions =
|
|
65
|
+
opts.trpcOptions as unknown as WSSHandlerOptions<TRouter>;
|
|
66
|
+
|
|
60
67
|
const onConnection = getWSConnectionHandler<TRouter>({
|
|
61
|
-
...
|
|
68
|
+
...trpcOptions,
|
|
62
69
|
});
|
|
63
70
|
|
|
64
|
-
fastify.get(prefix ?? '/', { websocket: true }, (socket, req) =>
|
|
65
|
-
onConnection(socket, req.raw)
|
|
66
|
-
|
|
71
|
+
fastify.get(prefix ?? '/', { websocket: true }, async (socket, req) => {
|
|
72
|
+
await onConnection(socket, req.raw);
|
|
73
|
+
if (trpcOptions?.keepAlive?.enabled) {
|
|
74
|
+
const { pingMs, pongWaitMs } = trpcOptions.keepAlive;
|
|
75
|
+
handleKeepAlive(socket, pingMs, pongWaitMs);
|
|
76
|
+
}
|
|
77
|
+
});
|
|
67
78
|
}
|
|
68
79
|
|
|
69
80
|
done();
|
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
|
|
12
12
|
import type { AnyRouter } from '../../@trpc/server';
|
|
13
13
|
import type { ResolveHTTPRequestOptionsContextFn } from '../../@trpc/server/http';
|
|
14
|
-
import { resolveResponse
|
|
14
|
+
import { resolveResponse } from '../../@trpc/server/http';
|
|
15
15
|
import type { FetchHandlerRequestOptions } from './types';
|
|
16
16
|
|
|
17
17
|
const trimSlashes = (path: string): string => {
|
|
@@ -32,7 +32,7 @@ export async function fetchRequestHandler<TRouter extends AnyRouter>(
|
|
|
32
32
|
return opts.createContext?.({ req: opts.req, resHeaders, ...innerOpts });
|
|
33
33
|
};
|
|
34
34
|
|
|
35
|
-
const url =
|
|
35
|
+
const url = new URL(opts.req.url);
|
|
36
36
|
|
|
37
37
|
const pathname = trimSlashes(url.pathname);
|
|
38
38
|
const endpoint = trimSlashes(opts.endpoint);
|
|
@@ -96,6 +96,7 @@ export function nextAppDirCaller<TContext, TMeta>(
|
|
|
96
96
|
getRawInput: async () => input,
|
|
97
97
|
path,
|
|
98
98
|
input,
|
|
99
|
+
signal: undefined,
|
|
99
100
|
})
|
|
100
101
|
.then((data) => {
|
|
101
102
|
if (data instanceof TRPCRedirectError) throw data;
|
|
@@ -112,6 +113,7 @@ export function nextAppDirCaller<TContext, TMeta>(
|
|
|
112
113
|
getRawInput: async () => input,
|
|
113
114
|
path,
|
|
114
115
|
input,
|
|
116
|
+
signal: undefined,
|
|
115
117
|
})
|
|
116
118
|
.then((data) => {
|
|
117
119
|
if (data instanceof TRPCRedirectError) throw data;
|
|
@@ -119,6 +121,7 @@ export function nextAppDirCaller<TContext, TMeta>(
|
|
|
119
121
|
})
|
|
120
122
|
.catch(handleError);
|
|
121
123
|
}
|
|
124
|
+
case 'subscription':
|
|
122
125
|
default: {
|
|
123
126
|
throw new TRPCError({
|
|
124
127
|
code: 'NOT_IMPLEMENTED',
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import * as nextNavigation from 'next/navigation
|
|
1
|
+
import * as nextNavigation from 'next/navigation';
|
|
2
2
|
import type { TRPCError } from '../../@trpc/server';
|
|
3
3
|
import { TRPCRedirectError } from './redirect';
|
|
4
4
|
|
|
@@ -62,6 +62,7 @@ export const rethrowNextErrors = (error: TRPCError) => {
|
|
|
62
62
|
|
|
63
63
|
// Before Next.js 15, we have to check and rethrow the error manually.
|
|
64
64
|
if (isRedirectError(cause) || isNotFoundError(cause)) {
|
|
65
|
-
|
|
65
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
66
|
+
throw cause!;
|
|
66
67
|
}
|
|
67
68
|
};
|
package/src/adapters/next.ts
CHANGED
|
@@ -11,12 +11,14 @@ import type { NextApiHandler, NextApiRequest, NextApiResponse } from 'next';
|
|
|
11
11
|
// @trpc/server
|
|
12
12
|
import type { AnyRouter } from '../@trpc/server';
|
|
13
13
|
// @trpc/server
|
|
14
|
-
import {
|
|
14
|
+
import { TRPCError } from '../@trpc/server';
|
|
15
|
+
// eslint-disable-next-line no-restricted-imports
|
|
16
|
+
import { run } from '../unstable-core-do-not-import';
|
|
15
17
|
import type {
|
|
16
18
|
NodeHTTPCreateContextFnOptions,
|
|
17
19
|
NodeHTTPHandlerOptions,
|
|
18
20
|
} from './node-http';
|
|
19
|
-
import { nodeHTTPRequestHandler } from './node-http';
|
|
21
|
+
import { internal_exceptionHandler, nodeHTTPRequestHandler } from './node-http';
|
|
20
22
|
|
|
21
23
|
export type CreateNextContextOptions = NodeHTTPCreateContextFnOptions<
|
|
22
24
|
NextApiRequest,
|
|
@@ -32,43 +34,36 @@ export function createNextApiHandler<TRouter extends AnyRouter>(
|
|
|
32
34
|
opts: NodeHTTPHandlerOptions<TRouter, NextApiRequest, NextApiResponse>,
|
|
33
35
|
): NextApiHandler {
|
|
34
36
|
return async (req, res) => {
|
|
35
|
-
|
|
36
|
-
if (typeof req.query['trpc'] === 'string') {
|
|
37
|
-
return req.query['trpc'];
|
|
38
|
-
}
|
|
39
|
-
if (Array.isArray(req.query['trpc'])) {
|
|
40
|
-
return req.query['trpc'].join('/');
|
|
41
|
-
}
|
|
42
|
-
return null;
|
|
43
|
-
}
|
|
44
|
-
const path = getPath();
|
|
37
|
+
let path = '';
|
|
45
38
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
39
|
+
await run(async () => {
|
|
40
|
+
path = run(() => {
|
|
41
|
+
if (typeof req.query['trpc'] === 'string') {
|
|
42
|
+
return req.query['trpc'];
|
|
43
|
+
}
|
|
44
|
+
if (Array.isArray(req.query['trpc'])) {
|
|
45
|
+
return req.query['trpc'].join('/');
|
|
46
|
+
}
|
|
47
|
+
throw new TRPCError({
|
|
50
48
|
message:
|
|
51
49
|
'Query "trpc" not found - is the file named `[trpc]`.ts or `[...trpc].ts`?',
|
|
52
50
|
code: 'INTERNAL_SERVER_ERROR',
|
|
53
|
-
})
|
|
54
|
-
type: 'unknown',
|
|
55
|
-
ctx: undefined,
|
|
56
|
-
path: undefined,
|
|
57
|
-
input: undefined,
|
|
51
|
+
});
|
|
58
52
|
});
|
|
59
|
-
res.statusCode = 500;
|
|
60
|
-
res.json({
|
|
61
|
-
id: -1,
|
|
62
|
-
error,
|
|
63
|
-
});
|
|
64
|
-
return;
|
|
65
|
-
}
|
|
66
53
|
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
54
|
+
await nodeHTTPRequestHandler({
|
|
55
|
+
...(opts as any),
|
|
56
|
+
req,
|
|
57
|
+
res,
|
|
58
|
+
path,
|
|
59
|
+
});
|
|
60
|
+
}).catch(
|
|
61
|
+
internal_exceptionHandler({
|
|
62
|
+
req,
|
|
63
|
+
res,
|
|
64
|
+
path,
|
|
65
|
+
...opts,
|
|
66
|
+
}),
|
|
67
|
+
);
|
|
73
68
|
};
|
|
74
69
|
}
|