Skip to content

Commit 54b5823

Browse files
authored
Merge pull request #2118 from floryst/improve-esm-and-ci
Improve esm and ci
2 parents 83927a1 + 41bf8d6 commit 54b5823

File tree

7 files changed

+104
-80
lines changed

7 files changed

+104
-80
lines changed

Sources/Common/Core/Base64/index.d.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,19 @@
44

55
/**
66
* Convert a Base64 string to an ArrayBuffer.
7-
* @param {string} b64Str
7+
* @param {string} b64Str
88
* @return An ArrayBuffer object.
99
*/
10-
export default function toArrayBuffer(b64Str: string): ArrayBuffer;
10+
export function toArrayBuffer(b64Str: string): ArrayBuffer;
11+
12+
/**
13+
* Convert a Base64 string to an ArrayBuffer.
14+
* @param {string} b64Str
15+
* @return An ArrayBuffer object.
16+
*/
17+
export function fromArrayBuffer(ab: ArrayBuffer): string;
18+
19+
export default {
20+
toArrayBuffer,
21+
fromArrayBuffer,
22+
};

Sources/Common/Core/Base64/index.js

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ function writeChunk(b64Str, chunk, dstOffset, uint8) {
119119
return offset;
120120
}
121121

122-
function toArrayBuffer(b64Str) {
122+
export function toArrayBuffer(b64Str) {
123123
const chunks = extractChunks(b64Str);
124124
const totalEncodedLength = chunks[chunks.length - 1].end + 1;
125125
const padding = (4 - (totalEncodedLength % 4)) % 4; // -length mod 4
@@ -138,6 +138,45 @@ function toArrayBuffer(b64Str) {
138138
return arrayBuffer;
139139
}
140140

141+
function encodeTriplet(v1, v2, v3) {
142+
const triplet = (v1 << 16) + (v2 << 8) + v3;
143+
return (
144+
BASE64_CODE[triplet >> 18] +
145+
BASE64_CODE[(triplet >> 12) & 0x3f] +
146+
BASE64_CODE[(triplet >> 6) & 0x3f] +
147+
BASE64_CODE[triplet & 0x3f]
148+
);
149+
}
150+
151+
export function fromArrayBuffer(ab) {
152+
const uint8 = new Uint8Array(ab);
153+
const leftoverLength = ab.byteLength % 3;
154+
const maxTripletIndex = ab.byteLength - leftoverLength;
155+
const segments = Array(maxTripletIndex / 3);
156+
for (let i = 0; i < segments.length; i++) {
157+
const bufOffset = i * 3;
158+
segments[i] = encodeTriplet(
159+
uint8[bufOffset],
160+
uint8[bufOffset + 1],
161+
uint8[bufOffset + 2]
162+
);
163+
}
164+
if (leftoverLength > 0) {
165+
const segment = encodeTriplet(
166+
uint8[maxTripletIndex],
167+
uint8[maxTripletIndex + 1] || 0,
168+
uint8[maxTripletIndex + 2] || 0
169+
);
170+
if (leftoverLength === 1) {
171+
segments.push(`${segment.substr(0, 2)}==`);
172+
} else if (leftoverLength === 2) {
173+
segments.push(`${segment.substr(0, 3)}=`);
174+
}
175+
}
176+
return segments.join('');
177+
}
178+
141179
export default {
142180
toArrayBuffer,
181+
fromArrayBuffer,
143182
};

Sources/IO/Core/BinaryHelper/index.js

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,8 @@
55
* expect proper Unicode or any other encoding.
66
*/
77
function arrayBufferToString(arrayBuffer) {
8-
if (typeof 'TextDecoder' !== 'undefined') {
9-
const decoder = new TextDecoder('latin1');
10-
return decoder.decode(arrayBuffer);
11-
}
12-
// fallback on platforms w/o TextDecoder
13-
const byteArray = new Uint8Array(arrayBuffer);
14-
const strArr = [];
15-
for (let i = 0; i < byteArray.length; ++i) {
16-
strArr[i] = String.fromCharCode(byteArray[i]);
17-
}
18-
return strArr.join('');
8+
const decoder = new TextDecoder('latin1');
9+
return decoder.decode(arrayBuffer);
1910
}
2011

2112
/**

Sources/IO/XML/XMLWriter/index.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { create } from 'xmlbuilder2';
2-
import { fromByteArray } from 'base64-js';
32
import pako from 'pako';
43

54
import macro from 'vtk.js/Sources/macros';
5+
import { fromArrayBuffer } from 'vtk.js/Sources/Common/Core/Base64';
66
import {
77
FormatTypes,
88
TYPED_ARRAY,
@@ -75,7 +75,9 @@ function processDataArray(
7575
uint8.set(blocks[blockId], uint8Offset);
7676
uint8Offset += header[3 + blockId];
7777
}
78-
return fromByteArray(headerUint8) + fromByteArray(uint8);
78+
return (
79+
fromArrayBuffer(headerUint8.buffer) + fromArrayBuffer(uint8.buffer)
80+
);
7981
}
8082
throw new Error('Only vtkZLibDataCompressor is supported');
8183
}

package-lock.json

Lines changed: 38 additions & 30 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,11 @@
104104
"prettier": "2.4.0",
105105
"process": "0.11.10",
106106
"rollup": "2.56.3",
107+
"rollup-plugin-auto-external": "2.0.0",
107108
"rollup-plugin-copy": "3.4.0",
108109
"rollup-plugin-ignore": "1.0.10",
109110
"rollup-plugin-polyfill-node": "0.7.0",
110111
"rollup-plugin-postcss": "4.0.1",
111-
"rollup-plugin-re": "1.0.7",
112112
"rollup-plugin-string": "3.0.0",
113113
"rollup-plugin-svgo": "1.1.0",
114114
"rollup-plugin-web-worker-loader": "1.6.1",

rollup.config.js

Lines changed: 5 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@ import json from '@rollup/plugin-json';
1212
import nodePolyfills from 'rollup-plugin-polyfill-node';
1313
import { nodeResolve } from '@rollup/plugin-node-resolve';
1414
import postcss from 'rollup-plugin-postcss';
15-
import replace from 'rollup-plugin-re';
1615
import { string } from 'rollup-plugin-string';
1716
import svgo from 'rollup-plugin-svgo';
1817
import webworkerLoader from 'rollup-plugin-web-worker-loader';
1918
import copy from 'rollup-plugin-copy';
19+
import autoExternal from 'rollup-plugin-auto-external';
20+
21+
import packageJSON from './package.json';
2022

2123
import { rewriteFilenames } from './Utilities/rollup/plugin-rewrite-filenames';
2224

@@ -95,39 +97,9 @@ export default {
9597
return name.replace(/^Sources[/\\]/, '');
9698
},
9799
},
98-
external: [/@babel\/runtime/],
100+
external: Object.keys(packageJSON.dependencies).map((name) => new RegExp(`^${name}`)),
99101
plugins: [
100-
// should be before commonjs
101-
replace({
102-
patterns: [
103-
{
104-
// match against jszip/lib/load.js
105-
// Workaround until https://github.com/Stuk/jszip/pull/731 is merged
106-
include: path.resolve(
107-
__dirname,
108-
'node_modules',
109-
'jszip',
110-
'lib',
111-
'load.js'
112-
),
113-
test: /'use strict';\nvar utils = require\('.\/utils'\);/m,
114-
replace: "'use strict'",
115-
},
116-
{
117-
// match against jszip/lib/compressedObject.js
118-
// Workaround until https://github.com/Stuk/jszip/pull/731 is merged
119-
include: path.resolve(
120-
__dirname,
121-
'node_modules',
122-
'jszip',
123-
'lib',
124-
'compressedObject.js'
125-
),
126-
test: /Crc32Probe'\);\nvar DataLengthProbe = require\('.\/stream\/DataLengthProbe'\);/m,
127-
replace: "Crc32Probe');\n",
128-
},
129-
],
130-
}),
102+
autoExternal(),
131103
alias({
132104
entries: [
133105
{ find: 'vtk.js', replacement: path.resolve(__dirname) },

0 commit comments

Comments
 (0)