@spirobel/mininext 0.3.0 → 0.3.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.
package/dist/html.d.ts CHANGED
@@ -54,5 +54,20 @@ export declare function isError(submissionResult: any | {
54
54
  error: HtmlString;
55
55
  };
56
56
  declare global {
57
- var Reloader: HtmlString | undefined;
57
+ var Reloader: BasedHtml | HtmlString | undefined;
58
58
  }
59
+ /**
60
+ * The difference between this and HtmlString is that it is fully resolved and only accepts primitive types.
61
+ * In plain english this means:
62
+ * It does not accept functions (that will be resolved at request time with (mini)=>mini.html) like mini.html does.
63
+ */
64
+ export declare class BasedHtml extends String {
65
+ }
66
+ export type BasedHtmlValues = number | string | undefined | null | boolean | BasedHtml | BasedHtml[];
67
+ /**
68
+ * The difference between this and HtmlString is that it is fully resolved and only accepts primitive types.
69
+ * @param strings - html literals
70
+ * @param values - values will get escaped to prevent xss
71
+ * @returns
72
+ */
73
+ export declare const basedHtml: (strings: TemplateStringsArray, ...values: BasedHtmlValues[]) => BasedHtml;
package/dist/html.js CHANGED
@@ -245,3 +245,35 @@ export async function htmlResponder(mini, maybeUnresolved, head = default_head,
245
245
  export function isError(submissionResult) {
246
246
  return ("error" in submissionResult && submissionResult.error instanceof HtmlString);
247
247
  }
248
+ /**
249
+ * The difference between this and HtmlString is that it is fully resolved and only accepts primitive types.
250
+ * In plain english this means:
251
+ * It does not accept functions (that will be resolved at request time with (mini)=>mini.html) like mini.html does.
252
+ */
253
+ export class BasedHtml extends String {
254
+ }
255
+ //TODO make it so we can embed BasedHtml into mini.html partially resolved html strings
256
+ /**
257
+ * The difference between this and HtmlString is that it is fully resolved and only accepts primitive types.
258
+ * @param strings - html literals
259
+ * @param values - values will get escaped to prevent xss
260
+ * @returns
261
+ */
262
+ export const basedHtml = (strings, ...values) => {
263
+ // Apply escapeHtml to each value before using them in the template string
264
+ // In case it didn't already get escaped
265
+ for (const [index, value] of values.entries()) {
266
+ // we can pass arrays of BasedHtml and they will get flattened automatically
267
+ if (Array.isArray(value) &&
268
+ value.every((val) => val instanceof BasedHtml)) {
269
+ // If the value is an array of BasedHtml objects, flatten it and add to ...values
270
+ values[index] = value.join("");
271
+ }
272
+ else if (!(value instanceof BasedHtml)) {
273
+ const notEmpty = value || "";
274
+ // values will be escaped by default
275
+ values[index] = Bun.escapeHTML(notEmpty + "");
276
+ }
277
+ }
278
+ return new BasedHtml(String.raw({ raw: strings }, ...values));
279
+ };
@@ -1,15 +1,15 @@
1
1
  /// <reference types="bun-types" />
2
2
  /// <reference types="bun-types" />
3
3
  import { url, Mini, type HtmlHandler } from "./url";
4
- import { isError, HtmlString, head, commonHead, cssReset } from "./html";
4
+ import { isError, HtmlString, BasedHtml, head, commonHead, cssReset, basedHtml as html } from "./html";
5
5
  import type { Server, WebSocketHandler } from "bun";
6
6
  declare global {
7
7
  var PROJECT_ROOT: string | undefined;
8
8
  }
9
9
  declare function build(backendPath?: string): Promise<void>;
10
- declare const standardDevReloader: HtmlString;
10
+ declare const standardDevReloader: BasedHtml;
11
11
  declare function makeEntrypoint(): Promise<{
12
12
  fetch: (req: Request, server: Server) => Promise<Response>;
13
13
  websocket: WebSocketHandler;
14
14
  }>;
15
- export { url, head, build, makeEntrypoint, isError, HtmlString, type HtmlHandler, Mini, standardDevReloader, commonHead, cssReset, };
15
+ export { html, url, head, build, makeEntrypoint, isError, BasedHtml, HtmlString, type HtmlHandler, Mini, standardDevReloader, commonHead, cssReset, };
package/dist/mininext.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { url, Mini } from "./url";
2
- import { html, isError, HtmlString, head, commonHead, cssReset } from "./html";
2
+ import { isError, HtmlString, BasedHtml, head, commonHead, cssReset, basedHtml as html, } from "./html";
3
3
  import { watch } from "fs/promises";
4
4
  import * as path from "path";
5
5
  function projectRoot() {
@@ -158,4 +158,4 @@ async function makeEntrypoint() {
158
158
  }
159
159
  return module.default();
160
160
  }
161
- export { url, head, build, makeEntrypoint, isError, HtmlString, Mini, standardDevReloader, commonHead, cssReset, };
161
+ export { html, url, head, build, makeEntrypoint, isError, BasedHtml, HtmlString, Mini, standardDevReloader, commonHead, cssReset, };
package/dist/url.d.ts CHANGED
@@ -2,7 +2,7 @@
2
2
  /// <reference types="bun-types" />
3
3
  import type { Server, WebSocketHandler } from "bun";
4
4
  import { html, json, dangerjson, HtmlString } from "./html";
5
- import type { DangerJsonInHtml, JsonString, JsonStringValues } from "./html";
5
+ import type { BasedHtml, DangerJsonInHtml, JsonString, JsonStringValues } from "./html";
6
6
  export type Form = {
7
7
  post: boolean;
8
8
  urlencoded: boolean;
@@ -193,6 +193,13 @@ export declare class url {
193
193
  * @param wsObject the websocketsocket object {@link WebSocketHandler}
194
194
  */
195
195
  static setWebsocket<T = undefined>(wsObject: WebSocketHandler<T>): void;
196
+ /**
197
+ * Send a message to all connected {@link ServerWebSocket} subscribed to a topic
198
+ * @param topic The topic to publish to
199
+ * @param message The data to send
200
+ * @returns 0 if the message was dropped, -1 if backpressure was applied, or the number of bytes sent.
201
+ */
202
+ static publishHtml(topic: string, message: BasedHtml): number;
196
203
  /**
197
204
  * Fetch handler that is called by the server when a request is made to any of the urls.
198
205
  * @param {Request} req - The Request object.
package/dist/url.js CHANGED
@@ -395,6 +395,15 @@ export class url {
395
395
  static setWebsocket(wsObject) {
396
396
  url.websocket = wsObject;
397
397
  }
398
+ /**
399
+ * Send a message to all connected {@link ServerWebSocket} subscribed to a topic
400
+ * @param topic The topic to publish to
401
+ * @param message The data to send
402
+ * @returns 0 if the message was dropped, -1 if backpressure was applied, or the number of bytes sent.
403
+ */
404
+ static publishHtml(topic, message) {
405
+ return url.server.publish(topic, message);
406
+ }
398
407
  /**
399
408
  * Fetch handler that is called by the server when a request is made to any of the urls.
400
409
  * @param {Request} req - The Request object.
package/mininext/html.ts CHANGED
@@ -295,5 +295,48 @@ export function isError(
295
295
  }
296
296
 
297
297
  declare global {
298
- var Reloader: HtmlString | undefined;
298
+ var Reloader: BasedHtml | HtmlString | undefined;
299
299
  }
300
+ /**
301
+ * The difference between this and HtmlString is that it is fully resolved and only accepts primitive types.
302
+ * In plain english this means:
303
+ * It does not accept functions (that will be resolved at request time with (mini)=>mini.html) like mini.html does.
304
+ */
305
+ export class BasedHtml extends String {}
306
+ export type BasedHtmlValues =
307
+ | number
308
+ | string
309
+ | undefined
310
+ | null
311
+ | boolean
312
+ | BasedHtml
313
+ | BasedHtml[];
314
+ //TODO make it so we can embed BasedHtml into mini.html partially resolved html strings
315
+ /**
316
+ * The difference between this and HtmlString is that it is fully resolved and only accepts primitive types.
317
+ * @param strings - html literals
318
+ * @param values - values will get escaped to prevent xss
319
+ * @returns
320
+ */
321
+ export const basedHtml = (
322
+ strings: TemplateStringsArray,
323
+ ...values: BasedHtmlValues[]
324
+ ) => {
325
+ // Apply escapeHtml to each value before using them in the template string
326
+ // In case it didn't already get escaped
327
+ for (const [index, value] of values.entries()) {
328
+ // we can pass arrays of BasedHtml and they will get flattened automatically
329
+ if (
330
+ Array.isArray(value) &&
331
+ value.every((val) => val instanceof BasedHtml)
332
+ ) {
333
+ // If the value is an array of BasedHtml objects, flatten it and add to ...values
334
+ values[index] = value.join("");
335
+ } else if (!(value instanceof BasedHtml)) {
336
+ const notEmpty = value || "";
337
+ // values will be escaped by default
338
+ values[index] = Bun.escapeHTML(notEmpty + "");
339
+ }
340
+ }
341
+ return new BasedHtml(String.raw({ raw: strings }, ...values));
342
+ };
@@ -1,5 +1,13 @@
1
1
  import { url, Mini, type HtmlHandler } from "./url";
2
- import { html, isError, HtmlString, head, commonHead, cssReset } from "./html";
2
+ import {
3
+ isError,
4
+ HtmlString,
5
+ BasedHtml,
6
+ head,
7
+ commonHead,
8
+ cssReset,
9
+ basedHtml as html,
10
+ } from "./html";
3
11
  import type { BunPlugin, Server, WebSocketHandler } from "bun";
4
12
  import { watch } from "fs/promises";
5
13
  import * as path from "path";
@@ -174,11 +182,13 @@ async function makeEntrypoint() {
174
182
  };
175
183
  }
176
184
  export {
185
+ html,
177
186
  url,
178
187
  head,
179
188
  build,
180
189
  makeEntrypoint,
181
190
  isError,
191
+ BasedHtml,
182
192
  HtmlString,
183
193
  type HtmlHandler,
184
194
  Mini,
package/mininext/url.ts CHANGED
@@ -1,6 +1,11 @@
1
1
  import type { Server, WebSocketHandler } from "bun";
2
2
  import { htmlResponder, html, json, dangerjson, HtmlString } from "./html";
3
- import type { DangerJsonInHtml, JsonString, JsonStringValues } from "./html";
3
+ import type {
4
+ BasedHtml,
5
+ DangerJsonInHtml,
6
+ JsonString,
7
+ JsonStringValues,
8
+ } from "./html";
4
9
  export type Form = {
5
10
  post: boolean;
6
11
  urlencoded: boolean;
@@ -497,7 +502,15 @@ export class url {
497
502
  static setWebsocket<T = undefined>(wsObject: WebSocketHandler<T>) {
498
503
  url.websocket = wsObject as WebSocketHandler;
499
504
  }
500
-
505
+ /**
506
+ * Send a message to all connected {@link ServerWebSocket} subscribed to a topic
507
+ * @param topic The topic to publish to
508
+ * @param message The data to send
509
+ * @returns 0 if the message was dropped, -1 if backpressure was applied, or the number of bytes sent.
510
+ */
511
+ static publishHtml(topic: string, message: BasedHtml) {
512
+ return url.server.publish(topic, message as string);
513
+ }
501
514
  /**
502
515
  * Fetch handler that is called by the server when a request is made to any of the urls.
503
516
  * @param {Request} req - The Request object.
package/package.json CHANGED
@@ -12,7 +12,7 @@
12
12
 
13
13
  },
14
14
  "files": ["dist", "mininext"],
15
- "version": "0.3.0",
15
+ "version": "0.3.2",
16
16
  "devDependencies": {
17
17
  "@types/bun": "latest"
18
18
  },