@minca/core 0.1.15 → 0.1.17
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +10 -10
- package/dist/elements/auth-styles.d.ts +1 -1
- package/dist/elements/auth-styles.d.ts.map +1 -1
- package/dist/elements/auth-styles.js +35 -9
- package/dist/elements/auth-styles.js.map +1 -1
- package/dist/elements/minca-auth.d.ts.map +1 -1
- package/dist/elements/minca-auth.js +169 -72
- package/dist/elements/minca-auth.js.map +1 -1
- package/dist/identity/abilities.d.ts +2 -0
- package/dist/identity/abilities.d.ts.map +1 -1
- package/dist/identity/abilities.js +2 -0
- package/dist/identity/abilities.js.map +1 -1
- package/dist/identity/account-vault.d.ts +15 -71
- package/dist/identity/account-vault.d.ts.map +1 -1
- package/dist/identity/account-vault.js +45 -104
- package/dist/identity/account-vault.js.map +1 -1
- package/dist/identity/channel.d.ts +3 -9
- package/dist/identity/channel.d.ts.map +1 -1
- package/dist/identity/channel.js +42 -42
- package/dist/identity/channel.js.map +1 -1
- package/dist/identity/contact-key.d.ts +14 -29
- package/dist/identity/contact-key.d.ts.map +1 -1
- package/dist/identity/contact-key.js +31 -51
- package/dist/identity/contact-key.js.map +1 -1
- package/dist/identity/crypto-p256.d.ts.map +1 -1
- package/dist/identity/crypto-p256.js +39 -53
- package/dist/identity/crypto-p256.js.map +1 -1
- package/dist/identity/hpke.d.ts.map +1 -1
- package/dist/identity/hpke.js +19 -19
- package/dist/identity/hpke.js.map +1 -1
- package/dist/identity/identity-manager.d.ts.map +1 -1
- package/dist/identity/identity-manager.js +1 -2
- package/dist/identity/identity-manager.js.map +1 -1
- package/dist/identity/policy.d.ts.map +1 -1
- package/dist/identity/policy.js +35 -61
- package/dist/identity/policy.js.map +1 -1
- package/dist/identity/recovery-code.d.ts +2 -8
- package/dist/identity/recovery-code.d.ts.map +1 -1
- package/dist/identity/recovery-code.js +4 -11
- package/dist/identity/recovery-code.js.map +1 -1
- package/dist/identity/root-signer.d.ts +1 -6
- package/dist/identity/root-signer.d.ts.map +1 -1
- package/dist/identity/root-signer.js +12 -15
- package/dist/identity/root-signer.js.map +1 -1
- package/dist/identity/ucan.d.ts.map +1 -1
- package/dist/identity/ucan.js +48 -39
- package/dist/identity/ucan.js.map +1 -1
- package/dist/identity/webauthn.d.ts +6 -11
- package/dist/identity/webauthn.d.ts.map +1 -1
- package/dist/identity/webauthn.js +18 -30
- package/dist/identity/webauthn.js.map +1 -1
- package/dist/inbox/inbox.d.ts.map +1 -1
- package/dist/inbox/inbox.js +40 -30
- package/dist/inbox/inbox.js.map +1 -1
- package/dist/inbox/index.d.ts +2 -0
- package/dist/inbox/index.d.ts.map +1 -1
- package/dist/inbox/index.js +1 -0
- package/dist/inbox/index.js.map +1 -1
- package/dist/inbox/names.d.ts +54 -0
- package/dist/inbox/names.d.ts.map +1 -0
- package/dist/inbox/names.js +299 -0
- package/dist/inbox/names.js.map +1 -0
- package/dist/index.d.ts +3 -6
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +9 -12
- package/dist/index.js.map +1 -1
- package/dist/network/mesh.d.ts +7 -20
- package/dist/network/mesh.d.ts.map +1 -1
- package/dist/network/mesh.js +267 -245
- package/dist/network/mesh.js.map +1 -1
- package/dist/network/relay-link.d.ts +31 -0
- package/dist/network/relay-link.d.ts.map +1 -0
- package/dist/network/relay-link.js +62 -0
- package/dist/network/relay-link.js.map +1 -0
- package/dist/network/relay-minca.d.ts.map +1 -1
- package/dist/network/relay-minca.js +11 -49
- package/dist/network/relay-minca.js.map +1 -1
- package/dist/network/relay-nostr.d.ts.map +1 -1
- package/dist/network/relay-nostr.js +23 -63
- package/dist/network/relay-nostr.js.map +1 -1
- package/dist/network/relay-socket.d.ts +1 -1
- package/dist/network/relay-socket.d.ts.map +1 -1
- package/dist/network/relay-socket.js +3 -1
- package/dist/network/relay-socket.js.map +1 -1
- package/dist/network/ws-transport.d.ts.map +1 -1
- package/dist/network/ws-transport.js +3 -0
- package/dist/network/ws-transport.js.map +1 -1
- package/dist/node/actions.d.ts +2 -0
- package/dist/node/actions.d.ts.map +1 -1
- package/dist/node/actions.js +89 -58
- package/dist/node/actions.js.map +1 -1
- package/dist/node/api.d.ts +7 -0
- package/dist/node/api.d.ts.map +1 -1
- package/dist/node/api.js +7 -0
- package/dist/node/api.js.map +1 -1
- package/dist/node/carrier.d.ts +5 -0
- package/dist/node/carrier.d.ts.map +1 -1
- package/dist/node/carrier.js +94 -73
- package/dist/node/carrier.js.map +1 -1
- package/dist/node/copy.d.ts +8 -7
- package/dist/node/copy.d.ts.map +1 -1
- package/dist/node/copy.js +54 -56
- package/dist/node/copy.js.map +1 -1
- package/dist/node/host.d.ts +39 -65
- package/dist/node/host.d.ts.map +1 -1
- package/dist/node/host.js +362 -379
- package/dist/node/host.js.map +1 -1
- package/dist/node/index.d.ts +2 -2
- package/dist/node/index.d.ts.map +1 -1
- package/dist/node/index.js +1 -1
- package/dist/node/index.js.map +1 -1
- package/dist/node/node-carriers.d.ts +35 -0
- package/dist/node/node-carriers.d.ts.map +1 -0
- package/dist/node/node-carriers.js +567 -0
- package/dist/node/node-carriers.js.map +1 -0
- package/dist/node/node-contacts.d.ts +11 -0
- package/dist/node/node-contacts.d.ts.map +1 -0
- package/dist/node/node-contacts.js +808 -0
- package/dist/node/node-contacts.js.map +1 -0
- package/dist/node/node-context.d.ts +76 -0
- package/dist/node/node-context.d.ts.map +1 -0
- package/dist/node/node-context.js +54 -0
- package/dist/node/node-context.js.map +1 -0
- package/dist/node/node-hosting.d.ts +20 -0
- package/dist/node/node-hosting.d.ts.map +1 -0
- package/dist/node/node-hosting.js +456 -0
- package/dist/node/node-hosting.js.map +1 -0
- package/dist/node/node-records.d.ts +18 -0
- package/dist/node/node-records.d.ts.map +1 -0
- package/dist/node/node-records.js +262 -0
- package/dist/node/node-records.js.map +1 -0
- package/dist/node/node.d.ts.map +1 -1
- package/dist/node/node.js +199 -1990
- package/dist/node/node.js.map +1 -1
- package/dist/node/remote.d.ts +5 -6
- package/dist/node/remote.d.ts.map +1 -1
- package/dist/node/remote.js +272 -260
- package/dist/node/remote.js.map +1 -1
- package/dist/node/space-catalog.d.ts +40 -0
- package/dist/node/space-catalog.d.ts.map +1 -0
- package/dist/node/space-catalog.js +158 -0
- package/dist/node/space-catalog.js.map +1 -0
- package/dist/node/space-checks.d.ts +25 -0
- package/dist/node/space-checks.d.ts.map +1 -0
- package/dist/node/space-checks.js +343 -0
- package/dist/node/space-checks.js.map +1 -0
- package/dist/node/space-history.d.ts +37 -0
- package/dist/node/space-history.d.ts.map +1 -0
- package/dist/node/space-history.js +350 -0
- package/dist/node/space-history.js.map +1 -0
- package/dist/node/space-holding.d.ts +53 -0
- package/dist/node/space-holding.d.ts.map +1 -0
- package/dist/node/space-holding.js +339 -0
- package/dist/node/space-holding.js.map +1 -0
- package/dist/node/space-index.d.ts +41 -0
- package/dist/node/space-index.d.ts.map +1 -0
- package/dist/node/space-index.js +217 -0
- package/dist/node/space-index.js.map +1 -0
- package/dist/node/space-keys.d.ts +34 -0
- package/dist/node/space-keys.d.ts.map +1 -0
- package/dist/node/space-keys.js +309 -0
- package/dist/node/space-keys.js.map +1 -0
- package/dist/node/space-lines.d.ts +33 -0
- package/dist/node/space-lines.d.ts.map +1 -0
- package/dist/node/space-lines.js +145 -0
- package/dist/node/space-lines.js.map +1 -0
- package/dist/node/space-membership.d.ts +69 -0
- package/dist/node/space-membership.d.ts.map +1 -0
- package/dist/node/space-membership.js +242 -0
- package/dist/node/space-membership.js.map +1 -0
- package/dist/node/space-mirrors.d.ts +43 -0
- package/dist/node/space-mirrors.d.ts.map +1 -0
- package/dist/node/space-mirrors.js +125 -0
- package/dist/node/space-mirrors.js.map +1 -0
- package/dist/node/space-peers.d.ts +104 -0
- package/dist/node/space-peers.d.ts.map +1 -0
- package/dist/node/space-peers.js +629 -0
- package/dist/node/space-peers.js.map +1 -0
- package/dist/node/space-profiles.d.ts +18 -0
- package/dist/node/space-profiles.d.ts.map +1 -0
- package/dist/node/space-profiles.js +105 -0
- package/dist/node/space-profiles.js.map +1 -0
- package/dist/node/space-rules.d.ts +29 -0
- package/dist/node/space-rules.d.ts.map +1 -0
- package/dist/node/space-rules.js +144 -0
- package/dist/node/space-rules.js.map +1 -0
- package/dist/node/space-runtime.d.ts +107 -140
- package/dist/node/space-runtime.d.ts.map +1 -1
- package/dist/node/space-runtime.js +331 -3985
- package/dist/node/space-runtime.js.map +1 -1
- package/dist/node/space-standing.d.ts +20 -0
- package/dist/node/space-standing.d.ts.map +1 -0
- package/dist/node/space-standing.js +317 -0
- package/dist/node/space-standing.js.map +1 -0
- package/dist/node/space-versions.d.ts +104 -0
- package/dist/node/space-versions.d.ts.map +1 -0
- package/dist/node/space-versions.js +153 -0
- package/dist/node/space-versions.js.map +1 -0
- package/dist/node/space-warrants.d.ts +25 -0
- package/dist/node/space-warrants.d.ts.map +1 -0
- package/dist/node/space-warrants.js +214 -0
- package/dist/node/space-warrants.js.map +1 -0
- package/dist/node/space-writing.d.ts +74 -0
- package/dist/node/space-writing.d.ts.map +1 -0
- package/dist/node/space-writing.js +442 -0
- package/dist/node/space-writing.js.map +1 -0
- package/dist/node/stores.d.ts +19 -50
- package/dist/node/stores.d.ts.map +1 -1
- package/dist/node/stores.js +14 -33
- package/dist/node/stores.js.map +1 -1
- package/dist/node/types.d.ts +243 -306
- package/dist/node/types.d.ts.map +1 -1
- package/dist/node/usage.d.ts +2 -24
- package/dist/node/usage.d.ts.map +1 -1
- package/dist/node/usage.js +118 -102
- package/dist/node/usage.js.map +1 -1
- package/dist/node/watch-notifications.d.ts.map +1 -1
- package/dist/node/watch-notifications.js +32 -29
- package/dist/node/watch-notifications.js.map +1 -1
- package/dist/node/worker.d.ts +5 -6
- package/dist/node/worker.d.ts.map +1 -1
- package/dist/node/worker.js +80 -72
- package/dist/node/worker.js.map +1 -1
- package/dist/node/writer-keys.d.ts +5 -6
- package/dist/node/writer-keys.d.ts.map +1 -1
- package/dist/node/writer-keys.js +66 -57
- package/dist/node/writer-keys.js.map +1 -1
- package/dist/node/writers.d.ts +12 -19
- package/dist/node/writers.d.ts.map +1 -1
- package/dist/node/writers.js +7 -15
- package/dist/node/writers.js.map +1 -1
- package/dist/privacy/direct.d.ts.map +1 -1
- package/dist/privacy/direct.js +31 -27
- package/dist/privacy/direct.js.map +1 -1
- package/dist/privacy/space-encryption.d.ts.map +1 -1
- package/dist/privacy/space-encryption.js +5 -9
- package/dist/privacy/space-encryption.js.map +1 -1
- package/dist/query/computed.d.ts.map +1 -1
- package/dist/query/computed.js +78 -77
- package/dist/query/computed.js.map +1 -1
- package/dist/query/engine.d.ts +3 -4
- package/dist/query/engine.d.ts.map +1 -1
- package/dist/query/engine.js +93 -86
- package/dist/query/engine.js.map +1 -1
- package/dist/query/filter.d.ts +8 -10
- package/dist/query/filter.d.ts.map +1 -1
- package/dist/query/filter.js +144 -161
- package/dist/query/filter.js.map +1 -1
- package/dist/records/cel-types.d.ts.map +1 -1
- package/dist/records/cel-types.js +13 -27
- package/dist/records/cel-types.js.map +1 -1
- package/dist/records/cel.d.ts.map +1 -1
- package/dist/records/cel.js +177 -242
- package/dist/records/cel.js.map +1 -1
- package/dist/records/rules.d.ts.map +1 -1
- package/dist/records/rules.js +46 -50
- package/dist/records/rules.js.map +1 -1
- package/dist/records/topics.d.ts.map +1 -1
- package/dist/records/topics.js +18 -22
- package/dist/records/topics.js.map +1 -1
- package/dist/schemas/automations.d.ts +8 -15
- package/dist/schemas/automations.d.ts.map +1 -1
- package/dist/schemas/automations.js +203 -165
- package/dist/schemas/automations.js.map +1 -1
- package/dist/schemas/blueprints.d.ts.map +1 -1
- package/dist/schemas/blueprints.js +14 -13
- package/dist/schemas/blueprints.js.map +1 -1
- package/dist/schemas/connectors.d.ts.map +1 -1
- package/dist/schemas/connectors.js +16 -11
- package/dist/schemas/connectors.js.map +1 -1
- package/dist/schemas/contacts.d.ts +57 -0
- package/dist/schemas/contacts.d.ts.map +1 -1
- package/dist/schemas/contacts.js +32 -0
- package/dist/schemas/contacts.js.map +1 -1
- package/dist/schemas/cron.d.ts.map +1 -1
- package/dist/schemas/cron.js +27 -28
- package/dist/schemas/cron.js.map +1 -1
- package/dist/schemas/defined.d.ts +6 -0
- package/dist/schemas/defined.d.ts.map +1 -0
- package/dist/schemas/defined.js +7 -0
- package/dist/schemas/defined.js.map +1 -0
- package/dist/schemas/guides.d.ts +3 -1
- package/dist/schemas/guides.d.ts.map +1 -1
- package/dist/schemas/guides.js +13 -12
- package/dist/schemas/guides.js.map +1 -1
- package/dist/schemas/index.d.ts +2 -2
- package/dist/schemas/index.d.ts.map +1 -1
- package/dist/schemas/index.js +18 -14
- package/dist/schemas/index.js.map +1 -1
- package/dist/schemas/library/social.d.ts +258 -0
- package/dist/schemas/library/social.d.ts.map +1 -1
- package/dist/schemas/library/social.js +28 -1
- package/dist/schemas/library/social.js.map +1 -1
- package/dist/session/account-hint.d.ts.map +1 -1
- package/dist/session/account-hint.js +8 -5
- package/dist/session/account-hint.js.map +1 -1
- package/dist/session/auth.d.ts +36 -2
- package/dist/session/auth.d.ts.map +1 -1
- package/dist/session/auth.js +95 -2
- package/dist/session/auth.js.map +1 -1
- package/dist/session/connect.d.ts +1 -0
- package/dist/session/connect.d.ts.map +1 -1
- package/dist/session/connect.js +3 -1
- package/dist/session/connect.js.map +1 -1
- package/dist/session/credentials.d.ts +8 -26
- package/dist/session/credentials.d.ts.map +1 -1
- package/dist/session/credentials.js +14 -32
- package/dist/session/credentials.js.map +1 -1
- package/dist/session/hosting.d.ts.map +1 -1
- package/dist/session/hosting.js +8 -2
- package/dist/session/hosting.js.map +1 -1
- package/dist/session/index.d.ts +1 -0
- package/dist/session/index.d.ts.map +1 -1
- package/dist/session/index.js +1 -0
- package/dist/session/index.js.map +1 -1
- package/dist/session/pairing.d.ts.map +1 -1
- package/dist/session/pairing.js +92 -88
- package/dist/session/pairing.js.map +1 -1
- package/dist/session/password-strength.d.ts.map +1 -1
- package/dist/session/password-strength.js +37 -37
- package/dist/session/password-strength.js.map +1 -1
- package/dist/session/places.d.ts.map +1 -1
- package/dist/session/places.js +18 -13
- package/dist/session/places.js.map +1 -1
- package/dist/session/popup.d.ts +13 -0
- package/dist/session/popup.d.ts.map +1 -0
- package/dist/session/popup.js +23 -0
- package/dist/session/popup.js.map +1 -0
- package/dist/space/constitution.d.ts.map +1 -1
- package/dist/space/constitution.js +22 -29
- package/dist/space/constitution.js.map +1 -1
- package/dist/space/grants.d.ts.map +1 -1
- package/dist/space/grants.js +37 -39
- package/dist/space/grants.js.map +1 -1
- package/dist/space/invite-link.d.ts.map +1 -1
- package/dist/space/invite-link.js +33 -33
- package/dist/space/invite-link.js.map +1 -1
- package/dist/space/own-automations.d.ts +13 -24
- package/dist/space/own-automations.d.ts.map +1 -1
- package/dist/space/own-automations.js +47 -37
- package/dist/space/own-automations.js.map +1 -1
- package/dist/space/presets.d.ts +2 -2
- package/dist/space/presets.d.ts.map +1 -1
- package/dist/space/presets.js +3 -2
- package/dist/space/presets.js.map +1 -1
- package/dist/space/roles.d.ts +56 -80
- package/dist/space/roles.d.ts.map +1 -1
- package/dist/space/roles.js +448 -441
- package/dist/space/roles.js.map +1 -1
- package/dist/space/space-access.d.ts +3 -4
- package/dist/space/space-access.d.ts.map +1 -1
- package/dist/space/space-access.js +3 -8
- package/dist/space/space-access.js.map +1 -1
- package/dist/storage/blob/directory.d.ts.map +1 -1
- package/dist/storage/blob/directory.js +39 -32
- package/dist/storage/blob/directory.js.map +1 -1
- package/dist/storage/blob/s3.d.ts.map +1 -1
- package/dist/storage/blob/s3.js +21 -19
- package/dist/storage/blob/s3.js.map +1 -1
- package/dist/storage/blob-store.d.ts +0 -9
- package/dist/storage/blob-store.d.ts.map +1 -1
- package/dist/storage/directory-access.d.ts +8 -32
- package/dist/storage/directory-access.d.ts.map +1 -1
- package/dist/storage/directory-access.js +11 -36
- package/dist/storage/directory-access.js.map +1 -1
- package/dist/storage/encrypted-adapter.d.ts +12 -32
- package/dist/storage/encrypted-adapter.d.ts.map +1 -1
- package/dist/storage/encrypted-adapter.js +31 -54
- package/dist/storage/encrypted-adapter.js.map +1 -1
- package/dist/storage/folder-adapter.d.ts +12 -26
- package/dist/storage/folder-adapter.d.ts.map +1 -1
- package/dist/storage/folder-adapter.js +37 -54
- package/dist/storage/folder-adapter.js.map +1 -1
- package/dist/storage/folder-reconcile.d.ts +12 -26
- package/dist/storage/folder-reconcile.d.ts.map +1 -1
- package/dist/storage/folder-reconcile.js +39 -48
- package/dist/storage/folder-reconcile.js.map +1 -1
- package/dist/storage/indexeddb-adapter.d.ts +2 -6
- package/dist/storage/indexeddb-adapter.d.ts.map +1 -1
- package/dist/storage/indexeddb-adapter.js +60 -114
- package/dist/storage/indexeddb-adapter.js.map +1 -1
- package/dist/storage/mirror.d.ts +0 -29
- package/dist/storage/mirror.d.ts.map +1 -1
- package/dist/storage/mirror.js +153 -110
- package/dist/storage/mirror.js.map +1 -1
- package/dist/storage/segment.d.ts.map +1 -1
- package/dist/storage/segment.js +3 -7
- package/dist/storage/segment.js.map +1 -1
- package/dist/storage/storage-provider.d.ts +6 -11
- package/dist/storage/storage-provider.d.ts.map +1 -1
- package/dist/storage/storage-provider.js +112 -116
- package/dist/storage/storage-provider.js.map +1 -1
- package/dist/sync/sync-engine.d.ts +10 -44
- package/dist/sync/sync-engine.d.ts.map +1 -1
- package/dist/sync/sync-engine.js +245 -247
- package/dist/sync/sync-engine.js.map +1 -1
- package/dist/sync/sync-messages.d.ts +1 -1
- package/dist/sync/sync-messages.d.ts.map +1 -1
- package/dist/sync/sync-messages.js +1 -1
- package/dist/sync/sync-messages.js.map +1 -1
- package/dist/utils/guards.d.ts +1 -0
- package/dist/utils/guards.d.ts.map +1 -1
- package/dist/utils/guards.js +1 -0
- package/dist/utils/guards.js.map +1 -1
- package/dist/utils/hmac.d.ts +5 -0
- package/dist/utils/hmac.d.ts.map +1 -0
- package/dist/utils/hmac.js +20 -0
- package/dist/utils/hmac.js.map +1 -0
- package/dist/utils/map.d.ts +10 -0
- package/dist/utils/map.d.ts.map +1 -0
- package/dist/utils/map.js +30 -0
- package/dist/utils/map.js.map +1 -0
- package/dist/utils/serial.d.ts +2 -0
- package/dist/utils/serial.d.ts.map +1 -1
- package/dist/utils/serial.js +9 -0
- package/dist/utils/serial.js.map +1 -1
- package/dist/validation/capability-gate.d.ts +7 -15
- package/dist/validation/capability-gate.d.ts.map +1 -1
- package/dist/validation/capability-gate.js +32 -36
- package/dist/validation/capability-gate.js.map +1 -1
- package/dist/validation/crypto-gate.d.ts.map +1 -1
- package/dist/validation/crypto-gate.js +12 -21
- package/dist/validation/crypto-gate.js.map +1 -1
- package/docs/README.md +1 -0
- package/docs/actions.md +32 -25
- package/docs/agents.md +28 -16
- package/docs/automations.md +1 -1
- package/docs/blueprints-and-guides.md +37 -5
- package/docs/building-an-app.md +7 -4
- package/docs/collections.md +3 -3
- package/docs/contacts.md +34 -1
- package/docs/contracts.md +1 -1
- package/docs/direct-messages.md +6 -2
- package/docs/inbox.md +5 -11
- package/docs/node.md +54 -22
- package/docs/records-and-queries.md +1 -1
- package/docs/sign-in.md +54 -26
- package/docs/spaces.md +9 -8
- package/docs/standard-library.md +37 -14
- package/docs/storage.md +5 -6
- package/docs/usernames.md +161 -0
- package/docs/warrants.md +1 -1
- package/package.json +1 -2
package/dist/node/host.js
CHANGED
|
@@ -12,7 +12,6 @@ import { createListeners } from '../utils/events.js';
|
|
|
12
12
|
const SPACE_SUBSCRIPTION_PREFIX = 'space:';
|
|
13
13
|
/** A space's own subscription id: `space:<space id>` */
|
|
14
14
|
export const spaceSubscription = (spaceId) => `${SPACE_SUBSCRIPTION_PREFIX}${spaceId}`;
|
|
15
|
-
const SUBSCRIPTION_PREFIX = 'subscription:';
|
|
16
15
|
/** The space a subscription is a space's own for, or null for an account's */
|
|
17
16
|
const spaceIdOf = (id) => id.startsWith(SPACE_SUBSCRIPTION_PREFIX) && id.length > SPACE_SUBSCRIPTION_PREFIX.length && id.length <= 200
|
|
18
17
|
? id.slice(SPACE_SUBSCRIPTION_PREFIX.length)
|
|
@@ -23,21 +22,13 @@ export class NotAllowedError extends Error {
|
|
|
23
22
|
super('This host only carries spaces for the accounts its owner named');
|
|
24
23
|
}
|
|
25
24
|
}
|
|
26
|
-
/** Where the subscriptions are kept in the bucket */
|
|
27
|
-
const BUCKET_PREFIX = 'host/subscriptions/';
|
|
28
|
-
/** Where each space's standing against the free line is kept, there and in the bucket */
|
|
29
|
-
const LINE_PREFIX = 'line:';
|
|
30
|
-
const LINE_BUCKET_PREFIX = 'host/lines/';
|
|
31
25
|
const DAY = 24 * 3600;
|
|
32
26
|
/** How far under every limit a space must stay, for its window, to be free again */
|
|
33
27
|
const UNDER = 0.8;
|
|
34
28
|
/** How long after the host first sees a space what arrives may be its history, rather than people writing now */
|
|
35
29
|
const CATCH_UP_SECONDS = 600;
|
|
36
|
-
/**
|
|
37
|
-
const STRUCK_PREFIX = 'struck:';
|
|
38
|
-
/** Days of notice a space gets when it goes over again after its grace was spent, before it is stopped */
|
|
30
|
+
/** Days of notice a space gets when it goes over again after its grace was spent */
|
|
39
31
|
const NOTICE_DAYS = 7;
|
|
40
|
-
const STRUCK_BUCKET_PREFIX = 'host/struck/';
|
|
41
32
|
/** Starts a host */
|
|
42
33
|
export async function createHostNode(config) {
|
|
43
34
|
const provider = config.provider ?? createP256Provider();
|
|
@@ -46,29 +37,33 @@ export async function createHostNode(config) {
|
|
|
46
37
|
const allowed = (account) => !config.allow || config.allow.includes(account);
|
|
47
38
|
const store = await config.stores('host');
|
|
48
39
|
const events = createListeners('host');
|
|
40
|
+
const limits = config.free ? null : (config.limits ?? null);
|
|
41
|
+
const lineGrace = (limits?.graceDays ?? 0) * DAY;
|
|
42
|
+
const keepSeconds = (limits?.keepDays ?? 0) * DAY;
|
|
43
|
+
// Each kind of record is kept in the host's store and in the bucket, which a new disk starts from.
|
|
44
|
+
const kept = {
|
|
45
|
+
subscriptions: shelf(store, config.mirror, 'subscription:', {
|
|
46
|
+
...bucketNames('host/subscriptions/', { encoded: true, suffix: '.json' }),
|
|
47
|
+
nameOf: (_key, bytes) => subscriptionIdIn(bytes),
|
|
48
|
+
}),
|
|
49
|
+
lines: shelf(store, config.mirror, 'line:', bucketNames('host/lines/', { suffix: '.json' })),
|
|
50
|
+
rooms: shelf(store, config.mirror, 'room:', bucketNames('host/rooms/', { encoded: true })),
|
|
51
|
+
struck: shelf(store, config.mirror, 'struck:', bucketNames('host/struck/')),
|
|
52
|
+
};
|
|
49
53
|
const read = async (id) => {
|
|
50
|
-
const
|
|
51
|
-
// Only `write`
|
|
54
|
+
const text = await kept.subscriptions.get(id);
|
|
55
|
+
// Only `write` puts anything here; a check would turn a damaged record into a missing one.
|
|
52
56
|
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- this host's own writes
|
|
53
|
-
return
|
|
57
|
+
return text ? JSON.parse(text) : null;
|
|
54
58
|
};
|
|
55
|
-
const bucketKey = (id) => `${BUCKET_PREFIX}${encodeURIComponent(id)}.json`;
|
|
56
59
|
const write = async (subscription) => {
|
|
57
|
-
|
|
58
|
-
await store.put(`${SUBSCRIPTION_PREFIX}${subscription.id}`, bytes);
|
|
59
|
-
// The bucket's copy is what a host with a new disk starts from.
|
|
60
|
-
await config.mirror?.put(bucketKey(subscription.id), bytes);
|
|
60
|
+
await kept.subscriptions.put(subscription.id, JSON.stringify(subscription));
|
|
61
61
|
return subscription;
|
|
62
62
|
};
|
|
63
|
-
const forget = async (id) => {
|
|
64
|
-
await store.delete(`${SUBSCRIPTION_PREFIX}${id}`);
|
|
65
|
-
await config.mirror?.delete(bucketKey(id));
|
|
66
|
-
};
|
|
67
63
|
const list = async () => {
|
|
68
|
-
const
|
|
69
|
-
return
|
|
64
|
+
const all = await Promise.all((await kept.subscriptions.names()).map(read));
|
|
65
|
+
return all.filter((subscription) => subscription !== null);
|
|
70
66
|
};
|
|
71
|
-
const limits = config.free ? null : (config.limits ?? null);
|
|
72
67
|
const state = (subscription) => {
|
|
73
68
|
if (config.free || subscription.paidUntil >= now())
|
|
74
69
|
return 'active';
|
|
@@ -78,7 +73,11 @@ export async function createHostNode(config) {
|
|
|
78
73
|
return subscription.paidUntil + graceSeconds >= now() ? 'grace' : 'lapsed';
|
|
79
74
|
};
|
|
80
75
|
/** Which room an account's subscription has, at a host with limits; null otherwise */
|
|
81
|
-
const plan = (subscription) =>
|
|
76
|
+
const plan = (subscription) => {
|
|
77
|
+
if (!limits || spaceIdOf(subscription.id))
|
|
78
|
+
return null;
|
|
79
|
+
return subscription.paidUntil >= now() ? 'paid' : 'free';
|
|
80
|
+
};
|
|
82
81
|
// ─── The free line ─────────────────────────────────────────────────
|
|
83
82
|
const meter = limits
|
|
84
83
|
? await createUsageMeter({
|
|
@@ -88,140 +87,77 @@ export async function createHostNode(config) {
|
|
|
88
87
|
})
|
|
89
88
|
: null;
|
|
90
89
|
const lines = new Map();
|
|
91
|
-
|
|
92
|
-
const
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
// A restart, or a new disk, keeps the grace clock where it was.
|
|
101
|
-
await config.mirror?.put(lineBucketKey(spaceId), bytes);
|
|
90
|
+
/** Each account over its room */
|
|
91
|
+
const rooms = new Map();
|
|
92
|
+
/** Spaces members' subscriptions once stopped carrying, and when: one back over the line gets no new grace */
|
|
93
|
+
const struck = new Map();
|
|
94
|
+
if (limits) {
|
|
95
|
+
for (const [spaceId, text] of await kept.lines.load()) {
|
|
96
|
+
const line = parseLine(JSON.parse(text));
|
|
97
|
+
if (line)
|
|
98
|
+
lines.set(spaceId, line);
|
|
102
99
|
}
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
100
|
+
for (const [account, text] of await kept.rooms.load()) {
|
|
101
|
+
const room = parseRoom(JSON.parse(text));
|
|
102
|
+
if (room)
|
|
103
|
+
rooms.set(account, room);
|
|
106
104
|
}
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
const bytes = await config.mirror.get(key);
|
|
112
|
-
const spaceId = key.slice(LINE_BUCKET_PREFIX.length, -'.json'.length);
|
|
113
|
-
if (bytes && spaceId)
|
|
114
|
-
await store.put(`${LINE_PREFIX}${spaceId}`, bytes);
|
|
115
|
-
}
|
|
116
|
-
for (const key of await store.list(LINE_PREFIX)) {
|
|
117
|
-
const bytes = await store.get(key);
|
|
118
|
-
const line = bytes ? JSON.parse(utf8Decode(bytes)) : null;
|
|
119
|
-
if (isRecord(line) &&
|
|
120
|
-
typeof line.since === 'number' &&
|
|
121
|
-
typeof line.from === 'number' &&
|
|
122
|
-
Array.isArray(line.reasons))
|
|
123
|
-
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- this host's own writes, shape checked
|
|
124
|
-
lines.set(key.slice(LINE_PREFIX.length), line);
|
|
105
|
+
for (const [spaceId, text] of await kept.struck.load()) {
|
|
106
|
+
const at = Number(text);
|
|
107
|
+
if (Number.isFinite(at))
|
|
108
|
+
struck.set(spaceId, at);
|
|
125
109
|
}
|
|
126
110
|
}
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
const bytes = await config.mirror.get(key);
|
|
136
|
-
// Named in the bucket by the account DID, encoded: the store by the DID itself.
|
|
137
|
-
if (bytes)
|
|
138
|
-
await store.put(`${ROOM_PREFIX}${decodeURIComponent(key.slice(ROOM_BUCKET_PREFIX.length))}`, bytes);
|
|
139
|
-
}
|
|
140
|
-
for (const key of await store.list(ROOM_PREFIX)) {
|
|
141
|
-
const bytes = await store.get(key);
|
|
142
|
-
const room = bytes ? JSON.parse(utf8Decode(bytes)) : null;
|
|
143
|
-
if (isRecord(room) && typeof room.full === 'number') {
|
|
144
|
-
const held = {};
|
|
145
|
-
if (isRecord(room.held))
|
|
146
|
-
for (const [spaceId, since] of Object.entries(room.held))
|
|
147
|
-
if (typeof since === 'number')
|
|
148
|
-
held[spaceId] = since;
|
|
149
|
-
rooms.set(key.slice(ROOM_PREFIX.length), {
|
|
150
|
-
full: room.full,
|
|
151
|
-
...(typeof room.fits === 'number' ? { fits: room.fits } : {}),
|
|
152
|
-
...(room.refused === true ? { refused: true } : {}),
|
|
153
|
-
...(Object.keys(held).length ? { held } : {}),
|
|
154
|
-
});
|
|
155
|
-
}
|
|
111
|
+
async function writeLine(spaceId, line) {
|
|
112
|
+
if (line) {
|
|
113
|
+
lines.set(spaceId, line);
|
|
114
|
+
await kept.lines.put(spaceId, JSON.stringify(line));
|
|
115
|
+
}
|
|
116
|
+
else {
|
|
117
|
+
lines.delete(spaceId);
|
|
118
|
+
await kept.lines.delete(spaceId);
|
|
156
119
|
}
|
|
157
120
|
}
|
|
158
|
-
|
|
159
|
-
if (
|
|
121
|
+
async function writeRoom(account, room) {
|
|
122
|
+
if (sameJson(room, rooms.get(account) ?? null))
|
|
160
123
|
return;
|
|
161
|
-
const key = encodeURIComponent(account);
|
|
162
124
|
if (room) {
|
|
163
125
|
rooms.set(account, room);
|
|
164
|
-
|
|
165
|
-
await store.put(`${ROOM_PREFIX}${account}`, bytes);
|
|
166
|
-
await config.mirror?.put(`${ROOM_BUCKET_PREFIX}${key}`, bytes);
|
|
126
|
+
await kept.rooms.put(account, JSON.stringify(room));
|
|
167
127
|
}
|
|
168
128
|
else {
|
|
169
129
|
rooms.delete(account);
|
|
170
|
-
await
|
|
171
|
-
await config.mirror?.delete(`${ROOM_BUCKET_PREFIX}${key}`);
|
|
130
|
+
await kept.rooms.delete(account);
|
|
172
131
|
}
|
|
173
|
-
}
|
|
174
|
-
|
|
132
|
+
}
|
|
133
|
+
async function strike(spaceId, at) {
|
|
134
|
+
struck.set(spaceId, at);
|
|
135
|
+
await kept.struck.put(spaceId, String(at));
|
|
136
|
+
}
|
|
137
|
+
async function unstrike(spaceId) {
|
|
138
|
+
if (!struck.delete(spaceId))
|
|
139
|
+
return;
|
|
140
|
+
await kept.struck.delete(spaceId);
|
|
141
|
+
}
|
|
175
142
|
const accountOf = (subscription) => subscription.carry?.account;
|
|
143
|
+
const roomOf = (subscription) => rooms.get(accountOf(subscription) ?? '');
|
|
176
144
|
/** Whether a subscription's account is past its notice of being over its room */
|
|
177
145
|
const outOfRoom = (subscription) => {
|
|
178
|
-
const room =
|
|
146
|
+
const room = roomOf(subscription);
|
|
179
147
|
return room !== undefined && room.full + lineGrace <= now();
|
|
180
148
|
};
|
|
181
|
-
/** Each carry space's spaces held back for want of its room
|
|
149
|
+
/** Each carry space's spaces held back for want of its room */
|
|
182
150
|
const holds = new Map();
|
|
183
151
|
/** Those whose copy the host still keeps: held back less than `keepDays` for some account */
|
|
184
152
|
const roomHeld = new Set();
|
|
185
153
|
/** Held back longer, their copy already deleted: not deleted again until the host restarts */
|
|
186
154
|
const dropped = new Set();
|
|
187
155
|
/** Spaces members' subscriptions no longer carry, being over the line with nobody paying */
|
|
188
|
-
const skipped = new Set([...lines].
|
|
189
|
-
/**
|
|
190
|
-
* Spaces members' subscriptions once stopped carrying, and when: one that
|
|
191
|
-
* comes back over the line after the host forgot it gets no new grace.
|
|
192
|
-
*/
|
|
193
|
-
const struck = new Map();
|
|
194
|
-
const strike = async (spaceId, at) => {
|
|
195
|
-
struck.set(spaceId, at);
|
|
196
|
-
const bytes = utf8Encode(String(at));
|
|
197
|
-
await store.put(`${STRUCK_PREFIX}${spaceId}`, bytes);
|
|
198
|
-
await config.mirror?.put(`${STRUCK_BUCKET_PREFIX}${spaceId}`, bytes);
|
|
199
|
-
};
|
|
200
|
-
const unstrike = async (spaceId) => {
|
|
201
|
-
if (!struck.delete(spaceId))
|
|
202
|
-
return;
|
|
203
|
-
await store.delete(`${STRUCK_PREFIX}${spaceId}`);
|
|
204
|
-
await config.mirror?.delete(`${STRUCK_BUCKET_PREFIX}${spaceId}`);
|
|
205
|
-
};
|
|
206
|
-
if (limits) {
|
|
207
|
-
if (config.mirror && (await store.list(STRUCK_PREFIX)).length === 0)
|
|
208
|
-
for (const key of await config.mirror.list(STRUCK_BUCKET_PREFIX)) {
|
|
209
|
-
const bytes = await config.mirror.get(key);
|
|
210
|
-
if (bytes)
|
|
211
|
-
await store.put(`${STRUCK_PREFIX}${key.slice(STRUCK_BUCKET_PREFIX.length)}`, bytes);
|
|
212
|
-
}
|
|
213
|
-
for (const key of await store.list(STRUCK_PREFIX)) {
|
|
214
|
-
const bytes = await store.get(key);
|
|
215
|
-
const at = bytes ? Number(utf8Decode(bytes)) : NaN;
|
|
216
|
-
if (Number.isFinite(at))
|
|
217
|
-
struck.set(key.slice(STRUCK_PREFIX.length), at);
|
|
218
|
-
}
|
|
219
|
-
}
|
|
156
|
+
const skipped = new Set([...lines].filter(([, line]) => line.stopped !== undefined).map(([id]) => id));
|
|
220
157
|
/**
|
|
221
158
|
* A version reached a carried space: its account wrote there, unless it is
|
|
222
|
-
* the host's own. Counted by when it arrived,
|
|
223
|
-
*
|
|
224
|
-
* then what says it is older than the window is its history.
|
|
159
|
+
* the host's own. Counted by when it arrived, except while the host first
|
|
160
|
+
* catches up on a space: then what is older than the window is history.
|
|
225
161
|
*/
|
|
226
162
|
const counted = (spaceId, version) => {
|
|
227
163
|
if (!meter || !limits)
|
|
@@ -229,20 +165,22 @@ export async function createHostNode(config) {
|
|
|
229
165
|
const account = version.proof?.payload.iss ?? version.author;
|
|
230
166
|
const written = Date.parse(version.createdAt) / 1000;
|
|
231
167
|
const old = Number.isFinite(written) && written < now() - limits.space.windowDays * DAY;
|
|
232
|
-
|
|
233
|
-
const
|
|
234
|
-
if (old &&
|
|
168
|
+
const count = async () => {
|
|
169
|
+
const catchingUp = now() - (await meter.firstSeen(spaceId)) < CATCH_UP_SECONDS;
|
|
170
|
+
if (old && catchingUp)
|
|
235
171
|
return;
|
|
236
172
|
if (!(await config.ignore?.(account)))
|
|
237
173
|
await meter.wrote(spaceId, account);
|
|
238
|
-
}
|
|
174
|
+
};
|
|
175
|
+
void count().catch(() => { });
|
|
239
176
|
};
|
|
240
177
|
/** Whether anyone but the account holds a role in its carry space: then it is a shared space */
|
|
241
178
|
const sharedCarry = async (carrySpace, account) => {
|
|
242
179
|
const entry = core.carried.get(carrySpace);
|
|
243
180
|
if (!entry)
|
|
244
181
|
return false;
|
|
245
|
-
|
|
182
|
+
const { members } = await entry.runtime.access();
|
|
183
|
+
return members.some((member) => member.did !== account && (member.founder || member.roles.length > 0));
|
|
246
184
|
};
|
|
247
185
|
/** Stops a carry space being any subscription's */
|
|
248
186
|
const letCarryGo = async (carrySpace) => {
|
|
@@ -261,18 +199,8 @@ export async function createHostNode(config) {
|
|
|
261
199
|
return undefined;
|
|
262
200
|
return (await entry.runtime.access()).members.map((member) => member.did);
|
|
263
201
|
};
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
for (const key of await config.mirror.list(BUCKET_PREFIX)) {
|
|
267
|
-
const bytes = await config.mirror.get(key);
|
|
268
|
-
if (!bytes)
|
|
269
|
-
continue;
|
|
270
|
-
const subscription = JSON.parse(utf8Decode(bytes));
|
|
271
|
-
if (isRecord(subscription) && typeof subscription.id === 'string')
|
|
272
|
-
await store.put(`${SUBSCRIPTION_PREFIX}${subscription.id}`, bytes);
|
|
273
|
-
}
|
|
274
|
-
}
|
|
275
|
-
/** The subscription an account's carry space belongs to, when it wrote `carry:closed` */
|
|
202
|
+
await kept.subscriptions.restore();
|
|
203
|
+
/** The account's carry space being let go, after it wrote `carry:closed` */
|
|
276
204
|
let closing = Promise.resolve();
|
|
277
205
|
/** Whether the carry core below is made, so its events may look at it */
|
|
278
206
|
let opened = false;
|
|
@@ -298,79 +226,67 @@ export async function createHostNode(config) {
|
|
|
298
226
|
}
|
|
299
227
|
: {}),
|
|
300
228
|
...(config.spacesPerCarry !== undefined ? { most: config.spacesPerCarry } : {}),
|
|
301
|
-
...(config.mirror
|
|
302
|
-
|
|
303
|
-
mirror: config.mirror,
|
|
304
|
-
// One stopped for being over the line is kept a while, so a fund brings it back as it was.
|
|
305
|
-
onRelease: async (spaceId) => {
|
|
306
|
-
if (!skipped.has(spaceId) && !roomHeld.has(spaceId))
|
|
307
|
-
await deleteMirrored(config.mirror, spaceId);
|
|
308
|
-
},
|
|
309
|
-
}
|
|
310
|
-
: {}),
|
|
229
|
+
...(config.mirror ? { mirror: config.mirror, onRelease: releaseMirrored(config.mirror) } : {}),
|
|
230
|
+
// The account stopped using this host: forget its carry space; the subscription stays paid.
|
|
311
231
|
onClosed: (carrySpace) => {
|
|
312
|
-
|
|
313
|
-
closing = closing.then(async () => {
|
|
314
|
-
for (const subscription of await list()) {
|
|
315
|
-
if (subscription.carry?.space !== carrySpace)
|
|
316
|
-
continue;
|
|
317
|
-
const { carry: _gone, ...rest } = subscription;
|
|
318
|
-
await write(rest);
|
|
319
|
-
}
|
|
320
|
-
await core.removeCarry(carrySpace);
|
|
321
|
-
});
|
|
232
|
+
closing = closing.then(() => letCarryGo(carrySpace));
|
|
322
233
|
},
|
|
323
234
|
});
|
|
324
235
|
opened = true;
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
236
|
+
/** One stopped for being over the line is kept a while, so a fund brings it back as it was */
|
|
237
|
+
function releaseMirrored(mirror) {
|
|
238
|
+
return async (spaceId) => {
|
|
239
|
+
if (!skipped.has(spaceId) && !roomHeld.has(spaceId))
|
|
240
|
+
await deleteMirrored(mirror, spaceId);
|
|
241
|
+
};
|
|
242
|
+
}
|
|
243
|
+
await carryAgain();
|
|
244
|
+
/** Carries again what was carried before a restart: the store caches the spaces, but the list is ours */
|
|
245
|
+
async function carryAgain() {
|
|
246
|
+
for (const subscription of await list()) {
|
|
247
|
+
if (state(subscription) === 'lapsed')
|
|
248
|
+
continue;
|
|
249
|
+
const fail = (error) => console.error(`Could not carry for subscription ${subscription.id}:`, error);
|
|
250
|
+
if (subscription.pass !== undefined)
|
|
251
|
+
await core.addPass(subscription.pass).catch(fail);
|
|
252
|
+
// An account taken off the list since is not carried again.
|
|
253
|
+
else if (subscription.carry && allowed(subscription.carry.account))
|
|
254
|
+
await core.addCarry(subscription.carry.account, subscription.carry.invite).catch(fail);
|
|
332
255
|
}
|
|
333
|
-
// An account taken off the list since is not carried again.
|
|
334
|
-
if (!subscription.carry || state(subscription) === 'lapsed' || !allowed(subscription.carry.account))
|
|
335
|
-
continue;
|
|
336
|
-
await core.addCarry(subscription.carry.account, subscription.carry.invite).catch((error) => {
|
|
337
|
-
console.error(`Could not carry for subscription ${subscription.id}:`, error);
|
|
338
|
-
});
|
|
339
256
|
}
|
|
340
|
-
/**
|
|
341
|
-
const
|
|
342
|
-
|
|
257
|
+
/** Stops carrying a carry space, unless another subscription still carries it */
|
|
258
|
+
const releaseCarry = async (carrySpace, except) => {
|
|
259
|
+
const others = await list();
|
|
260
|
+
if (others.some((other) => other.id !== except && other.carry?.space === carrySpace))
|
|
261
|
+
return;
|
|
262
|
+
await core.removeCarry(carrySpace);
|
|
263
|
+
};
|
|
264
|
+
/** Whether a space's own subscription pays for it now: the free line does not apply then */
|
|
343
265
|
const paysItself = async (spaceId) => {
|
|
344
|
-
const own = await read(
|
|
266
|
+
const own = await read(spaceSubscription(spaceId));
|
|
345
267
|
return !!own && state(own) === 'active';
|
|
346
268
|
};
|
|
347
|
-
/** The spaces the free line is about: those accounts' passes name, and
|
|
269
|
+
/** The spaces the free line is about: those accounts' passes name, and the carry spaces too */
|
|
348
270
|
const metered = () => {
|
|
349
|
-
const
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
found.add(spaceId);
|
|
353
|
-
// Carry spaces too: an account's own has one writer, and a shared space handed over as one is held to the line.
|
|
354
|
-
for (const spaceId of core.carried.keys())
|
|
355
|
-
found.add(spaceId);
|
|
356
|
-
return found;
|
|
271
|
+
const named = [...core.carries.values()].flatMap((entry) => [...entry.named]);
|
|
272
|
+
// An account's own carry space has one writer; a shared space handed over as one is held to the line.
|
|
273
|
+
return new Set([...named, ...core.carried.keys()]);
|
|
357
274
|
};
|
|
275
|
+
const measure = async (spaceId) => (await config.measure?.(spaceId).catch(() => 0)) ?? 0;
|
|
358
276
|
/** Where one space stands now: its counts, its size, and the line it is held to */
|
|
359
277
|
async function standing(spaceId, before, own) {
|
|
360
278
|
if (!limits || !meter)
|
|
361
279
|
return null;
|
|
362
280
|
const t = now();
|
|
363
|
-
|
|
364
|
-
const keep = (limits.keepDays ?? 0) * DAY;
|
|
365
|
-
// Members stopped carrying it, so nothing it does reaches here: it stays as it was, and is forgotten after a while.
|
|
281
|
+
// Members stopped carrying it, so nothing it does reaches here: it stays, and is forgotten after a while.
|
|
366
282
|
if (before?.stopped !== undefined && !own)
|
|
367
|
-
return before.stopped +
|
|
368
|
-
// Not carried now, so
|
|
283
|
+
return before.stopped + keepSeconds <= t ? null : before;
|
|
284
|
+
// Not carried now, so its members can't be read: it stays as it was until it is.
|
|
369
285
|
const members = await membersOf(spaceId);
|
|
370
286
|
if (!members)
|
|
371
287
|
return before ?? null;
|
|
372
288
|
const counts = await meter.usage(spaceId, members);
|
|
373
|
-
const bytes =
|
|
289
|
+
const bytes = await measure(spaceId);
|
|
374
290
|
const shared = counts.writers >= 2;
|
|
375
291
|
// One writer is never a community, whatever the limit says.
|
|
376
292
|
const most = Math.max(limits.space.writers, 1);
|
|
@@ -381,18 +297,17 @@ export async function createHostNode(config) {
|
|
|
381
297
|
reasons.push('bytes');
|
|
382
298
|
const wellUnder = counts.writers <= most * UNDER && (!shared || bytes <= limits.space.bytes * UNDER);
|
|
383
299
|
let line = before ?? null;
|
|
384
|
-
// Its grace was spent once, by a stop or by running out while it was under: over again,
|
|
385
|
-
// it gets only a short notice, never a whole grace again.
|
|
386
|
-
const notice = t - lineGrace + NOTICE_DAYS * DAY;
|
|
387
|
-
const spent = before ? before.calm === true && before.from + lineGrace <= t : struck.has(spaceId);
|
|
388
300
|
if (reasons.length > 0) {
|
|
301
|
+
// A grace spent once, by a stop or by running out while under, buys only a short notice next time.
|
|
302
|
+
const notice = t - lineGrace + NOTICE_DAYS * DAY;
|
|
303
|
+
const spent = before ? before.calm === true && before.from + lineGrace <= t : struck.has(spaceId);
|
|
389
304
|
const from = spent ? Math.max(before?.from ?? notice, notice) : (before?.from ?? t);
|
|
390
305
|
line = { since: before?.since ?? t, from, reasons, lastOver: t };
|
|
391
306
|
}
|
|
392
307
|
else if (line && wellUnder) {
|
|
393
308
|
const under = line.under ?? t;
|
|
394
309
|
// Free again only after a whole window well under, so a busy week does not flip it.
|
|
395
|
-
if (t - under >=
|
|
310
|
+
if (t - under >= limits.space.windowDays * DAY)
|
|
396
311
|
return null;
|
|
397
312
|
line = { ...line, calm: true, under };
|
|
398
313
|
}
|
|
@@ -402,100 +317,86 @@ export async function createHostNode(config) {
|
|
|
402
317
|
}
|
|
403
318
|
if (!line)
|
|
404
319
|
return null;
|
|
405
|
-
// While it pays for itself the line does not apply
|
|
406
|
-
// a while does not buy it again, so once the fund stops, what is left of it is all it has.
|
|
320
|
+
// While it pays for itself the line does not apply, but its grace is not given again.
|
|
407
321
|
if (own) {
|
|
408
322
|
const { stopped: _paid, ...rest } = line;
|
|
409
323
|
return rest;
|
|
410
324
|
}
|
|
411
|
-
// Stopped once its grace runs out if it is over then, or was
|
|
412
|
-
//
|
|
413
|
-
// under for an hour as the date passes buys nothing.
|
|
325
|
+
// Stopped once its grace runs out if it is over then, or was in the last days before:
|
|
326
|
+
// going under for an hour as the date passes buys nothing.
|
|
414
327
|
const until = line.from + lineGrace;
|
|
415
328
|
const lately = (line.lastOver ?? 0) >= until - NOTICE_DAYS * DAY;
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
: line;
|
|
329
|
+
const stops = (reasons.length > 0 || lately) && until <= t && line.stopped === undefined;
|
|
330
|
+
return stops ? { ...line, stopped: t } : line;
|
|
419
331
|
}
|
|
420
332
|
/**
|
|
421
|
-
*
|
|
422
|
-
* those past it. Run now and then. Says which spaces went over, stopped
|
|
423
|
-
*
|
|
333
|
+
* Holds each space to the free line, and lets members' subscriptions go of
|
|
334
|
+
* those past it. Run now and then. Says which spaces went over, stopped, or
|
|
335
|
+
* came back under.
|
|
424
336
|
*/
|
|
425
337
|
async function evaluate() {
|
|
426
|
-
const changed = {
|
|
427
|
-
over: [],
|
|
428
|
-
stopped: [],
|
|
429
|
-
under: [],
|
|
430
|
-
};
|
|
338
|
+
const changed = { over: [], stopped: [], under: [] };
|
|
431
339
|
if (!limits || !meter)
|
|
432
340
|
return changed;
|
|
433
341
|
await meter.prune();
|
|
434
342
|
const spaces = metered();
|
|
435
343
|
for (const spaceId of new Set([...spaces, ...lines.keys()]))
|
|
436
344
|
await evaluateOne(spaceId, spaces, changed).catch((error) => {
|
|
437
|
-
//
|
|
345
|
+
// Judged again next time; the others go on.
|
|
438
346
|
console.error(`Could not hold space ${spaceId} to the free line:`, error);
|
|
439
347
|
});
|
|
440
348
|
await core.refresh();
|
|
441
349
|
return changed;
|
|
442
350
|
}
|
|
443
351
|
async function evaluateOne(spaceId, spaces, changed) {
|
|
444
|
-
if (!meter)
|
|
352
|
+
if (!meter || !limits)
|
|
445
353
|
return;
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
await strike(spaceId, after.stopped);
|
|
466
|
-
// A shared space handed over as someone's carry space: it stops being theirs to carry.
|
|
467
|
-
// Skipped first, so letting it go keeps the bucket's copy.
|
|
468
|
-
skipped.add(spaceId);
|
|
469
|
-
if (core.carries.has(spaceId))
|
|
470
|
-
await letCarryGo(spaceId);
|
|
471
|
-
}
|
|
472
|
-
// A carry space someone else came to hold a role in is let go: it is a shared space.
|
|
473
|
-
const account = core.carries.get(spaceId)?.account;
|
|
474
|
-
if (account && (await sharedCarry(spaceId, account)))
|
|
354
|
+
const before = lines.get(spaceId);
|
|
355
|
+
const own = await paysItself(spaceId);
|
|
356
|
+
// A space nobody names for a while keeps its line, never dropped because it was let go for an hour.
|
|
357
|
+
const after = spaces.has(spaceId) || before ? await standing(spaceId, before, own) : null;
|
|
358
|
+
// A stop long forgotten, and a window more under: its grace is its own again.
|
|
359
|
+
const struckAt = struck.get(spaceId);
|
|
360
|
+
const forgotten = ((limits.keepDays ?? 0) + limits.space.windowDays) * DAY;
|
|
361
|
+
if (!before && !after && struckAt !== undefined && struckAt + forgotten <= now())
|
|
362
|
+
await unstrike(spaceId);
|
|
363
|
+
if (!sameJson(after, before ?? null))
|
|
364
|
+
await writeLine(spaceId, after);
|
|
365
|
+
if (after && !before)
|
|
366
|
+
changed.over.push(spaceId);
|
|
367
|
+
if (after?.stopped !== undefined && before?.stopped === undefined) {
|
|
368
|
+
changed.stopped.push(spaceId);
|
|
369
|
+
await strike(spaceId, after.stopped);
|
|
370
|
+
// Skipped first, so letting a carry space go keeps the bucket's copy.
|
|
371
|
+
skipped.add(spaceId);
|
|
372
|
+
if (core.carries.has(spaceId))
|
|
475
373
|
await letCarryGo(spaceId);
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
374
|
+
}
|
|
375
|
+
// A carry space someone else came to hold a role in is a shared space.
|
|
376
|
+
const account = core.carries.get(spaceId)?.account;
|
|
377
|
+
if (account && (await sharedCarry(spaceId, account)))
|
|
378
|
+
await letCarryGo(spaceId);
|
|
379
|
+
if (before && !after)
|
|
380
|
+
changed.under.push(spaceId);
|
|
381
|
+
if (before && !after && before.stopped === undefined)
|
|
382
|
+
await unstrike(spaceId);
|
|
383
|
+
if (after?.stopped !== undefined && !own)
|
|
384
|
+
skipped.add(spaceId);
|
|
385
|
+
else
|
|
386
|
+
skipped.delete(spaceId);
|
|
387
|
+
// Forgotten after it was kept a while: the host's copy goes too, unless something carries it.
|
|
388
|
+
if (before?.stopped !== undefined && !after) {
|
|
389
|
+
await meter.forget(spaceId);
|
|
390
|
+
if (config.mirror && !core.carried.has(spaceId))
|
|
391
|
+
await deleteMirrored(config.mirror, spaceId).catch(() => { });
|
|
492
392
|
}
|
|
493
393
|
}
|
|
494
394
|
/** The spaces over the line among these, as a status says them */
|
|
495
395
|
const overOf = (spaceIds) => [...spaceIds].flatMap((spaceId) => {
|
|
496
396
|
const line = lines.get(spaceId);
|
|
497
|
-
// One back under and carried
|
|
498
|
-
|
|
397
|
+
// One back under and carried keeps its line quietly, until free again.
|
|
398
|
+
const over = line && (!line.calm || line.stopped !== undefined);
|
|
399
|
+
return over
|
|
499
400
|
? [{ space: spaceId, since: line.since, until: line.from + lineGrace, reasons: line.reasons }]
|
|
500
401
|
: [];
|
|
501
402
|
});
|
|
@@ -505,6 +406,37 @@ export async function createHostNode(config) {
|
|
|
505
406
|
throw new Error('A subscription is named by its key, or by the space it is for');
|
|
506
407
|
return (await read(id)) ?? write({ id, paidUntil: 0, since: now() });
|
|
507
408
|
};
|
|
409
|
+
/** Which spaces are held back now, in a form to compare */
|
|
410
|
+
const holdsSignature = () => [...holds]
|
|
411
|
+
.map(([space, held]) => `${space}:${[...held].sort().join(',')}`)
|
|
412
|
+
.sort()
|
|
413
|
+
.join(' ');
|
|
414
|
+
/** The spaces some account holds back for less than `keepDays`, whose copy the host keeps */
|
|
415
|
+
function recountRoomHeld(t) {
|
|
416
|
+
roomHeld.clear();
|
|
417
|
+
for (const room of rooms.values())
|
|
418
|
+
for (const [spaceId, since] of Object.entries(room.held ?? {}))
|
|
419
|
+
if (since + keepSeconds > t)
|
|
420
|
+
roomHeld.add(spaceId);
|
|
421
|
+
}
|
|
422
|
+
/** Held back longer than `keepDays` for every account that holds it: the host's copy goes, as for a stop */
|
|
423
|
+
async function dropLongHeld() {
|
|
424
|
+
for (const held of holds.values())
|
|
425
|
+
for (const spaceId of held) {
|
|
426
|
+
if (roomHeld.has(spaceId) ||
|
|
427
|
+
dropped.has(spaceId) ||
|
|
428
|
+
skipped.has(spaceId) ||
|
|
429
|
+
core.carried.has(spaceId))
|
|
430
|
+
continue;
|
|
431
|
+
dropped.add(spaceId);
|
|
432
|
+
if (config.mirror)
|
|
433
|
+
await deleteMirrored(config.mirror, spaceId).catch(() => { });
|
|
434
|
+
}
|
|
435
|
+
// Carried again: should it be held that long again, its copy goes again.
|
|
436
|
+
for (const spaceId of dropped)
|
|
437
|
+
if (core.carried.has(spaceId))
|
|
438
|
+
dropped.delete(spaceId);
|
|
439
|
+
}
|
|
508
440
|
return Object.freeze({
|
|
509
441
|
did: core.did,
|
|
510
442
|
/** The short links its accounts keep, under a mailbox topic: what its relay answers besides drops */
|
|
@@ -519,9 +451,8 @@ export async function createHostNode(config) {
|
|
|
519
451
|
/** The limits it keeps to, or null when it has none */
|
|
520
452
|
limits,
|
|
521
453
|
/**
|
|
522
|
-
* Whether a subscription's spaces take more than its room
|
|
523
|
-
*
|
|
524
|
-
* a whole window of fitting, so a dip under for an hour starts nothing anew.
|
|
454
|
+
* Whether a subscription's spaces take more than its room. The notice runs
|
|
455
|
+
* from when they first did, and goes only after a whole window of fitting.
|
|
525
456
|
*/
|
|
526
457
|
async setFull(id, over) {
|
|
527
458
|
const subscription = await read(id);
|
|
@@ -530,27 +461,24 @@ export async function createHostNode(config) {
|
|
|
530
461
|
return;
|
|
531
462
|
const room = rooms.get(account);
|
|
532
463
|
const t = now();
|
|
533
|
-
if (over)
|
|
464
|
+
if (over) {
|
|
534
465
|
await writeRoom(account, {
|
|
535
466
|
full: room?.full ?? t,
|
|
536
467
|
...(room?.refused ? { refused: true } : {}),
|
|
537
468
|
...(room?.held ? { held: room.held } : {}),
|
|
538
469
|
});
|
|
539
|
-
|
|
540
|
-
const fits = room.fits ?? t;
|
|
541
|
-
// Fitting a whole window: the notice goes, though a refusal stays until the host's owner says.
|
|
542
|
-
if (t - fits >= limits.space.windowDays * DAY && !room.refused)
|
|
543
|
-
await writeRoom(account, null);
|
|
544
|
-
else
|
|
545
|
-
await writeRoom(account, { ...room, fits });
|
|
470
|
+
return;
|
|
546
471
|
}
|
|
472
|
+
if (!room)
|
|
473
|
+
return;
|
|
474
|
+
const fits = room.fits ?? t;
|
|
475
|
+
// A refusal stays until the host's owner says.
|
|
476
|
+
const fitWholeWindow = t - fits >= limits.space.windowDays * DAY && !room.refused;
|
|
477
|
+
await writeRoom(account, fitWholeWindow ? null : { ...room, fits });
|
|
547
478
|
},
|
|
548
479
|
/** Whether a subscription is past its notice of being over its room */
|
|
549
480
|
outOfRoom,
|
|
550
|
-
/**
|
|
551
|
-
* Holds back these solo spaces of a subscription's for want of room, and
|
|
552
|
-
* no others of its: the host keeps them, unserved, until they fit.
|
|
553
|
-
*/
|
|
481
|
+
/** Holds back these solo spaces of a subscription's for want of room, and no others: kept, unserved, until they fit */
|
|
554
482
|
async holdForRoom(id, spaceIds) {
|
|
555
483
|
const subscription = await read(id);
|
|
556
484
|
const carrySpace = subscription?.carry?.space;
|
|
@@ -564,47 +492,17 @@ export async function createHostNode(config) {
|
|
|
564
492
|
const held = Object.fromEntries(spaceIds.map((spaceId) => [spaceId, before[spaceId] ?? t]));
|
|
565
493
|
await writeRoom(account, Object.keys(held).length ? { ...rest, held } : rest);
|
|
566
494
|
}
|
|
567
|
-
const before =
|
|
568
|
-
.map(([space, held]) => `${space}:${[...held].sort().join(',')}`)
|
|
569
|
-
.sort()
|
|
570
|
-
.join(' ');
|
|
495
|
+
const before = holdsSignature();
|
|
571
496
|
if (spaceIds.length)
|
|
572
497
|
holds.set(carrySpace, new Set(spaceIds));
|
|
573
498
|
else
|
|
574
499
|
holds.delete(carrySpace);
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
for (const [spaceId, since] of Object.entries(one.held ?? {}))
|
|
579
|
-
if (since + keep > t)
|
|
580
|
-
roomHeld.add(spaceId);
|
|
581
|
-
// Held back longer than that for every account that holds it: the host's copy goes, as for a stop.
|
|
582
|
-
for (const held of holds.values())
|
|
583
|
-
for (const spaceId of held) {
|
|
584
|
-
if (roomHeld.has(spaceId) ||
|
|
585
|
-
dropped.has(spaceId) ||
|
|
586
|
-
skipped.has(spaceId) ||
|
|
587
|
-
core.carried.has(spaceId))
|
|
588
|
-
continue;
|
|
589
|
-
dropped.add(spaceId);
|
|
590
|
-
if (config.mirror)
|
|
591
|
-
await deleteMirrored(config.mirror, spaceId).catch(() => { });
|
|
592
|
-
}
|
|
593
|
-
// Carried again, by room made or someone else: should it be held that long again, its copy goes again.
|
|
594
|
-
for (const spaceId of dropped)
|
|
595
|
-
if (core.carried.has(spaceId))
|
|
596
|
-
dropped.delete(spaceId);
|
|
597
|
-
const after = [...holds]
|
|
598
|
-
.map(([space, held]) => `${space}:${[...held].sort().join(',')}`)
|
|
599
|
-
.sort()
|
|
600
|
-
.join(' ');
|
|
601
|
-
if (after !== before)
|
|
500
|
+
recountRoomHeld(t);
|
|
501
|
+
await dropLongHeld();
|
|
502
|
+
if (holdsSignature() !== before)
|
|
602
503
|
await core.refresh();
|
|
603
504
|
},
|
|
604
|
-
/**
|
|
605
|
-
* Stops carrying a subscription's carry space, which is over its room by
|
|
606
|
-
* itself past its notice, and turns it away until the host's owner says.
|
|
607
|
-
*/
|
|
505
|
+
/** Stops carrying a carry space over its room by itself past its notice, until the host's owner says */
|
|
608
506
|
async refuse(id) {
|
|
609
507
|
const subscription = await read(id);
|
|
610
508
|
if (!subscription?.carry)
|
|
@@ -612,8 +510,7 @@ export async function createHostNode(config) {
|
|
|
612
510
|
const { carry, ...rest } = subscription;
|
|
613
511
|
await writeRoom(carry.account, { ...(rooms.get(carry.account) ?? { full: now() }), refused: true });
|
|
614
512
|
await write(rest);
|
|
615
|
-
|
|
616
|
-
await core.removeCarry(carry.space);
|
|
513
|
+
await releaseCarry(carry.space, id);
|
|
617
514
|
},
|
|
618
515
|
async seen(id) {
|
|
619
516
|
const subscription = await read(id);
|
|
@@ -645,32 +542,30 @@ export async function createHostNode(config) {
|
|
|
645
542
|
async usage(spaceId) {
|
|
646
543
|
if (!meter)
|
|
647
544
|
return null;
|
|
648
|
-
const
|
|
649
|
-
const bytes = core.carried.has(spaceId) ?
|
|
650
|
-
return { writers
|
|
545
|
+
const { writers, readers, windowDays } = await meter.usage(spaceId, await membersOf(spaceId));
|
|
546
|
+
const bytes = core.carried.has(spaceId) ? await measure(spaceId) : 0;
|
|
547
|
+
return { writers, readers, bytes, windowDays };
|
|
651
548
|
},
|
|
652
549
|
/** When a subscription's notice of being over its room runs out, or null when it is not over */
|
|
653
550
|
fullUntil: (subscription) => {
|
|
654
|
-
const room =
|
|
551
|
+
const room = roomOf(subscription);
|
|
655
552
|
return room ? { since: room.full, until: room.full + lineGrace } : null;
|
|
656
553
|
},
|
|
657
|
-
/** Whether members' subscriptions hold a space back, being past the free line
|
|
554
|
+
/** Whether members' subscriptions hold a space back, being past the free line */
|
|
658
555
|
heldBack: (spaceId) => skipped.has(spaceId),
|
|
659
556
|
/** Whether a space pays for itself now, so no account's room counts it */
|
|
660
557
|
paysItself,
|
|
661
|
-
/** Moves a subscription's paid-until date
|
|
558
|
+
/** Moves a subscription's paid-until date: the one thing payments do */
|
|
662
559
|
async extend(id, until, customer) {
|
|
663
560
|
const subscription = await subscribe(id);
|
|
664
|
-
const extended = {
|
|
561
|
+
const extended = await write({
|
|
665
562
|
...subscription,
|
|
666
563
|
paidUntil: Math.max(subscription.paidUntil, until),
|
|
667
564
|
...(customer ? { customer } : {}),
|
|
668
|
-
};
|
|
669
|
-
await write(extended);
|
|
565
|
+
});
|
|
670
566
|
// Paid again in time: carry again what the grace period had kept.
|
|
671
|
-
if (extended.carry && !core.carries.has(extended.carry.space))
|
|
567
|
+
if (extended.carry && !core.carries.has(extended.carry.space))
|
|
672
568
|
await core.addCarry(extended.carry.account, extended.carry.invite);
|
|
673
|
-
}
|
|
674
569
|
if (extended.pass !== undefined)
|
|
675
570
|
await core.addPass(extended.pass).catch(() => { });
|
|
676
571
|
return extended;
|
|
@@ -680,9 +575,7 @@ export async function createHostNode(config) {
|
|
|
680
575
|
const subscription = await subscribe(id);
|
|
681
576
|
if (subscription.paidUntil === until)
|
|
682
577
|
return subscription;
|
|
683
|
-
const set = { ...subscription, paidUntil: until };
|
|
684
|
-
await write(set);
|
|
685
|
-
// Money came back in time: carry again what the grace period had kept.
|
|
578
|
+
const set = await write({ ...subscription, paidUntil: until });
|
|
686
579
|
if (set.pass !== undefined && state(set) !== 'lapsed')
|
|
687
580
|
await core.addPass(set.pass).catch(() => { });
|
|
688
581
|
return set;
|
|
@@ -705,37 +598,19 @@ export async function createHostNode(config) {
|
|
|
705
598
|
throw new Error('An account is named by its DID');
|
|
706
599
|
if (!allowed(account))
|
|
707
600
|
throw new NotAllowedError();
|
|
708
|
-
|
|
709
|
-
const room = rooms.get(account);
|
|
710
|
-
if (room?.refused && plan(subscription) !== 'paid')
|
|
711
|
-
throw new Error('This host stopped carrying for this account: its carry space was over its room');
|
|
712
|
-
if (room?.refused) {
|
|
713
|
-
const { refused: _lifted, ...rest } = room;
|
|
714
|
-
await writeRoom(account, rest);
|
|
715
|
-
}
|
|
716
|
-
// A shared space stopped for being over the line comes back only through its own fund.
|
|
601
|
+
await liftRefusal(account, plan(subscription));
|
|
717
602
|
const asked = parseSpaceInvite(invite).space.id;
|
|
718
|
-
|
|
719
|
-
throw new Error('That space is over this host’s free line: only its own fund carries it');
|
|
720
|
-
// One carried, or named, for others already is theirs to carry: taking it as a carry space
|
|
721
|
-
// would take it from them, and letting it go again would drop the host's copy.
|
|
722
|
-
if (limits && !core.carries.has(asked)) {
|
|
723
|
-
const others = core.carried.has(asked) || [...core.carries.values()].some((entry) => entry.named.has(asked));
|
|
724
|
-
if (others)
|
|
725
|
-
throw new Error('That is a space others are members of, not a carry space');
|
|
726
|
-
}
|
|
603
|
+
await checkCarrySpace(asked);
|
|
727
604
|
const space = await core.addCarry(account, invite);
|
|
728
|
-
//
|
|
729
|
-
// space, which members' own passes carry, each told of the line.
|
|
605
|
+
// Anyone else holding a role there makes it a shared space, which members' own passes carry.
|
|
730
606
|
if (limits && (await sharedCarry(space, account))) {
|
|
731
|
-
|
|
732
|
-
await core.removeCarry(space);
|
|
607
|
+
await releaseCarry(space, id);
|
|
733
608
|
throw new Error('That is a space others are members of, not a carry space');
|
|
734
609
|
}
|
|
735
610
|
const before = subscription.carry;
|
|
736
611
|
const attached = await write({ ...subscription, carry: { account, space, invite } });
|
|
737
|
-
if (before && before.space !== space
|
|
738
|
-
await
|
|
612
|
+
if (before && before.space !== space)
|
|
613
|
+
await releaseCarry(before.space, id);
|
|
739
614
|
return attached;
|
|
740
615
|
},
|
|
741
616
|
/** Stops carrying for a subscription; the subscription stays */
|
|
@@ -745,8 +620,7 @@ export async function createHostNode(config) {
|
|
|
745
620
|
return;
|
|
746
621
|
const { carry, ...rest } = subscription;
|
|
747
622
|
await write(rest);
|
|
748
|
-
|
|
749
|
-
await core.removeCarry(carry.space);
|
|
623
|
+
await releaseCarry(carry.space, id);
|
|
750
624
|
},
|
|
751
625
|
/**
|
|
752
626
|
* Carries a space for its own subscription, from a pass. A later pass (the
|
|
@@ -773,25 +647,19 @@ export async function createHostNode(config) {
|
|
|
773
647
|
},
|
|
774
648
|
/** Drops what lapsed past its grace period, and free subscriptions idle too long. Run now and then. */
|
|
775
649
|
async sweep() {
|
|
776
|
-
const
|
|
777
|
-
const
|
|
778
|
-
|
|
779
|
-
// One from before idle subscriptions were dropped counts from now, not from when it was made.
|
|
780
|
-
if (idle !== null && plan(subscription) === 'free' && subscription.seen === undefined)
|
|
781
|
-
subscription = await write({ ...subscription, seen: now() });
|
|
782
|
-
const idled = idle !== null && plan(subscription) === 'free' && (subscription.seen ?? now()) + idle < now();
|
|
783
|
-
if (state(subscription) !== 'lapsed' && !idled)
|
|
650
|
+
const gone = [];
|
|
651
|
+
for (const subscription of await list()) {
|
|
652
|
+
if (!(await expired(subscription)))
|
|
784
653
|
continue;
|
|
785
|
-
await
|
|
654
|
+
await kept.subscriptions.delete(subscription.id);
|
|
786
655
|
const spaceId = spaceIdOf(subscription.id);
|
|
787
656
|
if (spaceId)
|
|
788
657
|
await core.removePass(spaceId);
|
|
789
|
-
if (subscription.carry
|
|
790
|
-
await
|
|
791
|
-
|
|
792
|
-
dropped.push(subscription.id);
|
|
658
|
+
if (subscription.carry)
|
|
659
|
+
await releaseCarry(subscription.carry.space, subscription.id);
|
|
660
|
+
gone.push(subscription.id);
|
|
793
661
|
}
|
|
794
|
-
return
|
|
662
|
+
return gone;
|
|
795
663
|
},
|
|
796
664
|
spaces: core.spaces,
|
|
797
665
|
/** Reads every account's passes again, taking a space that waited for room: after what `full` says changed */
|
|
@@ -831,5 +699,120 @@ export async function createHostNode(config) {
|
|
|
831
699
|
events.clear();
|
|
832
700
|
},
|
|
833
701
|
});
|
|
702
|
+
/** A refused account comes back only by paying, since a balance has no room; a new key brings nothing back */
|
|
703
|
+
async function liftRefusal(account, room) {
|
|
704
|
+
const refused = rooms.get(account);
|
|
705
|
+
if (!refused?.refused)
|
|
706
|
+
return;
|
|
707
|
+
if (room !== 'paid')
|
|
708
|
+
throw new Error('This host stopped carrying for this account: its carry space was over its room');
|
|
709
|
+
const { refused: _lifted, ...rest } = refused;
|
|
710
|
+
await writeRoom(account, rest);
|
|
711
|
+
}
|
|
712
|
+
/** Refuses as a carry space one that is others' to carry */
|
|
713
|
+
async function checkCarrySpace(spaceId) {
|
|
714
|
+
// Stopped for being over the line, it comes back only through its own fund.
|
|
715
|
+
if (lines.get(spaceId)?.stopped !== undefined && !(await paysItself(spaceId)))
|
|
716
|
+
throw new Error('That space is over this host’s free line: only its own fund carries it');
|
|
717
|
+
if (!limits || core.carries.has(spaceId))
|
|
718
|
+
return;
|
|
719
|
+
// Taking it would take it from them, and letting it go again would drop the host's copy.
|
|
720
|
+
const others = core.carried.has(spaceId) || [...core.carries.values()].some((entry) => entry.named.has(spaceId));
|
|
721
|
+
if (others)
|
|
722
|
+
throw new Error('That is a space others are members of, not a carry space');
|
|
723
|
+
}
|
|
724
|
+
/** Lapsed past its grace, or a free subscription idle too long */
|
|
725
|
+
async function expired(subscription) {
|
|
726
|
+
const idle = limits?.idleDays !== undefined ? limits.idleDays * DAY : null;
|
|
727
|
+
if (idle === null || plan(subscription) !== 'free')
|
|
728
|
+
return state(subscription) === 'lapsed';
|
|
729
|
+
let seen = subscription.seen;
|
|
730
|
+
// One from before idle subscriptions were dropped counts from now, not from when it was made.
|
|
731
|
+
if (seen === undefined) {
|
|
732
|
+
seen = now();
|
|
733
|
+
await write({ ...subscription, seen });
|
|
734
|
+
}
|
|
735
|
+
return state(subscription) === 'lapsed' || seen + idle < now();
|
|
736
|
+
}
|
|
737
|
+
}
|
|
738
|
+
function bucketNames(prefix, { encoded = false, suffix = '' } = {}) {
|
|
739
|
+
return {
|
|
740
|
+
prefix,
|
|
741
|
+
key: (name) => `${prefix}${encoded ? encodeURIComponent(name) : name}${suffix}`,
|
|
742
|
+
nameOf: (key) => {
|
|
743
|
+
const name = key.slice(prefix.length, key.length - suffix.length);
|
|
744
|
+
return (encoded ? decodeURIComponent(name) : name) || null;
|
|
745
|
+
},
|
|
746
|
+
};
|
|
747
|
+
}
|
|
748
|
+
/** Records kept as text in the host's store under `prefix`, and in the bucket too */
|
|
749
|
+
function shelf(store, mirror, prefix, bucket) {
|
|
750
|
+
const at = (name) => `${prefix}${name}`;
|
|
751
|
+
const names = async () => (await store.list(prefix)).map((key) => key.slice(prefix.length));
|
|
752
|
+
async function get(name) {
|
|
753
|
+
const bytes = await store.get(at(name));
|
|
754
|
+
return bytes ? utf8Decode(bytes) : null;
|
|
755
|
+
}
|
|
756
|
+
/** A fresh disk starts from the bucket's copy */
|
|
757
|
+
async function restore() {
|
|
758
|
+
if (!mirror || (await names()).length > 0)
|
|
759
|
+
return;
|
|
760
|
+
for (const key of await mirror.list(bucket.prefix)) {
|
|
761
|
+
const bytes = await mirror.get(key);
|
|
762
|
+
const name = bytes && bucket.nameOf(key, bytes);
|
|
763
|
+
if (bytes && name)
|
|
764
|
+
await store.put(at(name), bytes);
|
|
765
|
+
}
|
|
766
|
+
}
|
|
767
|
+
return {
|
|
768
|
+
get,
|
|
769
|
+
names,
|
|
770
|
+
restore,
|
|
771
|
+
async put(name, text) {
|
|
772
|
+
const bytes = utf8Encode(text);
|
|
773
|
+
await store.put(at(name), bytes);
|
|
774
|
+
await mirror?.put(bucket.key(name), bytes);
|
|
775
|
+
},
|
|
776
|
+
async delete(name) {
|
|
777
|
+
await store.delete(at(name));
|
|
778
|
+
await mirror?.delete(bucket.key(name));
|
|
779
|
+
},
|
|
780
|
+
/** Every record, after restoring from the bucket */
|
|
781
|
+
async load() {
|
|
782
|
+
await restore();
|
|
783
|
+
const entries = [];
|
|
784
|
+
for (const name of await names()) {
|
|
785
|
+
const text = await get(name);
|
|
786
|
+
if (text !== null)
|
|
787
|
+
entries.push([name, text]);
|
|
788
|
+
}
|
|
789
|
+
return entries;
|
|
790
|
+
},
|
|
791
|
+
};
|
|
792
|
+
}
|
|
793
|
+
function subscriptionIdIn(bytes) {
|
|
794
|
+
const subscription = JSON.parse(utf8Decode(bytes));
|
|
795
|
+
return isRecord(subscription) && typeof subscription.id === 'string' ? subscription.id : null;
|
|
796
|
+
}
|
|
797
|
+
function parseLine(value) {
|
|
798
|
+
const fits = isRecord(value) &&
|
|
799
|
+
typeof value.since === 'number' &&
|
|
800
|
+
typeof value.from === 'number' &&
|
|
801
|
+
Array.isArray(value.reasons);
|
|
802
|
+
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- this host's own writes, shape checked
|
|
803
|
+
return fits ? value : null;
|
|
804
|
+
}
|
|
805
|
+
function parseRoom(value) {
|
|
806
|
+
if (!isRecord(value) || typeof value.full !== 'number')
|
|
807
|
+
return null;
|
|
808
|
+
const given = isRecord(value.held) ? Object.entries(value.held) : [];
|
|
809
|
+
const held = Object.fromEntries(given.filter((entry) => typeof entry[1] === 'number'));
|
|
810
|
+
return {
|
|
811
|
+
full: value.full,
|
|
812
|
+
...(typeof value.fits === 'number' ? { fits: value.fits } : {}),
|
|
813
|
+
...(value.refused === true ? { refused: true } : {}),
|
|
814
|
+
...(Object.keys(held).length ? { held } : {}),
|
|
815
|
+
};
|
|
834
816
|
}
|
|
817
|
+
const sameJson = (a, b) => JSON.stringify(a) === JSON.stringify(b);
|
|
835
818
|
//# sourceMappingURL=host.js.map
|