← Files Flows by plugin.spaceARCHIVED FILE

lib/local-access.mjs

3.27 KB · Nov 30, 2026 · 00:17 UTC

↓ Download file

import { flowsEnv } from './brand-compat.mjs';
import { readFile } from 'node:fs/promises';
import { join } from 'node:path';
import { randomUUID } from 'node:crypto';
import { withLibraryLock, writeState } from './local-state.mjs';

export const FREE_LOCAL_FLOWS = 3;
// null means no count limit, never a zero-slot allowance.
export const PAID_LOCAL_FLOWS = null;
export const OFFLINE_ACCESS_MS = 7 * 24 * 60 * 60 * 1000;
export const defaultCloudOrigin = () => flowsEnv('CLOUD_URL') ?? 'https://plugin.space';

// This cache is written only by the Node OAuth client, never from browser input.
// It contains no credentials. Local owners can modify their files/code; this is
// an application quota, not DRM or a security boundary against the device owner.
export function createLocalAccess(root, origin = defaultCloudOrigin(), now = Date.now) {
  const path = join(root, 'local-access.json');
  async function read() {
    try { const value = JSON.parse(await readFile(path, 'utf8')); return value && typeof value === 'object' && value.origin === origin ? value : null; }
    catch { return null; } // Missing/corrupt receipts grant only Free access.
  }
  return {
    async current() {
      const value = await read(), time = now();
      const validUntil = value ? Math.min(value.verifiedAt + OFFLINE_ACCESS_MS, value.paidUntil) : 0;
      const paid = Boolean(value && typeof value.accountId === 'string' && value.limit === PAID_LOCAL_FLOWS && Number.isFinite(value.verifiedAt) && value.verifiedAt <= time && Number.isFinite(value.paidUntil) && validUntil > time);
      return { limit: paid ? PAID_LOCAL_FLOWS : FREE_LOCAL_FLOWS, paid, connected: Boolean(value?.accountId), ...(paid ? { validUntil: new Date(validUntil).toISOString() } : {}) };
    },
    async connect(accountId) {
      if (typeof accountId !== 'string' || !accountId || accountId.length > 500) throw new Error('Invalid account identity.');
      const connectionId = randomUUID();
      await withLibraryLock(root, () => writeState(path, { origin, connectionId, accountId, limit: FREE_LOCAL_FLOWS }));
      return connectionId;
    },
    async verify(connectionId, accountId, value) {
      await withLibraryLock(root, async () => {
        const previous = await read();
        if (previous?.connectionId !== connectionId || previous.accountId !== accountId) return;
        const paidUntil = Date.parse(value?.paidUntil);
        const valid = value?.accountId === accountId && [FREE_LOCAL_FLOWS, PAID_LOCAL_FLOWS].includes(value?.localFlowLimit);
        const paid = valid && value.localFlowLimit === PAID_LOCAL_FLOWS && Number.isFinite(paidUntil) && paidUntil > now();
        await writeState(path, { origin, connectionId, accountId, limit: paid ? PAID_LOCAL_FLOWS : FREE_LOCAL_FLOWS, verifiedAt: now(), paidUntil: paid ? paidUntil : 0 });
      });
    },
    // `current` is checked under the lock: a caller whose request was overtaken
    // (for example by a newer sign-in) leaves the newer receipt in place.
    async disconnect(connectionId, current = () => true) {
      await withLibraryLock(root, async () => {
        if (!current()) return;
        if (connectionId && (await read())?.connectionId !== connectionId) return;
        await writeState(path, { origin, connectionId: randomUUID(), limit: FREE_LOCAL_FLOWS });
      });
    },
  };
}

SHA-256: f317138f8a48bffde0c9404eab6734486610e33dd1b3cfd61b1ade5787abc204