@viberaven/sessions 0.0.0-stage → 0.1.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.
Files changed (155) hide show
  1. package/LICENSE +202 -0
  2. package/NOTICE +41 -0
  3. package/README.md +136 -2
  4. package/THIRD_PARTY_NOTICES +6770 -0
  5. package/dist/browser.mjs +2642 -0
  6. package/examples/quickstart.mjs +11 -0
  7. package/licenses/Emscripten.txt +101 -0
  8. package/licenses/ICU.txt +542 -0
  9. package/licenses/OSSP-UUID.txt +24 -0
  10. package/licenses/OpenSSL.txt +177 -0
  11. package/licenses/PGlite.txt +176 -0
  12. package/licenses/PostgreSQL-extra.txt +3592 -0
  13. package/licenses/PostgreSQL.txt +23 -0
  14. package/licenses/README.md +36 -0
  15. package/licenses/Snowball.txt +29 -0
  16. package/licenses/async-mutex.txt +21 -0
  17. package/licenses/base64-js.txt +21 -0
  18. package/licenses/buffer.txt +21 -0
  19. package/licenses/compiler-rt.txt +311 -0
  20. package/licenses/dlmalloc.txt +135 -0
  21. package/licenses/esbuild.txt +21 -0
  22. package/licenses/ieee754.txt +11 -0
  23. package/licenses/libcxx.txt +311 -0
  24. package/licenses/libcxxabi.txt +311 -0
  25. package/licenses/libunwind.txt +311 -0
  26. package/licenses/libxml2-extra.txt +34 -0
  27. package/licenses/libxml2.txt +24 -0
  28. package/licenses/libxslt.txt +53 -0
  29. package/licenses/musl.txt +193 -0
  30. package/licenses/pg-protocol.txt +21 -0
  31. package/licenses/tinytar.txt +21 -0
  32. package/licenses/tslib.txt +28 -0
  33. package/licenses/tsup.txt +21 -0
  34. package/licenses/zlib.txt +22 -0
  35. package/package.json +74 -4
  36. package/runtime/engine/frozen/runtime/engine-host.mjs +377 -0
  37. package/runtime/engine/frozen/runtime/engine-logs/static-layout.json +21357 -0
  38. package/runtime/engine/frozen/runtime/pglite/packages/pglite/dist/amcheck.tar.gz +0 -0
  39. package/runtime/engine/frozen/runtime/pglite/packages/pglite/dist/auth_delay.tar.gz +0 -0
  40. package/runtime/engine/frozen/runtime/pglite/packages/pglite/dist/auto_explain.tar.gz +0 -0
  41. package/runtime/engine/frozen/runtime/pglite/packages/pglite/dist/basebackup_to_shell.tar.gz +0 -0
  42. package/runtime/engine/frozen/runtime/pglite/packages/pglite/dist/basic_archive.tar.gz +0 -0
  43. package/runtime/engine/frozen/runtime/pglite/packages/pglite/dist/bloom.tar.gz +0 -0
  44. package/runtime/engine/frozen/runtime/pglite/packages/pglite/dist/btree_gin.tar.gz +0 -0
  45. package/runtime/engine/frozen/runtime/pglite/packages/pglite/dist/btree_gist.tar.gz +0 -0
  46. package/runtime/engine/frozen/runtime/pglite/packages/pglite/dist/chunk-5L4W4HOZ.js +3 -0
  47. package/runtime/engine/frozen/runtime/pglite/packages/pglite/dist/chunk-C56CJGL3.js +6 -0
  48. package/runtime/engine/frozen/runtime/pglite/packages/pglite/dist/chunk-CMNLZGEJ.js +2 -0
  49. package/runtime/engine/frozen/runtime/pglite/packages/pglite/dist/chunk-DAMXLPQW.js +2 -0
  50. package/runtime/engine/frozen/runtime/pglite/packages/pglite/dist/chunk-EZSFBMVG.js +2 -0
  51. package/runtime/engine/frozen/runtime/pglite/packages/pglite/dist/chunk-GS6VKQIC.js +2 -0
  52. package/runtime/engine/frozen/runtime/pglite/packages/pglite/dist/chunk-OCWZREWN.js +2 -0
  53. package/runtime/engine/frozen/runtime/pglite/packages/pglite/dist/chunk-UQDQKWPM.js +2 -0
  54. package/runtime/engine/frozen/runtime/pglite/packages/pglite/dist/chunk-YFZE77YQ.js +9 -0
  55. package/runtime/engine/frozen/runtime/pglite/packages/pglite/dist/citext.tar.gz +0 -0
  56. package/runtime/engine/frozen/runtime/pglite/packages/pglite/dist/contrib/amcheck.js +2 -0
  57. package/runtime/engine/frozen/runtime/pglite/packages/pglite/dist/contrib/auto_explain.js +2 -0
  58. package/runtime/engine/frozen/runtime/pglite/packages/pglite/dist/contrib/bloom.js +2 -0
  59. package/runtime/engine/frozen/runtime/pglite/packages/pglite/dist/contrib/btree_gin.js +2 -0
  60. package/runtime/engine/frozen/runtime/pglite/packages/pglite/dist/contrib/btree_gist.js +2 -0
  61. package/runtime/engine/frozen/runtime/pglite/packages/pglite/dist/contrib/citext.js +2 -0
  62. package/runtime/engine/frozen/runtime/pglite/packages/pglite/dist/contrib/cube.js +2 -0
  63. package/runtime/engine/frozen/runtime/pglite/packages/pglite/dist/contrib/dict_int.js +2 -0
  64. package/runtime/engine/frozen/runtime/pglite/packages/pglite/dist/contrib/dict_xsyn.js +2 -0
  65. package/runtime/engine/frozen/runtime/pglite/packages/pglite/dist/contrib/earthdistance.js +2 -0
  66. package/runtime/engine/frozen/runtime/pglite/packages/pglite/dist/contrib/file_fdw.js +2 -0
  67. package/runtime/engine/frozen/runtime/pglite/packages/pglite/dist/contrib/fuzzystrmatch.js +2 -0
  68. package/runtime/engine/frozen/runtime/pglite/packages/pglite/dist/contrib/hstore.js +2 -0
  69. package/runtime/engine/frozen/runtime/pglite/packages/pglite/dist/contrib/intarray.js +2 -0
  70. package/runtime/engine/frozen/runtime/pglite/packages/pglite/dist/contrib/isn.js +2 -0
  71. package/runtime/engine/frozen/runtime/pglite/packages/pglite/dist/contrib/lo.js +2 -0
  72. package/runtime/engine/frozen/runtime/pglite/packages/pglite/dist/contrib/ltree.js +2 -0
  73. package/runtime/engine/frozen/runtime/pglite/packages/pglite/dist/contrib/moddatetime.js +2 -0
  74. package/runtime/engine/frozen/runtime/pglite/packages/pglite/dist/contrib/pageinspect.js +2 -0
  75. package/runtime/engine/frozen/runtime/pglite/packages/pglite/dist/contrib/pg_buffercache.js +2 -0
  76. package/runtime/engine/frozen/runtime/pglite/packages/pglite/dist/contrib/pg_freespacemap.js +2 -0
  77. package/runtime/engine/frozen/runtime/pglite/packages/pglite/dist/contrib/pg_stat_statements.js +2 -0
  78. package/runtime/engine/frozen/runtime/pglite/packages/pglite/dist/contrib/pg_surgery.js +2 -0
  79. package/runtime/engine/frozen/runtime/pglite/packages/pglite/dist/contrib/pg_trgm.js +2 -0
  80. package/runtime/engine/frozen/runtime/pglite/packages/pglite/dist/contrib/pg_visibility.js +2 -0
  81. package/runtime/engine/frozen/runtime/pglite/packages/pglite/dist/contrib/pg_walinspect.js +2 -0
  82. package/runtime/engine/frozen/runtime/pglite/packages/pglite/dist/contrib/pgcrypto.js +2 -0
  83. package/runtime/engine/frozen/runtime/pglite/packages/pglite/dist/contrib/seg.js +2 -0
  84. package/runtime/engine/frozen/runtime/pglite/packages/pglite/dist/contrib/tablefunc.js +2 -0
  85. package/runtime/engine/frozen/runtime/pglite/packages/pglite/dist/contrib/tcn.js +2 -0
  86. package/runtime/engine/frozen/runtime/pglite/packages/pglite/dist/contrib/tsm_system_rows.js +2 -0
  87. package/runtime/engine/frozen/runtime/pglite/packages/pglite/dist/contrib/tsm_system_time.js +2 -0
  88. package/runtime/engine/frozen/runtime/pglite/packages/pglite/dist/contrib/unaccent.js +2 -0
  89. package/runtime/engine/frozen/runtime/pglite/packages/pglite/dist/contrib/uuid_ossp.js +2 -0
  90. package/runtime/engine/frozen/runtime/pglite/packages/pglite/dist/cube.tar.gz +0 -0
  91. package/runtime/engine/frozen/runtime/pglite/packages/pglite/dist/dblink.tar.gz +0 -0
  92. package/runtime/engine/frozen/runtime/pglite/packages/pglite/dist/dict_int.tar.gz +0 -0
  93. package/runtime/engine/frozen/runtime/pglite/packages/pglite/dist/dict_xsyn.tar.gz +0 -0
  94. package/runtime/engine/frozen/runtime/pglite/packages/pglite/dist/earthdistance.tar.gz +0 -0
  95. package/runtime/engine/frozen/runtime/pglite/packages/pglite/dist/file_fdw.tar.gz +0 -0
  96. package/runtime/engine/frozen/runtime/pglite/packages/pglite/dist/fs/base.js +2 -0
  97. package/runtime/engine/frozen/runtime/pglite/packages/pglite/dist/fs/memfs-frontend.js +2 -0
  98. package/runtime/engine/frozen/runtime/pglite/packages/pglite/dist/fs/memfs2opfs.js +2 -0
  99. package/runtime/engine/frozen/runtime/pglite/packages/pglite/dist/fs/nodefs.js +2 -0
  100. package/runtime/engine/frozen/runtime/pglite/packages/pglite/dist/fs/opfs-ahp.js +4 -0
  101. package/runtime/engine/frozen/runtime/pglite/packages/pglite/dist/fuzzystrmatch.tar.gz +0 -0
  102. package/runtime/engine/frozen/runtime/pglite/packages/pglite/dist/hstore.tar.gz +0 -0
  103. package/runtime/engine/frozen/runtime/pglite/packages/pglite/dist/index.js +2 -0
  104. package/runtime/engine/frozen/runtime/pglite/packages/pglite/dist/initdb.js +2 -0
  105. package/runtime/engine/frozen/runtime/pglite/packages/pglite/dist/initdb.wasm +0 -0
  106. package/runtime/engine/frozen/runtime/pglite/packages/pglite/dist/intagg.tar.gz +0 -0
  107. package/runtime/engine/frozen/runtime/pglite/packages/pglite/dist/intarray.tar.gz +0 -0
  108. package/runtime/engine/frozen/runtime/pglite/packages/pglite/dist/isn.tar.gz +0 -0
  109. package/runtime/engine/frozen/runtime/pglite/packages/pglite/dist/live/index.js +134 -0
  110. package/runtime/engine/frozen/runtime/pglite/packages/pglite/dist/lo.tar.gz +0 -0
  111. package/runtime/engine/frozen/runtime/pglite/packages/pglite/dist/ltree.tar.gz +0 -0
  112. package/runtime/engine/frozen/runtime/pglite/packages/pglite/dist/node-postgres/index.js +2 -0
  113. package/runtime/engine/frozen/runtime/pglite/packages/pglite/dist/oid2name.tar.gz +0 -0
  114. package/runtime/engine/frozen/runtime/pglite/packages/pglite/dist/pageinspect.tar.gz +0 -0
  115. package/runtime/engine/frozen/runtime/pglite/packages/pglite/dist/passwordcheck.tar.gz +0 -0
  116. package/runtime/engine/frozen/runtime/pglite/packages/pglite/dist/pg_buffercache.tar.gz +0 -0
  117. package/runtime/engine/frozen/runtime/pglite/packages/pglite/dist/pg_freespacemap.tar.gz +0 -0
  118. package/runtime/engine/frozen/runtime/pglite/packages/pglite/dist/pg_logicalinspect.tar.gz +0 -0
  119. package/runtime/engine/frozen/runtime/pglite/packages/pglite/dist/pg_overexplain.tar.gz +0 -0
  120. package/runtime/engine/frozen/runtime/pglite/packages/pglite/dist/pg_prewarm.tar.gz +0 -0
  121. package/runtime/engine/frozen/runtime/pglite/packages/pglite/dist/pg_stat_statements.tar.gz +0 -0
  122. package/runtime/engine/frozen/runtime/pglite/packages/pglite/dist/pg_surgery.tar.gz +0 -0
  123. package/runtime/engine/frozen/runtime/pglite/packages/pglite/dist/pg_trgm.tar.gz +0 -0
  124. package/runtime/engine/frozen/runtime/pglite/packages/pglite/dist/pg_visibility.tar.gz +0 -0
  125. package/runtime/engine/frozen/runtime/pglite/packages/pglite/dist/pg_walinspect.tar.gz +0 -0
  126. package/runtime/engine/frozen/runtime/pglite/packages/pglite/dist/pgcrypto.tar.gz +0 -0
  127. package/runtime/engine/frozen/runtime/pglite/packages/pglite/dist/pglite.data +37614 -8
  128. package/runtime/engine/frozen/runtime/pglite/packages/pglite/dist/pglite.js +2 -0
  129. package/runtime/engine/frozen/runtime/pglite/packages/pglite/dist/pglite.wasm +0 -0
  130. package/runtime/engine/frozen/runtime/pglite/packages/pglite/dist/pgrowlocks.tar.gz +0 -0
  131. package/runtime/engine/frozen/runtime/pglite/packages/pglite/dist/pgstattuple.tar.gz +0 -0
  132. package/runtime/engine/frozen/runtime/pglite/packages/pglite/dist/postgres_fdw.tar.gz +0 -0
  133. package/runtime/engine/frozen/runtime/pglite/packages/pglite/dist/seg.tar.gz +0 -0
  134. package/runtime/engine/frozen/runtime/pglite/packages/pglite/dist/spi.tar.gz +0 -0
  135. package/runtime/engine/frozen/runtime/pglite/packages/pglite/dist/tablefunc.tar.gz +0 -0
  136. package/runtime/engine/frozen/runtime/pglite/packages/pglite/dist/tcn.tar.gz +0 -0
  137. package/runtime/engine/frozen/runtime/pglite/packages/pglite/dist/templating.js +2 -0
  138. package/runtime/engine/frozen/runtime/pglite/packages/pglite/dist/test_decoding.tar.gz +0 -0
  139. package/runtime/engine/frozen/runtime/pglite/packages/pglite/dist/tsm_system_rows.tar.gz +0 -0
  140. package/runtime/engine/frozen/runtime/pglite/packages/pglite/dist/tsm_system_time.tar.gz +0 -0
  141. package/runtime/engine/frozen/runtime/pglite/packages/pglite/dist/unaccent.tar.gz +0 -0
  142. package/runtime/engine/frozen/runtime/pglite/packages/pglite/dist/uuid-ossp.tar.gz +0 -0
  143. package/runtime/engine/frozen/runtime/pglite/packages/pglite/dist/vacuumlo.tar.gz +0 -0
  144. package/runtime/engine/frozen/runtime/pglite/packages/pglite/dist/worker/index.js +2 -0
  145. package/runtime/engine/frozen/runtime/pglite/packages/pglite/dist/xml2.tar.gz +0 -0
  146. package/runtime/race/pin.mjs +12 -0
  147. package/runtime/runtime-manifest.json +579 -0
  148. package/runtime/wait/initial-image.tgz +0 -0
  149. package/runtime/wait/wire.mjs +39 -0
  150. package/src/browser-platform.mjs +12 -0
  151. package/src/browser.d.ts +5 -0
  152. package/src/browser.mjs +43 -0
  153. package/src/index.d.ts +45 -0
  154. package/src/index.mjs +22 -0
  155. package/src/runtime-integrity.mjs +46 -0
Binary file
@@ -0,0 +1,39 @@
1
+ import assert from 'node:assert/strict';
2
+ export function simpleQuery(sql) {
3
+ const text=Buffer.from(sql+'\0');
4
+ const bytes=Buffer.alloc(text.length+5);
5
+ bytes[0]=81; bytes.writeInt32BE(text.length+4,1);text.copy(bytes,5);
6
+ return bytes;
7
+ }
8
+ export function startup(application_name='attach-B') {
9
+ const text=Buffer.from(`user\0postgres\0database\0postgres\0application_name\0${application_name}\0client_encoding\0UTF8\0\0`);
10
+ const bytes=Buffer.alloc(text.length+8);
11
+ bytes.writeInt32BE(bytes.length,0);bytes.writeInt32BE(196608,4);text.copy(bytes,8);
12
+ return bytes;
13
+ }
14
+ export function parseWire(input) {
15
+ const b=Buffer.from(input),results=[],notices=[],notifications=[];
16
+ let current={fields:[],rows:[]},status=null;
17
+ for(let o=0;o<b.length;) {
18
+ assert(o+5<=b.length,'truncated wire header');
19
+ const type=String.fromCharCode(b[o]),len=b.readInt32BE(o+1),end=o+1+len;
20
+ assert(len>=4 && end<=b.length,'truncated wire body');
21
+ let p=o+5;
22
+ const str=()=>{let q=b.indexOf(0,p);assert(q>=p&&q<end);let s=b.toString('utf8',p,q);p=q+1;return s;};
23
+ if(type==='T') {
24
+ const n=b.readInt16BE(p);p+=2;current={fields:[],rows:[]};
25
+ for(let i=0;i<n;i++){const name=str();p+=6;const oid=b.readInt32BE(p);p+=12;current.fields.push({name,oid});}
26
+ } else if(type==='D') {
27
+ const n=b.readInt16BE(p);p+=2;const row=[];
28
+ for(let i=0;i<n;i++){const size=b.readInt32BE(p);p+=4;row.push(size===-1?null:b.toString('utf8',p,p+size));if(size>=0)p+=size;}current.rows.push(row);
29
+ } else if(type==='C') {current.tag=str();results.push(current);current={fields:[],rows:[]};}
30
+ else if(type==='E'||type==='N') {
31
+ const fields={};while(p<end&&b[p]){const key=String.fromCharCode(b[p++]);fields[key]=str();}
32
+ const err={code:fields.C,message:fields.M};
33
+ if(type==='E') {results.push({error:err});current={fields:[],rows:[]};}else notices.push(err);
34
+ } else if(type==='Z')status=String.fromCharCode(b[p]);
35
+ else if(type==='A'){const pid=b.readInt32BE(p);p+=4;notifications.push({pid,channel:str(),payload:str()});}
36
+ o=end;
37
+ }
38
+ return {results,status,notices,notifications};
39
+ }
@@ -0,0 +1,12 @@
1
+ let layout;
2
+ export function setLayout(text) { layout = text; }
3
+ export const filesystem = {
4
+ readFileSync(url) {
5
+ if (!String(url).endsWith('/engine-logs/static-layout.json') || layout === undefined) throw new Error('Browser runtime attempted unsupported filesystem access');
6
+ return layout;
7
+ },
8
+ appendFileSync() { throw new Error('Filesystem logging is unavailable in Chrome'); }
9
+ };
10
+ export function assertion(value, message = 'Engine assertion failed') { if (!value) throw new Error(message); }
11
+ assertion.equal = (actual, expected, message) => assertion(actual === expected, message ?? `Engine assertion failed: ${String(actual)} !== ${String(expected)}`);
12
+ assertion.notEqual = (actual, expected, message) => assertion(actual !== expected, message ?? 'Engine assertion failed: values must differ');
@@ -0,0 +1,5 @@
1
+ import type { RuntimeManifest } from './index.js';
2
+ export { createHost, parseWire, simpleQuery, startup } from './index.js';
3
+ export type { EngineHost, HostOptions, WaitEvent, WireError, WireResult, WireResponse, RuntimeFile, RuntimeManifest } from './index.js';
4
+ export function verifyRuntime(): Promise<RuntimeManifest>;
5
+ export function loadExtensions(names?: string[]): Promise<Record<string, unknown>>;
@@ -0,0 +1,43 @@
1
+ import { createHost as open } from '../runtime/engine/frozen/runtime/engine-host.mjs';
2
+ import { parseWire, simpleQuery, startup } from '../runtime/wait/wire.mjs';
3
+ import { setLayout } from './browser-platform.mjs';
4
+ export { parseWire, simpleQuery, startup };
5
+ const root = new URL('../runtime/', import.meta.url);
6
+ const digest = async bytes => [...new Uint8Array(await crypto.subtle.digest('SHA-256', bytes))].map(b => b.toString(16).padStart(2, '0')).join('');
7
+ async function get(name) {
8
+ const response = await fetch(new URL(name, root));
9
+ if (!response.ok) throw new Error(`Cannot load Sessions runtime asset (${response.status}): ${name}`);
10
+ return new Uint8Array(await response.arrayBuffer());
11
+ }
12
+ export async function verifyRuntime() {
13
+ const manifest = JSON.parse(new TextDecoder().decode(await get('runtime-manifest.json')));
14
+ if (manifest.format !== 1 || manifest.engineVersion !== '18.6' || !Array.isArray(manifest.files) || manifest.files.length === 0 || !/^[a-f0-9]{64}$/.test(manifest.upstreamRuntimeSha256)) throw new Error('Unsupported runtime manifest');
15
+ const paths = new Set();
16
+ for (const file of manifest.files) {
17
+ if (!file.path || file.path.includes('\\') || file.path.startsWith('/') || file.path.split('/').some(p => !p || p === '.' || p === '..') || paths.has(file.path)) throw new Error('Invalid runtime path');
18
+ if (!/^[a-f0-9]{64}$/.test(file.sha256) || !Number.isSafeInteger(file.bytes) || file.bytes < 0) throw new Error('Invalid runtime file manifest');
19
+ paths.add(file.path);
20
+ const bytes = await get(file.path);
21
+ if (bytes.byteLength !== file.bytes || await digest(bytes) !== file.sha256) throw new Error(`Runtime hash mismatch: ${file.path}`);
22
+ }
23
+ for (const key of ['entry','image','wire','pin','layout']) if (!paths.has(manifest[key])) throw new Error(`Runtime missing ${key}`);
24
+ if (await digest(new TextEncoder().encode(manifest.files.map(f => `${f.path}:${f.sha256}`).join('\n'))) !== manifest.runtimeSha256) throw new Error('Runtime identity mismatch');
25
+ return manifest;
26
+ }
27
+ export async function createHost(options = {}) {
28
+ if (typeof WorkerGlobalScope === 'undefined' || !(self instanceof WorkerGlobalScope) || !/Chrome\//.test(navigator.userAgent) || /Edg\//.test(navigator.userAgent) || typeof WebAssembly.Suspending !== 'function') throw new Error('VibeRaven Sessions browser preview requires a Chrome Worker with JSPI enabled. Node users need Node 24+ and --experimental-wasm-jspi.');
29
+ const manifest = await verifyRuntime();
30
+ setLayout(new TextDecoder().decode(await get(manifest.layout)));
31
+ return open({ ...options, loadDataDir: options.loadDataDir ?? new Blob([await get(manifest.image)]) });
32
+ }
33
+
34
+ export async function loadExtensions(names = []) {
35
+ const manifest = await verifyRuntime(), extensions = {};
36
+ for (const name of names) {
37
+ if (!/^[a-z_]+$/.test(name)) throw new Error('Invalid extension name');
38
+ const extension = await import(new URL(manifest.dist + '/contrib/' + name + '.js', root).href);
39
+ if (!extension[name]) throw new Error('No bundled extension: ' + name);
40
+ extensions[name] = extension[name];
41
+ }
42
+ return extensions;
43
+ }
package/src/index.d.ts ADDED
@@ -0,0 +1,45 @@
1
+ export interface WireError { code: string; message: string }
2
+ export interface WireResult { fields?: Array<{ name: string; oid: number }>; rows?: Array<Array<string | null>>; tag?: string; error?: WireError }
3
+ export interface WireResponse { results: WireResult[]; status: string | null; notices: WireError[]; notifications: Array<{ pid: number; channel: string; payload: string }> }
4
+ export interface WaitEvent { pid: number; latch: number; timeout: number; cooperative?: boolean; [key: string]: unknown }
5
+ export interface HostOptions {
6
+ children?: number[];
7
+ loadDataDir?: Blob;
8
+ debug?: number;
9
+ extensions?: Record<string, unknown>;
10
+ extraStartParams?: string[];
11
+ postgresqlconf?: string[];
12
+ onStdout?: (text: string) => void;
13
+ onStderr?: (text: string) => void;
14
+ [key: string]: unknown;
15
+ }
16
+ export interface RuntimeFile { path: string; bytes: number; sha256: string }
17
+ export interface RuntimeManifest {
18
+ format: 1; engineVersion: string; upstreamRuntimeSha256: string; runtimeSha256: string;
19
+ entry: string; image: string; wire: string; pin: string; layout: string; dist: string; files: RuntimeFile[];
20
+ }
21
+ export interface EngineHost {
22
+ query(pid: number, sql: string): Promise<WireResponse>;
23
+ raw(pid: number, input: Uint8Array, onData?: (bytes: Uint8Array) => void): Promise<Uint8Array>;
24
+ attach(pid: number, startupPacket?: Uint8Array): Promise<Uint8Array>;
25
+ disconnect(pid: number): Promise<void>;
26
+ waitUntilParked(pid: number): Promise<WaitEvent>;
27
+ close(): Promise<void>;
28
+ signal(pid: number, signal: number): void;
29
+ parked: Map<number, WaitEvent>;
30
+ backends: Map<number, unknown>;
31
+ log: Array<Record<string, unknown>>;
32
+ waits: WaitEvent[];
33
+ failure: Error | null;
34
+ failureNotification: Promise<Error>;
35
+ onWait?: (event: WaitEvent) => void;
36
+ onProtocol?: (event: { pid: number; bytes: Uint8Array }) => void;
37
+ onBackendExit?: (event: { pid: number; status: number }) => void;
38
+ }
39
+ export function createHost(options?: HostOptions): Promise<EngineHost>;
40
+ export function parseWire(bytes: Uint8Array): WireResponse;
41
+ export function simpleQuery(sql: string): Uint8Array;
42
+ export function startup(applicationName?: string): Uint8Array;
43
+ /** Node verifies synchronously; the Chrome entry returns a Promise. */
44
+ export function verifyRuntime(directory?: string): RuntimeManifest;
45
+ export function loadExtensions(names?: string[]): Promise<Record<string, unknown>>;
package/src/index.mjs ADDED
@@ -0,0 +1,22 @@
1
+ import { readFileSync } from 'node:fs';
2
+ import { pathToFileURL } from 'node:url';
3
+ import { join } from 'node:path';
4
+ import { runtimeRoot, verifyRuntime } from './runtime-integrity.mjs';
5
+ export { verifyRuntime } from './runtime-integrity.mjs';
6
+ export { parseWire, simpleQuery, startup } from '../runtime/wait/wire.mjs';
7
+ export async function createHost(options = {}) {
8
+ if (typeof process === 'undefined' || !process.versions?.node || process.versions.bun || typeof globalThis.Deno !== 'undefined' || Number(process.versions.node.split('.')[0]) < 24 || typeof WebAssembly.Suspending !== 'function') throw new Error('VibeRaven Sessions requires Node 24+ with JSPI. Run: node --experimental-wasm-jspi <script.mjs>. Chrome with JSPI is supported through @viberaven/sessions/browser.');
9
+ const manifest = verifyRuntime();
10
+ const { createHost: open } = await import(pathToFileURL(join(runtimeRoot, manifest.entry)).href);
11
+ return open({ ...options, loadDataDir: options.loadDataDir ?? new Blob([readFileSync(join(runtimeRoot, manifest.image))]) });
12
+ }
13
+ export async function loadExtensions(names = []) {
14
+ const manifest = verifyRuntime(), extensions = {};
15
+ for (const name of names) {
16
+ if (!/^[a-z_]+$/.test(name)) throw new Error('Invalid extension name');
17
+ const extension = await import(pathToFileURL(join(runtimeRoot, manifest.dist, 'contrib', `${name}.js`)).href);
18
+ if (!extension[name]) throw new Error(`No bundled extension: ${name}`);
19
+ extensions[name] = extension[name];
20
+ }
21
+ return extensions;
22
+ }
@@ -0,0 +1,46 @@
1
+ import { createHash } from 'node:crypto';
2
+ import { readFileSync, readdirSync, lstatSync, realpathSync } from 'node:fs';
3
+ import { resolve, join, relative, isAbsolute } from 'node:path';
4
+ import { fileURLToPath } from 'node:url';
5
+ const sha = bytes => createHash('sha256').update(bytes).digest('hex');
6
+ export const runtimeRoot = fileURLToPath(new URL('../runtime/', import.meta.url));
7
+ function inside(root, name) {
8
+ if (typeof name !== 'string' || !name || name.includes('\\') || isAbsolute(name) || name.split('/').some(p => !p || p === '.' || p === '..')) throw new Error('Invalid runtime path');
9
+ const target = resolve(root, name), rel = relative(root, target);
10
+ if (!rel || rel.startsWith('..') || isAbsolute(rel)) throw new Error('Runtime path escapes slot');
11
+ return target;
12
+ }
13
+ export function verifyRuntime(directory = runtimeRoot) {
14
+ const root = realpathSync(directory);
15
+ const manifest = JSON.parse(readFileSync(join(root, 'runtime-manifest.json'), 'utf8'));
16
+ if (manifest.format !== 1 || manifest.engineVersion !== '18.6' || !Array.isArray(manifest.files) || manifest.files.length === 0) throw new Error('Unsupported runtime manifest');
17
+ if (!/^[a-f0-9]{64}$/.test(manifest.upstreamRuntimeSha256)) throw new Error('Missing pinned upstream runtime identity');
18
+ const listed = new Set();
19
+ for (const file of manifest.files) {
20
+ const target = inside(root, file.path);
21
+ if (listed.has(file.path) || !/^[a-f0-9]{64}$/.test(file.sha256) || !Number.isSafeInteger(file.bytes) || file.bytes < 0) throw new Error('Invalid runtime file manifest');
22
+ listed.add(file.path);
23
+ const segments = file.path.split('/');
24
+ for (let i = 1; i <= segments.length; i++) if (lstatSync(join(root, ...segments.slice(0, i))).isSymbolicLink()) throw new Error('Runtime symlinks are not allowed');
25
+ const stat = lstatSync(target);
26
+ if (!stat.isFile() || stat.size !== file.bytes || sha(readFileSync(target)) !== file.sha256) throw new Error(`Runtime hash mismatch: ${file.path}`);
27
+ }
28
+ const identity = sha(manifest.files.map(f => `${f.path}:${f.sha256}`).join('\n'));
29
+ if (manifest.runtimeSha256 !== identity) throw new Error('Runtime identity mismatch');
30
+ for (const name of ['entry', 'image', 'wire', 'pin', 'layout']) if (!listed.has(manifest[name])) throw new Error(`Runtime missing ${name}`);
31
+ inside(root, manifest.dist);
32
+ function walk(dir) {
33
+ for (const item of readdirSync(dir, { withFileTypes: true })) {
34
+ const target = join(dir, item.name), name = relative(root, target).replaceAll('\\', '/');
35
+ if (item.isSymbolicLink()) throw new Error('Runtime symlinks are not allowed');
36
+ if (item.isDirectory()) walk(target);
37
+ else if (!item.isFile() || name !== 'runtime-manifest.json' && !listed.has(name)) throw new Error(`Unlisted runtime file: ${name}`);
38
+ }
39
+ }
40
+ walk(root);
41
+ return manifest;
42
+ }
43
+ if (process.argv[1] && resolve(process.argv[1]) === fileURLToPath(import.meta.url)) {
44
+ try { const m = verifyRuntime(process.argv[2]); console.log(`Runtime verified: ${m.files.length} unchanged files; slot ${m.runtimeSha256}; upstream ${m.upstreamRuntimeSha256}`); }
45
+ catch (e) { console.error(e.message); process.exitCode = 1; }
46
+ }