@farm.js/preview-gateway 0.1.1-beta.4 → 0.1.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 +9 -0
- package/dist/{index-Br5mZiGr.d.ts → index-DeizJwbc.d.ts} +84 -8
- package/dist/index.d.ts +84 -8
- package/dist/index.js +190 -37
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -14,6 +14,15 @@ The gateway limits public request bodies and local preview response bodies to 5
|
|
|
14
14
|
|
|
15
15
|
Public disconnects are propagated through the request queue. The polling agent aborts the matching localhost fetch instead of leaving abandoned app work running until its timeout.
|
|
16
16
|
|
|
17
|
+
`authorizeSession` is an optional, required-when-configured access hook. It receives only
|
|
18
|
+
session ID, name, verified owner/project/key/grant IDs, and expiry—not transport tokens.
|
|
19
|
+
Return `true` to allow access; denial returns 403 and exceptions return a generic 503.
|
|
20
|
+
The gateway checks registration, public/control requests, queued dispatch and waiting responses.
|
|
21
|
+
Unlike `observer`, errors never fail open. Bound your authority request with a timeout;
|
|
22
|
+
the Infra example uses five seconds. Session DELETE remains available for cleanup.
|
|
23
|
+
Omitting the hook preserves self-hosted behavior. The persistent relay offers the same
|
|
24
|
+
hook, also checking idle sockets every five seconds and dispatch from its coordinator.
|
|
25
|
+
|
|
17
26
|
Managed deployments can configure `auth` with a GitHub OAuth client id and a signing secret. The gateway then exposes the CLI login exchange, issues account-bound tunnel grants scoped to one preview name and absolute expiry, and requires a grant before creating a session:
|
|
18
27
|
|
|
19
28
|
```ts
|
|
@@ -5,14 +5,30 @@ interface PreviewManagedAuthOptions {
|
|
|
5
5
|
/** Secret used to sign account tokens and one-preview tunnel grants. */
|
|
6
6
|
signingSecret: string;
|
|
7
7
|
/** Public client id for the Farming Labs GitHub OAuth app. */
|
|
8
|
-
githubClientId
|
|
8
|
+
githubClientId?: string;
|
|
9
|
+
/** First-party device login. Omit to preserve GitHub account authentication. */
|
|
10
|
+
deviceAuth?: {
|
|
11
|
+
issuer: string;
|
|
12
|
+
clientId: string;
|
|
13
|
+
/** Resolve an API key or approved device credential on every new grant. */
|
|
14
|
+
authorizeAccount(token: string): Promise<{
|
|
15
|
+
subject: string;
|
|
16
|
+
login: string;
|
|
17
|
+
expiresAt?: number;
|
|
18
|
+
keyId?: string;
|
|
19
|
+
project?: string;
|
|
20
|
+
} | null>;
|
|
21
|
+
};
|
|
22
|
+
/** Public console URL. Must not contain credentials or query parameters. */
|
|
23
|
+
dashboardUrl?: string;
|
|
9
24
|
accountTokenTtlMs?: number;
|
|
10
25
|
defaultSessionTtlMs?: number;
|
|
11
26
|
maxSessionTtlMs?: number;
|
|
12
27
|
githubApiUrl?: string;
|
|
13
28
|
fetch?: typeof fetch;
|
|
14
29
|
/**
|
|
15
|
-
* Shared abuse-control hook for
|
|
30
|
+
* Shared abuse-control hook for provider-token exchange and, in device mode,
|
|
31
|
+
* account credential verification on the tunnel-grant endpoint.
|
|
16
32
|
* Implementations must not consume the request body. Failures reject the
|
|
17
33
|
* exchange with a retryable 503 response instead of bypassing the limiter.
|
|
18
34
|
*/
|
|
@@ -25,7 +41,11 @@ interface PreviewAuthExchangeRateLimitResult {
|
|
|
25
41
|
}
|
|
26
42
|
type PreviewAuthExchangeRateLimiter = (request: Request) => PreviewAuthExchangeRateLimitResult | Promise<PreviewAuthExchangeRateLimitResult>;
|
|
27
43
|
interface PreviewAccountIdentity {
|
|
28
|
-
|
|
44
|
+
/** Optional credential restriction; never taken from the grant request. */
|
|
45
|
+
project?: string;
|
|
46
|
+
/** Non-secret identifier supplied by the credential verifier, never the CLI. */
|
|
47
|
+
keyId?: string;
|
|
48
|
+
provider: "github" | "device";
|
|
29
49
|
subject: string;
|
|
30
50
|
login: string;
|
|
31
51
|
name?: string;
|
|
@@ -37,7 +57,13 @@ interface PreviewAccountClaims extends PreviewAccountIdentity {
|
|
|
37
57
|
expiresAt: number;
|
|
38
58
|
}
|
|
39
59
|
interface PreviewTunnelGrantClaims {
|
|
60
|
+
/** Unique signed grant identity, used to revoke reconnects across transports. */
|
|
61
|
+
nonce?: string;
|
|
62
|
+
project?: string;
|
|
63
|
+
keyId?: string;
|
|
40
64
|
kind: "tunnel";
|
|
65
|
+
/** Absent on older GitHub grants. */
|
|
66
|
+
provider?: "github" | "device";
|
|
41
67
|
subject: string;
|
|
42
68
|
login: string;
|
|
43
69
|
name: string;
|
|
@@ -50,13 +76,23 @@ interface PreviewTunnelGrant {
|
|
|
50
76
|
expiresAt: number;
|
|
51
77
|
}
|
|
52
78
|
declare function getPreviewAuthPublicConfig(options: PreviewManagedAuthOptions): {
|
|
79
|
+
defaultSessionTtlMs: number;
|
|
80
|
+
maxSessionTtlMs: number;
|
|
81
|
+
dashboardUrl?: string | undefined;
|
|
82
|
+
provider: "device";
|
|
83
|
+
clientId: string;
|
|
84
|
+
issuer: string;
|
|
53
85
|
enabled: true;
|
|
54
86
|
controlAuth: "bearer";
|
|
55
|
-
|
|
56
|
-
clientId: string;
|
|
57
|
-
scope: string;
|
|
87
|
+
} | {
|
|
58
88
|
defaultSessionTtlMs: number;
|
|
59
89
|
maxSessionTtlMs: number;
|
|
90
|
+
dashboardUrl?: string | undefined;
|
|
91
|
+
provider: "github";
|
|
92
|
+
clientId: string | undefined;
|
|
93
|
+
scope: string;
|
|
94
|
+
enabled: true;
|
|
95
|
+
controlAuth: "bearer";
|
|
60
96
|
};
|
|
61
97
|
declare function exchangeGitHubPreviewToken(providerToken: string, options: PreviewManagedAuthOptions): Promise<{
|
|
62
98
|
token: string;
|
|
@@ -66,6 +102,7 @@ declare function exchangeGitHubPreviewToken(providerToken: string, options: Prev
|
|
|
66
102
|
declare function verifyPreviewAccountToken(token: string, options: Pick<PreviewManagedAuthOptions, "signingSecret">): PreviewAccountClaims;
|
|
67
103
|
declare function issuePreviewTunnelGrant(account: PreviewAccountClaims, input: {
|
|
68
104
|
name: string;
|
|
105
|
+
project?: string;
|
|
69
106
|
expiresInMs?: number;
|
|
70
107
|
}, options: PreviewManagedAuthOptions): PreviewTunnelGrant;
|
|
71
108
|
declare function verifyPreviewTunnelGrant(token: string, options: Pick<PreviewManagedAuthOptions, "signingSecret"> & {
|
|
@@ -78,7 +115,20 @@ declare class PreviewAuthError extends Error {
|
|
|
78
115
|
}
|
|
79
116
|
//#endregion
|
|
80
117
|
//#region src/index.d.ts
|
|
118
|
+
interface PreviewSessionAccess {
|
|
119
|
+
id: string;
|
|
120
|
+
name: string;
|
|
121
|
+
ownerId?: string;
|
|
122
|
+
project?: string;
|
|
123
|
+
keyId?: string;
|
|
124
|
+
grantId?: string;
|
|
125
|
+
expiresAt?: number;
|
|
126
|
+
}
|
|
81
127
|
interface PreviewGatewayOptions {
|
|
128
|
+
/** Required access control when supplied. Denials and errors fail closed, unlike telemetry. */
|
|
129
|
+
authorizeSession?: (session: PreviewSessionAccess) => Promise<boolean>;
|
|
130
|
+
/** Optional metadata-only telemetry. Failures never interrupt a preview. */
|
|
131
|
+
observer?: PreviewGatewayObserver;
|
|
82
132
|
domain?: string;
|
|
83
133
|
baseUrl?: string;
|
|
84
134
|
store?: PreviewGatewayStore;
|
|
@@ -93,6 +143,10 @@ interface PreviewGatewayOptions {
|
|
|
93
143
|
auth?: PreviewManagedAuthOptions;
|
|
94
144
|
}
|
|
95
145
|
interface PreviewGatewaySession {
|
|
146
|
+
grantId?: string;
|
|
147
|
+
project?: string;
|
|
148
|
+
keyId?: string;
|
|
149
|
+
ownerId?: string;
|
|
96
150
|
id: string;
|
|
97
151
|
name: string;
|
|
98
152
|
hostname: string;
|
|
@@ -133,6 +187,28 @@ interface PreviewGatewayStore {
|
|
|
133
187
|
deleteResponse(sessionId: string, requestId: string): Promise<void>;
|
|
134
188
|
deleteSession(session: PreviewGatewaySession): Promise<void>;
|
|
135
189
|
}
|
|
190
|
+
interface PreviewGatewayObserver {
|
|
191
|
+
session?(event: {
|
|
192
|
+
grantId?: string;
|
|
193
|
+
project?: string;
|
|
194
|
+
keyId?: string;
|
|
195
|
+
id: string;
|
|
196
|
+
name: string;
|
|
197
|
+
ownerId?: string;
|
|
198
|
+
publicUrl: string;
|
|
199
|
+
expiresAt?: number;
|
|
200
|
+
state: "connected" | "disconnected";
|
|
201
|
+
at: number;
|
|
202
|
+
}): void | Promise<void>;
|
|
203
|
+
request?(event: {
|
|
204
|
+
sessionId: string;
|
|
205
|
+
method: string;
|
|
206
|
+
path: string;
|
|
207
|
+
status: number;
|
|
208
|
+
durationMs: number;
|
|
209
|
+
at: number;
|
|
210
|
+
}): void | Promise<void>;
|
|
211
|
+
}
|
|
136
212
|
/**
|
|
137
213
|
* A gateway request handler, plus the name-ownership probe another preview
|
|
138
214
|
* transport needs before it claims the same public hostname.
|
|
@@ -187,5 +263,5 @@ declare class RedisRestPreviewGatewayStore implements PreviewGatewayStore {
|
|
|
187
263
|
private responseKey;
|
|
188
264
|
}
|
|
189
265
|
//#endregion
|
|
190
|
-
export { MemoryPreviewGatewayStore, NodePreviewGatewayHandler, type PreviewAccountClaims, type PreviewAccountIdentity, PreviewAuthError, type PreviewAuthExchangeRateLimitResult, type PreviewAuthExchangeRateLimiter, PreviewGatewayHandler, PreviewGatewayOptions, PreviewGatewayRequest, PreviewGatewayResponse, PreviewGatewaySession, PreviewGatewayStore, type PreviewManagedAuthOptions, type PreviewTunnelGrant, type PreviewTunnelGrantClaims, RedisRestPreviewGatewayStore, createNodePreviewGatewayHandler, createPreviewGatewayHandler, createPreviewGatewayStoreFromEnv, exchangeGitHubPreviewToken, getPreviewAuthPublicConfig, issuePreviewTunnelGrant, readPreviewBearerToken, verifyPreviewAccountToken, verifyPreviewTunnelGrant };
|
|
191
|
-
//# sourceMappingURL=index-
|
|
266
|
+
export { MemoryPreviewGatewayStore, NodePreviewGatewayHandler, type PreviewAccountClaims, type PreviewAccountIdentity, PreviewAuthError, type PreviewAuthExchangeRateLimitResult, type PreviewAuthExchangeRateLimiter, PreviewGatewayHandler, PreviewGatewayObserver, PreviewGatewayOptions, PreviewGatewayRequest, PreviewGatewayResponse, PreviewGatewaySession, PreviewGatewayStore, type PreviewManagedAuthOptions, PreviewSessionAccess, type PreviewTunnelGrant, type PreviewTunnelGrantClaims, RedisRestPreviewGatewayStore, createNodePreviewGatewayHandler, createPreviewGatewayHandler, createPreviewGatewayStoreFromEnv, exchangeGitHubPreviewToken, getPreviewAuthPublicConfig, issuePreviewTunnelGrant, readPreviewBearerToken, verifyPreviewAccountToken, verifyPreviewTunnelGrant };
|
|
267
|
+
//# sourceMappingURL=index-DeizJwbc.d.ts.map
|
package/dist/index.d.ts
CHANGED
|
@@ -5,14 +5,30 @@ interface PreviewManagedAuthOptions {
|
|
|
5
5
|
/** Secret used to sign account tokens and one-preview tunnel grants. */
|
|
6
6
|
signingSecret: string;
|
|
7
7
|
/** Public client id for the Farming Labs GitHub OAuth app. */
|
|
8
|
-
githubClientId
|
|
8
|
+
githubClientId?: string;
|
|
9
|
+
/** First-party device login. Omit to preserve GitHub account authentication. */
|
|
10
|
+
deviceAuth?: {
|
|
11
|
+
issuer: string;
|
|
12
|
+
clientId: string;
|
|
13
|
+
/** Resolve an API key or approved device credential on every new grant. */
|
|
14
|
+
authorizeAccount(token: string): Promise<{
|
|
15
|
+
subject: string;
|
|
16
|
+
login: string;
|
|
17
|
+
expiresAt?: number;
|
|
18
|
+
keyId?: string;
|
|
19
|
+
project?: string;
|
|
20
|
+
} | null>;
|
|
21
|
+
};
|
|
22
|
+
/** Public console URL. Must not contain credentials or query parameters. */
|
|
23
|
+
dashboardUrl?: string;
|
|
9
24
|
accountTokenTtlMs?: number;
|
|
10
25
|
defaultSessionTtlMs?: number;
|
|
11
26
|
maxSessionTtlMs?: number;
|
|
12
27
|
githubApiUrl?: string;
|
|
13
28
|
fetch?: typeof fetch;
|
|
14
29
|
/**
|
|
15
|
-
* Shared abuse-control hook for
|
|
30
|
+
* Shared abuse-control hook for provider-token exchange and, in device mode,
|
|
31
|
+
* account credential verification on the tunnel-grant endpoint.
|
|
16
32
|
* Implementations must not consume the request body. Failures reject the
|
|
17
33
|
* exchange with a retryable 503 response instead of bypassing the limiter.
|
|
18
34
|
*/
|
|
@@ -25,7 +41,11 @@ interface PreviewAuthExchangeRateLimitResult {
|
|
|
25
41
|
}
|
|
26
42
|
type PreviewAuthExchangeRateLimiter = (request: Request) => PreviewAuthExchangeRateLimitResult | Promise<PreviewAuthExchangeRateLimitResult>;
|
|
27
43
|
interface PreviewAccountIdentity {
|
|
28
|
-
|
|
44
|
+
/** Optional credential restriction; never taken from the grant request. */
|
|
45
|
+
project?: string;
|
|
46
|
+
/** Non-secret identifier supplied by the credential verifier, never the CLI. */
|
|
47
|
+
keyId?: string;
|
|
48
|
+
provider: "github" | "device";
|
|
29
49
|
subject: string;
|
|
30
50
|
login: string;
|
|
31
51
|
name?: string;
|
|
@@ -37,7 +57,13 @@ interface PreviewAccountClaims extends PreviewAccountIdentity {
|
|
|
37
57
|
expiresAt: number;
|
|
38
58
|
}
|
|
39
59
|
interface PreviewTunnelGrantClaims {
|
|
60
|
+
/** Unique signed grant identity, used to revoke reconnects across transports. */
|
|
61
|
+
nonce?: string;
|
|
62
|
+
project?: string;
|
|
63
|
+
keyId?: string;
|
|
40
64
|
kind: "tunnel";
|
|
65
|
+
/** Absent on older GitHub grants. */
|
|
66
|
+
provider?: "github" | "device";
|
|
41
67
|
subject: string;
|
|
42
68
|
login: string;
|
|
43
69
|
name: string;
|
|
@@ -50,13 +76,23 @@ interface PreviewTunnelGrant {
|
|
|
50
76
|
expiresAt: number;
|
|
51
77
|
}
|
|
52
78
|
declare function getPreviewAuthPublicConfig(options: PreviewManagedAuthOptions): {
|
|
79
|
+
defaultSessionTtlMs: number;
|
|
80
|
+
maxSessionTtlMs: number;
|
|
81
|
+
dashboardUrl?: string | undefined;
|
|
82
|
+
provider: "device";
|
|
83
|
+
clientId: string;
|
|
84
|
+
issuer: string;
|
|
53
85
|
enabled: true;
|
|
54
86
|
controlAuth: "bearer";
|
|
55
|
-
|
|
56
|
-
clientId: string;
|
|
57
|
-
scope: string;
|
|
87
|
+
} | {
|
|
58
88
|
defaultSessionTtlMs: number;
|
|
59
89
|
maxSessionTtlMs: number;
|
|
90
|
+
dashboardUrl?: string | undefined;
|
|
91
|
+
provider: "github";
|
|
92
|
+
clientId: string | undefined;
|
|
93
|
+
scope: string;
|
|
94
|
+
enabled: true;
|
|
95
|
+
controlAuth: "bearer";
|
|
60
96
|
};
|
|
61
97
|
declare function exchangeGitHubPreviewToken(providerToken: string, options: PreviewManagedAuthOptions): Promise<{
|
|
62
98
|
token: string;
|
|
@@ -66,6 +102,7 @@ declare function exchangeGitHubPreviewToken(providerToken: string, options: Prev
|
|
|
66
102
|
declare function verifyPreviewAccountToken(token: string, options: Pick<PreviewManagedAuthOptions, "signingSecret">): PreviewAccountClaims;
|
|
67
103
|
declare function issuePreviewTunnelGrant(account: PreviewAccountClaims, input: {
|
|
68
104
|
name: string;
|
|
105
|
+
project?: string;
|
|
69
106
|
expiresInMs?: number;
|
|
70
107
|
}, options: PreviewManagedAuthOptions): PreviewTunnelGrant;
|
|
71
108
|
declare function verifyPreviewTunnelGrant(token: string, options: Pick<PreviewManagedAuthOptions, "signingSecret"> & {
|
|
@@ -78,7 +115,20 @@ declare class PreviewAuthError extends Error {
|
|
|
78
115
|
}
|
|
79
116
|
//#endregion
|
|
80
117
|
//#region src/index.d.ts
|
|
118
|
+
interface PreviewSessionAccess {
|
|
119
|
+
id: string;
|
|
120
|
+
name: string;
|
|
121
|
+
ownerId?: string;
|
|
122
|
+
project?: string;
|
|
123
|
+
keyId?: string;
|
|
124
|
+
grantId?: string;
|
|
125
|
+
expiresAt?: number;
|
|
126
|
+
}
|
|
81
127
|
interface PreviewGatewayOptions {
|
|
128
|
+
/** Required access control when supplied. Denials and errors fail closed, unlike telemetry. */
|
|
129
|
+
authorizeSession?: (session: PreviewSessionAccess) => Promise<boolean>;
|
|
130
|
+
/** Optional metadata-only telemetry. Failures never interrupt a preview. */
|
|
131
|
+
observer?: PreviewGatewayObserver;
|
|
82
132
|
domain?: string;
|
|
83
133
|
baseUrl?: string;
|
|
84
134
|
store?: PreviewGatewayStore;
|
|
@@ -93,6 +143,10 @@ interface PreviewGatewayOptions {
|
|
|
93
143
|
auth?: PreviewManagedAuthOptions;
|
|
94
144
|
}
|
|
95
145
|
interface PreviewGatewaySession {
|
|
146
|
+
grantId?: string;
|
|
147
|
+
project?: string;
|
|
148
|
+
keyId?: string;
|
|
149
|
+
ownerId?: string;
|
|
96
150
|
id: string;
|
|
97
151
|
name: string;
|
|
98
152
|
hostname: string;
|
|
@@ -133,6 +187,28 @@ interface PreviewGatewayStore {
|
|
|
133
187
|
deleteResponse(sessionId: string, requestId: string): Promise<void>;
|
|
134
188
|
deleteSession(session: PreviewGatewaySession): Promise<void>;
|
|
135
189
|
}
|
|
190
|
+
interface PreviewGatewayObserver {
|
|
191
|
+
session?(event: {
|
|
192
|
+
grantId?: string;
|
|
193
|
+
project?: string;
|
|
194
|
+
keyId?: string;
|
|
195
|
+
id: string;
|
|
196
|
+
name: string;
|
|
197
|
+
ownerId?: string;
|
|
198
|
+
publicUrl: string;
|
|
199
|
+
expiresAt?: number;
|
|
200
|
+
state: "connected" | "disconnected";
|
|
201
|
+
at: number;
|
|
202
|
+
}): void | Promise<void>;
|
|
203
|
+
request?(event: {
|
|
204
|
+
sessionId: string;
|
|
205
|
+
method: string;
|
|
206
|
+
path: string;
|
|
207
|
+
status: number;
|
|
208
|
+
durationMs: number;
|
|
209
|
+
at: number;
|
|
210
|
+
}): void | Promise<void>;
|
|
211
|
+
}
|
|
136
212
|
/**
|
|
137
213
|
* A gateway request handler, plus the name-ownership probe another preview
|
|
138
214
|
* transport needs before it claims the same public hostname.
|
|
@@ -187,5 +263,5 @@ declare class RedisRestPreviewGatewayStore implements PreviewGatewayStore {
|
|
|
187
263
|
private responseKey;
|
|
188
264
|
}
|
|
189
265
|
//#endregion
|
|
190
|
-
export { MemoryPreviewGatewayStore, NodePreviewGatewayHandler, type PreviewAccountClaims, type PreviewAccountIdentity, PreviewAuthError, type PreviewAuthExchangeRateLimitResult, type PreviewAuthExchangeRateLimiter, PreviewGatewayHandler, PreviewGatewayOptions, PreviewGatewayRequest, PreviewGatewayResponse, PreviewGatewaySession, PreviewGatewayStore, type PreviewManagedAuthOptions, type PreviewTunnelGrant, type PreviewTunnelGrantClaims, RedisRestPreviewGatewayStore, createNodePreviewGatewayHandler, createPreviewGatewayHandler, createPreviewGatewayStoreFromEnv, exchangeGitHubPreviewToken, getPreviewAuthPublicConfig, issuePreviewTunnelGrant, readPreviewBearerToken, verifyPreviewAccountToken, verifyPreviewTunnelGrant };
|
|
191
|
-
//# sourceMappingURL=index-
|
|
266
|
+
export { MemoryPreviewGatewayStore, NodePreviewGatewayHandler, type PreviewAccountClaims, type PreviewAccountIdentity, PreviewAuthError, type PreviewAuthExchangeRateLimitResult, type PreviewAuthExchangeRateLimiter, PreviewGatewayHandler, PreviewGatewayObserver, PreviewGatewayOptions, PreviewGatewayRequest, PreviewGatewayResponse, PreviewGatewaySession, PreviewGatewayStore, type PreviewManagedAuthOptions, PreviewSessionAccess, type PreviewTunnelGrant, type PreviewTunnelGrantClaims, RedisRestPreviewGatewayStore, createNodePreviewGatewayHandler, createPreviewGatewayHandler, createPreviewGatewayStoreFromEnv, exchangeGitHubPreviewToken, getPreviewAuthPublicConfig, issuePreviewTunnelGrant, readPreviewBearerToken, verifyPreviewAccountToken, verifyPreviewTunnelGrant };
|
|
267
|
+
//# sourceMappingURL=index-DeizJwbc.d.ts.map
|
package/dist/index.js
CHANGED
|
@@ -13,15 +13,23 @@ function getPreviewAuthPublicConfig(options) {
|
|
|
13
13
|
return {
|
|
14
14
|
enabled: true,
|
|
15
15
|
controlAuth: "bearer",
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
16
|
+
...options.deviceAuth ? {
|
|
17
|
+
provider: "device",
|
|
18
|
+
clientId: options.deviceAuth.clientId,
|
|
19
|
+
issuer: options.deviceAuth.issuer
|
|
20
|
+
} : {
|
|
21
|
+
provider: "github",
|
|
22
|
+
clientId: options.githubClientId,
|
|
23
|
+
scope: "read:user"
|
|
24
|
+
},
|
|
25
|
+
...options.dashboardUrl ? { dashboardUrl: options.dashboardUrl } : {},
|
|
19
26
|
defaultSessionTtlMs: durations.defaultSessionTtlMs,
|
|
20
27
|
maxSessionTtlMs: durations.maxSessionTtlMs
|
|
21
28
|
};
|
|
22
29
|
}
|
|
23
30
|
async function exchangeGitHubPreviewToken(providerToken, options) {
|
|
24
31
|
validatePreviewAuthOptions(options);
|
|
32
|
+
if (options.deviceAuth) throw new PreviewAuthError(400, "Use the configured device sign-in flow.");
|
|
25
33
|
if (!providerToken || providerToken.length > 4096) throw new PreviewAuthError(400, "A GitHub access token is required.");
|
|
26
34
|
const response = await (options.fetch || fetch)(`${options.githubApiUrl || "https://api.github.com"}/user`, {
|
|
27
35
|
headers: {
|
|
@@ -66,17 +74,23 @@ function verifyPreviewAccountToken(token, options) {
|
|
|
66
74
|
function issuePreviewTunnelGrant(account, input, options) {
|
|
67
75
|
validatePreviewAuthOptions(options);
|
|
68
76
|
const durations = resolvePreviewDurations(options);
|
|
77
|
+
if (input.project !== void 0 && (typeof input.project !== "string" || !/^[a-z0-9][a-z0-9-]{0,62}$/.test(input.project))) throw new PreviewAuthError(400, "Preview project must be a lowercase slug of 1–63 characters.");
|
|
69
78
|
if (input.expiresInMs !== void 0 && (!Number.isSafeInteger(input.expiresInMs) || input.expiresInMs <= 0)) throw new PreviewAuthError(400, "Preview expiry must be a positive integer.");
|
|
70
79
|
const requestedTtlMs = input.expiresInMs ?? durations.defaultSessionTtlMs;
|
|
80
|
+
if (account.project !== void 0 && account.project !== (input.project ?? input.name)) throw new PreviewAuthError(403, "This API key is restricted to another preview project.");
|
|
71
81
|
const ttlMs = Math.max(MIN_SESSION_TTL_MS, Math.min(durations.maxSessionTtlMs, requestedTtlMs));
|
|
72
82
|
const issuedAt = Date.now();
|
|
73
|
-
const expiresAt = issuedAt + ttlMs;
|
|
83
|
+
const expiresAt = Math.min(issuedAt + ttlMs, account.expiresAt);
|
|
84
|
+
if (expiresAt <= issuedAt) throw new PreviewAuthError(401, "The account credential has expired.");
|
|
74
85
|
return {
|
|
75
86
|
token: signPreviewClaims({
|
|
76
87
|
kind: "tunnel",
|
|
88
|
+
provider: account.provider,
|
|
77
89
|
subject: account.subject,
|
|
78
90
|
login: account.login,
|
|
79
91
|
name: input.name,
|
|
92
|
+
project: input.project ?? input.name,
|
|
93
|
+
...account.keyId ? { keyId: account.keyId } : {},
|
|
80
94
|
issuedAt,
|
|
81
95
|
expiresAt,
|
|
82
96
|
nonce: randomUUID()
|
|
@@ -87,7 +101,7 @@ function issuePreviewTunnelGrant(account, input, options) {
|
|
|
87
101
|
}
|
|
88
102
|
function verifyPreviewTunnelGrant(token, options) {
|
|
89
103
|
const claims = verifyPreviewClaims(token, options.signingSecret);
|
|
90
|
-
if (claims.kind !== "tunnel" || typeof claims.subject !== "string" || typeof claims.login !== "string" || typeof claims.name !== "string" || options.name !== void 0 && claims.name !== options.name) throw new PreviewAuthError(401, "The Farm Preview tunnel grant is invalid.");
|
|
104
|
+
if (claims.kind !== "tunnel" || typeof claims.subject !== "string" || typeof claims.login !== "string" || typeof claims.name !== "string" || claims.nonce !== void 0 && (typeof claims.nonce !== "string" || !/^[\w-]{1,128}$/.test(claims.nonce)) || claims.project !== void 0 && (typeof claims.project !== "string" || !/^[a-z0-9][a-z0-9-]{0,62}$/.test(claims.project)) || claims.keyId !== void 0 && (typeof claims.keyId !== "string" || !/^[\w-]{1,128}$/.test(claims.keyId)) || options.name !== void 0 && claims.name !== options.name) throw new PreviewAuthError(401, "The Farm Preview tunnel grant is invalid.");
|
|
91
105
|
return claims;
|
|
92
106
|
}
|
|
93
107
|
function readPreviewBearerToken(request) {
|
|
@@ -110,13 +124,49 @@ function resolvePreviewDurations(options) {
|
|
|
110
124
|
}
|
|
111
125
|
function validatePreviewAuthOptions(options) {
|
|
112
126
|
if (Buffer.byteLength(options.signingSecret) < 32) throw new Error("Farm Preview auth signingSecret must contain at least 32 bytes.");
|
|
113
|
-
if (!options.githubClientId) throw new Error("Farm Preview auth requires a GitHub OAuth client id.");
|
|
127
|
+
if (!options.githubClientId && !options.deviceAuth) throw new Error("Farm Preview auth requires a GitHub OAuth client id or deviceAuth.");
|
|
128
|
+
if (options.deviceAuth) {
|
|
129
|
+
validatePublicAuthUrl(options.deviceAuth.issuer);
|
|
130
|
+
if (!options.deviceAuth.clientId) throw new Error("Farm Preview deviceAuth requires a clientId.");
|
|
131
|
+
}
|
|
132
|
+
if (options.dashboardUrl) validatePublicAuthUrl(options.dashboardUrl);
|
|
114
133
|
for (const [name, value] of [
|
|
115
134
|
["accountTokenTtlMs", options.accountTokenTtlMs],
|
|
116
135
|
["defaultSessionTtlMs", options.defaultSessionTtlMs],
|
|
117
136
|
["maxSessionTtlMs", options.maxSessionTtlMs]
|
|
118
137
|
]) if (value !== void 0 && (!Number.isSafeInteger(value) || value <= 0)) throw new Error(`Farm Preview auth ${name} must be a positive integer.`);
|
|
119
138
|
}
|
|
139
|
+
function validatePublicAuthUrl(value) {
|
|
140
|
+
const url = new URL(value);
|
|
141
|
+
const local = [
|
|
142
|
+
"localhost",
|
|
143
|
+
"127.0.0.1",
|
|
144
|
+
"[::1]"
|
|
145
|
+
].includes(url.hostname);
|
|
146
|
+
if (url.protocol !== "https:" && !(local && url.protocol === "http:") || url.username || url.password || url.search || url.hash) throw new Error("Farm Preview auth URLs must use HTTPS (HTTP only for loopback development), without credentials, queries or fragments.");
|
|
147
|
+
}
|
|
148
|
+
async function authorizePreviewAccount(token, options) {
|
|
149
|
+
if (!options.deviceAuth) return verifyPreviewAccountToken(token, options);
|
|
150
|
+
if (!token || token.length > 4096) throw new PreviewAuthError(401, "Invalid preview credential.");
|
|
151
|
+
let account;
|
|
152
|
+
try {
|
|
153
|
+
account = await options.deviceAuth.authorizeAccount(token);
|
|
154
|
+
} catch {
|
|
155
|
+
throw new PreviewAuthError(503, "Farm Preview authentication is temporarily unavailable.");
|
|
156
|
+
}
|
|
157
|
+
const now = Date.now();
|
|
158
|
+
if (!account || typeof account.subject !== "string" || !account.subject || typeof account.login !== "string" || !account.login || account.project !== void 0 && (typeof account.project !== "string" || !/^[a-z0-9][a-z0-9-]{0,62}$/.test(account.project)) || account.keyId !== void 0 && (typeof account.keyId !== "string" || !/^[\w-]{1,128}$/.test(account.keyId)) || account.expiresAt !== void 0 && (!Number.isSafeInteger(account.expiresAt) || account.expiresAt <= now)) throw new PreviewAuthError(401, "The Farm Infra credential is invalid or expired.");
|
|
159
|
+
return {
|
|
160
|
+
kind: "account",
|
|
161
|
+
provider: "device",
|
|
162
|
+
subject: account.subject,
|
|
163
|
+
login: account.login,
|
|
164
|
+
...account.keyId ? { keyId: account.keyId } : {},
|
|
165
|
+
...account.project ? { project: account.project } : {},
|
|
166
|
+
issuedAt: now,
|
|
167
|
+
expiresAt: account.expiresAt ?? now + DEFAULT_ACCOUNT_TOKEN_TTL_MS
|
|
168
|
+
};
|
|
169
|
+
}
|
|
120
170
|
function signPreviewClaims(claims, secret) {
|
|
121
171
|
const payload = Buffer.from(JSON.stringify(claims)).toString("base64url");
|
|
122
172
|
const signature = createHmac("sha256", secret).update(`${TOKEN_PREFIX}.${payload}`).digest("base64url");
|
|
@@ -145,6 +195,27 @@ function verifyPreviewClaims(token, secret) {
|
|
|
145
195
|
}
|
|
146
196
|
//#endregion
|
|
147
197
|
//#region src/index.ts
|
|
198
|
+
function observe(callback) {
|
|
199
|
+
if (!callback) return;
|
|
200
|
+
try {
|
|
201
|
+
Promise.resolve(callback()).catch(() => void 0);
|
|
202
|
+
} catch {}
|
|
203
|
+
}
|
|
204
|
+
function reportSession(config, session, state) {
|
|
205
|
+
if (!config.observer?.session) return;
|
|
206
|
+
observe(() => config.observer.session({
|
|
207
|
+
id: session.id,
|
|
208
|
+
grantId: session.grantId,
|
|
209
|
+
name: session.name,
|
|
210
|
+
project: session.project,
|
|
211
|
+
keyId: session.keyId,
|
|
212
|
+
ownerId: session.ownerId,
|
|
213
|
+
publicUrl: session.publicUrl,
|
|
214
|
+
expiresAt: session.expiresAt,
|
|
215
|
+
state,
|
|
216
|
+
at: Date.now()
|
|
217
|
+
}));
|
|
218
|
+
}
|
|
148
219
|
const DEFAULT_DOMAIN = "preview.farmjs.dev";
|
|
149
220
|
const DEFAULT_SESSION_TTL_MS = 18e5;
|
|
150
221
|
const DEFAULT_CLIENT_HEARTBEAT_TIMEOUT_MS = 2e4;
|
|
@@ -179,6 +250,8 @@ function createPreviewGatewayHandler(options = {}) {
|
|
|
179
250
|
if (options.auth) getPreviewAuthPublicConfig(options.auth);
|
|
180
251
|
const store = options.store || createPreviewGatewayStoreFromEnv();
|
|
181
252
|
const config = {
|
|
253
|
+
authorizeSession: options.authorizeSession,
|
|
254
|
+
observer: options.observer,
|
|
182
255
|
domain: normalizeDomain(options.domain || process.env.FARM_PREVIEW_DOMAIN || DEFAULT_DOMAIN),
|
|
183
256
|
baseUrl: options.baseUrl || process.env.FARM_PREVIEW_GATEWAY_URL,
|
|
184
257
|
sessionTtlMs: options.sessionTtlMs ?? DEFAULT_SESSION_TTL_MS,
|
|
@@ -212,6 +285,10 @@ function createPreviewGatewayHandler(options = {}) {
|
|
|
212
285
|
}
|
|
213
286
|
if (request.method === "POST" && url.pathname === "/api/tunnel/grants") {
|
|
214
287
|
if (!config.auth) return text("Managed preview authentication is not configured.", 404);
|
|
288
|
+
if (config.auth.deviceAuth) {
|
|
289
|
+
const limited = await limitPreviewAccountExchange(request, config.auth);
|
|
290
|
+
if (limited) return limited;
|
|
291
|
+
}
|
|
215
292
|
return await createTunnelGrant(request, config.auth);
|
|
216
293
|
}
|
|
217
294
|
if (request.method === "POST" && url.pathname === "/api/sessions") return await createSession(request, store, config);
|
|
@@ -270,12 +347,13 @@ async function exchangePreviewAccount(request, auth) {
|
|
|
270
347
|
async function createTunnelGrant(request, auth) {
|
|
271
348
|
const accountToken = readPreviewBearerToken(request);
|
|
272
349
|
if (!accountToken) throw new PreviewAuthError(401, "Sign in with Farm Preview before creating a tunnel grant.");
|
|
273
|
-
const account =
|
|
350
|
+
const account = await authorizePreviewAccount(accountToken, auth);
|
|
274
351
|
const input = await request.json().catch(() => ({}));
|
|
275
352
|
const name = sanitizePreviewName(input.name);
|
|
276
353
|
if (!name) throw new PreviewAuthError(400, "A valid preview name is required.");
|
|
277
354
|
return json(issuePreviewTunnelGrant(account, {
|
|
278
355
|
name,
|
|
356
|
+
project: input.project,
|
|
279
357
|
...input.expiresInMs !== void 0 ? { expiresInMs: input.expiresInMs } : {}
|
|
280
358
|
}, auth));
|
|
281
359
|
}
|
|
@@ -457,6 +535,10 @@ async function createSession(request, store, config) {
|
|
|
457
535
|
let name = requestedName || randomPreviewName();
|
|
458
536
|
let expiresAt = Date.now() + config.sessionTtlMs;
|
|
459
537
|
let slidingExpiration = true;
|
|
538
|
+
let ownerId;
|
|
539
|
+
let project;
|
|
540
|
+
let keyId;
|
|
541
|
+
let grantId;
|
|
460
542
|
if (config.auth) {
|
|
461
543
|
const grantToken = readPreviewBearerToken(request);
|
|
462
544
|
if (!grantToken) throw new GatewayHttpError(401, "Sign in with Farm Preview before creating a session.");
|
|
@@ -466,6 +548,10 @@ async function createSession(request, store, config) {
|
|
|
466
548
|
...requestedName ? { name: requestedName } : {}
|
|
467
549
|
});
|
|
468
550
|
name = grant.name;
|
|
551
|
+
ownerId = `${grant.provider || "github"}:${grant.subject}`;
|
|
552
|
+
project = grant.project ?? grant.name;
|
|
553
|
+
keyId = grant.keyId;
|
|
554
|
+
grantId = grant.nonce;
|
|
469
555
|
expiresAt = grant.expiresAt;
|
|
470
556
|
slidingExpiration = false;
|
|
471
557
|
} catch (error) {
|
|
@@ -485,6 +571,10 @@ async function createSession(request, store, config) {
|
|
|
485
571
|
}
|
|
486
572
|
const hostname = `${name}.${config.domain}`;
|
|
487
573
|
const session = {
|
|
574
|
+
...grantId ? { grantId } : {},
|
|
575
|
+
...project ? { project } : {},
|
|
576
|
+
...keyId ? { keyId } : {},
|
|
577
|
+
...ownerId ? { ownerId } : {},
|
|
488
578
|
id: randomId("sess"),
|
|
489
579
|
name,
|
|
490
580
|
hostname,
|
|
@@ -496,7 +586,9 @@ async function createSession(request, store, config) {
|
|
|
496
586
|
...slidingExpiration ? { slidingExpiration: true } : {},
|
|
497
587
|
lastHeartbeatAt: Date.now()
|
|
498
588
|
};
|
|
589
|
+
await requireSessionAccess(config, session);
|
|
499
590
|
await store.createSession(session, Math.max(1, expiresAt - Date.now()));
|
|
591
|
+
reportSession(config, session, "connected");
|
|
500
592
|
return json({
|
|
501
593
|
id: session.id,
|
|
502
594
|
name: session.name,
|
|
@@ -507,6 +599,7 @@ async function createSession(request, store, config) {
|
|
|
507
599
|
}
|
|
508
600
|
async function handleSessionRoute(request, url, store, config, route) {
|
|
509
601
|
const session = await requireSession(store, route.sessionId, readPreviewBearerToken(request) || url.searchParams.get("token"));
|
|
602
|
+
if (request.method !== "DELETE" || route.action) await requireSessionAccess(config, session);
|
|
510
603
|
if (request.method === "GET" && route.action === "requests") return await pollSessionRequests(url, store, config, session);
|
|
511
604
|
if (request.method === "POST" && route.action === "responses" && route.requestId) {
|
|
512
605
|
let response;
|
|
@@ -529,6 +622,7 @@ async function handleSessionRoute(request, url, store, config, route) {
|
|
|
529
622
|
}
|
|
530
623
|
if (request.method === "DELETE" && !route.action) {
|
|
531
624
|
await store.deleteSession(session);
|
|
625
|
+
reportSession(config, session, "disconnected");
|
|
532
626
|
return json({ ok: true });
|
|
533
627
|
}
|
|
534
628
|
return text("Preview gateway route not found.", 404);
|
|
@@ -565,10 +659,16 @@ function createOversizedPreviewResponse(maxBodyBytes) {
|
|
|
565
659
|
async function pollSessionRequests(url, store, config, session) {
|
|
566
660
|
const waitMs = clamp(Number(url.searchParams.get("wait") || config.pollTimeoutMs), 1e3, 25e3);
|
|
567
661
|
const deadline = Date.now() + waitMs;
|
|
662
|
+
let nextAccessCheckAt = Date.now() + 1e3;
|
|
568
663
|
await markSessionOnline(store, config, session);
|
|
569
664
|
while (Date.now() < deadline) {
|
|
665
|
+
if (Date.now() >= nextAccessCheckAt) {
|
|
666
|
+
await requireSessionAccess(config, session);
|
|
667
|
+
nextAccessCheckAt = Date.now() + 1e3;
|
|
668
|
+
}
|
|
570
669
|
const requests = await store.takeRequests(session.id, DEFAULT_POLL_REQUEST_LIMIT);
|
|
571
670
|
if (requests.length) {
|
|
671
|
+
await requireSessionAccess(config, session);
|
|
572
672
|
await markSessionOnline(store, config, session);
|
|
573
673
|
return json({ requests });
|
|
574
674
|
}
|
|
@@ -582,37 +682,63 @@ async function proxyPublicRequest(request, url, store, config, route) {
|
|
|
582
682
|
if (!session) return inactivePreviewResponse(request, route.name);
|
|
583
683
|
if (!isPreviewClientOnline(session, config)) {
|
|
584
684
|
await store.deleteSession(session);
|
|
685
|
+
reportSession(config, session, "disconnected");
|
|
585
686
|
return inactivePreviewResponse(request, route.name);
|
|
586
687
|
}
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
await store.enqueueRequest(session.id,
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
headers
|
|
615
|
-
|
|
688
|
+
await requireSessionAccess(config, session);
|
|
689
|
+
const startedAt = Date.now();
|
|
690
|
+
let status = 500;
|
|
691
|
+
try {
|
|
692
|
+
const previewRequest = await serializePreviewRequest(request, url, route.path, config.maxBodyBytes);
|
|
693
|
+
await store.enqueueRequest(session.id, previewRequest, remainingSessionTtl(session, config));
|
|
694
|
+
await store.touchSession(session, remainingSessionTtl(session, config));
|
|
695
|
+
const response = await waitForPreviewResponse(store, config, session, previewRequest.id, request.signal);
|
|
696
|
+
if (response === "cancelled") {
|
|
697
|
+
status = 499;
|
|
698
|
+
await store.enqueueRequest(session.id, {
|
|
699
|
+
...previewRequest,
|
|
700
|
+
body: void 0,
|
|
701
|
+
encoding: void 0,
|
|
702
|
+
cancelled: true,
|
|
703
|
+
createdAt: Date.now()
|
|
704
|
+
}, remainingSessionTtl(session, config));
|
|
705
|
+
return new Response(null, { status: 499 });
|
|
706
|
+
}
|
|
707
|
+
if (response === "stale") {
|
|
708
|
+
status = 503;
|
|
709
|
+
return inactivePreviewResponse(request, route.name);
|
|
710
|
+
}
|
|
711
|
+
if (!response) {
|
|
712
|
+
status = 504;
|
|
713
|
+
return text("The local Farm preview did not respond before the gateway timed out.", 504);
|
|
714
|
+
}
|
|
715
|
+
const headers = new Headers();
|
|
716
|
+
const responseHopByHopHeaders = getHopByHopHeaderNames(response.headers || {});
|
|
717
|
+
for (const [key, value] of Object.entries(response.headers || {})) {
|
|
718
|
+
const normalized = key.toLowerCase();
|
|
719
|
+
if (responseHopByHopHeaders.has(normalized) || normalized === "content-encoding") continue;
|
|
720
|
+
for (const single of Array.isArray(value) ? value : [value]) headers.append(key, single);
|
|
721
|
+
}
|
|
722
|
+
headers.set("cache-control", "no-store");
|
|
723
|
+
headers.set("x-farm-preview", session.name);
|
|
724
|
+
status = response.status || 200;
|
|
725
|
+
return new Response(response.body ? Buffer.from(response.body, response.encoding === "base64" ? "base64" : "utf8") : null, {
|
|
726
|
+
status,
|
|
727
|
+
headers
|
|
728
|
+
});
|
|
729
|
+
} catch (error) {
|
|
730
|
+
if (error instanceof GatewayHttpError) status = error.status;
|
|
731
|
+
throw error;
|
|
732
|
+
} finally {
|
|
733
|
+
if (config.observer?.request) observe(() => config.observer.request({
|
|
734
|
+
sessionId: session.id,
|
|
735
|
+
method: request.method,
|
|
736
|
+
path: route.path.split(/[?#]/, 1)[0].slice(0, 2048),
|
|
737
|
+
status,
|
|
738
|
+
durationMs: Date.now() - startedAt,
|
|
739
|
+
at: Date.now()
|
|
740
|
+
}));
|
|
741
|
+
}
|
|
616
742
|
}
|
|
617
743
|
async function waitForPreviewResponse(store, config, session, requestId, signal) {
|
|
618
744
|
const deadline = Date.now() + config.requestTimeoutMs;
|
|
@@ -620,6 +746,7 @@ async function waitForPreviewResponse(store, config, session, requestId, signal)
|
|
|
620
746
|
while (Date.now() < deadline) {
|
|
621
747
|
if (signal.aborted) return "cancelled";
|
|
622
748
|
if (Date.now() >= nextLivenessCheckAt) {
|
|
749
|
+
await requireSessionAccess(config, session);
|
|
623
750
|
const latestSession = await store.getSessionById(session.id);
|
|
624
751
|
if (!latestSession || !isPreviewClientOnline(latestSession, config)) {
|
|
625
752
|
if (latestSession) await store.deleteSession(latestSession);
|
|
@@ -629,6 +756,7 @@ async function waitForPreviewResponse(store, config, session, requestId, signal)
|
|
|
629
756
|
}
|
|
630
757
|
const response = await store.getResponse(session.id, requestId);
|
|
631
758
|
if (response) {
|
|
759
|
+
await requireSessionAccess(config, session);
|
|
632
760
|
await store.deleteResponse(session.id, requestId);
|
|
633
761
|
return response;
|
|
634
762
|
}
|
|
@@ -640,11 +768,31 @@ async function waitForPreviewResponse(store, config, session, requestId, signal)
|
|
|
640
768
|
}
|
|
641
769
|
}
|
|
642
770
|
}
|
|
771
|
+
async function requireSessionAccess(config, session) {
|
|
772
|
+
if (!config.authorizeSession) return;
|
|
773
|
+
const { id, name, ownerId, project, keyId, grantId, expiresAt } = session;
|
|
774
|
+
let allowed;
|
|
775
|
+
try {
|
|
776
|
+
allowed = await config.authorizeSession({
|
|
777
|
+
id,
|
|
778
|
+
name,
|
|
779
|
+
ownerId,
|
|
780
|
+
project,
|
|
781
|
+
keyId,
|
|
782
|
+
grantId,
|
|
783
|
+
expiresAt
|
|
784
|
+
});
|
|
785
|
+
} catch {
|
|
786
|
+
throw new GatewayHttpError(503, "Preview access verification is temporarily unavailable.");
|
|
787
|
+
}
|
|
788
|
+
if (allowed !== true) throw new GatewayHttpError(403, "This preview is no longer authorized.");
|
|
789
|
+
}
|
|
643
790
|
async function markSessionOnline(store, config, session) {
|
|
644
791
|
const now = Date.now();
|
|
645
792
|
session.lastHeartbeatAt = now;
|
|
646
793
|
if (session.slidingExpiration) session.expiresAt = now + config.sessionTtlMs;
|
|
647
794
|
await store.touchSession(session, remainingSessionTtl(session, config));
|
|
795
|
+
reportSession(config, session, "connected");
|
|
648
796
|
}
|
|
649
797
|
function remainingSessionTtl(session, config) {
|
|
650
798
|
return Math.max(1, Math.min(config.sessionTtlMs, session.expiresAt - Date.now()));
|
|
@@ -757,6 +905,11 @@ function headerValue(headers, key) {
|
|
|
757
905
|
return Array.isArray(value) ? value[0] : value;
|
|
758
906
|
}
|
|
759
907
|
function createPublicUrl(request, config, name) {
|
|
908
|
+
if (config.domain === "localhost" || config.domain.endsWith(".localhost")) {
|
|
909
|
+
const base = new URL(config.baseUrl || request.url);
|
|
910
|
+
base.hostname = `${name}.${config.domain}`;
|
|
911
|
+
return base.origin;
|
|
912
|
+
}
|
|
760
913
|
if (config.baseUrl) {
|
|
761
914
|
if (isLocalHost(new URL(config.baseUrl).hostname)) return `${config.baseUrl.replace(/\/+$/, "")}/__preview/${name}`;
|
|
762
915
|
}
|
|
@@ -819,7 +972,7 @@ function clamp(value, min, max) {
|
|
|
819
972
|
return Math.max(min, Math.min(max, value));
|
|
820
973
|
}
|
|
821
974
|
function isLocalHost(hostname) {
|
|
822
|
-
return hostname === "localhost" || hostname === "127.0.0.1" || hostname === "::1";
|
|
975
|
+
return hostname === "localhost" || hostname.endsWith(".localhost") || hostname === "127.0.0.1" || hostname === "::1";
|
|
823
976
|
}
|
|
824
977
|
var GatewayHttpError = class extends Error {
|
|
825
978
|
constructor(status, message) {
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":["DEFAULT_SESSION_TTL_MS","delay"],"sources":["../src/auth.ts","../src/index.ts"],"sourcesContent":["import { createHmac, randomUUID, timingSafeEqual } from \"node:crypto\";\n\nexport interface PreviewManagedAuthOptions {\n /** Secret used to sign account tokens and one-preview tunnel grants. */\n signingSecret: string;\n /** Public client id for the Farming Labs GitHub OAuth app. */\n githubClientId: string;\n accountTokenTtlMs?: number;\n defaultSessionTtlMs?: number;\n maxSessionTtlMs?: number;\n githubApiUrl?: string;\n fetch?: typeof fetch;\n /**\n * Shared abuse-control hook for the provider-token exchange endpoint.\n * Implementations must not consume the request body. Failures reject the\n * exchange with a retryable 503 response instead of bypassing the limiter.\n */\n rateLimitExchange?: PreviewAuthExchangeRateLimiter;\n}\n\nexport interface PreviewAuthExchangeRateLimitResult {\n allowed: boolean;\n /** How long the caller should wait before retrying a rejected exchange. */\n retryAfterMs?: number;\n}\n\nexport type PreviewAuthExchangeRateLimiter = (\n request: Request,\n) => PreviewAuthExchangeRateLimitResult | Promise<PreviewAuthExchangeRateLimitResult>;\n\nexport interface PreviewAccountIdentity {\n provider: \"github\";\n subject: string;\n login: string;\n name?: string;\n avatarUrl?: string;\n}\n\nexport interface PreviewAccountClaims extends PreviewAccountIdentity {\n kind: \"account\";\n issuedAt: number;\n expiresAt: number;\n}\n\nexport interface PreviewTunnelGrantClaims {\n kind: \"tunnel\";\n subject: string;\n login: string;\n name: string;\n issuedAt: number;\n expiresAt: number;\n}\n\nexport interface PreviewTunnelGrant {\n token: string;\n name: string;\n expiresAt: number;\n}\n\nconst TOKEN_PREFIX = \"farm_preview_v1\";\nconst DEFAULT_ACCOUNT_TOKEN_TTL_MS = 1000 * 60 * 60 * 24 * 7;\nconst DEFAULT_SESSION_TTL_MS = 1000 * 60 * 60;\nconst DEFAULT_MAX_SESSION_TTL_MS = 1000 * 60 * 60 * 24;\nconst MIN_SESSION_TTL_MS = 1000 * 60;\nconst MAX_CLOCK_SKEW_MS = 1000 * 60 * 5;\n\nexport function getPreviewAuthPublicConfig(options: PreviewManagedAuthOptions) {\n validatePreviewAuthOptions(options);\n const durations = resolvePreviewDurations(options);\n return {\n enabled: true as const,\n controlAuth: \"bearer\" as const,\n provider: \"github\" as const,\n clientId: options.githubClientId,\n scope: \"read:user\",\n defaultSessionTtlMs: durations.defaultSessionTtlMs,\n maxSessionTtlMs: durations.maxSessionTtlMs,\n };\n}\n\nexport async function exchangeGitHubPreviewToken(\n providerToken: string,\n options: PreviewManagedAuthOptions,\n): Promise<{ token: string; expiresAt: number; user: PreviewAccountIdentity }> {\n validatePreviewAuthOptions(options);\n if (!providerToken || providerToken.length > 4096) {\n throw new PreviewAuthError(400, \"A GitHub access token is required.\");\n }\n\n const request = options.fetch || fetch;\n const response = await request(`${options.githubApiUrl || \"https://api.github.com\"}/user`, {\n headers: {\n accept: \"application/vnd.github+json\",\n authorization: `Bearer ${providerToken}`,\n \"user-agent\": \"farm-preview-gateway\",\n \"x-github-api-version\": \"2026-03-10\",\n },\n signal: AbortSignal.timeout(10_000),\n });\n if (!response.ok) {\n await response.body?.cancel();\n throw new PreviewAuthError(401, \"GitHub could not verify this Farm Preview login.\");\n }\n\n const profile = (await response.json()) as {\n id?: number;\n login?: string;\n name?: string | null;\n avatar_url?: string;\n };\n if (!Number.isSafeInteger(profile.id) || !profile.login) {\n throw new PreviewAuthError(502, \"GitHub returned an invalid user profile.\");\n }\n\n const now = Date.now();\n const expiresAt = now + (options.accountTokenTtlMs ?? DEFAULT_ACCOUNT_TOKEN_TTL_MS);\n const user: PreviewAccountIdentity = {\n provider: \"github\",\n subject: String(profile.id),\n login: profile.login,\n ...(profile.name ? { name: profile.name } : {}),\n ...(profile.avatar_url ? { avatarUrl: profile.avatar_url } : {}),\n };\n const claims: PreviewAccountClaims = {\n kind: \"account\",\n ...user,\n issuedAt: now,\n expiresAt,\n };\n\n return {\n token: signPreviewClaims(claims, options.signingSecret),\n expiresAt,\n user,\n };\n}\n\nexport function verifyPreviewAccountToken(\n token: string,\n options: Pick<PreviewManagedAuthOptions, \"signingSecret\">,\n): PreviewAccountClaims {\n const claims = verifyPreviewClaims(token, options.signingSecret);\n if (\n claims.kind !== \"account\" ||\n claims.provider !== \"github\" ||\n typeof claims.subject !== \"string\" ||\n typeof claims.login !== \"string\"\n ) {\n throw new PreviewAuthError(401, \"The Farm Preview account token is invalid.\");\n }\n return claims as unknown as PreviewAccountClaims;\n}\n\nexport function issuePreviewTunnelGrant(\n account: PreviewAccountClaims,\n input: { name: string; expiresInMs?: number },\n options: PreviewManagedAuthOptions,\n): PreviewTunnelGrant {\n validatePreviewAuthOptions(options);\n const durations = resolvePreviewDurations(options);\n if (\n input.expiresInMs !== undefined &&\n (!Number.isSafeInteger(input.expiresInMs) || input.expiresInMs <= 0)\n ) {\n throw new PreviewAuthError(400, \"Preview expiry must be a positive integer.\");\n }\n const requestedTtlMs = input.expiresInMs ?? durations.defaultSessionTtlMs;\n const ttlMs = Math.max(MIN_SESSION_TTL_MS, Math.min(durations.maxSessionTtlMs, requestedTtlMs));\n const issuedAt = Date.now();\n const expiresAt = issuedAt + ttlMs;\n const claims: PreviewTunnelGrantClaims & { nonce: string } = {\n kind: \"tunnel\",\n subject: account.subject,\n login: account.login,\n name: input.name,\n issuedAt,\n expiresAt,\n nonce: randomUUID(),\n };\n return {\n token: signPreviewClaims(claims, options.signingSecret),\n name: input.name,\n expiresAt,\n };\n}\n\nexport function verifyPreviewTunnelGrant(\n token: string,\n options: Pick<PreviewManagedAuthOptions, \"signingSecret\"> & { name?: string },\n): PreviewTunnelGrantClaims {\n const claims = verifyPreviewClaims(token, options.signingSecret);\n if (\n claims.kind !== \"tunnel\" ||\n typeof claims.subject !== \"string\" ||\n typeof claims.login !== \"string\" ||\n typeof claims.name !== \"string\" ||\n (options.name !== undefined && claims.name !== options.name)\n ) {\n throw new PreviewAuthError(401, \"The Farm Preview tunnel grant is invalid.\");\n }\n return claims as unknown as PreviewTunnelGrantClaims;\n}\n\nexport function readPreviewBearerToken(request: Request): string | undefined {\n const authorization = request.headers.get(\"authorization\");\n if (!authorization || !/^Bearer /i.test(authorization)) return undefined;\n const token = authorization.slice(7).trim();\n return token || undefined;\n}\n\nexport class PreviewAuthError extends Error {\n constructor(\n public status: number,\n message: string,\n ) {\n super(message);\n }\n}\n\nfunction resolvePreviewDurations(options: PreviewManagedAuthOptions) {\n const maxSessionTtlMs = Math.max(\n MIN_SESSION_TTL_MS,\n options.maxSessionTtlMs ?? DEFAULT_MAX_SESSION_TTL_MS,\n );\n return {\n maxSessionTtlMs,\n defaultSessionTtlMs: Math.min(\n maxSessionTtlMs,\n Math.max(MIN_SESSION_TTL_MS, options.defaultSessionTtlMs ?? DEFAULT_SESSION_TTL_MS),\n ),\n };\n}\n\nfunction validatePreviewAuthOptions(options: PreviewManagedAuthOptions) {\n if (Buffer.byteLength(options.signingSecret) < 32) {\n throw new Error(\"Farm Preview auth signingSecret must contain at least 32 bytes.\");\n }\n if (!options.githubClientId) {\n throw new Error(\"Farm Preview auth requires a GitHub OAuth client id.\");\n }\n for (const [name, value] of [\n [\"accountTokenTtlMs\", options.accountTokenTtlMs],\n [\"defaultSessionTtlMs\", options.defaultSessionTtlMs],\n [\"maxSessionTtlMs\", options.maxSessionTtlMs],\n ] as const) {\n if (value !== undefined && (!Number.isSafeInteger(value) || value <= 0)) {\n throw new Error(`Farm Preview auth ${name} must be a positive integer.`);\n }\n }\n}\n\nfunction signPreviewClaims(claims: object, secret: string) {\n const payload = Buffer.from(JSON.stringify(claims)).toString(\"base64url\");\n const signature = createHmac(\"sha256\", secret)\n .update(`${TOKEN_PREFIX}.${payload}`)\n .digest(\"base64url\");\n return `${TOKEN_PREFIX}.${payload}.${signature}`;\n}\n\nfunction verifyPreviewClaims(token: string, secret: string): Record<string, unknown> {\n if (!token || token.length > 16_384) {\n throw new PreviewAuthError(401, \"The Farm Preview credential is invalid.\");\n }\n const [prefix, payload, presentedSignature, ...rest] = token.split(\".\");\n if (prefix !== TOKEN_PREFIX || !payload || !presentedSignature || rest.length) {\n throw new PreviewAuthError(401, \"The Farm Preview credential is invalid.\");\n }\n\n const expectedSignature = createHmac(\"sha256\", secret)\n .update(`${TOKEN_PREFIX}.${payload}`)\n .digest();\n let presented: Buffer;\n try {\n presented = Buffer.from(presentedSignature, \"base64url\");\n } catch {\n throw new PreviewAuthError(401, \"The Farm Preview credential is invalid.\");\n }\n if (\n presented.length !== expectedSignature.length ||\n !timingSafeEqual(presented, expectedSignature)\n ) {\n throw new PreviewAuthError(401, \"The Farm Preview credential is invalid.\");\n }\n\n let claims: unknown;\n try {\n claims = JSON.parse(Buffer.from(payload, \"base64url\").toString(\"utf8\"));\n } catch {\n throw new PreviewAuthError(401, \"The Farm Preview credential is invalid.\");\n }\n if (\n typeof claims !== \"object\" ||\n claims === null ||\n Array.isArray(claims) ||\n !(\"issuedAt\" in claims) ||\n !(\"expiresAt\" in claims) ||\n !Number.isSafeInteger(claims.issuedAt) ||\n !Number.isSafeInteger(claims.expiresAt) ||\n Number(claims.issuedAt) > Date.now() + MAX_CLOCK_SKEW_MS ||\n Number(claims.expiresAt) <= Date.now()\n ) {\n throw new PreviewAuthError(401, \"The Farm Preview credential has expired or is invalid.\");\n }\n return claims as Record<string, unknown>;\n}\n","import type { IncomingHttpHeaders, IncomingMessage, ServerResponse } from \"node:http\";\nimport { setTimeout as delay } from \"node:timers/promises\";\nimport {\n exchangeGitHubPreviewToken,\n getPreviewAuthPublicConfig,\n issuePreviewTunnelGrant,\n PreviewAuthError,\n readPreviewBearerToken,\n verifyPreviewAccountToken,\n verifyPreviewTunnelGrant,\n type PreviewAuthExchangeRateLimitResult,\n type PreviewManagedAuthOptions,\n} from \"./auth.js\";\n\nexport {\n exchangeGitHubPreviewToken,\n getPreviewAuthPublicConfig,\n issuePreviewTunnelGrant,\n PreviewAuthError,\n readPreviewBearerToken,\n verifyPreviewAccountToken,\n verifyPreviewTunnelGrant,\n type PreviewAccountClaims,\n type PreviewAuthExchangeRateLimiter,\n type PreviewAuthExchangeRateLimitResult,\n type PreviewAccountIdentity,\n type PreviewManagedAuthOptions,\n type PreviewTunnelGrant,\n type PreviewTunnelGrantClaims,\n} from \"./auth.js\";\n\nexport interface PreviewGatewayOptions {\n domain?: string;\n baseUrl?: string;\n store?: PreviewGatewayStore;\n sessionTtlMs?: number;\n clientHeartbeatTimeoutMs?: number;\n requestTimeoutMs?: number;\n pollTimeoutMs?: number;\n pollIntervalMs?: number;\n maxBodyBytes?: number;\n maxResponseBodyBytes?: number;\n /** Managed account login and one-preview grant configuration. */\n auth?: PreviewManagedAuthOptions;\n}\n\nexport interface PreviewGatewaySession {\n id: string;\n name: string;\n hostname: string;\n publicUrl: string;\n token: string;\n localUrl?: string;\n createdAt: number;\n expiresAt: number;\n /** Internal idle expiry marker. Missing means expiresAt is an absolute cap. */\n slidingExpiration?: true;\n lastHeartbeatAt?: number;\n}\n\nexport interface PreviewGatewayRequest {\n id: string;\n method: string;\n path: string;\n headers: Record<string, string>;\n body?: string | null;\n encoding?: \"base64\";\n createdAt: number;\n cancelled?: true;\n}\n\nexport interface PreviewGatewayResponse {\n status: number;\n // A header may carry multiple values (notably Set-Cookie: a login response\n // commonly sets a session cookie and a CSRF cookie). Those must survive as\n // an array through the store and back onto the public response.\n headers?: Record<string, string | string[]>;\n body?: string | null;\n encoding?: \"base64\";\n}\n\nexport interface PreviewGatewayStore {\n createSession(session: PreviewGatewaySession, ttlMs: number): Promise<void>;\n getSessionByName(name: string): Promise<PreviewGatewaySession | undefined>;\n getSessionById(id: string): Promise<PreviewGatewaySession | undefined>;\n touchSession(session: PreviewGatewaySession, ttlMs: number): Promise<void>;\n enqueueRequest(sessionId: string, request: PreviewGatewayRequest, ttlMs: number): Promise<void>;\n takeRequests(sessionId: string, limit: number): Promise<PreviewGatewayRequest[]>;\n saveResponse(\n sessionId: string,\n requestId: string,\n response: PreviewGatewayResponse,\n ttlMs: number,\n ): Promise<void>;\n getResponse(sessionId: string, requestId: string): Promise<PreviewGatewayResponse | undefined>;\n deleteResponse(sessionId: string, requestId: string): Promise<void>;\n deleteSession(session: PreviewGatewaySession): Promise<void>;\n}\n\ninterface PreviewGatewayRuntimeConfig {\n domain: string;\n baseUrl?: string;\n sessionTtlMs: number;\n clientHeartbeatTimeoutMs: number;\n requestTimeoutMs: number;\n pollTimeoutMs: number;\n pollIntervalMs: number;\n maxBodyBytes: number;\n maxResponseBodyBytes: number;\n auth?: PreviewManagedAuthOptions;\n}\n\nconst DEFAULT_DOMAIN = \"preview.farmjs.dev\";\nconst DEFAULT_SESSION_TTL_MS = 1000 * 60 * 30;\nconst DEFAULT_CLIENT_HEARTBEAT_TIMEOUT_MS = 1000 * 20;\nconst DEFAULT_REQUEST_TIMEOUT_MS = 25000;\nconst DEFAULT_POLL_TIMEOUT_MS = 15000;\nconst DEFAULT_POLL_INTERVAL_MS = 100;\nconst DEFAULT_MAX_BODY_BYTES = 1024 * 1024 * 5;\nconst DEFAULT_MAX_RESPONSE_BODY_BYTES = 1024 * 1024 * 5;\nconst DEFAULT_POLL_REQUEST_LIMIT = 50;\nconst HOP_BY_HOP_HEADERS = new Set([\n \"connection\",\n \"content-length\",\n \"keep-alive\",\n \"proxy-authenticate\",\n \"proxy-authorization\",\n \"te\",\n \"trailer\",\n \"transfer-encoding\",\n \"upgrade\",\n]);\n\nfunction getHopByHopHeaderNames(headers: Headers | Record<string, string | string[]>): Set<string> {\n const names = new Set(HOP_BY_HOP_HEADERS);\n const connection =\n headers instanceof Headers\n ? headers.get(\"connection\")\n : Object.entries(headers).find(([name]) => name.toLowerCase() === \"connection\")?.[1];\n const values = Array.isArray(connection) ? connection : connection ? [connection] : [];\n for (const value of values) {\n for (const name of value.split(\",\")) {\n const normalized = name.trim().toLowerCase();\n if (normalized) names.add(normalized);\n }\n }\n return names;\n}\n\n/**\n * A gateway request handler, plus the name-ownership probe another preview\n * transport needs before it claims the same public hostname.\n */\nexport type PreviewGatewayHandler = ((request: Request) => Promise<Response>) & {\n isPreviewNameClaimed(name: string): Promise<boolean>;\n};\n\nexport type NodePreviewGatewayHandler = ((\n req: IncomingMessage,\n res: ServerResponse,\n) => Promise<void>) & {\n isPreviewNameClaimed(name: string): Promise<boolean>;\n};\n\nexport function createPreviewGatewayHandler(\n options: PreviewGatewayOptions = {},\n): PreviewGatewayHandler {\n if (options.auth) getPreviewAuthPublicConfig(options.auth);\n const store = options.store || createPreviewGatewayStoreFromEnv();\n const config = {\n domain: normalizeDomain(options.domain || process.env.FARM_PREVIEW_DOMAIN || DEFAULT_DOMAIN),\n baseUrl: options.baseUrl || process.env.FARM_PREVIEW_GATEWAY_URL,\n sessionTtlMs: options.sessionTtlMs ?? DEFAULT_SESSION_TTL_MS,\n clientHeartbeatTimeoutMs:\n options.clientHeartbeatTimeoutMs ?? DEFAULT_CLIENT_HEARTBEAT_TIMEOUT_MS,\n requestTimeoutMs: options.requestTimeoutMs ?? DEFAULT_REQUEST_TIMEOUT_MS,\n pollTimeoutMs: options.pollTimeoutMs ?? DEFAULT_POLL_TIMEOUT_MS,\n pollIntervalMs: options.pollIntervalMs ?? DEFAULT_POLL_INTERVAL_MS,\n maxBodyBytes: options.maxBodyBytes ?? DEFAULT_MAX_BODY_BYTES,\n maxResponseBodyBytes: options.maxResponseBodyBytes ?? DEFAULT_MAX_RESPONSE_BODY_BYTES,\n auth: options.auth,\n };\n\n const handlePreviewGatewayRequest = async function handlePreviewGatewayRequest(\n request: Request,\n ): Promise<Response> {\n try {\n const url = new URL(request.url);\n\n if (request.method === \"GET\" && url.pathname === \"/api/health\") {\n return json({\n ok: true,\n domain: config.domain,\n store: store.constructor.name || \"PreviewGatewayStore\",\n });\n }\n\n if (request.method === \"GET\" && url.pathname === \"/api/auth/config\") {\n return json(\n config.auth\n ? getPreviewAuthPublicConfig(config.auth)\n : {\n enabled: false,\n controlAuth: \"bearer\",\n defaultSessionTtlMs: config.sessionTtlMs,\n maxSessionTtlMs: config.sessionTtlMs,\n },\n );\n }\n\n if (request.method === \"POST\" && url.pathname === \"/api/auth/exchange\") {\n if (!config.auth) return text(\"Managed preview authentication is not configured.\", 404);\n const rateLimitResponse = await limitPreviewAccountExchange(request, config.auth);\n if (rateLimitResponse) return rateLimitResponse;\n return await exchangePreviewAccount(request, config.auth);\n }\n\n if (request.method === \"POST\" && url.pathname === \"/api/tunnel/grants\") {\n if (!config.auth) return text(\"Managed preview authentication is not configured.\", 404);\n return await createTunnelGrant(request, config.auth);\n }\n\n if (request.method === \"POST\" && url.pathname === \"/api/sessions\") {\n return await createSession(request, store, config);\n }\n\n const sessionRoute = matchSessionRoute(url.pathname);\n if (sessionRoute) {\n return await handleSessionRoute(request, url, store, config, sessionRoute);\n }\n\n const publicRoute = resolvePublicPreviewRoute(request, url, config.domain);\n if (publicRoute) {\n return await proxyPublicRequest(request, url, store, config, publicRoute);\n }\n\n return json(\n {\n ok: true,\n message: \"Farm Preview Gateway\",\n createSession: \"/api/sessions\",\n health: \"/api/health\",\n },\n 200,\n );\n } catch (error) {\n if (error instanceof GatewayHttpError || error instanceof PreviewAuthError) {\n return text(error.message, error.status);\n }\n return text(\n error instanceof Error ? error.message : \"Unexpected preview gateway error.\",\n 500,\n );\n }\n };\n\n // The persistent relay serves the same public hostnames from a separate name\n // namespace, and its route wins over this handler, so it has to be able to\n // ask whether a live polling session already owns a name before accepting a\n // claim for it.\n return Object.assign(handlePreviewGatewayRequest, {\n async isPreviewNameClaimed(name: string) {\n const claimed = sanitizePreviewName(name);\n if (!claimed) return false;\n const session = await store.getSessionByName(claimed);\n return Boolean(session && isPreviewClientOnline(session, config));\n },\n });\n}\n\nasync function limitPreviewAccountExchange(request: Request, auth: PreviewManagedAuthOptions) {\n if (!auth.rateLimitExchange) return undefined;\n let result: PreviewAuthExchangeRateLimitResult;\n try {\n result = await auth.rateLimitExchange(request);\n } catch {\n return new Response(\"Farm Preview login is temporarily unavailable. Try again shortly.\", {\n status: 503,\n headers: {\n \"cache-control\": \"no-store\",\n \"content-type\": \"text/plain; charset=utf-8\",\n \"retry-after\": \"1\",\n },\n });\n }\n if (result.allowed) return undefined;\n\n const retryAfterMs =\n Number.isFinite(result.retryAfterMs) && (result.retryAfterMs ?? 0) > 0\n ? result.retryAfterMs!\n : 1000;\n return new Response(\"Too many Farm Preview login attempts. Try again shortly.\", {\n status: 429,\n headers: {\n \"cache-control\": \"no-store\",\n \"content-type\": \"text/plain; charset=utf-8\",\n \"retry-after\": String(Math.max(1, Math.ceil(retryAfterMs / 1000))),\n },\n });\n}\n\nasync function exchangePreviewAccount(request: Request, auth: PreviewManagedAuthOptions) {\n const input = (await request.json().catch(() => ({}))) as {\n provider?: string;\n accessToken?: string;\n };\n if (input.provider !== \"github\") {\n throw new PreviewAuthError(400, \"Farm Preview currently supports GitHub sign-in.\");\n }\n return json(await exchangeGitHubPreviewToken(input.accessToken || \"\", auth));\n}\n\nasync function createTunnelGrant(request: Request, auth: PreviewManagedAuthOptions) {\n const accountToken = readPreviewBearerToken(request);\n if (!accountToken) {\n throw new PreviewAuthError(401, \"Sign in with Farm Preview before creating a tunnel grant.\");\n }\n const account = verifyPreviewAccountToken(accountToken, auth);\n const input = (await request.json().catch(() => ({}))) as {\n name?: string;\n expiresInMs?: number;\n };\n const name = sanitizePreviewName(input.name);\n if (!name) throw new PreviewAuthError(400, \"A valid preview name is required.\");\n\n return json(\n issuePreviewTunnelGrant(\n account,\n {\n name,\n ...(input.expiresInMs !== undefined ? { expiresInMs: input.expiresInMs } : {}),\n },\n auth,\n ),\n );\n}\n\nexport function createNodePreviewGatewayHandler(\n options: PreviewGatewayOptions = {},\n): NodePreviewGatewayHandler {\n const handler = createPreviewGatewayHandler(options);\n\n const nodePreviewGatewayHandler = async function nodePreviewGatewayHandler(\n req: IncomingMessage,\n res: ServerResponse,\n ) {\n const controller = new AbortController();\n const abort = () => controller.abort();\n const abortOnClose = () => {\n if (!res.writableEnded) abort();\n };\n req.once(\"aborted\", abort);\n res.once(\"close\", abortOnClose);\n try {\n const response = await handler(nodeRequestToWebRequest(req, controller.signal));\n if (controller.signal.aborted || res.destroyed) {\n await response.body?.cancel();\n return;\n }\n res.statusCode = response.status;\n // Headers.forEach folds repeated headers into one comma-joined value, which\n // corrupts multiple Set-Cookie. Emit those separately as an array so each\n // cookie becomes its own header line.\n const setCookies = response.headers.getSetCookie?.() ?? [];\n response.headers.forEach((value, key) => {\n if (key.toLowerCase() === \"set-cookie\") return;\n res.setHeader(key, value);\n });\n if (setCookies.length > 0) {\n res.setHeader(\"set-cookie\", setCookies);\n }\n res.end(Buffer.from(await response.arrayBuffer()));\n } finally {\n req.off(\"aborted\", abort);\n res.off(\"close\", abortOnClose);\n }\n };\n\n return Object.assign(nodePreviewGatewayHandler, {\n isPreviewNameClaimed: handler.isPreviewNameClaimed,\n });\n}\n\nexport function createPreviewGatewayStoreFromEnv(): PreviewGatewayStore {\n const redisUrl = process.env.UPSTASH_REDIS_REST_URL || process.env.KV_REST_API_URL;\n const redisToken = process.env.UPSTASH_REDIS_REST_TOKEN || process.env.KV_REST_API_TOKEN;\n\n if (redisUrl && redisToken) {\n return new RedisRestPreviewGatewayStore({\n url: redisUrl,\n token: redisToken,\n });\n }\n\n return new MemoryPreviewGatewayStore();\n}\n\nexport class MemoryPreviewGatewayStore implements PreviewGatewayStore {\n private sessions = new Map<string, PreviewGatewaySession>();\n private names = new Map<string, string>();\n private queues = new Map<string, PreviewGatewayRequest[]>();\n private responses = new Map<string, PreviewGatewayResponse>();\n\n async createSession(session: PreviewGatewaySession): Promise<void> {\n this.sessions.set(session.id, session);\n this.names.set(session.name, session.id);\n this.queues.set(session.id, []);\n }\n\n async getSessionByName(name: string): Promise<PreviewGatewaySession | undefined> {\n const id = this.names.get(name);\n return id ? this.getSessionById(id) : undefined;\n }\n\n async getSessionById(id: string): Promise<PreviewGatewaySession | undefined> {\n const session = this.sessions.get(id);\n if (!session) return undefined;\n if (session.expiresAt <= Date.now()) {\n await this.deleteSession(session);\n return undefined;\n }\n return session;\n }\n\n async touchSession(session: PreviewGatewaySession, _ttlMs: number): Promise<void> {\n // A concurrent delete (the preview was stopped) must win: a poll handler\n // that outlives the DELETE must never resurrect a session that is no\n // longer stored, or its name would look \"online\" and block a restart.\n if (!this.sessions.has(session.id)) return;\n this.sessions.set(session.id, session);\n const nameOwner = this.names.get(session.name);\n if (!nameOwner || nameOwner === session.id) {\n this.names.set(session.name, session.id);\n }\n }\n\n async enqueueRequest(sessionId: string, request: PreviewGatewayRequest): Promise<void> {\n const queue = this.queues.get(sessionId) || [];\n queue.push(request);\n this.queues.set(sessionId, queue);\n }\n\n async takeRequests(sessionId: string, limit: number): Promise<PreviewGatewayRequest[]> {\n const queue = this.queues.get(sessionId) || [];\n return queue.splice(0, limit);\n }\n\n async saveResponse(\n sessionId: string,\n requestId: string,\n response: PreviewGatewayResponse,\n ): Promise<void> {\n this.responses.set(responseKey(sessionId, requestId), response);\n }\n\n async getResponse(\n sessionId: string,\n requestId: string,\n ): Promise<PreviewGatewayResponse | undefined> {\n return this.responses.get(responseKey(sessionId, requestId));\n }\n\n async deleteResponse(sessionId: string, requestId: string): Promise<void> {\n this.responses.delete(responseKey(sessionId, requestId));\n }\n\n async deleteSession(session: PreviewGatewaySession): Promise<void> {\n this.sessions.delete(session.id);\n // The name may have been taken over by a newer session; only remove the\n // mapping if it still points at the session being deleted.\n if (this.names.get(session.name) === session.id) {\n this.names.delete(session.name);\n }\n this.queues.delete(session.id);\n }\n}\n\nexport class RedisRestPreviewGatewayStore implements PreviewGatewayStore {\n constructor(private options: { url: string; token: string; prefix?: string }) {}\n\n async createSession(session: PreviewGatewaySession, ttlMs: number): Promise<void> {\n await this.command(\"SET\", this.sessionKey(session.id), JSON.stringify(session), \"PX\", ttlMs);\n await this.command(\"SET\", this.nameKey(session.name), session.id, \"PX\", ttlMs);\n await this.command(\"DEL\", this.queueKey(session.id));\n }\n\n async getSessionByName(name: string): Promise<PreviewGatewaySession | undefined> {\n const id = await this.command<string | null>(\"GET\", this.nameKey(name));\n return id ? this.getSessionById(id) : undefined;\n }\n\n async getSessionById(id: string): Promise<PreviewGatewaySession | undefined> {\n const value = await this.command<string | null>(\"GET\", this.sessionKey(id));\n return value ? JSON.parse(value) : undefined;\n }\n\n async touchSession(session: PreviewGatewaySession, ttlMs: number): Promise<void> {\n const nextSession = { ...session };\n const remainingTtlMs = Math.max(1, Math.min(ttlMs, session.expiresAt - Date.now()));\n // XX only refreshes a session key that still exists. A poll handler that\n // outlives a DELETE must not recreate the session (nor its name mapping),\n // which would otherwise block reclaiming the name on restart.\n const result = await this.command<string | null>(\n \"SET\",\n this.sessionKey(session.id),\n JSON.stringify(nextSession),\n \"PX\",\n remainingTtlMs,\n \"XX\",\n );\n if (result === null) return;\n const nameOwner = await this.command<string | null>(\"GET\", this.nameKey(session.name));\n if (!nameOwner || nameOwner === session.id) {\n await this.command(\"SET\", this.nameKey(session.name), session.id, \"PX\", remainingTtlMs);\n }\n await this.command(\"EXPIRE\", this.queueKey(session.id), Math.ceil(remainingTtlMs / 1000));\n }\n\n async enqueueRequest(\n sessionId: string,\n request: PreviewGatewayRequest,\n ttlMs: number,\n ): Promise<void> {\n await this.command(\"RPUSH\", this.queueKey(sessionId), JSON.stringify(request));\n await this.command(\"EXPIRE\", this.queueKey(sessionId), Math.ceil(ttlMs / 1000));\n }\n\n async takeRequests(sessionId: string, limit: number): Promise<PreviewGatewayRequest[]> {\n const values = await this.command<string[] | string | null>(\n \"LPOP\",\n this.queueKey(sessionId),\n limit,\n );\n if (!values) return [];\n const list = Array.isArray(values) ? values : [values];\n return list.map((value) => JSON.parse(value));\n }\n\n async saveResponse(\n sessionId: string,\n requestId: string,\n response: PreviewGatewayResponse,\n ttlMs: number,\n ): Promise<void> {\n await this.command(\n \"SET\",\n this.responseKey(sessionId, requestId),\n JSON.stringify(response),\n \"PX\",\n ttlMs,\n );\n }\n\n async getResponse(\n sessionId: string,\n requestId: string,\n ): Promise<PreviewGatewayResponse | undefined> {\n const value = await this.command<string | null>(\"GET\", this.responseKey(sessionId, requestId));\n return value ? JSON.parse(value) : undefined;\n }\n\n async deleteResponse(sessionId: string, requestId: string): Promise<void> {\n await this.command(\"DEL\", this.responseKey(sessionId, requestId));\n }\n\n async deleteSession(session: PreviewGatewaySession): Promise<void> {\n const keys = [this.sessionKey(session.id), this.queueKey(session.id)];\n // The name may have been taken over by a newer session; only remove the\n // mapping if it still points at the session being deleted.\n const nameOwner = await this.command<string | null>(\"GET\", this.nameKey(session.name));\n if (nameOwner === session.id) {\n keys.push(this.nameKey(session.name));\n }\n await this.command(\"DEL\", ...keys);\n }\n\n private async command<T = unknown>(...args: Array<string | number>) {\n const response = await fetch(this.options.url, {\n method: \"POST\",\n headers: {\n authorization: `Bearer ${this.options.token}`,\n \"content-type\": \"application/json\",\n },\n body: JSON.stringify(args),\n });\n\n if (!response.ok) {\n throw new Error(\n `Preview gateway Redis command failed (${response.status}): ${await response.text()}`,\n );\n }\n\n const data = (await response.json()) as { result?: T; error?: string };\n if (data.error) {\n throw new Error(`Preview gateway Redis command failed: ${data.error}`);\n }\n return data.result as T;\n }\n\n private key(value: string) {\n return `${this.options.prefix || \"farm-preview\"}:${value}`;\n }\n\n private sessionKey(id: string) {\n return this.key(`session:${id}`);\n }\n\n private nameKey(name: string) {\n return this.key(`name:${name}`);\n }\n\n private queueKey(id: string) {\n return this.key(`queue:${id}`);\n }\n\n private responseKey(sessionId: string, requestId: string) {\n return this.key(`response:${sessionId}:${requestId}`);\n }\n}\n\nasync function createSession(\n request: Request,\n store: PreviewGatewayStore,\n config: PreviewGatewayRuntimeConfig,\n) {\n const input = (await request.json().catch(() => ({}))) as {\n name?: string;\n localUrl?: string;\n expiresInMs?: number;\n };\n const requestedName = sanitizePreviewName(input.name);\n let name = requestedName || randomPreviewName();\n let expiresAt = Date.now() + config.sessionTtlMs;\n let slidingExpiration = true;\n\n if (config.auth) {\n const grantToken = readPreviewBearerToken(request);\n if (!grantToken) {\n throw new GatewayHttpError(401, \"Sign in with Farm Preview before creating a session.\");\n }\n try {\n const grant = verifyPreviewTunnelGrant(grantToken, {\n signingSecret: config.auth.signingSecret,\n ...(requestedName ? { name: requestedName } : {}),\n });\n name = grant.name;\n expiresAt = grant.expiresAt;\n slidingExpiration = false;\n } catch (error) {\n if (error instanceof PreviewAuthError)\n throw new GatewayHttpError(error.status, error.message);\n throw error;\n }\n } else if (Number.isFinite(input.expiresInMs)) {\n expiresAt = Date.now() + clamp(Number(input.expiresInMs), 60_000, config.sessionTtlMs);\n slidingExpiration = false;\n }\n\n // A name that is still owned by a live session cannot be claimed by a new\n // one — otherwise any caller could repoint an active preview's public URL\n // to their own session. Stale sessions (missed heartbeats) may be taken\n // over, matching how public routing treats them.\n for (let attempt = 0; ; attempt++) {\n const existing = await store.getSessionByName(name);\n if (!existing || !isPreviewClientOnline(existing, config)) {\n break;\n }\n if (requestedName || config.auth) {\n return text(\n `A Farm preview named \"${name}\" is already active. Choose another name or stop the running preview.`,\n 409,\n );\n }\n if (attempt >= 4) {\n return text(\"Could not allocate a unique preview name. Try again.\", 503);\n }\n name = randomPreviewName();\n }\n\n const hostname = `${name}.${config.domain}`;\n const session: PreviewGatewaySession = {\n id: randomId(\"sess\"),\n name,\n hostname,\n publicUrl: createPublicUrl(request, config, name),\n token: randomId(\"token\"),\n localUrl: input.localUrl,\n createdAt: Date.now(),\n expiresAt,\n ...(slidingExpiration ? { slidingExpiration: true as const } : {}),\n lastHeartbeatAt: Date.now(),\n };\n\n await store.createSession(session, Math.max(1, expiresAt - Date.now()));\n\n return json({\n id: session.id,\n name: session.name,\n token: session.token,\n publicUrl: session.publicUrl,\n ...(session.slidingExpiration ? {} : { expiresAt: session.expiresAt }),\n });\n}\n\nasync function handleSessionRoute(\n request: Request,\n url: URL,\n store: PreviewGatewayStore,\n config: PreviewGatewayRuntimeConfig,\n route: { sessionId: string; action?: string; requestId?: string },\n) {\n const session = await requireSession(\n store,\n route.sessionId,\n readPreviewBearerToken(request) || url.searchParams.get(\"token\"),\n );\n\n if (request.method === \"GET\" && route.action === \"requests\") {\n return await pollSessionRequests(url, store, config, session);\n }\n\n if (request.method === \"POST\" && route.action === \"responses\" && route.requestId) {\n let response: PreviewGatewayResponse;\n try {\n response = await parsePreviewResponse(request, config.maxResponseBodyBytes);\n } catch (error) {\n if (error instanceof GatewayHttpError && error.status === 413) {\n await store.saveResponse(\n session.id,\n route.requestId,\n createOversizedPreviewResponse(config.maxResponseBodyBytes),\n remainingSessionTtl(session, config),\n );\n }\n throw error;\n }\n await store.saveResponse(\n session.id,\n route.requestId,\n response,\n remainingSessionTtl(session, config),\n );\n await markSessionOnline(store, config, session);\n return json({ ok: true });\n }\n\n if (request.method === \"POST\" && route.action === \"heartbeat\") {\n await markSessionOnline(store, config, session);\n return json({\n ok: true,\n ...(session.slidingExpiration ? {} : { expiresAt: session.expiresAt }),\n });\n }\n\n if (request.method === \"DELETE\" && !route.action) {\n await store.deleteSession(session);\n return json({ ok: true });\n }\n\n return text(\"Preview gateway route not found.\", 404);\n}\n\nasync function parsePreviewResponse(request: Request, maxBodyBytes: number) {\n const maxEncodedBytes = Math.ceil(maxBodyBytes / 3) * 4 + 64 * 1024;\n const contentLength = Number(request.headers.get(\"content-length\") || 0);\n if (Number.isFinite(contentLength) && contentLength > maxEncodedBytes) {\n await request.body?.cancel();\n throw new GatewayHttpError(413, \"Preview response body is too large.\");\n }\n\n const bytes = await readBodyWithLimit(\n request,\n maxEncodedBytes,\n \"Preview response body is too large.\",\n );\n let response: PreviewGatewayResponse;\n try {\n response = JSON.parse(bytes.toString(\"utf8\")) as PreviewGatewayResponse;\n } catch {\n throw new GatewayHttpError(400, \"Preview response body must be valid JSON.\");\n }\n\n if (getPreviewResponseBodySize(response) > maxBodyBytes) {\n throw new GatewayHttpError(413, \"Preview response body is too large.\");\n }\n return response;\n}\n\nfunction getPreviewResponseBodySize(response: PreviewGatewayResponse) {\n if (!response.body) return 0;\n return Buffer.byteLength(response.body, response.encoding === \"base64\" ? \"base64\" : \"utf8\");\n}\n\nfunction createOversizedPreviewResponse(maxBodyBytes: number): PreviewGatewayResponse {\n return {\n status: 502,\n headers: { \"content-type\": \"text/plain; charset=utf-8\" },\n body: Buffer.from(\n `The local preview response exceeded the ${maxBodyBytes} byte limit.`,\n ).toString(\"base64\"),\n encoding: \"base64\",\n };\n}\n\nasync function pollSessionRequests(\n url: URL,\n store: PreviewGatewayStore,\n config: PreviewGatewayRuntimeConfig,\n session: PreviewGatewaySession,\n) {\n const waitMs = clamp(Number(url.searchParams.get(\"wait\") || config.pollTimeoutMs), 1000, 25000);\n const deadline = Date.now() + waitMs;\n\n await markSessionOnline(store, config, session);\n\n while (Date.now() < deadline) {\n const requests = await store.takeRequests(session.id, DEFAULT_POLL_REQUEST_LIMIT);\n if (requests.length) {\n await markSessionOnline(store, config, session);\n return json({ requests });\n }\n await delay(config.pollIntervalMs);\n }\n\n await markSessionOnline(store, config, session);\n return new Response(null, { status: 204 });\n}\n\nasync function proxyPublicRequest(\n request: Request,\n url: URL,\n store: PreviewGatewayStore,\n config: PreviewGatewayRuntimeConfig,\n route: { name: string; path: string },\n) {\n const session = await store.getSessionByName(route.name);\n if (!session) {\n return inactivePreviewResponse(request, route.name);\n }\n if (!isPreviewClientOnline(session, config)) {\n await store.deleteSession(session);\n return inactivePreviewResponse(request, route.name);\n }\n\n const previewRequest = await serializePreviewRequest(\n request,\n url,\n route.path,\n config.maxBodyBytes,\n );\n await store.enqueueRequest(session.id, previewRequest, remainingSessionTtl(session, config));\n await store.touchSession(session, remainingSessionTtl(session, config));\n\n const response = await waitForPreviewResponse(\n store,\n config,\n session,\n previewRequest.id,\n request.signal,\n );\n if (response === \"cancelled\") {\n await store.enqueueRequest(\n session.id,\n {\n ...previewRequest,\n body: undefined,\n encoding: undefined,\n cancelled: true,\n createdAt: Date.now(),\n },\n remainingSessionTtl(session, config),\n );\n return new Response(null, { status: 499 });\n }\n if (response === \"stale\") {\n return inactivePreviewResponse(request, route.name);\n }\n if (!response) {\n return text(\"The local Farm preview did not respond before the gateway timed out.\", 504);\n }\n\n const headers = new Headers();\n const responseHopByHopHeaders = getHopByHopHeaderNames(response.headers || {});\n for (const [key, value] of Object.entries(response.headers || {})) {\n const normalized = key.toLowerCase();\n if (responseHopByHopHeaders.has(normalized) || normalized === \"content-encoding\") {\n continue;\n }\n // Append multi-valued headers (Set-Cookie) so every value reaches the\n // visitor; Headers.set would keep only the last.\n for (const single of Array.isArray(value) ? value : [value]) {\n headers.append(key, single);\n }\n }\n headers.set(\"cache-control\", \"no-store\");\n headers.set(\"x-farm-preview\", session.name);\n\n return new Response(\n response.body\n ? Buffer.from(response.body, response.encoding === \"base64\" ? \"base64\" : \"utf8\")\n : null,\n {\n status: response.status || 200,\n headers,\n },\n );\n}\n\nasync function waitForPreviewResponse(\n store: PreviewGatewayStore,\n config: PreviewGatewayRuntimeConfig,\n session: PreviewGatewaySession,\n requestId: string,\n signal: AbortSignal,\n) {\n const deadline = Date.now() + config.requestTimeoutMs;\n let nextLivenessCheckAt = 0;\n\n while (Date.now() < deadline) {\n if (signal.aborted) return \"cancelled\";\n if (Date.now() >= nextLivenessCheckAt) {\n const latestSession = await store.getSessionById(session.id);\n if (!latestSession || !isPreviewClientOnline(latestSession, config)) {\n if (latestSession) await store.deleteSession(latestSession);\n return \"stale\";\n }\n nextLivenessCheckAt = Date.now() + 1000;\n }\n\n const response = await store.getResponse(session.id, requestId);\n if (response) {\n await store.deleteResponse(session.id, requestId);\n return response;\n }\n try {\n await delay(config.pollIntervalMs, undefined, { signal });\n } catch {\n if (signal.aborted) return \"cancelled\";\n throw new Error(\"Preview response polling was interrupted.\");\n }\n }\n\n return undefined;\n}\n\nasync function markSessionOnline(\n store: PreviewGatewayStore,\n config: PreviewGatewayRuntimeConfig,\n session: PreviewGatewaySession,\n) {\n const now = Date.now();\n session.lastHeartbeatAt = now;\n if (session.slidingExpiration) {\n session.expiresAt = now + config.sessionTtlMs;\n }\n await store.touchSession(session, remainingSessionTtl(session, config));\n}\n\nfunction remainingSessionTtl(session: PreviewGatewaySession, config: PreviewGatewayRuntimeConfig) {\n return Math.max(1, Math.min(config.sessionTtlMs, session.expiresAt - Date.now()));\n}\n\nfunction isPreviewClientOnline(\n session: PreviewGatewaySession,\n config: PreviewGatewayRuntimeConfig,\n) {\n const lastHeartbeatAt = session.lastHeartbeatAt || session.createdAt;\n return (\n session.expiresAt > Date.now() &&\n Date.now() - lastHeartbeatAt <= config.clientHeartbeatTimeoutMs\n );\n}\n\nasync function serializePreviewRequest(\n request: Request,\n url: URL,\n routePath: string,\n maxBodyBytes: number,\n): Promise<PreviewGatewayRequest> {\n const method = request.method.toUpperCase();\n const headers: Record<string, string> = {};\n const requestHopByHopHeaders = getHopByHopHeaderNames(request.headers);\n request.headers.forEach((value, key) => {\n const normalized = key.toLowerCase();\n if (\n !requestHopByHopHeaders.has(normalized) &&\n normalized !== \"forwarded\" &&\n !normalized.startsWith(\"x-forwarded-\")\n ) {\n headers[key] = value;\n }\n });\n\n headers[\"x-forwarded-host\"] = request.headers.get(\"host\") || url.host;\n headers[\"x-forwarded-proto\"] = url.protocol.replace(\":\", \"\");\n\n let body: string | undefined;\n if (method !== \"GET\" && method !== \"HEAD\") {\n const contentLength = Number(request.headers.get(\"content-length\") || 0);\n if (contentLength > maxBodyBytes) {\n throw new GatewayHttpError(413, \"Preview request body is too large.\");\n }\n\n const buffer = await readBodyWithLimit(\n request,\n maxBodyBytes,\n \"Preview request body is too large.\",\n );\n body = buffer.toString(\"base64\");\n }\n\n return {\n id: randomId(\"req\"),\n method,\n path: `${routePath}${url.search}`,\n headers,\n body,\n encoding: body ? \"base64\" : undefined,\n createdAt: Date.now(),\n };\n}\n\nasync function readBodyWithLimit(request: Request, maxBytes: number, message: string) {\n if (!request.body) return Buffer.alloc(0);\n const reader = request.body.getReader();\n const chunks: Uint8Array[] = [];\n let size = 0;\n try {\n while (true) {\n const { done, value } = await reader.read();\n if (done) break;\n size += value.byteLength;\n if (size > maxBytes) {\n await reader.cancel();\n throw new GatewayHttpError(413, message);\n }\n chunks.push(value);\n }\n } catch (error) {\n await reader.cancel().catch(() => undefined);\n throw error;\n }\n return Buffer.concat(chunks, size);\n}\n\nasync function requireSession(store: PreviewGatewayStore, sessionId: string, token: string | null) {\n const session = await store.getSessionById(sessionId);\n if (!session || !token || token !== session.token) {\n throw new GatewayHttpError(401, \"Invalid preview gateway session.\");\n }\n return session;\n}\n\nfunction resolvePublicPreviewRoute(request: Request, url: URL, domain: string) {\n const pathMatch = url.pathname.match(/^\\/__preview\\/([^/]+)(\\/.*)?$/);\n if (pathMatch) {\n return {\n name: pathMatch[1],\n path: pathMatch[2] || \"/\",\n };\n }\n\n const host = (request.headers.get(\"host\") || url.host).split(\":\")[0];\n if (!host || host === domain || !host.endsWith(`.${domain}`)) {\n return undefined;\n }\n\n return {\n name: host.slice(0, -(domain.length + 1)),\n path: url.pathname || \"/\",\n };\n}\n\nfunction matchSessionRoute(pathname: string) {\n const match = pathname.match(\n /^\\/api\\/sessions\\/([^/]+)(?:\\/(requests|heartbeat|responses)(?:\\/([^/]+))?)?$/,\n );\n if (!match) return undefined;\n return {\n sessionId: match[1],\n action: match[2],\n requestId: match[3],\n };\n}\n\nfunction nodeRequestToWebRequest(req: IncomingMessage, signal?: AbortSignal) {\n const host = headerValue(req.headers, \"host\") || \"localhost\";\n // This adapter is the public trust boundary. A visitor-controlled forwarded\n // protocol must not influence the authority serialized to the local app.\n const protocol = isLocalHost(host.split(\":\")[0]) ? \"http\" : \"https\";\n const url = `${protocol}://${host}${req.url || \"/\"}`;\n const headers = nodeHeadersToWebHeaders(req.headers);\n const method = req.method || \"GET\";\n const init: RequestInit & { duplex?: \"half\" } = {\n method,\n headers,\n signal,\n };\n\n if (method !== \"GET\" && method !== \"HEAD\") {\n init.body = req as unknown as BodyInit;\n init.duplex = \"half\";\n }\n\n return new Request(url, init);\n}\n\nfunction nodeHeadersToWebHeaders(headers: IncomingHttpHeaders) {\n const next = new Headers();\n for (const [key, value] of Object.entries(headers)) {\n if (value === undefined) continue;\n if (Array.isArray(value)) {\n for (const item of value) next.append(key, item);\n } else {\n next.set(key, value);\n }\n }\n return next;\n}\n\nfunction headerValue(headers: IncomingHttpHeaders, key: string) {\n const value = headers[key];\n return Array.isArray(value) ? value[0] : value;\n}\n\nfunction createPublicUrl(request: Request, config: PreviewGatewayRuntimeConfig, name: string) {\n if (config.baseUrl) {\n const base = new URL(config.baseUrl);\n if (isLocalHost(base.hostname)) {\n return `${config.baseUrl.replace(/\\/+$/, \"\")}/__preview/${name}`;\n }\n }\n\n const host = request.headers.get(\"host\") || new URL(request.url).host;\n if (isLocalHost(host.split(\":\")[0])) {\n return `${new URL(request.url).origin}/__preview/${name}`;\n }\n\n return `https://${name}.${config.domain}`;\n}\n\nfunction json(value: unknown, status = 200) {\n return new Response(JSON.stringify(value), {\n status,\n headers: {\n \"content-type\": \"application/json\",\n \"cache-control\": \"no-store\",\n },\n });\n}\n\nfunction text(value: string, status = 200) {\n return new Response(value, {\n status,\n headers: {\n \"content-type\": \"text/plain; charset=utf-8\",\n \"cache-control\": \"no-store\",\n },\n });\n}\n\nfunction inactivePreviewResponse(request: Request, name: string) {\n if (!request.headers.get(\"accept\")?.includes(\"text/html\")) {\n return text(`No active Farm preview is running for \"${name}\".`, 404);\n }\n const safeName = name.replace(/[&<>\"']/g, (character) => {\n if (character === \"&\") return \"&\";\n if (character === \"<\") return \"<\";\n if (character === \">\") return \">\";\n if (character === '\"') return \""\";\n return \"'\";\n });\n return new Response(\n `<!doctype html><html lang=\"en\"><meta charset=\"utf-8\"><meta name=\"viewport\" content=\"width=device-width,initial-scale=1\"><title>Farm preview expired</title><style>body{font:16px/1.5 system-ui,sans-serif;display:grid;min-height:100vh;margin:0;place-items:center;background:#f7f7f5;color:#171714}main{max-width:34rem;padding:2rem;text-align:center}h1{font-size:1.75rem}code{background:#e9e9e4;border-radius:.35rem;padding:.15rem .35rem}</style><main><h1>This Farm preview has expired</h1><p>The temporary preview <code>${safeName}</code> is no longer connected.</p><p>Ask its owner to run <code>farm preview</code> again.</p></main></html>`,\n {\n status: 410,\n headers: {\n \"content-type\": \"text/html; charset=utf-8\",\n \"cache-control\": \"no-store\",\n },\n },\n );\n}\n\nfunction sanitizePreviewName(value: string | undefined) {\n if (!value) return undefined;\n return value\n .toLowerCase()\n .replace(/[^a-z0-9-]+/g, \"-\")\n .replace(/^-+|-+$/g, \"\")\n .slice(0, 48);\n}\n\nfunction randomPreviewName() {\n return `farm-${Math.random().toString(36).slice(2, 8)}`;\n}\n\nfunction randomId(prefix: string) {\n return `${prefix}_${crypto.randomUUID().replace(/-/g, \"\")}`;\n}\n\nfunction normalizeDomain(value: string) {\n return value\n .replace(/^https?:\\/\\//, \"\")\n .replace(/^\\.*/, \"\")\n .replace(/\\/*$/, \"\");\n}\n\nfunction responseKey(sessionId: string, requestId: string) {\n return `${sessionId}:${requestId}`;\n}\n\nfunction clamp(value: number, min: number, max: number) {\n if (!Number.isFinite(value)) return min;\n return Math.max(min, Math.min(max, value));\n}\n\nfunction isLocalHost(hostname: string) {\n return hostname === \"localhost\" || hostname === \"127.0.0.1\" || hostname === \"::1\";\n}\n\nclass GatewayHttpError extends Error {\n constructor(\n public status: number,\n message: string,\n ) {\n super(message);\n }\n}\n"],"mappings":";;;AA2DA,MAAM,eAAe;AACrB,MAAM,+BAA+B;AACrC,MAAMA,2BAAyB;AAC/B,MAAM,6BAA6B;AACnC,MAAM,qBAAqB;AAC3B,MAAM,oBAAoB;AAE1B,SAAgB,2BAA2B,SAAoC;CAC7E,2BAA2B,OAAO;CAClC,MAAM,YAAY,wBAAwB,OAAO;CACjD,OAAO;EACL,SAAS;EACT,aAAa;EACb,UAAU;EACV,UAAU,QAAQ;EAClB,OAAO;EACP,qBAAqB,UAAU;EAC/B,iBAAiB,UAAU;CAC7B;AACF;AAEA,eAAsB,2BACpB,eACA,SAC6E;CAC7E,2BAA2B,OAAO;CAClC,IAAI,CAAC,iBAAiB,cAAc,SAAS,MAC3C,MAAM,IAAI,iBAAiB,KAAK,oCAAoC;CAItE,MAAM,WAAW,OADD,QAAQ,SAAS,MAAA,CACF,GAAG,QAAQ,gBAAgB,yBAAyB,QAAQ;EACzF,SAAS;GACP,QAAQ;GACR,eAAe,UAAU;GACzB,cAAc;G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AK,KAAK,IAAI,KAAK,KAAK,CAAC;AAC3C;AAEA,SAAS,YAAY,UAAkB;CACrC,OAAO,aAAa,eAAe,aAAa,eAAe,aAAa;AAC9E;AAEA,IAAM,mBAAN,cAA+B,MAAM;CACnC,YACE,QACA,SACA;EACA,MAAM,OAAO;EAHN,KAAA,SAAA;CAIT;AACF"}
|
|
1
|
+
{"version":3,"file":"index.js","names":["DEFAULT_SESSION_TTL_MS","delay"],"sources":["../src/auth.ts","../src/index.ts"],"sourcesContent":["import { createHmac, randomUUID, timingSafeEqual } from \"node:crypto\";\n\nexport interface PreviewManagedAuthOptions {\n /** Secret used to sign account tokens and one-preview tunnel grants. */\n signingSecret: string;\n /** Public client id for the Farming Labs GitHub OAuth app. */\n githubClientId?: string;\n /** First-party device login. Omit to preserve GitHub account authentication. */\n deviceAuth?: {\n issuer: string;\n clientId: string;\n /** Resolve an API key or approved device credential on every new grant. */\n authorizeAccount(token: string): Promise<{\n subject: string;\n login: string;\n expiresAt?: number;\n keyId?: string;\n project?: string;\n } | null>;\n };\n /** Public console URL. Must not contain credentials or query parameters. */\n dashboardUrl?: string;\n accountTokenTtlMs?: number;\n defaultSessionTtlMs?: number;\n maxSessionTtlMs?: number;\n githubApiUrl?: string;\n fetch?: typeof fetch;\n /**\n * Shared abuse-control hook for provider-token exchange and, in device mode,\n * account credential verification on the tunnel-grant endpoint.\n * Implementations must not consume the request body. Failures reject the\n * exchange with a retryable 503 response instead of bypassing the limiter.\n */\n rateLimitExchange?: PreviewAuthExchangeRateLimiter;\n}\n\nexport interface PreviewAuthExchangeRateLimitResult {\n allowed: boolean;\n /** How long the caller should wait before retrying a rejected exchange. */\n retryAfterMs?: number;\n}\n\nexport type PreviewAuthExchangeRateLimiter = (\n request: Request,\n) => PreviewAuthExchangeRateLimitResult | Promise<PreviewAuthExchangeRateLimitResult>;\n\nexport interface PreviewAccountIdentity {\n /** Optional credential restriction; never taken from the grant request. */\n project?: string;\n /** Non-secret identifier supplied by the credential verifier, never the CLI. */\n keyId?: string;\n provider: \"github\" | \"device\";\n subject: string;\n login: string;\n name?: string;\n avatarUrl?: string;\n}\n\nexport interface PreviewAccountClaims extends PreviewAccountIdentity {\n kind: \"account\";\n issuedAt: number;\n expiresAt: number;\n}\n\nexport interface PreviewTunnelGrantClaims {\n /** Unique signed grant identity, used to revoke reconnects across transports. */\n nonce?: string;\n project?: string;\n keyId?: string;\n kind: \"tunnel\";\n /** Absent on older GitHub grants. */\n provider?: \"github\" | \"device\";\n subject: string;\n login: string;\n name: string;\n issuedAt: number;\n expiresAt: number;\n}\n\nexport interface PreviewTunnelGrant {\n token: string;\n name: string;\n expiresAt: number;\n}\n\nconst TOKEN_PREFIX = \"farm_preview_v1\";\nconst DEFAULT_ACCOUNT_TOKEN_TTL_MS = 1000 * 60 * 60 * 24 * 7;\nconst DEFAULT_SESSION_TTL_MS = 1000 * 60 * 60;\nconst DEFAULT_MAX_SESSION_TTL_MS = 1000 * 60 * 60 * 24;\nconst MIN_SESSION_TTL_MS = 1000 * 60;\nconst MAX_CLOCK_SKEW_MS = 1000 * 60 * 5;\n\nexport function getPreviewAuthPublicConfig(options: PreviewManagedAuthOptions) {\n validatePreviewAuthOptions(options);\n const durations = resolvePreviewDurations(options);\n return {\n enabled: true as const,\n controlAuth: \"bearer\" as const,\n ...(options.deviceAuth\n ? {\n provider: \"device\" as const,\n clientId: options.deviceAuth.clientId,\n issuer: options.deviceAuth.issuer,\n }\n : { provider: \"github\" as const, clientId: options.githubClientId, scope: \"read:user\" }),\n ...(options.dashboardUrl ? { dashboardUrl: options.dashboardUrl } : {}),\n defaultSessionTtlMs: durations.defaultSessionTtlMs,\n maxSessionTtlMs: durations.maxSessionTtlMs,\n };\n}\n\nexport async function exchangeGitHubPreviewToken(\n providerToken: string,\n options: PreviewManagedAuthOptions,\n): Promise<{ token: string; expiresAt: number; user: PreviewAccountIdentity }> {\n validatePreviewAuthOptions(options);\n if (options.deviceAuth)\n throw new PreviewAuthError(400, \"Use the configured device sign-in flow.\");\n if (!providerToken || providerToken.length > 4096) {\n throw new PreviewAuthError(400, \"A GitHub access token is required.\");\n }\n\n const request = options.fetch || fetch;\n const response = await request(`${options.githubApiUrl || \"https://api.github.com\"}/user`, {\n headers: {\n accept: \"application/vnd.github+json\",\n authorization: `Bearer ${providerToken}`,\n \"user-agent\": \"farm-preview-gateway\",\n \"x-github-api-version\": \"2026-03-10\",\n },\n signal: AbortSignal.timeout(10_000),\n });\n if (!response.ok) {\n await response.body?.cancel();\n throw new PreviewAuthError(401, \"GitHub could not verify this Farm Preview login.\");\n }\n\n const profile = (await response.json()) as {\n id?: number;\n login?: string;\n name?: string | null;\n avatar_url?: string;\n };\n if (!Number.isSafeInteger(profile.id) || !profile.login) {\n throw new PreviewAuthError(502, \"GitHub returned an invalid user profile.\");\n }\n\n const now = Date.now();\n const expiresAt = now + (options.accountTokenTtlMs ?? DEFAULT_ACCOUNT_TOKEN_TTL_MS);\n const user: PreviewAccountIdentity = {\n provider: \"github\",\n subject: String(profile.id),\n login: profile.login,\n ...(profile.name ? { name: profile.name } : {}),\n ...(profile.avatar_url ? { avatarUrl: profile.avatar_url } : {}),\n };\n const claims: PreviewAccountClaims = {\n kind: \"account\",\n ...user,\n issuedAt: now,\n expiresAt,\n };\n\n return {\n token: signPreviewClaims(claims, options.signingSecret),\n expiresAt,\n user,\n };\n}\n\nexport function verifyPreviewAccountToken(\n token: string,\n options: Pick<PreviewManagedAuthOptions, \"signingSecret\">,\n): PreviewAccountClaims {\n const claims = verifyPreviewClaims(token, options.signingSecret);\n if (\n claims.kind !== \"account\" ||\n claims.provider !== \"github\" ||\n typeof claims.subject !== \"string\" ||\n typeof claims.login !== \"string\"\n ) {\n throw new PreviewAuthError(401, \"The Farm Preview account token is invalid.\");\n }\n return claims as unknown as PreviewAccountClaims;\n}\n\nexport function issuePreviewTunnelGrant(\n account: PreviewAccountClaims,\n input: { name: string; project?: string; expiresInMs?: number },\n options: PreviewManagedAuthOptions,\n): PreviewTunnelGrant {\n validatePreviewAuthOptions(options);\n const durations = resolvePreviewDurations(options);\n if (\n input.project !== undefined &&\n (typeof input.project !== \"string\" || !/^[a-z0-9][a-z0-9-]{0,62}$/.test(input.project))\n )\n throw new PreviewAuthError(400, \"Preview project must be a lowercase slug of 1–63 characters.\");\n if (\n input.expiresInMs !== undefined &&\n (!Number.isSafeInteger(input.expiresInMs) || input.expiresInMs <= 0)\n ) {\n throw new PreviewAuthError(400, \"Preview expiry must be a positive integer.\");\n }\n const requestedTtlMs = input.expiresInMs ?? durations.defaultSessionTtlMs;\n if (account.project !== undefined && account.project !== (input.project ?? input.name))\n throw new PreviewAuthError(403, \"This API key is restricted to another preview project.\");\n const ttlMs = Math.max(MIN_SESSION_TTL_MS, Math.min(durations.maxSessionTtlMs, requestedTtlMs));\n const issuedAt = Date.now();\n const expiresAt = Math.min(issuedAt + ttlMs, account.expiresAt);\n if (expiresAt <= issuedAt) throw new PreviewAuthError(401, \"The account credential has expired.\");\n const claims: PreviewTunnelGrantClaims & { nonce: string } = {\n kind: \"tunnel\",\n provider: account.provider,\n subject: account.subject,\n login: account.login,\n name: input.name,\n project: input.project ?? input.name,\n ...(account.keyId ? { keyId: account.keyId } : {}),\n issuedAt,\n expiresAt,\n nonce: randomUUID(),\n };\n return {\n token: signPreviewClaims(claims, options.signingSecret),\n name: input.name,\n expiresAt,\n };\n}\n\nexport function verifyPreviewTunnelGrant(\n token: string,\n options: Pick<PreviewManagedAuthOptions, \"signingSecret\"> & { name?: string },\n): PreviewTunnelGrantClaims {\n const claims = verifyPreviewClaims(token, options.signingSecret);\n if (\n claims.kind !== \"tunnel\" ||\n typeof claims.subject !== \"string\" ||\n typeof claims.login !== \"string\" ||\n typeof claims.name !== \"string\" ||\n (claims.nonce !== undefined &&\n (typeof claims.nonce !== \"string\" || !/^[\\w-]{1,128}$/.test(claims.nonce))) ||\n (claims.project !== undefined &&\n (typeof claims.project !== \"string\" || !/^[a-z0-9][a-z0-9-]{0,62}$/.test(claims.project))) ||\n (claims.keyId !== undefined &&\n (typeof claims.keyId !== \"string\" || !/^[\\w-]{1,128}$/.test(claims.keyId))) ||\n (options.name !== undefined && claims.name !== options.name)\n ) {\n throw new PreviewAuthError(401, \"The Farm Preview tunnel grant is invalid.\");\n }\n return claims as unknown as PreviewTunnelGrantClaims;\n}\n\nexport function readPreviewBearerToken(request: Request): string | undefined {\n const authorization = request.headers.get(\"authorization\");\n if (!authorization || !/^Bearer /i.test(authorization)) return undefined;\n const token = authorization.slice(7).trim();\n return token || undefined;\n}\n\nexport class PreviewAuthError extends Error {\n constructor(\n public status: number,\n message: string,\n ) {\n super(message);\n }\n}\n\nfunction resolvePreviewDurations(options: PreviewManagedAuthOptions) {\n const maxSessionTtlMs = Math.max(\n MIN_SESSION_TTL_MS,\n options.maxSessionTtlMs ?? DEFAULT_MAX_SESSION_TTL_MS,\n );\n return {\n maxSessionTtlMs,\n defaultSessionTtlMs: Math.min(\n maxSessionTtlMs,\n Math.max(MIN_SESSION_TTL_MS, options.defaultSessionTtlMs ?? DEFAULT_SESSION_TTL_MS),\n ),\n };\n}\n\nfunction validatePreviewAuthOptions(options: PreviewManagedAuthOptions) {\n if (Buffer.byteLength(options.signingSecret) < 32) {\n throw new Error(\"Farm Preview auth signingSecret must contain at least 32 bytes.\");\n }\n if (!options.githubClientId && !options.deviceAuth) {\n throw new Error(\"Farm Preview auth requires a GitHub OAuth client id or deviceAuth.\");\n }\n if (options.deviceAuth) {\n validatePublicAuthUrl(options.deviceAuth.issuer);\n if (!options.deviceAuth.clientId)\n throw new Error(\"Farm Preview deviceAuth requires a clientId.\");\n }\n if (options.dashboardUrl) validatePublicAuthUrl(options.dashboardUrl);\n for (const [name, value] of [\n [\"accountTokenTtlMs\", options.accountTokenTtlMs],\n [\"defaultSessionTtlMs\", options.defaultSessionTtlMs],\n [\"maxSessionTtlMs\", options.maxSessionTtlMs],\n ] as const) {\n if (value !== undefined && (!Number.isSafeInteger(value) || value <= 0)) {\n throw new Error(`Farm Preview auth ${name} must be a positive integer.`);\n }\n }\n}\n\nfunction validatePublicAuthUrl(value: string) {\n const url = new URL(value);\n const local = [\"localhost\", \"127.0.0.1\", \"[::1]\"].includes(url.hostname);\n if (\n (url.protocol !== \"https:\" && !(local && url.protocol === \"http:\")) ||\n url.username ||\n url.password ||\n url.search ||\n url.hash\n ) {\n throw new Error(\n \"Farm Preview auth URLs must use HTTPS (HTTP only for loopback development), without credentials, queries or fragments.\",\n );\n }\n}\n\nexport async function authorizePreviewAccount(\n token: string,\n options: PreviewManagedAuthOptions,\n): Promise<PreviewAccountClaims> {\n if (!options.deviceAuth) return verifyPreviewAccountToken(token, options);\n if (!token || token.length > 4096) throw new PreviewAuthError(401, \"Invalid preview credential.\");\n let account;\n try {\n account = await options.deviceAuth.authorizeAccount(token);\n } catch {\n throw new PreviewAuthError(503, \"Farm Preview authentication is temporarily unavailable.\");\n }\n const now = Date.now();\n if (\n !account ||\n typeof account.subject !== \"string\" ||\n !account.subject ||\n typeof account.login !== \"string\" ||\n !account.login ||\n (account.project !== undefined &&\n (typeof account.project !== \"string\" ||\n !/^[a-z0-9][a-z0-9-]{0,62}$/.test(account.project))) ||\n (account.keyId !== undefined &&\n (typeof account.keyId !== \"string\" || !/^[\\w-]{1,128}$/.test(account.keyId))) ||\n (account.expiresAt !== undefined &&\n (!Number.isSafeInteger(account.expiresAt) || account.expiresAt <= now))\n ) {\n throw new PreviewAuthError(401, \"The Farm Infra credential is invalid or expired.\");\n }\n return {\n kind: \"account\",\n provider: \"device\",\n subject: account.subject,\n login: account.login,\n ...(account.keyId ? { keyId: account.keyId } : {}),\n ...(account.project ? { project: account.project } : {}),\n issuedAt: now,\n expiresAt: account.expiresAt ?? now + DEFAULT_ACCOUNT_TOKEN_TTL_MS,\n };\n}\n\nfunction signPreviewClaims(claims: object, secret: string) {\n const payload = Buffer.from(JSON.stringify(claims)).toString(\"base64url\");\n const signature = createHmac(\"sha256\", secret)\n .update(`${TOKEN_PREFIX}.${payload}`)\n .digest(\"base64url\");\n return `${TOKEN_PREFIX}.${payload}.${signature}`;\n}\n\nfunction verifyPreviewClaims(token: string, secret: string): Record<string, unknown> {\n if (!token || token.length > 16_384) {\n throw new PreviewAuthError(401, \"The Farm Preview credential is invalid.\");\n }\n const [prefix, payload, presentedSignature, ...rest] = token.split(\".\");\n if (prefix !== TOKEN_PREFIX || !payload || !presentedSignature || rest.length) {\n throw new PreviewAuthError(401, \"The Farm Preview credential is invalid.\");\n }\n\n const expectedSignature = createHmac(\"sha256\", secret)\n .update(`${TOKEN_PREFIX}.${payload}`)\n .digest();\n let presented: Buffer;\n try {\n presented = Buffer.from(presentedSignature, \"base64url\");\n } catch {\n throw new PreviewAuthError(401, \"The Farm Preview credential is invalid.\");\n }\n if (\n presented.length !== expectedSignature.length ||\n !timingSafeEqual(presented, expectedSignature)\n ) {\n throw new PreviewAuthError(401, \"The Farm Preview credential is invalid.\");\n }\n\n let claims: unknown;\n try {\n claims = JSON.parse(Buffer.from(payload, \"base64url\").toString(\"utf8\"));\n } catch {\n throw new PreviewAuthError(401, \"The Farm Preview credential is invalid.\");\n }\n if (\n typeof claims !== \"object\" ||\n claims === null ||\n Array.isArray(claims) ||\n !(\"issuedAt\" in claims) ||\n !(\"expiresAt\" in claims) ||\n !Number.isSafeInteger(claims.issuedAt) ||\n !Number.isSafeInteger(claims.expiresAt) ||\n Number(claims.issuedAt) > Date.now() + MAX_CLOCK_SKEW_MS ||\n Number(claims.expiresAt) <= Date.now()\n ) {\n throw new PreviewAuthError(401, \"The Farm Preview credential has expired or is invalid.\");\n }\n return claims as Record<string, unknown>;\n}\n","import type { IncomingHttpHeaders, IncomingMessage, ServerResponse } from \"node:http\";\nimport { setTimeout as delay } from \"node:timers/promises\";\nimport {\n authorizePreviewAccount,\n exchangeGitHubPreviewToken,\n getPreviewAuthPublicConfig,\n issuePreviewTunnelGrant,\n PreviewAuthError,\n readPreviewBearerToken,\n verifyPreviewAccountToken,\n verifyPreviewTunnelGrant,\n type PreviewAuthExchangeRateLimitResult,\n type PreviewManagedAuthOptions,\n} from \"./auth.js\";\n\nexport {\n exchangeGitHubPreviewToken,\n getPreviewAuthPublicConfig,\n issuePreviewTunnelGrant,\n PreviewAuthError,\n readPreviewBearerToken,\n verifyPreviewAccountToken,\n verifyPreviewTunnelGrant,\n type PreviewAccountClaims,\n type PreviewAuthExchangeRateLimiter,\n type PreviewAuthExchangeRateLimitResult,\n type PreviewAccountIdentity,\n type PreviewManagedAuthOptions,\n type PreviewTunnelGrant,\n type PreviewTunnelGrantClaims,\n} from \"./auth.js\";\n\nexport interface PreviewSessionAccess {\n id: string;\n name: string;\n ownerId?: string;\n project?: string;\n keyId?: string;\n grantId?: string;\n expiresAt?: number;\n}\n\nexport interface PreviewGatewayOptions {\n /** Required access control when supplied. Denials and errors fail closed, unlike telemetry. */\n authorizeSession?: (session: PreviewSessionAccess) => Promise<boolean>;\n /** Optional metadata-only telemetry. Failures never interrupt a preview. */\n observer?: PreviewGatewayObserver;\n domain?: string;\n baseUrl?: string;\n store?: PreviewGatewayStore;\n sessionTtlMs?: number;\n clientHeartbeatTimeoutMs?: number;\n requestTimeoutMs?: number;\n pollTimeoutMs?: number;\n pollIntervalMs?: number;\n maxBodyBytes?: number;\n maxResponseBodyBytes?: number;\n /** Managed account login and one-preview grant configuration. */\n auth?: PreviewManagedAuthOptions;\n}\n\nexport interface PreviewGatewaySession {\n grantId?: string;\n project?: string;\n keyId?: string;\n ownerId?: string;\n id: string;\n name: string;\n hostname: string;\n publicUrl: string;\n token: string;\n localUrl?: string;\n createdAt: number;\n expiresAt: number;\n /** Internal idle expiry marker. Missing means expiresAt is an absolute cap. */\n slidingExpiration?: true;\n lastHeartbeatAt?: number;\n}\n\nexport interface PreviewGatewayRequest {\n id: string;\n method: string;\n path: string;\n headers: Record<string, string>;\n body?: string | null;\n encoding?: \"base64\";\n createdAt: number;\n cancelled?: true;\n}\n\nexport interface PreviewGatewayResponse {\n status: number;\n // A header may carry multiple values (notably Set-Cookie: a login response\n // commonly sets a session cookie and a CSRF cookie). Those must survive as\n // an array through the store and back onto the public response.\n headers?: Record<string, string | string[]>;\n body?: string | null;\n encoding?: \"base64\";\n}\n\nexport interface PreviewGatewayStore {\n createSession(session: PreviewGatewaySession, ttlMs: number): Promise<void>;\n getSessionByName(name: string): Promise<PreviewGatewaySession | undefined>;\n getSessionById(id: string): Promise<PreviewGatewaySession | undefined>;\n touchSession(session: PreviewGatewaySession, ttlMs: number): Promise<void>;\n enqueueRequest(sessionId: string, request: PreviewGatewayRequest, ttlMs: number): Promise<void>;\n takeRequests(sessionId: string, limit: number): Promise<PreviewGatewayRequest[]>;\n saveResponse(\n sessionId: string,\n requestId: string,\n response: PreviewGatewayResponse,\n ttlMs: number,\n ): Promise<void>;\n getResponse(sessionId: string, requestId: string): Promise<PreviewGatewayResponse | undefined>;\n deleteResponse(sessionId: string, requestId: string): Promise<void>;\n deleteSession(session: PreviewGatewaySession): Promise<void>;\n}\n\ninterface PreviewGatewayRuntimeConfig {\n authorizeSession?: PreviewGatewayOptions[\"authorizeSession\"];\n observer?: PreviewGatewayObserver;\n domain: string;\n baseUrl?: string;\n sessionTtlMs: number;\n clientHeartbeatTimeoutMs: number;\n requestTimeoutMs: number;\n pollTimeoutMs: number;\n pollIntervalMs: number;\n maxBodyBytes: number;\n maxResponseBodyBytes: number;\n auth?: PreviewManagedAuthOptions;\n}\n\nexport interface PreviewGatewayObserver {\n session?(event: {\n grantId?: string;\n project?: string;\n keyId?: string;\n id: string;\n name: string;\n ownerId?: string;\n publicUrl: string;\n expiresAt?: number;\n state: \"connected\" | \"disconnected\";\n at: number;\n }): void | Promise<void>;\n request?(event: {\n sessionId: string;\n method: string;\n path: string;\n status: number;\n durationMs: number;\n at: number;\n }): void | Promise<void>;\n}\n\nfunction observe(callback: (() => void | Promise<void>) | undefined) {\n if (!callback) return;\n try {\n void Promise.resolve(callback()).catch(() => undefined);\n } catch {\n /* Optional telemetry. */\n }\n}\n\nfunction reportSession(\n config: PreviewGatewayRuntimeConfig,\n session: PreviewGatewaySession,\n state: \"connected\" | \"disconnected\",\n) {\n if (!config.observer?.session) return;\n observe(() =>\n config.observer!.session!({\n id: session.id,\n grantId: session.grantId,\n name: session.name,\n project: session.project,\n keyId: session.keyId,\n ownerId: session.ownerId,\n publicUrl: session.publicUrl,\n expiresAt: session.expiresAt,\n state,\n at: Date.now(),\n }),\n );\n}\n\nconst DEFAULT_DOMAIN = \"preview.farmjs.dev\";\nconst DEFAULT_SESSION_TTL_MS = 1000 * 60 * 30;\nconst DEFAULT_CLIENT_HEARTBEAT_TIMEOUT_MS = 1000 * 20;\nconst DEFAULT_REQUEST_TIMEOUT_MS = 25000;\nconst DEFAULT_POLL_TIMEOUT_MS = 15000;\nconst DEFAULT_POLL_INTERVAL_MS = 100;\nconst DEFAULT_MAX_BODY_BYTES = 1024 * 1024 * 5;\nconst DEFAULT_MAX_RESPONSE_BODY_BYTES = 1024 * 1024 * 5;\nconst DEFAULT_POLL_REQUEST_LIMIT = 50;\nconst HOP_BY_HOP_HEADERS = new Set([\n \"connection\",\n \"content-length\",\n \"keep-alive\",\n \"proxy-authenticate\",\n \"proxy-authorization\",\n \"te\",\n \"trailer\",\n \"transfer-encoding\",\n \"upgrade\",\n]);\n\nfunction getHopByHopHeaderNames(headers: Headers | Record<string, string | string[]>): Set<string> {\n const names = new Set(HOP_BY_HOP_HEADERS);\n const connection =\n headers instanceof Headers\n ? headers.get(\"connection\")\n : Object.entries(headers).find(([name]) => name.toLowerCase() === \"connection\")?.[1];\n const values = Array.isArray(connection) ? connection : connection ? [connection] : [];\n for (const value of values) {\n for (const name of value.split(\",\")) {\n const normalized = name.trim().toLowerCase();\n if (normalized) names.add(normalized);\n }\n }\n return names;\n}\n\n/**\n * A gateway request handler, plus the name-ownership probe another preview\n * transport needs before it claims the same public hostname.\n */\nexport type PreviewGatewayHandler = ((request: Request) => Promise<Response>) & {\n isPreviewNameClaimed(name: string): Promise<boolean>;\n};\n\nexport type NodePreviewGatewayHandler = ((\n req: IncomingMessage,\n res: ServerResponse,\n) => Promise<void>) & {\n isPreviewNameClaimed(name: string): Promise<boolean>;\n};\n\nexport function createPreviewGatewayHandler(\n options: PreviewGatewayOptions = {},\n): PreviewGatewayHandler {\n if (options.auth) getPreviewAuthPublicConfig(options.auth);\n const store = options.store || createPreviewGatewayStoreFromEnv();\n const config = {\n authorizeSession: options.authorizeSession,\n observer: options.observer,\n domain: normalizeDomain(options.domain || process.env.FARM_PREVIEW_DOMAIN || DEFAULT_DOMAIN),\n baseUrl: options.baseUrl || process.env.FARM_PREVIEW_GATEWAY_URL,\n sessionTtlMs: options.sessionTtlMs ?? DEFAULT_SESSION_TTL_MS,\n clientHeartbeatTimeoutMs:\n options.clientHeartbeatTimeoutMs ?? DEFAULT_CLIENT_HEARTBEAT_TIMEOUT_MS,\n requestTimeoutMs: options.requestTimeoutMs ?? DEFAULT_REQUEST_TIMEOUT_MS,\n pollTimeoutMs: options.pollTimeoutMs ?? DEFAULT_POLL_TIMEOUT_MS,\n pollIntervalMs: options.pollIntervalMs ?? DEFAULT_POLL_INTERVAL_MS,\n maxBodyBytes: options.maxBodyBytes ?? DEFAULT_MAX_BODY_BYTES,\n maxResponseBodyBytes: options.maxResponseBodyBytes ?? DEFAULT_MAX_RESPONSE_BODY_BYTES,\n auth: options.auth,\n };\n\n const handlePreviewGatewayRequest = async function handlePreviewGatewayRequest(\n request: Request,\n ): Promise<Response> {\n try {\n const url = new URL(request.url);\n\n if (request.method === \"GET\" && url.pathname === \"/api/health\") {\n return json({\n ok: true,\n domain: config.domain,\n store: store.constructor.name || \"PreviewGatewayStore\",\n });\n }\n\n if (request.method === \"GET\" && url.pathname === \"/api/auth/config\") {\n return json(\n config.auth\n ? getPreviewAuthPublicConfig(config.auth)\n : {\n enabled: false,\n controlAuth: \"bearer\",\n defaultSessionTtlMs: config.sessionTtlMs,\n maxSessionTtlMs: config.sessionTtlMs,\n },\n );\n }\n\n if (request.method === \"POST\" && url.pathname === \"/api/auth/exchange\") {\n if (!config.auth) return text(\"Managed preview authentication is not configured.\", 404);\n const rateLimitResponse = await limitPreviewAccountExchange(request, config.auth);\n if (rateLimitResponse) return rateLimitResponse;\n return await exchangePreviewAccount(request, config.auth);\n }\n\n if (request.method === \"POST\" && url.pathname === \"/api/tunnel/grants\") {\n if (!config.auth) return text(\"Managed preview authentication is not configured.\", 404);\n if (config.auth.deviceAuth) {\n const limited = await limitPreviewAccountExchange(request, config.auth);\n if (limited) return limited;\n }\n return await createTunnelGrant(request, config.auth);\n }\n\n if (request.method === \"POST\" && url.pathname === \"/api/sessions\") {\n return await createSession(request, store, config);\n }\n\n const sessionRoute = matchSessionRoute(url.pathname);\n if (sessionRoute) {\n return await handleSessionRoute(request, url, store, config, sessionRoute);\n }\n\n const publicRoute = resolvePublicPreviewRoute(request, url, config.domain);\n if (publicRoute) {\n return await proxyPublicRequest(request, url, store, config, publicRoute);\n }\n\n return json(\n {\n ok: true,\n message: \"Farm Preview Gateway\",\n createSession: \"/api/sessions\",\n health: \"/api/health\",\n },\n 200,\n );\n } catch (error) {\n if (error instanceof GatewayHttpError || error instanceof PreviewAuthError) {\n return text(error.message, error.status);\n }\n return text(\n error instanceof Error ? error.message : \"Unexpected preview gateway error.\",\n 500,\n );\n }\n };\n\n // The persistent relay serves the same public hostnames from a separate name\n // namespace, and its route wins over this handler, so it has to be able to\n // ask whether a live polling session already owns a name before accepting a\n // claim for it.\n return Object.assign(handlePreviewGatewayRequest, {\n async isPreviewNameClaimed(name: string) {\n const claimed = sanitizePreviewName(name);\n if (!claimed) return false;\n const session = await store.getSessionByName(claimed);\n return Boolean(session && isPreviewClientOnline(session, config));\n },\n });\n}\n\nasync function limitPreviewAccountExchange(request: Request, auth: PreviewManagedAuthOptions) {\n if (!auth.rateLimitExchange) return undefined;\n let result: PreviewAuthExchangeRateLimitResult;\n try {\n result = await auth.rateLimitExchange(request);\n } catch {\n return new Response(\"Farm Preview login is temporarily unavailable. Try again shortly.\", {\n status: 503,\n headers: {\n \"cache-control\": \"no-store\",\n \"content-type\": \"text/plain; charset=utf-8\",\n \"retry-after\": \"1\",\n },\n });\n }\n if (result.allowed) return undefined;\n\n const retryAfterMs =\n Number.isFinite(result.retryAfterMs) && (result.retryAfterMs ?? 0) > 0\n ? result.retryAfterMs!\n : 1000;\n return new Response(\"Too many Farm Preview login attempts. Try again shortly.\", {\n status: 429,\n headers: {\n \"cache-control\": \"no-store\",\n \"content-type\": \"text/plain; charset=utf-8\",\n \"retry-after\": String(Math.max(1, Math.ceil(retryAfterMs / 1000))),\n },\n });\n}\n\nasync function exchangePreviewAccount(request: Request, auth: PreviewManagedAuthOptions) {\n const input = (await request.json().catch(() => ({}))) as {\n provider?: string;\n accessToken?: string;\n };\n if (input.provider !== \"github\") {\n throw new PreviewAuthError(400, \"Farm Preview currently supports GitHub sign-in.\");\n }\n return json(await exchangeGitHubPreviewToken(input.accessToken || \"\", auth));\n}\n\nasync function createTunnelGrant(request: Request, auth: PreviewManagedAuthOptions) {\n const accountToken = readPreviewBearerToken(request);\n if (!accountToken) {\n throw new PreviewAuthError(401, \"Sign in with Farm Preview before creating a tunnel grant.\");\n }\n const account = await authorizePreviewAccount(accountToken, auth);\n const input = (await request.json().catch(() => ({}))) as {\n name?: string;\n project?: string;\n expiresInMs?: number;\n };\n const name = sanitizePreviewName(input.name);\n if (!name) throw new PreviewAuthError(400, \"A valid preview name is required.\");\n\n return json(\n issuePreviewTunnelGrant(\n account,\n {\n name,\n project: input.project,\n ...(input.expiresInMs !== undefined ? { expiresInMs: input.expiresInMs } : {}),\n },\n auth,\n ),\n );\n}\n\nexport function createNodePreviewGatewayHandler(\n options: PreviewGatewayOptions = {},\n): NodePreviewGatewayHandler {\n const handler = createPreviewGatewayHandler(options);\n\n const nodePreviewGatewayHandler = async function nodePreviewGatewayHandler(\n req: IncomingMessage,\n res: ServerResponse,\n ) {\n const controller = new AbortController();\n const abort = () => controller.abort();\n const abortOnClose = () => {\n if (!res.writableEnded) abort();\n };\n req.once(\"aborted\", abort);\n res.once(\"close\", abortOnClose);\n try {\n const response = await handler(nodeRequestToWebRequest(req, controller.signal));\n if (controller.signal.aborted || res.destroyed) {\n await response.body?.cancel();\n return;\n }\n res.statusCode = response.status;\n // Headers.forEach folds repeated headers into one comma-joined value, which\n // corrupts multiple Set-Cookie. Emit those separately as an array so each\n // cookie becomes its own header line.\n const setCookies = response.headers.getSetCookie?.() ?? [];\n response.headers.forEach((value, key) => {\n if (key.toLowerCase() === \"set-cookie\") return;\n res.setHeader(key, value);\n });\n if (setCookies.length > 0) {\n res.setHeader(\"set-cookie\", setCookies);\n }\n res.end(Buffer.from(await response.arrayBuffer()));\n } finally {\n req.off(\"aborted\", abort);\n res.off(\"close\", abortOnClose);\n }\n };\n\n return Object.assign(nodePreviewGatewayHandler, {\n isPreviewNameClaimed: handler.isPreviewNameClaimed,\n });\n}\n\nexport function createPreviewGatewayStoreFromEnv(): PreviewGatewayStore {\n const redisUrl = process.env.UPSTASH_REDIS_REST_URL || process.env.KV_REST_API_URL;\n const redisToken = process.env.UPSTASH_REDIS_REST_TOKEN || process.env.KV_REST_API_TOKEN;\n\n if (redisUrl && redisToken) {\n return new RedisRestPreviewGatewayStore({\n url: redisUrl,\n token: redisToken,\n });\n }\n\n return new MemoryPreviewGatewayStore();\n}\n\nexport class MemoryPreviewGatewayStore implements PreviewGatewayStore {\n private sessions = new Map<string, PreviewGatewaySession>();\n private names = new Map<string, string>();\n private queues = new Map<string, PreviewGatewayRequest[]>();\n private responses = new Map<string, PreviewGatewayResponse>();\n\n async createSession(session: PreviewGatewaySession): Promise<void> {\n this.sessions.set(session.id, session);\n this.names.set(session.name, session.id);\n this.queues.set(session.id, []);\n }\n\n async getSessionByName(name: string): Promise<PreviewGatewaySession | undefined> {\n const id = this.names.get(name);\n return id ? this.getSessionById(id) : undefined;\n }\n\n async getSessionById(id: string): Promise<PreviewGatewaySession | undefined> {\n const session = this.sessions.get(id);\n if (!session) return undefined;\n if (session.expiresAt <= Date.now()) {\n await this.deleteSession(session);\n return undefined;\n }\n return session;\n }\n\n async touchSession(session: PreviewGatewaySession, _ttlMs: number): Promise<void> {\n // A concurrent delete (the preview was stopped) must win: a poll handler\n // that outlives the DELETE must never resurrect a session that is no\n // longer stored, or its name would look \"online\" and block a restart.\n if (!this.sessions.has(session.id)) return;\n this.sessions.set(session.id, session);\n const nameOwner = this.names.get(session.name);\n if (!nameOwner || nameOwner === session.id) {\n this.names.set(session.name, session.id);\n }\n }\n\n async enqueueRequest(sessionId: string, request: PreviewGatewayRequest): Promise<void> {\n const queue = this.queues.get(sessionId) || [];\n queue.push(request);\n this.queues.set(sessionId, queue);\n }\n\n async takeRequests(sessionId: string, limit: number): Promise<PreviewGatewayRequest[]> {\n const queue = this.queues.get(sessionId) || [];\n return queue.splice(0, limit);\n }\n\n async saveResponse(\n sessionId: string,\n requestId: string,\n response: PreviewGatewayResponse,\n ): Promise<void> {\n this.responses.set(responseKey(sessionId, requestId), response);\n }\n\n async getResponse(\n sessionId: string,\n requestId: string,\n ): Promise<PreviewGatewayResponse | undefined> {\n return this.responses.get(responseKey(sessionId, requestId));\n }\n\n async deleteResponse(sessionId: string, requestId: string): Promise<void> {\n this.responses.delete(responseKey(sessionId, requestId));\n }\n\n async deleteSession(session: PreviewGatewaySession): Promise<void> {\n this.sessions.delete(session.id);\n // The name may have been taken over by a newer session; only remove the\n // mapping if it still points at the session being deleted.\n if (this.names.get(session.name) === session.id) {\n this.names.delete(session.name);\n }\n this.queues.delete(session.id);\n }\n}\n\nexport class RedisRestPreviewGatewayStore implements PreviewGatewayStore {\n constructor(private options: { url: string; token: string; prefix?: string }) {}\n\n async createSession(session: PreviewGatewaySession, ttlMs: number): Promise<void> {\n await this.command(\"SET\", this.sessionKey(session.id), JSON.stringify(session), \"PX\", ttlMs);\n await this.command(\"SET\", this.nameKey(session.name), session.id, \"PX\", ttlMs);\n await this.command(\"DEL\", this.queueKey(session.id));\n }\n\n async getSessionByName(name: string): Promise<PreviewGatewaySession | undefined> {\n const id = await this.command<string | null>(\"GET\", this.nameKey(name));\n return id ? this.getSessionById(id) : undefined;\n }\n\n async getSessionById(id: string): Promise<PreviewGatewaySession | undefined> {\n const value = await this.command<string | null>(\"GET\", this.sessionKey(id));\n return value ? JSON.parse(value) : undefined;\n }\n\n async touchSession(session: PreviewGatewaySession, ttlMs: number): Promise<void> {\n const nextSession = { ...session };\n const remainingTtlMs = Math.max(1, Math.min(ttlMs, session.expiresAt - Date.now()));\n // XX only refreshes a session key that still exists. A poll handler that\n // outlives a DELETE must not recreate the session (nor its name mapping),\n // which would otherwise block reclaiming the name on restart.\n const result = await this.command<string | null>(\n \"SET\",\n this.sessionKey(session.id),\n JSON.stringify(nextSession),\n \"PX\",\n remainingTtlMs,\n \"XX\",\n );\n if (result === null) return;\n const nameOwner = await this.command<string | null>(\"GET\", this.nameKey(session.name));\n if (!nameOwner || nameOwner === session.id) {\n await this.command(\"SET\", this.nameKey(session.name), session.id, \"PX\", remainingTtlMs);\n }\n await this.command(\"EXPIRE\", this.queueKey(session.id), Math.ceil(remainingTtlMs / 1000));\n }\n\n async enqueueRequest(\n sessionId: string,\n request: PreviewGatewayRequest,\n ttlMs: number,\n ): Promise<void> {\n await this.command(\"RPUSH\", this.queueKey(sessionId), JSON.stringify(request));\n await this.command(\"EXPIRE\", this.queueKey(sessionId), Math.ceil(ttlMs / 1000));\n }\n\n async takeRequests(sessionId: string, limit: number): Promise<PreviewGatewayRequest[]> {\n const values = await this.command<string[] | string | null>(\n \"LPOP\",\n this.queueKey(sessionId),\n limit,\n );\n if (!values) return [];\n const list = Array.isArray(values) ? values : [values];\n return list.map((value) => JSON.parse(value));\n }\n\n async saveResponse(\n sessionId: string,\n requestId: string,\n response: PreviewGatewayResponse,\n ttlMs: number,\n ): Promise<void> {\n await this.command(\n \"SET\",\n this.responseKey(sessionId, requestId),\n JSON.stringify(response),\n \"PX\",\n ttlMs,\n );\n }\n\n async getResponse(\n sessionId: string,\n requestId: string,\n ): Promise<PreviewGatewayResponse | undefined> {\n const value = await this.command<string | null>(\"GET\", this.responseKey(sessionId, requestId));\n return value ? JSON.parse(value) : undefined;\n }\n\n async deleteResponse(sessionId: string, requestId: string): Promise<void> {\n await this.command(\"DEL\", this.responseKey(sessionId, requestId));\n }\n\n async deleteSession(session: PreviewGatewaySession): Promise<void> {\n const keys = [this.sessionKey(session.id), this.queueKey(session.id)];\n // The name may have been taken over by a newer session; only remove the\n // mapping if it still points at the session being deleted.\n const nameOwner = await this.command<string | null>(\"GET\", this.nameKey(session.name));\n if (nameOwner === session.id) {\n keys.push(this.nameKey(session.name));\n }\n await this.command(\"DEL\", ...keys);\n }\n\n private async command<T = unknown>(...args: Array<string | number>) {\n const response = await fetch(this.options.url, {\n method: \"POST\",\n headers: {\n authorization: `Bearer ${this.options.token}`,\n \"content-type\": \"application/json\",\n },\n body: JSON.stringify(args),\n });\n\n if (!response.ok) {\n throw new Error(\n `Preview gateway Redis command failed (${response.status}): ${await response.text()}`,\n );\n }\n\n const data = (await response.json()) as { result?: T; error?: string };\n if (data.error) {\n throw new Error(`Preview gateway Redis command failed: ${data.error}`);\n }\n return data.result as T;\n }\n\n private key(value: string) {\n return `${this.options.prefix || \"farm-preview\"}:${value}`;\n }\n\n private sessionKey(id: string) {\n return this.key(`session:${id}`);\n }\n\n private nameKey(name: string) {\n return this.key(`name:${name}`);\n }\n\n private queueKey(id: string) {\n return this.key(`queue:${id}`);\n }\n\n private responseKey(sessionId: string, requestId: string) {\n return this.key(`response:${sessionId}:${requestId}`);\n }\n}\n\nasync function createSession(\n request: Request,\n store: PreviewGatewayStore,\n config: PreviewGatewayRuntimeConfig,\n) {\n const input = (await request.json().catch(() => ({}))) as {\n name?: string;\n localUrl?: string;\n expiresInMs?: number;\n };\n const requestedName = sanitizePreviewName(input.name);\n let name = requestedName || randomPreviewName();\n let expiresAt = Date.now() + config.sessionTtlMs;\n let slidingExpiration = true;\n let ownerId: string | undefined;\n let project: string | undefined;\n let keyId: string | undefined;\n let grantId: string | undefined;\n\n if (config.auth) {\n const grantToken = readPreviewBearerToken(request);\n if (!grantToken) {\n throw new GatewayHttpError(401, \"Sign in with Farm Preview before creating a session.\");\n }\n try {\n const grant = verifyPreviewTunnelGrant(grantToken, {\n signingSecret: config.auth.signingSecret,\n ...(requestedName ? { name: requestedName } : {}),\n });\n name = grant.name;\n ownerId = `${grant.provider || \"github\"}:${grant.subject}`;\n project = grant.project ?? grant.name;\n keyId = grant.keyId;\n grantId = grant.nonce;\n expiresAt = grant.expiresAt;\n slidingExpiration = false;\n } catch (error) {\n if (error instanceof PreviewAuthError)\n throw new GatewayHttpError(error.status, error.message);\n throw error;\n }\n } else if (Number.isFinite(input.expiresInMs)) {\n expiresAt = Date.now() + clamp(Number(input.expiresInMs), 60_000, config.sessionTtlMs);\n slidingExpiration = false;\n }\n\n // A name that is still owned by a live session cannot be claimed by a new\n // one — otherwise any caller could repoint an active preview's public URL\n // to their own session. Stale sessions (missed heartbeats) may be taken\n // over, matching how public routing treats them.\n for (let attempt = 0; ; attempt++) {\n const existing = await store.getSessionByName(name);\n if (!existing || !isPreviewClientOnline(existing, config)) {\n break;\n }\n if (requestedName || config.auth) {\n return text(\n `A Farm preview named \"${name}\" is already active. Choose another name or stop the running preview.`,\n 409,\n );\n }\n if (attempt >= 4) {\n return text(\"Could not allocate a unique preview name. Try again.\", 503);\n }\n name = randomPreviewName();\n }\n\n const hostname = `${name}.${config.domain}`;\n const session: PreviewGatewaySession = {\n ...(grantId ? { grantId } : {}),\n ...(project ? { project } : {}),\n ...(keyId ? { keyId } : {}),\n ...(ownerId ? { ownerId } : {}),\n id: randomId(\"sess\"),\n name,\n hostname,\n publicUrl: createPublicUrl(request, config, name),\n token: randomId(\"token\"),\n localUrl: input.localUrl,\n createdAt: Date.now(),\n expiresAt,\n ...(slidingExpiration ? { slidingExpiration: true as const } : {}),\n lastHeartbeatAt: Date.now(),\n };\n\n await requireSessionAccess(config, session);\n await store.createSession(session, Math.max(1, expiresAt - Date.now()));\n reportSession(config, session, \"connected\");\n\n return json({\n id: session.id,\n name: session.name,\n token: session.token,\n publicUrl: session.publicUrl,\n ...(session.slidingExpiration ? {} : { expiresAt: session.expiresAt }),\n });\n}\n\nasync function handleSessionRoute(\n request: Request,\n url: URL,\n store: PreviewGatewayStore,\n config: PreviewGatewayRuntimeConfig,\n route: { sessionId: string; action?: string; requestId?: string },\n) {\n const session = await requireSession(\n store,\n route.sessionId,\n readPreviewBearerToken(request) || url.searchParams.get(\"token\"),\n );\n\n // Cleanup is still possible when the authority is unavailable or the grant is revoked.\n if (request.method !== \"DELETE\" || route.action) await requireSessionAccess(config, session);\n\n if (request.method === \"GET\" && route.action === \"requests\") {\n return await pollSessionRequests(url, store, config, session);\n }\n\n if (request.method === \"POST\" && route.action === \"responses\" && route.requestId) {\n let response: PreviewGatewayResponse;\n try {\n response = await parsePreviewResponse(request, config.maxResponseBodyBytes);\n } catch (error) {\n if (error instanceof GatewayHttpError && error.status === 413) {\n await store.saveResponse(\n session.id,\n route.requestId,\n createOversizedPreviewResponse(config.maxResponseBodyBytes),\n remainingSessionTtl(session, config),\n );\n }\n throw error;\n }\n await store.saveResponse(\n session.id,\n route.requestId,\n response,\n remainingSessionTtl(session, config),\n );\n await markSessionOnline(store, config, session);\n return json({ ok: true });\n }\n\n if (request.method === \"POST\" && route.action === \"heartbeat\") {\n await markSessionOnline(store, config, session);\n return json({\n ok: true,\n ...(session.slidingExpiration ? {} : { expiresAt: session.expiresAt }),\n });\n }\n\n if (request.method === \"DELETE\" && !route.action) {\n await store.deleteSession(session);\n reportSession(config, session, \"disconnected\");\n return json({ ok: true });\n }\n\n return text(\"Preview gateway route not found.\", 404);\n}\n\nasync function parsePreviewResponse(request: Request, maxBodyBytes: number) {\n const maxEncodedBytes = Math.ceil(maxBodyBytes / 3) * 4 + 64 * 1024;\n const contentLength = Number(request.headers.get(\"content-length\") || 0);\n if (Number.isFinite(contentLength) && contentLength > maxEncodedBytes) {\n await request.body?.cancel();\n throw new GatewayHttpError(413, \"Preview response body is too large.\");\n }\n\n const bytes = await readBodyWithLimit(\n request,\n maxEncodedBytes,\n \"Preview response body is too large.\",\n );\n let response: PreviewGatewayResponse;\n try {\n response = JSON.parse(bytes.toString(\"utf8\")) as PreviewGatewayResponse;\n } catch {\n throw new GatewayHttpError(400, \"Preview response body must be valid JSON.\");\n }\n\n if (getPreviewResponseBodySize(response) > maxBodyBytes) {\n throw new GatewayHttpError(413, \"Preview response body is too large.\");\n }\n return response;\n}\n\nfunction getPreviewResponseBodySize(response: PreviewGatewayResponse) {\n if (!response.body) return 0;\n return Buffer.byteLength(response.body, response.encoding === \"base64\" ? \"base64\" : \"utf8\");\n}\n\nfunction createOversizedPreviewResponse(maxBodyBytes: number): PreviewGatewayResponse {\n return {\n status: 502,\n headers: { \"content-type\": \"text/plain; charset=utf-8\" },\n body: Buffer.from(\n `The local preview response exceeded the ${maxBodyBytes} byte limit.`,\n ).toString(\"base64\"),\n encoding: \"base64\",\n };\n}\n\nasync function pollSessionRequests(\n url: URL,\n store: PreviewGatewayStore,\n config: PreviewGatewayRuntimeConfig,\n session: PreviewGatewaySession,\n) {\n const waitMs = clamp(Number(url.searchParams.get(\"wait\") || config.pollTimeoutMs), 1000, 25000);\n const deadline = Date.now() + waitMs;\n let nextAccessCheckAt = Date.now() + 1000;\n\n await markSessionOnline(store, config, session);\n\n while (Date.now() < deadline) {\n if (Date.now() >= nextAccessCheckAt) {\n await requireSessionAccess(config, session);\n nextAccessCheckAt = Date.now() + 1000;\n }\n const requests = await store.takeRequests(session.id, DEFAULT_POLL_REQUEST_LIMIT);\n if (requests.length) {\n await requireSessionAccess(config, session);\n await markSessionOnline(store, config, session);\n return json({ requests });\n }\n await delay(config.pollIntervalMs);\n }\n\n await markSessionOnline(store, config, session);\n return new Response(null, { status: 204 });\n}\n\nasync function proxyPublicRequest(\n request: Request,\n url: URL,\n store: PreviewGatewayStore,\n config: PreviewGatewayRuntimeConfig,\n route: { name: string; path: string },\n) {\n const session = await store.getSessionByName(route.name);\n if (!session) {\n return inactivePreviewResponse(request, route.name);\n }\n if (!isPreviewClientOnline(session, config)) {\n await store.deleteSession(session);\n reportSession(config, session, \"disconnected\");\n return inactivePreviewResponse(request, route.name);\n }\n\n await requireSessionAccess(config, session);\n\n const startedAt = Date.now();\n let status = 500;\n try {\n const previewRequest = await serializePreviewRequest(\n request,\n url,\n route.path,\n config.maxBodyBytes,\n );\n await store.enqueueRequest(session.id, previewRequest, remainingSessionTtl(session, config));\n await store.touchSession(session, remainingSessionTtl(session, config));\n\n const response = await waitForPreviewResponse(\n store,\n config,\n session,\n previewRequest.id,\n request.signal,\n );\n if (response === \"cancelled\") {\n status = 499;\n await store.enqueueRequest(\n session.id,\n {\n ...previewRequest,\n body: undefined,\n encoding: undefined,\n cancelled: true,\n createdAt: Date.now(),\n },\n remainingSessionTtl(session, config),\n );\n return new Response(null, { status: 499 });\n }\n if (response === \"stale\") {\n status = 503;\n return inactivePreviewResponse(request, route.name);\n }\n if (!response) {\n status = 504;\n return text(\"The local Farm preview did not respond before the gateway timed out.\", 504);\n }\n\n const headers = new Headers();\n const responseHopByHopHeaders = getHopByHopHeaderNames(response.headers || {});\n for (const [key, value] of Object.entries(response.headers || {})) {\n const normalized = key.toLowerCase();\n if (responseHopByHopHeaders.has(normalized) || normalized === \"content-encoding\") {\n continue;\n }\n // Append multi-valued headers (Set-Cookie) so every value reaches the\n // visitor; Headers.set would keep only the last.\n for (const single of Array.isArray(value) ? value : [value]) {\n headers.append(key, single);\n }\n }\n headers.set(\"cache-control\", \"no-store\");\n headers.set(\"x-farm-preview\", session.name);\n\n status = response.status || 200;\n return new Response(\n response.body\n ? Buffer.from(response.body, response.encoding === \"base64\" ? \"base64\" : \"utf8\")\n : null,\n {\n status,\n headers,\n },\n );\n } catch (error) {\n if (error instanceof GatewayHttpError) status = error.status;\n throw error;\n } finally {\n if (config.observer?.request)\n observe(() =>\n config.observer!.request!({\n sessionId: session.id,\n method: request.method,\n path: route.path.split(/[?#]/, 1)[0].slice(0, 2048),\n status,\n durationMs: Date.now() - startedAt,\n at: Date.now(),\n }),\n );\n }\n}\n\nasync function waitForPreviewResponse(\n store: PreviewGatewayStore,\n config: PreviewGatewayRuntimeConfig,\n session: PreviewGatewaySession,\n requestId: string,\n signal: AbortSignal,\n) {\n const deadline = Date.now() + config.requestTimeoutMs;\n let nextLivenessCheckAt = 0;\n\n while (Date.now() < deadline) {\n if (signal.aborted) return \"cancelled\";\n if (Date.now() >= nextLivenessCheckAt) {\n await requireSessionAccess(config, session);\n const latestSession = await store.getSessionById(session.id);\n if (!latestSession || !isPreviewClientOnline(latestSession, config)) {\n if (latestSession) await store.deleteSession(latestSession);\n return \"stale\";\n }\n nextLivenessCheckAt = Date.now() + 1000;\n }\n\n const response = await store.getResponse(session.id, requestId);\n if (response) {\n await requireSessionAccess(config, session);\n await store.deleteResponse(session.id, requestId);\n return response;\n }\n try {\n await delay(config.pollIntervalMs, undefined, { signal });\n } catch {\n if (signal.aborted) return \"cancelled\";\n throw new Error(\"Preview response polling was interrupted.\");\n }\n }\n\n return undefined;\n}\n\nasync function requireSessionAccess(\n config: PreviewGatewayRuntimeConfig,\n session: PreviewGatewaySession,\n) {\n if (!config.authorizeSession) return;\n const { id, name, ownerId, project, keyId, grantId, expiresAt } = session;\n let allowed;\n try {\n allowed = await config.authorizeSession({\n id,\n name,\n ownerId,\n project,\n keyId,\n grantId,\n expiresAt,\n });\n } catch {\n throw new GatewayHttpError(503, \"Preview access verification is temporarily unavailable.\");\n }\n if (allowed !== true) throw new GatewayHttpError(403, \"This preview is no longer authorized.\");\n}\n\nasync function markSessionOnline(\n store: PreviewGatewayStore,\n config: PreviewGatewayRuntimeConfig,\n session: PreviewGatewaySession,\n) {\n const now = Date.now();\n session.lastHeartbeatAt = now;\n if (session.slidingExpiration) {\n session.expiresAt = now + config.sessionTtlMs;\n }\n await store.touchSession(session, remainingSessionTtl(session, config));\n reportSession(config, session, \"connected\");\n}\n\nfunction remainingSessionTtl(session: PreviewGatewaySession, config: PreviewGatewayRuntimeConfig) {\n return Math.max(1, Math.min(config.sessionTtlMs, session.expiresAt - Date.now()));\n}\n\nfunction isPreviewClientOnline(\n session: PreviewGatewaySession,\n config: PreviewGatewayRuntimeConfig,\n) {\n const lastHeartbeatAt = session.lastHeartbeatAt || session.createdAt;\n return (\n session.expiresAt > Date.now() &&\n Date.now() - lastHeartbeatAt <= config.clientHeartbeatTimeoutMs\n );\n}\n\nasync function serializePreviewRequest(\n request: Request,\n url: URL,\n routePath: string,\n maxBodyBytes: number,\n): Promise<PreviewGatewayRequest> {\n const method = request.method.toUpperCase();\n const headers: Record<string, string> = {};\n const requestHopByHopHeaders = getHopByHopHeaderNames(request.headers);\n request.headers.forEach((value, key) => {\n const normalized = key.toLowerCase();\n if (\n !requestHopByHopHeaders.has(normalized) &&\n normalized !== \"forwarded\" &&\n !normalized.startsWith(\"x-forwarded-\")\n ) {\n headers[key] = value;\n }\n });\n\n headers[\"x-forwarded-host\"] = request.headers.get(\"host\") || url.host;\n headers[\"x-forwarded-proto\"] = url.protocol.replace(\":\", \"\");\n\n let body: string | undefined;\n if (method !== \"GET\" && method !== \"HEAD\") {\n const contentLength = Number(request.headers.get(\"content-length\") || 0);\n if (contentLength > maxBodyBytes) {\n throw new GatewayHttpError(413, \"Preview request body is too large.\");\n }\n\n const buffer = await readBodyWithLimit(\n request,\n maxBodyBytes,\n \"Preview request body is too large.\",\n );\n body = buffer.toString(\"base64\");\n }\n\n return {\n id: randomId(\"req\"),\n method,\n path: `${routePath}${url.search}`,\n headers,\n body,\n encoding: body ? \"base64\" : undefined,\n createdAt: Date.now(),\n };\n}\n\nasync function readBodyWithLimit(request: Request, maxBytes: number, message: string) {\n if (!request.body) return Buffer.alloc(0);\n const reader = request.body.getReader();\n const chunks: Uint8Array[] = [];\n let size = 0;\n try {\n while (true) {\n const { done, value } = await reader.read();\n if (done) break;\n size += value.byteLength;\n if (size > maxBytes) {\n await reader.cancel();\n throw new GatewayHttpError(413, message);\n }\n chunks.push(value);\n }\n } catch (error) {\n await reader.cancel().catch(() => undefined);\n throw error;\n }\n return Buffer.concat(chunks, size);\n}\n\nasync function requireSession(store: PreviewGatewayStore, sessionId: string, token: string | null) {\n const session = await store.getSessionById(sessionId);\n if (!session || !token || token !== session.token) {\n throw new GatewayHttpError(401, \"Invalid preview gateway session.\");\n }\n return session;\n}\n\nfunction resolvePublicPreviewRoute(request: Request, url: URL, domain: string) {\n const pathMatch = url.pathname.match(/^\\/__preview\\/([^/]+)(\\/.*)?$/);\n if (pathMatch) {\n return {\n name: pathMatch[1],\n path: pathMatch[2] || \"/\",\n };\n }\n\n const host = (request.headers.get(\"host\") || url.host).split(\":\")[0];\n if (!host || host === domain || !host.endsWith(`.${domain}`)) {\n return undefined;\n }\n\n return {\n name: host.slice(0, -(domain.length + 1)),\n path: url.pathname || \"/\",\n };\n}\n\nfunction matchSessionRoute(pathname: string) {\n const match = pathname.match(\n /^\\/api\\/sessions\\/([^/]+)(?:\\/(requests|heartbeat|responses)(?:\\/([^/]+))?)?$/,\n );\n if (!match) return undefined;\n return {\n sessionId: match[1],\n action: match[2],\n requestId: match[3],\n };\n}\n\nfunction nodeRequestToWebRequest(req: IncomingMessage, signal?: AbortSignal) {\n const host = headerValue(req.headers, \"host\") || \"localhost\";\n // This adapter is the public trust boundary. A visitor-controlled forwarded\n // protocol must not influence the authority serialized to the local app.\n const protocol = isLocalHost(host.split(\":\")[0]) ? \"http\" : \"https\";\n const url = `${protocol}://${host}${req.url || \"/\"}`;\n const headers = nodeHeadersToWebHeaders(req.headers);\n const method = req.method || \"GET\";\n const init: RequestInit & { duplex?: \"half\" } = {\n method,\n headers,\n signal,\n };\n\n if (method !== \"GET\" && method !== \"HEAD\") {\n init.body = req as unknown as BodyInit;\n init.duplex = \"half\";\n }\n\n return new Request(url, init);\n}\n\nfunction nodeHeadersToWebHeaders(headers: IncomingHttpHeaders) {\n const next = new Headers();\n for (const [key, value] of Object.entries(headers)) {\n if (value === undefined) continue;\n if (Array.isArray(value)) {\n for (const item of value) next.append(key, item);\n } else {\n next.set(key, value);\n }\n }\n return next;\n}\n\nfunction headerValue(headers: IncomingHttpHeaders, key: string) {\n const value = headers[key];\n return Array.isArray(value) ? value[0] : value;\n}\n\nfunction createPublicUrl(request: Request, config: PreviewGatewayRuntimeConfig, name: string) {\n // Each browser preview needs its own origin: a path prefix cannot relocate\n // root-relative scripts, imports, fetches, or client-side navigation.\n if (config.domain === \"localhost\" || config.domain.endsWith(\".localhost\")) {\n const base = new URL(config.baseUrl || request.url);\n base.hostname = `${name}.${config.domain}`;\n return base.origin;\n }\n if (config.baseUrl) {\n const base = new URL(config.baseUrl);\n if (isLocalHost(base.hostname)) {\n return `${config.baseUrl.replace(/\\/+$/, \"\")}/__preview/${name}`;\n }\n }\n\n const host = request.headers.get(\"host\") || new URL(request.url).host;\n if (isLocalHost(host.split(\":\")[0])) {\n return `${new URL(request.url).origin}/__preview/${name}`;\n }\n\n return `https://${name}.${config.domain}`;\n}\n\nfunction json(value: unknown, status = 200) {\n return new Response(JSON.stringify(value), {\n status,\n headers: {\n \"content-type\": \"application/json\",\n \"cache-control\": \"no-store\",\n },\n });\n}\n\nfunction text(value: string, status = 200) {\n return new Response(value, {\n status,\n headers: {\n \"content-type\": \"text/plain; charset=utf-8\",\n \"cache-control\": \"no-store\",\n },\n });\n}\n\nfunction inactivePreviewResponse(request: Request, name: string) {\n if (!request.headers.get(\"accept\")?.includes(\"text/html\")) {\n return text(`No active Farm preview is running for \"${name}\".`, 404);\n }\n const safeName = name.replace(/[&<>\"']/g, (character) => {\n if (character === \"&\") return \"&\";\n if (character === \"<\") return \"<\";\n if (character === \">\") return \">\";\n if (character === '\"') return \""\";\n return \"'\";\n });\n return new Response(\n `<!doctype html><html lang=\"en\"><meta charset=\"utf-8\"><meta name=\"viewport\" content=\"width=device-width,initial-scale=1\"><title>Farm preview expired</title><style>body{font:16px/1.5 system-ui,sans-serif;display:grid;min-height:100vh;margin:0;place-items:center;background:#f7f7f5;color:#171714}main{max-width:34rem;padding:2rem;text-align:center}h1{font-size:1.75rem}code{background:#e9e9e4;border-radius:.35rem;padding:.15rem .35rem}</style><main><h1>This Farm preview has expired</h1><p>The temporary preview <code>${safeName}</code> is no longer connected.</p><p>Ask its owner to run <code>farm preview</code> again.</p></main></html>`,\n {\n status: 410,\n headers: {\n \"content-type\": \"text/html; charset=utf-8\",\n \"cache-control\": \"no-store\",\n },\n },\n );\n}\n\nfunction sanitizePreviewName(value: string | undefined) {\n if (!value) return undefined;\n return value\n .toLowerCase()\n .replace(/[^a-z0-9-]+/g, \"-\")\n .replace(/^-+|-+$/g, \"\")\n .slice(0, 48);\n}\n\nfunction randomPreviewName() {\n return `farm-${Math.random().toString(36).slice(2, 8)}`;\n}\n\nfunction randomId(prefix: string) {\n return `${prefix}_${crypto.randomUUID().replace(/-/g, \"\")}`;\n}\n\nfunction normalizeDomain(value: string) {\n return value\n .replace(/^https?:\\/\\//, \"\")\n .replace(/^\\.*/, \"\")\n .replace(/\\/*$/, \"\");\n}\n\nfunction responseKey(sessionId: string, requestId: string) {\n return `${sessionId}:${requestId}`;\n}\n\nfunction clamp(value: number, min: number, max: number) {\n if (!Number.isFinite(value)) return min;\n return Math.max(min, Math.min(max, value));\n}\n\nfunction isLocalHost(hostname: string) {\n return (\n hostname === \"localhost\" ||\n hostname.endsWith(\".localhost\") ||\n hostname === \"127.0.0.1\" ||\n hostname === \"::1\"\n );\n}\n\nclass GatewayHttpError extends Error {\n constructor(\n public status: number,\n message: string,\n ) {\n super(message);\n }\n}\n"],"mappings":";;;AAqFA,MAAM,eAAe;AACrB,MAAM,+BAA+B;AACrC,MAAMA,2BAAyB;AAC/B,MAAM,6BAA6B;AACnC,MAAM,qBAAqB;AAC3B,MAAM,oBAAoB;AAE1B,SAAgB,2BAA2B,SAAoC;CAC7E,2BAA2B,OAAO;CAClC,MAAM,YAAY,wBAAwB,OAAO;CACjD,OAAO;EACL,SAAS;EACT,aAAa;EACb,GAAI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