Skip to content

Commit

Permalink
refactor: Fix #69 and make remote requests async
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidWells committed Jul 29, 2024
1 parent 0bcfb13 commit a5120d6
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 7 deletions.
4 changes: 2 additions & 2 deletions lib/transforms/code/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const fs = require('fs')
const path = require('path')
const remoteRequest = require('../../utils/remoteRequest')
const { remoteRequest } = require('../../utils/remoteRequest')
const { isLocalPath } = require('../../utils/fs')
const { deepLog } = require('../../utils/logs')
const { getLineCount, getTextBetweenLines } = require('../../utils/text')
Expand Down Expand Up @@ -183,7 +183,7 @@ async function resolveRemoteContent(options, settings, srcPath) {

// Try initial remote request if public url
if (!remoteContent) {
remoteContent = remoteRequest(src, settings, srcPath)
remoteContent = await remoteRequest(src, settings, srcPath)
}

return remoteContent
Expand Down
6 changes: 3 additions & 3 deletions lib/transforms/remote.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
const remoteRequest = require('../utils/remoteRequest')
const { remoteRequest } = require('../utils/remoteRequest')

module.exports = function REMOTE(api) {
module.exports = async function REMOTE(api) {
// console.log('REMOTE api', api)
const { options, content, settings } = api
const { regex } = settings
// console.log('MAKE REMOTE REQUEST')
const remoteContent = remoteRequest(options.url, settings, api.srcPath)
const remoteContent = await remoteRequest(options.url, settings, api.srcPath)
if (!remoteContent) {
return content
}
Expand Down
31 changes: 29 additions & 2 deletions lib/utils/remoteRequest.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
const fetch = require('node-fetch')
const request = require('sync-request')

module.exports = function remoteRequest(url, settings = {}, srcPath) {
function formatUrl(url = '') {
return url.match(/^https?:\/\//) ? url : `https://${url}`
}

function remoteRequestSync(url, settings = {}, srcPath) {
let body
const finalUrl = (url.match(/^https?:\/\//)) ? url : `https://${url}`
const finalUrl = formatUrl(url)
try {
// @ts-expect-error
const res = request('GET', finalUrl)
Expand All @@ -18,6 +23,28 @@ module.exports = function remoteRequest(url, settings = {}, srcPath) {
return body
}

async function remoteRequest(url, settings = {}, srcPath) {
let body
const finalUrl = formatUrl(url)
try {
const res = await fetch(finalUrl)
body = await res.text()
} catch (e) {
console.log(`⚠️ WARNING: REMOTE URL "${finalUrl}" NOT FOUND`)
const msg = (e.message || '').split('\n')[0] + `\nFix "${url}" value in ${srcPath}`
console.log(msg)
if (settings.failOnMissingRemote) {
throw new Error(msg)
}
}
return body
}

module.exports = {
remoteRequestSync,
remoteRequest
}

/*
TODO add file caching?
*/
Expand Down
66 changes: 66 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"is-valid-path": "^0.1.1",
"micro-mdx-parser": "^1.1.0",
"mri": "^1.2.0",
"node-fetch": "^2.7.0",
"oparser": "^3.0.13",
"smart-glob": "^1.0.2",
"sync-request": "^6.1.0"
Expand Down

0 comments on commit a5120d6

Please sign in to comment.