diff --git a/package.js b/package.js index f9e33b79a80..94faa89ba29 100644 --- a/package.js +++ b/package.js @@ -1,9 +1,10 @@ -const child_process = require('child_process'); -const crypto = require('crypto'); -const fs = require('fs').promises; -const os = require('os'); -const path = require('path'); +import * as child_process from 'child_process'; +import * as crypto from 'crypto'; +import * as fs from 'fs/promises'; +import * as os from 'os'; +import * as path from 'path'; +import * as url from 'url'; const args = process.argv.slice(2); @@ -16,8 +17,13 @@ const read = (match) => { let configuration = null; +const dirname = (...args) => { + const dir = path.dirname(url.fileURLToPath(import.meta.url)); + return path.join(dir, ...args); +}; + const load = async () => { - const file = path.join(__dirname, 'package.json'); + const file = dirname('package.json'); const content = await fs.readFile(file, 'utf-8'); configuration = JSON.parse(content); }; @@ -48,7 +54,7 @@ const access = async (path) => { }; const rm = async (...args) => { - const dir = path.join(__dirname, ...args); + const dir = dirname(...args); const exists = await access(dir); if (exists) { writeLine('rm ' + path.join(...args)); @@ -58,7 +64,7 @@ const rm = async (...args) => { }; const mkdir = async (...args) => { - const dir = path.join(__dirname, ...args); + const dir = dirname(...args); const exists = await access(dir); if (!exists) { writeLine('mkdir ' + path.join(...args)); @@ -84,13 +90,13 @@ const unlink = async (dir, filter) => { const exec = async (command, encoding) => { if (encoding) { - return child_process.execSync(command, { cwd: __dirname, encoding: encoding }); + return child_process.execSync(command, { cwd: dirname(), encoding: encoding }); } - child_process.execSync(command, { cwd: __dirname, stdio: [ 0,1,2 ] }); + child_process.execSync(command, { cwd: dirname(), stdio: [ 0,1,2 ] }); return ''; /* return new Promise((resolve, reject) => { - const child = child_process.exec(command, { cwd: __dirname }, (error, stdout, stderr) => { + const child = child_process.exec(command, { cwd: dirname() }, (error, stdout, stderr) => { if (error) { stderr = '\n' + stderr ; if (error.message && error.message.endsWith(stderr)) { @@ -208,7 +214,7 @@ const clean = async () => { }; const install = async () => { - const node_modules = path.join(__dirname, 'node_modules'); + const node_modules = dirname('node_modules'); let exists = await access(node_modules); if (exists) { const dependencies = Object.assign({}, configuration.dependencies, configuration.devDependencies); @@ -252,13 +258,13 @@ const build = async (target) => { await rm('dist', 'web'); await mkdir('dist', 'web'); writeLine('cp source/dir dist/dir'); - const source_dir = path.join(__dirname, 'source'); - const dist_dir = path.join(__dirname, 'dist', 'web'); + const source_dir = dirname('source'); + const dist_dir = dirname('dist', 'web'); const extensions = new Set([ 'html', 'css', 'js', 'json', 'ico', 'png' ]); await copy(source_dir, dist_dir, (file) => extensions.has(file.split('.').pop())); await rm('dist', 'web', 'app.js'); await rm('dist', 'web', 'electron.js'); - const contentFile = path.join(__dirname, 'dist', 'web', 'index.html'); + const contentFile = dirname('dist', 'web', 'index.html'); let content = await fs.readFile(contentFile, 'utf-8'); content = content.replace(/()/m, (match, p1, p2, p3) => { return p1 + configuration.version + p3; @@ -312,8 +318,8 @@ const publish = async (target) => { const url = 'https://x-access-token:' + GITHUB_TOKEN + '@github.com/' + GITHUB_USER + '/netron.git'; await exec('git clone --depth=1 ' + url + ' --branch gh-pages ./dist/gh-pages 2>&1 > /dev/null'); writeLine('cp dist/web dist/gh-pages'); - const source_dir = path.join(__dirname, 'dist', 'web'); - const target_dir = path.join(__dirname, 'dist', 'gh-pages'); + const source_dir = dirname('dist', 'web'); + const target_dir = dirname('dist', 'gh-pages'); await unlink(target_dir, (file) => file !== '.git'); await copy(source_dir, target_dir); await exec('git -C dist/gh-pages add --all'); @@ -398,7 +404,7 @@ const publish = async (target) => { const paths = [ 'dist', 'winget-pkgs', 'manifests', publisher[0].toLowerCase(), publisher.replace(' ', ''), product, version ]; await mkdir(...paths); writeLine('update manifest'); - const manifestFile = path.join(__dirname, ...paths, identifier); + const manifestFile = dirname(...paths, identifier); await fs.writeFile(manifestFile + '.yaml', [ '# yaml-language-server: $schema=https://aka.ms/winget-manifest.version.1.2.0.schema.json', 'PackageIdentifier: ' + identifier, @@ -496,7 +502,7 @@ const publish = async (target) => { const lint = async () => { await install(); writeLine('eslint'); - await exec('npx eslint *.js source/*.js test/*.js publish/*.js tools/*.js'); + await exec('npx eslint *.*js source/*.*js test/*.*js publish/*.*js tools/*.js'); writeLine('pylint'); await exec('python -m pip install --upgrade --quiet pylint'); await exec('python -m pylint -sn --recursive=y source test publish tools *.py'); @@ -580,7 +586,7 @@ const analyze = async () => { const version = async () => { await pull(); - const file = path.join(__dirname, 'package.json'); + const file = dirname('package.json'); let content = await fs.readFile(file, 'utf-8'); content = content.replace(/(\s*"version":\s")(\d\.\d\.\d)(",)/m, (match, p1, p2, p3) => { const version = Array.from((parseInt(p2.split('.').join(''), 10) + 1).toString()).join('.'); diff --git a/package.json b/package.json index d2020380f84..07e6adc7470 100755 --- a/package.json +++ b/package.json @@ -11,6 +11,7 @@ "description": "Visualizer for neural network, deep learning, and machine learning models", "license": "MIT", "repository": "lutzroeder/netron", + "type": "module", "main": "source/app.js", "scripts": { "start": "node package.js start", @@ -39,7 +40,7 @@ }, "eslintConfig": { "env": { - "es2017": true, + "es2020": true, "browser": true, "node": true }, @@ -80,6 +81,7 @@ "no-unreachable-loop": "error", "no-unused-private-class-members": "error", "no-use-before-define": "error", + "no-var": "error", "object-curly-spacing": [ "error", "always" ], "prefer-const": [ "error" ], "prefer-destructuring": [ "error", { "array": false } ], @@ -157,7 +159,7 @@ { "ext": "uff", "name": "UFF Model" }, { "ext": "xmodel", "name": "Vitis AI Model" } ], - "afterSign": "./publish/notarize.js", + "afterSign": "./publish/notarize.cjs", "publish": [ { "provider": "github", "releaseType": "release" } ], diff --git a/package.py b/package.py index bb2d9945570..c7abe56c54b 100644 --- a/package.py +++ b/package.py @@ -34,7 +34,7 @@ def _build(): shutil.rmtree(dist_pypi_dir, ignore_errors=True) shutil.copytree(source_dir, os.path.join(dist_pypi_dir, 'netron')) shutil.copyfile(os.path.join(publish_dir, 'setup.py'), os.path.join(dist_pypi_dir, 'setup.py')) - os.remove(os.path.join(dist_pypi_dir, 'netron', 'electron.js')) + os.remove(os.path.join(dist_pypi_dir, 'netron', 'electron.mjs')) os.remove(os.path.join(dist_pypi_dir, 'netron', 'app.js')) def _install(): diff --git a/publish/notarize.js b/publish/notarize.cjs similarity index 99% rename from publish/notarize.js rename to publish/notarize.cjs index e879d47b33a..e725a4f9ff7 100644 --- a/publish/notarize.js +++ b/publish/notarize.cjs @@ -16,4 +16,4 @@ exports.default = function (context) { } } return null; -}; \ No newline at end of file +}; diff --git a/source/acuity.js b/source/acuity.js index c0119a35c92..a8f94d57bd2 100644 --- a/source/acuity.js +++ b/source/acuity.js @@ -1,5 +1,5 @@ -var acuity = {}; +const acuity = {}; acuity.ModelFactory = class { @@ -554,6 +554,4 @@ acuity.Error = class extends Error { } }; -if (typeof module !== 'undefined' && typeof module.exports === 'object') { - module.exports.ModelFactory = acuity.ModelFactory; -} \ No newline at end of file +export const ModelFactory = acuity.ModelFactory; diff --git a/source/app.js b/source/app.js index ad0096a8eb4..f2c343b3635 100644 --- a/source/app.js +++ b/source/app.js @@ -1,14 +1,14 @@ -const electron = require('electron'); -const updater = require('electron-updater'); -const fs = require('fs'); -const os = require('os'); -const path = require('path'); -const process = require('process'); -const url = require('url'); -const base = require('./base'); +import * as electron from 'electron'; +import * as updater from 'electron-updater'; +import * as fs from 'fs'; +import * as os from 'os'; +import * as path from 'path'; +import * as process from 'process'; +import * as url from 'url'; +import * as base from './base.js'; -var app = {}; +const app = {}; app.Application = class { @@ -21,8 +21,9 @@ app.Application = class { } start() { - const packageFile = path.join(path.dirname(__dirname), 'package.json'); - const packageContent = fs.readFileSync(packageFile, 'utf-8'); + const dirname = path.dirname(url.fileURLToPath(import.meta.url)); + const packageFile = path.join(path.dirname(dirname), 'package.json'); + const packageContent = fs.readFileSync(packageFile, 'utf-8'); this._package = JSON.parse(packageContent); electron.app.setAppUserModelId('com.lutzroeder.netron'); @@ -128,7 +129,8 @@ app.Application = class { let open = false; if (argv.length > 1) { for (const arg of argv.slice(1)) { - if (!arg.startsWith('-') && arg !== path.dirname(__dirname)) { + const dirname = path.dirname(url.fileURLToPath(import.meta.url)); + if (!arg.startsWith('-') && arg !== path.dirname(dirname)) { const extension = path.extname(arg).toLowerCase(); if (extension !== '' && extension !== '.js' && fs.existsSync(arg)) { const stat = fs.statSync(arg); @@ -634,18 +636,19 @@ app.View = class { this._path = null; this._properties = new Map(); this._dispatch = []; + const dirname = path.dirname(url.fileURLToPath(import.meta.url)); const size = electron.screen.getPrimaryDisplay().workAreaSize; const options = { show: false, title: electron.app.name, backgroundColor: electron.nativeTheme.shouldUseDarkColors ? '#1d1d1d' : '#e6e6e6', - icon: electron.nativeImage.createFromPath(path.join(__dirname, 'icon.png')), + icon: electron.nativeImage.createFromPath(path.join(dirname, 'icon.png')), minWidth: 600, minHeight: 600, width: size.width > 1024 ? 1024 : size.width, height: size.height > 768 ? 768 : size.height, webPreferences: { - preload: path.join(__dirname, 'electron.js'), + preload: path.join(dirname, 'electron.mjs'), nodeIntegration: true } }; @@ -716,7 +719,8 @@ app.View = class { } _loadURL() { - const pathname = path.join(__dirname, 'index.html'); + const dirname = path.dirname(url.fileURLToPath(import.meta.url)); + const pathname = path.join(dirname, 'index.html'); let content = fs.readFileSync(pathname, 'utf-8'); content = content.replace(/<\s*script[^>]*>[\s\S]*?(<\s*\/script[^>]*>|$)/ig, ''); const data = 'data:text/html;charset=utf-8,' + encodeURIComponent(content); diff --git a/source/armnn-schema.js b/source/armnn-schema.js index de3df1b014e..fe7da30d97a 100644 --- a/source/armnn-schema.js +++ b/source/armnn-schema.js @@ -1,4 +1,7 @@ -var $root = flatbuffers.get('armnn'); + +import * as flatbuffers from './flatbuffers.js'; + +const $root = flatbuffers.get('armnn'); $root.armnnSerializer = $root.armnnSerializer || {}; diff --git a/source/armnn.js b/source/armnn.js index 880b641030c..a99d9432b20 100644 --- a/source/armnn.js +++ b/source/armnn.js @@ -1,6 +1,7 @@ -var armnn = {}; -var flatbuffers = require('./flatbuffers'); +import * as flatbuffers from './flatbuffers.js'; + +const armnn = {}; armnn.ModelFactory = class { @@ -315,6 +316,4 @@ armnn.Error = class extends Error { } }; -if (typeof module !== 'undefined' && typeof module.exports === 'object') { - module.exports.ModelFactory = armnn.ModelFactory; -} +export const ModelFactory = armnn.ModelFactory; diff --git a/source/barracuda.js b/source/barracuda.js index 9efb4627bce..d19f390d660 100755 --- a/source/barracuda.js +++ b/source/barracuda.js @@ -1,8 +1,9 @@ // Experimental -var barracuda = {}; -var base = require('./base'); +import * as base from './base.js'; + +const barracuda = {}; barracuda.ModelFactory = class { @@ -420,6 +421,5 @@ barracuda.Error = class extends Error { } }; -if (typeof module !== 'undefined' && typeof module.exports === 'object') { - module.exports.ModelFactory = barracuda.ModelFactory; -} +export const ModelFactory = barracuda.ModelFactory; + diff --git a/source/base.js b/source/base.js index 16e7ec2353e..46e8f55ba7a 100644 --- a/source/base.js +++ b/source/base.js @@ -1,5 +1,5 @@ -var base = {}; +const base = {}; base.Int64 = class Int64 { @@ -1108,13 +1108,11 @@ if (typeof window !== 'undefined' && typeof window.Long != 'undefined') { window.Uint64 = base.Uint64; } -if (typeof module !== 'undefined' && typeof module.exports === 'object') { - module.exports.Int64 = base.Int64; - module.exports.Uint64 = base.Uint64; - module.exports.Complex64 = base.Complex64; - module.exports.Complex128 = base.Complex128; - module.exports.BinaryStream = base.BinaryStream; - module.exports.BinaryReader = base.BinaryReader; - module.exports.Telemetry = base.Telemetry; - module.exports.Metadata = base.Metadata; -} +export const Int64 = base.Int64; +export const Uint64 = base.Uint64; +export const Complex64 = base.Complex64; +export const Complex128 = base.Complex128; +export const BinaryStream = base.BinaryStream; +export const BinaryReader = base.BinaryReader; +export const Telemetry = base.Telemetry; +export const Metadata = base.Metadata; diff --git a/source/bigdl-proto.js b/source/bigdl-proto.js index 147129ef4ef..c8410c7af60 100644 --- a/source/bigdl-proto.js +++ b/source/bigdl-proto.js @@ -1,4 +1,7 @@ -var $root = protobuf.get('bigdl'); + +import * as protobuf from './protobuf.js'; + +const $root = protobuf.get('bigdl'); $root.com = {}; diff --git a/source/bigdl.js b/source/bigdl.js index cae24e4f4d5..496c519286a 100644 --- a/source/bigdl.js +++ b/source/bigdl.js @@ -1,8 +1,9 @@ // Experimental -var bigdl = {}; -var protobuf = require('./protobuf'); +import * as protobuf from './protobuf.js'; + +const bigdl = {}; bigdl.ModelFactory = class { @@ -303,6 +304,5 @@ bigdl.Error = class extends Error { } }; -if (typeof module !== 'undefined' && typeof module.exports === 'object') { - module.exports.ModelFactory = bigdl.ModelFactory; -} +export const ModelFactory = bigdl.ModelFactory; + diff --git a/source/browser.js b/source/browser.js index 75913fbedfa..31298adcfdc 100644 --- a/source/browser.js +++ b/source/browser.js @@ -1,5 +1,7 @@  -var host = {}; +import * as base from './base.js'; + +const host = {}; host.BrowserHost = class { @@ -7,7 +9,6 @@ host.BrowserHost = class { this._window = window; this._navigator = window.navigator; this._document = window.document; - const base = require('./base'); this._telemetry = new base.Telemetry(this._window); this._window.eval = () => { throw new Error('window.eval() not supported.'); @@ -57,7 +58,7 @@ host.BrowserHost = class { const days = (new Date() - new Date(this._environment.date)) / (24 * 60 * 60 * 1000); if (days > 180) { this.document.body.classList.remove('spinner'); - this.window.terminate('Please update to the newest version.', 'Download', () => { + this.window.exports.terminate('Please update to the newest version.', 'Download', () => { const link = this._element('logo-github').href; this.openURL(link); }); @@ -140,7 +141,7 @@ host.BrowserHost = class { browser_capabilities: capabilities.map((capability) => capability.split('.').pop()).join(',') }); if (required.length > available.length) { - this.window.terminate('Your browser is not supported.'); + this.window.exports.terminate('Your browser is not supported.'); return new Promise(() => {}); } return Promise.resolve(); @@ -191,7 +192,6 @@ host.BrowserHost = class { }); const mobileSafari = this.environment('platform') === 'darwin' && navigator.maxTouchPoints && navigator.maxTouchPoints > 1; if (!mobileSafari) { - const base = require('./base'); const extensions = new base.Metadata().extensions.map((extension) => '.' + extension); openFileDialog.setAttribute('accept', extensions.join(', ')); } @@ -237,10 +237,8 @@ host.BrowserHost = class { return confirm(message + ' ' + detail); } - require(id) { - return new Promise((resolve, reject) => { - this.window.require(id, (module) => resolve(module), (error) => reject(error)); - }); + async require(id) { + return import(id + '.js'); } save(name, extension, defaultPath, callback) { @@ -371,7 +369,6 @@ host.BrowserHost = class { progress(0); if (request.status == 200) { if (request.responseType == 'arraybuffer') { - const base = require('./base'); const buffer = new Uint8Array(request.response); const stream = new base.BinaryStream(buffer); resolve(stream); @@ -484,7 +481,6 @@ host.BrowserHost = class { this.error('Error while loading Gist.', 'Gist does not contain a model file.'); return; } - const base = require('./base'); const file = json.files[key]; const identifier = file.filename; const encoder = new TextEncoder(); @@ -620,7 +616,6 @@ host.BrowserHost.BrowserFileContext = class { } else { const buffer = new Uint8Array(e.target.result); if (position === 0 && buffer.length === blob.size) { - const base = require('./base'); const stream = new base.BinaryStream(buffer); resolve(stream); } else { @@ -848,3 +843,9 @@ if (!('scrollBehavior' in window.document.documentElement.style)) { } }; } + +if (typeof window !== 'undefined' && window.exports) { + window.exports.browser = host; +} + +export const BrowserHost = host.BrowserHost; diff --git a/source/caffe-proto.js b/source/caffe-proto.js index ac18478068c..095f504f714 100644 --- a/source/caffe-proto.js +++ b/source/caffe-proto.js @@ -1,4 +1,7 @@ -var $root = protobuf.get('caffe'); + +import * as protobuf from './protobuf.js'; + +const $root = protobuf.get('caffe'); $root.caffe = {}; diff --git a/source/caffe.js b/source/caffe.js index 3052e149679..6aebf94350d 100644 --- a/source/caffe.js +++ b/source/caffe.js @@ -1,6 +1,7 @@ -var caffe = {}; -var protobuf = require('./protobuf'); +import * as protobuf from './protobuf.js'; + +const caffe = {}; caffe.ModelFactory = class { @@ -715,6 +716,4 @@ caffe.Error = class extends Error { } }; -if (typeof module !== 'undefined' && typeof module.exports === 'object') { - module.exports.ModelFactory = caffe.ModelFactory; -} \ No newline at end of file +export const ModelFactory = caffe.ModelFactory; diff --git a/source/caffe2-proto.js b/source/caffe2-proto.js index ce95e40df62..27df8991cb6 100644 --- a/source/caffe2-proto.js +++ b/source/caffe2-proto.js @@ -1,4 +1,7 @@ -var $root = protobuf.get('caffe2'); + +import * as protobuf from './protobuf.js'; + +const $root = protobuf.get('caffe2'); $root.caffe2 = {}; @@ -174,10 +177,10 @@ $root.caffe2.TensorProto.Segment = class Segment { } } if (!Object.prototype.hasOwnProperty.call(message, 'begin')) { - throw new protobuf.Error("Excepted 'begin'."); + throw new Error("Excepted 'begin'."); } if (!Object.prototype.hasOwnProperty.call(message, 'end')) { - throw new protobuf.Error("Excepted 'end'."); + throw new Error("Excepted 'end'."); } return message; } @@ -200,10 +203,10 @@ $root.caffe2.TensorProto.Segment = class Segment { } } if (!Object.prototype.hasOwnProperty.call(message, "begin")) { - throw new protobuf.Error("Excepted 'begin'."); + throw new Error("Excepted 'begin'."); } if (!Object.prototype.hasOwnProperty.call(message, "end")) { - throw new protobuf.Error("Excepted 'end'."); + throw new Error("Excepted 'end'."); } return message; } @@ -269,16 +272,16 @@ $root.caffe2.QTensorProto = class QTensorProto { } } if (!Object.prototype.hasOwnProperty.call(message, 'precision')) { - throw new protobuf.Error("Excepted 'precision'."); + throw new Error("Excepted 'precision'."); } if (!Object.prototype.hasOwnProperty.call(message, 'scale')) { - throw new protobuf.Error("Excepted 'scale'."); + throw new Error("Excepted 'scale'."); } if (!Object.prototype.hasOwnProperty.call(message, 'bias')) { - throw new protobuf.Error("Excepted 'bias'."); + throw new Error("Excepted 'bias'."); } if (!Object.prototype.hasOwnProperty.call(message, 'is_signed')) { - throw new protobuf.Error("Excepted 'is_signed'."); + throw new Error("Excepted 'is_signed'."); } return message; } @@ -331,16 +334,16 @@ $root.caffe2.QTensorProto = class QTensorProto { } } if (!Object.prototype.hasOwnProperty.call(message, "precision")) { - throw new protobuf.Error("Excepted 'precision'."); + throw new Error("Excepted 'precision'."); } if (!Object.prototype.hasOwnProperty.call(message, "scale")) { - throw new protobuf.Error("Excepted 'scale'."); + throw new Error("Excepted 'scale'."); } if (!Object.prototype.hasOwnProperty.call(message, "bias")) { - throw new protobuf.Error("Excepted 'bias'."); + throw new Error("Excepted 'bias'."); } if (!Object.prototype.hasOwnProperty.call(message, "is_signed")) { - throw new protobuf.Error("Excepted 'is_signed'."); + throw new Error("Excepted 'is_signed'."); } return message; } @@ -668,13 +671,13 @@ $root.caffe2.AOTConfig = class AOTConfig { } } if (!Object.prototype.hasOwnProperty.call(message, 'max_batch_size')) { - throw new protobuf.Error("Excepted 'max_batch_size'."); + throw new Error("Excepted 'max_batch_size'."); } if (!Object.prototype.hasOwnProperty.call(message, 'max_seq_size')) { - throw new protobuf.Error("Excepted 'max_seq_size'."); + throw new Error("Excepted 'max_seq_size'."); } if (!Object.prototype.hasOwnProperty.call(message, 'in_batch_broadcast')) { - throw new protobuf.Error("Excepted 'in_batch_broadcast'."); + throw new Error("Excepted 'in_batch_broadcast'."); } return message; } @@ -706,13 +709,13 @@ $root.caffe2.AOTConfig = class AOTConfig { } } if (!Object.prototype.hasOwnProperty.call(message, "max_batch_size")) { - throw new protobuf.Error("Excepted 'max_batch_size'."); + throw new Error("Excepted 'max_batch_size'."); } if (!Object.prototype.hasOwnProperty.call(message, "max_seq_size")) { - throw new protobuf.Error("Excepted 'max_seq_size'."); + throw new Error("Excepted 'max_seq_size'."); } if (!Object.prototype.hasOwnProperty.call(message, "in_batch_broadcast")) { - throw new protobuf.Error("Excepted 'in_batch_broadcast'."); + throw new Error("Excepted 'in_batch_broadcast'."); } return message; } @@ -1077,10 +1080,10 @@ $root.caffe2.MapFieldEntry = class MapFieldEntry { } } if (!Object.prototype.hasOwnProperty.call(message, 'key')) { - throw new protobuf.Error("Excepted 'key'."); + throw new Error("Excepted 'key'."); } if (!Object.prototype.hasOwnProperty.call(message, 'val')) { - throw new protobuf.Error("Excepted 'val'."); + throw new Error("Excepted 'val'."); } return message; } @@ -1103,10 +1106,10 @@ $root.caffe2.MapFieldEntry = class MapFieldEntry { } } if (!Object.prototype.hasOwnProperty.call(message, "key")) { - throw new protobuf.Error("Excepted 'key'."); + throw new Error("Excepted 'key'."); } if (!Object.prototype.hasOwnProperty.call(message, "val")) { - throw new protobuf.Error("Excepted 'val'."); + throw new Error("Excepted 'val'."); } return message; } @@ -1139,7 +1142,7 @@ $root.caffe2.BackendOptions = class BackendOptions { } } if (!Object.prototype.hasOwnProperty.call(message, 'backend_name')) { - throw new protobuf.Error("Excepted 'backend_name'."); + throw new Error("Excepted 'backend_name'."); } return message; } @@ -1162,7 +1165,7 @@ $root.caffe2.BackendOptions = class BackendOptions { } } if (!Object.prototype.hasOwnProperty.call(message, "backend_name")) { - throw new protobuf.Error("Excepted 'backend_name'."); + throw new Error("Excepted 'backend_name'."); } return message; } @@ -1201,7 +1204,7 @@ $root.caffe2.PartitionInfo = class PartitionInfo { } } if (!Object.prototype.hasOwnProperty.call(message, 'name')) { - throw new protobuf.Error("Excepted 'name'."); + throw new Error("Excepted 'name'."); } return message; } @@ -1230,7 +1233,7 @@ $root.caffe2.PartitionInfo = class PartitionInfo { } } if (!Object.prototype.hasOwnProperty.call(message, "name")) { - throw new protobuf.Error("Excepted 'name'."); + throw new Error("Excepted 'name'."); } return message; } diff --git a/source/caffe2.js b/source/caffe2.js index af8ce67131e..94ed890233f 100644 --- a/source/caffe2.js +++ b/source/caffe2.js @@ -1,6 +1,7 @@ -var caffe2 = {}; -var protobuf = require('./protobuf'); +import * as protobuf from './protobuf.js'; + +const caffe2 = {}; caffe2.ModelFactory = class { @@ -632,6 +633,4 @@ caffe2.Error = class extends Error { } }; -if (typeof module !== 'undefined' && typeof module.exports === 'object') { - module.exports.ModelFactory = caffe2.ModelFactory; -} \ No newline at end of file +export const ModelFactory = caffe2.ModelFactory; diff --git a/source/cambricon.js b/source/cambricon.js index 6a04aeab90a..f7dbc78c7af 100644 --- a/source/cambricon.js +++ b/source/cambricon.js @@ -1,5 +1,5 @@ -var cambricon = {}; +const cambricon = {}; cambricon.ModelFactory = class { @@ -28,6 +28,5 @@ cambricon.Error = class extends Error { } }; -if (typeof module !== 'undefined' && typeof module.exports === 'object') { - module.exports.ModelFactory = cambricon.ModelFactory; -} +export const ModelFactory = cambricon.ModelFactory; + diff --git a/source/circle-schema.js b/source/circle-schema.js index 33241697c93..59c3476697b 100644 --- a/source/circle-schema.js +++ b/source/circle-schema.js @@ -1,4 +1,7 @@ -var $root = flatbuffers.get('circle'); + +import * as flatbuffers from './flatbuffers.js'; + +const $root = flatbuffers.get('circle'); $root.circle = $root.circle || {}; diff --git a/source/circle.js b/source/circle.js index 89892be02fc..de8be65799f 100644 --- a/source/circle.js +++ b/source/circle.js @@ -1,8 +1,9 @@ -var circle = {}; -var flatbuffers = require('./flatbuffers'); -var flexbuffers = require('./flexbuffers'); -var zip = require('./zip'); +import * as flatbuffers from '../source/flatbuffers.js'; +import * as flexbuffers from '../source/flexbuffers.js'; +import * as zip from '../source/zip.js'; + +const circle = {}; circle.ModelFactory = class { @@ -665,6 +666,4 @@ circle.Error = class extends Error { } }; -if (typeof module !== 'undefined' && typeof module.exports === 'object') { - module.exports.ModelFactory = circle.ModelFactory; -} \ No newline at end of file +export const ModelFactory = circle.ModelFactory; diff --git a/source/cntk-proto.js b/source/cntk-proto.js index 0e00424e0f5..88f404bb4df 100644 --- a/source/cntk-proto.js +++ b/source/cntk-proto.js @@ -1,4 +1,7 @@ -var $root = protobuf.get('cntk'); + +import * as protobuf from './protobuf.js'; + +const $root = protobuf.get('cntk'); $root.CNTK = {}; diff --git a/source/cntk.js b/source/cntk.js index bb2b4766a70..a060da4d736 100644 --- a/source/cntk.js +++ b/source/cntk.js @@ -1,10 +1,10 @@ -var cntk = {}; -var base = require('./base'); -var protobuf = require('./protobuf'); +import * as base from './base.js'; +import * as protobuf from './protobuf.js'; -var cntk_v1 = {}; -var cntk_v2 = null; +const cntk = {}; +const cntk_v1 = {}; +let cntk_v2 = null; cntk.ModelFactory = class { @@ -1229,6 +1229,4 @@ cntk.Error = class extends Error { } }; -if (typeof module !== 'undefined' && typeof module.exports === 'object') { - module.exports.ModelFactory = cntk.ModelFactory; -} \ No newline at end of file +export const ModelFactory = cntk.ModelFactory; diff --git a/source/coreml-proto.js b/source/coreml-proto.js index 04e4d0f0aac..cde147b9b53 100644 --- a/source/coreml-proto.js +++ b/source/coreml-proto.js @@ -1,4 +1,7 @@ -var $root = protobuf.get('coreml'); + +import * as protobuf from './protobuf.js'; + +const $root = protobuf.get('coreml'); $root.CoreML = {}; diff --git a/source/coreml.js b/source/coreml.js index 50d8926decf..3e176bb222f 100644 --- a/source/coreml.js +++ b/source/coreml.js @@ -1,7 +1,8 @@ -var coreml = {}; -var base = require('./base'); -var protobuf = require('./protobuf'); +import * as base from './base.js'; +import * as protobuf from './protobuf.js'; + +const coreml = {}; coreml.ModelFactory = class { @@ -1431,6 +1432,4 @@ coreml.Error = class extends Error { } }; -if (typeof module !== 'undefined' && typeof module.exports === 'object') { - module.exports.ModelFactory = coreml.ModelFactory; -} \ No newline at end of file +export const ModelFactory = coreml.ModelFactory; diff --git a/source/dagre.js b/source/dagre.js index 9792bd8e5e0..2f2f8b89265 100644 --- a/source/dagre.js +++ b/source/dagre.js @@ -1,5 +1,5 @@ -var dagre = {}; +const dagre = {}; // Dagre graph layout // https://github.com/dagrejs/dagre @@ -2218,6 +2218,4 @@ dagre.Graph = class { } }; -if (typeof module !== 'undefined' && typeof module.exports === 'object') { - module.exports = dagre; -} +export const { layout, Graph } = dagre; diff --git a/source/darknet.js b/source/darknet.js index c7d39f99f84..1dabbd11fd4 100644 --- a/source/darknet.js +++ b/source/darknet.js @@ -1,6 +1,7 @@ -var darknet = {}; -var text = require('./text'); +import * as text from './text.js'; + +const darknet = {}; darknet.ModelFactory = class { @@ -1089,6 +1090,5 @@ darknet.Error = class extends Error { } }; -if (typeof module !== 'undefined' && typeof module.exports === 'object') { - module.exports.ModelFactory = darknet.ModelFactory; -} +export const ModelFactory = darknet.ModelFactory; + diff --git a/source/dl4j.js b/source/dl4j.js index e27f511519d..72e9757f6ac 100644 --- a/source/dl4j.js +++ b/source/dl4j.js @@ -1,7 +1,7 @@ // Experimental -var dl4j = {}; +const dl4j = {}; dl4j.ModelFactory = class { @@ -532,6 +532,4 @@ dl4j.Error = class extends Error { } }; -if (typeof module !== 'undefined' && typeof module.exports === 'object') { - module.exports.ModelFactory = dl4j.ModelFactory; -} \ No newline at end of file +export const ModelFactory = dl4j.ModelFactory; diff --git a/source/dlc-schema.js b/source/dlc-schema.js index 1b6fb78d7d6..b1f62704202 100644 --- a/source/dlc-schema.js +++ b/source/dlc-schema.js @@ -1,4 +1,7 @@ -var $root = flatbuffers.get('dlc'); + +import * as flatbuffers from './flatbuffers.js'; + +const $root = flatbuffers.get('dlc'); $root.dlc = $root.dlc || {}; diff --git a/source/dlc.js b/source/dlc.js index db96bf55bf8..3d185514921 100644 --- a/source/dlc.js +++ b/source/dlc.js @@ -1,7 +1,8 @@ -var dlc = {}; -var text = require('./text'); -var flatbuffers = require('./flatbuffers'); +import * as text from './text.js'; +import * as flatbuffers from './flatbuffers.js'; + +const dlc = {}; dlc.ModelFactory = class { @@ -665,6 +666,5 @@ dlc.Error = class extends Error { } }; -if (typeof module !== 'undefined' && typeof module.exports === 'object') { - module.exports.ModelFactory = dlc.ModelFactory; -} +export const ModelFactory = dlc.ModelFactory; + diff --git a/source/dnn-proto.js b/source/dnn-proto.js index 2a1805af2b7..d8a61626ecb 100644 --- a/source/dnn-proto.js +++ b/source/dnn-proto.js @@ -1,4 +1,7 @@ -var $root = protobuf.get('dnn'); + +import * as protobuf from './protobuf.js'; + +const $root = protobuf.get('dnn'); $root.dnn = {}; diff --git a/source/dnn.js b/source/dnn.js index 77e19b7f44c..00a89a357ca 100644 --- a/source/dnn.js +++ b/source/dnn.js @@ -1,8 +1,9 @@ // Experimental -var dnn = {}; -var protobuf = require('./protobuf'); +import * as protobuf from './protobuf.js'; + +const dnn = {}; dnn.ModelFactory = class { @@ -257,6 +258,5 @@ dnn.Error = class extends Error { } }; -if (typeof module !== 'undefined' && typeof module.exports === 'object') { - module.exports.ModelFactory = dnn.ModelFactory; -} +export const ModelFactory = dnn.ModelFactory; + diff --git a/source/electron.js b/source/electron.mjs similarity index 98% rename from source/electron.js rename to source/electron.mjs index 8a4cae6fa32..04c3ebb0a64 100644 --- a/source/electron.js +++ b/source/electron.mjs @@ -1,13 +1,14 @@ -var host = {}; +import * as electron from 'electron'; +import * as fs from 'fs'; +import * as http from 'http'; +import * as https from 'https'; +import * as path from 'path'; +import * as url from 'url'; +import * as base from './base.js'; +import * as view from './view.js'; -const electron = require('electron'); -const fs = require('fs'); -const http = require('http'); -const https = require('https'); -const process = require('process'); -const path = require('path'); -const base = require('./base'); +const host = {}; host.ElectronHost = class { @@ -253,7 +254,7 @@ host.ElectronHost = class { } async require(id) { - return require(id); + return import(id + '.js'); } save(name, extension, defaultPath, callback) { @@ -301,7 +302,8 @@ host.ElectronHost = class { async request(file, encoding, basename) { return new Promise((resolve, reject) => { - const pathname = path.join(basename || __dirname, file); + const dirname = path.dirname(url.fileURLToPath(import.meta.url)); + const pathname = path.join(basename || dirname, file); fs.stat(pathname, (err, stat) => { if (err && err.code === 'ENOENT') { reject(new Error("The file '" + file + "' does not exist.")); @@ -719,10 +721,7 @@ host.ElectronHost.Context = class { }; window.addEventListener('load', () => { - global.protobuf = require('./protobuf'); - global.flatbuffers = require('./flatbuffers'); const value = new host.ElectronHost(); - const view = require('./view'); window.__view__ = new view.View(value); window.__view__.start(); }); diff --git a/source/flatbuffers.js b/source/flatbuffers.js index f07b50b9a91..ea0316f0cf8 100644 --- a/source/flatbuffers.js +++ b/source/flatbuffers.js @@ -1,5 +1,5 @@ -var flatbuffers = {}; +const flatbuffers = {}; flatbuffers.get = (name) => { flatbuffers._roots = flatbuffers._roots || new Map(); @@ -139,14 +139,14 @@ flatbuffers.BinaryReader = class { string(offset, encoding) { offset += this.int32(offset); const length = this.int32(offset); - var result = ''; - var i = 0; + let result = ''; + let i = 0; offset += 4; if (encoding === 1) { return this._buffer.subarray(offset, offset + length); } while (i < length) { - var codePoint; + let codePoint; // Decode UTF-8 const a = this.uint8(offset + i++); if (a < 0xC0) { @@ -285,7 +285,7 @@ flatbuffers.BinaryReader = class { } _offset(bb_pos, vtableOffset) { - var vtable = bb_pos - this.int32(bb_pos); + const vtable = bb_pos - this.int32(bb_pos); return vtableOffset < this.int16(vtable) ? this.int16(vtable + vtableOffset) : 0; } @@ -380,8 +380,6 @@ flatbuffers.Error = class extends Error { } }; -if (typeof module !== "undefined" && typeof module.exports === "object") { - module.exports.BinaryReader = flatbuffers.BinaryReader; - module.exports.TextReader = flatbuffers.TextReader; - module.exports.get = flatbuffers.get; -} +export const BinaryReader = flatbuffers.BinaryReader; +export const TextReader = flatbuffers.TextReader; +export const get = flatbuffers.get; diff --git a/source/flax.js b/source/flax.js index 2c80dc7973e..94ac968e5fa 100644 --- a/source/flax.js +++ b/source/flax.js @@ -1,8 +1,9 @@ // Experimental -var flax = {}; -var python = require('./python'); +import * as python from './python.js'; + +const flax = {}; flax.ModelFactory = class { @@ -304,8 +305,7 @@ flax.Error = class extends Error { } }; -if (typeof module !== 'undefined' && typeof module.exports === 'object') { - module.exports.ModelFactory = flax.ModelFactory; -} +export const ModelFactory = flax.ModelFactory; + diff --git a/source/flexbuffers.js b/source/flexbuffers.js index 1285114f2c7..0984bb1a3cd 100644 --- a/source/flexbuffers.js +++ b/source/flexbuffers.js @@ -1,5 +1,5 @@ -var flexbuffers = {}; +const flexbuffers = {}; flexbuffers.BinaryReader = class { @@ -193,6 +193,4 @@ flexbuffers.Error = class extends Error { } }; -if (typeof module !== 'undefined' && typeof module.exports === 'object') { - module.exports.BinaryReader = flexbuffers.BinaryReader; -} \ No newline at end of file +export const BinaryReader = flexbuffers.BinaryReader; diff --git a/source/flux.js b/source/flux.js index 571a784e4ac..c1474f9dfca 100644 --- a/source/flux.js +++ b/source/flux.js @@ -1,8 +1,9 @@ // Experimental -var flux = {}; -var json = require('./json'); +import * as json from './json.js'; + +const flux = {}; flux.ModelFactory = class { @@ -80,6 +81,4 @@ flux.Error = class extends Error { } }; -if (typeof module !== 'undefined' && typeof module.exports === 'object') { - module.exports.ModelFactory = flux.ModelFactory; -} \ No newline at end of file +export const ModelFactory = flux.ModelFactory; diff --git a/source/grapher.js b/source/grapher.js index a9accb68918..728403951a0 100644 --- a/source/grapher.js +++ b/source/grapher.js @@ -1,6 +1,7 @@ -var grapher = {}; -var dagre = require('./dagre'); +import * as dagre from './dagre.js'; + +const grapher = {}; grapher.Graph = class { @@ -880,8 +881,4 @@ grapher.Edge.Path = class { } }; -if (typeof module !== 'undefined' && typeof module.exports === 'object') { - module.exports.Graph = grapher.Graph; - module.exports.Node = grapher.Node; - module.exports.Edge = grapher.Edge; -} \ No newline at end of file +export const { Graph, Node, Edge } = grapher; diff --git a/source/hailo.js b/source/hailo.js index 87ff6f32f1a..f4c90e32cf1 100644 --- a/source/hailo.js +++ b/source/hailo.js @@ -1,6 +1,6 @@ // Experimental -var hailo = {}; +const hailo = {}; hailo.ModelFactory = class { @@ -324,6 +324,5 @@ hailo.Error = class extends Error { } }; -if (typeof module !== 'undefined' && typeof module.exports === 'object') { - module.exports.ModelFactory = hailo.ModelFactory; -} +export const ModelFactory = hailo.ModelFactory; + diff --git a/source/hdf5.js b/source/hdf5.js index 422ef05d03c..e43f4b503e5 100755 --- a/source/hdf5.js +++ b/source/hdf5.js @@ -1,8 +1,9 @@ // Experimental HDF5 reader -var hdf5 = {}; -var zip = require('./zip'); +import * as zip from './zip.js'; + +const hdf5 = {}; hdf5.File = class { @@ -247,8 +248,8 @@ hdf5.Variable = class { } } const chunk_offset = node.fields; - var data_pos = chunk_offset.slice(); - var chunk_pos = data_pos.map(() => 0); + const data_pos = chunk_offset.slice(); + const chunk_pos = data_pos.map(() => 0); for (let chunk_index = 0; chunk_index < chunk_size; chunk_index++) { for (let i = max_dim; i >= 0; i--) { if (chunk_pos[i] >= chunk_shape[i]) { @@ -1630,6 +1631,4 @@ hdf5.Error = class extends Error { } }; -if (typeof module !== 'undefined' && typeof module.exports === 'object') { - module.exports.File = hdf5.File; -} \ No newline at end of file +export const File = hdf5.File; diff --git a/source/hickle.js b/source/hickle.js index 99e2de7ca06..f4146d12f76 100644 --- a/source/hickle.js +++ b/source/hickle.js @@ -1,5 +1,5 @@ -var hickle = {}; +const hickle = {}; hickle.ModelFactory = class { @@ -257,6 +257,4 @@ hickle.Error = class extends Error { } }; -if (typeof module !== 'undefined' && typeof module.exports === 'object') { - module.exports.ModelFactory = hickle.ModelFactory; -} \ No newline at end of file +export const ModelFactory = hickle.ModelFactory; diff --git a/source/imgdnn.js b/source/imgdnn.js index e6724a46b64..6a3dfe270be 100644 --- a/source/imgdnn.js +++ b/source/imgdnn.js @@ -1,5 +1,5 @@ -var imgdnn = {}; +const imgdnn = {}; imgdnn.ModelFactory = class { @@ -62,6 +62,5 @@ imgdnn.Error = class extends Error { } }; -if (typeof module !== 'undefined' && typeof module.exports === 'object') { - module.exports.ModelFactory = imgdnn.ModelFactory; -} +export const ModelFactory = imgdnn.ModelFactory; + diff --git a/source/index.js b/source/index.js index 3cd951bc283..bcde6a4ab3a 100644 --- a/source/index.js +++ b/source/index.js @@ -1,60 +1,44 @@ /* eslint-env es2015 */ +/* eslint-disable no-var */ + if (window.location.hostname.endsWith('.github.io')) { window.location.replace('https://netron.app'); } -window.require = function(id, callback, preload) { - var name = id.startsWith('./') ? id.substring(2) : id; - var key = name === 'browser' ? 'host' : name; - var value = window[key]; - if (callback) { - if (value) { - return callback(value); +window.exports = {}; + +window.exports.require = function(id, callback) { + var url = new URL(id + '.js', window.location.href).href; + var scripts = document.head.getElementsByTagName('script'); + for (var i = 0; i < scripts.length; i++) { + if (url === scripts[i].getAttribute('src')) { + throw new Error("Duplicate import of '" + url + "'."); } - window.module = { exports: {} }; - var url = new URL(id + '.js', window.location.href).href; - var script = document.createElement('script'); - script.setAttribute('id', 'script-' + id); - script.setAttribute('type', 'text/javascript'); - /* eslint-disable no-use-before-define */ - var loadHandler = function() { - script.removeEventListener('load', loadHandler); - script.removeEventListener('error', errorHandler); - var module = window[key]; - if (!module) { - if (preload) { - callback(null, new Error('The script \'' + id + '\' failed to load.')); - return; - } - module = window.module.exports; - window[key] = module; - } - delete window.module; - callback(module); - }; - var errorHandler = function(e) { - script.removeEventListener('load', loadHandler); - script.removeEventListener('error', errorHandler); - document.head.removeChild(script); - delete window.module; - callback(null, new Error('The script \'' + e.target.src + '\' failed to load.')); - }; - /* eslint-enable no-use-before-define */ - script.addEventListener('load', loadHandler, false); - script.addEventListener('error', errorHandler, false); - script.setAttribute('src', url); - document.head.appendChild(script); - return null; - } - if (!value) { - throw new Error("Module '" + id + "' not found."); } - return value; + var script = document.createElement('script'); + script.setAttribute('id', id); + script.setAttribute('type', 'module'); + /* eslint-disable no-use-before-define */ + var loadHandler = function() { + script.removeEventListener('load', loadHandler); + script.removeEventListener('error', errorHandler); + callback(); + }; + var errorHandler = function(e) { + script.removeEventListener('load', loadHandler); + script.removeEventListener('error', errorHandler); + callback(null, new Error('The script \'' + e.target.src + '\' failed to load.')); + }; + /* eslint-enable no-use-before-define */ + script.addEventListener('load', loadHandler, false); + script.addEventListener('error', errorHandler, false); + script.setAttribute('src', url); + document.head.appendChild(script); }; -window.preload = function(callback) { +window.exports.preload = function(callback) { var modules = [ [ './view' ], [ './json', './xml', './protobuf', './hdf5', './grapher', './browser' ], @@ -68,7 +52,7 @@ window.preload = function(callback) { var ids = modules.pop(); var resolved = ids.length; for (var i = 0; i < ids.length; i++) { - window.require(ids[i], function(module, error) { + window.exports.require(ids[i], function(module, error) { if (error) { callback(null, error); return; @@ -83,7 +67,7 @@ window.preload = function(callback) { next(); }; -window.terminate = function(message, action, callback) { +window.exports.terminate = function(message, action, callback) { document.getElementById('message-text').innerText = message; var button = document.getElementById('message-button'); if (action) { @@ -107,22 +91,21 @@ window.terminate = function(message, action, callback) { document.body.setAttribute('class', 'welcome message'); }; -window.addEventListener('error', (event) => { +window.addEventListener('error', function (event) { var error = event instanceof ErrorEvent && event.error && event.error instanceof Error ? event.error : new Error(event && event.message ? event.message : JSON.stringify(event)); - window.terminate(error.message); + window.exports.terminate(error.message); }); window.addEventListener('load', function() { if (!Symbol || !Symbol.asyncIterator) { throw new Error('Your browser is not supported.'); } - window.preload(function(value, error) { + window.exports.preload(function(value, error) { if (error) { - window.terminate(error.message); + window.exports.terminate(error.message); } else { - var host = new window.host.BrowserHost(); - var view = require('./view'); - window.__view__ = new view.View(host); + var host = new window.exports.browser.BrowserHost(); + window.__view__ = new window.exports.view.View(host); window.__view__.start(); } }); diff --git a/source/json.js b/source/json.js index 19aa7e65cf2..c776dbf5268 100755 --- a/source/json.js +++ b/source/json.js @@ -1,7 +1,8 @@ -var json = {}; -var bson = {}; -var text = require('./text'); +import * as text from './text.js'; + +const json = {}; +const bson = {}; json.TextReader = class { @@ -559,7 +560,5 @@ bson.Error = class extends Error { } }; -if (typeof module !== 'undefined' && typeof module.exports === 'object') { - module.exports.TextReader = json.TextReader; - module.exports.BinaryReader = json.BinaryReader; -} \ No newline at end of file +export const TextReader = json.TextReader; +export const BinaryReader = json.BinaryReader; diff --git a/source/keras-proto.js b/source/keras-proto.js index 8da028efe7d..16455c67f61 100644 --- a/source/keras-proto.js +++ b/source/keras-proto.js @@ -1,4 +1,7 @@ -var $root = protobuf.get('tf'); + +import * as protobuf from './protobuf.js'; + +const $root = protobuf.get('tf'); $root.third_party = {}; diff --git a/source/keras.js b/source/keras.js index 94db5c3e574..c2236f378f2 100644 --- a/source/keras.js +++ b/source/keras.js @@ -1,9 +1,10 @@ -var keras = {}; -var tfjs = {}; -var json = require('./json'); -var python = require('./python'); -var protobuf = require('./protobuf'); +import * as json from './json.js'; +import * as python from './python.js'; +import * as protobuf from './protobuf.js'; + +const keras = {}; +const tfjs = {}; keras.ModelFactory = class { @@ -1561,6 +1562,4 @@ tfjs.Error = class extends Error { } }; -if (typeof module !== 'undefined' && typeof module.exports === 'object') { - module.exports.ModelFactory = keras.ModelFactory; -} \ No newline at end of file +export const ModelFactory = keras.ModelFactory; diff --git a/source/kmodel.js b/source/kmodel.js index 76d5256f0c8..77dd60cb4c4 100644 --- a/source/kmodel.js +++ b/source/kmodel.js @@ -1,6 +1,7 @@ -var kmodel = {}; -var base = require('./base'); +import * as base from './base.js'; + +const kmodel = {}; kmodel.ModelFactory = class { @@ -1351,6 +1352,4 @@ kmodel.Error = class extends Error { } }; -if (typeof module !== 'undefined' && typeof module.exports === 'object') { - module.exports.ModelFactory = kmodel.ModelFactory; -} \ No newline at end of file +export const ModelFactory = kmodel.ModelFactory; diff --git a/source/lasagne.js b/source/lasagne.js index 1e070b0f521..ad3b4d3039f 100644 --- a/source/lasagne.js +++ b/source/lasagne.js @@ -1,7 +1,7 @@ // Experimental -var lasagne = {}; +const lasagne = {}; lasagne.ModelFactory = class { @@ -200,6 +200,4 @@ lasagne.Error = class extends Error { } }; -if (typeof module !== 'undefined' && typeof module.exports === 'object') { - module.exports.ModelFactory = lasagne.ModelFactory; -} \ No newline at end of file +export const ModelFactory = lasagne.ModelFactory; diff --git a/source/lightgbm.js b/source/lightgbm.js index 132947120df..653dc31705a 100644 --- a/source/lightgbm.js +++ b/source/lightgbm.js @@ -1,6 +1,7 @@ -var lightgbm = {}; -var python = require('./python'); +import * as python from './python.js'; + +const lightgbm = {}; lightgbm.ModelFactory = class { @@ -203,6 +204,5 @@ lightgbm.Error = class extends Error { } }; -if (typeof module !== 'undefined' && typeof module.exports === 'object') { - module.exports.ModelFactory = lightgbm.ModelFactory; -} +export const ModelFactory = lightgbm.ModelFactory; + diff --git a/source/mediapipe.js b/source/mediapipe.js index a9d36585e64..72caa9fffee 100644 --- a/source/mediapipe.js +++ b/source/mediapipe.js @@ -1,6 +1,7 @@ -var mediapipe = {}; -var protobuf = require('./protobuf'); +import * as protobuf from './protobuf.js'; + +const mediapipe = {}; mediapipe.ModelFactory = class { @@ -276,6 +277,4 @@ mediapipe.Error = class extends Error { } }; -if (typeof module !== 'undefined' && typeof module.exports === 'object') { - module.exports.ModelFactory = mediapipe.ModelFactory; -} \ No newline at end of file +export const ModelFactory = mediapipe.ModelFactory; diff --git a/source/megengine-schema.js b/source/megengine-schema.js index 23b36acdba3..c167123f433 100644 --- a/source/megengine-schema.js +++ b/source/megengine-schema.js @@ -1,4 +1,7 @@ -var $root = flatbuffers.get('megengine'); + +import * as flatbuffers from './flatbuffers.js'; + +const $root = flatbuffers.get('megengine'); $root.mgb = $root.mgb || {}; diff --git a/source/megengine.js b/source/megengine.js index 6d06c09f617..cae92ce1315 100644 --- a/source/megengine.js +++ b/source/megengine.js @@ -1,8 +1,9 @@ // Experimental -var megengine = {}; -var flatbuffers = require('./flatbuffers'); +import * as flatbuffers from './flatbuffers.js'; + +const megengine = {}; megengine.ModelFactory = class { @@ -414,7 +415,7 @@ megengine.Graph = class { if (opr.type === 'MultipleDeviceTensorWithFormatHolder' || opr.type === 'MultipleDeviceTensorHolder') { opr.type = 'ImmutableTensor'; } - for (var id = 0; id < opr.outputs.length; id++) { + for (let id = 0; id < opr.outputs.length; id++) { const keyId = opr.outputs[id]; const name = obj.middle_tensors[keyId] ? obj.middle_tensors[keyId].name : String(keyId); const type = opr.type; @@ -637,6 +638,5 @@ megengine.Error = class extends Error { } }; -if (typeof module !== 'undefined' && typeof module.exports === 'object') { - module.exports.ModelFactory = megengine.ModelFactory; -} +export const ModelFactory = megengine.ModelFactory; + diff --git a/source/mlir.js b/source/mlir.js index f0410e12eae..6a5f51de5fd 100644 --- a/source/mlir.js +++ b/source/mlir.js @@ -2,8 +2,9 @@ // Experimental // contributor @tucan9389 -var mlir = {}; -var text = require('./text'); +import * as text from './text.js'; + +const mlir = {}; mlir.ModelFactory = class { @@ -1214,6 +1215,5 @@ mlir.Error = class extends Error { } }; -if (typeof module !== 'undefined' && typeof module.exports === 'object') { - module.exports.ModelFactory = mlir.ModelFactory; -} +export const ModelFactory = mlir.ModelFactory; + diff --git a/source/mlnet.js b/source/mlnet.js index 6ed7d007324..295dd5ac66b 100644 --- a/source/mlnet.js +++ b/source/mlnet.js @@ -1,8 +1,9 @@ // Experimental -var mlnet = {}; -var base = require('./base'); +import * as base from './base.js'; + +const mlnet = {}; mlnet.ModelFactory = class { @@ -2455,6 +2456,4 @@ mlnet.Error = class extends Error { } }; -if (typeof module !== 'undefined' && typeof module.exports === 'object') { - module.exports.ModelFactory = mlnet.ModelFactory; -} \ No newline at end of file +export const ModelFactory = mlnet.ModelFactory; diff --git a/source/mnn-schema.js b/source/mnn-schema.js index aa8cb44dbb0..fd8f9f5f3c0 100644 --- a/source/mnn-schema.js +++ b/source/mnn-schema.js @@ -1,4 +1,7 @@ -var $root = flatbuffers.get('mnn'); + +import * as flatbuffers from './flatbuffers.js'; + +const $root = flatbuffers.get('mnn'); $root.MNN = $root.MNN || {}; diff --git a/source/mnn.js b/source/mnn.js index 38d7e120cdb..8f1a7e57608 100644 --- a/source/mnn.js +++ b/source/mnn.js @@ -1,6 +1,7 @@ -var mnn = {}; -var flatbuffers = require('./flatbuffers'); +import * as flatbuffers from './flatbuffers.js'; + +const mnn = {}; mnn.ModelFactory = class { @@ -417,6 +418,5 @@ mnn.Error = class extends Error { } }; -if (typeof module !== 'undefined' && typeof module.exports === 'object') { - module.exports.ModelFactory = mnn.ModelFactory; -} +export const ModelFactory = mnn.ModelFactory; + diff --git a/source/modular.js b/source/modular.js index 5c1aed2beff..cb88ffcec86 100644 --- a/source/modular.js +++ b/source/modular.js @@ -1,5 +1,5 @@ -var modular = {}; +const modular = {}; modular.ModelFactory = class { @@ -151,6 +151,4 @@ modular.Error = class extends Error { } }; -if (typeof module !== 'undefined' && typeof module.exports === 'object') { - module.exports.ModelFactory = modular.ModelFactory; -} \ No newline at end of file +export const ModelFactory = modular.ModelFactory; diff --git a/source/mslite-schema.js b/source/mslite-schema.js index 3ac96195144..5f2c488c029 100644 --- a/source/mslite-schema.js +++ b/source/mslite-schema.js @@ -1,4 +1,7 @@ -var $root = flatbuffers.get('mslite'); + +import * as flatbuffers from './flatbuffers.js'; + +const $root = flatbuffers.get('mslite'); $root.mindspore = $root.mindspore || {}; diff --git a/source/mslite.js b/source/mslite.js index 256b4f8ef87..e82055a1807 100644 --- a/source/mslite.js +++ b/source/mslite.js @@ -1,6 +1,7 @@ -var mslite = {}; -var flatbuffers = require('./flatbuffers'); +import * as flatbuffers from './flatbuffers.js'; + +const mslite = {}; mslite.ModelFactory = class { @@ -468,6 +469,4 @@ mslite.Error = class extends Error { } }; -if (typeof module !== 'undefined' && typeof module.exports === 'object') { - module.exports.ModelFactory = mslite.ModelFactory; -} \ No newline at end of file +export const ModelFactory = mslite.ModelFactory; diff --git a/source/mxnet.js b/source/mxnet.js index d857fd84582..08c737cc30a 100644 --- a/source/mxnet.js +++ b/source/mxnet.js @@ -1,7 +1,8 @@ -var mxnet = {}; -var json = require('./json'); -var base = require('./base'); +import * as base from './base.js'; +import * as json from './json.js'; + +const mxnet = {}; mxnet.ModelFactory = class { @@ -946,6 +947,4 @@ mxnet.Error = class extends Error { } }; -if (typeof module !== 'undefined' && typeof module.exports === 'object') { - module.exports.ModelFactory = mxnet.ModelFactory; -} \ No newline at end of file +export const ModelFactory = mxnet.ModelFactory; diff --git a/source/ncnn.js b/source/ncnn.js index 74e828644ca..a45f9e79384 100644 --- a/source/ncnn.js +++ b/source/ncnn.js @@ -1,7 +1,8 @@ -var ncnn = {}; -var text = require('./text'); -var base = require('./base'); +import * as base from './base.js'; +import * as text from './text.js'; + +const ncnn = {}; // https://github.com/Tencent/ncnn/wiki/param-and-model-file-structure // https://github.com/Tencent/ncnn/wiki/operation-param-weight-table @@ -923,6 +924,5 @@ ncnn.Error = class extends Error { } }; -if (typeof module !== 'undefined' && typeof module.exports === 'object') { - module.exports.ModelFactory = ncnn.ModelFactory; -} +export const ModelFactory = ncnn.ModelFactory; + diff --git a/source/nnabla-proto.js b/source/nnabla-proto.js index f731ce1bfe3..7d2878ada6b 100644 --- a/source/nnabla-proto.js +++ b/source/nnabla-proto.js @@ -1,4 +1,7 @@ -var $root = protobuf.get('nnabla'); + +import * as protobuf from './protobuf.js'; + +const $root = protobuf.get('nnabla'); $root.nnabla = {}; diff --git a/source/nnabla.js b/source/nnabla.js index 93f595d2aa9..9425ece3288 100644 --- a/source/nnabla.js +++ b/source/nnabla.js @@ -1,7 +1,8 @@ -var nnabla = {}; -var protobuf = require('./protobuf'); -var text = require('./text'); +import * as protobuf from './protobuf.js'; +import * as text from './text.js'; + +const nnabla = {}; nnabla.ModelFactory = class { @@ -281,6 +282,4 @@ nnabla.Error = class extends Error { } }; -if (typeof module !== 'undefined' && typeof module.exports === 'object') { - module.exports.ModelFactory = nnabla.ModelFactory; -} +export const ModelFactory = nnabla.ModelFactory; diff --git a/source/nnc.js b/source/nnc.js index da37e7ead24..457257a44fa 100644 --- a/source/nnc.js +++ b/source/nnc.js @@ -1,5 +1,5 @@ -var nnc = {}; +const nnc = {}; nnc.ModelFactory = class { @@ -25,8 +25,7 @@ nnc.Error = class extends Error { } }; -if (typeof module !== 'undefined' && typeof module.exports === 'object') { - module.exports.ModelFactory = nnc.ModelFactory; -} +export const ModelFactory = nnc.ModelFactory; + diff --git a/source/nnef.js b/source/nnef.js index cc795d8738c..53ce61e3113 100644 --- a/source/nnef.js +++ b/source/nnef.js @@ -1,6 +1,7 @@ -var nnef = {}; -var text = require('./text'); +import * as text from './text.js'; + +const nnef = {}; nnef.ModelFactory = class { @@ -79,6 +80,4 @@ nnef.Error = class extends Error { } }; -if (typeof module !== 'undefined' && typeof module.exports === 'object') { - module.exports.ModelFactory = nnef.ModelFactory; -} +export const ModelFactory = nnef.ModelFactory; diff --git a/source/numpy.js b/source/numpy.js index 59b46584577..21ffe6c015c 100644 --- a/source/numpy.js +++ b/source/numpy.js @@ -1,8 +1,9 @@ // Experimental -var numpy = {}; -var python = require('./python'); +import * as python from './python.js'; + +const numpy = {}; numpy.ModelFactory = class { @@ -353,6 +354,4 @@ numpy.Error = class extends Error { } }; -if (typeof module !== 'undefined' && typeof module.exports === 'object') { - module.exports.ModelFactory = numpy.ModelFactory; -} +export const ModelFactory = numpy.ModelFactory; diff --git a/source/om-proto.js b/source/om-proto.js index f95f16a605d..6712bae64e2 100644 --- a/source/om-proto.js +++ b/source/om-proto.js @@ -1,4 +1,7 @@ -var $root = protobuf.get('om'); + +import * as protobuf from './protobuf.js'; + +const $root = protobuf.get('om'); $root.ge = {}; diff --git a/source/om.js b/source/om.js index 0d924fe8f50..3d440057d45 100644 --- a/source/om.js +++ b/source/om.js @@ -1,10 +1,11 @@ // Experimental -var om = {}; -var svp = {}; -var protobuf = require('./protobuf'); -var base = require('./base'); +import * as base from './base.js'; +import * as protobuf from './protobuf.js'; + +const om = {}; +const svp = {}; om.ModelFactory = class { @@ -789,6 +790,4 @@ svp.Error = class extends Error { } }; -if (typeof module !== 'undefined' && typeof module.exports === 'object') { - module.exports.ModelFactory = om.ModelFactory; -} +export const ModelFactory = om.ModelFactory; diff --git a/source/onednn.js b/source/onednn.js index ad551f2836c..ee0f065debf 100644 --- a/source/onednn.js +++ b/source/onednn.js @@ -1,5 +1,5 @@ -var onednn = {}; +const onednn = {}; onednn.ModelFactory = class { @@ -403,6 +403,5 @@ onednn.Error = class extends Error { } }; -if (typeof module !== 'undefined' && typeof module.exports === 'object') { - module.exports.ModelFactory = onednn.ModelFactory; -} +export const ModelFactory = onednn.ModelFactory; + diff --git a/source/onnx-proto.js b/source/onnx-proto.js index 6c19b3c1c8c..8c58f497c5d 100644 --- a/source/onnx-proto.js +++ b/source/onnx-proto.js @@ -1,4 +1,7 @@ -var $root = protobuf.get('onnx'); + +import * as protobuf from './protobuf.js'; + +const $root = protobuf.get('onnx'); $root.onnx = {}; diff --git a/source/onnx-schema.js b/source/onnx-schema.js index ade2bd66d85..e9a2b850986 100644 --- a/source/onnx-schema.js +++ b/source/onnx-schema.js @@ -1,4 +1,7 @@ -var $root = flatbuffers.get('ort'); + +import * as flatbuffers from './flatbuffers.js'; + +const $root = flatbuffers.get('ort'); $root.onnxruntime = $root.onnxruntime || {}; diff --git a/source/onnx.js b/source/onnx.js index 934854f0306..9371ad20e7c 100644 --- a/source/onnx.js +++ b/source/onnx.js @@ -1,8 +1,9 @@ -var onnx = {}; -var protobuf = require('./protobuf'); -var flatbuffers = require('./flatbuffers'); -var text = require('./text'); +import * as protobuf from './protobuf.js'; +import * as flatbuffers from './flatbuffers.js'; +import * as text from './text.js'; + +const onnx = {}; onnx.ModelFactory = class { @@ -2816,6 +2817,4 @@ onnx.Error = class extends Error { } }; -if (typeof module !== 'undefined' && typeof module.exports === 'object') { - module.exports.ModelFactory = onnx.ModelFactory; -} +export const ModelFactory = onnx.ModelFactory; diff --git a/source/openvino.js b/source/openvino.js index 0b42e22471d..d88eff03b0f 100644 --- a/source/openvino.js +++ b/source/openvino.js @@ -1,6 +1,7 @@ -var openvino = {}; -var xml = require('./xml'); +import * as xml from './xml.js'; + +const openvino = {}; openvino.ModelFactory = class { @@ -785,6 +786,5 @@ openvino.Error = class extends Error { } }; -if (typeof module !== 'undefined' && typeof module.exports === 'object') { - module.exports.ModelFactory = openvino.ModelFactory; -} +export const ModelFactory = openvino.ModelFactory; + diff --git a/source/paddle-proto.js b/source/paddle-proto.js index 4d388aeeece..e298cc15fbd 100644 --- a/source/paddle-proto.js +++ b/source/paddle-proto.js @@ -1,4 +1,7 @@ -var $root = protobuf.get('paddle'); + +import * as protobuf from './protobuf.js'; + +const $root = protobuf.get('paddle'); $root.paddle = {}; @@ -92,10 +95,10 @@ $root.paddle.framework.proto.Complex = class Complex { } } if (!Object.prototype.hasOwnProperty.call(message, 'r')) { - throw new protobuf.Error("Excepted 'r'."); + throw new Error("Excepted 'r'."); } if (!Object.prototype.hasOwnProperty.call(message, 'i')) { - throw new protobuf.Error("Excepted 'i'."); + throw new Error("Excepted 'i'."); } return message; } @@ -118,10 +121,10 @@ $root.paddle.framework.proto.Complex = class Complex { } } if (!Object.prototype.hasOwnProperty.call(message, "r")) { - throw new protobuf.Error("Excepted 'r'."); + throw new Error("Excepted 'r'."); } if (!Object.prototype.hasOwnProperty.call(message, "i")) { - throw new protobuf.Error("Excepted 'i'."); + throw new Error("Excepted 'i'."); } return message; } @@ -162,7 +165,7 @@ $root.paddle.framework.proto.Scalar = class Scalar { } } if (!Object.prototype.hasOwnProperty.call(message, 'type')) { - throw new protobuf.Error("Excepted 'type'."); + throw new Error("Excepted 'type'."); } return message; } @@ -194,7 +197,7 @@ $root.paddle.framework.proto.Scalar = class Scalar { } } if (!Object.prototype.hasOwnProperty.call(message, "type")) { - throw new protobuf.Error("Excepted 'type'."); + throw new Error("Excepted 'type'."); } return message; } @@ -248,7 +251,7 @@ $root.paddle.framework.proto.OpDesc = class OpDesc { } } if (!Object.prototype.hasOwnProperty.call(message, 'type')) { - throw new protobuf.Error("Excepted 'type'."); + throw new Error("Excepted 'type'."); } return message; } @@ -280,7 +283,7 @@ $root.paddle.framework.proto.OpDesc = class OpDesc { } } if (!Object.prototype.hasOwnProperty.call(message, "type")) { - throw new protobuf.Error("Excepted 'type'."); + throw new Error("Excepted 'type'."); } return message; } @@ -375,10 +378,10 @@ $root.paddle.framework.proto.OpDesc.Attr = class Attr { } } if (!Object.prototype.hasOwnProperty.call(message, 'name')) { - throw new protobuf.Error("Excepted 'name'."); + throw new Error("Excepted 'name'."); } if (!Object.prototype.hasOwnProperty.call(message, 'type')) { - throw new protobuf.Error("Excepted 'type'."); + throw new Error("Excepted 'type'."); } return message; } @@ -455,10 +458,10 @@ $root.paddle.framework.proto.OpDesc.Attr = class Attr { } } if (!Object.prototype.hasOwnProperty.call(message, "name")) { - throw new protobuf.Error("Excepted 'name'."); + throw new Error("Excepted 'name'."); } if (!Object.prototype.hasOwnProperty.call(message, "type")) { - throw new protobuf.Error("Excepted 'type'."); + throw new Error("Excepted 'type'."); } return message; } @@ -500,7 +503,7 @@ $root.paddle.framework.proto.OpDesc.Var = class Var { } } if (!Object.prototype.hasOwnProperty.call(message, 'parameter')) { - throw new protobuf.Error("Excepted 'parameter'."); + throw new Error("Excepted 'parameter'."); } return message; } @@ -523,7 +526,7 @@ $root.paddle.framework.proto.OpDesc.Var = class Var { } } if (!Object.prototype.hasOwnProperty.call(message, "parameter")) { - throw new protobuf.Error("Excepted 'parameter'."); + throw new Error("Excepted 'parameter'."); } return message; } @@ -566,10 +569,10 @@ $root.paddle.framework.proto.OpProto = class OpProto { } } if (!Object.prototype.hasOwnProperty.call(message, 'type')) { - throw new protobuf.Error("Excepted 'type'."); + throw new Error("Excepted 'type'."); } if (!Object.prototype.hasOwnProperty.call(message, 'comment')) { - throw new protobuf.Error("Excepted 'comment'."); + throw new Error("Excepted 'comment'."); } return message; } @@ -601,10 +604,10 @@ $root.paddle.framework.proto.OpProto = class OpProto { } } if (!Object.prototype.hasOwnProperty.call(message, "type")) { - throw new protobuf.Error("Excepted 'type'."); + throw new Error("Excepted 'type'."); } if (!Object.prototype.hasOwnProperty.call(message, "comment")) { - throw new protobuf.Error("Excepted 'comment'."); + throw new Error("Excepted 'comment'."); } return message; } @@ -651,10 +654,10 @@ $root.paddle.framework.proto.OpProto.Var = class Var { } } if (!Object.prototype.hasOwnProperty.call(message, 'name')) { - throw new protobuf.Error("Excepted 'name'."); + throw new Error("Excepted 'name'."); } if (!Object.prototype.hasOwnProperty.call(message, 'comment')) { - throw new protobuf.Error("Excepted 'comment'."); + throw new Error("Excepted 'comment'."); } return message; } @@ -692,10 +695,10 @@ $root.paddle.framework.proto.OpProto.Var = class Var { } } if (!Object.prototype.hasOwnProperty.call(message, "name")) { - throw new protobuf.Error("Excepted 'name'."); + throw new Error("Excepted 'name'."); } if (!Object.prototype.hasOwnProperty.call(message, "comment")) { - throw new protobuf.Error("Excepted 'comment'."); + throw new Error("Excepted 'comment'."); } return message; } @@ -747,13 +750,13 @@ $root.paddle.framework.proto.OpProto.Attr = class Attr { } } if (!Object.prototype.hasOwnProperty.call(message, 'name')) { - throw new protobuf.Error("Excepted 'name'."); + throw new Error("Excepted 'name'."); } if (!Object.prototype.hasOwnProperty.call(message, 'type')) { - throw new protobuf.Error("Excepted 'type'."); + throw new Error("Excepted 'type'."); } if (!Object.prototype.hasOwnProperty.call(message, 'comment')) { - throw new protobuf.Error("Excepted 'comment'."); + throw new Error("Excepted 'comment'."); } return message; } @@ -791,13 +794,13 @@ $root.paddle.framework.proto.OpProto.Attr = class Attr { } } if (!Object.prototype.hasOwnProperty.call(message, "name")) { - throw new protobuf.Error("Excepted 'name'."); + throw new Error("Excepted 'name'."); } if (!Object.prototype.hasOwnProperty.call(message, "type")) { - throw new protobuf.Error("Excepted 'type'."); + throw new Error("Excepted 'type'."); } if (!Object.prototype.hasOwnProperty.call(message, "comment")) { - throw new protobuf.Error("Excepted 'comment'."); + throw new Error("Excepted 'comment'."); } return message; } @@ -861,7 +864,7 @@ $root.paddle.framework.proto.VarType = class VarType { } } if (!Object.prototype.hasOwnProperty.call(message, 'type')) { - throw new protobuf.Error("Excepted 'type'."); + throw new Error("Excepted 'type'."); } return message; } @@ -911,7 +914,7 @@ $root.paddle.framework.proto.VarType = class VarType { } } if (!Object.prototype.hasOwnProperty.call(message, "type")) { - throw new protobuf.Error("Excepted 'type'."); + throw new Error("Excepted 'type'."); } return message; } @@ -987,7 +990,7 @@ $root.paddle.framework.proto.VarType.TensorDesc = class TensorDesc { } } if (!Object.prototype.hasOwnProperty.call(message, 'data_type')) { - throw new protobuf.Error("Excepted 'data_type'."); + throw new Error("Excepted 'data_type'."); } return message; } @@ -1010,7 +1013,7 @@ $root.paddle.framework.proto.VarType.TensorDesc = class TensorDesc { } } if (!Object.prototype.hasOwnProperty.call(message, "data_type")) { - throw new protobuf.Error("Excepted 'data_type'."); + throw new Error("Excepted 'data_type'."); } return message; } @@ -1041,7 +1044,7 @@ $root.paddle.framework.proto.VarType.LoDTensorDesc = class LoDTensorDesc { } } if (!Object.prototype.hasOwnProperty.call(message, 'tensor')) { - throw new protobuf.Error("Excepted 'tensor'."); + throw new Error("Excepted 'tensor'."); } return message; } @@ -1064,7 +1067,7 @@ $root.paddle.framework.proto.VarType.LoDTensorDesc = class LoDTensorDesc { } } if (!Object.prototype.hasOwnProperty.call(message, "tensor")) { - throw new protobuf.Error("Excepted 'tensor'."); + throw new Error("Excepted 'tensor'."); } return message; } @@ -1096,7 +1099,7 @@ $root.paddle.framework.proto.VarType.LoDTensorArrayDesc = class LoDTensorArrayDe } } if (!Object.prototype.hasOwnProperty.call(message, 'tensor')) { - throw new protobuf.Error("Excepted 'tensor'."); + throw new Error("Excepted 'tensor'."); } return message; } @@ -1119,7 +1122,7 @@ $root.paddle.framework.proto.VarType.LoDTensorArrayDesc = class LoDTensorArrayDe } } if (!Object.prototype.hasOwnProperty.call(message, "tensor")) { - throw new protobuf.Error("Excepted 'tensor'."); + throw new Error("Excepted 'tensor'."); } return message; } @@ -1249,10 +1252,10 @@ $root.paddle.framework.proto.VarDesc = class VarDesc { } } if (!Object.prototype.hasOwnProperty.call(message, 'name')) { - throw new protobuf.Error("Excepted 'name'."); + throw new Error("Excepted 'name'."); } if (!Object.prototype.hasOwnProperty.call(message, 'type')) { - throw new protobuf.Error("Excepted 'type'."); + throw new Error("Excepted 'type'."); } return message; } @@ -1290,10 +1293,10 @@ $root.paddle.framework.proto.VarDesc = class VarDesc { } } if (!Object.prototype.hasOwnProperty.call(message, "name")) { - throw new protobuf.Error("Excepted 'name'."); + throw new Error("Excepted 'name'."); } if (!Object.prototype.hasOwnProperty.call(message, "type")) { - throw new protobuf.Error("Excepted 'type'."); + throw new Error("Excepted 'type'."); } return message; } @@ -1339,10 +1342,10 @@ $root.paddle.framework.proto.VarDesc.Attr = class Attr { } } if (!Object.prototype.hasOwnProperty.call(message, 'name')) { - throw new protobuf.Error("Excepted 'name'."); + throw new Error("Excepted 'name'."); } if (!Object.prototype.hasOwnProperty.call(message, 'type')) { - throw new protobuf.Error("Excepted 'type'."); + throw new Error("Excepted 'type'."); } return message; } @@ -1374,10 +1377,10 @@ $root.paddle.framework.proto.VarDesc.Attr = class Attr { } } if (!Object.prototype.hasOwnProperty.call(message, "name")) { - throw new protobuf.Error("Excepted 'name'."); + throw new Error("Excepted 'name'."); } if (!Object.prototype.hasOwnProperty.call(message, "type")) { - throw new protobuf.Error("Excepted 'type'."); + throw new Error("Excepted 'type'."); } return message; } @@ -1422,10 +1425,10 @@ $root.paddle.framework.proto.BlockDesc = class BlockDesc { } } if (!Object.prototype.hasOwnProperty.call(message, 'idx')) { - throw new protobuf.Error("Excepted 'idx'."); + throw new Error("Excepted 'idx'."); } if (!Object.prototype.hasOwnProperty.call(message, 'parent_idx')) { - throw new protobuf.Error("Excepted 'parent_idx'."); + throw new Error("Excepted 'parent_idx'."); } return message; } @@ -1457,10 +1460,10 @@ $root.paddle.framework.proto.BlockDesc = class BlockDesc { } } if (!Object.prototype.hasOwnProperty.call(message, "idx")) { - throw new protobuf.Error("Excepted 'idx'."); + throw new Error("Excepted 'idx'."); } if (!Object.prototype.hasOwnProperty.call(message, "parent_idx")) { - throw new protobuf.Error("Excepted 'parent_idx'."); + throw new Error("Excepted 'parent_idx'."); } return message; } @@ -1490,7 +1493,7 @@ $root.paddle.framework.proto.OpVersion = class OpVersion { } } if (!Object.prototype.hasOwnProperty.call(message, 'version')) { - throw new protobuf.Error("Excepted 'version'."); + throw new Error("Excepted 'version'."); } return message; } @@ -1510,7 +1513,7 @@ $root.paddle.framework.proto.OpVersion = class OpVersion { } } if (!Object.prototype.hasOwnProperty.call(message, "version")) { - throw new protobuf.Error("Excepted 'version'."); + throw new Error("Excepted 'version'."); } return message; } @@ -1582,10 +1585,10 @@ $root.paddle.framework.proto.OpVersionMap.OpVersionPair = class OpVersionPair { } } if (!Object.prototype.hasOwnProperty.call(message, 'op_name')) { - throw new protobuf.Error("Excepted 'op_name'."); + throw new Error("Excepted 'op_name'."); } if (!Object.prototype.hasOwnProperty.call(message, 'op_version')) { - throw new protobuf.Error("Excepted 'op_version'."); + throw new Error("Excepted 'op_version'."); } return message; } @@ -1608,10 +1611,10 @@ $root.paddle.framework.proto.OpVersionMap.OpVersionPair = class OpVersionPair { } } if (!Object.prototype.hasOwnProperty.call(message, "op_name")) { - throw new protobuf.Error("Excepted 'op_name'."); + throw new Error("Excepted 'op_name'."); } if (!Object.prototype.hasOwnProperty.call(message, "op_version")) { - throw new protobuf.Error("Excepted 'op_version'."); + throw new Error("Excepted 'op_version'."); } return message; } diff --git a/source/paddle-schema.js b/source/paddle-schema.js index dbdb2f5db9a..3d3d52267ac 100644 --- a/source/paddle-schema.js +++ b/source/paddle-schema.js @@ -1,4 +1,7 @@ -var $root = flatbuffers.get('paddlelite'); + +import * as flatbuffers from './flatbuffers.js'; + +const $root = flatbuffers.get('paddlelite'); $root.paddle = $root.paddle || {}; diff --git a/source/paddle.js b/source/paddle.js index 833fe61f2fd..36adc0e116e 100644 --- a/source/paddle.js +++ b/source/paddle.js @@ -1,9 +1,10 @@ -var paddle = {}; -var flatbuffers = require('./flatbuffers'); -var protobuf = require('./protobuf'); -var python = require('./python'); -var base = require('./base'); +import * as base from './base.js'; +import * as flatbuffers from './flatbuffers.js'; +import * as protobuf from './protobuf.js'; +import * as python from './python.js'; + +const paddle = {}; paddle.ModelFactory = class { @@ -970,6 +971,4 @@ paddle.Error = class extends Error { } }; -if (typeof module !== 'undefined' && typeof module.exports === 'object') { - module.exports.ModelFactory = paddle.ModelFactory; -} +export const ModelFactory = paddle.ModelFactory; diff --git a/source/pickle.js b/source/pickle.js index 018a9be2168..5601d1914a3 100644 --- a/source/pickle.js +++ b/source/pickle.js @@ -1,7 +1,7 @@ // Experimental -var pickle = {}; +const pickle = {}; pickle.ModelFactory = class { @@ -191,6 +191,4 @@ pickle.Error = class extends Error { } }; -if (typeof module !== 'undefined' && typeof module.exports === 'object') { - module.exports.ModelFactory = pickle.ModelFactory; -} \ No newline at end of file +export const ModelFactory = pickle.ModelFactory; diff --git a/source/protobuf.js b/source/protobuf.js index 791985abf2e..88a619a568c 100644 --- a/source/protobuf.js +++ b/source/protobuf.js @@ -1,7 +1,8 @@ -var protobuf = {}; -var base = require('./base'); -var text = require('./text'); +import * as base from './base.js'; +import * as text from './text.js'; + +const protobuf = {}; protobuf.get = (name) => { protobuf._roots = protobuf._roots || new Map(); @@ -1324,11 +1325,8 @@ protobuf.Error = class extends Error { } }; -if (typeof module !== 'undefined' && typeof module.exports === 'object') { - module.exports.BinaryReader = protobuf.BinaryReader; - module.exports.TextReader = protobuf.TextReader; - module.exports.Error = protobuf.Error; - module.exports.Int64 = protobuf.Int64; - module.exports.Uint64 = protobuf.Uint64; - module.exports.get = protobuf.get; -} \ No newline at end of file +export const BinaryReader = protobuf.BinaryReader; +export const TextReader = protobuf.TextReader; +export const Int64 = protobuf.Int64; +export const Uint64 = protobuf.Uint64; +export const get = protobuf.get; diff --git a/source/python.js b/source/python.js index d3fad6f2a96..3ca89907893 100644 --- a/source/python.js +++ b/source/python.js @@ -1,7 +1,7 @@ // Experimental Python Execution -var python = {}; +const python = {}; python.Parser = class { @@ -2280,7 +2280,7 @@ python.Execution = class { } else { content += '['; } - for (var t of Object.values(this.type)) { + for (const t of Object.values(this.type)) { content += t.__name__; } content += ']'; @@ -6719,6 +6719,4 @@ python.Error = class extends Error { } }; -if (typeof module !== 'undefined' && typeof module.exports === 'object') { - module.exports.Execution = python.Execution; -} \ No newline at end of file +export const Execution = python.Execution; diff --git a/source/pytorch-schema.js b/source/pytorch-schema.js index 0b779d5d54c..bb5cd626355 100644 --- a/source/pytorch-schema.js +++ b/source/pytorch-schema.js @@ -1,4 +1,7 @@ -var $root = flatbuffers.get('torch'); + +import * as flatbuffers from './flatbuffers.js'; + +const $root = flatbuffers.get('torch'); $root.torch = $root.torch || {}; diff --git a/source/pytorch.js b/source/pytorch.js index 817b2535ee1..ff7467f19e3 100644 --- a/source/pytorch.js +++ b/source/pytorch.js @@ -1,10 +1,11 @@ // Experimental -var pytorch = {}; -var python = require('./python'); -var base = require('./base'); -var flatbuffers = require('./flatbuffers'); +import * as base from './base.js'; +import * as flatbuffers from './flatbuffers.js'; +import * as python from './python.js'; + +const pytorch = {}; pytorch.ModelFactory = class { @@ -4247,6 +4248,4 @@ pytorch.Error = class extends Error { } }; -if (typeof module !== 'undefined' && typeof module.exports === 'object') { - module.exports.ModelFactory = pytorch.ModelFactory; -} +export const ModelFactory = pytorch.ModelFactory; diff --git a/source/rknn-schema.js b/source/rknn-schema.js index 8988c5501dc..64805502915 100644 --- a/source/rknn-schema.js +++ b/source/rknn-schema.js @@ -1,4 +1,7 @@ -var $root = flatbuffers.get('rknn'); + +import * as flatbuffers from './flatbuffers.js'; + +const $root = flatbuffers.get('rknn'); $root.rknn = $root.rknn || {}; diff --git a/source/rknn.js b/source/rknn.js index f7469db59ca..44abaf3f36f 100644 --- a/source/rknn.js +++ b/source/rknn.js @@ -1,9 +1,10 @@ -var rknn = {}; -var openvx = {}; -var base = require('./base'); -var flatbuffers = require('./flatbuffers'); -var json = require('./json'); +import * as base from './base.js'; +import * as flatbuffers from './flatbuffers.js'; +import * as json from './json.js'; + +const rknn = {}; +const openvx = {}; rknn.ModelFactory = class { @@ -678,6 +679,4 @@ rknn.Error = class extends Error { } }; -if (typeof module !== 'undefined' && typeof module.exports === 'object') { - module.exports.ModelFactory = rknn.ModelFactory; -} +export const ModelFactory = rknn.ModelFactory; diff --git a/source/safetensors.js b/source/safetensors.js index 9cd4b8e66a9..23370f52d4a 100644 --- a/source/safetensors.js +++ b/source/safetensors.js @@ -1,6 +1,7 @@ -var safetensors = {}; -var json = require('./json'); +import * as json from './json.js'; + +const safetensors = {}; safetensors.ModelFactory = class { @@ -159,6 +160,4 @@ safetensors.Error = class extends Error { } }; -if (typeof module !== 'undefined' && typeof module.exports === 'object') { - module.exports.ModelFactory = safetensors.ModelFactory; -} +export const ModelFactory = safetensors.ModelFactory; diff --git a/source/sentencepiece-proto.js b/source/sentencepiece-proto.js index 73b2044d1c3..100c6a645c2 100644 --- a/source/sentencepiece-proto.js +++ b/source/sentencepiece-proto.js @@ -1,4 +1,7 @@ -var $root = protobuf.get('sentencepiece'); + +import * as protobuf from './protobuf.js'; + +const $root = protobuf.get('sentencepiece'); $root.sentencepiece = {}; diff --git a/source/sentencepiece.js b/source/sentencepiece.js index 38b508d0db8..ea5c65703b5 100644 --- a/source/sentencepiece.js +++ b/source/sentencepiece.js @@ -1,6 +1,7 @@ -var sentencepiece = {}; -var protobuf = require('./protobuf'); +import * as protobuf from './protobuf.js'; + +const sentencepiece = {}; sentencepiece.ModelFactory = class { @@ -54,6 +55,5 @@ sentencepiece.Error = class extends Error { } }; -if (typeof module !== 'undefined' && typeof module.exports === 'object') { - module.exports.ModelFactory = sentencepiece.ModelFactory; -} +export const ModelFactory = sentencepiece.ModelFactory; + diff --git a/source/server.js b/source/server.js index 2cef8c0a6a6..762f499b224 100644 --- a/source/server.js +++ b/source/server.js @@ -1,7 +1,7 @@ // Experimental -var message = {}; +const message = {}; message.ModelFactory = class { @@ -247,6 +247,4 @@ message.Error = class extends Error { } }; -if (typeof module !== 'undefined' && typeof module.exports === 'object') { - module.exports.ModelFactory = message.ModelFactory; -} \ No newline at end of file +export const ModelFactory = message.ModelFactory; diff --git a/source/sklearn.js b/source/sklearn.js index fc90d9cc082..e9921c7b679 100644 --- a/source/sklearn.js +++ b/source/sklearn.js @@ -1,7 +1,7 @@ // Experimental -var sklearn = {}; +const sklearn = {}; sklearn.ModelFactory = class { @@ -336,6 +336,4 @@ sklearn.Error = class extends Error { } }; -if (typeof module !== 'undefined' && typeof module.exports === 'object') { - module.exports.ModelFactory = sklearn.ModelFactory; -} \ No newline at end of file +export const ModelFactory = sklearn.ModelFactory; diff --git a/source/tar.js b/source/tar.js index f2607c5b34b..b35d2145eb1 100644 --- a/source/tar.js +++ b/source/tar.js @@ -1,5 +1,5 @@ -var tar = {}; +const tar = {}; tar.Archive = class { @@ -162,6 +162,4 @@ tar.Error = class extends Error { } }; -if (typeof module !== 'undefined' && typeof module.exports === 'object') { - module.exports.Archive = tar.Archive; -} \ No newline at end of file +export const Archive = tar.Archive; \ No newline at end of file diff --git a/source/tengine.js b/source/tengine.js index 4a023531f9d..0ef6194206e 100755 --- a/source/tengine.js +++ b/source/tengine.js @@ -1,8 +1,9 @@ // Experimental -var tengine = {}; -var base = require('./base'); +import * as base from './base.js'; + +const tengine = {}; tengine.ModelFactory = class { @@ -785,6 +786,5 @@ tengine.Error = class extends Error { } }; -if (typeof module !== 'undefined' && typeof module.exports === 'object') { - module.exports.ModelFactory = tengine.ModelFactory; -} +export const ModelFactory = tengine.ModelFactory; + diff --git a/source/tensorrt.js b/source/tensorrt.js index 28ada0121e9..aa9cf157f0f 100644 --- a/source/tensorrt.js +++ b/source/tensorrt.js @@ -1,6 +1,7 @@ -var tensorrt = {}; -var base = require('./base'); +import * as base from './base.js'; + +const tensorrt = {}; tensorrt.ModelFactory = class { @@ -178,6 +179,4 @@ tensorrt.Error = class extends Error { } }; -if (typeof module !== 'undefined' && typeof module.exports === 'object') { - module.exports.ModelFactory = tensorrt.ModelFactory; -} +export const ModelFactory = tensorrt.ModelFactory; diff --git a/source/text.js b/source/text.js index 5b386469f12..57edeb29246 100644 --- a/source/text.js +++ b/source/text.js @@ -1,5 +1,5 @@ -var text = {}; +const text = {}; text.Decoder = class { @@ -340,7 +340,5 @@ text.Error = class extends Error { } }; -if (typeof module !== 'undefined' && typeof module.exports === 'object') { - module.exports.Decoder = text.Decoder; - module.exports.Reader = text.Reader; -} +export const Decoder = text.Decoder; +export const Reader = text.Reader; diff --git a/source/tf-proto.js b/source/tf-proto.js index f5af2eb5a8b..7b4ce2e537d 100644 --- a/source/tf-proto.js +++ b/source/tf-proto.js @@ -1,4 +1,7 @@ -var $root = protobuf.get('tf'); + +import * as protobuf from './protobuf.js'; + +const $root = protobuf.get('tf'); $root.tensorflow = {}; diff --git a/source/tf.js b/source/tf.js index 3102702c7c3..80dc007890e 100644 --- a/source/tf.js +++ b/source/tf.js @@ -1,10 +1,11 @@ // Experimental -var tf = {}; -var base = require('./base'); -var protobuf = require('./protobuf'); -var zip = require('./zip'); +import * as base from './base.js'; +import * as protobuf from './protobuf.js'; +import * as zip from './zip.js'; + +const tf = {}; tf.ModelFactory = class { @@ -471,7 +472,9 @@ tf.ModelFactory = class { try { for (const key of shards.keys()) { const stream = shards.get(key); + /* eslint-disable no-await-in-loop */ const archive = zip.Archive.open(stream, 'gzip'); + /* eslint-enable no-await-in-loop */ if (archive && archive.entries.size === 1) { const stream = archive.entries.values().next().value; const buffer = stream.peek(); @@ -2496,6 +2499,4 @@ tf.Error = class extends Error { } }; -if (typeof module !== 'undefined' && typeof module.exports === 'object') { - module.exports.ModelFactory = tf.ModelFactory; -} \ No newline at end of file +export const ModelFactory = tf.ModelFactory; diff --git a/source/tflite-schema.js b/source/tflite-schema.js index 1c340c4d506..7d3cc1655ae 100644 --- a/source/tflite-schema.js +++ b/source/tflite-schema.js @@ -1,4 +1,7 @@ -var $root = flatbuffers.get('tflite'); + +import * as flatbuffers from './flatbuffers.js'; + +const $root = flatbuffers.get('tflite'); $root.tflite = $root.tflite || {}; diff --git a/source/tflite.js b/source/tflite.js index 4d78b766def..22420f90a5d 100644 --- a/source/tflite.js +++ b/source/tflite.js @@ -1,8 +1,9 @@ -var tflite = {}; -var flatbuffers = require('./flatbuffers'); -var flexbuffers = require('./flexbuffers'); -var zip = require('./zip'); +import * as flatbuffers from './flatbuffers.js'; +import * as flexbuffers from './flexbuffers.js'; +import * as zip from './zip.js'; + +const tflite = {}; tflite.ModelFactory = class { @@ -681,6 +682,4 @@ tflite.Error = class extends Error { } }; -if (typeof module !== 'undefined' && typeof module.exports === 'object') { - module.exports.ModelFactory = tflite.ModelFactory; -} \ No newline at end of file +export const ModelFactory = tflite.ModelFactory; diff --git a/source/tnn.js b/source/tnn.js index fe557f7c8a6..d0a73b9110c 100644 --- a/source/tnn.js +++ b/source/tnn.js @@ -1,7 +1,8 @@ -var tnn = {}; -var text = require('./text'); -var base = require('./base'); +import * as base from './base.js'; +import * as text from './text.js'; + +const tnn = {}; tnn.ModelFactory = class { @@ -638,6 +639,4 @@ tnn.Error = class extends Error { } }; -if (typeof module !== 'undefined' && typeof module.exports === 'object') { - module.exports.ModelFactory = tnn.ModelFactory; -} \ No newline at end of file +export const ModelFactory = tnn.ModelFactory; diff --git a/source/torch.js b/source/torch.js index ca46bef416c..8f16d6d1d27 100644 --- a/source/torch.js +++ b/source/torch.js @@ -1,5 +1,5 @@ -var torch = {}; +const torch = {}; torch.ModelFactory = class { @@ -1178,6 +1178,5 @@ torch.TextReader = class { } }; -if (typeof module !== 'undefined' && typeof module.exports === 'object') { - module.exports.ModelFactory = torch.ModelFactory; -} +export const ModelFactory = torch.ModelFactory; + diff --git a/source/uff-proto.js b/source/uff-proto.js index 7771dab1292..8b89dc54f2f 100644 --- a/source/uff-proto.js +++ b/source/uff-proto.js @@ -1,4 +1,7 @@ -var $root = protobuf.get('uff'); + +import * as protobuf from './protobuf.js'; + +const $root = protobuf.get('uff'); $root.uff = {}; diff --git a/source/uff.js b/source/uff.js index ecbab0010a5..ed524c80ccd 100644 --- a/source/uff.js +++ b/source/uff.js @@ -1,6 +1,7 @@ -var uff = {}; -var protobuf = require('./protobuf'); +import * as protobuf from './protobuf.js'; + +const uff = {}; uff.ModelFactory = class { @@ -278,6 +279,5 @@ uff.Error = class extends Error { } }; -if (typeof module !== 'undefined' && typeof module.exports === 'object') { - module.exports.ModelFactory = uff.ModelFactory; -} +export const ModelFactory = uff.ModelFactory; + diff --git a/source/view.js b/source/view.js index 622887b9976..8454783f116 100644 --- a/source/view.js +++ b/source/view.js @@ -1,16 +1,17 @@ -var view = {}; -var markdown = {}; -var base = require('./base'); -var zip = require('./zip'); -var tar = require('./tar'); -var json = require('./json'); -var xml = require('./xml'); -var protobuf = require('./protobuf'); -var flatbuffers = require('./flatbuffers'); -var hdf5 = require('./hdf5'); -var python = require('./python'); -var grapher = require('./grapher'); +import * as base from './base.js'; +import * as zip from './zip.js'; +import * as tar from './tar.js'; +import * as json from './json.js'; +import * as xml from './xml.js'; +import * as protobuf from './protobuf.js'; +import * as flatbuffers from './flatbuffers.js'; +import * as hdf5 from './hdf5.js'; +import * as python from './python.js'; +import * as grapher from './grapher.js'; + +const view = {}; +const markdown = {}; view.View = class { @@ -34,6 +35,7 @@ view.View = class { async start() { try { + await zip.Archive.import(); await this._host.view(this); const options = this._host.get('options') || {}; for (const [name, value] of Object.entries(options)) { @@ -5273,7 +5275,6 @@ view.ModelFactoryService = class { } return obj instanceof Map && obj.size > 0; }; - let entries = context.entries; if (!check(entries)) { entries = content.peek('zip'); @@ -5314,7 +5315,9 @@ view.ModelFactoryService = class { for (const callback of callbacks) { let archive = null; try { + /* eslint-disable no-await-in-loop */ archive = callback(stream); + /* eslint-enable no-await-in-loop */ } catch (error) { // continue regardless of error } @@ -5509,28 +5512,37 @@ view.ModelFactoryService = class { let success = false; const next = async () => { if (modules.length > 0) { + let module = null; try { const id = modules.shift(); - const module = await this._host.require(id); + module = await this._host.require(id); if (!module.ModelFactory) { throw new view.Error("Failed to load module '" + id + "'."); } - const modelFactory = new module.ModelFactory(); - const target = modelFactory.match(context); - if (target) { - success = true; - const model = await modelFactory.open(context, target); - if (!model.identifier) { - model.identifier = context.identifier; - } - return model; - } } catch (error) { - if (context.stream && context.stream.position !== 0) { - context.stream.seek(0); - } + success = true; + modules.splice(0, modules.length); errors.push(error); } + if (module) { + try { + const modelFactory = new module.ModelFactory(); + const target = modelFactory.match(context); + if (target) { + success = true; + const model = await modelFactory.open(context, target); + if (!model.identifier) { + model.identifier = context.identifier; + } + return model; + } + } catch (error) { + if (context.stream && context.stream.position !== 0) { + context.stream.seek(0); + } + errors.push(error); + } + } return await next(); } if (success) { @@ -5853,10 +5865,12 @@ view.Error = class extends Error { } }; -if (typeof module !== 'undefined' && typeof module.exports === 'object') { - module.exports.View = view.View; - module.exports.ModelFactoryService = view.ModelFactoryService; - module.exports.Documentation = view.Documentation; - module.exports.Formatter = view.Formatter; - module.exports.Tensor = view.Tensor; +if (typeof window !== 'undefined' && window.exports) { + window.exports.view = view; } + +export const View = view.View; +export const ModelFactoryService = view.ModelFactoryService; +export const Documentation = view.Documentation; +export const Formatter = view.Formatter; +export const Tensor = view.Tensor; diff --git a/source/weka.js b/source/weka.js index 9c3d43172da..61a7479b41e 100644 --- a/source/weka.js +++ b/source/weka.js @@ -1,8 +1,8 @@ // Experimental -var weka = {}; -var java = {}; +const weka = {}; +const java = {}; weka.ModelFactory = class { @@ -146,8 +146,8 @@ java.io.InputObjectStream = class { } if (flags & 0x02) { // SC_SERIALIZABLE debugger; - var customObject = objects[classname]; - var hasReadObjectMethod = customObject && customObject.readObject; + const customObject = objects[classname]; + const hasReadObjectMethod = customObject && customObject.readObject; if (flags & 0x01) { // SC_WRITE_METHOD if (!hasReadObjectMethod) { throw new Error('Class "'+ classname + '" dose not implement readObject()'); @@ -251,6 +251,4 @@ java.io.Error = class extends Error { } }; -if (typeof module !== 'undefined' && typeof module.exports === 'object') { - module.exports.ModelFactory = weka.ModelFactory; -} \ No newline at end of file +export const ModelFactory = weka.ModelFactory; diff --git a/source/xml.js b/source/xml.js index 4fd5c14f3a6..5996c367e51 100644 --- a/source/xml.js +++ b/source/xml.js @@ -1,6 +1,6 @@ -var xml = {}; -var text = require('./text'); +const xml = {}; +import * as text from './text.js'; // https://www.w3.org/TR/xml @@ -1748,6 +1748,5 @@ xml.Error = class extends Error { } }; -if (typeof module !== 'undefined' && typeof module.exports === 'object') { - module.exports.TextReader = xml.TextReader; -} +export const TextReader = xml.TextReader; + diff --git a/source/xmodel-proto.js b/source/xmodel-proto.js index f49ffb1939f..1e1069642f9 100644 --- a/source/xmodel-proto.js +++ b/source/xmodel-proto.js @@ -1,4 +1,7 @@ -var $root = protobuf.get('xmodel'); + +import * as protobuf from './protobuf.js'; + +const $root = protobuf.get('xmodel'); $root.serial_v2 = {}; diff --git a/source/xmodel.js b/source/xmodel.js index 457529173af..05f69fb63b1 100644 --- a/source/xmodel.js +++ b/source/xmodel.js @@ -1,6 +1,7 @@ -var xmodel = {}; -var protobuf = require('./protobuf'); +import * as protobuf from './protobuf.js'; + +const xmodel = {}; xmodel.ModelFactory = class { @@ -382,6 +383,5 @@ xmodel.Error = class extends Error { } }; -if (typeof module !== 'undefined' && typeof module.exports === 'object') { - module.exports.ModelFactory = xmodel.ModelFactory; -} +export const ModelFactory = xmodel.ModelFactory; + diff --git a/source/zip.js b/source/zip.js index 869d54e49cc..d68afb8d598 100644 --- a/source/zip.js +++ b/source/zip.js @@ -1,10 +1,16 @@ -var zip = {}; -var gzip = {}; -var zlib = {}; +const zip = {}; +const gzip = {}; +const zlib = {}; zip.Archive = class { + static async import() { + if (typeof process === 'object' && typeof process.versions == 'object' && typeof process.versions.node !== 'undefined') { + zip.zlib = await import('zlib'); + } + } + static open(data, format) { const stream = data instanceof Uint8Array ? new zip.BinaryReader(data) : data; if (stream && stream.length > 2) { @@ -241,8 +247,8 @@ zip.Inflater = class { inflateRaw(data, length) { let buffer = null; - if (typeof process === 'object' && typeof process.versions == 'object' && typeof process.versions.node !== 'undefined') { - buffer = require('zlib').inflateRawSync(data); + if (zip.zlib) { + buffer = zip.zlib.inflateRawSync(data); } else { const reader = new zip.BitReader(data); const writer = length === undefined ? new zip.BlockWriter() : new zip.BufferWriter(length); @@ -922,6 +928,4 @@ gzip.Error = class extends Error { } }; -if (typeof module !== 'undefined' && typeof module.exports === 'object') { - module.exports.Archive = zip.Archive; -} \ No newline at end of file +export const Archive = zip.Archive; diff --git a/test/models.js b/test/models.js index 51100f60dd2..46197f28307 100755 --- a/test/models.js +++ b/test/models.js @@ -1,12 +1,12 @@ -const fs = require('fs').promises; -const path = require('path'); -const process = require('process'); - -const base = require('../source/base'); -const view = require('../source/view'); -const zip = require('../source/zip'); -const tar = require('../source/tar'); +import * as fs from 'fs/promises'; +import * as path from 'path'; +import * as process from 'process'; +import * as url from 'url'; +import * as base from '../source/base.js'; +import * as view from '../source/view.js'; +import * as zip from '../source/zip.js'; +import * as tar from '../source/tar.js'; const access = async (path) => { try { @@ -52,7 +52,8 @@ host.TestHost = class { constructor() { this._window = global.window; this._document = this._window.document; - this._sourceDir = path.join(__dirname, '..', 'source'); + const dirname = path.dirname(url.fileURLToPath(import.meta.url)); + this._sourceDir = path.join(dirname, '..', 'source'); } get window() { @@ -81,7 +82,7 @@ host.TestHost = class { async require(id) { const file = path.join(this._sourceDir, id + '.js'); - return require(file); + return await import('file://' + file); } async request(file, encoding, basename) { @@ -368,7 +369,8 @@ class Target { const target = item.target.split(','); this.target = item.type ? target : target.map((target) => path.resolve(process.cwd(), target)); this.action = new Set((this.action || '').split(';')); - this.folder = item.type ? path.normalize(path.join(__dirname, '..', 'third_party' , 'test', item.type)) : ''; + const dirname = path.dirname(url.fileURLToPath(import.meta.url)); + this.folder = item.type ? path.normalize(path.join(dirname, '..', 'third_party' , 'test', item.type)) : ''; this.name = this.type ? this.type + '/' + this.target[0] : this.target[0]; this.measures = new Map([ [ 'name', this.name ] ]); } @@ -605,7 +607,7 @@ class Target { } else { tensor.toString(); /* - const python = require('../source/python'); + const python = await import('../source/python.js'); const tensor = argument.initializer; if (tensor.type && tensor.type.dataType !== '?') { let data_type = tensor.type.dataType; @@ -698,9 +700,12 @@ class Target { } const main = async () => { + global.window = new Window(); + await zip.Archive.import(); try { let patterns = process.argv.length > 2 ? process.argv.slice(2) : []; - const configuration = await fs.readFile(__dirname + '/models.json', 'utf-8'); + const dirname = path.dirname(url.fileURLToPath(import.meta.url)); + const configuration = await fs.readFile(dirname + '/models.json', 'utf-8'); let targets = JSON.parse(configuration).reverse(); if (patterns.length > 0) { const exists = await Promise.all(patterns.map((pattern) => access(pattern))); @@ -713,7 +718,7 @@ const main = async () => { } const __host__ = new host.TestHost(); const measures = new Table([ 'name', 'download', 'load', 'validate', 'render' ]); - // await measures.log(path.join(__dirname, '..', 'dist', 'test', 'measures.csv')); + // await measures.log(path.join(dirname, '..', 'dist', 'test', 'measures.csv')); while (targets.length > 0) { const item = targets.pop(); const target = new Target(__host__, item); @@ -735,9 +740,4 @@ const main = async () => { } }; -global.protobuf = require('../source/protobuf'); -global.flatbuffers = require('../source/flatbuffers'); -global.TextDecoder = TextDecoder; -global.window = new Window(); - main(); \ No newline at end of file diff --git a/tools/circle_script.js b/tools/circle_script.js index 28371271abf..4eaeb718672 100644 --- a/tools/circle_script.js +++ b/tools/circle_script.js @@ -1,11 +1,13 @@ -const path = require('path'); -const flatc = require('./flatc'); -const fs = require('fs').promises; +import * as path from 'path'; +import * as fs from 'fs/promises'; +import * as url from 'url'; +import * as flatc from './flatc.js'; const main = async () => { - const schema = path.join(__dirname, '..', 'third_party', 'source', 'circle', 'nnpackage', 'schema', 'circle_schema.fbs'); - const file = path.join(__dirname, '..', 'source', 'circle-metadata.json'); + const dirname = path.dirname(url.fileURLToPath(import.meta.url)); + const schema = path.join(dirname, '..', 'third_party', 'source', 'circle', 'nnpackage', 'schema', 'circle_schema.fbs'); + const file = path.join(dirname, '..', 'source', 'circle-metadata.json'); const input = await fs.readFile(file, 'utf-8'); const json = JSON.parse(input); const operators = new Map(); @@ -69,4 +71,3 @@ const main = async () => { }; main(); - diff --git a/tools/flatc.js b/tools/flatc.js index d523c280829..3106e7e8294 100644 --- a/tools/flatc.js +++ b/tools/flatc.js @@ -1,7 +1,8 @@ +import * as fs from 'fs/promises'; +import * as path from 'path'; + const flatc = {}; -const fs = require('fs').promises; -const path = require('path'); flatc.Object = class { @@ -890,7 +891,10 @@ flatc.Generator = class { this._root = root; this._text = text; this._builder = new flatc.Generator.StringBuilder(); - this._builder.add("var $root = flatbuffers.get('" + this._root.name + "');"); + this._builder.add(""); + this._builder.add("import * as flatbuffers from './flatbuffers.js';"); + this._builder.add(""); + this._builder.add("const $root = flatbuffers.get('" + this._root.name + "');"); for (const namespace of this._root.namespaces.values()) { this._buildNamespace(namespace); } @@ -1203,7 +1207,7 @@ flatc.Generator.StringBuilder = class { constructor() { this._indentation = ''; - this._lines = []; + this._lines = [ '' ]; this._newline = true; } @@ -1287,15 +1291,12 @@ const main = async (args) => { process.exit(0); }; -if (typeof process === 'object' && Array.isArray(process.argv) && - process.argv.length > 1 && process.argv[1] === __filename) { +if (typeof process === 'object' && Array.isArray(process.argv) && process.argv.length > 1) { const args = process.argv.slice(2); main(args); } -if (typeof module !== 'undefined' && typeof module.exports === 'object') { - module.exports.Root = flatc.Root; - module.exports.Namespace = flatc.Namespace; - module.exports.Type = flatc.Type; - module.exports.Enum = flatc.Enum; -} +export const Root = flatc.Root; +export const Namespace = flatc.Namespace; +export const Type = flatc.Type; +export const Enum = flatc.Enum; diff --git a/tools/megengine_script.js b/tools/megengine_script.js index ee217980719..b983b9e2daa 100644 --- a/tools/megengine_script.js +++ b/tools/megengine_script.js @@ -1,10 +1,13 @@ -const path = require('path'); -const flatc = require('./flatc'); -const fs = require('fs').promises; + +import * as path from 'path'; +import * as fs from 'fs/promises'; +import * as url from 'url'; +import * as flatc from './flatc.js'; const main = async () => { - const schema = path.join(__dirname, '..', 'third_party', 'source', 'megengine', 'src', 'serialization', 'fbs', 'schema_v2.fbs'); - const file = path.join(__dirname, '..', 'source', 'megengine-metadata.json'); + const dirname = path.dirname(url.fileURLToPath(import.meta.url)); + const schema = path.join(dirname, '..', 'third_party', 'source', 'megengine', 'src', 'serialization', 'fbs', 'schema_v2.fbs'); + const file = path.join(dirname, '..', 'source', 'megengine-metadata.json'); const input = await fs.readFile(file, 'utf-8'); const json = JSON.parse(input); const category = { diff --git a/tools/mslite_metadata.js b/tools/mslite_metadata.js index e34104b012c..e8d36800725 100644 --- a/tools/mslite_metadata.js +++ b/tools/mslite_metadata.js @@ -1,11 +1,13 @@ -const path = require('path'); -const flatc = require('./flatc'); -const fs = require('fs').promises; +import * as path from 'path'; +import * as url from 'url'; +import * as fs from 'fs/promises'; +import * as flatc from './flatc.js'; const main = async () => { - const schema = path.join(__dirname, '..', 'third_party', 'source', 'mindspore', 'mindspore', 'lite', 'schema', 'ops.fbs'); - const file = path.join(__dirname, '..', 'source', 'mslite-metadata.json'); + const dirname = path.dirname(url.fileURLToPath(import.meta.url)); + const schema = path.join(dirname, '..', 'third_party', 'source', 'mindspore', 'mindspore', 'lite', 'schema', 'ops.fbs'); + const file = path.join(dirname, '..', 'source', 'mslite-metadata.json'); const input = await fs.readFile(file, 'utf-8'); const json = JSON.parse(input); const operators = new Map(); diff --git a/tools/protoc.js b/tools/protoc.js index 3e4a5b2c9e1..a20aa600b1c 100644 --- a/tools/protoc.js +++ b/tools/protoc.js @@ -1,7 +1,8 @@ +import * as fs from 'fs/promises'; +import * as path from 'path'; + const protoc = {}; -const fs = require('fs').promises; -const path = require('path'); protoc.Object = class { @@ -1129,7 +1130,10 @@ protoc.Generator = class { this._root = root; this._text = text; this._builder = new protoc.Generator.StringBuilder(); - this._builder.add("var $root = protobuf.get('" + this._root.alias + "');"); + this._builder.add(""); + this._builder.add("import * as protobuf from './protobuf.js';"); + this._builder.add(""); + this._builder.add("const $root = protobuf.get('" + this._root.alias + "');"); this._buildContent(this._root); this._content = this._builder.toString(); } @@ -1313,7 +1317,7 @@ protoc.Generator = class { for (const field of Array.from(type.fields.values()).filter((field) => field.required)) { this._builder.add("if (!Object.prototype.hasOwnProperty.call(message, '" + field.name + "')) {"); this._builder.indent(); - this._builder.add('throw new protobuf.Error("Excepted \'' + field.name + '\'.");'); + this._builder.add('throw new Error("Excepted \'' + field.name + '\'.");'); this._builder.outdent(); this._builder.add('}'); } @@ -1383,7 +1387,7 @@ protoc.Generator = class { for (const field of Array.from(type.fields.values()).filter((field) => field.required)) { this._builder.add('if (!Object.prototype.hasOwnProperty.call(message, "' + field.name + '")) {'); this._builder.indent(); - this._builder.add('throw new protobuf.Error("Excepted \'' + field.name + '\'.");'); + this._builder.add('throw new Error("Excepted \'' + field.name + '\'.");'); this._builder.outdent(); this._builder.add('}'); } @@ -1415,7 +1419,7 @@ protoc.Generator.StringBuilder = class { constructor() { this._indentation = ''; - this._lines = []; + this._lines = [ '' ]; this._newline = true; } @@ -1501,12 +1505,9 @@ const main = async (args) => { process.exit(0); }; -if (typeof process === 'object' && Array.isArray(process.argv) && - process.argv.length > 1 && process.argv[1] === __filename) { +if (typeof process === 'object' && Array.isArray(process.argv) && process.argv.length > 1) { const args = process.argv.slice(2); main(args); } -if (typeof module !== 'undefined' && typeof module.exports === 'object') { - module.exports.Root = protoc.Root; -} +export const Root = protoc.Root; diff --git a/tools/tflite_metadata.js b/tools/tflite_metadata.js index 23a2e48a4df..f403eca9bdf 100644 --- a/tools/tflite_metadata.js +++ b/tools/tflite_metadata.js @@ -1,11 +1,13 @@ -const path = require('path'); -const flatc = require('./flatc'); -const fs = require('fs').promises; +import * as path from 'path'; +import * as fs from 'fs/promises'; +import * as url from 'url'; +import * as flatc from './flatc.js'; const main = async () => { - const schema = path.join(__dirname, '..', 'third_party', 'source', 'tensorflow', 'tensorflow', 'lite', 'schema', 'schema.fbs'); - const file = path.join(__dirname, '..', 'source', 'tflite-metadata.json'); + const dirname = path.dirname(url.fileURLToPath(import.meta.url)); + const schema = path.join(dirname, '..', 'third_party', 'source', 'tensorflow', 'tensorflow', 'lite', 'schema', 'schema.fbs'); + const file = path.join(dirname, '..', 'source', 'tflite-metadata.json'); const input = await fs.readFile(file, 'utf-8'); const json = JSON.parse(input); const operators = new Map();