shardline-server 1.0.1

HTTP server boundary, runtime, and operator workflows for Shardline.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
use std::collections::BTreeMap;
use std::fmt;

use serde::{Deserialize, Serialize};
use shardline_protocol::{RepositoryProvider, SecretString, TokenScope};

/// Health response returned by the HTTP server.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct HealthResponse {
    /// Service status.
    pub status: String,
}

/// Readiness response returned by the HTTP server.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct ReadyResponse {
    /// Service status.
    pub status: String,
    /// Selected runtime role.
    pub server_role: String,
    /// Enabled runtime protocol frontends.
    pub server_frontends: Vec<String>,
    /// Selected metadata backend.
    pub metadata_backend: String,
    /// Selected immutable object-storage backend.
    pub object_backend: String,
    /// Selected reconstruction-cache backend.
    pub cache_backend: String,
}

/// Upload result for a single chunk.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct UploadChunkResult {
    /// Chunk hash in Xet CAS API hexadecimal ordering.
    pub hash: String,
    /// Byte offset inside the uploaded file.
    pub offset: u64,
    /// Chunk byte length.
    pub length: u64,
    /// Whether the upload inserted new chunk bytes.
    pub inserted: bool,
}

/// File upload response.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct UploadFileResponse {
    /// Uploaded file identifier.
    pub file_id: String,
    /// Immutable content identity for this uploaded file version.
    pub content_hash: String,
    /// Total uploaded byte length.
    pub total_bytes: u64,
    /// Server chunk size used for this upload.
    pub chunk_size: u64,
    /// Number of chunks inserted.
    pub inserted_chunks: u64,
    /// Number of chunks already present.
    pub reused_chunks: u64,
    /// Number of new bytes written to chunk storage.
    pub stored_bytes: u64,
    /// Ordered chunk upload results.
    pub chunks: Vec<UploadChunkResult>,
}

/// Storage stats response.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct ServerStatsResponse {
    /// Number of chunk objects stored.
    pub chunks: u64,
    /// Total bytes stored across chunk objects.
    pub chunk_bytes: u64,
    /// Number of file records stored.
    pub files: u64,
}

/// Provider-backed CAS token issuance request.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct ProviderTokenIssueRequest {
    /// Authenticated provider subject to authorize.
    pub subject: String,
    /// Repository owner or namespace.
    pub owner: String,
    /// Repository name.
    pub repo: String,
    /// Optional revision context. When omitted, the provider default revision is used.
    pub revision: Option<String>,
    /// Requested CAS scope.
    pub scope: TokenScope,
}

/// Provider-backed CAS token issuance response.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct ProviderTokenIssueResponse {
    /// Signed bearer token for subsequent CAS requests.
    pub token: SecretString,
    /// Issuer embedded into the token.
    pub issuer: String,
    /// Subject embedded into the token.
    pub subject: String,
    /// Repository hosting provider.
    pub provider: RepositoryProvider,
    /// Repository owner or namespace.
    pub owner: String,
    /// Repository name.
    pub repo: String,
    /// Scoped revision.
    pub revision: Option<String>,
    /// Granted CAS scope.
    pub scope: TokenScope,
    /// Token expiration timestamp as Unix seconds.
    pub expires_at_unix_seconds: u64,
}

/// Xet CAS access-token response consumed by reference clients.
#[derive(Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct XetCasTokenResponse {
    /// CAS server endpoint base URL.
    pub cas_url: String,
    /// Token expiration timestamp as Unix seconds.
    pub exp: u64,
    /// Signed bearer token for CAS requests.
    pub access_token: String,
}

impl fmt::Debug for XetCasTokenResponse {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("XetCasTokenResponse")
            .field("cas_url", &self.cas_url)
            .field("exp", &self.exp)
            .field("access_token", &"<redacted>")
            .finish()
    }
}

/// Git LFS authenticate response carrying Xet custom-transfer bootstrap headers.
#[derive(Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct GitLfsAuthenticateResponse {
    /// CAS endpoint URL.
    pub href: String,
    /// Headers consumed by the Xet custom transfer adapter.
    pub header: BTreeMap<String, String>,
    /// Relative token lifetime in seconds.
    pub expires_in: u64,
}

impl fmt::Debug for GitLfsAuthenticateResponse {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("GitLfsAuthenticateResponse")
            .field("href", &self.href)
            .field("header", &"<redacted>")
            .field("expires_in", &self.expires_in)
            .finish()
    }
}

/// OCI registry bearer-token exchange response.
#[derive(Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct OciRegistryTokenResponse {
    /// Bearer token returned by the registry token service.
    pub token: String,
    /// Duplicate bearer token field used by some clients.
    pub access_token: String,
    /// Relative token lifetime in seconds.
    pub expires_in: u64,
}

impl fmt::Debug for OciRegistryTokenResponse {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("OciRegistryTokenResponse")
            .field("token", &"<redacted>")
            .field("access_token", &"<redacted>")
            .field("expires_in", &self.expires_in)
            .finish()
    }
}

/// Provider webhook handling response.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct ProviderWebhookResponse {
    /// Repository hosting provider.
    pub provider: RepositoryProvider,
    /// Repository owner or namespace.
    pub owner: String,
    /// Repository name.
    pub repo: String,
    /// Provider delivery identifier.
    pub delivery_id: String,
    /// Normalized webhook event kind.
    pub event_kind: String,
    /// New repository owner or namespace when the event renamed the repository.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub new_owner: Option<String>,
    /// New repository name when the event renamed the repository.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub new_repo: Option<String>,
    /// Updated revision when the event described a push.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub revision: Option<String>,
    /// Number of affected immutable file-version records.
    pub affected_file_versions: u64,
    /// Number of distinct affected chunk objects.
    pub affected_chunks: u64,
    /// Number of retention holds inserted or refreshed by the event.
    pub applied_holds: u64,
    /// Retention applied to newly created holds, when the event mutated lifecycle state.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub retention_seconds: Option<u64>,
}

#[cfg(test)]
mod tests {
    use std::collections::BTreeMap;

    use shardline_protocol::{RepositoryProvider, SecretString, TokenScope};

    use super::*;

    fn health_response() -> HealthResponse {
        HealthResponse {
            status: "ok".to_owned(),
        }
    }

    fn ready_response() -> ReadyResponse {
        ReadyResponse {
            status: "ok".to_owned(),
            server_role: "all".to_owned(),
            server_frontends: vec!["xet".to_owned()],
            metadata_backend: "local".to_owned(),
            object_backend: "local".to_owned(),
            cache_backend: "memory".to_owned(),
        }
    }

    #[test]
    fn health_response_serde_round_trip() {
        let original = health_response();
        let json = serde_json::to_string(&original).unwrap();
        let deserialized: HealthResponse = serde_json::from_str(&json).unwrap();
        assert_eq!(original, deserialized);
        assert!(json.contains("ok"));
    }

    #[test]
    fn health_response_debug() {
        let response = health_response();
        let debug = format!("{response:?}");
        assert!(debug.contains("HealthResponse"));
        assert!(debug.contains("ok"));
    }

    #[test]
    fn ready_response_serde_round_trip() {
        let original = ready_response();
        let json = serde_json::to_string(&original).unwrap();
        let deserialized: ReadyResponse = serde_json::from_str(&json).unwrap();
        assert_eq!(original, deserialized);
    }

    #[test]
    fn ready_response_debug() {
        let response = ready_response();
        let debug = format!("{response:?}");
        assert!(debug.contains("ReadyResponse"));
    }

    #[test]
    fn upload_chunk_result_serde_round_trip() {
        let original = UploadChunkResult {
            hash: "abcdef123456".to_owned(),
            offset: 0,
            length: 4096,
            inserted: true,
        };
        let json = serde_json::to_string(&original).unwrap();
        let deserialized: UploadChunkResult = serde_json::from_str(&json).unwrap();
        assert_eq!(original, deserialized);
    }

    #[test]
    fn upload_file_response_serde_round_trip() {
        let original = UploadFileResponse {
            file_id: "test-file".to_owned(),
            content_hash: "content-hash-abc".to_owned(),
            total_bytes: 8192,
            chunk_size: 4096,
            inserted_chunks: 2,
            reused_chunks: 0,
            stored_bytes: 8192,
            chunks: vec![UploadChunkResult {
                hash: "chunk1".to_owned(),
                offset: 0,
                length: 4096,
                inserted: true,
            }],
        };
        let json = serde_json::to_string(&original).unwrap();
        let deserialized: UploadFileResponse = serde_json::from_str(&json).unwrap();
        assert_eq!(original, deserialized);
    }

    #[test]
    fn server_stats_response_serde_round_trip() {
        let original = ServerStatsResponse {
            chunks: 10,
            chunk_bytes: 40960,
            files: 5,
        };
        let json = serde_json::to_string(&original).unwrap();
        let deserialized: ServerStatsResponse = serde_json::from_str(&json).unwrap();
        assert_eq!(original, deserialized);
    }

    #[test]
    fn provider_token_issue_request_serde_round_trip() {
        let original = ProviderTokenIssueRequest {
            subject: "user".to_owned(),
            owner: "org".to_owned(),
            repo: "repo".to_owned(),
            revision: Some("main".to_owned()),
            scope: TokenScope::Read,
        };
        let json = serde_json::to_string(&original).unwrap();
        let deserialized: ProviderTokenIssueRequest = serde_json::from_str(&json).unwrap();
        assert_eq!(original, deserialized);
    }

    #[test]
    fn provider_token_issue_response_serde_round_trip() {
        let original = ProviderTokenIssueResponse {
            token: SecretString::from_secret("bearer-token"),
            issuer: "shardline".to_owned(),
            subject: "user".to_owned(),
            provider: RepositoryProvider::GitHub,
            owner: "org".to_owned(),
            repo: "repo".to_owned(),
            revision: Some("main".to_owned()),
            scope: TokenScope::Write,
            expires_at_unix_seconds: 1_700_000_000,
        };
        let json = serde_json::to_string(&original).unwrap();
        let deserialized: ProviderTokenIssueResponse = serde_json::from_str(&json).unwrap();
        assert_eq!(original, deserialized);
    }

    #[test]
    fn xet_cas_token_response_serde_round_trip() {
        let original = XetCasTokenResponse {
            cas_url: "http://cas.example.com".to_owned(),
            exp: 1_700_000_000,
            access_token: "access-token-value".to_owned(),
        };
        let json = serde_json::to_string(&original).unwrap();
        let deserialized: XetCasTokenResponse = serde_json::from_str(&json).unwrap();
        assert_eq!(original, deserialized);
        // camelCase serialization
        assert!(json.contains("casUrl"));
        assert!(json.contains("accessToken"));
    }

    #[test]
    fn git_lfs_authenticate_response_serde_round_trip() {
        let mut header = BTreeMap::new();
        header.insert("X-Custom".to_owned(), "value".to_owned());
        let original = GitLfsAuthenticateResponse {
            href: "http://lfs.example.com".to_owned(),
            header,
            expires_in: 3600,
        };
        let json = serde_json::to_string(&original).unwrap();
        let deserialized: GitLfsAuthenticateResponse = serde_json::from_str(&json).unwrap();
        assert_eq!(original, deserialized);
    }

    #[test]
    fn oci_registry_token_response_serde_round_trip() {
        let original = OciRegistryTokenResponse {
            token: "bearer-token".to_owned(),
            access_token: "access-token".to_owned(),
            expires_in: 300,
        };
        let json = serde_json::to_string(&original).unwrap();
        let deserialized: OciRegistryTokenResponse = serde_json::from_str(&json).unwrap();
        assert_eq!(original, deserialized);
    }

    #[test]
    fn provider_webhook_response_serde_round_trip() {
        let original = ProviderWebhookResponse {
            provider: RepositoryProvider::GitHub,
            owner: "org".to_owned(),
            repo: "repo".to_owned(),
            delivery_id: "delivery-123".to_owned(),
            event_kind: "revision_pushed".to_owned(),
            new_owner: None,
            new_repo: None,
            revision: Some("abc123".to_owned()),
            affected_file_versions: 5,
            affected_chunks: 20,
            applied_holds: 3,
            retention_seconds: Some(86_400),
        };
        let json = serde_json::to_string(&original).unwrap();
        let deserialized: ProviderWebhookResponse = serde_json::from_str(&json).unwrap();
        assert_eq!(original, deserialized);
    }

    #[test]
    fn provider_webhook_response_omits_optional_empty_fields() {
        let original = ProviderWebhookResponse {
            provider: RepositoryProvider::GitHub,
            owner: "org".to_owned(),
            repo: "repo".to_owned(),
            delivery_id: "delivery-123".to_owned(),
            event_kind: "push".to_owned(),
            new_owner: None,
            new_repo: None,
            revision: None,
            affected_file_versions: 0,
            affected_chunks: 0,
            applied_holds: 0,
            retention_seconds: None,
        };
        let json = serde_json::to_string(&original).unwrap();
        // Fields with skip_serializing_if should be absent
        assert!(
            !json.contains("\"new_owner\""),
            "unexpected new_owner: {json}"
        );
        assert!(
            !json.contains("\"new_repo\""),
            "unexpected new_repo: {json}"
        );
        assert!(
            !json.contains("\"revision\""),
            "unexpected revision: {json}"
        );
        assert!(
            !json.contains("\"retention_seconds\""),
            "unexpected retention_seconds: {json}"
        );
    }
}