rollup-packages-polyfill-core 0.5.0 → 0.13.0

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.
Files changed (77) hide show
  1. package/LICENSE.md +26 -0
  2. package/browser-test/index.js +20 -0
  3. package/browser-test/main.js +44 -0
  4. package/package.json +1 -1
  5. package/polyfills/LICENSE-browserify-fs.txt +472 -0
  6. package/polyfills/LICENSE-buffer-es6.txt +69 -0
  7. package/polyfills/LICENSE-crypto-browserify.txt +355 -0
  8. package/polyfills/LICENSE-process-es6.txt +32 -0
  9. package/polyfills/__http-lib/capability.js +52 -0
  10. package/polyfills/__http-lib/request.js +278 -0
  11. package/polyfills/__http-lib/response.js +185 -0
  12. package/polyfills/__http-lib/to-arraybuffer.js +30 -0
  13. package/polyfills/__readable-stream/buffer-list.js +59 -0
  14. package/polyfills/__readable-stream/duplex.js +45 -0
  15. package/polyfills/__readable-stream/passthrough.js +15 -0
  16. package/polyfills/__readable-stream/readable.js +896 -0
  17. package/polyfills/__readable-stream/transform.js +174 -0
  18. package/polyfills/__readable-stream/writable.js +483 -0
  19. package/polyfills/__zlib-lib/LICENSE +21 -0
  20. package/polyfills/__zlib-lib/adler32.js +31 -0
  21. package/polyfills/__zlib-lib/binding.js +290 -0
  22. package/polyfills/__zlib-lib/crc32.js +40 -0
  23. package/polyfills/__zlib-lib/deflate.js +1862 -0
  24. package/polyfills/__zlib-lib/inffast.js +325 -0
  25. package/polyfills/__zlib-lib/inflate.js +1650 -0
  26. package/polyfills/__zlib-lib/inftrees.js +329 -0
  27. package/polyfills/__zlib-lib/messages.js +11 -0
  28. package/polyfills/__zlib-lib/trees.js +1220 -0
  29. package/polyfills/__zlib-lib/utils.js +73 -0
  30. package/polyfills/__zlib-lib/zstream.js +28 -0
  31. package/polyfills/assert.js +488 -0
  32. package/polyfills/buffer-es6.js +1985 -0
  33. package/polyfills/console.js +13 -0
  34. package/polyfills/constants.js +498 -0
  35. package/polyfills/domain.js +100 -0
  36. package/polyfills/empty.js +1 -0
  37. package/polyfills/events.js +481 -0
  38. package/polyfills/global.js +3 -0
  39. package/polyfills/http.js +167 -0
  40. package/polyfills/inherits.js +25 -0
  41. package/polyfills/os.js +126 -0
  42. package/polyfills/path.js +234 -0
  43. package/polyfills/process-es6.js +223 -0
  44. package/polyfills/punycode.js +475 -0
  45. package/polyfills/querystring.js +147 -0
  46. package/polyfills/setimmediate.js +185 -0
  47. package/polyfills/stream.js +110 -0
  48. package/polyfills/string-decoder.js +220 -0
  49. package/polyfills/timers.js +76 -0
  50. package/polyfills/tty.js +20 -0
  51. package/polyfills/url.js +785 -0
  52. package/polyfills/util.js +716 -0
  53. package/polyfills/vm.js +202 -0
  54. package/polyfills/zlib.js +635 -0
  55. package/rollup.config.mjs +18 -0
  56. package/scripts/build-constants.js +18 -0
  57. package/scripts/build-polyfills.js +52 -0
  58. package/scripts/collect-polyfills.js +17 -0
  59. package/src/index.ts +92 -0
  60. package/src/modules.ts +61 -0
  61. package/src/polyfills.ts +1 -0
  62. package/test/examples/assert.js +12 -0
  63. package/test/examples/constants.js +7 -0
  64. package/test/examples/crypto-broken.js +2 -0
  65. package/test/examples/crypto.js +7 -0
  66. package/test/examples/domain.js +33 -0
  67. package/test/examples/events.js +7 -0
  68. package/test/examples/os.js +7 -0
  69. package/test/examples/path.js +11 -0
  70. package/test/examples/stream.js +31 -0
  71. package/test/examples/string-decoder.js +19 -0
  72. package/test/examples/url-file-url-to-path.js +11 -0
  73. package/test/examples/url-format.js +13 -0
  74. package/test/examples/url-parse.js +14 -0
  75. package/test/examples/zlib.js +24 -0
  76. package/test/index.js +77 -0
  77. package/tsconfig.json +14 -0
@@ -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
+ }
@@ -0,0 +1,59 @@
1
+ import {Buffer} from 'buffer';
2
+
3
+ export default BufferList;
4
+
5
+ function BufferList() {
6
+ this.head = null;
7
+ this.tail = null;
8
+ this.length = 0;
9
+ }
10
+
11
+ BufferList.prototype.push = function (v) {
12
+ var entry = { data: v, next: null };
13
+ if (this.length > 0) this.tail.next = entry;else this.head = entry;
14
+ this.tail = entry;
15
+ ++this.length;
16
+ };
17
+
18
+ BufferList.prototype.unshift = function (v) {
19
+ var entry = { data: v, next: this.head };
20
+ if (this.length === 0) this.tail = entry;
21
+ this.head = entry;
22
+ ++this.length;
23
+ };
24
+
25
+ BufferList.prototype.shift = function () {
26
+ if (this.length === 0) return;
27
+ var ret = this.head.data;
28
+ if (this.length === 1) this.head = this.tail = null;else this.head = this.head.next;
29
+ --this.length;
30
+ return ret;
31
+ };
32
+
33
+ BufferList.prototype.clear = function () {
34
+ this.head = this.tail = null;
35
+ this.length = 0;
36
+ };
37
+
38
+ BufferList.prototype.join = function (s) {
39
+ if (this.length === 0) return '';
40
+ var p = this.head;
41
+ var ret = '' + p.data;
42
+ while (p = p.next) {
43
+ ret += s + p.data;
44
+ }return ret;
45
+ };
46
+
47
+ BufferList.prototype.concat = function (n) {
48
+ if (this.length === 0) return Buffer.alloc(0);
49
+ if (this.length === 1) return this.head.data;
50
+ var ret = Buffer.allocUnsafe(n >>> 0);
51
+ var p = this.head;
52
+ var i = 0;
53
+ while (p) {
54
+ p.data.copy(ret, i);
55
+ i += p.data.length;
56
+ p = p.next;
57
+ }
58
+ return ret;
59
+ };
@@ -0,0 +1,45 @@
1
+
2
+ import {inherits} from 'util';
3
+ import {nextTick} from 'process';
4
+ import {Readable} from '\0polyfill-node._stream_readable';
5
+ import {Writable} from '\0polyfill-node._stream_writable';
6
+
7
+
8
+ inherits(Duplex, Readable);
9
+
10
+ var keys = Object.keys(Writable.prototype);
11
+ for (var v = 0; v < keys.length; v++) {
12
+ var method = keys[v];
13
+ if (!Duplex.prototype[method]) Duplex.prototype[method] = Writable.prototype[method];
14
+ }
15
+ export default Duplex;
16
+ export function Duplex(options) {
17
+ if (!(this instanceof Duplex)) return new Duplex(options);
18
+
19
+ Readable.call(this, options);
20
+ Writable.call(this, options);
21
+
22
+ if (options && options.readable === false) this.readable = false;
23
+
24
+ if (options && options.writable === false) this.writable = false;
25
+
26
+ this.allowHalfOpen = true;
27
+ if (options && options.allowHalfOpen === false) this.allowHalfOpen = false;
28
+
29
+ this.once('end', onend);
30
+ }
31
+
32
+ // the no-half-open enforcer
33
+ function onend() {
34
+ // if we allow half-open state, or if the writable side ended,
35
+ // then we're ok.
36
+ if (this.allowHalfOpen || this._writableState.ended) return;
37
+
38
+ // no more data can be written.
39
+ // But allow more writes to happen in this tick.
40
+ nextTick(onEndNT, this);
41
+ }
42
+
43
+ function onEndNT(self) {
44
+ self.end();
45
+ }
@@ -0,0 +1,15 @@
1
+
2
+ import {Transform} from '\0polyfill-node._stream_transform';
3
+
4
+ import {inherits} from 'util';
5
+ inherits(PassThrough, Transform);
6
+ export default PassThrough;
7
+ export function PassThrough(options) {
8
+ if (!(this instanceof PassThrough)) return new PassThrough(options);
9
+
10
+ Transform.call(this, options);
11
+ }
12
+
13
+ PassThrough.prototype._transform = function (chunk, encoding, cb) {
14
+ cb(null, chunk);
15
+ };