Skip to content

Commit eab87ab

Browse files
fix(loaders): vendor decodeText for r165 deprecation (#371)
1 parent 880e2eb commit eab87ab

File tree

10 files changed

+52
-18
lines changed

10 files changed

+52
-18
lines changed

src/_polyfill/LoaderUtils.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
export function decodeText(array) {
2+
if (typeof TextDecoder !== 'undefined') {
3+
return new TextDecoder().decode(array)
4+
}
5+
6+
// Avoid the String.fromCharCode.apply(null, array) shortcut, which
7+
// throws a "maximum call stack size exceeded" error for large arrays.
8+
9+
let s = ''
10+
11+
for (let i = 0, il = array.length; i < il; i++) {
12+
// Implicitly assumes little-endian.
13+
s += String.fromCharCode(array[i])
14+
}
15+
16+
try {
17+
// merges multi-byte utf-8 characters.
18+
19+
return decodeURIComponent(escape(s))
20+
} catch (e) {
21+
// see https://github.com/mrdoob/three.js/issues/16358
22+
23+
return s
24+
}
25+
}

src/loaders/3MFLoader.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
TextureLoader,
2121
} from 'three'
2222
import { unzipSync } from 'fflate'
23+
import { decodeText } from '../_polyfill/LoaderUtils'
2324

2425
/**
2526
*
@@ -121,14 +122,14 @@ class ThreeMFLoader extends Loader {
121122
//
122123

123124
const relsView = zip[relsName]
124-
const relsFileText = LoaderUtils.decodeText(relsView)
125+
const relsFileText = decodeText(relsView)
125126
const rels = parseRelsXml(relsFileText)
126127

127128
//
128129

129130
if (modelRelsName) {
130131
const relsView = zip[modelRelsName]
131-
const relsFileText = LoaderUtils.decodeText(relsView)
132+
const relsFileText = decodeText(relsView)
132133
modelRels = parseRelsXml(relsFileText)
133134
}
134135

@@ -138,7 +139,7 @@ class ThreeMFLoader extends Loader {
138139
const modelPart = modelPartNames[i]
139140
const view = zip[modelPart]
140141

141-
const fileText = LoaderUtils.decodeText(view)
142+
const fileText = decodeText(view)
142143
const xmlData = new DOMParser().parseFromString(fileText, 'application/xml')
143144

144145
if (xmlData.documentElement.nodeName.toLowerCase() !== 'model') {

src/loaders/AMFLoader.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
MeshPhongMaterial,
1111
} from 'three'
1212
import { unzipSync } from 'fflate'
13+
import { decodeText } from '../_polyfill/LoaderUtils'
1314

1415
/**
1516
* Description: Early release of an AMF Loader following the pattern of the
@@ -90,7 +91,7 @@ class AMFLoader extends Loader {
9091
view = new DataView(zip[file].buffer)
9192
}
9293

93-
const fileText = LoaderUtils.decodeText(view)
94+
const fileText = decodeText(view)
9495
const xmlData = new DOMParser().parseFromString(fileText, 'application/xml')
9596

9697
if (xmlData.documentElement.nodeName.toLowerCase() !== 'amf') {

src/loaders/FBXLoader.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import {
4242
} from 'three'
4343
import { unzlibSync } from 'fflate'
4444
import { NURBSCurve } from '../curves/NURBSCurve'
45+
import { decodeText } from '../_polyfill/LoaderUtils'
4546

4647
/**
4748
* Loader loads FBX file and generates Group representing FBX scene.
@@ -3005,7 +3006,7 @@ class BinaryReader {
30053006
const nullByte = a.indexOf(0)
30063007
if (nullByte >= 0) a = a.slice(0, nullByte)
30073008

3008-
return LoaderUtils.decodeText(new Uint8Array(a))
3009+
return decodeText(new Uint8Array(a))
30093010
}
30103011
}
30113012

@@ -3268,7 +3269,7 @@ function convertArrayBufferToString(buffer, from, to) {
32683269
if (from === undefined) from = 0
32693270
if (to === undefined) to = buffer.byteLength
32703271

3271-
return LoaderUtils.decodeText(new Uint8Array(buffer, from, to))
3272+
return decodeText(new Uint8Array(buffer, from, to))
32723273
}
32733274

32743275
function append(a, b) {

src/loaders/GLTFLoader.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ import {
6464
} from 'three'
6565
import { toTrianglesDrawMode } from '../utils/BufferGeometryUtils'
6666
import { version } from '../_polyfill/constants'
67+
import { decodeText } from '../_polyfill/LoaderUtils'
6768

6869
const SRGBColorSpace = 'srgb'
6970
const LinearSRGBColorSpace = 'srgb-linear'
@@ -257,7 +258,7 @@ class GLTFLoader extends Loader {
257258
if (typeof data === 'string') {
258259
json = JSON.parse(data)
259260
} else if (data instanceof ArrayBuffer) {
260-
const magic = LoaderUtils.decodeText(new Uint8Array(data.slice(0, 4)))
261+
const magic = decodeText(new Uint8Array(data.slice(0, 4)))
261262

262263
if (magic === BINARY_EXTENSION_HEADER_MAGIC) {
263264
try {
@@ -269,7 +270,7 @@ class GLTFLoader extends Loader {
269270

270271
json = JSON.parse(extensions[EXTENSIONS.KHR_BINARY_GLTF].content)
271272
} else {
272-
json = JSON.parse(LoaderUtils.decodeText(new Uint8Array(data)))
273+
json = JSON.parse(decodeText(new Uint8Array(data)))
273274
}
274275
} else {
275276
json = data
@@ -1446,7 +1447,7 @@ class GLTFBinaryExtension {
14461447
const headerView = new DataView(data, 0, BINARY_EXTENSION_HEADER_LENGTH)
14471448

14481449
this.header = {
1449-
magic: LoaderUtils.decodeText(new Uint8Array(data.slice(0, 4))),
1450+
magic: decodeText(new Uint8Array(data.slice(0, 4))),
14501451
version: headerView.getUint32(4, true),
14511452
length: headerView.getUint32(8, true),
14521453
}
@@ -1470,7 +1471,7 @@ class GLTFBinaryExtension {
14701471

14711472
if (chunkType === BINARY_EXTENSION_CHUNK_TYPES.JSON) {
14721473
const contentArray = new Uint8Array(data, BINARY_EXTENSION_HEADER_LENGTH + chunkIndex, chunkLength)
1473-
this.content = LoaderUtils.decodeText(contentArray)
1474+
this.content = decodeText(contentArray)
14741475
} else if (chunkType === BINARY_EXTENSION_CHUNK_TYPES.BIN) {
14751476
const byteOffset = BINARY_EXTENSION_HEADER_LENGTH + chunkIndex
14761477
this.body = data.slice(byteOffset, byteOffset + chunkLength)

src/loaders/PCDLoader.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { BufferGeometry, FileLoader, Float32BufferAttribute, Loader, LoaderUtils, Points, PointsMaterial } from 'three'
2+
import { decodeText } from '../_polyfill/LoaderUtils'
23

34
class PCDLoader extends Loader {
45
constructor(manager) {
@@ -158,7 +159,7 @@ class PCDLoader extends Loader {
158159
return PCDheader
159160
}
160161

161-
const textData = LoaderUtils.decodeText(new Uint8Array(data))
162+
const textData = decodeText(new Uint8Array(data))
162163

163164
// parse header (always ascii format)
164165

src/loaders/PLYLoader.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { BufferGeometry, FileLoader, Float32BufferAttribute, Loader, LoaderUtils } from 'three'
2+
import { decodeText } from '../_polyfill/LoaderUtils'
23

34
/**
45
* Description: A THREE loader for PLY ASCII files (known as the Polygon
@@ -421,7 +422,7 @@ class PLYLoader extends Loader {
421422
const scope = this
422423

423424
if (data instanceof ArrayBuffer) {
424-
const text = LoaderUtils.decodeText(new Uint8Array(data))
425+
const text = decodeText(new Uint8Array(data))
425426
const header = parseHeader(text)
426427

427428
geometry = header.format === 'ascii' ? parseASCII(text, header) : parseBinary(data, header)

src/loaders/STLLoader.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
LoaderUtils,
88
Vector3,
99
} from 'three'
10+
import { decodeText } from '../_polyfill/LoaderUtils'
1011

1112
/**
1213
* Description: A THREE loader for STL ASCII files, as created by Solidworks and other CAD programs.
@@ -305,7 +306,7 @@ class STLLoader extends Loader {
305306

306307
function ensureString(buffer) {
307308
if (typeof buffer !== 'string') {
308-
return LoaderUtils.decodeText(new Uint8Array(buffer))
309+
return decodeText(new Uint8Array(buffer))
309310
}
310311

311312
return buffer

src/loaders/VTKLoader.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { BufferAttribute, BufferGeometry, FileLoader, Float32BufferAttribute, Loader, LoaderUtils } from 'three'
22
import { unzlibSync } from 'fflate'
3+
import { decodeText } from '../_polyfill/LoaderUtils'
34

45
class VTKLoader extends Loader {
56
constructor(manager) {
@@ -885,12 +886,12 @@ class VTKLoader extends Loader {
885886
}
886887

887888
// get the 5 first lines of the files to check if there is the key word binary
888-
var meta = LoaderUtils.decodeText(new Uint8Array(data, 0, 250)).split('\n')
889+
var meta = decodeText(new Uint8Array(data, 0, 250)).split('\n')
889890

890891
if (meta[0].indexOf('xml') !== -1) {
891-
return parseXML(LoaderUtils.decodeText(data))
892+
return parseXML(decodeText(data))
892893
} else if (meta[2].includes('ASCII')) {
893-
return parseASCII(LoaderUtils.decodeText(data))
894+
return parseASCII(decodeText(data))
894895
} else {
895896
return parseBinary(data)
896897
}

src/loaders/XLoader.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
Vector2,
2020
Vector3,
2121
} from 'three'
22+
import { decodeText } from '../_polyfill/LoaderUtils'
2223

2324
var XLoader = (function () {
2425
var classCallCheck = function (instance, Constructor) {
@@ -363,7 +364,7 @@ var XLoader = (function () {
363364
key: '_ensureString',
364365
value: function _ensureString(buf) {
365366
if (typeof buf !== 'string') {
366-
return LoaderUtils.decodeText(new Uint8Array(buf))
367+
return decodeText(new Uint8Array(buf))
367368
} else {
368369
return buf
369370
}
@@ -381,7 +382,7 @@ var XLoader = (function () {
381382
{
382383
key: '_parseBinary',
383384
value: function _parseBinary(data) {
384-
return this._parseASCII(LoaderUtils.decodeText(new Uint8Array(data)))
385+
return this._parseASCII(decodeText(new Uint8Array(data)))
385386
},
386387
},
387388
{

0 commit comments

Comments
 (0)