MCPcopy
hub / github.com/cloudflare/capnweb / wrapClientStub

Function wrapClientStub

packages/capnweb-validate/src/internal/core.ts:1225–1279  ·  view source on GitHub ↗
(
  stub: object,
  validator: ServiceValidator
)

Source from the content-addressed store, hash-verified

1223// ---------------------------------------------------------------------------
1224
1225export function wrapClientStub(
1226 stub: object,
1227 validator: ServiceValidator
1228): object {
1229 return new Proxy(stub, {
1230 get(t, prop, receiver) {
1231 let orig = Reflect.get(t, prop, receiver);
1232 if (typeof prop !== "string") {
1233 // See wrapServerTarget: forward symbol-keyed members bound to the
1234 // target so disposal and other well-known-symbol hooks keep working.
1235 return typeof orig === "function" ? orig.bind(t) : orig;
1236 }
1237 let methodSpec = own(validator.methods, prop);
1238 if (methodSpec && !isUncheckedMethod(methodSpec) && methodSpec.isGetter) {
1239 return validateReturn(
1240 orig,
1241 methodSpec.returns,
1242 [validator.serviceName, prop],
1243 "client",
1244 validator.mode ?? "throw"
1245 );
1246 }
1247 if (typeof orig !== "function") return orig;
1248 if (!methodSpec) {
1249 if (prop === "dup") {
1250 return (...args: unknown[]): unknown =>
1251 wrapClientStub(
1252 (orig as (...a: unknown[]) => unknown).apply(t, args) as object,
1253 validator
1254 );
1255 }
1256 if (canPassThrough(prop, validator, "client"))
1257 return (orig as (...a: unknown[]) => unknown).bind(t);
1258 return (..._args: unknown[]): never =>
1259 missingMethod(validator.serviceName, prop);
1260 }
1261 let mode = validator.mode ?? "throw";
1262 return function wrapped(this: unknown, ...args: unknown[]): unknown {
1263 let result = Reflect.apply(
1264 orig as (...a: unknown[]) => unknown,
1265 t,
1266 args
1267 );
1268 if (isUncheckedMethod(methodSpec)) return result;
1269 return validateReturn(
1270 result,
1271 methodSpec.returns,
1272 [validator.serviceName, prop, "<return>"],
1273 "client",
1274 mode
1275 );
1276 };
1277 },
1278 });
1279}
1280
1281export function __validateStub<T>(
1282 stub: object,

Callers 8

wrapResolvedValueFunction · 0.85
getFunction · 0.85
__validateStubFunction · 0.85
getters.test.tsFile · 0.85
serviceFunction · 0.85

Calls

no outgoing calls

Tested by 1

serviceFunction · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…