This repository was archived by the owner on Oct 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathtext-encode-transform.js
More file actions
160 lines (140 loc) · 5.18 KB
/
text-encode-transform.js
File metadata and controls
160 lines (140 loc) · 5.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// Copyright 2016 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Polyfill for TextEncoderStream and TextDecoderStream
(function() {
'use strict';
if (typeof self.TextEncoderStream === 'function' &&
typeof self.TextDecoderStream === 'function') {
// The constructors exist. Assume that they work and don't replace them.
return;
}
if (typeof self.TextEncoder !== 'function') {
throw new ReferenceError('TextEncoder implementation required');
}
if (typeof self.TextDecoder !== 'function') {
throw new ReferenceError('TextDecoder implementation required');
}
// These symbols end up being different for every realm, so mixing objects
// created in one realm with methods created in another fails.
const codec = Symbol('codec');
const transform = Symbol('transform');
class TextEncoderStream {
constructor() {
this[codec] = new TextEncoder();
this[transform] =
new TransformStream(new TextEncodeTransformer(this[codec]));
}
}
class TextDecoderStream {
constructor(label = undefined, options = undefined) {
this[codec] = new TextDecoder(label, options);
this[transform] = new TransformStream(
new TextDecodeTransformer(this[codec]));
}
}
// ECMAScript class syntax will create getters that are non-enumerable, but we
// need them to be enumerable in WebIDL-style, so we add them manually.
// "readable" and "writable" are always delegated to the TransformStream
// object. Properties specified in |properties| are delegated to the
// underlying TextEncoder or TextDecoder.
function addDelegatingProperties(prototype, properties) {
for (const transformProperty of ['readable', 'writable']) {
addGetter(prototype, transformProperty, function() {
return this[transform][transformProperty];
});
}
for (const codecProperty of properties) {
addGetter(prototype, codecProperty, function() {
return this[codec][codecProperty];
});
}
}
function addGetter(prototype, property, getter) {
Object.defineProperty(prototype, property,
{
configurable: true,
enumerable: true,
get: getter
});
}
addDelegatingProperties(TextEncoderStream.prototype, ['encoding']);
addDelegatingProperties(TextDecoderStream.prototype,
['encoding', 'fatal', 'ignoreBOM']);
class TextEncodeTransformer {
constructor() {
this._encoder = new TextEncoder();
this._carry = undefined;
}
transform(chunk, controller) {
chunk = String(chunk);
if (this._carry !== undefined) {
chunk = this._carry + chunk;
this._carry = undefined;
}
const terminalCodeUnit = chunk.charCodeAt(chunk.length - 1);
if (terminalCodeUnit >= 0xD800 && terminalCodeUnit < 0xDC00) {
this._carry = chunk.substring(chunk.length - 1);
chunk = chunk.substring(0, chunk.length - 1);
}
const encoded = this._encoder.encode(chunk);
if (encoded.length > 0) {
controller.enqueue(encoded);
}
}
flush(controller) {
if (this._carry !== undefined) {
controller.enqueue(this._encoder.encode(this._carry));
this._carry = undefined;
}
}
}
class TextDecodeTransformer {
constructor(decoder) {
this._decoder = new TextDecoder(decoder.encoding, {
fatal: decoder.fatal,
ignoreBOM: decoder.ignoreBOM
});
}
transform(chunk, controller) {
const decoded = this._decoder.decode(chunk, {stream: true});
if (decoded != '') {
controller.enqueue(decoded);
}
}
flush(controller) {
// If {fatal: false} is in options (the default), then the final call to
// decode() can produce extra output (usually the unicode replacement
// character 0xFFFD). When fatal is true, this call is just used for its
// side-effect of throwing a TypeError exception if the input is
// incomplete.
var output = this._decoder.decode();
if (output !== '') {
controller.enqueue(output);
}
}
}
function exportAs(name, value) {
// Make it stringify as [object <name>] rather than [object Object].
value.prototype[Symbol.toStringTag] = name;
Object.defineProperty(self, name,
{
configurable: true,
enumerable: false,
writable: true,
value
});
}
exportAs('TextEncoderStream', TextEncoderStream);
exportAs('TextDecoderStream', TextDecoderStream);
})();