Skip to content

Commit

Permalink
fix(loaders): vendor decodeText for r165 deprecation (#371)
Browse files Browse the repository at this point in the history
  • Loading branch information
CodyJasonBennett authored Jun 1, 2024
1 parent 880e2eb commit eab87ab
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 18 deletions.
25 changes: 25 additions & 0 deletions src/_polyfill/LoaderUtils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
export function decodeText(array) {
if (typeof TextDecoder !== 'undefined') {
return new TextDecoder().decode(array)
}

// Avoid the String.fromCharCode.apply(null, array) shortcut, which
// throws a "maximum call stack size exceeded" error for large arrays.

let s = ''

for (let i = 0, il = array.length; i < il; i++) {
// Implicitly assumes little-endian.
s += String.fromCharCode(array[i])
}

try {
// merges multi-byte utf-8 characters.

return decodeURIComponent(escape(s))
} catch (e) {
// see https://github.com/mrdoob/three.js/issues/16358

return s
}
}
7 changes: 4 additions & 3 deletions src/loaders/3MFLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
TextureLoader,
} from 'three'
import { unzipSync } from 'fflate'
import { decodeText } from '../_polyfill/LoaderUtils'

/**
*
Expand Down Expand Up @@ -121,14 +122,14 @@ class ThreeMFLoader extends Loader {
//

const relsView = zip[relsName]
const relsFileText = LoaderUtils.decodeText(relsView)
const relsFileText = decodeText(relsView)
const rels = parseRelsXml(relsFileText)

//

if (modelRelsName) {
const relsView = zip[modelRelsName]
const relsFileText = LoaderUtils.decodeText(relsView)
const relsFileText = decodeText(relsView)
modelRels = parseRelsXml(relsFileText)
}

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

const fileText = LoaderUtils.decodeText(view)
const fileText = decodeText(view)
const xmlData = new DOMParser().parseFromString(fileText, 'application/xml')

if (xmlData.documentElement.nodeName.toLowerCase() !== 'model') {
Expand Down
3 changes: 2 additions & 1 deletion src/loaders/AMFLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
MeshPhongMaterial,
} from 'three'
import { unzipSync } from 'fflate'
import { decodeText } from '../_polyfill/LoaderUtils'

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

const fileText = LoaderUtils.decodeText(view)
const fileText = decodeText(view)
const xmlData = new DOMParser().parseFromString(fileText, 'application/xml')

if (xmlData.documentElement.nodeName.toLowerCase() !== 'amf') {
Expand Down
5 changes: 3 additions & 2 deletions src/loaders/FBXLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import {
} from 'three'
import { unzlibSync } from 'fflate'
import { NURBSCurve } from '../curves/NURBSCurve'
import { decodeText } from '../_polyfill/LoaderUtils'

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

return LoaderUtils.decodeText(new Uint8Array(a))
return decodeText(new Uint8Array(a))
}
}

Expand Down Expand Up @@ -3268,7 +3269,7 @@ function convertArrayBufferToString(buffer, from, to) {
if (from === undefined) from = 0
if (to === undefined) to = buffer.byteLength

return LoaderUtils.decodeText(new Uint8Array(buffer, from, to))
return decodeText(new Uint8Array(buffer, from, to))
}

function append(a, b) {
Expand Down
9 changes: 5 additions & 4 deletions src/loaders/GLTFLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ import {
} from 'three'
import { toTrianglesDrawMode } from '../utils/BufferGeometryUtils'
import { version } from '../_polyfill/constants'
import { decodeText } from '../_polyfill/LoaderUtils'

const SRGBColorSpace = 'srgb'
const LinearSRGBColorSpace = 'srgb-linear'
Expand Down Expand Up @@ -257,7 +258,7 @@ class GLTFLoader extends Loader {
if (typeof data === 'string') {
json = JSON.parse(data)
} else if (data instanceof ArrayBuffer) {
const magic = LoaderUtils.decodeText(new Uint8Array(data.slice(0, 4)))
const magic = decodeText(new Uint8Array(data.slice(0, 4)))

if (magic === BINARY_EXTENSION_HEADER_MAGIC) {
try {
Expand All @@ -269,7 +270,7 @@ class GLTFLoader extends Loader {

json = JSON.parse(extensions[EXTENSIONS.KHR_BINARY_GLTF].content)
} else {
json = JSON.parse(LoaderUtils.decodeText(new Uint8Array(data)))
json = JSON.parse(decodeText(new Uint8Array(data)))
}
} else {
json = data
Expand Down Expand Up @@ -1446,7 +1447,7 @@ class GLTFBinaryExtension {
const headerView = new DataView(data, 0, BINARY_EXTENSION_HEADER_LENGTH)

this.header = {
magic: LoaderUtils.decodeText(new Uint8Array(data.slice(0, 4))),
magic: decodeText(new Uint8Array(data.slice(0, 4))),
version: headerView.getUint32(4, true),
length: headerView.getUint32(8, true),
}
Expand All @@ -1470,7 +1471,7 @@ class GLTFBinaryExtension {

if (chunkType === BINARY_EXTENSION_CHUNK_TYPES.JSON) {
const contentArray = new Uint8Array(data, BINARY_EXTENSION_HEADER_LENGTH + chunkIndex, chunkLength)
this.content = LoaderUtils.decodeText(contentArray)
this.content = decodeText(contentArray)
} else if (chunkType === BINARY_EXTENSION_CHUNK_TYPES.BIN) {
const byteOffset = BINARY_EXTENSION_HEADER_LENGTH + chunkIndex
this.body = data.slice(byteOffset, byteOffset + chunkLength)
Expand Down
3 changes: 2 additions & 1 deletion src/loaders/PCDLoader.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { BufferGeometry, FileLoader, Float32BufferAttribute, Loader, LoaderUtils, Points, PointsMaterial } from 'three'
import { decodeText } from '../_polyfill/LoaderUtils'

class PCDLoader extends Loader {
constructor(manager) {
Expand Down Expand Up @@ -158,7 +159,7 @@ class PCDLoader extends Loader {
return PCDheader
}

const textData = LoaderUtils.decodeText(new Uint8Array(data))
const textData = decodeText(new Uint8Array(data))

// parse header (always ascii format)

Expand Down
3 changes: 2 additions & 1 deletion src/loaders/PLYLoader.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { BufferGeometry, FileLoader, Float32BufferAttribute, Loader, LoaderUtils } from 'three'
import { decodeText } from '../_polyfill/LoaderUtils'

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

if (data instanceof ArrayBuffer) {
const text = LoaderUtils.decodeText(new Uint8Array(data))
const text = decodeText(new Uint8Array(data))
const header = parseHeader(text)

geometry = header.format === 'ascii' ? parseASCII(text, header) : parseBinary(data, header)
Expand Down
3 changes: 2 additions & 1 deletion src/loaders/STLLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
LoaderUtils,
Vector3,
} from 'three'
import { decodeText } from '../_polyfill/LoaderUtils'

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

function ensureString(buffer) {
if (typeof buffer !== 'string') {
return LoaderUtils.decodeText(new Uint8Array(buffer))
return decodeText(new Uint8Array(buffer))
}

return buffer
Expand Down
7 changes: 4 additions & 3 deletions src/loaders/VTKLoader.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { BufferAttribute, BufferGeometry, FileLoader, Float32BufferAttribute, Loader, LoaderUtils } from 'three'
import { unzlibSync } from 'fflate'
import { decodeText } from '../_polyfill/LoaderUtils'

class VTKLoader extends Loader {
constructor(manager) {
Expand Down Expand Up @@ -885,12 +886,12 @@ class VTKLoader extends Loader {
}

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

if (meta[0].indexOf('xml') !== -1) {
return parseXML(LoaderUtils.decodeText(data))
return parseXML(decodeText(data))
} else if (meta[2].includes('ASCII')) {
return parseASCII(LoaderUtils.decodeText(data))
return parseASCII(decodeText(data))
} else {
return parseBinary(data)
}
Expand Down
5 changes: 3 additions & 2 deletions src/loaders/XLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
Vector2,
Vector3,
} from 'three'
import { decodeText } from '../_polyfill/LoaderUtils'

var XLoader = (function () {
var classCallCheck = function (instance, Constructor) {
Expand Down Expand Up @@ -363,7 +364,7 @@ var XLoader = (function () {
key: '_ensureString',
value: function _ensureString(buf) {
if (typeof buf !== 'string') {
return LoaderUtils.decodeText(new Uint8Array(buf))
return decodeText(new Uint8Array(buf))
} else {
return buf
}
Expand All @@ -381,7 +382,7 @@ var XLoader = (function () {
{
key: '_parseBinary',
value: function _parseBinary(data) {
return this._parseASCII(LoaderUtils.decodeText(new Uint8Array(data)))
return this._parseASCII(decodeText(new Uint8Array(data)))
},
},
{
Expand Down

0 comments on commit eab87ab

Please sign in to comment.