@volley/vwr-loader 1.0.5 → 1.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (55) hide show
  1. package/README.md +21 -0
  2. package/dist/amplitudeFlagFetcher.d.ts +4 -2
  3. package/dist/amplitudeFlagFetcher.d.ts.map +1 -1
  4. package/dist/amplitudeFlagFetcher.js +26 -19
  5. package/dist/amplitudeFlagFetcher.js.map +1 -1
  6. package/dist/cli.js +234 -0
  7. package/dist/cli.js.map +1 -0
  8. package/dist/envDefaults.d.ts +3 -0
  9. package/dist/envDefaults.d.ts.map +1 -1
  10. package/dist/envDefaults.js +26 -4
  11. package/dist/envDefaults.js.map +1 -1
  12. package/dist/getDeviceId.d.ts +2 -10
  13. package/dist/getDeviceId.d.ts.map +1 -1
  14. package/dist/getDeviceId.js +31 -18
  15. package/dist/getDeviceId.js.map +1 -1
  16. package/dist/getEnvironment.d.ts +1 -1
  17. package/dist/getEnvironment.d.ts.map +1 -1
  18. package/dist/getEnvironment.js +10 -7
  19. package/dist/getEnvironment.js.map +1 -1
  20. package/dist/getShellVersion.d.ts +2 -2
  21. package/dist/getShellVersion.d.ts.map +1 -1
  22. package/dist/getShellVersion.js +5 -3
  23. package/dist/getShellVersion.js.map +1 -1
  24. package/dist/index.html +1 -1
  25. package/dist/loadVwr.d.ts.map +1 -1
  26. package/dist/loadVwr.js +8 -6
  27. package/dist/loadVwr.js.map +1 -1
  28. package/dist/main.js +1 -1
  29. package/dist/main.js.map +1 -1
  30. package/dist/vwrConfig.d.ts +2 -0
  31. package/dist/vwrConfig.d.ts.map +1 -1
  32. package/dist/vwrConfig.js +148 -119
  33. package/dist/vwrConfig.js.map +1 -1
  34. package/package.json +16 -5
  35. package/src/amplitudeFlagFetcher.test.ts +36 -59
  36. package/src/amplitudeFlagFetcher.ts +31 -22
  37. package/src/envDefaults.ts +33 -4
  38. package/src/getDeviceId.test.ts +79 -24
  39. package/src/getDeviceId.ts +47 -22
  40. package/src/getEnvironment.test.ts +457 -0
  41. package/src/getEnvironment.ts +12 -9
  42. package/src/getShellVersion.test.ts +1 -1
  43. package/src/getShellVersion.ts +7 -3
  44. package/src/loadVwr.ts +13 -6
  45. package/src/main.ts +3 -1
  46. package/src/vite-env.d.ts +3 -0
  47. package/src/vwrConfig.test.ts +2 -2
  48. package/src/vwrConfig.ts +202 -199
  49. package/eslint.config.mjs +0 -23
  50. package/scripts/build.js +0 -2
  51. package/scripts/build.ts +0 -207
  52. package/tsconfig.eslint.json +0 -16
  53. package/tsconfig.json +0 -17
  54. package/vite.config.ts +0 -24
  55. package/vitest.config.ts +0 -8
package/src/vwrConfig.ts CHANGED
@@ -5,6 +5,8 @@ export type VWRConfig = {
5
5
  hubUrl: string
6
6
  vwrUrl: string
7
7
  launchUrl: string | undefined
8
+ platformApiUrl: string
9
+ platformAuthApiUrl: string
8
10
  trustedDomains: Array<string>
9
11
  }
10
12
 
@@ -38,227 +40,223 @@ const buildUrl = (path: string, base: string): URL => {
38
40
  return new URL(path, normalizedBase)
39
41
  }
40
42
 
41
- export const getVWRConfig = async (
42
- request: VWRConfigRequest,
43
- logger: Logger = defaultLogger
44
- ): Promise<VWRConfig> => {
45
- const timeout = request.timeout ?? DEFAULT_CONFIG_TIMEOUT
46
-
47
- logger.info(
48
- "[VWR Config] Fetching config with priority: local → device → shellVersion → environment → defaults"
49
- )
50
-
51
- // Fetch all configs in parallel for performance
52
- const [localConfig, deviceConfig, shellVersionConfig, environmentConfig] =
53
- await Promise.all([
54
- tryGetLocalConfig(request.configFile, timeout, logger),
55
- tryGetDeviceConfig(
56
- request.configUrl,
57
- request.configFile,
58
- request.platform,
59
- request.deviceId,
60
- timeout,
61
- logger
62
- ),
63
- tryGetShellVersionConfig(
64
- request.configUrl,
65
- request.configFile,
66
- request.environment,
67
- request.platform,
68
- request.shellVersion,
69
- timeout,
70
- logger
71
- ),
72
- tryGetEnvironmentConfig(
73
- request.configUrl,
74
- request.configFile,
75
- request.environment,
76
- timeout,
77
- logger
78
- ),
79
- ])
80
-
81
- // Return first successful config in priority order
82
- if (localConfig !== null) {
83
- logger.info("[VWR Config] ✓ Using config from: local", localConfig)
84
- return localConfig
85
- }
86
- if (deviceConfig !== null) {
87
- logger.info("[VWR Config] ✓ Using config from: device", deviceConfig)
88
- return deviceConfig
43
+ class VWRConfigFetcher {
44
+ private readonly defaults: VWRConfig
45
+ private readonly timeout: number
46
+ private readonly logger: Logger
47
+ private readonly configUrl: string
48
+ private readonly configFile: string
49
+ private readonly platform: string
50
+ private readonly deviceId: string
51
+ private readonly environment: string
52
+ private readonly shellVersion: string
53
+
54
+ constructor(request: VWRConfigRequest, logger: Logger) {
55
+ this.defaults = getDefaultConfig(request.environment)
56
+ this.timeout = request.timeout ?? DEFAULT_CONFIG_TIMEOUT
57
+ this.logger = logger
58
+ this.configUrl = request.configUrl
59
+ this.configFile = request.configFile
60
+ this.platform = request.platform
61
+ this.deviceId = request.deviceId
62
+ this.environment = request.environment
63
+ this.shellVersion = request.shellVersion
89
64
  }
90
- if (shellVersionConfig !== null) {
91
- logger.info(
92
- "[VWR Config] ✓ Using config from: shellVersion",
93
- shellVersionConfig
94
- )
95
- return shellVersionConfig
96
- }
97
- if (environmentConfig !== null) {
98
- logger.info(
99
- "[VWR Config] ✓ Using config from: environment",
100
- environmentConfig
65
+
66
+ async fetch(): Promise<VWRConfig> {
67
+ this.logger.info(
68
+ "[VWR Config] Fetching config with priority: local → device → shellVersion → environment → defaults"
101
69
  )
102
- return environmentConfig
103
- }
104
70
 
105
- // All fetches failed, use defaults
106
- logger.warn(
107
- "[VWR Config] All config fetches failed, using built-in defaults"
108
- )
109
- const defaultConfig = getDefaultConfig()
110
- return defaultConfig
111
- }
71
+ // Fetch all configs in parallel for performance
72
+ const [
73
+ localConfig,
74
+ deviceConfig,
75
+ shellVersionConfig,
76
+ environmentConfig,
77
+ ] = await Promise.all([
78
+ this.tryGetLocalConfig(),
79
+ this.tryGetDeviceConfig(),
80
+ this.tryGetShellVersionConfig(),
81
+ this.tryGetEnvironmentConfig(),
82
+ ])
112
83
 
113
- const tryGetLocalConfig = async (
114
- configFile: string,
115
- timeout: number,
116
- logger: Logger
117
- ): Promise<VWRConfig | null> => {
118
- try {
119
- const url = buildUrl(configFile, ENV_DEFAULTS["local"]?.configUrl ?? "")
120
- logger.info(`[VWR Config] Trying local: ${url}`)
121
- return tryGetVWRConfig(url, timeout)
122
- } catch (error) {
123
- logger.error(
124
- `[VWR Config] URL construction failed for local config: ${error}`
125
- )
126
- return null
127
- }
128
- }
84
+ // Return first successful config in priority order
85
+ if (localConfig !== null) {
86
+ this.logger.info(
87
+ "[VWR Config] ✓ Using config from: local",
88
+ localConfig
89
+ )
90
+ return localConfig
91
+ }
92
+ if (deviceConfig !== null) {
93
+ this.logger.info(
94
+ "[VWR Config] ✓ Using config from: device",
95
+ deviceConfig
96
+ )
97
+ return deviceConfig
98
+ }
99
+ if (shellVersionConfig !== null) {
100
+ this.logger.info(
101
+ "[VWR Config] ✓ Using config from: shellVersion",
102
+ shellVersionConfig
103
+ )
104
+ return shellVersionConfig
105
+ }
106
+ if (environmentConfig !== null) {
107
+ this.logger.info(
108
+ "[VWR Config] ✓ Using config from: environment",
109
+ environmentConfig
110
+ )
111
+ return environmentConfig
112
+ }
129
113
 
130
- const tryGetDeviceConfig = async (
131
- configUrl: string,
132
- configFile: string,
133
- platform: string,
134
- deviceId: string,
135
- timeout: number,
136
- logger: Logger
137
- ): Promise<VWRConfig | null> => {
138
- try {
139
- const url = buildUrl(
140
- `device/${platform}/${deviceId}/${configFile}`,
141
- configUrl
142
- )
143
- logger.info(`[VWR Config] Trying device: ${url}`)
144
- return tryGetVWRConfig(url, timeout)
145
- } catch (error) {
146
- logger.error(
147
- `[VWR Config] URL construction failed for device config: ${error}`
114
+ // All fetches failed, use defaults
115
+ this.logger.warn(
116
+ "[VWR Config] All config fetches failed, using built-in defaults"
148
117
  )
149
- return null
118
+ return this.defaults
150
119
  }
151
- }
152
120
 
153
- const tryGetEnvironmentConfig = async (
154
- configUrl: string,
155
- configFile: string,
156
- environment: string,
157
- timeout: number,
158
- logger: Logger
159
- ): Promise<VWRConfig | null> => {
160
- try {
161
- const url = buildUrl(
162
- `environments/${environment}/${configFile}`,
163
- configUrl
164
- )
165
- logger.info(`[VWR Config] Trying environment: ${url}`)
166
- return tryGetVWRConfig(url, timeout)
167
- } catch (error) {
168
- logger.error(
169
- `[VWR Config] URL construction failed for environment config: ${error}`
170
- )
171
- return null
121
+ private async tryGetLocalConfig(): Promise<VWRConfig | null> {
122
+ try {
123
+ const url = buildUrl(
124
+ this.configFile,
125
+ ENV_DEFAULTS["local"]?.configUrl ?? ""
126
+ )
127
+ this.logger.info(`[VWR Config] Trying local: ${url}`)
128
+ return this.tryFetchConfig(url)
129
+ } catch (error) {
130
+ this.logger.error(
131
+ `[VWR Config] URL construction failed for local config: ${error}`
132
+ )
133
+ return null
134
+ }
172
135
  }
173
- }
174
136
 
175
- const tryGetShellVersionConfig = async (
176
- configUrl: string,
177
- configFile: string,
178
- environment: string,
179
- platform: string,
180
- shellVersion: string,
181
- timeout: number,
182
- logger: Logger
183
- ): Promise<VWRConfig | null> => {
184
- try {
185
- const url = buildUrl(
186
- `shellVersion/${environment}/${platform}/${shellVersion}/${configFile}`,
187
- configUrl
188
- )
189
- logger.info(`[VWR Config] Trying shellVersion: ${url}`)
190
- return tryGetVWRConfig(url, timeout)
191
- } catch (error) {
192
- logger.error(
193
- `[VWR Config] URL construction failed for shellVersion config: ${error}`
194
- )
195
- return null
137
+ private async tryGetDeviceConfig(): Promise<VWRConfig | null> {
138
+ try {
139
+ const url = buildUrl(
140
+ `device/${this.platform}/${this.deviceId}/${this.configFile}`,
141
+ this.configUrl
142
+ )
143
+ this.logger.info(`[VWR Config] Trying device: ${url}`)
144
+ return this.tryFetchConfig(url)
145
+ } catch (error) {
146
+ this.logger.error(
147
+ `[VWR Config] URL construction failed for device config: ${error}`
148
+ )
149
+ return null
150
+ }
196
151
  }
197
- }
198
152
 
199
- const tryGetVWRConfig = async (
200
- url: URL,
201
- timeout: number
202
- ): Promise<VWRConfig | null> => {
203
- const controller = new AbortController()
204
- const timeoutId = setTimeout(() => controller.abort(), timeout)
205
-
206
- try {
207
- const response = await fetch(
208
- new Request(url, {
209
- headers: {
210
- "Content-Type": "application/json",
211
- },
212
- signal: controller.signal,
213
- })
214
- )
153
+ private async tryGetEnvironmentConfig(): Promise<VWRConfig | null> {
154
+ try {
155
+ const url = buildUrl(
156
+ `environments/${this.environment}/${this.configFile}`,
157
+ this.configUrl
158
+ )
159
+ this.logger.info(`[VWR Config] Trying environment: ${url}`)
160
+ return this.tryFetchConfig(url)
161
+ } catch (error) {
162
+ this.logger.error(
163
+ `[VWR Config] URL construction failed for environment config: ${error}`
164
+ )
165
+ return null
166
+ }
167
+ }
215
168
 
216
- if (!response.ok) {
217
- clearTimeout(timeoutId)
169
+ private async tryGetShellVersionConfig(): Promise<VWRConfig | null> {
170
+ try {
171
+ const url = buildUrl(
172
+ `shellVersion/${this.environment}/${this.platform}/${this.shellVersion}/${this.configFile}`,
173
+ this.configUrl
174
+ )
175
+ this.logger.info(`[VWR Config] Trying shellVersion: ${url}`)
176
+ return this.tryFetchConfig(url)
177
+ } catch (error) {
178
+ this.logger.error(
179
+ `[VWR Config] URL construction failed for shellVersion config: ${error}`
180
+ )
218
181
  return null
219
182
  }
183
+ }
220
184
 
221
- let config = await response.json()
222
- config = parseConfig(config)
185
+ private async tryFetchConfig(url: URL): Promise<VWRConfig | null> {
186
+ const controller = new AbortController()
187
+ const timeoutId = setTimeout(() => controller.abort(), this.timeout)
223
188
 
224
- clearTimeout(timeoutId)
225
- return config as VWRConfig
226
- } catch {
227
- clearTimeout(timeoutId)
228
- return null
229
- }
230
- }
189
+ try {
190
+ const response = await fetch(
191
+ new Request(url, {
192
+ headers: { "Content-Type": "application/json" },
193
+ signal: controller.signal,
194
+ })
195
+ )
231
196
 
232
- const parseConfig = (config: VWRConfig): VWRConfig => {
233
- const defaultConfig = getDefaultConfig()
197
+ if (!response.ok) {
198
+ clearTimeout(timeoutId)
199
+ return null
200
+ }
234
201
 
235
- if (
236
- !Array.isArray(config.trustedDomains) ||
237
- config.trustedDomains.length === 0
238
- ) {
239
- config.trustedDomains = defaultConfig.trustedDomains
202
+ const config = await response.json()
203
+ clearTimeout(timeoutId)
204
+ return this.parseConfig(config)
205
+ } catch {
206
+ clearTimeout(timeoutId)
207
+ return null
208
+ }
240
209
  }
241
210
 
242
- if (!config.vwrUrl) {
243
- config.vwrUrl = defaultConfig.vwrUrl
244
- if (!config.trustedDomains.includes(defaultConfig.vwrUrl)) {
245
- config.trustedDomains.push(defaultConfig.vwrUrl)
211
+ private parseConfig(config: Partial<VWRConfig>): VWRConfig {
212
+ const result: VWRConfig = {
213
+ hubUrl: config.hubUrl || this.defaults.hubUrl,
214
+ vwrUrl: config.vwrUrl || this.defaults.vwrUrl,
215
+ launchUrl: config.launchUrl,
216
+ platformApiUrl:
217
+ config.platformApiUrl || this.defaults.platformApiUrl,
218
+ platformAuthApiUrl:
219
+ config.platformAuthApiUrl || this.defaults.platformAuthApiUrl,
220
+ trustedDomains:
221
+ Array.isArray(config.trustedDomains) &&
222
+ config.trustedDomains.length > 0
223
+ ? [...config.trustedDomains]
224
+ : [...this.defaults.trustedDomains],
246
225
  }
247
- }
248
226
 
249
- if (!config.hubUrl) {
250
- config.hubUrl = defaultConfig.hubUrl
251
- if (!config.trustedDomains.includes(defaultConfig.hubUrl)) {
252
- config.trustedDomains.push(defaultConfig.hubUrl)
227
+ // Ensure vwrUrl origin is in trustedDomains
228
+ const vwrUrlOrigin = new URL(result.vwrUrl).origin
229
+ if (!result.trustedDomains.includes(vwrUrlOrigin)) {
230
+ result.trustedDomains.push(vwrUrlOrigin)
253
231
  }
254
- }
255
232
 
256
- // launchUrl is optional - only add to trustedDomains if explicitly set
257
- if (config.launchUrl && !config.trustedDomains.includes(config.launchUrl)) {
258
- config.trustedDomains.push(config.launchUrl)
233
+ // Ensure hubUrl origin is in trustedDomains
234
+ const hubUrlOrigin = new URL(result.hubUrl).origin
235
+ if (!result.trustedDomains.includes(hubUrlOrigin)) {
236
+ result.trustedDomains.push(hubUrlOrigin)
237
+ }
238
+
239
+ // launchUrl is optional - only add to trustedDomains if explicitly set
240
+ if (result.launchUrl) {
241
+ try {
242
+ const launchUrlOrigin = new URL(result.launchUrl).origin
243
+ if (!result.trustedDomains.includes(launchUrlOrigin)) {
244
+ result.trustedDomains.push(launchUrlOrigin)
245
+ }
246
+ } catch {
247
+ // Invalid launchUrl - skip adding to trustedDomains
248
+ }
249
+ }
250
+
251
+ return result
259
252
  }
253
+ }
260
254
 
261
- return config
255
+ export const getVWRConfig = async (
256
+ request: VWRConfigRequest,
257
+ logger: Logger = defaultLogger
258
+ ): Promise<VWRConfig> => {
259
+ return new VWRConfigFetcher(request, logger).fetch()
262
260
  }
263
261
 
264
262
  export const validateConfig = (config: VWRConfig): boolean => {
@@ -274,13 +272,12 @@ export const validateConfig = (config: VWRConfig): boolean => {
274
272
  return true
275
273
  }
276
274
 
277
- const getDefaultConfig = (): VWRConfig => {
278
- const ENVIRONMENT = import.meta.env.VITE_ENVIRONMENT || "dev"
279
- const defaults = ENV_DEFAULTS[ENVIRONMENT]
275
+ const getDefaultConfig = (environment: string): VWRConfig => {
276
+ const defaults = ENV_DEFAULTS[environment]
280
277
 
281
278
  if (!defaults) {
282
279
  throw new Error(
283
- `[VWR Config] Unknown environment: ${ENVIRONMENT}. Valid: local, dev, staging, prod`
280
+ `[VWR Config] Unknown environment: ${environment}. Valid: local, dev, staging, prod`
284
281
  )
285
282
  }
286
283
 
@@ -288,6 +285,12 @@ const getDefaultConfig = (): VWRConfig => {
288
285
  hubUrl: defaults.hubUrl,
289
286
  vwrUrl: defaults.vwrUrl,
290
287
  launchUrl: undefined,
291
- trustedDomains: [defaults.hubUrl, defaults.vwrUrl],
288
+ platformApiUrl: defaults.platformApiUrl,
289
+ platformAuthApiUrl: defaults.platformAuthApiUrl,
290
+ trustedDomains: [
291
+ new URL(defaults.hubUrl).origin,
292
+ new URL(defaults.vwrUrl).origin,
293
+ ...(defaults.trustedOrigins ?? []),
294
+ ],
292
295
  }
293
296
  }
package/eslint.config.mjs DELETED
@@ -1,23 +0,0 @@
1
- import baseConfig from "@platform/eslint-config"
2
-
3
- export default [
4
- ...baseConfig,
5
- {
6
- ignores: ["dist/**", "node_modules/**"],
7
- },
8
- {
9
- languageOptions: {
10
- parserOptions: {
11
- project: ["tsconfig.json", "tsconfig.eslint.json"],
12
- tsconfigRootDir: import.meta.dirname,
13
- },
14
- },
15
- rules: {
16
- "no-console": "off",
17
- "@typescript-eslint/explicit-function-return-type": "off",
18
- "@typescript-eslint/prefer-nullish-coalescing": "off",
19
- "@typescript-eslint/prefer-optional-chain": "off",
20
- "@typescript-eslint/no-explicit-any": "off",
21
- },
22
- },
23
- ]
package/scripts/build.js DELETED
@@ -1,2 +0,0 @@
1
- #!/usr/bin/env -S node --import tsx
2
- import "./build.ts"