From 4e87195739f2a5d9a05451b48773c8afdc680765 Mon Sep 17 00:00:00 2001 From: akiyamn Date: Sun, 24 Sep 2023 23:22:21 +1000 Subject: Initial commit (by create-cloudflare CLI) --- .../polyfills/http-lib/capability.js | 52 ++++ .../polyfills/http-lib/request.js | 278 +++++++++++++++++++++ .../polyfills/http-lib/response.js | 185 ++++++++++++++ .../polyfills/http-lib/to-arraybuffer.js | 30 +++ 4 files changed, 545 insertions(+) create mode 100644 node_modules/rollup-plugin-node-polyfills/polyfills/http-lib/capability.js create mode 100644 node_modules/rollup-plugin-node-polyfills/polyfills/http-lib/request.js create mode 100644 node_modules/rollup-plugin-node-polyfills/polyfills/http-lib/response.js create mode 100644 node_modules/rollup-plugin-node-polyfills/polyfills/http-lib/to-arraybuffer.js (limited to 'node_modules/rollup-plugin-node-polyfills/polyfills/http-lib') diff --git a/node_modules/rollup-plugin-node-polyfills/polyfills/http-lib/capability.js b/node_modules/rollup-plugin-node-polyfills/polyfills/http-lib/capability.js new file mode 100644 index 0000000..05210b9 --- /dev/null +++ b/node_modules/rollup-plugin-node-polyfills/polyfills/http-lib/capability.js @@ -0,0 +1,52 @@ +export var hasFetch = isFunction(global.fetch) && isFunction(global.ReadableStream) + +var _blobConstructor; +export function blobConstructor() { + if (typeof _blobConstructor !== 'undefined') { + return _blobConstructor; + } + try { + new global.Blob([new ArrayBuffer(1)]) + _blobConstructor = true + } catch (e) { + _blobConstructor = false + } + return _blobConstructor +} +var xhr; + +function checkTypeSupport(type) { + if (!xhr) { + xhr = new global.XMLHttpRequest() + // If location.host is empty, e.g. if this page/worker was loaded + // from a Blob, then use example.com to avoid an error + xhr.open('GET', global.location.host ? '/' : 'https://example.com') + } + try { + xhr.responseType = type + return xhr.responseType === type + } catch (e) { + return false + } + +} + +// For some strange reason, Safari 7.0 reports typeof global.ArrayBuffer === 'object'. +// Safari 7.1 appears to have fixed this bug. +var haveArrayBuffer = typeof global.ArrayBuffer !== 'undefined' +var haveSlice = haveArrayBuffer && isFunction(global.ArrayBuffer.prototype.slice) + +export var arraybuffer = haveArrayBuffer && checkTypeSupport('arraybuffer') + // These next two tests unavoidably show warnings in Chrome. Since fetch will always + // be used if it's available, just return false for these to avoid the warnings. +export var msstream = !hasFetch && haveSlice && checkTypeSupport('ms-stream') +export var mozchunkedarraybuffer = !hasFetch && haveArrayBuffer && + checkTypeSupport('moz-chunked-arraybuffer') +export var overrideMimeType = isFunction(xhr.overrideMimeType) +export var vbArray = isFunction(global.VBArray) + +function isFunction(value) { + return typeof value === 'function' +} + +xhr = null // Help gc diff --git a/node_modules/rollup-plugin-node-polyfills/polyfills/http-lib/request.js b/node_modules/rollup-plugin-node-polyfills/polyfills/http-lib/request.js new file mode 100644 index 0000000..6e9b9e5 --- /dev/null +++ b/node_modules/rollup-plugin-node-polyfills/polyfills/http-lib/request.js @@ -0,0 +1,278 @@ +import * as capability from './capability'; +import {inherits} from 'util'; +import {IncomingMessage, readyStates as rStates} from './response'; +import {Writable} from 'stream'; +import toArrayBuffer from './to-arraybuffer'; + +function decideMode(preferBinary, useFetch) { + if (capability.hasFetch && useFetch) { + return 'fetch' + } else if (capability.mozchunkedarraybuffer) { + return 'moz-chunked-arraybuffer' + } else if (capability.msstream) { + return 'ms-stream' + } else if (capability.arraybuffer && preferBinary) { + return 'arraybuffer' + } else if (capability.vbArray && preferBinary) { + return 'text:vbarray' + } else { + return 'text' + } +} +export default ClientRequest; + +function ClientRequest(opts) { + var self = this + Writable.call(self) + + self._opts = opts + self._body = [] + self._headers = {} + if (opts.auth) + self.setHeader('Authorization', 'Basic ' + new Buffer(opts.auth).toString('base64')) + Object.keys(opts.headers).forEach(function(name) { + self.setHeader(name, opts.headers[name]) + }) + + var preferBinary + var useFetch = true + if (opts.mode === 'disable-fetch') { + // If the use of XHR should be preferred and includes preserving the 'content-type' header + useFetch = false + preferBinary = true + } else if (opts.mode === 'prefer-streaming') { + // If streaming is a high priority but binary compatibility and + // the accuracy of the 'content-type' header aren't + preferBinary = false + } else if (opts.mode === 'allow-wrong-content-type') { + // If streaming is more important than preserving the 'content-type' header + preferBinary = !capability.overrideMimeType + } else if (!opts.mode || opts.mode === 'default' || opts.mode === 'prefer-fast') { + // Use binary if text streaming may corrupt data or the content-type header, or for speed + preferBinary = true + } else { + throw new Error('Invalid value for opts.mode') + } + self._mode = decideMode(preferBinary, useFetch) + + self.on('finish', function() { + self._onFinish() + }) +} + +inherits(ClientRequest, Writable) +// Taken from http://www.w3.org/TR/XMLHttpRequest/#the-setrequestheader%28%29-method +var unsafeHeaders = [ + 'accept-charset', + 'accept-encoding', + 'access-control-request-headers', + 'access-control-request-method', + 'connection', + 'content-length', + 'cookie', + 'cookie2', + 'date', + 'dnt', + 'expect', + 'host', + 'keep-alive', + 'origin', + 'referer', + 'te', + 'trailer', + 'transfer-encoding', + 'upgrade', + 'user-agent', + 'via' +] +ClientRequest.prototype.setHeader = function(name, value) { + var self = this + var lowerName = name.toLowerCase() + // This check is not necessary, but it prevents warnings from browsers about setting unsafe + // headers. To be honest I'm not entirely sure hiding these warnings is a good thing, but + // http-browserify did it, so I will too. + if (unsafeHeaders.indexOf(lowerName) !== -1) + return + + self._headers[lowerName] = { + name: name, + value: value + } +} + +ClientRequest.prototype.getHeader = function(name) { + var self = this + return self._headers[name.toLowerCase()].value +} + +ClientRequest.prototype.removeHeader = function(name) { + var self = this + delete self._headers[name.toLowerCase()] +} + +ClientRequest.prototype._onFinish = function() { + var self = this + + if (self._destroyed) + return + var opts = self._opts + + var headersObj = self._headers + var body + if (opts.method === 'POST' || opts.method === 'PUT' || opts.method === 'PATCH') { + if (capability.blobConstructor()) { + body = new global.Blob(self._body.map(function(buffer) { + return toArrayBuffer(buffer) + }), { + type: (headersObj['content-type'] || {}).value || '' + }) + } else { + // get utf8 string + body = Buffer.concat(self._body).toString() + } + } + + if (self._mode === 'fetch') { + var headers = Object.keys(headersObj).map(function(name) { + return [headersObj[name].name, headersObj[name].value] + }) + + global.fetch(self._opts.url, { + method: self._opts.method, + headers: headers, + body: body, + mode: 'cors', + credentials: opts.withCredentials ? 'include' : 'same-origin' + }).then(function(response) { + self._fetchResponse = response + self._connect() + }, function(reason) { + self.emit('error', reason) + }) + } else { + var xhr = self._xhr = new global.XMLHttpRequest() + try { + xhr.open(self._opts.method, self._opts.url, true) + } catch (err) { + process.nextTick(function() { + self.emit('error', err) + }) + return + } + + // Can't set responseType on really old browsers + if ('responseType' in xhr) + xhr.responseType = self._mode.split(':')[0] + + if ('withCredentials' in xhr) + xhr.withCredentials = !!opts.withCredentials + + if (self._mode === 'text' && 'overrideMimeType' in xhr) + xhr.overrideMimeType('text/plain; charset=x-user-defined') + + Object.keys(headersObj).forEach(function(name) { + xhr.setRequestHeader(headersObj[name].name, headersObj[name].value) + }) + + self._response = null + xhr.onreadystatechange = function() { + switch (xhr.readyState) { + case rStates.LOADING: + case rStates.DONE: + self._onXHRProgress() + break + } + } + // Necessary for streaming in Firefox, since xhr.response is ONLY defined + // in onprogress, not in onreadystatechange with xhr.readyState = 3 + if (self._mode === 'moz-chunked-arraybuffer') { + xhr.onprogress = function() { + self._onXHRProgress() + } + } + + xhr.onerror = function() { + if (self._destroyed) + return + self.emit('error', new Error('XHR error')) + } + + try { + xhr.send(body) + } catch (err) { + process.nextTick(function() { + self.emit('error', err) + }) + return + } + } +} + +/** + * Checks if xhr.status is readable and non-zero, indicating no error. + * Even though the spec says it should be available in readyState 3, + * accessing it throws an exception in IE8 + */ +function statusValid(xhr) { + try { + var status = xhr.status + return (status !== null && status !== 0) + } catch (e) { + return false + } +} + +ClientRequest.prototype._onXHRProgress = function() { + var self = this + + if (!statusValid(self._xhr) || self._destroyed) + return + + if (!self._response) + self._connect() + + self._response._onXHRProgress() +} + +ClientRequest.prototype._connect = function() { + var self = this + + if (self._destroyed) + return + + self._response = new IncomingMessage(self._xhr, self._fetchResponse, self._mode) + self.emit('response', self._response) +} + +ClientRequest.prototype._write = function(chunk, encoding, cb) { + var self = this + + self._body.push(chunk) + cb() +} + +ClientRequest.prototype.abort = ClientRequest.prototype.destroy = function() { + var self = this + self._destroyed = true + if (self._response) + self._response._destroyed = true + if (self._xhr) + self._xhr.abort() + // Currently, there isn't a way to truly abort a fetch. + // If you like bikeshedding, see https://github.com/whatwg/fetch/issues/27 +} + +ClientRequest.prototype.end = function(data, encoding, cb) { + var self = this + if (typeof data === 'function') { + cb = data + data = undefined + } + + Writable.prototype.end.call(self, data, encoding, cb) +} + +ClientRequest.prototype.flushHeaders = function() {} +ClientRequest.prototype.setTimeout = function() {} +ClientRequest.prototype.setNoDelay = function() {} +ClientRequest.prototype.setSocketKeepAlive = function() {} diff --git a/node_modules/rollup-plugin-node-polyfills/polyfills/http-lib/response.js b/node_modules/rollup-plugin-node-polyfills/polyfills/http-lib/response.js new file mode 100644 index 0000000..459a3ef --- /dev/null +++ b/node_modules/rollup-plugin-node-polyfills/polyfills/http-lib/response.js @@ -0,0 +1,185 @@ +import {overrideMimeType} from './capability'; +import {inherits} from 'util'; +import {Readable} from 'stream'; + +var rStates = { + UNSENT: 0, + OPENED: 1, + HEADERS_RECEIVED: 2, + LOADING: 3, + DONE: 4 +} +export { + rStates as readyStates +}; +export function IncomingMessage(xhr, response, mode) { + var self = this + Readable.call(self) + + self._mode = mode + self.headers = {} + self.rawHeaders = [] + self.trailers = {} + self.rawTrailers = [] + + // Fake the 'close' event, but only once 'end' fires + self.on('end', function() { + // The nextTick is necessary to prevent the 'request' module from causing an infinite loop + process.nextTick(function() { + self.emit('close') + }) + }) + var read; + if (mode === 'fetch') { + self._fetchResponse = response + + self.url = response.url + self.statusCode = response.status + self.statusMessage = response.statusText + // backwards compatible version of for ( of ): + // for (var ,_i,_it = [Symbol.iterator](); = (_i = _it.next()).value,!_i.done;) + for (var header, _i, _it = response.headers[Symbol.iterator](); header = (_i = _it.next()).value, !_i.done;) { + self.headers[header[0].toLowerCase()] = header[1] + self.rawHeaders.push(header[0], header[1]) + } + + // TODO: this doesn't respect backpressure. Once WritableStream is available, this can be fixed + var reader = response.body.getReader() + + read = function () { + reader.read().then(function(result) { + if (self._destroyed) + return + if (result.done) { + self.push(null) + return + } + self.push(new Buffer(result.value)) + read() + }) + } + read() + + } else { + self._xhr = xhr + self._pos = 0 + + self.url = xhr.responseURL + self.statusCode = xhr.status + self.statusMessage = xhr.statusText + var headers = xhr.getAllResponseHeaders().split(/\r?\n/) + headers.forEach(function(header) { + var matches = header.match(/^([^:]+):\s*(.*)/) + if (matches) { + var key = matches[1].toLowerCase() + if (key === 'set-cookie') { + if (self.headers[key] === undefined) { + self.headers[key] = [] + } + self.headers[key].push(matches[2]) + } else if (self.headers[key] !== undefined) { + self.headers[key] += ', ' + matches[2] + } else { + self.headers[key] = matches[2] + } + self.rawHeaders.push(matches[1], matches[2]) + } + }) + + self._charset = 'x-user-defined' + if (!overrideMimeType) { + var mimeType = self.rawHeaders['mime-type'] + if (mimeType) { + var charsetMatch = mimeType.match(/;\s*charset=([^;])(;|$)/) + if (charsetMatch) { + self._charset = charsetMatch[1].toLowerCase() + } + } + if (!self._charset) + self._charset = 'utf-8' // best guess + } + } +} + +inherits(IncomingMessage, Readable) + +IncomingMessage.prototype._read = function() {} + +IncomingMessage.prototype._onXHRProgress = function() { + var self = this + + var xhr = self._xhr + + var response = null + switch (self._mode) { + case 'text:vbarray': // For IE9 + if (xhr.readyState !== rStates.DONE) + break + try { + // This fails in IE8 + response = new global.VBArray(xhr.responseBody).toArray() + } catch (e) { + // pass + } + if (response !== null) { + self.push(new Buffer(response)) + break + } + // Falls through in IE8 + case 'text': + try { // This will fail when readyState = 3 in IE9. Switch mode and wait for readyState = 4 + response = xhr.responseText + } catch (e) { + self._mode = 'text:vbarray' + break + } + if (response.length > self._pos) { + var newData = response.substr(self._pos) + if (self._charset === 'x-user-defined') { + var buffer = new Buffer(newData.length) + for (var i = 0; i < newData.length; i++) + buffer[i] = newData.charCodeAt(i) & 0xff + + self.push(buffer) + } else { + self.push(newData, self._charset) + } + self._pos = response.length + } + break + case 'arraybuffer': + if (xhr.readyState !== rStates.DONE || !xhr.response) + break + response = xhr.response + self.push(new Buffer(new Uint8Array(response))) + break + case 'moz-chunked-arraybuffer': // take whole + response = xhr.response + if (xhr.readyState !== rStates.LOADING || !response) + break + self.push(new Buffer(new Uint8Array(response))) + break + case 'ms-stream': + response = xhr.response + if (xhr.readyState !== rStates.LOADING) + break + var reader = new global.MSStreamReader() + reader.onprogress = function() { + if (reader.result.byteLength > self._pos) { + self.push(new Buffer(new Uint8Array(reader.result.slice(self._pos)))) + self._pos = reader.result.byteLength + } + } + reader.onload = function() { + self.push(null) + } + // reader.onerror = ??? // TODO: this + reader.readAsArrayBuffer(response) + break + } + + // The ms-stream case handles end separately in reader.onload() + if (self._xhr.readyState === rStates.DONE && self._mode !== 'ms-stream') { + self.push(null) + } +} diff --git a/node_modules/rollup-plugin-node-polyfills/polyfills/http-lib/to-arraybuffer.js b/node_modules/rollup-plugin-node-polyfills/polyfills/http-lib/to-arraybuffer.js new file mode 100644 index 0000000..b0a4671 --- /dev/null +++ b/node_modules/rollup-plugin-node-polyfills/polyfills/http-lib/to-arraybuffer.js @@ -0,0 +1,30 @@ +// from https://github.com/jhiesey/to-arraybuffer/blob/6502d9850e70ba7935a7df4ad86b358fc216f9f0/index.js + +// MIT License +// Copyright (c) 2016 John Hiesey +import {isBuffer} from 'buffer'; +export default function (buf) { + // If the buffer is backed by a Uint8Array, a faster version will work + if (buf instanceof Uint8Array) { + // If the buffer isn't a subarray, return the underlying ArrayBuffer + if (buf.byteOffset === 0 && buf.byteLength === buf.buffer.byteLength) { + return buf.buffer + } else if (typeof buf.buffer.slice === 'function') { + // Otherwise we need to get a proper copy + return buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.byteLength) + } + } + + if (isBuffer(buf)) { + // This is the slow version that will work with any Buffer + // implementation (even in old browsers) + var arrayCopy = new Uint8Array(buf.length) + var len = buf.length + for (var i = 0; i < len; i++) { + arrayCopy[i] = buf[i] + } + return arrayCopy.buffer + } else { + throw new Error('Argument must be a Buffer') + } +} -- cgit v1.2.3