-
-
Notifications
You must be signed in to change notification settings - Fork 933
/
connectStoreController.ts
159 lines (147 loc) · 5.03 KB
/
connectStoreController.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import { fetch } from '@pnpm/fetch'
import {
FetchPackageToStoreOptions,
PackageFilesResponse,
PackageResponse,
RequestPackageOptions,
StoreController,
WantedDependency,
} from '@pnpm/store-controller-types'
import { DependencyManifest } from '@pnpm/types'
import pLimit from 'p-limit'
import pShare from 'promise-share'
import { v4 as uuidv4 } from 'uuid'
export type StoreServerController = StoreController & {
stop: () => Promise<void>
}
export async function connectStoreController (
initOpts: {
remotePrefix: string
concurrency?: number
}
): Promise<StoreServerController> {
const remotePrefix = initOpts.remotePrefix
const limitedFetch = limitFetch.bind(null, pLimit(initOpts.concurrency ?? 100))
return new Promise((resolve, reject) => {
resolve({
close: async () => { },
fetchPackage: fetchPackage.bind(null, remotePrefix, limitedFetch),
getFilesIndexFilePath: () => ({ filesIndexFile: '', target: '' }), // NOT IMPLEMENTED
importPackage: async (to: string, opts: {
filesResponse: PackageFilesResponse
force: boolean
}) => {
return limitedFetch(`${remotePrefix}/importPackage`, {
opts,
to,
}) as Promise<{ importMethod: string | undefined, isBuilt: boolean }>
},
prune: async () => {
await limitedFetch(`${remotePrefix}/prune`, {})
},
requestPackage: requestPackage.bind(null, remotePrefix, limitedFetch),
stop: async () => {
await limitedFetch(`${remotePrefix}/stop`, {})
},
upload: async (builtPkgLocation: string, opts: { filesIndexFile: string, sideEffectsCacheKey: string }) => {
await limitedFetch(`${remotePrefix}/upload`, {
builtPkgLocation,
opts,
})
},
})
})
}
function limitFetch<T>(limit: (fn: () => PromiseLike<T>) => Promise<T>, url: string, body: object): Promise<T> { // eslint-disable-line
return limit(async () => {
// TODO: the http://unix: should be also supported by the fetcher
// but it fails with node-fetch-unix as of v2.3.0
if (url.startsWith('http://unix:')) {
url = url.replace('http://unix:', 'unix:')
}
const response = await fetch(url, {
body: JSON.stringify(body),
headers: { 'Content-Type': 'application/json' },
method: 'POST',
retry: {
retries: 100,
},
})
if (!response.ok) {
throw await response.json()
}
const json = await response.json() as any // eslint-disable-line
if (json.error) {
throw json.error
}
return json as T
})
}
async function requestPackage (
remotePrefix: string,
limitedFetch: (url: string, body: object) => any, // eslint-disable-line
wantedDependency: WantedDependency,
options: RequestPackageOptions
): Promise<PackageResponse> {
const msgId = uuidv4()
return limitedFetch(`${remotePrefix}/requestPackage`, {
msgId,
options,
wantedDependency,
})
.then((packageResponseBody: object) => {
const fetchingBundledManifest = !packageResponseBody['fetchingBundledManifestInProgress'] // eslint-disable-line
? undefined
: limitedFetch(`${remotePrefix}/rawManifestResponse`, {
msgId,
})
delete packageResponseBody['fetchingBundledManifestInProgress'] // eslint-disable-line
if (options.skipFetch) {
return {
body: packageResponseBody,
bundledManifest: fetchingBundledManifest && pShare(fetchingBundledManifest),
}
}
const fetchingFiles = limitedFetch(`${remotePrefix}/packageFilesResponse`, {
msgId,
})
return {
body: packageResponseBody,
bundledManifest: fetchingBundledManifest && pShare(fetchingBundledManifest),
files: pShare(fetchingFiles),
finishing: pShare(Promise.all([fetchingBundledManifest, fetchingFiles]).then(() => undefined)),
}
})
}
function fetchPackage (
remotePrefix: string,
limitedFetch: (url: string, body: object) => any, // eslint-disable-line
options: FetchPackageToStoreOptions
): {
bundledManifest?: () => Promise<DependencyManifest>
files: () => Promise<PackageFilesResponse>
filesIndexFile: string
finishing: () => Promise<void>
inStoreLocation: string
} {
const msgId = uuidv4()
return limitedFetch(`${remotePrefix}/fetchPackage`, {
msgId,
options,
})
.then((fetchResponseBody: object & { filesIndexFile: string, inStoreLocation: string }) => {
const fetchingBundledManifest = options.fetchRawManifest
? limitedFetch(`${remotePrefix}/rawManifestResponse`, { msgId })
: undefined
const fetchingFiles = limitedFetch(`${remotePrefix}/packageFilesResponse`, {
msgId,
})
return {
bundledManifest: fetchingBundledManifest && pShare(fetchingBundledManifest),
files: pShare(fetchingFiles),
filesIndexFile: fetchResponseBody.filesIndexFile,
finishing: pShare(Promise.all([fetchingBundledManifest, fetchingFiles]).then(() => undefined)),
inStoreLocation: fetchResponseBody.inStoreLocation,
}
})
}