Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@effect/rpc

Package Overview
Dependencies
Maintainers
3
Versions
290
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@effect/rpc - npm Package Compare versions

Comparing version 0.27.2 to 0.27.3

42

dist/cjs/Resolver.js

@@ -6,3 +6,3 @@ "use strict";

});
exports.toClient = exports.make = exports.annotateHeadersEffect = exports.annotateHeaders = void 0;
exports.toClient = exports.makeEffect = exports.make = exports.annotateHeadersEffect = exports.annotateHeaders = void 0;
var Headers = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/platform/Http/Headers"));

@@ -12,2 +12,4 @@ var Schema = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/schema/Schema"));

var Cause = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("effect/Cause"));
var Channel = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("effect/Channel"));
var Chunk = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("effect/Chunk"));
var Effect = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("effect/Effect"));

@@ -92,5 +94,41 @@ var Exit = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("effect/Exit"));

* @since 1.0.0
* @category constructors
*/
exports.make = make;
const makeEffect = handler => () => {
const getDecode = (0, _rpc.withRequestTag)(req => Schema.decodeUnknown(Serializable.exitSchema(req)));
const getDecodeChunk = (0, _rpc.withRequestTag)(req => Schema.decodeUnknown(Schema.chunk(Serializable.exitSchema(req))));
return RequestResolver.makeBatched(requests => (0, _Function.pipe)(Effect.forEach(requests, _ => Effect.map(Serializable.serialize(_.request), request => ({
..._,
request
}))), Effect.flatMap(handler), Effect.filterOrDieMessage(_ => Array.isArray(_) && _.length === requests.length, "@effect/rpc: handler must return an array of responses with the same length as the requests."), Effect.flatMap(Effect.forEach((response, index) => {
const request = requests[index];
if (_rpc.StreamRequestTypeId in request.request) {
return (0, _Function.pipe)(getDecodeChunk(request.request)(response), Effect.orDie, Effect.matchCauseEffect({
onFailure: cause => Request.succeed(request, Stream.failCause(cause)),
onSuccess: chunk => {
const lastExit = Chunk.unsafeLast(chunk);
const channel = Exit.match(lastExit, {
onFailure: cause => chunk.length > 1 ? Channel.zipRight(Channel.write(Chunk.map(Chunk.dropRight(chunk, 1), exit => exit.value)), Channel.failCause(cause)) : Channel.failCause(cause),
onSuccess: _ => Channel.write(Chunk.map(chunk, exit => exit.value))
});
return Request.succeed(request, Stream.fromChannel(channel));
}
}));
}
return Effect.matchCauseEffect(Effect.orDie(getDecode(request.request)(response)), {
onFailure: cause => Request.failCause(request, cause),
onSuccess: exit => Request.complete(request, exit)
});
}, {
discard: true
})), Effect.orDie, Effect.catchAllCause(cause => Effect.forEach(requests, request => Request.failCause(request, cause), {
discard: true
}))));
};
/**
* @since 1.0.0
* @category combinators
*/
exports.make = make;
exports.makeEffect = makeEffect;
const annotateHeaders = exports.annotateHeaders = /*#__PURE__*/(0, _Function.dual)(2, (self, headers) => {

@@ -97,0 +135,0 @@ const resolved = Headers.fromInput(headers);

@@ -6,3 +6,3 @@ "use strict";

});
exports.toHandlerUndecoded = exports.toHandlerRaw = exports.toHandler = exports.provideServiceEffect = exports.provideService = exports.make = exports.isRouter = exports.TypeId = void 0;
exports.toHandlerUndecoded = exports.toHandlerRaw = exports.toHandlerEffect = exports.toHandler = exports.provideServiceEffect = exports.provideService = exports.make = exports.isRouter = exports.TypeId = void 0;
var Schema = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/schema/Schema"));

@@ -157,2 +157,42 @@ var Serializable = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/schema/Serializable"));

exports.toHandler = toHandler;
const toHandlerEffect = (router, options) => {
const spanPrefix = options?.spanPrefix ?? "Rpc.router ";
const schema = Schema.union(...[...router.rpcs].map(rpc => Schema.transform(rpc.schema, Schema.to(Schema.tuple(rpc.schema, Schema.any)), request => [request, rpc], ([request]) => request)));
const schemaArray = Schema.array(Rpc.RequestSchema(schema));
const decode = Schema.decodeUnknown(schemaArray);
const getEncode = (0, _rpc.withRequestTag)(req => Schema.encode(Serializable.exitSchema(req)));
const getEncodeChunk = (0, _rpc.withRequestTag)(req => Schema.encode(Schema.chunk(Serializable.exitSchema(req))));
return u => Effect.flatMap(decode(u), Effect.forEach(req => {
const [request, rpc] = req.request;
if (rpc._tag === "Effect") {
const encode = getEncode(request);
return (0, _Function.pipe)(Effect.exit(rpc.handler(request)), Effect.flatMap(encode), Effect.orDie, Effect.locally(Rpc.currentHeaders, req.headers), Effect.withSpan(`${spanPrefix}${request._tag}`, {
parent: {
_tag: "ExternalSpan",
traceId: req.traceId,
spanId: req.spanId,
sampled: req.sampled,
context: Context.empty()
}
}));
}
const encode = getEncodeChunk(request);
return (0, _Function.pipe)(rpc.handler(request), Stream.map(Exit.succeed), Stream.catchAllCause(cause => Stream.succeed(Exit.failCause(cause))), Stream.runCollect, Effect.flatMap(encode), Effect.locally(Rpc.currentHeaders, req.headers), Effect.withSpan(`${spanPrefix}${request._tag}`, {
parent: {
_tag: "ExternalSpan",
traceId: req.traceId,
spanId: req.spanId,
sampled: req.sampled,
context: Context.empty()
}
}));
}, {
concurrency: "unbounded"
}));
};
/**
* @since 1.0.0
* @category combinators
*/
exports.toHandlerEffect = toHandlerEffect;
const toHandlerRaw = router => {

@@ -159,0 +199,0 @@ const schema = Schema.union(...[...router.rpcs].map(rpc => Schema.transform(Schema.to(rpc.schema), Schema.to(Schema.tuple(rpc.schema, Schema.any)), request => [request, rpc], ([request]) => request)));

@@ -21,2 +21,7 @@ /**

* @since 1.0.0
* @category constructors
*/
export declare const makeEffect: <HR, E>(handler: (u: ReadonlyArray<unknown>) => Effect.Effect<unknown, E, HR>) => <R extends Router.Router<any, any>>() => RequestResolver.RequestResolver<Rpc.Request<Router.Router.Request<R>>, HR | Serializable.SerializableWithResult.Context<Router.Router.Request<R>>>;
/**
* @since 1.0.0
* @category combinators

@@ -23,0 +28,0 @@ */

@@ -60,2 +60,7 @@ /**

type Response = [index: number, response: Schema.ExitFrom<any, any> | ReadonlyArray<Schema.ExitFrom<any, any>>];
/**
* @since 1.0.0
* @category models
*/
type ResponseEffect = Schema.ExitFrom<any, any> | ReadonlyArray<Schema.ExitFrom<any, any>>;
}

@@ -102,2 +107,9 @@ /**

*/
export declare const toHandlerEffect: <R extends Router<any, any>>(router: R, options?: {
readonly spanPrefix?: string;
}) => (u: unknown) => Effect.Effect<readonly Router.ResponseEffect[], ParseError, Router.Context<R>>;
/**
* @since 1.0.0
* @category combinators
*/
export declare const toHandlerRaw: <R extends Router<any, any>>(router: R) => <Req extends Router.Request<R>>(request: Req) => Rpc.Rpc.Result<Req, Router.ContextRaw<R>>;

@@ -104,0 +116,0 @@ /**

@@ -8,2 +8,4 @@ /**

import * as Cause from "effect/Cause";
import * as Channel from "effect/Channel";
import * as Chunk from "effect/Chunk";
import * as Effect from "effect/Effect";

@@ -59,2 +61,37 @@ import * as Exit from "effect/Exit";

* @since 1.0.0
* @category constructors
*/
export const makeEffect = handler => () => {
const getDecode = withRequestTag(req => Schema.decodeUnknown(Serializable.exitSchema(req)));
const getDecodeChunk = withRequestTag(req => Schema.decodeUnknown(Schema.chunk(Serializable.exitSchema(req))));
return RequestResolver.makeBatched(requests => pipe(Effect.forEach(requests, _ => Effect.map(Serializable.serialize(_.request), request => ({
..._,
request
}))), Effect.flatMap(handler), Effect.filterOrDieMessage(_ => Array.isArray(_) && _.length === requests.length, "@effect/rpc: handler must return an array of responses with the same length as the requests."), Effect.flatMap(Effect.forEach((response, index) => {
const request = requests[index];
if (StreamRequestTypeId in request.request) {
return pipe(getDecodeChunk(request.request)(response), Effect.orDie, Effect.matchCauseEffect({
onFailure: cause => Request.succeed(request, Stream.failCause(cause)),
onSuccess: chunk => {
const lastExit = Chunk.unsafeLast(chunk);
const channel = Exit.match(lastExit, {
onFailure: cause => chunk.length > 1 ? Channel.zipRight(Channel.write(Chunk.map(Chunk.dropRight(chunk, 1), exit => exit.value)), Channel.failCause(cause)) : Channel.failCause(cause),
onSuccess: _ => Channel.write(Chunk.map(chunk, exit => exit.value))
});
return Request.succeed(request, Stream.fromChannel(channel));
}
}));
}
return Effect.matchCauseEffect(Effect.orDie(getDecode(request.request)(response)), {
onFailure: cause => Request.failCause(request, cause),
onSuccess: exit => Request.complete(request, exit)
});
}, {
discard: true
})), Effect.orDie, Effect.catchAllCause(cause => Effect.forEach(requests, request => Request.failCause(request, cause), {
discard: true
}))));
};
/**
* @since 1.0.0
* @category combinators

@@ -61,0 +98,0 @@ */

@@ -122,2 +122,41 @@ import * as Schema from "@effect/schema/Schema";

*/
export const toHandlerEffect = (router, options) => {
const spanPrefix = options?.spanPrefix ?? "Rpc.router ";
const schema = Schema.union(...[...router.rpcs].map(rpc => Schema.transform(rpc.schema, Schema.to(Schema.tuple(rpc.schema, Schema.any)), request => [request, rpc], ([request]) => request)));
const schemaArray = Schema.array(Rpc.RequestSchema(schema));
const decode = Schema.decodeUnknown(schemaArray);
const getEncode = withRequestTag(req => Schema.encode(Serializable.exitSchema(req)));
const getEncodeChunk = withRequestTag(req => Schema.encode(Schema.chunk(Serializable.exitSchema(req))));
return u => Effect.flatMap(decode(u), Effect.forEach(req => {
const [request, rpc] = req.request;
if (rpc._tag === "Effect") {
const encode = getEncode(request);
return pipe(Effect.exit(rpc.handler(request)), Effect.flatMap(encode), Effect.orDie, Effect.locally(Rpc.currentHeaders, req.headers), Effect.withSpan(`${spanPrefix}${request._tag}`, {
parent: {
_tag: "ExternalSpan",
traceId: req.traceId,
spanId: req.spanId,
sampled: req.sampled,
context: Context.empty()
}
}));
}
const encode = getEncodeChunk(request);
return pipe(rpc.handler(request), Stream.map(Exit.succeed), Stream.catchAllCause(cause => Stream.succeed(Exit.failCause(cause))), Stream.runCollect, Effect.flatMap(encode), Effect.locally(Rpc.currentHeaders, req.headers), Effect.withSpan(`${spanPrefix}${request._tag}`, {
parent: {
_tag: "ExternalSpan",
traceId: req.traceId,
spanId: req.spanId,
sampled: req.sampled,
context: Context.empty()
}
}));
}, {
concurrency: "unbounded"
}));
};
/**
* @since 1.0.0
* @category combinators
*/
export const toHandlerRaw = router => {

@@ -124,0 +163,0 @@ const schema = Schema.union(...[...router.rpcs].map(rpc => Schema.transform(Schema.to(rpc.schema), Schema.to(Schema.tuple(rpc.schema, Schema.any)), request => [request, rpc], ([request]) => request)));

8

package.json
{
"name": "@effect/rpc",
"version": "0.27.2",
"version": "0.27.3",
"description": "Functional programming in TypeScript",

@@ -12,5 +12,5 @@ "license": "MIT",

"peerDependencies": {
"@effect/platform": "^0.44.2",
"@effect/schema": "^0.62.1",
"effect": "^2.3.1"
"@effect/platform": "^0.44.3",
"@effect/schema": "^0.62.2",
"effect": "^2.3.2"
},

@@ -17,0 +17,0 @@ "main": "./dist/cjs/index.js",

@@ -9,2 +9,4 @@ /**

import * as Cause from "effect/Cause"
import * as Channel from "effect/Channel"
import * as Chunk from "effect/Chunk"
import * as Effect from "effect/Effect"

@@ -109,2 +111,73 @@ import * as Exit from "effect/Exit"

* @since 1.0.0
* @category constructors
*/
export const makeEffect = <HR, E>(
handler: (u: ReadonlyArray<unknown>) => Effect.Effect<unknown, E, HR>
) =>
<R extends Router.Router<any, any>>(): RequestResolver.RequestResolver<
Rpc.Request<Router.Router.Request<R>>,
Serializable.SerializableWithResult.Context<Router.Router.Request<R>> | HR
> => {
const getDecode = withRequestTag((req) => Schema.decodeUnknown(Serializable.exitSchema(req)))
const getDecodeChunk = withRequestTag((req) => Schema.decodeUnknown(Schema.chunk(Serializable.exitSchema(req))))
return RequestResolver.makeBatched((requests: Array<Rpc.Request<Schema.TaggedRequest.Any>>) =>
pipe(
Effect.forEach(requests, (_) =>
Effect.map(
Serializable.serialize(_.request),
(request) => ({ ..._, request })
)),
Effect.flatMap(handler),
Effect.filterOrDieMessage(
(_): _ is Array<unknown> => Array.isArray(_) && _.length === requests.length,
"@effect/rpc: handler must return an array of responses with the same length as the requests."
),
Effect.flatMap(Effect.forEach((response, index) => {
const request = requests[index]
if (StreamRequestTypeId in request.request) {
return pipe(
getDecodeChunk(request.request)(response),
Effect.orDie,
Effect.matchCauseEffect({
onFailure: (cause) => Request.succeed(request, Stream.failCause(cause)),
onSuccess: (chunk) => {
const lastExit = Chunk.unsafeLast(chunk)
const channel = Exit.match(lastExit, {
onFailure: (cause) =>
chunk.length > 1 ?
Channel.zipRight(
Channel.write(Chunk.map(
Chunk.dropRight(chunk, 1),
(exit) => (exit as Exit.Success<any, any>).value
)),
Channel.failCause(cause)
) :
Channel.failCause(cause),
onSuccess: (_) => Channel.write(Chunk.map(chunk, (exit) => (exit as Exit.Success<any, any>).value))
})
return Request.succeed(request, Stream.fromChannel(channel))
}
})
)
}
return Effect.matchCauseEffect(Effect.orDie(getDecode(request.request)(response)), {
onFailure: (cause) => Request.failCause(request, cause as any),
onSuccess: (exit) => Request.complete(request, exit as any)
})
}, { discard: true })),
Effect.orDie,
Effect.catchAllCause((cause) =>
Effect.forEach(
requests,
(request) => Request.failCause(request, cause),
{ discard: true }
)
)
)
)
}
/**
* @since 1.0.0
* @category combinators

@@ -111,0 +184,0 @@ */

@@ -81,2 +81,8 @@ /**

export type Response = [index: number, response: Schema.ExitFrom<any, any> | ReadonlyArray<Schema.ExitFrom<any, any>>]
/**
* @since 1.0.0
* @category models
*/
export type ResponseEffect = Schema.ExitFrom<any, any> | ReadonlyArray<Schema.ExitFrom<any, any>>
}

@@ -287,2 +293,71 @@

*/
export const toHandlerEffect = <R extends Router<any, any>>(router: R, options?: {
readonly spanPrefix?: string
}) => {
const spanPrefix = options?.spanPrefix ?? "Rpc.router "
const schema: Schema.Schema<any, unknown, readonly [Schema.TaggedRequest.Any, Rpc.Rpc<any, any>]> = Schema
.union(
...[...router.rpcs].map((rpc) =>
Schema.transform(
rpc.schema,
Schema.to(Schema.tuple(rpc.schema, Schema.any)),
(request) => [request, rpc] as const,
([request]) => request
)
)
)
const schemaArray = Schema.array(Rpc.RequestSchema(schema))
const decode = Schema.decodeUnknown(schemaArray)
const getEncode = withRequestTag((req) => Schema.encode(Serializable.exitSchema(req)))
const getEncodeChunk = withRequestTag((req) => Schema.encode(Schema.chunk(Serializable.exitSchema(req))))
return (u: unknown): Effect.Effect<ReadonlyArray<Router.ResponseEffect>, ParseError, Router.Context<R>> =>
Effect.flatMap(
decode(u),
Effect.forEach((req): Effect.Effect<Router.ResponseEffect, ParseError, any> => {
const [request, rpc] = req.request
if (rpc._tag === "Effect") {
const encode = getEncode(request)
return pipe(
Effect.exit(rpc.handler(request)),
Effect.flatMap(encode),
Effect.orDie,
Effect.locally(Rpc.currentHeaders, req.headers as any),
Effect.withSpan(`${spanPrefix}${request._tag}`, {
parent: {
_tag: "ExternalSpan",
traceId: req.traceId,
spanId: req.spanId,
sampled: req.sampled,
context: Context.empty()
}
})
)
}
const encode = getEncodeChunk(request)
return pipe(
rpc.handler(request),
Stream.map(Exit.succeed),
Stream.catchAllCause((cause) => Stream.succeed(Exit.failCause(cause))),
Stream.runCollect,
Effect.flatMap(encode),
Effect.locally(Rpc.currentHeaders, req.headers as any),
Effect.withSpan(`${spanPrefix}${request._tag}`, {
parent: {
_tag: "ExternalSpan",
traceId: req.traceId,
spanId: req.spanId,
sampled: req.sampled,
context: Context.empty()
}
})
)
}, { concurrency: "unbounded" })
)
}
/**
* @since 1.0.0
* @category combinators
*/
export const toHandlerRaw = <R extends Router<any, any>>(router: R) => {

@@ -289,0 +364,0 @@ const schema: Schema.Schema<

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

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc