Skip to content

Commit

Permalink
fix: remove safe-join dependency (#2900)
Browse files Browse the repository at this point in the history
  • Loading branch information
radiantly authored Jul 11, 2021
1 parent 307e128 commit 0767251
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 19 deletions.
11 changes: 0 additions & 11 deletions npm-shrinkwrap.json

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

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,6 @@
"pump": "^3.0.0",
"raw-body": "^2.4.1",
"resolve": "^1.12.0",
"safe-join": "^0.1.3",
"semver": "^7.3.4",
"source-map-support": "^0.5.19",
"static-server": "^2.2.1",
Expand Down
12 changes: 5 additions & 7 deletions src/utils/read-repo-url.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
const url = require('url')

const fetch = require('node-fetch')
const { safeJoin } = require('safe-join')

// supported repo host types
const GITHUB = Symbol('GITHUB')
// const BITBUCKET = Symbol('BITBUCKET')
// const GITLAB = Symbol('GITLAB')
const GITHUB = 'GitHub'

/**
* Takes a url like https://github.com/netlify-labs/all-the-functions/tree/master/functions/9-using-middleware
Expand All @@ -27,7 +24,7 @@ const getRepoURLContents = async function (repoHost, ownerAndRepo, contentsPath)
// naive joining strategy for now
if (repoHost === GITHUB) {
// https://developer.github.com/v3/repos/contents/#get-contents
const APIURL = safeJoin('https://api.github.com/repos', ownerAndRepo, 'contents', contentsPath)
const APIURL = `https://api.github.com/repos/${ownerAndRepo}/contents/${contentsPath}`
try {
const res = await fetch(APIURL)
return await res.json()
Expand All @@ -51,13 +48,14 @@ const parseRepoURL = function (repoHost, URL) {
if (repoHost === GITHUB) {
// https://developer.github.com/v3/repos/contents/#get-contents
// what if it's not master? note that our contents retrieval may assume it is master
const [ownerAndRepo, contentsPath] = URL.path.split('/tree/master')
const [ownerAndRepo, contentsPath] = URL.path.slice(1).split('/tree/master/')
return [ownerAndRepo, contentsPath]
}
throw new Error('unsupported host ', repoHost)
throw new Error(`Unsupported host ${repoHost}`)
}

module.exports = {
parseRepoURL,
readRepoURL,
validateRepoURL,
}
17 changes: 17 additions & 0 deletions src/utils/read-repo-url.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const test = require('ava')

const { parseRepoURL } = require('./read-repo-url')

test('parseRepoURL: should parse GitHub URL', (t) => {
const url = new URL('https://github.com/netlify-labs/all-the-functions/tree/master/functions/9-using-middleware')
// parseRepoURL expects the result of url.parse
const [repo, contentPath] = parseRepoURL('GitHub', { path: url.pathname })

t.is(repo, 'netlify-labs/all-the-functions')
t.is(contentPath, 'functions/9-using-middleware')
})

test('parseRepoURL: should fail on GitLab URL', (t) => {
const url = new URL('https://gitlab.com/netlify-labs/all-the-functions/-/blob/master/functions/9-using-middleware')
t.throws(() => parseRepoURL('GitLab', { path: url.pathname }), { message: 'Unsupported host GitLab' })
})

1 comment on commit 0767251

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📊 Benchmark results

Package size: 329 MB

Please sign in to comment.