11import * as Effect from "effect/Effect" ;
22import * as Schema from "effect/Schema" ;
33
4+ import { getUrlDiagnostics } from "@t3tools/shared/urlDiagnostics" ;
5+
46export interface UpdateManifestFile {
57 readonly url : string ;
68 readonly sha512 : string ;
@@ -10,6 +12,9 @@ export interface UpdateManifestFile {
1012export const UpdateManifestScalar = Schema . Union ( [ Schema . String , Schema . Number , Schema . Boolean ] ) ;
1113export type UpdateManifestScalar = typeof UpdateManifestScalar . Type ;
1214
15+ export const UpdateManifestScalarType = Schema . Literals ( [ "string" , "number" , "boolean" ] ) ;
16+ export type UpdateManifestScalarType = typeof UpdateManifestScalarType . Type ;
17+
1318export const UpdateManifestParseReason = Schema . Literals ( [
1419 "incomplete file entry" ,
1520 "sha512 without a file entry" ,
@@ -59,30 +64,40 @@ export class UpdateManifestExtraConflictError extends Schema.TaggedErrorClass<Up
5964 {
6065 platformLabel : Schema . String ,
6166 key : Schema . String ,
62- primaryValue : UpdateManifestScalar ,
63- secondaryValue : UpdateManifestScalar ,
67+ primaryValueType : UpdateManifestScalarType ,
68+ primaryValueLength : Schema . optionalKey ( Schema . Number ) ,
69+ secondaryValueType : UpdateManifestScalarType ,
70+ secondaryValueLength : Schema . optionalKey ( Schema . Number ) ,
6471 } ,
6572) {
6673 override get message ( ) : string {
67- return `Cannot merge ${ this . platformLabel } update manifests: conflicting '${ this . key } ' values (' ${ this . primaryValue } ' vs ' ${ this . secondaryValue } ') .` ;
74+ return `Cannot merge ${ this . platformLabel } update manifests: conflicting '${ this . key } ' ${ this . primaryValueType } and ${ this . secondaryValueType } values .` ;
6875 }
6976}
7077
7178export class UpdateManifestFileConflictError extends Schema . TaggedErrorClass < UpdateManifestFileConflictError > ( ) (
7279 "UpdateManifestFileConflictError" ,
7380 {
7481 platformLabel : Schema . String ,
75- url : Schema . String ,
82+ urlInputLength : Schema . Number ,
83+ urlProtocol : Schema . optionalKey ( Schema . String ) ,
84+ urlHostname : Schema . optionalKey ( Schema . String ) ,
7685 existingManifest : Schema . Literals ( [ "primary" , "secondary" ] ) ,
77- existingSha512 : Schema . String ,
86+ existingSha512Length : Schema . Number ,
7887 existingSize : Schema . Number ,
7988 conflictingManifest : Schema . Literals ( [ "primary" , "secondary" ] ) ,
80- conflictingSha512 : Schema . String ,
89+ conflictingSha512Length : Schema . Number ,
8190 conflictingSize : Schema . Number ,
91+ sha512Conflict : Schema . Boolean ,
92+ sizeConflict : Schema . Boolean ,
8293 } ,
8394) {
8495 override get message ( ) : string {
85- return `Cannot merge ${ this . platformLabel } update manifests: conflicting file entry for ${ this . url } .` ;
96+ const origin =
97+ this . urlProtocol === undefined || this . urlHostname === undefined
98+ ? ""
99+ : ` at ${ this . urlProtocol } //${ this . urlHostname } ` ;
100+ return `Cannot merge ${ this . platformLabel } update manifests: conflicting file entry${ origin } (URL input length: ${ this . urlInputLength } ).` ;
86101 }
87102}
88103
@@ -327,6 +342,12 @@ export function parseUpdateManifest(
327342 } ;
328343}
329344
345+ function getScalarType ( value : UpdateManifestScalar ) : UpdateManifestScalarType {
346+ if ( typeof value === "string" ) return "string" ;
347+ if ( typeof value === "number" ) return "number" ;
348+ return "boolean" ;
349+ }
350+
330351function mergeExtras (
331352 primary : Readonly < Record < string , UpdateManifestScalar > > ,
332353 secondary : Readonly < Record < string , UpdateManifestScalar > > ,
@@ -340,8 +361,10 @@ function mergeExtras(
340361 throw new UpdateManifestExtraConflictError ( {
341362 platformLabel,
342363 key,
343- primaryValue : existing ,
344- secondaryValue : value ,
364+ primaryValueType : getScalarType ( existing ) ,
365+ ...( typeof existing === "string" ? { primaryValueLength : existing . length } : { } ) ,
366+ secondaryValueType : getScalarType ( value ) ,
367+ ...( typeof value === "string" ? { secondaryValueLength : value . length } : { } ) ,
345368 } ) ;
346369 }
347370 merged [ key ] = value ;
@@ -374,15 +397,24 @@ export function mergeUpdateManifests(
374397 for ( const file of files ) {
375398 const existing = filesByUrl . get ( file . url ) ;
376399 if ( existing && ( existing . file . sha512 !== file . sha512 || existing . file . size !== file . size ) ) {
400+ const urlDiagnostics = getUrlDiagnostics ( file . url ) ;
377401 throw new UpdateManifestFileConflictError ( {
378402 platformLabel,
379- url : file . url ,
403+ urlInputLength : urlDiagnostics . inputLength ,
404+ ...( urlDiagnostics . protocol === undefined
405+ ? { }
406+ : { urlProtocol : urlDiagnostics . protocol } ) ,
407+ ...( urlDiagnostics . hostname === undefined
408+ ? { }
409+ : { urlHostname : urlDiagnostics . hostname } ) ,
380410 existingManifest : existing . manifest ,
381- existingSha512 : existing . file . sha512 ,
411+ existingSha512Length : existing . file . sha512 . length ,
382412 existingSize : existing . file . size ,
383413 conflictingManifest : manifest ,
384- conflictingSha512 : file . sha512 ,
414+ conflictingSha512Length : file . sha512 . length ,
385415 conflictingSize : file . size ,
416+ sha512Conflict : existing . file . sha512 !== file . sha512 ,
417+ sizeConflict : existing . file . size !== file . size ,
386418 } ) ;
387419 }
388420 filesByUrl . set ( file . url , { manifest, file } ) ;
0 commit comments