-
Notifications
You must be signed in to change notification settings - Fork 16
/
index.ts
161 lines (148 loc) · 5.05 KB
/
index.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
160
161
import { grpc } from "@improbable-eng/grpc-web"
import {
CancelStorageJobRequest,
CancelStorageJobResponse,
ListStorageJobsRequest,
ListStorageJobsResponse,
StorageConfigForJobRequest,
StorageConfigForJobResponse,
StorageJob,
StorageJobRequest,
StorageJobResponse,
StorageJobsSelector,
StorageJobsSummaryRequest,
StorageJobsSummaryResponse,
WatchStorageJobsRequest,
} from "@textile/grpc-powergate-client/dist/powergate/user/v1/user_pb"
import { UserServiceClient } from "@textile/grpc-powergate-client/dist/powergate/user/v1/user_pb_service"
import { Config } from "../types"
import { promise } from "../util"
import { ListOptions, ListSelect } from "./types"
export { ListOptions, ListSelect }
export interface StorageJobs {
/**
* Get the current state of a storage job.
* @param jobId The job id to query.
* @returns The current state of the storage job.
*/
get: (jobId: string) => Promise<StorageJobResponse.AsObject>
/**
* Get the storage config associated with the specified storage job id.
* @param jobId The cid of the desired storage config.
* @returns The storage config associated with the provided job id.
*/
storageConfig: (jobId: string) => Promise<StorageConfigForJobResponse.AsObject>
/**
* Lists StorageJobs according to the provided ListOptions.
* @param opts Optional ListOptions to control the behavior of listing jobs.
* @returns An object containing a list of storage jobs.
*/
list: (opts?: ListOptions) => Promise<ListStorageJobsResponse.AsObject>
/**
* Get a summary of jobs in the user for the specified cids or all cids.
* @param cid An optional cid to get a job summary for, providing no cid means all cids.
* @returns An object containing a summary of jobs.
*/
summary: (cid?: string) => Promise<StorageJobsSummaryResponse.AsObject>
/**
* Listen for job updates for the provided job ids.
* @param handler The callback to receive job updates.
* @param jobs A list of job ids to watch.
* @returns A function that can be used to cancel watching.
*/
watch: (handler: (event: StorageJob.AsObject) => void, ...jobs: string[]) => () => void
/**
* Cancel a job.
* @param jobId The id of the job to cancel.
*/
cancel: (jobId: string) => Promise<CancelStorageJobResponse.AsObject>
}
/**
* @ignore
*/
export const createStorageJobs = (config: Config, getMeta: () => grpc.Metadata): StorageJobs => {
const client = new UserServiceClient(config.host, config)
return {
get: (jobId: string) => {
const req = new StorageJobRequest()
req.setJobId(jobId)
return promise(
(cb) => client.storageJob(req, getMeta(), cb),
(res: StorageJobResponse) => res.toObject(),
)
},
storageConfig: (jobId: string) => {
const req = new StorageConfigForJobRequest()
req.setJobId(jobId)
return promise(
(cb) => client.storageConfigForJob(req, getMeta(), cb),
(res: StorageConfigForJobResponse) => res.toObject(),
)
},
list: (opts?: ListOptions) => {
const req = new ListStorageJobsRequest()
if (opts?.ascending) {
req.setAscending(opts.ascending)
}
if (opts?.cidFilter) {
req.setCidFilter(opts.cidFilter)
}
if (opts?.limit) {
req.setLimit(opts.limit)
}
if (opts?.nextPageToken) {
req.setNextPageToken(opts.nextPageToken)
}
if (opts?.select != undefined) {
switch (opts.select) {
case ListSelect.All:
req.setSelector(StorageJobsSelector.STORAGE_JOBS_SELECTOR_ALL)
break
case ListSelect.Queued:
req.setSelector(StorageJobsSelector.STORAGE_JOBS_SELECTOR_QUEUED)
break
case ListSelect.Executing:
req.setSelector(StorageJobsSelector.STORAGE_JOBS_SELECTOR_EXECUTING)
break
case ListSelect.Final:
req.setSelector(StorageJobsSelector.STORAGE_JOBS_SELECTOR_FINAL)
break
}
}
return promise(
(cb) => client.listStorageJobs(req, getMeta(), cb),
(res: ListStorageJobsResponse) => res.toObject(),
)
},
summary: (cid?: string) => {
const req = new StorageJobsSummaryRequest()
if (cid) {
req.setCid(cid)
}
return promise(
(cb) => client.storageJobsSummary(req, getMeta(), cb),
(res: StorageJobsSummaryResponse) => res.toObject(),
)
},
watch: (handler: (event: StorageJob.AsObject) => void, ...jobs: string[]) => {
const req = new WatchStorageJobsRequest()
req.setJobIdsList(jobs)
const stream = client.watchStorageJobs(req, getMeta())
stream.on("data", (res) => {
const job = res.getStorageJob()?.toObject()
if (job) {
handler(job)
}
})
return stream.cancel
},
cancel: (jobId: string) => {
const req = new CancelStorageJobRequest()
req.setJobId(jobId)
return promise(
(cb) => client.cancelStorageJob(req, getMeta(), cb),
(res: CancelStorageJobResponse) => res.toObject(),
)
},
}
}