@trpc/server 11.0.0-next-beta.303 → 11.0.0-next-beta.305

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.
@@ -1 +1 @@
1
- {"version":3,"file":"fastifyTRPCPlugin.d.ts","sourceRoot":"","sources":["../../../src/adapters/fastify/fastifyTRPCPlugin.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,KAAK,EAAE,eAAe,EAAE,YAAY,EAAE,cAAc,EAAE,MAAM,SAAS,CAAC;AAC7E,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,GAAG,CAAC;AAE/C,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAC;AACpD,OAAO,KAAK,EAAE,8BAA8B,EAAE,MAAM,cAAc,CAAC;AAKnE,MAAM,WAAW,wBAAwB,CAAC,OAAO,SAAS,SAAS;IACjE,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,MAAM,CAAC,EAAE,OAAO,CAAC;IACjB,WAAW,EAAE,qBAAqB,CAAC,OAAO,EAAE,cAAc,EAAE,YAAY,CAAC,CAAC;CAC3E;AAED,MAAM,MAAM,2BAA2B,GAAG,8BAA8B,CACtE,cAAc,EACd,YAAY,CACb,CAAC;AAEF,wBAAgB,iBAAiB,CAAC,OAAO,SAAS,SAAS,EACzD,OAAO,EAAE,eAAe,EACxB,IAAI,EAAE,wBAAwB,CAAC,OAAO,CAAC,EACvC,IAAI,EAAE,CAAC,GAAG,CAAC,EAAE,KAAK,KAAK,IAAI,QAoC5B"}
1
+ {"version":3,"file":"fastifyTRPCPlugin.d.ts","sourceRoot":"","sources":["../../../src/adapters/fastify/fastifyTRPCPlugin.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,KAAK,EAAE,eAAe,EAAE,YAAY,EAAE,cAAc,EAAE,MAAM,SAAS,CAAC;AAC7E,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,GAAG,CAAC;AAE/C,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAC;AACpD,OAAO,KAAK,EAAE,8BAA8B,EAAE,MAAM,cAAc,CAAC;AAKnE,MAAM,WAAW,wBAAwB,CAAC,OAAO,SAAS,SAAS;IACjE,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,MAAM,CAAC,EAAE,OAAO,CAAC;IACjB,WAAW,EAAE,qBAAqB,CAAC,OAAO,EAAE,cAAc,EAAE,YAAY,CAAC,CAAC;CAC3E;AAED,MAAM,MAAM,2BAA2B,GAAG,8BAA8B,CACtE,cAAc,EACd,YAAY,CACb,CAAC;AAEF,wBAAgB,iBAAiB,CAAC,OAAO,SAAS,SAAS,EACzD,OAAO,EAAE,eAAe,EACxB,IAAI,EAAE,wBAAwB,CAAC,OAAO,CAAC,EACvC,IAAI,EAAE,CAAC,GAAG,CAAC,EAAE,KAAK,KAAK,IAAI,QAmC5B"}
@@ -20,7 +20,6 @@ function fastifyTRPCPlugin(fastify, opts, done) {
20
20
  _done(null, body);
21
21
  });
22
22
  let prefix = opts.prefix ?? '';
23
- const websocketPrefix = prefix;
24
23
  // https://github.com/fastify/fastify-plugin/blob/fe079bef6557a83794bf437e14b9b9edb8a74104/plugin.js#L11
25
24
  // @ts-expect-error property 'default' does not exists on type ...
26
25
  if (typeof fastifyTRPCPlugin.default !== 'function') {
@@ -36,15 +35,12 @@ function fastifyTRPCPlugin(fastify, opts, done) {
36
35
  });
37
36
  });
38
37
  if (opts.useWSS) {
39
- ws.applyWSSHandler({
40
- ...opts.trpcOptions,
41
- prefix: websocketPrefix,
42
- wss: fastify.websocketServer
38
+ const onConnection = ws.getWSConnectionHandler({
39
+ ...opts.trpcOptions
43
40
  });
44
- // eslint-disable-next-line @typescript-eslint/no-empty-function
45
41
  fastify.get(prefix ?? '/', {
46
42
  websocket: true
47
- }, ()=>{});
43
+ }, ({ socket }, req)=>onConnection(socket, req.raw));
48
44
  }
49
45
  done();
50
46
  }
@@ -1,4 +1,4 @@
1
- import { applyWSSHandler } from '../ws.mjs';
1
+ import { getWSConnectionHandler } from '../ws.mjs';
2
2
  import { fastifyRequestHandler } from './fastifyRequestHandler.mjs';
3
3
 
4
4
  /**
@@ -18,7 +18,6 @@ function fastifyTRPCPlugin(fastify, opts, done) {
18
18
  _done(null, body);
19
19
  });
20
20
  let prefix = opts.prefix ?? '';
21
- const websocketPrefix = prefix;
22
21
  // https://github.com/fastify/fastify-plugin/blob/fe079bef6557a83794bf437e14b9b9edb8a74104/plugin.js#L11
23
22
  // @ts-expect-error property 'default' does not exists on type ...
24
23
  if (typeof fastifyTRPCPlugin.default !== 'function') {
@@ -34,15 +33,12 @@ function fastifyTRPCPlugin(fastify, opts, done) {
34
33
  });
35
34
  });
36
35
  if (opts.useWSS) {
37
- applyWSSHandler({
38
- ...opts.trpcOptions,
39
- prefix: websocketPrefix,
40
- wss: fastify.websocketServer
36
+ const onConnection = getWSConnectionHandler({
37
+ ...opts.trpcOptions
41
38
  });
42
- // eslint-disable-next-line @typescript-eslint/no-empty-function
43
39
  fastify.get(prefix ?? '/', {
44
40
  websocket: true
45
- }, ()=>{});
41
+ }, ({ socket }, req)=>onConnection(socket, req.raw));
46
42
  }
47
43
  done();
48
44
  }
@@ -15,10 +15,7 @@ export type CreateWSSContextFnOptions = Omit<NodeHTTPCreateContextFnOptions<Inco
15
15
  * @public
16
16
  */
17
17
  export type CreateWSSContextFn<TRouter extends AnyRouter> = (opts: CreateWSSContextFnOptions) => MaybePromise<inferRouterContext<TRouter>>;
18
- /**
19
- * Web socket server handler
20
- */
21
- export type WSSHandlerOptions<TRouter extends AnyRouter> = BaseHandlerOptions<TRouter, IncomingMessage> & (object extends inferRouterContext<TRouter> ? {
18
+ export type WSConnectionHandlerOptions<TRouter extends AnyRouter> = BaseHandlerOptions<TRouter, IncomingMessage> & (object extends inferRouterContext<TRouter> ? {
22
19
  /**
23
20
  * @link https://trpc.io/docs/v11/context
24
21
  **/
@@ -28,11 +25,16 @@ export type WSSHandlerOptions<TRouter extends AnyRouter> = BaseHandlerOptions<TR
28
25
  * @link https://trpc.io/docs/v11/context
29
26
  **/
30
27
  createContext: CreateWSSContextFn<TRouter>;
31
- }) & {
28
+ });
29
+ /**
30
+ * Web socket server handler
31
+ */
32
+ export type WSSHandlerOptions<TRouter extends AnyRouter> = WSConnectionHandlerOptions<TRouter> & {
32
33
  wss: ws.WebSocketServer;
33
34
  process?: NodeJS.Process;
34
35
  prefix?: string;
35
36
  };
37
+ export declare function getWSConnectionHandler<TRouter extends AnyRouter>(opts: WSConnectionHandlerOptions<TRouter>): (client: ws.WebSocket, req: IncomingMessage) => Promise<void>;
36
38
  export declare function applyWSSHandler<TRouter extends AnyRouter>(opts: WSSHandlerOptions<TRouter>): {
37
39
  broadcastReconnectNotification: () => void;
38
40
  };
@@ -1 +1 @@
1
- {"version":3,"file":"ws.d.ts","sourceRoot":"","sources":["../../src/adapters/ws.ts"],"names":[],"mappings":";;;AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,MAAM,CAAC;AAC5C,OAAO,KAAK,EAAE,MAAM,IAAI,CAAC;AACzB,OAAO,KAAK,EAAE,SAAS,EAAE,kBAAkB,EAAE,MAAM,iBAAiB,CAAC;AAQrE,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAC;AAY/D,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,gCAAgC,CAAC;AACnE,OAAO,KAAK,EAAE,8BAA8B,EAAE,MAAM,aAAa,CAAC;AAQlE;;GAEG;AACH,MAAM,MAAM,yBAAyB,GAAG,IAAI,CAC1C,8BAA8B,CAAC,eAAe,EAAE,EAAE,CAAC,SAAS,CAAC,EAC7D,MAAM,CACP,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,kBAAkB,CAAC,OAAO,SAAS,SAAS,IAAI,CAC1D,IAAI,EAAE,yBAAyB,KAC5B,YAAY,CAAC,kBAAkB,CAAC,OAAO,CAAC,CAAC,CAAC;AAE/C;;GAEG;AACH,MAAM,MAAM,iBAAiB,CAAC,OAAO,SAAS,SAAS,IAAI,kBAAkB,CAC3E,OAAO,EACP,eAAe,CAChB,GACC,CAAC,MAAM,SAAS,kBAAkB,CAAC,OAAO,CAAC,GACvC;IACE;;QAEI;IACJ,aAAa,CAAC,EAAE,kBAAkB,CAAC,OAAO,CAAC,CAAC;CAC7C,GACD;IACE;;QAEI;IACJ,aAAa,EAAE,kBAAkB,CAAC,OAAO,CAAC,CAAC;CAC5C,CAAC,GAAG;IACP,GAAG,EAAE,EAAE,CAAC,eAAe,CAAC;IACxB,OAAO,CAAC,EAAE,MAAM,CAAC,OAAO,CAAC;IACzB,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB,CAAC;AAEJ,wBAAgB,eAAe,CAAC,OAAO,SAAS,SAAS,EACvD,IAAI,EAAE,iBAAiB,CAAC,OAAO,CAAC;;EA4QjC"}
1
+ {"version":3,"file":"ws.d.ts","sourceRoot":"","sources":["../../src/adapters/ws.ts"],"names":[],"mappings":";;;AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,MAAM,CAAC;AAC5C,OAAO,KAAK,EAAE,MAAM,IAAI,CAAC;AACzB,OAAO,KAAK,EAAE,SAAS,EAAE,kBAAkB,EAAE,MAAM,iBAAiB,CAAC;AAQrE,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAC;AAY/D,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,gCAAgC,CAAC;AACnE,OAAO,KAAK,EAAE,8BAA8B,EAAE,MAAM,aAAa,CAAC;AAQlE;;GAEG;AACH,MAAM,MAAM,yBAAyB,GAAG,IAAI,CAC1C,8BAA8B,CAAC,eAAe,EAAE,EAAE,CAAC,SAAS,CAAC,EAC7D,MAAM,CACP,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,kBAAkB,CAAC,OAAO,SAAS,SAAS,IAAI,CAC1D,IAAI,EAAE,yBAAyB,KAC5B,YAAY,CAAC,kBAAkB,CAAC,OAAO,CAAC,CAAC,CAAC;AAE/C,MAAM,MAAM,0BAA0B,CAAC,OAAO,SAAS,SAAS,IAC9D,kBAAkB,CAAC,OAAO,EAAE,eAAe,CAAC,GAC1C,CAAC,MAAM,SAAS,kBAAkB,CAAC,OAAO,CAAC,GACvC;IACE;;QAEI;IACJ,aAAa,CAAC,EAAE,kBAAkB,CAAC,OAAO,CAAC,CAAC;CAC7C,GACD;IACE;;QAEI;IACJ,aAAa,EAAE,kBAAkB,CAAC,OAAO,CAAC,CAAC;CAC5C,CAAC,CAAC;AAEX;;GAEG;AACH,MAAM,MAAM,iBAAiB,CAAC,OAAO,SAAS,SAAS,IACrD,0BAA0B,CAAC,OAAO,CAAC,GAAG;IACpC,GAAG,EAAE,EAAE,CAAC,eAAe,CAAC;IACxB,OAAO,CAAC,EAAE,MAAM,CAAC,OAAO,CAAC;IACzB,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB,CAAC;AAEJ,wBAAgB,sBAAsB,CAAC,OAAO,SAAS,SAAS,EAC9D,IAAI,EAAE,0BAA0B,CAAC,OAAO,CAAC,YAKnB,YAAY,OAAO,eAAe,mBAoPzD;AAED,wBAAgB,eAAe,CAAC,OAAO,SAAS,SAAS,EACvD,IAAI,EAAE,iBAAiB,CAAC,OAAO,CAAC;;EA2BjC"}
@@ -12,13 +12,10 @@ var observable = require('../observable/observable.js');
12
12
  * Importing ws causes a build error
13
13
  * @link https://github.com/trpc/trpc/pull/5279
14
14
  */ const WEBSOCKET_OPEN = 1; /* ws.WebSocket.OPEN */
15
- function applyWSSHandler(opts) {
16
- const { wss , createContext , router: router$1 , prefix } = opts;
15
+ function getWSConnectionHandler(opts) {
16
+ const { createContext , router: router$1 } = opts;
17
17
  const { transformer: transformer$1 } = router$1._def._config;
18
- wss.on('connection', async (client, req)=>{
19
- if (prefix && !req.url?.startsWith(prefix)) {
20
- return;
21
- }
18
+ return async (client, req)=>{
22
19
  const clientSubscriptions = new Map();
23
20
  function respond(untransformedJSON) {
24
21
  client.send(JSON.stringify(transformer.transformTRPCResponse(router$1._def._config, untransformedJSON)));
@@ -260,6 +257,16 @@ function applyWSSHandler(opts) {
260
257
  }
261
258
  }
262
259
  await createContextAsync();
260
+ };
261
+ }
262
+ function applyWSSHandler(opts) {
263
+ const { wss , prefix } = opts;
264
+ const onConnection = getWSConnectionHandler(opts);
265
+ wss.on('connection', async (client, req)=>{
266
+ if (prefix && !req.url?.startsWith(prefix)) {
267
+ return;
268
+ }
269
+ await onConnection(client, req);
263
270
  });
264
271
  return {
265
272
  broadcastReconnectNotification: ()=>{
@@ -278,3 +285,4 @@ function applyWSSHandler(opts) {
278
285
  }
279
286
 
280
287
  exports.applyWSSHandler = applyWSSHandler;
288
+ exports.getWSConnectionHandler = getWSConnectionHandler;
@@ -10,13 +10,10 @@ import { isObservable } from '../observable/observable.mjs';
10
10
  * Importing ws causes a build error
11
11
  * @link https://github.com/trpc/trpc/pull/5279
12
12
  */ const WEBSOCKET_OPEN = 1; /* ws.WebSocket.OPEN */
13
- function applyWSSHandler(opts) {
14
- const { wss , createContext , router , prefix } = opts;
13
+ function getWSConnectionHandler(opts) {
14
+ const { createContext , router } = opts;
15
15
  const { transformer } = router._def._config;
16
- wss.on('connection', async (client, req)=>{
17
- if (prefix && !req.url?.startsWith(prefix)) {
18
- return;
19
- }
16
+ return async (client, req)=>{
20
17
  const clientSubscriptions = new Map();
21
18
  function respond(untransformedJSON) {
22
19
  client.send(JSON.stringify(transformTRPCResponse(router._def._config, untransformedJSON)));
@@ -258,6 +255,16 @@ function applyWSSHandler(opts) {
258
255
  }
259
256
  }
260
257
  await createContextAsync();
258
+ };
259
+ }
260
+ function applyWSSHandler(opts) {
261
+ const { wss , prefix } = opts;
262
+ const onConnection = getWSConnectionHandler(opts);
263
+ wss.on('connection', async (client, req)=>{
264
+ if (prefix && !req.url?.startsWith(prefix)) {
265
+ return;
266
+ }
267
+ await onConnection(client, req);
261
268
  });
262
269
  return {
263
270
  broadcastReconnectNotification: ()=>{
@@ -275,4 +282,4 @@ function applyWSSHandler(opts) {
275
282
  };
276
283
  }
277
284
 
278
- export { applyWSSHandler };
285
+ export { applyWSSHandler, getWSConnectionHandler };
@@ -1,20 +1,21 @@
1
1
  {
2
- "bundleSize": 93108,
3
- "bundleOrigSize": 148167,
4
- "bundleReduction": 37.16,
2
+ "bundleSize": 92666,
3
+ "bundleOrigSize": 147947,
4
+ "bundleReduction": 37.37,
5
5
  "modules": [
6
6
  {
7
7
  "id": "/src/adapters/ws.ts",
8
- "size": 9970,
9
- "origSize": 9269,
8
+ "size": 10170,
9
+ "origSize": 9698,
10
10
  "renderedExports": [
11
+ "getWSConnectionHandler",
11
12
  "applyWSSHandler"
12
13
  ],
13
14
  "removedExports": [],
14
15
  "dependents": [
15
16
  "/src/adapters/fastify/fastifyTRPCPlugin.ts"
16
17
  ],
17
- "percent": 10.71,
18
+ "percent": 10.97,
18
19
  "reduction": 0
19
20
  },
20
21
  {
@@ -26,7 +27,7 @@
26
27
  ],
27
28
  "removedExports": [],
28
29
  "dependents": [],
29
- "percent": 10.08,
30
+ "percent": 10.13,
30
31
  "reduction": 21.23
31
32
  },
32
33
  {
@@ -45,7 +46,7 @@
45
46
  "/src/unstable-core-do-not-import/http/resolveHTTPResponse.ts",
46
47
  "/src/unstable-core-do-not-import/initTRPC.ts"
47
48
  ],
48
- "percent": 6.52,
49
+ "percent": 6.55,
49
50
  "reduction": 38.58
50
51
  },
51
52
  {
@@ -61,7 +62,7 @@
61
62
  "/src/unstable-core-do-not-import.ts",
62
63
  "/src/unstable-core-do-not-import/initTRPC.ts"
63
64
  ],
64
- "percent": 6.13,
65
+ "percent": 6.16,
65
66
  "reduction": 58.57
66
67
  },
67
68
  {
@@ -77,7 +78,7 @@
77
78
  "dependents": [
78
79
  "/src/adapters/node-http/content-type/form-data/index.ts"
79
80
  ],
80
- "percent": 5.35,
81
+ "percent": 5.37,
81
82
  "reduction": 36.77
82
83
  },
83
84
  {
@@ -91,7 +92,7 @@
91
92
  ],
92
93
  "removedExports": [],
93
94
  "dependents": [],
94
- "percent": 4.75,
95
+ "percent": 4.77,
95
96
  "reduction": 32.71
96
97
  },
97
98
  {
@@ -105,7 +106,7 @@
105
106
  "dependents": [
106
107
  "/src/adapters/node-http/index.ts"
107
108
  ],
108
- "percent": 4.22,
109
+ "percent": 4.24,
109
110
  "reduction": 13.56
110
111
  },
111
112
  {
@@ -119,25 +120,9 @@
119
120
  "dependents": [
120
121
  "/src/adapters/fetch/index.ts"
121
122
  ],
122
- "percent": 3.82,
123
+ "percent": 3.84,
123
124
  "reduction": 9.5
124
125
  },
125
- {
126
- "id": "/src/observable/observable.ts",
127
- "size": 3430,
128
- "origSize": 3627,
129
- "renderedExports": [
130
- "isObservable",
131
- "observable",
132
- "observableToPromise"
133
- ],
134
- "removedExports": [],
135
- "dependents": [
136
- "/src/observable/index.ts"
137
- ],
138
- "percent": 3.68,
139
- "reduction": 5.43
140
- },
141
126
  {
142
127
  "id": "/src/adapters/aws-lambda/utils.ts",
143
128
  "size": 3308,
@@ -154,13 +139,30 @@
154
139
  "dependents": [
155
140
  "/src/adapters/aws-lambda/index.ts"
156
141
  ],
157
- "percent": 3.55,
142
+ "percent": 3.57,
158
143
  "reduction": 36.48
159
144
  },
145
+ {
146
+ "id": "/src/observable/observable.ts",
147
+ "size": 3129,
148
+ "origSize": 3150,
149
+ "renderedExports": [
150
+ "isObservable",
151
+ "observable",
152
+ "observableToPromise"
153
+ ],
154
+ "removedExports": [],
155
+ "dependents": [
156
+ "/src/observable/index.ts",
157
+ "/src/observable/operators.ts"
158
+ ],
159
+ "percent": 3.38,
160
+ "reduction": 0.67
161
+ },
160
162
  {
161
163
  "id": "/src/observable/operators.ts",
162
- "size": 2980,
163
- "origSize": 2812,
164
+ "size": 2756,
165
+ "origSize": 2720,
164
166
  "renderedExports": [
165
167
  "map",
166
168
  "share",
@@ -170,7 +172,7 @@
170
172
  "dependents": [
171
173
  "/src/observable/index.ts"
172
174
  ],
173
- "percent": 3.2,
175
+ "percent": 2.97,
174
176
  "reduction": 0
175
177
  },
176
178
  {
@@ -190,7 +192,7 @@
190
192
  "/src/unstable-core-do-not-import/router.ts",
191
193
  "/src/unstable-core-do-not-import/initTRPC.ts"
192
194
  ],
193
- "percent": 2.95,
195
+ "percent": 2.96,
194
196
  "reduction": 45.94
195
197
  },
196
198
  {
@@ -204,7 +206,7 @@
204
206
  "dependents": [
205
207
  "/src/unstable-core-do-not-import.ts"
206
208
  ],
207
- "percent": 2.93,
209
+ "percent": 2.94,
208
210
  "reduction": 39.13
209
211
  },
210
212
  {
@@ -224,7 +226,7 @@
224
226
  "/src/unstable-core-do-not-import/initTRPC.ts",
225
227
  "/src/unstable-core-do-not-import/procedureBuilder.ts"
226
228
  ],
227
- "percent": 2.82,
229
+ "percent": 2.83,
228
230
  "reduction": 55.5
229
231
  },
230
232
  {
@@ -239,7 +241,7 @@
239
241
  "/src/adapters/fastify/index.ts",
240
242
  "/src/adapters/fastify/fastifyTRPCPlugin.ts"
241
243
  ],
242
- "percent": 2.78,
244
+ "percent": 2.79,
243
245
  "reduction": 35.33
244
246
  },
245
247
  {
@@ -251,7 +253,7 @@
251
253
  ],
252
254
  "removedExports": [],
253
255
  "dependents": [],
254
- "percent": 2.73,
256
+ "percent": 2.74,
255
257
  "reduction": 31.37
256
258
  },
257
259
  {
@@ -272,7 +274,7 @@
272
274
  "/src/unstable-core-do-not-import/middleware.ts",
273
275
  "/src/unstable-core-do-not-import/procedureBuilder.ts"
274
276
  ],
275
- "percent": 1.86,
277
+ "percent": 1.87,
276
278
  "reduction": 19.47
277
279
  },
278
280
  {
@@ -286,23 +288,9 @@
286
288
  "dependents": [
287
289
  "/src/unstable-core-do-not-import/http/resolveHTTPResponse.ts"
288
290
  ],
289
- "percent": 1.76,
291
+ "percent": 1.77,
290
292
  "reduction": 34.79
291
293
  },
292
- {
293
- "id": "/src/adapters/fastify/fastifyTRPCPlugin.ts",
294
- "size": 1603,
295
- "origSize": 2343,
296
- "renderedExports": [
297
- "fastifyTRPCPlugin"
298
- ],
299
- "removedExports": [],
300
- "dependents": [
301
- "/src/adapters/fastify/index.ts"
302
- ],
303
- "percent": 1.72,
304
- "reduction": 31.58
305
- },
306
294
  {
307
295
  "id": "/src/adapters/next.ts",
308
296
  "size": 1602,
@@ -312,7 +300,7 @@
312
300
  ],
313
301
  "removedExports": [],
314
302
  "dependents": [],
315
- "percent": 1.72,
303
+ "percent": 1.73,
316
304
  "reduction": 29.12
317
305
  },
318
306
  {
@@ -326,7 +314,7 @@
326
314
  "dependents": [
327
315
  "/src/adapters/node-http/content-type/json/index.ts"
328
316
  ],
329
- "percent": 1.71,
317
+ "percent": 1.72,
330
318
  "reduction": 0.19
331
319
  },
332
320
  {
@@ -338,7 +326,7 @@
338
326
  ],
339
327
  "removedExports": [],
340
328
  "dependents": [],
341
- "percent": 1.7,
329
+ "percent": 1.71,
342
330
  "reduction": 21.04
343
331
  },
344
332
  {
@@ -368,9 +356,23 @@
368
356
  "/src/unstable-core-do-not-import/router.ts",
369
357
  "/src/unstable-core-do-not-import/initTRPC.ts"
370
358
  ],
371
- "percent": 1.62,
359
+ "percent": 1.63,
372
360
  "reduction": 5.58
373
361
  },
362
+ {
363
+ "id": "/src/adapters/fastify/fastifyTRPCPlugin.ts",
364
+ "size": 1486,
365
+ "origSize": 2263,
366
+ "renderedExports": [
367
+ "fastifyTRPCPlugin"
368
+ ],
369
+ "removedExports": [],
370
+ "dependents": [
371
+ "/src/adapters/fastify/index.ts"
372
+ ],
373
+ "percent": 1.6,
374
+ "reduction": 34.33
375
+ },
374
376
  {
375
377
  "id": "/src/adapters/node-http/content-type/form-data/streamSlice.ts",
376
378
  "size": 1393,
@@ -398,7 +400,7 @@
398
400
  "/src/unstable-core-do-not-import/http/resolveHTTPResponse.ts",
399
401
  "/src/unstable-core-do-not-import/error/getErrorShape.ts"
400
402
  ],
401
- "percent": 1.36,
403
+ "percent": 1.37,
402
404
  "reduction": 22.76
403
405
  },
404
406
  {
@@ -536,7 +538,7 @@
536
538
  ],
537
539
  "removedExports": [],
538
540
  "dependents": [],
539
- "percent": 0.36,
541
+ "percent": 0.37,
540
542
  "reduction": 71.37
541
543
  },
542
544
  {
@@ -550,7 +552,7 @@
550
552
  "dependents": [
551
553
  "/src/adapters/node-http/nodeHTTPRequestHandler.ts"
552
554
  ],
553
- "percent": 0.31,
555
+ "percent": 0.32,
554
556
  "reduction": 35.11
555
557
  },
556
558
  {
@@ -588,10 +590,10 @@
588
590
  ],
589
591
  "removedExports": [],
590
592
  "dependents": [
591
- "/src/adapters/node-http/content-type/form-data/index.ts",
592
- "/src/adapters/node-http/content-type/json/index.ts"
593
+ "/src/adapters/node-http/content-type/json/index.ts",
594
+ "/src/adapters/node-http/content-type/form-data/index.ts"
593
595
  ],
594
- "percent": 0.12,
596
+ "percent": 0.13,
595
597
  "reduction": 86.68
596
598
  },
597
599
  {
@@ -1 +1 @@
1
- {"version":3,"file":"observable.d.ts","sourceRoot":"","sources":["../../src/observable/observable.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,UAAU,EACV,QAAQ,EAER,aAAa,EAEd,MAAM,SAAS,CAAC;AAMjB,cAAc;AACd,MAAM,MAAM,oBAAoB,CAAC,WAAW,IAAI,WAAW,SAAS,UAAU,CAC5E,MAAM,MAAM,EACZ,OAAO,CACR,GACG,MAAM,GACN,KAAK,CAAC;AAEV,cAAc;AACd,wBAAgB,YAAY,CAAC,CAAC,EAAE,OAAO,GAAG,CAAC,IAAI,UAAU,CAAC,OAAO,EAAE,OAAO,CAAC,CAE1E;AAED,cAAc;AACd,wBAAgB,UAAU,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,EACjD,SAAS,EAAE,CAAC,QAAQ,EAAE,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC,KAAK,aAAa,GAC/D,UAAU,CAAC,MAAM,EAAE,MAAM,CAAC,CA6D5B;AA8BD,gBAAgB;AAChB,wBAAgB,mBAAmB,CAAC,MAAM,EACxC,UAAU,EAAE,UAAU,CAAC,MAAM,EAAE,OAAO,CAAC;;iBAEtB,IAAI;EAkCtB"}
1
+ {"version":3,"file":"observable.d.ts","sourceRoot":"","sources":["../../src/observable/observable.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,UAAU,EACV,QAAQ,EAER,aAAa,EAEd,MAAM,SAAS,CAAC;AAEjB,cAAc;AACd,MAAM,MAAM,oBAAoB,CAAC,WAAW,IAAI,WAAW,SAAS,UAAU,CAC5E,MAAM,MAAM,EACZ,OAAO,CACR,GACG,MAAM,GACN,KAAK,CAAC;AAEV,cAAc;AACd,wBAAgB,YAAY,CAAC,CAAC,EAAE,OAAO,GAAG,CAAC,IAAI,UAAU,CAAC,OAAO,EAAE,OAAO,CAAC,CAE1E;AAED,cAAc;AACd,wBAAgB,UAAU,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,EACjD,SAAS,EAAE,CAAC,QAAQ,EAAE,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC,KAAK,aAAa,GAC/D,UAAU,CAAC,MAAM,EAAE,MAAM,CAAC,CA6D5B;AAcD,gBAAgB;AAChB,wBAAgB,mBAAmB,CAAC,MAAM,EACxC,UAAU,EAAE,UAAU,CAAC,MAAM,EAAE,OAAO,CAAC;;iBAEtB,IAAI;EAkCtB"}
@@ -1,8 +1,5 @@
1
1
  'use strict';
2
2
 
3
- function identity(x) {
4
- return x;
5
- }
6
3
  /** @public */ function isObservable(x) {
7
4
  return typeof x === 'object' && x !== null && 'subscribe' in x;
8
5
  }
@@ -60,22 +57,13 @@ function identity(x) {
60
57
  };
61
58
  },
62
59
  pipe (...operations) {
63
- return pipeFromArray(operations)(self);
60
+ return operations.reduce(pipeReducer, self);
64
61
  }
65
62
  };
66
63
  return self;
67
64
  }
68
- function pipeFromArray(fns) {
69
- if (fns.length === 0) {
70
- return identity;
71
- }
72
- if (fns.length === 1) {
73
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
74
- return fns[0];
75
- }
76
- return function piped(input) {
77
- return fns.reduce((prev, fn)=>fn(prev), input);
78
- };
65
+ function pipeReducer(prev, fn) {
66
+ return fn(prev);
79
67
  }
80
68
  class ObservableAbortError extends Error {
81
69
  constructor(message){
@@ -1,6 +1,3 @@
1
- function identity(x) {
2
- return x;
3
- }
4
1
  /** @public */ function isObservable(x) {
5
2
  return typeof x === 'object' && x !== null && 'subscribe' in x;
6
3
  }
@@ -58,22 +55,13 @@ function identity(x) {
58
55
  };
59
56
  },
60
57
  pipe (...operations) {
61
- return pipeFromArray(operations)(self);
58
+ return operations.reduce(pipeReducer, self);
62
59
  }
63
60
  };
64
61
  return self;
65
62
  }
66
- function pipeFromArray(fns) {
67
- if (fns.length === 0) {
68
- return identity;
69
- }
70
- if (fns.length === 1) {
71
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
72
- return fns[0];
73
- }
74
- return function piped(input) {
75
- return fns.reduce((prev, fn)=>fn(prev), input);
76
- };
63
+ function pipeReducer(prev, fn) {
64
+ return fn(prev);
77
65
  }
78
66
  class ObservableAbortError extends Error {
79
67
  constructor(message){
@@ -1 +1 @@
1
- {"version":3,"file":"operators.d.ts","sourceRoot":"","sources":["../../src/observable/operators.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,wBAAwB,EACxB,QAAQ,EACR,gBAAgB,EAEjB,MAAM,SAAS,CAAC;AAEjB,wBAAgB,GAAG,CAAC,YAAY,EAAE,MAAM,EAAE,WAAW,EACnD,OAAO,EAAE,CAAC,KAAK,EAAE,YAAY,EAAE,KAAK,EAAE,MAAM,KAAK,WAAW,GAC3D,gBAAgB,CAAC,YAAY,EAAE,MAAM,EAAE,WAAW,EAAE,MAAM,CAAC,CAoB7D;AAED,UAAU,WAAW;CAAG;AACxB,wBAAgB,KAAK,CAAC,MAAM,EAAE,MAAM,EAClC,KAAK,CAAC,EAAE,WAAW,GAClB,wBAAwB,CAAC,MAAM,EAAE,MAAM,CAAC,CA2D1C;AAED,wBAAgB,GAAG,CAAC,MAAM,EAAE,MAAM,EAChC,QAAQ,EAAE,OAAO,CAAC,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,GAC1C,wBAAwB,CAAC,MAAM,EAAE,MAAM,CAAC,CAqB1C"}
1
+ {"version":3,"file":"operators.d.ts","sourceRoot":"","sources":["../../src/observable/operators.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,wBAAwB,EACxB,QAAQ,EACR,gBAAgB,EAEjB,MAAM,SAAS,CAAC;AAEjB,wBAAgB,GAAG,CAAC,YAAY,EAAE,MAAM,EAAE,WAAW,EACnD,OAAO,EAAE,CAAC,KAAK,EAAE,YAAY,EAAE,KAAK,EAAE,MAAM,KAAK,WAAW,GAC3D,gBAAgB,CAAC,YAAY,EAAE,MAAM,EAAE,WAAW,EAAE,MAAM,CAAC,CAkB7D;AAED,UAAU,WAAW;CAAG;AACxB,wBAAgB,KAAK,CAAC,MAAM,EAAE,MAAM,EAClC,KAAK,CAAC,EAAE,WAAW,GAClB,wBAAwB,CAAC,MAAM,EAAE,MAAM,CAAC,CAyD1C;AAED,wBAAgB,GAAG,CAAC,MAAM,EAAE,MAAM,EAChC,QAAQ,EAAE,OAAO,CAAC,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,GAC1C,wBAAwB,CAAC,MAAM,EAAE,MAAM,CAAC,CAmB1C"}
@@ -1,28 +1,28 @@
1
1
  'use strict';
2
2
 
3
+ var observable = require('./observable.js');
4
+
3
5
  function map(project) {
4
- return (originalObserver)=>{
5
- return {
6
- subscribe (observer) {
7
- let index = 0;
8
- const subscription = originalObserver.subscribe({
9
- next (value) {
10
- observer.next?.(project(value, index++));
11
- },
12
- error (error) {
13
- observer.error?.(error);
14
- },
15
- complete () {
16
- observer.complete?.();
17
- }
18
- });
19
- return subscription;
20
- }
21
- };
6
+ return (source)=>{
7
+ return observable.observable((destination)=>{
8
+ let index = 0;
9
+ const subscription = source.subscribe({
10
+ next (value) {
11
+ destination.next(project(value, index++));
12
+ },
13
+ error (error) {
14
+ destination.error(error);
15
+ },
16
+ complete () {
17
+ destination.complete();
18
+ }
19
+ });
20
+ return subscription;
21
+ });
22
22
  };
23
23
  }
24
24
  function share(_opts) {
25
- return (originalObserver)=>{
25
+ return (source)=>{
26
26
  let refCount = 0;
27
27
  let subscription = null;
28
28
  const observers = [];
@@ -30,7 +30,7 @@ function share(_opts) {
30
30
  if (subscription) {
31
31
  return;
32
32
  }
33
- subscription = originalObserver.subscribe({
33
+ subscription = source.subscribe({
34
34
  next (value) {
35
35
  for (const observer of observers){
36
36
  observer.next?.(value);
@@ -56,45 +56,41 @@ function share(_opts) {
56
56
  _sub.unsubscribe();
57
57
  }
58
58
  }
59
- return {
60
- subscribe (observer) {
61
- refCount++;
62
- observers.push(observer);
63
- startIfNeeded();
64
- return {
65
- unsubscribe () {
66
- refCount--;
67
- resetIfNeeded();
68
- const index = observers.findIndex((v)=>v === observer);
69
- if (index > -1) {
70
- observers.splice(index, 1);
71
- }
59
+ return observable.observable((subscriber)=>{
60
+ refCount++;
61
+ observers.push(subscriber);
62
+ startIfNeeded();
63
+ return {
64
+ unsubscribe () {
65
+ refCount--;
66
+ resetIfNeeded();
67
+ const index = observers.findIndex((v)=>v === subscriber);
68
+ if (index > -1) {
69
+ observers.splice(index, 1);
72
70
  }
73
- };
74
- }
75
- };
71
+ }
72
+ };
73
+ });
76
74
  };
77
75
  }
78
76
  function tap(observer) {
79
- return (originalObserver)=>{
80
- return {
81
- subscribe (observer2) {
82
- return originalObserver.subscribe({
83
- next (v) {
84
- observer.next?.(v);
85
- observer2.next?.(v);
86
- },
87
- error (v) {
88
- observer.error?.(v);
89
- observer2.error?.(v);
90
- },
91
- complete () {
92
- observer.complete?.();
93
- observer2.complete?.();
94
- }
95
- });
96
- }
97
- };
77
+ return (source)=>{
78
+ return observable.observable((destination)=>{
79
+ return source.subscribe({
80
+ next (value) {
81
+ observer.next?.(value);
82
+ destination.next(value);
83
+ },
84
+ error (error) {
85
+ observer.error?.(error);
86
+ destination.error(error);
87
+ },
88
+ complete () {
89
+ observer.complete?.();
90
+ destination.complete();
91
+ }
92
+ });
93
+ });
98
94
  };
99
95
  }
100
96
 
@@ -1,26 +1,26 @@
1
+ import { observable } from './observable.mjs';
2
+
1
3
  function map(project) {
2
- return (originalObserver)=>{
3
- return {
4
- subscribe (observer) {
5
- let index = 0;
6
- const subscription = originalObserver.subscribe({
7
- next (value) {
8
- observer.next?.(project(value, index++));
9
- },
10
- error (error) {
11
- observer.error?.(error);
12
- },
13
- complete () {
14
- observer.complete?.();
15
- }
16
- });
17
- return subscription;
18
- }
19
- };
4
+ return (source)=>{
5
+ return observable((destination)=>{
6
+ let index = 0;
7
+ const subscription = source.subscribe({
8
+ next (value) {
9
+ destination.next(project(value, index++));
10
+ },
11
+ error (error) {
12
+ destination.error(error);
13
+ },
14
+ complete () {
15
+ destination.complete();
16
+ }
17
+ });
18
+ return subscription;
19
+ });
20
20
  };
21
21
  }
22
22
  function share(_opts) {
23
- return (originalObserver)=>{
23
+ return (source)=>{
24
24
  let refCount = 0;
25
25
  let subscription = null;
26
26
  const observers = [];
@@ -28,7 +28,7 @@ function share(_opts) {
28
28
  if (subscription) {
29
29
  return;
30
30
  }
31
- subscription = originalObserver.subscribe({
31
+ subscription = source.subscribe({
32
32
  next (value) {
33
33
  for (const observer of observers){
34
34
  observer.next?.(value);
@@ -54,45 +54,41 @@ function share(_opts) {
54
54
  _sub.unsubscribe();
55
55
  }
56
56
  }
57
- return {
58
- subscribe (observer) {
59
- refCount++;
60
- observers.push(observer);
61
- startIfNeeded();
62
- return {
63
- unsubscribe () {
64
- refCount--;
65
- resetIfNeeded();
66
- const index = observers.findIndex((v)=>v === observer);
67
- if (index > -1) {
68
- observers.splice(index, 1);
69
- }
57
+ return observable((subscriber)=>{
58
+ refCount++;
59
+ observers.push(subscriber);
60
+ startIfNeeded();
61
+ return {
62
+ unsubscribe () {
63
+ refCount--;
64
+ resetIfNeeded();
65
+ const index = observers.findIndex((v)=>v === subscriber);
66
+ if (index > -1) {
67
+ observers.splice(index, 1);
70
68
  }
71
- };
72
- }
73
- };
69
+ }
70
+ };
71
+ });
74
72
  };
75
73
  }
76
74
  function tap(observer) {
77
- return (originalObserver)=>{
78
- return {
79
- subscribe (observer2) {
80
- return originalObserver.subscribe({
81
- next (v) {
82
- observer.next?.(v);
83
- observer2.next?.(v);
84
- },
85
- error (v) {
86
- observer.error?.(v);
87
- observer2.error?.(v);
88
- },
89
- complete () {
90
- observer.complete?.();
91
- observer2.complete?.();
92
- }
93
- });
94
- }
95
- };
75
+ return (source)=>{
76
+ return observable((destination)=>{
77
+ return source.subscribe({
78
+ next (value) {
79
+ observer.next?.(value);
80
+ destination.next(value);
81
+ },
82
+ error (error) {
83
+ observer.error?.(error);
84
+ destination.error(error);
85
+ },
86
+ complete () {
87
+ observer.complete?.();
88
+ destination.complete();
89
+ }
90
+ });
91
+ });
96
92
  };
97
93
  }
98
94
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trpc/server",
3
- "version": "11.0.0-next-beta.303+cdf215127",
3
+ "version": "11.0.0-next-beta.305+0a3c261bd",
4
4
  "description": "The tRPC server library",
5
5
  "author": "KATT",
6
6
  "license": "MIT",
@@ -157,5 +157,5 @@
157
157
  "funding": [
158
158
  "https://trpc.io/sponsor"
159
159
  ],
160
- "gitHead": "cdf215127a172e497468dec04acb8ea12e840e84"
160
+ "gitHead": "0a3c261bd712dd84bf19c6d378a0ea92e1674e1a"
161
161
  }
@@ -14,7 +14,7 @@ import type { FastifyHandlerOptions } from '.';
14
14
  import type { AnyRouter } from '../../@trpc/server';
15
15
  import type { NodeHTTPCreateContextFnOptions } from '../node-http';
16
16
  import type { WSSHandlerOptions } from '../ws';
17
- import { applyWSSHandler } from '../ws';
17
+ import { getWSConnectionHandler } from '../ws';
18
18
  import { fastifyRequestHandler } from './fastifyRequestHandler';
19
19
 
20
20
  export interface FastifyTRPCPluginOptions<TRouter extends AnyRouter> {
@@ -43,7 +43,6 @@ export function fastifyTRPCPlugin<TRouter extends AnyRouter>(
43
43
  );
44
44
 
45
45
  let prefix = opts.prefix ?? '';
46
- const websocketPrefix = prefix;
47
46
 
48
47
  // https://github.com/fastify/fastify-plugin/blob/fe079bef6557a83794bf437e14b9b9edb8a74104/plugin.js#L11
49
48
  // @ts-expect-error property 'default' does not exists on type ...
@@ -57,13 +56,13 @@ export function fastifyTRPCPlugin<TRouter extends AnyRouter>(
57
56
  });
58
57
 
59
58
  if (opts.useWSS) {
60
- applyWSSHandler<TRouter>({
59
+ const onConnection = getWSConnectionHandler<TRouter>({
61
60
  ...(opts.trpcOptions as unknown as WSSHandlerOptions<TRouter>),
62
- prefix: websocketPrefix,
63
- wss: fastify.websocketServer,
64
61
  });
65
- // eslint-disable-next-line @typescript-eslint/no-empty-function
66
- fastify.get(prefix ?? '/', { websocket: true }, () => {});
62
+
63
+ fastify.get(prefix ?? '/', { websocket: true }, ({ socket }, req) =>
64
+ onConnection(socket, req.raw),
65
+ );
67
66
  }
68
67
 
69
68
  done();
@@ -44,42 +44,39 @@ export type CreateWSSContextFn<TRouter extends AnyRouter> = (
44
44
  opts: CreateWSSContextFnOptions,
45
45
  ) => MaybePromise<inferRouterContext<TRouter>>;
46
46
 
47
+ export type WSConnectionHandlerOptions<TRouter extends AnyRouter> =
48
+ BaseHandlerOptions<TRouter, IncomingMessage> &
49
+ (object extends inferRouterContext<TRouter>
50
+ ? {
51
+ /**
52
+ * @link https://trpc.io/docs/v11/context
53
+ **/
54
+ createContext?: CreateWSSContextFn<TRouter>;
55
+ }
56
+ : {
57
+ /**
58
+ * @link https://trpc.io/docs/v11/context
59
+ **/
60
+ createContext: CreateWSSContextFn<TRouter>;
61
+ });
62
+
47
63
  /**
48
64
  * Web socket server handler
49
65
  */
50
- export type WSSHandlerOptions<TRouter extends AnyRouter> = BaseHandlerOptions<
51
- TRouter,
52
- IncomingMessage
53
- > &
54
- (object extends inferRouterContext<TRouter>
55
- ? {
56
- /**
57
- * @link https://trpc.io/docs/v11/context
58
- **/
59
- createContext?: CreateWSSContextFn<TRouter>;
60
- }
61
- : {
62
- /**
63
- * @link https://trpc.io/docs/v11/context
64
- **/
65
- createContext: CreateWSSContextFn<TRouter>;
66
- }) & {
66
+ export type WSSHandlerOptions<TRouter extends AnyRouter> =
67
+ WSConnectionHandlerOptions<TRouter> & {
67
68
  wss: ws.WebSocketServer;
68
69
  process?: NodeJS.Process;
69
70
  prefix?: string;
70
71
  };
71
72
 
72
- export function applyWSSHandler<TRouter extends AnyRouter>(
73
- opts: WSSHandlerOptions<TRouter>,
73
+ export function getWSConnectionHandler<TRouter extends AnyRouter>(
74
+ opts: WSConnectionHandlerOptions<TRouter>,
74
75
  ) {
75
- const { wss, createContext, router, prefix } = opts;
76
-
76
+ const { createContext, router } = opts;
77
77
  const { transformer } = router._def._config;
78
- wss.on('connection', async (client, req) => {
79
- if (prefix && !req.url?.startsWith(prefix)) {
80
- return;
81
- }
82
78
 
79
+ return async (client: ws.WebSocket, req: IncomingMessage) => {
83
80
  const clientSubscriptions = new Map<number | string, Unsubscribable>();
84
81
 
85
82
  function respond(untransformedJSON: TRPCResponseMessage) {
@@ -322,6 +319,21 @@ export function applyWSSHandler<TRouter extends AnyRouter>(
322
319
  }
323
320
  }
324
321
  await createContextAsync();
322
+ };
323
+ }
324
+
325
+ export function applyWSSHandler<TRouter extends AnyRouter>(
326
+ opts: WSSHandlerOptions<TRouter>,
327
+ ) {
328
+ const { wss, prefix } = opts;
329
+
330
+ const onConnection = getWSConnectionHandler(opts);
331
+ wss.on('connection', async (client, req) => {
332
+ if (prefix && !req.url?.startsWith(prefix)) {
333
+ return;
334
+ }
335
+
336
+ await onConnection(client, req);
325
337
  });
326
338
 
327
339
  return {
@@ -6,10 +6,6 @@ import type {
6
6
  UnaryFunction,
7
7
  } from './types';
8
8
 
9
- function identity<TType>(x: TType): TType {
10
- return x;
11
- }
12
-
13
9
  /** @public */
14
10
  export type inferObservableValue<TObservable> = TObservable extends Observable<
15
11
  infer TValue,
@@ -83,30 +79,14 @@ export function observable<TValue, TError = unknown>(
83
79
  pipe(
84
80
  ...operations: OperatorFunction<any, any, any, any>[]
85
81
  ): Observable<any, any> {
86
- return pipeFromArray(operations)(self) as any;
82
+ return operations.reduce(pipeReducer, self);
87
83
  },
88
84
  };
89
85
  return self;
90
86
  }
91
87
 
92
- function pipeFromArray<TSource, TReturn>(
93
- fns: UnaryFunction<TSource, TReturn>[],
94
- ): UnaryFunction<TSource, TReturn> {
95
- if (fns.length === 0) {
96
- return identity as UnaryFunction<any, any>;
97
- }
98
-
99
- if (fns.length === 1) {
100
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
101
- return fns[0]!;
102
- }
103
-
104
- return function piped(input: TSource): TReturn {
105
- return fns.reduce(
106
- (prev: any, fn: UnaryFunction<TSource, TReturn>) => fn(prev),
107
- input as any,
108
- );
109
- };
88
+ function pipeReducer(prev: any, fn: UnaryFunction<any, any>) {
89
+ return fn(prev);
110
90
  }
111
91
 
112
92
  class ObservableAbortError extends Error {
@@ -1,3 +1,4 @@
1
+ import { observable } from './observable';
1
2
  import type {
2
3
  MonoTypeOperatorFunction,
3
4
  Observer,
@@ -8,24 +9,22 @@ import type {
8
9
  export function map<TValueBefore, TError, TValueAfter>(
9
10
  project: (value: TValueBefore, index: number) => TValueAfter,
10
11
  ): OperatorFunction<TValueBefore, TError, TValueAfter, TError> {
11
- return (originalObserver) => {
12
- return {
13
- subscribe(observer) {
14
- let index = 0;
15
- const subscription = originalObserver.subscribe({
16
- next(value) {
17
- observer.next?.(project(value, index++));
18
- },
19
- error(error) {
20
- observer.error?.(error);
21
- },
22
- complete() {
23
- observer.complete?.();
24
- },
25
- });
26
- return subscription;
27
- },
28
- };
12
+ return (source) => {
13
+ return observable((destination) => {
14
+ let index = 0;
15
+ const subscription = source.subscribe({
16
+ next(value) {
17
+ destination.next(project(value, index++));
18
+ },
19
+ error(error) {
20
+ destination.error(error);
21
+ },
22
+ complete() {
23
+ destination.complete();
24
+ },
25
+ });
26
+ return subscription;
27
+ });
29
28
  };
30
29
  }
31
30
 
@@ -33,7 +32,7 @@ interface ShareConfig {}
33
32
  export function share<TValue, TError>(
34
33
  _opts?: ShareConfig,
35
34
  ): MonoTypeOperatorFunction<TValue, TError> {
36
- return (originalObserver) => {
35
+ return (source) => {
37
36
  let refCount = 0;
38
37
 
39
38
  let subscription: Unsubscribable | null = null;
@@ -43,7 +42,7 @@ export function share<TValue, TError>(
43
42
  if (subscription) {
44
43
  return;
45
44
  }
46
- subscription = originalObserver.subscribe({
45
+ subscription = source.subscribe({
47
46
  next(value) {
48
47
  for (const observer of observers) {
49
48
  observer.next?.(value);
@@ -70,50 +69,46 @@ export function share<TValue, TError>(
70
69
  }
71
70
  }
72
71
 
73
- return {
74
- subscribe(observer) {
75
- refCount++;
72
+ return observable((subscriber) => {
73
+ refCount++;
76
74
 
77
- observers.push(observer);
78
- startIfNeeded();
79
- return {
80
- unsubscribe() {
81
- refCount--;
82
- resetIfNeeded();
75
+ observers.push(subscriber);
76
+ startIfNeeded();
77
+ return {
78
+ unsubscribe() {
79
+ refCount--;
80
+ resetIfNeeded();
83
81
 
84
- const index = observers.findIndex((v) => v === observer);
82
+ const index = observers.findIndex((v) => v === subscriber);
85
83
 
86
- if (index > -1) {
87
- observers.splice(index, 1);
88
- }
89
- },
90
- };
91
- },
92
- };
84
+ if (index > -1) {
85
+ observers.splice(index, 1);
86
+ }
87
+ },
88
+ };
89
+ });
93
90
  };
94
91
  }
95
92
 
96
93
  export function tap<TValue, TError>(
97
94
  observer: Partial<Observer<TValue, TError>>,
98
95
  ): MonoTypeOperatorFunction<TValue, TError> {
99
- return (originalObserver) => {
100
- return {
101
- subscribe(observer2) {
102
- return originalObserver.subscribe({
103
- next(v) {
104
- observer.next?.(v);
105
- observer2.next?.(v);
106
- },
107
- error(v) {
108
- observer.error?.(v);
109
- observer2.error?.(v);
110
- },
111
- complete() {
112
- observer.complete?.();
113
- observer2.complete?.();
114
- },
115
- });
116
- },
117
- };
96
+ return (source) => {
97
+ return observable((destination) => {
98
+ return source.subscribe({
99
+ next(value) {
100
+ observer.next?.(value);
101
+ destination.next(value);
102
+ },
103
+ error(error) {
104
+ observer.error?.(error);
105
+ destination.error(error);
106
+ },
107
+ complete() {
108
+ observer.complete?.();
109
+ destination.complete();
110
+ },
111
+ });
112
+ });
118
113
  };
119
114
  }