@amalgm/live 0.2.81-preview.47277674832 → 0.2.81-preview.47301712017

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.
@@ -1,3 +1,4 @@
1
+ import type { UnitPlace } from '../entities/units.js';
1
2
  import type { GraphPageRef } from '../entities/graph-pages.js';
2
3
  import type { OutboxRow } from './outbox.js';
3
4
  import type { AcceptedEntityRecord, EntityRecordInlineCargo, LocalEntityRecord } from './contracts.js';
@@ -45,6 +46,7 @@ export interface EntityRecordStorePort {
45
46
  countPending(through?: number): MaybePromise<number>;
46
47
  durableThrough(localSequence: number): MaybePromise<void>;
47
48
  readPackagePage(ref: GraphPageRef): MaybePromise<readonly unknown[]>;
49
+ readPlace(uuid: string): MaybePromise<UnitPlace | null>;
48
50
  stampAccepted(stamps: readonly EntityRecordStamp[]): MaybePromise<void>;
49
51
  readReceipts(authorityId: string, through: number): MaybePromise<EntityRecordReceipts>;
50
52
  readDeliveryCursor(authorityId: string): MaybePromise<number | null>;
@@ -5,7 +5,8 @@ export declare class EntityRecordRail {
5
5
  constructor(options: EntityRecordRailOptions);
6
6
  start(): Promise<void>;
7
7
  get sendPaused(): EntityRecordRefusedError | null;
8
- get sendHeld(): EntityRecordRefusedError | null;
8
+ get sendHeld(): unknown;
9
+ sendWaits(entityId: string): Promise<unknown>;
9
10
  localRecordsAvailable(): void;
10
11
  hold(entityId: string): void;
11
12
  release(entityId: string): void;
@@ -1 +1 @@
1
- var u=Object.defineProperty;var h=(o,t)=>u(o,"name",{value:t,configurable:!0});import{channelRecords as l}from"./contracts.js";import{ReceiveLoop as y}from"./receive-loop.js";import{SendLine as w}from"./send-line.js";import{UnsentCount as f}from"./unsent-count.js";class v{static{h(this,"EntityRecordRail")}#h;#n;#o;#d;#c;#e;#t;#s;#r=!1;#i=null;constructor(t){if(!t.authorityId.trim())throw new Error("entity record authority is required");this.#h=t.authorityId,this.#n=t.store,this.#o=t.schedule,this.#d=t.onOutboxAccepted??(()=>{}),this.#c=t.onBackgroundError??(()=>{});const r=h(e=>this.#a(e),"onError");this.#e=new y({authorityId:t.authorityId,store:t.store,authority:t.authority,schedule:t.schedule,channels:t.channels??[],onInboxCommitted:t.onInboxCommitted??(()=>{}),onError:r}),this.#t=new w({store:t.store,cargo:t.cargo,authority:t.authority,schedule:t.schedule,onStamped:h(e=>this.#u(e),"onStamped"),onError:r});const i=t.onUnsent;this.#s=i?new f({read:h(()=>t.store.countPending(),"read"),say:i,onError:r,schedule:t.schedule}):null}async start(){if(this.#r)throw new Error("entity record rail already started");if(this.#i)throw new Error("entity record rail is stopped");this.#r=!0,this.#s?.changed(),this.#e.start(),await this.#t.start()}get sendPaused(){return this.#t.paused}get sendHeld(){return this.#t.held}localRecordsAvailable(){this.#s?.changed(),this.#t.wake()}hold(t){this.#e.hold(t)}release(t){this.#e.release(t)}async flush(){if(!this.#r)throw new Error("entity record rail has not started");if(this.#i)throw new Error("entity record rail is stopped");await this.#e.coverReceipts(await this.#t.settle())}async receiveThrough(t){if(!this.#r)throw new Error("entity record rail has not started");await this.#e.receiveThrough(t)}async receiveCurrent(){if(!this.#r)throw new Error("entity record rail has not started");await this.#e.receiveCurrent()}async close(t,r){if(!Number.isFinite(t)||t<0)throw new Error("entity record close budget must be a non-negative number of milliseconds");const i=this.#t.stamped;if(this.#r&&!this.#i&&t>0){let s=h(()=>{},"expire");const n=new Promise(a=>{s=a}),c=this.#o.schedule(t,()=>s());try{const a=this.#t.settle();await Promise.race([a.catch(d=>{!this.#i&&d!==this.#t.paused&&this.#a(d)}),n])}finally{c.cancel()}}const e=this.stop();return await r(),await e,{accepted:this.#t.stamped-i,pending:await this.#n.countPending(),unconfirmed:await this.#n.countPending(this.#t.uncertainThrough)}}stop(){return this.#i??=Promise.all([this.#e.stop(),this.#t.stop(),this.#s?.stop()]).then(()=>{}),this.#i}#u(t){this.#s?.changed();const r=new Set;let i=!1;for(const{row:e,accepted:s}of t){for(const{entityId:n}of l(e.record))r.add(n),s.authorityId!==this.#h&&this.#e.ring({authorityId:s.authorityId,entityId:n});s.authorityId===this.#h&&(i=!0)}i&&this.#e.ring({authorityId:this.#h,entityId:null});try{this.#d([...r])}catch(e){this.#a(e)}}#a(t){try{this.#c(t)}catch{}}}export{v as EntityRecordRail};
1
+ var u=Object.defineProperty;var n=(o,t)=>u(o,"name",{value:t,configurable:!0});import{channelRecords as l}from"./contracts.js";import{ReceiveLoop as y}from"./receive-loop.js";import{SendLine as w}from"./send-line.js";import{UnsentCount as f}from"./unsent-count.js";class v{static{n(this,"EntityRecordRail")}#n;#h;#o;#d;#c;#e;#t;#s;#r=!1;#i=null;constructor(t){if(!t.authorityId.trim())throw new Error("entity record authority is required");this.#n=t.authorityId,this.#h=t.store,this.#o=t.schedule,this.#d=t.onOutboxAccepted??(()=>{}),this.#c=t.onBackgroundError??(()=>{});const r=n(e=>this.#a(e),"onError");this.#e=new y({authorityId:t.authorityId,store:t.store,authority:t.authority,schedule:t.schedule,channels:t.channels??[],onInboxCommitted:t.onInboxCommitted??(()=>{}),onError:r}),this.#t=new w({store:t.store,cargo:t.cargo,authority:t.authority,schedule:t.schedule,onStamped:n(e=>this.#u(e),"onStamped"),onError:r});const i=t.onUnsent;this.#s=i?new f({read:n(()=>t.store.countPending(),"read"),say:i,onError:r,schedule:t.schedule}):null}async start(){if(this.#r)throw new Error("entity record rail already started");if(this.#i)throw new Error("entity record rail is stopped");this.#r=!0,this.#s?.changed(),this.#e.start(),await this.#t.start()}get sendPaused(){return this.#t.paused}get sendHeld(){return this.#t.held}sendWaits(t){return this.#t.waits(t)}localRecordsAvailable(){this.#s?.changed(),this.#t.wake()}hold(t){this.#e.hold(t)}release(t){this.#e.release(t)}async flush(){if(!this.#r)throw new Error("entity record rail has not started");if(this.#i)throw new Error("entity record rail is stopped");await this.#e.coverReceipts(await this.#t.settle())}async receiveThrough(t){if(!this.#r)throw new Error("entity record rail has not started");await this.#e.receiveThrough(t)}async receiveCurrent(){if(!this.#r)throw new Error("entity record rail has not started");await this.#e.receiveCurrent()}async close(t,r){if(!Number.isFinite(t)||t<0)throw new Error("entity record close budget must be a non-negative number of milliseconds");const i=this.#t.stamped;if(this.#r&&!this.#i&&t>0){let s=n(()=>{},"expire");const h=new Promise(a=>{s=a}),c=this.#o.schedule(t,()=>s());try{const a=this.#t.settle();await Promise.race([a.catch(d=>{!this.#i&&d!==this.#t.paused&&this.#a(d)}),h])}finally{c.cancel()}}const e=this.stop();return await r(),await e,{accepted:this.#t.stamped-i,pending:await this.#h.countPending(),unconfirmed:await this.#h.countPending(this.#t.uncertainThrough)}}stop(){return this.#i??=Promise.all([this.#e.stop(),this.#t.stop(),this.#s?.stop()]).then(()=>{}),this.#i}#u(t){this.#s?.changed();const r=new Set;let i=!1;for(const{row:e,accepted:s}of t){for(const{entityId:h}of l(e.record))r.add(h),s.authorityId!==this.#n&&this.#e.ring({authorityId:s.authorityId,entityId:h});s.authorityId===this.#n&&(i=!0)}i&&this.#e.ring({authorityId:this.#n,entityId:null});try{this.#d([...r])}catch(e){this.#a(e)}}#a(t){try{this.#c(t)}catch{}}}export{v as EntityRecordRail};
@@ -0,0 +1,25 @@
1
+ import type { GraphPageRef } from '../entities/graph-pages.js';
2
+ import type { UnitPlace } from '../entities/units.js';
3
+ import type { MaybePromise, PendingEntityRecord } from './rail-ports.js';
4
+ import { type WaitingChange } from './waits.js';
5
+ export interface AheadScanInput {
6
+ readonly waiting: ReadonlyMap<number, unknown>;
7
+ readonly pause: {
8
+ readonly row: PendingEntityRecord;
9
+ readonly reached: boolean;
10
+ } | null;
11
+ readonly readPage: (ref: GraphPageRef) => MaybePromise<readonly unknown[]>;
12
+ readonly readPlace: (uuid: string) => MaybePromise<UnitPlace | null>;
13
+ }
14
+ export declare class AheadScan {
15
+ #private;
16
+ constructor(input: AheadScanInput);
17
+ get after(): number;
18
+ get reach(): number | null;
19
+ get ready(): readonly PendingEntityRecord[];
20
+ get wanting(): boolean;
21
+ behind(change: WaitingChange): Promise<boolean>;
22
+ judge(rows: readonly PendingEntityRecord[]): Promise<void>;
23
+ stamped(rows: readonly PendingEntityRecord[]): void;
24
+ stepAside(refused: PendingEntityRecord): Promise<void>;
25
+ }
@@ -0,0 +1 @@
1
+ var l=Object.defineProperty;var o=(c,e)=>l(c,"name",{value:e,configurable:!0});import{channelRecords as h}from"./contracts.js";import{freesStorage as u}from"./send-batch.js";import{ENTITY_RECORD_LOOKAHEAD_ROWS as f}from"./send-preparation.js";import{Waits as d}from"./waits.js";class q{static{o(this,"AheadScan")}#t;#s;#e=[];#a=0;#i=null;#r=new Set;constructor(e){this.#t=e,this.#s=new d(e.readPlace)}get after(){return this.#a}get reach(){return this.#i}get ready(){return this.#e}get wanting(){return this.#e.length<f}behind(e){return this.#s.behind(e)}async judge(e){for(const t of e){if(!this.wanting)return;await this.#n(t),this.#a=t.localSequence}}stamped(e){const t=new Set(e.map(s=>s.localSequence));this.#e.splice(0,this.#e.length,...this.#e.filter(s=>!t.has(s.localSequence)))}async stepAside(e){const t=e.localSequence,s=this.#e.filter(a=>a.localSequence>t);this.#e.splice(0,this.#e.length,...this.#e.filter(a=>a.localSequence<t)),await this.#c(e);for(const a of s)await this.#n(a)}async#n(e){const{pause:t,waiting:s}=this.#t;if(t?.row.localSequence!==e.localSequence&&!s.has(e.localSequence)&&this.#o(e)){if(!await this.#h(e)){this.#e.push(e);return}t&&!t.reached&&this.#r.has(e.record.entityId)&&(this.#i??=e.localSequence)}await this.#c(e)}#o(e){const{pause:t}=this.#t;return t===null||e.record.authorityId===t.row.record.authorityId&&u(e)}async#h(e){for(const{entityId:t,change:s}of h(e.record))if(await this.#s.behind({entityId:t,facets:s.facets,result:s.result}))return!0;return!1}async#c(e){const t=this.#t.pause?.row.localSequence===e.localSequence;for(const{entityId:s,change:a}of h(e.record)){const i=[];if(a.replay.kind==="package")for(const r of a.replay.pages)for(const n of await this.#t.readPage(r))i.push(n);if(await this.#s.hold({entityId:s,facets:a.facets,result:a.result},i),t)for(const r of[s,...i.map(({uuid:n})=>n)])this.#r.add(r)}}}export{q as AheadScan};
@@ -1 +1 @@
1
- var E=Object.defineProperty;var a=(r,e)=>E(r,"name",{value:e,configurable:!0});import{failure as y,recoveryTrigger as R}from"../faults/need.js";import{asRefusal as w}from"../detection/refusal.js";import{channelRecords as b}from"./contracts.js";import{acceptOutboxRow as m,isOwnRecord as I}from"./outbox.js";const S=256,T=16*1024*1024,q="entity_record_cargo_missing",x="files_over_storage_limit",O=new Set(["trash","delete"]);function C(r){const{facets:e,cargo:t}=r.record.change;return t.length===0&&e.length>0&&e.every(o=>O.has(o))}a(C,"freesStorage");function B(r,e){const t=r[0];if(!t)return[];const o=[];let n=0;for(const d of r){if(d.record.authorityId!==t.record.authorityId||o.length===S)break;const c=e(d);if(!c||o.length>0&&n+c.bytes>T)break;o.push(c),n+=c.bytes}return o}a(B,"selectBatch");const A=new Set(["entity_record_read_forbidden","entity_record_write_forbidden",x]);class D extends Error{static{a(this,"EntityRecordRefusedError")}code;mutationId;need;constructor(e,t,o=null){super(`entity record ${e} was refused: ${t.code}: ${t.message}`),this.name="EntityRecordRefusedError",this.code=t.code,this.mutationId=e;const n=A.has(t.code);this.need=n?null:{law:"record-refused",trigger:R(t.code),subject:o,lost:e},n&&w(this)}}function G(r,e){const t=r[0],o=t?{id:t.record.authorityId,type:"authority"}:null,n=a(()=>y("entity record batch answer does not match the offered batch","invariant-broken","answer-not-offered",o),"notOffered"),d=new Map,c=[];let _=0,l=0;for(;l<e.accepted.length;){const s=r[c.length];if(s===void 0)throw n();const f=b(s.record),h=e.accepted.slice(l,l+f.length);if(h.length!==f.length)throw n();m(s,h[0].record);let u=_;h.forEach(({deliverySequence:g,record:i},p)=>{if(p>0&&!I(s,i)||i.entityId!==f[p].entityId)throw n();if(g<=u&&!C(s)||i.globalSequence<=(d.get(i.entityId)??0))throw y("entity record batch answer breaks this computer's acceptance order","invariant-broken","answer-out-of-order",o,s.record.mutationId);u=Math.max(u,g),d.set(i.entityId,i.globalSequence)}),_=Math.max(_,u),c.push(Object.freeze({row:s,accepted:h[0].record,deliverySequence:h.at(-1).deliverySequence})),l+=f.length}if(e.refusal===null!=(c.length===r.length))throw n();return c}a(G,"acceptedPrefix");export{T as ENTITY_RECORD_BATCH_BYTES,S as ENTITY_RECORD_BATCH_ROWS,q as ENTITY_RECORD_CARGO_MISSING,x as ENTITY_RECORD_OVER_STORAGE_LIMIT,D as EntityRecordRefusedError,G as acceptedPrefix,C as freesStorage,B as selectBatch};
1
+ var g=Object.defineProperty;var a=(r,e)=>g(r,"name",{value:e,configurable:!0});import{failure as _,recoveryTrigger as y}from"../faults/need.js";import{asRefusal as p}from"../detection/refusal.js";import{channelRecords as E}from"./contracts.js";import{acceptOutboxRow as R,isOwnRecord as w}from"./outbox.js";const b=256,S=16*1024*1024,N="entity_record_cargo_missing",I="files_over_storage_limit",m=new Set(["trash","delete"]);function k(r){const{facets:e,cargo:t}=r.record.change;return t.length===0&&e.length>0&&e.every(o=>m.has(o))}a(k,"freesStorage");function q(r,e){const t=r[0];if(!t)return[];const o=[];let n=0;for(const d of r){if(d.record.authorityId!==t.record.authorityId||o.length===b)break;const c=e(d);if(!c||o.length>0&&n+c.bytes>S)break;o.push(c),n+=c.bytes}return o}a(q,"selectBatch");const T=new Set(["entity_record_read_forbidden","entity_record_write_forbidden",I]);class Y extends Error{static{a(this,"EntityRecordRefusedError")}code;mutationId;need;constructor(e,t,o=null){super(`entity record ${e} was refused: ${t.code}: ${t.message}`),this.name="EntityRecordRefusedError",this.code=t.code,this.mutationId=e;const n=T.has(t.code);this.need=n?null:{law:"record-refused",trigger:y(t.code),subject:o,lost:e},n&&p(this)}}function B(r,e){const t=r[0],o=t?{id:t.record.authorityId,type:"authority"}:null,n=a(()=>_("entity record batch answer does not match the offered batch","invariant-broken","answer-not-offered",o),"notOffered"),d=new Map,c=[];let l=0;for(;l<e.accepted.length;){const s=r[c.length];if(s===void 0)throw n();const f=E(s.record),h=e.accepted.slice(l,l+f.length);if(h.length!==f.length)throw n();R(s,h[0].record),h.forEach(({record:i},u)=>{if(u>0&&!w(s,i)||i.entityId!==f[u].entityId)throw n();if(i.globalSequence<=(d.get(i.entityId)??0))throw _("entity record batch answer breaks this computer's acceptance order","invariant-broken","answer-out-of-order",o,s.record.mutationId);d.set(i.entityId,i.globalSequence)}),c.push(Object.freeze({row:s,accepted:h[0].record,deliverySequence:h.at(-1).deliverySequence})),l+=f.length}if(e.refusal===null!=(c.length===r.length))throw n();return c}a(B,"acceptedPrefix");export{S as ENTITY_RECORD_BATCH_BYTES,b as ENTITY_RECORD_BATCH_ROWS,N as ENTITY_RECORD_CARGO_MISSING,I as ENTITY_RECORD_OVER_STORAGE_LIMIT,Y as EntityRecordRefusedError,B as acceptedPrefix,k as freesStorage,q as selectBatch};
@@ -14,7 +14,8 @@ export declare class SendLine {
14
14
  get stamped(): number;
15
15
  get uncertainThrough(): number | undefined;
16
16
  get paused(): EntityRecordRefusedError | null;
17
- get held(): EntityRecordRefusedError | null;
17
+ get held(): unknown;
18
+ waits(entityId: string): Promise<unknown>;
18
19
  start(): Promise<void>;
19
20
  resume(): void;
20
21
  wake(): void;
@@ -1 +1 @@
1
- var y=Object.defineProperty;var d=(p,t)=>y(p,"name",{value:t,configurable:!0});import{OwnedEffects as E}from"./owned-effects.js";import{RetryClock as b}from"./retry-clock.js";import{ENTITY_RECORD_CARGO_MISSING as I,ENTITY_RECORD_OVER_STORAGE_LIMIT as T,EntityRecordRefusedError as o,acceptedPrefix as S,selectBatch as O}from"./send-batch.js";import{FreeingScan as R}from"./send-freeing.js";import{ENTITY_RECORD_LOOKAHEAD_ROWS as w,SendPreparation as k}from"./send-preparation.js";const f=Object.freeze({progressed:!1,failure:null}),q=d(()=>new Error("entity record Send stopped before stamping the rows it owes"),"sendStopped");class C{static{d(this,"SendLine")}#t;#c;#e;#s;#i=new E(q);#I;#d;#a=null;#y=!1;#r=!1;#o=!1;#n=null;#w=null;#f=null;#p=0;#m=!1;#E=0;#T=[];#S=null;#l=null;#b=new Set;#h=0;#u=0;#O=0;#g=0;constructor(t){this.#t=t.store,this.#c=t.authority,this.#I=t.onStamped,this.#d=t.onError,this.#e=new k(t.cargo,t.onError),this.#s=new b(t.schedule)}get stamped(){return this.#O}get uncertainThrough(){return this.#y?this.#h:void 0}get paused(){return this.#n}get held(){return this.#w}start(){if(!this.#a){const t=d(()=>this.resume(),"resume");this.#T=[this.#c.onStorageChanged(t),this.#c.onDisconnect(t)]}return this.#a??=this.#i.run(()=>this.#t.readLocalSequence()).then(t=>{this.#h=this.#u=t,this.#y=!0,this.wake()}),this.#a}resume(){this.#E+=1,this.#m=!1,this.#n&&(this.#n=null,this.#f=null,this.wake())}wake(){this.#r||(this.#o=!0,this.#R())}async settle(){if(!this.#a)throw new Error("entity record Send has not started");const t=this.#i.run(()=>this.#t.readLocalSequence()),e=t.catch(()=>0);this.#b.add(e);try{const[,h]=await Promise.all([this.#a,t]);let r=null;for(;this.#g<h;){const[i]=await this.#i.run(()=>this.#t.readPending(1));if(!i||i.localSequence>h)break;if(!(i.localSequence<=this.#g)){if(r)throw r.error;if(this.#n)throw this.#n;({failure:r}=await(this.#l??this.#k()))}}return h}finally{this.#b.delete(e),this.wake()}}async stop(){this.#r=!0,this.#s.cancel();for(const t of this.#T)t();await Promise.allSettled([this.#a,this.#S,this.#l,this.#e.stop(),this.#i.stop()])}#R(){if(!this.#y||this.#r||this.#s.armed||this.#S)return;const t=this.#_();this.#S=t,t.then(()=>{this.#S=null,this.#o&&this.#R()})}async#_(){for(;!this.#r&&this.#o&&!this.#s.armed;){this.#o=!1;const t=await(this.#l??this.#k());if(t.failure)return;t.progressed&&(this.#o=!0)}}#k(){this.#s.cancel();const t=this.#f?this.#L(this.#f):this.#P();return this.#l=t,t.then(()=>{this.#l===t&&(this.#l=null)}),t}async#P(){let t=[],e=0;const h=this.#E,r=this.#p;this.#p=0;try{const i=await this.#i.run(()=>this.#t.readPending(w));i.length===0&&(this.#w=null);const a=await this.#N(),s=i.filter(n=>n.localSequence<=a),l=this.#u;this.#e.retain(s,l);const m=s[0];if(!m||(await this.#e.head(m),r>0&&await this.#e.settledThrough(r),this.#r))return f;const g=O(s,n=>this.#e.prepared(n));await this.#i.run(()=>this.#t.durableThrough(g[g.length-1].row.localSequence)),t=g;const u=await this.#c.submit(t.map(({submission:n})=>n)),c=S(t.map(({row:n})=>n),u);if(c.length>0&&(await this.#t.stampAccepted(c),e=c.length,this.#w=null,this.#q(c,!0),this.#s.succeeded(),this.#m=!1),u.refusal){const n=t[e].row;if(n.localSequence>l||u.refusal.code!==I)throw this.#e.forget([n]),new o(n.record.mutationId,u.refusal,{id:n.record.entityId,type:n.record.change.result.type});this.#u=n.localSequence-1}return{progressed:!0,failure:null}}catch(i){if(!(i instanceof o)&&t.length>e&&(this.#h=Math.max(this.#h,t[t.length-1].row.localSequence)),this.#u=this.#h,i instanceof o?this.#w=i:this.#p||=r,!this.#r){const a=i instanceof o&&i.code===T,s=a&&this.#E===h;s&&(this.#n=i,this.#f=new R(this.#m,l=>this.#i.run(()=>this.#t.readPackagePage(l))),this.#e.retain([],this.#u)),a?this.#o=!0:this.#s.arm(()=>this.wake()),s&&r>0||this.#d(i)}return{progressed:e>0,failure:{error:i}}}}async#L(t){let e=[],h=0;try{for(;t.wanting;){const s=await this.#i.run(()=>this.#t.readPending(w,t.after));if(await t.judge(s),s.length<w)break}if(t.reach!==null)return this.#n=null,this.#f=null,this.#p=t.reach,this.#m=!0,{progressed:!0,failure:null};const r=t.offer();if(r.length===0||this.#r)return f;await this.#i.run(()=>this.#t.durableThrough(r[r.length-1].row.localSequence)),e=r;const i=await this.#c.submit(e.map(({submission:s})=>s)),a=S(e.map(({row:s})=>s),i);if(a.length>0&&(await this.#t.stampAccepted(a),h=a.length,t.stamped(a),this.#q(a,!1)),i.refusal){const s=e[h].row;t.refused(),this.#d(new o(s.record.mutationId,i.refusal,{id:s.record.entityId,type:s.record.change.result.type}))}return{progressed:h>0||i.refusal!==null,failure:null}}catch(r){return e.length>h&&(this.#h=Math.max(this.#h,e[e.length-1].row.localSequence)),this.#u=this.#h,this.#r||(this.#s.arm(()=>this.wake()),this.#d(r)),f}}async#N(){for(const t of[...this.#b]){const e=await t;if(e>this.#g)return e}return Number.POSITIVE_INFINITY}#q(t,e){this.#O+=t.length,e&&(this.#g=Math.max(this.#g,t[t.length-1].row.localSequence)),this.#e.forget(t.map(({row:h})=>h));try{this.#I(t)}catch(h){this.#d(h)}}}export{C as SendLine};
1
+ var k=Object.defineProperty;var l=(c,t)=>k(c,"name",{value:t,configurable:!0});import{classifyFault as I}from"../faults/classify.js";import{OwnedEffects as O}from"./owned-effects.js";import{RetryClock as S}from"./retry-clock.js";import{ENTITY_RECORD_CARGO_MISSING as T,ENTITY_RECORD_OVER_STORAGE_LIMIT as E,EntityRecordRefusedError as g,acceptedPrefix as P,selectBatch as q}from"./send-batch.js";import{AheadScan as _}from"./send-ahead.js";import{ENTITY_RECORD_LOOKAHEAD_ROWS as p,SendPreparation as D}from"./send-preparation.js";const b=Object.freeze({progressed:!1,failure:null}),m=Object.freeze({progressed:!0,failure:null}),R=l(()=>new Error("entity record Send stopped before stamping the rows it owes"),"sendStopped");function A(c){if(c instanceof g)return c.code!==E;const t=I(c);return t.kind==="refusal"||t.kind==="need"&&t.need.subject?.type!=="authority"}l(A,"aboutOneRow");class x{static{l(this,"SendLine")}#e;#p;#s;#h;#m;#i=new O(R);#I;#a;#o=null;#b=!1;#l=!1;#n=!1;#r=new Map;#t=null;#c=null;#y=0;#S=!1;#R=0;#O=[];#E=null;#u=null;#k=new Set;#d=0;#f=0;#T=0;#g=0;constructor(t){this.#e=t.store,this.#p=t.authority,this.#I=t.onStamped,this.#a=t.onError,this.#s=new D(t.cargo,t.onError),this.#h=new S(t.schedule),this.#m=new S(t.schedule)}get stamped(){return this.#T}get uncertainThrough(){return this.#b?this.#d:void 0}get paused(){return this.#t?.error??null}get held(){return this.#t?this.#t.error:this.#r.size>0?this.#_():null}async waits(t){if(this.#t)return this.#t.error;const e=this.#c;if(!e)return null;const s=await this.#i.run(()=>this.#e.readPlace(t)),n={type:s?.type??"folder",parentUUID:s?.parentUUID??null,name:""};return await e.behind({entityId:t,facets:[],result:n})?this.held:null}start(){if(!this.#o){const t=l(()=>this.resume(),"resume");this.#O=[this.#p.onStorageChanged(t),this.#p.onDisconnect(t)]}return this.#o??=this.#i.run(()=>this.#e.readLocalSequence()).then(t=>{this.#d=this.#f=t,this.#b=!0,this.wake()}),this.#o}resume(){this.#R+=1,this.#S=!1,this.#t&&(this.#t=null,this.#w(),this.wake())}wake(){this.#l||(this.#n=!0,this.#P())}async settle(){if(!this.#o)throw new Error("entity record Send has not started");const t=this.#i.run(()=>this.#e.readLocalSequence()),e=t.catch(()=>0);this.#k.add(e);try{const[,s]=await Promise.all([this.#o,t]);let n=null,a=!1;for(;this.#g<s;){const[o]=await this.#i.run(()=>this.#e.readPending(1));if(!o||o.localSequence>s)break;if(o.localSequence<=this.#g)continue;if(n)throw n.error;if(this.#t)throw this.#t.error;if(a)throw this.#_();const i=this.#u,h=await(i??this.#q());n=h.failure,a=!i&&!h.progressed&&this.#r.size>0}return s}finally{this.#k.delete(e),this.wake()}}async stop(){this.#l=!0,this.#h.cancel(),this.#m.cancel();for(const t of this.#O)t();await Promise.allSettled([this.#o,this.#E,this.#u,this.#s.stop(),this.#i.stop()])}#P(){if(!this.#b||this.#l||this.#h.armed||this.#E)return;const t=this.#D();this.#E=t,t.then(()=>{this.#E=null,this.#n&&this.#P()})}async#D(){for(;!this.#l&&this.#n&&!this.#h.armed;){this.#n=!1;const t=await(this.#u??this.#q());if(t.failure)return;t.progressed&&(this.#n=!0)}}#q(){this.#h.cancel();const t=this.#L();return this.#u=t,t.then(()=>{this.#u===t&&(this.#u=null)}),t}async#A(){const t=this.#c;if(!t)return this.#i.run(()=>this.#e.readPending(p));for(;t.wanting;){const e=await this.#i.run(()=>this.#e.readPending(p,t.after));if(await t.judge(e),e.length<p)break}return t!==this.#c?null:t.reach!==null?(this.#t=null,this.#y=t.reach,this.#S=!0,this.#w(),null):t.ready}async#L(){let t=[],e=0,s;const n=this.#R,a=this.#y;this.#y=0;const o=this.#c;try{const i=await this.#A();if(i===null)return m;const h=await this.#M(),u=i.filter(r=>r.localSequence<=h),y=this.#f;if(this.#s.retain(u,y),s=u[0],!s||(await this.#s.head(s),a>0&&await this.#s.settledThrough(a),this.#l))return b;const w=q(u,r=>this.#s.prepared(r));await this.#i.run(()=>this.#e.durableThrough(w[w.length-1].row.localSequence)),t=w;const f=await this.#p.submit(t.map(({submission:r})=>r)),d=P(t.map(({row:r})=>r),f);if(d.length>0&&(await this.#e.stampAccepted(d),e=d.length,o?.stamped(d.map(({row:r})=>r)),this.#j(d,o===null),this.#h.succeeded(),o===null&&this.#m.succeeded(),this.#t||(this.#S=!1)),f.refusal){const r=t[e].row;if(r.localSequence>y||f.refusal.code!==T)throw this.#s.forget([r]),s=r,new g(r.record.mutationId,f.refusal,{id:r.record.entityId,type:r.record.change.result.type});this.#f=r.localSequence-1}return m}catch(i){const h=i instanceof g;if(!h&&t.length>e&&(this.#d=Math.max(this.#d,t[t.length-1].row.localSequence)),this.#f=this.#d,h||(this.#y||=a),this.#l)return{progressed:e>0,failure:{error:i}};if(s&&(h||t.length===0)&&(A(i)||h&&this.#t))return await this.#N(s,i),this.#a(i),m;if(h&&i.code===E){const u=this.#R===n;return u&&(this.#t={error:i,row:s},this.#w(),this.#s.retain([],this.#f)),this.#n=!0,u&&a>0||this.#a(i),{progressed:e>0,failure:{error:i}}}return this.#h.arm(()=>this.wake()),this.#a(i),{progressed:e>0,failure:{error:i}}}}async#N(t,e){this.#r.set(t.localSequence,e),this.#s.forget([t]);const s=this.#c;try{s?await s.stepAside(t):this.#w()}catch(n){this.#a(n),this.#w()}this.#m.arm(()=>this.#C()),this.#n=!0}#C(){this.#r.clear(),this.#w(),this.#f=this.#d,this.wake()}#w(){const t=this.#t;this.#c=this.#r.size>0||t?new _({waiting:this.#r,pause:t&&{row:t.row,reached:this.#S},readPage:l(e=>this.#i.run(()=>this.#e.readPackagePage(e)),"readPage"),readPlace:l(e=>this.#i.run(()=>this.#e.readPlace(e)),"readPlace")}):null}#_(){return this.#r.get(Math.min(...this.#r.keys()))??R()}async#M(){for(const t of[...this.#k]){const e=await t;if(e>this.#g)return e}return Number.POSITIVE_INFINITY}#j(t,e){this.#T+=t.length,e&&(this.#g=Math.max(this.#g,t[t.length-1].row.localSequence)),this.#s.forget(t.map(({row:s})=>s));try{this.#I(t)}catch(s){this.#a(s)}}}export{x as SendLine};
@@ -1 +1 @@
1
- var l=Object.defineProperty;var h=(a,t)=>l(a,"name",{value:t,configurable:!0});import{utf8Length as p}from"../entities/bytes.js";import{encodeEntityRecord as m}from"./codec.js";import{recordCargo as c}from"./contracts.js";import{ENTITY_RECORD_BATCH_BYTES as f}from"./send-batch.js";import{INLINE_ARTIFACT_FRAME_BYTES as g,submissionBytes as E}from"./submit-frame.js";const S=512,u=f,I=4,y=Promise.resolve();class w{static{h(this,"SendPreparation")}#c;#f;#t=new Map;#e=[];#u=0;#o=0;#d=0;#r=!1;#i=new Set;constructor(t,e){this.#c=t,this.#f=e}retain(t,e){if(this.#r)return;this.#e=t,this.#u=e;const r=new Map(t.map(i=>[i.record.mutationId,i]));for(const[i,o]of this.#t){const s=r.get(i);(!s||o.carriesCargo!==this.#h(s))&&this.#s(i)}this.#n()}forget(t){if(t.length===0)return;const e=new Set(t.map(r=>r.record.mutationId));this.#e=this.#e.filter(r=>!e.has(r.record.mutationId));for(const r of e)this.#s(r);this.#n()}prepared(t){return this.#t.get(t.record.mutationId)?.ready??null}async head(t){if(t!==this.#e[0])throw new Error("only the lookahead head is awaited");this.#t.get(t.record.mutationId)?.failure&&this.#s(t.record.mutationId),this.#n();let e=this.#t.get(t.record.mutationId);for(;!e;){if(this.#r)throw new Error("entity record preparation stopped");if(this.#i.size===0)throw new Error(`entity record ${t.record.mutationId} exceeds the prepared byte bound`);await Promise.race(this.#i),e=this.#t.get(t.record.mutationId)}if(await e.settled,e.failure)throw e.failure.error;return e.ready}async settledThrough(t){for(;;){const e=[...this.#t.values()].filter(({row:r,ready:i,failure:o})=>r.localSequence<=t&&!i&&!o);if(e.length===0)return;await Promise.race(e.map(({settled:r})=>r))}}stop(){this.#r=!0,this.#e=[];for(const t of[...this.#t.keys()])this.#s(t);return Promise.allSettled([...this.#i]).then(()=>{})}#n(){if(!this.#r)for(const[t,e]of this.#e.entries()){if(this.#t.has(e.record.mutationId))continue;const r=this.#h(e)?c(e.record).length*g:0;if(r>0&&this.#d>=I)return;const i=m(e.record),o=p(i),s=o+Math.min(r,Math.max(0,f-o));if(t===0&&this.#l(s),this.#o+s>u)return;this.#p(e,i,s)}}#l(t){for(let e=this.#e.length-1;e>0&&this.#o+t>u;e-=1){const r=this.#e[e].record.mutationId;this.#t.get(r)?.ready&&this.#s(r)}}#h(t){return t.localSequence>this.#u&&c(t.record).length>0}#p(t,e,r){if(this.#o+=r,!this.#h(t)){const s=Object.freeze({recordJson:e,cargo:Object.freeze([])});this.#t.set(t.record.mutationId,{row:t,carriesCargo:!1,reserved:r,ready:Object.freeze({row:t,submission:s,bytes:r}),failure:null,settled:y,dropped:!1});return}this.#d+=1;let i;const o=Promise.resolve().then(()=>{if(this.#r)throw new Error("entity record preparation stopped");return this.#c.ensure(t.record)}).then(s=>{const d=Object.freeze({recordJson:e,cargo:Object.freeze([...s??[]])}),n=E(d);if(n>r)throw new Error(`cargo for ${t.record.mutationId} exceeds what one row may carry inline`);i.ready=Object.freeze({row:t,submission:d,bytes:n}),this.#a(i,n)}).catch(s=>{i.failure={error:s},this.#a(i,0)}).finally(()=>{this.#d-=1,this.#i.delete(o),i.dropped&&this.#a(i,0),this.#n()}).catch(s=>this.#f(s));i={row:t,carriesCargo:!0,reserved:r,ready:null,failure:null,settled:o,dropped:!1},this.#i.add(o),this.#t.set(t.record.mutationId,i)}#s(t){const e=this.#t.get(t);e&&(this.#t.delete(t),e.dropped=!0,(e.ready||e.failure)&&this.#a(e,0))}#a(t,e){const r=t.dropped?0:e;this.#o+=r-t.reserved,t.reserved=r}}export{I as ENTITY_RECORD_CARGO_READS,S as ENTITY_RECORD_LOOKAHEAD_ROWS,u as ENTITY_RECORD_PREPARED_BYTES,w as SendPreparation};
1
+ var m=Object.defineProperty;var d=(n,t)=>m(n,"name",{value:t,configurable:!0});import{utf8Length as g}from"../entities/bytes.js";import{failure as c}from"../faults/need.js";import{encodeEntityRecord as I}from"./codec.js";import{recordCargo as f}from"./contracts.js";import{ENTITY_RECORD_BATCH_BYTES as u}from"./send-batch.js";import{INLINE_ARTIFACT_FRAME_BYTES as y,submissionBytes as E}from"./submit-frame.js";const l=d(n=>({id:n.record.entityId,type:n.record.change.result.type}),"subjectOf"),z=512,p=u,b=4,R=Promise.resolve();class P{static{d(this,"SendPreparation")}#c;#f;#t=new Map;#e=[];#u=0;#s=0;#d=0;#r=!1;#i=new Set;constructor(t,e){this.#c=t,this.#f=e}retain(t,e){if(this.#r)return;this.#e=t,this.#u=e;const r=new Map(t.map(i=>[i.record.mutationId,i]));for(const[i,s]of this.#t){const o=r.get(i);(!o||s.carriesCargo!==this.#h(o))&&this.#o(i)}this.#n()}forget(t){if(t.length===0)return;const e=new Set(t.map(r=>r.record.mutationId));this.#e=this.#e.filter(r=>!e.has(r.record.mutationId));for(const r of e)this.#o(r);this.#n()}prepared(t){return this.#t.get(t.record.mutationId)?.ready??null}async head(t){if(t!==this.#e[0])throw new Error("only the lookahead head is awaited");this.#t.get(t.record.mutationId)?.failure&&this.#o(t.record.mutationId),this.#n();let e=this.#t.get(t.record.mutationId);for(;!e;){if(this.#r)throw new Error("entity record preparation stopped");if(this.#i.size===0)throw c(`entity record ${t.record.mutationId} exceeds the prepared byte bound`,"invariant-broken","row-too-large",l(t),t.record.mutationId);await Promise.race(this.#i),e=this.#t.get(t.record.mutationId)}if(await e.settled,e.failure)throw e.failure.error;return e.ready}async settledThrough(t){for(;;){const e=[...this.#t.values()].filter(({row:r,ready:i,failure:s})=>r.localSequence<=t&&!i&&!s);if(e.length===0)return;await Promise.race(e.map(({settled:r})=>r))}}stop(){this.#r=!0,this.#e=[];for(const t of[...this.#t.keys()])this.#o(t);return Promise.allSettled([...this.#i]).then(()=>{})}#n(){if(!this.#r)for(const[t,e]of this.#e.entries()){if(this.#t.has(e.record.mutationId))continue;const r=this.#h(e)?f(e.record).length*y:0;if(r>0&&this.#d>=b)return;const i=I(e.record),s=g(i),o=s+Math.min(r,Math.max(0,u-s));if(t===0&&this.#l(o),this.#s+o>p)return;this.#p(e,i,o)}}#l(t){for(let e=this.#e.length-1;e>0&&this.#s+t>p;e-=1){const r=this.#e[e].record.mutationId;this.#t.get(r)?.ready&&this.#o(r)}}#h(t){return t.localSequence>this.#u&&f(t.record).length>0}#p(t,e,r){if(this.#s+=r,!this.#h(t)){const o=Object.freeze({recordJson:e,cargo:Object.freeze([])});this.#t.set(t.record.mutationId,{row:t,carriesCargo:!1,reserved:r,ready:Object.freeze({row:t,submission:o,bytes:r}),failure:null,settled:R,dropped:!1});return}this.#d+=1;let i;const s=Promise.resolve().then(()=>{if(this.#r)throw new Error("entity record preparation stopped");return this.#c.ensure(t.record)}).then(o=>{const h=Object.freeze({recordJson:e,cargo:Object.freeze([...o??[]])}),a=E(h);if(a>r)throw c(`cargo for ${t.record.mutationId} exceeds what one row may carry inline`,"invariant-broken","cargo-too-large",l(t),t.record.mutationId);i.ready=Object.freeze({row:t,submission:h,bytes:a}),this.#a(i,a)}).catch(o=>{i.failure={error:o},this.#a(i,0)}).finally(()=>{this.#d-=1,this.#i.delete(s),i.dropped&&this.#a(i,0),this.#n()}).catch(o=>this.#f(o));i={row:t,carriesCargo:!0,reserved:r,ready:null,failure:null,settled:s,dropped:!1},this.#i.add(s),this.#t.set(t.record.mutationId,i)}#o(t){const e=this.#t.get(t);e&&(this.#t.delete(t),e.dropped=!0,(e.ready||e.failure)&&this.#a(e,0))}#a(t,e){const r=t.dropped?0:e;this.#s+=r-t.reserved,t.reserved=r}}export{b as ENTITY_RECORD_CARGO_READS,z as ENTITY_RECORD_LOOKAHEAD_ROWS,p as ENTITY_RECORD_PREPARED_BYTES,P as SendPreparation};
@@ -0,0 +1,27 @@
1
+ import type { ChangeFacet } from '../detection/record-proposal.js';
2
+ import type { EntityType } from '../entities/types.js';
3
+ import type { UnitPlace } from '../entities/units.js';
4
+ type MaybePromise<T> = T | Promise<T>;
5
+ export interface WaitingChange {
6
+ readonly entityId: string;
7
+ readonly facets: readonly ChangeFacet[];
8
+ readonly result: {
9
+ readonly type: EntityType;
10
+ readonly parentUUID: string | null;
11
+ readonly name: string;
12
+ };
13
+ }
14
+ export interface WaitingMember {
15
+ readonly uuid: string;
16
+ readonly type: EntityType;
17
+ readonly parentUUID: string | null;
18
+ readonly name: string;
19
+ }
20
+ export declare class Waits {
21
+ #private;
22
+ constructor(place: (uuid: string) => MaybePromise<UnitPlace | null>);
23
+ get empty(): boolean;
24
+ hold(change: WaitingChange, members?: readonly WaitingMember[]): Promise<void>;
25
+ behind(change: WaitingChange): Promise<boolean>;
26
+ }
27
+ export {};
@@ -0,0 +1 @@
1
+ var u=Object.defineProperty;var a=(n,t)=>u(n,"name",{value:t,configurable:!0});const l=new Set(["create","rename","move","restore"]),h=new Set(["move","restore","delete"]),o=a((n,t)=>n===null?null:`${n}\0${t}`,"slot");class f{static{a(this,"Waits")}#r;#e=new Map;#t=new Set;#i=new Set;#n=new Set;#a=new Set;#l=!1;#s=!1;constructor(t){this.#r=t}get empty(){return this.#t.size===0}async hold(t,i=[]){this.#t.add(t.entityId),this.#o(t.result.parentUUID,t.result.name,t.result.type),t.facets.includes("rename")&&t.result.parentUUID!==null&&this.#a.add(t.result.parentUUID),t.facets.includes("move")&&(this.#l=!0);for(const s of i)this.#t.add(s.uuid),this.#o(s.parentUUID,s.name,s.type);let e=t.result.type==="repo.git"?t.entityId:null;for(const s of await this.#u(t.result.parentUUID))this.#i.add(s),e===null&&(await this.#h(s))?.type==="repo.git"&&(e=s);e!==null&&(this.#t.add(e),this.#s=!0)}async behind(t){if(this.empty)return!1;const{entityId:i,facets:r,result:e}=t;if(this.#t.has(i)||r.some(s=>l.has(s))&&e.parentUUID!==null&&(this.#n.has(o(e.parentUUID,e.name))||this.#a.has(e.parentUUID))||this.#l&&r.some(s=>l.has(s)||s==="delete")||this.#s&&e.type==="repo.git"||this.#i.has(i)&&r.some(s=>h.has(s)))return!0;for(const s of await this.#u(e.parentUUID))if(this.#t.has(s))return!0;return!1}#o(t,i,r){const e=o(t,i);e!==null&&this.#n.add(e),r==="repo.git"&&(this.#s=!0)}async#u(t){const i=[];for(let r=t;r!==null&&!i.includes(r);){i.push(r);const e=await this.#h(r);if(!e)break;r=e.parentUUID}return i}async#h(t){return this.#e.has(t)||this.#e.set(t,await this.#r(t)),this.#e.get(t)}}export{f as Waits};
@@ -1 +1 @@
1
- "use strict";var u=Object.defineProperty;var n=(o,t)=>u(o,"name",{value:t,configurable:!0});Object.defineProperty(exports,"__esModule",{value:!0}),exports.EntityRecordRail=void 0;const l=require("./contracts.js"),y=require("./receive-loop.js"),w=require("./send-line.js"),f=require("./unsent-count.js");class m{static{n(this,"EntityRecordRail")}#n;#h;#o;#d;#c;#e;#t;#s;#r=!1;#i=null;constructor(t){if(!t.authorityId.trim())throw new Error("entity record authority is required");this.#n=t.authorityId,this.#h=t.store,this.#o=t.schedule,this.#d=t.onOutboxAccepted??(()=>{}),this.#c=t.onBackgroundError??(()=>{});const r=n(e=>this.#a(e),"onError");this.#e=new y.ReceiveLoop({authorityId:t.authorityId,store:t.store,authority:t.authority,schedule:t.schedule,channels:t.channels??[],onInboxCommitted:t.onInboxCommitted??(()=>{}),onError:r}),this.#t=new w.SendLine({store:t.store,cargo:t.cargo,authority:t.authority,schedule:t.schedule,onStamped:n(e=>this.#u(e),"onStamped"),onError:r});const i=t.onUnsent;this.#s=i?new f.UnsentCount({read:n(()=>t.store.countPending(),"read"),say:i,onError:r,schedule:t.schedule}):null}async start(){if(this.#r)throw new Error("entity record rail already started");if(this.#i)throw new Error("entity record rail is stopped");this.#r=!0,this.#s?.changed(),this.#e.start(),await this.#t.start()}get sendPaused(){return this.#t.paused}get sendHeld(){return this.#t.held}localRecordsAvailable(){this.#s?.changed(),this.#t.wake()}hold(t){this.#e.hold(t)}release(t){this.#e.release(t)}async flush(){if(!this.#r)throw new Error("entity record rail has not started");if(this.#i)throw new Error("entity record rail is stopped");await this.#e.coverReceipts(await this.#t.settle())}async receiveThrough(t){if(!this.#r)throw new Error("entity record rail has not started");await this.#e.receiveThrough(t)}async receiveCurrent(){if(!this.#r)throw new Error("entity record rail has not started");await this.#e.receiveCurrent()}async close(t,r){if(!Number.isFinite(t)||t<0)throw new Error("entity record close budget must be a non-negative number of milliseconds");const i=this.#t.stamped;if(this.#r&&!this.#i&&t>0){let s=n(()=>{},"expire");const h=new Promise(a=>{s=a}),c=this.#o.schedule(t,()=>s());try{const a=this.#t.settle();await Promise.race([a.catch(d=>{!this.#i&&d!==this.#t.paused&&this.#a(d)}),h])}finally{c.cancel()}}const e=this.stop();return await r(),await e,{accepted:this.#t.stamped-i,pending:await this.#h.countPending(),unconfirmed:await this.#h.countPending(this.#t.uncertainThrough)}}stop(){return this.#i??=Promise.all([this.#e.stop(),this.#t.stop(),this.#s?.stop()]).then(()=>{}),this.#i}#u(t){this.#s?.changed();const r=new Set;let i=!1;for(const{row:e,accepted:s}of t){for(const{entityId:h}of(0,l.channelRecords)(e.record))r.add(h),s.authorityId!==this.#n&&this.#e.ring({authorityId:s.authorityId,entityId:h});s.authorityId===this.#n&&(i=!0)}i&&this.#e.ring({authorityId:this.#n,entityId:null});try{this.#d([...r])}catch(e){this.#a(e)}}#a(t){try{this.#c(t)}catch{}}}exports.EntityRecordRail=m;
1
+ "use strict";var u=Object.defineProperty;var n=(o,t)=>u(o,"name",{value:t,configurable:!0});Object.defineProperty(exports,"__esModule",{value:!0}),exports.EntityRecordRail=void 0;const l=require("./contracts.js"),y=require("./receive-loop.js"),w=require("./send-line.js"),f=require("./unsent-count.js");class m{static{n(this,"EntityRecordRail")}#n;#h;#o;#d;#c;#e;#t;#s;#r=!1;#i=null;constructor(t){if(!t.authorityId.trim())throw new Error("entity record authority is required");this.#n=t.authorityId,this.#h=t.store,this.#o=t.schedule,this.#d=t.onOutboxAccepted??(()=>{}),this.#c=t.onBackgroundError??(()=>{});const r=n(e=>this.#a(e),"onError");this.#e=new y.ReceiveLoop({authorityId:t.authorityId,store:t.store,authority:t.authority,schedule:t.schedule,channels:t.channels??[],onInboxCommitted:t.onInboxCommitted??(()=>{}),onError:r}),this.#t=new w.SendLine({store:t.store,cargo:t.cargo,authority:t.authority,schedule:t.schedule,onStamped:n(e=>this.#u(e),"onStamped"),onError:r});const i=t.onUnsent;this.#s=i?new f.UnsentCount({read:n(()=>t.store.countPending(),"read"),say:i,onError:r,schedule:t.schedule}):null}async start(){if(this.#r)throw new Error("entity record rail already started");if(this.#i)throw new Error("entity record rail is stopped");this.#r=!0,this.#s?.changed(),this.#e.start(),await this.#t.start()}get sendPaused(){return this.#t.paused}get sendHeld(){return this.#t.held}sendWaits(t){return this.#t.waits(t)}localRecordsAvailable(){this.#s?.changed(),this.#t.wake()}hold(t){this.#e.hold(t)}release(t){this.#e.release(t)}async flush(){if(!this.#r)throw new Error("entity record rail has not started");if(this.#i)throw new Error("entity record rail is stopped");await this.#e.coverReceipts(await this.#t.settle())}async receiveThrough(t){if(!this.#r)throw new Error("entity record rail has not started");await this.#e.receiveThrough(t)}async receiveCurrent(){if(!this.#r)throw new Error("entity record rail has not started");await this.#e.receiveCurrent()}async close(t,r){if(!Number.isFinite(t)||t<0)throw new Error("entity record close budget must be a non-negative number of milliseconds");const i=this.#t.stamped;if(this.#r&&!this.#i&&t>0){let s=n(()=>{},"expire");const h=new Promise(a=>{s=a}),c=this.#o.schedule(t,()=>s());try{const a=this.#t.settle();await Promise.race([a.catch(d=>{!this.#i&&d!==this.#t.paused&&this.#a(d)}),h])}finally{c.cancel()}}const e=this.stop();return await r(),await e,{accepted:this.#t.stamped-i,pending:await this.#h.countPending(),unconfirmed:await this.#h.countPending(this.#t.uncertainThrough)}}stop(){return this.#i??=Promise.all([this.#e.stop(),this.#t.stop(),this.#s?.stop()]).then(()=>{}),this.#i}#u(t){this.#s?.changed();const r=new Set;let i=!1;for(const{row:e,accepted:s}of t){for(const{entityId:h}of(0,l.channelRecords)(e.record))r.add(h),s.authorityId!==this.#n&&this.#e.ring({authorityId:s.authorityId,entityId:h});s.authorityId===this.#n&&(i=!0)}i&&this.#e.ring({authorityId:this.#n,entityId:null});try{this.#d([...r])}catch(e){this.#a(e)}}#a(t){try{this.#c(t)}catch{}}}exports.EntityRecordRail=m;
@@ -0,0 +1 @@
1
+ "use strict";var l=Object.defineProperty;var o=(r,e)=>l(r,"name",{value:e,configurable:!0});Object.defineProperty(exports,"__esModule",{value:!0}),exports.AheadScan=void 0;const h=require("./contracts.js"),u=require("./send-batch.js"),d=require("./send-preparation.js"),f=require("./waits.js");class p{static{o(this,"AheadScan")}#t;#s;#e=[];#a=0;#n=null;#c=new Set;constructor(e){this.#t=e,this.#s=new f.Waits(e.readPlace)}get after(){return this.#a}get reach(){return this.#n}get ready(){return this.#e}get wanting(){return this.#e.length<d.ENTITY_RECORD_LOOKAHEAD_ROWS}behind(e){return this.#s.behind(e)}async judge(e){for(const t of e){if(!this.wanting)return;await this.#i(t),this.#a=t.localSequence}}stamped(e){const t=new Set(e.map(s=>s.localSequence));this.#e.splice(0,this.#e.length,...this.#e.filter(s=>!t.has(s.localSequence)))}async stepAside(e){const t=e.localSequence,s=this.#e.filter(a=>a.localSequence>t);this.#e.splice(0,this.#e.length,...this.#e.filter(a=>a.localSequence<t)),await this.#r(e);for(const a of s)await this.#i(a)}async#i(e){const{pause:t,waiting:s}=this.#t;if(t?.row.localSequence!==e.localSequence&&!s.has(e.localSequence)&&this.#o(e)){if(!await this.#h(e)){this.#e.push(e);return}t&&!t.reached&&this.#c.has(e.record.entityId)&&(this.#n??=e.localSequence)}await this.#r(e)}#o(e){const{pause:t}=this.#t;return t===null||e.record.authorityId===t.row.record.authorityId&&(0,u.freesStorage)(e)}async#h(e){for(const{entityId:t,change:s}of(0,h.channelRecords)(e.record))if(await this.#s.behind({entityId:t,facets:s.facets,result:s.result}))return!0;return!1}async#r(e){const t=this.#t.pause?.row.localSequence===e.localSequence;for(const{entityId:s,change:a}of(0,h.channelRecords)(e.record)){const n=[];if(a.replay.kind==="package")for(const c of a.replay.pages)for(const i of await this.#t.readPage(c))n.push(i);if(await this.#s.hold({entityId:s,facets:a.facets,result:a.result},n),t)for(const c of[s,...n.map(({uuid:i})=>i)])this.#c.add(c)}}}exports.AheadScan=p;
@@ -1 +1 @@
1
- "use strict";var y=Object.defineProperty;var a=(r,e)=>y(r,"name",{value:e,configurable:!0});Object.defineProperty(exports,"__esModule",{value:!0}),exports.EntityRecordRefusedError=exports.ENTITY_RECORD_OVER_STORAGE_LIMIT=exports.ENTITY_RECORD_CARGO_MISSING=exports.ENTITY_RECORD_BATCH_BYTES=exports.ENTITY_RECORD_BATCH_ROWS=void 0,exports.freesStorage=g,exports.selectBatch=w,exports.acceptedPrefix=A;const l=require("../faults/need.js"),O=require("../detection/refusal.js"),S=require("./contracts.js"),I=require("./outbox.js");exports.ENTITY_RECORD_BATCH_ROWS=256,exports.ENTITY_RECORD_BATCH_BYTES=16*1024*1024,exports.ENTITY_RECORD_CARGO_MISSING="entity_record_cargo_missing",exports.ENTITY_RECORD_OVER_STORAGE_LIMIT="files_over_storage_limit";const C=new Set(["trash","delete"]);function g(r){const{facets:e,cargo:t}=r.record.change;return t.length===0&&e.length>0&&e.every(n=>C.has(n))}a(g,"freesStorage");function w(r,e){const t=r[0];if(!t)return[];const n=[];let o=0;for(const d of r){if(d.record.authorityId!==t.record.authorityId||n.length===exports.ENTITY_RECORD_BATCH_ROWS)break;const c=e(d);if(!c||n.length>0&&o+c.bytes>exports.ENTITY_RECORD_BATCH_BYTES)break;n.push(c),o+=c.bytes}return n}a(w,"selectBatch");const b=new Set(["entity_record_read_forbidden","entity_record_write_forbidden",exports.ENTITY_RECORD_OVER_STORAGE_LIMIT]);class p extends Error{static{a(this,"EntityRecordRefusedError")}code;mutationId;need;constructor(e,t,n=null){super(`entity record ${e} was refused: ${t.code}: ${t.message}`),this.name="EntityRecordRefusedError",this.code=t.code,this.mutationId=e;const o=b.has(t.code);this.need=o?null:{law:"record-refused",trigger:(0,l.recoveryTrigger)(t.code),subject:n,lost:e},o&&(0,O.asRefusal)(this)}}exports.EntityRecordRefusedError=p;function A(r,e){const t=r[0],n=t?{id:t.record.authorityId,type:"authority"}:null,o=a(()=>(0,l.failure)("entity record batch answer does not match the offered batch","invariant-broken","answer-not-offered",n),"notOffered"),d=new Map,c=[];let u=0,h=0;for(;h<e.accepted.length;){const s=r[c.length];if(s===void 0)throw o();const R=(0,S.channelRecords)(s.record),_=e.accepted.slice(h,h+R.length);if(_.length!==R.length)throw o();(0,I.acceptOutboxRow)(s,_[0].record);let E=u;_.forEach(({deliverySequence:T,record:i},f)=>{if(f>0&&!(0,I.isOwnRecord)(s,i)||i.entityId!==R[f].entityId)throw o();if(T<=E&&!g(s)||i.globalSequence<=(d.get(i.entityId)??0))throw(0,l.failure)("entity record batch answer breaks this computer's acceptance order","invariant-broken","answer-out-of-order",n,s.record.mutationId);E=Math.max(E,T),d.set(i.entityId,i.globalSequence)}),u=Math.max(u,E),c.push(Object.freeze({row:s,accepted:_[0].record,deliverySequence:_.at(-1).deliverySequence})),h+=R.length}if(e.refusal===null!=(c.length===r.length))throw o();return c}a(A,"acceptedPrefix");
1
+ "use strict";var T=Object.defineProperty;var a=(r,e)=>T(r,"name",{value:e,configurable:!0});Object.defineProperty(exports,"__esModule",{value:!0}),exports.EntityRecordRefusedError=exports.ENTITY_RECORD_OVER_STORAGE_LIMIT=exports.ENTITY_RECORD_CARGO_MISSING=exports.ENTITY_RECORD_BATCH_BYTES=exports.ENTITY_RECORD_BATCH_ROWS=void 0,exports.freesStorage=g,exports.selectBatch=O,exports.acceptedPrefix=w;const u=require("../faults/need.js"),f=require("../detection/refusal.js"),I=require("./contracts.js"),l=require("./outbox.js");exports.ENTITY_RECORD_BATCH_ROWS=256,exports.ENTITY_RECORD_BATCH_BYTES=16*1024*1024,exports.ENTITY_RECORD_CARGO_MISSING="entity_record_cargo_missing",exports.ENTITY_RECORD_OVER_STORAGE_LIMIT="files_over_storage_limit";const y=new Set(["trash","delete"]);function g(r){const{facets:e,cargo:t}=r.record.change;return t.length===0&&e.length>0&&e.every(n=>y.has(n))}a(g,"freesStorage");function O(r,e){const t=r[0];if(!t)return[];const n=[];let o=0;for(const d of r){if(d.record.authorityId!==t.record.authorityId||n.length===exports.ENTITY_RECORD_BATCH_ROWS)break;const c=e(d);if(!c||n.length>0&&o+c.bytes>exports.ENTITY_RECORD_BATCH_BYTES)break;n.push(c),o+=c.bytes}return n}a(O,"selectBatch");const S=new Set(["entity_record_read_forbidden","entity_record_write_forbidden",exports.ENTITY_RECORD_OVER_STORAGE_LIMIT]);class C extends Error{static{a(this,"EntityRecordRefusedError")}code;mutationId;need;constructor(e,t,n=null){super(`entity record ${e} was refused: ${t.code}: ${t.message}`),this.name="EntityRecordRefusedError",this.code=t.code,this.mutationId=e;const o=S.has(t.code);this.need=o?null:{law:"record-refused",trigger:(0,u.recoveryTrigger)(t.code),subject:n,lost:e},o&&(0,f.asRefusal)(this)}}exports.EntityRecordRefusedError=C;function w(r,e){const t=r[0],n=t?{id:t.record.authorityId,type:"authority"}:null,o=a(()=>(0,u.failure)("entity record batch answer does not match the offered batch","invariant-broken","answer-not-offered",n),"notOffered"),d=new Map,c=[];let h=0;for(;h<e.accepted.length;){const s=r[c.length];if(s===void 0)throw o();const R=(0,I.channelRecords)(s.record),_=e.accepted.slice(h,h+R.length);if(_.length!==R.length)throw o();(0,l.acceptOutboxRow)(s,_[0].record),_.forEach(({record:i},E)=>{if(E>0&&!(0,l.isOwnRecord)(s,i)||i.entityId!==R[E].entityId)throw o();if(i.globalSequence<=(d.get(i.entityId)??0))throw(0,u.failure)("entity record batch answer breaks this computer's acceptance order","invariant-broken","answer-out-of-order",n,s.record.mutationId);d.set(i.entityId,i.globalSequence)}),c.push(Object.freeze({row:s,accepted:_[0].record,deliverySequence:_.at(-1).deliverySequence})),h+=R.length}if(e.refusal===null!=(c.length===r.length))throw o();return c}a(w,"acceptedPrefix");
@@ -1 +1 @@
1
- "use strict";var _=Object.defineProperty;var d=(p,t)=>_(p,"name",{value:t,configurable:!0});Object.defineProperty(exports,"__esModule",{value:!0}),exports.SendLine=void 0;const y=require("./owned-effects.js"),E=require("./retry-clock.js"),o=require("./send-batch.js"),S=require("./send-freeing.js"),f=require("./send-preparation.js"),g=Object.freeze({progressed:!1,failure:null}),R=d(()=>new Error("entity record Send stopped before stamping the rows it owes"),"sendStopped");class O{static{d(this,"SendLine")}#t;#c;#e;#i;#s=new y.OwnedEffects(R);#R;#d;#a=null;#y=!1;#r=!1;#o=!1;#n=null;#w=null;#f=null;#p=0;#m=!1;#E=0;#O=[];#_=null;#l=null;#S=new Set;#h=0;#u=0;#T=0;#g=0;constructor(t){this.#t=t.store,this.#c=t.authority,this.#R=t.onStamped,this.#d=t.onError,this.#e=new f.SendPreparation(t.cargo,t.onError),this.#i=new E.RetryClock(t.schedule)}get stamped(){return this.#T}get uncertainThrough(){return this.#y?this.#h:void 0}get paused(){return this.#n}get held(){return this.#w}start(){if(!this.#a){const t=d(()=>this.resume(),"resume");this.#O=[this.#c.onStorageChanged(t),this.#c.onDisconnect(t)]}return this.#a??=this.#s.run(()=>this.#t.readLocalSequence()).then(t=>{this.#h=this.#u=t,this.#y=!0,this.wake()}),this.#a}resume(){this.#E+=1,this.#m=!1,this.#n&&(this.#n=null,this.#f=null,this.wake())}wake(){this.#r||(this.#o=!0,this.#b())}async settle(){if(!this.#a)throw new Error("entity record Send has not started");const t=this.#s.run(()=>this.#t.readLocalSequence()),e=t.catch(()=>0);this.#S.add(e);try{const[,h]=await Promise.all([this.#a,t]);let r=null;for(;this.#g<h;){const[s]=await this.#s.run(()=>this.#t.readPending(1));if(!s||s.localSequence>h)break;if(!(s.localSequence<=this.#g)){if(r)throw r.error;if(this.#n)throw this.#n;({failure:r}=await(this.#l??this.#I()))}}return h}finally{this.#S.delete(e),this.wake()}}async stop(){this.#r=!0,this.#i.cancel();for(const t of this.#O)t();await Promise.allSettled([this.#a,this.#_,this.#l,this.#e.stop(),this.#s.stop()])}#b(){if(!this.#y||this.#r||this.#i.armed||this.#_)return;const t=this.#k();this.#_=t,t.then(()=>{this.#_=null,this.#o&&this.#b()})}async#k(){for(;!this.#r&&this.#o&&!this.#i.armed;){this.#o=!1;const t=await(this.#l??this.#I());if(t.failure)return;t.progressed&&(this.#o=!0)}}#I(){this.#i.cancel();const t=this.#f?this.#P(this.#f):this.#L();return this.#l=t,t.then(()=>{this.#l===t&&(this.#l=null)}),t}async#L(){let t=[],e=0;const h=this.#E,r=this.#p;this.#p=0;try{const s=await this.#s.run(()=>this.#t.readPending(f.ENTITY_RECORD_LOOKAHEAD_ROWS));s.length===0&&(this.#w=null);const a=await this.#A(),i=s.filter(n=>n.localSequence<=a),l=this.#u;this.#e.retain(i,l);const m=i[0];if(!m||(await this.#e.head(m),r>0&&await this.#e.settledThrough(r),this.#r))return g;const w=(0,o.selectBatch)(i,n=>this.#e.prepared(n));await this.#s.run(()=>this.#t.durableThrough(w[w.length-1].row.localSequence)),t=w;const u=await this.#c.submit(t.map(({submission:n})=>n)),c=(0,o.acceptedPrefix)(t.map(({row:n})=>n),u);if(c.length>0&&(await this.#t.stampAccepted(c),e=c.length,this.#w=null,this.#q(c,!0),this.#i.succeeded(),this.#m=!1),u.refusal){const n=t[e].row;if(n.localSequence>l||u.refusal.code!==o.ENTITY_RECORD_CARGO_MISSING)throw this.#e.forget([n]),new o.EntityRecordRefusedError(n.record.mutationId,u.refusal,{id:n.record.entityId,type:n.record.change.result.type});this.#u=n.localSequence-1}return{progressed:!0,failure:null}}catch(s){if(!(s instanceof o.EntityRecordRefusedError)&&t.length>e&&(this.#h=Math.max(this.#h,t[t.length-1].row.localSequence)),this.#u=this.#h,s instanceof o.EntityRecordRefusedError?this.#w=s:this.#p||=r,!this.#r){const a=s instanceof o.EntityRecordRefusedError&&s.code===o.ENTITY_RECORD_OVER_STORAGE_LIMIT,i=a&&this.#E===h;i&&(this.#n=s,this.#f=new S.FreeingScan(this.#m,l=>this.#s.run(()=>this.#t.readPackagePage(l))),this.#e.retain([],this.#u)),a?this.#o=!0:this.#i.arm(()=>this.wake()),i&&r>0||this.#d(s)}return{progressed:e>0,failure:{error:s}}}}async#P(t){let e=[],h=0;try{for(;t.wanting;){const i=await this.#s.run(()=>this.#t.readPending(f.ENTITY_RECORD_LOOKAHEAD_ROWS,t.after));if(await t.judge(i),i.length<f.ENTITY_RECORD_LOOKAHEAD_ROWS)break}if(t.reach!==null)return this.#n=null,this.#f=null,this.#p=t.reach,this.#m=!0,{progressed:!0,failure:null};const r=t.offer();if(r.length===0||this.#r)return g;await this.#s.run(()=>this.#t.durableThrough(r[r.length-1].row.localSequence)),e=r;const s=await this.#c.submit(e.map(({submission:i})=>i)),a=(0,o.acceptedPrefix)(e.map(({row:i})=>i),s);if(a.length>0&&(await this.#t.stampAccepted(a),h=a.length,t.stamped(a),this.#q(a,!1)),s.refusal){const i=e[h].row;t.refused(),this.#d(new o.EntityRecordRefusedError(i.record.mutationId,s.refusal,{id:i.record.entityId,type:i.record.change.result.type}))}return{progressed:h>0||s.refusal!==null,failure:null}}catch(r){return e.length>h&&(this.#h=Math.max(this.#h,e[e.length-1].row.localSequence)),this.#u=this.#h,this.#r||(this.#i.arm(()=>this.wake()),this.#d(r)),g}}async#A(){for(const t of[...this.#S]){const e=await t;if(e>this.#g)return e}return Number.POSITIVE_INFINITY}#q(t,e){this.#T+=t.length,e&&(this.#g=Math.max(this.#g,t[t.length-1].row.localSequence)),this.#e.forget(t.map(({row:h})=>h));try{this.#R(t)}catch(h){this.#d(h)}}}exports.SendLine=O;
1
+ "use strict";var m=Object.defineProperty;var u=(l,t)=>m(l,"name",{value:t,configurable:!0});Object.defineProperty(exports,"__esModule",{value:!0}),exports.SendLine=void 0;const O=require("../faults/classify.js"),T=require("./owned-effects.js"),E=require("./retry-clock.js"),a=require("./send-batch.js"),I=require("./send-ahead.js"),g=require("./send-preparation.js"),S=Object.freeze({progressed:!1,failure:null}),_=Object.freeze({progressed:!0,failure:null}),R=u(()=>new Error("entity record Send stopped before stamping the rows it owes"),"sendStopped");function b(l){if(l instanceof a.EntityRecordRefusedError)return l.code!==a.ENTITY_RECORD_OVER_STORAGE_LIMIT;const t=(0,O.classifyFault)(l);return t.kind==="refusal"||t.kind==="need"&&t.need.subject?.type!=="authority"}u(b,"aboutOneRow");class k{static{u(this,"SendLine")}#e;#p;#s;#n;#_;#i=new T.OwnedEffects(R);#T;#a;#c=null;#R=!1;#o=!1;#h=!1;#r=new Map;#t=null;#u=null;#y=0;#E=!1;#m=0;#I=[];#S=null;#l=null;#O=new Set;#d=0;#f=0;#b=0;#g=0;constructor(t){this.#e=t.store,this.#p=t.authority,this.#T=t.onStamped,this.#a=t.onError,this.#s=new g.SendPreparation(t.cargo,t.onError),this.#n=new E.RetryClock(t.schedule),this.#_=new E.RetryClock(t.schedule)}get stamped(){return this.#b}get uncertainThrough(){return this.#R?this.#d:void 0}get paused(){return this.#t?.error??null}get held(){return this.#t?this.#t.error:this.#r.size>0?this.#P():null}async waits(t){if(this.#t)return this.#t.error;const e=this.#u;if(!e)return null;const s=await this.#i.run(()=>this.#e.readPlace(t)),h={type:s?.type??"folder",parentUUID:s?.parentUUID??null,name:""};return await e.behind({entityId:t,facets:[],result:h})?this.held:null}start(){if(!this.#c){const t=u(()=>this.resume(),"resume");this.#I=[this.#p.onStorageChanged(t),this.#p.onDisconnect(t)]}return this.#c??=this.#i.run(()=>this.#e.readLocalSequence()).then(t=>{this.#d=this.#f=t,this.#R=!0,this.wake()}),this.#c}resume(){this.#m+=1,this.#E=!1,this.#t&&(this.#t=null,this.#w(),this.wake())}wake(){this.#o||(this.#h=!0,this.#k())}async settle(){if(!this.#c)throw new Error("entity record Send has not started");const t=this.#i.run(()=>this.#e.readLocalSequence()),e=t.catch(()=>0);this.#O.add(e);try{const[,s]=await Promise.all([this.#c,t]);let h=null,c=!1;for(;this.#g<s;){const[o]=await this.#i.run(()=>this.#e.readPending(1));if(!o||o.localSequence>s)break;if(o.localSequence<=this.#g)continue;if(h)throw h.error;if(this.#t)throw this.#t.error;if(c)throw this.#P();const i=this.#l,n=await(i??this.#q());h=n.failure,c=!i&&!n.progressed&&this.#r.size>0}return s}finally{this.#O.delete(e),this.wake()}}async stop(){this.#o=!0,this.#n.cancel(),this.#_.cancel();for(const t of this.#I)t();await Promise.allSettled([this.#c,this.#S,this.#l,this.#s.stop(),this.#i.stop()])}#k(){if(!this.#R||this.#o||this.#n.armed||this.#S)return;const t=this.#D();this.#S=t,t.then(()=>{this.#S=null,this.#h&&this.#k()})}async#D(){for(;!this.#o&&this.#h&&!this.#n.armed;){this.#h=!1;const t=await(this.#l??this.#q());if(t.failure)return;t.progressed&&(this.#h=!0)}}#q(){this.#n.cancel();const t=this.#L();return this.#l=t,t.then(()=>{this.#l===t&&(this.#l=null)}),t}async#A(){const t=this.#u;if(!t)return this.#i.run(()=>this.#e.readPending(g.ENTITY_RECORD_LOOKAHEAD_ROWS));for(;t.wanting;){const e=await this.#i.run(()=>this.#e.readPending(g.ENTITY_RECORD_LOOKAHEAD_ROWS,t.after));if(await t.judge(e),e.length<g.ENTITY_RECORD_LOOKAHEAD_ROWS)break}return t!==this.#u?null:t.reach!==null?(this.#t=null,this.#y=t.reach,this.#E=!0,this.#w(),null):t.ready}async#L(){let t=[],e=0,s;const h=this.#m,c=this.#y;this.#y=0;const o=this.#u;try{const i=await this.#A();if(i===null)return _;const n=await this.#N(),d=i.filter(r=>r.localSequence<=n),y=this.#f;if(this.#s.retain(d,y),s=d[0],!s||(await this.#s.head(s),c>0&&await this.#s.settledThrough(c),this.#o))return S;const p=(0,a.selectBatch)(d,r=>this.#s.prepared(r));await this.#i.run(()=>this.#e.durableThrough(p[p.length-1].row.localSequence)),t=p;const w=await this.#p.submit(t.map(({submission:r})=>r)),f=(0,a.acceptedPrefix)(t.map(({row:r})=>r),w);if(f.length>0&&(await this.#e.stampAccepted(f),e=f.length,o?.stamped(f.map(({row:r})=>r)),this.#M(f,o===null),this.#n.succeeded(),o===null&&this.#_.succeeded(),this.#t||(this.#E=!1)),w.refusal){const r=t[e].row;if(r.localSequence>y||w.refusal.code!==a.ENTITY_RECORD_CARGO_MISSING)throw this.#s.forget([r]),s=r,new a.EntityRecordRefusedError(r.record.mutationId,w.refusal,{id:r.record.entityId,type:r.record.change.result.type});this.#f=r.localSequence-1}return _}catch(i){const n=i instanceof a.EntityRecordRefusedError;if(!n&&t.length>e&&(this.#d=Math.max(this.#d,t[t.length-1].row.localSequence)),this.#f=this.#d,n||(this.#y||=c),this.#o)return{progressed:e>0,failure:{error:i}};if(s&&(n||t.length===0)&&(b(i)||n&&this.#t))return await this.#j(s,i),this.#a(i),_;if(n&&i.code===a.ENTITY_RECORD_OVER_STORAGE_LIMIT){const d=this.#m===h;return d&&(this.#t={error:i,row:s},this.#w(),this.#s.retain([],this.#f)),this.#h=!0,d&&c>0||this.#a(i),{progressed:e>0,failure:{error:i}}}return this.#n.arm(()=>this.wake()),this.#a(i),{progressed:e>0,failure:{error:i}}}}async#j(t,e){this.#r.set(t.localSequence,e),this.#s.forget([t]);const s=this.#u;try{s?await s.stepAside(t):this.#w()}catch(h){this.#a(h),this.#w()}this.#_.arm(()=>this.#C()),this.#h=!0}#C(){this.#r.clear(),this.#w(),this.#f=this.#d,this.wake()}#w(){const t=this.#t;this.#u=this.#r.size>0||t?new I.AheadScan({waiting:this.#r,pause:t&&{row:t.row,reached:this.#E},readPage:u(e=>this.#i.run(()=>this.#e.readPackagePage(e)),"readPage"),readPlace:u(e=>this.#i.run(()=>this.#e.readPlace(e)),"readPlace")}):null}#P(){return this.#r.get(Math.min(...this.#r.keys()))??R()}async#N(){for(const t of[...this.#O]){const e=await t;if(e>this.#g)return e}return Number.POSITIVE_INFINITY}#M(t,e){this.#b+=t.length,e&&(this.#g=Math.max(this.#g,t[t.length-1].row.localSequence)),this.#s.forget(t.map(({row:s})=>s));try{this.#T(t)}catch(s){this.#a(s)}}}exports.SendLine=k;
@@ -1 +1 @@
1
- "use strict";var l=Object.defineProperty;var h=(a,e)=>l(a,"name",{value:e,configurable:!0});Object.defineProperty(exports,"__esModule",{value:!0}),exports.SendPreparation=exports.ENTITY_RECORD_CARGO_READS=exports.ENTITY_RECORD_PREPARED_BYTES=exports.ENTITY_RECORD_LOOKAHEAD_ROWS=void 0;const E=require("../entities/bytes.js"),_=require("./codec.js"),c=require("./contracts.js"),u=require("./send-batch.js"),f=require("./submit-frame.js");exports.ENTITY_RECORD_LOOKAHEAD_ROWS=512,exports.ENTITY_RECORD_PREPARED_BYTES=u.ENTITY_RECORD_BATCH_BYTES,exports.ENTITY_RECORD_CARGO_READS=4;const p=Promise.resolve();class R{static{h(this,"SendPreparation")}#c;#u;#e=new Map;#t=[];#f=0;#o=0;#d=0;#r=!1;#i=new Set;constructor(e,t){this.#c=e,this.#u=t}retain(e,t){if(this.#r)return;this.#t=e,this.#f=t;const r=new Map(e.map(i=>[i.record.mutationId,i]));for(const[i,o]of this.#e){const s=r.get(i);(!s||o.carriesCargo!==this.#h(s))&&this.#s(i)}this.#n()}forget(e){if(e.length===0)return;const t=new Set(e.map(r=>r.record.mutationId));this.#t=this.#t.filter(r=>!t.has(r.record.mutationId));for(const r of t)this.#s(r);this.#n()}prepared(e){return this.#e.get(e.record.mutationId)?.ready??null}async head(e){if(e!==this.#t[0])throw new Error("only the lookahead head is awaited");this.#e.get(e.record.mutationId)?.failure&&this.#s(e.record.mutationId),this.#n();let t=this.#e.get(e.record.mutationId);for(;!t;){if(this.#r)throw new Error("entity record preparation stopped");if(this.#i.size===0)throw new Error(`entity record ${e.record.mutationId} exceeds the prepared byte bound`);await Promise.race(this.#i),t=this.#e.get(e.record.mutationId)}if(await t.settled,t.failure)throw t.failure.error;return t.ready}async settledThrough(e){for(;;){const t=[...this.#e.values()].filter(({row:r,ready:i,failure:o})=>r.localSequence<=e&&!i&&!o);if(t.length===0)return;await Promise.race(t.map(({settled:r})=>r))}}stop(){this.#r=!0,this.#t=[];for(const e of[...this.#e.keys()])this.#s(e);return Promise.allSettled([...this.#i]).then(()=>{})}#n(){if(!this.#r)for(const[e,t]of this.#t.entries()){if(this.#e.has(t.record.mutationId))continue;const r=this.#h(t)?(0,c.recordCargo)(t.record).length*f.INLINE_ARTIFACT_FRAME_BYTES:0;if(r>0&&this.#d>=exports.ENTITY_RECORD_CARGO_READS)return;const i=(0,_.encodeEntityRecord)(t.record),o=(0,E.utf8Length)(i),s=o+Math.min(r,Math.max(0,u.ENTITY_RECORD_BATCH_BYTES-o));if(e===0&&this.#l(s),this.#o+s>exports.ENTITY_RECORD_PREPARED_BYTES)return;this.#E(t,i,s)}}#l(e){for(let t=this.#t.length-1;t>0&&this.#o+e>exports.ENTITY_RECORD_PREPARED_BYTES;t-=1){const r=this.#t[t].record.mutationId;this.#e.get(r)?.ready&&this.#s(r)}}#h(e){return e.localSequence>this.#f&&(0,c.recordCargo)(e.record).length>0}#E(e,t,r){if(this.#o+=r,!this.#h(e)){const s=Object.freeze({recordJson:t,cargo:Object.freeze([])});this.#e.set(e.record.mutationId,{row:e,carriesCargo:!1,reserved:r,ready:Object.freeze({row:e,submission:s,bytes:r}),failure:null,settled:p,dropped:!1});return}this.#d+=1;let i;const o=Promise.resolve().then(()=>{if(this.#r)throw new Error("entity record preparation stopped");return this.#c.ensure(e.record)}).then(s=>{const d=Object.freeze({recordJson:t,cargo:Object.freeze([...s??[]])}),n=(0,f.submissionBytes)(d);if(n>r)throw new Error(`cargo for ${e.record.mutationId} exceeds what one row may carry inline`);i.ready=Object.freeze({row:e,submission:d,bytes:n}),this.#a(i,n)}).catch(s=>{i.failure={error:s},this.#a(i,0)}).finally(()=>{this.#d-=1,this.#i.delete(o),i.dropped&&this.#a(i,0),this.#n()}).catch(s=>this.#u(s));i={row:e,carriesCargo:!0,reserved:r,ready:null,failure:null,settled:o,dropped:!1},this.#i.add(o),this.#e.set(e.record.mutationId,i)}#s(e){const t=this.#e.get(e);t&&(this.#e.delete(e),t.dropped=!0,(t.ready||t.failure)&&this.#a(t,0))}#a(e,t){const r=e.dropped?0:t;this.#o+=r-e.reserved,e.reserved=r}}exports.SendPreparation=R;
1
+ "use strict";var E=Object.defineProperty;var d=(n,e)=>E(n,"name",{value:e,configurable:!0});Object.defineProperty(exports,"__esModule",{value:!0}),exports.SendPreparation=exports.ENTITY_RECORD_CARGO_READS=exports.ENTITY_RECORD_PREPARED_BYTES=exports.ENTITY_RECORD_LOOKAHEAD_ROWS=void 0;const p=require("../entities/bytes.js"),h=require("../faults/need.js"),R=require("./codec.js"),u=require("./contracts.js"),l=require("./send-batch.js"),f=require("./submit-frame.js"),_=d(n=>({id:n.record.entityId,type:n.record.change.result.type}),"subjectOf");exports.ENTITY_RECORD_LOOKAHEAD_ROWS=512,exports.ENTITY_RECORD_PREPARED_BYTES=l.ENTITY_RECORD_BATCH_BYTES,exports.ENTITY_RECORD_CARGO_READS=4;const m=Promise.resolve();class T{static{d(this,"SendPreparation")}#h;#u;#e=new Map;#t=[];#l=0;#o=0;#d=0;#r=!1;#i=new Set;constructor(e,t){this.#h=e,this.#u=t}retain(e,t){if(this.#r)return;this.#t=e,this.#l=t;const r=new Map(e.map(i=>[i.record.mutationId,i]));for(const[i,o]of this.#e){const s=r.get(i);(!s||o.carriesCargo!==this.#c(s))&&this.#s(i)}this.#n()}forget(e){if(e.length===0)return;const t=new Set(e.map(r=>r.record.mutationId));this.#t=this.#t.filter(r=>!t.has(r.record.mutationId));for(const r of t)this.#s(r);this.#n()}prepared(e){return this.#e.get(e.record.mutationId)?.ready??null}async head(e){if(e!==this.#t[0])throw new Error("only the lookahead head is awaited");this.#e.get(e.record.mutationId)?.failure&&this.#s(e.record.mutationId),this.#n();let t=this.#e.get(e.record.mutationId);for(;!t;){if(this.#r)throw new Error("entity record preparation stopped");if(this.#i.size===0)throw(0,h.failure)(`entity record ${e.record.mutationId} exceeds the prepared byte bound`,"invariant-broken","row-too-large",_(e),e.record.mutationId);await Promise.race(this.#i),t=this.#e.get(e.record.mutationId)}if(await t.settled,t.failure)throw t.failure.error;return t.ready}async settledThrough(e){for(;;){const t=[...this.#e.values()].filter(({row:r,ready:i,failure:o})=>r.localSequence<=e&&!i&&!o);if(t.length===0)return;await Promise.race(t.map(({settled:r})=>r))}}stop(){this.#r=!0,this.#t=[];for(const e of[...this.#e.keys()])this.#s(e);return Promise.allSettled([...this.#i]).then(()=>{})}#n(){if(!this.#r)for(const[e,t]of this.#t.entries()){if(this.#e.has(t.record.mutationId))continue;const r=this.#c(t)?(0,u.recordCargo)(t.record).length*f.INLINE_ARTIFACT_FRAME_BYTES:0;if(r>0&&this.#d>=exports.ENTITY_RECORD_CARGO_READS)return;const i=(0,R.encodeEntityRecord)(t.record),o=(0,p.utf8Length)(i),s=o+Math.min(r,Math.max(0,l.ENTITY_RECORD_BATCH_BYTES-o));if(e===0&&this.#f(s),this.#o+s>exports.ENTITY_RECORD_PREPARED_BYTES)return;this.#_(t,i,s)}}#f(e){for(let t=this.#t.length-1;t>0&&this.#o+e>exports.ENTITY_RECORD_PREPARED_BYTES;t-=1){const r=this.#t[t].record.mutationId;this.#e.get(r)?.ready&&this.#s(r)}}#c(e){return e.localSequence>this.#l&&(0,u.recordCargo)(e.record).length>0}#_(e,t,r){if(this.#o+=r,!this.#c(e)){const s=Object.freeze({recordJson:t,cargo:Object.freeze([])});this.#e.set(e.record.mutationId,{row:e,carriesCargo:!1,reserved:r,ready:Object.freeze({row:e,submission:s,bytes:r}),failure:null,settled:m,dropped:!1});return}this.#d+=1;let i;const o=Promise.resolve().then(()=>{if(this.#r)throw new Error("entity record preparation stopped");return this.#h.ensure(e.record)}).then(s=>{const c=Object.freeze({recordJson:t,cargo:Object.freeze([...s??[]])}),a=(0,f.submissionBytes)(c);if(a>r)throw(0,h.failure)(`cargo for ${e.record.mutationId} exceeds what one row may carry inline`,"invariant-broken","cargo-too-large",_(e),e.record.mutationId);i.ready=Object.freeze({row:e,submission:c,bytes:a}),this.#a(i,a)}).catch(s=>{i.failure={error:s},this.#a(i,0)}).finally(()=>{this.#d-=1,this.#i.delete(o),i.dropped&&this.#a(i,0),this.#n()}).catch(s=>this.#u(s));i={row:e,carriesCargo:!0,reserved:r,ready:null,failure:null,settled:o,dropped:!1},this.#i.add(o),this.#e.set(e.record.mutationId,i)}#s(e){const t=this.#e.get(e);t&&(this.#e.delete(e),t.dropped=!0,(t.ready||t.failure)&&this.#a(t,0))}#a(e,t){const r=e.dropped?0:t;this.#o+=r-e.reserved,e.reserved=r}}exports.SendPreparation=T;
@@ -0,0 +1 @@
1
+ "use strict";var u=Object.defineProperty;var a=(n,t)=>u(n,"name",{value:t,configurable:!0});Object.defineProperty(exports,"__esModule",{value:!0}),exports.Waits=void 0;const l=new Set(["create","rename","move","restore"]),h=new Set(["move","restore","delete"]),o=a((n,t)=>n===null?null:`${n}\0${t}`,"slot");class p{static{a(this,"Waits")}#r;#e=new Map;#t=new Set;#i=new Set;#n=new Set;#a=new Set;#l=!1;#s=!1;constructor(t){this.#r=t}get empty(){return this.#t.size===0}async hold(t,i=[]){this.#t.add(t.entityId),this.#o(t.result.parentUUID,t.result.name,t.result.type),t.facets.includes("rename")&&t.result.parentUUID!==null&&this.#a.add(t.result.parentUUID),t.facets.includes("move")&&(this.#l=!0);for(const s of i)this.#t.add(s.uuid),this.#o(s.parentUUID,s.name,s.type);let e=t.result.type==="repo.git"?t.entityId:null;for(const s of await this.#u(t.result.parentUUID))this.#i.add(s),e===null&&(await this.#h(s))?.type==="repo.git"&&(e=s);e!==null&&(this.#t.add(e),this.#s=!0)}async behind(t){if(this.empty)return!1;const{entityId:i,facets:r,result:e}=t;if(this.#t.has(i)||r.some(s=>l.has(s))&&e.parentUUID!==null&&(this.#n.has(o(e.parentUUID,e.name))||this.#a.has(e.parentUUID))||this.#l&&r.some(s=>l.has(s)||s==="delete")||this.#s&&e.type==="repo.git"||this.#i.has(i)&&r.some(s=>h.has(s)))return!0;for(const s of await this.#u(e.parentUUID))if(this.#t.has(s))return!0;return!1}#o(t,i,r){const e=o(t,i);e!==null&&this.#n.add(e),r==="repo.git"&&(this.#s=!0)}async#u(t){const i=[];for(let r=t;r!==null&&!i.includes(r);){i.push(r);const e=await this.#h(r);if(!e)break;r=e.parentUUID}return i}async#h(t){return this.#e.has(t)||this.#e.set(t,await this.#r(t)),this.#e.get(t)}}exports.Waits=p;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@amalgm/live",
3
- "version": "0.2.81-preview.47277674832",
3
+ "version": "0.2.81-preview.47301712017",
4
4
  "description": "Portable realtime SDK for Amalgm — wire contracts, mutation discipline, and delivery state machines behind host-injected adapters.",
5
5
  "type": "module",
6
6
  "main": "./dist-cjs/index.js",
@@ -133,11 +133,11 @@
133
133
  "dependencies": {
134
134
  "@amalgm/core": "0.5.1-preview.46345295340"
135
135
  },
136
- "gitHead": "7899db08682311eaaca260cc50ee288f0b1b802f",
136
+ "gitHead": "cf2b4a0cda52f6d04bbb2719b12d06b127095b41",
137
137
  "amalgmSource": {
138
138
  "repository": "amalgm-inc/amalgm-live",
139
139
  "branch": "preview",
140
- "commit": "7899db08682311eaaca260cc50ee288f0b1b802f",
141
- "occurrence": "47277674832"
140
+ "commit": "cf2b4a0cda52f6d04bbb2719b12d06b127095b41",
141
+ "occurrence": "47301712017"
142
142
  }
143
143
  }
@@ -1,14 +0,0 @@
1
- import { type GraphPageRef } from '../entities/graph-pages.js';
2
- import type { EntityRecordStamp, MaybePromise, PendingEntityRecord } from './rail-ports.js';
3
- import { type PreparedEntityRecord } from './send-batch.js';
4
- export declare class FreeingScan {
5
- #private;
6
- constructor(reached: boolean, readPage: (ref: GraphPageRef) => MaybePromise<readonly unknown[]>);
7
- get after(): number;
8
- get reach(): number | null;
9
- get wanting(): boolean;
10
- judge(rows: readonly PendingEntityRecord[]): Promise<void>;
11
- offer(): readonly PreparedEntityRecord[];
12
- stamped(stamps: readonly EntityRecordStamp[]): void;
13
- refused(): void;
14
- }
@@ -1 +0,0 @@
1
- var d=Object.defineProperty;var o=(c,e)=>d(c,"name",{value:e,configurable:!0});import{pageHolding as f}from"../entities/graph-pages.js";import{encodeEntityRecord as u}from"./codec.js";import{ENTITY_RECORD_BATCH_BYTES as l,ENTITY_RECORD_BATCH_ROWS as g,freesStorage as p}from"./send-batch.js";import{submissionBytes as y}from"./submit-frame.js";class T{static{o(this,"FreeingScan")}#i;#t=null;#s=new Set;#e=[];#c=!1;#o=null;#a=0;#h=[];#d;#r=null;constructor(e,t){this.#i=e,this.#d=t}get after(){return this.#a}get reach(){return this.#o}get wanting(){return this.#e.length<g}async judge(e){for(const t of e){if(!this.wanting)return;if(this.#a=t.localSequence,this.#t??=t,t.record.authorityId===this.#t.record.authorityId&&p(t)){const{entityId:r}=t.record,s=this.#s.has(r)?r:await this.#f(t);if(s===null&&!(this.#c&&t.record.change.facets.includes("delete"))){this.#e.push(t);continue}!this.#i&&s===this.#t.record.entityId&&(this.#o??=t.localSequence)}this.#n(t,this.#s)}}async#f(e){const t=[e.record.change.result.parentUUID,e.record.entityId];for(const{anchor:r,pages:s}of this.#h){const n=f(s,t);if(n<0)continue;const i=s[n];if(this.#r?.sha256!==i.sha256){const a=await this.#d(i);this.#r={sha256:i.sha256,members:new Set(a.map(h=>h.uuid))}}if(this.#r.members.has(e.record.entityId))return r}return null}offer(){const e=[];let t=0;for(const r of this.#e){const s=Object.freeze({recordJson:u(r.record),cargo:Object.freeze([])}),n=y(s);if(e.length>0&&t+n>l)break;e.push(Object.freeze({row:r,submission:s,bytes:n})),t+=n}return e}stamped(e){this.#e.splice(0,e.length)}refused(){const[e,...t]=this.#e.splice(0),r=new Set;this.#n(e,r);for(const s of t)r.has(s.record.entityId)?this.#n(s,r):this.#e.push(s);for(const s of r)this.#s.add(s)}#n(e,t){e.record.change.facets.includes("move")&&(this.#c=!0);const{replay:r}=e.record.change;r.kind==="package"&&this.#h.push({anchor:e.record.entityId,pages:r.pages}),t.add(e.record.entityId);const s=e.record.change.result.parentUUID;s!==null&&t.add(s)}}export{T as FreeingScan};
@@ -1 +0,0 @@
1
- "use strict";var u=Object.defineProperty;var o=(a,e)=>u(a,"name",{value:e,configurable:!0});Object.defineProperty(exports,"__esModule",{value:!0}),exports.FreeingScan=void 0;const f=require("../entities/graph-pages.js"),l=require("./codec.js"),i=require("./send-batch.js"),g=require("./submit-frame.js");class p{static{o(this,"FreeingScan")}#c;#t=null;#s=new Set;#e=[];#i=!1;#a=null;#o=0;#h=[];#d;#r=null;constructor(e,t){this.#c=e,this.#d=t}get after(){return this.#o}get reach(){return this.#a}get wanting(){return this.#e.length<i.ENTITY_RECORD_BATCH_ROWS}async judge(e){for(const t of e){if(!this.wanting)return;if(this.#o=t.localSequence,this.#t??=t,t.record.authorityId===this.#t.record.authorityId&&(0,i.freesStorage)(t)){const{entityId:r}=t.record,s=this.#s.has(r)?r:await this.#u(t);if(s===null&&!(this.#i&&t.record.change.facets.includes("delete"))){this.#e.push(t);continue}!this.#c&&s===this.#t.record.entityId&&(this.#a??=t.localSequence)}this.#n(t,this.#s)}}async#u(e){const t=[e.record.change.result.parentUUID,e.record.entityId];for(const{anchor:r,pages:s}of this.#h){const n=(0,f.pageHolding)(s,t);if(n<0)continue;const c=s[n];if(this.#r?.sha256!==c.sha256){const h=await this.#d(c);this.#r={sha256:c.sha256,members:new Set(h.map(d=>d.uuid))}}if(this.#r.members.has(e.record.entityId))return r}return null}offer(){const e=[];let t=0;for(const r of this.#e){const s=Object.freeze({recordJson:(0,l.encodeEntityRecord)(r.record),cargo:Object.freeze([])}),n=(0,g.submissionBytes)(s);if(e.length>0&&t+n>i.ENTITY_RECORD_BATCH_BYTES)break;e.push(Object.freeze({row:r,submission:s,bytes:n})),t+=n}return e}stamped(e){this.#e.splice(0,e.length)}refused(){const[e,...t]=this.#e.splice(0),r=new Set;this.#n(e,r);for(const s of t)r.has(s.record.entityId)?this.#n(s,r):this.#e.push(s);for(const s of r)this.#s.add(s)}#n(e,t){e.record.change.facets.includes("move")&&(this.#i=!0);const{replay:r}=e.record.change;r.kind==="package"&&this.#h.push({anchor:e.record.entityId,pages:r.pages}),t.add(e.record.entityId);const s=e.record.change.result.parentUUID;s!==null&&t.add(s)}}exports.FreeingScan=p;