Skip to content
This repository was archived by the owner on Jan 15, 2025. It is now read-only.

Commit 161c029

Browse files
Chris McConnellcosmicshuai
andauthored
Switch to axios to support proxy server when merging. (#1190)
* Switch to axios to support proxy server when merging. Normalize sorting to reduce variation. * modify https proxy setting * refactor * fix lint * fix lint Co-authored-by: Chris McConnell <chrimc> Co-authored-by: cosmicshuai <[email protected]>
1 parent 1a5070e commit 161c029

File tree

15 files changed

+567
-365
lines changed

15 files changed

+567
-365
lines changed

.vscode/launch.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,26 @@
157157
"./packages/lu/lib/**"
158158
]
159159
},
160+
{
161+
"type": "node",
162+
"request": "launch",
163+
"name": "Dialog merge root-package schema",
164+
"preLaunchTask": "${defaultBuildTask}",
165+
"program": "${workspaceFolder}/packages/dialog/bin/run",
166+
"outputCapture": "std",
167+
"outFiles": [
168+
"./packages/dialog/lib/**"
169+
],
170+
"args": [
171+
"dialog:merge",
172+
"npm/node_modules/root-package/package.json",
173+
"-o",
174+
"oracles/root-package",
175+
"--verbose"
176+
],
177+
"internalConsoleOptions": "openOnSessionStart",
178+
"cwd": "${workspaceFolder}/packages/dialog/test/commands/dialog/"
179+
},
160180
{
161181
"type": "node",
162182
"request": "launch",

common/config/rush/pnpm-lock.yaml

Lines changed: 22 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/dialog/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,11 @@
4545
"@types/lru-cache": "^5.1.0",
4646
"@types/xml2js": "^0.4.4",
4747
"ajv": "^6.12.2",
48+
"axios": "~0.21.1",
49+
"https-proxy-agent": "^5.0.0",
4850
"chalk": "^2.4.2",
4951
"clone": "^2.1.2",
5052
"fs-extra": "^8.1.0",
51-
"get-uri": "~3.0.2",
5253
"globby": "^11.0.0",
5354
"@apidevtools/json-schema-ref-parser": "^9.0.1",
5455
"os": "~0.1.1",
@@ -80,4 +81,4 @@
8081
"tslint": "^5.18.0",
8182
"typescript": "^4.0.3"
8283
}
83-
}
84+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*!
2+
* Copyright (c) Microsoft Corporation. All rights reserved.
3+
* Licensed under the MIT License.
4+
*/
5+
6+
import * as fs from 'fs-extra'
7+
const axios = require('axios')
8+
const url = require('url')
9+
const httpsProxyAgent = require('https-proxy-agent')
10+
11+
const httpsProxy = function (config) {
12+
const parsed = url.parse(config.url)
13+
const protocol = parsed.protocol
14+
if (protocol !== 'https:') {
15+
return config
16+
}
17+
18+
/* tslint:disable:no-string-literal */
19+
const envProxy = process.env['HTTPS_PROXY'] || process.env['https_proxy']
20+
/* tslint:enable:no-string-literal */
21+
if (envProxy) {
22+
const parsed = url.parse(envProxy)
23+
const proxyOpt = {
24+
hostname: parsed.hostname,
25+
port: parsed.port
26+
}
27+
28+
if (parsed.auth) {
29+
(proxyOpt as any).auth = parsed.auth
30+
}
31+
32+
config.httpsAgent = httpsProxyAgent(proxyOpt)
33+
//Disable direct proxy
34+
config.proxy = false
35+
}
36+
37+
return config
38+
}
39+
axios.interceptors.request.use(httpsProxy)
40+
41+
const filePrefix = 'file:///'
42+
43+
// Get JSON from a URL.
44+
export default async function getJSON(url: string): Promise<any> {
45+
if (url.startsWith(filePrefix)) {
46+
return fs.readJSON(url.substring(filePrefix.length))
47+
}
48+
49+
const resp = await axios.get(url);
50+
return resp.data
51+
}

packages/dialog/src/library/schemaMerger.ts

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ import * as xp from 'xml2js'
1212
import Ajv = require('ajv')
1313
import parser from '@apidevtools/json-schema-ref-parser'
1414
import {JsonPointer as ptr} from 'json-ptr'
15+
import getJSON from './getJSON'
1516

1617
const allof = require('json-schema-merge-allof')
1718
const clone = require('clone')
18-
const getUri = require('get-uri')
1919
const glob = require('globby')
2020
const semverRsort = require('semver/functions/rsort')
2121
const util = require('util')
@@ -40,16 +40,6 @@ function pathName(path: string | undefined, extension: string): string {
4040
return path ? `${path}/${extension}` : extension
4141
}
4242

43-
// Get JSON from a URI.
44-
async function getJSON(uri: string): Promise<any> {
45-
const stream = await getUri(uri)
46-
let data = ''
47-
for await (const chunk of stream) {
48-
data += chunk.toString()
49-
}
50-
return JSON.parse(data)
51-
}
52-
5343
// Convert to the right kind of slash.
5444
// ppath.normalize did not do this properly on the mac.
5545
function normalize(path: string): string {
@@ -67,6 +57,11 @@ function forwardSlashes(input: string): string {
6757
return input.replace(/\\/g, '/')
6858
}
6959

60+
// Consistent sort comparison
61+
function sortFunction(a: any, b: any): number {
62+
return a.localeCompare(b, 'en')
63+
}
64+
7065
// Deep merge of JSON objects
7166
function mergeObjects(obj1: any, obj2: any): any {
7267
const target = {}
@@ -83,7 +78,7 @@ function mergeObjects(obj1: any, obj2: any): any {
8378
merger(obj1)
8479
merger(obj2)
8580
const finalTarget = {}
86-
for (const key of Object.keys(target).sort()) {
81+
for (const key of Object.keys(target).sort(sortFunction)) {
8782
finalTarget[key] = target[key]
8883
}
8984
return finalTarget
@@ -649,7 +644,7 @@ export class SchemaMerger {
649644
this.validateAndExpandPolicies()
650645
const oneOf = Object.keys(this.definitions)
651646
.filter(kind => !this.isInterface(kind) && this.definitions[kind].$role)
652-
.sort()
647+
.sort(sortFunction)
653648
.map(kind => {
654649
return {$ref: `#/definitions/${kind}`}
655650
})
@@ -661,7 +656,7 @@ export class SchemaMerger {
661656
this.currentFile = this.output + '.schema'
662657
this.currentKind = ''
663658
const finalDefinitions: any = {}
664-
for (const key of Object.keys(this.definitions).sort()) {
659+
for (const key of Object.keys(this.definitions).sort(sortFunction)) {
665660
finalDefinitions[key] = this.definitions[key]
666661
}
667662
let finalSchema: any = {
@@ -778,7 +773,7 @@ export class SchemaMerger {
778773
if (!this.failed) {
779774
for (const locale of Object.keys(result)) {
780775
const uischema = {$schema: this.metaUISchemaId}
781-
for (const key of Object.keys(result[locale]).sort()) {
776+
for (const key of Object.keys(result[locale]).sort(sortFunction)) {
782777
uischema[key] = result[locale][key]
783778
}
784779
this.currentFile = ppath.join(ppath.dirname(this.output), outputName + (locale ? '.' + locale : '') + '.uischema')
@@ -1788,7 +1783,7 @@ export class SchemaMerger {
17881783
for (this.currentKind in this.definitions) {
17891784
const definition = this.definitions[this.currentKind]
17901785
if (this.isInterface(this.currentKind) && definition.oneOf) {
1791-
definition.oneOf = definition.oneOf.sort((a: any, b: any) => (a.$ref || a.type).localeCompare(b.$ref || b.type))
1786+
definition.oneOf = definition.oneOf.sort((a: any, b: any) => sortFunction(a.$ref ?? a.type, b.$ref ?? b.type))
17921787
}
17931788
}
17941789
}

packages/dialog/src/library/schemaTracker.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,14 @@
55

66
import Ajv = require('ajv')
77
import parser from '@apidevtools/json-schema-ref-parser'
8-
9-
let getUri: any = require('get-uri')
8+
import http from './getJSON'
109

1110
// Get JSON from a URI.
1211
async function getJSON(uri: string): Promise<any> {
1312
if (uri.indexOf(':') < 2) {
1413
uri = `file:///${uri}`
1514
}
16-
let stream = await getUri(uri)
17-
let data = ''
18-
for await (let chunk of stream) {
19-
data += chunk.toString()
20-
}
21-
return JSON.parse(data)
15+
return http(uri)
2216
}
2317

2418
export class SchemaTracker {

packages/dialog/test/commands/dialog/merge.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ describe('dialog:merge', async () => {
148148
console.log('Start missing component schema')
149149
const [merged, lines] = await merge(['schemas/badSchemas/missingComponent.schema'], 'app.schema')
150150
assert(!merged, 'Merge should have failed')
151-
assert(countMatches(/file does not exist/i, lines) === 1, 'No missing component schema')
151+
assert(countMatches(/status code 404/i, lines) === 1, 'No missing component schema')
152152
})
153153

154154
it('mismatched component schema', async () => {
@@ -495,7 +495,7 @@ describe('dialog:merge', async () => {
495495
.persist()
496496
const [merged, lines] = await merge(['schemas/*.schema'], 'app.schema')
497497
assert(!merged, 'Merging should fail')
498-
assert(countMatches(/internal server error/i, lines) === 1, 'Did not detect server error')
498+
assert(countMatches(/status code 500/i, lines) === 1, 'Did not detect server error')
499499
scope.done()
500500
nock.cleanAll()
501501
})

0 commit comments

Comments
 (0)