@trpc/server
Advanced tools
Comparing version 1.1.1 to 1.2.0
@@ -1,4 +0,4 @@ | ||
import type * as express from 'express'; | ||
import * as express from 'express'; | ||
import { BaseOptions, CreateContextFn, CreateContextFnOptions } from '../http'; | ||
import type { Router } from '../router'; | ||
import { Router } from '../router'; | ||
export declare type CreateExpressContextOptions = CreateContextFnOptions<express.Request, express.Response>; | ||
@@ -5,0 +5,0 @@ export declare type CreateExpressContextFn<TContext> = CreateContextFn<TContext, express.Request, express.Response>; |
@@ -1,4 +0,4 @@ | ||
import type { NextApiHandler, NextApiRequest, NextApiResponse } from 'next'; | ||
import { NextApiHandler, NextApiRequest, NextApiResponse } from 'next'; | ||
import { BaseOptions, CreateContextFn, CreateContextFnOptions } from '../http'; | ||
import type { Router } from '../router'; | ||
import { Router } from '../router'; | ||
export declare type CreateNextContextOptions = CreateContextFnOptions<NextApiRequest, NextApiResponse>; | ||
@@ -5,0 +5,0 @@ export declare type CreateNextContextFn<TContext> = CreateContextFn<TContext, NextApiRequest, NextApiResponse>; |
/// <reference types="node" /> | ||
import type { EventEmitter } from 'events'; | ||
import type qs from 'qs'; | ||
import { DataTransformer } from './transformer'; | ||
import http from 'http'; | ||
import qs from 'qs'; | ||
import { InputValidationError } from './errors'; | ||
import { Router } from './router'; | ||
import { DataTransformer } from './transformer'; | ||
export declare class HTTPError extends Error { | ||
@@ -16,2 +16,3 @@ readonly statusCode: number; | ||
notFound: (message?: string | undefined) => HTTPError; | ||
payloadTooLarge: (message?: string | undefined) => HTTPError; | ||
}; | ||
@@ -33,3 +34,3 @@ export declare type HTTPSuccessResponseEnvelope<TOutput> = { | ||
export declare function getErrorResponseEnvelope(_err?: Partial<HTTPError> | InputValidationError<Error>): HTTPErrorResponseEnvelope; | ||
export declare function getQueryInput<TRequest extends BaseRequest>(req: TRequest): unknown; | ||
export declare function getQueryInput(query: qs.ParsedQs): unknown; | ||
export declare type CreateContextFnOptions<TRequest, TResponse> = { | ||
@@ -40,12 +41,8 @@ req: TRequest; | ||
export declare type CreateContextFn<TContext, TRequest, TResponse> = (opts: CreateContextFnOptions<TRequest, TResponse>) => TContext | Promise<TContext>; | ||
interface BaseRequest { | ||
export declare type BaseRequest = http.IncomingMessage & { | ||
method?: string; | ||
query: qs.ParsedQs; | ||
query?: qs.ParsedQs; | ||
body?: any; | ||
} | ||
interface BaseResponse extends EventEmitter { | ||
status: (code: number) => BaseResponse; | ||
json: (data: unknown) => any; | ||
statusCode?: number; | ||
} | ||
}; | ||
export declare type BaseResponse = http.ServerResponse; | ||
export interface BaseOptions { | ||
@@ -60,4 +57,5 @@ subscriptions?: { | ||
transformer?: DataTransformer; | ||
maxBodySize?: number; | ||
} | ||
export declare function requestHandler<TContext, TRouter extends Router<TContext, any, any, any>, TCreateContextFn extends CreateContextFn<TContext, TRequest, TResponse>, TRequest extends BaseRequest, TResponse extends BaseResponse>({ req, res, router, endpoint, subscriptions, createContext, teardown, transformer, }: { | ||
export declare function requestHandler<TContext, TRouter extends Router<TContext, any, any, any>, TCreateContextFn extends CreateContextFn<TContext, TRequest, TResponse>, TRequest extends BaseRequest, TResponse extends BaseResponse>({ req, res, router, endpoint, subscriptions, createContext, teardown, transformer, maxBodySize, }: { | ||
req: TRequest; | ||
@@ -69,2 +67,1 @@ res: TResponse; | ||
} & BaseOptions): Promise<void>; | ||
export {}; |
import { Subscription } from './subscription'; | ||
import { Prefixer, ThenArg } from './types'; | ||
export declare type RouteInputParser<TInput = unknown> = { | ||
parse: (input: unknown) => TInput; | ||
export declare type RouteInputParserZodEsque<TInput = unknown> = { | ||
parse: (input: any) => TInput; | ||
}; | ||
export declare type RouteInputParserCustomValidatorEsque<TInput = unknown> = (input: unknown) => TInput; | ||
export declare type RouteInputParserYupEsque<TInput = unknown> = { | ||
validateSync: (input: unknown) => TInput; | ||
}; | ||
export declare type RouteInputParserJoiEsque<TInput = unknown> = { | ||
validate: (input: unknown) => { | ||
value: TInput; | ||
}; | ||
}; | ||
export declare type RouteInputParser<TInput = unknown> = RouteInputParserZodEsque<TInput> | RouteInputParserYupEsque<TInput> | RouteInputParserCustomValidatorEsque<TInput> | RouteInputParserJoiEsque<TInput>; | ||
export declare type RouteResolver<TContext = unknown, TInput = unknown, TOutput = unknown> = (opts: { | ||
@@ -7,0 +17,0 @@ ctx: TContext; |
@@ -5,3 +5,6 @@ 'use strict'; | ||
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; } | ||
var events = require('events'); | ||
var url = _interopDefault(require('url')); | ||
@@ -1216,2 +1219,5 @@ function assertNotBrowser() { | ||
return new HTTPError(404, message != null ? message : 'Not found'); | ||
}, | ||
payloadTooLarge: function payloadTooLarge(message) { | ||
return new HTTPError(413, message != null ? message : 'Payload Too Large'); | ||
} | ||
@@ -1243,5 +1249,5 @@ }; | ||
} | ||
function getQueryInput(req) { | ||
function getQueryInput(query) { | ||
var input = undefined; | ||
var queryInput = req.query.input; | ||
var queryInput = query.input; | ||
@@ -1265,3 +1271,51 @@ if (!queryInput) { | ||
} | ||
function requestHandler(_x) { | ||
function getPostBody(_x) { | ||
return _getPostBody.apply(this, arguments); | ||
} | ||
function _getPostBody() { | ||
_getPostBody = _asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee(_ref) { | ||
var req, maxBodySize; | ||
return runtime_1.wrap(function _callee$(_context) { | ||
while (1) { | ||
switch (_context.prev = _context.next) { | ||
case 0: | ||
req = _ref.req, maxBodySize = _ref.maxBodySize; | ||
return _context.abrupt("return", new Promise(function (resolve, reject) { | ||
if (req.body) { | ||
resolve(req.body); | ||
return; | ||
} | ||
var body = ''; | ||
req.on('data', function (data) { | ||
body += data; | ||
if (typeof maxBodySize === 'number' && body.length > maxBodySize) { | ||
reject(httpError.payloadTooLarge()); | ||
req.connection.destroy(); | ||
} | ||
}); | ||
req.on('end', function () { | ||
try { | ||
var json = JSON.parse(body); | ||
resolve(json); | ||
} catch (err) { | ||
reject(httpError.badRequest("Body couldn't be parsed as json")); | ||
} | ||
}); | ||
})); | ||
case 2: | ||
case "end": | ||
return _context.stop(); | ||
} | ||
} | ||
}, _callee); | ||
})); | ||
return _getPostBody.apply(this, arguments); | ||
} | ||
function requestHandler(_x2) { | ||
return _requestHandler.apply(this, arguments); | ||
@@ -1271,10 +1325,10 @@ } | ||
function _requestHandler() { | ||
_requestHandler = _asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee(_ref) { | ||
var req, res, router, endpoint, subscriptions, createContext, teardown, _ref$transformer, transformer, _req$method, _res$statusCode, output, ctx, method, deserializeInput, input, _input, _subscriptions$timeou, _input2, sub, onClose, timeout, timer, json, _json; | ||
_requestHandler = _asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee2(_ref2) { | ||
var req, res, router, endpoint, subscriptions, createContext, teardown, _ref2$transformer, transformer, maxBodySize, _req$method, _res$statusCode, output, ctx, method, deserializeInput, body, input, query, _input, _subscriptions$timeou, _body, _input2, sub, onClose, timeout, timer, json, _json; | ||
return runtime_1.wrap(function _callee$(_context) { | ||
return runtime_1.wrap(function _callee2$(_context2) { | ||
while (1) { | ||
switch (_context.prev = _context.next) { | ||
switch (_context2.prev = _context2.next) { | ||
case 0: | ||
req = _ref.req, res = _ref.res, router = _ref.router, endpoint = _ref.endpoint, subscriptions = _ref.subscriptions, createContext = _ref.createContext, teardown = _ref.teardown, _ref$transformer = _ref.transformer, transformer = _ref$transformer === void 0 ? { | ||
req = _ref2.req, res = _ref2.res, router = _ref2.router, endpoint = _ref2.endpoint, subscriptions = _ref2.subscriptions, createContext = _ref2.createContext, teardown = _ref2.teardown, _ref2$transformer = _ref2.transformer, transformer = _ref2$transformer === void 0 ? { | ||
serialize: function serialize(data) { | ||
@@ -1286,5 +1340,12 @@ return data; | ||
} | ||
} : _ref$transformer; | ||
_context.prev = 1; | ||
_context.next = 4; | ||
} : _ref2$transformer, maxBodySize = _ref2.maxBodySize; | ||
_context2.prev = 1; | ||
_context2.t0 = createContext; | ||
if (!_context2.t0) { | ||
_context2.next = 7; | ||
break; | ||
} | ||
_context2.next = 6; | ||
return createContext({ | ||
@@ -1295,4 +1356,7 @@ req: req, | ||
case 4: | ||
ctx = _context.sent; | ||
case 6: | ||
_context2.t0 = _context2.sent; | ||
case 7: | ||
ctx = _context2.t0; | ||
method = (_req$method = req.method) != null ? _req$method : 'GET'; | ||
@@ -1305,8 +1369,16 @@ | ||
if (!(method === 'POST')) { | ||
_context.next = 14; | ||
_context2.next = 20; | ||
break; | ||
} | ||
input = deserializeInput(req.body.input); | ||
_context.next = 11; | ||
_context2.next = 13; | ||
return getPostBody({ | ||
req: req, | ||
maxBodySize: maxBodySize | ||
}); | ||
case 13: | ||
body = _context2.sent; | ||
input = deserializeInput(body.input); | ||
_context2.next = 17; | ||
return router.invoke({ | ||
@@ -1319,15 +1391,17 @@ target: 'mutations', | ||
case 11: | ||
output = _context.sent; | ||
_context.next = 47; | ||
case 17: | ||
output = _context2.sent; | ||
_context2.next = 57; | ||
break; | ||
case 14: | ||
case 20: | ||
if (!(method === 'GET')) { | ||
_context.next = 21; | ||
_context2.next = 28; | ||
break; | ||
} | ||
_input = deserializeInput(getQueryInput(req)); | ||
_context.next = 18; | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
query = req.query ? req.query : url.parse(req.url, true).query; | ||
_input = deserializeInput(getQueryInput(query)); | ||
_context2.next = 25; | ||
return router.invoke({ | ||
@@ -1340,15 +1414,23 @@ target: 'queries', | ||
case 18: | ||
output = _context.sent; | ||
_context.next = 47; | ||
case 25: | ||
output = _context2.sent; | ||
_context2.next = 57; | ||
break; | ||
case 21: | ||
case 28: | ||
if (!(method === 'PATCH')) { | ||
_context.next = 46; | ||
_context2.next = 56; | ||
break; | ||
} | ||
_input2 = deserializeInput(req.body.input); | ||
_context.next = 25; | ||
_context2.next = 31; | ||
return getPostBody({ | ||
req: req, | ||
maxBodySize: maxBodySize | ||
}); | ||
case 31: | ||
_body = _context2.sent; | ||
_input2 = deserializeInput(_body.input); | ||
_context2.next = 35; | ||
return router.invoke({ | ||
@@ -1361,4 +1443,4 @@ target: 'subscriptions', | ||
case 25: | ||
sub = _context.sent; | ||
case 35: | ||
sub = _context2.sent; | ||
@@ -1382,20 +1464,20 @@ onClose = function onClose() { | ||
}, timeout); | ||
_context.prev = 30; | ||
_context.next = 33; | ||
_context2.prev = 40; | ||
_context2.next = 43; | ||
return sub.onceOutputAndStop(); | ||
case 33: | ||
output = _context.sent; | ||
case 43: | ||
output = _context2.sent; | ||
res.off('close', onClose); | ||
_context.next = 44; | ||
_context2.next = 54; | ||
break; | ||
case 37: | ||
_context.prev = 37; | ||
_context.t0 = _context["catch"](30); | ||
case 47: | ||
_context2.prev = 47; | ||
_context2.t1 = _context2["catch"](40); | ||
res.off('close', onClose); | ||
clearTimeout(timer); | ||
if (!(_context.t0 instanceof SubscriptionDestroyError && _context.t0.reason === 'timeout')) { | ||
_context.next = 43; | ||
if (!(_context2.t1 instanceof SubscriptionDestroyError && _context2.t1.reason === 'timeout')) { | ||
_context2.next = 53; | ||
break; | ||
@@ -1406,13 +1488,13 @@ } | ||
case 43: | ||
throw _context.t0; | ||
case 53: | ||
throw _context2.t1; | ||
case 44: | ||
_context.next = 47; | ||
case 54: | ||
_context2.next = 57; | ||
break; | ||
case 46: | ||
case 56: | ||
throw httpError.badRequest("Unexpected request method " + method); | ||
case 47: | ||
case 57: | ||
json = { | ||
@@ -1423,39 +1505,43 @@ ok: true, | ||
}; | ||
res.status(json.statusCode).json(json); | ||
_context.next = 55; | ||
res.statusCode = json.statusCode; | ||
res.setHeader('Content-Type', 'application/json'); | ||
res.end(JSON.stringify(json)); | ||
_context2.next = 69; | ||
break; | ||
case 51: | ||
_context.prev = 51; | ||
_context.t1 = _context["catch"](1); | ||
_json = getErrorResponseEnvelope(_context.t1); | ||
res.status(_json.statusCode).json(_json); | ||
case 63: | ||
_context2.prev = 63; | ||
_context2.t2 = _context2["catch"](1); | ||
_json = getErrorResponseEnvelope(_context2.t2); | ||
res.statusCode = _json.statusCode; | ||
res.setHeader('Content-Type', 'application/json'); | ||
res.end(JSON.stringify(_json)); | ||
case 55: | ||
_context.prev = 55; | ||
_context.t2 = teardown; | ||
case 69: | ||
_context2.prev = 69; | ||
_context2.t3 = teardown; | ||
if (!_context.t2) { | ||
_context.next = 60; | ||
if (!_context2.t3) { | ||
_context2.next = 74; | ||
break; | ||
} | ||
_context.next = 60; | ||
_context2.next = 74; | ||
return teardown(); | ||
case 60: | ||
_context.next = 65; | ||
case 74: | ||
_context2.next = 79; | ||
break; | ||
case 62: | ||
_context.prev = 62; | ||
_context.t3 = _context["catch"](55); | ||
console.error('Teardown failed', _context.t3); | ||
case 76: | ||
_context2.prev = 76; | ||
_context2.t4 = _context2["catch"](69); | ||
console.error('Teardown failed', _context2.t4); | ||
case 65: | ||
case 79: | ||
case "end": | ||
return _context.stop(); | ||
return _context2.stop(); | ||
} | ||
} | ||
}, _callee, null, [[1, 51], [30, 37], [55, 62]]); | ||
}, _callee2, null, [[1, 63], [40, 47], [69, 76]]); | ||
})); | ||
@@ -1579,3 +1665,17 @@ return _requestHandler.apply(this, arguments); | ||
try { | ||
return route.input.parse(rawInput); | ||
var anyInput = route.input; | ||
if (typeof anyInput.parse === 'function') { | ||
return anyInput.parse(rawInput); | ||
} | ||
if (typeof anyInput === 'function') { | ||
return anyInput(rawInput); | ||
} | ||
if (typeof anyInput.validateSync === 'function') { | ||
return anyInput.validateSync(rawInput); | ||
} | ||
throw new Error('Could not find a validator fn'); | ||
} catch (_err) { | ||
@@ -1582,0 +1682,0 @@ var err = new InputValidationError(_err); |
@@ -1,2 +0,2 @@ | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t=require("events");function e(){if("undefined"!=typeof window&&void 0===process.env.JEST_WORKER_ID)throw new Error("Imported server-only code in the broowser")}function r(t,e,r,n,o,i,u){try{var s=t[i](u),a=s.value}catch(t){return void r(t)}s.done?e(a):Promise.resolve(a).then(n,o)}function n(t){return function(){var e=this,n=arguments;return new Promise((function(o,i){var u=t.apply(e,n);function s(t){r(u,o,i,s,a,"next",t)}function a(t){r(u,o,i,s,a,"throw",t)}s(void 0)}))}}function o(){return(o=Object.assign||function(t){for(var e=1;e<arguments.length;e++){var r=arguments[e];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(t[n]=r[n])}return t}).apply(this,arguments)}function i(t,e){t.prototype=Object.create(e.prototype),t.prototype.constructor=t,t.__proto__=e}function u(t){return(u=Object.setPrototypeOf?Object.getPrototypeOf:function(t){return t.__proto__||Object.getPrototypeOf(t)})(t)}function s(t,e){return(s=Object.setPrototypeOf||function(t,e){return t.__proto__=e,t})(t,e)}function a(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Date.prototype.toString.call(Reflect.construct(Date,[],(function(){}))),!0}catch(t){return!1}}function c(t,e,r){return(c=a()?Reflect.construct:function(t,e,r){var n=[null];n.push.apply(n,e);var o=new(Function.bind.apply(t,n));return r&&s(o,r.prototype),o}).apply(null,arguments)}function f(t){var e="function"==typeof Map?new Map:void 0;return(f=function(t){if(null===t||-1===Function.toString.call(t).indexOf("[native code]"))return t;if("function"!=typeof t)throw new TypeError("Super expression must either be null or a function");if(void 0!==e){if(e.has(t))return e.get(t);e.set(t,r)}function r(){return c(t,arguments,u(this).constructor)}return r.prototype=Object.create(t.prototype,{constructor:{value:r,enumerable:!1,writable:!0,configurable:!0}}),s(r,t)})(t)}function p(t){if(void 0===t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return t}function h(t,e){return t(e={exports:{}},e.exports),e.exports}var l=h((function(t){var e=function(t){var e=Object.prototype,r=e.hasOwnProperty,n="function"==typeof Symbol?Symbol:{},o=n.iterator||"@@iterator",i=n.asyncIterator||"@@asyncIterator",u=n.toStringTag||"@@toStringTag";function s(t,e,r){return Object.defineProperty(t,e,{value:r,enumerable:!0,configurable:!0,writable:!0}),t[e]}try{s({},"")}catch(t){s=function(t,e,r){return t[e]=r}}function a(t,e,r,n){var o=Object.create((e&&e.prototype instanceof p?e:p).prototype),i=new O(n||[]);return o._invoke=function(t,e,r){var n="suspendedStart";return function(o,i){if("executing"===n)throw new Error("Generator is already running");if("completed"===n){if("throw"===o)throw i;return{value:void 0,done:!0}}for(r.method=o,r.arg=i;;){var u=r.delegate;if(u){var s=x(u,r);if(s){if(s===f)continue;return s}}if("next"===r.method)r.sent=r._sent=r.arg;else if("throw"===r.method){if("suspendedStart"===n)throw n="completed",r.arg;r.dispatchException(r.arg)}else"return"===r.method&&r.abrupt("return",r.arg);n="executing";var a=c(t,e,r);if("normal"===a.type){if(n=r.done?"completed":"suspendedYield",a.arg===f)continue;return{value:a.arg,done:r.done}}"throw"===a.type&&(n="completed",r.method="throw",r.arg=a.arg)}}}(t,r,i),o}function c(t,e,r){try{return{type:"normal",arg:t.call(e,r)}}catch(t){return{type:"throw",arg:t}}}t.wrap=a;var f={};function p(){}function h(){}function l(){}var d={};d[o]=function(){return this};var v=Object.getPrototypeOf,y=v&&v(v(_([])));y&&y!==e&&r.call(y,o)&&(d=y);var m=l.prototype=p.prototype=Object.create(d);function w(t){["next","throw","return"].forEach((function(e){s(t,e,(function(t){return this._invoke(e,t)}))}))}function g(t,e){var n;this._invoke=function(o,i){function u(){return new e((function(n,u){!function n(o,i,u,s){var a=c(t[o],t,i);if("throw"!==a.type){var f=a.arg,p=f.value;return p&&"object"==typeof p&&r.call(p,"__await")?e.resolve(p.__await).then((function(t){n("next",t,u,s)}),(function(t){n("throw",t,u,s)})):e.resolve(p).then((function(t){f.value=t,u(f)}),(function(t){return n("throw",t,u,s)}))}s(a.arg)}(o,i,n,u)}))}return n=n?n.then(u,u):u()}}function x(t,e){var r=t.iterator[e.method];if(void 0===r){if(e.delegate=null,"throw"===e.method){if(t.iterator.return&&(e.method="return",e.arg=void 0,x(t,e),"throw"===e.method))return f;e.method="throw",e.arg=new TypeError("The iterator does not provide a 'throw' method")}return f}var n=c(r,t.iterator,e.arg);if("throw"===n.type)return e.method="throw",e.arg=n.arg,e.delegate=null,f;var o=n.arg;return o?o.done?(e[t.resultName]=o.value,e.next=t.nextLoc,"return"!==e.method&&(e.method="next",e.arg=void 0),e.delegate=null,f):o:(e.method="throw",e.arg=new TypeError("iterator result is not an object"),e.delegate=null,f)}function b(t){var e={tryLoc:t[0]};1 in t&&(e.catchLoc=t[1]),2 in t&&(e.finallyLoc=t[2],e.afterLoc=t[3]),this.tryEntries.push(e)}function E(t){var e=t.completion||{};e.type="normal",delete e.arg,t.completion=e}function O(t){this.tryEntries=[{tryLoc:"root"}],t.forEach(b,this),this.reset(!0)}function _(t){if(t){var e=t[o];if(e)return e.call(t);if("function"==typeof t.next)return t;if(!isNaN(t.length)){var n=-1,i=function e(){for(;++n<t.length;)if(r.call(t,n))return e.value=t[n],e.done=!1,e;return e.value=void 0,e.done=!0,e};return i.next=i}}return{next:k}}function k(){return{value:void 0,done:!0}}return h.prototype=m.constructor=l,l.constructor=h,h.displayName=s(l,u,"GeneratorFunction"),t.isGeneratorFunction=function(t){var e="function"==typeof t&&t.constructor;return!!e&&(e===h||"GeneratorFunction"===(e.displayName||e.name))},t.mark=function(t){return Object.setPrototypeOf?Object.setPrototypeOf(t,l):(t.__proto__=l,s(t,u,"GeneratorFunction")),t.prototype=Object.create(m),t},t.awrap=function(t){return{__await:t}},w(g.prototype),g.prototype[i]=function(){return this},t.AsyncIterator=g,t.async=function(e,r,n,o,i){void 0===i&&(i=Promise);var u=new g(a(e,r,n,o),i);return t.isGeneratorFunction(r)?u:u.next().then((function(t){return t.done?t.value:u.next()}))},w(m),s(m,u,"Generator"),m[o]=function(){return this},m.toString=function(){return"[object Generator]"},t.keys=function(t){var e=[];for(var r in t)e.push(r);return e.reverse(),function r(){for(;e.length;){var n=e.pop();if(n in t)return r.value=n,r.done=!1,r}return r.done=!0,r}},t.values=_,O.prototype={constructor:O,reset:function(t){if(this.prev=0,this.next=0,this.sent=this._sent=void 0,this.done=!1,this.delegate=null,this.method="next",this.arg=void 0,this.tryEntries.forEach(E),!t)for(var e in this)"t"===e.charAt(0)&&r.call(this,e)&&!isNaN(+e.slice(1))&&(this[e]=void 0)},stop:function(){this.done=!0;var t=this.tryEntries[0].completion;if("throw"===t.type)throw t.arg;return this.rval},dispatchException:function(t){if(this.done)throw t;var e=this;function n(r,n){return u.type="throw",u.arg=t,e.next=r,n&&(e.method="next",e.arg=void 0),!!n}for(var o=this.tryEntries.length-1;o>=0;--o){var i=this.tryEntries[o],u=i.completion;if("root"===i.tryLoc)return n("end");if(i.tryLoc<=this.prev){var s=r.call(i,"catchLoc"),a=r.call(i,"finallyLoc");if(s&&a){if(this.prev<i.catchLoc)return n(i.catchLoc,!0);if(this.prev<i.finallyLoc)return n(i.finallyLoc)}else if(s){if(this.prev<i.catchLoc)return n(i.catchLoc,!0)}else{if(!a)throw new Error("try statement without catch or finally");if(this.prev<i.finallyLoc)return n(i.finallyLoc)}}}},abrupt:function(t,e){for(var n=this.tryEntries.length-1;n>=0;--n){var o=this.tryEntries[n];if(o.tryLoc<=this.prev&&r.call(o,"finallyLoc")&&this.prev<o.finallyLoc){var i=o;break}}i&&("break"===t||"continue"===t)&&i.tryLoc<=e&&e<=i.finallyLoc&&(i=null);var u=i?i.completion:{};return u.type=t,u.arg=e,i?(this.method="next",this.next=i.finallyLoc,f):this.complete(u)},complete:function(t,e){if("throw"===t.type)throw t.arg;return"break"===t.type||"continue"===t.type?this.next=t.arg:"return"===t.type?(this.rval=this.arg=t.arg,this.method="return",this.next="end"):"normal"===t.type&&e&&(this.next=e),f},finish:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var r=this.tryEntries[e];if(r.finallyLoc===t)return this.complete(r.completion,r.afterLoc),E(r),f}},catch:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var r=this.tryEntries[e];if(r.tryLoc===t){var n=r.completion;if("throw"===n.type){var o=n.arg;E(r)}return o}}throw new Error("illegal catch attempt")},delegateYield:function(t,e,r){return this.delegate={iterator:_(t),resultName:e,nextLoc:r},"next"===this.method&&(this.arg=void 0),f}},t}(t.exports);try{regeneratorRuntime=e}catch(t){Function("r","regeneratorRuntime = r")(e)}})),d=function(t){function e(r){var n;return(n=t.call(this,r.message)||this).originalError=r,Object.setPrototypeOf(p(n),e.prototype),n}return i(e,t),e}(f(Error)),v=function(t){function e(r){var n;return n=t.call(this,r)||this,Object.setPrototypeOf(p(n),e.prototype),n}return i(e,t),e}(f(Error)),y=function(t){function e(r){var n;return(n=t.call(this,r)||this).reason=r,Object.setPrototypeOf(p(n),e.prototype),n}return i(e,t),e}(f(Error)),m=function(t){function e(){return t.apply(this,arguments)||this}return i(e,t),e}(t.EventEmitter),w=function(){function t(t){this.isDestroyed=!1,this.events=new m,this.opts=o({getInitialOutput:function(){}},t)}var e=t.prototype;return e.destroy=function(t){this.isDestroyed||(this.isDestroyed=!0,this.events.emit("destroy",t),this.events.removeAllListeners())},e.start=function(){var t=n(l.mark((function t(){var e,r,n=this;return l.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(!this.isDestroyed){t.next=2;break}throw new Error("Called start() on a destroyed subscription");case 2:return t.prev=2,e={error:function(t){return n.emitError(t)},data:function(t){return n.emitOutput(t)}},t.next=6,this.opts.getInitialOutput(e);case 6:r=this.opts.start(e),this.events.on("destroy",(function(){r()})),t.next=13;break;case 10:t.prev=10,t.t0=t.catch(2),this.emitError(t.t0);case 13:case"end":return t.stop()}}),t,this,[[2,10]])})));return function(){return t.apply(this,arguments)}}(),e.onceOutputAndStop=function(){var t=n(l.mark((function t(){var e=this;return l.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.abrupt("return",new Promise(function(){var t=n(l.mark((function t(r,n){var o,i,u,s;return l.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:o=function(t){n(new y(t)),s()},i=function(t){r(t),s(),e.destroy("stopped")},u=function(t){n(t),s(),e.destroy("stopped")},s=function(){e.events.off("data",i),e.events.off("destroy",o),e.events.off("error",u)},e.events.once("data",i),e.events.once("destroy",o),e.events.once("error",u),e.start().catch((function(){}));case 8:case"end":return t.stop()}}),t)})));return function(e,r){return t.apply(this,arguments)}}()));case 1:case"end":return t.stop()}}),t)})));return function(){return t.apply(this,arguments)}}(),e.emitOutput=function(t){this.events.emit("data",t)},e.emitError=function(t){this.events.emit("error",t)},t}();e();var g=function(t){function e(r,n){var o;return(o=t.call(this,n)||this).statusCode=r,Object.setPrototypeOf(p(o),e.prototype),o}return i(e,t),e}(f(Error)),x={forbidden:function(t){return new g(403,null!=t?t:"Forbidden")},unauthorized:function(t){return new g(401,null!=t?t:"Unauthorized")},badRequest:function(t){return new g(400,null!=t?t:"Bad Request")},notFound:function(t){return new g(404,null!=t?t:"Not found")}};function b(t){var e,r,n=t;return n instanceof d?n=x.badRequest(n.message):n instanceof v&&(n=x.notFound(n.message)),{ok:!1,statusCode:"number"==typeof(null==(e=n)?void 0:e.statusCode)?n.statusCode:500,error:{message:"string"==typeof(null==(r=n)?void 0:r.message)?n.message:"Internal Server Error",stack:void 0}}}function E(t){var e=void 0,r=t.query.input;if(!r)return e;if("string"!=typeof r)throw x.badRequest("Expected query.input to be a JSON string");try{e=JSON.parse(r)}catch(t){throw x.badRequest("Expected query.input to be a JSON string")}return e}function O(t){return _.apply(this,arguments)}function _(){return(_=n(l.mark((function t(e){var r,n,o,i,u,s,a,c,f,p,h,d,v,m,w,O,_,k,j,q,L,P,R,S,T;return l.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=e.req,n=e.res,o=e.router,i=e.endpoint,u=e.subscriptions,s=e.createContext,a=e.teardown,f=void 0===(c=e.transformer)?{serialize:function(t){return t},deserialize:function(t){return t}}:c,t.prev=1,t.next=4,s({req:r,res:n});case 4:if(v=t.sent,m=null!=(p=r.method)?p:"GET",w=function(t){return t?f.deserialize(t):t},"POST"!==m){t.next=14;break}return O=w(r.body.input),t.next=11,o.invoke({target:"mutations",input:O,ctx:v,path:i});case 11:d=t.sent,t.next=47;break;case 14:if("GET"!==m){t.next=21;break}return _=w(E(r)),t.next=18,o.invoke({target:"queries",input:_,ctx:v,path:i});case 18:d=t.sent,t.next=47;break;case 21:if("PATCH"!==m){t.next=46;break}return j=w(r.body.input),t.next=25,o.invoke({target:"subscriptions",input:j,ctx:v,path:i});case 25:return q=t.sent,n.once("close",L=function(){q.destroy("closed")}),P=null!=(k=null==u?void 0:u.timeout)?k:9e3,R=setTimeout((function(){q.destroy("timeout")}),P),t.prev=30,t.next=33,q.onceOutputAndStop();case 33:d=t.sent,n.off("close",L),t.next=44;break;case 37:if(t.prev=37,t.t0=t.catch(30),n.off("close",L),clearTimeout(R),!(t.t0 instanceof y&&"timeout"===t.t0.reason)){t.next=43;break}throw new g(408,"Subscription exceeded "+P+"ms - please reconnect.");case 43:throw t.t0;case 44:t.next=47;break;case 46:throw x.badRequest("Unexpected request method "+m);case 47:S={ok:!0,statusCode:null!=(h=n.statusCode)?h:200,data:f.serialize(d)},n.status(S.statusCode).json(S),t.next=55;break;case 51:t.prev=51,t.t1=t.catch(1),T=b(t.t1),n.status(T.statusCode).json(T);case 55:if(t.prev=55,t.t2=a,!t.t2){t.next=60;break}return t.next=60,a();case 60:t.next=65;break;case 62:t.prev=62,t.t3=t.catch(55),console.error("Teardown failed",t.t3);case 65:case"end":return t.stop()}}),t,null,[[1,51],[30,37],[55,62]])})))).apply(this,arguments)}e();var k=function(){function t(t){this._def=null!=t?t:{queries:{},mutations:{},subscriptions:{}}}t.prefixRoutes=function(t,e){var r={};for(var n in t)r[e+n]=t[n];return r};var e=t.prototype;return e.query=function(e,r){var n,o=new t({queries:(n={},n[e]=r,n),mutations:{},subscriptions:{}});return this.merge(o)},e.mutation=function(e,r){var n,o=new t({queries:{},mutations:(n={},n[e]=r,n),subscriptions:{}});return this.merge(o)},e.subscription=function(e,r){var n,o=new t({queries:{},mutations:{},subscriptions:(n={},n[e]=r,n)});return this.merge(o)},e.merge=function(e,r){var n,i=this,u="";if("string"==typeof e&&r instanceof t)u=e,n=r;else{if(!(e instanceof t))throw new Error("Invalid args");n=e}var s=Object.keys(n._def.queries).filter((function(t){return i.has("queries",t)})),a=Object.keys(n._def.mutations).filter((function(t){return i.has("mutations",t)})),c=Object.keys(n._def.subscriptions).filter((function(t){return i.has("subscriptions",t)})),f=[].concat(s,a,c);if(f.length)throw new Error("Duplicate endpoint(s): "+f.join(", "));return new t({queries:o({},this._def.queries,t.prefixRoutes(n._def.queries,u)),mutations:o({},this._def.mutations,t.prefixRoutes(n._def.mutations,u)),subscriptions:o({},this._def.subscriptions,t.prefixRoutes(n._def.subscriptions,u))})},t.getInput=function(t,e){if(t.input)try{return t.input.parse(e)}catch(t){throw new d(t)}},e.invoke=function(){var e=n(l.mark((function e(r){var n,o;return l.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(this.has(r.target,r.path)){e.next=2;break}throw new v('No such route "'+r.path+'"');case 2:return o=t.getInput(n=this._def[r.target][r.path],r.input),e.abrupt("return",n.resolve({ctx:r.ctx,input:o}));case 7:case"end":return e.stop()}}),e,this)})));return function(t){return e.apply(this,arguments)}}(),e.has=function(t,e){return!!this._def[t][e]},t}();exports.HTTPError=g,exports.Router=k,exports.Subscription=w,exports.SubscriptionDestroyError=y,exports.assertNotBrowser=e,exports.createExpressMiddleware=function(t){return function(e,r){var n=e.path.substr(1);O(o({},t,{req:e,res:r,endpoint:n}))}},exports.createNextApiHandler=function(t){return function(){var e=n(l.mark((function e(r,n){var i,u;return l.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(null!==(i=Array.isArray(r.query.trpc)?r.query.trpc.join("/"):null)){e.next=5;break}return u=b(new Error('Query "trpc" not found - is the file named [...trpc].ts?')),n.status(u.statusCode).json(u),e.abrupt("return");case 5:return e.next=7,O(o({},t,{req:r,res:n,endpoint:i}));case 7:case"end":return e.stop()}}),e)})));return function(t,r){return e.apply(this,arguments)}}()},exports.getErrorResponseEnvelope=b,exports.getQueryInput=E,exports.httpError=x,exports.requestHandler=O,exports.router=function(){return new k},exports.subscriptionPullFatory=function(t){var e,r=!1;function o(t){return i.apply(this,arguments)}function i(){return(i=n(l.mark((function n(i){return l.wrap((function(n){for(;;)switch(n.prev=n.next){case 0:if(!r){n.next=2;break}return n.abrupt("return");case 2:return n.prev=2,n.next=5,t.pull(i);case 5:n.next=10;break;case 7:n.prev=7,n.t0=n.catch(2),i.error(n.t0);case 10:r||(e=setTimeout((function(){return o(i)}),t.interval));case 11:case"end":return n.stop()}}),n,null,[[2,7]])})))).apply(this,arguments)}return new w({start:function(t){return o(t),function(){clearTimeout(e),r=!0}}})}; | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t,e=require("events"),r=(t=require("url"))&&"object"==typeof t&&"default"in t?t.default:t;function n(){if("undefined"!=typeof window&&void 0===process.env.JEST_WORKER_ID)throw new Error("Imported server-only code in the broowser")}function o(t,e,r,n,o,i,u){try{var a=t[i](u),s=a.value}catch(t){return void r(t)}a.done?e(s):Promise.resolve(s).then(n,o)}function i(t){return function(){var e=this,r=arguments;return new Promise((function(n,i){var u=t.apply(e,r);function a(t){o(u,n,i,a,s,"next",t)}function s(t){o(u,n,i,a,s,"throw",t)}a(void 0)}))}}function u(){return(u=Object.assign||function(t){for(var e=1;e<arguments.length;e++){var r=arguments[e];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(t[n]=r[n])}return t}).apply(this,arguments)}function a(t,e){t.prototype=Object.create(e.prototype),t.prototype.constructor=t,t.__proto__=e}function s(t){return(s=Object.setPrototypeOf?Object.getPrototypeOf:function(t){return t.__proto__||Object.getPrototypeOf(t)})(t)}function c(t,e){return(c=Object.setPrototypeOf||function(t,e){return t.__proto__=e,t})(t,e)}function f(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Date.prototype.toString.call(Reflect.construct(Date,[],(function(){}))),!0}catch(t){return!1}}function p(t,e,r){return(p=f()?Reflect.construct:function(t,e,r){var n=[null];n.push.apply(n,e);var o=new(Function.bind.apply(t,n));return r&&c(o,r.prototype),o}).apply(null,arguments)}function l(t){var e="function"==typeof Map?new Map:void 0;return(l=function(t){if(null===t||-1===Function.toString.call(t).indexOf("[native code]"))return t;if("function"!=typeof t)throw new TypeError("Super expression must either be null or a function");if(void 0!==e){if(e.has(t))return e.get(t);e.set(t,r)}function r(){return p(t,arguments,s(this).constructor)}return r.prototype=Object.create(t.prototype,{constructor:{value:r,enumerable:!1,writable:!0,configurable:!0}}),c(r,t)})(t)}function h(t){if(void 0===t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return t}function d(t,e){return t(e={exports:{}},e.exports),e.exports}var y=d((function(t){var e=function(t){var e=Object.prototype,r=e.hasOwnProperty,n="function"==typeof Symbol?Symbol:{},o=n.iterator||"@@iterator",i=n.asyncIterator||"@@asyncIterator",u=n.toStringTag||"@@toStringTag";function a(t,e,r){return Object.defineProperty(t,e,{value:r,enumerable:!0,configurable:!0,writable:!0}),t[e]}try{a({},"")}catch(t){a=function(t,e,r){return t[e]=r}}function s(t,e,r,n){var o=Object.create((e&&e.prototype instanceof p?e:p).prototype),i=new O(n||[]);return o._invoke=function(t,e,r){var n="suspendedStart";return function(o,i){if("executing"===n)throw new Error("Generator is already running");if("completed"===n){if("throw"===o)throw i;return{value:void 0,done:!0}}for(r.method=o,r.arg=i;;){var u=r.delegate;if(u){var a=g(u,r);if(a){if(a===f)continue;return a}}if("next"===r.method)r.sent=r._sent=r.arg;else if("throw"===r.method){if("suspendedStart"===n)throw n="completed",r.arg;r.dispatchException(r.arg)}else"return"===r.method&&r.abrupt("return",r.arg);n="executing";var s=c(t,e,r);if("normal"===s.type){if(n=r.done?"completed":"suspendedYield",s.arg===f)continue;return{value:s.arg,done:r.done}}"throw"===s.type&&(n="completed",r.method="throw",r.arg=s.arg)}}}(t,r,i),o}function c(t,e,r){try{return{type:"normal",arg:t.call(e,r)}}catch(t){return{type:"throw",arg:t}}}t.wrap=s;var f={};function p(){}function l(){}function h(){}var d={};d[o]=function(){return this};var y=Object.getPrototypeOf,v=y&&y(y(k([])));v&&v!==e&&r.call(v,o)&&(d=v);var m=h.prototype=p.prototype=Object.create(d);function w(t){["next","throw","return"].forEach((function(e){a(t,e,(function(t){return this._invoke(e,t)}))}))}function x(t,e){var n;this._invoke=function(o,i){function u(){return new e((function(n,u){!function n(o,i,u,a){var s=c(t[o],t,i);if("throw"!==s.type){var f=s.arg,p=f.value;return p&&"object"==typeof p&&r.call(p,"__await")?e.resolve(p.__await).then((function(t){n("next",t,u,a)}),(function(t){n("throw",t,u,a)})):e.resolve(p).then((function(t){f.value=t,u(f)}),(function(t){return n("throw",t,u,a)}))}a(s.arg)}(o,i,n,u)}))}return n=n?n.then(u,u):u()}}function g(t,e){var r=t.iterator[e.method];if(void 0===r){if(e.delegate=null,"throw"===e.method){if(t.iterator.return&&(e.method="return",e.arg=void 0,g(t,e),"throw"===e.method))return f;e.method="throw",e.arg=new TypeError("The iterator does not provide a 'throw' method")}return f}var n=c(r,t.iterator,e.arg);if("throw"===n.type)return e.method="throw",e.arg=n.arg,e.delegate=null,f;var o=n.arg;return o?o.done?(e[t.resultName]=o.value,e.next=t.nextLoc,"return"!==e.method&&(e.method="next",e.arg=void 0),e.delegate=null,f):o:(e.method="throw",e.arg=new TypeError("iterator result is not an object"),e.delegate=null,f)}function b(t){var e={tryLoc:t[0]};1 in t&&(e.catchLoc=t[1]),2 in t&&(e.finallyLoc=t[2],e.afterLoc=t[3]),this.tryEntries.push(e)}function E(t){var e=t.completion||{};e.type="normal",delete e.arg,t.completion=e}function O(t){this.tryEntries=[{tryLoc:"root"}],t.forEach(b,this),this.reset(!0)}function k(t){if(t){var e=t[o];if(e)return e.call(t);if("function"==typeof t.next)return t;if(!isNaN(t.length)){var n=-1,i=function e(){for(;++n<t.length;)if(r.call(t,n))return e.value=t[n],e.done=!1,e;return e.value=void 0,e.done=!0,e};return i.next=i}}return{next:_}}function _(){return{value:void 0,done:!0}}return l.prototype=m.constructor=h,h.constructor=l,l.displayName=a(h,u,"GeneratorFunction"),t.isGeneratorFunction=function(t){var e="function"==typeof t&&t.constructor;return!!e&&(e===l||"GeneratorFunction"===(e.displayName||e.name))},t.mark=function(t){return Object.setPrototypeOf?Object.setPrototypeOf(t,h):(t.__proto__=h,a(t,u,"GeneratorFunction")),t.prototype=Object.create(m),t},t.awrap=function(t){return{__await:t}},w(x.prototype),x.prototype[i]=function(){return this},t.AsyncIterator=x,t.async=function(e,r,n,o,i){void 0===i&&(i=Promise);var u=new x(s(e,r,n,o),i);return t.isGeneratorFunction(r)?u:u.next().then((function(t){return t.done?t.value:u.next()}))},w(m),a(m,u,"Generator"),m[o]=function(){return this},m.toString=function(){return"[object Generator]"},t.keys=function(t){var e=[];for(var r in t)e.push(r);return e.reverse(),function r(){for(;e.length;){var n=e.pop();if(n in t)return r.value=n,r.done=!1,r}return r.done=!0,r}},t.values=k,O.prototype={constructor:O,reset:function(t){if(this.prev=0,this.next=0,this.sent=this._sent=void 0,this.done=!1,this.delegate=null,this.method="next",this.arg=void 0,this.tryEntries.forEach(E),!t)for(var e in this)"t"===e.charAt(0)&&r.call(this,e)&&!isNaN(+e.slice(1))&&(this[e]=void 0)},stop:function(){this.done=!0;var t=this.tryEntries[0].completion;if("throw"===t.type)throw t.arg;return this.rval},dispatchException:function(t){if(this.done)throw t;var e=this;function n(r,n){return u.type="throw",u.arg=t,e.next=r,n&&(e.method="next",e.arg=void 0),!!n}for(var o=this.tryEntries.length-1;o>=0;--o){var i=this.tryEntries[o],u=i.completion;if("root"===i.tryLoc)return n("end");if(i.tryLoc<=this.prev){var a=r.call(i,"catchLoc"),s=r.call(i,"finallyLoc");if(a&&s){if(this.prev<i.catchLoc)return n(i.catchLoc,!0);if(this.prev<i.finallyLoc)return n(i.finallyLoc)}else if(a){if(this.prev<i.catchLoc)return n(i.catchLoc,!0)}else{if(!s)throw new Error("try statement without catch or finally");if(this.prev<i.finallyLoc)return n(i.finallyLoc)}}}},abrupt:function(t,e){for(var n=this.tryEntries.length-1;n>=0;--n){var o=this.tryEntries[n];if(o.tryLoc<=this.prev&&r.call(o,"finallyLoc")&&this.prev<o.finallyLoc){var i=o;break}}i&&("break"===t||"continue"===t)&&i.tryLoc<=e&&e<=i.finallyLoc&&(i=null);var u=i?i.completion:{};return u.type=t,u.arg=e,i?(this.method="next",this.next=i.finallyLoc,f):this.complete(u)},complete:function(t,e){if("throw"===t.type)throw t.arg;return"break"===t.type||"continue"===t.type?this.next=t.arg:"return"===t.type?(this.rval=this.arg=t.arg,this.method="return",this.next="end"):"normal"===t.type&&e&&(this.next=e),f},finish:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var r=this.tryEntries[e];if(r.finallyLoc===t)return this.complete(r.completion,r.afterLoc),E(r),f}},catch:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var r=this.tryEntries[e];if(r.tryLoc===t){var n=r.completion;if("throw"===n.type){var o=n.arg;E(r)}return o}}throw new Error("illegal catch attempt")},delegateYield:function(t,e,r){return this.delegate={iterator:k(t),resultName:e,nextLoc:r},"next"===this.method&&(this.arg=void 0),f}},t}(t.exports);try{regeneratorRuntime=e}catch(t){Function("r","regeneratorRuntime = r")(e)}})),v=function(t){function e(r){var n;return(n=t.call(this,r.message)||this).originalError=r,Object.setPrototypeOf(h(n),e.prototype),n}return a(e,t),e}(l(Error)),m=function(t){function e(r){var n;return n=t.call(this,r)||this,Object.setPrototypeOf(h(n),e.prototype),n}return a(e,t),e}(l(Error)),w=function(t){function e(r){var n;return(n=t.call(this,r)||this).reason=r,Object.setPrototypeOf(h(n),e.prototype),n}return a(e,t),e}(l(Error)),x=function(t){function e(){return t.apply(this,arguments)||this}return a(e,t),e}(e.EventEmitter),g=function(){function t(t){this.isDestroyed=!1,this.events=new x,this.opts=u({getInitialOutput:function(){}},t)}var e=t.prototype;return e.destroy=function(t){this.isDestroyed||(this.isDestroyed=!0,this.events.emit("destroy",t),this.events.removeAllListeners())},e.start=function(){var t=i(y.mark((function t(){var e,r,n=this;return y.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(!this.isDestroyed){t.next=2;break}throw new Error("Called start() on a destroyed subscription");case 2:return t.prev=2,e={error:function(t){return n.emitError(t)},data:function(t){return n.emitOutput(t)}},t.next=6,this.opts.getInitialOutput(e);case 6:r=this.opts.start(e),this.events.on("destroy",(function(){r()})),t.next=13;break;case 10:t.prev=10,t.t0=t.catch(2),this.emitError(t.t0);case 13:case"end":return t.stop()}}),t,this,[[2,10]])})));return function(){return t.apply(this,arguments)}}(),e.onceOutputAndStop=function(){var t=i(y.mark((function t(){var e=this;return y.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.abrupt("return",new Promise(function(){var t=i(y.mark((function t(r,n){var o,i,u,a;return y.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:o=function(t){n(new w(t)),a()},i=function(t){r(t),a(),e.destroy("stopped")},u=function(t){n(t),a(),e.destroy("stopped")},a=function(){e.events.off("data",i),e.events.off("destroy",o),e.events.off("error",u)},e.events.once("data",i),e.events.once("destroy",o),e.events.once("error",u),e.start().catch((function(){}));case 8:case"end":return t.stop()}}),t)})));return function(e,r){return t.apply(this,arguments)}}()));case 1:case"end":return t.stop()}}),t)})));return function(){return t.apply(this,arguments)}}(),e.emitOutput=function(t){this.events.emit("data",t)},e.emitError=function(t){this.events.emit("error",t)},t}();n();var b=function(t){function e(r,n){var o;return(o=t.call(this,n)||this).statusCode=r,Object.setPrototypeOf(h(o),e.prototype),o}return a(e,t),e}(l(Error)),E={forbidden:function(t){return new b(403,null!=t?t:"Forbidden")},unauthorized:function(t){return new b(401,null!=t?t:"Unauthorized")},badRequest:function(t){return new b(400,null!=t?t:"Bad Request")},notFound:function(t){return new b(404,null!=t?t:"Not found")},payloadTooLarge:function(t){return new b(413,null!=t?t:"Payload Too Large")}};function O(t){var e,r,n=t;return n instanceof v?n=E.badRequest(n.message):n instanceof m&&(n=E.notFound(n.message)),{ok:!1,statusCode:"number"==typeof(null==(e=n)?void 0:e.statusCode)?n.statusCode:500,error:{message:"string"==typeof(null==(r=n)?void 0:r.message)?n.message:"Internal Server Error",stack:void 0}}}function k(t){var e=void 0,r=t.input;if(!r)return e;if("string"!=typeof r)throw E.badRequest("Expected query.input to be a JSON string");try{e=JSON.parse(r)}catch(t){throw E.badRequest("Expected query.input to be a JSON string")}return e}function _(t){return q.apply(this,arguments)}function q(){return(q=i(y.mark((function t(e){var r,n;return y.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=e.req,n=e.maxBodySize,t.abrupt("return",new Promise((function(t,e){if(r.body)t(r.body);else{var o="";r.on("data",(function(t){o+=t,"number"==typeof n&&o.length>n&&(e(E.payloadTooLarge()),r.connection.destroy())})),r.on("end",(function(){try{var r=JSON.parse(o);t(r)}catch(t){e(E.badRequest("Body couldn't be parsed as json"))}}))}})));case 2:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function j(t){return L.apply(this,arguments)}function L(){return(L=i(y.mark((function t(e){var n,o,i,u,a,s,c,f,p,l,h,d,v,m,x,g,q,j,L,S,P,R,T,N,C,F,I;return y.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=e.req,o=e.res,i=e.router,u=e.endpoint,a=e.subscriptions,s=e.createContext,c=e.teardown,p=void 0===(f=e.transformer)?{serialize:function(t){return t},deserialize:function(t){return t}}:f,l=e.maxBodySize,t.prev=1,t.t0=s,!t.t0){t.next=7;break}return t.next=6,s({req:n,res:o});case 6:t.t0=t.sent;case 7:if(m=t.t0,x=null!=(h=n.method)?h:"GET",g=function(t){return t?p.deserialize(t):t},"POST"!==x){t.next=20;break}return t.next=13,_({req:n,maxBodySize:l});case 13:return q=g(t.sent.input),t.next=17,i.invoke({target:"mutations",input:q,ctx:m,path:u});case 17:v=t.sent,t.next=57;break;case 20:if("GET"!==x){t.next=28;break}return j=n.query?n.query:r.parse(n.url,!0).query,L=g(k(j)),t.next=25,i.invoke({target:"queries",input:L,ctx:m,path:u});case 25:v=t.sent,t.next=57;break;case 28:if("PATCH"!==x){t.next=56;break}return t.next=31,_({req:n,maxBodySize:l});case 31:return P=g(t.sent.input),t.next=35,i.invoke({target:"subscriptions",input:P,ctx:m,path:u});case 35:return R=t.sent,o.once("close",T=function(){R.destroy("closed")}),N=null!=(S=null==a?void 0:a.timeout)?S:9e3,C=setTimeout((function(){R.destroy("timeout")}),N),t.prev=40,t.next=43,R.onceOutputAndStop();case 43:v=t.sent,o.off("close",T),t.next=54;break;case 47:if(t.prev=47,t.t1=t.catch(40),o.off("close",T),clearTimeout(C),!(t.t1 instanceof w&&"timeout"===t.t1.reason)){t.next=53;break}throw new b(408,"Subscription exceeded "+N+"ms - please reconnect.");case 53:throw t.t1;case 54:t.next=57;break;case 56:throw E.badRequest("Unexpected request method "+x);case 57:F={ok:!0,statusCode:null!=(d=o.statusCode)?d:200,data:p.serialize(v)},o.statusCode=F.statusCode,o.setHeader("Content-Type","application/json"),o.end(JSON.stringify(F)),t.next=69;break;case 63:t.prev=63,t.t2=t.catch(1),I=O(t.t2),o.statusCode=I.statusCode,o.setHeader("Content-Type","application/json"),o.end(JSON.stringify(I));case 69:if(t.prev=69,t.t3=c,!t.t3){t.next=74;break}return t.next=74,c();case 74:t.next=79;break;case 76:t.prev=76,t.t4=t.catch(69),console.error("Teardown failed",t.t4);case 79:case"end":return t.stop()}}),t,null,[[1,63],[40,47],[69,76]])})))).apply(this,arguments)}n();var S=function(){function t(t){this._def=null!=t?t:{queries:{},mutations:{},subscriptions:{}}}t.prefixRoutes=function(t,e){var r={};for(var n in t)r[e+n]=t[n];return r};var e=t.prototype;return e.query=function(e,r){var n,o=new t({queries:(n={},n[e]=r,n),mutations:{},subscriptions:{}});return this.merge(o)},e.mutation=function(e,r){var n,o=new t({queries:{},mutations:(n={},n[e]=r,n),subscriptions:{}});return this.merge(o)},e.subscription=function(e,r){var n,o=new t({queries:{},mutations:{},subscriptions:(n={},n[e]=r,n)});return this.merge(o)},e.merge=function(e,r){var n,o=this,i="";if("string"==typeof e&&r instanceof t)i=e,n=r;else{if(!(e instanceof t))throw new Error("Invalid args");n=e}var a=Object.keys(n._def.queries).filter((function(t){return o.has("queries",t)})),s=Object.keys(n._def.mutations).filter((function(t){return o.has("mutations",t)})),c=Object.keys(n._def.subscriptions).filter((function(t){return o.has("subscriptions",t)})),f=[].concat(a,s,c);if(f.length)throw new Error("Duplicate endpoint(s): "+f.join(", "));return new t({queries:u({},this._def.queries,t.prefixRoutes(n._def.queries,i)),mutations:u({},this._def.mutations,t.prefixRoutes(n._def.mutations,i)),subscriptions:u({},this._def.subscriptions,t.prefixRoutes(n._def.subscriptions,i))})},t.getInput=function(t,e){if(t.input)try{var r=t.input;if("function"==typeof r.parse)return r.parse(e);if("function"==typeof r)return r(e);if("function"==typeof r.validateSync)return r.validateSync(e);throw new Error("Could not find a validator fn")}catch(t){throw new v(t)}},e.invoke=function(){var e=i(y.mark((function e(r){var n,o;return y.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(this.has(r.target,r.path)){e.next=2;break}throw new m('No such route "'+r.path+'"');case 2:return o=t.getInput(n=this._def[r.target][r.path],r.input),e.abrupt("return",n.resolve({ctx:r.ctx,input:o}));case 7:case"end":return e.stop()}}),e,this)})));return function(t){return e.apply(this,arguments)}}(),e.has=function(t,e){return!!this._def[t][e]},t}();exports.HTTPError=b,exports.Router=S,exports.Subscription=g,exports.SubscriptionDestroyError=w,exports.assertNotBrowser=n,exports.createExpressMiddleware=function(t){return function(e,r){var n=e.path.substr(1);j(u({},t,{req:e,res:r,endpoint:n}))}},exports.createNextApiHandler=function(t){return function(){var e=i(y.mark((function e(r,n){var o,i;return y.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(null!==(o=Array.isArray(r.query.trpc)?r.query.trpc.join("/"):null)){e.next=5;break}return i=O(new Error('Query "trpc" not found - is the file named [...trpc].ts?')),n.status(i.statusCode).json(i),e.abrupt("return");case 5:return e.next=7,j(u({},t,{req:r,res:n,endpoint:o}));case 7:case"end":return e.stop()}}),e)})));return function(t,r){return e.apply(this,arguments)}}()},exports.getErrorResponseEnvelope=O,exports.getQueryInput=k,exports.httpError=E,exports.requestHandler=j,exports.router=function(){return new S},exports.subscriptionPullFatory=function(t){var e,r=!1;function n(t){return o.apply(this,arguments)}function o(){return(o=i(y.mark((function o(i){return y.wrap((function(o){for(;;)switch(o.prev=o.next){case 0:if(!r){o.next=2;break}return o.abrupt("return");case 2:return o.prev=2,o.next=5,t.pull(i);case 5:o.next=10;break;case 7:o.prev=7,o.t0=o.catch(2),i.error(o.t0);case 10:r||(e=setTimeout((function(){return n(i)}),t.interval));case 11:case"end":return o.stop()}}),o,null,[[2,7]])})))).apply(this,arguments)}return new g({start:function(t){return n(t),function(){clearTimeout(e),r=!0}}})}; | ||
//# sourceMappingURL=server.cjs.production.min.js.map |
import { EventEmitter } from 'events'; | ||
import url from 'url'; | ||
@@ -1211,2 +1212,5 @@ function assertNotBrowser() { | ||
return new HTTPError(404, message != null ? message : 'Not found'); | ||
}, | ||
payloadTooLarge: function payloadTooLarge(message) { | ||
return new HTTPError(413, message != null ? message : 'Payload Too Large'); | ||
} | ||
@@ -1238,5 +1242,5 @@ }; | ||
} | ||
function getQueryInput(req) { | ||
function getQueryInput(query) { | ||
var input = undefined; | ||
var queryInput = req.query.input; | ||
var queryInput = query.input; | ||
@@ -1260,3 +1264,51 @@ if (!queryInput) { | ||
} | ||
function requestHandler(_x) { | ||
function getPostBody(_x) { | ||
return _getPostBody.apply(this, arguments); | ||
} | ||
function _getPostBody() { | ||
_getPostBody = _asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee(_ref) { | ||
var req, maxBodySize; | ||
return runtime_1.wrap(function _callee$(_context) { | ||
while (1) { | ||
switch (_context.prev = _context.next) { | ||
case 0: | ||
req = _ref.req, maxBodySize = _ref.maxBodySize; | ||
return _context.abrupt("return", new Promise(function (resolve, reject) { | ||
if (req.body) { | ||
resolve(req.body); | ||
return; | ||
} | ||
var body = ''; | ||
req.on('data', function (data) { | ||
body += data; | ||
if (typeof maxBodySize === 'number' && body.length > maxBodySize) { | ||
reject(httpError.payloadTooLarge()); | ||
req.connection.destroy(); | ||
} | ||
}); | ||
req.on('end', function () { | ||
try { | ||
var json = JSON.parse(body); | ||
resolve(json); | ||
} catch (err) { | ||
reject(httpError.badRequest("Body couldn't be parsed as json")); | ||
} | ||
}); | ||
})); | ||
case 2: | ||
case "end": | ||
return _context.stop(); | ||
} | ||
} | ||
}, _callee); | ||
})); | ||
return _getPostBody.apply(this, arguments); | ||
} | ||
function requestHandler(_x2) { | ||
return _requestHandler.apply(this, arguments); | ||
@@ -1266,10 +1318,10 @@ } | ||
function _requestHandler() { | ||
_requestHandler = _asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee(_ref) { | ||
var req, res, router, endpoint, subscriptions, createContext, teardown, _ref$transformer, transformer, _req$method, _res$statusCode, output, ctx, method, deserializeInput, input, _input, _subscriptions$timeou, _input2, sub, onClose, timeout, timer, json, _json; | ||
_requestHandler = _asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee2(_ref2) { | ||
var req, res, router, endpoint, subscriptions, createContext, teardown, _ref2$transformer, transformer, maxBodySize, _req$method, _res$statusCode, output, ctx, method, deserializeInput, body, input, query, _input, _subscriptions$timeou, _body, _input2, sub, onClose, timeout, timer, json, _json; | ||
return runtime_1.wrap(function _callee$(_context) { | ||
return runtime_1.wrap(function _callee2$(_context2) { | ||
while (1) { | ||
switch (_context.prev = _context.next) { | ||
switch (_context2.prev = _context2.next) { | ||
case 0: | ||
req = _ref.req, res = _ref.res, router = _ref.router, endpoint = _ref.endpoint, subscriptions = _ref.subscriptions, createContext = _ref.createContext, teardown = _ref.teardown, _ref$transformer = _ref.transformer, transformer = _ref$transformer === void 0 ? { | ||
req = _ref2.req, res = _ref2.res, router = _ref2.router, endpoint = _ref2.endpoint, subscriptions = _ref2.subscriptions, createContext = _ref2.createContext, teardown = _ref2.teardown, _ref2$transformer = _ref2.transformer, transformer = _ref2$transformer === void 0 ? { | ||
serialize: function serialize(data) { | ||
@@ -1281,5 +1333,12 @@ return data; | ||
} | ||
} : _ref$transformer; | ||
_context.prev = 1; | ||
_context.next = 4; | ||
} : _ref2$transformer, maxBodySize = _ref2.maxBodySize; | ||
_context2.prev = 1; | ||
_context2.t0 = createContext; | ||
if (!_context2.t0) { | ||
_context2.next = 7; | ||
break; | ||
} | ||
_context2.next = 6; | ||
return createContext({ | ||
@@ -1290,4 +1349,7 @@ req: req, | ||
case 4: | ||
ctx = _context.sent; | ||
case 6: | ||
_context2.t0 = _context2.sent; | ||
case 7: | ||
ctx = _context2.t0; | ||
method = (_req$method = req.method) != null ? _req$method : 'GET'; | ||
@@ -1300,8 +1362,16 @@ | ||
if (!(method === 'POST')) { | ||
_context.next = 14; | ||
_context2.next = 20; | ||
break; | ||
} | ||
input = deserializeInput(req.body.input); | ||
_context.next = 11; | ||
_context2.next = 13; | ||
return getPostBody({ | ||
req: req, | ||
maxBodySize: maxBodySize | ||
}); | ||
case 13: | ||
body = _context2.sent; | ||
input = deserializeInput(body.input); | ||
_context2.next = 17; | ||
return router.invoke({ | ||
@@ -1314,15 +1384,17 @@ target: 'mutations', | ||
case 11: | ||
output = _context.sent; | ||
_context.next = 47; | ||
case 17: | ||
output = _context2.sent; | ||
_context2.next = 57; | ||
break; | ||
case 14: | ||
case 20: | ||
if (!(method === 'GET')) { | ||
_context.next = 21; | ||
_context2.next = 28; | ||
break; | ||
} | ||
_input = deserializeInput(getQueryInput(req)); | ||
_context.next = 18; | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
query = req.query ? req.query : url.parse(req.url, true).query; | ||
_input = deserializeInput(getQueryInput(query)); | ||
_context2.next = 25; | ||
return router.invoke({ | ||
@@ -1335,15 +1407,23 @@ target: 'queries', | ||
case 18: | ||
output = _context.sent; | ||
_context.next = 47; | ||
case 25: | ||
output = _context2.sent; | ||
_context2.next = 57; | ||
break; | ||
case 21: | ||
case 28: | ||
if (!(method === 'PATCH')) { | ||
_context.next = 46; | ||
_context2.next = 56; | ||
break; | ||
} | ||
_input2 = deserializeInput(req.body.input); | ||
_context.next = 25; | ||
_context2.next = 31; | ||
return getPostBody({ | ||
req: req, | ||
maxBodySize: maxBodySize | ||
}); | ||
case 31: | ||
_body = _context2.sent; | ||
_input2 = deserializeInput(_body.input); | ||
_context2.next = 35; | ||
return router.invoke({ | ||
@@ -1356,4 +1436,4 @@ target: 'subscriptions', | ||
case 25: | ||
sub = _context.sent; | ||
case 35: | ||
sub = _context2.sent; | ||
@@ -1377,20 +1457,20 @@ onClose = function onClose() { | ||
}, timeout); | ||
_context.prev = 30; | ||
_context.next = 33; | ||
_context2.prev = 40; | ||
_context2.next = 43; | ||
return sub.onceOutputAndStop(); | ||
case 33: | ||
output = _context.sent; | ||
case 43: | ||
output = _context2.sent; | ||
res.off('close', onClose); | ||
_context.next = 44; | ||
_context2.next = 54; | ||
break; | ||
case 37: | ||
_context.prev = 37; | ||
_context.t0 = _context["catch"](30); | ||
case 47: | ||
_context2.prev = 47; | ||
_context2.t1 = _context2["catch"](40); | ||
res.off('close', onClose); | ||
clearTimeout(timer); | ||
if (!(_context.t0 instanceof SubscriptionDestroyError && _context.t0.reason === 'timeout')) { | ||
_context.next = 43; | ||
if (!(_context2.t1 instanceof SubscriptionDestroyError && _context2.t1.reason === 'timeout')) { | ||
_context2.next = 53; | ||
break; | ||
@@ -1401,13 +1481,13 @@ } | ||
case 43: | ||
throw _context.t0; | ||
case 53: | ||
throw _context2.t1; | ||
case 44: | ||
_context.next = 47; | ||
case 54: | ||
_context2.next = 57; | ||
break; | ||
case 46: | ||
case 56: | ||
throw httpError.badRequest("Unexpected request method " + method); | ||
case 47: | ||
case 57: | ||
json = { | ||
@@ -1418,39 +1498,43 @@ ok: true, | ||
}; | ||
res.status(json.statusCode).json(json); | ||
_context.next = 55; | ||
res.statusCode = json.statusCode; | ||
res.setHeader('Content-Type', 'application/json'); | ||
res.end(JSON.stringify(json)); | ||
_context2.next = 69; | ||
break; | ||
case 51: | ||
_context.prev = 51; | ||
_context.t1 = _context["catch"](1); | ||
_json = getErrorResponseEnvelope(_context.t1); | ||
res.status(_json.statusCode).json(_json); | ||
case 63: | ||
_context2.prev = 63; | ||
_context2.t2 = _context2["catch"](1); | ||
_json = getErrorResponseEnvelope(_context2.t2); | ||
res.statusCode = _json.statusCode; | ||
res.setHeader('Content-Type', 'application/json'); | ||
res.end(JSON.stringify(_json)); | ||
case 55: | ||
_context.prev = 55; | ||
_context.t2 = teardown; | ||
case 69: | ||
_context2.prev = 69; | ||
_context2.t3 = teardown; | ||
if (!_context.t2) { | ||
_context.next = 60; | ||
if (!_context2.t3) { | ||
_context2.next = 74; | ||
break; | ||
} | ||
_context.next = 60; | ||
_context2.next = 74; | ||
return teardown(); | ||
case 60: | ||
_context.next = 65; | ||
case 74: | ||
_context2.next = 79; | ||
break; | ||
case 62: | ||
_context.prev = 62; | ||
_context.t3 = _context["catch"](55); | ||
console.error('Teardown failed', _context.t3); | ||
case 76: | ||
_context2.prev = 76; | ||
_context2.t4 = _context2["catch"](69); | ||
console.error('Teardown failed', _context2.t4); | ||
case 65: | ||
case 79: | ||
case "end": | ||
return _context.stop(); | ||
return _context2.stop(); | ||
} | ||
} | ||
}, _callee, null, [[1, 51], [30, 37], [55, 62]]); | ||
}, _callee2, null, [[1, 63], [40, 47], [69, 76]]); | ||
})); | ||
@@ -1574,3 +1658,17 @@ return _requestHandler.apply(this, arguments); | ||
try { | ||
return route.input.parse(rawInput); | ||
var anyInput = route.input; | ||
if (typeof anyInput.parse === 'function') { | ||
return anyInput.parse(rawInput); | ||
} | ||
if (typeof anyInput === 'function') { | ||
return anyInput(rawInput); | ||
} | ||
if (typeof anyInput.validateSync === 'function') { | ||
return anyInput.validateSync(rawInput); | ||
} | ||
throw new Error('Could not find a validator fn'); | ||
} catch (_err) { | ||
@@ -1577,0 +1675,0 @@ var err = new InputValidationError(_err); |
{ | ||
"name": "@trpc/server", | ||
"version": "1.1.1", | ||
"version": "1.2.0", | ||
"description": "TRPC Server", | ||
@@ -40,6 +40,9 @@ "author": "KATT", | ||
"express": "^4.17.1", | ||
"myzod": "^1.3.1", | ||
"next": "^10.0.5", | ||
"ts-json-validator": "^0.7.1", | ||
"yup": "^0.32.8", | ||
"zod": "^1.11.11" | ||
}, | ||
"gitHead": "e4812361ceb1735e0b04281e908d538a32d21ad7" | ||
"gitHead": "d4d5e7a347c39d2169a87fdb207de669db60758b" | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
387389
22
3256
7