reffy 20.0.13 → 20.0.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -21
- package/README.md +151 -151
- package/index.js +29 -29
- package/package.json +5 -5
- package/reffy.js +324 -324
- package/schemas/browserlib/extract-algorithms.json +52 -52
- package/schemas/browserlib/extract-cssdfn.json +108 -108
- package/schemas/browserlib/extract-dfns.json +90 -90
- package/schemas/browserlib/extract-elements.json +17 -17
- package/schemas/browserlib/extract-events.json +31 -31
- package/schemas/browserlib/extract-headings.json +19 -19
- package/schemas/browserlib/extract-ids.json +7 -7
- package/schemas/browserlib/extract-links.json +12 -12
- package/schemas/browserlib/extract-refs.json +12 -12
- package/schemas/common.json +876 -876
- package/schemas/files/extracts/algorithms.json +12 -12
- package/schemas/files/extracts/css.json +16 -16
- package/schemas/files/extracts/dfns.json +12 -12
- package/schemas/files/extracts/elements.json +12 -12
- package/schemas/files/extracts/events.json +12 -12
- package/schemas/files/extracts/headings.json +12 -12
- package/schemas/files/extracts/ids.json +12 -12
- package/schemas/files/extracts/links.json +12 -12
- package/schemas/files/extracts/refs.json +12 -12
- package/schemas/files/index.json +59 -59
- package/schemas/postprocessing/events.json +50 -50
- package/schemas/postprocessing/idlnames-parsed.json +27 -27
- package/schemas/postprocessing/idlnames.json +17 -17
- package/schemas/postprocessing/idlparsed.json +67 -67
- package/src/browserlib/clone-and-clean.mjs +24 -24
- package/src/browserlib/create-outline.mjs +353 -353
- package/src/browserlib/extract-algorithms.mjs +723 -723
- package/src/browserlib/extract-cddl.mjs +125 -125
- package/src/browserlib/extract-dfns.mjs +1093 -1093
- package/src/browserlib/extract-headings.mjs +76 -76
- package/src/browserlib/extract-ids.mjs +28 -28
- package/src/browserlib/extract-links.mjs +45 -45
- package/src/browserlib/extract-references.mjs +308 -308
- package/src/browserlib/extract-webidl.mjs +89 -89
- package/src/browserlib/get-absolute-url.mjs +29 -29
- package/src/browserlib/get-code-elements.mjs +20 -20
- package/src/browserlib/get-generator.mjs +26 -26
- package/src/browserlib/get-lastmodified-date.mjs +13 -13
- package/src/browserlib/get-revision.mjs +12 -12
- package/src/browserlib/get-title.mjs +14 -14
- package/src/browserlib/informative-selector.mjs +24 -24
- package/src/browserlib/map-ids-to-headings.mjs +173 -173
- package/src/browserlib/reffy.json +85 -85
- package/src/browserlib/trim-spaces.mjs +35 -35
- package/src/cli/check-missing-dfns.js +587 -587
- package/src/cli/merge-crawl-results.js +132 -132
- package/src/cli/parse-webidl.js +447 -447
- package/src/lib/css-grammar-parse-tree.schema.json +109 -109
- package/src/lib/css-grammar-parser.js +440 -440
- package/src/lib/fetch.js +51 -51
- package/src/lib/markdown-report.js +360 -360
- package/src/lib/mock-server.js +218 -218
- package/src/lib/post-processor.js +322 -322
- package/src/lib/throttled-queue.js +129 -129
- package/src/postprocessing/annotate-links.js +41 -41
- package/src/postprocessing/csscomplete.js +48 -48
- package/src/postprocessing/idlnames.js +391 -391
- package/src/postprocessing/idlparsed.js +179 -179
- package/src/postprocessing/patch-dfns.js +51 -51
- package/src/specs/missing-css-rules.json +197 -197
- package/src/specs/spec-equivalents.json +149 -149
- package/src/browserlib/extract-editors.mjs~ +0 -14
- package/src/browserlib/extract-events.mjs~ +0 -3
- package/src/browserlib/generate-es-dfn-report.sh~ +0 -4
- package/src/browserlib/get-revision.mjs~ +0 -7
- package/src/cli/csstree-grammar-check.js +0 -28
- package/src/cli/csstree-grammar-check.js~ +0 -10
- package/src/cli/csstree-grammar-parser.js +0 -11
- package/src/cli/csstree-grammar-parser.js~ +0 -1
- package/src/cli/extract-editors.js~ +0 -38
- package/src/cli/process-specs.js~ +0 -28
- package/src/postprocessing/annotate-links.js~ +0 -8
- package/src/postprocessing/events.js~ +0 -245
package/src/lib/mock-server.js
CHANGED
|
@@ -1,218 +1,218 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Setup a proxy server that intercepts some network requests. To be used in
|
|
3
|
-
* tests not to hit the network.
|
|
4
|
-
*
|
|
5
|
-
* @module mock-server
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
import { MockAgent, setGlobalDispatcher } from 'undici';
|
|
9
|
-
import path from 'node:path';
|
|
10
|
-
import { existsSync, readFileSync } from 'node:fs';
|
|
11
|
-
import { fileURLToPath } from 'node:url';
|
|
12
|
-
|
|
13
|
-
const scriptPath = path.dirname(fileURLToPath(import.meta.url));
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* Determine the path to the "node_modules" folder. The path depends on whether
|
|
17
|
-
* Reffy is run directly, or installed as a library.
|
|
18
|
-
*
|
|
19
|
-
* @function
|
|
20
|
-
* @return {String} Path to the node_modules folder.
|
|
21
|
-
*/
|
|
22
|
-
function getModulesFolder() {
|
|
23
|
-
const rootFolder = path.resolve(scriptPath, '../..');
|
|
24
|
-
let folder = path.resolve(rootFolder, 'node_modules');
|
|
25
|
-
if (existsSync(folder)) {
|
|
26
|
-
return folder;
|
|
27
|
-
}
|
|
28
|
-
folder = path.resolve(rootFolder, '..');
|
|
29
|
-
return folder;
|
|
30
|
-
}
|
|
31
|
-
const modulesFolder = getModulesFolder();
|
|
32
|
-
|
|
33
|
-
const mockSpecs = {
|
|
34
|
-
"/woff/woff2/": {
|
|
35
|
-
html: `
|
|
36
|
-
<title>WOFF2</title>
|
|
37
|
-
<body>
|
|
38
|
-
<dfn id='foo' data-dfn-type="dfn">Foo</dfn>
|
|
39
|
-
<a href="https://www.w3.org/TR/bar/#baz">bar</a>
|
|
40
|
-
<ul class='toc'><li><a href='page.html'>page</a></ul>`,
|
|
41
|
-
pages: {
|
|
42
|
-
"page.html": `<h2 id='bar'>Heading in subpage</h2>`
|
|
43
|
-
}
|
|
44
|
-
},
|
|
45
|
-
"/mediacapture-output/": `
|
|
46
|
-
<script>respecConfig = { shortName: 'test' };</script>
|
|
47
|
-
<script src='https://www.w3.org/Tools/respec/respec-w3c'></script>
|
|
48
|
-
<div id=abstract></div>
|
|
49
|
-
<pre class='idl'>[Exposed=Window] interface Foo { attribute DOMString bar; };</pre>`,
|
|
50
|
-
"/accelerometer/": `<html><meta name='document-revision' content='c0917d216986f88bdd43c72c0b13352c71f283aa'>
|
|
51
|
-
<h2>Normative references</h2>
|
|
52
|
-
<dl>
|
|
53
|
-
<dt>FOO</dt>
|
|
54
|
-
<dd><a href='https://www.w3.org/TR/Foo'>Foo</a></dd>
|
|
55
|
-
</dl>`,
|
|
56
|
-
"/pointerlock/": `<html>
|
|
57
|
-
<h1>Pointer Lock 2.0`,
|
|
58
|
-
"/TR/remote-playback/": {
|
|
59
|
-
html: `<title>Published version</title>
|
|
60
|
-
<body><h1>Published version</h1></body>`,
|
|
61
|
-
domain: 'https://www.w3.org'
|
|
62
|
-
}
|
|
63
|
-
};
|
|
64
|
-
|
|
65
|
-
const respecHiglight = readFileSync(
|
|
66
|
-
path.join(modulesFolder, "respec-hljs", "dist", "respec-highlight.js"),
|
|
67
|
-
'utf8'
|
|
68
|
-
);
|
|
69
|
-
const respecW3C = readFileSync(
|
|
70
|
-
path.join(modulesFolder, "respec", "builds", "respec-w3c.js"),
|
|
71
|
-
'utf8'
|
|
72
|
-
);
|
|
73
|
-
|
|
74
|
-
const mockAgent = new MockAgent();
|
|
75
|
-
setGlobalDispatcher(mockAgent);
|
|
76
|
-
mockAgent.disableNetConnect();
|
|
77
|
-
// for chrome devtool protocol
|
|
78
|
-
mockAgent.enableNetConnect('127.0.0.1');
|
|
79
|
-
|
|
80
|
-
for (const [path, desc] of Object.entries(mockSpecs)) {
|
|
81
|
-
mockAgent.get(desc.domain || "https://w3c.github.io")
|
|
82
|
-
.intercept({ method: "GET", path })
|
|
83
|
-
.reply(200, desc.html || desc, {
|
|
84
|
-
headers: { "Content-Type": "text/html" }
|
|
85
|
-
})
|
|
86
|
-
.persist();
|
|
87
|
-
|
|
88
|
-
for (const [page, pageContent] of Object.entries(desc.pages || {})) {
|
|
89
|
-
mockAgent.get(desc.domain || "https://w3c.github.io")
|
|
90
|
-
.intercept({ method: "GET", path: path + page })
|
|
91
|
-
.reply(200, pageContent, {
|
|
92
|
-
headers: { "Content-Type": "text/html" }
|
|
93
|
-
})
|
|
94
|
-
.persist();
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
// Handling requests generated by ReSpec documents
|
|
100
|
-
mockAgent
|
|
101
|
-
.get("https://api.specref.org")
|
|
102
|
-
.intercept({ method: "GET", path: "/bibrefs?refs=webidl" })
|
|
103
|
-
.reply(200, { webidl: { href: "https://webidl.spec.whatwg.org/" } }, {
|
|
104
|
-
headers: {
|
|
105
|
-
"Content-Type": "application/json",
|
|
106
|
-
"Access-Control-Allow-Origin": "*"
|
|
107
|
-
}
|
|
108
|
-
})
|
|
109
|
-
.persist();
|
|
110
|
-
|
|
111
|
-
mockAgent
|
|
112
|
-
.get("https://www.w3.org")
|
|
113
|
-
.intercept({ method: "GET", path: "/scripts/TR/2021/fixup.js" })
|
|
114
|
-
.reply(200, '')
|
|
115
|
-
.persist();
|
|
116
|
-
|
|
117
|
-
mockAgent
|
|
118
|
-
.get("https://www.w3.org")
|
|
119
|
-
.intercept({ method: "GET", path: "/StyleSheets/TR/2021/logos/W3C" })
|
|
120
|
-
.reply(200, '')
|
|
121
|
-
.persist();
|
|
122
|
-
|
|
123
|
-
mockAgent
|
|
124
|
-
.get("https://www.w3.org")
|
|
125
|
-
.intercept({ method: "GET", path: "/Tools/respec/respec-highlight" })
|
|
126
|
-
.reply(200, respecHiglight, {
|
|
127
|
-
headers: { "Content-Type": "application/js" }
|
|
128
|
-
})
|
|
129
|
-
.persist();
|
|
130
|
-
|
|
131
|
-
mockAgent
|
|
132
|
-
.get("https://www.w3.org")
|
|
133
|
-
.intercept({ method: "GET", path: "/Tools/respec/respec-w3c" })
|
|
134
|
-
.reply(200, respecW3C, {
|
|
135
|
-
headers: { "Content-Type": "application/js" }
|
|
136
|
-
})
|
|
137
|
-
.persist();
|
|
138
|
-
|
|
139
|
-
mockAgent
|
|
140
|
-
.get("https://www.w3.org")
|
|
141
|
-
.intercept({ method: "GET", path: "/TR/idontexist/" })
|
|
142
|
-
.reply(404, '');
|
|
143
|
-
|
|
144
|
-
mockAgent
|
|
145
|
-
.get("https://www.w3.org")
|
|
146
|
-
.intercept({ method: "GET", path: "/TR/ididnotchange/" })
|
|
147
|
-
.reply(({ headers }) => {
|
|
148
|
-
// NB: Before Node.js v18.17.0, the headers parameters is not an instance
|
|
149
|
-
// of Headers as suggested in examples, but rather an array that alternates
|
|
150
|
-
// header names and header values. Bug detailed at:
|
|
151
|
-
// https://github.com/nodejs/undici/issues/2078
|
|
152
|
-
// Bug fix was integrated in Node.js v18.17.0.
|
|
153
|
-
// Code below can be simplified when support for Node.js v18 gets dropped.
|
|
154
|
-
let value;
|
|
155
|
-
if (Array.isArray(headers)) {
|
|
156
|
-
const pos = headers.findIndex(h => h === 'If-Modified-Since');
|
|
157
|
-
if (pos === -1) {
|
|
158
|
-
return { statusCode: 200, data: 'Unexpected If-Modified-Since header' };
|
|
159
|
-
}
|
|
160
|
-
value = headers[pos+1];
|
|
161
|
-
}
|
|
162
|
-
else {
|
|
163
|
-
value = headers['If-Modified-Since'];
|
|
164
|
-
}
|
|
165
|
-
if (value === "Fri, 11 Feb 2022 00:00:42 GMT") {
|
|
166
|
-
return { statusCode: 304 };
|
|
167
|
-
} else {
|
|
168
|
-
return { statusCode: 200, data: 'Unexpected If-Modified-Since header' };
|
|
169
|
-
}
|
|
170
|
-
});
|
|
171
|
-
|
|
172
|
-
mockAgent
|
|
173
|
-
.get("https://www.w3.org")
|
|
174
|
-
.intercept({ method: "GET", path: "/TR/iredirect/" })
|
|
175
|
-
.reply(200,
|
|
176
|
-
`<!DOCTYPE html><script>window.location = '/TR/recentlyupdated/';</script>`,
|
|
177
|
-
{
|
|
178
|
-
headers: {
|
|
179
|
-
"Content-Type": "text/html",
|
|
180
|
-
"Last-Modified": "Fri, 11 Feb 2022 00:00:42 GMT"
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
);
|
|
184
|
-
|
|
185
|
-
mockAgent
|
|
186
|
-
.get("https://www.w3.org")
|
|
187
|
-
.intercept({ method: "GET", path: "/TR/recentlyupdated/" })
|
|
188
|
-
.reply(200,
|
|
189
|
-
`<html><title>Recently updated</title>
|
|
190
|
-
<h1>Recently updated</h1>`,
|
|
191
|
-
{
|
|
192
|
-
headers: {
|
|
193
|
-
"Content-Type": "text/html",
|
|
194
|
-
"Last-Modified": (new Date()).toString()
|
|
195
|
-
}
|
|
196
|
-
}
|
|
197
|
-
);
|
|
198
|
-
|
|
199
|
-
mockAgent
|
|
200
|
-
.get("https://drafts.csswg.org")
|
|
201
|
-
.intercept({ method: "GET", path: "/server-hiccup/" })
|
|
202
|
-
.reply(200,
|
|
203
|
-
`<html><title>Server hiccup</title>
|
|
204
|
-
<h1> Index of Server Hiccup Module Level 42 </h1>`,
|
|
205
|
-
{ headers: { "Content-Type": "text/html" } })
|
|
206
|
-
.persist();
|
|
207
|
-
|
|
208
|
-
/*nock.emitter.on('error', function (err) {
|
|
209
|
-
console.error(err);
|
|
210
|
-
});
|
|
211
|
-
nock.emitter.on('no match', function(req, options, requestBody) {
|
|
212
|
-
// 127.0.0.1 is used by the devtool protocol, we ignore it
|
|
213
|
-
if (req && req.hostname !== '127.0.0.1') {
|
|
214
|
-
console.error("No match for nock request on " + (options ? options.href : req.href));
|
|
215
|
-
}
|
|
216
|
-
});*/
|
|
217
|
-
|
|
218
|
-
export default mockAgent;
|
|
1
|
+
/**
|
|
2
|
+
* Setup a proxy server that intercepts some network requests. To be used in
|
|
3
|
+
* tests not to hit the network.
|
|
4
|
+
*
|
|
5
|
+
* @module mock-server
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { MockAgent, setGlobalDispatcher } from 'undici';
|
|
9
|
+
import path from 'node:path';
|
|
10
|
+
import { existsSync, readFileSync } from 'node:fs';
|
|
11
|
+
import { fileURLToPath } from 'node:url';
|
|
12
|
+
|
|
13
|
+
const scriptPath = path.dirname(fileURLToPath(import.meta.url));
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Determine the path to the "node_modules" folder. The path depends on whether
|
|
17
|
+
* Reffy is run directly, or installed as a library.
|
|
18
|
+
*
|
|
19
|
+
* @function
|
|
20
|
+
* @return {String} Path to the node_modules folder.
|
|
21
|
+
*/
|
|
22
|
+
function getModulesFolder() {
|
|
23
|
+
const rootFolder = path.resolve(scriptPath, '../..');
|
|
24
|
+
let folder = path.resolve(rootFolder, 'node_modules');
|
|
25
|
+
if (existsSync(folder)) {
|
|
26
|
+
return folder;
|
|
27
|
+
}
|
|
28
|
+
folder = path.resolve(rootFolder, '..');
|
|
29
|
+
return folder;
|
|
30
|
+
}
|
|
31
|
+
const modulesFolder = getModulesFolder();
|
|
32
|
+
|
|
33
|
+
const mockSpecs = {
|
|
34
|
+
"/woff/woff2/": {
|
|
35
|
+
html: `
|
|
36
|
+
<title>WOFF2</title>
|
|
37
|
+
<body>
|
|
38
|
+
<dfn id='foo' data-dfn-type="dfn">Foo</dfn>
|
|
39
|
+
<a href="https://www.w3.org/TR/bar/#baz">bar</a>
|
|
40
|
+
<ul class='toc'><li><a href='page.html'>page</a></ul>`,
|
|
41
|
+
pages: {
|
|
42
|
+
"page.html": `<h2 id='bar'>Heading in subpage</h2>`
|
|
43
|
+
}
|
|
44
|
+
},
|
|
45
|
+
"/mediacapture-output/": `
|
|
46
|
+
<script>respecConfig = { shortName: 'test' };</script>
|
|
47
|
+
<script src='https://www.w3.org/Tools/respec/respec-w3c'></script>
|
|
48
|
+
<div id=abstract></div>
|
|
49
|
+
<pre class='idl'>[Exposed=Window] interface Foo { attribute DOMString bar; };</pre>`,
|
|
50
|
+
"/accelerometer/": `<html><meta name='document-revision' content='c0917d216986f88bdd43c72c0b13352c71f283aa'>
|
|
51
|
+
<h2>Normative references</h2>
|
|
52
|
+
<dl>
|
|
53
|
+
<dt>FOO</dt>
|
|
54
|
+
<dd><a href='https://www.w3.org/TR/Foo'>Foo</a></dd>
|
|
55
|
+
</dl>`,
|
|
56
|
+
"/pointerlock/": `<html>
|
|
57
|
+
<h1>Pointer Lock 2.0`,
|
|
58
|
+
"/TR/remote-playback/": {
|
|
59
|
+
html: `<title>Published version</title>
|
|
60
|
+
<body><h1>Published version</h1></body>`,
|
|
61
|
+
domain: 'https://www.w3.org'
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
const respecHiglight = readFileSync(
|
|
66
|
+
path.join(modulesFolder, "respec-hljs", "dist", "respec-highlight.js"),
|
|
67
|
+
'utf8'
|
|
68
|
+
);
|
|
69
|
+
const respecW3C = readFileSync(
|
|
70
|
+
path.join(modulesFolder, "respec", "builds", "respec-w3c.js"),
|
|
71
|
+
'utf8'
|
|
72
|
+
);
|
|
73
|
+
|
|
74
|
+
const mockAgent = new MockAgent();
|
|
75
|
+
setGlobalDispatcher(mockAgent);
|
|
76
|
+
mockAgent.disableNetConnect();
|
|
77
|
+
// for chrome devtool protocol
|
|
78
|
+
mockAgent.enableNetConnect('127.0.0.1');
|
|
79
|
+
|
|
80
|
+
for (const [path, desc] of Object.entries(mockSpecs)) {
|
|
81
|
+
mockAgent.get(desc.domain || "https://w3c.github.io")
|
|
82
|
+
.intercept({ method: "GET", path })
|
|
83
|
+
.reply(200, desc.html || desc, {
|
|
84
|
+
headers: { "Content-Type": "text/html" }
|
|
85
|
+
})
|
|
86
|
+
.persist();
|
|
87
|
+
|
|
88
|
+
for (const [page, pageContent] of Object.entries(desc.pages || {})) {
|
|
89
|
+
mockAgent.get(desc.domain || "https://w3c.github.io")
|
|
90
|
+
.intercept({ method: "GET", path: path + page })
|
|
91
|
+
.reply(200, pageContent, {
|
|
92
|
+
headers: { "Content-Type": "text/html" }
|
|
93
|
+
})
|
|
94
|
+
.persist();
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
// Handling requests generated by ReSpec documents
|
|
100
|
+
mockAgent
|
|
101
|
+
.get("https://api.specref.org")
|
|
102
|
+
.intercept({ method: "GET", path: "/bibrefs?refs=webidl" })
|
|
103
|
+
.reply(200, { webidl: { href: "https://webidl.spec.whatwg.org/" } }, {
|
|
104
|
+
headers: {
|
|
105
|
+
"Content-Type": "application/json",
|
|
106
|
+
"Access-Control-Allow-Origin": "*"
|
|
107
|
+
}
|
|
108
|
+
})
|
|
109
|
+
.persist();
|
|
110
|
+
|
|
111
|
+
mockAgent
|
|
112
|
+
.get("https://www.w3.org")
|
|
113
|
+
.intercept({ method: "GET", path: "/scripts/TR/2021/fixup.js" })
|
|
114
|
+
.reply(200, '')
|
|
115
|
+
.persist();
|
|
116
|
+
|
|
117
|
+
mockAgent
|
|
118
|
+
.get("https://www.w3.org")
|
|
119
|
+
.intercept({ method: "GET", path: "/StyleSheets/TR/2021/logos/W3C" })
|
|
120
|
+
.reply(200, '')
|
|
121
|
+
.persist();
|
|
122
|
+
|
|
123
|
+
mockAgent
|
|
124
|
+
.get("https://www.w3.org")
|
|
125
|
+
.intercept({ method: "GET", path: "/Tools/respec/respec-highlight" })
|
|
126
|
+
.reply(200, respecHiglight, {
|
|
127
|
+
headers: { "Content-Type": "application/js" }
|
|
128
|
+
})
|
|
129
|
+
.persist();
|
|
130
|
+
|
|
131
|
+
mockAgent
|
|
132
|
+
.get("https://www.w3.org")
|
|
133
|
+
.intercept({ method: "GET", path: "/Tools/respec/respec-w3c" })
|
|
134
|
+
.reply(200, respecW3C, {
|
|
135
|
+
headers: { "Content-Type": "application/js" }
|
|
136
|
+
})
|
|
137
|
+
.persist();
|
|
138
|
+
|
|
139
|
+
mockAgent
|
|
140
|
+
.get("https://www.w3.org")
|
|
141
|
+
.intercept({ method: "GET", path: "/TR/idontexist/" })
|
|
142
|
+
.reply(404, '');
|
|
143
|
+
|
|
144
|
+
mockAgent
|
|
145
|
+
.get("https://www.w3.org")
|
|
146
|
+
.intercept({ method: "GET", path: "/TR/ididnotchange/" })
|
|
147
|
+
.reply(({ headers }) => {
|
|
148
|
+
// NB: Before Node.js v18.17.0, the headers parameters is not an instance
|
|
149
|
+
// of Headers as suggested in examples, but rather an array that alternates
|
|
150
|
+
// header names and header values. Bug detailed at:
|
|
151
|
+
// https://github.com/nodejs/undici/issues/2078
|
|
152
|
+
// Bug fix was integrated in Node.js v18.17.0.
|
|
153
|
+
// Code below can be simplified when support for Node.js v18 gets dropped.
|
|
154
|
+
let value;
|
|
155
|
+
if (Array.isArray(headers)) {
|
|
156
|
+
const pos = headers.findIndex(h => h === 'If-Modified-Since');
|
|
157
|
+
if (pos === -1) {
|
|
158
|
+
return { statusCode: 200, data: 'Unexpected If-Modified-Since header' };
|
|
159
|
+
}
|
|
160
|
+
value = headers[pos+1];
|
|
161
|
+
}
|
|
162
|
+
else {
|
|
163
|
+
value = headers['If-Modified-Since'];
|
|
164
|
+
}
|
|
165
|
+
if (value === "Fri, 11 Feb 2022 00:00:42 GMT") {
|
|
166
|
+
return { statusCode: 304 };
|
|
167
|
+
} else {
|
|
168
|
+
return { statusCode: 200, data: 'Unexpected If-Modified-Since header' };
|
|
169
|
+
}
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
mockAgent
|
|
173
|
+
.get("https://www.w3.org")
|
|
174
|
+
.intercept({ method: "GET", path: "/TR/iredirect/" })
|
|
175
|
+
.reply(200,
|
|
176
|
+
`<!DOCTYPE html><script>window.location = '/TR/recentlyupdated/';</script>`,
|
|
177
|
+
{
|
|
178
|
+
headers: {
|
|
179
|
+
"Content-Type": "text/html",
|
|
180
|
+
"Last-Modified": "Fri, 11 Feb 2022 00:00:42 GMT"
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
);
|
|
184
|
+
|
|
185
|
+
mockAgent
|
|
186
|
+
.get("https://www.w3.org")
|
|
187
|
+
.intercept({ method: "GET", path: "/TR/recentlyupdated/" })
|
|
188
|
+
.reply(200,
|
|
189
|
+
`<html><title>Recently updated</title>
|
|
190
|
+
<h1>Recently updated</h1>`,
|
|
191
|
+
{
|
|
192
|
+
headers: {
|
|
193
|
+
"Content-Type": "text/html",
|
|
194
|
+
"Last-Modified": (new Date()).toString()
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
);
|
|
198
|
+
|
|
199
|
+
mockAgent
|
|
200
|
+
.get("https://drafts.csswg.org")
|
|
201
|
+
.intercept({ method: "GET", path: "/server-hiccup/" })
|
|
202
|
+
.reply(200,
|
|
203
|
+
`<html><title>Server hiccup</title>
|
|
204
|
+
<h1> Index of Server Hiccup Module Level 42 </h1>`,
|
|
205
|
+
{ headers: { "Content-Type": "text/html" } })
|
|
206
|
+
.persist();
|
|
207
|
+
|
|
208
|
+
/*nock.emitter.on('error', function (err) {
|
|
209
|
+
console.error(err);
|
|
210
|
+
});
|
|
211
|
+
nock.emitter.on('no match', function(req, options, requestBody) {
|
|
212
|
+
// 127.0.0.1 is used by the devtool protocol, we ignore it
|
|
213
|
+
if (req && req.hostname !== '127.0.0.1') {
|
|
214
|
+
console.error("No match for nock request on " + (options ? options.href : req.href));
|
|
215
|
+
}
|
|
216
|
+
});*/
|
|
217
|
+
|
|
218
|
+
export default mockAgent;
|