mirror of
https://github.com/pnpm/pnpm.git
synced 2026-05-04 05:56:24 -04:00
82 lines
2.3 KiB
TypeScript
82 lines
2.3 KiB
TypeScript
import {
|
|
PackageResponse,
|
|
RequestPackageOptions,
|
|
WantedDependency,
|
|
} from '@pnpm/package-requester'
|
|
|
|
import got = require('got')
|
|
import pLimit = require('p-limit')
|
|
import {StoreController} from 'package-store'
|
|
import uuid = require('uuid')
|
|
|
|
export default function (
|
|
initOpts: {
|
|
remotePrefix: string,
|
|
concurrency?: number,
|
|
},
|
|
): Promise<StoreController> {
|
|
const remotePrefix = initOpts.remotePrefix
|
|
const limitedFetch = fetch.bind(null, pLimit(initOpts.concurrency || 100))
|
|
|
|
return new Promise((resolve, reject) => {
|
|
resolve({
|
|
close: async () => { return },
|
|
prune: async () => {
|
|
await limitedFetch(`${remotePrefix}/prune`, {})
|
|
},
|
|
requestPackage: requestPackage.bind(null, remotePrefix, limitedFetch),
|
|
saveState: async () => {
|
|
await limitedFetch(`${remotePrefix}/saveState`, {})
|
|
},
|
|
updateConnections: async (prefix: string, opts: {addDependencies: string[], removeDependencies: string[], prune: boolean}) => {
|
|
await limitedFetch(`${remotePrefix}/updateConnections`, {
|
|
opts,
|
|
prefix,
|
|
})
|
|
},
|
|
})
|
|
})
|
|
}
|
|
|
|
function fetch(limit: (fn: () => PromiseLike<object>) => Promise<object>, url: string, body: object): Promise<object> { // tslint:disable-line
|
|
return limit(async () => {
|
|
const response = await got(url, {
|
|
body: JSON.stringify(body),
|
|
headers: {'Content-Type': 'application/json'},
|
|
method: 'POST',
|
|
retries: () => {
|
|
return 100
|
|
},
|
|
})
|
|
return JSON.parse(response.body)
|
|
})
|
|
}
|
|
|
|
function requestPackage (
|
|
remotePrefix: string,
|
|
limitedFetch: (url: string, body: object) => any, // tslint:disable-line
|
|
wantedDependency: WantedDependency,
|
|
options: RequestPackageOptions,
|
|
): Promise<PackageResponse> {
|
|
const msgId = uuid.v4()
|
|
|
|
return limitedFetch(`${remotePrefix}/requestPackage`, {
|
|
msgId,
|
|
options,
|
|
wantedDependency,
|
|
})
|
|
.then((packageResponse: PackageResponse) => {
|
|
const fetchingManifest = limitedFetch(`${remotePrefix}/manifestResponse`, {
|
|
msgId,
|
|
})
|
|
const fetchingFiles = limitedFetch(`${remotePrefix}/packageFilesResponse`, {
|
|
msgId,
|
|
})
|
|
return Object.assign(packageResponse, {
|
|
fetchingFiles,
|
|
fetchingManifest,
|
|
finishing: Promise.all([fetchingManifest, fetchingFiles]).then(() => undefined),
|
|
})
|
|
})
|
|
}
|