@pritzlaff/nano-app 0.5.1 → 0.5.2

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.
@@ -409,7 +409,7 @@ var json = (s) => ({ type: s.label ?? "", schema: JSON.parse(JSON.stringify(s.sc
409
409
  var describeProvider = (p, views) => ({
410
410
  coord: p.coord,
411
411
  meta: { ...p.meta },
412
- views: { ...views },
412
+ views: Object.fromEntries(Object.entries(views).map(([c, v]) => [c, typeof v === "string" ? v : [...v]])),
413
413
  implementations: [...p.implementations.values()].map((i) => ({
414
414
  coord: i.coord,
415
415
  ...i.description !== undefined && { description: i.description },
@@ -461,7 +461,7 @@ function implError(i) {
461
461
  }
462
462
  return schema(i.config, `${i.coord} config`);
463
463
  }
464
- var described = (p) => !obj(p) || !str(p.coord) || !obj(p.meta) || !Object.values(p.meta).every(str) || !obj(p.views) || !Object.values(p.views).every(str) || !Array.isArray(p.implementations) || p.built !== undefined && (!obj(p.built) || !Object.values(p.built).every(str)) ? "a provider needs coord, meta, views and implementations (and a built, if any, of strings)" : p.implementations.map(implError).find((w) => w !== undefined);
464
+ var described = (p) => !obj(p) || !str(p.coord) || !obj(p.meta) || !Object.values(p.meta).every(str) || !obj(p.views) || !Object.values(p.views).every((v) => str(v) || Array.isArray(v) && v.every(str)) || !Array.isArray(p.implementations) || p.built !== undefined && (!obj(p.built) || !Object.values(p.built).every(str)) ? "a provider needs coord, meta, views (tags, or lists of tags) and implementations (and a built, if any, of strings)" : p.implementations.map(implError).find((w) => w !== undefined);
465
465
  var result = (v) => isNanoResult(v);
466
466
  var FIELDS = {
467
467
  build: { pod: str, runtimes: strs, coord: str, url: str, config: any, edges: arrOf(edge) },
@@ -2313,6 +2313,173 @@ async function load2(url, opts = {}) {
2313
2313
  return app.value;
2314
2314
  }
2315
2315
  var Application = { create: create4, load: load2 };
2316
+ // ../nano/src/nano-deploy.ts
2317
+ var nameOf = (coord) => {
2318
+ const n = coord.slice(coord.lastIndexOf(".") + 1);
2319
+ return n[0].toLowerCase() + n.slice(1);
2320
+ };
2321
+ var nsOf = (coord) => coord.slice(0, coord.lastIndexOf("."));
2322
+ var must = (r) => {
2323
+ if (!r.ok)
2324
+ throw new Error(`${r.fail.type}: ${r.fail.reason}`);
2325
+ return r.value;
2326
+ };
2327
+ function place(roots) {
2328
+ const names = new Map, taken = new Map;
2329
+ const claim = (name, b) => {
2330
+ const prior = taken.get(name);
2331
+ if (prior && prior !== b)
2332
+ throw new Error(`two chains named ${name}`);
2333
+ if (names.has(b))
2334
+ return false;
2335
+ names.set(b, name);
2336
+ taken.set(name, b);
2337
+ return true;
2338
+ };
2339
+ const visit = (b) => {
2340
+ for (const [k, u] of b.uses)
2341
+ if (claim(k, u))
2342
+ visit(u);
2343
+ };
2344
+ for (const [k, b] of Object.entries(roots))
2345
+ claim(k, b);
2346
+ for (const b of Object.values(roots))
2347
+ visit(b);
2348
+ return names;
2349
+ }
2350
+ var deploymentOf = (names) => ({
2351
+ pods: Object.fromEntries([...names].map(([b, n]) => [n, Pod.lazy(b.implementation, b.cfg && { config: b.cfg.value })])),
2352
+ runtimes: { main: {
2353
+ pods: [...names.values()],
2354
+ subscriptions: [...names].flatMap(([b, n]) => b.uses.map(([, u]) => ({ kind: "call", from: n, to: names.get(u) })))
2355
+ } }
2356
+ });
2357
+ function registryOf(chains) {
2358
+ const registry = Registry.create();
2359
+ for (const [ns, bs] of Map.groupBy(chains, (b) => nsOf(b.coord)))
2360
+ must(registry.add(Provider.define(ns, bs.map((b) => b.implementation))));
2361
+ return registry;
2362
+ }
2363
+ async function deploy(root) {
2364
+ const names = place({ [nameOf(root.coord)]: root });
2365
+ const app = must(await Application.create(must(AppDeployment.assemble(deploymentOf(names), registryOf([...names.keys()])))));
2366
+ return Object.assign(Object.create(app.runtime("main").get(root.interface)), { app });
2367
+ }
2368
+
2369
+ // ../nano/src/nano-app.ts
2370
+ var flow = (steps) => ({ kind: typeof steps.at(-1) === "function" ? "pending" : "flow", steps, andThen: (next) => flow([...steps, next]) });
2371
+ var cap = (s) => s[0].toUpperCase() + s.slice(1);
2372
+ var coordOf = (h) => h.chain.coord;
2373
+ function build(ns, uses, flows) {
2374
+ const names = place(Object.fromEntries(uses)), pipes = [];
2375
+ const pod2 = (h) => names.get(h.chain) ?? (() => {
2376
+ throw new Error(`${coordOf(h)} is not in the app`);
2377
+ })();
2378
+ const pipe = (a, b) => pipes.push({ kind: "pipe", from: pod2(a), source: port2(a.chain.interface, a.op), to: pod2(b), target: port2(b.chain.interface, b.op) });
2379
+ for (const { steps } of flows) {
2380
+ let from = steps[0], fns = [];
2381
+ for (const s of steps.slice(1)) {
2382
+ if (typeof s === "function") {
2383
+ fns.push(s);
2384
+ continue;
2385
+ }
2386
+ if (fns.length) {
2387
+ const name = `${nameOf(coordOf(from))}${cap(from.op.name)}To${cap(nameOf(coordOf(s)))}${cap(s.op.name)}`;
2388
+ const run = fns;
2389
+ const m = Nano.create(`${ns}.${cap(name)}`).action("map", from.op.output, s.op.input, async (x) => {
2390
+ for (const f of run)
2391
+ x = await f(x);
2392
+ return x;
2393
+ });
2394
+ if ([...names.values()].includes(name))
2395
+ throw new Error(`two mappers named ${name}`);
2396
+ names.set(m, name);
2397
+ pipe(from, m.actions.map);
2398
+ from = m.actions.map;
2399
+ fns = [];
2400
+ }
2401
+ pipe(from, s);
2402
+ from = s;
2403
+ }
2404
+ }
2405
+ const d = deploymentOf(names);
2406
+ d.runtimes.main.subscriptions = [...d.runtimes.main.subscriptions, ...pipes];
2407
+ return { names, deployment: d };
2408
+ }
2409
+ function app(ns, uses = [], flows = []) {
2410
+ let built;
2411
+ const get = () => built ??= build(ns, uses, flows);
2412
+ return {
2413
+ use: (deps) => app(ns, [...uses, ...Object.entries(deps)], flows),
2414
+ subscribe: (...more) => app(ns, uses, [...flows, ...more]),
2415
+ get deployment() {
2416
+ return get().deployment;
2417
+ },
2418
+ get provider() {
2419
+ const chains = [...get().names.keys()], stray = chains.filter((c) => nsOf(c.coord) !== ns).map((c) => c.coord);
2420
+ if (stray.length)
2421
+ throw new Error(`not in ${ns}: ${stray.join(", ")}`);
2422
+ return Provider.define(ns, chains.map((c) => c.implementation));
2423
+ },
2424
+ async deploy() {
2425
+ const { names, deployment } = get();
2426
+ const app2 = must(await Application.create(must(AppDeployment.assemble(deployment, registryOf([...names.keys()])))));
2427
+ const clients = uses.map(([k, b]) => [k, app2.runtime("main").get(b.interface, { pod: names.get(b) })]);
2428
+ return { ...Object.fromEntries(clients), app: app2 };
2429
+ }
2430
+ };
2431
+ }
2432
+
2433
+ // ../nano/src/nano.ts
2434
+ var none = async function* () {};
2435
+ function chain(spec) {
2436
+ const { coord, ops, uses, init, cfg } = spec;
2437
+ let iface, impl2;
2438
+ const api = () => iface ??= Interface.define(coord, Object.fromEntries(Object.entries(ops).map(([k, v]) => [k, v.op])));
2439
+ const builder2 = () => api().impl(`${nsOf(coord)}/${nameOf(coord)}`).needs(Object.fromEntries(uses.map(([k, u]) => [k, u.interface])));
2440
+ const implementation = () => impl2 ??= (cfg ? builder2().config(cfg.schema) : builder2()).make((ctx) => {
2441
+ const { config } = ctx;
2442
+ const state = init?.();
2443
+ const handle = (c) => Object.assign(Object.create(c), {
2444
+ state,
2445
+ config,
2446
+ publish: (o, d) => c.publish(typeof o === "string" ? api().ops[o] : o, d)
2447
+ });
2448
+ return Object.fromEntries(Object.entries(ops).map(([name, { op, fn }]) => [name, op.kind === "event" ? () => fn(undefined, handle(ctx)) : (input, invocation) => fn(input, handle(invocation ?? ctx))]));
2449
+ });
2450
+ const handles = (kind) => () => Object.fromEntries(Object.values(api().ops).filter((o) => o.kind === kind).map((o) => {
2451
+ const h = { chain: self, op: o, andThen: (next) => flow([h, next]) };
2452
+ return [o.name, h];
2453
+ }));
2454
+ const self = {
2455
+ coord,
2456
+ uses,
2457
+ cfg,
2458
+ get interface() {
2459
+ return api();
2460
+ },
2461
+ get implementation() {
2462
+ return implementation();
2463
+ },
2464
+ get actions() {
2465
+ return handles("action")();
2466
+ },
2467
+ get events() {
2468
+ return handles("event")();
2469
+ },
2470
+ state: (i) => chain({ ...spec, init: i }),
2471
+ config: (schema, value2) => chain({ ...spec, cfg: { schema, value: value2 } }),
2472
+ use: (deps) => chain({ ...spec, uses: [...uses, ...Object.entries(deps)] }),
2473
+ action: (name, input, output, fn) => chain({ ...spec, ops: { ...ops, [name]: { op: Op.action(input, output), fn } } }),
2474
+ emit: (name, data, gen) => chain({ ...spec, ops: { ...ops, [name]: { op: Op.event(data), fn: (_, h) => (gen ?? none)(h) } } }),
2475
+ deploy: () => deploy(self)
2476
+ };
2477
+ return self;
2478
+ }
2479
+ var Nano = {
2480
+ create: (coord) => chain({ coord, ops: {}, uses: [] }),
2481
+ app: (namespace3) => app(namespace3)
2482
+ };
2316
2483
  export {
2317
2484
  success,
2318
2485
  selectorData,
@@ -2338,6 +2505,7 @@ export {
2338
2505
  Pods,
2339
2506
  Pod,
2340
2507
  Op,
2508
+ Nano,
2341
2509
  Interface,
2342
2510
  Implementation,
2343
2511
  DeploymentPolicies,
@@ -1,6 +1,6 @@
1
1
  {
2
- "@pritzlaff/nano-core": "0.5.0",
3
- "@pritzlaff/nano-elements": "0.5.0",
2
+ "@pritzlaff/nano-core": "0.5.1",
3
+ "@pritzlaff/nano-elements": "0.5.1",
4
4
  "lit": "3.3.3",
5
5
  "typebox": "1.4.4"
6
6
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pritzlaff/nano-app",
3
- "version": "0.5.1",
3
+ "version": "0.5.2",
4
4
  "description": "test-nano: serve the nano app and a project on loopback, save the project's nano.json, rebuild on change.",
5
5
  "license": "MIT",
6
6
  "type": "module",