@astralarium/repo-env 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Astralarium
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,4 @@
1
- # Temporary Holding Version
1
+ # `@astralarium/repo-env`
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Validated Wrangler bindings shared by workers.
4
+ Apps spread `coreEnvironment(config)` into their environment.
@@ -0,0 +1,51 @@
1
+ /** OAuth client credentials. */
2
+ export interface OAuthCredentials {
3
+ clientId: string;
4
+ clientSecret: string;
5
+ }
6
+ /** Configuration every worker shares; apps extend it with their own fields. */
7
+ export interface CoreConfig {
8
+ /** Public HTTP(S) origin. */
9
+ siteOrigin: string;
10
+ /** Auth signing secret; at least 32 characters. */
11
+ authSecret: string;
12
+ /** Postgres connection URL naming a database. */
13
+ databaseUrl: string;
14
+ /** Local HTTPS origins auth and the dev server answer beside `siteOrigin`. */
15
+ localOrigins?: readonly string[];
16
+ }
17
+ /** Wrangler bindings and Hyperdrive override common to an app's workers;
18
+ * workers declaring `LOCAL_ORIGINS` answer those origins too. */
19
+ export interface CoreEnvironment extends Record<string, string> {
20
+ SITE_ORIGIN: string;
21
+ BETTER_AUTH_SECRET: string;
22
+ CLOUDFLARE_HYPERDRIVE_LOCAL_CONNECTION_STRING_DB_APP: string;
23
+ LOCAL_ORIGINS: string;
24
+ }
25
+ /** Public bindings available during prerender and at runtime. */
26
+ export interface PublicWebEnvironment {
27
+ SITE_ORIGIN?: string;
28
+ LOCAL_ORIGINS?: string;
29
+ }
30
+ /** Absolute HTTP(S) origin, without credentials, path, query or fragment. */
31
+ export declare function parseOrigin(value: string | undefined, key?: string): string;
32
+ /** Deduplicated HTTPS origins from a comma list, with their hosts and hostnames. */
33
+ export declare function parseLocalOrigins(value: string | undefined): {
34
+ origins: string[];
35
+ hosts: string[];
36
+ hostnames: string[];
37
+ };
38
+ /** Optional OAuth pair read from `env[idKey]`/`env[secretKey]`;
39
+ * undefined when both are unset, invalid when one is set. */
40
+ export declare function credentialPair<K extends string>(env: Partial<Record<K, string>>, idKey: K, secretKey: K): OAuthCredentials | undefined;
41
+ /** Public configuration; never reads runtime secrets. */
42
+ export declare function publicWebConfig(env: PublicWebEnvironment): {
43
+ siteOrigin: string;
44
+ localOrigins: {
45
+ origins: string[];
46
+ hosts: string[];
47
+ hostnames: string[];
48
+ };
49
+ };
50
+ /** Validated process environment every worker shares. */
51
+ export declare function coreEnvironment(config: CoreConfig): CoreEnvironment;
package/dist/index.js ADDED
@@ -0,0 +1,73 @@
1
+ import { ExpectedError } from "@astralarium/repo-telemetry";
2
+ /** Absolute HTTP(S) origin, without credentials, path, query or fragment. */
3
+ export function parseOrigin(value, key = "SITE_ORIGIN") {
4
+ try {
5
+ const url = new URL(value ?? "");
6
+ if (!["http:", "https:"].includes(url.protocol) ||
7
+ url.username ||
8
+ url.password ||
9
+ url.pathname !== "/" ||
10
+ url.search ||
11
+ url.hash)
12
+ throw new Error();
13
+ return url.origin;
14
+ }
15
+ catch {
16
+ throw new ExpectedError(`${key} must be an HTTP(S) origin`);
17
+ }
18
+ }
19
+ /** Deduplicated HTTPS origins from a comma list, with their hosts and hostnames. */
20
+ export function parseLocalOrigins(value) {
21
+ const origins = [
22
+ ...new Set((value ? value.split(",") : []).map((origin) => {
23
+ const parsed = parseOrigin(origin.trim(), "LOCAL_ORIGINS");
24
+ if (!parsed.startsWith("https://"))
25
+ throw new ExpectedError("LOCAL_ORIGINS must use HTTPS");
26
+ return parsed;
27
+ })),
28
+ ];
29
+ return {
30
+ origins,
31
+ hosts: origins.map((origin) => new URL(origin).host),
32
+ hostnames: origins.map((origin) => new URL(origin).hostname),
33
+ };
34
+ }
35
+ /** Optional OAuth pair read from `env[idKey]`/`env[secretKey]`;
36
+ * undefined when both are unset, invalid when one is set. */
37
+ export function credentialPair(env, idKey, secretKey) {
38
+ const clientId = env[idKey];
39
+ const clientSecret = env[secretKey];
40
+ if (!clientId && !clientSecret)
41
+ return undefined;
42
+ if (!clientId || !clientSecret)
43
+ throw new ExpectedError(`${idKey} and ${secretKey} must be set together`);
44
+ return { clientId, clientSecret };
45
+ }
46
+ /** Public configuration; never reads runtime secrets. */
47
+ export function publicWebConfig(env) {
48
+ return {
49
+ siteOrigin: parseOrigin(env.SITE_ORIGIN),
50
+ localOrigins: parseLocalOrigins(env.LOCAL_ORIGINS),
51
+ };
52
+ }
53
+ /** Validated process environment every worker shares. */
54
+ export function coreEnvironment(config) {
55
+ if (config.authSecret.length < 32)
56
+ throw new ExpectedError("BETTER_AUTH_SECRET must contain at least 32 characters");
57
+ try {
58
+ const database = new URL(config.databaseUrl);
59
+ if (!["postgres:", "postgresql:"].includes(database.protocol) ||
60
+ !database.hostname ||
61
+ database.pathname.length <= 1)
62
+ throw new Error();
63
+ }
64
+ catch {
65
+ throw new ExpectedError("DB_APP must be a Postgres connection URL with a database");
66
+ }
67
+ return {
68
+ SITE_ORIGIN: parseOrigin(config.siteOrigin),
69
+ BETTER_AUTH_SECRET: config.authSecret,
70
+ CLOUDFLARE_HYPERDRIVE_LOCAL_CONNECTION_STRING_DB_APP: config.databaseUrl,
71
+ LOCAL_ORIGINS: parseLocalOrigins(config.localOrigins?.join(",")).origins.join(","),
72
+ };
73
+ }
@@ -0,0 +1,2 @@
1
+ /** Local HTTPS origins a test dev server answers beside its site origin. */
2
+ export declare const TEST_LOCAL_ORIGINS: string[];
@@ -0,0 +1,5 @@
1
+ /** Local HTTPS origins a test dev server answers beside its site origin. */
2
+ export const TEST_LOCAL_ORIGINS = [
3
+ "https://localhost:3000",
4
+ "https://mymac.local:3000",
5
+ ];
package/package.json CHANGED
@@ -1,6 +1,42 @@
1
1
  {
2
2
  "name": "@astralarium/repo-env",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.1.0",
4
+ "license": "MIT",
5
+ "homepage": "https://github.com/astralarium/repo/tree/main/packages/repo-env#readme",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/astralarium/repo.git",
9
+ "directory": "packages/repo-env"
10
+ },
11
+ "type": "module",
12
+ "files": [
13
+ "dist"
14
+ ],
15
+ "exports": {
16
+ ".": "./dist/index.js",
17
+ "./testkit": "./dist/testkit/index.js"
18
+ },
19
+ "publishConfig": {
20
+ "access": "public"
21
+ },
22
+ "dependencies": {
23
+ "@astralarium/repo-telemetry": "^0.1.0"
24
+ },
25
+ "devDependencies": {
26
+ "@types/node": "^24.2.0",
27
+ "eslint": "^10.2.1",
28
+ "typescript": "^6.0.3",
29
+ "vitest": "^5.0.3",
30
+ "@astralarium/repo-config-typescript": "^0.1.0",
31
+ "@astralarium/repo-config-eslint": "^0.1.0",
32
+ "@astralarium/repo-package-files": "^0.1.0"
33
+ },
34
+ "engines": {
35
+ "node": ">=24.2"
36
+ },
37
+ "scripts": {
38
+ "check-types": "tsc --noEmit && tsc -p tsconfig.build.json --noEmit",
39
+ "lint": "eslint",
40
+ "test": "vitest"
41
+ }
6
42
  }