2014-11-07 12:38:12 -05:00
|
|
|
/**
|
|
|
|
* Wrapper for built-in http.js to emulate the browser XMLHttpRequest object.
|
|
|
|
*
|
|
|
|
* This can be used with JS designed for browsers to improve reuse of code and
|
|
|
|
* allow the use of existing libraries.
|
|
|
|
*
|
|
|
|
* Usage: include("XMLHttpRequest.js") and use XMLHttpRequest per W3C specs.
|
|
|
|
*
|
|
|
|
* @author Dan DeFelippi <dan@driverdan.com>
|
|
|
|
* @contributor David Ellis <d.f.ellis@ieee.org>
|
|
|
|
* @license MIT
|
|
|
|
*/
|
|
|
|
|
2020-06-24 05:40:08 -04:00
|
|
|
const { URL } = require('url')
|
2021-07-13 07:04:45 -04:00
|
|
|
const spawn = require('child_process').spawn
|
|
|
|
const fs = require('fs')
|
2014-11-07 12:38:12 -05:00
|
|
|
|
2020-06-24 05:28:28 -04:00
|
|
|
exports.XMLHttpRequest = function () {
|
2014-11-07 12:38:12 -05:00
|
|
|
/**
|
|
|
|
* Private variables
|
|
|
|
*/
|
2021-07-13 07:04:45 -04:00
|
|
|
const self = this
|
|
|
|
const http = require('http')
|
|
|
|
const https = require('https')
|
2014-11-07 12:38:12 -05:00
|
|
|
|
|
|
|
// Holds http.js objects
|
2021-07-13 07:04:45 -04:00
|
|
|
let request
|
|
|
|
let response
|
2014-11-07 12:38:12 -05:00
|
|
|
|
|
|
|
// Request settings
|
2021-07-13 07:04:45 -04:00
|
|
|
let settings = {}
|
2014-11-07 12:38:12 -05:00
|
|
|
|
|
|
|
// Set some default headers
|
2021-07-13 07:04:45 -04:00
|
|
|
const defaultHeaders = {
|
2020-06-24 05:28:28 -04:00
|
|
|
'User-Agent': 'node-XMLHttpRequest',
|
2021-07-13 07:04:45 -04:00
|
|
|
Accept: '*/*',
|
2020-06-24 05:28:28 -04:00
|
|
|
}
|
2014-11-07 12:38:12 -05:00
|
|
|
|
2021-07-13 07:04:45 -04:00
|
|
|
let headers = defaultHeaders
|
2014-11-07 12:38:12 -05:00
|
|
|
|
|
|
|
// These headers are not user setable.
|
|
|
|
// The following are allowed but banned in the spec:
|
|
|
|
// * user-agent
|
2021-07-13 07:04:45 -04:00
|
|
|
const forbiddenRequestHeaders = [
|
2020-06-24 05:28:28 -04:00
|
|
|
'accept-charset',
|
|
|
|
'accept-encoding',
|
|
|
|
'access-control-request-headers',
|
|
|
|
'access-control-request-method',
|
|
|
|
'connection',
|
|
|
|
'content-length',
|
|
|
|
'content-transfer-encoding',
|
|
|
|
// "cookie",
|
|
|
|
'cookie2',
|
|
|
|
'date',
|
|
|
|
'expect',
|
|
|
|
'host',
|
|
|
|
'keep-alive',
|
|
|
|
'origin',
|
|
|
|
'referer',
|
|
|
|
'te',
|
|
|
|
'trailer',
|
|
|
|
'transfer-encoding',
|
|
|
|
'upgrade',
|
2021-07-13 07:04:45 -04:00
|
|
|
'via',
|
2020-06-24 05:28:28 -04:00
|
|
|
]
|
2014-11-07 12:38:12 -05:00
|
|
|
|
|
|
|
// These request methods are not allowed
|
2021-07-13 07:04:45 -04:00
|
|
|
const forbiddenRequestMethods = ['TRACE', 'TRACK', 'CONNECT']
|
2014-11-07 12:38:12 -05:00
|
|
|
|
|
|
|
// Send flag
|
2021-07-13 07:04:45 -04:00
|
|
|
let sendFlag = false
|
2014-11-07 12:38:12 -05:00
|
|
|
// Error flag, used when errors occur or abort is called
|
2021-07-13 07:04:45 -04:00
|
|
|
let errorFlag = false
|
2014-11-07 12:38:12 -05:00
|
|
|
|
|
|
|
// Event listeners
|
2021-07-13 07:04:45 -04:00
|
|
|
const listeners = {}
|
2014-11-07 12:38:12 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Constants
|
|
|
|
*/
|
|
|
|
|
2020-06-24 05:28:28 -04:00
|
|
|
this.UNSENT = 0
|
|
|
|
this.OPENED = 1
|
|
|
|
this.HEADERS_RECEIVED = 2
|
|
|
|
this.LOADING = 3
|
|
|
|
this.DONE = 4
|
2014-11-07 12:38:12 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Public vars
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Current state
|
2020-06-24 05:28:28 -04:00
|
|
|
this.readyState = this.UNSENT
|
2014-11-07 12:38:12 -05:00
|
|
|
|
|
|
|
// default ready state change handler in case one is not set or is set late
|
2020-06-24 05:28:28 -04:00
|
|
|
this.onreadystatechange = null
|
2014-11-07 12:38:12 -05:00
|
|
|
|
|
|
|
// Result & response
|
2020-06-24 05:28:28 -04:00
|
|
|
this.responseText = ''
|
|
|
|
this.responseXML = ''
|
|
|
|
this.status = null
|
|
|
|
this.statusText = null
|
2014-11-07 12:38:12 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Private methods
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if the specified header is allowed.
|
|
|
|
*
|
|
|
|
* @param string header Header to validate
|
|
|
|
* @return boolean False if not allowed, otherwise true
|
|
|
|
*/
|
2021-07-13 07:04:45 -04:00
|
|
|
const isAllowedHttpHeader = function (header) {
|
2020-06-24 05:28:28 -04:00
|
|
|
return (
|
|
|
|
header && forbiddenRequestHeaders.indexOf(header.toLowerCase()) === -1
|
|
|
|
)
|
|
|
|
}
|
2014-11-07 12:38:12 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if the specified method is allowed.
|
|
|
|
*
|
|
|
|
* @param string method Request method to validate
|
|
|
|
* @return boolean False if not allowed, otherwise true
|
|
|
|
*/
|
2021-07-13 07:04:45 -04:00
|
|
|
const isAllowedHttpMethod = function (method) {
|
2020-06-24 05:28:28 -04:00
|
|
|
return method && forbiddenRequestMethods.indexOf(method) === -1
|
|
|
|
}
|
2014-11-07 12:38:12 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Public methods
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Open the connection. Currently supports local server requests.
|
|
|
|
*
|
|
|
|
* @param string method Connection method (eg GET, POST)
|
|
|
|
* @param string url URL for the connection.
|
|
|
|
* @param boolean async Asynchronous connection. Default is true.
|
|
|
|
* @param string user Username for basic authentication (optional)
|
|
|
|
* @param string password Password for basic authentication (optional)
|
|
|
|
*/
|
2020-06-24 05:28:28 -04:00
|
|
|
this.open = function (method, url, async, user, password) {
|
|
|
|
this.abort()
|
|
|
|
errorFlag = false
|
2014-11-07 12:38:12 -05:00
|
|
|
|
|
|
|
// Check for valid request method
|
|
|
|
if (!isAllowedHttpMethod(method)) {
|
2020-06-24 05:40:08 -04:00
|
|
|
throw new Error('SecurityError: Request method not allowed')
|
2014-11-07 12:38:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
settings = {
|
2020-06-24 05:28:28 -04:00
|
|
|
method: method,
|
|
|
|
url: url.toString(),
|
|
|
|
async: typeof async !== 'boolean' ? true : async,
|
|
|
|
user: user || null,
|
2021-07-13 07:04:45 -04:00
|
|
|
password: password || null,
|
2020-06-24 05:28:28 -04:00
|
|
|
}
|
2014-11-07 12:38:12 -05:00
|
|
|
|
2020-06-24 05:28:28 -04:00
|
|
|
setState(this.OPENED)
|
|
|
|
}
|
2014-11-07 12:38:12 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets a header for the request.
|
|
|
|
*
|
|
|
|
* @param string header Header name
|
|
|
|
* @param string value Header value
|
|
|
|
*/
|
2020-06-24 05:28:28 -04:00
|
|
|
this.setRequestHeader = function (header, value) {
|
2020-06-24 05:40:08 -04:00
|
|
|
if (this.readyState !== this.OPENED) {
|
|
|
|
throw new Error(
|
|
|
|
'INVALID_STATE_ERR: setRequestHeader can only be called when state is OPEN'
|
|
|
|
)
|
2014-11-07 12:38:12 -05:00
|
|
|
}
|
|
|
|
if (!isAllowedHttpHeader(header)) {
|
2020-06-24 05:28:28 -04:00
|
|
|
console.warn('Refused to set unsafe header "' + header + '"')
|
|
|
|
return
|
2014-11-07 12:38:12 -05:00
|
|
|
}
|
|
|
|
if (sendFlag) {
|
2020-06-24 05:40:08 -04:00
|
|
|
throw new Error('INVALID_STATE_ERR: send flag is true')
|
2014-11-07 12:38:12 -05:00
|
|
|
}
|
2020-06-24 05:28:28 -04:00
|
|
|
headers[header] = value
|
|
|
|
}
|
2014-11-07 12:38:12 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets a header from the server response.
|
|
|
|
*
|
|
|
|
* @param string header Name of header to get.
|
|
|
|
* @return string Text of the header or null if it doesn't exist.
|
|
|
|
*/
|
2020-06-24 05:28:28 -04:00
|
|
|
this.getResponseHeader = function (header) {
|
|
|
|
if (
|
|
|
|
typeof header === 'string' &&
|
|
|
|
this.readyState > this.OPENED &&
|
|
|
|
response.headers[header.toLowerCase()] &&
|
|
|
|
!errorFlag
|
2014-11-07 12:38:12 -05:00
|
|
|
) {
|
2020-06-24 05:28:28 -04:00
|
|
|
return response.headers[header.toLowerCase()]
|
2014-11-07 12:38:12 -05:00
|
|
|
}
|
|
|
|
|
2020-06-24 05:28:28 -04:00
|
|
|
return null
|
|
|
|
}
|
2014-11-07 12:38:12 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets all the response headers.
|
|
|
|
*
|
|
|
|
* @return string A string with all response headers separated by CR+LF
|
|
|
|
*/
|
2020-06-24 05:28:28 -04:00
|
|
|
this.getAllResponseHeaders = function () {
|
2014-11-07 12:38:12 -05:00
|
|
|
if (this.readyState < this.HEADERS_RECEIVED || errorFlag) {
|
2020-06-24 05:28:28 -04:00
|
|
|
return ''
|
2014-11-07 12:38:12 -05:00
|
|
|
}
|
2021-07-13 07:04:45 -04:00
|
|
|
let result = ''
|
2014-11-07 12:38:12 -05:00
|
|
|
|
2021-07-13 07:04:45 -04:00
|
|
|
for (const i in response.headers) {
|
2014-11-07 12:38:12 -05:00
|
|
|
// Cookie headers are excluded
|
2020-06-24 05:28:28 -04:00
|
|
|
if (i !== 'set-cookie' && i !== 'set-cookie2') {
|
|
|
|
result += i + ': ' + response.headers[i] + '\r\n'
|
2014-11-07 12:38:12 -05:00
|
|
|
}
|
|
|
|
}
|
2020-06-24 05:28:28 -04:00
|
|
|
return result.substr(0, result.length - 2)
|
|
|
|
}
|
2014-11-07 12:38:12 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets a request header
|
|
|
|
*
|
|
|
|
* @param string name Name of header to get
|
|
|
|
* @return string Returns the request header or empty string if not set
|
|
|
|
*/
|
2020-06-24 05:28:28 -04:00
|
|
|
this.getRequestHeader = function (name) {
|
2014-11-07 12:38:12 -05:00
|
|
|
// @TODO Make this case insensitive
|
2020-06-24 05:28:28 -04:00
|
|
|
if (typeof name === 'string' && headers[name]) {
|
|
|
|
return headers[name]
|
2014-11-07 12:38:12 -05:00
|
|
|
}
|
|
|
|
|
2020-06-24 05:28:28 -04:00
|
|
|
return ''
|
2014-11-07 12:38:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sends the request to the server.
|
|
|
|
*
|
|
|
|
* @param string data Optional data to send as request body.
|
|
|
|
*/
|
2020-06-24 05:28:28 -04:00
|
|
|
this.send = function (data) {
|
2020-06-24 05:40:08 -04:00
|
|
|
if (this.readyState !== this.OPENED) {
|
|
|
|
throw new Error(
|
|
|
|
'INVALID_STATE_ERR: connection must be opened before send() is called'
|
|
|
|
)
|
2014-11-07 12:38:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (sendFlag) {
|
2020-06-24 05:40:08 -04:00
|
|
|
throw new Error('INVALID_STATE_ERR: send has already been called')
|
2014-11-07 12:38:12 -05:00
|
|
|
}
|
|
|
|
|
2021-07-13 07:04:45 -04:00
|
|
|
let host
|
|
|
|
let ssl = false
|
|
|
|
let local = false
|
|
|
|
const url = new URL(settings.url)
|
2014-11-07 12:38:12 -05:00
|
|
|
|
|
|
|
// Determine the server
|
|
|
|
switch (url.protocol) {
|
|
|
|
case 'https:':
|
2020-06-24 05:28:28 -04:00
|
|
|
ssl = true
|
2020-06-24 05:40:08 -04:00
|
|
|
host = url.hostname
|
|
|
|
break
|
2014-11-07 12:38:12 -05:00
|
|
|
case 'http:':
|
2020-06-24 05:40:08 -04:00
|
|
|
host = url.hostname
|
2020-06-24 05:28:28 -04:00
|
|
|
break
|
2014-11-07 12:38:12 -05:00
|
|
|
|
|
|
|
case 'file:':
|
2020-06-24 05:28:28 -04:00
|
|
|
local = true
|
|
|
|
break
|
2014-11-07 12:38:12 -05:00
|
|
|
|
|
|
|
case undefined:
|
|
|
|
case '':
|
2020-06-24 05:40:08 -04:00
|
|
|
host = 'localhost'
|
2020-06-24 05:28:28 -04:00
|
|
|
break
|
2014-11-07 12:38:12 -05:00
|
|
|
|
|
|
|
default:
|
2020-06-24 05:40:08 -04:00
|
|
|
throw new Error('Protocol not supported.')
|
2014-11-07 12:38:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Load files off the local filesystem (file://)
|
|
|
|
if (local) {
|
2020-06-24 05:28:28 -04:00
|
|
|
if (settings.method !== 'GET') {
|
2020-06-24 05:40:08 -04:00
|
|
|
throw new Error('XMLHttpRequest: Only GET method is supported')
|
2014-11-07 12:38:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (settings.async) {
|
2020-06-24 05:28:28 -04:00
|
|
|
fs.readFile(url.pathname, 'utf8', (error, data) => {
|
2014-11-07 12:38:12 -05:00
|
|
|
if (error) {
|
2020-06-24 05:28:28 -04:00
|
|
|
self.handleError(error)
|
2014-11-07 12:38:12 -05:00
|
|
|
} else {
|
2020-06-24 05:28:28 -04:00
|
|
|
self.status = 200
|
|
|
|
self.responseText = data
|
|
|
|
setState(self.DONE)
|
2014-11-07 12:38:12 -05:00
|
|
|
}
|
2020-06-24 05:28:28 -04:00
|
|
|
})
|
2014-11-07 12:38:12 -05:00
|
|
|
} else {
|
|
|
|
try {
|
2020-06-24 05:28:28 -04:00
|
|
|
this.responseText = fs.readFileSync(url.pathname, 'utf8')
|
|
|
|
this.status = 200
|
|
|
|
setState(self.DONE)
|
|
|
|
} catch (e) {
|
|
|
|
this.handleError(e)
|
2014-11-07 12:38:12 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-24 05:28:28 -04:00
|
|
|
return
|
2014-11-07 12:38:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Default to port 80. If accessing localhost on another port be sure
|
|
|
|
// to use http://localhost:port/path
|
2021-07-13 07:04:45 -04:00
|
|
|
const port = url.port || (ssl ? 443 : 80)
|
2014-11-07 12:38:12 -05:00
|
|
|
// Add query string if one is used
|
2021-07-13 07:04:45 -04:00
|
|
|
const uri = url.pathname + (url.search ? url.search : '')
|
2014-11-07 12:38:12 -05:00
|
|
|
|
|
|
|
// Set the Host header or the server may reject the request
|
2020-06-24 05:28:28 -04:00
|
|
|
headers.Host = host
|
2014-11-07 12:38:12 -05:00
|
|
|
if (!((ssl && port === 443) || port === 80)) {
|
2020-06-24 05:28:28 -04:00
|
|
|
headers.Host += ':' + url.port
|
2014-11-07 12:38:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set Basic Auth if necessary
|
|
|
|
if (settings.user) {
|
2020-06-24 05:28:28 -04:00
|
|
|
if (typeof settings.password === 'undefined') {
|
|
|
|
settings.password = ''
|
2014-11-07 12:38:12 -05:00
|
|
|
}
|
2021-07-13 07:04:45 -04:00
|
|
|
const authBuf = Buffer.from(settings.user + ':' + settings.password)
|
2020-06-24 05:28:28 -04:00
|
|
|
headers.Authorization = 'Basic ' + authBuf.toString('base64')
|
2014-11-07 12:38:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set content length header
|
2020-06-24 05:28:28 -04:00
|
|
|
if (settings.method === 'GET' || settings.method === 'HEAD') {
|
|
|
|
data = null
|
2014-11-07 12:38:12 -05:00
|
|
|
} else if (data) {
|
2020-06-24 05:28:28 -04:00
|
|
|
headers['Content-Length'] = Buffer.byteLength(data)
|
2014-11-07 12:38:12 -05:00
|
|
|
|
2020-06-24 05:28:28 -04:00
|
|
|
if (!headers['Content-Type']) {
|
|
|
|
headers['Content-Type'] = 'text/plain;charset=UTF-8'
|
2014-11-07 12:38:12 -05:00
|
|
|
}
|
2020-06-24 05:28:28 -04:00
|
|
|
} else if (settings.method === 'POST') {
|
2014-11-07 12:38:12 -05:00
|
|
|
// For a post with no data set Content-Length: 0.
|
|
|
|
// This is required by buggy servers that don't meet the specs.
|
2020-06-24 05:28:28 -04:00
|
|
|
headers['Content-Length'] = 0
|
2014-11-07 12:38:12 -05:00
|
|
|
}
|
|
|
|
|
2021-07-13 07:04:45 -04:00
|
|
|
const options = {
|
2014-11-07 12:38:12 -05:00
|
|
|
host: host,
|
|
|
|
port: port,
|
|
|
|
path: uri,
|
|
|
|
method: settings.method,
|
2021-07-13 07:04:45 -04:00
|
|
|
headers: headers,
|
2020-06-24 05:28:28 -04:00
|
|
|
}
|
2014-11-07 12:38:12 -05:00
|
|
|
|
|
|
|
// Reset error flag
|
2020-06-24 05:28:28 -04:00
|
|
|
errorFlag = false
|
2014-11-07 12:38:12 -05:00
|
|
|
|
|
|
|
// Handle async requests
|
|
|
|
if (settings.async) {
|
|
|
|
// Use the proper protocol
|
2021-07-13 07:04:45 -04:00
|
|
|
const doRequest = ssl ? https.request : http.request
|
2014-11-07 12:38:12 -05:00
|
|
|
|
|
|
|
// Request is being sent, set send flag
|
2020-06-24 05:28:28 -04:00
|
|
|
sendFlag = true
|
2014-11-07 12:38:12 -05:00
|
|
|
|
|
|
|
// As per spec, this is called here for historical reasons.
|
2020-06-24 05:28:28 -04:00
|
|
|
self.dispatchEvent('readystatechange')
|
2014-11-07 12:38:12 -05:00
|
|
|
|
|
|
|
// Create the request
|
2021-07-13 07:04:45 -04:00
|
|
|
request = doRequest(options, resp => {
|
2020-06-24 05:28:28 -04:00
|
|
|
response = resp
|
|
|
|
response.setEncoding('utf8')
|
2014-11-07 12:38:12 -05:00
|
|
|
|
2020-06-24 05:28:28 -04:00
|
|
|
setState(self.HEADERS_RECEIVED)
|
|
|
|
self.status = response.statusCode
|
2014-11-07 12:38:12 -05:00
|
|
|
|
2021-07-13 07:04:45 -04:00
|
|
|
response.on('data', chunk => {
|
2014-11-07 12:38:12 -05:00
|
|
|
// Make sure there's some data
|
|
|
|
if (chunk) {
|
2020-06-24 05:28:28 -04:00
|
|
|
self.responseText += chunk
|
2014-11-07 12:38:12 -05:00
|
|
|
}
|
|
|
|
// Don't emit state changes if the connection has been aborted.
|
|
|
|
if (sendFlag) {
|
2020-06-24 05:28:28 -04:00
|
|
|
setState(self.LOADING)
|
2014-11-07 12:38:12 -05:00
|
|
|
}
|
2020-06-24 05:28:28 -04:00
|
|
|
})
|
2014-11-07 12:38:12 -05:00
|
|
|
|
2020-06-24 05:28:28 -04:00
|
|
|
response.on('end', () => {
|
2014-11-07 12:38:12 -05:00
|
|
|
if (sendFlag) {
|
|
|
|
// Discard the 'end' event if the connection has been aborted
|
2020-06-24 05:28:28 -04:00
|
|
|
setState(self.DONE)
|
|
|
|
sendFlag = false
|
2014-11-07 12:38:12 -05:00
|
|
|
}
|
2020-06-24 05:28:28 -04:00
|
|
|
})
|
2014-11-07 12:38:12 -05:00
|
|
|
|
2021-07-13 07:04:45 -04:00
|
|
|
response.on('error', error => {
|
2020-06-24 05:28:28 -04:00
|
|
|
self.handleError(error)
|
|
|
|
})
|
2021-07-13 07:04:45 -04:00
|
|
|
}).on('error', error => {
|
2020-06-24 05:28:28 -04:00
|
|
|
self.handleError(error)
|
|
|
|
})
|
2014-11-07 12:38:12 -05:00
|
|
|
|
|
|
|
// Node 0.4 and later won't accept empty data. Make sure it's needed.
|
|
|
|
if (data) {
|
2020-06-24 05:28:28 -04:00
|
|
|
request.write(data)
|
2014-11-07 12:38:12 -05:00
|
|
|
}
|
|
|
|
|
2020-06-24 05:28:28 -04:00
|
|
|
request.end()
|
2014-11-07 12:38:12 -05:00
|
|
|
|
2020-06-24 05:28:28 -04:00
|
|
|
self.dispatchEvent('loadstart')
|
|
|
|
} else {
|
|
|
|
// Synchronous
|
2014-11-07 12:38:12 -05:00
|
|
|
// Create a temporary file for communication with the other Node process
|
2021-07-13 07:04:45 -04:00
|
|
|
const syncFile = '.node-xmlhttprequest-sync-' + process.pid
|
2020-06-24 05:28:28 -04:00
|
|
|
fs.writeFileSync(syncFile, '', 'utf8')
|
2014-11-07 12:38:12 -05:00
|
|
|
// The async request the other Node process executes
|
2021-07-13 07:04:45 -04:00
|
|
|
const execString =
|
2020-06-24 05:28:28 -04:00
|
|
|
"var http = require('http'), https = require('https'), fs = require('fs');" +
|
|
|
|
'var doRequest = http' +
|
|
|
|
(ssl ? 's' : '') +
|
|
|
|
'.request;' +
|
|
|
|
'var options = ' +
|
|
|
|
JSON.stringify(options) +
|
|
|
|
';' +
|
|
|
|
"var responseText = '';" +
|
|
|
|
'var req = doRequest(options, function(response) {' +
|
|
|
|
"response.setEncoding('utf8');" +
|
|
|
|
"response.on('data', function(chunk) {" +
|
|
|
|
'responseText += chunk;' +
|
|
|
|
'});' +
|
|
|
|
"response.on('end', function() {" +
|
|
|
|
"fs.writeFileSync('" +
|
|
|
|
syncFile +
|
|
|
|
"', 'NODE-XMLHTTPREQUEST-STATUS:' + response.statusCode + ',' + responseText, 'utf8');" +
|
|
|
|
'});' +
|
|
|
|
"response.on('error', function(error) {" +
|
|
|
|
"fs.writeFileSync('" +
|
|
|
|
syncFile +
|
|
|
|
"', 'NODE-XMLHTTPREQUEST-ERROR:' + JSON.stringify(error), 'utf8');" +
|
|
|
|
'});' +
|
|
|
|
"}).on('error', function(error) {" +
|
|
|
|
"fs.writeFileSync('" +
|
|
|
|
syncFile +
|
|
|
|
"', 'NODE-XMLHTTPREQUEST-ERROR:' + JSON.stringify(error), 'utf8');" +
|
|
|
|
'});' +
|
|
|
|
(data ? "req.write('" + data.replace(/'/g, "\\'") + "');" : '') +
|
|
|
|
'req.end();'
|
2014-11-07 12:38:12 -05:00
|
|
|
// Start the other Node Process, executing this string
|
2020-06-24 05:40:08 -04:00
|
|
|
const syncProc = spawn(process.argv[0], ['-e', execString])
|
|
|
|
while ((self.responseText = fs.readFileSync(syncFile, 'utf8')) === '') {
|
2014-11-07 12:38:12 -05:00
|
|
|
// Wait while the file is empty
|
|
|
|
}
|
|
|
|
// Kill the child process once the file has data
|
2020-06-24 05:28:28 -04:00
|
|
|
syncProc.stdin.end()
|
2014-11-07 12:38:12 -05:00
|
|
|
// Remove the temporary file
|
2020-06-24 05:28:28 -04:00
|
|
|
fs.unlinkSync(syncFile)
|
2014-11-07 12:38:12 -05:00
|
|
|
if (self.responseText.match(/^NODE-XMLHTTPREQUEST-ERROR:/)) {
|
|
|
|
// If the file returned an error, handle it
|
2021-07-13 07:04:45 -04:00
|
|
|
const errorObj = self.responseText.replace(
|
2020-06-24 05:28:28 -04:00
|
|
|
/^NODE-XMLHTTPREQUEST-ERROR:/,
|
|
|
|
''
|
|
|
|
)
|
|
|
|
self.handleError(errorObj)
|
2014-11-07 12:38:12 -05:00
|
|
|
} else {
|
|
|
|
// If the file returned okay, parse its data and move to the DONE state
|
2020-06-24 05:28:28 -04:00
|
|
|
self.status = self.responseText.replace(
|
|
|
|
/^NODE-XMLHTTPREQUEST-STATUS:([0-9]*),.*/,
|
|
|
|
'$1'
|
|
|
|
)
|
|
|
|
self.responseText = self.responseText.replace(
|
|
|
|
/^NODE-XMLHTTPREQUEST-STATUS:[0-9]*,(.*)/,
|
|
|
|
'$1'
|
|
|
|
)
|
|
|
|
setState(self.DONE)
|
2014-11-07 12:38:12 -05:00
|
|
|
}
|
|
|
|
}
|
2020-06-24 05:28:28 -04:00
|
|
|
}
|
2014-11-07 12:38:12 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Called when an error is encountered to deal with it.
|
|
|
|
*/
|
2020-06-24 05:28:28 -04:00
|
|
|
this.handleError = function (error) {
|
|
|
|
this.status = 503
|
|
|
|
this.statusText = error
|
|
|
|
this.responseText = error.stack
|
|
|
|
errorFlag = true
|
|
|
|
setState(this.DONE)
|
|
|
|
}
|
2014-11-07 12:38:12 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Aborts a request.
|
|
|
|
*/
|
2020-06-24 05:28:28 -04:00
|
|
|
this.abort = function () {
|
2014-11-07 12:38:12 -05:00
|
|
|
if (request) {
|
2020-06-24 05:28:28 -04:00
|
|
|
request.abort()
|
|
|
|
request = null
|
2014-11-07 12:38:12 -05:00
|
|
|
}
|
|
|
|
|
2020-06-24 05:28:28 -04:00
|
|
|
headers = defaultHeaders
|
|
|
|
this.responseText = ''
|
|
|
|
this.responseXML = ''
|
2014-11-07 12:38:12 -05:00
|
|
|
|
2020-06-24 05:28:28 -04:00
|
|
|
errorFlag = true
|
2014-11-07 12:38:12 -05:00
|
|
|
|
2020-06-24 05:28:28 -04:00
|
|
|
if (
|
|
|
|
this.readyState !== this.UNSENT &&
|
|
|
|
(this.readyState !== this.OPENED || sendFlag) &&
|
|
|
|
this.readyState !== this.DONE
|
|
|
|
) {
|
|
|
|
sendFlag = false
|
|
|
|
setState(this.DONE)
|
2014-11-07 12:38:12 -05:00
|
|
|
}
|
2020-06-24 05:28:28 -04:00
|
|
|
this.readyState = this.UNSENT
|
|
|
|
}
|
2014-11-07 12:38:12 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds an event listener. Preferred method of binding to events.
|
|
|
|
*/
|
2020-06-24 05:28:28 -04:00
|
|
|
this.addEventListener = function (event, callback) {
|
2014-11-07 12:38:12 -05:00
|
|
|
if (!(event in listeners)) {
|
2020-06-24 05:28:28 -04:00
|
|
|
listeners[event] = []
|
2014-11-07 12:38:12 -05:00
|
|
|
}
|
|
|
|
// Currently allows duplicate callbacks. Should it?
|
2020-06-24 05:28:28 -04:00
|
|
|
listeners[event].push(callback)
|
|
|
|
}
|
2014-11-07 12:38:12 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove an event callback that has already been bound.
|
|
|
|
* Only works on the matching funciton, cannot be a copy.
|
|
|
|
*/
|
2020-06-24 05:28:28 -04:00
|
|
|
this.removeEventListener = function (event, callback) {
|
2014-11-07 12:38:12 -05:00
|
|
|
if (event in listeners) {
|
|
|
|
// Filter will return a new array with the callback removed
|
2021-07-13 07:04:45 -04:00
|
|
|
listeners[event] = listeners[event].filter(ev => {
|
2020-06-24 05:28:28 -04:00
|
|
|
return ev !== callback
|
|
|
|
})
|
2014-11-07 12:38:12 -05:00
|
|
|
}
|
2020-06-24 05:28:28 -04:00
|
|
|
}
|
2014-11-07 12:38:12 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Dispatch any events, including both "on" methods and events attached using addEventListener.
|
|
|
|
*/
|
2020-06-24 05:28:28 -04:00
|
|
|
this.dispatchEvent = function (event) {
|
|
|
|
if (typeof self['on' + event] === 'function') {
|
|
|
|
self['on' + event]()
|
2014-11-07 12:38:12 -05:00
|
|
|
}
|
|
|
|
if (event in listeners) {
|
2021-07-13 07:04:45 -04:00
|
|
|
for (let i = 0, len = listeners[event].length; i < len; i++) {
|
2020-06-24 05:28:28 -04:00
|
|
|
listeners[event][i].call(self)
|
2014-11-07 12:38:12 -05:00
|
|
|
}
|
|
|
|
}
|
2020-06-24 05:28:28 -04:00
|
|
|
}
|
2014-11-07 12:38:12 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Changes readyState and calls onreadystatechange.
|
|
|
|
*
|
|
|
|
* @param int state New state
|
|
|
|
*/
|
2020-06-24 05:28:28 -04:00
|
|
|
var setState = function (state) {
|
2014-11-07 12:38:12 -05:00
|
|
|
if (self.readyState !== state) {
|
2020-06-24 05:28:28 -04:00
|
|
|
self.readyState = state
|
|
|
|
|
|
|
|
if (
|
|
|
|
settings.async ||
|
|
|
|
self.readyState < self.OPENED ||
|
|
|
|
self.readyState === self.DONE
|
|
|
|
) {
|
|
|
|
self.dispatchEvent('readystatechange')
|
2014-11-07 12:38:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (self.readyState === self.DONE && !errorFlag) {
|
2020-06-24 05:28:28 -04:00
|
|
|
self.dispatchEvent('load')
|
2014-11-07 12:38:12 -05:00
|
|
|
// @TODO figure out InspectorInstrumentation::didLoadXHR(cookie)
|
2020-06-24 05:28:28 -04:00
|
|
|
self.dispatchEvent('loadend')
|
2014-11-07 12:38:12 -05:00
|
|
|
}
|
|
|
|
}
|
2020-06-24 05:28:28 -04:00
|
|
|
}
|
|
|
|
}
|