Skip to content

Commit a3ebc5e

Browse files
authored
[js/web] do not use nodejs type 'Buffer' in web (#9839)
* [js/web] do not use nodejs type 'Buffer' in web * resolve comments and validate tests * remove 'Buffer' in test
1 parent 6eb0c8d commit a3ebc5e

8 files changed

+20
-17
lines changed

js/web/lib/onnxjs/attribute.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {onnxruntime} from './ort-schema/ort-generated';
77
import ortFbs = onnxruntime.experimental.fbs;
88

99
import {Tensor} from './tensor';
10-
import {LongUtil} from './util';
10+
import {decodeUtf8String, LongUtil} from './util';
1111

1212
export declare namespace Attribute {
1313
export interface DataTypeMap {
@@ -171,7 +171,7 @@ export class Attribute {
171171
// string attributes are returned as string, so no conversion is needed.
172172
if (attr instanceof onnx.AttributeProto) {
173173
const utf8String = value as Uint8Array;
174-
return Buffer.from(utf8String.buffer, utf8String.byteOffset, utf8String.byteLength).toString();
174+
return decodeUtf8String(utf8String);
175175
}
176176
}
177177

@@ -181,8 +181,7 @@ export class Attribute {
181181
// format strings attributes are returned as string[], so no conversion is needed.
182182
if (attr instanceof onnx.AttributeProto) {
183183
const utf8Strings = value as Uint8Array[];
184-
return utf8Strings.map(
185-
utf8String => Buffer.from(utf8String.buffer, utf8String.byteOffset, utf8String.byteLength).toString());
184+
return utf8Strings.map(decodeUtf8String);
186185
}
187186
}
188187

js/web/lib/onnxjs/session.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ export class Session {
6363
if (typeof fetch === 'undefined') {
6464
// node
6565
const buf = await promisify(readFile)(arg);
66-
this.initialize(Buffer.from(buf), isOrtFormat);
66+
this.initialize(buf, isOrtFormat);
6767
} else {
6868
// browser
6969
const response = await fetch(arg);

js/web/lib/onnxjs/tensor.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {onnxruntime} from './ort-schema/ort-generated';
99

1010
import ortFbs = onnxruntime.experimental.fbs;
1111

12-
import {ProtoUtil, ShapeUtil} from './util';
12+
import {decodeUtf8String, ProtoUtil, ShapeUtil} from './util';
1313

1414
export declare namespace Tensor {
1515
export interface DataTypeMap {
@@ -217,8 +217,7 @@ export class Tensor {
217217
// When it's STRING type, the value should always be stored in field
218218
// 'stringData'
219219
tensorProto.stringData!.forEach((str, i) => {
220-
const buf = Buffer.from(str.buffer, str.byteOffset, str.byteLength);
221-
value.data[i] = buf.toString();
220+
value.data[i] = decodeUtf8String(str);
222221
});
223222

224223
} else if (

js/web/lib/onnxjs/util.ts

+4
Original file line numberDiff line numberDiff line change
@@ -1249,3 +1249,7 @@ export class PoolConvUtil {
12491249

12501250
export const MIN_CLIP = -3.4028234663852886e+38;
12511251
export const MAX_CLIP = 3.4028234663852886e+38;
1252+
1253+
export function decodeUtf8String(buffer: Uint8Array): string {
1254+
return new TextDecoder().decode(buffer);
1255+
}

js/web/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
"@types/mocha": "^8.2.2",
4444
"@types/npmlog": "^4.1.2",
4545
"@types/platform": "^1.3.3",
46+
"base64-js": "^1.5.1",
4647
"chai": "^4.3.4",
4748
"dir-compare": "^3.3.0",
4849
"electron": "^12.2.3",

js/web/test/test-runner.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -43,20 +43,20 @@ function fromInternalTensor(tensor: Tensor): ort.Tensor {
4343
return new ort.Tensor(tensor.type, tensor.data as ort.Tensor.DataType, tensor.dims);
4444
}
4545

46-
async function loadFile(uri: string): Promise<Uint8Array|ArrayBuffer> {
46+
async function loadFile(uri: string): Promise<Uint8Array> {
4747
if (typeof fetch === 'undefined') {
4848
// node
4949
return promisify(readFile)(uri);
5050
} else {
5151
// browser
5252
const response = await fetch(uri);
53-
return response.arrayBuffer();
53+
return new Uint8Array(await response.arrayBuffer());
5454
}
5555
}
5656

5757
async function loadTensorProto(uriOrData: string|Uint8Array): Promise<Test.NamedTensor> {
5858
const buf = (typeof uriOrData === 'string') ? await loadFile(uriOrData) : uriOrData;
59-
const tensorProto = onnxProto.TensorProto.decode(Buffer.from(buf));
59+
const tensorProto = onnxProto.TensorProto.decode(buf);
6060
const tensor = Tensor.fromProto(tensorProto);
6161
// add property 'name' to the tensor object.
6262
const namedTensor = fromInternalTensor(tensor) as unknown as Test.NamedTensor;

js/web/test/test-shared.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License.
33

4+
import * as base64 from 'base64-js';
45
import * as fs from 'fs';
56
import {promisify} from 'util';
67

78
import {Attribute} from '../lib/onnxjs/attribute';
89
import {Graph} from '../lib/onnxjs/graph';
910

1011
export function base64toBuffer(data: string): Uint8Array {
11-
return Buffer.from(data, 'base64');
12+
return base64.toByteArray(data);
1213
}
1314

1415
export function bufferToBase64(buffer: Uint8Array): string {
15-
return Buffer.from(buffer).toString('base64');
16+
return base64.fromByteArray(buffer);
1617
}
1718

1819
async function readFile(file: string) {
@@ -22,14 +23,13 @@ async function readFile(file: string) {
2223
} else {
2324
// browser
2425
const response = await fetch(file);
25-
const buffer = await response.arrayBuffer();
26-
return Buffer.from(buffer);
26+
return new Uint8Array(await response.arrayBuffer());
2727
}
2828
}
2929

3030
export async function readJsonFile(file: string): Promise<any> {
3131
const content = await readFile(file);
32-
return JSON.parse(content.toString());
32+
return JSON.parse(new TextDecoder().decode(content));
3333
}
3434

3535
/**

js/web/webpack.config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ function buildTestRunnerConfig({
245245
new webpack.DefinePlugin({ BUILD_DEFS: DEFAULT_BUILD_DEFS }),
246246
new webpack.WatchIgnorePlugin({ paths: [/\.js$/, /\.d\.ts$/] }),
247247
new NodePolyfillPlugin({
248-
excludeAliases: ["console"]
248+
excludeAliases: ["console", "Buffer"]
249249
}),
250250
],
251251
module: {

0 commit comments

Comments
 (0)