@@ -4,7 +4,9 @@ import * as NodeHttpPlatform from "@effect/platform-node/NodeHttpPlatform";
44import * as NodeFSP from "node:fs/promises" ;
55import {
66 AssetAccessError ,
7+ AssetCreateUrlInput ,
78 AssetPreviewTypeValidationError ,
9+ SOURCE_CONTROL_MEDIA_ASSET ,
810 ThreadId ,
911 TurnItemId ,
1012} from "@t3tools/contracts" ;
@@ -15,12 +17,11 @@ import * as DateTime from "effect/DateTime";
1517import * as Effect from "effect/Effect" ;
1618import * as FileSystem from "effect/FileSystem" ;
1719import * as Layer from "effect/Layer" ;
18- import * as Redacted from "effect/Redacted" ;
1920import * as Path from "effect/Path" ;
2021import * as PlatformError from "effect/PlatformError" ;
2122import * as Schema from "effect/Schema" ;
2223import * as TestClock from "effect/testing/TestClock" ;
23- import { HttpClient , HttpClientResponse , HttpServerResponse } from "effect/http" ;
24+ import { HttpClientRequest , HttpClientResponse , HttpServerResponse } from "effect/http" ;
2425import { vi } from "vite-plus/test" ;
2526
2627import * as ServerSecretStore from "../auth/ServerSecretStore.ts" ;
@@ -33,8 +34,10 @@ import { assetFileResponse } from "../http.ts";
3334import { ASSET_ROUTE_PREFIX , issueAssetUrl , readMcpAppHtml , resolveAsset } from "./AssetAccess.ts" ;
3435import * as NativeAppIconResolver from "./NativeAppIconResolver.ts" ;
3536import { symlinksSupported } from "@t3tools/shared/testing/symlinks" ;
36- import * as GitHubCredentials from "@t3tools/source-control-github/server/GitHubCredentials" ;
37- import { githubMediaResponse } from "./GitHubMediaFetch.ts" ;
37+ import type { SourceControlDriverInstance } from "@t3tools/source-control-core/server/driver" ;
38+ import * as BuiltInDrivers from "../sourceControl/builtInDrivers.ts" ;
39+ import { sourceControlClients } from "../sourceControl/sourceControlClients.ts" ;
40+ import { sourceControlMediaResponse } from "./sourceControlMediaResponse.ts" ;
3841import * as HostProcess from "@t3tools/shared/HostProcess" ;
3942
4043vi . mock ( "node:fs/promises" , async ( importOriginal ) => {
@@ -178,53 +181,6 @@ describe("AssetAccess", () => {
178181 ) ;
179182 } ) ;
180183
181- it . effect ( "loads private media immediately after login with the GitHub credential" , ( ) => {
182- let lookups = 0 ;
183- const authorizations : Array < string | undefined > = [ ] ;
184- return Effect . gen ( function * ( ) {
185- const asset = {
186- url : "https://raw.githubusercontent.com/owner/repo/main/shot.png" ,
187- cwd : "/repo" ,
188- expiresAt : Number . MAX_SAFE_INTEGER ,
189- } ;
190- expect ( ( yield * githubMediaResponse ( asset , { } ) ) . status ) . toBe ( 404 ) ;
191- expect ( ( yield * githubMediaResponse ( asset , { } ) ) . status ) . toBe ( 200 ) ;
192- expect ( ( yield * githubMediaResponse ( asset , { } ) ) . status ) . toBe ( 200 ) ;
193- expect ( lookups ) . toBe ( 3 ) ;
194- expect ( authorizations ) . toEqual ( [ undefined , "Bearer signed-in" , "Bearer signed-in" ] ) ;
195- } ) . pipe (
196- Effect . provide (
197- Layer . mock ( GitHubCredentials . GitHubCredentials ) ( {
198- get : ( host ) =>
199- ++ lookups === 1
200- ? Effect . fail ( new GitHubCredentials . GitHubNotSignedInError ( { host } ) )
201- : Effect . succeed ( {
202- host,
203- token : Redacted . make ( "signed-in" ) ,
204- source : "gh" as const ,
205- fingerprint : "fingerprint" ,
206- } ) ,
207- } ) ,
208- ) ,
209- Effect . provideService (
210- HttpClient . HttpClient ,
211- HttpClient . make ( ( request ) => {
212- authorizations . push ( request . headers . authorization ) ;
213- return Effect . succeed (
214- HttpClientResponse . fromWeb (
215- request ,
216- new Response ( null , {
217- status : request . headers . authorization ? 200 : 404 ,
218- headers : { "content-type" : "image/png" } ,
219- } ) ,
220- ) ,
221- ) ;
222- } ) ,
223- ) ,
224- Effect . scoped ,
225- ) ;
226- } ) ;
227-
228184 it . effect ( "serves an image a tool returned inline from the stored item" , ( ) =>
229185 Effect . gen ( function * ( ) {
230186 const resource = {
@@ -994,51 +950,66 @@ describe("AssetAccess", () => {
994950 } ) . pipe ( Effect . provide ( layerTest ) ) ,
995951 ) ;
996952
997- it . effect ( "serves GitHub-hosted pull request media through the repository's credential" , ( ) =>
998- Effect . gen ( function * ( ) {
999- const resolve = ( relativeUrl : string ) => {
1000- const suffix = relativeUrl . slice ( `${ ASSET_ROUTE_PREFIX } /` . length ) ;
1001- const separator = suffix . indexOf ( "/" ) ;
1002- return resolveAsset ( suffix . slice ( 0 , separator ) , suffix . slice ( separator + 1 ) ) ;
1003- } ;
1004- const issue = ( url : string ) =>
1005- issueAssetUrl ( { resource : { _tag : "github-media" , cwd : "/repo" , url } } ) ;
1006-
1007- const attachment = yield * issue (
1008- "https://github.com/user-attachments/assets/1a1842fb-6383-492f-873c-57aa0033fa6c" ,
1009- ) ;
1010- expect ( attachment . relativeUrl . endsWith ( "/1a1842fb-6383-492f-873c-57aa0033fa6c" ) ) . toBe ( true ) ;
1011- expect ( yield * resolve ( attachment . relativeUrl ) ) . toEqual ( {
1012- kind : "github-media" ,
1013- url : "https://github.com/user-attachments/assets/1a1842fb-6383-492f-873c-57aa0033fa6c" ,
1014- cwd : "/repo" ,
1015- expiresAt : attachment . expiresAt ,
953+ it . effect ( "serves a host's credentialed media through that host's driver" , ( ) => {
954+ const fetched : Array < { url : string ; headers : Readonly < Record < string , string > > } > = [ ] ;
955+ const url = "https://github.com/owner/repo/blob/main/docs/shot.png" ;
956+ const media = sourceControlClients . mediaUrl ( url ) ! ;
957+ const layerDrivers = Layer . succeed ( BuiltInDrivers . SourceControlDrivers , [
958+ {
959+ driver : { kind : media . definition . kind , definition : media . definition } ,
960+ instance : {
961+ sourceControl : { } as SourceControlDriverInstance [ "sourceControl" ] ,
962+ discovery : { } as SourceControlDriverInstance [ "discovery" ] ,
963+ pullRequests : null ,
964+ fetchMedia : ( request ) =>
965+ Effect . sync ( ( ) => {
966+ fetched . push ( request ) ;
967+ return HttpClientResponse . fromWeb (
968+ HttpClientRequest . get ( request . url ) ,
969+ new Response ( "bytes" , { status : 206 , headers : { "content-length" : "5" } } ) ,
970+ ) ;
971+ } ) ,
972+ } ,
973+ } ,
974+ ] ) ;
975+ return Effect . gen ( function * ( ) {
976+ // Every client and server release spells the resource this way on the wire.
977+ const { resource } = yield * Schema . decodeUnknownEffect ( AssetCreateUrlInput ) ( {
978+ resource : { _tag : "github-media" , cwd : "/repo" , url } ,
1016979 } ) ;
1017-
1018- const committed = yield * issue ( "https://github.com/owner/repo/blob/main/docs/shot.png" ) ;
1019- expect ( yield * resolve ( committed . relativeUrl ) ) . toMatchObject ( {
1020- url : "https://raw.githubusercontent.com/owner/repo/main/docs/shot.png" ,
980+ const issued = yield * issueAssetUrl ( { resource } ) ;
981+ const suffix = issued . relativeUrl . slice ( `${ ASSET_ROUTE_PREFIX } /` . length ) ;
982+ const separator = suffix . indexOf ( "/" ) ;
983+ const asset = yield * resolveAsset ( suffix . slice ( 0 , separator ) , suffix . slice ( separator + 1 ) ) ;
984+ expect ( asset ) . toEqual ( {
985+ kind : "source-control-media" ,
986+ url : media . fetchUrl ,
987+ cwd : "/repo" ,
988+ expiresAt : issued . expiresAt ,
1021989 } ) ;
990+ if ( asset ?. kind !== "source-control-media" ) return ;
1022991
1023- const legacy = yield * issue ( "https://github.com/owner/repo/assets/45952064/1a1842fb" ) ;
1024- expect ( yield * resolve ( legacy . relativeUrl ) ) . toMatchObject ( {
1025- url : "https://github.com/owner/repo/assets/45952064/1a1842fb" ,
1026- } ) ;
1027- const lfs = yield * issue ( "https://media.githubusercontent.com/media/owner/repo/main/a.mp4" ) ;
1028- expect ( yield * resolve ( lfs . relativeUrl ) ) . toMatchObject ( {
1029- url : "https://media.githubusercontent.com/media/owner/repo/main/a.mp4" ,
992+ const response = yield * sourceControlMediaResponse ( asset , {
993+ range : "bytes=0-4" ,
994+ cookie : "session" ,
1030995 } ) ;
1031- const awkward = yield * issue ( "https://raw.githubusercontent.com/o/r/main/100%.png" ) ;
1032- expect ( awkward . relativeUrl . endsWith ( "/100%25.png" ) ) . toBe ( true ) ;
996+ expect ( fetched ) . toEqual ( [ { url : media . fetchUrl , headers : { range : "bytes=0-4" } } ] ) ;
997+ expect ( response . status ) . toBe ( 206 ) ;
998+ // The upstream sent no type, so it comes from the file name the host gave the media.
999+ expect ( response . headers [ "content-type" ] ) . toBe ( "image/png" ) ;
1000+ expect ( response . headers [ "content-length" ] ) . toBe ( "5" ) ;
1001+ } ) . pipe ( Effect . provide ( Layer . merge ( layerTest , layerDrivers ) ) , Effect . scoped ) ;
1002+ } ) ;
10331003
1004+ it . effect ( "refuses media no host keeps behind a credential, without echoing the URL" , ( ) =>
1005+ Effect . gen ( function * ( ) {
10341006 for ( const url of [
10351007 "https://example.com/shot.png" ,
10361008 "https://example.com/shot.png?token=private-media-token" ,
1037- "http://github.com/user-attachments/assets/1a1842fb" ,
1038- "https://github.com/owner/repo/pull/1" ,
1039- "https://github.com/owner/repo/blob/main/" ,
10401009 ] ) {
1041- const error = yield * issue ( url ) . pipe ( Effect . flip ) ;
1010+ const error = yield * issueAssetUrl ( {
1011+ resource : { _tag : SOURCE_CONTROL_MEDIA_ASSET , cwd : "/repo" , url } ,
1012+ } ) . pipe ( Effect . flip ) ;
10421013 expect ( error . _tag ) . toBe ( "AssetGitHubMediaUrlValidationError" ) ;
10431014 const encoded = yield * Schema . encodeEffect ( Schema . fromJsonString ( AssetAccessError ) ) ( error ) ;
10441015 expect ( encoded ) . not . toContain ( url ) ;
0 commit comments