Исходный код СУБД 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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
 
//
// This script is used to download WebAssembly build artifacts from CI pipeline.
//
// The goal of this script is to save time for ORT Web developers. For most TypeScript tasks, there is no change in the
// WebAssembly side, so there is no need to rebuild WebAssembly.
//
// It performs the following operations:
// 1. use GitHub Actions REST API as much as possible to get metadata about the status of build and workflow runs.
//    - query the main branch if no "run" parameter is specified
//    - if "run" is specified, it can be a run ID, PR number, branch name, or commit SHA. Try each possibility.
//
// 2. When the artifact is found, use GitHub CLI (gh) to download the artifacts directly to the dist folder.
//
 
import fs from 'fs';
import { bootstrap as globalAgentBootstrap } from 'global-agent';
import https from 'https';
import path from 'path';
import minimist from 'minimist';
import { execSync } from 'child_process';
 
const HELP_MESSAGE = `
pull-prebuilt-wasm-artifacts
 
Usage:
  npm run pull:wasm [run] [options]
 
  node ./pull-prebuilt-wasm-artifacts [run] [options]
 
  Run can be specified in one of the following ways:
    action_run_id
    PR number
    branch name (default: "main")
 
Options:
  -d  --debug       specify the debug build type of the artifacts to download.
  -l  --latest      if set, will always use the latest build, even if it is not completed yet.
  -h  --help        print this message and exit
`;
 
const args = minimist(process.argv.slice(2), {
  alias: {
    debug: ['d'],
    help: ['h'],
    latest: ['l'],
  },
});
 
if (args.help || args.h) {
  console.log(HELP_MESSAGE);
  process.exit();
}
 
// Check if GitHub CLI (gh) is installed and available in PATH
try {
  execSync('gh --version', { stdio: 'pipe' }).toString().trim();
} catch {
  console.error('Error: GitHub CLI (gh) is not installed or not in PATH.');
  console.error('Please install it from https://cli.github.com/ and try again.');
  process.exit(1);
}
 
// in NPM script, the args are parsed as:
// npm [npm_command] [npm_flags] -- [user_flags]
//
// The npm_flags will be parsed and removed by NPM, so they will not be available in process.argv directly. Instead,
// they are available in process.env.npm_config_* variables.
//
// For example, if the user runs the command like this:
// > npm run pull:wasm -- --debug
// In this case, `--debug` will be available in `args.debug`
//
// If the user runs the command like this:
// > npm run pull:wasm --debug
// In this case, `--debug` will be available in `process.env.npm_config_debug`, but not in `args.debug` directly.
//
// The following code checks both the command line arguments and the npm_config_* environment variables to get the correct values.
const debug = args.debug || process.env.npm_config_d || process.env.npm_config_debug;
const latest = args.latest || process.env.npm_config_l || process.env.npm_config_latest;
 
const folderName = debug ? 'Debug_wasm' : 'Release_wasm';
const allowImcomplete = latest;
 
const run = args._[0]; // The first non-option argument
 
const GITHUB_ACTION_REQUEST_OPTIONS = {
  headers: {
    'user-agent': 'onnxruntime-web artifact pull',
    accept: 'application/vnd.github+json',
    'X-GitHub-Api-Version': '2022-11-28',
  },
};
 
async function downloadJson(url: string): Promise<any> {
  return new Promise((resolve, reject) => {
    https
      .get(url, GITHUB_ACTION_REQUEST_OPTIONS, (res) => {
        const { statusCode } = res;
        const contentType = res.headers['content-type'];
 
        if (!statusCode) {
          reject(new Error('No response statud code from server.'));
          return;
        }
        if (statusCode >= 400 && statusCode < 500) {
          resolve(null);
          return;
        } else if (statusCode !== 200) {
          reject(new Error(`Failed to download build list. HTTP status code = ${statusCode}`));
          return;
        }
        if (!contentType || !/^application\/json/.test(contentType)) {
          reject(new Error(`unexpected content type: ${contentType}`));
          return;
        }
        res.setEncoding('utf8');
        let rawData = '';
        res.on('data', (chunk) => {
          rawData += chunk;
        });
        res.on('end', () => {
          try {
            resolve(JSON.parse(rawData));
          } catch (e) {
            reject(e);
          }
        });
        res.on('error', (err) => {
          reject(err);
        });
      })
      .on('error', (err) => {
        reject(err);
      });
  });
}
 
async function downloadArtifactsForRun(run: any): Promise<void> {
  const data = await downloadJson(run.artifacts_url);
 
  for (const v of data.artifacts) {
    if (v.name === folderName && !v.expired) {
      console.log(`=== Ready to download artifacts "${folderName}" from run: ${run.id} ===`);
 
      const WASM_FOLDER = path.join(__dirname, '../dist');
      if (!fs.existsSync(WASM_FOLDER)) {
        fs.mkdirSync(WASM_FOLDER);
      } else {
        fs.readdirSync(WASM_FOLDER).forEach((file) => {
          if (
            [
              'ort-wasm-simd-threaded.asyncify.mjs',
              'ort-wasm-simd-threaded.asyncify.wasm',
              'ort-wasm-simd-threaded.jsep.mjs',
              'ort-wasm-simd-threaded.jsep.wasm',
              'ort-wasm-simd-threaded.jspi.mjs',
              'ort-wasm-simd-threaded.jspi.wasm',
              'ort-wasm-simd-threaded.mjs',
              'ort-wasm-simd-threaded.wasm',
            ].includes(file)
          ) {
            const filePath = path.join(WASM_FOLDER, file);
            console.log(`Deleting old file: ${filePath}`);
            fs.unlinkSync(filePath);
          }
        });
      }
 
      execSync(`gh run download ${run.id} -n ${folderName} -D "${WASM_FOLDER}" -R Microsoft/onnxruntime`);
 
      return;
    }
  }
 
  throw new Error(`No artifact "${folderName}" found for the specified build.`);
}
 
async function main() {
  // Bootstrap global-agent to honor the proxy settings in
  // environment variables, e.g. GLOBAL_AGENT_HTTPS_PROXY.
  // See the https://github.com/gajus/global-agent ReadMe.md regarding environment variables.
  globalAgentBootstrap();
 
  console.log(
    `=== Start to pull WebAssembly artifacts "${folderName}" from CI for ${run ? `Run: "${run}"` : 'main branch'} ===`,
  );
 
  let sha: string | undefined;
 
  // If param `run` is specified, we try to figure out what it is.
  if (!run) {
    // API reference: https://docs.github.com/en/rest/actions/workflow-runs?apiVersion=2022-11-28#list-workflow-runs-for-a-workflow
    const mainRunData = await downloadJson(
      `https://api.github.com/repos/Microsoft/onnxruntime/actions/workflows/152051496/runs?branch=main${allowImcomplete ? '' : '&status=success'}&per_page=1&exclude_pull_requests=1`,
    );
    if (mainRunData.workflow_runs.length === 0) {
      throw new Error('No build found');
    }
    const run = mainRunData.workflow_runs[0];
    await downloadArtifactsForRun(run);
  } else {
    // If `run` is a number, it is a run ID or PR number
    const runId = parseInt(run, 10);
    // check if runId only contains digits
    const isRunIdDigitsOnly = /^\d+$/.test(run);
    if (isRunIdDigitsOnly && !isNaN(runId)) {
      // Try to treat it as a run ID
      console.log('  # Trying to treat it as a run ID: ' + runId);
      const runData = await downloadJson(`https://api.github.com/repos/Microsoft/onnxruntime/actions/runs/${runId}`);
      if (runData) {
        console.log(`=== Found run: ${runId} ===`);
        await downloadArtifactsForRun(runData);
        return;
      }
 
      // If not found, try to treat it as a PR number
      console.log(`  # Run ID ${runId} not found or not accessible.`);
 
      // API reference: https://docs.github.com/en/rest/pulls/pulls?apiVersion=2022-11-28#get-a-pull-request
      const prData = await downloadJson(`https://api.github.com/repos/Microsoft/onnxruntime/pulls/${runId}`);
      sha = prData?.head?.sha;
    }
 
    if (sha) {
      console.log(`  # Found PR #${run} with SHA: ${sha}`);
    } else {
      // Try to treat the run parameter as a branch name or commit SHA
      console.log(`  # Trying to treat "${run}" as a branch name`);
 
      // First, try as a branch name
      const branchData = await downloadJson(
        `https://api.github.com/repos/Microsoft/onnxruntime/branches/${encodeURIComponent(run)}`,
      );
      if (branchData) {
        sha = branchData.commit.sha;
        console.log(`  # Found branch "${run}" with SHA: ${sha}`);
      } else {
        const isPossibleSha = /^[0-9a-f]{7,40}$/.test(`${run}`.trim());
        if (isPossibleSha) {
          // If not a branch, try as a commit SHA (works with both full and short SHA)
          console.log(`  # Trying to treat "${run}" as a commit SHA`);
          const commitData = await downloadJson(
            `https://api.github.com/repos/Microsoft/onnxruntime/commits/${encodeURIComponent(run)}`,
          );
          if (commitData) {
            sha = commitData.sha;
            console.log(`  # Found commit with full SHA: ${sha}`);
          }
        }
      }
 
      if (!sha) {
        throw new Error(`Could not identify "${run}" as a run ID, PR number, branch name, or commit SHA`);
      }
    }
 
    // Now that we have the SHA, query for workflow runs associated with this SHA
    const workflowRunsData = await downloadJson(
      `https://api.github.com/repos/Microsoft/onnxruntime/actions/workflows/152051496/runs?head_sha=${sha}`,
    );
 
    if (!workflowRunsData || workflowRunsData.workflow_runs.length === 0) {
      throw new Error(`No Web CI workflow runs found for SHA: ${sha}`);
    }
 
    // Get the latest run
    const latestRun = workflowRunsData.workflow_runs[0];
    console.log(`=== Found run for SHA ${sha}: ${latestRun.html_url} ===`);
 
    // Download artifacts from this run
    await downloadArtifactsForRun(latestRun);
  }
}
 
void main();