@trpc/server 11.3.1 → 11.3.2-canary.2
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/adapters/aws-lambda/package.json +1 -0
- package/adapters/express/package.json +1 -0
- package/adapters/fastify/package.json +1 -0
- package/adapters/fetch/package.json +1 -0
- package/adapters/next/package.json +1 -0
- package/adapters/next-app-dir/package.json +1 -0
- package/adapters/node-http/package.json +1 -0
- package/adapters/standalone/package.json +1 -0
- package/adapters/ws/package.json +1 -0
- package/dist/adapters/aws-lambda/index.cjs +255 -0
- package/dist/adapters/aws-lambda/index.d.cts +31 -0
- package/dist/adapters/aws-lambda/index.d.cts.map +1 -0
- package/dist/adapters/aws-lambda/index.d.mts +31 -0
- package/dist/adapters/aws-lambda/index.d.mts.map +1 -0
- package/dist/adapters/aws-lambda/index.mjs +248 -57
- package/dist/adapters/aws-lambda/index.mjs.map +1 -0
- package/dist/adapters/express.cjs +34 -0
- package/dist/adapters/express.d.cts +15 -0
- package/dist/adapters/express.d.cts.map +1 -0
- package/dist/adapters/express.d.mts +15 -0
- package/dist/adapters/express.d.mts.map +1 -0
- package/dist/adapters/express.mjs +30 -32
- package/dist/adapters/express.mjs.map +1 -0
- package/dist/adapters/fastify/index.cjs +75 -0
- package/dist/adapters/fastify/index.d.cts +29 -0
- package/dist/adapters/fastify/index.d.cts.map +1 -0
- package/dist/adapters/fastify/index.d.mts +29 -0
- package/dist/adapters/fastify/index.d.mts.map +1 -0
- package/dist/adapters/fastify/index.mjs +75 -2
- package/dist/adapters/fastify/index.mjs.map +1 -0
- package/dist/adapters/fetch/index.cjs +58 -0
- package/dist/adapters/fetch/index.d.cts +30 -0
- package/dist/adapters/fetch/index.d.cts.map +1 -0
- package/dist/adapters/fetch/index.d.mts +30 -0
- package/dist/adapters/fetch/index.d.mts.map +1 -0
- package/dist/adapters/fetch/index.mjs +59 -1
- package/dist/adapters/fetch/index.mjs.map +1 -0
- package/dist/adapters/next-app-dir.cjs +3050 -0
- package/dist/adapters/next-app-dir.d.cts +58 -0
- package/dist/adapters/next-app-dir.d.cts.map +1 -0
- package/dist/adapters/next-app-dir.d.mts +58 -0
- package/dist/adapters/next-app-dir.d.mts.map +1 -0
- package/dist/adapters/next-app-dir.mjs +3048 -4
- package/dist/adapters/next-app-dir.mjs.map +1 -0
- package/dist/adapters/next.cjs +41 -0
- package/dist/adapters/next.d.cts +19 -0
- package/dist/adapters/next.d.cts.map +1 -0
- package/dist/adapters/next.d.mts +19 -0
- package/dist/adapters/next.d.mts.map +1 -0
- package/dist/adapters/next.mjs +37 -44
- package/dist/adapters/next.mjs.map +1 -0
- package/dist/adapters/node-http/index.cjs +15 -0
- package/dist/adapters/node-http/index.d.cts +5 -0
- package/dist/adapters/node-http/index.d.mts +5 -0
- package/dist/adapters/node-http/index.mjs +12 -2
- package/dist/adapters/standalone.cjs +52 -0
- package/dist/adapters/standalone.d.cts +33 -0
- package/dist/adapters/standalone.d.cts.map +1 -0
- package/dist/adapters/standalone.d.mts +33 -0
- package/dist/adapters/standalone.d.mts.map +1 -0
- package/dist/adapters/standalone.mjs +40 -34
- package/dist/adapters/standalone.mjs.map +1 -0
- package/dist/adapters/ws.cjs +17 -0
- package/dist/adapters/ws.d.cts +61 -0
- package/dist/adapters/ws.d.cts.map +1 -0
- package/dist/adapters/ws.d.mts +61 -0
- package/dist/adapters/ws.d.mts.map +1 -0
- package/dist/adapters/ws.mjs +14 -539
- package/dist/contentTypeParsers-SN4WL9ze.mjs +13 -0
- package/dist/contentTypeParsers-SN4WL9ze.mjs.map +1 -0
- package/dist/contentTypeParsers-iAFF_pJG.cjs +18 -0
- package/dist/getErrorShape-CsikfkAc.cjs +201 -0
- package/dist/getErrorShape-DyYil4aT.mjs +149 -0
- package/dist/getErrorShape-DyYil4aT.mjs.map +1 -0
- package/dist/http-CWyjOa1l.mjs +0 -0
- package/dist/http-DXy3XyhL.cjs +0 -0
- package/dist/http.cjs +14 -0
- package/dist/http.d.cts +3 -0
- package/dist/http.d.mts +3 -0
- package/dist/http.mjs +9 -8
- package/dist/index.cjs +22 -0
- package/dist/index.d-BiUz7kM_.d.cts +65 -0
- package/dist/index.d-BiUz7kM_.d.cts.map +1 -0
- package/dist/index.d-ChWYhXbd.d.cts +99 -0
- package/dist/index.d-ChWYhXbd.d.cts.map +1 -0
- package/dist/index.d-CvZXeEyR.d.cts +11 -0
- package/dist/index.d-CvZXeEyR.d.cts.map +1 -0
- package/dist/index.d-D4qZxQJh.d.mts +65 -0
- package/dist/index.d-D4qZxQJh.d.mts.map +1 -0
- package/dist/index.d-Ocy_AnXj.d.mts +99 -0
- package/dist/index.d-Ocy_AnXj.d.mts.map +1 -0
- package/dist/index.d-vq_QHko2.d.mts +11 -0
- package/dist/index.d-vq_QHko2.d.mts.map +1 -0
- package/dist/index.d.cts +4 -0
- package/dist/index.d.mts +4 -0
- package/dist/index.mjs +6 -13
- package/dist/initTRPC-COaJMShh.mjs +339 -0
- package/dist/initTRPC-COaJMShh.mjs.map +1 -0
- package/dist/initTRPC-DjEpHmY2.cjs +397 -0
- package/dist/node-http-BPR68yI4.cjs +243 -0
- package/dist/node-http-BUQnHuGI.mjs +221 -0
- package/dist/node-http-BUQnHuGI.mjs.map +1 -0
- package/dist/observable/index.cjs +13 -0
- package/dist/observable/index.d.cts +2 -0
- package/dist/observable/index.d.mts +2 -0
- package/dist/observable/index.mjs +4 -3
- package/dist/observable-B1orLHHI.mjs +155 -0
- package/dist/observable-B1orLHHI.mjs.map +1 -0
- package/dist/observable-BwdrSFZU.cjs +178 -0
- package/dist/observable-C6qq2Ydk.cjs +180 -0
- package/dist/observable-CFXA_tyK.mjs +146 -0
- package/dist/observable-CFXA_tyK.mjs.map +1 -0
- package/dist/parseTRPCMessage-ByQWigsq.cjs +73 -0
- package/dist/parseTRPCMessage-CNyYMSRB.mjs +63 -0
- package/dist/parseTRPCMessage-CNyYMSRB.mjs.map +1 -0
- package/dist/resolveResponse-B2CuaT_1.cjs +2042 -0
- package/dist/resolveResponse-DPbYgJDD.mjs +1912 -0
- package/dist/resolveResponse-DPbYgJDD.mjs.map +1 -0
- package/dist/rpc.cjs +6 -0
- package/dist/rpc.d.cts +3 -0
- package/dist/rpc.d.mts +3 -0
- package/dist/rpc.mjs +4 -5
- package/dist/shared.cjs +5 -0
- package/dist/shared.d.cts +4 -0
- package/dist/shared.d.mts +4 -0
- package/dist/shared.mjs +4 -6
- package/dist/tracked-Dl9sBZxY.cjs +483 -0
- package/dist/tracked-GEWPoL0C.mjs +383 -0
- package/dist/tracked-GEWPoL0C.mjs.map +1 -0
- package/dist/unstable-core-do-not-import-D89CaGtL.mjs +29 -0
- package/dist/unstable-core-do-not-import-D89CaGtL.mjs.map +1 -0
- package/dist/unstable-core-do-not-import-DFQys1IC.cjs +34 -0
- package/dist/unstable-core-do-not-import.cjs +83 -0
- package/dist/unstable-core-do-not-import.d-CSxj_rbP.d.cts +1974 -0
- package/dist/unstable-core-do-not-import.d-CSxj_rbP.d.cts.map +1 -0
- package/dist/unstable-core-do-not-import.d-ptrxwuSa.d.mts +1974 -0
- package/dist/unstable-core-do-not-import.d-ptrxwuSa.d.mts.map +1 -0
- package/dist/unstable-core-do-not-import.d.cts +3 -0
- package/dist/unstable-core-do-not-import.d.mts +3 -0
- package/dist/unstable-core-do-not-import.mjs +11 -30
- package/dist/utils-BHZJcBRv.mjs +137 -0
- package/dist/utils-BHZJcBRv.mjs.map +1 -0
- package/dist/utils-DVO6HZiR.cjs +220 -0
- package/dist/ws-C2nEUNk_.cjs +425 -0
- package/dist/ws-eIVIMTrw.mjs +409 -0
- package/dist/ws-eIVIMTrw.mjs.map +1 -0
- package/http/package.json +1 -0
- package/observable/package.json +1 -0
- package/package.json +131 -55
- package/rpc/package.json +1 -0
- package/shared/package.json +1 -0
- package/src/@trpc/server/index.ts +24 -0
- package/src/unstable-core-do-not-import/http/contentType.ts +5 -4
- package/src/unstable-core-do-not-import/initTRPC.ts +79 -4
- package/src/unstable-core-do-not-import/router.ts +36 -12
- package/src/unstable-core-do-not-import/utils.ts +3 -2
- package/unstable-core-do-not-import/package.json +1 -0
- package/adapters/aws-lambda/index.d.ts +0 -1
- package/adapters/aws-lambda/index.js +0 -1
- package/adapters/express/index.d.ts +0 -1
- package/adapters/express/index.js +0 -1
- package/adapters/fastify/index.d.ts +0 -1
- package/adapters/fastify/index.js +0 -1
- package/adapters/fetch/index.d.ts +0 -1
- package/adapters/fetch/index.js +0 -1
- package/adapters/next/index.d.ts +0 -1
- package/adapters/next/index.js +0 -1
- package/adapters/next-app-dir/index.d.ts +0 -1
- package/adapters/next-app-dir/index.js +0 -1
- package/adapters/node-http/content-type/form-data/index.d.ts +0 -1
- package/adapters/node-http/content-type/form-data/index.js +0 -1
- package/adapters/node-http/content-type/json/index.d.ts +0 -1
- package/adapters/node-http/content-type/json/index.js +0 -1
- package/adapters/node-http/index.d.ts +0 -1
- package/adapters/node-http/index.js +0 -1
- package/adapters/standalone/index.d.ts +0 -1
- package/adapters/standalone/index.js +0 -1
- package/adapters/ws/index.d.ts +0 -1
- package/adapters/ws/index.js +0 -1
- package/dist/@trpc/server/http.d.ts +0 -8
- package/dist/@trpc/server/http.d.ts.map +0 -1
- package/dist/@trpc/server/index.d.ts +0 -65
- package/dist/@trpc/server/index.d.ts.map +0 -1
- package/dist/@trpc/server/rpc.d.ts +0 -3
- package/dist/@trpc/server/rpc.d.ts.map +0 -1
- package/dist/adapters/aws-lambda/getPlanner.d.ts +0 -15
- package/dist/adapters/aws-lambda/getPlanner.d.ts.map +0 -1
- package/dist/adapters/aws-lambda/getPlanner.js +0 -179
- package/dist/adapters/aws-lambda/getPlanner.mjs +0 -177
- package/dist/adapters/aws-lambda/index.d.ts +0 -23
- package/dist/adapters/aws-lambda/index.d.ts.map +0 -1
- package/dist/adapters/aws-lambda/index.js +0 -67
- package/dist/adapters/express.d.ts +0 -15
- package/dist/adapters/express.d.ts.map +0 -1
- package/dist/adapters/express.js +0 -39
- package/dist/adapters/fastify/fastifyRequestHandler.d.ts +0 -22
- package/dist/adapters/fastify/fastifyRequestHandler.d.ts.map +0 -1
- package/dist/adapters/fastify/fastifyRequestHandler.js +0 -49
- package/dist/adapters/fastify/fastifyRequestHandler.mjs +0 -47
- package/dist/adapters/fastify/fastifyTRPCPlugin.d.ts +0 -21
- package/dist/adapters/fastify/fastifyTRPCPlugin.d.ts.map +0 -1
- package/dist/adapters/fastify/fastifyTRPCPlugin.js +0 -56
- package/dist/adapters/fastify/fastifyTRPCPlugin.mjs +0 -54
- package/dist/adapters/fastify/index.d.ts +0 -3
- package/dist/adapters/fastify/index.d.ts.map +0 -1
- package/dist/adapters/fastify/index.js +0 -9
- package/dist/adapters/fetch/fetchRequestHandler.d.ts +0 -13
- package/dist/adapters/fetch/fetchRequestHandler.d.ts.map +0 -1
- package/dist/adapters/fetch/fetchRequestHandler.js +0 -77
- package/dist/adapters/fetch/fetchRequestHandler.mjs +0 -75
- package/dist/adapters/fetch/index.d.ts +0 -3
- package/dist/adapters/fetch/index.d.ts.map +0 -1
- package/dist/adapters/fetch/index.js +0 -7
- package/dist/adapters/fetch/types.d.ts +0 -27
- package/dist/adapters/fetch/types.d.ts.map +0 -1
- package/dist/adapters/next-app-dir/nextAppDirCaller.d.ts +0 -25
- package/dist/adapters/next-app-dir/nextAppDirCaller.d.ts.map +0 -1
- package/dist/adapters/next-app-dir/nextAppDirCaller.js +0 -94
- package/dist/adapters/next-app-dir/nextAppDirCaller.mjs +0 -92
- package/dist/adapters/next-app-dir/notFound.d.ts +0 -7
- package/dist/adapters/next-app-dir/notFound.d.ts.map +0 -1
- package/dist/adapters/next-app-dir/notFound.js +0 -18
- package/dist/adapters/next-app-dir/notFound.mjs +0 -16
- package/dist/adapters/next-app-dir/redirect.d.ts +0 -18
- package/dist/adapters/next-app-dir/redirect.d.ts.map +0 -1
- package/dist/adapters/next-app-dir/redirect.js +0 -49
- package/dist/adapters/next-app-dir/redirect.mjs +0 -46
- package/dist/adapters/next-app-dir/rethrowNextErrors.d.ts +0 -6
- package/dist/adapters/next-app-dir/rethrowNextErrors.d.ts.map +0 -1
- package/dist/adapters/next-app-dir/rethrowNextErrors.js +0 -70
- package/dist/adapters/next-app-dir/rethrowNextErrors.mjs +0 -49
- package/dist/adapters/next-app-dir.d.ts +0 -7
- package/dist/adapters/next-app-dir.d.ts.map +0 -1
- package/dist/adapters/next-app-dir.js +0 -13
- package/dist/adapters/next.d.ts +0 -19
- package/dist/adapters/next.d.ts.map +0 -1
- package/dist/adapters/next.js +0 -51
- package/dist/adapters/node-http/incomingMessageToRequest.d.ts +0 -12
- package/dist/adapters/node-http/incomingMessageToRequest.d.ts.map +0 -1
- package/dist/adapters/node-http/incomingMessageToRequest.js +0 -123
- package/dist/adapters/node-http/incomingMessageToRequest.mjs +0 -120
- package/dist/adapters/node-http/index.d.ts +0 -4
- package/dist/adapters/node-http/index.d.ts.map +0 -1
- package/dist/adapters/node-http/index.js +0 -11
- package/dist/adapters/node-http/nodeHTTPRequestHandler.d.ts +0 -20
- package/dist/adapters/node-http/nodeHTTPRequestHandler.d.ts.map +0 -1
- package/dist/adapters/node-http/nodeHTTPRequestHandler.js +0 -96
- package/dist/adapters/node-http/nodeHTTPRequestHandler.mjs +0 -93
- package/dist/adapters/node-http/types.d.ts +0 -81
- package/dist/adapters/node-http/types.d.ts.map +0 -1
- package/dist/adapters/node-http/writeResponse.d.ts +0 -18
- package/dist/adapters/node-http/writeResponse.d.ts.map +0 -1
- package/dist/adapters/node-http/writeResponse.js +0 -83
- package/dist/adapters/node-http/writeResponse.mjs +0 -80
- package/dist/adapters/standalone.d.ts +0 -36
- package/dist/adapters/standalone.d.ts.map +0 -1
- package/dist/adapters/standalone.js +0 -49
- package/dist/adapters/ws.d.ts +0 -54
- package/dist/adapters/ws.d.ts.map +0 -1
- package/dist/adapters/ws.js +0 -544
- package/dist/http.d.ts +0 -2
- package/dist/http.d.ts.map +0 -1
- package/dist/http.js +0 -19
- package/dist/index.d.ts +0 -2
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js +0 -35
- package/dist/node_modules/.pnpm/@rollup_plugin-typescript@12.1.2_rollup@4.34.8_tslib@2.8.1_typescript@5.8.2/node_modules/tslib/tslib.es6.js +0 -73
- package/dist/observable/behaviorSubject.d.ts +0 -15
- package/dist/observable/behaviorSubject.d.ts.map +0 -1
- package/dist/observable/behaviorSubject.js +0 -40
- package/dist/observable/behaviorSubject.mjs +0 -38
- package/dist/observable/index.d.ts +0 -5
- package/dist/observable/index.d.ts.map +0 -1
- package/dist/observable/index.js +0 -18
- package/dist/observable/observable.d.ts +0 -12
- package/dist/observable/observable.d.ts.map +0 -1
- package/dist/observable/observable.js +0 -180
- package/dist/observable/observable.mjs +0 -175
- package/dist/observable/operators.d.ts +0 -10
- package/dist/observable/operators.d.ts.map +0 -1
- package/dist/observable/operators.js +0 -134
- package/dist/observable/operators.mjs +0 -128
- package/dist/observable/types.d.ts +0 -26
- package/dist/observable/types.d.ts.map +0 -1
- package/dist/rpc.d.ts +0 -2
- package/dist/rpc.d.ts.map +0 -1
- package/dist/rpc.js +0 -13
- package/dist/shared.d.ts +0 -26
- package/dist/shared.d.ts.map +0 -1
- package/dist/shared.js +0 -13
- package/dist/unstable-core-do-not-import/clientish/inference.d.ts +0 -17
- package/dist/unstable-core-do-not-import/clientish/inference.d.ts.map +0 -1
- package/dist/unstable-core-do-not-import/clientish/inferrable.d.ts +0 -36
- package/dist/unstable-core-do-not-import/clientish/inferrable.d.ts.map +0 -1
- package/dist/unstable-core-do-not-import/clientish/serialize.d.ts +0 -69
- package/dist/unstable-core-do-not-import/clientish/serialize.d.ts.map +0 -1
- package/dist/unstable-core-do-not-import/createProxy.d.ts +0 -19
- package/dist/unstable-core-do-not-import/createProxy.d.ts.map +0 -1
- package/dist/unstable-core-do-not-import/createProxy.js +0 -76
- package/dist/unstable-core-do-not-import/createProxy.mjs +0 -73
- package/dist/unstable-core-do-not-import/error/TRPCError.d.ts +0 -13
- package/dist/unstable-core-do-not-import/error/TRPCError.d.ts.map +0 -1
- package/dist/unstable-core-do-not-import/error/TRPCError.js +0 -76
- package/dist/unstable-core-do-not-import/error/TRPCError.mjs +0 -72
- package/dist/unstable-core-do-not-import/error/formatter.d.ts +0 -38
- package/dist/unstable-core-do-not-import/error/formatter.d.ts.map +0 -1
- package/dist/unstable-core-do-not-import/error/formatter.js +0 -7
- package/dist/unstable-core-do-not-import/error/formatter.mjs +0 -5
- package/dist/unstable-core-do-not-import/error/getErrorShape.d.ts +0 -15
- package/dist/unstable-core-do-not-import/error/getErrorShape.d.ts.map +0 -1
- package/dist/unstable-core-do-not-import/error/getErrorShape.js +0 -31
- package/dist/unstable-core-do-not-import/error/getErrorShape.mjs +0 -29
- package/dist/unstable-core-do-not-import/http/abortError.d.ts +0 -5
- package/dist/unstable-core-do-not-import/http/abortError.d.ts.map +0 -1
- package/dist/unstable-core-do-not-import/http/abortError.js +0 -13
- package/dist/unstable-core-do-not-import/http/abortError.mjs +0 -10
- package/dist/unstable-core-do-not-import/http/contentType.d.ts +0 -13
- package/dist/unstable-core-do-not-import/http/contentType.d.ts.map +0 -1
- package/dist/unstable-core-do-not-import/http/contentType.js +0 -229
- package/dist/unstable-core-do-not-import/http/contentType.mjs +0 -227
- package/dist/unstable-core-do-not-import/http/contentTypeParsers.d.ts +0 -14
- package/dist/unstable-core-do-not-import/http/contentTypeParsers.d.ts.map +0 -1
- package/dist/unstable-core-do-not-import/http/contentTypeParsers.js +0 -14
- package/dist/unstable-core-do-not-import/http/contentTypeParsers.mjs +0 -12
- package/dist/unstable-core-do-not-import/http/formDataToObject.d.ts +0 -2
- package/dist/unstable-core-do-not-import/http/formDataToObject.d.ts.map +0 -1
- package/dist/unstable-core-do-not-import/http/formDataToObject.js +0 -40
- package/dist/unstable-core-do-not-import/http/formDataToObject.mjs +0 -38
- package/dist/unstable-core-do-not-import/http/getHTTPStatusCode.d.ts +0 -10
- package/dist/unstable-core-do-not-import/http/getHTTPStatusCode.d.ts.map +0 -1
- package/dist/unstable-core-do-not-import/http/getHTTPStatusCode.js +0 -85
- package/dist/unstable-core-do-not-import/http/getHTTPStatusCode.mjs +0 -78
- package/dist/unstable-core-do-not-import/http/parseConnectionParams.d.ts +0 -4
- package/dist/unstable-core-do-not-import/http/parseConnectionParams.d.ts.map +0 -1
- package/dist/unstable-core-do-not-import/http/parseConnectionParams.js +0 -42
- package/dist/unstable-core-do-not-import/http/parseConnectionParams.mjs +0 -39
- package/dist/unstable-core-do-not-import/http/resolveResponse.d.ts +0 -15
- package/dist/unstable-core-do-not-import/http/resolveResponse.d.ts.map +0 -1
- package/dist/unstable-core-do-not-import/http/resolveResponse.js +0 -572
- package/dist/unstable-core-do-not-import/http/resolveResponse.mjs +0 -570
- package/dist/unstable-core-do-not-import/http/types.d.ts +0 -138
- package/dist/unstable-core-do-not-import/http/types.d.ts.map +0 -1
- package/dist/unstable-core-do-not-import/initTRPC.d.ts +0 -94
- package/dist/unstable-core-do-not-import/initTRPC.d.ts.map +0 -1
- package/dist/unstable-core-do-not-import/initTRPC.js +0 -82
- package/dist/unstable-core-do-not-import/initTRPC.mjs +0 -80
- package/dist/unstable-core-do-not-import/middleware.d.ts +0 -107
- package/dist/unstable-core-do-not-import/middleware.d.ts.map +0 -1
- package/dist/unstable-core-do-not-import/middleware.js +0 -96
- package/dist/unstable-core-do-not-import/middleware.mjs +0 -90
- package/dist/unstable-core-do-not-import/parser.d.ts +0 -44
- package/dist/unstable-core-do-not-import/parser.d.ts.map +0 -1
- package/dist/unstable-core-do-not-import/parser.js +0 -54
- package/dist/unstable-core-do-not-import/parser.mjs +0 -52
- package/dist/unstable-core-do-not-import/procedure.d.ts +0 -77
- package/dist/unstable-core-do-not-import/procedure.d.ts.map +0 -1
- package/dist/unstable-core-do-not-import/procedure.js +0 -9
- package/dist/unstable-core-do-not-import/procedure.mjs +0 -7
- package/dist/unstable-core-do-not-import/procedureBuilder.d.ts +0 -161
- package/dist/unstable-core-do-not-import/procedureBuilder.d.ts.map +0 -1
- package/dist/unstable-core-do-not-import/procedureBuilder.js +0 -201
- package/dist/unstable-core-do-not-import/procedureBuilder.mjs +0 -199
- package/dist/unstable-core-do-not-import/rootConfig.d.ts +0 -98
- package/dist/unstable-core-do-not-import/rootConfig.d.ts.map +0 -1
- package/dist/unstable-core-do-not-import/rootConfig.js +0 -8
- package/dist/unstable-core-do-not-import/rootConfig.mjs +0 -6
- package/dist/unstable-core-do-not-import/router.d.ts +0 -98
- package/dist/unstable-core-do-not-import/router.d.ts.map +0 -1
- package/dist/unstable-core-do-not-import/router.js +0 -285
- package/dist/unstable-core-do-not-import/router.mjs +0 -278
- package/dist/unstable-core-do-not-import/rpc/codes.d.ts +0 -45
- package/dist/unstable-core-do-not-import/rpc/codes.d.ts.map +0 -1
- package/dist/unstable-core-do-not-import/rpc/codes.js +0 -69
- package/dist/unstable-core-do-not-import/rpc/codes.mjs +0 -65
- package/dist/unstable-core-do-not-import/rpc/envelopes.d.ts +0 -109
- package/dist/unstable-core-do-not-import/rpc/envelopes.d.ts.map +0 -1
- package/dist/unstable-core-do-not-import/rpc/index.d.ts +0 -5
- package/dist/unstable-core-do-not-import/rpc/index.d.ts.map +0 -1
- package/dist/unstable-core-do-not-import/rpc/parseTRPCMessage.d.ts +0 -5
- package/dist/unstable-core-do-not-import/rpc/parseTRPCMessage.d.ts.map +0 -1
- package/dist/unstable-core-do-not-import/rpc/parseTRPCMessage.js +0 -63
- package/dist/unstable-core-do-not-import/rpc/parseTRPCMessage.mjs +0 -61
- package/dist/unstable-core-do-not-import/stream/jsonl.d.ts +0 -119
- package/dist/unstable-core-do-not-import/stream/jsonl.d.ts.map +0 -1
- package/dist/unstable-core-do-not-import/stream/jsonl.js +0 -607
- package/dist/unstable-core-do-not-import/stream/jsonl.mjs +0 -603
- package/dist/unstable-core-do-not-import/stream/sse.d.ts +0 -108
- package/dist/unstable-core-do-not-import/stream/sse.d.ts.map +0 -1
- package/dist/unstable-core-do-not-import/stream/sse.js +0 -373
- package/dist/unstable-core-do-not-import/stream/sse.mjs +0 -369
- package/dist/unstable-core-do-not-import/stream/sse.types.d.ts +0 -31
- package/dist/unstable-core-do-not-import/stream/sse.types.d.ts.map +0 -1
- package/dist/unstable-core-do-not-import/stream/tracked.d.ts +0 -31
- package/dist/unstable-core-do-not-import/stream/tracked.d.ts.map +0 -1
- package/dist/unstable-core-do-not-import/stream/tracked.js +0 -29
- package/dist/unstable-core-do-not-import/stream/tracked.mjs +0 -25
- package/dist/unstable-core-do-not-import/stream/utils/asyncIterable.d.ts +0 -17
- package/dist/unstable-core-do-not-import/stream/utils/asyncIterable.d.ts.map +0 -1
- package/dist/unstable-core-do-not-import/stream/utils/asyncIterable.js +0 -174
- package/dist/unstable-core-do-not-import/stream/utils/asyncIterable.mjs +0 -170
- package/dist/unstable-core-do-not-import/stream/utils/createDeferred.d.ts +0 -7
- package/dist/unstable-core-do-not-import/stream/utils/createDeferred.d.ts.map +0 -1
- package/dist/unstable-core-do-not-import/stream/utils/createDeferred.js +0 -17
- package/dist/unstable-core-do-not-import/stream/utils/createDeferred.mjs +0 -15
- package/dist/unstable-core-do-not-import/stream/utils/disposable.d.ts +0 -17
- package/dist/unstable-core-do-not-import/stream/utils/disposable.d.ts.map +0 -1
- package/dist/unstable-core-do-not-import/stream/utils/disposable.js +0 -46
- package/dist/unstable-core-do-not-import/stream/utils/disposable.mjs +0 -43
- package/dist/unstable-core-do-not-import/stream/utils/mergeAsyncIterables.d.ts +0 -17
- package/dist/unstable-core-do-not-import/stream/utils/mergeAsyncIterables.d.ts.map +0 -1
- package/dist/unstable-core-do-not-import/stream/utils/mergeAsyncIterables.js +0 -241
- package/dist/unstable-core-do-not-import/stream/utils/mergeAsyncIterables.mjs +0 -239
- package/dist/unstable-core-do-not-import/stream/utils/readableStreamFrom.d.ts +0 -8
- package/dist/unstable-core-do-not-import/stream/utils/readableStreamFrom.d.ts.map +0 -1
- package/dist/unstable-core-do-not-import/stream/utils/readableStreamFrom.js +0 -25
- package/dist/unstable-core-do-not-import/stream/utils/readableStreamFrom.mjs +0 -23
- package/dist/unstable-core-do-not-import/stream/utils/timerResource.d.ts +0 -5
- package/dist/unstable-core-do-not-import/stream/utils/timerResource.d.ts.map +0 -1
- package/dist/unstable-core-do-not-import/stream/utils/timerResource.js +0 -26
- package/dist/unstable-core-do-not-import/stream/utils/timerResource.mjs +0 -23
- package/dist/unstable-core-do-not-import/stream/utils/withPing.d.ts +0 -7
- package/dist/unstable-core-do-not-import/stream/utils/withPing.d.ts.map +0 -1
- package/dist/unstable-core-do-not-import/stream/utils/withPing.js +0 -130
- package/dist/unstable-core-do-not-import/stream/utils/withPing.mjs +0 -127
- package/dist/unstable-core-do-not-import/transformer.d.ts +0 -99
- package/dist/unstable-core-do-not-import/transformer.d.ts.map +0 -1
- package/dist/unstable-core-do-not-import/transformer.js +0 -106
- package/dist/unstable-core-do-not-import/transformer.mjs +0 -101
- package/dist/unstable-core-do-not-import/types.d.ts +0 -127
- package/dist/unstable-core-do-not-import/types.d.ts.map +0 -1
- package/dist/unstable-core-do-not-import/utils.d.ts +0 -42
- package/dist/unstable-core-do-not-import/utils.d.ts.map +0 -1
- package/dist/unstable-core-do-not-import/utils.js +0 -96
- package/dist/unstable-core-do-not-import/utils.mjs +0 -83
- package/dist/unstable-core-do-not-import.d.ts +0 -47
- package/dist/unstable-core-do-not-import.d.ts.map +0 -1
- package/dist/unstable-core-do-not-import.js +0 -109
- package/dist/vendor/cookie-es/set-cookie/split.d.ts +0 -18
- package/dist/vendor/cookie-es/set-cookie/split.d.ts.map +0 -1
- package/dist/vendor/cookie-es/set-cookie/split.js +0 -79
- package/dist/vendor/cookie-es/set-cookie/split.mjs +0 -77
- package/dist/vendor/standard-schema-v1/error.d.ts +0 -13
- package/dist/vendor/standard-schema-v1/error.d.ts.map +0 -1
- package/dist/vendor/standard-schema-v1/error.js +0 -28
- package/dist/vendor/standard-schema-v1/error.mjs +0 -26
- package/dist/vendor/standard-schema-v1/spec.d.ts +0 -60
- package/dist/vendor/standard-schema-v1/spec.d.ts.map +0 -1
- package/dist/vendor/unpromise/index.d.ts +0 -3
- package/dist/vendor/unpromise/index.d.ts.map +0 -1
- package/dist/vendor/unpromise/types.d.ts +0 -28
- package/dist/vendor/unpromise/types.d.ts.map +0 -1
- package/dist/vendor/unpromise/unpromise.d.ts +0 -121
- package/dist/vendor/unpromise/unpromise.d.ts.map +0 -1
- package/dist/vendor/unpromise/unpromise.js +0 -292
- package/dist/vendor/unpromise/unpromise.mjs +0 -289
- package/http/index.d.ts +0 -1
- package/http/index.js +0 -1
- package/observable/index.d.ts +0 -1
- package/observable/index.js +0 -1
- package/rpc/index.d.ts +0 -1
- package/rpc/index.js +0 -1
- package/shared/index.d.ts +0 -1
- package/shared/index.js +0 -1
- package/unstable-core-do-not-import/index.d.ts +0 -1
- package/unstable-core-do-not-import/index.js +0 -1
|
@@ -0,0 +1,1912 @@
|
|
|
1
|
+
import { getErrorShape, getHTTPStatusCode } from "./getErrorShape-DyYil4aT.mjs";
|
|
2
|
+
import { TRPCError, getProcedureAtPath, getTRPCErrorFromUnknown, isTrackedEnvelope, transformTRPCResponse } from "./tracked-GEWPoL0C.mjs";
|
|
3
|
+
import { identity, isAsyncIterable, isFunction, isObject, run } from "./utils-BHZJcBRv.mjs";
|
|
4
|
+
import { isObservable, observableToAsyncIterable } from "./observable-B1orLHHI.mjs";
|
|
5
|
+
|
|
6
|
+
//#region rolldown:runtime
|
|
7
|
+
var __create = Object.create;
|
|
8
|
+
var __defProp = Object.defineProperty;
|
|
9
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
10
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
11
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
12
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
13
|
+
var __commonJS = (cb, mod) => function() {
|
|
14
|
+
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
|
|
15
|
+
};
|
|
16
|
+
var __copyProps = (to, from, except, desc) => {
|
|
17
|
+
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
18
|
+
key = keys[i];
|
|
19
|
+
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
20
|
+
get: ((k) => from[k]).bind(null, key),
|
|
21
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
return to;
|
|
25
|
+
};
|
|
26
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
27
|
+
value: mod,
|
|
28
|
+
enumerable: true
|
|
29
|
+
}) : target, mod));
|
|
30
|
+
|
|
31
|
+
//#endregion
|
|
32
|
+
//#region src/unstable-core-do-not-import/http/parseConnectionParams.ts
|
|
33
|
+
function parseConnectionParamsFromUnknown(parsed) {
|
|
34
|
+
try {
|
|
35
|
+
if (parsed === null) return null;
|
|
36
|
+
if (!isObject(parsed)) throw new Error("Expected object");
|
|
37
|
+
const nonStringValues = Object.entries(parsed).filter(([_key, value]) => typeof value !== "string");
|
|
38
|
+
if (nonStringValues.length > 0) throw new Error(`Expected connectionParams to be string values. Got ${nonStringValues.map(([key, value]) => `${key}: ${typeof value}`).join(", ")}`);
|
|
39
|
+
return parsed;
|
|
40
|
+
} catch (cause) {
|
|
41
|
+
throw new TRPCError({
|
|
42
|
+
code: "PARSE_ERROR",
|
|
43
|
+
message: "Invalid connection params shape",
|
|
44
|
+
cause
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
function parseConnectionParamsFromString(str) {
|
|
49
|
+
let parsed;
|
|
50
|
+
try {
|
|
51
|
+
parsed = JSON.parse(str);
|
|
52
|
+
} catch (cause) {
|
|
53
|
+
throw new TRPCError({
|
|
54
|
+
code: "PARSE_ERROR",
|
|
55
|
+
message: "Not JSON-parsable query params",
|
|
56
|
+
cause
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
return parseConnectionParamsFromUnknown(parsed);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
//#endregion
|
|
63
|
+
//#region src/unstable-core-do-not-import/http/contentType.ts
|
|
64
|
+
/**
|
|
65
|
+
* Memoize a function that takes no arguments
|
|
66
|
+
* @internal
|
|
67
|
+
*/
|
|
68
|
+
function memo(fn) {
|
|
69
|
+
let promise = null;
|
|
70
|
+
const sym = Symbol.for("@trpc/server/http/memo");
|
|
71
|
+
let value = sym;
|
|
72
|
+
return {
|
|
73
|
+
read: async () => {
|
|
74
|
+
if (value !== sym) return value;
|
|
75
|
+
promise ??= fn().catch((cause) => {
|
|
76
|
+
if (cause instanceof TRPCError) throw cause;
|
|
77
|
+
throw new TRPCError({
|
|
78
|
+
code: "BAD_REQUEST",
|
|
79
|
+
message: cause instanceof Error ? cause.message : "Invalid input",
|
|
80
|
+
cause
|
|
81
|
+
});
|
|
82
|
+
});
|
|
83
|
+
value = await promise;
|
|
84
|
+
promise = null;
|
|
85
|
+
return value;
|
|
86
|
+
},
|
|
87
|
+
result: () => {
|
|
88
|
+
return value !== sym ? value : void 0;
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
const jsonContentTypeHandler = {
|
|
93
|
+
isMatch(req) {
|
|
94
|
+
return !!req.headers.get("content-type")?.startsWith("application/json");
|
|
95
|
+
},
|
|
96
|
+
async parse(opts) {
|
|
97
|
+
const { req } = opts;
|
|
98
|
+
const isBatchCall = opts.searchParams.get("batch") === "1";
|
|
99
|
+
const paths = isBatchCall ? opts.path.split(",") : [opts.path];
|
|
100
|
+
const getInputs = memo(async () => {
|
|
101
|
+
let inputs = void 0;
|
|
102
|
+
if (req.method === "GET") {
|
|
103
|
+
const queryInput = opts.searchParams.get("input");
|
|
104
|
+
if (queryInput) inputs = JSON.parse(queryInput);
|
|
105
|
+
} else inputs = await req.json();
|
|
106
|
+
if (inputs === void 0) return {};
|
|
107
|
+
if (!isBatchCall) return { 0: opts.router._def._config.transformer.input.deserialize(inputs) };
|
|
108
|
+
if (!isObject(inputs)) throw new TRPCError({
|
|
109
|
+
code: "BAD_REQUEST",
|
|
110
|
+
message: "\"input\" needs to be an object when doing a batch call"
|
|
111
|
+
});
|
|
112
|
+
const acc = {};
|
|
113
|
+
for (const index of paths.keys()) {
|
|
114
|
+
const input = inputs[index];
|
|
115
|
+
if (input !== void 0) acc[index] = opts.router._def._config.transformer.input.deserialize(input);
|
|
116
|
+
}
|
|
117
|
+
return acc;
|
|
118
|
+
});
|
|
119
|
+
const calls = await Promise.all(paths.map(async (path, index) => {
|
|
120
|
+
const procedure = await getProcedureAtPath(opts.router, path);
|
|
121
|
+
return {
|
|
122
|
+
path,
|
|
123
|
+
procedure,
|
|
124
|
+
getRawInput: async () => {
|
|
125
|
+
const inputs = await getInputs.read();
|
|
126
|
+
let input = inputs[index];
|
|
127
|
+
if (procedure?._def.type === "subscription") {
|
|
128
|
+
const lastEventId = opts.headers.get("last-event-id") ?? opts.searchParams.get("lastEventId") ?? opts.searchParams.get("Last-Event-Id");
|
|
129
|
+
if (lastEventId) if (isObject(input)) input = {
|
|
130
|
+
...input,
|
|
131
|
+
lastEventId
|
|
132
|
+
};
|
|
133
|
+
else input ??= { lastEventId };
|
|
134
|
+
}
|
|
135
|
+
return input;
|
|
136
|
+
},
|
|
137
|
+
result: () => {
|
|
138
|
+
return getInputs.result()?.[index];
|
|
139
|
+
}
|
|
140
|
+
};
|
|
141
|
+
}));
|
|
142
|
+
const types = new Set(calls.map((call) => call.procedure?._def.type).filter(Boolean));
|
|
143
|
+
/* istanbul ignore if -- @preserve */
|
|
144
|
+
if (types.size > 1) throw new TRPCError({
|
|
145
|
+
code: "BAD_REQUEST",
|
|
146
|
+
message: `Cannot mix procedure types in call: ${Array.from(types).join(", ")}`
|
|
147
|
+
});
|
|
148
|
+
const type = types.values().next().value ?? "unknown";
|
|
149
|
+
const connectionParamsStr = opts.searchParams.get("connectionParams");
|
|
150
|
+
const info = {
|
|
151
|
+
isBatchCall,
|
|
152
|
+
accept: req.headers.get("trpc-accept"),
|
|
153
|
+
calls,
|
|
154
|
+
type,
|
|
155
|
+
connectionParams: connectionParamsStr === null ? null : parseConnectionParamsFromString(connectionParamsStr),
|
|
156
|
+
signal: req.signal,
|
|
157
|
+
url: opts.url
|
|
158
|
+
};
|
|
159
|
+
return info;
|
|
160
|
+
}
|
|
161
|
+
};
|
|
162
|
+
const formDataContentTypeHandler = {
|
|
163
|
+
isMatch(req) {
|
|
164
|
+
return !!req.headers.get("content-type")?.startsWith("multipart/form-data");
|
|
165
|
+
},
|
|
166
|
+
async parse(opts) {
|
|
167
|
+
const { req } = opts;
|
|
168
|
+
if (req.method !== "POST") throw new TRPCError({
|
|
169
|
+
code: "METHOD_NOT_SUPPORTED",
|
|
170
|
+
message: "Only POST requests are supported for multipart/form-data requests"
|
|
171
|
+
});
|
|
172
|
+
const getInputs = memo(async () => {
|
|
173
|
+
const fd = await req.formData();
|
|
174
|
+
return fd;
|
|
175
|
+
});
|
|
176
|
+
const procedure = await getProcedureAtPath(opts.router, opts.path);
|
|
177
|
+
return {
|
|
178
|
+
accept: null,
|
|
179
|
+
calls: [{
|
|
180
|
+
path: opts.path,
|
|
181
|
+
getRawInput: getInputs.read,
|
|
182
|
+
result: getInputs.result,
|
|
183
|
+
procedure
|
|
184
|
+
}],
|
|
185
|
+
isBatchCall: false,
|
|
186
|
+
type: "mutation",
|
|
187
|
+
connectionParams: null,
|
|
188
|
+
signal: req.signal,
|
|
189
|
+
url: opts.url
|
|
190
|
+
};
|
|
191
|
+
}
|
|
192
|
+
};
|
|
193
|
+
const octetStreamContentTypeHandler = {
|
|
194
|
+
isMatch(req) {
|
|
195
|
+
return !!req.headers.get("content-type")?.startsWith("application/octet-stream");
|
|
196
|
+
},
|
|
197
|
+
async parse(opts) {
|
|
198
|
+
const { req } = opts;
|
|
199
|
+
if (req.method !== "POST") throw new TRPCError({
|
|
200
|
+
code: "METHOD_NOT_SUPPORTED",
|
|
201
|
+
message: "Only POST requests are supported for application/octet-stream requests"
|
|
202
|
+
});
|
|
203
|
+
const getInputs = memo(async () => {
|
|
204
|
+
return req.body;
|
|
205
|
+
});
|
|
206
|
+
return {
|
|
207
|
+
calls: [{
|
|
208
|
+
path: opts.path,
|
|
209
|
+
getRawInput: getInputs.read,
|
|
210
|
+
result: getInputs.result,
|
|
211
|
+
procedure: await getProcedureAtPath(opts.router, opts.path)
|
|
212
|
+
}],
|
|
213
|
+
isBatchCall: false,
|
|
214
|
+
accept: null,
|
|
215
|
+
type: "mutation",
|
|
216
|
+
connectionParams: null,
|
|
217
|
+
signal: req.signal,
|
|
218
|
+
url: opts.url
|
|
219
|
+
};
|
|
220
|
+
}
|
|
221
|
+
};
|
|
222
|
+
const handlers = [
|
|
223
|
+
jsonContentTypeHandler,
|
|
224
|
+
formDataContentTypeHandler,
|
|
225
|
+
octetStreamContentTypeHandler
|
|
226
|
+
];
|
|
227
|
+
function getContentTypeHandler(req) {
|
|
228
|
+
const handler = handlers.find((handler$1) => handler$1.isMatch(req));
|
|
229
|
+
if (handler) return handler;
|
|
230
|
+
if (!handler && req.method === "GET") return jsonContentTypeHandler;
|
|
231
|
+
throw new TRPCError({
|
|
232
|
+
code: "UNSUPPORTED_MEDIA_TYPE",
|
|
233
|
+
message: req.headers.has("content-type") ? `Unsupported content-type "${req.headers.get("content-type")}` : "Missing content-type header"
|
|
234
|
+
});
|
|
235
|
+
}
|
|
236
|
+
async function getRequestInfo(opts) {
|
|
237
|
+
const handler = getContentTypeHandler(opts.req);
|
|
238
|
+
return await handler.parse(opts);
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
//#endregion
|
|
242
|
+
//#region src/unstable-core-do-not-import/http/abortError.ts
|
|
243
|
+
function isAbortError(error) {
|
|
244
|
+
return isObject(error) && error["name"] === "AbortError";
|
|
245
|
+
}
|
|
246
|
+
function throwAbortError(message = "AbortError") {
|
|
247
|
+
throw new DOMException(message, "AbortError");
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
//#endregion
|
|
251
|
+
//#region src/vendor/unpromise/unpromise.ts
|
|
252
|
+
/** Memory safe (weakmapped) cache of the ProxyPromise for each Promise,
|
|
253
|
+
* which is retained for the lifetime of the original Promise.
|
|
254
|
+
*/
|
|
255
|
+
const subscribableCache = /* @__PURE__ */ new WeakMap();
|
|
256
|
+
/** A NOOP function allowing a consistent interface for settled
|
|
257
|
+
* SubscribedPromises (settled promises are not subscribed - they resolve
|
|
258
|
+
* immediately). */
|
|
259
|
+
const NOOP = () => {};
|
|
260
|
+
/**
|
|
261
|
+
* Every `Promise<T>` can be shadowed by a single `ProxyPromise<T>`. It is
|
|
262
|
+
* created once, cached and reused throughout the lifetime of the Promise. Get a
|
|
263
|
+
* Promise's ProxyPromise using `Unpromise.proxy(promise)`.
|
|
264
|
+
*
|
|
265
|
+
* The `ProxyPromise<T>` attaches handlers to the original `Promise<T>`
|
|
266
|
+
* `.then()` and `.catch()` just once. Promises derived from it use a
|
|
267
|
+
* subscription- (and unsubscription-) based mechanism that monitors these
|
|
268
|
+
* handlers.
|
|
269
|
+
*
|
|
270
|
+
* Every time you call `.subscribe()`, `.then()` `.catch()` or `.finally()` on a
|
|
271
|
+
* `ProxyPromise<T>` it returns a `SubscribedPromise<T>` having an additional
|
|
272
|
+
* `unsubscribe()` method. Calling `unsubscribe()` detaches reference chains
|
|
273
|
+
* from the original, potentially long-lived Promise, eliminating memory leaks.
|
|
274
|
+
*
|
|
275
|
+
* This approach can eliminate the memory leaks that otherwise come about from
|
|
276
|
+
* repeated `race()` or `any()` calls invoking `.then()` and `.catch()` multiple
|
|
277
|
+
* times on the same long-lived native Promise (subscriptions which can never be
|
|
278
|
+
* cleaned up).
|
|
279
|
+
*
|
|
280
|
+
* `Unpromise.race(promises)` is a reference implementation of `Promise.race`
|
|
281
|
+
* avoiding memory leaks when using long-lived unsettled Promises.
|
|
282
|
+
*
|
|
283
|
+
* `Unpromise.any(promises)` is a reference implementation of `Promise.any`
|
|
284
|
+
* avoiding memory leaks when using long-lived unsettled Promises.
|
|
285
|
+
*
|
|
286
|
+
* `Unpromise.resolve(promise)` returns an ephemeral `SubscribedPromise<T>` for
|
|
287
|
+
* any given `Promise<T>` facilitating arbitrary async/await patterns. Behind
|
|
288
|
+
* the scenes, `resolve` is implemented simply as
|
|
289
|
+
* `Unpromise.proxy(promise).subscribe()`. Don't forget to call `.unsubscribe()`
|
|
290
|
+
* to tidy up!
|
|
291
|
+
*
|
|
292
|
+
*/
|
|
293
|
+
var Unpromise = class Unpromise {
|
|
294
|
+
/** INSTANCE IMPLEMENTATION */
|
|
295
|
+
/** The promise shadowed by this Unpromise<T> */
|
|
296
|
+
promise;
|
|
297
|
+
/** Promises expecting eventual settlement (unless unsubscribed first). This list is deleted
|
|
298
|
+
* after the original promise settles - no further notifications will be issued. */
|
|
299
|
+
subscribers = [];
|
|
300
|
+
/** The Promise's settlement (recorded when it fulfils or rejects). This is consulted when
|
|
301
|
+
* calling .subscribe() .then() .catch() .finally() to see if an immediately-resolving Promise
|
|
302
|
+
* can be returned, and therefore subscription can be bypassed. */
|
|
303
|
+
settlement = null;
|
|
304
|
+
constructor(arg) {
|
|
305
|
+
if (typeof arg === "function") this.promise = new Promise(arg);
|
|
306
|
+
else this.promise = arg;
|
|
307
|
+
const thenReturn = this.promise.then((value) => {
|
|
308
|
+
const { subscribers } = this;
|
|
309
|
+
this.subscribers = null;
|
|
310
|
+
this.settlement = {
|
|
311
|
+
status: "fulfilled",
|
|
312
|
+
value
|
|
313
|
+
};
|
|
314
|
+
subscribers?.forEach(({ resolve }) => {
|
|
315
|
+
resolve(value);
|
|
316
|
+
});
|
|
317
|
+
});
|
|
318
|
+
if ("catch" in thenReturn) thenReturn.catch((reason) => {
|
|
319
|
+
const { subscribers } = this;
|
|
320
|
+
this.subscribers = null;
|
|
321
|
+
this.settlement = {
|
|
322
|
+
status: "rejected",
|
|
323
|
+
reason
|
|
324
|
+
};
|
|
325
|
+
subscribers?.forEach(({ reject }) => {
|
|
326
|
+
reject(reason);
|
|
327
|
+
});
|
|
328
|
+
});
|
|
329
|
+
}
|
|
330
|
+
/** Create a promise that mitigates uncontrolled subscription to a long-lived
|
|
331
|
+
* Promise via .then() and .catch() - otherwise a source of memory leaks.
|
|
332
|
+
*
|
|
333
|
+
* The returned promise has an `unsubscribe()` method which can be called when
|
|
334
|
+
* the Promise is no longer being tracked by application logic, and which
|
|
335
|
+
* ensures that there is no reference chain from the original promise to the
|
|
336
|
+
* new one, and therefore no memory leak.
|
|
337
|
+
*
|
|
338
|
+
* If original promise has not yet settled, this adds a new unique promise
|
|
339
|
+
* that listens to then/catch events, along with an `unsubscribe()` method to
|
|
340
|
+
* detach it.
|
|
341
|
+
*
|
|
342
|
+
* If original promise has settled, then creates a new Promise.resolve() or
|
|
343
|
+
* Promise.reject() and provided unsubscribe is a noop.
|
|
344
|
+
*
|
|
345
|
+
* If you call `unsubscribe()` before the returned Promise has settled, it
|
|
346
|
+
* will never settle.
|
|
347
|
+
*/
|
|
348
|
+
subscribe() {
|
|
349
|
+
let promise;
|
|
350
|
+
let unsubscribe;
|
|
351
|
+
const { settlement } = this;
|
|
352
|
+
if (settlement === null) {
|
|
353
|
+
if (this.subscribers === null) throw new Error("Unpromise settled but still has subscribers");
|
|
354
|
+
const subscriber = withResolvers();
|
|
355
|
+
this.subscribers = listWithMember(this.subscribers, subscriber);
|
|
356
|
+
promise = subscriber.promise;
|
|
357
|
+
unsubscribe = () => {
|
|
358
|
+
if (this.subscribers !== null) this.subscribers = listWithoutMember(this.subscribers, subscriber);
|
|
359
|
+
};
|
|
360
|
+
} else {
|
|
361
|
+
const { status } = settlement;
|
|
362
|
+
if (status === "fulfilled") promise = Promise.resolve(settlement.value);
|
|
363
|
+
else promise = Promise.reject(settlement.reason);
|
|
364
|
+
unsubscribe = NOOP;
|
|
365
|
+
}
|
|
366
|
+
return Object.assign(promise, { unsubscribe });
|
|
367
|
+
}
|
|
368
|
+
/** STANDARD PROMISE METHODS (but returning a SubscribedPromise) */
|
|
369
|
+
then(onfulfilled, onrejected) {
|
|
370
|
+
const subscribed = this.subscribe();
|
|
371
|
+
const { unsubscribe } = subscribed;
|
|
372
|
+
return Object.assign(subscribed.then(onfulfilled, onrejected), { unsubscribe });
|
|
373
|
+
}
|
|
374
|
+
catch(onrejected) {
|
|
375
|
+
const subscribed = this.subscribe();
|
|
376
|
+
const { unsubscribe } = subscribed;
|
|
377
|
+
return Object.assign(subscribed.catch(onrejected), { unsubscribe });
|
|
378
|
+
}
|
|
379
|
+
finally(onfinally) {
|
|
380
|
+
const subscribed = this.subscribe();
|
|
381
|
+
const { unsubscribe } = subscribed;
|
|
382
|
+
return Object.assign(subscribed.finally(onfinally), { unsubscribe });
|
|
383
|
+
}
|
|
384
|
+
/** TOSTRING SUPPORT */
|
|
385
|
+
[Symbol.toStringTag] = "Unpromise";
|
|
386
|
+
/** Unpromise STATIC METHODS */
|
|
387
|
+
/** Create or Retrieve the proxy Unpromise (a re-used Unpromise for the VM lifetime
|
|
388
|
+
* of the provided Promise reference) */
|
|
389
|
+
static proxy(promise) {
|
|
390
|
+
const cached = Unpromise.getSubscribablePromise(promise);
|
|
391
|
+
return typeof cached !== "undefined" ? cached : Unpromise.createSubscribablePromise(promise);
|
|
392
|
+
}
|
|
393
|
+
/** Create and store an Unpromise keyed by an original Promise. */
|
|
394
|
+
static createSubscribablePromise(promise) {
|
|
395
|
+
const created = new Unpromise(promise);
|
|
396
|
+
subscribableCache.set(promise, created);
|
|
397
|
+
subscribableCache.set(created, created);
|
|
398
|
+
return created;
|
|
399
|
+
}
|
|
400
|
+
/** Retrieve a previously-created Unpromise keyed by an original Promise. */
|
|
401
|
+
static getSubscribablePromise(promise) {
|
|
402
|
+
return subscribableCache.get(promise);
|
|
403
|
+
}
|
|
404
|
+
/** Promise STATIC METHODS */
|
|
405
|
+
/** Lookup the Unpromise for this promise, and derive a SubscribedPromise from
|
|
406
|
+
* it (that can be later unsubscribed to eliminate Memory leaks) */
|
|
407
|
+
static resolve(value) {
|
|
408
|
+
const promise = typeof value === "object" && value !== null && "then" in value && typeof value.then === "function" ? value : Promise.resolve(value);
|
|
409
|
+
return Unpromise.proxy(promise).subscribe();
|
|
410
|
+
}
|
|
411
|
+
static async any(values) {
|
|
412
|
+
const valuesArray = Array.isArray(values) ? values : [...values];
|
|
413
|
+
const subscribedPromises = valuesArray.map(Unpromise.resolve);
|
|
414
|
+
try {
|
|
415
|
+
return await Promise.any(subscribedPromises);
|
|
416
|
+
} finally {
|
|
417
|
+
subscribedPromises.forEach(({ unsubscribe }) => {
|
|
418
|
+
unsubscribe();
|
|
419
|
+
});
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
static async race(values) {
|
|
423
|
+
const valuesArray = Array.isArray(values) ? values : [...values];
|
|
424
|
+
const subscribedPromises = valuesArray.map(Unpromise.resolve);
|
|
425
|
+
try {
|
|
426
|
+
return await Promise.race(subscribedPromises);
|
|
427
|
+
} finally {
|
|
428
|
+
subscribedPromises.forEach(({ unsubscribe }) => {
|
|
429
|
+
unsubscribe();
|
|
430
|
+
});
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
/** Create a race of SubscribedPromises that will fulfil to a single winning
|
|
434
|
+
* Promise (in a 1-Tuple). Eliminates memory leaks from long-lived promises
|
|
435
|
+
* accumulating .then() and .catch() subscribers. Allows simple logic to
|
|
436
|
+
* consume the result, like...
|
|
437
|
+
* ```ts
|
|
438
|
+
* const [ winner ] = await Unpromise.race([ promiseA, promiseB ]);
|
|
439
|
+
* if(winner === promiseB){
|
|
440
|
+
* const result = await promiseB;
|
|
441
|
+
* // do the thing
|
|
442
|
+
* }
|
|
443
|
+
* ```
|
|
444
|
+
* */
|
|
445
|
+
static async raceReferences(promises) {
|
|
446
|
+
const selfPromises = promises.map(resolveSelfTuple);
|
|
447
|
+
try {
|
|
448
|
+
return await Promise.race(selfPromises);
|
|
449
|
+
} finally {
|
|
450
|
+
for (const promise of selfPromises) promise.unsubscribe();
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
};
|
|
454
|
+
/** Promises a 1-tuple containing the original promise when it resolves. Allows
|
|
455
|
+
* awaiting the eventual Promise ***reference*** (easy to destructure and
|
|
456
|
+
* exactly compare with ===). Avoids resolving to the Promise ***value*** (which
|
|
457
|
+
* may be ambiguous and therefore hard to identify as the winner of a race).
|
|
458
|
+
* You can call unsubscribe on the Promise to mitigate memory leaks.
|
|
459
|
+
* */
|
|
460
|
+
function resolveSelfTuple(promise) {
|
|
461
|
+
return Unpromise.proxy(promise).then(() => [promise]);
|
|
462
|
+
}
|
|
463
|
+
/** VENDORED (Future) PROMISE UTILITIES */
|
|
464
|
+
/** Reference implementation of https://github.com/tc39/proposal-promise-with-resolvers */
|
|
465
|
+
function withResolvers() {
|
|
466
|
+
let resolve;
|
|
467
|
+
let reject;
|
|
468
|
+
const promise = new Promise((_resolve, _reject) => {
|
|
469
|
+
resolve = _resolve;
|
|
470
|
+
reject = _reject;
|
|
471
|
+
});
|
|
472
|
+
return {
|
|
473
|
+
promise,
|
|
474
|
+
resolve,
|
|
475
|
+
reject
|
|
476
|
+
};
|
|
477
|
+
}
|
|
478
|
+
/** IMMUTABLE LIST OPERATIONS */
|
|
479
|
+
function listWithMember(arr, member) {
|
|
480
|
+
return [...arr, member];
|
|
481
|
+
}
|
|
482
|
+
function listWithoutIndex(arr, index) {
|
|
483
|
+
return [...arr.slice(0, index), ...arr.slice(index + 1)];
|
|
484
|
+
}
|
|
485
|
+
function listWithoutMember(arr, member) {
|
|
486
|
+
const index = arr.indexOf(member);
|
|
487
|
+
if (index !== -1) return listWithoutIndex(arr, index);
|
|
488
|
+
return arr;
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
//#endregion
|
|
492
|
+
//#region src/unstable-core-do-not-import/stream/utils/disposable.ts
|
|
493
|
+
Symbol.dispose ??= Symbol();
|
|
494
|
+
Symbol.asyncDispose ??= Symbol();
|
|
495
|
+
/**
|
|
496
|
+
* Takes a value and a dispose function and returns a new object that implements the Disposable interface.
|
|
497
|
+
* The returned object is the original value augmented with a Symbol.dispose method.
|
|
498
|
+
* @param thing The value to make disposable
|
|
499
|
+
* @param dispose Function to call when disposing the resource
|
|
500
|
+
* @returns The original value with Symbol.dispose method added
|
|
501
|
+
*/
|
|
502
|
+
function makeResource(thing, dispose) {
|
|
503
|
+
const it = thing;
|
|
504
|
+
const existing = it[Symbol.dispose];
|
|
505
|
+
it[Symbol.dispose] = () => {
|
|
506
|
+
dispose();
|
|
507
|
+
existing?.();
|
|
508
|
+
};
|
|
509
|
+
return it;
|
|
510
|
+
}
|
|
511
|
+
/**
|
|
512
|
+
* Takes a value and an async dispose function and returns a new object that implements the AsyncDisposable interface.
|
|
513
|
+
* The returned object is the original value augmented with a Symbol.asyncDispose method.
|
|
514
|
+
* @param thing The value to make async disposable
|
|
515
|
+
* @param dispose Async function to call when disposing the resource
|
|
516
|
+
* @returns The original value with Symbol.asyncDispose method added
|
|
517
|
+
*/
|
|
518
|
+
function makeAsyncResource(thing, dispose) {
|
|
519
|
+
const it = thing;
|
|
520
|
+
const existing = it[Symbol.asyncDispose];
|
|
521
|
+
it[Symbol.asyncDispose] = async () => {
|
|
522
|
+
await dispose();
|
|
523
|
+
await existing?.();
|
|
524
|
+
};
|
|
525
|
+
return it;
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
//#endregion
|
|
529
|
+
//#region src/unstable-core-do-not-import/stream/utils/timerResource.ts
|
|
530
|
+
const disposablePromiseTimerResult = Symbol();
|
|
531
|
+
function timerResource(ms) {
|
|
532
|
+
let timer = null;
|
|
533
|
+
return makeResource({ start() {
|
|
534
|
+
if (timer) throw new Error("Timer already started");
|
|
535
|
+
const promise = new Promise((resolve) => {
|
|
536
|
+
timer = setTimeout(() => resolve(disposablePromiseTimerResult), ms);
|
|
537
|
+
});
|
|
538
|
+
return promise;
|
|
539
|
+
} }, () => {
|
|
540
|
+
if (timer) clearTimeout(timer);
|
|
541
|
+
});
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
//#endregion
|
|
545
|
+
//#region ../../node_modules/.pnpm/@oxc-project+runtime@0.72.2/node_modules/@oxc-project/runtime/src/helpers/usingCtx.js
|
|
546
|
+
var require_usingCtx = __commonJS({ "../../node_modules/.pnpm/@oxc-project+runtime@0.72.2/node_modules/@oxc-project/runtime/src/helpers/usingCtx.js"(exports, module) {
|
|
547
|
+
function _usingCtx() {
|
|
548
|
+
var r = "function" == typeof SuppressedError ? SuppressedError : function(r$1, e$1) {
|
|
549
|
+
var n$1 = Error();
|
|
550
|
+
return n$1.name = "SuppressedError", n$1.error = r$1, n$1.suppressed = e$1, n$1;
|
|
551
|
+
}, e = {}, n = [];
|
|
552
|
+
function using(r$1, e$1) {
|
|
553
|
+
if (null != e$1) {
|
|
554
|
+
if (Object(e$1) !== e$1) throw new TypeError("using declarations can only be used with objects, functions, null, or undefined.");
|
|
555
|
+
if (r$1) var o = e$1[Symbol.asyncDispose || Symbol["for"]("Symbol.asyncDispose")];
|
|
556
|
+
if (void 0 === o && (o = e$1[Symbol.dispose || Symbol["for"]("Symbol.dispose")], r$1)) var t = o;
|
|
557
|
+
if ("function" != typeof o) throw new TypeError("Object is not disposable.");
|
|
558
|
+
t && (o = function o$1() {
|
|
559
|
+
try {
|
|
560
|
+
t.call(e$1);
|
|
561
|
+
} catch (r$2) {
|
|
562
|
+
return Promise.reject(r$2);
|
|
563
|
+
}
|
|
564
|
+
}), n.push({
|
|
565
|
+
v: e$1,
|
|
566
|
+
d: o,
|
|
567
|
+
a: r$1
|
|
568
|
+
});
|
|
569
|
+
} else r$1 && n.push({
|
|
570
|
+
d: e$1,
|
|
571
|
+
a: r$1
|
|
572
|
+
});
|
|
573
|
+
return e$1;
|
|
574
|
+
}
|
|
575
|
+
return {
|
|
576
|
+
e,
|
|
577
|
+
u: using.bind(null, !1),
|
|
578
|
+
a: using.bind(null, !0),
|
|
579
|
+
d: function d() {
|
|
580
|
+
var o, t = this.e, s = 0;
|
|
581
|
+
function next() {
|
|
582
|
+
for (; o = n.pop();) try {
|
|
583
|
+
if (!o.a && 1 === s) return s = 0, n.push(o), Promise.resolve().then(next);
|
|
584
|
+
if (o.d) {
|
|
585
|
+
var r$1 = o.d.call(o.v);
|
|
586
|
+
if (o.a) return s |= 2, Promise.resolve(r$1).then(next, err);
|
|
587
|
+
} else s |= 1;
|
|
588
|
+
} catch (r$2) {
|
|
589
|
+
return err(r$2);
|
|
590
|
+
}
|
|
591
|
+
if (1 === s) return t !== e ? Promise.reject(t) : Promise.resolve();
|
|
592
|
+
if (t !== e) throw t;
|
|
593
|
+
}
|
|
594
|
+
function err(n$1) {
|
|
595
|
+
return t = t !== e ? new r(n$1, t) : n$1, next();
|
|
596
|
+
}
|
|
597
|
+
return next();
|
|
598
|
+
}
|
|
599
|
+
};
|
|
600
|
+
}
|
|
601
|
+
module.exports = _usingCtx, module.exports.__esModule = true, module.exports["default"] = module.exports;
|
|
602
|
+
} });
|
|
603
|
+
|
|
604
|
+
//#endregion
|
|
605
|
+
//#region src/unstable-core-do-not-import/stream/utils/asyncIterable.ts
|
|
606
|
+
var import_usingCtx$4 = __toESM(require_usingCtx(), 1);
|
|
607
|
+
function iteratorResource(iterable) {
|
|
608
|
+
const iterator = iterable[Symbol.asyncIterator]();
|
|
609
|
+
if (iterator[Symbol.asyncDispose]) return iterator;
|
|
610
|
+
return makeAsyncResource(iterator, async () => {
|
|
611
|
+
await iterator.return?.();
|
|
612
|
+
});
|
|
613
|
+
}
|
|
614
|
+
/**
|
|
615
|
+
* Derives a new {@link AsyncGenerator} based on {@link iterable}, that automatically aborts after the specified duration.
|
|
616
|
+
*/
|
|
617
|
+
async function* withMaxDuration(iterable, opts) {
|
|
618
|
+
try {
|
|
619
|
+
var _usingCtx$1 = (0, import_usingCtx$4.default)();
|
|
620
|
+
const iterator = _usingCtx$1.a(iteratorResource(iterable));
|
|
621
|
+
const timer = _usingCtx$1.u(timerResource(opts.maxDurationMs));
|
|
622
|
+
const timerPromise = timer.start();
|
|
623
|
+
let result;
|
|
624
|
+
while (true) {
|
|
625
|
+
result = await Unpromise.race([iterator.next(), timerPromise]);
|
|
626
|
+
if (result === disposablePromiseTimerResult) throwAbortError();
|
|
627
|
+
if (result.done) return result;
|
|
628
|
+
yield result.value;
|
|
629
|
+
result = null;
|
|
630
|
+
}
|
|
631
|
+
} catch (_) {
|
|
632
|
+
_usingCtx$1.e = _;
|
|
633
|
+
} finally {
|
|
634
|
+
await _usingCtx$1.d();
|
|
635
|
+
}
|
|
636
|
+
}
|
|
637
|
+
/**
|
|
638
|
+
* Derives a new {@link AsyncGenerator} based of {@link iterable}, that yields its first
|
|
639
|
+
* {@link count} values. Then, a grace period of {@link gracePeriodMs} is started in which further
|
|
640
|
+
* values may still come through. After this period, the generator aborts.
|
|
641
|
+
*/
|
|
642
|
+
async function* takeWithGrace(iterable, opts) {
|
|
643
|
+
try {
|
|
644
|
+
var _usingCtx3 = (0, import_usingCtx$4.default)();
|
|
645
|
+
const iterator = _usingCtx3.a(iteratorResource(iterable));
|
|
646
|
+
let result;
|
|
647
|
+
const timer = _usingCtx3.u(timerResource(opts.gracePeriodMs));
|
|
648
|
+
let count = opts.count;
|
|
649
|
+
let timerPromise = new Promise(() => {});
|
|
650
|
+
while (true) {
|
|
651
|
+
result = await Unpromise.race([iterator.next(), timerPromise]);
|
|
652
|
+
if (result === disposablePromiseTimerResult) throwAbortError();
|
|
653
|
+
if (result.done) return result.value;
|
|
654
|
+
yield result.value;
|
|
655
|
+
if (--count === 0) timerPromise = timer.start();
|
|
656
|
+
result = null;
|
|
657
|
+
}
|
|
658
|
+
} catch (_) {
|
|
659
|
+
_usingCtx3.e = _;
|
|
660
|
+
} finally {
|
|
661
|
+
await _usingCtx3.d();
|
|
662
|
+
}
|
|
663
|
+
}
|
|
664
|
+
|
|
665
|
+
//#endregion
|
|
666
|
+
//#region src/unstable-core-do-not-import/stream/utils/createDeferred.ts
|
|
667
|
+
function createDeferred() {
|
|
668
|
+
let resolve;
|
|
669
|
+
let reject;
|
|
670
|
+
const promise = new Promise((res, rej) => {
|
|
671
|
+
resolve = res;
|
|
672
|
+
reject = rej;
|
|
673
|
+
});
|
|
674
|
+
return {
|
|
675
|
+
promise,
|
|
676
|
+
resolve,
|
|
677
|
+
reject
|
|
678
|
+
};
|
|
679
|
+
}
|
|
680
|
+
|
|
681
|
+
//#endregion
|
|
682
|
+
//#region src/unstable-core-do-not-import/stream/utils/mergeAsyncIterables.ts
|
|
683
|
+
var import_usingCtx$3 = __toESM(require_usingCtx(), 1);
|
|
684
|
+
function createManagedIterator(iterable, onResult) {
|
|
685
|
+
const iterator = iterable[Symbol.asyncIterator]();
|
|
686
|
+
let state = "idle";
|
|
687
|
+
function cleanup() {
|
|
688
|
+
state = "done";
|
|
689
|
+
onResult = () => {};
|
|
690
|
+
}
|
|
691
|
+
function pull() {
|
|
692
|
+
if (state !== "idle") return;
|
|
693
|
+
state = "pending";
|
|
694
|
+
const next = iterator.next();
|
|
695
|
+
next.then((result) => {
|
|
696
|
+
if (result.done) {
|
|
697
|
+
state = "done";
|
|
698
|
+
onResult({
|
|
699
|
+
status: "return",
|
|
700
|
+
value: result.value
|
|
701
|
+
});
|
|
702
|
+
cleanup();
|
|
703
|
+
return;
|
|
704
|
+
}
|
|
705
|
+
state = "idle";
|
|
706
|
+
onResult({
|
|
707
|
+
status: "yield",
|
|
708
|
+
value: result.value
|
|
709
|
+
});
|
|
710
|
+
}).catch((cause) => {
|
|
711
|
+
onResult({
|
|
712
|
+
status: "error",
|
|
713
|
+
error: cause
|
|
714
|
+
});
|
|
715
|
+
cleanup();
|
|
716
|
+
});
|
|
717
|
+
}
|
|
718
|
+
return {
|
|
719
|
+
pull,
|
|
720
|
+
destroy: async () => {
|
|
721
|
+
cleanup();
|
|
722
|
+
await iterator.return?.();
|
|
723
|
+
}
|
|
724
|
+
};
|
|
725
|
+
}
|
|
726
|
+
/**
|
|
727
|
+
* Creates a new async iterable that merges multiple async iterables into a single stream.
|
|
728
|
+
* Values from the input iterables are yielded in the order they resolve, similar to Promise.race().
|
|
729
|
+
*
|
|
730
|
+
* New iterables can be added dynamically using the returned {@link MergedAsyncIterables.add} method, even after iteration has started.
|
|
731
|
+
*
|
|
732
|
+
* If any of the input iterables throws an error, that error will be propagated through the merged stream.
|
|
733
|
+
* Other iterables will not continue to be processed.
|
|
734
|
+
*
|
|
735
|
+
* @template TYield The type of values yielded by the input iterables
|
|
736
|
+
*/
|
|
737
|
+
function mergeAsyncIterables() {
|
|
738
|
+
let state = "idle";
|
|
739
|
+
let flushSignal = createDeferred();
|
|
740
|
+
/**
|
|
741
|
+
* used while {@link state} is `idle`
|
|
742
|
+
*/
|
|
743
|
+
const iterables = [];
|
|
744
|
+
/**
|
|
745
|
+
* used while {@link state} is `pending`
|
|
746
|
+
*/
|
|
747
|
+
const iterators = /* @__PURE__ */ new Set();
|
|
748
|
+
const buffer = [];
|
|
749
|
+
function initIterable(iterable) {
|
|
750
|
+
if (state !== "pending") return;
|
|
751
|
+
const iterator = createManagedIterator(iterable, (result) => {
|
|
752
|
+
if (state !== "pending") return;
|
|
753
|
+
switch (result.status) {
|
|
754
|
+
case "yield":
|
|
755
|
+
buffer.push([iterator, result]);
|
|
756
|
+
break;
|
|
757
|
+
case "return":
|
|
758
|
+
iterators.delete(iterator);
|
|
759
|
+
break;
|
|
760
|
+
case "error":
|
|
761
|
+
buffer.push([iterator, result]);
|
|
762
|
+
iterators.delete(iterator);
|
|
763
|
+
break;
|
|
764
|
+
}
|
|
765
|
+
flushSignal.resolve();
|
|
766
|
+
});
|
|
767
|
+
iterators.add(iterator);
|
|
768
|
+
iterator.pull();
|
|
769
|
+
}
|
|
770
|
+
return {
|
|
771
|
+
add(iterable) {
|
|
772
|
+
switch (state) {
|
|
773
|
+
case "idle":
|
|
774
|
+
iterables.push(iterable);
|
|
775
|
+
break;
|
|
776
|
+
case "pending":
|
|
777
|
+
initIterable(iterable);
|
|
778
|
+
break;
|
|
779
|
+
case "done": break;
|
|
780
|
+
}
|
|
781
|
+
},
|
|
782
|
+
async *[Symbol.asyncIterator]() {
|
|
783
|
+
try {
|
|
784
|
+
var _usingCtx$1 = (0, import_usingCtx$3.default)();
|
|
785
|
+
if (state !== "idle") throw new Error("Cannot iterate twice");
|
|
786
|
+
state = "pending";
|
|
787
|
+
const _finally = _usingCtx$1.a(makeAsyncResource({}, async () => {
|
|
788
|
+
state = "done";
|
|
789
|
+
const errors = [];
|
|
790
|
+
await Promise.all(Array.from(iterators.values()).map(async (it) => {
|
|
791
|
+
try {
|
|
792
|
+
await it.destroy();
|
|
793
|
+
} catch (cause) {
|
|
794
|
+
errors.push(cause);
|
|
795
|
+
}
|
|
796
|
+
}));
|
|
797
|
+
buffer.length = 0;
|
|
798
|
+
iterators.clear();
|
|
799
|
+
flushSignal.resolve();
|
|
800
|
+
if (errors.length > 0) throw new AggregateError(errors);
|
|
801
|
+
}));
|
|
802
|
+
while (iterables.length > 0) initIterable(iterables.shift());
|
|
803
|
+
while (iterators.size > 0) {
|
|
804
|
+
await flushSignal.promise;
|
|
805
|
+
while (buffer.length > 0) {
|
|
806
|
+
const [iterator, result] = buffer.shift();
|
|
807
|
+
switch (result.status) {
|
|
808
|
+
case "yield":
|
|
809
|
+
yield result.value;
|
|
810
|
+
iterator.pull();
|
|
811
|
+
break;
|
|
812
|
+
case "error": throw result.error;
|
|
813
|
+
}
|
|
814
|
+
}
|
|
815
|
+
flushSignal = createDeferred();
|
|
816
|
+
}
|
|
817
|
+
} catch (_) {
|
|
818
|
+
_usingCtx$1.e = _;
|
|
819
|
+
} finally {
|
|
820
|
+
await _usingCtx$1.d();
|
|
821
|
+
}
|
|
822
|
+
}
|
|
823
|
+
};
|
|
824
|
+
}
|
|
825
|
+
|
|
826
|
+
//#endregion
|
|
827
|
+
//#region src/unstable-core-do-not-import/stream/utils/readableStreamFrom.ts
|
|
828
|
+
/**
|
|
829
|
+
* Creates a ReadableStream from an AsyncIterable.
|
|
830
|
+
*
|
|
831
|
+
* @param iterable - The source AsyncIterable to stream from
|
|
832
|
+
* @returns A ReadableStream that yields values from the AsyncIterable
|
|
833
|
+
*/
|
|
834
|
+
function readableStreamFrom(iterable) {
|
|
835
|
+
const iterator = iterable[Symbol.asyncIterator]();
|
|
836
|
+
return new ReadableStream({
|
|
837
|
+
async cancel() {
|
|
838
|
+
await iterator.return?.();
|
|
839
|
+
},
|
|
840
|
+
async pull(controller) {
|
|
841
|
+
const result = await iterator.next();
|
|
842
|
+
if (result.done) {
|
|
843
|
+
controller.close();
|
|
844
|
+
return;
|
|
845
|
+
}
|
|
846
|
+
controller.enqueue(result.value);
|
|
847
|
+
}
|
|
848
|
+
});
|
|
849
|
+
}
|
|
850
|
+
|
|
851
|
+
//#endregion
|
|
852
|
+
//#region src/unstable-core-do-not-import/stream/utils/withPing.ts
|
|
853
|
+
var import_usingCtx$2 = __toESM(require_usingCtx(), 1);
|
|
854
|
+
const PING_SYM = Symbol("ping");
|
|
855
|
+
/**
|
|
856
|
+
* Derives a new {@link AsyncGenerator} based of {@link iterable}, that yields {@link PING_SYM}
|
|
857
|
+
* whenever no value has been yielded for {@link pingIntervalMs}.
|
|
858
|
+
*/
|
|
859
|
+
async function* withPing(iterable, pingIntervalMs) {
|
|
860
|
+
try {
|
|
861
|
+
var _usingCtx$1 = (0, import_usingCtx$2.default)();
|
|
862
|
+
const iterator = _usingCtx$1.a(iteratorResource(iterable));
|
|
863
|
+
let result;
|
|
864
|
+
let nextPromise = iterator.next();
|
|
865
|
+
while (true) try {
|
|
866
|
+
var _usingCtx3 = (0, import_usingCtx$2.default)();
|
|
867
|
+
const pingPromise = _usingCtx3.u(timerResource(pingIntervalMs));
|
|
868
|
+
result = await Unpromise.race([nextPromise, pingPromise.start()]);
|
|
869
|
+
if (result === disposablePromiseTimerResult) {
|
|
870
|
+
yield PING_SYM;
|
|
871
|
+
continue;
|
|
872
|
+
}
|
|
873
|
+
if (result.done) return result.value;
|
|
874
|
+
nextPromise = iterator.next();
|
|
875
|
+
yield result.value;
|
|
876
|
+
result = null;
|
|
877
|
+
} catch (_) {
|
|
878
|
+
_usingCtx3.e = _;
|
|
879
|
+
} finally {
|
|
880
|
+
_usingCtx3.d();
|
|
881
|
+
}
|
|
882
|
+
} catch (_) {
|
|
883
|
+
_usingCtx$1.e = _;
|
|
884
|
+
} finally {
|
|
885
|
+
await _usingCtx$1.d();
|
|
886
|
+
}
|
|
887
|
+
}
|
|
888
|
+
|
|
889
|
+
//#endregion
|
|
890
|
+
//#region src/unstable-core-do-not-import/stream/jsonl.ts
|
|
891
|
+
var import_usingCtx$1 = __toESM(require_usingCtx(), 1);
|
|
892
|
+
function isPlainObject(value) {
|
|
893
|
+
return Object.prototype.toString.call(value) === "[object Object]";
|
|
894
|
+
}
|
|
895
|
+
const CHUNK_VALUE_TYPE_PROMISE = 0;
|
|
896
|
+
const CHUNK_VALUE_TYPE_ASYNC_ITERABLE = 1;
|
|
897
|
+
const PROMISE_STATUS_FULFILLED = 0;
|
|
898
|
+
const PROMISE_STATUS_REJECTED = 1;
|
|
899
|
+
const ASYNC_ITERABLE_STATUS_RETURN = 0;
|
|
900
|
+
const ASYNC_ITERABLE_STATUS_YIELD = 1;
|
|
901
|
+
const ASYNC_ITERABLE_STATUS_ERROR = 2;
|
|
902
|
+
function isPromise(value) {
|
|
903
|
+
return (isObject(value) || isFunction(value)) && typeof value?.["then"] === "function" && typeof value?.["catch"] === "function";
|
|
904
|
+
}
|
|
905
|
+
var MaxDepthError = class extends Error {
|
|
906
|
+
constructor(path) {
|
|
907
|
+
super("Max depth reached at path: " + path.join("."));
|
|
908
|
+
this.path = path;
|
|
909
|
+
}
|
|
910
|
+
};
|
|
911
|
+
async function* createBatchStreamProducer(opts) {
|
|
912
|
+
const { data } = opts;
|
|
913
|
+
let counter = 0;
|
|
914
|
+
const placeholder = 0;
|
|
915
|
+
const mergedIterables = mergeAsyncIterables();
|
|
916
|
+
function registerAsync(callback) {
|
|
917
|
+
const idx = counter++;
|
|
918
|
+
const iterable$1 = callback(idx);
|
|
919
|
+
mergedIterables.add(iterable$1);
|
|
920
|
+
return idx;
|
|
921
|
+
}
|
|
922
|
+
function encodePromise(promise, path) {
|
|
923
|
+
return registerAsync(async function* (idx) {
|
|
924
|
+
const error = checkMaxDepth(path);
|
|
925
|
+
if (error) {
|
|
926
|
+
promise.catch((cause) => {
|
|
927
|
+
opts.onError?.({
|
|
928
|
+
error: cause,
|
|
929
|
+
path
|
|
930
|
+
});
|
|
931
|
+
});
|
|
932
|
+
promise = Promise.reject(error);
|
|
933
|
+
}
|
|
934
|
+
try {
|
|
935
|
+
const next = await promise;
|
|
936
|
+
yield [
|
|
937
|
+
idx,
|
|
938
|
+
PROMISE_STATUS_FULFILLED,
|
|
939
|
+
encode(next, path)
|
|
940
|
+
];
|
|
941
|
+
} catch (cause) {
|
|
942
|
+
opts.onError?.({
|
|
943
|
+
error: cause,
|
|
944
|
+
path
|
|
945
|
+
});
|
|
946
|
+
yield [
|
|
947
|
+
idx,
|
|
948
|
+
PROMISE_STATUS_REJECTED,
|
|
949
|
+
opts.formatError?.({
|
|
950
|
+
error: cause,
|
|
951
|
+
path
|
|
952
|
+
})
|
|
953
|
+
];
|
|
954
|
+
}
|
|
955
|
+
});
|
|
956
|
+
}
|
|
957
|
+
function encodeAsyncIterable(iterable$1, path) {
|
|
958
|
+
return registerAsync(async function* (idx) {
|
|
959
|
+
try {
|
|
960
|
+
var _usingCtx$1 = (0, import_usingCtx$1.default)();
|
|
961
|
+
const error = checkMaxDepth(path);
|
|
962
|
+
if (error) throw error;
|
|
963
|
+
const iterator = _usingCtx$1.a(iteratorResource(iterable$1));
|
|
964
|
+
try {
|
|
965
|
+
while (true) {
|
|
966
|
+
const next = await iterator.next();
|
|
967
|
+
if (next.done) {
|
|
968
|
+
yield [
|
|
969
|
+
idx,
|
|
970
|
+
ASYNC_ITERABLE_STATUS_RETURN,
|
|
971
|
+
encode(next.value, path)
|
|
972
|
+
];
|
|
973
|
+
break;
|
|
974
|
+
}
|
|
975
|
+
yield [
|
|
976
|
+
idx,
|
|
977
|
+
ASYNC_ITERABLE_STATUS_YIELD,
|
|
978
|
+
encode(next.value, path)
|
|
979
|
+
];
|
|
980
|
+
}
|
|
981
|
+
} catch (cause) {
|
|
982
|
+
opts.onError?.({
|
|
983
|
+
error: cause,
|
|
984
|
+
path
|
|
985
|
+
});
|
|
986
|
+
yield [
|
|
987
|
+
idx,
|
|
988
|
+
ASYNC_ITERABLE_STATUS_ERROR,
|
|
989
|
+
opts.formatError?.({
|
|
990
|
+
error: cause,
|
|
991
|
+
path
|
|
992
|
+
})
|
|
993
|
+
];
|
|
994
|
+
}
|
|
995
|
+
} catch (_) {
|
|
996
|
+
_usingCtx$1.e = _;
|
|
997
|
+
} finally {
|
|
998
|
+
await _usingCtx$1.d();
|
|
999
|
+
}
|
|
1000
|
+
});
|
|
1001
|
+
}
|
|
1002
|
+
function checkMaxDepth(path) {
|
|
1003
|
+
if (opts.maxDepth && path.length > opts.maxDepth) return new MaxDepthError(path);
|
|
1004
|
+
return null;
|
|
1005
|
+
}
|
|
1006
|
+
function encodeAsync(value, path) {
|
|
1007
|
+
if (isPromise(value)) return [CHUNK_VALUE_TYPE_PROMISE, encodePromise(value, path)];
|
|
1008
|
+
if (isAsyncIterable(value)) {
|
|
1009
|
+
if (opts.maxDepth && path.length >= opts.maxDepth) throw new Error("Max depth reached");
|
|
1010
|
+
return [CHUNK_VALUE_TYPE_ASYNC_ITERABLE, encodeAsyncIterable(value, path)];
|
|
1011
|
+
}
|
|
1012
|
+
return null;
|
|
1013
|
+
}
|
|
1014
|
+
function encode(value, path) {
|
|
1015
|
+
if (value === void 0) return [[]];
|
|
1016
|
+
const reg = encodeAsync(value, path);
|
|
1017
|
+
if (reg) return [[placeholder], [null, ...reg]];
|
|
1018
|
+
if (!isPlainObject(value)) return [[value]];
|
|
1019
|
+
const newObj = {};
|
|
1020
|
+
const asyncValues = [];
|
|
1021
|
+
for (const [key, item] of Object.entries(value)) {
|
|
1022
|
+
const transformed = encodeAsync(item, [...path, key]);
|
|
1023
|
+
if (!transformed) {
|
|
1024
|
+
newObj[key] = item;
|
|
1025
|
+
continue;
|
|
1026
|
+
}
|
|
1027
|
+
newObj[key] = placeholder;
|
|
1028
|
+
asyncValues.push([key, ...transformed]);
|
|
1029
|
+
}
|
|
1030
|
+
return [[newObj], ...asyncValues];
|
|
1031
|
+
}
|
|
1032
|
+
const newHead = {};
|
|
1033
|
+
for (const [key, item] of Object.entries(data)) newHead[key] = encode(item, [key]);
|
|
1034
|
+
yield newHead;
|
|
1035
|
+
let iterable = mergedIterables;
|
|
1036
|
+
if (opts.pingMs) iterable = withPing(mergedIterables, opts.pingMs);
|
|
1037
|
+
for await (const value of iterable) yield value;
|
|
1038
|
+
}
|
|
1039
|
+
/**
|
|
1040
|
+
* JSON Lines stream producer
|
|
1041
|
+
* @see https://jsonlines.org/
|
|
1042
|
+
*/
|
|
1043
|
+
function jsonlStreamProducer(opts) {
|
|
1044
|
+
let stream = readableStreamFrom(createBatchStreamProducer(opts));
|
|
1045
|
+
const { serialize } = opts;
|
|
1046
|
+
if (serialize) stream = stream.pipeThrough(new TransformStream({ transform(chunk, controller) {
|
|
1047
|
+
if (chunk === PING_SYM) controller.enqueue(PING_SYM);
|
|
1048
|
+
else controller.enqueue(serialize(chunk));
|
|
1049
|
+
} }));
|
|
1050
|
+
return stream.pipeThrough(new TransformStream({ transform(chunk, controller) {
|
|
1051
|
+
if (chunk === PING_SYM) controller.enqueue(" ");
|
|
1052
|
+
else controller.enqueue(JSON.stringify(chunk) + "\n");
|
|
1053
|
+
} })).pipeThrough(new TextEncoderStream());
|
|
1054
|
+
}
|
|
1055
|
+
var AsyncError = class extends Error {
|
|
1056
|
+
constructor(data) {
|
|
1057
|
+
super("Received error from server");
|
|
1058
|
+
this.data = data;
|
|
1059
|
+
}
|
|
1060
|
+
};
|
|
1061
|
+
const nodeJsStreamToReaderEsque = (source) => {
|
|
1062
|
+
return { getReader() {
|
|
1063
|
+
const stream = new ReadableStream({ start(controller) {
|
|
1064
|
+
source.on("data", (chunk) => {
|
|
1065
|
+
controller.enqueue(chunk);
|
|
1066
|
+
});
|
|
1067
|
+
source.on("end", () => {
|
|
1068
|
+
controller.close();
|
|
1069
|
+
});
|
|
1070
|
+
source.on("error", (error) => {
|
|
1071
|
+
controller.error(error);
|
|
1072
|
+
});
|
|
1073
|
+
} });
|
|
1074
|
+
return stream.getReader();
|
|
1075
|
+
} };
|
|
1076
|
+
};
|
|
1077
|
+
function createLineAccumulator(from) {
|
|
1078
|
+
const reader = "getReader" in from ? from.getReader() : nodeJsStreamToReaderEsque(from).getReader();
|
|
1079
|
+
let lineAggregate = "";
|
|
1080
|
+
return new ReadableStream({
|
|
1081
|
+
async pull(controller) {
|
|
1082
|
+
const { done, value } = await reader.read();
|
|
1083
|
+
if (done) controller.close();
|
|
1084
|
+
else controller.enqueue(value);
|
|
1085
|
+
},
|
|
1086
|
+
cancel() {
|
|
1087
|
+
return reader.cancel();
|
|
1088
|
+
}
|
|
1089
|
+
}).pipeThrough(new TextDecoderStream()).pipeThrough(new TransformStream({ transform(chunk, controller) {
|
|
1090
|
+
lineAggregate += chunk;
|
|
1091
|
+
const parts = lineAggregate.split("\n");
|
|
1092
|
+
lineAggregate = parts.pop() ?? "";
|
|
1093
|
+
for (const part of parts) controller.enqueue(part);
|
|
1094
|
+
} }));
|
|
1095
|
+
}
|
|
1096
|
+
function createConsumerStream(from) {
|
|
1097
|
+
const stream = createLineAccumulator(from);
|
|
1098
|
+
let sentHead = false;
|
|
1099
|
+
return stream.pipeThrough(new TransformStream({ transform(line, controller) {
|
|
1100
|
+
if (!sentHead) {
|
|
1101
|
+
const head = JSON.parse(line);
|
|
1102
|
+
controller.enqueue(head);
|
|
1103
|
+
sentHead = true;
|
|
1104
|
+
} else {
|
|
1105
|
+
const chunk = JSON.parse(line);
|
|
1106
|
+
controller.enqueue(chunk);
|
|
1107
|
+
}
|
|
1108
|
+
} }));
|
|
1109
|
+
}
|
|
1110
|
+
/**
|
|
1111
|
+
* Creates a handler for managing stream controllers and their lifecycle
|
|
1112
|
+
*/
|
|
1113
|
+
function createStreamsManager(abortController) {
|
|
1114
|
+
const controllerMap = /* @__PURE__ */ new Map();
|
|
1115
|
+
/**
|
|
1116
|
+
* Checks if there are no pending controllers or deferred promises
|
|
1117
|
+
*/
|
|
1118
|
+
function isEmpty() {
|
|
1119
|
+
return Array.from(controllerMap.values()).every((c) => c.closed);
|
|
1120
|
+
}
|
|
1121
|
+
/**
|
|
1122
|
+
* Creates a stream controller
|
|
1123
|
+
*/
|
|
1124
|
+
function createStreamController() {
|
|
1125
|
+
let originalController;
|
|
1126
|
+
const stream = new ReadableStream({ start(controller) {
|
|
1127
|
+
originalController = controller;
|
|
1128
|
+
} });
|
|
1129
|
+
const streamController = {
|
|
1130
|
+
enqueue: (v) => originalController.enqueue(v),
|
|
1131
|
+
close: () => {
|
|
1132
|
+
originalController.close();
|
|
1133
|
+
clear();
|
|
1134
|
+
if (isEmpty()) abortController.abort();
|
|
1135
|
+
},
|
|
1136
|
+
closed: false,
|
|
1137
|
+
getReaderResource: () => {
|
|
1138
|
+
const reader = stream.getReader();
|
|
1139
|
+
return makeResource(reader, () => {
|
|
1140
|
+
reader.releaseLock();
|
|
1141
|
+
streamController.close();
|
|
1142
|
+
});
|
|
1143
|
+
},
|
|
1144
|
+
error: (reason) => {
|
|
1145
|
+
originalController.error(reason);
|
|
1146
|
+
clear();
|
|
1147
|
+
}
|
|
1148
|
+
};
|
|
1149
|
+
function clear() {
|
|
1150
|
+
Object.assign(streamController, {
|
|
1151
|
+
closed: true,
|
|
1152
|
+
close: () => {},
|
|
1153
|
+
enqueue: () => {},
|
|
1154
|
+
getReaderResource: null,
|
|
1155
|
+
error: () => {}
|
|
1156
|
+
});
|
|
1157
|
+
}
|
|
1158
|
+
return streamController;
|
|
1159
|
+
}
|
|
1160
|
+
/**
|
|
1161
|
+
* Gets or creates a stream controller
|
|
1162
|
+
*/
|
|
1163
|
+
function getOrCreate(chunkId) {
|
|
1164
|
+
let c = controllerMap.get(chunkId);
|
|
1165
|
+
if (!c) {
|
|
1166
|
+
c = createStreamController();
|
|
1167
|
+
controllerMap.set(chunkId, c);
|
|
1168
|
+
}
|
|
1169
|
+
return c;
|
|
1170
|
+
}
|
|
1171
|
+
/**
|
|
1172
|
+
* Cancels all pending controllers and rejects deferred promises
|
|
1173
|
+
*/
|
|
1174
|
+
function cancelAll(reason) {
|
|
1175
|
+
for (const controller of controllerMap.values()) controller.error(reason);
|
|
1176
|
+
}
|
|
1177
|
+
return {
|
|
1178
|
+
getOrCreate,
|
|
1179
|
+
isEmpty,
|
|
1180
|
+
cancelAll
|
|
1181
|
+
};
|
|
1182
|
+
}
|
|
1183
|
+
/**
|
|
1184
|
+
* JSON Lines stream consumer
|
|
1185
|
+
* @see https://jsonlines.org/
|
|
1186
|
+
*/
|
|
1187
|
+
async function jsonlStreamConsumer(opts) {
|
|
1188
|
+
const { deserialize = (v) => v } = opts;
|
|
1189
|
+
let source = createConsumerStream(opts.from);
|
|
1190
|
+
if (deserialize) source = source.pipeThrough(new TransformStream({ transform(chunk, controller) {
|
|
1191
|
+
controller.enqueue(deserialize(chunk));
|
|
1192
|
+
} }));
|
|
1193
|
+
let headDeferred = createDeferred();
|
|
1194
|
+
const streamManager = createStreamsManager(opts.abortController);
|
|
1195
|
+
function decodeChunkDefinition(value) {
|
|
1196
|
+
const [_path, type, chunkId] = value;
|
|
1197
|
+
const controller = streamManager.getOrCreate(chunkId);
|
|
1198
|
+
switch (type) {
|
|
1199
|
+
case CHUNK_VALUE_TYPE_PROMISE: return run(async () => {
|
|
1200
|
+
try {
|
|
1201
|
+
var _usingCtx3 = (0, import_usingCtx$1.default)();
|
|
1202
|
+
const reader = _usingCtx3.u(controller.getReaderResource());
|
|
1203
|
+
const { value: value$1 } = await reader.read();
|
|
1204
|
+
const [_chunkId, status, data] = value$1;
|
|
1205
|
+
switch (status) {
|
|
1206
|
+
case PROMISE_STATUS_FULFILLED: return decode(data);
|
|
1207
|
+
case PROMISE_STATUS_REJECTED: throw opts.formatError?.({ error: data }) ?? new AsyncError(data);
|
|
1208
|
+
}
|
|
1209
|
+
} catch (_) {
|
|
1210
|
+
_usingCtx3.e = _;
|
|
1211
|
+
} finally {
|
|
1212
|
+
_usingCtx3.d();
|
|
1213
|
+
}
|
|
1214
|
+
});
|
|
1215
|
+
case CHUNK_VALUE_TYPE_ASYNC_ITERABLE: return run(async function* () {
|
|
1216
|
+
try {
|
|
1217
|
+
var _usingCtx4 = (0, import_usingCtx$1.default)();
|
|
1218
|
+
const reader = _usingCtx4.u(controller.getReaderResource());
|
|
1219
|
+
while (true) {
|
|
1220
|
+
const { value: value$1 } = await reader.read();
|
|
1221
|
+
const [_chunkId, status, data] = value$1;
|
|
1222
|
+
switch (status) {
|
|
1223
|
+
case ASYNC_ITERABLE_STATUS_YIELD:
|
|
1224
|
+
yield decode(data);
|
|
1225
|
+
break;
|
|
1226
|
+
case ASYNC_ITERABLE_STATUS_RETURN: return decode(data);
|
|
1227
|
+
case ASYNC_ITERABLE_STATUS_ERROR: throw opts.formatError?.({ error: data }) ?? new AsyncError(data);
|
|
1228
|
+
}
|
|
1229
|
+
}
|
|
1230
|
+
} catch (_) {
|
|
1231
|
+
_usingCtx4.e = _;
|
|
1232
|
+
} finally {
|
|
1233
|
+
_usingCtx4.d();
|
|
1234
|
+
}
|
|
1235
|
+
});
|
|
1236
|
+
}
|
|
1237
|
+
}
|
|
1238
|
+
function decode(value) {
|
|
1239
|
+
const [[data], ...asyncProps] = value;
|
|
1240
|
+
for (const value$1 of asyncProps) {
|
|
1241
|
+
const [key] = value$1;
|
|
1242
|
+
const decoded = decodeChunkDefinition(value$1);
|
|
1243
|
+
if (key === null) return decoded;
|
|
1244
|
+
data[key] = decoded;
|
|
1245
|
+
}
|
|
1246
|
+
return data;
|
|
1247
|
+
}
|
|
1248
|
+
const closeOrAbort = (reason) => {
|
|
1249
|
+
headDeferred?.reject(reason);
|
|
1250
|
+
streamManager.cancelAll(reason);
|
|
1251
|
+
};
|
|
1252
|
+
source.pipeTo(new WritableStream({
|
|
1253
|
+
write(chunkOrHead) {
|
|
1254
|
+
if (headDeferred) {
|
|
1255
|
+
const head = chunkOrHead;
|
|
1256
|
+
for (const [key, value] of Object.entries(chunkOrHead)) {
|
|
1257
|
+
const parsed = decode(value);
|
|
1258
|
+
head[key] = parsed;
|
|
1259
|
+
}
|
|
1260
|
+
headDeferred.resolve(head);
|
|
1261
|
+
headDeferred = null;
|
|
1262
|
+
return;
|
|
1263
|
+
}
|
|
1264
|
+
const chunk = chunkOrHead;
|
|
1265
|
+
const [idx] = chunk;
|
|
1266
|
+
const controller = streamManager.getOrCreate(idx);
|
|
1267
|
+
controller.enqueue(chunk);
|
|
1268
|
+
},
|
|
1269
|
+
close: () => closeOrAbort(new Error("Stream closed")),
|
|
1270
|
+
abort: closeOrAbort
|
|
1271
|
+
}), { signal: opts.abortController.signal }).catch((error) => {
|
|
1272
|
+
opts.onError?.({ error });
|
|
1273
|
+
closeOrAbort(error);
|
|
1274
|
+
});
|
|
1275
|
+
return [await headDeferred.promise, streamManager];
|
|
1276
|
+
}
|
|
1277
|
+
|
|
1278
|
+
//#endregion
|
|
1279
|
+
//#region src/unstable-core-do-not-import/stream/sse.ts
|
|
1280
|
+
var import_usingCtx = __toESM(require_usingCtx(), 1);
|
|
1281
|
+
const PING_EVENT = "ping";
|
|
1282
|
+
const SERIALIZED_ERROR_EVENT = "serialized-error";
|
|
1283
|
+
const CONNECTED_EVENT = "connected";
|
|
1284
|
+
const RETURN_EVENT = "return";
|
|
1285
|
+
/**
|
|
1286
|
+
*
|
|
1287
|
+
* @see https://html.spec.whatwg.org/multipage/server-sent-events.html
|
|
1288
|
+
*/
|
|
1289
|
+
function sseStreamProducer(opts) {
|
|
1290
|
+
const { serialize = identity } = opts;
|
|
1291
|
+
const ping = {
|
|
1292
|
+
enabled: opts.ping?.enabled ?? false,
|
|
1293
|
+
intervalMs: opts.ping?.intervalMs ?? 1e3
|
|
1294
|
+
};
|
|
1295
|
+
const client = opts.client ?? {};
|
|
1296
|
+
if (ping.enabled && client.reconnectAfterInactivityMs && ping.intervalMs > client.reconnectAfterInactivityMs) throw new Error(`Ping interval must be less than client reconnect interval to prevent unnecessary reconnection - ping.intervalMs: ${ping.intervalMs} client.reconnectAfterInactivityMs: ${client.reconnectAfterInactivityMs}`);
|
|
1297
|
+
async function* generator() {
|
|
1298
|
+
yield {
|
|
1299
|
+
event: CONNECTED_EVENT,
|
|
1300
|
+
data: JSON.stringify(client)
|
|
1301
|
+
};
|
|
1302
|
+
let iterable = opts.data;
|
|
1303
|
+
if (opts.emitAndEndImmediately) iterable = takeWithGrace(iterable, {
|
|
1304
|
+
count: 1,
|
|
1305
|
+
gracePeriodMs: 1
|
|
1306
|
+
});
|
|
1307
|
+
if (opts.maxDurationMs && opts.maxDurationMs > 0 && opts.maxDurationMs !== Infinity) iterable = withMaxDuration(iterable, { maxDurationMs: opts.maxDurationMs });
|
|
1308
|
+
if (ping.enabled && ping.intervalMs !== Infinity && ping.intervalMs > 0) iterable = withPing(iterable, ping.intervalMs);
|
|
1309
|
+
let value;
|
|
1310
|
+
let chunk;
|
|
1311
|
+
for await (value of iterable) {
|
|
1312
|
+
if (value === PING_SYM) {
|
|
1313
|
+
yield {
|
|
1314
|
+
event: PING_EVENT,
|
|
1315
|
+
data: ""
|
|
1316
|
+
};
|
|
1317
|
+
continue;
|
|
1318
|
+
}
|
|
1319
|
+
chunk = isTrackedEnvelope(value) ? {
|
|
1320
|
+
id: value[0],
|
|
1321
|
+
data: value[1]
|
|
1322
|
+
} : { data: value };
|
|
1323
|
+
chunk.data = JSON.stringify(serialize(chunk.data));
|
|
1324
|
+
yield chunk;
|
|
1325
|
+
value = null;
|
|
1326
|
+
chunk = null;
|
|
1327
|
+
}
|
|
1328
|
+
}
|
|
1329
|
+
async function* generatorWithErrorHandling() {
|
|
1330
|
+
try {
|
|
1331
|
+
yield* generator();
|
|
1332
|
+
yield {
|
|
1333
|
+
event: RETURN_EVENT,
|
|
1334
|
+
data: ""
|
|
1335
|
+
};
|
|
1336
|
+
} catch (cause) {
|
|
1337
|
+
if (isAbortError(cause)) return;
|
|
1338
|
+
const error = getTRPCErrorFromUnknown(cause);
|
|
1339
|
+
const data = opts.formatError?.({ error }) ?? null;
|
|
1340
|
+
yield {
|
|
1341
|
+
event: SERIALIZED_ERROR_EVENT,
|
|
1342
|
+
data: JSON.stringify(serialize(data))
|
|
1343
|
+
};
|
|
1344
|
+
}
|
|
1345
|
+
}
|
|
1346
|
+
const stream = readableStreamFrom(generatorWithErrorHandling());
|
|
1347
|
+
return stream.pipeThrough(new TransformStream({ transform(chunk, controller) {
|
|
1348
|
+
if ("event" in chunk) controller.enqueue(`event: ${chunk.event}\n`);
|
|
1349
|
+
if ("data" in chunk) controller.enqueue(`data: ${chunk.data}\n`);
|
|
1350
|
+
if ("id" in chunk) controller.enqueue(`id: ${chunk.id}\n`);
|
|
1351
|
+
if ("comment" in chunk) controller.enqueue(`: ${chunk.comment}\n`);
|
|
1352
|
+
controller.enqueue("\n\n");
|
|
1353
|
+
} })).pipeThrough(new TextEncoderStream());
|
|
1354
|
+
}
|
|
1355
|
+
async function withTimeout(opts) {
|
|
1356
|
+
try {
|
|
1357
|
+
var _usingCtx$1 = (0, import_usingCtx.default)();
|
|
1358
|
+
const timeoutPromise = _usingCtx$1.u(timerResource(opts.timeoutMs));
|
|
1359
|
+
const res = await Unpromise.race([opts.promise, timeoutPromise.start()]);
|
|
1360
|
+
if (res === disposablePromiseTimerResult) return await opts.onTimeout();
|
|
1361
|
+
return res;
|
|
1362
|
+
} catch (_) {
|
|
1363
|
+
_usingCtx$1.e = _;
|
|
1364
|
+
} finally {
|
|
1365
|
+
_usingCtx$1.d();
|
|
1366
|
+
}
|
|
1367
|
+
}
|
|
1368
|
+
/**
|
|
1369
|
+
* @see https://html.spec.whatwg.org/multipage/server-sent-events.html
|
|
1370
|
+
*/
|
|
1371
|
+
function sseStreamConsumer(opts) {
|
|
1372
|
+
const { deserialize = (v) => v } = opts;
|
|
1373
|
+
let clientOptions = {};
|
|
1374
|
+
const signal = opts.signal;
|
|
1375
|
+
let _es = null;
|
|
1376
|
+
const createStream = () => new ReadableStream({
|
|
1377
|
+
async start(controller) {
|
|
1378
|
+
const [url, init] = await Promise.all([opts.url(), opts.init()]);
|
|
1379
|
+
const eventSource = _es = new opts.EventSource(url, init);
|
|
1380
|
+
controller.enqueue({
|
|
1381
|
+
type: "connecting",
|
|
1382
|
+
eventSource: _es,
|
|
1383
|
+
event: null
|
|
1384
|
+
});
|
|
1385
|
+
eventSource.addEventListener(CONNECTED_EVENT, (_msg) => {
|
|
1386
|
+
const msg = _msg;
|
|
1387
|
+
const options = JSON.parse(msg.data);
|
|
1388
|
+
clientOptions = options;
|
|
1389
|
+
controller.enqueue({
|
|
1390
|
+
type: "connected",
|
|
1391
|
+
options,
|
|
1392
|
+
eventSource
|
|
1393
|
+
});
|
|
1394
|
+
});
|
|
1395
|
+
eventSource.addEventListener(SERIALIZED_ERROR_EVENT, (_msg) => {
|
|
1396
|
+
const msg = _msg;
|
|
1397
|
+
controller.enqueue({
|
|
1398
|
+
type: "serialized-error",
|
|
1399
|
+
error: deserialize(JSON.parse(msg.data)),
|
|
1400
|
+
eventSource
|
|
1401
|
+
});
|
|
1402
|
+
});
|
|
1403
|
+
eventSource.addEventListener(PING_EVENT, () => {
|
|
1404
|
+
controller.enqueue({
|
|
1405
|
+
type: "ping",
|
|
1406
|
+
eventSource
|
|
1407
|
+
});
|
|
1408
|
+
});
|
|
1409
|
+
eventSource.addEventListener(RETURN_EVENT, () => {
|
|
1410
|
+
eventSource.close();
|
|
1411
|
+
controller.close();
|
|
1412
|
+
_es = null;
|
|
1413
|
+
});
|
|
1414
|
+
eventSource.addEventListener("error", (event) => {
|
|
1415
|
+
if (eventSource.readyState === eventSource.CLOSED) controller.error(event);
|
|
1416
|
+
else controller.enqueue({
|
|
1417
|
+
type: "connecting",
|
|
1418
|
+
eventSource,
|
|
1419
|
+
event
|
|
1420
|
+
});
|
|
1421
|
+
});
|
|
1422
|
+
eventSource.addEventListener("message", (_msg) => {
|
|
1423
|
+
const msg = _msg;
|
|
1424
|
+
const chunk = deserialize(JSON.parse(msg.data));
|
|
1425
|
+
const def = { data: chunk };
|
|
1426
|
+
if (msg.lastEventId) def.id = msg.lastEventId;
|
|
1427
|
+
controller.enqueue({
|
|
1428
|
+
type: "data",
|
|
1429
|
+
data: def,
|
|
1430
|
+
eventSource
|
|
1431
|
+
});
|
|
1432
|
+
});
|
|
1433
|
+
const onAbort = () => {
|
|
1434
|
+
try {
|
|
1435
|
+
eventSource.close();
|
|
1436
|
+
controller.close();
|
|
1437
|
+
} catch {}
|
|
1438
|
+
};
|
|
1439
|
+
if (signal.aborted) onAbort();
|
|
1440
|
+
else signal.addEventListener("abort", onAbort);
|
|
1441
|
+
},
|
|
1442
|
+
cancel() {
|
|
1443
|
+
_es?.close();
|
|
1444
|
+
}
|
|
1445
|
+
});
|
|
1446
|
+
const getStreamResource = () => {
|
|
1447
|
+
let stream = createStream();
|
|
1448
|
+
let reader = stream.getReader();
|
|
1449
|
+
async function dispose() {
|
|
1450
|
+
await reader.cancel();
|
|
1451
|
+
_es = null;
|
|
1452
|
+
}
|
|
1453
|
+
return makeAsyncResource({
|
|
1454
|
+
read() {
|
|
1455
|
+
return reader.read();
|
|
1456
|
+
},
|
|
1457
|
+
async recreate() {
|
|
1458
|
+
await dispose();
|
|
1459
|
+
stream = createStream();
|
|
1460
|
+
reader = stream.getReader();
|
|
1461
|
+
}
|
|
1462
|
+
}, dispose);
|
|
1463
|
+
};
|
|
1464
|
+
return run(async function* () {
|
|
1465
|
+
try {
|
|
1466
|
+
var _usingCtx3 = (0, import_usingCtx.default)();
|
|
1467
|
+
const stream = _usingCtx3.a(getStreamResource());
|
|
1468
|
+
while (true) {
|
|
1469
|
+
let promise = stream.read();
|
|
1470
|
+
const timeoutMs = clientOptions.reconnectAfterInactivityMs;
|
|
1471
|
+
if (timeoutMs) promise = withTimeout({
|
|
1472
|
+
promise,
|
|
1473
|
+
timeoutMs,
|
|
1474
|
+
onTimeout: async () => {
|
|
1475
|
+
const res = {
|
|
1476
|
+
value: {
|
|
1477
|
+
type: "timeout",
|
|
1478
|
+
ms: timeoutMs,
|
|
1479
|
+
eventSource: _es
|
|
1480
|
+
},
|
|
1481
|
+
done: false
|
|
1482
|
+
};
|
|
1483
|
+
await stream.recreate();
|
|
1484
|
+
return res;
|
|
1485
|
+
}
|
|
1486
|
+
});
|
|
1487
|
+
const result = await promise;
|
|
1488
|
+
if (result.done) return result.value;
|
|
1489
|
+
yield result.value;
|
|
1490
|
+
}
|
|
1491
|
+
} catch (_) {
|
|
1492
|
+
_usingCtx3.e = _;
|
|
1493
|
+
} finally {
|
|
1494
|
+
await _usingCtx3.d();
|
|
1495
|
+
}
|
|
1496
|
+
});
|
|
1497
|
+
}
|
|
1498
|
+
const sseHeaders = {
|
|
1499
|
+
"Content-Type": "text/event-stream",
|
|
1500
|
+
"Cache-Control": "no-cache, no-transform",
|
|
1501
|
+
"X-Accel-Buffering": "no",
|
|
1502
|
+
Connection: "keep-alive"
|
|
1503
|
+
};
|
|
1504
|
+
|
|
1505
|
+
//#endregion
|
|
1506
|
+
//#region src/unstable-core-do-not-import/http/resolveResponse.ts
|
|
1507
|
+
function errorToAsyncIterable(err) {
|
|
1508
|
+
return run(async function* () {
|
|
1509
|
+
throw err;
|
|
1510
|
+
});
|
|
1511
|
+
}
|
|
1512
|
+
const TYPE_ACCEPTED_METHOD_MAP = {
|
|
1513
|
+
mutation: ["POST"],
|
|
1514
|
+
query: ["GET"],
|
|
1515
|
+
subscription: ["GET"]
|
|
1516
|
+
};
|
|
1517
|
+
const TYPE_ACCEPTED_METHOD_MAP_WITH_METHOD_OVERRIDE = {
|
|
1518
|
+
mutation: ["POST"],
|
|
1519
|
+
query: ["GET", "POST"],
|
|
1520
|
+
subscription: ["GET", "POST"]
|
|
1521
|
+
};
|
|
1522
|
+
function initResponse(initOpts) {
|
|
1523
|
+
const { ctx, info, responseMeta, untransformedJSON, errors = [], headers } = initOpts;
|
|
1524
|
+
let status = untransformedJSON ? getHTTPStatusCode(untransformedJSON) : 200;
|
|
1525
|
+
const eagerGeneration = !untransformedJSON;
|
|
1526
|
+
const data = eagerGeneration ? [] : Array.isArray(untransformedJSON) ? untransformedJSON : [untransformedJSON];
|
|
1527
|
+
const meta = responseMeta?.({
|
|
1528
|
+
ctx,
|
|
1529
|
+
info,
|
|
1530
|
+
paths: info?.calls.map((call) => call.path),
|
|
1531
|
+
data,
|
|
1532
|
+
errors,
|
|
1533
|
+
eagerGeneration,
|
|
1534
|
+
type: info?.calls.find((call) => call.procedure?._def.type)?.procedure?._def.type ?? "unknown"
|
|
1535
|
+
}) ?? {};
|
|
1536
|
+
if (meta.headers) {
|
|
1537
|
+
if (meta.headers instanceof Headers) for (const [key, value] of meta.headers.entries()) headers.append(key, value);
|
|
1538
|
+
else
|
|
1539
|
+
/**
|
|
1540
|
+
* @deprecated, delete in v12
|
|
1541
|
+
*/
|
|
1542
|
+
for (const [key, value] of Object.entries(meta.headers)) if (Array.isArray(value)) for (const v of value) headers.append(key, v);
|
|
1543
|
+
else if (typeof value === "string") headers.set(key, value);
|
|
1544
|
+
}
|
|
1545
|
+
if (meta.status) status = meta.status;
|
|
1546
|
+
return { status };
|
|
1547
|
+
}
|
|
1548
|
+
function caughtErrorToData(cause, errorOpts) {
|
|
1549
|
+
const { router, req, onError } = errorOpts.opts;
|
|
1550
|
+
const error = getTRPCErrorFromUnknown(cause);
|
|
1551
|
+
onError?.({
|
|
1552
|
+
error,
|
|
1553
|
+
path: errorOpts.path,
|
|
1554
|
+
input: errorOpts.input,
|
|
1555
|
+
ctx: errorOpts.ctx,
|
|
1556
|
+
type: errorOpts.type,
|
|
1557
|
+
req
|
|
1558
|
+
});
|
|
1559
|
+
const untransformedJSON = { error: getErrorShape({
|
|
1560
|
+
config: router._def._config,
|
|
1561
|
+
error,
|
|
1562
|
+
type: errorOpts.type,
|
|
1563
|
+
path: errorOpts.path,
|
|
1564
|
+
input: errorOpts.input,
|
|
1565
|
+
ctx: errorOpts.ctx
|
|
1566
|
+
}) };
|
|
1567
|
+
const transformedJSON = transformTRPCResponse(router._def._config, untransformedJSON);
|
|
1568
|
+
const body = JSON.stringify(transformedJSON);
|
|
1569
|
+
return {
|
|
1570
|
+
error,
|
|
1571
|
+
untransformedJSON,
|
|
1572
|
+
body
|
|
1573
|
+
};
|
|
1574
|
+
}
|
|
1575
|
+
/**
|
|
1576
|
+
* Check if a value is a stream-like object
|
|
1577
|
+
* - if it's an async iterable
|
|
1578
|
+
* - if it's an object with async iterables or promises
|
|
1579
|
+
*/
|
|
1580
|
+
function isDataStream(v) {
|
|
1581
|
+
if (!isObject(v)) return false;
|
|
1582
|
+
if (isAsyncIterable(v)) return true;
|
|
1583
|
+
return Object.values(v).some(isPromise) || Object.values(v).some(isAsyncIterable);
|
|
1584
|
+
}
|
|
1585
|
+
async function resolveResponse(opts) {
|
|
1586
|
+
const { router, req } = opts;
|
|
1587
|
+
const headers = new Headers([["vary", "trpc-accept"]]);
|
|
1588
|
+
const config = router._def._config;
|
|
1589
|
+
const url = new URL(req.url);
|
|
1590
|
+
if (req.method === "HEAD") return new Response(null, { status: 204 });
|
|
1591
|
+
const allowBatching = opts.allowBatching ?? opts.batching?.enabled ?? true;
|
|
1592
|
+
const allowMethodOverride = (opts.allowMethodOverride ?? false) && req.method === "POST";
|
|
1593
|
+
const infoTuple = await run(async () => {
|
|
1594
|
+
try {
|
|
1595
|
+
return [void 0, await getRequestInfo({
|
|
1596
|
+
req,
|
|
1597
|
+
path: decodeURIComponent(opts.path),
|
|
1598
|
+
router,
|
|
1599
|
+
searchParams: url.searchParams,
|
|
1600
|
+
headers: opts.req.headers,
|
|
1601
|
+
url
|
|
1602
|
+
})];
|
|
1603
|
+
} catch (cause) {
|
|
1604
|
+
return [getTRPCErrorFromUnknown(cause), void 0];
|
|
1605
|
+
}
|
|
1606
|
+
});
|
|
1607
|
+
const ctxManager = run(() => {
|
|
1608
|
+
let result = void 0;
|
|
1609
|
+
return {
|
|
1610
|
+
valueOrUndefined: () => {
|
|
1611
|
+
if (!result) return void 0;
|
|
1612
|
+
return result[1];
|
|
1613
|
+
},
|
|
1614
|
+
value: () => {
|
|
1615
|
+
const [err, ctx] = result;
|
|
1616
|
+
if (err) throw err;
|
|
1617
|
+
return ctx;
|
|
1618
|
+
},
|
|
1619
|
+
create: async (info) => {
|
|
1620
|
+
if (result) throw new Error("This should only be called once - report a bug in tRPC");
|
|
1621
|
+
try {
|
|
1622
|
+
const ctx = await opts.createContext({ info });
|
|
1623
|
+
result = [void 0, ctx];
|
|
1624
|
+
} catch (cause) {
|
|
1625
|
+
result = [getTRPCErrorFromUnknown(cause), void 0];
|
|
1626
|
+
}
|
|
1627
|
+
}
|
|
1628
|
+
};
|
|
1629
|
+
});
|
|
1630
|
+
const methodMapper = allowMethodOverride ? TYPE_ACCEPTED_METHOD_MAP_WITH_METHOD_OVERRIDE : TYPE_ACCEPTED_METHOD_MAP;
|
|
1631
|
+
/**
|
|
1632
|
+
* @deprecated
|
|
1633
|
+
*/
|
|
1634
|
+
const isStreamCall = req.headers.get("trpc-accept") === "application/jsonl";
|
|
1635
|
+
const experimentalSSE = config.sse?.enabled ?? true;
|
|
1636
|
+
try {
|
|
1637
|
+
const [infoError, info] = infoTuple;
|
|
1638
|
+
if (infoError) throw infoError;
|
|
1639
|
+
if (info.isBatchCall && !allowBatching) throw new TRPCError({
|
|
1640
|
+
code: "BAD_REQUEST",
|
|
1641
|
+
message: `Batching is not enabled on the server`
|
|
1642
|
+
});
|
|
1643
|
+
/* istanbul ignore if -- @preserve */
|
|
1644
|
+
if (isStreamCall && !info.isBatchCall) throw new TRPCError({
|
|
1645
|
+
message: `Streaming requests must be batched (you can do a batch of 1)`,
|
|
1646
|
+
code: "BAD_REQUEST"
|
|
1647
|
+
});
|
|
1648
|
+
await ctxManager.create(info);
|
|
1649
|
+
const rpcCalls = info.calls.map(async (call) => {
|
|
1650
|
+
const proc = call.procedure;
|
|
1651
|
+
try {
|
|
1652
|
+
if (opts.error) throw opts.error;
|
|
1653
|
+
if (!proc) throw new TRPCError({
|
|
1654
|
+
code: "NOT_FOUND",
|
|
1655
|
+
message: `No procedure found on path "${call.path}"`
|
|
1656
|
+
});
|
|
1657
|
+
if (!methodMapper[proc._def.type].includes(req.method)) throw new TRPCError({
|
|
1658
|
+
code: "METHOD_NOT_SUPPORTED",
|
|
1659
|
+
message: `Unsupported ${req.method}-request to ${proc._def.type} procedure at path "${call.path}"`
|
|
1660
|
+
});
|
|
1661
|
+
if (proc._def.type === "subscription") {
|
|
1662
|
+
/* istanbul ignore if -- @preserve */
|
|
1663
|
+
if (info.isBatchCall) throw new TRPCError({
|
|
1664
|
+
code: "BAD_REQUEST",
|
|
1665
|
+
message: `Cannot batch subscription calls`
|
|
1666
|
+
});
|
|
1667
|
+
}
|
|
1668
|
+
const data = await proc({
|
|
1669
|
+
path: call.path,
|
|
1670
|
+
getRawInput: call.getRawInput,
|
|
1671
|
+
ctx: ctxManager.value(),
|
|
1672
|
+
type: proc._def.type,
|
|
1673
|
+
signal: opts.req.signal
|
|
1674
|
+
});
|
|
1675
|
+
return [void 0, { data }];
|
|
1676
|
+
} catch (cause) {
|
|
1677
|
+
const error = getTRPCErrorFromUnknown(cause);
|
|
1678
|
+
const input = call.result();
|
|
1679
|
+
opts.onError?.({
|
|
1680
|
+
error,
|
|
1681
|
+
path: call.path,
|
|
1682
|
+
input,
|
|
1683
|
+
ctx: ctxManager.valueOrUndefined(),
|
|
1684
|
+
type: call.procedure?._def.type ?? "unknown",
|
|
1685
|
+
req: opts.req
|
|
1686
|
+
});
|
|
1687
|
+
return [error, void 0];
|
|
1688
|
+
}
|
|
1689
|
+
});
|
|
1690
|
+
if (!info.isBatchCall) {
|
|
1691
|
+
const [call] = info.calls;
|
|
1692
|
+
const [error, result] = await rpcCalls[0];
|
|
1693
|
+
switch (info.type) {
|
|
1694
|
+
case "unknown":
|
|
1695
|
+
case "mutation":
|
|
1696
|
+
case "query": {
|
|
1697
|
+
headers.set("content-type", "application/json");
|
|
1698
|
+
if (isDataStream(result?.data)) throw new TRPCError({
|
|
1699
|
+
code: "UNSUPPORTED_MEDIA_TYPE",
|
|
1700
|
+
message: "Cannot use stream-like response in non-streaming request - use httpBatchStreamLink"
|
|
1701
|
+
});
|
|
1702
|
+
const res = error ? { error: getErrorShape({
|
|
1703
|
+
config,
|
|
1704
|
+
ctx: ctxManager.valueOrUndefined(),
|
|
1705
|
+
error,
|
|
1706
|
+
input: call.result(),
|
|
1707
|
+
path: call.path,
|
|
1708
|
+
type: info.type
|
|
1709
|
+
}) } : { result: { data: result.data } };
|
|
1710
|
+
const headResponse$1 = initResponse({
|
|
1711
|
+
ctx: ctxManager.valueOrUndefined(),
|
|
1712
|
+
info,
|
|
1713
|
+
responseMeta: opts.responseMeta,
|
|
1714
|
+
errors: error ? [error] : [],
|
|
1715
|
+
headers,
|
|
1716
|
+
untransformedJSON: [res]
|
|
1717
|
+
});
|
|
1718
|
+
return new Response(JSON.stringify(transformTRPCResponse(config, res)), {
|
|
1719
|
+
status: headResponse$1.status,
|
|
1720
|
+
headers
|
|
1721
|
+
});
|
|
1722
|
+
}
|
|
1723
|
+
case "subscription": {
|
|
1724
|
+
const iterable = run(() => {
|
|
1725
|
+
if (error) return errorToAsyncIterable(error);
|
|
1726
|
+
if (!experimentalSSE) return errorToAsyncIterable(new TRPCError({
|
|
1727
|
+
code: "METHOD_NOT_SUPPORTED",
|
|
1728
|
+
message: "Missing experimental flag \"sseSubscriptions\""
|
|
1729
|
+
}));
|
|
1730
|
+
if (!isObservable(result.data) && !isAsyncIterable(result.data)) return errorToAsyncIterable(new TRPCError({
|
|
1731
|
+
message: `Subscription ${call.path} did not return an observable or a AsyncGenerator`,
|
|
1732
|
+
code: "INTERNAL_SERVER_ERROR"
|
|
1733
|
+
}));
|
|
1734
|
+
const dataAsIterable = isObservable(result.data) ? observableToAsyncIterable(result.data, opts.req.signal) : result.data;
|
|
1735
|
+
return dataAsIterable;
|
|
1736
|
+
});
|
|
1737
|
+
const stream = sseStreamProducer({
|
|
1738
|
+
...config.sse,
|
|
1739
|
+
data: iterable,
|
|
1740
|
+
serialize: (v) => config.transformer.output.serialize(v),
|
|
1741
|
+
formatError(errorOpts) {
|
|
1742
|
+
const error$1 = getTRPCErrorFromUnknown(errorOpts.error);
|
|
1743
|
+
const input = call?.result();
|
|
1744
|
+
const path = call?.path;
|
|
1745
|
+
const type = call?.procedure?._def.type ?? "unknown";
|
|
1746
|
+
opts.onError?.({
|
|
1747
|
+
error: error$1,
|
|
1748
|
+
path,
|
|
1749
|
+
input,
|
|
1750
|
+
ctx: ctxManager.valueOrUndefined(),
|
|
1751
|
+
req: opts.req,
|
|
1752
|
+
type
|
|
1753
|
+
});
|
|
1754
|
+
const shape = getErrorShape({
|
|
1755
|
+
config,
|
|
1756
|
+
ctx: ctxManager.valueOrUndefined(),
|
|
1757
|
+
error: error$1,
|
|
1758
|
+
input,
|
|
1759
|
+
path,
|
|
1760
|
+
type
|
|
1761
|
+
});
|
|
1762
|
+
return shape;
|
|
1763
|
+
}
|
|
1764
|
+
});
|
|
1765
|
+
for (const [key, value] of Object.entries(sseHeaders)) headers.set(key, value);
|
|
1766
|
+
const headResponse$1 = initResponse({
|
|
1767
|
+
ctx: ctxManager.valueOrUndefined(),
|
|
1768
|
+
info,
|
|
1769
|
+
responseMeta: opts.responseMeta,
|
|
1770
|
+
errors: [],
|
|
1771
|
+
headers,
|
|
1772
|
+
untransformedJSON: null
|
|
1773
|
+
});
|
|
1774
|
+
return new Response(stream, {
|
|
1775
|
+
headers,
|
|
1776
|
+
status: headResponse$1.status
|
|
1777
|
+
});
|
|
1778
|
+
}
|
|
1779
|
+
}
|
|
1780
|
+
}
|
|
1781
|
+
if (info.accept === "application/jsonl") {
|
|
1782
|
+
headers.set("content-type", "application/json");
|
|
1783
|
+
headers.set("transfer-encoding", "chunked");
|
|
1784
|
+
const headResponse$1 = initResponse({
|
|
1785
|
+
ctx: ctxManager.valueOrUndefined(),
|
|
1786
|
+
info,
|
|
1787
|
+
responseMeta: opts.responseMeta,
|
|
1788
|
+
errors: [],
|
|
1789
|
+
headers,
|
|
1790
|
+
untransformedJSON: null
|
|
1791
|
+
});
|
|
1792
|
+
const stream = jsonlStreamProducer({
|
|
1793
|
+
...config.jsonl,
|
|
1794
|
+
maxDepth: Infinity,
|
|
1795
|
+
data: rpcCalls.map(async (res) => {
|
|
1796
|
+
const [error, result] = await res;
|
|
1797
|
+
const call = info.calls[0];
|
|
1798
|
+
if (error) return { error: getErrorShape({
|
|
1799
|
+
config,
|
|
1800
|
+
ctx: ctxManager.valueOrUndefined(),
|
|
1801
|
+
error,
|
|
1802
|
+
input: call.result(),
|
|
1803
|
+
path: call.path,
|
|
1804
|
+
type: call.procedure?._def.type ?? "unknown"
|
|
1805
|
+
}) };
|
|
1806
|
+
/**
|
|
1807
|
+
* Not very pretty, but we need to wrap nested data in promises
|
|
1808
|
+
* Our stream producer will only resolve top-level async values or async values that are directly nested in another async value
|
|
1809
|
+
*/
|
|
1810
|
+
const iterable = isObservable(result.data) ? observableToAsyncIterable(result.data, opts.req.signal) : Promise.resolve(result.data);
|
|
1811
|
+
return { result: Promise.resolve({ data: iterable }) };
|
|
1812
|
+
}),
|
|
1813
|
+
serialize: config.transformer.output.serialize,
|
|
1814
|
+
onError: (cause) => {
|
|
1815
|
+
opts.onError?.({
|
|
1816
|
+
error: getTRPCErrorFromUnknown(cause),
|
|
1817
|
+
path: void 0,
|
|
1818
|
+
input: void 0,
|
|
1819
|
+
ctx: ctxManager.valueOrUndefined(),
|
|
1820
|
+
req: opts.req,
|
|
1821
|
+
type: info?.type ?? "unknown"
|
|
1822
|
+
});
|
|
1823
|
+
},
|
|
1824
|
+
formatError(errorOpts) {
|
|
1825
|
+
const call = info?.calls[errorOpts.path[0]];
|
|
1826
|
+
const error = getTRPCErrorFromUnknown(errorOpts.error);
|
|
1827
|
+
const input = call?.result();
|
|
1828
|
+
const path = call?.path;
|
|
1829
|
+
const type = call?.procedure?._def.type ?? "unknown";
|
|
1830
|
+
const shape = getErrorShape({
|
|
1831
|
+
config,
|
|
1832
|
+
ctx: ctxManager.valueOrUndefined(),
|
|
1833
|
+
error,
|
|
1834
|
+
input,
|
|
1835
|
+
path,
|
|
1836
|
+
type
|
|
1837
|
+
});
|
|
1838
|
+
return shape;
|
|
1839
|
+
}
|
|
1840
|
+
});
|
|
1841
|
+
return new Response(stream, {
|
|
1842
|
+
headers,
|
|
1843
|
+
status: headResponse$1.status
|
|
1844
|
+
});
|
|
1845
|
+
}
|
|
1846
|
+
/**
|
|
1847
|
+
* Non-streaming response:
|
|
1848
|
+
* - await all responses in parallel, blocking on the slowest one
|
|
1849
|
+
* - create headers with known response body
|
|
1850
|
+
* - return a complete HTTPResponse
|
|
1851
|
+
*/
|
|
1852
|
+
headers.set("content-type", "application/json");
|
|
1853
|
+
const results = (await Promise.all(rpcCalls)).map((res) => {
|
|
1854
|
+
const [error, result] = res;
|
|
1855
|
+
if (error) return res;
|
|
1856
|
+
if (isDataStream(result.data)) return [new TRPCError({
|
|
1857
|
+
code: "UNSUPPORTED_MEDIA_TYPE",
|
|
1858
|
+
message: "Cannot use stream-like response in non-streaming request - use httpBatchStreamLink"
|
|
1859
|
+
}), void 0];
|
|
1860
|
+
return res;
|
|
1861
|
+
});
|
|
1862
|
+
const resultAsRPCResponse = results.map(([error, result], index) => {
|
|
1863
|
+
const call = info.calls[index];
|
|
1864
|
+
if (error) return { error: getErrorShape({
|
|
1865
|
+
config,
|
|
1866
|
+
ctx: ctxManager.valueOrUndefined(),
|
|
1867
|
+
error,
|
|
1868
|
+
input: call.result(),
|
|
1869
|
+
path: call.path,
|
|
1870
|
+
type: call.procedure?._def.type ?? "unknown"
|
|
1871
|
+
}) };
|
|
1872
|
+
return { result: { data: result.data } };
|
|
1873
|
+
});
|
|
1874
|
+
const errors = results.map(([error]) => error).filter(Boolean);
|
|
1875
|
+
const headResponse = initResponse({
|
|
1876
|
+
ctx: ctxManager.valueOrUndefined(),
|
|
1877
|
+
info,
|
|
1878
|
+
responseMeta: opts.responseMeta,
|
|
1879
|
+
untransformedJSON: resultAsRPCResponse,
|
|
1880
|
+
errors,
|
|
1881
|
+
headers
|
|
1882
|
+
});
|
|
1883
|
+
return new Response(JSON.stringify(transformTRPCResponse(config, resultAsRPCResponse)), {
|
|
1884
|
+
status: headResponse.status,
|
|
1885
|
+
headers
|
|
1886
|
+
});
|
|
1887
|
+
} catch (cause) {
|
|
1888
|
+
const [_infoError, info] = infoTuple;
|
|
1889
|
+
const ctx = ctxManager.valueOrUndefined();
|
|
1890
|
+
const { error, untransformedJSON, body } = caughtErrorToData(cause, {
|
|
1891
|
+
opts,
|
|
1892
|
+
ctx: ctxManager.valueOrUndefined(),
|
|
1893
|
+
type: info?.type ?? "unknown"
|
|
1894
|
+
});
|
|
1895
|
+
const headResponse = initResponse({
|
|
1896
|
+
ctx,
|
|
1897
|
+
info,
|
|
1898
|
+
responseMeta: opts.responseMeta,
|
|
1899
|
+
untransformedJSON,
|
|
1900
|
+
errors: [error],
|
|
1901
|
+
headers
|
|
1902
|
+
});
|
|
1903
|
+
return new Response(body, {
|
|
1904
|
+
status: headResponse.status,
|
|
1905
|
+
headers
|
|
1906
|
+
});
|
|
1907
|
+
}
|
|
1908
|
+
}
|
|
1909
|
+
|
|
1910
|
+
//#endregion
|
|
1911
|
+
export { Unpromise, __commonJS, __toESM, createDeferred, getRequestInfo, isAbortError, isPromise, iteratorResource, jsonlStreamConsumer, jsonlStreamProducer, makeAsyncResource, makeResource, parseConnectionParamsFromString, parseConnectionParamsFromUnknown, require_usingCtx, resolveResponse, sseHeaders, sseStreamConsumer, sseStreamProducer, takeWithGrace, throwAbortError, withMaxDuration };
|
|
1912
|
+
//# sourceMappingURL=resolveResponse-DPbYgJDD.mjs.map
|