@xenosystem/primitives 0.1.0 → 0.2.1

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 CHANGED
@@ -6,6 +6,7 @@ XENO back-end rung 1. Two families, one subpath each:
6
6
  the layout version, atomic writes, locks, the copy-only migrator, `doctor`, and the static check.
7
7
  - `@xenosystem/primitives/secrets` – envelope-encrypted credentials; the OS store holds one key per person per machine.
8
8
  - `@xenosystem/primitives/secrets/testing` – an in-memory key store for tests only.
9
+ - `@xenosystem/primitives/updates` – an installed app updates only for an account allowed to: points electron-updater at the platform's gated feed (`/api/updates/<slug>/feed`) with the account's own fresh token. Signed out and no plan are states, not errors.
9
10
 
10
11
  Authority: `XENO LOCAL HOME - SPEC.md` (LOCKED). Zero runtime dependencies. Proprietary (`UNLICENSED`).
11
12
 
@@ -0,0 +1,76 @@
1
+ /**
2
+ * `@xenosystem/primitives/updates` — an installed XENO app updates only for an account allowed to.
3
+ *
4
+ * Owner, 2026-10-25: "nobody without a plan should be able to download them." The platform serves each app's
5
+ * update feed behind sign-in and the plan (`GET /api/updates/<slug>/feed/latest.yml`, xeno-platform
6
+ * `productDownloadRoutes.js`): every installer URL in it is a five-minute grant for exactly that version, and
7
+ * the sha512 is untouched, so electron-updater still verifies the bytes. This family points an app's
8
+ * electron-updater at that feed with the account's own token. It is the ONE place every app does this, so the
9
+ * gate cannot be implemented differently, or forgotten, per app (`docs/DOWNLOAD-GATE.md` Phase 3b).
10
+ *
11
+ * Model-free and dependency-free: the app passes its own `autoUpdater` and a function that returns its current
12
+ * access token (its account SDK). Nothing here stores a token, and no token appears in any result or error.
13
+ *
14
+ * Outcomes are states, not exceptions, because "not signed in" and "no plan" are normal for a free install:
15
+ * signed-out no token: the network is never touched
16
+ * not-entitled the platform said 401/403: the account has no plan that allows updates
17
+ * up-to-date / available / error
18
+ */
19
+ /** The slice of electron-updater's `autoUpdater` this uses. */
20
+ export interface AutoUpdaterLike {
21
+ setFeedURL(options: {
22
+ provider: 'generic';
23
+ url: string;
24
+ }): void;
25
+ requestHeaders: Record<string, string> | null;
26
+ disableDifferentialDownload: boolean;
27
+ autoDownload: boolean;
28
+ checkForUpdates(): Promise<{
29
+ updateInfo: {
30
+ version: string;
31
+ };
32
+ } | null>;
33
+ }
34
+ export interface GrantedUpdaterOptions {
35
+ /** The product's catalog slug: `hub`, `canvas`, … */
36
+ slug: string;
37
+ /** The platform origin, e.g. `https://xenosystem.ai`. */
38
+ platformBase: string;
39
+ /**
40
+ * The request headers for one request, from the app's account SDK; `null` when signed out. Preferred: it
41
+ * carries sender-constrained (DPoP) proofs, which are bound to the exact method and URL.
42
+ */
43
+ authorize?: (method: 'GET', url: string) => Promise<Record<string, string> | null>;
44
+ /** Or, for a plain bearer token: the account's current access token; `null` when signed out. */
45
+ getAccessToken?: () => Promise<string | null>;
46
+ /** `process.platform`; decides which feed file electron-updater fetches. */
47
+ platform?: string;
48
+ updater: AutoUpdaterLike;
49
+ /** The running app's version (`app.getVersion()`). */
50
+ currentVersion: string;
51
+ }
52
+ export type UpdateCheck = {
53
+ state: 'signed-out';
54
+ } | {
55
+ state: 'not-entitled';
56
+ } | {
57
+ state: 'up-to-date';
58
+ version: string;
59
+ } | {
60
+ state: 'available';
61
+ version: string;
62
+ } | {
63
+ state: 'error';
64
+ code: string;
65
+ };
66
+ /** The gated feed for a product. electron-updater appends `/latest.yml` (or `-linux`/`-mac`) itself. */
67
+ export declare function gatedFeedUrl(platformBase: string, slug: string): string;
68
+ /** The file electron-updater's generic provider fetches on this platform. */
69
+ export declare function feedFileFor(platform?: string): string;
70
+ /** Compare dotted versions numerically; a pre-release tag sorts below its release. */
71
+ export declare function isNewer(candidate: string, current: string): boolean;
72
+ export declare function createGrantedUpdater(options: GrantedUpdaterOptions): {
73
+ feedUrl: string;
74
+ /** Configure the updater with FRESH credentials and ask the gated feed. Call before every check. */
75
+ check(): Promise<UpdateCheck>;
76
+ };
@@ -0,0 +1,117 @@
1
+ "use strict";
2
+ /**
3
+ * `@xenosystem/primitives/updates` — an installed XENO app updates only for an account allowed to.
4
+ *
5
+ * Owner, 2026-10-25: "nobody without a plan should be able to download them." The platform serves each app's
6
+ * update feed behind sign-in and the plan (`GET /api/updates/<slug>/feed/latest.yml`, xeno-platform
7
+ * `productDownloadRoutes.js`): every installer URL in it is a five-minute grant for exactly that version, and
8
+ * the sha512 is untouched, so electron-updater still verifies the bytes. This family points an app's
9
+ * electron-updater at that feed with the account's own token. It is the ONE place every app does this, so the
10
+ * gate cannot be implemented differently, or forgotten, per app (`docs/DOWNLOAD-GATE.md` Phase 3b).
11
+ *
12
+ * Model-free and dependency-free: the app passes its own `autoUpdater` and a function that returns its current
13
+ * access token (its account SDK). Nothing here stores a token, and no token appears in any result or error.
14
+ *
15
+ * Outcomes are states, not exceptions, because "not signed in" and "no plan" are normal for a free install:
16
+ * signed-out no token: the network is never touched
17
+ * not-entitled the platform said 401/403: the account has no plan that allows updates
18
+ * up-to-date / available / error
19
+ */
20
+ Object.defineProperty(exports, "__esModule", { value: true });
21
+ exports.gatedFeedUrl = gatedFeedUrl;
22
+ exports.feedFileFor = feedFileFor;
23
+ exports.isNewer = isNewer;
24
+ exports.createGrantedUpdater = createGrantedUpdater;
25
+ /** The gated feed for a product. electron-updater appends `/latest.yml` (or `-linux`/`-mac`) itself. */
26
+ function gatedFeedUrl(platformBase, slug) {
27
+ if (!/^[a-z0-9-]+$/.test(slug))
28
+ throw new Error(`invalid product slug: ${slug}`);
29
+ const base = String(platformBase).replace(/\/+$/, '');
30
+ if (!/^https:\/\//.test(base) && !/^http:\/\/(127\.0\.0\.1|localhost)(:\d+)?$/.test(base)) {
31
+ // A token must never ride plain HTTP to anything but a local test server.
32
+ throw new Error('platformBase must be https (or a loopback test server)');
33
+ }
34
+ return `${base}/api/updates/${slug}/feed`;
35
+ }
36
+ /** The file electron-updater's generic provider fetches on this platform. */
37
+ function feedFileFor(platform = process.platform) {
38
+ return platform === 'darwin' ? 'latest-mac.yml' : platform === 'linux' ? 'latest-linux.yml' : 'latest.yml';
39
+ }
40
+ /** Compare dotted versions numerically; a pre-release tag sorts below its release. */
41
+ function isNewer(candidate, current) {
42
+ const parse = (v) => {
43
+ const [core = '', pre] = String(v).replace(/^v/i, '').split('-', 2);
44
+ return { nums: core.split('.').map((n) => Number.parseInt(n, 10) || 0), pre: pre ?? null };
45
+ };
46
+ const a = parse(candidate);
47
+ const b = parse(current);
48
+ for (let i = 0; i < Math.max(a.nums.length, b.nums.length); i += 1) {
49
+ const d = (a.nums[i] ?? 0) - (b.nums[i] ?? 0);
50
+ if (d !== 0)
51
+ return d > 0;
52
+ }
53
+ if (a.pre === b.pre)
54
+ return false;
55
+ if (a.pre === null)
56
+ return true;
57
+ if (b.pre === null)
58
+ return false;
59
+ return a.pre > b.pre;
60
+ }
61
+ const statusOf = (error) => {
62
+ const e = error;
63
+ const direct = Number(e?.statusCode ?? e?.status);
64
+ if (Number.isInteger(direct) && direct > 0)
65
+ return direct;
66
+ // electron-updater's HttpError message starts with the status ("403 Forbidden …")
67
+ const m = /\b(4\d\d|5\d\d)\b/.exec(String(e?.message ?? ''));
68
+ return m ? Number(m[1]) : null;
69
+ };
70
+ function createGrantedUpdater(options) {
71
+ const { slug, platformBase, authorize, getAccessToken, updater, currentVersion } = options;
72
+ if (!authorize && !getAccessToken)
73
+ throw new Error('createGrantedUpdater needs authorize or getAccessToken');
74
+ const url = gatedFeedUrl(platformBase, slug);
75
+ const feedFile = `${url}/${feedFileFor(options.platform)}`;
76
+ const headersFor = async () => {
77
+ try {
78
+ if (authorize)
79
+ return await authorize('GET', feedFile);
80
+ const token = await getAccessToken();
81
+ return token ? { Authorization: `Bearer ${token}` } : null;
82
+ }
83
+ catch {
84
+ return null;
85
+ }
86
+ };
87
+ return {
88
+ feedUrl: url,
89
+ /** Configure the updater with FRESH credentials and ask the gated feed. Call before every check. */
90
+ async check() {
91
+ const headers = await headersFor();
92
+ if (!headers) {
93
+ updater.requestHeaders = null;
94
+ return { state: 'signed-out' };
95
+ }
96
+ updater.setFeedURL({ provider: 'generic', url });
97
+ // Only the feed request carries these. The installer is fetched from the grant URL inside the feed, which
98
+ // needs no credential; a DPoP proof is bound to the feed URL and would be refused anywhere else anyway.
99
+ updater.requestHeaders = headers;
100
+ // A grant URL with `.blockmap` appended is not a file; a full download is correct and is what runs.
101
+ updater.disableDifferentialDownload = true;
102
+ try {
103
+ const result = await updater.checkForUpdates();
104
+ const version = result?.updateInfo?.version;
105
+ if (!version)
106
+ return { state: 'up-to-date', version: currentVersion };
107
+ return isNewer(version, currentVersion) ? { state: 'available', version } : { state: 'up-to-date', version: currentVersion };
108
+ }
109
+ catch (error) {
110
+ const status = statusOf(error);
111
+ if (status === 401 || status === 403)
112
+ return { state: status === 401 ? 'signed-out' : 'not-entitled' };
113
+ return { state: 'error', code: status ? `http_${status}` : 'network' };
114
+ }
115
+ },
116
+ };
117
+ }
@@ -0,0 +1,76 @@
1
+ /**
2
+ * `@xenosystem/primitives/updates` — an installed XENO app updates only for an account allowed to.
3
+ *
4
+ * Owner, 2026-10-25: "nobody without a plan should be able to download them." The platform serves each app's
5
+ * update feed behind sign-in and the plan (`GET /api/updates/<slug>/feed/latest.yml`, xeno-platform
6
+ * `productDownloadRoutes.js`): every installer URL in it is a five-minute grant for exactly that version, and
7
+ * the sha512 is untouched, so electron-updater still verifies the bytes. This family points an app's
8
+ * electron-updater at that feed with the account's own token. It is the ONE place every app does this, so the
9
+ * gate cannot be implemented differently, or forgotten, per app (`docs/DOWNLOAD-GATE.md` Phase 3b).
10
+ *
11
+ * Model-free and dependency-free: the app passes its own `autoUpdater` and a function that returns its current
12
+ * access token (its account SDK). Nothing here stores a token, and no token appears in any result or error.
13
+ *
14
+ * Outcomes are states, not exceptions, because "not signed in" and "no plan" are normal for a free install:
15
+ * signed-out no token: the network is never touched
16
+ * not-entitled the platform said 401/403: the account has no plan that allows updates
17
+ * up-to-date / available / error
18
+ */
19
+ /** The slice of electron-updater's `autoUpdater` this uses. */
20
+ export interface AutoUpdaterLike {
21
+ setFeedURL(options: {
22
+ provider: 'generic';
23
+ url: string;
24
+ }): void;
25
+ requestHeaders: Record<string, string> | null;
26
+ disableDifferentialDownload: boolean;
27
+ autoDownload: boolean;
28
+ checkForUpdates(): Promise<{
29
+ updateInfo: {
30
+ version: string;
31
+ };
32
+ } | null>;
33
+ }
34
+ export interface GrantedUpdaterOptions {
35
+ /** The product's catalog slug: `hub`, `canvas`, … */
36
+ slug: string;
37
+ /** The platform origin, e.g. `https://xenosystem.ai`. */
38
+ platformBase: string;
39
+ /**
40
+ * The request headers for one request, from the app's account SDK; `null` when signed out. Preferred: it
41
+ * carries sender-constrained (DPoP) proofs, which are bound to the exact method and URL.
42
+ */
43
+ authorize?: (method: 'GET', url: string) => Promise<Record<string, string> | null>;
44
+ /** Or, for a plain bearer token: the account's current access token; `null` when signed out. */
45
+ getAccessToken?: () => Promise<string | null>;
46
+ /** `process.platform`; decides which feed file electron-updater fetches. */
47
+ platform?: string;
48
+ updater: AutoUpdaterLike;
49
+ /** The running app's version (`app.getVersion()`). */
50
+ currentVersion: string;
51
+ }
52
+ export type UpdateCheck = {
53
+ state: 'signed-out';
54
+ } | {
55
+ state: 'not-entitled';
56
+ } | {
57
+ state: 'up-to-date';
58
+ version: string;
59
+ } | {
60
+ state: 'available';
61
+ version: string;
62
+ } | {
63
+ state: 'error';
64
+ code: string;
65
+ };
66
+ /** The gated feed for a product. electron-updater appends `/latest.yml` (or `-linux`/`-mac`) itself. */
67
+ export declare function gatedFeedUrl(platformBase: string, slug: string): string;
68
+ /** The file electron-updater's generic provider fetches on this platform. */
69
+ export declare function feedFileFor(platform?: string): string;
70
+ /** Compare dotted versions numerically; a pre-release tag sorts below its release. */
71
+ export declare function isNewer(candidate: string, current: string): boolean;
72
+ export declare function createGrantedUpdater(options: GrantedUpdaterOptions): {
73
+ feedUrl: string;
74
+ /** Configure the updater with FRESH credentials and ask the gated feed. Call before every check. */
75
+ check(): Promise<UpdateCheck>;
76
+ };
@@ -0,0 +1,111 @@
1
+ /**
2
+ * `@xenosystem/primitives/updates` — an installed XENO app updates only for an account allowed to.
3
+ *
4
+ * Owner, 2026-10-25: "nobody without a plan should be able to download them." The platform serves each app's
5
+ * update feed behind sign-in and the plan (`GET /api/updates/<slug>/feed/latest.yml`, xeno-platform
6
+ * `productDownloadRoutes.js`): every installer URL in it is a five-minute grant for exactly that version, and
7
+ * the sha512 is untouched, so electron-updater still verifies the bytes. This family points an app's
8
+ * electron-updater at that feed with the account's own token. It is the ONE place every app does this, so the
9
+ * gate cannot be implemented differently, or forgotten, per app (`docs/DOWNLOAD-GATE.md` Phase 3b).
10
+ *
11
+ * Model-free and dependency-free: the app passes its own `autoUpdater` and a function that returns its current
12
+ * access token (its account SDK). Nothing here stores a token, and no token appears in any result or error.
13
+ *
14
+ * Outcomes are states, not exceptions, because "not signed in" and "no plan" are normal for a free install:
15
+ * signed-out no token: the network is never touched
16
+ * not-entitled the platform said 401/403: the account has no plan that allows updates
17
+ * up-to-date / available / error
18
+ */
19
+ /** The gated feed for a product. electron-updater appends `/latest.yml` (or `-linux`/`-mac`) itself. */
20
+ export function gatedFeedUrl(platformBase, slug) {
21
+ if (!/^[a-z0-9-]+$/.test(slug))
22
+ throw new Error(`invalid product slug: ${slug}`);
23
+ const base = String(platformBase).replace(/\/+$/, '');
24
+ if (!/^https:\/\//.test(base) && !/^http:\/\/(127\.0\.0\.1|localhost)(:\d+)?$/.test(base)) {
25
+ // A token must never ride plain HTTP to anything but a local test server.
26
+ throw new Error('platformBase must be https (or a loopback test server)');
27
+ }
28
+ return `${base}/api/updates/${slug}/feed`;
29
+ }
30
+ /** The file electron-updater's generic provider fetches on this platform. */
31
+ export function feedFileFor(platform = process.platform) {
32
+ return platform === 'darwin' ? 'latest-mac.yml' : platform === 'linux' ? 'latest-linux.yml' : 'latest.yml';
33
+ }
34
+ /** Compare dotted versions numerically; a pre-release tag sorts below its release. */
35
+ export function isNewer(candidate, current) {
36
+ const parse = (v) => {
37
+ const [core = '', pre] = String(v).replace(/^v/i, '').split('-', 2);
38
+ return { nums: core.split('.').map((n) => Number.parseInt(n, 10) || 0), pre: pre ?? null };
39
+ };
40
+ const a = parse(candidate);
41
+ const b = parse(current);
42
+ for (let i = 0; i < Math.max(a.nums.length, b.nums.length); i += 1) {
43
+ const d = (a.nums[i] ?? 0) - (b.nums[i] ?? 0);
44
+ if (d !== 0)
45
+ return d > 0;
46
+ }
47
+ if (a.pre === b.pre)
48
+ return false;
49
+ if (a.pre === null)
50
+ return true;
51
+ if (b.pre === null)
52
+ return false;
53
+ return a.pre > b.pre;
54
+ }
55
+ const statusOf = (error) => {
56
+ const e = error;
57
+ const direct = Number(e?.statusCode ?? e?.status);
58
+ if (Number.isInteger(direct) && direct > 0)
59
+ return direct;
60
+ // electron-updater's HttpError message starts with the status ("403 Forbidden …")
61
+ const m = /\b(4\d\d|5\d\d)\b/.exec(String(e?.message ?? ''));
62
+ return m ? Number(m[1]) : null;
63
+ };
64
+ export function createGrantedUpdater(options) {
65
+ const { slug, platformBase, authorize, getAccessToken, updater, currentVersion } = options;
66
+ if (!authorize && !getAccessToken)
67
+ throw new Error('createGrantedUpdater needs authorize or getAccessToken');
68
+ const url = gatedFeedUrl(platformBase, slug);
69
+ const feedFile = `${url}/${feedFileFor(options.platform)}`;
70
+ const headersFor = async () => {
71
+ try {
72
+ if (authorize)
73
+ return await authorize('GET', feedFile);
74
+ const token = await getAccessToken();
75
+ return token ? { Authorization: `Bearer ${token}` } : null;
76
+ }
77
+ catch {
78
+ return null;
79
+ }
80
+ };
81
+ return {
82
+ feedUrl: url,
83
+ /** Configure the updater with FRESH credentials and ask the gated feed. Call before every check. */
84
+ async check() {
85
+ const headers = await headersFor();
86
+ if (!headers) {
87
+ updater.requestHeaders = null;
88
+ return { state: 'signed-out' };
89
+ }
90
+ updater.setFeedURL({ provider: 'generic', url });
91
+ // Only the feed request carries these. The installer is fetched from the grant URL inside the feed, which
92
+ // needs no credential; a DPoP proof is bound to the feed URL and would be refused anywhere else anyway.
93
+ updater.requestHeaders = headers;
94
+ // A grant URL with `.blockmap` appended is not a file; a full download is correct and is what runs.
95
+ updater.disableDifferentialDownload = true;
96
+ try {
97
+ const result = await updater.checkForUpdates();
98
+ const version = result?.updateInfo?.version;
99
+ if (!version)
100
+ return { state: 'up-to-date', version: currentVersion };
101
+ return isNewer(version, currentVersion) ? { state: 'available', version } : { state: 'up-to-date', version: currentVersion };
102
+ }
103
+ catch (error) {
104
+ const status = statusOf(error);
105
+ if (status === 401 || status === 403)
106
+ return { state: status === 401 ? 'signed-out' : 'not-entitled' };
107
+ return { state: 'error', code: status ? `http_${status}` : 'network' };
108
+ }
109
+ },
110
+ };
111
+ }
package/package.json CHANGED
@@ -1,24 +1,57 @@
1
1
  {
2
2
  "name": "@xenosystem/primitives",
3
- "version": "0.1.0",
4
- "description": "XENO back-end rung 1 primitives: dirs (the XENO local home) and secrets (envelope-encrypted credentials).",
3
+ "version": "0.2.1",
4
+ "description": "XENO back-end rung 1 primitives: dirs (the XENO local home), secrets (envelope-encrypted credentials) and updates (the gated app updater).",
5
5
  "license": "UNLICENSED",
6
6
  "type": "module",
7
7
  "sideEffects": false,
8
- "engines": { "node": ">=22.18" },
9
- "files": ["dist", "README.md"],
8
+ "engines": {
9
+ "node": ">=22.18"
10
+ },
11
+ "files": [
12
+ "dist",
13
+ "README.md"
14
+ ],
10
15
  "exports": {
11
16
  "./dirs": {
12
- "import": { "types": "./dist/esm/dirs/index.d.ts", "default": "./dist/esm/dirs/index.js" },
13
- "require": { "types": "./dist/cjs/dirs/index.d.ts", "default": "./dist/cjs/dirs/index.js" }
17
+ "import": {
18
+ "types": "./dist/esm/dirs/index.d.ts",
19
+ "default": "./dist/esm/dirs/index.js"
20
+ },
21
+ "require": {
22
+ "types": "./dist/cjs/dirs/index.d.ts",
23
+ "default": "./dist/cjs/dirs/index.js"
24
+ }
14
25
  },
15
26
  "./secrets": {
16
- "import": { "types": "./dist/esm/secrets/index.d.ts", "default": "./dist/esm/secrets/index.js" },
17
- "require": { "types": "./dist/cjs/secrets/index.d.ts", "default": "./dist/cjs/secrets/index.js" }
27
+ "import": {
28
+ "types": "./dist/esm/secrets/index.d.ts",
29
+ "default": "./dist/esm/secrets/index.js"
30
+ },
31
+ "require": {
32
+ "types": "./dist/cjs/secrets/index.d.ts",
33
+ "default": "./dist/cjs/secrets/index.js"
34
+ }
35
+ },
36
+ "./updates": {
37
+ "import": {
38
+ "types": "./dist/esm/updates/index.d.ts",
39
+ "default": "./dist/esm/updates/index.js"
40
+ },
41
+ "require": {
42
+ "types": "./dist/cjs/updates/index.d.ts",
43
+ "default": "./dist/cjs/updates/index.js"
44
+ }
18
45
  },
19
46
  "./secrets/testing": {
20
- "import": { "types": "./dist/esm/secrets/testing.d.ts", "default": "./dist/esm/secrets/testing.js" },
21
- "require": { "types": "./dist/cjs/secrets/testing.d.ts", "default": "./dist/cjs/secrets/testing.js" }
47
+ "import": {
48
+ "types": "./dist/esm/secrets/testing.d.ts",
49
+ "default": "./dist/esm/secrets/testing.js"
50
+ },
51
+ "require": {
52
+ "types": "./dist/cjs/secrets/testing.d.ts",
53
+ "default": "./dist/cjs/secrets/testing.js"
54
+ }
22
55
  },
23
56
  "./package.json": "./package.json"
24
57
  },
@@ -32,7 +65,10 @@
32
65
  "test": "npm run typecheck && node --test \"src/**/*.test.ts\"",
33
66
  "prepublishOnly": "node ../../scripts/release-gate.mjs"
34
67
  },
35
- "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org/" },
68
+ "publishConfig": {
69
+ "access": "public",
70
+ "registry": "https://registry.npmjs.org/"
71
+ },
36
72
  "devDependencies": {
37
73
  "@types/node": "24.19.2",
38
74
  "typescript": "5.9.3"