( endpoint: string, pathName: string, method = "GET", body?: unknown, )
| 915 | } |
| 916 | |
| 917 | function requestBuffer( |
| 918 | endpoint: string, |
| 919 | pathName: string, |
| 920 | method = "GET", |
| 921 | body?: unknown, |
| 922 | ): Promise<Buffer> { |
| 923 | const url = new URL(pathName, endpoint); |
| 924 | const payload = |
| 925 | body === undefined ? undefined : Buffer.from(JSON.stringify(body)); |
| 926 | return new Promise((resolve, reject) => { |
| 927 | const request = http.request( |
| 928 | url, |
| 929 | { |
| 930 | method, |
| 931 | headers: payload |
| 932 | ? { |
| 933 | "content-type": "application/json", |
| 934 | "content-length": String(payload.length), |
| 935 | origin: endpoint, |
| 936 | } |
| 937 | : { origin: endpoint }, |
| 938 | }, |
| 939 | (response) => { |
| 940 | const chunks: Buffer[] = []; |
| 941 | response.on("data", (chunk: Buffer) => chunks.push(chunk)); |
| 942 | response.on("end", () => { |
| 943 | const buffer = Buffer.concat(chunks); |
| 944 | if ( |
| 945 | (response.statusCode ?? 500) < 200 || |
| 946 | (response.statusCode ?? 500) >= 300 |
| 947 | ) { |
| 948 | reject( |
| 949 | new Error( |
| 950 | `${method} ${pathName} returned ${response.statusCode}: ${ |
| 951 | buffer.toString("utf8") || response.statusMessage || "" |
| 952 | }`, |
| 953 | ), |
| 954 | ); |
| 955 | } else { |
| 956 | resolve(buffer); |
| 957 | } |
| 958 | }); |
| 959 | }, |
| 960 | ); |
| 961 | request.on("error", reject); |
| 962 | if (payload) { |
| 963 | request.write(payload); |
| 964 | } |
| 965 | request.end(); |
| 966 | }); |
| 967 | } |
| 968 | |
| 969 | function treeQuery(options: QueryOptions = {}): string { |
| 970 | const params = new URLSearchParams(); |
no test coverage detected