@trpc/server 11.0.0-rc.645 → 11.0.0-rc.648

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.
Files changed (84) hide show
  1. package/dist/adapters/aws-lambda/index.js +1 -1
  2. package/dist/adapters/aws-lambda/index.mjs +1 -1
  3. package/dist/adapters/express.js +1 -1
  4. package/dist/adapters/express.mjs +1 -1
  5. package/dist/adapters/fastify/fastifyRequestHandler.js +1 -1
  6. package/dist/adapters/fastify/fastifyRequestHandler.mjs +1 -1
  7. package/dist/adapters/fetch/fetchRequestHandler.js +1 -1
  8. package/dist/adapters/fetch/fetchRequestHandler.mjs +1 -1
  9. package/dist/adapters/next-app-dir/nextAppDirCaller.js +1 -1
  10. package/dist/adapters/next-app-dir/nextAppDirCaller.mjs +1 -1
  11. package/dist/adapters/next-app-dir/notFound.js +1 -1
  12. package/dist/adapters/next-app-dir/notFound.mjs +1 -1
  13. package/dist/adapters/next-app-dir/redirect.js +15 -3
  14. package/dist/adapters/next-app-dir/redirect.mjs +14 -2
  15. package/dist/adapters/next.js +1 -1
  16. package/dist/adapters/next.mjs +1 -1
  17. package/dist/adapters/node-http/incomingMessageToRequest.js +1 -1
  18. package/dist/adapters/node-http/incomingMessageToRequest.mjs +1 -1
  19. package/dist/adapters/node-http/nodeHTTPRequestHandler.js +1 -1
  20. package/dist/adapters/node-http/nodeHTTPRequestHandler.mjs +1 -1
  21. package/dist/adapters/node-http/writeResponse.js +1 -1
  22. package/dist/adapters/node-http/writeResponse.mjs +1 -1
  23. package/dist/adapters/standalone.js +1 -1
  24. package/dist/adapters/standalone.mjs +1 -1
  25. package/dist/adapters/ws.d.ts.map +1 -1
  26. package/dist/adapters/ws.js +144 -65
  27. package/dist/adapters/ws.mjs +144 -65
  28. package/dist/bundle-analysis.json +190 -183
  29. package/dist/http.js +1 -1
  30. package/dist/http.mjs +1 -1
  31. package/dist/index.js +1 -1
  32. package/dist/index.mjs +1 -1
  33. package/dist/node_modules/.pnpm/@rollup_plugin-typescript@12.1.1_rollup@4.27.4_tslib@2.8.1_typescript@5.6.2/node_modules/tslib/tslib.es6.js +73 -0
  34. package/dist/rpc.js +1 -1
  35. package/dist/rpc.mjs +1 -1
  36. package/dist/shared.js +1 -1
  37. package/dist/shared.mjs +1 -1
  38. package/dist/unstable-core-do-not-import/error/TRPCError.d.ts +1 -1
  39. package/dist/unstable-core-do-not-import/error/TRPCError.js +14 -2
  40. package/dist/unstable-core-do-not-import/error/TRPCError.mjs +13 -1
  41. package/dist/unstable-core-do-not-import/http/resolveResponse.d.ts.map +1 -1
  42. package/dist/unstable-core-do-not-import/http/resolveResponse.js +3 -9
  43. package/dist/unstable-core-do-not-import/http/resolveResponse.mjs +3 -9
  44. package/dist/unstable-core-do-not-import/stream/jsonl.d.ts +7 -33
  45. package/dist/unstable-core-do-not-import/stream/jsonl.d.ts.map +1 -1
  46. package/dist/unstable-core-do-not-import/stream/jsonl.js +338 -209
  47. package/dist/unstable-core-do-not-import/stream/jsonl.mjs +337 -208
  48. package/dist/unstable-core-do-not-import/stream/sse.d.ts.map +1 -1
  49. package/dist/unstable-core-do-not-import/stream/sse.js +143 -74
  50. package/dist/unstable-core-do-not-import/stream/sse.mjs +144 -75
  51. package/dist/unstable-core-do-not-import/stream/utils/asyncIterable.d.ts +1 -0
  52. package/dist/unstable-core-do-not-import/stream/utils/asyncIterable.d.ts.map +1 -1
  53. package/dist/unstable-core-do-not-import/stream/utils/asyncIterable.js +103 -10
  54. package/dist/unstable-core-do-not-import/stream/utils/asyncIterable.mjs +104 -12
  55. package/dist/unstable-core-do-not-import/stream/utils/disposable.d.ts +17 -0
  56. package/dist/unstable-core-do-not-import/stream/utils/disposable.d.ts.map +1 -0
  57. package/dist/unstable-core-do-not-import/stream/utils/disposable.js +44 -0
  58. package/dist/unstable-core-do-not-import/stream/utils/disposable.mjs +41 -0
  59. package/dist/unstable-core-do-not-import/stream/utils/timerResource.d.ts +1 -2
  60. package/dist/unstable-core-do-not-import/stream/utils/timerResource.d.ts.map +1 -1
  61. package/dist/unstable-core-do-not-import/stream/utils/timerResource.js +8 -10
  62. package/dist/unstable-core-do-not-import/stream/utils/timerResource.mjs +8 -10
  63. package/dist/unstable-core-do-not-import/stream/utils/withPing.d.ts.map +1 -1
  64. package/dist/unstable-core-do-not-import/stream/utils/withPing.js +113 -24
  65. package/dist/unstable-core-do-not-import/stream/utils/withPing.mjs +114 -25
  66. package/dist/unstable-core-do-not-import/transformer.d.ts +1 -1
  67. package/dist/unstable-core-do-not-import.d.ts +1 -0
  68. package/dist/unstable-core-do-not-import.d.ts.map +1 -1
  69. package/dist/unstable-core-do-not-import.js +3 -0
  70. package/dist/unstable-core-do-not-import.mjs +1 -0
  71. package/dist/vendor/unpromise/unpromise.js +17 -6
  72. package/dist/vendor/unpromise/unpromise.mjs +13 -2
  73. package/package.json +2 -3
  74. package/src/adapters/ws.ts +7 -8
  75. package/src/unstable-core-do-not-import/http/resolveResponse.ts +3 -9
  76. package/src/unstable-core-do-not-import/stream/jsonl.ts +128 -153
  77. package/src/unstable-core-do-not-import/stream/sse.ts +64 -79
  78. package/src/unstable-core-do-not-import/stream/utils/asyncIterable.ts +50 -48
  79. package/src/unstable-core-do-not-import/stream/utils/disposable.ts +52 -0
  80. package/src/unstable-core-do-not-import/stream/utils/timerResource.ts +17 -15
  81. package/src/unstable-core-do-not-import/stream/utils/withPing.ts +18 -19
  82. package/src/unstable-core-do-not-import.ts +1 -0
  83. package/dist/node_modules/.pnpm/@swc_helpers@0.5.13/node_modules/@swc/helpers/esm/_define_property.js +0 -11
  84. package/dist/node_modules/.pnpm/@swc_helpers@0.5.13/node_modules/@swc/helpers/esm/_define_property.mjs +0 -9
@@ -1,12 +1,91 @@
1
1
  import { Unpromise } from '../../../vendor/unpromise/unpromise.mjs';
2
- import { disposablePromiseTimerResult, timerResource } from './timerResource.mjs';
2
+ import { makeAsyncResource } from './disposable.mjs';
3
+ import { timerResource, disposablePromiseTimerResult } from './timerResource.mjs';
3
4
 
5
+ function _ts_add_disposable_resource(env, value, async) {
6
+ if (value !== null && value !== void 0) {
7
+ if (typeof value !== "object" && typeof value !== "function") throw new TypeError("Object expected.");
8
+ var dispose, inner;
9
+ if (async) {
10
+ if (!Symbol.asyncDispose) throw new TypeError("Symbol.asyncDispose is not defined.");
11
+ dispose = value[Symbol.asyncDispose];
12
+ }
13
+ if (dispose === void 0) {
14
+ if (!Symbol.dispose) throw new TypeError("Symbol.dispose is not defined.");
15
+ dispose = value[Symbol.dispose];
16
+ if (async) inner = dispose;
17
+ }
18
+ if (typeof dispose !== "function") throw new TypeError("Object not disposable.");
19
+ if (inner) dispose = function() {
20
+ try {
21
+ inner.call(this);
22
+ } catch (e) {
23
+ return Promise.reject(e);
24
+ }
25
+ };
26
+ env.stack.push({
27
+ value: value,
28
+ dispose: dispose,
29
+ async: async
30
+ });
31
+ } else if (async) {
32
+ env.stack.push({
33
+ async: true
34
+ });
35
+ }
36
+ return value;
37
+ }
38
+ function _ts_dispose_resources(env) {
39
+ var _SuppressedError = typeof SuppressedError === "function" ? SuppressedError : function(error, suppressed, message) {
40
+ var e = new Error(message);
41
+ return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
42
+ };
43
+ return (_ts_dispose_resources = function _ts_dispose_resources(env) {
44
+ function fail(e) {
45
+ env.error = env.hasError ? new _SuppressedError(e, env.error, "An error was suppressed during disposal.") : e;
46
+ env.hasError = true;
47
+ }
48
+ var r, s = 0;
49
+ function next() {
50
+ while(r = env.stack.pop()){
51
+ try {
52
+ if (!r.async && s === 1) return s = 0, env.stack.push(r), Promise.resolve().then(next);
53
+ if (r.dispose) {
54
+ var result = r.dispose.call(r.value);
55
+ if (r.async) return s |= 2, Promise.resolve(result).then(next, function(e) {
56
+ fail(e);
57
+ return next();
58
+ });
59
+ } else s |= 1;
60
+ } catch (e) {
61
+ fail(e);
62
+ }
63
+ }
64
+ if (s === 1) return env.hasError ? Promise.reject(env.error) : Promise.resolve();
65
+ if (env.hasError) throw env.error;
66
+ }
67
+ return next();
68
+ })(env);
69
+ }
70
+ function iteratorResource(iterable) {
71
+ const iterator = iterable[Symbol.asyncIterator]();
72
+ return makeAsyncResource(iterator, async ()=>{
73
+ await iterator.return?.();
74
+ });
75
+ }
4
76
  /**
5
77
  * Derives a new {@link AsyncGenerator} based on {@link iterable}, that automatically stops after the specified duration.
6
78
  */ async function* withMaxDuration(iterable, opts) {
7
- const iterator = iterable[Symbol.asyncIterator]();
8
- const timer = timerResource(opts.maxDurationMs);
79
+ const env = {
80
+ stack: [],
81
+ error: void 0,
82
+ hasError: false
83
+ };
9
84
  try {
85
+ const iterator = _ts_add_disposable_resource(env, iteratorResource(iterable), true);
86
+ ;
87
+ const timer = _ts_add_disposable_resource(env, timerResource(opts.maxDurationMs), false);
88
+ ;
10
89
  const timerPromise = timer.start();
11
90
  // declaration outside the loop for garbage collection reasons
12
91
  let result;
@@ -27,10 +106,12 @@ import { disposablePromiseTimerResult, timerResource } from './timerResource.mjs
27
106
  // free up reference for garbage collection
28
107
  result = null;
29
108
  }
109
+ } catch (e) {
110
+ env.error = e;
111
+ env.hasError = true;
30
112
  } finally{
31
- // dispose timer
32
- // Shouldn't be needed, but build breaks with `using` keyword
33
- timer[Symbol.dispose]();
113
+ const result = _ts_dispose_resources(env);
114
+ if (result) await result;
34
115
  }
35
116
  }
36
117
  /**
@@ -38,11 +119,18 @@ import { disposablePromiseTimerResult, timerResource } from './timerResource.mjs
38
119
  * {@link count} values. Then, a grace period of {@link gracePeriodMs} is started in which further
39
120
  * values may still come through. After this period, the generator stops.
40
121
  */ async function* takeWithGrace(iterable, opts) {
41
- const iterator = iterable[Symbol.asyncIterator]();
42
- // declaration outside the loop for garbage collection reasons
43
- let result;
44
- const timer = timerResource(opts.gracePeriodMs);
122
+ const env = {
123
+ stack: [],
124
+ error: void 0,
125
+ hasError: false
126
+ };
45
127
  try {
128
+ const iterator = _ts_add_disposable_resource(env, iteratorResource(iterable), true);
129
+ ;
130
+ // declaration outside the loop for garbage collection reasons
131
+ let result;
132
+ const timer = _ts_add_disposable_resource(env, timerResource(opts.gracePeriodMs), false);
133
+ ;
46
134
  let count = opts.count;
47
135
  let timerPromise = new Promise(()=>{
48
136
  // never resolves
@@ -67,9 +155,13 @@ import { disposablePromiseTimerResult, timerResource } from './timerResource.mjs
67
155
  // free up reference for garbage collection
68
156
  result = null;
69
157
  }
158
+ } catch (e) {
159
+ env.error = e;
160
+ env.hasError = true;
70
161
  } finally{
71
- timer[Symbol.dispose]();
162
+ const result = _ts_dispose_resources(env);
163
+ if (result) await result;
72
164
  }
73
165
  }
74
166
 
75
- export { takeWithGrace, withMaxDuration };
167
+ export { iteratorResource, takeWithGrace, withMaxDuration };
@@ -0,0 +1,17 @@
1
+ /**
2
+ * Takes a value and a dispose function and returns a new object that implements the Disposable interface.
3
+ * The returned object is the original value augmented with a Symbol.dispose method.
4
+ * @param thing The value to make disposable
5
+ * @param dispose Function to call when disposing the resource
6
+ * @returns The original value with Symbol.dispose method added
7
+ */
8
+ export declare function makeResource<T>(thing: T, dispose: () => void): T & Disposable;
9
+ /**
10
+ * Takes a value and an async dispose function and returns a new object that implements the AsyncDisposable interface.
11
+ * The returned object is the original value augmented with a Symbol.asyncDispose method.
12
+ * @param thing The value to make async disposable
13
+ * @param dispose Async function to call when disposing the resource
14
+ * @returns The original value with Symbol.asyncDispose method added
15
+ */
16
+ export declare function makeAsyncResource<T>(thing: T, dispose: () => Promise<void>): T & AsyncDisposable;
17
+ //# sourceMappingURL=disposable.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"disposable.d.ts","sourceRoot":"","sources":["../../../../src/unstable-core-do-not-import/stream/utils/disposable.ts"],"names":[],"mappings":"AAQA;;;;;;GAMG;AACH,wBAAgB,YAAY,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,EAAE,OAAO,EAAE,MAAM,IAAI,GAAG,CAAC,GAAG,UAAU,CAY7E;AAED;;;;;;GAMG;AACH,wBAAgB,iBAAiB,CAAC,CAAC,EACjC,KAAK,EAAE,CAAC,EACR,OAAO,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,GAC3B,CAAC,GAAG,eAAe,CAYrB"}
@@ -0,0 +1,44 @@
1
+ 'use strict';
2
+
3
+ // @ts-expect-error - polyfilling symbol
4
+ // eslint-disable-next-line no-restricted-syntax
5
+ var _Symbol, // @ts-expect-error - polyfilling symbol
6
+ // eslint-disable-next-line no-restricted-syntax
7
+ _Symbol1;
8
+ (_Symbol = Symbol).dispose ?? (_Symbol.dispose = Symbol());
9
+ (_Symbol1 = Symbol).asyncDispose ?? (_Symbol1.asyncDispose = Symbol());
10
+ /**
11
+ * Takes a value and a dispose function and returns a new object that implements the Disposable interface.
12
+ * The returned object is the original value augmented with a Symbol.dispose method.
13
+ * @param thing The value to make disposable
14
+ * @param dispose Function to call when disposing the resource
15
+ * @returns The original value with Symbol.dispose method added
16
+ */ function makeResource(thing, dispose) {
17
+ const it = thing;
18
+ // eslint-disable-next-line no-restricted-syntax
19
+ if (it[Symbol.dispose]) {
20
+ throw new Error('Symbol.dispose already exists');
21
+ }
22
+ // eslint-disable-next-line no-restricted-syntax
23
+ it[Symbol.dispose] = dispose;
24
+ return it;
25
+ }
26
+ /**
27
+ * Takes a value and an async dispose function and returns a new object that implements the AsyncDisposable interface.
28
+ * The returned object is the original value augmented with a Symbol.asyncDispose method.
29
+ * @param thing The value to make async disposable
30
+ * @param dispose Async function to call when disposing the resource
31
+ * @returns The original value with Symbol.asyncDispose method added
32
+ */ function makeAsyncResource(thing, dispose) {
33
+ const it = thing;
34
+ // eslint-disable-next-line no-restricted-syntax
35
+ if (it[Symbol.asyncDispose]) {
36
+ throw new Error('Symbol.asyncDispose already exists');
37
+ }
38
+ // eslint-disable-next-line no-restricted-syntax
39
+ it[Symbol.asyncDispose] = dispose;
40
+ return it;
41
+ }
42
+
43
+ exports.makeAsyncResource = makeAsyncResource;
44
+ exports.makeResource = makeResource;
@@ -0,0 +1,41 @@
1
+ // @ts-expect-error - polyfilling symbol
2
+ // eslint-disable-next-line no-restricted-syntax
3
+ var _Symbol, // @ts-expect-error - polyfilling symbol
4
+ // eslint-disable-next-line no-restricted-syntax
5
+ _Symbol1;
6
+ (_Symbol = Symbol).dispose ?? (_Symbol.dispose = Symbol());
7
+ (_Symbol1 = Symbol).asyncDispose ?? (_Symbol1.asyncDispose = Symbol());
8
+ /**
9
+ * Takes a value and a dispose function and returns a new object that implements the Disposable interface.
10
+ * The returned object is the original value augmented with a Symbol.dispose method.
11
+ * @param thing The value to make disposable
12
+ * @param dispose Function to call when disposing the resource
13
+ * @returns The original value with Symbol.dispose method added
14
+ */ function makeResource(thing, dispose) {
15
+ const it = thing;
16
+ // eslint-disable-next-line no-restricted-syntax
17
+ if (it[Symbol.dispose]) {
18
+ throw new Error('Symbol.dispose already exists');
19
+ }
20
+ // eslint-disable-next-line no-restricted-syntax
21
+ it[Symbol.dispose] = dispose;
22
+ return it;
23
+ }
24
+ /**
25
+ * Takes a value and an async dispose function and returns a new object that implements the AsyncDisposable interface.
26
+ * The returned object is the original value augmented with a Symbol.asyncDispose method.
27
+ * @param thing The value to make async disposable
28
+ * @param dispose Async function to call when disposing the resource
29
+ * @returns The original value with Symbol.asyncDispose method added
30
+ */ function makeAsyncResource(thing, dispose) {
31
+ const it = thing;
32
+ // eslint-disable-next-line no-restricted-syntax
33
+ if (it[Symbol.asyncDispose]) {
34
+ throw new Error('Symbol.asyncDispose already exists');
35
+ }
36
+ // eslint-disable-next-line no-restricted-syntax
37
+ it[Symbol.asyncDispose] = dispose;
38
+ return it;
39
+ }
40
+
41
+ export { makeAsyncResource, makeResource };
@@ -1,6 +1,5 @@
1
1
  export declare const disposablePromiseTimerResult: unique symbol;
2
2
  export declare function timerResource(ms: number): {
3
3
  start(): Promise<typeof disposablePromiseTimerResult>;
4
- [Symbol.dispose]: () => void;
5
- };
4
+ } & Disposable;
6
5
  //# sourceMappingURL=timerResource.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"timerResource.d.ts","sourceRoot":"","sources":["../../../../src/unstable-core-do-not-import/stream/utils/timerResource.ts"],"names":[],"mappings":"AAGA,eAAO,MAAM,4BAA4B,eAAW,CAAC;AACrD,wBAAgB,aAAa,CAAC,EAAE,EAAE,MAAM;;;EAsBvC"}
1
+ {"version":3,"file":"timerResource.d.ts","sourceRoot":"","sources":["../../../../src/unstable-core-do-not-import/stream/utils/timerResource.ts"],"names":[],"mappings":"AAEA,eAAO,MAAM,4BAA4B,eAAW,CAAC;AAErD,wBAAgB,aAAa,CAAC,EAAE,EAAE,MAAM;;eAwBvC"}
@@ -1,12 +1,11 @@
1
1
  'use strict';
2
2
 
3
- // @ts-expect-error polyfill
4
- var _Symbol;
5
- (_Symbol = Symbol).dispose ?? (_Symbol.dispose = Symbol());
3
+ var disposable = require('./disposable.js');
4
+
6
5
  const disposablePromiseTimerResult = Symbol();
7
6
  function timerResource(ms) {
8
7
  let timer = null;
9
- return {
8
+ return disposable.makeResource({
10
9
  start () {
11
10
  if (timer) {
12
11
  throw new Error('Timer already started');
@@ -15,13 +14,12 @@ function timerResource(ms) {
15
14
  timer = setTimeout(()=>resolve(disposablePromiseTimerResult), ms);
16
15
  });
17
16
  return promise;
18
- },
19
- [Symbol.dispose]: ()=>{
20
- if (timer) {
21
- clearTimeout(timer);
22
- }
23
17
  }
24
- };
18
+ }, ()=>{
19
+ if (timer) {
20
+ clearTimeout(timer);
21
+ }
22
+ });
25
23
  }
26
24
 
27
25
  exports.disposablePromiseTimerResult = disposablePromiseTimerResult;
@@ -1,10 +1,9 @@
1
- // @ts-expect-error polyfill
2
- var _Symbol;
3
- (_Symbol = Symbol).dispose ?? (_Symbol.dispose = Symbol());
1
+ import { makeResource } from './disposable.mjs';
2
+
4
3
  const disposablePromiseTimerResult = Symbol();
5
4
  function timerResource(ms) {
6
5
  let timer = null;
7
- return {
6
+ return makeResource({
8
7
  start () {
9
8
  if (timer) {
10
9
  throw new Error('Timer already started');
@@ -13,13 +12,12 @@ function timerResource(ms) {
13
12
  timer = setTimeout(()=>resolve(disposablePromiseTimerResult), ms);
14
13
  });
15
14
  return promise;
16
- },
17
- [Symbol.dispose]: ()=>{
18
- if (timer) {
19
- clearTimeout(timer);
20
- }
21
15
  }
22
- };
16
+ }, ()=>{
17
+ if (timer) {
18
+ clearTimeout(timer);
19
+ }
20
+ });
23
21
  }
24
22
 
25
23
  export { disposablePromiseTimerResult, timerResource };
@@ -1 +1 @@
1
- {"version":3,"file":"withPing.d.ts","sourceRoot":"","sources":["../../../../src/unstable-core-do-not-import/stream/utils/withPing.ts"],"names":[],"mappings":"AAGA,eAAO,MAAM,QAAQ,eAAiB,CAAC;AAEvC;;;GAGG;AACH,wBAAuB,QAAQ,CAAC,MAAM,EACpC,QAAQ,EAAE,aAAa,CAAC,MAAM,CAAC,EAC/B,cAAc,EAAE,MAAM,GACrB,cAAc,CAAC,MAAM,GAAG,OAAO,QAAQ,CAAC,CAmC1C"}
1
+ {"version":3,"file":"withPing.d.ts","sourceRoot":"","sources":["../../../../src/unstable-core-do-not-import/stream/utils/withPing.ts"],"names":[],"mappings":"AAIA,eAAO,MAAM,QAAQ,eAAiB,CAAC;AAEvC;;;GAGG;AACH,wBAAuB,QAAQ,CAAC,MAAM,EACpC,QAAQ,EAAE,aAAa,CAAC,MAAM,CAAC,EAC/B,cAAc,EAAE,MAAM,GACrB,cAAc,CAAC,MAAM,GAAG,OAAO,QAAQ,CAAC,CAiC1C"}
@@ -1,39 +1,128 @@
1
1
  'use strict';
2
2
 
3
3
  var unpromise = require('../../../vendor/unpromise/unpromise.js');
4
+ var asyncIterable = require('./asyncIterable.js');
4
5
  var timerResource = require('./timerResource.js');
5
6
 
7
+ function _ts_add_disposable_resource(env, value, async) {
8
+ if (value !== null && value !== void 0) {
9
+ if (typeof value !== "object" && typeof value !== "function") throw new TypeError("Object expected.");
10
+ var dispose, inner;
11
+ if (async) {
12
+ if (!Symbol.asyncDispose) throw new TypeError("Symbol.asyncDispose is not defined.");
13
+ dispose = value[Symbol.asyncDispose];
14
+ }
15
+ if (dispose === void 0) {
16
+ if (!Symbol.dispose) throw new TypeError("Symbol.dispose is not defined.");
17
+ dispose = value[Symbol.dispose];
18
+ if (async) inner = dispose;
19
+ }
20
+ if (typeof dispose !== "function") throw new TypeError("Object not disposable.");
21
+ if (inner) dispose = function() {
22
+ try {
23
+ inner.call(this);
24
+ } catch (e) {
25
+ return Promise.reject(e);
26
+ }
27
+ };
28
+ env.stack.push({
29
+ value: value,
30
+ dispose: dispose,
31
+ async: async
32
+ });
33
+ } else if (async) {
34
+ env.stack.push({
35
+ async: true
36
+ });
37
+ }
38
+ return value;
39
+ }
40
+ function _ts_dispose_resources(env) {
41
+ var _SuppressedError = typeof SuppressedError === "function" ? SuppressedError : function(error, suppressed, message) {
42
+ var e = new Error(message);
43
+ return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
44
+ };
45
+ return (_ts_dispose_resources = function _ts_dispose_resources(env) {
46
+ function fail(e) {
47
+ env.error = env.hasError ? new _SuppressedError(e, env.error, "An error was suppressed during disposal.") : e;
48
+ env.hasError = true;
49
+ }
50
+ var r, s = 0;
51
+ function next() {
52
+ while(r = env.stack.pop()){
53
+ try {
54
+ if (!r.async && s === 1) return s = 0, env.stack.push(r), Promise.resolve().then(next);
55
+ if (r.dispose) {
56
+ var result = r.dispose.call(r.value);
57
+ if (r.async) return s |= 2, Promise.resolve(result).then(next, function(e) {
58
+ fail(e);
59
+ return next();
60
+ });
61
+ } else s |= 1;
62
+ } catch (e) {
63
+ fail(e);
64
+ }
65
+ }
66
+ if (s === 1) return env.hasError ? Promise.reject(env.error) : Promise.resolve();
67
+ if (env.hasError) throw env.error;
68
+ }
69
+ return next();
70
+ })(env);
71
+ }
6
72
  const PING_SYM = Symbol('ping');
7
73
  /**
8
74
  * Derives a new {@link AsyncGenerator} based of {@link iterable}, that yields {@link PING_SYM}
9
75
  * whenever no value has been yielded for {@link pingIntervalMs}.
10
76
  */ async function* withPing(iterable, pingIntervalMs) {
11
- const iterator = iterable[Symbol.asyncIterator]();
12
- // declaration outside the loop for garbage collection reasons
13
- let result;
14
- let nextPromise = iterator.next();
15
- while(true){
16
- const pingPromise = timerResource.timerResource(pingIntervalMs);
17
- try {
18
- result = await unpromise.Unpromise.race([
19
- nextPromise,
20
- pingPromise.start()
21
- ]);
22
- if (result === timerResource.disposablePromiseTimerResult) {
23
- // cancelled
24
- yield PING_SYM;
25
- continue;
26
- }
27
- if (result.done) {
28
- return result.value;
77
+ const env = {
78
+ stack: [],
79
+ error: void 0,
80
+ hasError: false
81
+ };
82
+ try {
83
+ const iterator = _ts_add_disposable_resource(env, asyncIterable.iteratorResource(iterable), true);
84
+ ;
85
+ // declaration outside the loop for garbage collection reasons
86
+ let result;
87
+ let nextPromise = iterator.next();
88
+ while(true){
89
+ const env = {
90
+ stack: [],
91
+ error: void 0,
92
+ hasError: false
93
+ };
94
+ try {
95
+ const pingPromise = _ts_add_disposable_resource(env, timerResource.timerResource(pingIntervalMs), false);
96
+ ;
97
+ result = await unpromise.Unpromise.race([
98
+ nextPromise,
99
+ pingPromise.start()
100
+ ]);
101
+ if (result === timerResource.disposablePromiseTimerResult) {
102
+ // cancelled
103
+ yield PING_SYM;
104
+ continue;
105
+ }
106
+ if (result.done) {
107
+ return result.value;
108
+ }
109
+ nextPromise = iterator.next();
110
+ yield result.value;
111
+ // free up reference for garbage collection
112
+ result = null;
113
+ } catch (e) {
114
+ env.error = e;
115
+ env.hasError = true;
116
+ } finally{
117
+ _ts_dispose_resources(env);
29
118
  }
30
- nextPromise = iterator.next();
31
- yield result.value;
32
- // free up reference for garbage collection
33
- result = null;
34
- } finally{
35
- pingPromise[Symbol.dispose]();
36
119
  }
120
+ } catch (e) {
121
+ env.error = e;
122
+ env.hasError = true;
123
+ } finally{
124
+ const result = _ts_dispose_resources(env);
125
+ if (result) await result;
37
126
  }
38
127
  }
39
128
 
@@ -1,37 +1,126 @@
1
1
  import { Unpromise } from '../../../vendor/unpromise/unpromise.mjs';
2
- import { disposablePromiseTimerResult, timerResource } from './timerResource.mjs';
2
+ import { iteratorResource } from './asyncIterable.mjs';
3
+ import { timerResource, disposablePromiseTimerResult } from './timerResource.mjs';
3
4
 
5
+ function _ts_add_disposable_resource(env, value, async) {
6
+ if (value !== null && value !== void 0) {
7
+ if (typeof value !== "object" && typeof value !== "function") throw new TypeError("Object expected.");
8
+ var dispose, inner;
9
+ if (async) {
10
+ if (!Symbol.asyncDispose) throw new TypeError("Symbol.asyncDispose is not defined.");
11
+ dispose = value[Symbol.asyncDispose];
12
+ }
13
+ if (dispose === void 0) {
14
+ if (!Symbol.dispose) throw new TypeError("Symbol.dispose is not defined.");
15
+ dispose = value[Symbol.dispose];
16
+ if (async) inner = dispose;
17
+ }
18
+ if (typeof dispose !== "function") throw new TypeError("Object not disposable.");
19
+ if (inner) dispose = function() {
20
+ try {
21
+ inner.call(this);
22
+ } catch (e) {
23
+ return Promise.reject(e);
24
+ }
25
+ };
26
+ env.stack.push({
27
+ value: value,
28
+ dispose: dispose,
29
+ async: async
30
+ });
31
+ } else if (async) {
32
+ env.stack.push({
33
+ async: true
34
+ });
35
+ }
36
+ return value;
37
+ }
38
+ function _ts_dispose_resources(env) {
39
+ var _SuppressedError = typeof SuppressedError === "function" ? SuppressedError : function(error, suppressed, message) {
40
+ var e = new Error(message);
41
+ return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
42
+ };
43
+ return (_ts_dispose_resources = function _ts_dispose_resources(env) {
44
+ function fail(e) {
45
+ env.error = env.hasError ? new _SuppressedError(e, env.error, "An error was suppressed during disposal.") : e;
46
+ env.hasError = true;
47
+ }
48
+ var r, s = 0;
49
+ function next() {
50
+ while(r = env.stack.pop()){
51
+ try {
52
+ if (!r.async && s === 1) return s = 0, env.stack.push(r), Promise.resolve().then(next);
53
+ if (r.dispose) {
54
+ var result = r.dispose.call(r.value);
55
+ if (r.async) return s |= 2, Promise.resolve(result).then(next, function(e) {
56
+ fail(e);
57
+ return next();
58
+ });
59
+ } else s |= 1;
60
+ } catch (e) {
61
+ fail(e);
62
+ }
63
+ }
64
+ if (s === 1) return env.hasError ? Promise.reject(env.error) : Promise.resolve();
65
+ if (env.hasError) throw env.error;
66
+ }
67
+ return next();
68
+ })(env);
69
+ }
4
70
  const PING_SYM = Symbol('ping');
5
71
  /**
6
72
  * Derives a new {@link AsyncGenerator} based of {@link iterable}, that yields {@link PING_SYM}
7
73
  * whenever no value has been yielded for {@link pingIntervalMs}.
8
74
  */ async function* withPing(iterable, pingIntervalMs) {
9
- const iterator = iterable[Symbol.asyncIterator]();
10
- // declaration outside the loop for garbage collection reasons
11
- let result;
12
- let nextPromise = iterator.next();
13
- while(true){
14
- const pingPromise = timerResource(pingIntervalMs);
15
- try {
16
- result = await Unpromise.race([
17
- nextPromise,
18
- pingPromise.start()
19
- ]);
20
- if (result === disposablePromiseTimerResult) {
21
- // cancelled
22
- yield PING_SYM;
23
- continue;
24
- }
25
- if (result.done) {
26
- return result.value;
75
+ const env = {
76
+ stack: [],
77
+ error: void 0,
78
+ hasError: false
79
+ };
80
+ try {
81
+ const iterator = _ts_add_disposable_resource(env, iteratorResource(iterable), true);
82
+ ;
83
+ // declaration outside the loop for garbage collection reasons
84
+ let result;
85
+ let nextPromise = iterator.next();
86
+ while(true){
87
+ const env = {
88
+ stack: [],
89
+ error: void 0,
90
+ hasError: false
91
+ };
92
+ try {
93
+ const pingPromise = _ts_add_disposable_resource(env, timerResource(pingIntervalMs), false);
94
+ ;
95
+ result = await Unpromise.race([
96
+ nextPromise,
97
+ pingPromise.start()
98
+ ]);
99
+ if (result === disposablePromiseTimerResult) {
100
+ // cancelled
101
+ yield PING_SYM;
102
+ continue;
103
+ }
104
+ if (result.done) {
105
+ return result.value;
106
+ }
107
+ nextPromise = iterator.next();
108
+ yield result.value;
109
+ // free up reference for garbage collection
110
+ result = null;
111
+ } catch (e) {
112
+ env.error = e;
113
+ env.hasError = true;
114
+ } finally{
115
+ _ts_dispose_resources(env);
27
116
  }
28
- nextPromise = iterator.next();
29
- yield result.value;
30
- // free up reference for garbage collection
31
- result = null;
32
- } finally{
33
- pingPromise[Symbol.dispose]();
34
117
  }
118
+ } catch (e) {
119
+ env.error = e;
120
+ env.hasError = true;
121
+ } finally{
122
+ const result = _ts_dispose_resources(env);
123
+ if (result) await result;
35
124
  }
36
125
  }
37
126
 
@@ -63,7 +63,7 @@ export declare const defaultTransformer: CombinedDataTransformer;
63
63
  /**
64
64
  * Takes a unserialized `TRPCResponse` and serializes it with the router's transformers
65
65
  **/
66
- export declare function transformTRPCResponse<TResponse extends TRPCResponse | TRPCResponse[] | TRPCResponseMessage | TRPCResponseMessage[]>(config: RootConfig<AnyRootTypes>, itemOrItems: TResponse): import("./rpc").TRPCErrorResponse<import("./rpc").TRPCErrorShape<object>> | import("./rpc").TRPCSuccessResponse<unknown> | ({
66
+ export declare function transformTRPCResponse<TResponse extends TRPCResponse | TRPCResponse[] | TRPCResponseMessage | TRPCResponseMessage[]>(config: RootConfig<AnyRootTypes>, itemOrItems: TResponse): import("./rpc").TRPCSuccessResponse<unknown> | import("./rpc").TRPCErrorResponse<import("./rpc").TRPCErrorShape<object>> | ({
67
67
  id: import("./rpc").JSONRPC2.RequestId;
68
68
  } & TRPCResultMessage<unknown>) | (TRPCResponse | TRPCResponseMessage)[];
69
69
  /** @internal */
@@ -36,6 +36,7 @@ export * from './unstable-core-do-not-import/stream/sse.types';
36
36
  export * from './unstable-core-do-not-import/stream/sse';
37
37
  export * from './unstable-core-do-not-import/stream/tracked';
38
38
  export * from './unstable-core-do-not-import/stream/utils/createDeferred';
39
+ export * from './unstable-core-do-not-import/stream/utils/disposable';
39
40
  export * from './unstable-core-do-not-import/transformer';
40
41
  export * from './unstable-core-do-not-import/types';
41
42
  export * from './unstable-core-do-not-import/utils';