6
6
* found in the LICENSE file at https://angular.dev/license
7
7
*/
8
8
9
+ import assert from 'node:assert' ;
9
10
import { randomUUID } from 'node:crypto' ;
10
11
import { mkdirSync , readFileSync , writeFileSync } from 'node:fs' ;
11
12
import { join } from 'node:path' ;
12
13
import type { Connect } from 'vite' ;
13
14
15
+ type DevToolsJson = {
16
+ workspace : {
17
+ root : string ;
18
+ uuid : string ;
19
+ } ;
20
+ } ;
21
+
14
22
const CHROME_DEVTOOLS_ROUTE = '/.well-known/appspecific/com.chrome.devtools.json' ;
15
23
16
24
export function createChromeDevtoolsMiddleware (
17
25
cacheDir : string ,
18
26
projectRoot : string ,
19
27
) : Connect . NextHandleFunction {
20
- let devtoolsConfig : string ;
28
+ let devtoolsConfig : string | undefined ;
21
29
const devtoolsConfigPath = join ( cacheDir , 'com.chrome.devtools.json' ) ;
22
30
23
31
return function chromeDevtoolsMiddleware ( req , res , next ) {
@@ -27,22 +35,27 @@ export function createChromeDevtoolsMiddleware(
27
35
return ;
28
36
}
29
37
30
- // We store the UUID and re-use it to ensure Chrome does not repeatedly ask for permissions when restarting the dev server.
31
- try {
32
- devtoolsConfig ??= readFileSync ( devtoolsConfigPath , 'utf-8' ) ;
33
- } catch {
34
- const devtoolsConfigJson = {
35
- workspace : {
36
- root : projectRoot ,
37
- uuid : randomUUID ( ) ,
38
- } ,
39
- } ;
40
-
41
- devtoolsConfig = JSON . stringify ( devtoolsConfigJson , undefined , 2 ) ;
38
+ if ( ! devtoolsConfig ) {
39
+ // We store the UUID and re-use it to ensure Chrome does not repeatedly ask for permissions when restarting the dev server.
42
40
try {
43
- mkdirSync ( cacheDir , { recursive : true } ) ;
44
- writeFileSync ( devtoolsConfigPath , devtoolsConfig ) ;
45
- } catch { }
41
+ const devtoolsConfig = readFileSync ( devtoolsConfigPath , 'utf-8' ) ;
42
+ const devtoolsConfigJson : DevToolsJson = JSON . parse ( devtoolsConfig ) ;
43
+ assert . equal ( projectRoot , devtoolsConfigJson ?. workspace . root ) ;
44
+ } catch {
45
+ const devtoolsConfigJson : DevToolsJson = {
46
+ workspace : {
47
+ root : projectRoot ,
48
+ uuid : randomUUID ( ) ,
49
+ } ,
50
+ } ;
51
+
52
+ devtoolsConfig = JSON . stringify ( devtoolsConfigJson , undefined , 2 ) ;
53
+
54
+ try {
55
+ mkdirSync ( cacheDir , { recursive : true } ) ;
56
+ writeFileSync ( devtoolsConfigPath , devtoolsConfig ) ;
57
+ } catch { }
58
+ }
46
59
}
47
60
48
61
res . setHeader ( 'Content-Type' , 'application/json' ) ;
0 commit comments