@@ -63,33 +63,52 @@ async function loadOutline(url_str: string):Promise<OpmlData> {
63
63
64
64
retVal . messages . push ( "Fetched url in " + ( Date . now ( ) - start ) + "ms." ) ;
65
65
retVal . messages . push (
66
- `Content length: ${ xml_resp . headers . get ( "Content-Length" ) } `
66
+ `Content length (from header) : ${ xml_resp . headers . get ( "Content-Length" ) } `
67
67
) ;
68
- const contentType = xml_resp . headers . get ( "Content-Type" ) ;
68
+ let contentType = xml_resp . headers . get ( "Content-Type" ) ;
69
69
retVal . messages . push ( `Content type: ${ contentType || "(null)" } ` ) ;
70
70
if ( ! contentType ) {
71
- retVal . errorCount ++ ;
72
71
retVal . messages . push ( "No content type provided." ) ;
73
- return retVal ;
72
+ contentType = "" ;
74
73
}
74
+
75
+ let isXml :boolean ;
75
76
if (
76
- ! contentType . startsWith ( "text/xml" ) &&
77
- ! contentType . startsWith ( "application/xml" ) &&
78
- ! contentType . startsWith ( "text/plain " )
77
+ contentType . startsWith ( "text/xml" ) &&
78
+ contentType . startsWith ( "application/xml" ) &&
79
+ contentType . startsWith ( "text/x-opml " )
79
80
) {
81
+ isXml = true ;
82
+ } else if (
83
+ contentType . startsWith ( "text/plain" ) ||
84
+ contentType . startsWith ( "application/octet-stream" ) ||
85
+ contentType == ""
86
+ ) {
87
+ isXml = false ;
88
+ retVal . messages . push ( "Content type is not XML." ) ;
89
+ } else {
80
90
retVal . errorCount ++ ;
81
91
retVal . messages . push ( "Invalid content type: " + contentType ) ;
82
92
return retVal ;
83
93
}
84
94
85
- const xml_str = await xml_resp . text ( ) ;
95
+ let xml_str :string ;
96
+ try {
97
+ xml_str = await xml_resp . text ( ) ;
98
+ } catch ( err : unknown ) {
99
+ retVal . errorCount ++ ;
100
+ retVal . messages . push ( "Unable to get text: " + errorMessage ( err ) ) ;
101
+ return retVal ;
102
+ }
86
103
87
- if ( contentType . startsWith ( "text/plain" ) ) {
104
+ if ( isXml == false ) {
88
105
if ( xml_str . trim ( ) . startsWith ( "<" ) ) {
89
- retVal . messages . push ( "Content type is text/plain but starts with '<'. Parsing as XML." ) ;
106
+ retVal . messages . push ( "Content starts with '<'. Attempting to parse as XML." ) ;
90
107
} else {
91
108
retVal . errorCount ++ ;
92
- retVal . messages . push ( "Content type is text/plain and does not start with '<'. Not parsing." ) ;
109
+ retVal . messages . push (
110
+ "Content does not start with '<'. Not parsing."
111
+ ) ;
93
112
return retVal ;
94
113
}
95
114
}
@@ -111,17 +130,22 @@ async function loadOutline(url_str: string):Promise<OpmlData> {
111
130
return retVal ;
112
131
}
113
132
114
- retVal . messages . push ( `Entries: ${ retVal . count } ` ) ;
115
- retVal . messages . push ( `Parsing complete in ${ Date . now ( ) - start } ms.` ) ;
133
+ retVal . messages . push ( `XML parsing complete in ${ Date . now ( ) - start } ms.` ) ;
116
134
117
135
let xmlTitle = xml_data . opml ?. head ?. title ;
118
136
if ( xmlTitle && xmlTitle . indexOf ( "&" ) >= 0 ) {
119
137
xmlTitle = he . decode ( xmlTitle ) ;
120
138
}
121
139
retVal . title = xmlTitle || "" ;
122
140
141
+ const outlineStart = Date . now ( ) ;
123
142
processNode ( retVal , retVal . root , xml_data . opml . body . outline as OpmlOutline [ ] ) ;
124
143
144
+ retVal . messages . push ( `Entries: ${ retVal . count } ` ) ;
145
+ retVal . messages . push ( `Outline parsing complete in ${ Date . now ( ) - outlineStart } ms.` ) ;
146
+
147
+ retVal . messages . push ( `Total time: ${ Date . now ( ) - start } ms.` ) ;
148
+
125
149
retVal . success = retVal . errorCount === 0 ;
126
150
127
151
return retVal ;
0 commit comments