brumate 0.0.1 → 0.1.1

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.
Files changed (81) hide show
  1. package/README.md +43 -72
  2. package/dist/boot-DN1ZU8Hg.d.ts +37 -0
  3. package/dist/cli.js +2 -2
  4. package/dist/{client-DuDTDePF.d.ts → client-DwJInP_a.d.ts} +133 -44
  5. package/dist/client-routes-DakuM32C.js +94 -0
  6. package/dist/client-routes-DakuM32C.js.map +1 -0
  7. package/dist/{cookies-rgIAc8IE.js → cookies-Av5WFmcf.js} +18 -8
  8. package/dist/cookies-Av5WFmcf.js.map +1 -0
  9. package/dist/{devtools-Bm8cS7hX.js → devtools-BKjnHrnv.js} +10 -5
  10. package/dist/devtools-BKjnHrnv.js.map +1 -0
  11. package/dist/{devtools-panel-CTbDSBBA.js → devtools-panel-AWOn0VK2.js} +23 -26
  12. package/dist/devtools-panel-AWOn0VK2.js.map +1 -0
  13. package/dist/{files-D_JUcSU5.js → files-DdT84mPy.js} +22 -3
  14. package/dist/{files-D_JUcSU5.js.map → files-DdT84mPy.js.map} +1 -1
  15. package/dist/{html-CxWpXngy.js → html-DEeoepg3.js} +47 -10
  16. package/dist/html-DEeoepg3.js.map +1 -0
  17. package/dist/{html-DV-Tezgq.d.ts → html-Du8jY2Cc.d.ts} +14 -2
  18. package/dist/index.d.ts +65 -6
  19. package/dist/index.js +20 -4
  20. package/dist/index.js.map +1 -1
  21. package/dist/{inspect-CGCWfaEx.js → inspect-BPchdWbU.js} +887 -172
  22. package/dist/inspect-BPchdWbU.js.map +1 -0
  23. package/dist/loader-Dc9OwY5I.js +101 -0
  24. package/dist/loader-Dc9OwY5I.js.map +1 -0
  25. package/dist/{navigate-C5dP89YL.js → navigate-CzLjnoPH.js} +112 -59
  26. package/dist/navigate-CzLjnoPH.js.map +1 -0
  27. package/dist/{produce-BjQGMpqL.d.ts → produce-CtZ32UGG.d.ts} +13 -3
  28. package/dist/{rpc-B86KKjzx.js → rpc-9fSqL_YK.js} +57 -12
  29. package/dist/rpc-9fSqL_YK.js.map +1 -0
  30. package/dist/runtime/client.d.ts +4 -4
  31. package/dist/runtime/client.js +599 -306
  32. package/dist/runtime/client.js.map +1 -1
  33. package/dist/runtime/devtools.d.ts +3 -1
  34. package/dist/runtime/devtools.js +1 -1
  35. package/dist/runtime/hot.d.ts +1 -1
  36. package/dist/runtime/hot.js +9 -5
  37. package/dist/runtime/hot.js.map +1 -1
  38. package/dist/runtime/html.d.ts +2 -2
  39. package/dist/runtime/html.js +1 -1
  40. package/dist/runtime/loader.d.ts +12 -2
  41. package/dist/runtime/loader.js +3 -1
  42. package/dist/runtime/server.d.ts +4 -4
  43. package/dist/runtime/server.js +5 -5
  44. package/dist/runtime/signals.d.ts +2 -2
  45. package/dist/runtime/signals.js +5 -5
  46. package/dist/runtime/signals.js.map +1 -1
  47. package/dist/runtime/static-loader.d.ts +3 -0
  48. package/dist/runtime/static-loader.js +1 -0
  49. package/dist/runtime/testing.d.ts +21 -2
  50. package/dist/runtime/testing.js +83 -7
  51. package/dist/runtime/testing.js.map +1 -1
  52. package/dist/{server-sQCvduOz.d.ts → server-BxqjLUnV.d.ts} +211 -20
  53. package/dist/{server-CX3JnPCs.js → server-DZsgnPKS.js} +595 -185
  54. package/dist/server-DZsgnPKS.js.map +1 -0
  55. package/dist/shared-CWYOd0H3.js +40 -0
  56. package/dist/shared-CWYOd0H3.js.map +1 -0
  57. package/dist/static-loader-BcFh4ebX.d.ts +11 -0
  58. package/dist/static-loader-ClNkHZGn.js +185 -0
  59. package/dist/static-loader-ClNkHZGn.js.map +1 -0
  60. package/dist/stream-bgNCMcaA.js +280 -0
  61. package/dist/stream-bgNCMcaA.js.map +1 -0
  62. package/dist/vite.d.ts +30 -5
  63. package/dist/vite.js +501 -62
  64. package/dist/vite.js.map +1 -1
  65. package/package.json +7 -2
  66. package/types.d.ts +5 -0
  67. package/dist/cookies-rgIAc8IE.js.map +0 -1
  68. package/dist/devtools-Bm8cS7hX.js.map +0 -1
  69. package/dist/devtools-panel-CTbDSBBA.js.map +0 -1
  70. package/dist/html-CxWpXngy.js.map +0 -1
  71. package/dist/inspect-CGCWfaEx.js.map +0 -1
  72. package/dist/loader-BUhNqDHw.d.ts +0 -30
  73. package/dist/loader-py4qu6tQ.js +0 -215
  74. package/dist/loader-py4qu6tQ.js.map +0 -1
  75. package/dist/navigate-C5dP89YL.js.map +0 -1
  76. package/dist/rpc-B86KKjzx.js.map +0 -1
  77. package/dist/server-CX3JnPCs.js.map +0 -1
  78. package/dist/shared-BE_oBSDM.js +0 -176
  79. package/dist/shared-BE_oBSDM.js.map +0 -1
  80. package/dist/stream-cvhqOzfs.js +0 -145
  81. package/dist/stream-cvhqOzfs.js.map +0 -1
@@ -1,176 +0,0 @@
1
- import { d as rawHTML, l as isRaw, u as raw } from "./html-CxWpXngy.js";
2
- import { Feature, createPlugin } from "seroval";
3
- //#region src/runtime/shared.ts
4
- /** An element that the server rendered. The client finds it by its anchor. */
5
- var ElementReference = class {
6
- scope;
7
- anchor;
8
- constructor(scope, anchor) {
9
- this.scope = scope;
10
- this.anchor = anchor;
11
- }
12
- };
13
- /** A client function: an export of a client module, and the scope it reads. */
14
- var FunctionReference = class {
15
- /** `<component id>#<export name>`. */
16
- ref;
17
- scope;
18
- constructor(ref, scope) {
19
- this.ref = ref;
20
- this.scope = scope;
21
- }
22
- };
23
- /** Server functions that are passed to the client, with their references. */
24
- const functionReferences = /* @__PURE__ */ new WeakMap();
25
- const ElementPlugin = createPlugin({
26
- tag: "brumate/element",
27
- test: (value) => value instanceof ElementReference,
28
- parse: {
29
- sync: (value, ctx) => ({
30
- a: ctx.parse(value.scope),
31
- b: ctx.parse(value.anchor)
32
- }),
33
- async: async (value, ctx) => ({
34
- a: await ctx.parse(value.scope),
35
- b: await ctx.parse(value.anchor)
36
- }),
37
- stream: (value, ctx) => ({
38
- a: ctx.parse(value.scope),
39
- b: ctx.parse(value.anchor)
40
- })
41
- },
42
- serialize: (node, ctx) => `{s:${ctx.serialize(node.a)},a:${ctx.serialize(node.b)}}`,
43
- deserialize: (node, ctx) => new ElementReference(ctx.deserialize(node.a), ctx.deserialize(node.b))
44
- });
45
- const FunctionPlugin = createPlugin({
46
- tag: "brumate/function",
47
- test: (value) => typeof value === "function" && functionReferences.has(value),
48
- parse: {
49
- sync: (value, ctx) => {
50
- const reference = functionReferences.get(value);
51
- return {
52
- a: ctx.parse(reference.ref),
53
- b: ctx.parse(reference.scope)
54
- };
55
- },
56
- async: async (value, ctx) => {
57
- const reference = functionReferences.get(value);
58
- return {
59
- a: await ctx.parse(reference.ref),
60
- b: await ctx.parse(reference.scope)
61
- };
62
- },
63
- stream: (value, ctx) => {
64
- const reference = functionReferences.get(value);
65
- return {
66
- a: ctx.parse(reference.ref),
67
- b: ctx.parse(reference.scope)
68
- };
69
- }
70
- },
71
- serialize: (node, ctx) => `{r:${ctx.serialize(node.a)},s:${ctx.serialize(node.b)}}`,
72
- deserialize: (node, ctx) => new FunctionReference(ctx.deserialize(node.a), ctx.deserialize(node.b))
73
- });
74
- /** HTML, such as the `children` of a component, so the client can insert it again. */
75
- const RawPlugin = createPlugin({
76
- tag: "brumate/raw",
77
- test: isRaw,
78
- parse: {
79
- sync: (value, ctx) => ({ h: ctx.parse(rawHTML(value)) }),
80
- async: async (value, ctx) => ({ h: await ctx.parse(rawHTML(value)) }),
81
- stream: (value, ctx) => ({ h: ctx.parse(rawHTML(value)) })
82
- },
83
- serialize: (node, ctx) => ctx.serialize(node.h),
84
- deserialize: (node, ctx) => raw(ctx.deserialize(node.h))
85
- });
86
- /**
87
- * A component in the page state, such as the value of a dynamic tag. The
88
- * client gets a function with the same ID that renders nothing. The client
89
- * code that changes the value uses the component's render module instead.
90
- */
91
- const ComponentPlugin = createPlugin({
92
- tag: "brumate/component",
93
- test: (value) => typeof value === "function" && typeof value.brumateId === "string",
94
- parse: {
95
- sync: (value, ctx) => ({ c: ctx.parse(value.brumateId) }),
96
- async: async (value, ctx) => ({ c: await ctx.parse(value.brumateId) }),
97
- stream: (value, ctx) => ({ c: ctx.parse(value.brumateId) })
98
- },
99
- serialize: (node, ctx) => ctx.serialize(node.c),
100
- deserialize: (node, ctx) => Object.assign(() => "", { brumateId: ctx.deserialize(node.c) })
101
- });
102
- /** A value that its string makes again, such as a `URL` from its `href`. */
103
- function stringPlugin(tag, type) {
104
- return createPlugin({
105
- tag,
106
- test: (value) => value instanceof type,
107
- parse: {
108
- sync: (value, ctx) => ({ v: ctx.parse(value.toString()) }),
109
- async: async (value, ctx) => ({ v: await ctx.parse(value.toString()) }),
110
- stream: (value, ctx) => ({ v: ctx.parse(value.toString()) })
111
- },
112
- serialize: (node, ctx) => `new ${type.name}(${ctx.serialize(node.v)})`,
113
- deserialize: (node, ctx) => new type(ctx.deserialize(node.v))
114
- });
115
- }
116
- const URLPlugin = stringPlugin("brumate/url", URL);
117
- const SearchParamsPlugin = stringPlugin("brumate/search-params", URLSearchParams);
118
- /**
119
- * A file or a blob in the arguments of a server function call. The call
120
- * sends it as a part of multipart form data, named `file<index>`, instead
121
- * of in the JSON of the arguments.
122
- */
123
- var Upload = class {
124
- index;
125
- constructor(index) {
126
- this.index = index;
127
- }
128
- };
129
- const UploadPlugin = createPlugin({
130
- tag: "brumate/upload",
131
- test: (value) => value instanceof Upload,
132
- parse: {
133
- sync: (value, ctx) => ({ i: ctx.parse(value.index) }),
134
- async: async (value, ctx) => ({ i: await ctx.parse(value.index) }),
135
- stream: (value, ctx) => ({ i: ctx.parse(value.index) })
136
- },
137
- serialize: (node, ctx) => `{u:${ctx.serialize(node.i)}}`,
138
- deserialize: (node, ctx) => new Upload(ctx.deserialize(node.i))
139
- });
140
- /**
141
- * Replaces each value in arrays and plain objects that `replace` returns a
142
- * value for. Returns a copy, or the value itself when nothing changed.
143
- */
144
- function replaceDeep(value, replace) {
145
- const replaced = replace(value);
146
- if (replaced !== void 0) return replaced;
147
- if (Array.isArray(value)) return value.map((item) => replaceDeep(item, replace));
148
- if (value !== null && typeof value === "object" && Object.getPrototypeOf(value) === Object.prototype) return Object.fromEntries(Object.entries(value).map(([key, item]) => [key, replaceDeep(item, replace)]));
149
- return value;
150
- }
151
- /** True for a value that `for await` reads, such as the result of an async generator. */
152
- function isAsyncIterable(value) {
153
- return value !== null && typeof value === "object" && typeof value[Symbol.asyncIterator] === "function";
154
- }
155
- const plugins = [
156
- ElementPlugin,
157
- FunctionPlugin,
158
- RawPlugin,
159
- ComponentPlugin,
160
- UploadPlugin,
161
- URLPlugin,
162
- SearchParamsPlugin
163
- ];
164
- /**
165
- * Features that are off in both directions:
166
- *
167
- * - Errors keep their name and message only. A stack trace would show the
168
- * server's files and code to the browser.
169
- * - Regular expressions are not sent. A pattern from a client could take
170
- * the server a long time to match, and reading one is refused as well.
171
- */
172
- const disabledFeatures = Feature.ErrorPrototypeStack | Feature.RegExp;
173
- //#endregion
174
- export { functionReferences as a, replaceDeep as c, disabledFeatures as i, FunctionReference as n, isAsyncIterable as o, Upload as r, plugins as s, ElementReference as t };
175
-
176
- //# sourceMappingURL=shared-BE_oBSDM.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"shared-BE_oBSDM.js","names":[],"sources":["../src/runtime/shared.ts"],"sourcesContent":["import { Feature, createPlugin } from 'seroval';\nimport type { Plugin, SerovalNode } from 'seroval';\nimport { isRaw, raw, rawHTML } from './html.ts';\nimport type { Raw } from './html.ts';\n\n/** An element that the server rendered. The client finds it by its anchor. */\nexport class ElementReference {\n readonly scope: number;\n readonly anchor: number;\n\n constructor(scope: number, anchor: number) {\n this.scope = scope;\n this.anchor = anchor;\n }\n}\n\n/** A client function: an export of a client module, and the scope it reads. */\nexport class FunctionReference {\n /** `<component id>#<export name>`. */\n readonly ref: string;\n readonly scope: number;\n\n constructor(ref: string, scope: number) {\n this.ref = ref;\n this.scope = scope;\n }\n}\n\n/** Server functions that are passed to the client, with their references. */\nexport const functionReferences: WeakMap<object, FunctionReference> = new WeakMap();\n\ntype ReferenceNode = { a: SerovalNode; b: SerovalNode };\n\nconst ElementPlugin = createPlugin<ElementReference, ReferenceNode>({\n tag: 'brumate/element',\n test: (value) => value instanceof ElementReference,\n parse: {\n sync: (value, ctx) => ({ a: ctx.parse(value.scope), b: ctx.parse(value.anchor) }),\n async: async (value, ctx) => ({\n a: await ctx.parse(value.scope),\n b: await ctx.parse(value.anchor),\n }),\n stream: (value, ctx) => ({ a: ctx.parse(value.scope), b: ctx.parse(value.anchor) }),\n },\n serialize: (node, ctx) => `{s:${ctx.serialize(node.a)},a:${ctx.serialize(node.b)}}`,\n deserialize: (node, ctx) =>\n new ElementReference(ctx.deserialize<number>(node.a), ctx.deserialize<number>(node.b)),\n});\n\nconst FunctionPlugin = createPlugin<object, ReferenceNode>({\n tag: 'brumate/function',\n test: (value) => typeof value === 'function' && functionReferences.has(value),\n parse: {\n sync: (value, ctx) => {\n const reference = functionReferences.get(value)!;\n return { a: ctx.parse(reference.ref), b: ctx.parse(reference.scope) };\n },\n async: async (value, ctx) => {\n const reference = functionReferences.get(value)!;\n return { a: await ctx.parse(reference.ref), b: await ctx.parse(reference.scope) };\n },\n stream: (value, ctx) => {\n const reference = functionReferences.get(value)!;\n return { a: ctx.parse(reference.ref), b: ctx.parse(reference.scope) };\n },\n },\n serialize: (node, ctx) => `{r:${ctx.serialize(node.a)},s:${ctx.serialize(node.b)}}`,\n deserialize: (node, ctx) =>\n new FunctionReference(ctx.deserialize<string>(node.a), ctx.deserialize<number>(node.b)),\n});\n\n/** HTML, such as the `children` of a component, so the client can insert it again. */\nconst RawPlugin = createPlugin<Raw, { h: SerovalNode }>({\n tag: 'brumate/raw',\n test: isRaw,\n parse: {\n sync: (value, ctx) => ({ h: ctx.parse(rawHTML(value)) }),\n async: async (value, ctx) => ({ h: await ctx.parse(rawHTML(value)) }),\n stream: (value, ctx) => ({ h: ctx.parse(rawHTML(value)) }),\n },\n serialize: (node, ctx) => ctx.serialize(node.h),\n deserialize: (node, ctx) => raw(ctx.deserialize<string>(node.h)),\n});\n\n/** A component, which the compiler marks with its ID. */\ntype ComponentValue = ((...args: never[]) => unknown) & { brumateId: string };\n\n/**\n * A component in the page state, such as the value of a dynamic tag. The\n * client gets a function with the same ID that renders nothing. The client\n * code that changes the value uses the component's render module instead.\n */\nconst ComponentPlugin = createPlugin<ComponentValue, { c: SerovalNode }>({\n tag: 'brumate/component',\n test: (value) =>\n typeof value === 'function' && typeof (value as Partial<ComponentValue>).brumateId === 'string',\n parse: {\n sync: (value, ctx) => ({ c: ctx.parse(value.brumateId) }),\n async: async (value, ctx) => ({ c: await ctx.parse(value.brumateId) }),\n stream: (value, ctx) => ({ c: ctx.parse(value.brumateId) }),\n },\n serialize: (node, ctx) => ctx.serialize(node.c),\n deserialize: (node, ctx) =>\n Object.assign(() => '', { brumateId: ctx.deserialize<string>(node.c) }),\n});\n\n/** A value that its string makes again, such as a `URL` from its `href`. */\nfunction stringPlugin<T extends { toString(): string }>(\n tag: string,\n type: new (text: string) => T,\n): Plugin<T, { v: SerovalNode }> {\n return createPlugin<T, { v: SerovalNode }>({\n tag,\n test: (value) => value instanceof type,\n parse: {\n sync: (value, ctx) => ({ v: ctx.parse(value.toString()) }),\n async: async (value, ctx) => ({ v: await ctx.parse(value.toString()) }),\n stream: (value, ctx) => ({ v: ctx.parse(value.toString()) }),\n },\n serialize: (node, ctx) => `new ${type.name}(${ctx.serialize(node.v)})`,\n deserialize: (node, ctx) => new type(ctx.deserialize<string>(node.v)),\n });\n}\n\nconst URLPlugin = stringPlugin('brumate/url', URL);\nconst SearchParamsPlugin = stringPlugin('brumate/search-params', URLSearchParams);\n\n/**\n * A file or a blob in the arguments of a server function call. The call\n * sends it as a part of multipart form data, named `file<index>`, instead\n * of in the JSON of the arguments.\n */\nexport class Upload {\n readonly index: number;\n\n constructor(index: number) {\n this.index = index;\n }\n}\n\nconst UploadPlugin = createPlugin<Upload, { i: SerovalNode }>({\n tag: 'brumate/upload',\n test: (value) => value instanceof Upload,\n parse: {\n sync: (value, ctx) => ({ i: ctx.parse(value.index) }),\n async: async (value, ctx) => ({ i: await ctx.parse(value.index) }),\n stream: (value, ctx) => ({ i: ctx.parse(value.index) }),\n },\n serialize: (node, ctx) => `{u:${ctx.serialize(node.i)}}`,\n deserialize: (node, ctx) => new Upload(ctx.deserialize<number>(node.i)),\n});\n\n/**\n * Replaces each value in arrays and plain objects that `replace` returns a\n * value for. Returns a copy, or the value itself when nothing changed.\n */\nexport function replaceDeep(value: unknown, replace: (value: unknown) => unknown): unknown {\n const replaced = replace(value);\n if (replaced !== undefined) {\n return replaced;\n }\n if (Array.isArray(value)) {\n return value.map((item) => replaceDeep(item, replace));\n }\n if (\n value !== null &&\n typeof value === 'object' &&\n Object.getPrototypeOf(value) === Object.prototype\n ) {\n return Object.fromEntries(\n Object.entries(value).map(([key, item]) => [key, replaceDeep(item, replace)]),\n );\n }\n return value;\n}\n\n/** True for a value that `for await` reads, such as the result of an async generator. */\nexport function isAsyncIterable(value: unknown): value is AsyncIterable<unknown> {\n return (\n value !== null &&\n typeof value === 'object' &&\n typeof (value as AsyncIterable<unknown>)[Symbol.asyncIterator] === 'function'\n );\n}\n\n// oxlint-disable-next-line typescript/no-explicit-any -- Plugin is invariant, so any is needed to hold every plugin.\nexport const plugins: Plugin<any, any>[] = [\n ElementPlugin,\n FunctionPlugin,\n RawPlugin,\n ComponentPlugin,\n UploadPlugin,\n URLPlugin,\n SearchParamsPlugin,\n];\n\n/**\n * Features that are off in both directions:\n *\n * - Errors keep their name and message only. A stack trace would show the\n * server's files and code to the browser.\n * - Regular expressions are not sent. A pattern from a client could take\n * the server a long time to match, and reading one is refused as well.\n */\nexport const disabledFeatures: number = Feature.ErrorPrototypeStack | Feature.RegExp;\n\n/** The state of one scope in the page. */\nexport interface ScopeState {\n /** The scope's ID in the compiled component. */\n k?: number;\n /** The ID of the scope that contains this one. Component root scopes have none. */\n p?: number;\n /** The component ID of a component's root scope. */\n m?: string;\n /** The item of a list item scope. */\n i?: unknown;\n /** The scope that rendered a component's root scope, for finding contexts. */\n o?: number;\n /** The values that the scope provides, by context key. */\n v?: Record<string, unknown>;\n /** The slots that hold the contexts that the scope reads, by context key. */\n x?: Record<string, number>;\n /** Slot values, in the order of the scope's layout. */\n s?: unknown[];\n /** The scope IDs of child components, by anchor index. */\n c?: Record<number, number>;\n}\n\n/** The state of a page, by scope ID. */\nexport type PageState = Record<number, ScopeState>;\n"],"mappings":";;;;AAMA,IAAa,mBAAb,MAA8B;CAC5B;CACA;CAEA,YAAY,OAAe,QAAgB;EACzC,KAAK,QAAQ;EACb,KAAK,SAAS;CAChB;AACF;;AAGA,IAAa,oBAAb,MAA+B;;CAE7B;CACA;CAEA,YAAY,KAAa,OAAe;EACtC,KAAK,MAAM;EACX,KAAK,QAAQ;CACf;AACF;;AAGA,MAAa,qCAAyD,IAAI,QAAQ;AAIlF,MAAM,gBAAgB,aAA8C;CAClE,KAAK;CACL,OAAO,UAAU,iBAAiB;CAClC,OAAO;EACL,OAAO,OAAO,SAAS;GAAE,GAAG,IAAI,MAAM,MAAM,KAAK;GAAG,GAAG,IAAI,MAAM,MAAM,MAAM;EAAE;EAC/E,OAAO,OAAO,OAAO,SAAS;GAC5B,GAAG,MAAM,IAAI,MAAM,MAAM,KAAK;GAC9B,GAAG,MAAM,IAAI,MAAM,MAAM,MAAM;EACjC;EACA,SAAS,OAAO,SAAS;GAAE,GAAG,IAAI,MAAM,MAAM,KAAK;GAAG,GAAG,IAAI,MAAM,MAAM,MAAM;EAAE;CACnF;CACA,YAAY,MAAM,QAAQ,MAAM,IAAI,UAAU,KAAK,CAAC,EAAE,KAAK,IAAI,UAAU,KAAK,CAAC,EAAE;CACjF,cAAc,MAAM,QAClB,IAAI,iBAAiB,IAAI,YAAoB,KAAK,CAAC,GAAG,IAAI,YAAoB,KAAK,CAAC,CAAC;AACzF,CAAC;AAED,MAAM,iBAAiB,aAAoC;CACzD,KAAK;CACL,OAAO,UAAU,OAAO,UAAU,cAAc,mBAAmB,IAAI,KAAK;CAC5E,O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AM,IAAI,MAAM,MAAM,KAAK,EAAE;EAChE,SAAS,OAAO,SAAS,EAAE,GAAG,IAAI,MAAM,MAAM,KAAK,EAAE;CACvD;CACA,YAAY,MAAM,QAAQ,MAAM,IAAI,UAAU,KAAK,CAAC,EAAE;CACtD,cAAc,MAAM,QAAQ,IAAI,OAAO,IAAI,YAAoB,KAAK,CAAC,CAAC;AACxE,CAAC;;;;;AAMD,SAAgB,YAAY,OAAgB,SAA+C;CACzF,MAAM,WAAW,QAAQ,KAAK;CAC9B,IAAI,aAAa,KAAA,GACf,OAAO;CAET,IAAI,MAAM,QAAQ,KAAK,GACrB,OAAO,MAAM,KAAK,SAAS,YAAY,MAAM,OAAO,CAAC;CAEvD,IACE,UAAU,QACV,OAAO,UAAU,YACjB,OAAO,eAAe,KAAK,MAAM,OAAO,WAExC,OAAO,OAAO,YACZ,OAAO,QAAQ,KAAK,CAAC,CAAC,KAAK,CAAC,KAAK,UAAU,CAAC,KAAK,YAAY,MAAM,OAAO,CAAC,CAAC,CAC9E;CAEF,OAAO;AACT;;AAGA,SAAgB,gBAAgB,OAAiD;CAC/E,OACE,UAAU,QACV,OAAO,UAAU,YACjB,OAAQ,MAAiC,OAAO,mBAAmB;AAEvE;AAGA,MAAa,UAA8B;CACzC;CACA;CACA;CACA;CACA;CACA;CACA;AACF;;;;;;;;;AAUA,MAAa,mBAA2B,QAAQ,sBAAsB,QAAQ"}
@@ -1,145 +0,0 @@
1
- import { i as disabledFeatures, s as plugins } from "./shared-BE_oBSDM.js";
2
- import { fromCrossJSON, isStream, toCrossJSONStream } from "seroval";
3
- //#region src/runtime/stream.ts
4
- const HEADER_SIZE = 12;
5
- const encoder = new TextEncoder();
6
- const decoder = new TextDecoder();
7
- /** The content type of a stream of chunks. */
8
- const STREAM_TYPE = "application/x-brumate-stream";
9
- function header(size) {
10
- return encoder.encode(`;0x${size.toString(16).padStart(8, "0")};`);
11
- }
12
- /** Serializes a value as a stream of chunks. */
13
- function toChunkStream(value) {
14
- return new ReadableStream({ start(controller) {
15
- toCrossJSONStream(value, {
16
- refs: /* @__PURE__ */ new Map(),
17
- plugins,
18
- disabledFeatures,
19
- onParse(node) {
20
- const data = encoder.encode(JSON.stringify(node));
21
- controller.enqueue(header(data.length));
22
- controller.enqueue(data);
23
- },
24
- onDone() {
25
- controller.close();
26
- },
27
- onError(error) {
28
- controller.error(error);
29
- }
30
- });
31
- } });
32
- }
33
- /** Reads chunks of a stream, buffering only the bytes of the chunk it waits for. */
34
- var ChunkReader = class {
35
- reader;
36
- pieces = [];
37
- length = 0;
38
- done = false;
39
- constructor(stream) {
40
- this.reader = stream.getReader();
41
- }
42
- async fill(size) {
43
- while (this.length < size && !this.done) {
44
- const piece = await this.reader.read();
45
- if (piece.done) this.done = true;
46
- else {
47
- this.pieces.push(piece.value);
48
- this.length += piece.value.length;
49
- }
50
- }
51
- }
52
- take(size) {
53
- const result = new Uint8Array(size);
54
- let offset = 0;
55
- while (offset < size) {
56
- const piece = this.pieces[0];
57
- const count = Math.min(piece.length, size - offset);
58
- result.set(piece.subarray(0, count), offset);
59
- offset += count;
60
- if (count === piece.length) this.pieces.shift();
61
- else this.pieces[0] = piece.subarray(count);
62
- }
63
- this.length -= size;
64
- return result;
65
- }
66
- /** The data of the next chunk, or undefined at the end of the stream. */
67
- async next() {
68
- await this.fill(HEADER_SIZE);
69
- if (this.length === 0) return;
70
- const head = this.length < HEADER_SIZE ? "" : decoder.decode(this.take(HEADER_SIZE));
71
- const match = /^;0x([0-9a-f]{8});$/.exec(head);
72
- if (!match) throw new Error("The answer of a server function is not a stream of chunks");
73
- const size = Number.parseInt(match[1], 16);
74
- await this.fill(size);
75
- if (this.length < size) throw new Error("The answer of a server function ended in the middle of a chunk");
76
- return decoder.decode(this.take(size));
77
- }
78
- cancel(reason) {
79
- this.done = true;
80
- this.reader.cancel(reason).catch(() => {});
81
- }
82
- };
83
- const ignore = () => {};
84
- /**
85
- * The refs of a stream. A promise in the answer can fail with nothing that
86
- * reads it, such as a field the caller ignores, so its failure is claimed
87
- * here. Code that awaits it still sees the failure.
88
- */
89
- var Refs = class extends Map {
90
- set(id, value) {
91
- if (value instanceof Promise) value.catch(ignore);
92
- return super.set(id, value);
93
- }
94
- };
95
- /**
96
- * Fails the promises and async iterables that still wait for a chunk, when
97
- * the stream ends early. Failing a settled one does nothing.
98
- */
99
- function failPending(refs, error) {
100
- for (const value of refs.values()) try {
101
- if (isStream(value)) value.throw(error);
102
- else if (value !== null && typeof value === "object") {
103
- const resolver = value;
104
- if (resolver.p instanceof Promise && typeof resolver.f === "function") {
105
- resolver.p.catch(ignore);
106
- resolver.f(error);
107
- }
108
- }
109
- } catch {}
110
- }
111
- /**
112
- * Reads the first chunk of a stream as the value, and the rest in the
113
- * background, so the promises and async iterables in it settle as their
114
- * chunks arrive.
115
- */
116
- async function fromChunkStream(stream) {
117
- const reader = new ChunkReader(stream);
118
- const refs = new Refs();
119
- const read = (data) => fromCrossJSON(JSON.parse(data), {
120
- refs,
121
- plugins,
122
- disabledFeatures
123
- });
124
- const first = await reader.next();
125
- if (first === void 0) throw new Error("The answer of a server function is empty");
126
- const value = read(first);
127
- (async () => {
128
- try {
129
- for (;;) {
130
- const data = await reader.next();
131
- if (data === void 0) break;
132
- read(data);
133
- }
134
- failPending(refs, /* @__PURE__ */ new Error("The answer of a server function ended early"));
135
- } catch (error) {
136
- reader.cancel(error);
137
- failPending(refs, error);
138
- }
139
- })();
140
- return value;
141
- }
142
- //#endregion
143
- export { fromChunkStream as n, toChunkStream as r, STREAM_TYPE as t };
144
-
145
- //# sourceMappingURL=stream-cvhqOzfs.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"stream-cvhqOzfs.js","names":[],"sources":["../src/runtime/stream.ts"],"sourcesContent":["// The answer of a server function, as a stream of seroval chunks. Promises\n// and async iterables in the answer settle as their chunks arrive.\n//\n// Each chunk is a 12-byte header with the size of its data in bytes, as 8\n// hex digits, and the data: `;0x0000001f;{...}`. The reader knows how many\n// bytes to wait for, and can refuse a chunk that is too large.\n\nimport { fromCrossJSON, isStream, toCrossJSONStream } from 'seroval';\nimport type { SerovalNode } from 'seroval';\nimport { disabledFeatures, plugins } from './shared.ts';\n\nconst HEADER_SIZE = 12;\nconst encoder = new TextEncoder();\nconst decoder = new TextDecoder();\n\n/** The content type of a stream of chunks. */\nexport const STREAM_TYPE = 'application/x-brumate-stream';\n\nfunction header(size: number): Uint8Array {\n return encoder.encode(`;0x${size.toString(16).padStart(8, '0')};`);\n}\n\n/** Serializes a value as a stream of chunks. */\nexport function toChunkStream(value: unknown): ReadableStream<Uint8Array> {\n return new ReadableStream({\n start(controller) {\n toCrossJSONStream(value, {\n refs: new Map(),\n plugins,\n disabledFeatures,\n onParse(node) {\n const data = encoder.encode(JSON.stringify(node));\n controller.enqueue(header(data.length));\n controller.enqueue(data);\n },\n onDone() {\n controller.close();\n },\n onError(error) {\n controller.error(error);\n },\n });\n },\n });\n}\n\n/** Reads chunks of a stream, buffering only the bytes of the chunk it waits for. */\nclass ChunkReader {\n private readonly reader: ReadableStreamDefaultReader<Uint8Array>;\n private pieces: Uint8Array[] = [];\n private length = 0;\n private done = false;\n\n constructor(stream: ReadableStream<Uint8Array>) {\n this.reader = stream.getReader();\n }\n\n private async fill(size: number): Promise<void> {\n while (this.length < size && !this.done) {\n // oxlint-disable-next-line no-await-in-loop -- Pieces arrive in order.\n const piece = await this.reader.read();\n if (piece.done) {\n this.done = true;\n } else {\n this.pieces.push(piece.value);\n this.length += piece.value.length;\n }\n }\n }\n\n private take(size: number): Uint8Array {\n const result = new Uint8Array(size);\n let offset = 0;\n while (offset < size) {\n const piece = this.pieces[0]!;\n const count = Math.min(piece.length, size - offset);\n result.set(piece.subarray(0, count), offset);\n offset += count;\n if (count === piece.length) {\n this.pieces.shift();\n } else {\n this.pieces[0] = piece.subarray(count);\n }\n }\n this.length -= size;\n return result;\n }\n\n /** The data of the next chunk, or undefined at the end of the stream. */\n async next(): Promise<string | undefined> {\n await this.fill(HEADER_SIZE);\n if (this.length === 0) {\n return undefined;\n }\n const head = this.length < HEADER_SIZE ? '' : decoder.decode(this.take(HEADER_SIZE));\n const match = /^;0x([0-9a-f]{8});$/.exec(head);\n if (!match) {\n throw new Error('The answer of a server function is not a stream of chunks');\n }\n const size = Number.parseInt(match[1]!, 16);\n await this.fill(size);\n if (this.length < size) {\n throw new Error('The answer of a server function ended in the middle of a chunk');\n }\n return decoder.decode(this.take(size));\n }\n\n cancel(reason: unknown): void {\n this.done = true;\n void this.reader.cancel(reason).catch(() => {});\n }\n}\n\nconst ignore = (): void => {};\n\n/**\n * The refs of a stream. A promise in the answer can fail with nothing that\n * reads it, such as a field the caller ignores, so its failure is claimed\n * here. Code that awaits it still sees the failure.\n */\nclass Refs extends Map<number, unknown> {\n override set(id: number, value: unknown): this {\n if (value instanceof Promise) {\n value.catch(ignore);\n }\n return super.set(id, value);\n }\n}\n\n/**\n * Fails the promises and async iterables that still wait for a chunk, when\n * the stream ends early. Failing a settled one does nothing.\n */\nfunction failPending(refs: Refs, error: unknown): void {\n for (const value of refs.values()) {\n try {\n if (isStream(value)) {\n value.throw(error);\n } else if (value !== null && typeof value === 'object') {\n const resolver = value as { p?: unknown; f?: unknown };\n if (resolver.p instanceof Promise && typeof resolver.f === 'function') {\n resolver.p.catch(ignore);\n (resolver.f as (error: unknown) => void)(error);\n }\n }\n } catch {\n // A value that a plugin made, which has no such methods.\n }\n }\n}\n\n/**\n * Reads the first chunk of a stream as the value, and the rest in the\n * background, so the promises and async iterables in it settle as their\n * chunks arrive.\n */\nexport async function fromChunkStream(stream: ReadableStream<Uint8Array>): Promise<unknown> {\n const reader = new ChunkReader(stream);\n const refs = new Refs();\n const read = (data: string): unknown =>\n fromCrossJSON(JSON.parse(data) as SerovalNode, { refs, plugins, disabledFeatures });\n const first = await reader.next();\n if (first === undefined) {\n throw new Error('The answer of a server function is empty');\n }\n const value = read(first);\n void (async () => {\n try {\n for (;;) {\n // oxlint-disable-next-line no-await-in-loop -- Chunks arrive in order.\n const data = await reader.next();\n if (data === undefined) {\n break;\n }\n read(data);\n }\n failPending(refs, new Error('The answer of a server function ended early'));\n } catch (error) {\n reader.cancel(error);\n failPending(refs, error);\n }\n })();\n return value;\n}\n"],"mappings":";;;AAWA,MAAM,cAAc;AACpB,MAAM,UAAU,IAAI,YAAY;AAChC,MAAM,UAAU,IAAI,YAAY;;AAGhC,MAAa,cAAc;AAE3B,SAAS,OAAO,MAA0B;CACxC,OAAO,QAAQ,OAAO,MAAM,KAAK,SAAS,EAAE,CAAC,CAAC,SAAS,GAAG,GAAG,EAAE,EAAE;AACnE;;AAGA,SAAgB,cAAc,OAA4C;CACxE,OAAO,IAAI,eAAe,EACxB,MAAM,YAAY;EAChB,kBAAkB,OAAO;GACvB,sBAAM,IAAI,IAAI;GACd;GACA;GACA,QAAQ,MAAM;IACZ,MAAM,OAAO,QAAQ,OAAO,KAAK,UAAU,IAAI,CAAC;IAChD,WAAW,QAAQ,OAAO,KAAK,MAAM,CAAC;IACtC,WAAW,QAAQ,IAAI;GACzB;GACA,SAAS;IACP,WAAW,MAAM;GACnB;GACA,QAAQ,OAAO;IACb,WAAW,MAAM,KAAK;GACxB;EACF,CAAC;CACH,EACF,CAAC;AACH;;AAGA,IAAM,cAAN,MAAkB;CAChB;CACA,SAA+B,CAAC;CAChC,SAAiB;CACjB,OAAe;CAEf,YAAY,QAAoC;EAC9C,KAAK,SAAS,OAAO,UAAU;CACjC;CAEA,MAAc,KAAK,MAA6B;EAC9C,OAAO,KAAK,SAAS,QAAQ,CAAC,KAAK,MAAM;GAEvC,MAAM,QAAQ,MAAM,KAAK,OAAO,KAAK;GACrC,IAAI,MAAM,MACR,KAAK,OAAO;QACP;IACL,KAAK,OAAO,KAAK,MAAM,KAAK;IAC5B,KAAK,UAAU,MAAM,MAAM;GAC7B;EACF;CACF;CAEA,KAAa,MAA0B;EACrC,MAAM,SAAS,IAAI,WAAW,IAAI;EAClC,IAAI,SAAS;EACb,OAAO,SAAS,MAAM;GACpB,M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