@haatlabs-core/logger 0.0.0-stage → 0.11.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/LICENSE +201 -0
- package/README.md +10 -2
- package/dist/cjs/index.d.ts +14 -0
- package/dist/cjs/index.js +167 -0
- package/dist/cjs/package.json +3 -0
- package/dist/cjs/redis-watch.d.ts +17 -0
- package/dist/cjs/redis-watch.js +40 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.js +148 -0
- package/dist/index.js.map +1 -0
- package/dist/redis-watch.d.ts +17 -0
- package/dist/redis-watch.js +21 -0
- package/dist/redis-watch.js.map +1 -0
- package/package.json +54 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "{}"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2026 Haatlabs
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
CHANGED
|
@@ -1,3 +1,11 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @haatlabs-core/logger
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Pino, with secrets scrubbed from every record: sensitive keys, credentials in URLs, SQL
|
|
4
|
+
parameters in database errors.
|
|
5
|
+
|
|
6
|
+
import { createLogger } from '@haatlabs-core/logger';
|
|
7
|
+
const logger = createLogger({ name: 'api', level: 'info' });
|
|
8
|
+
|
|
9
|
+
## Install
|
|
10
|
+
|
|
11
|
+
pnpm add @haatlabs-core/logger
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { Logger as PinoLogger } from 'pino';
|
|
2
|
+
export type Logger = PinoLogger;
|
|
3
|
+
export declare const REDACTED = "[REDACTED]";
|
|
4
|
+
export declare const TRUNCATED = "[TRUNCATED]";
|
|
5
|
+
export declare const CIRCULAR = "[CIRCULAR]";
|
|
6
|
+
export declare const MAX_SCRUB_DEPTH = 12;
|
|
7
|
+
export declare const SENSITIVE_KEYS: readonly ["accessToken", "access_token", "accessTokenEncrypted", "apiSecret", "clientSecret", "client_secret", "password", "passwordHash", "token", "sessionToken", "subject_token", "apiKey", "keyHash", "csrfToken", "authorization", "cookie", "set-cookie", "x-shopify-access-token", "x-shopify-hmac-sha256", "x-csrf-token", "sessionId", "secret", "code", "hmac", "state", "browserNonceHash", "encryptionKey", "databaseUrl", "url", "systemUrl"];
|
|
8
|
+
export declare function createLogger(options: {
|
|
9
|
+
level: string;
|
|
10
|
+
name: string;
|
|
11
|
+
destination?: NodeJS.WritableStream;
|
|
12
|
+
}): Logger;
|
|
13
|
+
export { watchRedis } from './redis-watch.js';
|
|
14
|
+
export type { RedisWatchLogger, WatchedRedis } from './redis-watch.js';
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __export = (target, all) => {
|
|
6
|
+
for (var name in all)
|
|
7
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
+
};
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
+
var index_exports = {};
|
|
19
|
+
__export(index_exports, {
|
|
20
|
+
CIRCULAR: () => CIRCULAR,
|
|
21
|
+
MAX_SCRUB_DEPTH: () => MAX_SCRUB_DEPTH,
|
|
22
|
+
REDACTED: () => REDACTED,
|
|
23
|
+
SENSITIVE_KEYS: () => SENSITIVE_KEYS,
|
|
24
|
+
TRUNCATED: () => TRUNCATED,
|
|
25
|
+
createLogger: () => createLogger,
|
|
26
|
+
watchRedis: () => import_redis_watch.watchRedis
|
|
27
|
+
});
|
|
28
|
+
module.exports = __toCommonJS(index_exports);
|
|
29
|
+
var import_api = require("@opentelemetry/api");
|
|
30
|
+
var import_pino = require("pino");
|
|
31
|
+
var import_redis_watch = require("./redis-watch.js");
|
|
32
|
+
const REDACTED = "[REDACTED]";
|
|
33
|
+
const TRUNCATED = "[TRUNCATED]";
|
|
34
|
+
const CIRCULAR = "[CIRCULAR]";
|
|
35
|
+
const MAX_SCRUB_DEPTH = 12;
|
|
36
|
+
const SENSITIVE_KEYS = [
|
|
37
|
+
"accessToken",
|
|
38
|
+
"access_token",
|
|
39
|
+
"accessTokenEncrypted",
|
|
40
|
+
"apiSecret",
|
|
41
|
+
"clientSecret",
|
|
42
|
+
"client_secret",
|
|
43
|
+
"password",
|
|
44
|
+
"passwordHash",
|
|
45
|
+
"token",
|
|
46
|
+
"sessionToken",
|
|
47
|
+
"subject_token",
|
|
48
|
+
"apiKey",
|
|
49
|
+
"keyHash",
|
|
50
|
+
"csrfToken",
|
|
51
|
+
"authorization",
|
|
52
|
+
"cookie",
|
|
53
|
+
"set-cookie",
|
|
54
|
+
"x-shopify-access-token",
|
|
55
|
+
"x-shopify-hmac-sha256",
|
|
56
|
+
"x-csrf-token",
|
|
57
|
+
"sessionId",
|
|
58
|
+
"secret",
|
|
59
|
+
"code",
|
|
60
|
+
"hmac",
|
|
61
|
+
"state",
|
|
62
|
+
"browserNonceHash",
|
|
63
|
+
"encryptionKey",
|
|
64
|
+
"databaseUrl",
|
|
65
|
+
"url",
|
|
66
|
+
"systemUrl"
|
|
67
|
+
];
|
|
68
|
+
const SENSITIVE_LOOKUP = new Set(SENSITIVE_KEYS.map((key) => key.toLowerCase()));
|
|
69
|
+
const URL_CREDENTIALS = /([a-z][a-z0-9+.-]*:\/\/[^\s:@/]*:)[^\s/]*@/gi;
|
|
70
|
+
const PARAMS_LINE = /^params:/m;
|
|
71
|
+
const FAILED_QUERY_PREFIX = "Failed query:";
|
|
72
|
+
const QUERY_FAILED_TEXT = "Database query failed";
|
|
73
|
+
const ERROR_CODE_LABEL = /^[A-Z][A-Z0-9_]{0,63}$/;
|
|
74
|
+
const scrubUrlCredentials = (text) => text.replace(URL_CREDENTIALS, `$1${REDACTED}@`);
|
|
75
|
+
function scrubQueryText(text) {
|
|
76
|
+
const paramsAt = text.search(PARAMS_LINE);
|
|
77
|
+
if (paramsAt < 0)
|
|
78
|
+
return text;
|
|
79
|
+
const head = text.slice(0, paramsAt).trimEnd();
|
|
80
|
+
return head.startsWith(FAILED_QUERY_PREFIX) ? QUERY_FAILED_TEXT : head;
|
|
81
|
+
}
|
|
82
|
+
function scrubErrorMessage(message) {
|
|
83
|
+
return scrubUrlCredentials(scrubQueryText(message));
|
|
84
|
+
}
|
|
85
|
+
function scrubErrorStack(stack, message) {
|
|
86
|
+
if (message === "" || !stack.includes(message))
|
|
87
|
+
return scrubUrlCredentials(scrubQueryText(stack));
|
|
88
|
+
return scrubUrlCredentials(stack.replace(message, () => scrubErrorMessage(message)));
|
|
89
|
+
}
|
|
90
|
+
const isWrappedQueryError = (error) => "query" in error && "params" in error;
|
|
91
|
+
function isPlainObject(value) {
|
|
92
|
+
const prototype = Object.getPrototypeOf(value);
|
|
93
|
+
return prototype === Object.prototype || prototype === null;
|
|
94
|
+
}
|
|
95
|
+
function scrubValue(value, depth, ancestors) {
|
|
96
|
+
if (typeof value === "string")
|
|
97
|
+
return scrubUrlCredentials(value);
|
|
98
|
+
if (typeof value !== "object" || value === null)
|
|
99
|
+
return value;
|
|
100
|
+
if (ancestors.includes(value))
|
|
101
|
+
return CIRCULAR;
|
|
102
|
+
if (depth > MAX_SCRUB_DEPTH)
|
|
103
|
+
return TRUNCATED;
|
|
104
|
+
const path = [...ancestors, value];
|
|
105
|
+
if (Array.isArray(value))
|
|
106
|
+
return value.map((item) => scrubValue(item, depth + 1, path));
|
|
107
|
+
const isError = value instanceof Error;
|
|
108
|
+
if (!isError && !isPlainObject(value))
|
|
109
|
+
return value;
|
|
110
|
+
const result = {};
|
|
111
|
+
if (isError) {
|
|
112
|
+
result.type = value.name;
|
|
113
|
+
result.message = scrubErrorMessage(value.message);
|
|
114
|
+
if (value.stack !== void 0)
|
|
115
|
+
result.stack = scrubErrorStack(value.stack, value.message);
|
|
116
|
+
if (value.cause !== void 0)
|
|
117
|
+
result.cause = scrubValue(value.cause, depth + 1, path);
|
|
118
|
+
}
|
|
119
|
+
const wrappedQuery = isError && isWrappedQueryError(value);
|
|
120
|
+
for (const [key, child] of Object.entries(value)) {
|
|
121
|
+
if (wrappedQuery && (key === "query" || key === "params"))
|
|
122
|
+
continue;
|
|
123
|
+
const keepErrorCode = isError && key === "code" && ERROR_CODE_LABEL.test(String(child));
|
|
124
|
+
result[key] = SENSITIVE_LOOKUP.has(key.toLowerCase()) && !keepErrorCode ? REDACTED : scrubValue(child, depth + 1, path);
|
|
125
|
+
}
|
|
126
|
+
return result;
|
|
127
|
+
}
|
|
128
|
+
function scrub(record) {
|
|
129
|
+
const scrubbed = scrubValue(record, 0, []);
|
|
130
|
+
return typeof scrubbed === "object" && scrubbed !== null ? scrubbed : {};
|
|
131
|
+
}
|
|
132
|
+
function withScrubbedChildren(logger) {
|
|
133
|
+
const child = logger.child.bind(logger);
|
|
134
|
+
const scrubbedChild = (bindings, childOptions) => withScrubbedChildren(child(scrub(bindings), childOptions));
|
|
135
|
+
Object.defineProperty(logger, "child", {
|
|
136
|
+
value: scrubbedChild,
|
|
137
|
+
configurable: true,
|
|
138
|
+
writable: true
|
|
139
|
+
});
|
|
140
|
+
return logger;
|
|
141
|
+
}
|
|
142
|
+
function traceIds() {
|
|
143
|
+
const context = import_api.trace.getActiveSpan()?.spanContext();
|
|
144
|
+
return context && (0, import_api.isSpanContextValid)(context) ? { trace_id: context.traceId, span_id: context.spanId } : {};
|
|
145
|
+
}
|
|
146
|
+
function createLogger(options) {
|
|
147
|
+
return withScrubbedChildren((0, import_pino.pino)({
|
|
148
|
+
name: options.name,
|
|
149
|
+
level: options.level,
|
|
150
|
+
// Scrubbing runs on the whole record before serializers, so errors are
|
|
151
|
+
// converted here and the default `err` serializer is replaced by identity.
|
|
152
|
+
formatters: { log: scrub, bindings: scrub },
|
|
153
|
+
serializers: { err: (value) => value },
|
|
154
|
+
timestamp: import_pino.pino.stdTimeFunctions.isoTime,
|
|
155
|
+
mixin: traceIds
|
|
156
|
+
}, options.destination ?? import_pino.pino.destination({ sync: false })));
|
|
157
|
+
}
|
|
158
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
159
|
+
0 && (module.exports = {
|
|
160
|
+
CIRCULAR,
|
|
161
|
+
MAX_SCRUB_DEPTH,
|
|
162
|
+
REDACTED,
|
|
163
|
+
SENSITIVE_KEYS,
|
|
164
|
+
TRUNCATED,
|
|
165
|
+
createLogger,
|
|
166
|
+
watchRedis
|
|
167
|
+
});
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/** What watchRedis logs through: the warn and info of any pino-style logger. */
|
|
2
|
+
export interface RedisWatchLogger {
|
|
3
|
+
warn(fields: Record<string, unknown>, msg: string): void;
|
|
4
|
+
info(fields: Record<string, unknown>, msg: string): void;
|
|
5
|
+
}
|
|
6
|
+
/** An ioredis client, or anything else that emits its 'error' and 'ready' events. */
|
|
7
|
+
export interface WatchedRedis {
|
|
8
|
+
on(event: 'error', listener: (err: Error) => void): unknown;
|
|
9
|
+
on(event: 'ready', listener: () => void): unknown;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Logs a Redis client's outages: once when it becomes unreachable, with the error, and once
|
|
13
|
+
* when it is back, with how many errors it saw meanwhile. ioredis emits an error on every
|
|
14
|
+
* reconnect attempt, and without a listener prints each as "Unhandled error event". `name`
|
|
15
|
+
* says which Redis (queue, api, cache) in each line.
|
|
16
|
+
*/
|
|
17
|
+
export declare function watchRedis<T extends WatchedRedis>(redis: T, logger: RedisWatchLogger, name: string): T;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __export = (target, all) => {
|
|
6
|
+
for (var name in all)
|
|
7
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
+
};
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
+
var redis_watch_exports = {};
|
|
19
|
+
__export(redis_watch_exports, {
|
|
20
|
+
watchRedis: () => watchRedis
|
|
21
|
+
});
|
|
22
|
+
module.exports = __toCommonJS(redis_watch_exports);
|
|
23
|
+
function watchRedis(redis, logger, name) {
|
|
24
|
+
let errors = 0;
|
|
25
|
+
redis.on("error", (err) => {
|
|
26
|
+
errors += 1;
|
|
27
|
+
if (errors === 1)
|
|
28
|
+
logger.warn({ redis: name, err }, "Redis unreachable");
|
|
29
|
+
});
|
|
30
|
+
redis.on("ready", () => {
|
|
31
|
+
if (errors > 0)
|
|
32
|
+
logger.info({ redis: name, errors }, "Redis reachable again");
|
|
33
|
+
errors = 0;
|
|
34
|
+
});
|
|
35
|
+
return redis;
|
|
36
|
+
}
|
|
37
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
38
|
+
0 && (module.exports = {
|
|
39
|
+
watchRedis
|
|
40
|
+
});
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { Logger as PinoLogger } from 'pino';
|
|
2
|
+
export type Logger = PinoLogger;
|
|
3
|
+
export declare const REDACTED = "[REDACTED]";
|
|
4
|
+
export declare const TRUNCATED = "[TRUNCATED]";
|
|
5
|
+
export declare const CIRCULAR = "[CIRCULAR]";
|
|
6
|
+
export declare const MAX_SCRUB_DEPTH = 12;
|
|
7
|
+
export declare const SENSITIVE_KEYS: readonly ["accessToken", "access_token", "accessTokenEncrypted", "apiSecret", "clientSecret", "client_secret", "password", "passwordHash", "token", "sessionToken", "subject_token", "apiKey", "keyHash", "csrfToken", "authorization", "cookie", "set-cookie", "x-shopify-access-token", "x-shopify-hmac-sha256", "x-csrf-token", "sessionId", "secret", "code", "hmac", "state", "browserNonceHash", "encryptionKey", "databaseUrl", "url", "systemUrl"];
|
|
8
|
+
export declare function createLogger(options: {
|
|
9
|
+
level: string;
|
|
10
|
+
name: string;
|
|
11
|
+
destination?: NodeJS.WritableStream;
|
|
12
|
+
}): Logger;
|
|
13
|
+
export { watchRedis } from './redis-watch.js';
|
|
14
|
+
export type { RedisWatchLogger, WatchedRedis } from './redis-watch.js';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import { isSpanContextValid, trace } from '@opentelemetry/api';
|
|
2
|
+
import { pino } from 'pino';
|
|
3
|
+
export const REDACTED = '[REDACTED]';
|
|
4
|
+
export const TRUNCATED = '[TRUNCATED]';
|
|
5
|
+
export const CIRCULAR = '[CIRCULAR]';
|
|
6
|
+
export const MAX_SCRUB_DEPTH = 12;
|
|
7
|
+
export const SENSITIVE_KEYS = [
|
|
8
|
+
'accessToken',
|
|
9
|
+
'access_token',
|
|
10
|
+
'accessTokenEncrypted',
|
|
11
|
+
'apiSecret',
|
|
12
|
+
'clientSecret',
|
|
13
|
+
'client_secret',
|
|
14
|
+
'password',
|
|
15
|
+
'passwordHash',
|
|
16
|
+
'token',
|
|
17
|
+
'sessionToken',
|
|
18
|
+
'subject_token',
|
|
19
|
+
'apiKey',
|
|
20
|
+
'keyHash',
|
|
21
|
+
'csrfToken',
|
|
22
|
+
'authorization',
|
|
23
|
+
'cookie',
|
|
24
|
+
'set-cookie',
|
|
25
|
+
'x-shopify-access-token',
|
|
26
|
+
'x-shopify-hmac-sha256',
|
|
27
|
+
'x-csrf-token',
|
|
28
|
+
'sessionId',
|
|
29
|
+
'secret',
|
|
30
|
+
'code',
|
|
31
|
+
'hmac',
|
|
32
|
+
'state',
|
|
33
|
+
'browserNonceHash',
|
|
34
|
+
'encryptionKey',
|
|
35
|
+
'databaseUrl',
|
|
36
|
+
'url',
|
|
37
|
+
'systemUrl',
|
|
38
|
+
];
|
|
39
|
+
const SENSITIVE_LOOKUP = new Set(SENSITIVE_KEYS.map((key) => key.toLowerCase()));
|
|
40
|
+
/** `scheme://user:password@host`: the password is replaced, the rest stays readable. */
|
|
41
|
+
const URL_CREDENTIALS = /([a-z][a-z0-9+.-]*:\/\/[^\s:@/]*:)[^\s/]*@/gi;
|
|
42
|
+
const PARAMS_LINE = /^params:/m;
|
|
43
|
+
const FAILED_QUERY_PREFIX = 'Failed query:';
|
|
44
|
+
const QUERY_FAILED_TEXT = 'Database query failed';
|
|
45
|
+
/** An error's own `code` (ECONNRESET, SHOPIFY_AUTH_ERROR) is a label, not an OAuth code. */
|
|
46
|
+
const ERROR_CODE_LABEL = /^[A-Z][A-Z0-9_]{0,63}$/;
|
|
47
|
+
const scrubUrlCredentials = (text) => text.replace(URL_CREDENTIALS, `$1${REDACTED}@`);
|
|
48
|
+
/** Bound query parameters follow a `params:` line; the SQL precedes it. Neither is logged. */
|
|
49
|
+
function scrubQueryText(text) {
|
|
50
|
+
const paramsAt = text.search(PARAMS_LINE);
|
|
51
|
+
if (paramsAt < 0)
|
|
52
|
+
return text;
|
|
53
|
+
const head = text.slice(0, paramsAt).trimEnd();
|
|
54
|
+
return head.startsWith(FAILED_QUERY_PREFIX) ? QUERY_FAILED_TEXT : head;
|
|
55
|
+
}
|
|
56
|
+
function scrubErrorMessage(message) {
|
|
57
|
+
return scrubUrlCredentials(scrubQueryText(message));
|
|
58
|
+
}
|
|
59
|
+
/** Keeps the frames of a stack whose message was rewritten; cuts at `params:` if the message is absent. */
|
|
60
|
+
function scrubErrorStack(stack, message) {
|
|
61
|
+
if (message === '' || !stack.includes(message))
|
|
62
|
+
return scrubUrlCredentials(scrubQueryText(stack));
|
|
63
|
+
return scrubUrlCredentials(stack.replace(message, () => scrubErrorMessage(message)));
|
|
64
|
+
}
|
|
65
|
+
/** Drizzle puts the SQL and the bound values on the error as `query` and `params`. */
|
|
66
|
+
const isWrappedQueryError = (error) => 'query' in error && 'params' in error;
|
|
67
|
+
function isPlainObject(value) {
|
|
68
|
+
const prototype = Object.getPrototypeOf(value);
|
|
69
|
+
return prototype === Object.prototype || prototype === null;
|
|
70
|
+
}
|
|
71
|
+
function scrubValue(value, depth, ancestors) {
|
|
72
|
+
if (typeof value === 'string')
|
|
73
|
+
return scrubUrlCredentials(value);
|
|
74
|
+
if (typeof value !== 'object' || value === null)
|
|
75
|
+
return value;
|
|
76
|
+
if (ancestors.includes(value))
|
|
77
|
+
return CIRCULAR;
|
|
78
|
+
if (depth > MAX_SCRUB_DEPTH)
|
|
79
|
+
return TRUNCATED;
|
|
80
|
+
const path = [...ancestors, value];
|
|
81
|
+
if (Array.isArray(value))
|
|
82
|
+
return value.map((item) => scrubValue(item, depth + 1, path));
|
|
83
|
+
const isError = value instanceof Error;
|
|
84
|
+
if (!isError && !isPlainObject(value))
|
|
85
|
+
return value;
|
|
86
|
+
const result = {};
|
|
87
|
+
if (isError) {
|
|
88
|
+
// Error fields are non-enumerable, so copy the ones that make an error useful.
|
|
89
|
+
result.type = value.name;
|
|
90
|
+
result.message = scrubErrorMessage(value.message);
|
|
91
|
+
if (value.stack !== undefined)
|
|
92
|
+
result.stack = scrubErrorStack(value.stack, value.message);
|
|
93
|
+
if (value.cause !== undefined)
|
|
94
|
+
result.cause = scrubValue(value.cause, depth + 1, path);
|
|
95
|
+
}
|
|
96
|
+
const wrappedQuery = isError && isWrappedQueryError(value);
|
|
97
|
+
for (const [key, child] of Object.entries(value)) {
|
|
98
|
+
if (wrappedQuery && (key === 'query' || key === 'params'))
|
|
99
|
+
continue;
|
|
100
|
+
const keepErrorCode = isError && key === 'code' && ERROR_CODE_LABEL.test(String(child));
|
|
101
|
+
result[key] =
|
|
102
|
+
SENSITIVE_LOOKUP.has(key.toLowerCase()) && !keepErrorCode
|
|
103
|
+
? REDACTED
|
|
104
|
+
: scrubValue(child, depth + 1, path);
|
|
105
|
+
}
|
|
106
|
+
return result;
|
|
107
|
+
}
|
|
108
|
+
function scrub(record) {
|
|
109
|
+
const scrubbed = scrubValue(record, 0, []);
|
|
110
|
+
return typeof scrubbed === 'object' && scrubbed !== null
|
|
111
|
+
? scrubbed
|
|
112
|
+
: {};
|
|
113
|
+
}
|
|
114
|
+
// Pino does not run `formatters.bindings` on child bindings, so child() is wrapped.
|
|
115
|
+
function withScrubbedChildren(logger) {
|
|
116
|
+
const child = logger.child.bind(logger);
|
|
117
|
+
const scrubbedChild = (bindings, childOptions) => withScrubbedChildren(child(scrub(bindings), childOptions));
|
|
118
|
+
Object.defineProperty(logger, 'child', {
|
|
119
|
+
value: scrubbedChild,
|
|
120
|
+
configurable: true,
|
|
121
|
+
writable: true,
|
|
122
|
+
});
|
|
123
|
+
return logger;
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* The active span's IDs, so a line can be followed to its trace (Grafana links `trace_id` to
|
|
127
|
+
* Tempo). Nothing outside a span, or when tracing is off.
|
|
128
|
+
*/
|
|
129
|
+
function traceIds() {
|
|
130
|
+
const context = trace.getActiveSpan()?.spanContext();
|
|
131
|
+
return context && isSpanContextValid(context)
|
|
132
|
+
? { trace_id: context.traceId, span_id: context.spanId }
|
|
133
|
+
: {};
|
|
134
|
+
}
|
|
135
|
+
export function createLogger(options) {
|
|
136
|
+
return withScrubbedChildren(pino({
|
|
137
|
+
name: options.name,
|
|
138
|
+
level: options.level,
|
|
139
|
+
// Scrubbing runs on the whole record before serializers, so errors are
|
|
140
|
+
// converted here and the default `err` serializer is replaced by identity.
|
|
141
|
+
formatters: { log: scrub, bindings: scrub },
|
|
142
|
+
serializers: { err: (value) => value },
|
|
143
|
+
timestamp: pino.stdTimeFunctions.isoTime,
|
|
144
|
+
mixin: traceIds,
|
|
145
|
+
}, options.destination ?? pino.destination({ sync: false })));
|
|
146
|
+
}
|
|
147
|
+
export { watchRedis } from './redis-watch.js';
|
|
148
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,KAAK,EAAE,MAAM,oBAAoB,CAAC;AAC/D,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AAI5B,MAAM,CAAC,MAAM,QAAQ,GAAG,YAAY,CAAC;AACrC,MAAM,CAAC,MAAM,SAAS,GAAG,aAAa,CAAC;AACvC,MAAM,CAAC,MAAM,QAAQ,GAAG,YAAY,CAAC;AACrC,MAAM,CAAC,MAAM,eAAe,GAAG,EAAE,CAAC;AAElC,MAAM,CAAC,MAAM,cAAc,GAAG;IAC5B,aAAa;IACb,cAAc;IACd,sBAAsB;IACtB,WAAW;IACX,cAAc;IACd,eAAe;IACf,UAAU;IACV,cAAc;IACd,OAAO;IACP,cAAc;IACd,eAAe;IACf,QAAQ;IACR,SAAS;IACT,WAAW;IACX,eAAe;IACf,QAAQ;IACR,YAAY;IACZ,w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AC3C,WAAW,EAAE,EAAE,GAAG,EAAE,CAAC,KAAc,EAAE,EAAE,CAAC,KAAK,EAAE;QAC/C,SAAS,EAAE,IAAI,CAAC,gBAAgB,CAAC,OAAO;QACxC,KAAK,EAAE,QAAQ;KAChB,EACD,OAAO,CAAC,WAAW,IAAI,IAAI,CAAC,WAAW,CAAC,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC,CACzD,CACF,CAAC;AACJ,CAAC;AAED,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/** What watchRedis logs through: the warn and info of any pino-style logger. */
|
|
2
|
+
export interface RedisWatchLogger {
|
|
3
|
+
warn(fields: Record<string, unknown>, msg: string): void;
|
|
4
|
+
info(fields: Record<string, unknown>, msg: string): void;
|
|
5
|
+
}
|
|
6
|
+
/** An ioredis client, or anything else that emits its 'error' and 'ready' events. */
|
|
7
|
+
export interface WatchedRedis {
|
|
8
|
+
on(event: 'error', listener: (err: Error) => void): unknown;
|
|
9
|
+
on(event: 'ready', listener: () => void): unknown;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Logs a Redis client's outages: once when it becomes unreachable, with the error, and once
|
|
13
|
+
* when it is back, with how many errors it saw meanwhile. ioredis emits an error on every
|
|
14
|
+
* reconnect attempt, and without a listener prints each as "Unhandled error event". `name`
|
|
15
|
+
* says which Redis (queue, api, cache) in each line.
|
|
16
|
+
*/
|
|
17
|
+
export declare function watchRedis<T extends WatchedRedis>(redis: T, logger: RedisWatchLogger, name: string): T;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Logs a Redis client's outages: once when it becomes unreachable, with the error, and once
|
|
3
|
+
* when it is back, with how many errors it saw meanwhile. ioredis emits an error on every
|
|
4
|
+
* reconnect attempt, and without a listener prints each as "Unhandled error event". `name`
|
|
5
|
+
* says which Redis (queue, api, cache) in each line.
|
|
6
|
+
*/
|
|
7
|
+
export function watchRedis(redis, logger, name) {
|
|
8
|
+
let errors = 0;
|
|
9
|
+
redis.on('error', (err) => {
|
|
10
|
+
errors += 1;
|
|
11
|
+
if (errors === 1)
|
|
12
|
+
logger.warn({ redis: name, err }, 'Redis unreachable');
|
|
13
|
+
});
|
|
14
|
+
redis.on('ready', () => {
|
|
15
|
+
if (errors > 0)
|
|
16
|
+
logger.info({ redis: name, errors }, 'Redis reachable again');
|
|
17
|
+
errors = 0;
|
|
18
|
+
});
|
|
19
|
+
return redis;
|
|
20
|
+
}
|
|
21
|
+
//# sourceMappingURL=redis-watch.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"redis-watch.js","sourceRoot":"","sources":["../src/redis-watch.ts"],"names":[],"mappings":"AAYA;;;;;GAKG;AACH,MAAM,UAAU,UAAU,CACxB,KAAQ,EACR,MAAwB,EACxB,IAAY;IAEZ,IAAI,MAAM,GAAG,CAAC,CAAC;IACf,KAAK,CAAC,EAAE,CAAC,OAAO,EAAE,CAAC,GAAG,EAAE,EAAE;QACxB,MAAM,IAAI,CAAC,CAAC;QACZ,IAAI,MAAM,KAAK,CAAC;YAAE,MAAM,CAAC,IAAI,CAAC,EAAE,KAAK,EAAE,IAAI,EAAE,GAAG,EAAE,EAAE,mBAAmB,CAAC,CAAC;IAC3E,CAAC,CAAC,CAAC;IACH,KAAK,CAAC,EAAE,CAAC,OAAO,EAAE,GAAG,EAAE;QACrB,IAAI,MAAM,GAAG,CAAC;YAAE,MAAM,CAAC,IAAI,CAAC,EAAE,KAAK,EAAE,IAAI,EAAE,MAAM,EAAE,EAAE,uBAAuB,CAAC,CAAC;QAC9E,MAAM,GAAG,CAAC,CAAC;IACb,CAAC,CAAC,CAAC;IACH,OAAO,KAAK,CAAC;AACf,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,56 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@haatlabs-core/logger",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.11.0",
|
|
4
|
+
"description": "Pino logging with secrets scrubbed from every record.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"haatlabs",
|
|
7
|
+
"pino"
|
|
8
|
+
],
|
|
9
|
+
"homepage": "https://haatlabs.shop/docs/packages/logger/",
|
|
10
|
+
"license": "Apache-2.0",
|
|
11
|
+
"type": "module",
|
|
12
|
+
"engines": {
|
|
13
|
+
"node": ">=24"
|
|
14
|
+
},
|
|
15
|
+
"repository": {
|
|
16
|
+
"type": "git",
|
|
17
|
+
"url": "git+https://github.com/Haatlabs/haatlabs-platform.git",
|
|
18
|
+
"directory": "packages/logger"
|
|
19
|
+
},
|
|
20
|
+
"publishConfig": {
|
|
21
|
+
"access": "public",
|
|
22
|
+
"registry": "https://registry.npmjs.org"
|
|
23
|
+
},
|
|
24
|
+
"files": [
|
|
25
|
+
"dist"
|
|
26
|
+
],
|
|
27
|
+
"exports": {
|
|
28
|
+
".": {
|
|
29
|
+
"import": {
|
|
30
|
+
"types": "./dist/index.d.ts",
|
|
31
|
+
"default": "./dist/index.js"
|
|
32
|
+
},
|
|
33
|
+
"require": {
|
|
34
|
+
"types": "./dist/cjs/index.d.ts",
|
|
35
|
+
"default": "./dist/cjs/index.js"
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
},
|
|
39
|
+
"dependencies": {
|
|
40
|
+
"@opentelemetry/api": "^1.9.1",
|
|
41
|
+
"pino": "^10.4.0"
|
|
42
|
+
},
|
|
43
|
+
"devDependencies": {
|
|
44
|
+
"@opentelemetry/sdk-trace-node": "^2.11.0",
|
|
45
|
+
"@haatlabs-core/observability": "0.11.0",
|
|
46
|
+
"@haatlabs-core/tsconfig": "0.11.0",
|
|
47
|
+
"@haatlabs-core/vitest-config": "0.11.0"
|
|
48
|
+
},
|
|
49
|
+
"scripts": {
|
|
50
|
+
"build": "rm -rf dist && tsc -p tsconfig.build.json && node ../../scripts/build-cjs.mjs",
|
|
51
|
+
"typecheck": "tsc --noEmit",
|
|
52
|
+
"test": "vitest run",
|
|
53
|
+
"check:package": "publint --strict && attw --pack . --profile node16"
|
|
54
|
+
},
|
|
55
|
+
"platformRegistry": "npm"
|
|
56
|
+
}
|