Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

list-github-dir-content

Package Overview
Dependencies
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

list-github-dir-content - npm Package Compare versions

Comparing version 2.0.0 to 3.0.0-0

68

index.js
const fetch = require('node-fetch'); // Automatically excluded in browser bundles
async function api(endpoint, token) {
// Matches '/<user>/<repo>/tree/<ref>/<dir>'
const urlParserRegex = /^[/]([^/]+)[/]([^/]+)[/]tree[/]([^/]+)[/](.*)/;
function parseResource(resource) {
if (typeof resource === 'string') {
const parsedUrl = new URL(resource);
resource = {};
[, resource.user, resource.repository, resource.ref, resource.directory] =
urlParserRegex.exec(parsedUrl.pathname) || [];
if (typeof resource.directory !== 'string') {
throw new TypeError('Unable to parse GitHub URL');
}
if (parsedUrl.hostname !== 'github.com') {
resource.api = `https://${parsedUrl.host}/api/v3`;
}
}
resource.api = resource.api || 'https://api.github.com';
resource.ref = resource.ref || 'HEAD';
// TODO: Validate
return resource;
}
async function githubApi(api, endpoint, token) {
token = token ? `&access_token=${token}` : '';
const response = await fetch(`https://api.github.com/repos/${endpoint}${token}`);
const response = await fetch(`${api}/repos/${endpoint}${token}`);
return response.json();

@@ -12,12 +36,15 @@ }

async function viaContentsApi({
user,
repository,
ref = 'HEAD',
directory,
resource,
token,
getFullData = false
}) {
resource = parseResource(resource);
const files = [];
const requests = [];
const contents = await api(`${user}/${repository}/contents/${directory}?ref=${ref}`, token);
const contents = await githubApi(
resource.api,
`${resource.user}/${resource.repository}/contents/${resource.directory}?ref=${resource.ref}`,
token
);
for (const item of contents) {

@@ -28,6 +55,3 @@ if (item.type === 'file') {

requests.push(viaContentsApi({
user,
repository,
ref,
directory: item.path,
resource,
token,

@@ -46,17 +70,21 @@ getFullData

async function viaTreesApi({
user,
repository,
ref = 'HEAD',
directory,
resource,
token,
getFullData = false
}) {
if (!directory.endsWith('/')) {
directory += '/';
resource = parseResource(resource);
const files = [];
const contents = await githubApi(
resource.api,
`${resource.user}/${resource.repository}/git/trees/${resource.ref}?recursive=1`,
token
);
if (!resource.directory.endsWith('/')) {
resource.directory += '/';
}
const files = [];
const contents = await api(`${user}/${repository}/git/trees/${ref}?recursive=1`, token);
for (const item of contents.tree) {
if (item.type === 'blob' && item.path.startsWith(directory)) {
if (item.type === 'blob' && item.path.startsWith(resource.directory)) {
files.push(getFullData ? item : item.path);

@@ -63,0 +91,0 @@ }

{
"name": "list-github-dir-content",
"version": "2.0.0",
"version": "3.0.0-0",
"description": "List all the files in a GitHub repo’s directory",

@@ -5,0 +5,0 @@ "keywords": [

@@ -71,2 +71,10 @@ # list-github-dir-content [![Build Status](https://travis-ci.org/fregante/list-github-dir-content.svg?branch=master)](https://travis-ci.org/fregante/list-github-dir-content)

#### api
Type: `string`
Default: `"https://api.github.com"`
The GitHub API endpoint to use, like `https://ghe.example.com/api`.
#### user

@@ -73,0 +81,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc