@poppinss/utils 7.0.0-next.0 → 7.0.0-next.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/README.md CHANGED
@@ -40,9 +40,7 @@ Even though I do not care much about package size (most of my work is consumed o
40
40
 
41
41
  Here's the last checked install size of this package.
42
42
 
43
- <a href="https://pkg-size.dev/@poppinss/utils@next">
44
- <img src="https://pkg-size.dev/badge/install/319382" title="Install size for @poppinss/utils">
45
- </a>
43
+ <a href="https://pkg-size.dev/@poppinss/utils@next"><img src="https://pkg-size.dev/badge/install/370120" title="Install size for @poppinss/utils"></a>
46
44
 
47
45
  ## Installation
48
46
 
@@ -0,0 +1,14 @@
1
+ // modules/exception.ts
2
+ import {
3
+ RuntimeException,
4
+ InvalidArgumentsException,
5
+ Exception,
6
+ createError
7
+ } from "@poppinss/exception";
8
+
9
+ export {
10
+ RuntimeException,
11
+ InvalidArgumentsException,
12
+ Exception,
13
+ createError
14
+ };
@@ -0,0 +1,23 @@
1
+ // src/natural_sort.ts
2
+ function naturalSort(current, next) {
3
+ return current.localeCompare(next, void 0, { numeric: true, sensitivity: "base" });
4
+ }
5
+
6
+ // src/is_script_file.ts
7
+ import { extname } from "path";
8
+ var JS_MODULES = [".js", ".json", ".cjs", ".mjs"];
9
+ function isScriptFile(filePath) {
10
+ const ext = extname(filePath);
11
+ if (JS_MODULES.includes(ext)) {
12
+ return true;
13
+ }
14
+ if (ext === ".ts" && !filePath.endsWith(".d.ts")) {
15
+ return true;
16
+ }
17
+ return false;
18
+ }
19
+
20
+ export {
21
+ naturalSort,
22
+ isScriptFile
23
+ };
@@ -0,0 +1,33 @@
1
+ // modules/json/safe_parse.ts
2
+ import { parse } from "secure-json-parse";
3
+ function safeParse(jsonString, reviver) {
4
+ return parse(jsonString, reviver, {
5
+ protoAction: "remove",
6
+ constructorAction: "remove"
7
+ });
8
+ }
9
+
10
+ // modules/json/safe_stringify.ts
11
+ import { configure } from "safe-stable-stringify";
12
+ var stringify = configure({
13
+ bigint: false,
14
+ circularValue: void 0,
15
+ deterministic: false
16
+ });
17
+ function jsonStringifyReplacer(replacer) {
18
+ return function(key, value) {
19
+ const val = replacer ? replacer.call(this, key, value) : value;
20
+ if (typeof val === "bigint") {
21
+ return val.toString();
22
+ }
23
+ return val;
24
+ };
25
+ }
26
+ function safeStringify(value, replacer, space) {
27
+ return stringify(value, jsonStringifyReplacer(replacer), space);
28
+ }
29
+
30
+ export {
31
+ safeParse,
32
+ safeStringify
33
+ };
package/build/index.js CHANGED
@@ -1,16 +1,14 @@
1
- var __defProp = Object.defineProperty;
2
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
- var __getOwnPropNames = Object.getOwnPropertyNames;
4
- var __hasOwnProp = Object.prototype.hasOwnProperty;
5
- var __copyProps = (to, from, except, desc) => {
6
- if (from && typeof from === "object" || typeof from === "function") {
7
- for (let key of __getOwnPropNames(from))
8
- if (!__hasOwnProp.call(to, key) && key !== except)
9
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
10
- }
11
- return to;
12
- };
13
- var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
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";
14
12
 
15
13
  // src/secret.ts
16
14
  var REDACTED = "[redacted]";
@@ -64,53 +62,29 @@ function flatten(input, glue, keepNullish) {
64
62
  }
65
63
 
66
64
  // src/safe_equal.ts
67
- import { Buffer as Buffer2 } from "buffer";
65
+ import { Buffer } from "buffer";
68
66
  import { timingSafeEqual } from "crypto";
69
67
  function safeEqual(trustedValue, userInput) {
70
68
  if (typeof trustedValue === "string" && typeof userInput === "string") {
71
- const trustedLength = Buffer2.byteLength(trustedValue);
72
- const trustedValueBuffer = Buffer2.alloc(trustedLength, 0, "utf-8");
69
+ const trustedLength = Buffer.byteLength(trustedValue);
70
+ const trustedValueBuffer = Buffer.alloc(trustedLength, 0, "utf-8");
73
71
  trustedValueBuffer.write(trustedValue);
74
- const userValueBuffer = Buffer2.alloc(trustedLength, 0, "utf-8");
72
+ const userValueBuffer = Buffer.alloc(trustedLength, 0, "utf-8");
75
73
  userValueBuffer.write(userInput);
76
- return timingSafeEqual(trustedValueBuffer, userValueBuffer) && trustedLength === Buffer2.byteLength(userInput);
74
+ return timingSafeEqual(trustedValueBuffer, userValueBuffer) && trustedLength === Buffer.byteLength(userInput);
77
75
  }
78
76
  return timingSafeEqual(
79
- Buffer2.from(trustedValue),
80
- Buffer2.from(userInput)
77
+ Buffer.from(trustedValue),
78
+ Buffer.from(userInput)
81
79
  );
82
80
  }
83
81
 
84
- // src/natural_sort.ts
85
- function naturalSort(current, next) {
86
- return current.localeCompare(next, void 0, { numeric: true, sensitivity: "base" });
87
- }
88
-
89
- // src/is_script_file.ts
90
- import { extname } from "path";
91
- var JS_MODULES = [".js", ".json", ".cjs", ".mjs"];
92
- function isScriptFile(filePath) {
93
- const ext = extname(filePath);
94
- if (JS_MODULES.includes(ext)) {
95
- return true;
96
- }
97
- if (ext === ".ts" && !filePath.endsWith(".d.ts")) {
98
- return true;
99
- }
100
- return false;
101
- }
102
-
103
- // modules/exception.ts
104
- var exception_exports = {};
105
- __reExport(exception_exports, exception_star);
106
- import * as exception_star from "@poppinss/exception";
107
-
108
82
  // src/import_default.ts
109
83
  async function importDefault(importFn, filePath) {
110
84
  const moduleExports = await importFn();
111
85
  if (!("default" in moduleExports)) {
112
86
  const errorMessage = filePath ? `Missing "export default" in module "${filePath}"` : `Missing "export default" from lazy import "${importFn}"`;
113
- throw new exception_exports.RuntimeException(errorMessage, {
87
+ throw new RuntimeException(errorMessage, {
114
88
  cause: {
115
89
  source: importFn
116
90
  }
@@ -121,37 +95,6 @@ async function importDefault(importFn, filePath) {
121
95
 
122
96
  // src/message_builder.ts
123
97
  import string from "@poppinss/string";
124
-
125
- // modules/json/safe_parse.ts
126
- import { parse } from "secure-json-parse";
127
- function safeParse(jsonString, reviver) {
128
- return parse(jsonString, reviver, {
129
- protoAction: "remove",
130
- constructorAction: "remove"
131
- });
132
- }
133
-
134
- // modules/json/safe_stringify.ts
135
- import { configure } from "safe-stable-stringify";
136
- var stringify = configure({
137
- bigint: false,
138
- circularValue: void 0,
139
- deterministic: false
140
- });
141
- function jsonStringifyReplacer(replacer) {
142
- return function(key, value) {
143
- const val = replacer ? replacer.call(this, key, value) : value;
144
- if (typeof val === "bigint") {
145
- return val.toString();
146
- }
147
- return val;
148
- };
149
- }
150
- function safeStringify(value, replacer, space) {
151
- return stringify(value, jsonStringifyReplacer(replacer), space);
152
- }
153
-
154
- // src/message_builder.ts
155
98
  var MessageBuilder = class {
156
99
  #getExpiryDate(expiresIn) {
157
100
  if (!expiresIn) {
@@ -0,0 +1,31 @@
1
+ // modules/assert.ts
2
+ import { inspect } from "util";
3
+ import { AssertionError } from "assert";
4
+ function assert(value, message) {
5
+ return assertExists(value, message);
6
+ }
7
+ function assertExists(value, message) {
8
+ if (!value) {
9
+ throw new AssertionError({ message: message ?? "value is falsy" });
10
+ }
11
+ }
12
+ function assertUnreachable(x) {
13
+ throw new AssertionError({ message: `unreachable code executed: ${inspect(x)}` });
14
+ }
15
+ function assertNotNull(value, message) {
16
+ if (value === null) {
17
+ throw new AssertionError({ message: message ?? "unexpected null value" });
18
+ }
19
+ }
20
+ function assertIsDefined(value, message) {
21
+ if (value === void 0) {
22
+ throw new AssertionError({ message: message ?? "unexpected undefined value" });
23
+ }
24
+ }
25
+ export {
26
+ assert,
27
+ assertExists,
28
+ assertIsDefined,
29
+ assertNotNull,
30
+ assertUnreachable
31
+ };
@@ -0,0 +1,43 @@
1
+ // modules/base64.ts
2
+ var Base64 = class {
3
+ encode(data, encoding) {
4
+ if (typeof data === "string") {
5
+ return Buffer.from(data, encoding).toString("base64");
6
+ }
7
+ if (Buffer.isBuffer(data)) {
8
+ return data.toString("base64");
9
+ }
10
+ return Buffer.from(data).toString("base64");
11
+ }
12
+ decode(encoded, encoding = "utf-8", strict = false) {
13
+ if (Buffer.isBuffer(encoded)) {
14
+ return encoded.toString(encoding);
15
+ }
16
+ const decoded = Buffer.from(encoded, "base64").toString(encoding);
17
+ const isInvalid = this.encode(decoded, encoding) !== encoded;
18
+ if (strict && isInvalid) {
19
+ throw new Error("Cannot decode malformed value");
20
+ }
21
+ return isInvalid ? null : decoded;
22
+ }
23
+ urlEncode(data, encoding) {
24
+ const encoded = typeof data === "string" ? this.encode(data, encoding) : this.encode(data);
25
+ return encoded.replace(/\+/g, "-").replace(/\//g, "_").replace(/\=/g, "");
26
+ }
27
+ urlDecode(encoded, encoding = "utf-8", strict = false) {
28
+ if (Buffer.isBuffer(encoded)) {
29
+ return encoded.toString(encoding);
30
+ }
31
+ const decoded = Buffer.from(encoded, "base64").toString(encoding);
32
+ const isInvalid = this.urlEncode(decoded, encoding) !== encoded;
33
+ if (strict && isInvalid) {
34
+ throw new Error("Cannot urlDecode malformed value");
35
+ }
36
+ return isInvalid ? null : decoded;
37
+ }
38
+ };
39
+ var base64 = new Base64();
40
+ var base64_default = base64;
41
+ export {
42
+ base64_default as default
43
+ };
@@ -1 +1 @@
1
- export * from '@poppinss/exception';
1
+ export { RuntimeException, InvalidArgumentsException, Exception, createError, } from '@poppinss/exception';
@@ -0,0 +1,12 @@
1
+ import {
2
+ Exception,
3
+ InvalidArgumentsException,
4
+ RuntimeException,
5
+ createError
6
+ } from "../chunk-SKNTF5Q5.js";
7
+ export {
8
+ Exception,
9
+ InvalidArgumentsException,
10
+ RuntimeException,
11
+ createError
12
+ };
@@ -0,0 +1,82 @@
1
+ import {
2
+ isScriptFile,
3
+ naturalSort
4
+ } from "../../chunk-XFX47BKO.js";
5
+
6
+ // modules/fs/fs_read_all.ts
7
+ import string from "@poppinss/string";
8
+ import { join, relative, sep } from "path";
9
+ import { readdir, stat } from "fs/promises";
10
+ import { fileURLToPath, pathToFileURL } from "url";
11
+ async function fsReadAll(location, options) {
12
+ const normalizedLocation = typeof location === "string" ? location : fileURLToPath(location);
13
+ const normalizedOptions = Object.assign({ absolute: false, sort: naturalSort }, options);
14
+ const pathType = normalizedOptions.pathType || "relative";
15
+ try {
16
+ await stat(normalizedLocation);
17
+ } catch (error) {
18
+ if (normalizedOptions.ignoreMissingRoot) {
19
+ return [];
20
+ }
21
+ throw error;
22
+ }
23
+ const dirents = await readdir(normalizedLocation, { recursive: true, withFileTypes: true });
24
+ const files = dirents.filter((dirent) => {
25
+ if (!dirent.isFile()) {
26
+ return false;
27
+ }
28
+ if (dirent.name.startsWith(".") || dirent.parentPath.split(sep).some((segment) => segment.startsWith("."))) {
29
+ return false;
30
+ }
31
+ return true;
32
+ }).map((file) => {
33
+ switch (pathType) {
34
+ case "relative":
35
+ return join(relative(normalizedLocation, file.parentPath), file.name);
36
+ case "absolute":
37
+ return join(file.parentPath, file.name);
38
+ case "unixRelative":
39
+ return string.toUnixSlash(join(relative(normalizedLocation, file.parentPath), file.name));
40
+ case "unixAbsolute":
41
+ return string.toUnixSlash(join(file.parentPath, file.name));
42
+ case "url":
43
+ return pathToFileURL(join(file.parentPath, file.name)).href;
44
+ }
45
+ });
46
+ if (normalizedOptions.filter) {
47
+ return files.filter(normalizedOptions.filter).sort(normalizedOptions.sort);
48
+ }
49
+ return files.sort(normalizedOptions.sort);
50
+ }
51
+
52
+ // modules/fs/fs_import_all.ts
53
+ import { fileURLToPath as fileURLToPath2 } from "url";
54
+ import lodash from "@poppinss/utils/lodash";
55
+ import { extname, relative as relative2, sep as sep2 } from "path";
56
+ async function importFile(basePath, fileURL, values, options) {
57
+ const filePath = fileURLToPath2(fileURL);
58
+ const fileExtension = extname(filePath);
59
+ const collectionKey = relative2(basePath, filePath).replace(new RegExp(`${fileExtension}$`), "").split(sep2);
60
+ const exportedValue = fileExtension === ".json" ? await import(fileURL, { with: { type: "json" } }) : await import(fileURL);
61
+ lodash.set(
62
+ values,
63
+ options.transformKeys ? options.transformKeys(collectionKey) : collectionKey,
64
+ exportedValue.default ? exportedValue.default : { ...exportedValue }
65
+ );
66
+ }
67
+ async function fsImportAll(location, options) {
68
+ options = options || {};
69
+ const collection = {};
70
+ const normalizedLocation = typeof location === "string" ? location : fileURLToPath2(location);
71
+ const files = await fsReadAll(normalizedLocation, {
72
+ filter: isScriptFile,
73
+ ...options,
74
+ pathType: "url"
75
+ });
76
+ await Promise.all(files.map((file) => importFile(normalizedLocation, file, collection, options)));
77
+ return collection;
78
+ }
79
+ export {
80
+ fsImportAll,
81
+ fsReadAll
82
+ };
@@ -0,0 +1,8 @@
1
+ import {
2
+ safeParse,
3
+ safeStringify
4
+ } from "../../chunk-YFSCSJNE.js";
5
+ export {
6
+ safeParse,
7
+ safeStringify
8
+ };
@@ -0,0 +1,6 @@
1
+ // modules/string/main.ts
2
+ import string from "@poppinss/string";
3
+ var main_default = string;
4
+ export {
5
+ main_default as default
6
+ };
@@ -0,0 +1,6 @@
1
+ // modules/string/string_builder.ts
2
+ import StringBuilder from "@poppinss/string/builder";
3
+ var string_builder_default = StringBuilder;
4
+ export {
5
+ string_builder_default as default
6
+ };
@@ -0,0 +1,2 @@
1
+ // modules/types.ts
2
+ export * from "@poppinss/types";
package/package.json CHANGED
@@ -1,13 +1,12 @@
1
1
  {
2
2
  "name": "@poppinss/utils",
3
- "version": "7.0.0-next.0",
3
+ "version": "7.0.0-next.1",
4
4
  "description": "Handy utilities for repetitive work",
5
5
  "main": "build/index.js",
6
6
  "type": "module",
7
7
  "files": [
8
8
  "build",
9
9
  "!build/tests",
10
- "!build/test_helpers",
11
10
  "!build/bin",
12
11
  "lodash"
13
12
  ],
@@ -98,14 +97,14 @@
98
97
  "tsup": {
99
98
  "entry": [
100
99
  "./index.ts",
101
- "./src/assert.ts",
102
- "./src/string/main.ts",
103
- "./src/string/string_builder.ts",
104
- "./src/json/main.ts",
105
- "./src/types.ts",
106
- "./src/slash.ts",
107
- "./src/exception.ts",
108
- "./src/exceptions/main.ts"
100
+ "./modules/fs/main.ts",
101
+ "./modules/json/main.ts",
102
+ "./modules/string/main.ts",
103
+ "./modules/string/string_builder.ts",
104
+ "./modules/assert.ts",
105
+ "./modules/base64.ts",
106
+ "./modules/exception.ts",
107
+ "./modules/types.ts"
109
108
  ],
110
109
  "outDir": "./build",
111
110
  "clean": true,