diff --git a/js/web/lib/onnxjs/session.ts b/js/web/lib/onnxjs/session.ts index 3a27a424e7..790be3c740 100644 --- a/js/web/lib/onnxjs/session.ts +++ b/js/web/lib/onnxjs/session.ts @@ -60,7 +60,7 @@ export class Session { this._model = new Model(); if (typeof arg === 'string') { const isOrtFormat = arg.endsWith('.ort'); - if (typeof fetch === 'undefined') { + if (typeof process !== 'undefined' && process.versions && process.versions.node) { // node const buf = await promisify(readFile)(arg); this.initialize(buf, isOrtFormat); diff --git a/js/web/lib/wasm/session-handler.ts b/js/web/lib/wasm/session-handler.ts index 308104b617..d89a226306 100644 --- a/js/web/lib/wasm/session-handler.ts +++ b/js/web/lib/wasm/session-handler.ts @@ -31,7 +31,7 @@ export class OnnxruntimeWebAssemblySessionHandler implements SessionHandler { } if (typeof pathOrBuffer === 'string') { - if (typeof fetch === 'undefined') { + if (typeof process !== 'undefined' && process.versions && process.versions.node) { // node const model = await promisify(readFile)(pathOrBuffer); [this.sessionId, this.inputNames, this.outputNames] = await createSession(model, options); diff --git a/js/web/test/test-runner.ts b/js/web/test/test-runner.ts index 6035b32a8f..e05c643e04 100644 --- a/js/web/test/test-runner.ts +++ b/js/web/test/test-runner.ts @@ -2,11 +2,10 @@ // Licensed under the MIT License. import {expect} from 'chai'; -import {readFile} from 'fs'; import {onnx} from 'onnx-proto'; import * as ort from 'onnxruntime-common'; import {extname} from 'path'; -import {inspect, promisify} from 'util'; +import {inspect} from 'util'; import {Attribute} from '../lib/onnxjs/attribute'; import {InferenceHandler, resolveBackend, SessionHandler} from '../lib/onnxjs/backend'; @@ -16,7 +15,7 @@ import {Operator} from '../lib/onnxjs/operators'; import {Tensor} from '../lib/onnxjs/tensor'; import {ProtoUtil} from '../lib/onnxjs/util'; -import {base64toBuffer, createMockGraph} from './test-shared'; +import {base64toBuffer, createMockGraph, readFile} from './test-shared'; import {Test} from './test-types'; // the threshold that used to compare 2 float numbers. See above for TensorResultValidator.floatEqual(). @@ -46,19 +45,8 @@ function fromInternalTensor(tensor: Tensor): ort.Tensor { return new ort.Tensor(tensor.type, tensor.data as ort.Tensor.DataType, tensor.dims); } -async function loadFile(uri: string): Promise { - if (typeof fetch === 'undefined') { - // node - return promisify(readFile)(uri); - } else { - // browser - const response = await fetch(uri); - return new Uint8Array(await response.arrayBuffer()); - } -} - async function loadTensorProto(uriOrData: string|Uint8Array, allowInt64 = false): Promise { - const buf = (typeof uriOrData === 'string') ? await loadFile(uriOrData) : uriOrData; + const buf = (typeof uriOrData === 'string') ? await readFile(uriOrData) : uriOrData; const tensorProto = onnx.TensorProto.decode(buf); let tensor: ort.Tensor; diff --git a/js/web/test/test-shared.ts b/js/web/test/test-shared.ts index 41a485bb3d..68d7852ce8 100644 --- a/js/web/test/test-shared.ts +++ b/js/web/test/test-shared.ts @@ -16,8 +16,8 @@ export function bufferToBase64(buffer: Uint8Array): string { return base64.fromByteArray(buffer); } -async function readFile(file: string) { - if (typeof fetch === 'undefined') { +export async function readFile(file: string) { + if (typeof process !== 'undefined' && process.versions && process.versions.node) { // node return promisify(fs.readFile)(file); } else {