forked from capricorn86/happy-dom
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
capricorn86#526@minor: Continue Adds support for XMLHttpRequest (Sync…
…) and remove outdated annotation, fixes some typo error.
- Loading branch information
Showing
7 changed files
with
137 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
packages/happy-dom/src/xml-http-request/XMLHttpRequestSyncWorker.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import Window from '../window/Window'; | ||
import * as util from 'util'; | ||
|
||
const window = new Window(); | ||
const xhr = new window.XMLHttpRequest(); | ||
|
||
const chunks = []; | ||
|
||
process.stdin.on('data', (chunk) => { | ||
chunks.push(chunk); | ||
}); | ||
|
||
process.stdin.on('end', () => { | ||
const buffer = Buffer.concat(chunks); | ||
|
||
const serials = JSON.parse(buffer.toString()); | ||
if (serials.body && serials.body.type === 'Buffer' && serials.body.data) { | ||
serials.body = Buffer.from(serials.body.data); | ||
} | ||
if (serials.origin) { | ||
window.location.href = serials.origin; | ||
} | ||
|
||
if (serials.cookie) { | ||
window.document.cookie = serials.cookie; | ||
} | ||
|
||
xhr.overrideMimeType(serials.mimeType); | ||
xhr.open(serials.method, serials.uri, true, serials.user, serials.password); | ||
if (serials.headers) { | ||
Object.keys(serials.headers).forEach((key) => { | ||
xhr.setRequestHeader(key, serials.headers[key]); | ||
}); | ||
} | ||
|
||
|
||
xhr.timeout = serials.timeout; | ||
|
||
try { | ||
xhr.addEventListener('loadend', () => { | ||
if (xhr._syncGetError()) { | ||
const err = xhr._syncGetError(); | ||
xhr._syncSetErrorString(err.stack || util.inspect(err)); | ||
} | ||
|
||
process.stdout.write( | ||
JSON.stringify({ | ||
responseURL: xhr.responseUrl, | ||
responseText: xhr.responseText, | ||
status: xhr.status, | ||
statusText: xhr.statusText, | ||
}), | ||
() => { | ||
process.exit(0); | ||
} | ||
); | ||
}); | ||
|
||
xhr.send(serials.body); | ||
} catch (error) { | ||
// Properties.error += error.stack || util.inspect(error); | ||
process.stdout.write( | ||
JSON.stringify({ | ||
responseURL: xhr.responseUrl, | ||
status: xhr.status, | ||
statusText: xhr.statusText | ||
}), | ||
() => { | ||
process.exit(0); | ||
} | ||
); | ||
} | ||
}); |