@yz-xingtu/ops-cli 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +1274 -0
- package/package.json +36 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,1274 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
// src/index.ts
|
|
4
|
+
import { Command } from "commander";
|
|
5
|
+
import axios from "axios";
|
|
6
|
+
|
|
7
|
+
// ../shared/src/logger.ts
|
|
8
|
+
import pino from "pino";
|
|
9
|
+
var isDev = process.env.NODE_ENV !== "production";
|
|
10
|
+
var isNextRuntime = Boolean(process.env.NEXT_RUNTIME);
|
|
11
|
+
var baseOptions = {
|
|
12
|
+
level: process.env.LOG_LEVEL ?? (isDev ? "debug" : "info"),
|
|
13
|
+
base: {
|
|
14
|
+
env: process.env.NODE_ENV ?? "development"
|
|
15
|
+
},
|
|
16
|
+
redact: {
|
|
17
|
+
paths: [
|
|
18
|
+
"*.password",
|
|
19
|
+
"*.hashedPassword",
|
|
20
|
+
"*.cookie",
|
|
21
|
+
"*.encryptedCookie",
|
|
22
|
+
"*.encryptedApiKey",
|
|
23
|
+
"*.encryptedAntiContent",
|
|
24
|
+
"headers.authorization",
|
|
25
|
+
"headers.cookie"
|
|
26
|
+
],
|
|
27
|
+
censor: "[REDACTED]"
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
var transport = isDev && !isNextRuntime ? {
|
|
31
|
+
target: "pino-pretty",
|
|
32
|
+
options: { colorize: true, translateTime: "SYS:standard", singleLine: false }
|
|
33
|
+
} : void 0;
|
|
34
|
+
var rootLogger = pino({
|
|
35
|
+
...baseOptions,
|
|
36
|
+
...transport ? { transport } : {}
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
// ../shared/src/rbac.ts
|
|
40
|
+
var RESOURCES = {
|
|
41
|
+
TENANT: "tenant",
|
|
42
|
+
USER: "user",
|
|
43
|
+
ROLE: "role",
|
|
44
|
+
SHOP: "shop",
|
|
45
|
+
PDD_ACCOUNT: "pdd_account",
|
|
46
|
+
WAREHOUSE: "warehouse",
|
|
47
|
+
WAREHOUSE_GROUP: "warehouse_group",
|
|
48
|
+
AREA: "area",
|
|
49
|
+
PRODUCT: "product",
|
|
50
|
+
PRODUCT_COST: "product_cost",
|
|
51
|
+
WAREHOUSE_MAPPING: "warehouse_mapping",
|
|
52
|
+
SALES: "sales",
|
|
53
|
+
INVENTORY: "inventory",
|
|
54
|
+
STORAGE_FEE: "storage_fee",
|
|
55
|
+
PROFIT: "profit",
|
|
56
|
+
JOB: "job",
|
|
57
|
+
JOB_RUN: "job_run",
|
|
58
|
+
ALERT: "alert",
|
|
59
|
+
NOTIFICATION_CHANNEL: "notification_channel",
|
|
60
|
+
NOTIFICATION_RULE: "notification_rule",
|
|
61
|
+
EXPORT: "export",
|
|
62
|
+
AI_CONFIG: "ai_config",
|
|
63
|
+
PROMPT_TEMPLATE: "prompt_template",
|
|
64
|
+
AUDIT_LOG: "audit_log",
|
|
65
|
+
TENANT_CONFIG: "tenant_config"
|
|
66
|
+
};
|
|
67
|
+
var ACTIONS = {
|
|
68
|
+
CREATE: "create",
|
|
69
|
+
READ: "read",
|
|
70
|
+
UPDATE: "update",
|
|
71
|
+
DELETE: "delete",
|
|
72
|
+
RUN: "run",
|
|
73
|
+
EXPORT: "export",
|
|
74
|
+
MANAGE: "manage"
|
|
75
|
+
};
|
|
76
|
+
function p(resource, action) {
|
|
77
|
+
return `${resource}:${action}`;
|
|
78
|
+
}
|
|
79
|
+
var ALL_READ = Object.values(RESOURCES).map(
|
|
80
|
+
(r) => p(r, ACTIONS.READ)
|
|
81
|
+
);
|
|
82
|
+
var ALL_WRITE = Object.values(RESOURCES).flatMap((r) => [
|
|
83
|
+
p(r, ACTIONS.CREATE),
|
|
84
|
+
p(r, ACTIONS.UPDATE),
|
|
85
|
+
p(r, ACTIONS.DELETE)
|
|
86
|
+
]);
|
|
87
|
+
var OPERATOR_PERMS = [
|
|
88
|
+
...ALL_READ,
|
|
89
|
+
p(RESOURCES.SHOP, ACTIONS.CREATE),
|
|
90
|
+
p(RESOURCES.SHOP, ACTIONS.UPDATE),
|
|
91
|
+
p(RESOURCES.PDD_ACCOUNT, ACTIONS.CREATE),
|
|
92
|
+
p(RESOURCES.PDD_ACCOUNT, ACTIONS.UPDATE),
|
|
93
|
+
p(RESOURCES.WAREHOUSE, ACTIONS.CREATE),
|
|
94
|
+
p(RESOURCES.WAREHOUSE, ACTIONS.UPDATE),
|
|
95
|
+
p(RESOURCES.PRODUCT, ACTIONS.CREATE),
|
|
96
|
+
p(RESOURCES.PRODUCT, ACTIONS.UPDATE),
|
|
97
|
+
p(RESOURCES.PRODUCT_COST, ACTIONS.CREATE),
|
|
98
|
+
p(RESOURCES.PRODUCT_COST, ACTIONS.UPDATE),
|
|
99
|
+
p(RESOURCES.JOB, ACTIONS.RUN),
|
|
100
|
+
p(RESOURCES.EXPORT, ACTIONS.CREATE)
|
|
101
|
+
];
|
|
102
|
+
var SYSTEM_ROLES = {
|
|
103
|
+
OWNER: {
|
|
104
|
+
code: "owner",
|
|
105
|
+
name: "Owner",
|
|
106
|
+
description: "\u79DF\u6237\u6240\u6709\u8005\uFF0C\u5168\u90E8\u6743\u9650\u3002",
|
|
107
|
+
permissions: [
|
|
108
|
+
...ALL_READ,
|
|
109
|
+
...ALL_WRITE,
|
|
110
|
+
p(RESOURCES.JOB, ACTIONS.RUN),
|
|
111
|
+
p(RESOURCES.EXPORT, ACTIONS.CREATE),
|
|
112
|
+
p(RESOURCES.TENANT, ACTIONS.MANAGE),
|
|
113
|
+
p(RESOURCES.USER, ACTIONS.MANAGE),
|
|
114
|
+
p(RESOURCES.ROLE, ACTIONS.MANAGE)
|
|
115
|
+
]
|
|
116
|
+
},
|
|
117
|
+
ADMIN: {
|
|
118
|
+
code: "admin",
|
|
119
|
+
name: "Admin",
|
|
120
|
+
description: "\u7BA1\u7406\u5458\uFF0C\u53EF\u7BA1\u7406\u7528\u6237\u4E0E\u914D\u7F6E\u3002",
|
|
121
|
+
permissions: [
|
|
122
|
+
...ALL_READ,
|
|
123
|
+
...ALL_WRITE,
|
|
124
|
+
p(RESOURCES.JOB, ACTIONS.RUN),
|
|
125
|
+
p(RESOURCES.EXPORT, ACTIONS.CREATE),
|
|
126
|
+
p(RESOURCES.USER, ACTIONS.MANAGE)
|
|
127
|
+
]
|
|
128
|
+
},
|
|
129
|
+
OPERATOR: {
|
|
130
|
+
code: "operator",
|
|
131
|
+
name: "Operator",
|
|
132
|
+
description: "\u8FD0\u8425\uFF0C\u53EF\u7BA1\u7406\u5E97\u94FA/\u91C7\u96C6\u4E0E\u5BFC\u51FA\u3002",
|
|
133
|
+
permissions: OPERATOR_PERMS
|
|
134
|
+
},
|
|
135
|
+
VIEWER: {
|
|
136
|
+
code: "viewer",
|
|
137
|
+
name: "Viewer",
|
|
138
|
+
description: "\u53EA\u8BFB\uFF0C\u4EC5\u67E5\u770B\u6570\u636E\u3002",
|
|
139
|
+
permissions: ALL_READ
|
|
140
|
+
}
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
// ../../node_modules/jose/dist/node/esm/runtime/base64url.js
|
|
144
|
+
import { Buffer as Buffer2 } from "buffer";
|
|
145
|
+
|
|
146
|
+
// ../../node_modules/jose/dist/node/esm/lib/buffer_utils.js
|
|
147
|
+
var encoder = new TextEncoder();
|
|
148
|
+
var decoder = new TextDecoder();
|
|
149
|
+
var MAX_INT32 = 2 ** 32;
|
|
150
|
+
function concat(...buffers) {
|
|
151
|
+
const size = buffers.reduce((acc, { length }) => acc + length, 0);
|
|
152
|
+
const buf = new Uint8Array(size);
|
|
153
|
+
let i = 0;
|
|
154
|
+
for (const buffer of buffers) {
|
|
155
|
+
buf.set(buffer, i);
|
|
156
|
+
i += buffer.length;
|
|
157
|
+
}
|
|
158
|
+
return buf;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
// ../../node_modules/jose/dist/node/esm/runtime/base64url.js
|
|
162
|
+
var encode = (input) => Buffer2.from(input).toString("base64url");
|
|
163
|
+
|
|
164
|
+
// ../../node_modules/jose/dist/node/esm/util/errors.js
|
|
165
|
+
var JOSEError = class extends Error {
|
|
166
|
+
static code = "ERR_JOSE_GENERIC";
|
|
167
|
+
code = "ERR_JOSE_GENERIC";
|
|
168
|
+
constructor(message2, options) {
|
|
169
|
+
super(message2, options);
|
|
170
|
+
this.name = this.constructor.name;
|
|
171
|
+
Error.captureStackTrace?.(this, this.constructor);
|
|
172
|
+
}
|
|
173
|
+
};
|
|
174
|
+
var JOSENotSupported = class extends JOSEError {
|
|
175
|
+
static code = "ERR_JOSE_NOT_SUPPORTED";
|
|
176
|
+
code = "ERR_JOSE_NOT_SUPPORTED";
|
|
177
|
+
};
|
|
178
|
+
var JWSInvalid = class extends JOSEError {
|
|
179
|
+
static code = "ERR_JWS_INVALID";
|
|
180
|
+
code = "ERR_JWS_INVALID";
|
|
181
|
+
};
|
|
182
|
+
var JWTInvalid = class extends JOSEError {
|
|
183
|
+
static code = "ERR_JWT_INVALID";
|
|
184
|
+
code = "ERR_JWT_INVALID";
|
|
185
|
+
};
|
|
186
|
+
|
|
187
|
+
// ../../node_modules/jose/dist/node/esm/runtime/is_key_object.js
|
|
188
|
+
import * as util from "util";
|
|
189
|
+
var is_key_object_default = (obj) => util.types.isKeyObject(obj);
|
|
190
|
+
|
|
191
|
+
// ../../node_modules/jose/dist/node/esm/runtime/webcrypto.js
|
|
192
|
+
import * as crypto from "crypto";
|
|
193
|
+
import * as util2 from "util";
|
|
194
|
+
var webcrypto2 = crypto.webcrypto;
|
|
195
|
+
var webcrypto_default = webcrypto2;
|
|
196
|
+
var isCryptoKey = (key) => util2.types.isCryptoKey(key);
|
|
197
|
+
|
|
198
|
+
// ../../node_modules/jose/dist/node/esm/lib/crypto_key.js
|
|
199
|
+
function unusable(name, prop = "algorithm.name") {
|
|
200
|
+
return new TypeError(`CryptoKey does not support this operation, its ${prop} must be ${name}`);
|
|
201
|
+
}
|
|
202
|
+
function isAlgorithm(algorithm, name) {
|
|
203
|
+
return algorithm.name === name;
|
|
204
|
+
}
|
|
205
|
+
function getHashLength(hash) {
|
|
206
|
+
return parseInt(hash.name.slice(4), 10);
|
|
207
|
+
}
|
|
208
|
+
function getNamedCurve(alg) {
|
|
209
|
+
switch (alg) {
|
|
210
|
+
case "ES256":
|
|
211
|
+
return "P-256";
|
|
212
|
+
case "ES384":
|
|
213
|
+
return "P-384";
|
|
214
|
+
case "ES512":
|
|
215
|
+
return "P-521";
|
|
216
|
+
default:
|
|
217
|
+
throw new Error("unreachable");
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
function checkUsage(key, usages) {
|
|
221
|
+
if (usages.length && !usages.some((expected) => key.usages.includes(expected))) {
|
|
222
|
+
let msg = "CryptoKey does not support this operation, its usages must include ";
|
|
223
|
+
if (usages.length > 2) {
|
|
224
|
+
const last = usages.pop();
|
|
225
|
+
msg += `one of ${usages.join(", ")}, or ${last}.`;
|
|
226
|
+
} else if (usages.length === 2) {
|
|
227
|
+
msg += `one of ${usages[0]} or ${usages[1]}.`;
|
|
228
|
+
} else {
|
|
229
|
+
msg += `${usages[0]}.`;
|
|
230
|
+
}
|
|
231
|
+
throw new TypeError(msg);
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
function checkSigCryptoKey(key, alg, ...usages) {
|
|
235
|
+
switch (alg) {
|
|
236
|
+
case "HS256":
|
|
237
|
+
case "HS384":
|
|
238
|
+
case "HS512": {
|
|
239
|
+
if (!isAlgorithm(key.algorithm, "HMAC"))
|
|
240
|
+
throw unusable("HMAC");
|
|
241
|
+
const expected = parseInt(alg.slice(2), 10);
|
|
242
|
+
const actual = getHashLength(key.algorithm.hash);
|
|
243
|
+
if (actual !== expected)
|
|
244
|
+
throw unusable(`SHA-${expected}`, "algorithm.hash");
|
|
245
|
+
break;
|
|
246
|
+
}
|
|
247
|
+
case "RS256":
|
|
248
|
+
case "RS384":
|
|
249
|
+
case "RS512": {
|
|
250
|
+
if (!isAlgorithm(key.algorithm, "RSASSA-PKCS1-v1_5"))
|
|
251
|
+
throw unusable("RSASSA-PKCS1-v1_5");
|
|
252
|
+
const expected = parseInt(alg.slice(2), 10);
|
|
253
|
+
const actual = getHashLength(key.algorithm.hash);
|
|
254
|
+
if (actual !== expected)
|
|
255
|
+
throw unusable(`SHA-${expected}`, "algorithm.hash");
|
|
256
|
+
break;
|
|
257
|
+
}
|
|
258
|
+
case "PS256":
|
|
259
|
+
case "PS384":
|
|
260
|
+
case "PS512": {
|
|
261
|
+
if (!isAlgorithm(key.algorithm, "RSA-PSS"))
|
|
262
|
+
throw unusable("RSA-PSS");
|
|
263
|
+
const expected = parseInt(alg.slice(2), 10);
|
|
264
|
+
const actual = getHashLength(key.algorithm.hash);
|
|
265
|
+
if (actual !== expected)
|
|
266
|
+
throw unusable(`SHA-${expected}`, "algorithm.hash");
|
|
267
|
+
break;
|
|
268
|
+
}
|
|
269
|
+
case "EdDSA": {
|
|
270
|
+
if (key.algorithm.name !== "Ed25519" && key.algorithm.name !== "Ed448") {
|
|
271
|
+
throw unusable("Ed25519 or Ed448");
|
|
272
|
+
}
|
|
273
|
+
break;
|
|
274
|
+
}
|
|
275
|
+
case "Ed25519": {
|
|
276
|
+
if (!isAlgorithm(key.algorithm, "Ed25519"))
|
|
277
|
+
throw unusable("Ed25519");
|
|
278
|
+
break;
|
|
279
|
+
}
|
|
280
|
+
case "ES256":
|
|
281
|
+
case "ES384":
|
|
282
|
+
case "ES512": {
|
|
283
|
+
if (!isAlgorithm(key.algorithm, "ECDSA"))
|
|
284
|
+
throw unusable("ECDSA");
|
|
285
|
+
const expected = getNamedCurve(alg);
|
|
286
|
+
const actual = key.algorithm.namedCurve;
|
|
287
|
+
if (actual !== expected)
|
|
288
|
+
throw unusable(expected, "algorithm.namedCurve");
|
|
289
|
+
break;
|
|
290
|
+
}
|
|
291
|
+
default:
|
|
292
|
+
throw new TypeError("CryptoKey does not support this operation");
|
|
293
|
+
}
|
|
294
|
+
checkUsage(key, usages);
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
// ../../node_modules/jose/dist/node/esm/lib/invalid_key_input.js
|
|
298
|
+
function message(msg, actual, ...types4) {
|
|
299
|
+
types4 = types4.filter(Boolean);
|
|
300
|
+
if (types4.length > 2) {
|
|
301
|
+
const last = types4.pop();
|
|
302
|
+
msg += `one of type ${types4.join(", ")}, or ${last}.`;
|
|
303
|
+
} else if (types4.length === 2) {
|
|
304
|
+
msg += `one of type ${types4[0]} or ${types4[1]}.`;
|
|
305
|
+
} else {
|
|
306
|
+
msg += `of type ${types4[0]}.`;
|
|
307
|
+
}
|
|
308
|
+
if (actual == null) {
|
|
309
|
+
msg += ` Received ${actual}`;
|
|
310
|
+
} else if (typeof actual === "function" && actual.name) {
|
|
311
|
+
msg += ` Received function ${actual.name}`;
|
|
312
|
+
} else if (typeof actual === "object" && actual != null) {
|
|
313
|
+
if (actual.constructor?.name) {
|
|
314
|
+
msg += ` Received an instance of ${actual.constructor.name}`;
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
return msg;
|
|
318
|
+
}
|
|
319
|
+
var invalid_key_input_default = (actual, ...types4) => {
|
|
320
|
+
return message("Key must be ", actual, ...types4);
|
|
321
|
+
};
|
|
322
|
+
function withAlg(alg, actual, ...types4) {
|
|
323
|
+
return message(`Key for the ${alg} algorithm must be `, actual, ...types4);
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
// ../../node_modules/jose/dist/node/esm/runtime/is_key_like.js
|
|
327
|
+
var is_key_like_default = (key) => is_key_object_default(key) || isCryptoKey(key);
|
|
328
|
+
var types3 = ["KeyObject"];
|
|
329
|
+
if (globalThis.CryptoKey || webcrypto_default?.CryptoKey) {
|
|
330
|
+
types3.push("CryptoKey");
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
// ../../node_modules/jose/dist/node/esm/lib/is_disjoint.js
|
|
334
|
+
var isDisjoint = (...headers) => {
|
|
335
|
+
const sources = headers.filter(Boolean);
|
|
336
|
+
if (sources.length === 0 || sources.length === 1) {
|
|
337
|
+
return true;
|
|
338
|
+
}
|
|
339
|
+
let acc;
|
|
340
|
+
for (const header of sources) {
|
|
341
|
+
const parameters = Object.keys(header);
|
|
342
|
+
if (!acc || acc.size === 0) {
|
|
343
|
+
acc = new Set(parameters);
|
|
344
|
+
continue;
|
|
345
|
+
}
|
|
346
|
+
for (const parameter of parameters) {
|
|
347
|
+
if (acc.has(parameter)) {
|
|
348
|
+
return false;
|
|
349
|
+
}
|
|
350
|
+
acc.add(parameter);
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
return true;
|
|
354
|
+
};
|
|
355
|
+
var is_disjoint_default = isDisjoint;
|
|
356
|
+
|
|
357
|
+
// ../../node_modules/jose/dist/node/esm/lib/is_object.js
|
|
358
|
+
function isObjectLike(value) {
|
|
359
|
+
return typeof value === "object" && value !== null;
|
|
360
|
+
}
|
|
361
|
+
function isObject(input) {
|
|
362
|
+
if (!isObjectLike(input) || Object.prototype.toString.call(input) !== "[object Object]") {
|
|
363
|
+
return false;
|
|
364
|
+
}
|
|
365
|
+
if (Object.getPrototypeOf(input) === null) {
|
|
366
|
+
return true;
|
|
367
|
+
}
|
|
368
|
+
let proto = input;
|
|
369
|
+
while (Object.getPrototypeOf(proto) !== null) {
|
|
370
|
+
proto = Object.getPrototypeOf(proto);
|
|
371
|
+
}
|
|
372
|
+
return Object.getPrototypeOf(input) === proto;
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
// ../../node_modules/jose/dist/node/esm/runtime/get_named_curve.js
|
|
376
|
+
import { KeyObject } from "crypto";
|
|
377
|
+
|
|
378
|
+
// ../../node_modules/jose/dist/node/esm/lib/is_jwk.js
|
|
379
|
+
function isJWK(key) {
|
|
380
|
+
return isObject(key) && typeof key.kty === "string";
|
|
381
|
+
}
|
|
382
|
+
function isPrivateJWK(key) {
|
|
383
|
+
return key.kty !== "oct" && typeof key.d === "string";
|
|
384
|
+
}
|
|
385
|
+
function isPublicJWK(key) {
|
|
386
|
+
return key.kty !== "oct" && typeof key.d === "undefined";
|
|
387
|
+
}
|
|
388
|
+
function isSecretJWK(key) {
|
|
389
|
+
return isJWK(key) && key.kty === "oct" && typeof key.k === "string";
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
// ../../node_modules/jose/dist/node/esm/runtime/get_named_curve.js
|
|
393
|
+
var namedCurveToJOSE = (namedCurve) => {
|
|
394
|
+
switch (namedCurve) {
|
|
395
|
+
case "prime256v1":
|
|
396
|
+
return "P-256";
|
|
397
|
+
case "secp384r1":
|
|
398
|
+
return "P-384";
|
|
399
|
+
case "secp521r1":
|
|
400
|
+
return "P-521";
|
|
401
|
+
case "secp256k1":
|
|
402
|
+
return "secp256k1";
|
|
403
|
+
default:
|
|
404
|
+
throw new JOSENotSupported("Unsupported key curve for this operation");
|
|
405
|
+
}
|
|
406
|
+
};
|
|
407
|
+
var getNamedCurve2 = (kee, raw) => {
|
|
408
|
+
let key;
|
|
409
|
+
if (isCryptoKey(kee)) {
|
|
410
|
+
key = KeyObject.from(kee);
|
|
411
|
+
} else if (is_key_object_default(kee)) {
|
|
412
|
+
key = kee;
|
|
413
|
+
} else if (isJWK(kee)) {
|
|
414
|
+
return kee.crv;
|
|
415
|
+
} else {
|
|
416
|
+
throw new TypeError(invalid_key_input_default(kee, ...types3));
|
|
417
|
+
}
|
|
418
|
+
if (key.type === "secret") {
|
|
419
|
+
throw new TypeError('only "private" or "public" type keys can be used for this operation');
|
|
420
|
+
}
|
|
421
|
+
switch (key.asymmetricKeyType) {
|
|
422
|
+
case "ed25519":
|
|
423
|
+
case "ed448":
|
|
424
|
+
return `Ed${key.asymmetricKeyType.slice(2)}`;
|
|
425
|
+
case "x25519":
|
|
426
|
+
case "x448":
|
|
427
|
+
return `X${key.asymmetricKeyType.slice(1)}`;
|
|
428
|
+
case "ec": {
|
|
429
|
+
const namedCurve = key.asymmetricKeyDetails.namedCurve;
|
|
430
|
+
if (raw) {
|
|
431
|
+
return namedCurve;
|
|
432
|
+
}
|
|
433
|
+
return namedCurveToJOSE(namedCurve);
|
|
434
|
+
}
|
|
435
|
+
default:
|
|
436
|
+
throw new TypeError("Invalid asymmetric key type for this operation");
|
|
437
|
+
}
|
|
438
|
+
};
|
|
439
|
+
var get_named_curve_default = getNamedCurve2;
|
|
440
|
+
|
|
441
|
+
// ../../node_modules/jose/dist/node/esm/runtime/check_key_length.js
|
|
442
|
+
import { KeyObject as KeyObject2 } from "crypto";
|
|
443
|
+
var check_key_length_default = (key, alg) => {
|
|
444
|
+
let modulusLength;
|
|
445
|
+
try {
|
|
446
|
+
if (key instanceof KeyObject2) {
|
|
447
|
+
modulusLength = key.asymmetricKeyDetails?.modulusLength;
|
|
448
|
+
} else {
|
|
449
|
+
modulusLength = Buffer.from(key.n, "base64url").byteLength << 3;
|
|
450
|
+
}
|
|
451
|
+
} catch {
|
|
452
|
+
}
|
|
453
|
+
if (typeof modulusLength !== "number" || modulusLength < 2048) {
|
|
454
|
+
throw new TypeError(`${alg} requires key modulusLength to be 2048 bits or larger`);
|
|
455
|
+
}
|
|
456
|
+
};
|
|
457
|
+
|
|
458
|
+
// ../../node_modules/jose/dist/node/esm/lib/check_key_type.js
|
|
459
|
+
var tag = (key) => key?.[Symbol.toStringTag];
|
|
460
|
+
var jwkMatchesOp = (alg, key, usage) => {
|
|
461
|
+
if (key.use !== void 0 && key.use !== "sig") {
|
|
462
|
+
throw new TypeError("Invalid key for this operation, when present its use must be sig");
|
|
463
|
+
}
|
|
464
|
+
if (key.key_ops !== void 0 && key.key_ops.includes?.(usage) !== true) {
|
|
465
|
+
throw new TypeError(`Invalid key for this operation, when present its key_ops must include ${usage}`);
|
|
466
|
+
}
|
|
467
|
+
if (key.alg !== void 0 && key.alg !== alg) {
|
|
468
|
+
throw new TypeError(`Invalid key for this operation, when present its alg must be ${alg}`);
|
|
469
|
+
}
|
|
470
|
+
return true;
|
|
471
|
+
};
|
|
472
|
+
var symmetricTypeCheck = (alg, key, usage, allowJwk) => {
|
|
473
|
+
if (key instanceof Uint8Array)
|
|
474
|
+
return;
|
|
475
|
+
if (allowJwk && isJWK(key)) {
|
|
476
|
+
if (isSecretJWK(key) && jwkMatchesOp(alg, key, usage))
|
|
477
|
+
return;
|
|
478
|
+
throw new TypeError(`JSON Web Key for symmetric algorithms must have JWK "kty" (Key Type) equal to "oct" and the JWK "k" (Key Value) present`);
|
|
479
|
+
}
|
|
480
|
+
if (!is_key_like_default(key)) {
|
|
481
|
+
throw new TypeError(withAlg(alg, key, ...types3, "Uint8Array", allowJwk ? "JSON Web Key" : null));
|
|
482
|
+
}
|
|
483
|
+
if (key.type !== "secret") {
|
|
484
|
+
throw new TypeError(`${tag(key)} instances for symmetric algorithms must be of type "secret"`);
|
|
485
|
+
}
|
|
486
|
+
};
|
|
487
|
+
var asymmetricTypeCheck = (alg, key, usage, allowJwk) => {
|
|
488
|
+
if (allowJwk && isJWK(key)) {
|
|
489
|
+
switch (usage) {
|
|
490
|
+
case "sign":
|
|
491
|
+
if (isPrivateJWK(key) && jwkMatchesOp(alg, key, usage))
|
|
492
|
+
return;
|
|
493
|
+
throw new TypeError(`JSON Web Key for this operation be a private JWK`);
|
|
494
|
+
case "verify":
|
|
495
|
+
if (isPublicJWK(key) && jwkMatchesOp(alg, key, usage))
|
|
496
|
+
return;
|
|
497
|
+
throw new TypeError(`JSON Web Key for this operation be a public JWK`);
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
if (!is_key_like_default(key)) {
|
|
501
|
+
throw new TypeError(withAlg(alg, key, ...types3, allowJwk ? "JSON Web Key" : null));
|
|
502
|
+
}
|
|
503
|
+
if (key.type === "secret") {
|
|
504
|
+
throw new TypeError(`${tag(key)} instances for asymmetric algorithms must not be of type "secret"`);
|
|
505
|
+
}
|
|
506
|
+
if (usage === "sign" && key.type === "public") {
|
|
507
|
+
throw new TypeError(`${tag(key)} instances for asymmetric algorithm signing must be of type "private"`);
|
|
508
|
+
}
|
|
509
|
+
if (usage === "decrypt" && key.type === "public") {
|
|
510
|
+
throw new TypeError(`${tag(key)} instances for asymmetric algorithm decryption must be of type "private"`);
|
|
511
|
+
}
|
|
512
|
+
if (key.algorithm && usage === "verify" && key.type === "private") {
|
|
513
|
+
throw new TypeError(`${tag(key)} instances for asymmetric algorithm verifying must be of type "public"`);
|
|
514
|
+
}
|
|
515
|
+
if (key.algorithm && usage === "encrypt" && key.type === "private") {
|
|
516
|
+
throw new TypeError(`${tag(key)} instances for asymmetric algorithm encryption must be of type "public"`);
|
|
517
|
+
}
|
|
518
|
+
};
|
|
519
|
+
function checkKeyType(allowJwk, alg, key, usage) {
|
|
520
|
+
const symmetric = alg.startsWith("HS") || alg === "dir" || alg.startsWith("PBES2") || /^A\d{3}(?:GCM)?KW$/.test(alg);
|
|
521
|
+
if (symmetric) {
|
|
522
|
+
symmetricTypeCheck(alg, key, usage, allowJwk);
|
|
523
|
+
} else {
|
|
524
|
+
asymmetricTypeCheck(alg, key, usage, allowJwk);
|
|
525
|
+
}
|
|
526
|
+
}
|
|
527
|
+
var check_key_type_default = checkKeyType.bind(void 0, false);
|
|
528
|
+
var checkKeyTypeWithJwk = checkKeyType.bind(void 0, true);
|
|
529
|
+
|
|
530
|
+
// ../../node_modules/jose/dist/node/esm/lib/validate_crit.js
|
|
531
|
+
function validateCrit(Err, recognizedDefault, recognizedOption, protectedHeader, joseHeader) {
|
|
532
|
+
if (joseHeader.crit !== void 0 && protectedHeader?.crit === void 0) {
|
|
533
|
+
throw new Err('"crit" (Critical) Header Parameter MUST be integrity protected');
|
|
534
|
+
}
|
|
535
|
+
if (!protectedHeader || protectedHeader.crit === void 0) {
|
|
536
|
+
return /* @__PURE__ */ new Set();
|
|
537
|
+
}
|
|
538
|
+
if (!Array.isArray(protectedHeader.crit) || protectedHeader.crit.length === 0 || protectedHeader.crit.some((input) => typeof input !== "string" || input.length === 0)) {
|
|
539
|
+
throw new Err('"crit" (Critical) Header Parameter MUST be an array of non-empty strings when present');
|
|
540
|
+
}
|
|
541
|
+
let recognized;
|
|
542
|
+
if (recognizedOption !== void 0) {
|
|
543
|
+
recognized = new Map([...Object.entries(recognizedOption), ...recognizedDefault.entries()]);
|
|
544
|
+
} else {
|
|
545
|
+
recognized = recognizedDefault;
|
|
546
|
+
}
|
|
547
|
+
for (const parameter of protectedHeader.crit) {
|
|
548
|
+
if (!recognized.has(parameter)) {
|
|
549
|
+
throw new JOSENotSupported(`Extension Header Parameter "${parameter}" is not recognized`);
|
|
550
|
+
}
|
|
551
|
+
if (joseHeader[parameter] === void 0) {
|
|
552
|
+
throw new Err(`Extension Header Parameter "${parameter}" is missing`);
|
|
553
|
+
}
|
|
554
|
+
if (recognized.get(parameter) && protectedHeader[parameter] === void 0) {
|
|
555
|
+
throw new Err(`Extension Header Parameter "${parameter}" MUST be integrity protected`);
|
|
556
|
+
}
|
|
557
|
+
}
|
|
558
|
+
return new Set(protectedHeader.crit);
|
|
559
|
+
}
|
|
560
|
+
var validate_crit_default = validateCrit;
|
|
561
|
+
|
|
562
|
+
// ../../node_modules/jose/dist/node/esm/runtime/dsa_digest.js
|
|
563
|
+
function dsaDigest(alg) {
|
|
564
|
+
switch (alg) {
|
|
565
|
+
case "PS256":
|
|
566
|
+
case "RS256":
|
|
567
|
+
case "ES256":
|
|
568
|
+
case "ES256K":
|
|
569
|
+
return "sha256";
|
|
570
|
+
case "PS384":
|
|
571
|
+
case "RS384":
|
|
572
|
+
case "ES384":
|
|
573
|
+
return "sha384";
|
|
574
|
+
case "PS512":
|
|
575
|
+
case "RS512":
|
|
576
|
+
case "ES512":
|
|
577
|
+
return "sha512";
|
|
578
|
+
case "Ed25519":
|
|
579
|
+
case "EdDSA":
|
|
580
|
+
return void 0;
|
|
581
|
+
default:
|
|
582
|
+
throw new JOSENotSupported(`alg ${alg} is not supported either by JOSE or your javascript runtime`);
|
|
583
|
+
}
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
// ../../node_modules/jose/dist/node/esm/runtime/node_key.js
|
|
587
|
+
import { constants, KeyObject as KeyObject3 } from "crypto";
|
|
588
|
+
var ecCurveAlgMap = /* @__PURE__ */ new Map([
|
|
589
|
+
["ES256", "P-256"],
|
|
590
|
+
["ES256K", "secp256k1"],
|
|
591
|
+
["ES384", "P-384"],
|
|
592
|
+
["ES512", "P-521"]
|
|
593
|
+
]);
|
|
594
|
+
function keyForCrypto(alg, key) {
|
|
595
|
+
let asymmetricKeyType;
|
|
596
|
+
let asymmetricKeyDetails;
|
|
597
|
+
let isJWK2;
|
|
598
|
+
if (key instanceof KeyObject3) {
|
|
599
|
+
asymmetricKeyType = key.asymmetricKeyType;
|
|
600
|
+
asymmetricKeyDetails = key.asymmetricKeyDetails;
|
|
601
|
+
} else {
|
|
602
|
+
isJWK2 = true;
|
|
603
|
+
switch (key.kty) {
|
|
604
|
+
case "RSA":
|
|
605
|
+
asymmetricKeyType = "rsa";
|
|
606
|
+
break;
|
|
607
|
+
case "EC":
|
|
608
|
+
asymmetricKeyType = "ec";
|
|
609
|
+
break;
|
|
610
|
+
case "OKP": {
|
|
611
|
+
if (key.crv === "Ed25519") {
|
|
612
|
+
asymmetricKeyType = "ed25519";
|
|
613
|
+
break;
|
|
614
|
+
}
|
|
615
|
+
if (key.crv === "Ed448") {
|
|
616
|
+
asymmetricKeyType = "ed448";
|
|
617
|
+
break;
|
|
618
|
+
}
|
|
619
|
+
throw new TypeError("Invalid key for this operation, its crv must be Ed25519 or Ed448");
|
|
620
|
+
}
|
|
621
|
+
default:
|
|
622
|
+
throw new TypeError("Invalid key for this operation, its kty must be RSA, OKP, or EC");
|
|
623
|
+
}
|
|
624
|
+
}
|
|
625
|
+
let options;
|
|
626
|
+
switch (alg) {
|
|
627
|
+
case "Ed25519":
|
|
628
|
+
if (asymmetricKeyType !== "ed25519") {
|
|
629
|
+
throw new TypeError(`Invalid key for this operation, its asymmetricKeyType must be ed25519`);
|
|
630
|
+
}
|
|
631
|
+
break;
|
|
632
|
+
case "EdDSA":
|
|
633
|
+
if (!["ed25519", "ed448"].includes(asymmetricKeyType)) {
|
|
634
|
+
throw new TypeError("Invalid key for this operation, its asymmetricKeyType must be ed25519 or ed448");
|
|
635
|
+
}
|
|
636
|
+
break;
|
|
637
|
+
case "RS256":
|
|
638
|
+
case "RS384":
|
|
639
|
+
case "RS512":
|
|
640
|
+
if (asymmetricKeyType !== "rsa") {
|
|
641
|
+
throw new TypeError("Invalid key for this operation, its asymmetricKeyType must be rsa");
|
|
642
|
+
}
|
|
643
|
+
check_key_length_default(key, alg);
|
|
644
|
+
break;
|
|
645
|
+
case "PS256":
|
|
646
|
+
case "PS384":
|
|
647
|
+
case "PS512":
|
|
648
|
+
if (asymmetricKeyType === "rsa-pss") {
|
|
649
|
+
const { hashAlgorithm, mgf1HashAlgorithm, saltLength } = asymmetricKeyDetails;
|
|
650
|
+
const length = parseInt(alg.slice(-3), 10);
|
|
651
|
+
if (hashAlgorithm !== void 0 && (hashAlgorithm !== `sha${length}` || mgf1HashAlgorithm !== hashAlgorithm)) {
|
|
652
|
+
throw new TypeError(`Invalid key for this operation, its RSA-PSS parameters do not meet the requirements of "alg" ${alg}`);
|
|
653
|
+
}
|
|
654
|
+
if (saltLength !== void 0 && saltLength > length >> 3) {
|
|
655
|
+
throw new TypeError(`Invalid key for this operation, its RSA-PSS parameter saltLength does not meet the requirements of "alg" ${alg}`);
|
|
656
|
+
}
|
|
657
|
+
} else if (asymmetricKeyType !== "rsa") {
|
|
658
|
+
throw new TypeError("Invalid key for this operation, its asymmetricKeyType must be rsa or rsa-pss");
|
|
659
|
+
}
|
|
660
|
+
check_key_length_default(key, alg);
|
|
661
|
+
options = {
|
|
662
|
+
padding: constants.RSA_PKCS1_PSS_PADDING,
|
|
663
|
+
saltLength: constants.RSA_PSS_SALTLEN_DIGEST
|
|
664
|
+
};
|
|
665
|
+
break;
|
|
666
|
+
case "ES256":
|
|
667
|
+
case "ES256K":
|
|
668
|
+
case "ES384":
|
|
669
|
+
case "ES512": {
|
|
670
|
+
if (asymmetricKeyType !== "ec") {
|
|
671
|
+
throw new TypeError("Invalid key for this operation, its asymmetricKeyType must be ec");
|
|
672
|
+
}
|
|
673
|
+
const actual = get_named_curve_default(key);
|
|
674
|
+
const expected = ecCurveAlgMap.get(alg);
|
|
675
|
+
if (actual !== expected) {
|
|
676
|
+
throw new TypeError(`Invalid key curve for the algorithm, its curve must be ${expected}, got ${actual}`);
|
|
677
|
+
}
|
|
678
|
+
options = { dsaEncoding: "ieee-p1363" };
|
|
679
|
+
break;
|
|
680
|
+
}
|
|
681
|
+
default:
|
|
682
|
+
throw new JOSENotSupported(`alg ${alg} is not supported either by JOSE or your javascript runtime`);
|
|
683
|
+
}
|
|
684
|
+
if (isJWK2) {
|
|
685
|
+
return { format: "jwk", key, ...options };
|
|
686
|
+
}
|
|
687
|
+
return options ? { ...options, key } : key;
|
|
688
|
+
}
|
|
689
|
+
|
|
690
|
+
// ../../node_modules/jose/dist/node/esm/runtime/sign.js
|
|
691
|
+
import * as crypto2 from "crypto";
|
|
692
|
+
import { promisify } from "util";
|
|
693
|
+
|
|
694
|
+
// ../../node_modules/jose/dist/node/esm/runtime/hmac_digest.js
|
|
695
|
+
function hmacDigest(alg) {
|
|
696
|
+
switch (alg) {
|
|
697
|
+
case "HS256":
|
|
698
|
+
return "sha256";
|
|
699
|
+
case "HS384":
|
|
700
|
+
return "sha384";
|
|
701
|
+
case "HS512":
|
|
702
|
+
return "sha512";
|
|
703
|
+
default:
|
|
704
|
+
throw new JOSENotSupported(`alg ${alg} is not supported either by JOSE or your javascript runtime`);
|
|
705
|
+
}
|
|
706
|
+
}
|
|
707
|
+
|
|
708
|
+
// ../../node_modules/jose/dist/node/esm/runtime/get_sign_verify_key.js
|
|
709
|
+
import { KeyObject as KeyObject4, createSecretKey } from "crypto";
|
|
710
|
+
function getSignVerifyKey(alg, key, usage) {
|
|
711
|
+
if (key instanceof Uint8Array) {
|
|
712
|
+
if (!alg.startsWith("HS")) {
|
|
713
|
+
throw new TypeError(invalid_key_input_default(key, ...types3));
|
|
714
|
+
}
|
|
715
|
+
return createSecretKey(key);
|
|
716
|
+
}
|
|
717
|
+
if (key instanceof KeyObject4) {
|
|
718
|
+
return key;
|
|
719
|
+
}
|
|
720
|
+
if (isCryptoKey(key)) {
|
|
721
|
+
checkSigCryptoKey(key, alg, usage);
|
|
722
|
+
return KeyObject4.from(key);
|
|
723
|
+
}
|
|
724
|
+
if (isJWK(key)) {
|
|
725
|
+
if (alg.startsWith("HS")) {
|
|
726
|
+
return createSecretKey(Buffer.from(key.k, "base64url"));
|
|
727
|
+
}
|
|
728
|
+
return key;
|
|
729
|
+
}
|
|
730
|
+
throw new TypeError(invalid_key_input_default(key, ...types3, "Uint8Array", "JSON Web Key"));
|
|
731
|
+
}
|
|
732
|
+
|
|
733
|
+
// ../../node_modules/jose/dist/node/esm/runtime/sign.js
|
|
734
|
+
var oneShotSign = promisify(crypto2.sign);
|
|
735
|
+
var sign2 = async (alg, key, data) => {
|
|
736
|
+
const k = getSignVerifyKey(alg, key, "sign");
|
|
737
|
+
if (alg.startsWith("HS")) {
|
|
738
|
+
const hmac = crypto2.createHmac(hmacDigest(alg), k);
|
|
739
|
+
hmac.update(data);
|
|
740
|
+
return hmac.digest();
|
|
741
|
+
}
|
|
742
|
+
return oneShotSign(dsaDigest(alg), data, keyForCrypto(alg, k));
|
|
743
|
+
};
|
|
744
|
+
var sign_default = sign2;
|
|
745
|
+
|
|
746
|
+
// ../../node_modules/jose/dist/node/esm/lib/epoch.js
|
|
747
|
+
var epoch_default = (date) => Math.floor(date.getTime() / 1e3);
|
|
748
|
+
|
|
749
|
+
// ../../node_modules/jose/dist/node/esm/lib/secs.js
|
|
750
|
+
var minute = 60;
|
|
751
|
+
var hour = minute * 60;
|
|
752
|
+
var day = hour * 24;
|
|
753
|
+
var week = day * 7;
|
|
754
|
+
var year = day * 365.25;
|
|
755
|
+
var REGEX = /^(\+|\-)? ?(\d+|\d+\.\d+) ?(seconds?|secs?|s|minutes?|mins?|m|hours?|hrs?|h|days?|d|weeks?|w|years?|yrs?|y)(?: (ago|from now))?$/i;
|
|
756
|
+
var secs_default = (str) => {
|
|
757
|
+
const matched = REGEX.exec(str);
|
|
758
|
+
if (!matched || matched[4] && matched[1]) {
|
|
759
|
+
throw new TypeError("Invalid time period format");
|
|
760
|
+
}
|
|
761
|
+
const value = parseFloat(matched[2]);
|
|
762
|
+
const unit = matched[3].toLowerCase();
|
|
763
|
+
let numericDate;
|
|
764
|
+
switch (unit) {
|
|
765
|
+
case "sec":
|
|
766
|
+
case "secs":
|
|
767
|
+
case "second":
|
|
768
|
+
case "seconds":
|
|
769
|
+
case "s":
|
|
770
|
+
numericDate = Math.round(value);
|
|
771
|
+
break;
|
|
772
|
+
case "minute":
|
|
773
|
+
case "minutes":
|
|
774
|
+
case "min":
|
|
775
|
+
case "mins":
|
|
776
|
+
case "m":
|
|
777
|
+
numericDate = Math.round(value * minute);
|
|
778
|
+
break;
|
|
779
|
+
case "hour":
|
|
780
|
+
case "hours":
|
|
781
|
+
case "hr":
|
|
782
|
+
case "hrs":
|
|
783
|
+
case "h":
|
|
784
|
+
numericDate = Math.round(value * hour);
|
|
785
|
+
break;
|
|
786
|
+
case "day":
|
|
787
|
+
case "days":
|
|
788
|
+
case "d":
|
|
789
|
+
numericDate = Math.round(value * day);
|
|
790
|
+
break;
|
|
791
|
+
case "week":
|
|
792
|
+
case "weeks":
|
|
793
|
+
case "w":
|
|
794
|
+
numericDate = Math.round(value * week);
|
|
795
|
+
break;
|
|
796
|
+
default:
|
|
797
|
+
numericDate = Math.round(value * year);
|
|
798
|
+
break;
|
|
799
|
+
}
|
|
800
|
+
if (matched[1] === "-" || matched[4] === "ago") {
|
|
801
|
+
return -numericDate;
|
|
802
|
+
}
|
|
803
|
+
return numericDate;
|
|
804
|
+
};
|
|
805
|
+
|
|
806
|
+
// ../../node_modules/jose/dist/node/esm/jws/flattened/sign.js
|
|
807
|
+
var FlattenedSign = class {
|
|
808
|
+
_payload;
|
|
809
|
+
_protectedHeader;
|
|
810
|
+
_unprotectedHeader;
|
|
811
|
+
constructor(payload) {
|
|
812
|
+
if (!(payload instanceof Uint8Array)) {
|
|
813
|
+
throw new TypeError("payload must be an instance of Uint8Array");
|
|
814
|
+
}
|
|
815
|
+
this._payload = payload;
|
|
816
|
+
}
|
|
817
|
+
setProtectedHeader(protectedHeader) {
|
|
818
|
+
if (this._protectedHeader) {
|
|
819
|
+
throw new TypeError("setProtectedHeader can only be called once");
|
|
820
|
+
}
|
|
821
|
+
this._protectedHeader = protectedHeader;
|
|
822
|
+
return this;
|
|
823
|
+
}
|
|
824
|
+
setUnprotectedHeader(unprotectedHeader) {
|
|
825
|
+
if (this._unprotectedHeader) {
|
|
826
|
+
throw new TypeError("setUnprotectedHeader can only be called once");
|
|
827
|
+
}
|
|
828
|
+
this._unprotectedHeader = unprotectedHeader;
|
|
829
|
+
return this;
|
|
830
|
+
}
|
|
831
|
+
async sign(key, options) {
|
|
832
|
+
if (!this._protectedHeader && !this._unprotectedHeader) {
|
|
833
|
+
throw new JWSInvalid("either setProtectedHeader or setUnprotectedHeader must be called before #sign()");
|
|
834
|
+
}
|
|
835
|
+
if (!is_disjoint_default(this._protectedHeader, this._unprotectedHeader)) {
|
|
836
|
+
throw new JWSInvalid("JWS Protected and JWS Unprotected Header Parameter names must be disjoint");
|
|
837
|
+
}
|
|
838
|
+
const joseHeader = {
|
|
839
|
+
...this._protectedHeader,
|
|
840
|
+
...this._unprotectedHeader
|
|
841
|
+
};
|
|
842
|
+
const extensions = validate_crit_default(JWSInvalid, /* @__PURE__ */ new Map([["b64", true]]), options?.crit, this._protectedHeader, joseHeader);
|
|
843
|
+
let b64 = true;
|
|
844
|
+
if (extensions.has("b64")) {
|
|
845
|
+
b64 = this._protectedHeader.b64;
|
|
846
|
+
if (typeof b64 !== "boolean") {
|
|
847
|
+
throw new JWSInvalid('The "b64" (base64url-encode payload) Header Parameter must be a boolean');
|
|
848
|
+
}
|
|
849
|
+
}
|
|
850
|
+
const { alg } = joseHeader;
|
|
851
|
+
if (typeof alg !== "string" || !alg) {
|
|
852
|
+
throw new JWSInvalid('JWS "alg" (Algorithm) Header Parameter missing or invalid');
|
|
853
|
+
}
|
|
854
|
+
checkKeyTypeWithJwk(alg, key, "sign");
|
|
855
|
+
let payload = this._payload;
|
|
856
|
+
if (b64) {
|
|
857
|
+
payload = encoder.encode(encode(payload));
|
|
858
|
+
}
|
|
859
|
+
let protectedHeader;
|
|
860
|
+
if (this._protectedHeader) {
|
|
861
|
+
protectedHeader = encoder.encode(encode(JSON.stringify(this._protectedHeader)));
|
|
862
|
+
} else {
|
|
863
|
+
protectedHeader = encoder.encode("");
|
|
864
|
+
}
|
|
865
|
+
const data = concat(protectedHeader, encoder.encode("."), payload);
|
|
866
|
+
const signature = await sign_default(alg, key, data);
|
|
867
|
+
const jws = {
|
|
868
|
+
signature: encode(signature),
|
|
869
|
+
payload: ""
|
|
870
|
+
};
|
|
871
|
+
if (b64) {
|
|
872
|
+
jws.payload = decoder.decode(payload);
|
|
873
|
+
}
|
|
874
|
+
if (this._unprotectedHeader) {
|
|
875
|
+
jws.header = this._unprotectedHeader;
|
|
876
|
+
}
|
|
877
|
+
if (this._protectedHeader) {
|
|
878
|
+
jws.protected = decoder.decode(protectedHeader);
|
|
879
|
+
}
|
|
880
|
+
return jws;
|
|
881
|
+
}
|
|
882
|
+
};
|
|
883
|
+
|
|
884
|
+
// ../../node_modules/jose/dist/node/esm/jws/compact/sign.js
|
|
885
|
+
var CompactSign = class {
|
|
886
|
+
_flattened;
|
|
887
|
+
constructor(payload) {
|
|
888
|
+
this._flattened = new FlattenedSign(payload);
|
|
889
|
+
}
|
|
890
|
+
setProtectedHeader(protectedHeader) {
|
|
891
|
+
this._flattened.setProtectedHeader(protectedHeader);
|
|
892
|
+
return this;
|
|
893
|
+
}
|
|
894
|
+
async sign(key, options) {
|
|
895
|
+
const jws = await this._flattened.sign(key, options);
|
|
896
|
+
if (jws.payload === void 0) {
|
|
897
|
+
throw new TypeError("use the flattened module for creating JWS with b64: false");
|
|
898
|
+
}
|
|
899
|
+
return `${jws.protected}.${jws.payload}.${jws.signature}`;
|
|
900
|
+
}
|
|
901
|
+
};
|
|
902
|
+
|
|
903
|
+
// ../../node_modules/jose/dist/node/esm/jwt/produce.js
|
|
904
|
+
function validateInput(label, input) {
|
|
905
|
+
if (!Number.isFinite(input)) {
|
|
906
|
+
throw new TypeError(`Invalid ${label} input`);
|
|
907
|
+
}
|
|
908
|
+
return input;
|
|
909
|
+
}
|
|
910
|
+
var ProduceJWT = class {
|
|
911
|
+
_payload;
|
|
912
|
+
constructor(payload = {}) {
|
|
913
|
+
if (!isObject(payload)) {
|
|
914
|
+
throw new TypeError("JWT Claims Set MUST be an object");
|
|
915
|
+
}
|
|
916
|
+
this._payload = payload;
|
|
917
|
+
}
|
|
918
|
+
setIssuer(issuer) {
|
|
919
|
+
this._payload = { ...this._payload, iss: issuer };
|
|
920
|
+
return this;
|
|
921
|
+
}
|
|
922
|
+
setSubject(subject) {
|
|
923
|
+
this._payload = { ...this._payload, sub: subject };
|
|
924
|
+
return this;
|
|
925
|
+
}
|
|
926
|
+
setAudience(audience) {
|
|
927
|
+
this._payload = { ...this._payload, aud: audience };
|
|
928
|
+
return this;
|
|
929
|
+
}
|
|
930
|
+
setJti(jwtId) {
|
|
931
|
+
this._payload = { ...this._payload, jti: jwtId };
|
|
932
|
+
return this;
|
|
933
|
+
}
|
|
934
|
+
setNotBefore(input) {
|
|
935
|
+
if (typeof input === "number") {
|
|
936
|
+
this._payload = { ...this._payload, nbf: validateInput("setNotBefore", input) };
|
|
937
|
+
} else if (input instanceof Date) {
|
|
938
|
+
this._payload = { ...this._payload, nbf: validateInput("setNotBefore", epoch_default(input)) };
|
|
939
|
+
} else {
|
|
940
|
+
this._payload = { ...this._payload, nbf: epoch_default(/* @__PURE__ */ new Date()) + secs_default(input) };
|
|
941
|
+
}
|
|
942
|
+
return this;
|
|
943
|
+
}
|
|
944
|
+
setExpirationTime(input) {
|
|
945
|
+
if (typeof input === "number") {
|
|
946
|
+
this._payload = { ...this._payload, exp: validateInput("setExpirationTime", input) };
|
|
947
|
+
} else if (input instanceof Date) {
|
|
948
|
+
this._payload = { ...this._payload, exp: validateInput("setExpirationTime", epoch_default(input)) };
|
|
949
|
+
} else {
|
|
950
|
+
this._payload = { ...this._payload, exp: epoch_default(/* @__PURE__ */ new Date()) + secs_default(input) };
|
|
951
|
+
}
|
|
952
|
+
return this;
|
|
953
|
+
}
|
|
954
|
+
setIssuedAt(input) {
|
|
955
|
+
if (typeof input === "undefined") {
|
|
956
|
+
this._payload = { ...this._payload, iat: epoch_default(/* @__PURE__ */ new Date()) };
|
|
957
|
+
} else if (input instanceof Date) {
|
|
958
|
+
this._payload = { ...this._payload, iat: validateInput("setIssuedAt", epoch_default(input)) };
|
|
959
|
+
} else if (typeof input === "string") {
|
|
960
|
+
this._payload = {
|
|
961
|
+
...this._payload,
|
|
962
|
+
iat: validateInput("setIssuedAt", epoch_default(/* @__PURE__ */ new Date()) + secs_default(input))
|
|
963
|
+
};
|
|
964
|
+
} else {
|
|
965
|
+
this._payload = { ...this._payload, iat: validateInput("setIssuedAt", input) };
|
|
966
|
+
}
|
|
967
|
+
return this;
|
|
968
|
+
}
|
|
969
|
+
};
|
|
970
|
+
|
|
971
|
+
// ../../node_modules/jose/dist/node/esm/jwt/sign.js
|
|
972
|
+
var SignJWT = class extends ProduceJWT {
|
|
973
|
+
_protectedHeader;
|
|
974
|
+
setProtectedHeader(protectedHeader) {
|
|
975
|
+
this._protectedHeader = protectedHeader;
|
|
976
|
+
return this;
|
|
977
|
+
}
|
|
978
|
+
async sign(key, options) {
|
|
979
|
+
const sig = new CompactSign(encoder.encode(JSON.stringify(this._payload)));
|
|
980
|
+
sig.setProtectedHeader(this._protectedHeader);
|
|
981
|
+
if (Array.isArray(this._protectedHeader?.crit) && this._protectedHeader.crit.includes("b64") && this._protectedHeader.b64 === false) {
|
|
982
|
+
throw new JWTInvalid("JWTs MUST NOT use unencoded payload");
|
|
983
|
+
}
|
|
984
|
+
return sig.sign(key, options);
|
|
985
|
+
}
|
|
986
|
+
};
|
|
987
|
+
|
|
988
|
+
// ../shared/src/jwt.ts
|
|
989
|
+
var enc = new TextEncoder();
|
|
990
|
+
async function signOpsToken(secret, payload, ttlSeconds = 30 * 24 * 3600) {
|
|
991
|
+
return await new SignJWT(payload).setProtectedHeader({ alg: "HS256" }).setIssuedAt().setExpirationTime(`${ttlSeconds}s`).setIssuer("duoduo-ops").setAudience("duoduo-ops").sign(enc.encode(secret));
|
|
992
|
+
}
|
|
993
|
+
|
|
994
|
+
// ../shared/src/paths.ts
|
|
995
|
+
import { existsSync } from "fs";
|
|
996
|
+
import path from "path";
|
|
997
|
+
|
|
998
|
+
// ../shared/src/env-loader.ts
|
|
999
|
+
import { existsSync as existsSync2 } from "fs";
|
|
1000
|
+
import path2 from "path";
|
|
1001
|
+
async function loadEnvFiles(opts = {}) {
|
|
1002
|
+
const dotenv = (await import("dotenv")).default;
|
|
1003
|
+
const start = path2.resolve(opts.startDir ?? process.cwd());
|
|
1004
|
+
const candidateDirs = /* @__PURE__ */ new Set();
|
|
1005
|
+
let dir = start;
|
|
1006
|
+
for (; ; ) {
|
|
1007
|
+
candidateDirs.add(dir);
|
|
1008
|
+
if (existsSync2(path2.join(dir, "turbo.json"))) break;
|
|
1009
|
+
const parent = path2.dirname(dir);
|
|
1010
|
+
if (parent === dir) break;
|
|
1011
|
+
dir = parent;
|
|
1012
|
+
}
|
|
1013
|
+
const root = dir;
|
|
1014
|
+
for (const sub of ["apps/web", "packages/db"]) {
|
|
1015
|
+
candidateDirs.add(path2.join(root, sub));
|
|
1016
|
+
}
|
|
1017
|
+
for (const p2 of opts.extra ?? []) {
|
|
1018
|
+
candidateDirs.add(path2.dirname(path2.resolve(p2)));
|
|
1019
|
+
}
|
|
1020
|
+
const loaded = [];
|
|
1021
|
+
const skipped = [];
|
|
1022
|
+
for (const d of candidateDirs) {
|
|
1023
|
+
const p2 = path2.join(d, ".env");
|
|
1024
|
+
if (!existsSync2(p2)) {
|
|
1025
|
+
skipped.push(p2);
|
|
1026
|
+
continue;
|
|
1027
|
+
}
|
|
1028
|
+
dotenv.config({ path: p2, override: opts.override ?? false });
|
|
1029
|
+
loaded.push(p2);
|
|
1030
|
+
}
|
|
1031
|
+
for (const p2 of opts.extra ?? []) {
|
|
1032
|
+
if (!existsSync2(p2)) continue;
|
|
1033
|
+
if (loaded.includes(path2.resolve(p2))) continue;
|
|
1034
|
+
dotenv.config({ path: p2, override: opts.override ?? false });
|
|
1035
|
+
loaded.push(p2);
|
|
1036
|
+
}
|
|
1037
|
+
if (!opts.silent && loaded.length > 0) {
|
|
1038
|
+
console.log(`[env-loader] loaded ${loaded.length} .env file(s): ${loaded.join(", ")}`);
|
|
1039
|
+
}
|
|
1040
|
+
return { loaded, skipped };
|
|
1041
|
+
}
|
|
1042
|
+
|
|
1043
|
+
// src/index.ts
|
|
1044
|
+
await loadEnvFiles({ silent: true });
|
|
1045
|
+
function out(data, pretty) {
|
|
1046
|
+
process.stdout.write(JSON.stringify(data, null, pretty ? 2 : 0) + "\n");
|
|
1047
|
+
}
|
|
1048
|
+
function die(msg, data) {
|
|
1049
|
+
process.stderr.write(
|
|
1050
|
+
JSON.stringify({ ok: false, error: msg, ...data ? { detail: data } : {} }, null, 2) + "\n"
|
|
1051
|
+
);
|
|
1052
|
+
process.exit(1);
|
|
1053
|
+
}
|
|
1054
|
+
function makeClient(api, token) {
|
|
1055
|
+
return axios.create({
|
|
1056
|
+
baseURL: api.replace(/\/$/, ""),
|
|
1057
|
+
headers: { Authorization: `Bearer ${token}`, "Content-Type": "application/json" },
|
|
1058
|
+
timeout: 3e4
|
|
1059
|
+
});
|
|
1060
|
+
}
|
|
1061
|
+
async function call(client, method, path3, payload) {
|
|
1062
|
+
try {
|
|
1063
|
+
const res = method === "get" ? await client.get(path3, { params: payload }) : await client.post(path3, payload);
|
|
1064
|
+
if (!res.data.ok) die(res.data.message ?? "API error", res.data);
|
|
1065
|
+
return res.data.data;
|
|
1066
|
+
} catch (err) {
|
|
1067
|
+
if (axios.isAxiosError(err)) {
|
|
1068
|
+
const d = err.response?.data;
|
|
1069
|
+
die(d?.message ?? err.message, { status: err.response?.status });
|
|
1070
|
+
}
|
|
1071
|
+
throw err;
|
|
1072
|
+
}
|
|
1073
|
+
}
|
|
1074
|
+
var program = new Command();
|
|
1075
|
+
program.name("duoduo-ops").description("\u591A\u591A\u8FD0\u8425\u540E\u53F0 CLI\uFF1A\u67E5\u8BE2\u9500\u552E/\u5E93\u5B58/\u5229\u6DA6/\u4ED3\u50A8\u8D39\uFF0C\u7BA1\u7406\u4E3B\u6570\u636E").version("0.1.0").option("--api <url>", "\u4E91\u7AEF API \u5730\u5740", process.env.INGEST_API_BASE ?? "http://localhost:3000").option("--token <jwt>", "Ops JWT\uFF08\u4F18\u5148\u7EA7\uFF1A--token > OPS_JWT \u73AF\u5883\u53D8\u91CF\uFF09", process.env.OPS_JWT).option("--pretty", "\u7F8E\u5316 JSON \u8F93\u51FA", false);
|
|
1076
|
+
function globalOpts(cmd) {
|
|
1077
|
+
const root = cmd.parent ?? cmd;
|
|
1078
|
+
return root.opts();
|
|
1079
|
+
}
|
|
1080
|
+
function requireToken(cmd) {
|
|
1081
|
+
const g = globalOpts(cmd);
|
|
1082
|
+
if (!g.token) die("\u7F3A\u5C11 Ops JWT\uFF0C\u8BF7\u901A\u8FC7 --token \u6216 OPS_JWT \u73AF\u5883\u53D8\u91CF\u63D0\u4F9B");
|
|
1083
|
+
return { api: g.api, token: g.token, pretty: g.pretty };
|
|
1084
|
+
}
|
|
1085
|
+
program.command("gen-token").description("\u751F\u6210 Ops JWT\uFF08\u9700\u8981 WORKER_JWT_SECRET \u548C\u79DF\u6237 slug\uFF09").requiredOption("--tenant-slug <slug>", "\u79DF\u6237 slug\uFF08\u53EF\u5728\u540E\u53F0\u300C\u8BBE\u7F6E\u300D\u9875\u6216\u6CE8\u518C\u90AE\u4EF6\u4E2D\u67E5\u770B\uFF09").option("--tenant-id <uuid>", "\u79DF\u6237 UUID\uFF08\u53EF\u9009\uFF0C\u9ED8\u8BA4\u81EA\u52A8\u4F7F\u7528 slug\uFF09").option("--ttl <days>", "\u6709\u6548\u671F\uFF08\u5929\uFF09", "30").action(async (opts, cmd) => {
|
|
1086
|
+
const g = globalOpts(cmd);
|
|
1087
|
+
const secret = process.env.WORKER_JWT_SECRET;
|
|
1088
|
+
if (!secret) die("\u7F3A\u5C11 WORKER_JWT_SECRET \u73AF\u5883\u53D8\u91CF");
|
|
1089
|
+
const tenantId = opts.tenantId ?? opts.tenantSlug;
|
|
1090
|
+
const ttlSeconds = Math.round(parseFloat(opts.ttl) * 24 * 3600);
|
|
1091
|
+
const token = await signOpsToken(
|
|
1092
|
+
secret,
|
|
1093
|
+
{ sub: "ops-cli", tenantId, tenantSlug: opts.tenantSlug },
|
|
1094
|
+
ttlSeconds
|
|
1095
|
+
);
|
|
1096
|
+
out({ token, tenantId, tenantSlug: opts.tenantSlug, ttlDays: opts.ttl }, g.pretty);
|
|
1097
|
+
});
|
|
1098
|
+
var salesCmd = program.command("sales").description("\u9500\u552E\u76F8\u5173\u64CD\u4F5C");
|
|
1099
|
+
salesCmd.command("query").description("\u67E5\u8BE2\u9500\u552E\u660E\u7EC6").option("--from <date>", "\u8D77\u59CB\u65E5\u671F YYYY-MM-DD").option("--to <date>", "\u7ED3\u675F\u65E5\u671F YYYY-MM-DD").option("--shop <id>", "\u6309\u5E97\u94FA UUID \u8FC7\u6EE4\uFF08\u4ECE shops list \u83B7\u53D6\uFF09").option("--warehouse <id>", "\u6309\u4ED3\u5E93 UUID \u8FC7\u6EE4\uFF08\u4ECE warehouses list \u83B7\u53D6\uFF09").option("--sku <sku>", "\u6309 SKU \u6A21\u7CCA\u8FC7\u6EE4").option("--limit <n>", "\u6700\u591A\u8FD4\u56DE\u6761\u6570\uFF08\u2264500\uFF09", "200").action(async (opts, cmd) => {
|
|
1100
|
+
const { api, token, pretty } = requireToken(cmd.parent);
|
|
1101
|
+
const client = makeClient(api, token);
|
|
1102
|
+
const params = {};
|
|
1103
|
+
if (opts.from) params.from = opts.from;
|
|
1104
|
+
if (opts.to) params.to = opts.to;
|
|
1105
|
+
if (opts.shop) params.shopId = opts.shop;
|
|
1106
|
+
if (opts.warehouse) params.warehouseId = opts.warehouse;
|
|
1107
|
+
if (opts.sku) params.sku = opts.sku;
|
|
1108
|
+
params.limit = opts.limit;
|
|
1109
|
+
const data = await call(client, "get", "/api/sales", params);
|
|
1110
|
+
out(data, pretty);
|
|
1111
|
+
});
|
|
1112
|
+
var invCmd = program.command("inventory").description("\u5E93\u5B58\u76F8\u5173\u64CD\u4F5C");
|
|
1113
|
+
invCmd.command("query").description("\u67E5\u8BE2\u5E93\u5B58\u5FEB\u7167").option("--from <date>", "\u8D77\u59CB\u65E5\u671F YYYY-MM-DD").option("--to <date>", "\u7ED3\u675F\u65E5\u671F YYYY-MM-DD").option("--shop <id>", "\u6309\u5E97\u94FA UUID \u8FC7\u6EE4\uFF08\u4ECE shops list \u83B7\u53D6\uFF09").option("--warehouse <id>", "\u6309\u4ED3\u5E93 UUID \u8FC7\u6EE4").option("--sku <sku>", "\u6309 SKU \u6A21\u7CCA\u8FC7\u6EE4").option("--is-shared <bool>", "true=\u4EC5\u5171\u4EAB\u4ED3 / false=\u4EC5\u4E2D\u5FC3\u4ED3").option("--page <n>", "\u9875\u7801", "1").option("--page-size <n>", "\u6BCF\u9875\u6761\u6570\uFF08\u2264200\uFF09", "50").action(async (opts, cmd) => {
|
|
1114
|
+
const { api, token, pretty } = requireToken(cmd.parent);
|
|
1115
|
+
const client = makeClient(api, token);
|
|
1116
|
+
const params = {};
|
|
1117
|
+
if (opts.from) params.from = opts.from;
|
|
1118
|
+
if (opts.to) params.to = opts.to;
|
|
1119
|
+
if (opts.shop) params.shopId = opts.shop;
|
|
1120
|
+
if (opts.warehouse) params.warehouseId = opts.warehouse;
|
|
1121
|
+
if (opts.sku) params.sku = opts.sku;
|
|
1122
|
+
if (opts.isShared) params.isShared = opts.isShared;
|
|
1123
|
+
params.page = opts.page;
|
|
1124
|
+
params.pageSize = opts.pageSize;
|
|
1125
|
+
const data = await call(client, "get", "/api/inventory", params);
|
|
1126
|
+
out(data, pretty);
|
|
1127
|
+
});
|
|
1128
|
+
var profitCmd = program.command("profit").description("\u5229\u6DA6\u76F8\u5173\u64CD\u4F5C");
|
|
1129
|
+
profitCmd.command("query").description("\u67E5\u8BE2\u5229\u6DA6\u6C47\u603B").option("--from <date>", "\u8D77\u59CB\u65E5\u671F YYYY-MM-DD").option("--to <date>", "\u7ED3\u675F\u65E5\u671F YYYY-MM-DD").option("--warehouses <ids>", "\u4ED3\u5E93 ID \u5217\u8868\uFF08\u9017\u53F7\u5206\u9694\uFF09").option("--shops <ids>", "\u5E97\u94FA ID \u5217\u8868\uFF08\u9017\u53F7\u5206\u9694\uFF09").option("--page <n>", "\u9875\u7801", "1").option("--page-size <n>", "\u6BCF\u9875\u6761\u6570\uFF08\u2264500\uFF09", "100").action(async (opts, cmd) => {
|
|
1130
|
+
const { api, token, pretty } = requireToken(cmd.parent);
|
|
1131
|
+
const client = makeClient(api, token);
|
|
1132
|
+
const params = {};
|
|
1133
|
+
if (opts.from) params.from = opts.from;
|
|
1134
|
+
if (opts.to) params.to = opts.to;
|
|
1135
|
+
if (opts.warehouses) params.warehouseIds = opts.warehouses;
|
|
1136
|
+
if (opts.shops) params.shopIds = opts.shops;
|
|
1137
|
+
params.page = opts.page;
|
|
1138
|
+
params.pageSize = opts.pageSize;
|
|
1139
|
+
const data = await call(client, "get", "/api/profit", params);
|
|
1140
|
+
out(data, pretty);
|
|
1141
|
+
});
|
|
1142
|
+
profitCmd.command("recompute").description("\u91CD\u7B97\u6307\u5B9A\u65E5\u671F\u533A\u95F4\u5185\u7684\u5229\u6DA6").requiredOption("--from <date>", "\u8D77\u59CB\u65E5\u671F YYYY-MM-DD").requiredOption("--to <date>", "\u7ED3\u675F\u65E5\u671F YYYY-MM-DD").action(async (opts, cmd) => {
|
|
1143
|
+
const { api, token, pretty } = requireToken(cmd.parent);
|
|
1144
|
+
const client = makeClient(api, token);
|
|
1145
|
+
const data = await call(client, "post", "/api/profit/recompute", {
|
|
1146
|
+
fromDate: opts.from,
|
|
1147
|
+
toDate: opts.to
|
|
1148
|
+
});
|
|
1149
|
+
out(data, pretty);
|
|
1150
|
+
});
|
|
1151
|
+
var sfCmd = program.command("storage-fees").description("\u4ED3\u50A8\u8D39\u76F8\u5173\u64CD\u4F5C");
|
|
1152
|
+
sfCmd.command("query").description("\u67E5\u8BE2\u4ED3\u50A8\u8D39\u660E\u7EC6").option("--from <date>", "\u8D77\u59CB\u65E5\u671F YYYY-MM-DD").option("--to <date>", "\u7ED3\u675F\u65E5\u671F YYYY-MM-DD").option("--warehouse <id>", "\u6309\u5171\u4EAB\u4ED3 ID \u8FC7\u6EE4").option("--shop <id>", "\u6309\u5E97\u94FA ID \u8FC7\u6EE4").option("--page <n>", "\u9875\u7801", "1").option("--page-size <n>", "\u6BCF\u9875\u6761\u6570\uFF08\u2264200\uFF09", "50").action(async (opts, cmd) => {
|
|
1153
|
+
const { api, token, pretty } = requireToken(cmd.parent);
|
|
1154
|
+
const client = makeClient(api, token);
|
|
1155
|
+
const params = {};
|
|
1156
|
+
if (opts.from) params.from = opts.from;
|
|
1157
|
+
if (opts.to) params.to = opts.to;
|
|
1158
|
+
if (opts.warehouse) params.warehouseId = opts.warehouse;
|
|
1159
|
+
if (opts.shop) params.shopId = opts.shop;
|
|
1160
|
+
params.page = opts.page;
|
|
1161
|
+
params.pageSize = opts.pageSize;
|
|
1162
|
+
const data = await call(client, "get", "/api/storage-fees", params);
|
|
1163
|
+
out(data, pretty);
|
|
1164
|
+
});
|
|
1165
|
+
var shopsCmd = program.command("shops").description("\u5E97\u94FA\u76F8\u5173\u64CD\u4F5C");
|
|
1166
|
+
shopsCmd.command("list").description("\u5217\u51FA\u6240\u6709\u5E97\u94FA").action(async (_opts, cmd) => {
|
|
1167
|
+
const { api, token, pretty } = requireToken(cmd.parent);
|
|
1168
|
+
const client = makeClient(api, token);
|
|
1169
|
+
const data = await call(client, "get", "/api/shops", {});
|
|
1170
|
+
out(data, pretty);
|
|
1171
|
+
});
|
|
1172
|
+
var productsCmd = program.command("products").description("\u5546\u54C1\uFF08\u4E3B\u6570\u636E\uFF09\u76F8\u5173\u64CD\u4F5C");
|
|
1173
|
+
productsCmd.command("list").description("\u67E5\u8BE2\u5546\u54C1\u5217\u8868\uFF08\u6309 SKU/\u540D\u79F0\u6A21\u7CCA\u641C\u7D22\uFF09").option("--q <keyword>", "\u6A21\u7CCA\u641C\u7D22 SKU \u6216\u5546\u54C1\u540D").action(async (opts, cmd) => {
|
|
1174
|
+
const { api, token, pretty } = requireToken(cmd.parent);
|
|
1175
|
+
const client = makeClient(api, token);
|
|
1176
|
+
const params = {};
|
|
1177
|
+
if (opts.q) params.q = opts.q;
|
|
1178
|
+
const data = await call(client, "get", "/api/products", params);
|
|
1179
|
+
out(data, pretty);
|
|
1180
|
+
});
|
|
1181
|
+
var warehousesCmd = program.command("warehouses").description("\u4ED3\u5E93\uFF08\u4E3B\u6570\u636E\uFF09\u76F8\u5173\u64CD\u4F5C");
|
|
1182
|
+
warehousesCmd.command("list").description("\u5217\u51FA\u6240\u6709\u4ED3\u5E93\uFF08\u542B\u4ED3\u5E93\u7EC4\u3001\u533A\u57DF\u4FE1\u606F\uFF09").action(async (_opts, cmd) => {
|
|
1183
|
+
const { api, token, pretty } = requireToken(cmd.parent);
|
|
1184
|
+
const client = makeClient(api, token);
|
|
1185
|
+
const data = await call(client, "get", "/api/warehouses", {});
|
|
1186
|
+
out(data, pretty);
|
|
1187
|
+
});
|
|
1188
|
+
var pcCmd = program.command("product-costs").description("\u5546\u54C1\u6210\u672C\u76F8\u5173\u64CD\u4F5C");
|
|
1189
|
+
pcCmd.command("add").description("\u6DFB\u52A0/\u66F4\u65B0\u5546\u54C1\u6210\u672C\uFF08\u6309\u5546\u54C1+\u751F\u6548\u65E5\u671F\u5E42\u7B49\u5199\u5165\uFF09").requiredOption("--product-id <id>", "\u5546\u54C1 UUID\uFF08\u53EF\u4ECE products list \u83B7\u53D6\uFF09").requiredOption("--cost <fen>", "\u6210\u672C\u4EF7\uFF08\u5355\u4F4D\uFF1A\u5206\uFF0C\u6574\u6570\uFF09").requiredOption("--date <date>", "\u751F\u6548\u65E5\u671F YYYY-MM-DD").option("--remark <text>", "\u5907\u6CE8").action(async (opts, cmd) => {
|
|
1190
|
+
const { api, token, pretty } = requireToken(cmd.parent);
|
|
1191
|
+
const client = makeClient(api, token);
|
|
1192
|
+
const costFen = parseInt(opts.cost, 10);
|
|
1193
|
+
if (isNaN(costFen) || costFen < 0) die("--cost \u5FC5\u987B\u662F\u975E\u8D1F\u6574\u6570\uFF08\u5206\uFF09");
|
|
1194
|
+
const data = await call(client, "post", "/api/product-costs", {
|
|
1195
|
+
productId: opts.productId,
|
|
1196
|
+
costPriceFen: costFen,
|
|
1197
|
+
effectiveDate: opts.date,
|
|
1198
|
+
...opts.remark ? { remark: opts.remark } : {}
|
|
1199
|
+
});
|
|
1200
|
+
out(data, pretty);
|
|
1201
|
+
});
|
|
1202
|
+
pcCmd.command("list").description("\u67E5\u8BE2\u5546\u54C1\u6210\u672C\u8BB0\u5F55").option("--product-id <id>", "\u6309\u5546\u54C1 ID \u8FC7\u6EE4").action(async (opts, cmd) => {
|
|
1203
|
+
const { api, token, pretty } = requireToken(cmd.parent);
|
|
1204
|
+
const client = makeClient(api, token);
|
|
1205
|
+
const params = {};
|
|
1206
|
+
if (opts.productId) params.productId = opts.productId;
|
|
1207
|
+
const data = await call(client, "get", "/api/product-costs", params);
|
|
1208
|
+
out(data, pretty);
|
|
1209
|
+
});
|
|
1210
|
+
var wmCmd = program.command("warehouse-mappings").description("\u4ED3\u5E93\u6620\u5C04\uFF08\u5171\u4EAB\u4ED3\u2192\u4E2D\u5FC3\u4ED3\uFF09\u76F8\u5173\u64CD\u4F5C");
|
|
1211
|
+
wmCmd.command("add").description("\u6DFB\u52A0/\u66F4\u65B0\u4ED3\u5E93\u6620\u5C04\uFF08\u6309\u4ED3\u5E93\u5BF9\u5E42\u7B49\u5199\u5165\uFF09").requiredOption("--shared <code>", "\u5171\u4EAB\u4ED3 code").requiredOption("--center <code>", "\u4E2D\u5FC3\u4ED3 code").option("--weight <n>", "\u5206\u644A\u6743\u91CD\uFF08\u9ED8\u8BA4 1\uFF09", "1").action(async (opts, cmd) => {
|
|
1212
|
+
const { api, token, pretty } = requireToken(cmd.parent);
|
|
1213
|
+
const client = makeClient(api, token);
|
|
1214
|
+
const data = await call(client, "post", "/api/warehouse-mappings", {
|
|
1215
|
+
sharedWarehouseCode: opts.shared,
|
|
1216
|
+
centerWarehouseCode: opts.center,
|
|
1217
|
+
weight: parseFloat(opts.weight)
|
|
1218
|
+
});
|
|
1219
|
+
out(data, pretty);
|
|
1220
|
+
});
|
|
1221
|
+
wmCmd.command("list").description("\u5217\u51FA\u6240\u6709\u4ED3\u5E93\u6620\u5C04").action(async (_opts, cmd) => {
|
|
1222
|
+
const { api, token, pretty } = requireToken(cmd.parent);
|
|
1223
|
+
const client = makeClient(api, token);
|
|
1224
|
+
const data = await call(client, "get", "/api/warehouse-mappings", {});
|
|
1225
|
+
out(data, pretty);
|
|
1226
|
+
});
|
|
1227
|
+
var exportsCmd = program.command("exports").description("\u5F02\u6B65\u5BFC\u51FA\u4EFB\u52A1\u76F8\u5173\u64CD\u4F5C");
|
|
1228
|
+
exportsCmd.command("create").description("\u53D1\u8D77\u5F02\u6B65\u5BFC\u51FA\u4EFB\u52A1").requiredOption("--type <type>", "\u5BFC\u51FA\u7C7B\u578B\uFF1ASALES | INVENTORY | PROFIT | STORAGE_FEE | ORDER_RAW").option("--from <date>", "\u8D77\u59CB\u65E5\u671F YYYY-MM-DD").option("--to <date>", "\u7ED3\u675F\u65E5\u671F YYYY-MM-DD").option("--shop <id>", "\u6309\u5E97\u94FA ID \u8FC7\u6EE4").option("--warehouse <id>", "\u6309\u4ED3\u5E93 ID \u8FC7\u6EE4").option("--sku <sku>", "\u6309 SKU \u8FC7\u6EE4\uFF08SALES / INVENTORY\uFF09").option("--group-by <dim>", "\u5229\u6DA6\u805A\u5408\u7EF4\u5EA6\uFF1Aday | warehouse | shop").action(async (opts, cmd) => {
|
|
1229
|
+
const { api, token, pretty } = requireToken(cmd.parent);
|
|
1230
|
+
const client = makeClient(api, token);
|
|
1231
|
+
const validTypes = ["SALES", "INVENTORY", "PROFIT", "STORAGE_FEE", "ORDER_RAW"];
|
|
1232
|
+
if (!validTypes.includes(opts.type)) die(`--type \u5FC5\u987B\u662F\u4EE5\u4E0B\u4E4B\u4E00: ${validTypes.join(" | ")}`);
|
|
1233
|
+
const body = { type: opts.type };
|
|
1234
|
+
if (opts.from) body.from = opts.from;
|
|
1235
|
+
if (opts.to) body.to = opts.to;
|
|
1236
|
+
if (opts.shop) body.shopId = opts.shop;
|
|
1237
|
+
if (opts.warehouse) body.warehouseId = opts.warehouse;
|
|
1238
|
+
if (opts.sku) body.sku = opts.sku;
|
|
1239
|
+
if (opts.groupBy) body.groupBy = opts.groupBy;
|
|
1240
|
+
const data = await call(client, "post", "/api/exports", body);
|
|
1241
|
+
out(data, pretty);
|
|
1242
|
+
});
|
|
1243
|
+
exportsCmd.command("list").description("\u5217\u51FA\u6700\u8FD1 100 \u6761\u5BFC\u51FA\u8BB0\u5F55").action(async (_opts, cmd) => {
|
|
1244
|
+
const { api, token, pretty } = requireToken(cmd.parent);
|
|
1245
|
+
const client = makeClient(api, token);
|
|
1246
|
+
const data = await call(client, "get", "/api/exports", {});
|
|
1247
|
+
out(data, pretty);
|
|
1248
|
+
});
|
|
1249
|
+
exportsCmd.command("get <id>").description("\u67E5\u8BE2\u5355\u6761\u5BFC\u51FA\u4EFB\u52A1\u7684\u72B6\u6001\uFF08\u53EF\u8F6E\u8BE2\u76F4\u5230 status=SUCCESS\uFF09").action(async (id, _opts, cmd) => {
|
|
1250
|
+
const { api, token, pretty } = requireToken(cmd.parent);
|
|
1251
|
+
const client = makeClient(api, token);
|
|
1252
|
+
const data = await call(client, "get", `/api/exports/${id}`, {});
|
|
1253
|
+
out(data, pretty);
|
|
1254
|
+
});
|
|
1255
|
+
exportsCmd.command("download <id>").description("\u4E0B\u8F7D\u5DF2\u5B8C\u6210\u7684\u5BFC\u51FA\u6587\u4EF6\uFF08status=SUCCESS \u540E\u53EF\u7528\uFF09").requiredOption("--output <path>", "\u4FDD\u5B58\u8DEF\u5F84\uFF0C\u4F8B\u5982 ./sales.xlsx").action(async (id, opts, cmd) => {
|
|
1256
|
+
const { api, token } = requireToken(cmd.parent);
|
|
1257
|
+
const client = makeClient(api, token);
|
|
1258
|
+
const job = await call(client, "get", `/api/exports/${id}`, {});
|
|
1259
|
+
if (job.status !== "SUCCESS") die(`\u4EFB\u52A1\u5C1A\u672A\u5B8C\u6210\uFF0C\u5F53\u524D\u72B6\u6001: ${job.status}`, { id, status: job.status });
|
|
1260
|
+
const { createWriteStream } = await import("fs");
|
|
1261
|
+
const res = await client.get(`/api/exports/${id}/download`, { responseType: "stream" });
|
|
1262
|
+
await new Promise((resolve, reject) => {
|
|
1263
|
+
const ws = createWriteStream(opts.output);
|
|
1264
|
+
res.data.pipe(ws);
|
|
1265
|
+
ws.on("finish", resolve);
|
|
1266
|
+
ws.on("error", reject);
|
|
1267
|
+
});
|
|
1268
|
+
process.stderr.write(`\u6587\u4EF6\u5DF2\u4FDD\u5B58\u81F3: ${opts.output}
|
|
1269
|
+
`);
|
|
1270
|
+
out({ ok: true, path: opts.output }, false);
|
|
1271
|
+
});
|
|
1272
|
+
program.parseAsync(process.argv).catch((err) => {
|
|
1273
|
+
die(String(err));
|
|
1274
|
+
});
|
package/package.json
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@yz-xingtu/ops-cli",
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "多多运营后台 CLI — 销售/库存/利润/仓储费查询、主数据管理,通过 OPS_JWT 与云端 API 交互",
|
|
6
|
+
"keywords": ["duoduo", "ops", "cli", "sales", "inventory", "profit"],
|
|
7
|
+
"files": ["dist"],
|
|
8
|
+
"bin": {
|
|
9
|
+
"duoduo-ops": "dist/index.js"
|
|
10
|
+
},
|
|
11
|
+
"publishConfig": {
|
|
12
|
+
"access": "public",
|
|
13
|
+
"registry": "https://registry.npmjs.org/"
|
|
14
|
+
},
|
|
15
|
+
"scripts": {
|
|
16
|
+
"build": "tsup",
|
|
17
|
+
"dev": "tsx src/index.ts",
|
|
18
|
+
"lint": "echo \"(ops-cli) lint skipped\"",
|
|
19
|
+
"typecheck": "tsc --noEmit"
|
|
20
|
+
},
|
|
21
|
+
"dependencies": {
|
|
22
|
+
"axios": "^1.7.7",
|
|
23
|
+
"commander": "^12.1.0",
|
|
24
|
+
"dotenv": "^16.4.5"
|
|
25
|
+
},
|
|
26
|
+
"devDependencies": {
|
|
27
|
+
"@duoduo/shared": "*",
|
|
28
|
+
"@types/node": "^20.12.7",
|
|
29
|
+
"tsup": "^8.3.0",
|
|
30
|
+
"tsx": "^4.7.1",
|
|
31
|
+
"typescript": "^5.4.5"
|
|
32
|
+
},
|
|
33
|
+
"engines": {
|
|
34
|
+
"node": ">=18.18"
|
|
35
|
+
}
|
|
36
|
+
}
|