@@ -13,6 +13,7 @@ import {
1313import { mintNodeWorkspaceTransferToken } from "./node-workspace-transfer-token.js" ;
1414import {
1515 MAX_UPLOAD_BYTES ,
16+ NodeWorkspaceTransferInvalidError ,
1617 NodeWorkspaceTransferLimitError ,
1718 RequestByteReader ,
1819 streamUploadFile ,
@@ -34,7 +35,10 @@ import {
3435} from "./workspace-reconcile.js" ;
3536import { workerWorkspaceTransferPaths } from "./workspace-result-staging.js" ;
3637
37- export { isNodeWorkspaceTransferLimitError } from "./node-workspace-upload-reader.js" ;
38+ export {
39+ isNodeWorkspaceTransferLimitError ,
40+ nodeWorkspaceTransferInvalidReason ,
41+ } from "./node-workspace-upload-reader.js" ;
3842
3943const TRANSFER_TIMEOUT_MS = 10 * 60_000 ;
4044const MANIFEST_REF_PATTERN = / ^ s h a 2 5 6 : [ a - f 0 - 9 ] { 64 } $ / u;
@@ -599,13 +603,30 @@ export function createNodeWorkspaceTransferService(options: {
599603 }
600604 const raw = ( await reader . readExactly ( bytes ) ) . toString ( "utf8" ) ;
601605 const ref = expectedRef ?? `sha256:${ createHash ( "sha256" ) . update ( raw ) . digest ( "hex" ) } ` ;
602- return { raw, ref, manifest : parseWorkerWorkspaceManifest ( raw , ref ) } ;
606+ try {
607+ return { raw, ref, manifest : parseWorkerWorkspaceManifest ( raw , ref ) } ;
608+ } catch ( error ) {
609+ throw new NodeWorkspaceTransferInvalidError (
610+ "manifest" ,
611+ "Workspace transfer manifest is invalid" ,
612+ { cause : error } ,
613+ ) ;
614+ }
603615 } ;
604616 const base = await readManifest ( operation . baseManifestRef ) ;
605617 assertCurrent ( ) ;
606618 const current = await readManifest ( ) ;
607619 assertCurrent ( ) ;
608- const transferPaths = workerWorkspaceTransferPaths ( current . manifest , base . manifest ) ;
620+ let transferPaths : string [ ] ;
621+ try {
622+ transferPaths = workerWorkspaceTransferPaths ( current . manifest , base . manifest ) ;
623+ } catch ( error ) {
624+ throw new NodeWorkspaceTransferInvalidError (
625+ "manifest" ,
626+ "Workspace transfer manifests cannot be reconciled" ,
627+ { cause : error } ,
628+ ) ;
629+ }
609630 const transferPathSet = new Set ( transferPaths ) ;
610631 stagingRoot = await fsp . mkdtemp ( path . join ( authorization . context . temporaryRoot , "upload-" ) ) ;
611632 const currentByPath = new Map ( current . manifest . entries . map ( ( entry ) => [ entry . path , entry ] ) ) ;
@@ -614,32 +635,57 @@ export function createNodeWorkspaceTransferService(options: {
614635 if ( ! entry ) {
615636 continue ;
616637 }
617- const destination = entryPath ( stagingRoot , relative ) ;
618- await fsp . mkdir ( path . dirname ( destination ) , { recursive : true , mode : 0o700 } ) ;
619- assertCurrent ( ) ;
620- if ( entry . type === "symlink" ) {
621- await fsp . symlink ( entry . target , destination ) ;
638+ try {
639+ const destination = entryPath ( stagingRoot , relative ) ;
640+ await fsp . mkdir ( path . dirname ( destination ) , { recursive : true , mode : 0o700 } ) ;
622641 assertCurrent ( ) ;
623- } else {
624- const handle = await fsp . open ( destination , "wx" , entry . mode ) ;
625- try {
626- await streamUploadFile ( { reader, handle, entry, assertCurrent } ) ;
627- } finally {
628- await handle . close ( ) ;
642+ if ( entry . type === "symlink" ) {
643+ await fsp . symlink ( entry . target , destination ) ;
644+ assertCurrent ( ) ;
645+ } else {
646+ const handle = await fsp . open ( destination , "wx" , entry . mode ) ;
647+ try {
648+ await streamUploadFile ( { reader, handle, entry, assertCurrent } ) ;
649+ } finally {
650+ await handle . close ( ) ;
651+ }
652+ assertCurrent ( ) ;
629653 }
630- assertCurrent ( ) ;
654+ } catch ( error ) {
655+ if ( error instanceof NodeWorkspaceTransferInvalidError ) {
656+ throw error ;
657+ }
658+ if ( params . signal . aborted || ! authorizationCurrent ( authorization ) ) {
659+ throw error ;
660+ }
661+ throw new NodeWorkspaceTransferInvalidError (
662+ "staging" ,
663+ "Workspace transfer payload could not be staged" ,
664+ { cause : error } ,
665+ ) ;
631666 }
632667 }
633668 await reader . assertEnd ( ) ;
634669 assertCurrent ( ) ;
635670 if ( reader . bytesRead !== contentLength ) {
636- throw new Error ( "Workspace transfer upload length is inconsistent" ) ;
671+ throw new NodeWorkspaceTransferInvalidError (
672+ "content_length" ,
673+ "Workspace transfer upload length is inconsistent" ,
674+ ) ;
675+ }
676+ try {
677+ await assertWorkspaceMatchesManifest ( {
678+ root : stagingRoot ,
679+ manifest : current . manifest ,
680+ entries : current . manifest . entries . filter ( ( entry ) => transferPathSet . has ( entry . path ) ) ,
681+ } ) ;
682+ } catch ( error ) {
683+ throw new NodeWorkspaceTransferInvalidError (
684+ "staging" ,
685+ "Workspace transfer payload did not match its staged result" ,
686+ { cause : error } ,
687+ ) ;
637688 }
638- await assertWorkspaceMatchesManifest ( {
639- root : stagingRoot ,
640- manifest : current . manifest ,
641- entries : current . manifest . entries . filter ( ( entry ) => transferPathSet . has ( entry . path ) ) ,
642- } ) ;
643689 assertCurrent ( ) ;
644690 operation . uploaded = {
645691 base : base . manifest ,
0 commit comments