Skip to content

Commit

Permalink
Load PackageList from memory-cache, if artifactory does not answer.//…
Browse files Browse the repository at this point in the history
… also: cleanup code
  • Loading branch information
dmstern committed Dec 5, 2018
1 parent 18cbab5 commit b73f4d8
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 21 deletions.
41 changes: 28 additions & 13 deletions server/artifactory-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as fs from 'fs-extra';
import * as tar from 'tar';
import * as showdown from 'showdown';
import * as emoji from 'node-emoji';
import { PackagesResponse } from '../types/PackageResponse';
import { PackagesResponse } from '../types/PackagesResponse';
import PackageId from '../types/PackageId';
import getFiles from './fileLister';
import * as os from 'os';
Expand All @@ -13,22 +13,24 @@ import configService from './config-service.js';

let instance;

const tmpDir = path.join(os.homedir(), '.npmfrog', 'package-cache');
const packageDetailCache = {};
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';

interface AdditionalCode {
readme: string;
fileList;
}

class ArtifactoryService {
public baseURL: string;
private tmpDir: string;
private packageDetailCache: PackagesResponse;

constructor() {
if (instance) {
return instance;
}
this.tmpDir = path.join(os.homedir(), '.npmfrog', 'package-cache');
this.packageDetailCache = {};
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';

configService
.getConfig()
.then(config => {
Expand Down Expand Up @@ -56,7 +58,16 @@ class ArtifactoryService {
);
});
}
return axios.get(`/-/all`);
return axios
.get(`/-/all`)
.then(response => {
return response;
})
.catch(error => {
return new Promise<AxiosResponse>((resolve, reject) => {
resolve(this.createAxiosResponse(this.packageDetailCache));
});
});
}

public async getPackageDetail({
Expand All @@ -66,7 +77,7 @@ class ArtifactoryService {
}: PackageId): Promise<AxiosResponse> {
const latestVersionResponse = await this.getDistTags({ scope, packageName });
const currentVersion = version || latestVersionResponse.data.latest;
const key = `${scope}-${packageName}-${currentVersion}`;
const key = `${scope}-${packageName}`;

const packageDetailResponse: AxiosResponse = process.env.MOCK
? await new Promise<AxiosResponse>((resolve, reject) => {
Expand All @@ -79,9 +90,9 @@ class ArtifactoryService {
}
resolve(this.createAxiosResponse(data));
})
: packageDetailCache[key]
: this.packageDetailCache[key]
? await new Promise<AxiosResponse>((resolve, reject) => {
resolve(this.createAxiosResponse(packageDetailCache[key]));
resolve(this.createAxiosResponse(this.packageDetailCache[key]));
})
: await axios.get(`/${this.name2url({ scope, packageName })}`);

Expand All @@ -104,7 +115,7 @@ class ArtifactoryService {
: await new Promise<AdditionalCode>(async (resolve, reject) => {
const packageDetail = packageDetailResponse.data;
const downloadUrl = packageDetail.versions[currentVersion].dist.tarball;
const storageDir = path.join(tmpDir, scope, packageName, currentVersion);
const storageDir = path.join(this.tmpDir, scope, packageName, currentVersion);
if (fs.existsSync(storageDir)) {
this.readAdditionalCode(storageDir).then(response => {
resolve(response);
Expand Down Expand Up @@ -146,7 +157,12 @@ class ArtifactoryService {
Object.assign(packageDetailResponse.data, additionalCode);

return new Promise<AxiosResponse>((resolve, reject) => {
packageDetailCache[key] = packageDetailResponse.data;
if (!this.packageDetailCache[key]) {
this.packageDetailCache[key] = Object.assign(
{ _isCached: true },
packageDetailResponse.data,
);
}
resolve(packageDetailResponse);
});
}
Expand All @@ -169,15 +185,14 @@ class ArtifactoryService {
format: string = 'string',
): Promise<string | Buffer> {
const absPath = path.join(
tmpDir,
this.tmpDir,
packageId.scope,
packageId.packageName,
packageId.version,
'package',
filepath,
);
const fileContent = fs.readFileSync(absPath);
console.log(format === 'string');
return format === 'string' ? fileContent.toString() : fileContent;
}

Expand Down
33 changes: 27 additions & 6 deletions src/components/PackageList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,16 @@
<v-list subheader three-line v-else class="package-list">
<v-subheader
class="title"
v-if="config && config.artifactory">Found {{packages.data.length}}/{{packages.all.length}} npm packages on &nbsp;
v-if="config && config.artifactory">Found {{countFreshPackages(packages.data)}}/{{countFreshPackages(packages.all)}} npm packages on &nbsp;
<ExternalLink
:href="`http${
config.artifactory.https? 's' : ''
}://${
config.artifactory.host
}/artifactory/webapp/#/artifacts/browse/tree/General/${
config.artifactory.repoKey
}`"
:text="config.artifactory.host">
</ExternalLink>
</ExternalLink>.
<span v-if="countFreshPackages(packages.data) === 0">&nbsp;Displaying cached packages instead:</span>
</v-subheader>
<template v-for="(item, index) in packages.data">
<v-list-tile
Expand All @@ -24,7 +23,15 @@
@click="$router.push(`package/${item.name}`)"
>
<v-list-tile-content>
<v-list-tile-title class="font-weight-medium">{{ item.name }}</v-list-tile-title>
<v-list-tile-title class="font-weight-medium">
{{ item.name }}
<v-chip
small
v-if="item._isCached"
disabled
label
><v-icon>{{$vuetify.icons.cache}}</v-icon> cached</v-chip>
</v-list-tile-title>
<v-list-tile-sub-title>{{item.description}}</v-list-tile-sub-title>
<v-list-tile-sub-title class="package-list--keywords">
<v-chip v-for="keyword in item.keywords" :key="keyword" small>{{keyword}}</v-chip>
Expand Down Expand Up @@ -72,7 +79,7 @@
<script lang="ts">
import { Component, Prop, Vue } from 'vue-property-decorator';
import DataStore from '@/services/DataStore';
import { PackagesResponse } from '../../types/PackageResponse';
import { PackagesResponse } from '../../types/PackagesResponse';
import Package from '../../types/Package';
import { EventBus, Events } from '@/services/event-bus';
import LoadingSpinner from '@/components/LoadingSpinner.vue';
Expand Down Expand Up @@ -133,6 +140,16 @@ export default class Packages extends Vue {
);
}
private countFreshPackages(packages: PackagesResponse): number {
let count = 0;
for (const key of Object.keys(packages)) {
if (!packages[key]._isCached) {
count++;
}
}
return count;
}
private loadConfig(): void {
DataStore.Instance.getConfig().then(config => {
if (config) {
Expand All @@ -153,6 +170,10 @@ export default class Packages extends Vue {
.package-list {
padding-bottom: 88px;
.v-list__tile__title {
height: initial;
}
.v-list__tile__content {
padding: 0.4em 0;
}
Expand Down
1 change: 1 addition & 0 deletions src/plugins/vuetify.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export const icons = {
install: 'mdi-package-down',
version: 'fas fa-code-branch',
legal: 'fas fa-balance-scale',
cache: 'fas fa-archive',
code: 'fas fa-code',
created: 'fas fa-bolt',
updated: 'far fa-clock',
Expand Down
2 changes: 1 addition & 1 deletion src/services/BackendApi.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import axios, { AxiosInstance, AxiosPromise } from 'axios';
import { PackagesResponse } from '../../types/PackageResponse';
import { PackagesResponse } from '../../types/PackagesResponse';
import { PackageMetaDataDTO } from '../../types/package-meta-data';
import { IPackageJSON } from '../../types/package-json';
import Config from '../../types/Config';
Expand Down
2 changes: 1 addition & 1 deletion src/services/DataStore.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import BackendApi from './BackendApi';
import { PackagesResponse } from '../../types/PackageResponse';
import { PackagesResponse } from '../../types/PackagesResponse';
import Package from '../../types/Package';
import { Tag } from '../../types/Tag';
import { PackageMetaDataDTO } from '../../types/package-meta-data';
Expand Down
File renamed without changes.
1 change: 1 addition & 0 deletions types/package-meta-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export interface PackageMetaDataDTO extends IPackageJSON {
readonly users: {};
readonly _id: string;
readonly _rev: string;
readonly _isCached?: boolean;
}

export interface ITimes {
Expand Down

0 comments on commit b73f4d8

Please sign in to comment.