@metamask/utils 8.0.0 → 8.2.0
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/CHANGELOG.md +25 -1
- package/dist/assert.js +16 -0
- package/dist/assert.js.map +1 -0
- package/dist/assert.mjs +16 -0
- package/dist/assert.mjs.map +1 -0
- package/dist/base64.js +11 -0
- package/dist/base64.js.map +1 -0
- package/dist/base64.mjs +11 -0
- package/dist/base64.mjs.map +1 -0
- package/dist/bytes.js +43 -0
- package/dist/bytes.js.map +1 -0
- package/dist/bytes.mjs +43 -0
- package/dist/bytes.mjs.map +1 -0
- package/dist/caip-types.js +40 -0
- package/dist/caip-types.js.map +1 -0
- package/dist/caip-types.mjs +40 -0
- package/dist/caip-types.mjs.map +1 -0
- package/dist/checksum.js +12 -0
- package/dist/checksum.js.map +1 -0
- package/dist/checksum.mjs +12 -0
- package/dist/checksum.mjs.map +1 -0
- package/dist/chunk-2CY4F77D.mjs +95 -0
- package/dist/chunk-2CY4F77D.mjs.map +1 -0
- package/dist/chunk-2LBGT4GH.js +15 -0
- package/dist/chunk-2LBGT4GH.js.map +1 -0
- package/dist/chunk-2TBCL6EF.js +1 -0
- package/dist/chunk-2TBCL6EF.js.map +1 -0
- package/dist/chunk-3W5G4CYI.js +25 -0
- package/dist/chunk-3W5G4CYI.js.map +1 -0
- package/dist/chunk-4JNG5UTO.js +44 -0
- package/dist/chunk-4JNG5UTO.js.map +1 -0
- package/dist/chunk-4RMX5YWE.js +34 -0
- package/dist/chunk-4RMX5YWE.js.map +1 -0
- package/dist/chunk-5WQRFJVQ.js +257 -0
- package/dist/chunk-5WQRFJVQ.js.map +1 -0
- package/dist/chunk-65KBH4FZ.mjs +257 -0
- package/dist/chunk-65KBH4FZ.mjs.map +1 -0
- package/dist/chunk-7S5AWFX7.mjs +61 -0
- package/dist/chunk-7S5AWFX7.mjs.map +1 -0
- package/dist/chunk-AY6FDCBT.mjs +1 -0
- package/dist/chunk-AY6FDCBT.mjs.map +1 -0
- package/dist/chunk-BKR45GQY.mjs +273 -0
- package/dist/chunk-BKR45GQY.mjs.map +1 -0
- package/dist/chunk-CSDMTDND.mjs +59 -0
- package/dist/chunk-CSDMTDND.mjs.map +1 -0
- package/dist/chunk-DMPG3H3G.js +61 -0
- package/dist/chunk-DMPG3H3G.js.map +1 -0
- package/dist/chunk-DYMCEQIV.mjs +1 -0
- package/dist/chunk-DYMCEQIV.mjs.map +1 -0
- package/dist/chunk-EQMZL4XU.js +1 -0
- package/dist/chunk-EQMZL4XU.js.map +1 -0
- package/dist/chunk-G66TREC6.mjs +69 -0
- package/dist/chunk-G66TREC6.mjs.map +1 -0
- package/dist/chunk-H4YFDLB7.mjs +70 -0
- package/dist/chunk-H4YFDLB7.mjs.map +1 -0
- package/dist/chunk-HLDWS2JR.mjs +35 -0
- package/dist/chunk-HLDWS2JR.mjs.map +1 -0
- package/dist/chunk-I575FZFH.mjs +1 -0
- package/dist/chunk-I575FZFH.mjs.map +1 -0
- package/dist/chunk-JCYFTGED.js +95 -0
- package/dist/chunk-JCYFTGED.js.map +1 -0
- package/dist/chunk-JPAL7Q5S.mjs +90 -0
- package/dist/chunk-JPAL7Q5S.mjs.map +1 -0
- package/dist/chunk-LC2CRSWD.js +1 -0
- package/dist/chunk-LC2CRSWD.js.map +1 -0
- package/dist/chunk-N53OILN6.mjs +16 -0
- package/dist/chunk-N53OILN6.mjs.map +1 -0
- package/dist/chunk-QVEKZRZ2.js +70 -0
- package/dist/chunk-QVEKZRZ2.js.map +1 -0
- package/dist/chunk-RIRDOQPX.mjs +15 -0
- package/dist/chunk-RIRDOQPX.mjs.map +1 -0
- package/dist/chunk-RKRGAFXY.js +1 -0
- package/dist/chunk-RKRGAFXY.js.map +1 -0
- package/dist/chunk-RRYOWRCV.mjs +1 -0
- package/dist/chunk-RRYOWRCV.mjs.map +1 -0
- package/dist/chunk-TGOMTREC.mjs +75 -0
- package/dist/chunk-TGOMTREC.mjs.map +1 -0
- package/dist/chunk-THNLGEXV.mjs +34 -0
- package/dist/chunk-THNLGEXV.mjs.map +1 -0
- package/dist/chunk-U7ZUGCE7.js +75 -0
- package/dist/chunk-U7ZUGCE7.js.map +1 -0
- package/dist/chunk-UOTVU7OQ.js +1 -0
- package/dist/chunk-UOTVU7OQ.js.map +1 -0
- package/dist/chunk-VBYRLAP6.js +59 -0
- package/dist/chunk-VBYRLAP6.js.map +1 -0
- package/dist/chunk-VL5DYXC5.mjs +122 -0
- package/dist/chunk-VL5DYXC5.mjs.map +1 -0
- package/dist/chunk-VSEW3V2L.mjs +44 -0
- package/dist/chunk-VSEW3V2L.mjs.map +1 -0
- package/dist/chunk-WA4DHIND.mjs +1 -0
- package/dist/chunk-WA4DHIND.mjs.map +1 -0
- package/dist/chunk-WQDTPIQK.js +122 -0
- package/dist/chunk-WQDTPIQK.js.map +1 -0
- package/dist/chunk-WRBN6DWM.js +35 -0
- package/dist/chunk-WRBN6DWM.js.map +1 -0
- package/dist/chunk-X66SUIEF.mjs +25 -0
- package/dist/chunk-X66SUIEF.mjs.map +1 -0
- package/dist/chunk-XYYGZF5Y.js +16 -0
- package/dist/chunk-XYYGZF5Y.js.map +1 -0
- package/dist/chunk-YQJBIHC7.js +273 -0
- package/dist/chunk-YQJBIHC7.js.map +1 -0
- package/dist/chunk-Z2RGWDD7.js +90 -0
- package/dist/chunk-Z2RGWDD7.js.map +1 -0
- package/dist/chunk-ZRNIY7OW.js +69 -0
- package/dist/chunk-ZRNIY7OW.js.map +1 -0
- package/dist/coercers.js +18 -0
- package/dist/coercers.js.map +1 -0
- package/dist/coercers.mjs +18 -0
- package/dist/coercers.mjs.map +1 -0
- package/dist/collections.js +10 -0
- package/dist/collections.js.map +1 -0
- package/dist/collections.mjs +10 -0
- package/dist/collections.mjs.map +1 -0
- package/dist/encryption-types.js +2 -0
- package/dist/encryption-types.js.map +1 -0
- package/dist/encryption-types.mjs +2 -0
- package/dist/encryption-types.mjs.map +1 -0
- package/dist/errors.js +17 -0
- package/dist/errors.js.map +1 -0
- package/dist/errors.mjs +17 -0
- package/dist/errors.mjs.map +1 -0
- package/dist/fs.js +26 -0
- package/dist/fs.js.map +1 -0
- package/dist/fs.mjs +26 -0
- package/dist/fs.mjs.map +1 -0
- package/dist/hex.js +35 -0
- package/dist/hex.js.map +1 -0
- package/dist/hex.mjs +35 -0
- package/dist/hex.mjs.map +1 -0
- package/dist/index.js +289 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +289 -0
- package/dist/index.mjs.map +1 -0
- package/dist/json.js +75 -0
- package/dist/json.js.map +1 -0
- package/dist/json.mjs +75 -0
- package/dist/json.mjs.map +1 -0
- package/dist/keyring.js +2 -0
- package/dist/keyring.js.map +1 -0
- package/dist/keyring.mjs +2 -0
- package/dist/keyring.mjs.map +1 -0
- package/dist/logging.js +10 -0
- package/dist/logging.js.map +1 -0
- package/dist/logging.mjs +10 -0
- package/dist/logging.mjs.map +1 -0
- package/dist/misc.js +28 -0
- package/dist/misc.js.map +1 -0
- package/dist/misc.mjs +28 -0
- package/dist/misc.mjs.map +1 -0
- package/dist/node.js +309 -0
- package/dist/node.js.map +1 -0
- package/dist/node.mjs +309 -0
- package/dist/node.mjs.map +1 -0
- package/dist/number.js +18 -0
- package/dist/number.js.map +1 -0
- package/dist/number.mjs +18 -0
- package/dist/number.mjs.map +1 -0
- package/dist/opaque.js +2 -0
- package/dist/opaque.js.map +1 -0
- package/dist/opaque.mjs +2 -0
- package/dist/opaque.mjs.map +1 -0
- package/dist/time.js +12 -0
- package/dist/time.js.map +1 -0
- package/dist/time.mjs +12 -0
- package/dist/time.mjs.map +1 -0
- package/dist/transaction-types.js +2 -0
- package/dist/transaction-types.js.map +1 -0
- package/dist/transaction-types.mjs +2 -0
- package/dist/transaction-types.mjs.map +1 -0
- package/dist/types/assert.d.ts.map +1 -1
- package/dist/types/bytes.d.ts +14 -0
- package/dist/types/bytes.d.ts.map +1 -1
- package/dist/types/errors.d.ts +58 -0
- package/dist/types/errors.d.ts.map +1 -0
- package/dist/types/fs.d.ts +133 -0
- package/dist/types/fs.d.ts.map +1 -0
- package/dist/types/index.d.ts +1 -0
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/json.d.ts +83 -47
- package/dist/types/json.d.ts.map +1 -1
- package/dist/types/node.d.ts +3 -0
- package/dist/types/node.d.ts.map +1 -0
- package/dist/versions.js +27 -0
- package/dist/versions.js.map +1 -0
- package/dist/versions.mjs +27 -0
- package/dist/versions.mjs.map +1 -0
- package/package.json +21 -20
- package/dist/cjs/assert.js +0 -116
- package/dist/cjs/assert.js.map +0 -1
- package/dist/cjs/base64.js +0 -32
- package/dist/cjs/base64.js.map +0 -1
- package/dist/cjs/bytes.js +0 -255
- package/dist/cjs/bytes.js.map +0 -1
- package/dist/cjs/caip-types.js +0 -115
- package/dist/cjs/caip-types.js.map +0 -1
- package/dist/cjs/checksum.js +0 -17
- package/dist/cjs/checksum.js.map +0 -1
- package/dist/cjs/coercers.js +0 -96
- package/dist/cjs/coercers.js.map +0 -1
- package/dist/cjs/collections.js +0 -157
- package/dist/cjs/collections.js.map +0 -1
- package/dist/cjs/encryption-types.js +0 -6
- package/dist/cjs/encryption-types.js.map +0 -1
- package/dist/cjs/hex.js +0 -107
- package/dist/cjs/hex.js.map +0 -1
- package/dist/cjs/index.js +0 -37
- package/dist/cjs/index.js.map +0 -1
- package/dist/cjs/json.js +0 -263
- package/dist/cjs/json.js.map +0 -1
- package/dist/cjs/keyring.js +0 -6
- package/dist/cjs/keyring.js.map +0 -1
- package/dist/cjs/logging.js +0 -33
- package/dist/cjs/logging.js.map +0 -1
- package/dist/cjs/misc.js +0 -111
- package/dist/cjs/misc.js.map +0 -1
- package/dist/cjs/number.js +0 -52
- package/dist/cjs/number.js.map +0 -1
- package/dist/cjs/opaque.js +0 -8
- package/dist/cjs/opaque.js.map +0 -1
- package/dist/cjs/time.js +0 -63
- package/dist/cjs/time.js.map +0 -1
- package/dist/cjs/transaction-types.js +0 -6
- package/dist/cjs/transaction-types.js.map +0 -1
- package/dist/cjs/versions.js +0 -79
- package/dist/cjs/versions.js.map +0 -1
- package/dist/esm/assert.js +0 -133
- package/dist/esm/assert.js.map +0 -1
- package/dist/esm/base64.js +0 -28
- package/dist/esm/base64.js.map +0 -1
- package/dist/esm/bytes.js +0 -334
- package/dist/esm/bytes.js.map +0 -1
- package/dist/esm/caip-types.js +0 -99
- package/dist/esm/caip-types.js.map +0 -1
- package/dist/esm/checksum.js +0 -7
- package/dist/esm/checksum.js.map +0 -1
- package/dist/esm/coercers.js +0 -153
- package/dist/esm/coercers.js.map +0 -1
- package/dist/esm/collections.js +0 -140
- package/dist/esm/collections.js.map +0 -1
- package/dist/esm/encryption-types.js +0 -3
- package/dist/esm/encryption-types.js.map +0 -1
- package/dist/esm/hex.js +0 -108
- package/dist/esm/hex.js.map +0 -1
- package/dist/esm/index.js +0 -20
- package/dist/esm/index.js.map +0 -1
- package/dist/esm/json.js +0 -301
- package/dist/esm/json.js.map +0 -1
- package/dist/esm/keyring.js +0 -3
- package/dist/esm/keyring.js.map +0 -1
- package/dist/esm/logging.js +0 -34
- package/dist/esm/logging.js.map +0 -1
- package/dist/esm/misc.js +0 -129
- package/dist/esm/misc.js.map +0 -1
- package/dist/esm/number.js +0 -92
- package/dist/esm/number.js.map +0 -1
- package/dist/esm/opaque.js +0 -5
- package/dist/esm/opaque.js.map +0 -1
- package/dist/esm/package.json +0 -1
- package/dist/esm/time.js +0 -53
- package/dist/esm/time.js.map +0 -1
- package/dist/esm/transaction-types.js +0 -3
- package/dist/esm/transaction-types.js.map +0 -1
- package/dist/esm/versions.js +0 -78
- package/dist/esm/versions.js.map +0 -1
- package/dist/types/caip-types.test-d.d.ts +0 -2
- package/dist/types/caip-types.test-d.d.ts.map +0 -1
- package/dist/types/hex.test-d.d.ts +0 -2
- package/dist/types/hex.test-d.d.ts.map +0 -1
- package/dist/types/json.test-d.d.ts +0 -2
- package/dist/types/json.test-d.d.ts.map +0 -1
- package/dist/types/misc.test-d.d.ts +0 -2
- package/dist/types/misc.test-d.d.ts.map +0 -1
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }// src/caip-types.ts
|
|
2
|
+
var _superstruct = require('superstruct');
|
|
3
|
+
var CAIP_CHAIN_ID_REGEX = /^(?<namespace>[-a-z0-9]{3,8}):(?<reference>[-_a-zA-Z0-9]{1,32})$/u;
|
|
4
|
+
var CAIP_NAMESPACE_REGEX = /^[-a-z0-9]{3,8}$/u;
|
|
5
|
+
var CAIP_REFERENCE_REGEX = /^[-_a-zA-Z0-9]{1,32}$/u;
|
|
6
|
+
var CAIP_ACCOUNT_ID_REGEX = /^(?<chainId>(?<namespace>[-a-z0-9]{3,8}):(?<reference>[-_a-zA-Z0-9]{1,32})):(?<accountAddress>[-.%a-zA-Z0-9]{1,128})$/u;
|
|
7
|
+
var CAIP_ACCOUNT_ADDRESS_REGEX = /^[-.%a-zA-Z0-9]{1,128}$/u;
|
|
8
|
+
var CaipChainIdStruct = _superstruct.pattern.call(void 0, _superstruct.string.call(void 0, ), CAIP_CHAIN_ID_REGEX);
|
|
9
|
+
var CaipNamespaceStruct = _superstruct.pattern.call(void 0, _superstruct.string.call(void 0, ), CAIP_NAMESPACE_REGEX);
|
|
10
|
+
var CaipReferenceStruct = _superstruct.pattern.call(void 0, _superstruct.string.call(void 0, ), CAIP_REFERENCE_REGEX);
|
|
11
|
+
var CaipAccountIdStruct = _superstruct.pattern.call(void 0, _superstruct.string.call(void 0, ), CAIP_ACCOUNT_ID_REGEX);
|
|
12
|
+
var CaipAccountAddressStruct = _superstruct.pattern.call(void 0,
|
|
13
|
+
_superstruct.string.call(void 0, ),
|
|
14
|
+
CAIP_ACCOUNT_ADDRESS_REGEX
|
|
15
|
+
);
|
|
16
|
+
function isCaipChainId(value) {
|
|
17
|
+
return _superstruct.is.call(void 0, value, CaipChainIdStruct);
|
|
18
|
+
}
|
|
19
|
+
function isCaipNamespace(value) {
|
|
20
|
+
return _superstruct.is.call(void 0, value, CaipNamespaceStruct);
|
|
21
|
+
}
|
|
22
|
+
function isCaipReference(value) {
|
|
23
|
+
return _superstruct.is.call(void 0, value, CaipReferenceStruct);
|
|
24
|
+
}
|
|
25
|
+
function isCaipAccountId(value) {
|
|
26
|
+
return _superstruct.is.call(void 0, value, CaipAccountIdStruct);
|
|
27
|
+
}
|
|
28
|
+
function isCaipAccountAddress(value) {
|
|
29
|
+
return _superstruct.is.call(void 0, value, CaipAccountAddressStruct);
|
|
30
|
+
}
|
|
31
|
+
function parseCaipChainId(caipChainId) {
|
|
32
|
+
const match = CAIP_CHAIN_ID_REGEX.exec(caipChainId);
|
|
33
|
+
if (!_optionalChain([match, 'optionalAccess', _ => _.groups])) {
|
|
34
|
+
throw new Error("Invalid CAIP chain ID.");
|
|
35
|
+
}
|
|
36
|
+
return {
|
|
37
|
+
namespace: match.groups.namespace,
|
|
38
|
+
reference: match.groups.reference
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
function parseCaipAccountId(caipAccountId) {
|
|
42
|
+
const match = CAIP_ACCOUNT_ID_REGEX.exec(caipAccountId);
|
|
43
|
+
if (!_optionalChain([match, 'optionalAccess', _2 => _2.groups])) {
|
|
44
|
+
throw new Error("Invalid CAIP account ID.");
|
|
45
|
+
}
|
|
46
|
+
return {
|
|
47
|
+
address: match.groups.accountAddress,
|
|
48
|
+
chainId: match.groups.chainId,
|
|
49
|
+
chain: {
|
|
50
|
+
namespace: match.groups.namespace,
|
|
51
|
+
reference: match.groups.reference
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
exports.CAIP_CHAIN_ID_REGEX = CAIP_CHAIN_ID_REGEX; exports.CAIP_NAMESPACE_REGEX = CAIP_NAMESPACE_REGEX; exports.CAIP_REFERENCE_REGEX = CAIP_REFERENCE_REGEX; exports.CAIP_ACCOUNT_ID_REGEX = CAIP_ACCOUNT_ID_REGEX; exports.CAIP_ACCOUNT_ADDRESS_REGEX = CAIP_ACCOUNT_ADDRESS_REGEX; exports.CaipChainIdStruct = CaipChainIdStruct; exports.CaipNamespaceStruct = CaipNamespaceStruct; exports.CaipReferenceStruct = CaipReferenceStruct; exports.CaipAccountIdStruct = CaipAccountIdStruct; exports.CaipAccountAddressStruct = CaipAccountAddressStruct; exports.isCaipChainId = isCaipChainId; exports.isCaipNamespace = isCaipNamespace; exports.isCaipReference = isCaipReference; exports.isCaipAccountId = isCaipAccountId; exports.isCaipAccountAddress = isCaipAccountAddress; exports.parseCaipChainId = parseCaipChainId; exports.parseCaipAccountId = parseCaipAccountId;
|
|
75
|
+
//# sourceMappingURL=chunk-U7ZUGCE7.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/caip-types.ts"],"names":[],"mappings":";AACA,SAAS,IAAI,SAAS,cAAc;AAE7B,IAAM,sBACX;AAEK,IAAM,uBAAuB;AAE7B,IAAM,uBAAuB;AAE7B,IAAM,wBACX;AAEK,IAAM,6BAA6B;AAKnC,IAAM,oBAAoB,QAAQ,OAAO,GAAG,mBAAmB;AAM/D,IAAM,sBAAsB,QAAQ,OAAO,GAAG,oBAAoB;AAMlE,IAAM,sBAAsB,QAAQ,OAAO,GAAG,oBAAoB;AAMlE,IAAM,sBAAsB,QAAQ,OAAO,GAAG,qBAAqB;AAMnE,IAAM,2BAA2B;AAAA,EACtC,OAAO;AAAA,EACP;AACF;AASO,SAAS,cAAc,OAAsC;AAClE,SAAO,GAAG,OAAO,iBAAiB;AACpC;AAQO,SAAS,gBAAgB,OAAwC;AACtE,SAAO,GAAG,OAAO,mBAAmB;AACtC;AAQO,SAAS,gBAAgB,OAAwC;AACtE,SAAO,GAAG,OAAO,mBAAmB;AACtC;AAQO,SAAS,gBAAgB,OAAwC;AACtE,SAAO,GAAG,OAAO,mBAAmB;AACtC;AAQO,SAAS,qBACd,OAC6B;AAC7B,SAAO,GAAG,OAAO,wBAAwB;AAC3C;AASO,SAAS,iBAAiB,aAG/B;AACA,QAAM,QAAQ,oBAAoB,KAAK,WAAW;AAClD,MAAI,CAAC,OAAO,QAAQ;AAClB,UAAM,IAAI,MAAM,wBAAwB;AAAA,EAC1C;AAEA,SAAO;AAAA,IACL,WAAW,MAAM,OAAO;AAAA,IACxB,WAAW,MAAM,OAAO;AAAA,EAC1B;AACF;AASO,SAAS,mBAAmB,eAIjC;AACA,QAAM,QAAQ,sBAAsB,KAAK,aAAa;AACtD,MAAI,CAAC,OAAO,QAAQ;AAClB,UAAM,IAAI,MAAM,0BAA0B;AAAA,EAC5C;AAEA,SAAO;AAAA,IACL,SAAS,MAAM,OAAO;AAAA,IACtB,SAAS,MAAM,OAAO;AAAA,IACtB,OAAO;AAAA,MACL,WAAW,MAAM,OAAO;AAAA,MACxB,WAAW,MAAM,OAAO;AAAA,IAC1B;AAAA,EACF;AACF","sourcesContent":["import type { Infer } from 'superstruct';\nimport { is, pattern, string } from 'superstruct';\n\nexport const CAIP_CHAIN_ID_REGEX =\n /^(?<namespace>[-a-z0-9]{3,8}):(?<reference>[-_a-zA-Z0-9]{1,32})$/u;\n\nexport const CAIP_NAMESPACE_REGEX = /^[-a-z0-9]{3,8}$/u;\n\nexport const CAIP_REFERENCE_REGEX = /^[-_a-zA-Z0-9]{1,32}$/u;\n\nexport const CAIP_ACCOUNT_ID_REGEX =\n /^(?<chainId>(?<namespace>[-a-z0-9]{3,8}):(?<reference>[-_a-zA-Z0-9]{1,32})):(?<accountAddress>[-.%a-zA-Z0-9]{1,128})$/u;\n\nexport const CAIP_ACCOUNT_ADDRESS_REGEX = /^[-.%a-zA-Z0-9]{1,128}$/u;\n\n/**\n * A CAIP-2 chain ID, i.e., a human-readable namespace and reference.\n */\nexport const CaipChainIdStruct = pattern(string(), CAIP_CHAIN_ID_REGEX);\nexport type CaipChainId = `${string}:${string}`;\n\n/**\n * A CAIP-2 namespace, i.e., the first part of a CAIP chain ID.\n */\nexport const CaipNamespaceStruct = pattern(string(), CAIP_NAMESPACE_REGEX);\nexport type CaipNamespace = Infer<typeof CaipNamespaceStruct>;\n\n/**\n * A CAIP-2 reference, i.e., the second part of a CAIP chain ID.\n */\nexport const CaipReferenceStruct = pattern(string(), CAIP_REFERENCE_REGEX);\nexport type CaipReference = Infer<typeof CaipReferenceStruct>;\n\n/**\n * A CAIP-10 account ID, i.e., a human-readable namespace, reference, and account address.\n */\nexport const CaipAccountIdStruct = pattern(string(), CAIP_ACCOUNT_ID_REGEX);\nexport type CaipAccountId = `${string}:${string}:${string}`;\n\n/**\n * A CAIP-10 account address, i.e., the third part of the CAIP account ID.\n */\nexport const CaipAccountAddressStruct = pattern(\n string(),\n CAIP_ACCOUNT_ADDRESS_REGEX,\n);\nexport type CaipAccountAddress = Infer<typeof CaipAccountAddressStruct>;\n\n/**\n * Check if the given value is a {@link CaipChainId}.\n *\n * @param value - The value to check.\n * @returns Whether the value is a {@link CaipChainId}.\n */\nexport function isCaipChainId(value: unknown): value is CaipChainId {\n return is(value, CaipChainIdStruct);\n}\n\n/**\n * Check if the given value is a {@link CaipNamespace}.\n *\n * @param value - The value to check.\n * @returns Whether the value is a {@link CaipNamespace}.\n */\nexport function isCaipNamespace(value: unknown): value is CaipNamespace {\n return is(value, CaipNamespaceStruct);\n}\n\n/**\n * Check if the given value is a {@link CaipReference}.\n *\n * @param value - The value to check.\n * @returns Whether the value is a {@link CaipReference}.\n */\nexport function isCaipReference(value: unknown): value is CaipReference {\n return is(value, CaipReferenceStruct);\n}\n\n/**\n * Check if the given value is a {@link CaipAccountId}.\n *\n * @param value - The value to check.\n * @returns Whether the value is a {@link CaipAccountId}.\n */\nexport function isCaipAccountId(value: unknown): value is CaipAccountId {\n return is(value, CaipAccountIdStruct);\n}\n\n/**\n * Check if a value is a {@link CaipAccountAddress}.\n *\n * @param value - The value to validate.\n * @returns True if the value is a valid {@link CaipAccountAddress}.\n */\nexport function isCaipAccountAddress(\n value: unknown,\n): value is CaipAccountAddress {\n return is(value, CaipAccountAddressStruct);\n}\n\n/**\n * Parse a CAIP-2 chain ID to an object containing the namespace and reference.\n * This validates the CAIP-2 chain ID before parsing it.\n *\n * @param caipChainId - The CAIP-2 chain ID to validate and parse.\n * @returns The parsed CAIP-2 chain ID.\n */\nexport function parseCaipChainId(caipChainId: CaipChainId): {\n namespace: CaipNamespace;\n reference: CaipReference;\n} {\n const match = CAIP_CHAIN_ID_REGEX.exec(caipChainId);\n if (!match?.groups) {\n throw new Error('Invalid CAIP chain ID.');\n }\n\n return {\n namespace: match.groups.namespace as CaipNamespace,\n reference: match.groups.reference as CaipReference,\n };\n}\n\n/**\n * Parse an CAIP-10 account ID to an object containing the chain ID, parsed chain ID, and account address.\n * This validates the CAIP-10 account ID before parsing it.\n *\n * @param caipAccountId - The CAIP-10 account ID to validate and parse.\n * @returns The parsed CAIP-10 account ID.\n */\nexport function parseCaipAccountId(caipAccountId: CaipAccountId): {\n address: CaipAccountAddress;\n chainId: CaipChainId;\n chain: { namespace: CaipNamespace; reference: CaipReference };\n} {\n const match = CAIP_ACCOUNT_ID_REGEX.exec(caipAccountId);\n if (!match?.groups) {\n throw new Error('Invalid CAIP account ID.');\n }\n\n return {\n address: match.groups.accountAddress as CaipAccountAddress,\n chainId: match.groups.chainId as CaipChainId,\n chain: {\n namespace: match.groups.namespace as CaipNamespace,\n reference: match.groups.reference as CaipReference,\n },\n };\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";//# sourceMappingURL=chunk-UOTVU7OQ.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"names":[],"mappings":""}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }
|
|
2
|
+
|
|
3
|
+
var _chunkDMPG3H3Gjs = require('./chunk-DMPG3H3G.js');
|
|
4
|
+
|
|
5
|
+
// src/assert.ts
|
|
6
|
+
var _superstruct = require('superstruct');
|
|
7
|
+
function isConstructable(fn) {
|
|
8
|
+
return Boolean(typeof _optionalChain([fn, 'optionalAccess', _ => _.prototype, 'optionalAccess', _2 => _2.constructor, 'optionalAccess', _3 => _3.name]) === "string");
|
|
9
|
+
}
|
|
10
|
+
function getErrorMessageWithoutTrailingPeriod(error) {
|
|
11
|
+
return _chunkDMPG3H3Gjs.getErrorMessage.call(void 0, error).replace(/\.$/u, "");
|
|
12
|
+
}
|
|
13
|
+
function getError(ErrorWrapper, message) {
|
|
14
|
+
if (isConstructable(ErrorWrapper)) {
|
|
15
|
+
return new ErrorWrapper({
|
|
16
|
+
message
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
return ErrorWrapper({
|
|
20
|
+
message
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
var AssertionError = class extends Error {
|
|
24
|
+
constructor(options) {
|
|
25
|
+
super(options.message);
|
|
26
|
+
this.code = "ERR_ASSERTION";
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
function assert(value, message = "Assertion failed.", ErrorWrapper = AssertionError) {
|
|
30
|
+
if (!value) {
|
|
31
|
+
if (message instanceof Error) {
|
|
32
|
+
throw message;
|
|
33
|
+
}
|
|
34
|
+
throw getError(ErrorWrapper, message);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
function assertStruct(value, struct, errorPrefix = "Assertion failed", ErrorWrapper = AssertionError) {
|
|
38
|
+
try {
|
|
39
|
+
_superstruct.assert.call(void 0, value, struct);
|
|
40
|
+
} catch (error) {
|
|
41
|
+
throw getError(
|
|
42
|
+
ErrorWrapper,
|
|
43
|
+
`${errorPrefix}: ${getErrorMessageWithoutTrailingPeriod(error)}.`
|
|
44
|
+
);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
function assertExhaustive(_object) {
|
|
48
|
+
throw new Error(
|
|
49
|
+
"Invalid branch reached. Should be detected during compilation."
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
exports.AssertionError = AssertionError; exports.assert = assert; exports.assertStruct = assertStruct; exports.assertExhaustive = assertExhaustive;
|
|
59
|
+
//# sourceMappingURL=chunk-VBYRLAP6.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/assert.ts"],"names":[],"mappings":";;;;;AACA,SAAS,UAAU,yBAAyB;AAe5C,SAAS,gBACP,IACgD;AAEhD,SAAO,QAAQ,OAAO,IAAI,WAAW,aAAa,SAAS,QAAQ;AACrE;AAYA,SAAS,qCAAqC,OAAwB;AAEpE,SAAO,gBAAgB,KAAK,EAAE,QAAQ,QAAQ,EAAE;AAClD;AAUA,SAAS,SAAS,cAAyC,SAAiB;AAC1E,MAAI,gBAAgB,YAAY,GAAG;AACjC,WAAO,IAAI,aAAa;AAAA,MACtB;AAAA,IACF,CAAC;AAAA,EACH;AACA,SAAO,aAAa;AAAA,IAClB;AAAA,EACF,CAAC;AACH;AAKO,IAAM,iBAAN,cAA6B,MAAM;AAAA,EAGxC,YAAY,SAA8B;AACxC,UAAM,QAAQ,OAAO;AAHvB,SAAS,OAAO;AAAA,EAIhB;AACF;AAcO,SAAS,OACd,OACA,UAA0B,qBAE1B,eAA0C,gBAC3B;AACf,MAAI,CAAC,OAAO;AACV,QAAI,mBAAmB,OAAO;AAC5B,YAAM;AAAA,IACR;AAEA,UAAM,SAAS,cAAc,OAAO;AAAA,EACtC;AACF;AAaO,SAAS,aACd,OACA,QACA,cAAc,oBAEd,eAA0C,gBACnB;AACvB,MAAI;AACF,sBAAkB,OAAO,MAAM;AAAA,EACjC,SAAS,OAAO;AACd,UAAM;AAAA,MACJ;AAAA,MACA,GAAG,WAAW,KAAK,qCAAqC,KAAK,CAAC;AAAA,IAChE;AAAA,EACF;AACF;AAqBO,SAAS,iBAAiB,SAAuB;AACtD,QAAM,IAAI;AAAA,IACR;AAAA,EACF;AACF","sourcesContent":["import type { Struct } from 'superstruct';\nimport { assert as assertSuperstruct } from 'superstruct';\n\nimport { getErrorMessage } from './errors';\n\nexport type AssertionErrorConstructor =\n | (new (args: { message: string }) => Error)\n | ((args: { message: string }) => Error);\n\n/**\n * Check if a value is a constructor, i.e., a function that can be called with\n * the `new` keyword.\n *\n * @param fn - The value to check.\n * @returns `true` if the value is a constructor, or `false` otherwise.\n */\nfunction isConstructable(\n fn: AssertionErrorConstructor,\n): fn is new (args: { message: string }) => Error {\n /* istanbul ignore next */\n return Boolean(typeof fn?.prototype?.constructor?.name === 'string');\n}\n\n/**\n * Attempts to obtain the message from a possible error object. If it is\n * possible to do so, any trailing period will be removed from the message;\n * otherwise an empty string is returned.\n *\n * @param error - The error object to get the message from.\n * @returns The message without any trailing period if `error` is an object\n * with a `message` property; the string version of `error` without any trailing\n * period if it is not `undefined` or `null`; otherwise an empty string.\n */\nfunction getErrorMessageWithoutTrailingPeriod(error: unknown): string {\n // We'll add our own period.\n return getErrorMessage(error).replace(/\\.$/u, '');\n}\n\n/**\n * Initialise an {@link AssertionErrorConstructor} error.\n *\n * @param ErrorWrapper - The error class to use.\n * @param message - The error message.\n * @returns The error object.\n */\n// eslint-disable-next-line @typescript-eslint/naming-convention\nfunction getError(ErrorWrapper: AssertionErrorConstructor, message: string) {\n if (isConstructable(ErrorWrapper)) {\n return new ErrorWrapper({\n message,\n });\n }\n return ErrorWrapper({\n message,\n });\n}\n\n/**\n * The default error class that is thrown if an assertion fails.\n */\nexport class AssertionError extends Error {\n readonly code = 'ERR_ASSERTION';\n\n constructor(options: { message: string }) {\n super(options.message);\n }\n}\n\n/**\n * Same as Node.js assert.\n * If the value is falsy, throws an error, does nothing otherwise.\n *\n * @throws {@link AssertionError} If value is falsy.\n * @param value - The test that should be truthy to pass.\n * @param message - Message to be passed to {@link AssertionError} or an\n * {@link Error} instance to throw.\n * @param ErrorWrapper - The error class to throw if the assertion fails.\n * Defaults to {@link AssertionError}. If a custom error class is provided for\n * the `message` argument, this argument is ignored.\n */\nexport function assert(\n value: any,\n message: string | Error = 'Assertion failed.',\n // eslint-disable-next-line @typescript-eslint/naming-convention\n ErrorWrapper: AssertionErrorConstructor = AssertionError,\n): asserts value {\n if (!value) {\n if (message instanceof Error) {\n throw message;\n }\n\n throw getError(ErrorWrapper, message);\n }\n}\n\n/**\n * Assert a value against a Superstruct struct.\n *\n * @param value - The value to validate.\n * @param struct - The struct to validate against.\n * @param errorPrefix - A prefix to add to the error message. Defaults to\n * \"Assertion failed\".\n * @param ErrorWrapper - The error class to throw if the assertion fails.\n * Defaults to {@link AssertionError}.\n * @throws If the value is not valid.\n */\nexport function assertStruct<Type, Schema>(\n value: unknown,\n struct: Struct<Type, Schema>,\n errorPrefix = 'Assertion failed',\n // eslint-disable-next-line @typescript-eslint/naming-convention\n ErrorWrapper: AssertionErrorConstructor = AssertionError,\n): asserts value is Type {\n try {\n assertSuperstruct(value, struct);\n } catch (error) {\n throw getError(\n ErrorWrapper,\n `${errorPrefix}: ${getErrorMessageWithoutTrailingPeriod(error)}.`,\n );\n }\n}\n\n/**\n * Use in the default case of a switch that you want to be fully exhaustive.\n * Using this function forces the compiler to enforce exhaustivity during\n * compile-time.\n *\n * @example\n * ```\n * const number = 1;\n * switch (number) {\n * case 0:\n * ...\n * case 1:\n * ...\n * default:\n * assertExhaustive(snapPrefix);\n * }\n * ```\n * @param _object - The object on which the switch is being operated.\n */\nexport function assertExhaustive(_object: never): never {\n throw new Error(\n 'Invalid branch reached. Should be detected during compilation.',\n );\n}\n"]}
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import {
|
|
2
|
+
isErrorWithCode,
|
|
3
|
+
wrapError
|
|
4
|
+
} from "./chunk-7S5AWFX7.mjs";
|
|
5
|
+
|
|
6
|
+
// src/fs.ts
|
|
7
|
+
import fs from "fs";
|
|
8
|
+
import os from "os";
|
|
9
|
+
import path from "path";
|
|
10
|
+
async function readFile(filePath) {
|
|
11
|
+
try {
|
|
12
|
+
return await fs.promises.readFile(filePath, "utf8");
|
|
13
|
+
} catch (error) {
|
|
14
|
+
throw wrapError(error, `Could not read file '${filePath}'`);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
async function writeFile(filePath, content) {
|
|
18
|
+
try {
|
|
19
|
+
await fs.promises.mkdir(path.dirname(filePath), { recursive: true });
|
|
20
|
+
await fs.promises.writeFile(filePath, content);
|
|
21
|
+
} catch (error) {
|
|
22
|
+
throw wrapError(error, `Could not write file '${filePath}'`);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
async function readJsonFile(filePath, {
|
|
26
|
+
parser = JSON
|
|
27
|
+
} = {}) {
|
|
28
|
+
try {
|
|
29
|
+
const content = await fs.promises.readFile(filePath, "utf8");
|
|
30
|
+
return parser.parse(content);
|
|
31
|
+
} catch (error) {
|
|
32
|
+
throw wrapError(error, `Could not read JSON file '${filePath}'`);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
async function writeJsonFile(filePath, jsonValue, {
|
|
36
|
+
stringifier = JSON,
|
|
37
|
+
prettify = false
|
|
38
|
+
} = {}) {
|
|
39
|
+
try {
|
|
40
|
+
await fs.promises.mkdir(path.dirname(filePath), { recursive: true });
|
|
41
|
+
const json = prettify ? stringifier.stringify(jsonValue, null, " ") : stringifier.stringify(jsonValue);
|
|
42
|
+
await fs.promises.writeFile(filePath, json);
|
|
43
|
+
} catch (error) {
|
|
44
|
+
throw wrapError(error, `Could not write JSON file '${filePath}'`);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
async function fileExists(filePath) {
|
|
48
|
+
try {
|
|
49
|
+
const stats = await fs.promises.stat(filePath);
|
|
50
|
+
return stats.isFile();
|
|
51
|
+
} catch (error) {
|
|
52
|
+
if (isErrorWithCode(error) && error.code === "ENOENT") {
|
|
53
|
+
return false;
|
|
54
|
+
}
|
|
55
|
+
throw wrapError(error, `Could not determine if file exists '${filePath}'`);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
async function directoryExists(directoryPath) {
|
|
59
|
+
try {
|
|
60
|
+
const stats = await fs.promises.stat(directoryPath);
|
|
61
|
+
return stats.isDirectory();
|
|
62
|
+
} catch (error) {
|
|
63
|
+
if (isErrorWithCode(error) && error.code === "ENOENT") {
|
|
64
|
+
return false;
|
|
65
|
+
}
|
|
66
|
+
throw wrapError(
|
|
67
|
+
error,
|
|
68
|
+
`Could not determine if directory exists '${directoryPath}'`
|
|
69
|
+
);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
async function ensureDirectoryStructureExists(directoryPath) {
|
|
73
|
+
try {
|
|
74
|
+
await fs.promises.mkdir(directoryPath, { recursive: true });
|
|
75
|
+
} catch (error) {
|
|
76
|
+
throw wrapError(
|
|
77
|
+
error,
|
|
78
|
+
`Could not create directory structure '${directoryPath}'`
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
async function forceRemove(entryPath) {
|
|
83
|
+
try {
|
|
84
|
+
return await fs.promises.rm(entryPath, {
|
|
85
|
+
recursive: true,
|
|
86
|
+
force: true
|
|
87
|
+
});
|
|
88
|
+
} catch (error) {
|
|
89
|
+
throw wrapError(error, `Could not remove file or directory '${entryPath}'`);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
function createSandbox(projectName) {
|
|
93
|
+
const timestamp = (/* @__PURE__ */ new Date()).getTime();
|
|
94
|
+
const directoryPath = path.join(os.tmpdir(), `${projectName}--${timestamp}`);
|
|
95
|
+
return {
|
|
96
|
+
directoryPath,
|
|
97
|
+
async withinSandbox(test) {
|
|
98
|
+
if (await directoryExists(directoryPath)) {
|
|
99
|
+
throw new Error(`${directoryPath} already exists. Cannot continue.`);
|
|
100
|
+
}
|
|
101
|
+
await ensureDirectoryStructureExists(directoryPath);
|
|
102
|
+
try {
|
|
103
|
+
await test({ directoryPath });
|
|
104
|
+
} finally {
|
|
105
|
+
await forceRemove(directoryPath);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
export {
|
|
112
|
+
readFile,
|
|
113
|
+
writeFile,
|
|
114
|
+
readJsonFile,
|
|
115
|
+
writeJsonFile,
|
|
116
|
+
fileExists,
|
|
117
|
+
directoryExists,
|
|
118
|
+
ensureDirectoryStructureExists,
|
|
119
|
+
forceRemove,
|
|
120
|
+
createSandbox
|
|
121
|
+
};
|
|
122
|
+
//# sourceMappingURL=chunk-VL5DYXC5.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/fs.ts"],"sourcesContent":["// This file is intended to be used only in a Node.js context.\n/* eslint-disable import/no-nodejs-modules */\n\nimport fs from 'fs';\nimport os from 'os';\nimport path from 'path';\n\nimport { isErrorWithCode, wrapError } from './errors';\nimport type { Json } from './json';\n\n/**\n * Information about the file sandbox provided to tests that need temporary\n * access to the filesystem.\n */\nexport type FileSandbox = {\n directoryPath: string;\n withinSandbox: (\n test: (args: { directoryPath: string }) => Promise<void>,\n ) => Promise<void>;\n};\n\n/**\n * Read the file at the given path, assuming its content is encoded as UTF-8.\n *\n * @param filePath - The path to the file.\n * @returns The content of the file.\n * @throws An error with a stack trace if reading fails in any way.\n */\nexport async function readFile(filePath: string): Promise<string> {\n try {\n return await fs.promises.readFile(filePath, 'utf8');\n } catch (error) {\n throw wrapError(error, `Could not read file '${filePath}'`);\n }\n}\n\n/**\n * Write content to the file at the given path, creating the directory structure\n * for the file automatically if necessary.\n *\n * @param filePath - The path to the file.\n * @param content - The new content of the file.\n * @throws An error with a stack trace if writing fails in any way.\n */\nexport async function writeFile(\n filePath: string,\n content: string,\n): Promise<void> {\n try {\n await fs.promises.mkdir(path.dirname(filePath), { recursive: true });\n await fs.promises.writeFile(filePath, content);\n } catch (error) {\n throw wrapError(error, `Could not write file '${filePath}'`);\n }\n}\n\n/**\n * Read the assumed JSON file at the given path, attempts to parse it, and\n * get the resulting object. Supports a custom parser (in case you want to\n * use the [JSON5](https://www.npmjs.com/package/json5) package instead).\n *\n * @param filePath - The path segments pointing to the JSON file. Will be passed\n * to path.join().\n * @param options - Options to this function.\n * @param options.parser - The parser object to use. Defaults to `JSON`.\n * @param options.parser.parse - A function that parses JSON data.\n * @returns The object corresponding to the parsed JSON file, typed against the\n * struct.\n * @throws An error with a stack trace if reading fails in any way, or if the\n * parsed value is not a plain object.\n */\nexport async function readJsonFile<Value extends Json>(\n filePath: string,\n {\n parser = JSON,\n }: {\n parser?: {\n parse: (\n text: Parameters<typeof JSON.parse>[0],\n ) => ReturnType<typeof JSON.parse>;\n };\n } = {},\n): Promise<Value> {\n try {\n const content = await fs.promises.readFile(filePath, 'utf8');\n return parser.parse(content);\n } catch (error) {\n throw wrapError(error, `Could not read JSON file '${filePath}'`);\n }\n}\n\n/**\n * Attempt to write the given JSON-like value to the file at the given path,\n * creating the directory structure for the file automatically if necessary.\n * Adds a newline to the end of the file. Supports a custom parser (in case you\n * want to use the [JSON5](https://www.npmjs.com/package/json5) package\n * instead).\n *\n * @param filePath - The path to write the JSON file to, including the file\n * itself.\n * @param jsonValue - The JSON-like value to write to the file. Make sure that\n * JSON.stringify can handle it.\n * @param options - The options to this function.\n * @param options.prettify - Whether to format the JSON as it is turned into a\n * string such that it is broken up into separate lines (using 2 spaces as\n * indentation).\n * @param options.stringifier - The stringifier to use. Defaults to `JSON`.\n * @param options.stringifier.stringify - A function that stringifies JSON.\n * @returns The object corresponding to the parsed JSON file, typed against the\n * struct.\n * @throws An error with a stack trace if writing fails in any way.\n */\nexport async function writeJsonFile(\n filePath: string,\n jsonValue: Json,\n {\n stringifier = JSON,\n prettify = false,\n }: {\n stringifier?: {\n stringify: typeof JSON.stringify;\n };\n prettify?: boolean;\n } = {},\n): Promise<void> {\n try {\n await fs.promises.mkdir(path.dirname(filePath), { recursive: true });\n const json = prettify\n ? stringifier.stringify(jsonValue, null, ' ')\n : stringifier.stringify(jsonValue);\n await fs.promises.writeFile(filePath, json);\n } catch (error) {\n throw wrapError(error, `Could not write JSON file '${filePath}'`);\n }\n}\n\n/**\n * Test the given path to determine whether it represents a file.\n *\n * @param filePath - The path to a (supposed) file on the filesystem.\n * @returns A promise for true if the file exists or false otherwise.\n * @throws An error with a stack trace if reading fails in any way.\n */\nexport async function fileExists(filePath: string): Promise<boolean> {\n try {\n const stats = await fs.promises.stat(filePath);\n return stats.isFile();\n } catch (error) {\n if (isErrorWithCode(error) && error.code === 'ENOENT') {\n return false;\n }\n\n throw wrapError(error, `Could not determine if file exists '${filePath}'`);\n }\n}\n\n/**\n * Test the given path to determine whether it represents a directory.\n *\n * @param directoryPath - The path to a (supposed) directory on the filesystem.\n * @returns A promise for true if the file exists or false otherwise.\n * @throws An error with a stack trace if reading fails in any way.\n */\nexport async function directoryExists(directoryPath: string): Promise<boolean> {\n try {\n const stats = await fs.promises.stat(directoryPath);\n return stats.isDirectory();\n } catch (error) {\n if (isErrorWithCode(error) && error.code === 'ENOENT') {\n return false;\n }\n\n throw wrapError(\n error,\n `Could not determine if directory exists '${directoryPath}'`,\n );\n }\n}\n\n/**\n * Create the given directory along with any directories leading up to the\n * directory, or do nothing if the directory already exists.\n *\n * @param directoryPath - The path to the desired directory.\n * @throws An error with a stack trace if reading fails in any way.\n */\nexport async function ensureDirectoryStructureExists(\n directoryPath: string,\n): Promise<void> {\n try {\n await fs.promises.mkdir(directoryPath, { recursive: true });\n } catch (error) {\n throw wrapError(\n error,\n `Could not create directory structure '${directoryPath}'`,\n );\n }\n}\n\n/**\n * Remove the given file or directory if it exists, or do nothing if it does\n * not.\n *\n * @param entryPath - The path to the file or directory.\n * @throws An error with a stack trace if removal fails in any way.\n */\nexport async function forceRemove(entryPath: string): Promise<void> {\n try {\n return await fs.promises.rm(entryPath, {\n recursive: true,\n force: true,\n });\n } catch (error) {\n throw wrapError(error, `Could not remove file or directory '${entryPath}'`);\n }\n}\n\n/**\n * Construct a sandbox object which can be used in tests that need temporary\n * access to the filesystem.\n *\n * @param projectName - The name of the project.\n * @returns The sandbox object. This contains a `withinSandbox` function which\n * can be used in tests (see example).\n * @example\n * ```typescript\n * const { withinSandbox } = createSandbox('utils');\n *\n * // ... later ...\n *\n * it('does something with the filesystem', async () => {\n * await withinSandbox(async ({ directoryPath }) => {\n * await fs.promises.writeFile(\n * path.join(directoryPath, 'some-file'),\n * 'some content',\n * 'utf8'\n * );\n * })\n * });\n * ```\n */\nexport function createSandbox(projectName: string): FileSandbox {\n const timestamp = new Date().getTime();\n const directoryPath = path.join(os.tmpdir(), `${projectName}--${timestamp}`);\n\n return {\n directoryPath,\n async withinSandbox(\n test: (args: { directoryPath: string }) => Promise<void>,\n ) {\n if (await directoryExists(directoryPath)) {\n throw new Error(`${directoryPath} already exists. Cannot continue.`);\n }\n\n await ensureDirectoryStructureExists(directoryPath);\n\n try {\n await test({ directoryPath });\n } finally {\n await forceRemove(directoryPath);\n }\n },\n };\n}\n"],"mappings":"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","names":[]}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import {
|
|
2
|
+
add0x,
|
|
3
|
+
assertIsHexString
|
|
4
|
+
} from "./chunk-BKR45GQY.mjs";
|
|
5
|
+
import {
|
|
6
|
+
assert
|
|
7
|
+
} from "./chunk-CSDMTDND.mjs";
|
|
8
|
+
|
|
9
|
+
// src/number.ts
|
|
10
|
+
var numberToHex = (value) => {
|
|
11
|
+
assert(typeof value === "number", "Value must be a number.");
|
|
12
|
+
assert(value >= 0, "Value must be a non-negative number.");
|
|
13
|
+
assert(
|
|
14
|
+
Number.isSafeInteger(value),
|
|
15
|
+
"Value is not a safe integer. Use `bigIntToHex` instead."
|
|
16
|
+
);
|
|
17
|
+
return add0x(value.toString(16));
|
|
18
|
+
};
|
|
19
|
+
var bigIntToHex = (value) => {
|
|
20
|
+
assert(typeof value === "bigint", "Value must be a bigint.");
|
|
21
|
+
assert(value >= 0, "Value must be a non-negative bigint.");
|
|
22
|
+
return add0x(value.toString(16));
|
|
23
|
+
};
|
|
24
|
+
var hexToNumber = (value) => {
|
|
25
|
+
assertIsHexString(value);
|
|
26
|
+
const numberValue = parseInt(value, 16);
|
|
27
|
+
assert(
|
|
28
|
+
Number.isSafeInteger(numberValue),
|
|
29
|
+
"Value is not a safe integer. Use `hexToBigInt` instead."
|
|
30
|
+
);
|
|
31
|
+
return numberValue;
|
|
32
|
+
};
|
|
33
|
+
var hexToBigInt = (value) => {
|
|
34
|
+
assertIsHexString(value);
|
|
35
|
+
return BigInt(add0x(value));
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
export {
|
|
39
|
+
numberToHex,
|
|
40
|
+
bigIntToHex,
|
|
41
|
+
hexToNumber,
|
|
42
|
+
hexToBigInt
|
|
43
|
+
};
|
|
44
|
+
//# sourceMappingURL=chunk-VSEW3V2L.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/number.ts"],"sourcesContent":["import { assert } from './assert';\nimport { add0x, assertIsHexString } from './hex';\n\n/**\n * Convert a number to a hexadecimal string. This verifies that the number is a\n * non-negative safe integer.\n *\n * To convert a `bigint` to a hexadecimal string instead, use\n * {@link bigIntToHex}.\n *\n * @example\n * ```typescript\n * numberToHex(0); // '0x0'\n * numberToHex(1); // '0x1'\n * numberToHex(16); // '0x10'\n * ```\n * @param value - The number to convert to a hexadecimal string.\n * @returns The hexadecimal string, with the \"0x\"-prefix.\n * @throws If the number is not a non-negative safe integer.\n */\nexport const numberToHex = (value: number): string => {\n assert(typeof value === 'number', 'Value must be a number.');\n assert(value >= 0, 'Value must be a non-negative number.');\n assert(\n Number.isSafeInteger(value),\n 'Value is not a safe integer. Use `bigIntToHex` instead.',\n );\n\n return add0x(value.toString(16));\n};\n\n/**\n * Convert a `bigint` to a hexadecimal string. This verifies that the `bigint`\n * is a non-negative integer.\n *\n * To convert a number to a hexadecimal string instead, use {@link numberToHex}.\n *\n * @example\n * ```typescript\n * bigIntToHex(0n); // '0x0'\n * bigIntToHex(1n); // '0x1'\n * bigIntToHex(16n); // '0x10'\n * ```\n * @param value - The `bigint` to convert to a hexadecimal string.\n * @returns The hexadecimal string, with the \"0x\"-prefix.\n * @throws If the `bigint` is not a non-negative integer.\n */\nexport const bigIntToHex = (value: bigint): string => {\n assert(typeof value === 'bigint', 'Value must be a bigint.');\n assert(value >= 0, 'Value must be a non-negative bigint.');\n\n return add0x(value.toString(16));\n};\n\n/**\n * Convert a hexadecimal string to a number. This verifies that the string is a\n * valid hex string, and that the resulting number is a safe integer. Both\n * \"0x\"-prefixed and unprefixed strings are supported.\n *\n * To convert a hexadecimal string to a `bigint` instead, use\n * {@link hexToBigInt}.\n *\n * @example\n * ```typescript\n * hexToNumber('0x0'); // 0\n * hexToNumber('0x1'); // 1\n * hexToNumber('0x10'); // 16\n * ```\n * @param value - The hexadecimal string to convert to a number.\n * @returns The number.\n * @throws If the value is not a valid hexadecimal string, or if the resulting\n * number is not a safe integer.\n */\nexport const hexToNumber = (value: string): number => {\n assertIsHexString(value);\n\n // `parseInt` accepts values without the \"0x\"-prefix, whereas `Number` does\n // not. Using this is slightly faster than `Number(add0x(value))`.\n const numberValue = parseInt(value, 16);\n\n assert(\n Number.isSafeInteger(numberValue),\n 'Value is not a safe integer. Use `hexToBigInt` instead.',\n );\n\n return numberValue;\n};\n\n/**\n * Convert a hexadecimal string to a `bigint`. This verifies that the string is\n * a valid hex string. Both \"0x\"-prefixed and unprefixed strings are supported.\n *\n * To convert a hexadecimal string to a number instead, use {@link hexToNumber}.\n *\n * @example\n * ```typescript\n * hexToBigInt('0x0'); // 0n\n * hexToBigInt('0x1'); // 1n\n * hexToBigInt('0x10'); // 16n\n * ```\n * @param value - The hexadecimal string to convert to a `bigint`.\n * @returns The `bigint`.\n * @throws If the value is not a valid hexadecimal string.\n */\nexport const hexToBigInt = (value: string): bigint => {\n assertIsHexString(value);\n\n // The `BigInt` constructor requires the \"0x\"-prefix to parse a hex string.\n return BigInt(add0x(value));\n};\n"],"mappings":";;;;;;;;;AAoBO,IAAM,cAAc,CAAC,UAA0B;AACpD,SAAO,OAAO,UAAU,UAAU,yBAAyB;AAC3D,SAAO,SAAS,GAAG,sCAAsC;AACzD;AAAA,IACE,OAAO,cAAc,KAAK;AAAA,IAC1B;AAAA,EACF;AAEA,SAAO,MAAM,MAAM,SAAS,EAAE,CAAC;AACjC;AAkBO,IAAM,cAAc,CAAC,UAA0B;AACpD,SAAO,OAAO,UAAU,UAAU,yBAAyB;AAC3D,SAAO,SAAS,GAAG,sCAAsC;AAEzD,SAAO,MAAM,MAAM,SAAS,EAAE,CAAC;AACjC;AAqBO,IAAM,cAAc,CAAC,UAA0B;AACpD,oBAAkB,KAAK;AAIvB,QAAM,cAAc,SAAS,OAAO,EAAE;AAEtC;AAAA,IACE,OAAO,cAAc,WAAW;AAAA,IAChC;AAAA,EACF;AAEA,SAAO;AACT;AAkBO,IAAM,cAAc,CAAC,UAA0B;AACpD,oBAAkB,KAAK;AAGvB,SAAO,OAAO,MAAM,KAAK,CAAC;AAC5B;","names":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
//# sourceMappingURL=chunk-WA4DHIND.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
var _chunkDMPG3H3Gjs = require('./chunk-DMPG3H3G.js');
|
|
5
|
+
|
|
6
|
+
// src/fs.ts
|
|
7
|
+
var _fs = require('fs'); var _fs2 = _interopRequireDefault(_fs);
|
|
8
|
+
var _os = require('os'); var _os2 = _interopRequireDefault(_os);
|
|
9
|
+
var _path = require('path'); var _path2 = _interopRequireDefault(_path);
|
|
10
|
+
async function readFile(filePath) {
|
|
11
|
+
try {
|
|
12
|
+
return await _fs2.default.promises.readFile(filePath, "utf8");
|
|
13
|
+
} catch (error) {
|
|
14
|
+
throw _chunkDMPG3H3Gjs.wrapError.call(void 0, error, `Could not read file '${filePath}'`);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
async function writeFile(filePath, content) {
|
|
18
|
+
try {
|
|
19
|
+
await _fs2.default.promises.mkdir(_path2.default.dirname(filePath), { recursive: true });
|
|
20
|
+
await _fs2.default.promises.writeFile(filePath, content);
|
|
21
|
+
} catch (error) {
|
|
22
|
+
throw _chunkDMPG3H3Gjs.wrapError.call(void 0, error, `Could not write file '${filePath}'`);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
async function readJsonFile(filePath, {
|
|
26
|
+
parser = JSON
|
|
27
|
+
} = {}) {
|
|
28
|
+
try {
|
|
29
|
+
const content = await _fs2.default.promises.readFile(filePath, "utf8");
|
|
30
|
+
return parser.parse(content);
|
|
31
|
+
} catch (error) {
|
|
32
|
+
throw _chunkDMPG3H3Gjs.wrapError.call(void 0, error, `Could not read JSON file '${filePath}'`);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
async function writeJsonFile(filePath, jsonValue, {
|
|
36
|
+
stringifier = JSON,
|
|
37
|
+
prettify = false
|
|
38
|
+
} = {}) {
|
|
39
|
+
try {
|
|
40
|
+
await _fs2.default.promises.mkdir(_path2.default.dirname(filePath), { recursive: true });
|
|
41
|
+
const json = prettify ? stringifier.stringify(jsonValue, null, " ") : stringifier.stringify(jsonValue);
|
|
42
|
+
await _fs2.default.promises.writeFile(filePath, json);
|
|
43
|
+
} catch (error) {
|
|
44
|
+
throw _chunkDMPG3H3Gjs.wrapError.call(void 0, error, `Could not write JSON file '${filePath}'`);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
async function fileExists(filePath) {
|
|
48
|
+
try {
|
|
49
|
+
const stats = await _fs2.default.promises.stat(filePath);
|
|
50
|
+
return stats.isFile();
|
|
51
|
+
} catch (error) {
|
|
52
|
+
if (_chunkDMPG3H3Gjs.isErrorWithCode.call(void 0, error) && error.code === "ENOENT") {
|
|
53
|
+
return false;
|
|
54
|
+
}
|
|
55
|
+
throw _chunkDMPG3H3Gjs.wrapError.call(void 0, error, `Could not determine if file exists '${filePath}'`);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
async function directoryExists(directoryPath) {
|
|
59
|
+
try {
|
|
60
|
+
const stats = await _fs2.default.promises.stat(directoryPath);
|
|
61
|
+
return stats.isDirectory();
|
|
62
|
+
} catch (error) {
|
|
63
|
+
if (_chunkDMPG3H3Gjs.isErrorWithCode.call(void 0, error) && error.code === "ENOENT") {
|
|
64
|
+
return false;
|
|
65
|
+
}
|
|
66
|
+
throw _chunkDMPG3H3Gjs.wrapError.call(void 0,
|
|
67
|
+
error,
|
|
68
|
+
`Could not determine if directory exists '${directoryPath}'`
|
|
69
|
+
);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
async function ensureDirectoryStructureExists(directoryPath) {
|
|
73
|
+
try {
|
|
74
|
+
await _fs2.default.promises.mkdir(directoryPath, { recursive: true });
|
|
75
|
+
} catch (error) {
|
|
76
|
+
throw _chunkDMPG3H3Gjs.wrapError.call(void 0,
|
|
77
|
+
error,
|
|
78
|
+
`Could not create directory structure '${directoryPath}'`
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
async function forceRemove(entryPath) {
|
|
83
|
+
try {
|
|
84
|
+
return await _fs2.default.promises.rm(entryPath, {
|
|
85
|
+
recursive: true,
|
|
86
|
+
force: true
|
|
87
|
+
});
|
|
88
|
+
} catch (error) {
|
|
89
|
+
throw _chunkDMPG3H3Gjs.wrapError.call(void 0, error, `Could not remove file or directory '${entryPath}'`);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
function createSandbox(projectName) {
|
|
93
|
+
const timestamp = (/* @__PURE__ */ new Date()).getTime();
|
|
94
|
+
const directoryPath = _path2.default.join(_os2.default.tmpdir(), `${projectName}--${timestamp}`);
|
|
95
|
+
return {
|
|
96
|
+
directoryPath,
|
|
97
|
+
async withinSandbox(test) {
|
|
98
|
+
if (await directoryExists(directoryPath)) {
|
|
99
|
+
throw new Error(`${directoryPath} already exists. Cannot continue.`);
|
|
100
|
+
}
|
|
101
|
+
await ensureDirectoryStructureExists(directoryPath);
|
|
102
|
+
try {
|
|
103
|
+
await test({ directoryPath });
|
|
104
|
+
} finally {
|
|
105
|
+
await forceRemove(directoryPath);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
exports.readFile = readFile; exports.writeFile = writeFile; exports.readJsonFile = readJsonFile; exports.writeJsonFile = writeJsonFile; exports.fileExists = fileExists; exports.directoryExists = directoryExists; exports.ensureDirectoryStructureExists = ensureDirectoryStructureExists; exports.forceRemove = forceRemove; exports.createSandbox = createSandbox;
|
|
122
|
+
//# sourceMappingURL=chunk-WQDTPIQK.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/fs.ts"],"names":[],"mappings":"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","sourcesContent":["// This file is intended to be used only in a Node.js context.\n/* eslint-disable import/no-nodejs-modules */\n\nimport fs from 'fs';\nimport os from 'os';\nimport path from 'path';\n\nimport { isErrorWithCode, wrapError } from './errors';\nimport type { Json } from './json';\n\n/**\n * Information about the file sandbox provided to tests that need temporary\n * access to the filesystem.\n */\nexport type FileSandbox = {\n directoryPath: string;\n withinSandbox: (\n test: (args: { directoryPath: string }) => Promise<void>,\n ) => Promise<void>;\n};\n\n/**\n * Read the file at the given path, assuming its content is encoded as UTF-8.\n *\n * @param filePath - The path to the file.\n * @returns The content of the file.\n * @throws An error with a stack trace if reading fails in any way.\n */\nexport async function readFile(filePath: string): Promise<string> {\n try {\n return await fs.promises.readFile(filePath, 'utf8');\n } catch (error) {\n throw wrapError(error, `Could not read file '${filePath}'`);\n }\n}\n\n/**\n * Write content to the file at the given path, creating the directory structure\n * for the file automatically if necessary.\n *\n * @param filePath - The path to the file.\n * @param content - The new content of the file.\n * @throws An error with a stack trace if writing fails in any way.\n */\nexport async function writeFile(\n filePath: string,\n content: string,\n): Promise<void> {\n try {\n await fs.promises.mkdir(path.dirname(filePath), { recursive: true });\n await fs.promises.writeFile(filePath, content);\n } catch (error) {\n throw wrapError(error, `Could not write file '${filePath}'`);\n }\n}\n\n/**\n * Read the assumed JSON file at the given path, attempts to parse it, and\n * get the resulting object. Supports a custom parser (in case you want to\n * use the [JSON5](https://www.npmjs.com/package/json5) package instead).\n *\n * @param filePath - The path segments pointing to the JSON file. Will be passed\n * to path.join().\n * @param options - Options to this function.\n * @param options.parser - The parser object to use. Defaults to `JSON`.\n * @param options.parser.parse - A function that parses JSON data.\n * @returns The object corresponding to the parsed JSON file, typed against the\n * struct.\n * @throws An error with a stack trace if reading fails in any way, or if the\n * parsed value is not a plain object.\n */\nexport async function readJsonFile<Value extends Json>(\n filePath: string,\n {\n parser = JSON,\n }: {\n parser?: {\n parse: (\n text: Parameters<typeof JSON.parse>[0],\n ) => ReturnType<typeof JSON.parse>;\n };\n } = {},\n): Promise<Value> {\n try {\n const content = await fs.promises.readFile(filePath, 'utf8');\n return parser.parse(content);\n } catch (error) {\n throw wrapError(error, `Could not read JSON file '${filePath}'`);\n }\n}\n\n/**\n * Attempt to write the given JSON-like value to the file at the given path,\n * creating the directory structure for the file automatically if necessary.\n * Adds a newline to the end of the file. Supports a custom parser (in case you\n * want to use the [JSON5](https://www.npmjs.com/package/json5) package\n * instead).\n *\n * @param filePath - The path to write the JSON file to, including the file\n * itself.\n * @param jsonValue - The JSON-like value to write to the file. Make sure that\n * JSON.stringify can handle it.\n * @param options - The options to this function.\n * @param options.prettify - Whether to format the JSON as it is turned into a\n * string such that it is broken up into separate lines (using 2 spaces as\n * indentation).\n * @param options.stringifier - The stringifier to use. Defaults to `JSON`.\n * @param options.stringifier.stringify - A function that stringifies JSON.\n * @returns The object corresponding to the parsed JSON file, typed against the\n * struct.\n * @throws An error with a stack trace if writing fails in any way.\n */\nexport async function writeJsonFile(\n filePath: string,\n jsonValue: Json,\n {\n stringifier = JSON,\n prettify = false,\n }: {\n stringifier?: {\n stringify: typeof JSON.stringify;\n };\n prettify?: boolean;\n } = {},\n): Promise<void> {\n try {\n await fs.promises.mkdir(path.dirname(filePath), { recursive: true });\n const json = prettify\n ? stringifier.stringify(jsonValue, null, ' ')\n : stringifier.stringify(jsonValue);\n await fs.promises.writeFile(filePath, json);\n } catch (error) {\n throw wrapError(error, `Could not write JSON file '${filePath}'`);\n }\n}\n\n/**\n * Test the given path to determine whether it represents a file.\n *\n * @param filePath - The path to a (supposed) file on the filesystem.\n * @returns A promise for true if the file exists or false otherwise.\n * @throws An error with a stack trace if reading fails in any way.\n */\nexport async function fileExists(filePath: string): Promise<boolean> {\n try {\n const stats = await fs.promises.stat(filePath);\n return stats.isFile();\n } catch (error) {\n if (isErrorWithCode(error) && error.code === 'ENOENT') {\n return false;\n }\n\n throw wrapError(error, `Could not determine if file exists '${filePath}'`);\n }\n}\n\n/**\n * Test the given path to determine whether it represents a directory.\n *\n * @param directoryPath - The path to a (supposed) directory on the filesystem.\n * @returns A promise for true if the file exists or false otherwise.\n * @throws An error with a stack trace if reading fails in any way.\n */\nexport async function directoryExists(directoryPath: string): Promise<boolean> {\n try {\n const stats = await fs.promises.stat(directoryPath);\n return stats.isDirectory();\n } catch (error) {\n if (isErrorWithCode(error) && error.code === 'ENOENT') {\n return false;\n }\n\n throw wrapError(\n error,\n `Could not determine if directory exists '${directoryPath}'`,\n );\n }\n}\n\n/**\n * Create the given directory along with any directories leading up to the\n * directory, or do nothing if the directory already exists.\n *\n * @param directoryPath - The path to the desired directory.\n * @throws An error with a stack trace if reading fails in any way.\n */\nexport async function ensureDirectoryStructureExists(\n directoryPath: string,\n): Promise<void> {\n try {\n await fs.promises.mkdir(directoryPath, { recursive: true });\n } catch (error) {\n throw wrapError(\n error,\n `Could not create directory structure '${directoryPath}'`,\n );\n }\n}\n\n/**\n * Remove the given file or directory if it exists, or do nothing if it does\n * not.\n *\n * @param entryPath - The path to the file or directory.\n * @throws An error with a stack trace if removal fails in any way.\n */\nexport async function forceRemove(entryPath: string): Promise<void> {\n try {\n return await fs.promises.rm(entryPath, {\n recursive: true,\n force: true,\n });\n } catch (error) {\n throw wrapError(error, `Could not remove file or directory '${entryPath}'`);\n }\n}\n\n/**\n * Construct a sandbox object which can be used in tests that need temporary\n * access to the filesystem.\n *\n * @param projectName - The name of the project.\n * @returns The sandbox object. This contains a `withinSandbox` function which\n * can be used in tests (see example).\n * @example\n * ```typescript\n * const { withinSandbox } = createSandbox('utils');\n *\n * // ... later ...\n *\n * it('does something with the filesystem', async () => {\n * await withinSandbox(async ({ directoryPath }) => {\n * await fs.promises.writeFile(\n * path.join(directoryPath, 'some-file'),\n * 'some content',\n * 'utf8'\n * );\n * })\n * });\n * ```\n */\nexport function createSandbox(projectName: string): FileSandbox {\n const timestamp = new Date().getTime();\n const directoryPath = path.join(os.tmpdir(), `${projectName}--${timestamp}`);\n\n return {\n directoryPath,\n async withinSandbox(\n test: (args: { directoryPath: string }) => Promise<void>,\n ) {\n if (await directoryExists(directoryPath)) {\n throw new Error(`${directoryPath} already exists. Cannot continue.`);\n }\n\n await ensureDirectoryStructureExists(directoryPath);\n\n try {\n await test({ directoryPath });\n } finally {\n await forceRemove(directoryPath);\n }\n },\n };\n}\n"]}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } }
|
|
2
|
+
|
|
3
|
+
var _chunkVBYRLAP6js = require('./chunk-VBYRLAP6.js');
|
|
4
|
+
|
|
5
|
+
// src/base64.ts
|
|
6
|
+
var _superstruct = require('superstruct');
|
|
7
|
+
var base64 = (struct, options = {}) => {
|
|
8
|
+
const paddingRequired = _nullishCoalesce(options.paddingRequired, () => ( false));
|
|
9
|
+
const characterSet = _nullishCoalesce(options.characterSet, () => ( "base64"));
|
|
10
|
+
let letters;
|
|
11
|
+
if (characterSet === "base64") {
|
|
12
|
+
letters = String.raw`[A-Za-z0-9+\/]`;
|
|
13
|
+
} else {
|
|
14
|
+
_chunkVBYRLAP6js.assert.call(void 0, characterSet === "base64url");
|
|
15
|
+
letters = String.raw`[-_A-Za-z0-9]`;
|
|
16
|
+
}
|
|
17
|
+
let re;
|
|
18
|
+
if (paddingRequired) {
|
|
19
|
+
re = new RegExp(
|
|
20
|
+
`^(?:${letters}{4})*(?:${letters}{3}=|${letters}{2}==)?$`,
|
|
21
|
+
"u"
|
|
22
|
+
);
|
|
23
|
+
} else {
|
|
24
|
+
re = new RegExp(
|
|
25
|
+
`^(?:${letters}{4})*(?:${letters}{2,3}|${letters}{3}=|${letters}{2}==)?$`,
|
|
26
|
+
"u"
|
|
27
|
+
);
|
|
28
|
+
}
|
|
29
|
+
return _superstruct.pattern.call(void 0, struct, re);
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
exports.base64 = base64;
|
|
35
|
+
//# sourceMappingURL=chunk-WRBN6DWM.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/base64.ts"],"names":[],"mappings":";;;;;AACA,SAAS,eAAe;AA4BjB,IAAM,SAAS,CACpB,QACA,UAAyB,CAAC,MACvB;AACH,QAAM,kBAAkB,QAAQ,mBAAmB;AACnD,QAAM,eAAe,QAAQ,gBAAgB;AAE7C,MAAI;AACJ,MAAI,iBAAiB,UAAU;AAC7B,cAAU,OAAO;AAAA,EACnB,OAAO;AACL,WAAO,iBAAiB,WAAW;AACnC,cAAU,OAAO;AAAA,EACnB;AAEA,MAAI;AACJ,MAAI,iBAAiB;AACnB,SAAK,IAAI;AAAA,MACP,OAAO,OAAO,WAAW,OAAO,QAAQ,OAAO;AAAA,MAC/C;AAAA,IACF;AAAA,EACF,OAAO;AACL,SAAK,IAAI;AAAA,MACP,OAAO,OAAO,WAAW,OAAO,SAAS,OAAO,QAAQ,OAAO;AAAA,MAC/D;AAAA,IACF;AAAA,EACF;AAEA,SAAO,QAAQ,QAAQ,EAAE;AAC3B","sourcesContent":["import type { Struct } from 'superstruct';\nimport { pattern } from 'superstruct';\n\nimport { assert } from './assert';\n\nexport type Base64Options = {\n /**\n * Is the `=` padding at the end required or not.\n *\n * @default false\n */\n // Padding is optional in RFC 4648, that's why the default value is false\n paddingRequired?: boolean;\n /**\n * Which character set should be used.\n * The sets are based on {@link https://datatracker.ietf.org/doc/html/rfc4648 RFC 4648}.\n *\n * @default 'base64'\n */\n characterSet?: 'base64' | 'base64url';\n};\n\n/**\n * Ensure that a provided string-based struct is valid base64.\n *\n * @param struct - The string based struct.\n * @param options - Optional options to specialize base64 validation. See {@link Base64Options} documentation.\n * @returns A superstruct validating base64.\n */\nexport const base64 = <Type extends string, Schema>(\n struct: Struct<Type, Schema>,\n options: Base64Options = {},\n) => {\n const paddingRequired = options.paddingRequired ?? false;\n const characterSet = options.characterSet ?? 'base64';\n\n let letters: string;\n if (characterSet === 'base64') {\n letters = String.raw`[A-Za-z0-9+\\/]`;\n } else {\n assert(characterSet === 'base64url');\n letters = String.raw`[-_A-Za-z0-9]`;\n }\n\n let re: RegExp;\n if (paddingRequired) {\n re = new RegExp(\n `^(?:${letters}{4})*(?:${letters}{3}=|${letters}{2}==)?$`,\n 'u',\n );\n } else {\n re = new RegExp(\n `^(?:${letters}{4})*(?:${letters}{2,3}|${letters}{3}=|${letters}{2}==)?$`,\n 'u',\n );\n }\n\n return pattern(struct, re);\n};\n"]}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
var __accessCheck = (obj, member, msg) => {
|
|
2
|
+
if (!member.has(obj))
|
|
3
|
+
throw TypeError("Cannot " + msg);
|
|
4
|
+
};
|
|
5
|
+
var __privateGet = (obj, member, getter) => {
|
|
6
|
+
__accessCheck(obj, member, "read from private field");
|
|
7
|
+
return getter ? getter.call(obj) : member.get(obj);
|
|
8
|
+
};
|
|
9
|
+
var __privateAdd = (obj, member, value) => {
|
|
10
|
+
if (member.has(obj))
|
|
11
|
+
throw TypeError("Cannot add the same private member more than once");
|
|
12
|
+
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
|
13
|
+
};
|
|
14
|
+
var __privateSet = (obj, member, value, setter) => {
|
|
15
|
+
__accessCheck(obj, member, "write to private field");
|
|
16
|
+
setter ? setter.call(obj, value) : member.set(obj, value);
|
|
17
|
+
return value;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
export {
|
|
21
|
+
__privateGet,
|
|
22
|
+
__privateAdd,
|
|
23
|
+
__privateSet
|
|
24
|
+
};
|
|
25
|
+
//# sourceMappingURL=chunk-X66SUIEF.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true});
|
|
2
|
+
|
|
3
|
+
var _chunkWRBN6DWMjs = require('./chunk-WRBN6DWM.js');
|
|
4
|
+
|
|
5
|
+
// src/checksum.ts
|
|
6
|
+
var _superstruct = require('superstruct');
|
|
7
|
+
var ChecksumStruct = _superstruct.size.call(void 0,
|
|
8
|
+
_chunkWRBN6DWMjs.base64.call(void 0, _superstruct.string.call(void 0, ), { paddingRequired: true }),
|
|
9
|
+
44,
|
|
10
|
+
44
|
|
11
|
+
);
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
exports.ChecksumStruct = ChecksumStruct;
|
|
16
|
+
//# sourceMappingURL=chunk-XYYGZF5Y.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/checksum.ts"],"names":[],"mappings":";;;;;AAAA,SAAS,MAAM,cAAc;AAItB,IAAM,iBAAiB;AAAA,EAC5B,OAAO,OAAO,GAAG,EAAE,iBAAiB,KAAK,CAAC;AAAA,EAC1C;AAAA,EACA;AACF","sourcesContent":["import { size, string } from 'superstruct';\n\nimport { base64 } from './base64';\n\nexport const ChecksumStruct = size(\n base64(string(), { paddingRequired: true }),\n 44,\n 44,\n);\n"]}
|