MCPcopy
hub / github.com/wooorm/starry-night / readOrFetch

Function readOrFetch

script/list.js:252–272  ·  view source on GitHub ↗

* Get a file. * * @param {Readonly } localUrl * URL to file system. * @param {Readonly } remoteUrl * URL to remote. * @returns {Promise } * Content, if local or remote.

(localUrl, remoteUrl)

Source from the content-addressed store, hash-verified

250 * Content, if local or remote.
251 */
252async function readOrFetch(localUrl, remoteUrl) {
253 try {
254 return String(await fs.readFile(localUrl))
255 } catch (error) {
256 const exception = /** @type {NodeJS.ErrnoException} */ (error)
257 if (exception.code !== 'ENOENT') {
258 throw exception
259 }
260 }
261
262 try {
263 const response = await fetch(remoteUrl, {
264 headers: {Authorization: 'bearer ' + ghKey}
265 })
266 const responseText = await response.text()
267 await fs.writeFile(localUrl, responseText)
268 return responseText
269 } catch (error) {
270 console.error('Could not fetch `%s`', remoteUrl, error)
271 }
272}

Callers 1

listFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected