@kabobcompany/lift 0.3.0 → 0.4.0

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.
package/README.md CHANGED
@@ -34,6 +34,10 @@ and `storage` are the rest. An app reaches only its own files.
34
34
  `sendSms({ to, text })` a text message to one phone, both on Kabob's account and within
35
35
  the limits Lift sets; `email()` and `sms()` say what those are and what was sent today.
36
36
 
37
+ `channelTicket({ channels, client })` lets one client of the app listen to channels over a
38
+ long connection to Lift, and `publish(channel, event, data)` says something to everyone
39
+ listening; `listeners` and `channels` show who is connected.
40
+
37
41
  A refusal is thrown as a `LiftError` with `status`, `code`, `message` and, on a 422,
38
42
  `issues`.
39
43
 
package/dist/index.d.ts CHANGED
@@ -496,4 +496,78 @@ export declare const sms: (options?: Paid) => Promise<{
496
496
  countries: string[];
497
497
  };
498
498
  }>;
499
+ export type ChannelTicket = {
500
+ /** What the client presents. */
501
+ ticket: string;
502
+ /** Where the client connects with it: `new EventSource(url)`. */
503
+ url: string;
504
+ expires_at: string;
505
+ };
506
+ /**
507
+ * A ticket for one client, naming the channels it may listen to:
508
+ *
509
+ * app.get("/api/ticket", requireCompany, async (c) =>
510
+ * c.json(await channelTicket({ channels: [`company-${c.get("company").id}`], client: c.get("session").user.id })))
511
+ *
512
+ * Give one only to a client the app has recognised (a session, a paired
513
+ * device), and only for channels that client should hear: whoever holds the
514
+ * ticket hears them. `client` is the app's own word for who this is and
515
+ * `meta` a small object, both shown back in `listeners`. It works for `ttl`
516
+ * seconds (an hour unless said, a day at most); the stream ends then with a
517
+ * `lift.expired` event and the client asks for a new one. 20 channels a
518
+ * ticket at most.
519
+ */
520
+ export declare const channelTicket: (ask: {
521
+ channels: string[];
522
+ client?: string | number;
523
+ meta?: Record<string, unknown>;
524
+ ttl?: number;
525
+ }) => Promise<ChannelTicket>;
526
+ /**
527
+ * Say something to everyone listening to a channel:
528
+ *
529
+ * await publish(`brand-${brand.id}`, "content", { version: 12 }, { retain: true })
530
+ *
531
+ * `event` is one word for what happened and `data` small JSON (16 KB): say
532
+ * what changed and let the client fetch it. With `retain` the message is
533
+ * also kept as the channel's latest, and whoever connects later gets it
534
+ * first. Answers with the message's id and how many were listening.
535
+ *
536
+ * A channel needs no creating: any name of letters, digits, dashes, dots,
537
+ * colons and underscores. A message reaches who is connected now; it is not
538
+ * a queue, so what must not be lost also goes in the app's database.
539
+ */
540
+ export declare const publish: (channel: string, event: string, data?: unknown, options?: {
541
+ retain?: boolean;
542
+ }) => Promise<{
543
+ id: number;
544
+ listeners: number;
545
+ }>;
546
+ export type Listener = {
547
+ client: string | null;
548
+ meta: Record<string, unknown>;
549
+ since: string;
550
+ };
551
+ /** Who is connected to a channel right now, longest first. */
552
+ export declare const listeners: (channel: string) => Promise<Listener[]>;
553
+ /** The app's channels that somebody listens to or that keep a message, its open connections, and the limits. */
554
+ export declare const channels: () => Promise<{
555
+ connections: number;
556
+ channels: {
557
+ name: string;
558
+ listeners: number;
559
+ kept: {
560
+ event: string;
561
+ at: string;
562
+ } | null;
563
+ }[];
564
+ limits: {
565
+ connections_per_app: number;
566
+ channels_per_ticket: number;
567
+ message_bytes: number;
568
+ publishes_per_minute: number;
569
+ };
570
+ }>;
571
+ /** Forget what a channel keeps as its latest. */
572
+ export declare const forgetKept: (channel: string) => Promise<void>;
499
573
  export {};
package/dist/index.js CHANGED
@@ -466,3 +466,51 @@ export async function sendSms(message, options = {}) {
466
466
  }
467
467
  /** How many segments the app sent today, its limits, and the countries Lift texts. */
468
468
  export const sms = (options = {}) => sending("sms", "GET", "", undefined, options);
469
+ // Channels: telling many clients something at once. Clients (screens,
470
+ // kiosks, phones, open pages) each hold one long connection to Lift and
471
+ // listen; the app's server says who may listen to what (`channelTicket`) and
472
+ // what to say (`publish`). The app holds no connection itself, so it can
473
+ // sleep and be deployed without anyone dropping. Needs the app's Channels
474
+ // switch on Lift.
475
+ const channelsCall = async (method, path, body) => paidAnswer(await paid("channels", method, path, body, { timeoutMs: 25_000 }), "Lift");
476
+ /**
477
+ * A ticket for one client, naming the channels it may listen to:
478
+ *
479
+ * app.get("/api/ticket", requireCompany, async (c) =>
480
+ * c.json(await channelTicket({ channels: [`company-${c.get("company").id}`], client: c.get("session").user.id })))
481
+ *
482
+ * Give one only to a client the app has recognised (a session, a paired
483
+ * device), and only for channels that client should hear: whoever holds the
484
+ * ticket hears them. `client` is the app's own word for who this is and
485
+ * `meta` a small object, both shown back in `listeners`. It works for `ttl`
486
+ * seconds (an hour unless said, a day at most); the stream ends then with a
487
+ * `lift.expired` event and the client asks for a new one. 20 channels a
488
+ * ticket at most.
489
+ */
490
+ export const channelTicket = (ask) => channelsCall("POST", "tickets", ask);
491
+ /**
492
+ * Say something to everyone listening to a channel:
493
+ *
494
+ * await publish(`brand-${brand.id}`, "content", { version: 12 }, { retain: true })
495
+ *
496
+ * `event` is one word for what happened and `data` small JSON (16 KB): say
497
+ * what changed and let the client fetch it. With `retain` the message is
498
+ * also kept as the channel's latest, and whoever connects later gets it
499
+ * first. Answers with the message's id and how many were listening.
500
+ *
501
+ * A channel needs no creating: any name of letters, digits, dashes, dots,
502
+ * colons and underscores. A message reaches who is connected now; it is not
503
+ * a queue, so what must not be lost also goes in the app's database.
504
+ */
505
+ export const publish = (channel, event, data, options = {}) => channelsCall("POST", "publish", {
506
+ channel,
507
+ event,
508
+ ...(data !== undefined && { data }),
509
+ ...(options.retain && { retain: true }),
510
+ });
511
+ /** Who is connected to a channel right now, longest first. */
512
+ export const listeners = async (channel) => (await channelsCall("GET", `${encodeURIComponent(channel)}/listeners`)).listeners;
513
+ /** The app's channels that somebody listens to or that keep a message, its open connections, and the limits. */
514
+ export const channels = () => channelsCall("GET", "");
515
+ /** Forget what a channel keeps as its latest. */
516
+ export const forgetKept = (channel) => channelsCall("DELETE", `${encodeURIComponent(channel)}/kept`);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@kabobcompany/lift",
3
- "version": "0.3.0",
4
- "description": "For apps hosted on Lift: call Kabob products (Lookr) as the signed-in person, run public actions, use OpenAI and Seedance on Kabob's account, keep files in Lift's storage, run background jobs, and send email and text messages.",
3
+ "version": "0.4.0",
4
+ "description": "For apps hosted on Lift: call Kabob products (Lookr) as the signed-in person, run public actions, use OpenAI and Seedance on Kabob's account, keep files in Lift's storage, run background jobs, send email and text messages, and publish to long-lived channels.",
5
5
  "license": "UNLICENSED",
6
6
  "type": "module",
7
7
  "main": "./dist/index.js",