@poppinss/utils 7.0.0-next.3 → 7.0.0-next.4

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.
@@ -0,0 +1,2 @@
1
+ import { Exception, InvalidArgumentsException, RuntimeException, createError } from "@poppinss/exception";
2
+ export { createError as i, InvalidArgumentsException as n, RuntimeException as r, Exception as t };
package/build/index.js CHANGED
@@ -1,179 +1,108 @@
1
- import {
2
- RuntimeException
3
- } from "./chunk-SKNTF5Q5.js";
4
- import {
5
- isScriptFile,
6
- naturalSort
7
- } from "./chunk-XFX47BKO.js";
8
- import {
9
- safeParse,
10
- safeStringify
11
- } from "./chunk-YFSCSJNE.js";
12
-
13
- // src/secret.ts
14
- var REDACTED = "[redacted]";
15
- var Secret = class _Secret {
16
- /** The secret value */
17
- #value;
18
- #keyword;
19
- constructor(value, redactedKeyword) {
20
- this.#value = value;
21
- this.#keyword = redactedKeyword || REDACTED;
22
- }
23
- toJSON() {
24
- return this.#keyword;
25
- }
26
- valueOf() {
27
- return this.#keyword;
28
- }
29
- [Symbol.for("nodejs.util.inspect.custom")]() {
30
- return this.#keyword;
31
- }
32
- toLocaleString() {
33
- return this.#keyword;
34
- }
35
- toString() {
36
- return this.#keyword;
37
- }
38
- /**
39
- * Returns the original value
40
- */
41
- release() {
42
- return this.#value;
43
- }
44
- /**
45
- * Transform the original value and create a new
46
- * secret from it.
47
- */
48
- map(transformFunc) {
49
- return new _Secret(transformFunc(this.#value));
50
- }
1
+ import { n as safeParse, t as safeStringify } from "./main-_zxQ-frM.js";
2
+ import { n as naturalSort, t as isScriptFile } from "./is_script_file-CbSEfxE_.js";
3
+ import { r as RuntimeException } from "./exception-L7vjh-Gv.js";
4
+ import { flattie } from "flattie";
5
+ import { Buffer } from "node:buffer";
6
+ import { timingSafeEqual } from "node:crypto";
7
+ import string from "@poppinss/string";
8
+ import lodash from "@poppinss/utils/lodash";
9
+ const REDACTED = "[redacted]";
10
+ var Secret = class Secret {
11
+ #value;
12
+ #keyword;
13
+ constructor(value, redactedKeyword) {
14
+ this.#value = value;
15
+ this.#keyword = redactedKeyword || REDACTED;
16
+ }
17
+ toJSON() {
18
+ return this.#keyword;
19
+ }
20
+ valueOf() {
21
+ return this.#keyword;
22
+ }
23
+ [Symbol.for("nodejs.util.inspect.custom")]() {
24
+ return this.#keyword;
25
+ }
26
+ toLocaleString() {
27
+ return this.#keyword;
28
+ }
29
+ toString() {
30
+ return this.#keyword;
31
+ }
32
+ release() {
33
+ return this.#value;
34
+ }
35
+ map(transformFunc) {
36
+ return new Secret(transformFunc(this.#value));
37
+ }
51
38
  };
52
-
53
- // src/compose.ts
54
39
  function compose(superclass, ...mixins) {
55
- return mixins.reduce((c, mixin) => mixin(c), superclass);
40
+ return mixins.reduce((c, mixin) => mixin(c), superclass);
56
41
  }
57
-
58
- // src/flatten.ts
59
- import { flattie } from "flattie";
60
42
  function flatten(input, glue, keepNullish) {
61
- return flattie(input, glue, keepNullish);
43
+ return flattie(input, glue, keepNullish);
62
44
  }
63
-
64
- // src/safe_equal.ts
65
- import { Buffer } from "buffer";
66
- import { timingSafeEqual } from "crypto";
67
45
  function safeEqual(trustedValue, userInput) {
68
- if (typeof trustedValue === "string" && typeof userInput === "string") {
69
- const trustedLength = Buffer.byteLength(trustedValue);
70
- const trustedValueBuffer = Buffer.alloc(trustedLength, 0, "utf-8");
71
- trustedValueBuffer.write(trustedValue);
72
- const userValueBuffer = Buffer.alloc(trustedLength, 0, "utf-8");
73
- userValueBuffer.write(userInput);
74
- return timingSafeEqual(trustedValueBuffer, userValueBuffer) && trustedLength === Buffer.byteLength(userInput);
75
- }
76
- return timingSafeEqual(
77
- Buffer.from(trustedValue),
78
- Buffer.from(userInput)
79
- );
46
+ if (typeof trustedValue === "string" && typeof userInput === "string") {
47
+ const trustedLength = Buffer.byteLength(trustedValue);
48
+ const trustedValueBuffer = Buffer.alloc(trustedLength, 0, "utf-8");
49
+ trustedValueBuffer.write(trustedValue);
50
+ const userValueBuffer = Buffer.alloc(trustedLength, 0, "utf-8");
51
+ userValueBuffer.write(userInput);
52
+ return timingSafeEqual(trustedValueBuffer, userValueBuffer) && trustedLength === Buffer.byteLength(userInput);
53
+ }
54
+ return timingSafeEqual(Buffer.from(trustedValue), Buffer.from(userInput));
80
55
  }
81
-
82
- // src/import_default.ts
83
56
  async function importDefault(importFn, filePath) {
84
- const moduleExports = await importFn();
85
- if (!("default" in moduleExports)) {
86
- const errorMessage = filePath ? `Missing "export default" in module "${filePath}"` : `Missing "export default" from lazy import "${importFn}"`;
87
- throw new RuntimeException(errorMessage, {
88
- cause: {
89
- source: importFn
90
- }
91
- });
92
- }
93
- return moduleExports.default;
57
+ const moduleExports = await importFn();
58
+ if (!("default" in moduleExports)) throw new RuntimeException(filePath ? `Missing "export default" in module "${filePath}"` : `Missing "export default" from lazy import "${importFn}"`, { cause: { source: importFn } });
59
+ return moduleExports.default;
94
60
  }
95
-
96
- // src/message_builder.ts
97
- import string from "@poppinss/string";
98
61
  var MessageBuilder = class {
99
- #getExpiryDate(expiresIn) {
100
- if (!expiresIn) {
101
- return void 0;
102
- }
103
- const expiryMs = string.milliseconds.parse(expiresIn);
104
- return new Date(Date.now() + expiryMs);
105
- }
106
- /**
107
- * Returns a boolean telling, if message has been expired or not
108
- */
109
- #isExpired(message) {
110
- if (!message.expiryDate) {
111
- return false;
112
- }
113
- const expiryDate = new Date(message.expiryDate);
114
- return Number.isNaN(expiryDate.getTime()) || expiryDate < /* @__PURE__ */ new Date();
115
- }
116
- /**
117
- * Builds a message by encoding expiry date and purpose inside it.
118
- */
119
- build(message, expiresIn, purpose) {
120
- const expiryDate = this.#getExpiryDate(expiresIn);
121
- return safeStringify({ message, purpose, expiryDate });
122
- }
123
- /**
124
- * Verifies the message for expiry and purpose.
125
- */
126
- verify(message, purpose) {
127
- const parsed = safeParse(message);
128
- if (typeof parsed !== "object" || !parsed) {
129
- return null;
130
- }
131
- if (!parsed.message) {
132
- return null;
133
- }
134
- if (parsed.purpose !== purpose) {
135
- return null;
136
- }
137
- if (this.#isExpired(parsed)) {
138
- return null;
139
- }
140
- return parsed.message;
141
- }
62
+ #getExpiryDate(expiresIn) {
63
+ if (!expiresIn) return;
64
+ const expiryMs = string.milliseconds.parse(expiresIn);
65
+ return new Date(Date.now() + expiryMs);
66
+ }
67
+ #isExpired(message) {
68
+ if (!message.expiryDate) return false;
69
+ const expiryDate = new Date(message.expiryDate);
70
+ return Number.isNaN(expiryDate.getTime()) || expiryDate < /* @__PURE__ */ new Date();
71
+ }
72
+ build(message, expiresIn, purpose) {
73
+ return safeStringify({
74
+ message,
75
+ purpose,
76
+ expiryDate: this.#getExpiryDate(expiresIn)
77
+ });
78
+ }
79
+ verify(message, purpose) {
80
+ const parsed = safeParse(message);
81
+ if (typeof parsed !== "object" || !parsed) return null;
82
+ if (!parsed.message) return null;
83
+ if (parsed.purpose !== purpose) return null;
84
+ if (this.#isExpired(parsed)) return null;
85
+ return parsed.message;
86
+ }
142
87
  };
143
-
144
- // src/define_static_property.ts
145
- import lodash from "@poppinss/utils/lodash";
146
- function defineStaticProperty(self, propertyName, {
147
- initialValue,
148
- strategy
149
- }) {
150
- if (!self.hasOwnProperty(propertyName)) {
151
- const value = self[propertyName];
152
- if (strategy === "define" || value === void 0) {
153
- Object.defineProperty(self, propertyName, {
154
- value: initialValue,
155
- configurable: true,
156
- enumerable: true,
157
- writable: true
158
- });
159
- return;
160
- }
161
- Object.defineProperty(self, propertyName, {
162
- value: typeof strategy === "function" ? strategy(value) : lodash.cloneDeep(value),
163
- configurable: true,
164
- enumerable: true,
165
- writable: true
166
- });
167
- }
88
+ function defineStaticProperty(self, propertyName, { initialValue, strategy }) {
89
+ if (!self.hasOwnProperty(propertyName)) {
90
+ const value = self[propertyName];
91
+ if (strategy === "define" || value === void 0) {
92
+ Object.defineProperty(self, propertyName, {
93
+ value: initialValue,
94
+ configurable: true,
95
+ enumerable: true,
96
+ writable: true
97
+ });
98
+ return;
99
+ }
100
+ Object.defineProperty(self, propertyName, {
101
+ value: typeof strategy === "function" ? strategy(value) : lodash.cloneDeep(value),
102
+ configurable: true,
103
+ enumerable: true,
104
+ writable: true
105
+ });
106
+ }
168
107
  }
169
- export {
170
- MessageBuilder,
171
- Secret,
172
- compose,
173
- defineStaticProperty,
174
- flatten,
175
- importDefault,
176
- isScriptFile,
177
- naturalSort,
178
- safeEqual
179
- };
108
+ export { MessageBuilder, Secret, compose, defineStaticProperty, flatten, importDefault, isScriptFile, naturalSort, safeEqual };
@@ -0,0 +1,20 @@
1
+ import { extname } from "node:path";
2
+ function naturalSort(current, next) {
3
+ return current.localeCompare(next, void 0, {
4
+ numeric: true,
5
+ sensitivity: "base"
6
+ });
7
+ }
8
+ const JS_MODULES = [
9
+ ".js",
10
+ ".json",
11
+ ".cjs",
12
+ ".mjs"
13
+ ];
14
+ function isScriptFile(filePath) {
15
+ const ext = extname(filePath);
16
+ if (JS_MODULES.includes(ext)) return true;
17
+ if (ext === ".ts" && !filePath.endsWith(".d.ts")) return true;
18
+ return false;
19
+ }
20
+ export { naturalSort as n, isScriptFile as t };