1- import { readFile , stat , unlink } from "fs/promises" ;
1+ import { FileHandle , readFile , stat , unlink } from "fs/promises" ;
22import { STALE_TIMEOUT_MS , VSCODE_COMMAND_TIMEOUT_MS } from "./constants" ;
33import { getRequestPath , getResponsePath } from "./paths" ;
44import { Request , Response } from "./types" ;
5- import { writeJSONExclusive } from "./fileUtils" ;
5+ import { writeJSON } from "./fileUtils" ;
66
77/**
88 * Reads the JSON-encoded request from the request file, unlinking the file
@@ -15,8 +15,6 @@ export async function readRequest(): Promise<Request> {
1515 const stats = await stat ( requestPath ) ;
1616 const request = JSON . parse ( await readFile ( requestPath , "utf-8" ) ) ;
1717
18- await unlink ( requestPath ) ;
19-
2018 if (
2119 Math . abs ( stats . mtimeMs - new Date ( ) . getTime ( ) ) > VSCODE_COMMAND_TIMEOUT_MS
2220 ) {
@@ -29,38 +27,10 @@ export async function readRequest(): Promise<Request> {
2927}
3028
3129/**
32- * Writes the response to the response file as JSON. If a stale response file
33- * exists, it will be removed. If a non-stale response file exists, assumes
34- * there is another VSCode instance mysteriously trying to handle the request
35- * as well and fails.
30+ * Writes the response to the response file as JSON.
31+ * @param file The file to write to
3632 * @param response The response object to JSON-encode and write to disk
3733 */
38- export async function writeResponse ( response : Response ) {
39- const responsePath = getResponsePath ( ) ;
40-
41- try {
42- await writeJSONExclusive ( responsePath , response ) ;
43- } catch ( err ) {
44- if ( err . code !== "EEXIST" ) {
45- throw err ;
46- }
47-
48- try {
49- const stats = await stat ( responsePath ) ;
50-
51- if ( Math . abs ( stats . mtimeMs - new Date ( ) . getTime ( ) ) < STALE_TIMEOUT_MS ) {
52- throw new Error ( "Another process has an active response file" ) ;
53- }
54-
55- console . log ( "Removing stale response file" ) ;
56- await unlink ( responsePath ) ;
57- } catch ( err ) {
58- // If the file was removed for whatever reason in the interim we just continue
59- if ( err . code !== "ENOENT" ) {
60- throw err ;
61- }
62- }
63-
64- await writeJSONExclusive ( responsePath , response ) ;
65- }
34+ export async function writeResponse ( file : FileHandle , response : Response ) {
35+ await writeJSON ( file , response ) ;
6636}
0 commit comments