toiljs 0.0.15 → 0.0.19

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 (273) hide show
  1. package/.babelrc +13 -13
  2. package/.gitattributes +2 -2
  3. package/.github/ISSUE_TEMPLATE/bug_report.md +38 -38
  4. package/.github/ISSUE_TEMPLATE/bug_report.yml +90 -90
  5. package/.github/ISSUE_TEMPLATE/config.yml +8 -8
  6. package/.github/ISSUE_TEMPLATE/feature_request.md +20 -20
  7. package/.github/PULL_REQUEST_TEMPLATE.md +43 -43
  8. package/.github/changelog-config.json +45 -45
  9. package/.github/dependabot.yml +27 -27
  10. package/.github/workflows/ci.yml +191 -191
  11. package/.prettierrc.json +11 -11
  12. package/.vscode/settings.json +9 -9
  13. package/CHANGELOG.md +116 -5
  14. package/LICENSE +187 -187
  15. package/README.md +524 -315
  16. package/as-pect.asconfig.json +34 -34
  17. package/as-pect.config.js +65 -65
  18. package/assets/logo.svg +36 -36
  19. package/build/backend/.tsbuildinfo +1 -1
  20. package/build/backend/index.d.ts +1 -0
  21. package/build/backend/index.js +20 -1
  22. package/build/cli/.tsbuildinfo +1 -1
  23. package/build/cli/index.js +1320 -696
  24. package/build/client/.tsbuildinfo +1 -1
  25. package/build/client/dev/devtools.d.ts +6 -0
  26. package/build/client/dev/devtools.js +479 -0
  27. package/build/client/dev/error-overlay.d.ts +9 -0
  28. package/build/client/dev/error-overlay.js +19 -4
  29. package/build/client/errors.d.ts +1 -0
  30. package/build/client/errors.js +3 -0
  31. package/build/client/index.d.ts +2 -0
  32. package/build/client/index.js +2 -0
  33. package/build/client/navigation/prefetch.d.ts +1 -0
  34. package/build/client/navigation/prefetch.js +35 -0
  35. package/build/client/routing/Router.js +1 -1
  36. package/build/client/routing/hooks.js +6 -2
  37. package/build/client/routing/loader.d.ts +23 -0
  38. package/build/client/routing/loader.js +53 -7
  39. package/build/client/routing/mount.js +4 -3
  40. package/build/client/rpc.d.ts +1 -0
  41. package/build/client/rpc.js +37 -0
  42. package/build/compiler/.tsbuildinfo +1 -1
  43. package/build/compiler/config.d.ts +16 -0
  44. package/build/compiler/config.js +9 -0
  45. package/build/compiler/docs.js +78 -21
  46. package/build/compiler/generate.js +5 -4
  47. package/build/compiler/index.d.ts +3 -2
  48. package/build/compiler/index.js +2 -2
  49. package/build/compiler/plugin.js +228 -0
  50. package/build/compiler/prerender.d.ts +1 -0
  51. package/build/compiler/prerender.js +1 -1
  52. package/build/compiler/seo.d.ts +1 -1
  53. package/build/compiler/seo.js +20 -5
  54. package/build/compiler/ssg.js +39 -2
  55. package/build/compiler/vite.js +25 -0
  56. package/build/io/.tsbuildinfo +1 -1
  57. package/build/io/codec.d.ts +54 -0
  58. package/build/io/codec.js +143 -0
  59. package/build/io/index.d.ts +1 -2
  60. package/build/io/index.js +1 -2
  61. package/build/logger/.tsbuildinfo +1 -1
  62. package/build/shared/.tsbuildinfo +1 -1
  63. package/eslint.config.js +48 -48
  64. package/examples/basic/client/404.tsx +11 -11
  65. package/examples/basic/client/components/.gitkeep +1 -1
  66. package/examples/basic/client/global-error.tsx +13 -13
  67. package/examples/basic/client/layout.tsx +25 -25
  68. package/examples/basic/client/public/images/.gitkeep +1 -1
  69. package/examples/basic/client/public/images/logo.svg +36 -36
  70. package/examples/basic/client/public/robots.txt +2 -2
  71. package/examples/basic/client/routes/docs/[...slug].tsx +12 -12
  72. package/examples/basic/client/routes/features/error/error.tsx +16 -16
  73. package/examples/basic/client/routes/features/index.tsx +1 -1
  74. package/examples/basic/client/routes/features/template/b.tsx +14 -14
  75. package/examples/basic/client/routes/files/[[...slug]].tsx +21 -21
  76. package/examples/basic/client/routes/gallery/layout.tsx +13 -13
  77. package/examples/basic/client/routes/io.tsx +23 -24
  78. package/examples/basic/client/routes/loader-demo/loading.tsx +13 -13
  79. package/examples/basic/client/routes/rest.tsx +74 -0
  80. package/examples/basic/client/routes/rpc.tsx +43 -0
  81. package/examples/basic/client/routes/search.tsx +61 -61
  82. package/examples/basic/client/toil.tsx +5 -5
  83. package/package.json +167 -148
  84. package/presets/eslint.js +88 -88
  85. package/presets/no-uint8array-tostring.js +200 -200
  86. package/presets/prettier-plugin.js +51 -0
  87. package/presets/prettier.json +19 -18
  88. package/presets/tsconfig.json +37 -37
  89. package/server/runtime/README.md +97 -0
  90. package/server/runtime/abort/abort.ts +27 -0
  91. package/server/runtime/env/Server.ts +61 -0
  92. package/server/runtime/envelope.ts +191 -0
  93. package/server/runtime/exports/index.ts +52 -0
  94. package/server/runtime/handlers/ToilHandler.ts +34 -0
  95. package/server/runtime/index.ts +26 -0
  96. package/server/runtime/lang/Potential.ts +5 -0
  97. package/server/runtime/memory.ts +81 -0
  98. package/server/runtime/request.ts +55 -0
  99. package/server/runtime/response.ts +86 -0
  100. package/server/runtime/rest/Rest.ts +39 -0
  101. package/server/runtime/rest/RestHandler.ts +20 -0
  102. package/server/runtime/rest/RouteContext.ts +82 -0
  103. package/server/runtime/rest/match.ts +48 -0
  104. package/server/runtime/tsconfig.json +7 -0
  105. package/src/backend/index.ts +202 -160
  106. package/src/cli/create.ts +15 -5
  107. package/src/cli/diagnostics.ts +81 -0
  108. package/src/cli/doctor.ts +384 -7
  109. package/src/cli/index.ts +11 -2
  110. package/src/cli/proc.ts +50 -50
  111. package/src/cli/updates.ts +69 -69
  112. package/src/cli/validate.ts +31 -31
  113. package/src/client/channel/channel.ts +146 -146
  114. package/src/client/components/Form.tsx +65 -65
  115. package/src/client/components/Script.tsx +113 -113
  116. package/src/client/components/Slot.tsx +21 -21
  117. package/src/client/dev/devtools.tsx +1018 -0
  118. package/src/client/dev/error-overlay.tsx +30 -4
  119. package/src/client/errors.ts +11 -0
  120. package/src/client/head/head.ts +167 -167
  121. package/src/client/head/metadata.ts +112 -112
  122. package/src/client/index.ts +91 -89
  123. package/src/client/navigation/NavLink.tsx +86 -86
  124. package/src/client/navigation/navigation.ts +235 -235
  125. package/src/client/navigation/prefetch.ts +169 -130
  126. package/src/client/navigation/scroll.ts +53 -53
  127. package/src/client/routing/Router.tsx +8 -2
  128. package/src/client/routing/action.ts +122 -122
  129. package/src/client/routing/error-boundary.tsx +43 -43
  130. package/src/client/routing/hooks.ts +21 -6
  131. package/src/client/routing/loader.ts +325 -235
  132. package/src/client/routing/match.ts +47 -47
  133. package/src/client/routing/mount.tsx +54 -52
  134. package/src/client/routing/params-context.ts +10 -10
  135. package/src/client/routing/slot-context.ts +7 -7
  136. package/src/client/rpc.ts +64 -0
  137. package/src/client/search/search.ts +189 -189
  138. package/src/client/search/use-page-search.ts +73 -73
  139. package/src/client/types.ts +73 -73
  140. package/src/compiler/config.ts +221 -182
  141. package/src/compiler/docs.ts +285 -228
  142. package/src/compiler/generate.ts +395 -394
  143. package/src/compiler/index.ts +66 -57
  144. package/src/compiler/pages.ts +70 -70
  145. package/src/compiler/plugin.ts +258 -2
  146. package/src/compiler/prerender.ts +156 -156
  147. package/src/compiler/seo.ts +417 -390
  148. package/src/compiler/ssg.ts +171 -126
  149. package/src/compiler/vite.ts +34 -0
  150. package/src/io/FastMap.ts +151 -127
  151. package/src/io/FastSet.ts +15 -1
  152. package/src/io/codec.ts +217 -0
  153. package/src/io/index.ts +10 -11
  154. package/src/io/lengths.ts +14 -14
  155. package/src/io/types.ts +19 -18
  156. package/src/logger/index.ts +22 -22
  157. package/src/shared/index.ts +10 -10
  158. package/std/client/index.d.ts +15 -15
  159. package/std/client/package.json +3 -3
  160. package/test/assembly/example.spec.ts +17 -7
  161. package/test/channel.test.ts +21 -21
  162. package/test/doctor.test.ts +65 -0
  163. package/test/dom/Link.test.tsx +47 -47
  164. package/test/dom/NavLink.test.tsx +37 -37
  165. package/test/dom/error-overlay.test.tsx +44 -44
  166. package/test/dom/loader.test.tsx +121 -121
  167. package/test/dom/navigation.test.ts +59 -59
  168. package/test/dom/revalidate.test.tsx +38 -38
  169. package/test/dom/route-head.test.tsx +78 -78
  170. package/test/dom/router-loading.test.tsx +44 -44
  171. package/test/dom/scroll.test.ts +56 -56
  172. package/test/dom/use-metadata.test.tsx +58 -58
  173. package/test/errors.test.ts +21 -0
  174. package/test/io.test.ts +117 -93
  175. package/test/navlink.test.ts +28 -28
  176. package/test/placeholder.test.ts +9 -9
  177. package/test/prettier-plugin.test.ts +46 -0
  178. package/test/routes.test.ts +76 -76
  179. package/test/rpc.test.ts +50 -0
  180. package/test/seo.test.ts +175 -164
  181. package/test/slot-layouts.test.ts +69 -69
  182. package/test/ssg.test.ts +36 -36
  183. package/test/update.test.ts +44 -44
  184. package/test/validate.test.ts +42 -42
  185. package/tests/data-parity/generated-parity.ts +99 -0
  186. package/tests/data-parity/parity.ts +80 -0
  187. package/tests/data-parity/spec.ts +46 -0
  188. package/toil-routes.d.ts +7 -0
  189. package/tsconfig.backend.json +13 -13
  190. package/tsconfig.base.json +35 -35
  191. package/tsconfig.cli.json +13 -13
  192. package/tsconfig.client.json +14 -14
  193. package/tsconfig.compiler.json +13 -13
  194. package/tsconfig.io.json +12 -12
  195. package/tsconfig.json +22 -22
  196. package/tsconfig.logger.json +12 -12
  197. package/tsconfig.server.json +10 -10
  198. package/tsconfig.shared.json +12 -12
  199. package/vitest.config.ts +26 -26
  200. package/.idea/codeStyles/Project.xml +0 -54
  201. package/.idea/codeStyles/codeStyleConfig.xml +0 -5
  202. package/.idea/inspectionProfiles/Project_Default.xml +0 -6
  203. package/.idea/modules.xml +0 -8
  204. package/.idea/prettier.xml +0 -7
  205. package/.idea/toiljs.iml +0 -8
  206. package/.idea/vcs.xml +0 -6
  207. package/.toil/entry.tsx +0 -9
  208. package/.toil/index.html +0 -12
  209. package/.toil/routes.ts +0 -9
  210. package/build/cli/configure.d.ts +0 -16
  211. package/build/cli/configure.js +0 -272
  212. package/build/cli/create.d.ts +0 -16
  213. package/build/cli/create.js +0 -420
  214. package/build/cli/diagnostics.d.ts +0 -55
  215. package/build/cli/diagnostics.js +0 -333
  216. package/build/cli/doctor.d.ts +0 -6
  217. package/build/cli/doctor.js +0 -249
  218. package/build/cli/features.d.ts +0 -25
  219. package/build/cli/features.js +0 -107
  220. package/build/cli/index.d.ts +0 -2
  221. package/build/cli/proc.d.ts +0 -6
  222. package/build/cli/proc.js +0 -31
  223. package/build/cli/ui.d.ts +0 -9
  224. package/build/cli/ui.js +0 -75
  225. package/build/cli/update.d.ts +0 -7
  226. package/build/cli/update.js +0 -117
  227. package/build/cli/updates.d.ts +0 -10
  228. package/build/cli/updates.js +0 -45
  229. package/build/cli/validate.d.ts +0 -4
  230. package/build/cli/validate.js +0 -19
  231. package/build/client/Link.d.ts +0 -8
  232. package/build/client/Link.js +0 -44
  233. package/build/client/NavLink.d.ts +0 -14
  234. package/build/client/NavLink.js +0 -37
  235. package/build/client/Router.d.ts +0 -7
  236. package/build/client/Router.js +0 -55
  237. package/build/client/channel.d.ts +0 -23
  238. package/build/client/channel.js +0 -94
  239. package/build/client/error-boundary.d.ts +0 -16
  240. package/build/client/error-boundary.js +0 -19
  241. package/build/client/head.d.ts +0 -26
  242. package/build/client/head.js +0 -87
  243. package/build/client/hooks.d.ts +0 -17
  244. package/build/client/hooks.js +0 -48
  245. package/build/client/lazy.d.ts +0 -16
  246. package/build/client/lazy.js +0 -53
  247. package/build/client/match.d.ts +0 -2
  248. package/build/client/match.js +0 -32
  249. package/build/client/mount.d.ts +0 -2
  250. package/build/client/mount.js +0 -13
  251. package/build/client/navigation.d.ts +0 -13
  252. package/build/client/navigation.js +0 -97
  253. package/build/client/params-context.d.ts +0 -2
  254. package/build/client/params-context.js +0 -2
  255. package/build/client/prefetch.d.ts +0 -11
  256. package/build/client/prefetch.js +0 -100
  257. package/build/client/runtime.d.ts +0 -31
  258. package/build/client/runtime.js +0 -112
  259. package/build/client/scroll.d.ts +0 -8
  260. package/build/client/scroll.js +0 -36
  261. package/build/io/BinaryReader.d.ts +0 -44
  262. package/build/io/BinaryReader.js +0 -244
  263. package/build/io/BinaryWriter.d.ts +0 -44
  264. package/build/io/BinaryWriter.js +0 -297
  265. package/build/server/release.wasm +0 -0
  266. package/build/server/release.wat +0 -9
  267. package/src/io/BinaryReader.ts +0 -340
  268. package/src/io/BinaryWriter.ts +0 -385
  269. package/src/server/index.ts +0 -10
  270. package/src/server/main.ts +0 -13
  271. package/src/server/tsconfig.json +0 -4
  272. package/toil-env.d.ts +0 -16
  273. package/toilconfig.json +0 -30
@@ -1,31 +0,0 @@
1
- import { type ComponentType, type ReactNode } from 'react';
2
- import { type RouteParams } from './match.js';
3
- export interface RouteDef {
4
- readonly pattern: string;
5
- readonly load: () => Promise<{
6
- default: ComponentType;
7
- }>;
8
- }
9
- export type LayoutLoader = (() => Promise<{
10
- default: ComponentType<{
11
- children?: ReactNode;
12
- }>;
13
- }>) | null;
14
- export type NotFoundLoader = (() => Promise<{
15
- default: ComponentType;
16
- }>) | null;
17
- export declare function navigate(href: string): void;
18
- export declare function useParams(): RouteParams;
19
- export declare function useNavigate(): (href: string) => void;
20
- export declare function useLocation(): string;
21
- export declare function Link(props: {
22
- href: string;
23
- className?: string;
24
- children?: ReactNode;
25
- }): ReactNode;
26
- export declare function Router(props: {
27
- routes: RouteDef[];
28
- layout?: LayoutLoader;
29
- notFound?: NotFoundLoader;
30
- }): ReactNode;
31
- export declare function mount(routes: RouteDef[], layout?: LayoutLoader, notFound?: NotFoundLoader): void;
@@ -1,112 +0,0 @@
1
- import { jsx as _jsx } from "react/jsx-runtime";
2
- import { createContext, lazy, Suspense, useContext, useEffect, useState, } from 'react';
3
- import { createRoot } from 'react-dom/client';
4
- import { matchRoute } from './match.js';
5
- const listeners = new Set();
6
- export function navigate(href) {
7
- window.history.pushState({}, '', href);
8
- for (const listener of listeners)
9
- listener();
10
- }
11
- const ParamsContext = createContext({});
12
- export function useParams() {
13
- return useContext(ParamsContext);
14
- }
15
- export function useNavigate() {
16
- return navigate;
17
- }
18
- export function useLocation() {
19
- const [pathname, setPathname] = useState(() => window.location.pathname);
20
- useEffect(() => {
21
- const update = () => {
22
- setPathname(window.location.pathname);
23
- };
24
- listeners.add(update);
25
- window.addEventListener('popstate', update);
26
- return () => {
27
- listeners.delete(update);
28
- window.removeEventListener('popstate', update);
29
- };
30
- }, []);
31
- return pathname;
32
- }
33
- export function Link(props) {
34
- const { href, className, children } = props;
35
- const onClick = (e) => {
36
- if (e.defaultPrevented ||
37
- e.button !== 0 ||
38
- e.metaKey ||
39
- e.ctrlKey ||
40
- e.shiftKey ||
41
- e.altKey)
42
- return;
43
- e.preventDefault();
44
- navigate(href);
45
- };
46
- return (_jsx("a", { href: href, className: className, onClick: onClick, children: children }));
47
- }
48
- const pageCache = new Map();
49
- function pageComponent(route) {
50
- let component = pageCache.get(route);
51
- if (!component) {
52
- component = lazy(route.load);
53
- pageCache.set(route, component);
54
- }
55
- return component;
56
- }
57
- let layoutComponent = null;
58
- let layoutLoader = null;
59
- function resolveLayout(loader) {
60
- if (layoutLoader !== loader || !layoutComponent) {
61
- layoutComponent = lazy(loader);
62
- layoutLoader = loader;
63
- }
64
- return layoutComponent;
65
- }
66
- let notFoundComponent = null;
67
- let notFoundLoader = null;
68
- function resolveNotFound(loader) {
69
- if (notFoundLoader !== loader || !notFoundComponent) {
70
- notFoundComponent = lazy(loader);
71
- notFoundLoader = loader;
72
- }
73
- return notFoundComponent;
74
- }
75
- export function Router(props) {
76
- const { routes, layout = null, notFound = null } = props;
77
- const pathname = useLocation();
78
- let matched;
79
- let params = {};
80
- for (const route of routes) {
81
- const result = matchRoute(route.pattern, pathname);
82
- if (result) {
83
- matched = route;
84
- params = result;
85
- break;
86
- }
87
- }
88
- let page;
89
- if (matched) {
90
- const Page = pageComponent(matched);
91
- page = (_jsx(Suspense, { fallback: null, children: _jsx(Page, {}) }));
92
- }
93
- else if (notFound) {
94
- const NotFound = resolveNotFound(notFound);
95
- page = (_jsx(Suspense, { fallback: null, children: _jsx(NotFound, {}) }));
96
- }
97
- else {
98
- page = _jsx("div", { style: { padding: 24, fontFamily: 'system-ui' }, children: "404 \u2014 Not found" });
99
- }
100
- const withParams = _jsx(ParamsContext.Provider, { value: params, children: page });
101
- if (layout) {
102
- const Layout = resolveLayout(layout);
103
- return (_jsx(Suspense, { fallback: null, children: _jsx(Layout, { children: withParams }) }));
104
- }
105
- return withParams;
106
- }
107
- export function mount(routes, layout = null, notFound = null) {
108
- const el = document.getElementById('root');
109
- if (!el)
110
- throw new Error('toil: #root element not found');
111
- createRoot(el).render(_jsx(Router, { routes: routes, layout: layout, notFound: notFound }));
112
- }
@@ -1,8 +0,0 @@
1
- export declare function enableManualScrollRestoration(): void;
2
- export declare function rememberScroll(key: string): void;
3
- export declare function planScroll(opts: {
4
- restoreKey?: string;
5
- hash: string;
6
- toTop: boolean;
7
- }): void;
8
- export declare function applyScroll(): void;
@@ -1,36 +0,0 @@
1
- const positions = new Map();
2
- let plan = null;
3
- export function enableManualScrollRestoration() {
4
- if (typeof window !== 'undefined' && 'scrollRestoration' in window.history) {
5
- window.history.scrollRestoration = 'manual';
6
- }
7
- }
8
- export function rememberScroll(key) {
9
- positions.set(key, window.scrollY);
10
- }
11
- export function planScroll(opts) {
12
- plan = {
13
- restore: opts.restoreKey !== undefined ? (positions.get(opts.restoreKey) ?? 0) : null,
14
- hash: opts.hash,
15
- toTop: opts.toTop,
16
- };
17
- }
18
- export function applyScroll() {
19
- const current = plan;
20
- plan = null;
21
- if (!current)
22
- return;
23
- if (current.hash) {
24
- const el = document.getElementById(decodeURIComponent(current.hash.slice(1)));
25
- if (el) {
26
- el.scrollIntoView();
27
- return;
28
- }
29
- }
30
- if (current.restore !== null) {
31
- window.scrollTo(0, current.restore);
32
- return;
33
- }
34
- if (current.toTop)
35
- window.scrollTo(0, 0);
36
- }
@@ -1,44 +0,0 @@
1
- import type { BufferLike, i16, i32, i64, i8, Selector, u16, u32, u8 } from './types.js';
2
- export declare class BinaryReader {
3
- private buffer;
4
- private currentOffset;
5
- constructor(bytes: BufferLike);
6
- get byteLength(): number;
7
- static stringCompare(a: string, b: string): number;
8
- static bigintCompare(a: bigint, b: bigint): number;
9
- static numberCompare(a: number, b: number): number;
10
- setBuffer(bytes: BufferLike): void;
11
- length(): number;
12
- bytesLeft(): number;
13
- readI8(): i8;
14
- readI16(be?: boolean): i16;
15
- readI32(be?: boolean): i32;
16
- readI64(be?: boolean): i64;
17
- readU8(): u8;
18
- readU16(be?: boolean): u16;
19
- readU32(be?: boolean): u32;
20
- readU64(be?: boolean): bigint;
21
- readU128(be?: boolean): bigint;
22
- readU256(be?: boolean): bigint;
23
- readI128(be?: boolean): bigint;
24
- readBoolean(): boolean;
25
- readSelector(): Selector;
26
- readBytes(length: u32, zeroStop?: boolean): Uint8Array;
27
- readString(length: u32): string;
28
- readStringWithLength(be?: boolean): string;
29
- readBytesWithLength(maxLength?: number, be?: boolean): Uint8Array;
30
- readArrayOfBuffer(be?: boolean): Uint8Array[];
31
- readU256Array(be?: boolean): bigint[];
32
- readU128Array(be?: boolean): bigint[];
33
- readU64Array(be?: boolean): bigint[];
34
- readU32Array(be?: boolean): u32[];
35
- readU16Array(be?: boolean): u16[];
36
- readU8Array(): u8[];
37
- readStringArray(be?: boolean): string[];
38
- readBytesArray(be?: boolean): Uint8Array[];
39
- getOffset(): u16;
40
- setOffset(offset: u16): void;
41
- verifyEnd(size: number): void;
42
- private reverseBytes;
43
- private toHexString;
44
- }
@@ -1,244 +0,0 @@
1
- import { I128_BYTE_LENGTH, I16_BYTE_LENGTH, I32_BYTE_LENGTH, I64_BYTE_LENGTH, I8_BYTE_LENGTH, U128_BYTE_LENGTH, U16_BYTE_LENGTH, U256_BYTE_LENGTH, U32_BYTE_LENGTH, U64_BYTE_LENGTH, U8_BYTE_LENGTH, } from './lengths.js';
2
- export class BinaryReader {
3
- buffer;
4
- currentOffset = 0;
5
- constructor(bytes) {
6
- this.buffer = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
7
- }
8
- get byteLength() {
9
- return this.buffer.byteLength;
10
- }
11
- static stringCompare(a, b) {
12
- return a.localeCompare(b);
13
- }
14
- static bigintCompare(a, b) {
15
- if (a < b)
16
- return -1;
17
- if (a > b)
18
- return 1;
19
- return 0;
20
- }
21
- static numberCompare(a, b) {
22
- if (a < b)
23
- return -1;
24
- if (a > b)
25
- return 1;
26
- return 0;
27
- }
28
- setBuffer(bytes) {
29
- this.buffer = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
30
- this.currentOffset = 0;
31
- }
32
- length() {
33
- return this.buffer.byteLength;
34
- }
35
- bytesLeft() {
36
- return this.buffer.byteLength - this.currentOffset;
37
- }
38
- readI8() {
39
- this.verifyEnd(this.currentOffset + I8_BYTE_LENGTH);
40
- const value = this.buffer.getInt8(this.currentOffset);
41
- this.currentOffset += I8_BYTE_LENGTH;
42
- return value;
43
- }
44
- readI16(be = true) {
45
- this.verifyEnd(this.currentOffset + I16_BYTE_LENGTH);
46
- const value = this.buffer.getInt16(this.currentOffset, !be);
47
- this.currentOffset += I16_BYTE_LENGTH;
48
- return value;
49
- }
50
- readI32(be = true) {
51
- this.verifyEnd(this.currentOffset + I32_BYTE_LENGTH);
52
- const value = this.buffer.getInt32(this.currentOffset, !be);
53
- this.currentOffset += I32_BYTE_LENGTH;
54
- return value;
55
- }
56
- readI64(be = true) {
57
- this.verifyEnd(this.currentOffset + I64_BYTE_LENGTH);
58
- const value = this.buffer.getBigInt64(this.currentOffset, !be);
59
- this.currentOffset += I64_BYTE_LENGTH;
60
- return value;
61
- }
62
- readU8() {
63
- this.verifyEnd(this.currentOffset + U8_BYTE_LENGTH);
64
- const value = this.buffer.getUint8(this.currentOffset);
65
- this.currentOffset += U8_BYTE_LENGTH;
66
- return value;
67
- }
68
- readU16(be = true) {
69
- this.verifyEnd(this.currentOffset + U16_BYTE_LENGTH);
70
- const value = this.buffer.getUint16(this.currentOffset, !be);
71
- this.currentOffset += U16_BYTE_LENGTH;
72
- return value;
73
- }
74
- readU32(be = true) {
75
- this.verifyEnd(this.currentOffset + U32_BYTE_LENGTH);
76
- const value = this.buffer.getUint32(this.currentOffset, !be);
77
- this.currentOffset += U32_BYTE_LENGTH;
78
- return value;
79
- }
80
- readU64(be = true) {
81
- this.verifyEnd(this.currentOffset + U64_BYTE_LENGTH);
82
- const value = this.buffer.getBigUint64(this.currentOffset, !be);
83
- this.currentOffset += U64_BYTE_LENGTH;
84
- return value;
85
- }
86
- readU128(be = true) {
87
- const raw = this.readBytes(U128_BYTE_LENGTH);
88
- let bytes = raw;
89
- if (!be) {
90
- bytes = this.reverseBytes(raw);
91
- }
92
- return BigInt('0x' + this.toHexString(bytes));
93
- }
94
- readU256(be = true) {
95
- const raw = this.readBytes(U256_BYTE_LENGTH);
96
- let bytes = raw;
97
- if (!be) {
98
- bytes = this.reverseBytes(raw);
99
- }
100
- return BigInt('0x' + this.toHexString(bytes));
101
- }
102
- readI128(be = true) {
103
- const raw = this.readBytes(I128_BYTE_LENGTH);
104
- let bytes = raw;
105
- if (!be) {
106
- bytes = this.reverseBytes(raw);
107
- }
108
- let value = BigInt('0x' + this.toHexString(bytes));
109
- const signBitMask = 0x80;
110
- if (bytes[0] & signBitMask) {
111
- const twoTo128 = BigInt(1) << BigInt(128);
112
- value = value - twoTo128;
113
- }
114
- return value;
115
- }
116
- readBoolean() {
117
- return this.readU8() !== 0;
118
- }
119
- readSelector() {
120
- return this.readU32(true);
121
- }
122
- readBytes(length, zeroStop = false) {
123
- this.verifyEnd(this.currentOffset + length);
124
- let bytes = new Uint8Array(length);
125
- for (let i = 0; i < length; i++) {
126
- const b = this.buffer.getUint8(this.currentOffset++);
127
- if (zeroStop && b === 0) {
128
- bytes = new Uint8Array(bytes.subarray(0, i));
129
- break;
130
- }
131
- bytes[i] = b;
132
- }
133
- return bytes;
134
- }
135
- readString(length) {
136
- const textDecoder = new TextDecoder();
137
- const bytes = this.readBytes(length, false);
138
- return textDecoder.decode(bytes);
139
- }
140
- readStringWithLength(be = true) {
141
- const length = this.readU32(be);
142
- return this.readString(length);
143
- }
144
- readBytesWithLength(maxLength = 0, be = true) {
145
- const length = this.readU32(be);
146
- if (maxLength > 0 && length > maxLength) {
147
- throw new Error('Data length exceeds maximum length.');
148
- }
149
- return this.readBytes(length);
150
- }
151
- readArrayOfBuffer(be = true) {
152
- const length = this.readU16(be);
153
- const result = new Array(length);
154
- for (let i = 0; i < length; i++) {
155
- result[i] = this.readBytesWithLength();
156
- }
157
- return result;
158
- }
159
- readU256Array(be = true) {
160
- const length = this.readU16(be);
161
- const result = new Array(length);
162
- for (let i = 0; i < length; i++) {
163
- result[i] = this.readU256(be);
164
- }
165
- return result;
166
- }
167
- readU128Array(be = true) {
168
- const length = this.readU16(be);
169
- const result = new Array(length);
170
- for (let i = 0; i < length; i++) {
171
- result[i] = this.readU128(be);
172
- }
173
- return result;
174
- }
175
- readU64Array(be = true) {
176
- const length = this.readU16(be);
177
- const result = new Array(length);
178
- for (let i = 0; i < length; i++) {
179
- result[i] = this.readU64(be);
180
- }
181
- return result;
182
- }
183
- readU32Array(be = true) {
184
- const length = this.readU16(be);
185
- const result = new Array(length);
186
- for (let i = 0; i < length; i++) {
187
- result[i] = this.readU32(be);
188
- }
189
- return result;
190
- }
191
- readU16Array(be = true) {
192
- const length = this.readU16(be);
193
- const result = new Array(length);
194
- for (let i = 0; i < length; i++) {
195
- result[i] = this.readU16(be);
196
- }
197
- return result;
198
- }
199
- readU8Array() {
200
- const length = this.readU16(true);
201
- const result = new Array(length);
202
- for (let i = 0; i < length; i++) {
203
- result[i] = this.readU8();
204
- }
205
- return result;
206
- }
207
- readStringArray(be = true) {
208
- const length = this.readU16(be);
209
- const result = new Array(length);
210
- for (let i = 0; i < length; i++) {
211
- result[i] = this.readStringWithLength(be);
212
- }
213
- return result;
214
- }
215
- readBytesArray(be = true) {
216
- const length = this.readU16(be);
217
- const result = new Array(length);
218
- for (let i = 0; i < length; i++) {
219
- result[i] = this.readBytesWithLength(0, be);
220
- }
221
- return result;
222
- }
223
- getOffset() {
224
- return this.currentOffset;
225
- }
226
- setOffset(offset) {
227
- this.currentOffset = offset;
228
- }
229
- verifyEnd(size) {
230
- if (size > this.buffer.byteLength) {
231
- throw new Error(`Attempt to read beyond buffer length: requested up to byte offset ${size}, but buffer is only ${this.buffer.byteLength} bytes.`);
232
- }
233
- }
234
- reverseBytes(bytes) {
235
- const out = new Uint8Array(bytes.length);
236
- for (let i = 0; i < bytes.length; i++) {
237
- out[i] = bytes[bytes.length - 1 - i];
238
- }
239
- return out;
240
- }
241
- toHexString(bytes) {
242
- return Array.from(bytes, (b) => b.toString(16).padStart(2, '0')).join('');
243
- }
244
- }
@@ -1,44 +0,0 @@
1
- import type { i16, i32, i64, i8, Selector, u16, u32, u64, u8 } from './types.js';
2
- import { BinaryReader } from './BinaryReader.js';
3
- export declare class BinaryWriter implements Disposable {
4
- private currentOffset;
5
- private buffer;
6
- constructor(length?: number);
7
- static estimateArrayOfBufferLength(values: Uint8Array[]): u32;
8
- writeU8(value: u8): void;
9
- writeU16(value: u16, be?: boolean): void;
10
- writeU32(value: u32, be?: boolean): void;
11
- writeU64(value: u64, be?: boolean): void;
12
- writeI8(value: i8): void;
13
- writeI16(value: i16, be?: boolean): void;
14
- writeI32(value: i32, be?: boolean): void;
15
- writeI64(value: i64, be?: boolean): void;
16
- writeSelector(value: Selector): void;
17
- writeBoolean(value: boolean): void;
18
- writeI128(bigIntValue: bigint, be?: boolean): void;
19
- writeU256(bigIntValue: bigint, be?: boolean): void;
20
- writeU128(bigIntValue: bigint, be?: boolean): void;
21
- writeBytes(value: Uint8Array): void;
22
- writeString(value: string): void;
23
- writeStringWithLength(value: string): void;
24
- getBuffer(clear?: boolean): Uint8Array;
25
- reset(): void;
26
- toBytesReader(): BinaryReader;
27
- getOffset(): u32;
28
- setOffset(offset: u32): void;
29
- clear(): void;
30
- [Symbol.dispose](): void;
31
- allocSafe(size: u32): void;
32
- writeBytesWithLength(value: Uint8Array): void;
33
- writeArrayOfBuffer(values: Uint8Array[], be?: boolean): void;
34
- writeU32Array(value: u32[], be?: boolean): void;
35
- writeU256Array(value: bigint[], be?: boolean): void;
36
- writeU128Array(value: bigint[], be?: boolean): void;
37
- writeStringArray(value: string[]): void;
38
- writeU16Array(value: u16[], be?: boolean): void;
39
- writeU8Array(value: u8[]): void;
40
- writeU64Array(value: bigint[], be?: boolean): void;
41
- writeBytesArray(value: Uint8Array[]): void;
42
- private resize;
43
- private getDefaultBuffer;
44
- }