@@ -28,9 +28,9 @@ function checkKnownCorrectRequestFail(href, headers, status, body) {
28
28
29
29
/**
30
30
* Returns `undefined` if no error, a string if an error does occur.
31
- * @param {(message?: string) } callback
31
+ * @param {(message?: string) => void } callback
32
32
*/
33
- function checkHttp ( href , callback ) {
33
+ function checkHttpInner ( href , callback ) {
34
34
// Prefer https: > http: where possible, but allow http: when https: is inaccessible.
35
35
36
36
const url = new URL ( href )
@@ -67,6 +67,39 @@ function checkHttp(href, callback) {
67
67
} )
68
68
}
69
69
70
+ // Kill the remaining duplication by using a global cache.
71
+ const urlCache = new Map ( )
72
+
73
+ /**
74
+ * Returns `undefined` if no error, a string if an error does occur.
75
+ * @param {(message?: string) => void } callback
76
+ */
77
+ function checkHttp ( href , callback ) {
78
+ if ( href . includes ( "#" ) ) {
79
+ process . exitCode = 1
80
+ callback ( `Expected href to be sanitized of hashes, but found ${ href } ` )
81
+ }
82
+
83
+ if ( urlCache . has ( href ) ) {
84
+ const message = urlCache . get ( href )
85
+ if ( Array . isArray ( message ) ) {
86
+ message . push ( callback )
87
+ } else {
88
+ process . nextTick ( callback , message )
89
+ }
90
+ } else {
91
+ const queue = [ ]
92
+ urlCache . set ( href , queue )
93
+ checkHttpInner ( href , ( message ) => {
94
+ urlCache . set ( href , message )
95
+ process . nextTick ( callback , message )
96
+ for ( const callback of queue ) {
97
+ process . nextTick ( callback , message )
98
+ }
99
+ } )
100
+ }
101
+ }
102
+
70
103
module . exports = {
71
104
checkHttp,
72
105
}
0 commit comments