(method, requestPath, body, options = {})
| 1865 | } |
| 1866 | |
| 1867 | function httpBuffer(method, requestPath, body, options = {}) { |
| 1868 | const payload = body === undefined ? null : Buffer.from(JSON.stringify(body)); |
| 1869 | const startedAt = Date.now(); |
| 1870 | logHttp(method, requestPath, body); |
| 1871 | return new Promise((resolve, reject) => { |
| 1872 | const request = http.request( |
| 1873 | { |
| 1874 | hostname: "127.0.0.1", |
| 1875 | port: serverPort, |
| 1876 | path: requestPath, |
| 1877 | method, |
| 1878 | headers: { |
| 1879 | Origin: origin, |
| 1880 | Accept: "application/json", |
| 1881 | ...(payload |
| 1882 | ? { |
| 1883 | "Content-Type": "application/json", |
| 1884 | "Content-Length": payload.length, |
| 1885 | } |
| 1886 | : {}), |
| 1887 | }, |
| 1888 | }, |
| 1889 | (response) => { |
| 1890 | const chunks = []; |
| 1891 | response.on("data", (chunk) => chunks.push(chunk)); |
| 1892 | response.on("end", () => { |
| 1893 | const buffer = Buffer.concat(chunks); |
| 1894 | if (response.statusCode < 200 || response.statusCode >= 300) { |
| 1895 | reject( |
| 1896 | new Error( |
| 1897 | `${method} ${requestPath} returned ${response.statusCode}: ${buffer.toString("utf8")}`, |
| 1898 | ), |
| 1899 | ); |
| 1900 | return; |
| 1901 | } |
| 1902 | const elapsedMs = Date.now() - startedAt; |
| 1903 | if (options.maxElapsedMs && elapsedMs > options.maxElapsedMs) { |
| 1904 | reject( |
| 1905 | new Error( |
| 1906 | `${method} ${requestPath} took ${elapsedMs}ms, above ${options.maxElapsedMs}ms budget`, |
| 1907 | ), |
| 1908 | ); |
| 1909 | return; |
| 1910 | } |
| 1911 | logHttpResult(method, requestPath, elapsedMs, buffer); |
| 1912 | resolve(buffer); |
| 1913 | }); |
| 1914 | }, |
| 1915 | ); |
| 1916 | request.on("error", reject); |
| 1917 | if (payload) { |
| 1918 | request.write(payload); |
| 1919 | } |
| 1920 | request.end(); |
| 1921 | }); |
| 1922 | } |
| 1923 | |
| 1924 | function openSimulatorApp(udid) { |
no test coverage detected