@zauru-sdk/utils 6.28.0 → 6.30.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/dist/esm/zauru/auth.loaders.js +7 -12
- package/dist/esm/zauru/cloudwatch-logger.server.js +59 -3
- package/dist/esm/zauru/cloudwatch.server.js +3 -1
- package/dist/esm/zauru/remix-entry.server.js +2 -0
- package/dist/esm/zauru/remix-handle-request.server.js +17 -0
- package/dist/zauru/cloudwatch-logger.server.d.ts +5 -0
- package/dist/zauru/cloudwatch.server.d.ts +1 -7
- package/dist/zauru/remix-entry.server.d.ts +0 -7
- package/package.json +4 -4
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { redirect } from "@remix-run/node";
|
|
2
|
-
import { isOauthUserinfoUnauthorizedError } from "@zauru-sdk/common";
|
|
2
|
+
import { ZAURU_OAUTH_REAUTH_COOKIE, hasZauruOauthReauthCookie, isOauthUserinfoUnauthorizedError, } from "@zauru-sdk/common";
|
|
3
3
|
import { config } from "@zauru-sdk/config";
|
|
4
4
|
import { commitSession, destroySession, getHeaders, getRefreshSession, getSession, loginWebApp, } from "@zauru-sdk/services";
|
|
5
5
|
const loginErrorPayload = (description, title) => ({
|
|
@@ -27,16 +27,12 @@ const evaluateLoginAccess = async (extras, session, headers) => {
|
|
|
27
27
|
return Response.json(loginErrorPayload(description));
|
|
28
28
|
}
|
|
29
29
|
};
|
|
30
|
-
const OAUTH_REAUTH_COOKIE = "zauru_oauth_reauth";
|
|
31
30
|
const authorizeUrl = (request) => {
|
|
32
31
|
const url = new URL(request.url);
|
|
33
32
|
const hostname = `${url.port ? "http://" : "https://"}${url.hostname}${url.port ? `:${url.port}` : ""}`;
|
|
34
33
|
return `${config.oauthBaseURL}/dialog/authorize?client_id=${config.oauthClientID}&response_type=code&redirect_uri=${hostname}/login`;
|
|
35
34
|
};
|
|
36
|
-
const
|
|
37
|
-
.split(";")
|
|
38
|
-
.some((part) => part.trim() === `${OAUTH_REAUTH_COOKIE}=1`);
|
|
39
|
-
const oauthReauthSetCookie = () => `${OAUTH_REAUTH_COOKIE}=1; Path=/; Max-Age=120; SameSite=Lax`;
|
|
35
|
+
const oauthReauthSetCookie = () => `${ZAURU_OAUTH_REAUTH_COOKIE}=1; Path=/; Max-Age=120; SameSite=Lax`;
|
|
40
36
|
const redirectOauthReauth = async (session) => {
|
|
41
37
|
const headers = new Headers();
|
|
42
38
|
headers.append("Set-Cookie", await destroySession(session));
|
|
@@ -92,7 +88,7 @@ export function createLoginAction(extras) {
|
|
|
92
88
|
if (loginResponse.error || !loginResponse.data) {
|
|
93
89
|
const userMsg = loginResponse.userMsg?.toString() ?? "";
|
|
94
90
|
if (isOauthUserinfoUnauthorizedError(userMsg) &&
|
|
95
|
-
!
|
|
91
|
+
!hasZauruOauthReauthCookie(request.headers.get("Cookie"))) {
|
|
96
92
|
return redirectOauthReauth(session);
|
|
97
93
|
}
|
|
98
94
|
return Response.json(loginErrorPayload(userMsg));
|
|
@@ -139,11 +135,10 @@ export function createLogoutLoader(extras) {
|
|
|
139
135
|
const url = new URL(args.request.url);
|
|
140
136
|
if (url.searchParams.get("reauth") === "1") {
|
|
141
137
|
const session = await getSession(args.request.headers.get("Cookie"));
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
});
|
|
138
|
+
const headers = new Headers();
|
|
139
|
+
headers.append("Set-Cookie", await destroySession(session));
|
|
140
|
+
headers.append("Set-Cookie", oauthReauthSetCookie());
|
|
141
|
+
return redirect("/login", { headers });
|
|
147
142
|
}
|
|
148
143
|
return guard(args);
|
|
149
144
|
};
|
|
@@ -51,12 +51,65 @@ let clientKey = null;
|
|
|
51
51
|
const sequenceTokens = {};
|
|
52
52
|
/** Serializes PutLogEvents so sequence tokens are not overwritten. */
|
|
53
53
|
let putChain = Promise.resolve();
|
|
54
|
+
let processHandlersInstalled = false;
|
|
55
|
+
let loggedConfigOnce = false;
|
|
54
56
|
function envTrim(name) {
|
|
55
57
|
const v = process.env[name];
|
|
56
58
|
if (!v || v.trim() === "")
|
|
57
59
|
return undefined;
|
|
58
60
|
return v.trim();
|
|
59
61
|
}
|
|
62
|
+
/**
|
|
63
|
+
* Netlify streaming functions freeze the isolate when the response ends.
|
|
64
|
+
* `waitUntil` keeps the Lambda awake until PutLogEvents finishes.
|
|
65
|
+
*/
|
|
66
|
+
function keepIsolateAlive(work) {
|
|
67
|
+
const g = globalThis;
|
|
68
|
+
try {
|
|
69
|
+
g.waitUntil?.(work);
|
|
70
|
+
g.Netlify?.waitUntil?.(work);
|
|
71
|
+
}
|
|
72
|
+
catch {
|
|
73
|
+
// Hosts without waitUntil still await in the request path.
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
function logConfigOnce() {
|
|
77
|
+
if (loggedConfigOnce)
|
|
78
|
+
return;
|
|
79
|
+
loggedConfigOnce = true;
|
|
80
|
+
const cfg = resolveCloudWatchConfig();
|
|
81
|
+
if (cfg.enabled) {
|
|
82
|
+
console.log(`[observability] CloudWatch enabled group=${cfg.logGroup} env=${cfg.appEnv} creds=${cfg.prefixLabel} region=${cfg.region}`);
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
console.log(`[observability] CloudWatch disabled: ${cfg.skipReason}`);
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Catch throws that happen after Remix already returned 200 (fire-and-forget
|
|
89
|
+
* async work). Remix `handleError` does not see those.
|
|
90
|
+
*/
|
|
91
|
+
export function installProcessErrorHandlers() {
|
|
92
|
+
if (processHandlersInstalled)
|
|
93
|
+
return;
|
|
94
|
+
if (typeof process === "undefined" || typeof process.on !== "function") {
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
processHandlersInstalled = true;
|
|
98
|
+
const report = (source, reason) => {
|
|
99
|
+
const err = reason instanceof Error ? reason : new Error(String(reason));
|
|
100
|
+
void logError("server", {
|
|
101
|
+
message: err.message,
|
|
102
|
+
stack: err.stack,
|
|
103
|
+
source,
|
|
104
|
+
});
|
|
105
|
+
};
|
|
106
|
+
process.on("unhandledRejection", (reason) => {
|
|
107
|
+
report("unhandledRejection", reason);
|
|
108
|
+
});
|
|
109
|
+
process.on("uncaughtException", (err) => {
|
|
110
|
+
report("uncaughtException", err);
|
|
111
|
+
});
|
|
112
|
+
}
|
|
60
113
|
function resolveAwsCreds() {
|
|
61
114
|
for (const c of AWS_CANDIDATES) {
|
|
62
115
|
const region = envTrim(c.region);
|
|
@@ -201,9 +254,9 @@ async function fetchSequenceToken(cw, logGroup, stream) {
|
|
|
201
254
|
const res = await cw.send(new DescribeLogStreamsCommand({
|
|
202
255
|
logGroupName: logGroup,
|
|
203
256
|
logStreamNamePrefix: stream,
|
|
204
|
-
limit: 1,
|
|
205
257
|
}));
|
|
206
|
-
|
|
258
|
+
const match = res.logStreams?.find((s) => s.logStreamName === stream);
|
|
259
|
+
return match?.uploadSequenceToken;
|
|
207
260
|
}
|
|
208
261
|
catch {
|
|
209
262
|
return undefined;
|
|
@@ -350,6 +403,7 @@ async function emitToCloudWatch(origin, lines) {
|
|
|
350
403
|
* For client/server also prints with `console.error` (Netlify fallback).
|
|
351
404
|
*/
|
|
352
405
|
export async function logEvent(origin, payload) {
|
|
406
|
+
logConfigOnce();
|
|
353
407
|
const line = buildLogLine(origin, {
|
|
354
408
|
...payload,
|
|
355
409
|
appVersion: payload.appVersion ?? getDefaultAppVersion(),
|
|
@@ -360,7 +414,9 @@ export async function logEvent(origin, payload) {
|
|
|
360
414
|
else {
|
|
361
415
|
console.log(`[observability] ${line}`);
|
|
362
416
|
}
|
|
363
|
-
|
|
417
|
+
const pending = emitToCloudWatch(origin, [line]);
|
|
418
|
+
keepIsolateAlive(pending);
|
|
419
|
+
return pending;
|
|
364
420
|
}
|
|
365
421
|
/**
|
|
366
422
|
* Sends an error event to CloudWatch (and console.error as fallback).
|
|
@@ -4,5 +4,7 @@
|
|
|
4
4
|
* Import from `@zauru-sdk/utils/cloudwatch.server` (not the main barrel) so
|
|
5
5
|
* the AWS SDK stays out of the client bundle.
|
|
6
6
|
*/
|
|
7
|
-
|
|
7
|
+
import { installProcessErrorHandlers } from "./cloudwatch-logger.server.js";
|
|
8
|
+
installProcessErrorHandlers();
|
|
9
|
+
export { installProcessErrorHandlers, isCloudWatchEnabled, logError, logEvent, resolveCloudWatchConfig, withBackgroundLogs, } from "./cloudwatch-logger.server.js";
|
|
8
10
|
export { createClientErrorsAction, handleError, } from "./cloudwatch-remix.server.js";
|
|
@@ -5,4 +5,6 @@
|
|
|
5
5
|
* export { handleRequest as default, handleError } from "@zauru-sdk/utils/remix-entry.server";
|
|
6
6
|
* ```
|
|
7
7
|
*/
|
|
8
|
+
import { installProcessErrorHandlers } from "./cloudwatch-logger.server.js";
|
|
9
|
+
installProcessErrorHandlers();
|
|
8
10
|
export { handleRequest, handleError } from "./remix-handle-request.server.js";
|
|
@@ -3,6 +3,7 @@ import { PassThrough } from "node:stream";
|
|
|
3
3
|
import { createReadableStreamFromReadable } from "@remix-run/node";
|
|
4
4
|
import { RemixServer } from "@remix-run/react";
|
|
5
5
|
import { renderToPipeableStream } from "react-dom/server";
|
|
6
|
+
import { logError } from "./cloudwatch-logger.server.js";
|
|
6
7
|
import { handleError } from "./cloudwatch-remix.server.js";
|
|
7
8
|
const ABORT_DELAY = 5_000;
|
|
8
9
|
const RemixServerComponent = RemixServer;
|
|
@@ -53,6 +54,14 @@ function handleBotRequest(request, responseStatusCode, responseHeaders, remixCon
|
|
|
53
54
|
responseStatusCode = 500;
|
|
54
55
|
if (shellRendered) {
|
|
55
56
|
console.error(error);
|
|
57
|
+
const err = error instanceof Error ? error : new Error(String(error));
|
|
58
|
+
void logError("server", {
|
|
59
|
+
message: err.message,
|
|
60
|
+
stack: err.stack,
|
|
61
|
+
url: request.url,
|
|
62
|
+
method: request.method,
|
|
63
|
+
source: "ssr",
|
|
64
|
+
});
|
|
56
65
|
}
|
|
57
66
|
},
|
|
58
67
|
});
|
|
@@ -85,6 +94,14 @@ function handleBrowserRequest(request, responseStatusCode, responseHeaders, remi
|
|
|
85
94
|
responseStatusCode = 500;
|
|
86
95
|
if (shellRendered) {
|
|
87
96
|
console.error(error);
|
|
97
|
+
const err = error instanceof Error ? error : new Error(String(error));
|
|
98
|
+
void logError("server", {
|
|
99
|
+
message: err.message,
|
|
100
|
+
stack: err.stack,
|
|
101
|
+
url: request.url,
|
|
102
|
+
method: request.method,
|
|
103
|
+
source: "ssr",
|
|
104
|
+
});
|
|
88
105
|
}
|
|
89
106
|
},
|
|
90
107
|
});
|
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
export type LogOrigin = "client" | "server" | "background";
|
|
2
2
|
export type LogLevel = "error" | "info" | "warn";
|
|
3
|
+
/**
|
|
4
|
+
* Catch throws that happen after Remix already returned 200 (fire-and-forget
|
|
5
|
+
* async work). Remix `handleError` does not see those.
|
|
6
|
+
*/
|
|
7
|
+
export declare function installProcessErrorHandlers(): void;
|
|
3
8
|
export type CloudWatchConfigInfo = {
|
|
4
9
|
enabled: boolean;
|
|
5
10
|
logGroup?: string;
|
|
@@ -1,8 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
* Server-only CloudWatch observability for Remix webapps.
|
|
3
|
-
*
|
|
4
|
-
* Import from `@zauru-sdk/utils/cloudwatch.server` (not the main barrel) so
|
|
5
|
-
* the AWS SDK stays out of the client bundle.
|
|
6
|
-
*/
|
|
7
|
-
export { isCloudWatchEnabled, logError, logEvent, resolveCloudWatchConfig, withBackgroundLogs, type CloudWatchConfigInfo, type LogErrorResult, type LogLevel, type LogOrigin, type LogPayload, } from "./cloudwatch-logger.server.js";
|
|
1
|
+
export { installProcessErrorHandlers, isCloudWatchEnabled, logError, logEvent, resolveCloudWatchConfig, withBackgroundLogs, type CloudWatchConfigInfo, type LogErrorResult, type LogLevel, type LogOrigin, type LogPayload, } from "./cloudwatch-logger.server.js";
|
|
8
2
|
export { createClientErrorsAction, handleError, } from "./cloudwatch-remix.server.js";
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zauru-sdk/utils",
|
|
3
|
-
"version": "6.
|
|
3
|
+
"version": "6.30.0",
|
|
4
4
|
"description": "Utilidades, parseo de request y demás para desarrollo de webapps de Zauru",
|
|
5
5
|
"main": "./dist/esm/index.js",
|
|
6
6
|
"module": "./dist/esm/index.js",
|
|
@@ -71,12 +71,12 @@
|
|
|
71
71
|
"dependencies": {
|
|
72
72
|
"@aws-sdk/client-cloudwatch-logs": "^3.1123.0",
|
|
73
73
|
"@remix-run/node": "^2.8.1",
|
|
74
|
-
"@zauru-sdk/common": "^6.
|
|
74
|
+
"@zauru-sdk/common": "^6.14.0",
|
|
75
75
|
"@zauru-sdk/config": "^6.0.0",
|
|
76
76
|
"@zauru-sdk/graphql": "^6.9.1",
|
|
77
|
-
"@zauru-sdk/services": "^6.
|
|
77
|
+
"@zauru-sdk/services": "^6.17.0",
|
|
78
78
|
"@zauru-sdk/types": "^6.29.0",
|
|
79
79
|
"moment": "^2.30.1"
|
|
80
80
|
},
|
|
81
|
-
"gitHead": "
|
|
81
|
+
"gitHead": "8e5cb991b0da7d1a5d70cd6275d7bfdc52ee1102"
|
|
82
82
|
}
|