@mehmetb/rollup-plugin-node-builtins 3.0.1
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/README.md +101 -0
- package/dist/constants.js +474 -0
- package/dist/rollup-plugin-node-builtins.cjs.js +77 -0
- package/dist/rollup-plugin-node-builtins.es6.js +75 -0
- package/package.json +42 -0
- package/src/es6/assert.js +488 -0
- package/src/es6/console.js +13 -0
- package/src/es6/domain.js +100 -0
- package/src/es6/empty.js +1 -0
- package/src/es6/events.js +475 -0
- package/src/es6/http-lib/capability.js +52 -0
- package/src/es6/http-lib/request.js +278 -0
- package/src/es6/http-lib/response.js +185 -0
- package/src/es6/http-lib/to-arraybuffer.js +30 -0
- package/src/es6/http.js +167 -0
- package/src/es6/inherits.js +25 -0
- package/src/es6/os.js +113 -0
- package/src/es6/path.js +234 -0
- package/src/es6/punycode.js +475 -0
- package/src/es6/qs.js +147 -0
- package/src/es6/readable-stream/buffer-list.js +59 -0
- package/src/es6/readable-stream/duplex.js +45 -0
- package/src/es6/readable-stream/passthrough.js +15 -0
- package/src/es6/readable-stream/readable.js +896 -0
- package/src/es6/readable-stream/transform.js +174 -0
- package/src/es6/readable-stream/writable.js +483 -0
- package/src/es6/setimmediate.js +185 -0
- package/src/es6/stream.js +110 -0
- package/src/es6/string-decoder.js +220 -0
- package/src/es6/timers.js +76 -0
- package/src/es6/tty.js +20 -0
- package/src/es6/url.js +745 -0
- package/src/es6/util.js +598 -0
- package/src/es6/vm.js +202 -0
- package/src/es6/zlib-lib/LICENSE +21 -0
- package/src/es6/zlib-lib/adler32.js +31 -0
- package/src/es6/zlib-lib/binding.js +269 -0
- package/src/es6/zlib-lib/crc32.js +40 -0
- package/src/es6/zlib-lib/deflate.js +1862 -0
- package/src/es6/zlib-lib/inffast.js +325 -0
- package/src/es6/zlib-lib/inflate.js +1650 -0
- package/src/es6/zlib-lib/inftrees.js +329 -0
- package/src/es6/zlib-lib/messages.js +11 -0
- package/src/es6/zlib-lib/trees.js +1220 -0
- package/src/es6/zlib-lib/utils.js +73 -0
- package/src/es6/zlib-lib/zstream.js +28 -0
- package/src/es6/zlib.js +635 -0
- package/src/index.js +73 -0
@@ -0,0 +1,278 @@
|
|
1
|
+
import * as capability from './capability';
|
2
|
+
import {inherits} from 'util';
|
3
|
+
import {IncomingMessage, readyStates as rStates} from './response';
|
4
|
+
import {Writable} from 'stream';
|
5
|
+
import toArrayBuffer from './to-arraybuffer';
|
6
|
+
|
7
|
+
function decideMode(preferBinary, useFetch) {
|
8
|
+
if (capability.hasFetch && useFetch) {
|
9
|
+
return 'fetch'
|
10
|
+
} else if (capability.mozchunkedarraybuffer) {
|
11
|
+
return 'moz-chunked-arraybuffer'
|
12
|
+
} else if (capability.msstream) {
|
13
|
+
return 'ms-stream'
|
14
|
+
} else if (capability.arraybuffer && preferBinary) {
|
15
|
+
return 'arraybuffer'
|
16
|
+
} else if (capability.vbArray && preferBinary) {
|
17
|
+
return 'text:vbarray'
|
18
|
+
} else {
|
19
|
+
return 'text'
|
20
|
+
}
|
21
|
+
}
|
22
|
+
export default ClientRequest;
|
23
|
+
|
24
|
+
function ClientRequest(opts) {
|
25
|
+
var self = this
|
26
|
+
Writable.call(self)
|
27
|
+
|
28
|
+
self._opts = opts
|
29
|
+
self._body = []
|
30
|
+
self._headers = {}
|
31
|
+
if (opts.auth)
|
32
|
+
self.setHeader('Authorization', 'Basic ' + new Buffer(opts.auth).toString('base64'))
|
33
|
+
Object.keys(opts.headers).forEach(function(name) {
|
34
|
+
self.setHeader(name, opts.headers[name])
|
35
|
+
})
|
36
|
+
|
37
|
+
var preferBinary
|
38
|
+
var useFetch = true
|
39
|
+
if (opts.mode === 'disable-fetch') {
|
40
|
+
// If the use of XHR should be preferred and includes preserving the 'content-type' header
|
41
|
+
useFetch = false
|
42
|
+
preferBinary = true
|
43
|
+
} else if (opts.mode === 'prefer-streaming') {
|
44
|
+
// If streaming is a high priority but binary compatibility and
|
45
|
+
// the accuracy of the 'content-type' header aren't
|
46
|
+
preferBinary = false
|
47
|
+
} else if (opts.mode === 'allow-wrong-content-type') {
|
48
|
+
// If streaming is more important than preserving the 'content-type' header
|
49
|
+
preferBinary = !capability.overrideMimeType
|
50
|
+
} else if (!opts.mode || opts.mode === 'default' || opts.mode === 'prefer-fast') {
|
51
|
+
// Use binary if text streaming may corrupt data or the content-type header, or for speed
|
52
|
+
preferBinary = true
|
53
|
+
} else {
|
54
|
+
throw new Error('Invalid value for opts.mode')
|
55
|
+
}
|
56
|
+
self._mode = decideMode(preferBinary, useFetch)
|
57
|
+
|
58
|
+
self.on('finish', function() {
|
59
|
+
self._onFinish()
|
60
|
+
})
|
61
|
+
}
|
62
|
+
|
63
|
+
inherits(ClientRequest, Writable)
|
64
|
+
// Taken from http://www.w3.org/TR/XMLHttpRequest/#the-setrequestheader%28%29-method
|
65
|
+
var unsafeHeaders = [
|
66
|
+
'accept-charset',
|
67
|
+
'accept-encoding',
|
68
|
+
'access-control-request-headers',
|
69
|
+
'access-control-request-method',
|
70
|
+
'connection',
|
71
|
+
'content-length',
|
72
|
+
'cookie',
|
73
|
+
'cookie2',
|
74
|
+
'date',
|
75
|
+
'dnt',
|
76
|
+
'expect',
|
77
|
+
'host',
|
78
|
+
'keep-alive',
|
79
|
+
'origin',
|
80
|
+
'referer',
|
81
|
+
'te',
|
82
|
+
'trailer',
|
83
|
+
'transfer-encoding',
|
84
|
+
'upgrade',
|
85
|
+
'user-agent',
|
86
|
+
'via'
|
87
|
+
]
|
88
|
+
ClientRequest.prototype.setHeader = function(name, value) {
|
89
|
+
var self = this
|
90
|
+
var lowerName = name.toLowerCase()
|
91
|
+
// This check is not necessary, but it prevents warnings from browsers about setting unsafe
|
92
|
+
// headers. To be honest I'm not entirely sure hiding these warnings is a good thing, but
|
93
|
+
// http-browserify did it, so I will too.
|
94
|
+
if (unsafeHeaders.indexOf(lowerName) !== -1)
|
95
|
+
return
|
96
|
+
|
97
|
+
self._headers[lowerName] = {
|
98
|
+
name: name,
|
99
|
+
value: value
|
100
|
+
}
|
101
|
+
}
|
102
|
+
|
103
|
+
ClientRequest.prototype.getHeader = function(name) {
|
104
|
+
var self = this
|
105
|
+
return self._headers[name.toLowerCase()].value
|
106
|
+
}
|
107
|
+
|
108
|
+
ClientRequest.prototype.removeHeader = function(name) {
|
109
|
+
var self = this
|
110
|
+
delete self._headers[name.toLowerCase()]
|
111
|
+
}
|
112
|
+
|
113
|
+
ClientRequest.prototype._onFinish = function() {
|
114
|
+
var self = this
|
115
|
+
|
116
|
+
if (self._destroyed)
|
117
|
+
return
|
118
|
+
var opts = self._opts
|
119
|
+
|
120
|
+
var headersObj = self._headers
|
121
|
+
var body
|
122
|
+
if (opts.method === 'POST' || opts.method === 'PUT' || opts.method === 'PATCH') {
|
123
|
+
if (capability.blobConstructor()) {
|
124
|
+
body = new global.Blob(self._body.map(function(buffer) {
|
125
|
+
return toArrayBuffer(buffer)
|
126
|
+
}), {
|
127
|
+
type: (headersObj['content-type'] || {}).value || ''
|
128
|
+
})
|
129
|
+
} else {
|
130
|
+
// get utf8 string
|
131
|
+
body = Buffer.concat(self._body).toString()
|
132
|
+
}
|
133
|
+
}
|
134
|
+
|
135
|
+
if (self._mode === 'fetch') {
|
136
|
+
var headers = Object.keys(headersObj).map(function(name) {
|
137
|
+
return [headersObj[name].name, headersObj[name].value]
|
138
|
+
})
|
139
|
+
|
140
|
+
global.fetch(self._opts.url, {
|
141
|
+
method: self._opts.method,
|
142
|
+
headers: headers,
|
143
|
+
body: body,
|
144
|
+
mode: 'cors',
|
145
|
+
credentials: opts.withCredentials ? 'include' : 'same-origin'
|
146
|
+
}).then(function(response) {
|
147
|
+
self._fetchResponse = response
|
148
|
+
self._connect()
|
149
|
+
}, function(reason) {
|
150
|
+
self.emit('error', reason)
|
151
|
+
})
|
152
|
+
} else {
|
153
|
+
var xhr = self._xhr = new global.XMLHttpRequest()
|
154
|
+
try {
|
155
|
+
xhr.open(self._opts.method, self._opts.url, true)
|
156
|
+
} catch (err) {
|
157
|
+
process.nextTick(function() {
|
158
|
+
self.emit('error', err)
|
159
|
+
})
|
160
|
+
return
|
161
|
+
}
|
162
|
+
|
163
|
+
// Can't set responseType on really old browsers
|
164
|
+
if ('responseType' in xhr)
|
165
|
+
xhr.responseType = self._mode.split(':')[0]
|
166
|
+
|
167
|
+
if ('withCredentials' in xhr)
|
168
|
+
xhr.withCredentials = !!opts.withCredentials
|
169
|
+
|
170
|
+
if (self._mode === 'text' && 'overrideMimeType' in xhr)
|
171
|
+
xhr.overrideMimeType('text/plain; charset=x-user-defined')
|
172
|
+
|
173
|
+
Object.keys(headersObj).forEach(function(name) {
|
174
|
+
xhr.setRequestHeader(headersObj[name].name, headersObj[name].value)
|
175
|
+
})
|
176
|
+
|
177
|
+
self._response = null
|
178
|
+
xhr.onreadystatechange = function() {
|
179
|
+
switch (xhr.readyState) {
|
180
|
+
case rStates.LOADING:
|
181
|
+
case rStates.DONE:
|
182
|
+
self._onXHRProgress()
|
183
|
+
break
|
184
|
+
}
|
185
|
+
}
|
186
|
+
// Necessary for streaming in Firefox, since xhr.response is ONLY defined
|
187
|
+
// in onprogress, not in onreadystatechange with xhr.readyState = 3
|
188
|
+
if (self._mode === 'moz-chunked-arraybuffer') {
|
189
|
+
xhr.onprogress = function() {
|
190
|
+
self._onXHRProgress()
|
191
|
+
}
|
192
|
+
}
|
193
|
+
|
194
|
+
xhr.onerror = function() {
|
195
|
+
if (self._destroyed)
|
196
|
+
return
|
197
|
+
self.emit('error', new Error('XHR error'))
|
198
|
+
}
|
199
|
+
|
200
|
+
try {
|
201
|
+
xhr.send(body)
|
202
|
+
} catch (err) {
|
203
|
+
process.nextTick(function() {
|
204
|
+
self.emit('error', err)
|
205
|
+
})
|
206
|
+
return
|
207
|
+
}
|
208
|
+
}
|
209
|
+
}
|
210
|
+
|
211
|
+
/**
|
212
|
+
* Checks if xhr.status is readable and non-zero, indicating no error.
|
213
|
+
* Even though the spec says it should be available in readyState 3,
|
214
|
+
* accessing it throws an exception in IE8
|
215
|
+
*/
|
216
|
+
function statusValid(xhr) {
|
217
|
+
try {
|
218
|
+
var status = xhr.status
|
219
|
+
return (status !== null && status !== 0)
|
220
|
+
} catch (e) {
|
221
|
+
return false
|
222
|
+
}
|
223
|
+
}
|
224
|
+
|
225
|
+
ClientRequest.prototype._onXHRProgress = function() {
|
226
|
+
var self = this
|
227
|
+
|
228
|
+
if (!statusValid(self._xhr) || self._destroyed)
|
229
|
+
return
|
230
|
+
|
231
|
+
if (!self._response)
|
232
|
+
self._connect()
|
233
|
+
|
234
|
+
self._response._onXHRProgress()
|
235
|
+
}
|
236
|
+
|
237
|
+
ClientRequest.prototype._connect = function() {
|
238
|
+
var self = this
|
239
|
+
|
240
|
+
if (self._destroyed)
|
241
|
+
return
|
242
|
+
|
243
|
+
self._response = new IncomingMessage(self._xhr, self._fetchResponse, self._mode)
|
244
|
+
self.emit('response', self._response)
|
245
|
+
}
|
246
|
+
|
247
|
+
ClientRequest.prototype._write = function(chunk, encoding, cb) {
|
248
|
+
var self = this
|
249
|
+
|
250
|
+
self._body.push(chunk)
|
251
|
+
cb()
|
252
|
+
}
|
253
|
+
|
254
|
+
ClientRequest.prototype.abort = ClientRequest.prototype.destroy = function() {
|
255
|
+
var self = this
|
256
|
+
self._destroyed = true
|
257
|
+
if (self._response)
|
258
|
+
self._response._destroyed = true
|
259
|
+
if (self._xhr)
|
260
|
+
self._xhr.abort()
|
261
|
+
// Currently, there isn't a way to truly abort a fetch.
|
262
|
+
// If you like bikeshedding, see https://github.com/whatwg/fetch/issues/27
|
263
|
+
}
|
264
|
+
|
265
|
+
ClientRequest.prototype.end = function(data, encoding, cb) {
|
266
|
+
var self = this
|
267
|
+
if (typeof data === 'function') {
|
268
|
+
cb = data
|
269
|
+
data = undefined
|
270
|
+
}
|
271
|
+
|
272
|
+
Writable.prototype.end.call(self, data, encoding, cb)
|
273
|
+
}
|
274
|
+
|
275
|
+
ClientRequest.prototype.flushHeaders = function() {}
|
276
|
+
ClientRequest.prototype.setTimeout = function() {}
|
277
|
+
ClientRequest.prototype.setNoDelay = function() {}
|
278
|
+
ClientRequest.prototype.setSocketKeepAlive = function() {}
|
@@ -0,0 +1,185 @@
|
|
1
|
+
import {overrideMimeType} from './capability';
|
2
|
+
import {inherits} from 'util';
|
3
|
+
import {Readable} from 'stream';
|
4
|
+
|
5
|
+
var rStates = {
|
6
|
+
UNSENT: 0,
|
7
|
+
OPENED: 1,
|
8
|
+
HEADERS_RECEIVED: 2,
|
9
|
+
LOADING: 3,
|
10
|
+
DONE: 4
|
11
|
+
}
|
12
|
+
export {
|
13
|
+
rStates as readyStates
|
14
|
+
};
|
15
|
+
export function IncomingMessage(xhr, response, mode) {
|
16
|
+
var self = this
|
17
|
+
Readable.call(self)
|
18
|
+
|
19
|
+
self._mode = mode
|
20
|
+
self.headers = {}
|
21
|
+
self.rawHeaders = []
|
22
|
+
self.trailers = {}
|
23
|
+
self.rawTrailers = []
|
24
|
+
|
25
|
+
// Fake the 'close' event, but only once 'end' fires
|
26
|
+
self.on('end', function() {
|
27
|
+
// The nextTick is necessary to prevent the 'request' module from causing an infinite loop
|
28
|
+
process.nextTick(function() {
|
29
|
+
self.emit('close')
|
30
|
+
})
|
31
|
+
})
|
32
|
+
var read;
|
33
|
+
if (mode === 'fetch') {
|
34
|
+
self._fetchResponse = response
|
35
|
+
|
36
|
+
self.url = response.url
|
37
|
+
self.statusCode = response.status
|
38
|
+
self.statusMessage = response.statusText
|
39
|
+
// backwards compatible version of for (<item> of <iterable>):
|
40
|
+
// for (var <item>,_i,_it = <iterable>[Symbol.iterator](); <item> = (_i = _it.next()).value,!_i.done;)
|
41
|
+
for (var header, _i, _it = response.headers[Symbol.iterator](); header = (_i = _it.next()).value, !_i.done;) {
|
42
|
+
self.headers[header[0].toLowerCase()] = header[1]
|
43
|
+
self.rawHeaders.push(header[0], header[1])
|
44
|
+
}
|
45
|
+
|
46
|
+
// TODO: this doesn't respect backpressure. Once WritableStream is available, this can be fixed
|
47
|
+
var reader = response.body.getReader()
|
48
|
+
|
49
|
+
read = function () {
|
50
|
+
reader.read().then(function(result) {
|
51
|
+
if (self._destroyed)
|
52
|
+
return
|
53
|
+
if (result.done) {
|
54
|
+
self.push(null)
|
55
|
+
return
|
56
|
+
}
|
57
|
+
self.push(new Buffer(result.value))
|
58
|
+
read()
|
59
|
+
})
|
60
|
+
}
|
61
|
+
read()
|
62
|
+
|
63
|
+
} else {
|
64
|
+
self._xhr = xhr
|
65
|
+
self._pos = 0
|
66
|
+
|
67
|
+
self.url = xhr.responseURL
|
68
|
+
self.statusCode = xhr.status
|
69
|
+
self.statusMessage = xhr.statusText
|
70
|
+
var headers = xhr.getAllResponseHeaders().split(/\r?\n/)
|
71
|
+
headers.forEach(function(header) {
|
72
|
+
var matches = header.match(/^([^:]+):\s*(.*)/)
|
73
|
+
if (matches) {
|
74
|
+
var key = matches[1].toLowerCase()
|
75
|
+
if (key === 'set-cookie') {
|
76
|
+
if (self.headers[key] === undefined) {
|
77
|
+
self.headers[key] = []
|
78
|
+
}
|
79
|
+
self.headers[key].push(matches[2])
|
80
|
+
} else if (self.headers[key] !== undefined) {
|
81
|
+
self.headers[key] += ', ' + matches[2]
|
82
|
+
} else {
|
83
|
+
self.headers[key] = matches[2]
|
84
|
+
}
|
85
|
+
self.rawHeaders.push(matches[1], matches[2])
|
86
|
+
}
|
87
|
+
})
|
88
|
+
|
89
|
+
self._charset = 'x-user-defined'
|
90
|
+
if (!overrideMimeType) {
|
91
|
+
var mimeType = self.rawHeaders['mime-type']
|
92
|
+
if (mimeType) {
|
93
|
+
var charsetMatch = mimeType.match(/;\s*charset=([^;])(;|$)/)
|
94
|
+
if (charsetMatch) {
|
95
|
+
self._charset = charsetMatch[1].toLowerCase()
|
96
|
+
}
|
97
|
+
}
|
98
|
+
if (!self._charset)
|
99
|
+
self._charset = 'utf-8' // best guess
|
100
|
+
}
|
101
|
+
}
|
102
|
+
}
|
103
|
+
|
104
|
+
inherits(IncomingMessage, Readable)
|
105
|
+
|
106
|
+
IncomingMessage.prototype._read = function() {}
|
107
|
+
|
108
|
+
IncomingMessage.prototype._onXHRProgress = function() {
|
109
|
+
var self = this
|
110
|
+
|
111
|
+
var xhr = self._xhr
|
112
|
+
|
113
|
+
var response = null
|
114
|
+
switch (self._mode) {
|
115
|
+
case 'text:vbarray': // For IE9
|
116
|
+
if (xhr.readyState !== rStates.DONE)
|
117
|
+
break
|
118
|
+
try {
|
119
|
+
// This fails in IE8
|
120
|
+
response = new global.VBArray(xhr.responseBody).toArray()
|
121
|
+
} catch (e) {
|
122
|
+
// pass
|
123
|
+
}
|
124
|
+
if (response !== null) {
|
125
|
+
self.push(new Buffer(response))
|
126
|
+
break
|
127
|
+
}
|
128
|
+
// Falls through in IE8
|
129
|
+
case 'text':
|
130
|
+
try { // This will fail when readyState = 3 in IE9. Switch mode and wait for readyState = 4
|
131
|
+
response = xhr.responseText
|
132
|
+
} catch (e) {
|
133
|
+
self._mode = 'text:vbarray'
|
134
|
+
break
|
135
|
+
}
|
136
|
+
if (response.length > self._pos) {
|
137
|
+
var newData = response.substr(self._pos)
|
138
|
+
if (self._charset === 'x-user-defined') {
|
139
|
+
var buffer = new Buffer(newData.length)
|
140
|
+
for (var i = 0; i < newData.length; i++)
|
141
|
+
buffer[i] = newData.charCodeAt(i) & 0xff
|
142
|
+
|
143
|
+
self.push(buffer)
|
144
|
+
} else {
|
145
|
+
self.push(newData, self._charset)
|
146
|
+
}
|
147
|
+
self._pos = response.length
|
148
|
+
}
|
149
|
+
break
|
150
|
+
case 'arraybuffer':
|
151
|
+
if (xhr.readyState !== rStates.DONE || !xhr.response)
|
152
|
+
break
|
153
|
+
response = xhr.response
|
154
|
+
self.push(new Buffer(new Uint8Array(response)))
|
155
|
+
break
|
156
|
+
case 'moz-chunked-arraybuffer': // take whole
|
157
|
+
response = xhr.response
|
158
|
+
if (xhr.readyState !== rStates.LOADING || !response)
|
159
|
+
break
|
160
|
+
self.push(new Buffer(new Uint8Array(response)))
|
161
|
+
break
|
162
|
+
case 'ms-stream':
|
163
|
+
response = xhr.response
|
164
|
+
if (xhr.readyState !== rStates.LOADING)
|
165
|
+
break
|
166
|
+
var reader = new global.MSStreamReader()
|
167
|
+
reader.onprogress = function() {
|
168
|
+
if (reader.result.byteLength > self._pos) {
|
169
|
+
self.push(new Buffer(new Uint8Array(reader.result.slice(self._pos))))
|
170
|
+
self._pos = reader.result.byteLength
|
171
|
+
}
|
172
|
+
}
|
173
|
+
reader.onload = function() {
|
174
|
+
self.push(null)
|
175
|
+
}
|
176
|
+
// reader.onerror = ??? // TODO: this
|
177
|
+
reader.readAsArrayBuffer(response)
|
178
|
+
break
|
179
|
+
}
|
180
|
+
|
181
|
+
// The ms-stream case handles end separately in reader.onload()
|
182
|
+
if (self._xhr.readyState === rStates.DONE && self._mode !== 'ms-stream') {
|
183
|
+
self.push(null)
|
184
|
+
}
|
185
|
+
}
|
@@ -0,0 +1,30 @@
|
|
1
|
+
// from https://github.com/jhiesey/to-arraybuffer/blob/6502d9850e70ba7935a7df4ad86b358fc216f9f0/index.js
|
2
|
+
|
3
|
+
// MIT License
|
4
|
+
// Copyright (c) 2016 John Hiesey
|
5
|
+
import {isBuffer} from 'buffer';
|
6
|
+
export default function (buf) {
|
7
|
+
// If the buffer is backed by a Uint8Array, a faster version will work
|
8
|
+
if (buf instanceof Uint8Array) {
|
9
|
+
// If the buffer isn't a subarray, return the underlying ArrayBuffer
|
10
|
+
if (buf.byteOffset === 0 && buf.byteLength === buf.buffer.byteLength) {
|
11
|
+
return buf.buffer
|
12
|
+
} else if (typeof buf.buffer.slice === 'function') {
|
13
|
+
// Otherwise we need to get a proper copy
|
14
|
+
return buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.byteLength)
|
15
|
+
}
|
16
|
+
}
|
17
|
+
|
18
|
+
if (isBuffer(buf)) {
|
19
|
+
// This is the slow version that will work with any Buffer
|
20
|
+
// implementation (even in old browsers)
|
21
|
+
var arrayCopy = new Uint8Array(buf.length)
|
22
|
+
var len = buf.length
|
23
|
+
for (var i = 0; i < len; i++) {
|
24
|
+
arrayCopy[i] = buf[i]
|
25
|
+
}
|
26
|
+
return arrayCopy.buffer
|
27
|
+
} else {
|
28
|
+
throw new Error('Argument must be a Buffer')
|
29
|
+
}
|
30
|
+
}
|
package/src/es6/http.js
ADDED
@@ -0,0 +1,167 @@
|
|
1
|
+
/*
|
2
|
+
this and http-lib folder
|
3
|
+
|
4
|
+
The MIT License
|
5
|
+
|
6
|
+
Copyright (c) 2015 John Hiesey
|
7
|
+
|
8
|
+
Permission is hereby granted, free of charge,
|
9
|
+
to any person obtaining a copy of this software and
|
10
|
+
associated documentation files (the "Software"), to
|
11
|
+
deal in the Software without restriction, including
|
12
|
+
without limitation the rights to use, copy, modify,
|
13
|
+
merge, publish, distribute, sublicense, and/or sell
|
14
|
+
copies of the Software, and to permit persons to whom
|
15
|
+
the Software is furnished to do so,
|
16
|
+
subject to the following conditions:
|
17
|
+
|
18
|
+
The above copyright notice and this permission notice
|
19
|
+
shall be included in all copies or substantial portions of the Software.
|
20
|
+
|
21
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
22
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
23
|
+
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
24
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
|
25
|
+
ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
26
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
27
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
28
|
+
|
29
|
+
*/
|
30
|
+
import ClientRequest from './http-lib/request';
|
31
|
+
import {parse} from 'url';
|
32
|
+
|
33
|
+
export function request(opts, cb) {
|
34
|
+
if (typeof opts === 'string')
|
35
|
+
opts = parse(opts)
|
36
|
+
|
37
|
+
|
38
|
+
// Normally, the page is loaded from http or https, so not specifying a protocol
|
39
|
+
// will result in a (valid) protocol-relative url. However, this won't work if
|
40
|
+
// the protocol is something else, like 'file:'
|
41
|
+
var defaultProtocol = global.location.protocol.search(/^https?:$/) === -1 ? 'http:' : ''
|
42
|
+
|
43
|
+
var protocol = opts.protocol || defaultProtocol
|
44
|
+
var host = opts.hostname || opts.host
|
45
|
+
var port = opts.port
|
46
|
+
var path = opts.path || '/'
|
47
|
+
|
48
|
+
// Necessary for IPv6 addresses
|
49
|
+
if (host && host.indexOf(':') !== -1)
|
50
|
+
host = '[' + host + ']'
|
51
|
+
|
52
|
+
// This may be a relative url. The browser should always be able to interpret it correctly.
|
53
|
+
opts.url = (host ? (protocol + '//' + host) : '') + (port ? ':' + port : '') + path
|
54
|
+
opts.method = (opts.method || 'GET').toUpperCase()
|
55
|
+
opts.headers = opts.headers || {}
|
56
|
+
|
57
|
+
// Also valid opts.auth, opts.mode
|
58
|
+
|
59
|
+
var req = new ClientRequest(opts)
|
60
|
+
if (cb)
|
61
|
+
req.on('response', cb)
|
62
|
+
return req
|
63
|
+
}
|
64
|
+
|
65
|
+
export function get(opts, cb) {
|
66
|
+
var req = request(opts, cb)
|
67
|
+
req.end()
|
68
|
+
return req
|
69
|
+
}
|
70
|
+
|
71
|
+
export function Agent() {}
|
72
|
+
Agent.defaultMaxSockets = 4
|
73
|
+
|
74
|
+
export var METHODS = [
|
75
|
+
'CHECKOUT',
|
76
|
+
'CONNECT',
|
77
|
+
'COPY',
|
78
|
+
'DELETE',
|
79
|
+
'GET',
|
80
|
+
'HEAD',
|
81
|
+
'LOCK',
|
82
|
+
'M-SEARCH',
|
83
|
+
'MERGE',
|
84
|
+
'MKACTIVITY',
|
85
|
+
'MKCOL',
|
86
|
+
'MOVE',
|
87
|
+
'NOTIFY',
|
88
|
+
'OPTIONS',
|
89
|
+
'PATCH',
|
90
|
+
'POST',
|
91
|
+
'PROPFIND',
|
92
|
+
'PROPPATCH',
|
93
|
+
'PURGE',
|
94
|
+
'PUT',
|
95
|
+
'REPORT',
|
96
|
+
'SEARCH',
|
97
|
+
'SUBSCRIBE',
|
98
|
+
'TRACE',
|
99
|
+
'UNLOCK',
|
100
|
+
'UNSUBSCRIBE'
|
101
|
+
]
|
102
|
+
export var STATUS_CODES = {
|
103
|
+
100: 'Continue',
|
104
|
+
101: 'Switching Protocols',
|
105
|
+
102: 'Processing', // RFC 2518, obsoleted by RFC 4918
|
106
|
+
200: 'OK',
|
107
|
+
201: 'Created',
|
108
|
+
202: 'Accepted',
|
109
|
+
203: 'Non-Authoritative Information',
|
110
|
+
204: 'No Content',
|
111
|
+
205: 'Reset Content',
|
112
|
+
206: 'Partial Content',
|
113
|
+
207: 'Multi-Status', // RFC 4918
|
114
|
+
300: 'Multiple Choices',
|
115
|
+
301: 'Moved Permanently',
|
116
|
+
302: 'Moved Temporarily',
|
117
|
+
303: 'See Other',
|
118
|
+
304: 'Not Modified',
|
119
|
+
305: 'Use Proxy',
|
120
|
+
307: 'Temporary Redirect',
|
121
|
+
400: 'Bad Request',
|
122
|
+
401: 'Unauthorized',
|
123
|
+
402: 'Payment Required',
|
124
|
+
403: 'Forbidden',
|
125
|
+
404: 'Not Found',
|
126
|
+
405: 'Method Not Allowed',
|
127
|
+
406: 'Not Acceptable',
|
128
|
+
407: 'Proxy Authentication Required',
|
129
|
+
408: 'Request Time-out',
|
130
|
+
409: 'Conflict',
|
131
|
+
410: 'Gone',
|
132
|
+
411: 'Length Required',
|
133
|
+
412: 'Precondition Failed',
|
134
|
+
413: 'Request Entity Too Large',
|
135
|
+
414: 'Request-URI Too Large',
|
136
|
+
415: 'Unsupported Media Type',
|
137
|
+
416: 'Requested Range Not Satisfiable',
|
138
|
+
417: 'Expectation Failed',
|
139
|
+
418: 'I\'m a teapot', // RFC 2324
|
140
|
+
422: 'Unprocessable Entity', // RFC 4918
|
141
|
+
423: 'Locked', // RFC 4918
|
142
|
+
424: 'Failed Dependency', // RFC 4918
|
143
|
+
425: 'Unordered Collection', // RFC 4918
|
144
|
+
426: 'Upgrade Required', // RFC 2817
|
145
|
+
428: 'Precondition Required', // RFC 6585
|
146
|
+
429: 'Too Many Requests', // RFC 6585
|
147
|
+
431: 'Request Header Fields Too Large', // RFC 6585
|
148
|
+
500: 'Internal Server Error',
|
149
|
+
501: 'Not Implemented',
|
150
|
+
502: 'Bad Gateway',
|
151
|
+
503: 'Service Unavailable',
|
152
|
+
504: 'Gateway Time-out',
|
153
|
+
505: 'HTTP Version Not Supported',
|
154
|
+
506: 'Variant Also Negotiates', // RFC 2295
|
155
|
+
507: 'Insufficient Storage', // RFC 4918
|
156
|
+
509: 'Bandwidth Limit Exceeded',
|
157
|
+
510: 'Not Extended', // RFC 2774
|
158
|
+
511: 'Network Authentication Required' // RFC 6585
|
159
|
+
};
|
160
|
+
|
161
|
+
export default {
|
162
|
+
request,
|
163
|
+
get,
|
164
|
+
Agent,
|
165
|
+
METHODS,
|
166
|
+
STATUS_CODES
|
167
|
+
}
|
@@ -0,0 +1,25 @@
|
|
1
|
+
|
2
|
+
var inherits;
|
3
|
+
if (typeof Object.create === 'function'){
|
4
|
+
inherits = function inherits(ctor, superCtor) {
|
5
|
+
// implementation from standard node.js 'util' module
|
6
|
+
ctor.super_ = superCtor
|
7
|
+
ctor.prototype = Object.create(superCtor.prototype, {
|
8
|
+
constructor: {
|
9
|
+
value: ctor,
|
10
|
+
enumerable: false,
|
11
|
+
writable: true,
|
12
|
+
configurable: true
|
13
|
+
}
|
14
|
+
});
|
15
|
+
};
|
16
|
+
} else {
|
17
|
+
inherits = function inherits(ctor, superCtor) {
|
18
|
+
ctor.super_ = superCtor
|
19
|
+
var TempCtor = function () {}
|
20
|
+
TempCtor.prototype = superCtor.prototype
|
21
|
+
ctor.prototype = new TempCtor()
|
22
|
+
ctor.prototype.constructor = ctor
|
23
|
+
}
|
24
|
+
}
|
25
|
+
export default inherits;
|