Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
3 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
 
'use strict';
 
const path = require('path');
const fs = require('fs-extra');
const { spawn } = require('child_process');
const startServer = require('./simple-http-server');
const minimist = require('minimist');
 
const { NODEJS_TEST_CASES, BROWSER_TEST_CASES, BUNDLER_TEST_CASES } = require('./run-data');
 
// commandline arguments
const parsedArgs = minimist(process.argv.slice(2));
const BROWSER = parsedArgs.browser || 'Chrome_headless';
// Preserve the existing test folder
// When this flag is set, the test folder will not be cleared before running the tests.
// NPM install will not be run again.
// This is useful for debugging test failures as it makes it much faster to re-run.
const PRESERVE = parsedArgs.preserve || parsedArgs.p;
 
// copy whole folder to out-side of <ORT_ROOT>/js/ because we need to test in a folder that no `package.json` file
// exists in its parent folder.
// here we use <ORT_ROOT>/build/js/e2e/ for the test
 
const TEST_E2E_SRC_FOLDER = __dirname;
const JS_ROOT_FOLDER = path.resolve(__dirname, '../../..');
const TEST_E2E_RUN_FOLDER = path.resolve(JS_ROOT_FOLDER, '../build/js/e2e');
const NPM_CACHE_FOLDER = path.resolve(TEST_E2E_RUN_FOLDER, '../npm_cache');
const CHROME_USER_DATA_FOLDER = path.resolve(TEST_E2E_RUN_FOLDER, '../user_data');
if (!PRESERVE) {
  fs.emptyDirSync(TEST_E2E_RUN_FOLDER);
  fs.emptyDirSync(NPM_CACHE_FOLDER);
  fs.emptyDirSync(CHROME_USER_DATA_FOLDER);
}
fs.copySync(TEST_E2E_SRC_FOLDER, TEST_E2E_RUN_FOLDER);
 
// always use a new folder as user-data-dir
let nextUserDataDirId = 0;
function getNextUserDataDir() {
  const dir = path.resolve(CHROME_USER_DATA_FOLDER, nextUserDataDirId.toString());
  nextUserDataDirId++;
  fs.emptyDirSync(dir);
  return dir;
}
 
async function main() {
  // find packed package
  const { globbySync } = await import('globby');
 
  const ORT_COMMON_FOLDER = path.resolve(JS_ROOT_FOLDER, 'common');
  const ORT_COMMON_PACKED_FILEPATH_CANDIDATES = globbySync('onnxruntime-common-*.tgz', { cwd: ORT_COMMON_FOLDER });
 
  const PACKAGES_TO_INSTALL = [];
 
  if (ORT_COMMON_PACKED_FILEPATH_CANDIDATES.length === 1) {
    PACKAGES_TO_INSTALL.push(path.resolve(ORT_COMMON_FOLDER, ORT_COMMON_PACKED_FILEPATH_CANDIDATES[0]));
  } else if (ORT_COMMON_PACKED_FILEPATH_CANDIDATES.length > 1) {
    throw new Error('multiple packages found for onnxruntime-common.');
  }
 
  const ORT_WEB_FOLDER = path.resolve(JS_ROOT_FOLDER, 'web');
  const ORT_WEB_PACKED_FILEPATH_CANDIDATES = globbySync('onnxruntime-web-*.tgz', { cwd: ORT_WEB_FOLDER });
  if (ORT_WEB_PACKED_FILEPATH_CANDIDATES.length !== 1) {
    throw new Error('cannot find exactly single package for onnxruntime-web.');
  }
  PACKAGES_TO_INSTALL.push(path.resolve(ORT_WEB_FOLDER, ORT_WEB_PACKED_FILEPATH_CANDIDATES[0]));
 
  // we start here:
 
  if (!PRESERVE) {
    // install dev dependencies
    await runInShell(`npm ci`);
 
    // npm install with "--cache" to install packed packages with an empty cache folder
    await runInShell(`npm install --cache "${NPM_CACHE_FOLDER}" ${PACKAGES_TO_INSTALL.map((i) => `"${i}"`).join(' ')}`);
  }
 
  // perform exports testing
  {
    const testExportsMain = require(path.join(TEST_E2E_RUN_FOLDER, './exports/main'));
    await testExportsMain(PRESERVE, PACKAGES_TO_INSTALL);
  }
 
  // perform type (typescript related) testing
  {
    const testTypeMain = require(path.join(TEST_E2E_RUN_FOLDER, './type/main'));
    await testTypeMain(PRESERVE, PACKAGES_TO_INSTALL);
  }
 
  // prepare .wasm files for path override testing
  prepareWasmPathOverrideFiles();
 
  // Setup the wwwroot folder for hosting .wasm files (for cross-origin testing)
  const serverWwwRoot = path.resolve(TEST_E2E_RUN_FOLDER, 'wwwroot');
  fs.emptyDirSync(serverWwwRoot);
 
  // prepare ESM loaders
  prepareEsmLoaderFiles();
 
  await fs.symlink(
    path.resolve(TEST_E2E_RUN_FOLDER, 'node_modules', 'onnxruntime-web', 'dist'),
    path.join(serverWwwRoot, 'dist'),
    'junction',
  );
  await fs.symlink(
    path.resolve(TEST_E2E_RUN_FOLDER, 'test-wasm-path-override'),
    path.join(serverWwwRoot, 'test-wasm-path-override'),
    'junction',
  );
  await fs.symlink(path.resolve(TEST_E2E_RUN_FOLDER, 'test-data'), path.join(serverWwwRoot, 'test-data'), 'junction');
 
  // start a HTTP server for hosting .wasm files (for cross-origin testing)
  const server = startServer(serverWwwRoot, 8081);
 
  // await delay(1000 * 3600);  // wait for 1 hour
 
  try {
    // test case run in Node.js
    await testAllNodejsCases();
 
    // test cases with self-host (ort hosted in same origin)
    await testAllBrowserCases({ hostInKarma: true });
 
    // test cases without self-host (ort hosted in different origin)
    await testAllBrowserCases({ hostInKarma: false });
 
    // run bundlers
    await runInShell(`npm run build`);
 
    // test package consuming test
    await testAllBrowserPackagesConsumingCases();
  } finally {
    // close the server after all tests
    await server.close();
  }
}
 
function prepareEsmLoaderFiles() {
  const allEsmFiles = [...new Set(BROWSER_TEST_CASES.map((i) => i[3]).filter((i) => i && i.endsWith('.mjs')))];
 
  // self-hosted
  fs.emptyDirSync(path.join(TEST_E2E_RUN_FOLDER, 'esm-loaders'));
  fs.emptyDirSync(path.join(TEST_E2E_RUN_FOLDER, 'wwwroot', 'esm-loaders'));
  allEsmFiles.forEach((i) => {
    fs.writeFileSync(
      path.join(TEST_E2E_RUN_FOLDER, 'esm-loaders', i),
      `import * as x from '../node_modules/onnxruntime-web/dist/${i}'; globalThis.ort = x;`,
    );
    fs.writeFileSync(
      path.join(TEST_E2E_RUN_FOLDER, 'wwwroot', 'esm-loaders', i),
      `import * as x from '../dist/${i}'; globalThis.ort = x;`,
    );
  });
}
 
function prepareWasmPathOverrideFiles() {
  const folder = path.join(TEST_E2E_RUN_FOLDER, 'test-wasm-path-override');
  const sourceFile = path.join(
    TEST_E2E_RUN_FOLDER,
    'node_modules',
    'onnxruntime-web',
    'dist',
    'ort-wasm-simd-threaded',
  );
  fs.emptyDirSync(folder);
  fs.copyFileSync(`${sourceFile}.mjs`, path.join(folder, 'ort-wasm-simd-threaded.mjs'));
  fs.copyFileSync(`${sourceFile}.wasm`, path.join(folder, 'ort-wasm-simd-threaded.wasm'));
  fs.copyFileSync(`${sourceFile}.mjs`, path.join(folder, 'renamed.mjs'));
  fs.copyFileSync(`${sourceFile}.wasm`, path.join(folder, 'renamed.wasm'));
  // TODO: add .asyncify/.jspi
  fs.copyFileSync(`${sourceFile}.jsep.mjs`, path.join(folder, 'ort-wasm-simd-threaded.jsep.mjs'));
  fs.copyFileSync(`${sourceFile}.jsep.wasm`, path.join(folder, 'ort-wasm-simd-threaded.jsep.wasm'));
  fs.copyFileSync(`${sourceFile}.jsep.mjs`, path.join(folder, 'jsep-renamed.mjs'));
  fs.copyFileSync(`${sourceFile}.jsep.wasm`, path.join(folder, 'jsep-renamed.wasm'));
}
 
async function testAllNodejsCases() {
  for (const caseName of NODEJS_TEST_CASES) {
    await runInShell(`node ./node_modules/mocha/bin/mocha --timeout 10000 ${caseName}`);
  }
}
 
async function testAllBrowserCases({ hostInKarma }) {
  for (const [testForSameOrigin, testForCrossOrigin, main, ortMain, args] of BROWSER_TEST_CASES) {
    if (hostInKarma && testForSameOrigin) {
      await runKarma({ hostInKarma, main, ortMain, args });
      await runKarma({ hostInKarma, main, ortMain, args, enableSharedArrayBuffer: true });
    }
    if (!hostInKarma && testForCrossOrigin) {
      await runKarma({ hostInKarma, main, ortMain, args });
      await runKarma({ hostInKarma, main, ortMain, args, enableSharedArrayBuffer: true });
    }
  }
}
 
async function testAllBrowserPackagesConsumingCases() {
  for (const [main, format] of BUNDLER_TEST_CASES) {
    await runKarma({ hostInKarma: true, main, ortMain: '', format });
    await runKarma({ hostInKarma: true, main, ortMain: '', format, enableSharedArrayBuffer: true });
  }
}
 
async function runKarma({
  hostInKarma,
  main,
  browser = BROWSER,
  ortMain = 'ort.min.js',
  format = 'iife',
  enableSharedArrayBuffer = false,
  args = [],
}) {
  const selfHostFlag = hostInKarma ? '--self-host' : '';
  const argsStr = args.map((i) => `--test-args=${i}`).join(' ');
  const formatFlag = `--format=${format}`;
  const enableSharedArrayBufferFlag = enableSharedArrayBuffer ? '--enable-shared-array-buffer' : '';
  await runInShell(
    `npx karma start --single-run --browsers ${browser} ${selfHostFlag} --ort-main=${ortMain} --test-main=${
      main
    } --user-data=${getNextUserDataDir()} ${argsStr} ${formatFlag} ${enableSharedArrayBufferFlag}`,
  );
}
 
async function runInShell(cmd) {
  console.log('===============================================================');
  console.log(' Running command in shell:');
  console.log(' > ' + cmd);
  console.log('===============================================================');
  let complete = false;
  const childProcess = spawn(cmd, { shell: true, stdio: 'inherit', cwd: TEST_E2E_RUN_FOLDER });
  childProcess.on('close', function (code) {
    if (code !== 0) {
      process.exit(code);
    } else {
      complete = true;
    }
  });
  while (!complete) {
    await delay(100);
  }
}
 
async function delay(ms) {
  return new Promise(function (resolve) {
    setTimeout(function () {
      resolve();
    }, ms);
  });
}
 
main();