MIRROR: javascript for 馃悳's, a tiny runtime with big ambitions
1#ifndef PKG_H
2#define PKG_H
3
4#include <stdint.h>
5#include <stddef.h>
6#include <stdbool.h>
7
8typedef enum {
9 PKG_OK = 0,
10 PKG_OUT_OF_MEMORY = -1,
11 PKG_INVALID_LOCKFILE = -2,
12 PKG_IO_ERROR = -3,
13 PKG_NETWORK_ERROR = -4,
14 PKG_CACHE_ERROR = -5,
15 PKG_EXTRACT_ERROR = -6,
16 PKG_RESOLVE_ERROR = -7,
17 PKG_INVALID_ARGUMENT = -8,
18 PKG_NOT_FOUND = -9,
19 PKG_INTEGRITY_MISMATCH = -10,
20} pkg_error_t;
21
22typedef enum {
23 PKG_PHASE_RESOLVING = 0,
24 PKG_PHASE_FETCHING = 1,
25 PKG_PHASE_EXTRACTING = 2,
26 PKG_PHASE_LINKING = 3,
27 PKG_PHASE_CACHING = 4,
28 PKG_PHASE_POSTINSTALL = 5,
29} pkg_phase_t;
30
31typedef void (*pkg_progress_cb)(
32 void *user_data,
33 pkg_phase_t phase,
34 uint32_t current,
35 uint32_t total,
36 const char *message
37);
38
39typedef struct {
40 const char *cache_dir;
41 const char *registry_url;
42 uint32_t max_connections;
43 pkg_progress_cb progress_callback;
44 void *user_data;
45 bool verbose;
46} pkg_options_t;
47
48typedef struct pkg_context pkg_context_t;
49
50const char *pkg_error_string(const pkg_context_t *ctx);
51
52pkg_context_t *pkg_init(const pkg_options_t *options);
53
54pkg_error_t pkg_install(
55 pkg_context_t *ctx,
56 const char *package_json_path,
57 const char *lockfile_path,
58 const char *node_modules_path
59);
60
61pkg_error_t pkg_resolve_and_install(
62 pkg_context_t *ctx,
63 const char *package_json_path,
64 const char *lockfile_path,
65 const char *node_modules_path
66);
67
68pkg_error_t pkg_add(
69 pkg_context_t *ctx,
70 const char *package_json_path,
71 const char *package_spec,
72 bool dev
73);
74
75pkg_error_t pkg_add_many(
76 pkg_context_t *ctx,
77 const char *package_json_path,
78 const char *const *package_specs,
79 uint32_t count,
80 bool dev
81);
82
83pkg_error_t pkg_remove(
84 pkg_context_t *ctx,
85 const char *package_json_path,
86 const char *package_name
87);
88
89void pkg_free(pkg_context_t *ctx);
90void pkg_cache_sync(pkg_context_t *ctx);
91
92typedef struct {
93 uint64_t total_size;
94 uint64_t db_size;
95 uint32_t package_count;
96} pkg_cache_stats_t;
97
98pkg_error_t pkg_cache_stats(pkg_context_t *ctx, pkg_cache_stats_t *out);
99
100int32_t pkg_cache_prune(pkg_context_t *ctx, uint32_t max_age_days);
101
102typedef struct {
103 uint32_t package_count;
104 uint32_t cache_hits;
105 uint32_t cache_misses;
106 uint32_t files_linked;
107 uint32_t files_copied;
108 uint32_t packages_installed;
109 uint32_t packages_skipped;
110 uint64_t elapsed_ms;
111} pkg_install_result_t;
112
113typedef struct {
114 const char *name;
115 const char *version;
116 bool direct;
117} pkg_added_package_t;
118
119typedef struct {
120 const char *name;
121 const char *script;
122} pkg_lifecycle_script_t;
123
124uint32_t pkg_get_added_count(const pkg_context_t *ctx);
125uint32_t pkg_count_installed(const char *node_modules_path);
126uint32_t pkg_get_lifecycle_script_count(const pkg_context_t *ctx);
127
128pkg_error_t pkg_discover_lifecycle_scripts(
129 pkg_context_t *ctx,
130 const char *node_modules_path
131);
132
133pkg_error_t pkg_get_lifecycle_script(
134 const pkg_context_t *ctx,
135 uint32_t index,
136 pkg_lifecycle_script_t *out
137);
138
139pkg_error_t pkg_run_postinstall(
140 pkg_context_t *ctx,
141 const char *node_modules_path,
142 const char **package_names,
143 uint32_t count
144);
145
146pkg_error_t pkg_add_trusted_dependencies(
147 const char *package_json_path,
148 const char **package_names,
149 uint32_t count
150);
151
152pkg_error_t pkg_get_install_result(
153 pkg_context_t *ctx,
154 pkg_install_result_t *out
155);
156
157pkg_error_t pkg_get_added_package(
158 const pkg_context_t *ctx,
159 uint32_t index,
160 pkg_added_package_t *out
161);
162
163int pkg_get_latest_available_version(
164 pkg_context_t *ctx,
165 const char *package_name,
166 const char *installed_version,
167 char *out_version,
168 size_t out_version_len
169);
170
171int pkg_get_bin_path(
172 const char *node_modules_path,
173 const char *bin_name,
174 char *out_path,
175 size_t out_path_len
176);
177
178typedef void (*pkg_bin_callback)(
179 const char *name,
180 void *user_data
181);
182
183int pkg_list_bins(
184 const char *node_modules_path,
185 pkg_bin_callback callback,
186 void *user_data
187);
188
189int pkg_list_package_bins(
190 const char *node_modules_path,
191 const char *package_name,
192 pkg_bin_callback callback,
193 void *user_data
194);
195
196int pkg_get_script(
197 const char *package_json_path,
198 const char *script_name,
199 char *out_script,
200 size_t out_script_len
201);
202
203typedef struct {
204 int exit_code;
205 int signal;
206} pkg_script_result_t;
207
208pkg_error_t pkg_run_script(
209 const char *package_json_path,
210 const char *script_name,
211 const char *node_modules_path,
212 const char *extra_args,
213 pkg_script_result_t *result
214);
215
216typedef void (*pkg_script_callback)(
217 const char *name,
218 const char *command,
219 void *user_data
220);
221
222int pkg_list_scripts(
223 const char *package_json_path,
224 pkg_script_callback callback,
225 void *user_data
226);
227
228typedef struct {
229 uint8_t peer: 1;
230 uint8_t dev: 1;
231 uint8_t optional: 1;
232 uint8_t direct: 1;
233 uint8_t _reserved: 4;
234} pkg_dep_type_t;
235
236typedef void (*pkg_why_callback)(
237 const char *name,
238 const char *version,
239 const char *constraint,
240 pkg_dep_type_t dep_type,
241 void *user_data
242);
243
244typedef struct {
245 char target_version[64];
246 bool found;
247 bool is_peer;
248 bool is_dev;
249 bool is_direct;
250} pkg_why_info_t;
251
252int pkg_why_info(
253 const char *lockfile_path,
254 const char *package_name,
255 pkg_why_info_t *out
256);
257
258int pkg_why(
259 const char *lockfile_path,
260 const char *package_name,
261 pkg_why_callback callback,
262 void *user_data
263);
264
265typedef struct {
266 const char *name;
267 const char *version;
268 const char *description;
269 const char *license;
270 const char *homepage;
271 const char *tarball;
272 const char *shasum;
273 const char *integrity;
274 const char *keywords;
275 const char *published;
276 uint32_t dep_count;
277 uint32_t version_count;
278 uint64_t unpacked_size;
279} pkg_info_t;
280
281typedef struct {
282 const char *tag;
283 const char *version;
284} pkg_dist_tag_t;
285
286typedef struct {
287 const char *name;
288 const char *email;
289} pkg_maintainer_t;
290
291pkg_error_t pkg_info(
292 pkg_context_t *ctx,
293 const char *package_spec,
294 pkg_info_t *out
295);
296
297uint32_t pkg_info_dist_tag_count(const pkg_context_t *ctx);
298pkg_error_t pkg_info_get_dist_tag(const pkg_context_t *ctx, uint32_t index, pkg_dist_tag_t *out);
299
300uint32_t pkg_info_maintainer_count(const pkg_context_t *ctx);
301pkg_error_t pkg_info_get_maintainer(const pkg_context_t *ctx, uint32_t index, pkg_maintainer_t *out);
302
303typedef struct {
304 const char *name;
305 const char *version;
306} pkg_dependency_t;
307
308uint32_t pkg_info_dependency_count(const pkg_context_t *ctx);
309pkg_error_t pkg_info_get_dependency(const pkg_context_t *ctx, uint32_t index, pkg_dependency_t *out);
310
311pkg_error_t pkg_exec_temp(
312 pkg_context_t *ctx,
313 const char *package_spec,
314 char *out_bin_path,
315 size_t out_bin_path_len
316);
317
318pkg_error_t pkg_add_global(
319 pkg_context_t *ctx,
320 const char *package_spec
321);
322
323pkg_error_t pkg_add_global_many(
324 pkg_context_t *ctx,
325 const char *const *package_specs,
326 uint32_t count
327);
328
329pkg_error_t pkg_remove_global(
330 pkg_context_t *ctx,
331 const char *package_name
332);
333
334typedef void (*pkg_global_list_callback)(
335 const char *name,
336 const char *version,
337 void *user_data
338);
339
340uint32_t pkg_count_global(pkg_context_t *ctx);
341uint32_t pkg_count_local(pkg_context_t *ctx);
342
343pkg_error_t pkg_list_global(
344 pkg_context_t *ctx,
345 pkg_global_list_callback callback,
346 void *user_data
347);
348
349pkg_error_t pkg_list_local(
350 pkg_context_t *ctx,
351 pkg_global_list_callback callback,
352 void *user_data
353);
354
355#endif