@periodic/strontium-next 1.0.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 +27 -0
- package/LICENSE +21 -0
- package/README.md +719 -0
- package/dist/chunk-5YPMJUNO.mjs +146 -0
- package/dist/edge-BycyYwkg.d.mts +75 -0
- package/dist/edge-BycyYwkg.d.ts +75 -0
- package/dist/edge.d.mts +3 -0
- package/dist/edge.d.ts +3 -0
- package/dist/edge.js +147 -0
- package/dist/edge.mjs +10 -0
- package/dist/index.d.mts +54 -0
- package/dist/index.d.ts +54 -0
- package/dist/index.js +246 -0
- package/dist/index.mjs +75 -0
- package/package.json +86 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/index.ts
|
|
31
|
+
var index_exports = {};
|
|
32
|
+
__export(index_exports, {
|
|
33
|
+
STRONTIUM_IDEMPOTENCY_HEADER: () => STRONTIUM_IDEMPOTENCY_HEADER,
|
|
34
|
+
STRONTIUM_REQUEST_ID_HEADER: () => STRONTIUM_REQUEST_ID_HEADER,
|
|
35
|
+
STRONTIUM_TRACE_HEADER: () => STRONTIUM_TRACE_HEADER,
|
|
36
|
+
createBrowserStrontiumClient: () => createBrowserStrontiumClient,
|
|
37
|
+
createEdgeStrontiumClient: () => createServerStrontiumClient,
|
|
38
|
+
createServerStrontiumClient: () => createServerStrontiumClient,
|
|
39
|
+
createStrontiumMiddleware: () => createStrontiumMiddleware,
|
|
40
|
+
extractStrontiumContext: () => extractStrontiumContext,
|
|
41
|
+
getClientStrontiumClient: () => getClientStrontiumClient,
|
|
42
|
+
resetClientInstance: () => resetClientInstance,
|
|
43
|
+
strontiumRevalidateTag: () => strontiumRevalidateTag,
|
|
44
|
+
strontiumRevalidateTags: () => strontiumRevalidateTags,
|
|
45
|
+
taggedRequest: () => taggedRequest,
|
|
46
|
+
withCacheTags: () => withCacheTags,
|
|
47
|
+
wrapRouteHandler: () => wrapRouteHandler
|
|
48
|
+
});
|
|
49
|
+
module.exports = __toCommonJS(index_exports);
|
|
50
|
+
|
|
51
|
+
// src/server.ts
|
|
52
|
+
var import_strontium = require("@periodic/strontium");
|
|
53
|
+
function createServerStrontiumClient(options) {
|
|
54
|
+
const { request, ...clientConfig } = options;
|
|
55
|
+
const forwardedHeaders = {};
|
|
56
|
+
if (request) {
|
|
57
|
+
const headersToForward = [
|
|
58
|
+
"x-request-id",
|
|
59
|
+
"x-correlation-id",
|
|
60
|
+
"x-trace-id",
|
|
61
|
+
"x-b3-traceid",
|
|
62
|
+
"x-b3-spanid",
|
|
63
|
+
"traceparent",
|
|
64
|
+
"tracestate",
|
|
65
|
+
"authorization"
|
|
66
|
+
];
|
|
67
|
+
for (const header of headersToForward) {
|
|
68
|
+
const value = request.headers.get(header);
|
|
69
|
+
if (value) {
|
|
70
|
+
forwardedHeaders[header] = value;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
return (0, import_strontium.createStrontiumClient)({
|
|
75
|
+
protocolMode: "idempotent",
|
|
76
|
+
mode: "strict",
|
|
77
|
+
timeoutMs: 1e4,
|
|
78
|
+
retry: {
|
|
79
|
+
enabled: true,
|
|
80
|
+
maxAttempts: 3,
|
|
81
|
+
strategy: "exponential",
|
|
82
|
+
baseDelayMs: 100,
|
|
83
|
+
jitter: true,
|
|
84
|
+
retryOn: ["network", "5xx"]
|
|
85
|
+
},
|
|
86
|
+
circuitBreaker: {
|
|
87
|
+
failureThreshold: 5,
|
|
88
|
+
resetTimeoutMs: 3e4
|
|
89
|
+
},
|
|
90
|
+
...clientConfig,
|
|
91
|
+
headers: {
|
|
92
|
+
...forwardedHeaders,
|
|
93
|
+
...clientConfig.headers ?? {}
|
|
94
|
+
}
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// src/client.ts
|
|
99
|
+
var import_strontium2 = require("@periodic/strontium");
|
|
100
|
+
var _clientInstance = null;
|
|
101
|
+
function getClientStrontiumClient(config) {
|
|
102
|
+
if (typeof window === "undefined") {
|
|
103
|
+
return null;
|
|
104
|
+
}
|
|
105
|
+
if (!_clientInstance) {
|
|
106
|
+
_clientInstance = (0, import_strontium2.createStrontiumClient)({
|
|
107
|
+
dedupe: true,
|
|
108
|
+
mode: "performance",
|
|
109
|
+
...config
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
return _clientInstance;
|
|
113
|
+
}
|
|
114
|
+
function resetClientInstance() {
|
|
115
|
+
_clientInstance = null;
|
|
116
|
+
}
|
|
117
|
+
function createBrowserStrontiumClient(config) {
|
|
118
|
+
if (typeof window === "undefined") return null;
|
|
119
|
+
return (0, import_strontium2.createStrontiumClient)({ dedupe: true, mode: "performance", ...config });
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// src/middleware.ts
|
|
123
|
+
var STRONTIUM_REQUEST_ID_HEADER = "x-strontium-request-id";
|
|
124
|
+
var STRONTIUM_IDEMPOTENCY_HEADER = "x-strontium-idempotency-key";
|
|
125
|
+
var STRONTIUM_TRACE_HEADER = "x-strontium-trace-id";
|
|
126
|
+
function generateId(prefix) {
|
|
127
|
+
return `${prefix}_${Date.now()}_${Math.random().toString(36).slice(2, 9)}`;
|
|
128
|
+
}
|
|
129
|
+
function createStrontiumMiddleware(options = {}) {
|
|
130
|
+
const { idempotencyNamespace = "strontium" } = options;
|
|
131
|
+
return async function strontiumMiddleware(request, next) {
|
|
132
|
+
const requestId = request.headers.get(STRONTIUM_REQUEST_ID_HEADER) ?? generateId("req");
|
|
133
|
+
const idempotencyKey = request.headers.get(STRONTIUM_IDEMPOTENCY_HEADER) ?? generateId(idempotencyNamespace);
|
|
134
|
+
const traceId = request.headers.get("traceparent") ?? request.headers.get("x-trace-id") ?? generateId("trace");
|
|
135
|
+
const requestHeaders = new Headers(request.headers);
|
|
136
|
+
requestHeaders.set(STRONTIUM_REQUEST_ID_HEADER, requestId);
|
|
137
|
+
requestHeaders.set(STRONTIUM_IDEMPOTENCY_HEADER, idempotencyKey);
|
|
138
|
+
requestHeaders.set(STRONTIUM_TRACE_HEADER, traceId);
|
|
139
|
+
const response = await next();
|
|
140
|
+
response.headers.set(STRONTIUM_REQUEST_ID_HEADER, requestId);
|
|
141
|
+
response.headers.set(STRONTIUM_TRACE_HEADER, traceId);
|
|
142
|
+
return response;
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
function extractStrontiumContext(request) {
|
|
146
|
+
return {
|
|
147
|
+
requestId: request.headers.get(STRONTIUM_REQUEST_ID_HEADER) ?? generateId("req"),
|
|
148
|
+
idempotencyKey: request.headers.get(STRONTIUM_IDEMPOTENCY_HEADER) ?? generateId("strontium"),
|
|
149
|
+
traceId: request.headers.get(STRONTIUM_TRACE_HEADER) ?? void 0
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
// src/route-handler.ts
|
|
154
|
+
var import_server = require("next/server");
|
|
155
|
+
var import_strontium3 = require("@periodic/strontium");
|
|
156
|
+
function wrapRouteHandler(handler, _options = {}) {
|
|
157
|
+
return async function wrappedHandler(request, context) {
|
|
158
|
+
try {
|
|
159
|
+
return await handler(request, context);
|
|
160
|
+
} catch (err) {
|
|
161
|
+
return mapErrorToResponse(err);
|
|
162
|
+
}
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
function mapErrorToResponse(err) {
|
|
166
|
+
if (err instanceof import_strontium3.CircuitOpenError) {
|
|
167
|
+
return import_server.NextResponse.json(
|
|
168
|
+
{ error: "Service temporarily unavailable", code: err.code },
|
|
169
|
+
{ status: 503, headers: { "Retry-After": "30" } }
|
|
170
|
+
);
|
|
171
|
+
}
|
|
172
|
+
if (err instanceof import_strontium3.TimeoutError) {
|
|
173
|
+
return import_server.NextResponse.json(
|
|
174
|
+
{ error: "Upstream request timed out", code: err.code },
|
|
175
|
+
{ status: 504 }
|
|
176
|
+
);
|
|
177
|
+
}
|
|
178
|
+
if (err instanceof import_strontium3.RetryExhaustedError) {
|
|
179
|
+
return import_server.NextResponse.json(
|
|
180
|
+
{ error: "Upstream service unavailable after retries", code: err.code },
|
|
181
|
+
{ status: 502 }
|
|
182
|
+
);
|
|
183
|
+
}
|
|
184
|
+
if (err instanceof import_strontium3.ResponseValidationError) {
|
|
185
|
+
return import_server.NextResponse.json(
|
|
186
|
+
{ error: "Upstream response failed validation", code: err.code },
|
|
187
|
+
{ status: 502 }
|
|
188
|
+
);
|
|
189
|
+
}
|
|
190
|
+
if (err instanceof import_strontium3.IntegrityViolationError) {
|
|
191
|
+
return import_server.NextResponse.json(
|
|
192
|
+
{ error: "Request integrity violation", code: err.code },
|
|
193
|
+
{ status: 409 }
|
|
194
|
+
);
|
|
195
|
+
}
|
|
196
|
+
if (err instanceof import_strontium3.NetworkError) {
|
|
197
|
+
return import_server.NextResponse.json(
|
|
198
|
+
{ error: "Network error communicating with upstream", code: err.code },
|
|
199
|
+
{ status: 502 }
|
|
200
|
+
);
|
|
201
|
+
}
|
|
202
|
+
return import_server.NextResponse.json({ error: "Internal server error" }, { status: 500 });
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
// src/cache.ts
|
|
206
|
+
function withCacheTags(headers, tags) {
|
|
207
|
+
return {
|
|
208
|
+
...headers,
|
|
209
|
+
// Next.js reads cache tags from this header in fetch()
|
|
210
|
+
"x-next-cache-tags": tags.join(",")
|
|
211
|
+
};
|
|
212
|
+
}
|
|
213
|
+
function taggedRequest(request, tags) {
|
|
214
|
+
return {
|
|
215
|
+
...request,
|
|
216
|
+
headers: withCacheTags(request.headers ?? {}, tags)
|
|
217
|
+
};
|
|
218
|
+
}
|
|
219
|
+
async function strontiumRevalidateTag(tag) {
|
|
220
|
+
try {
|
|
221
|
+
const { revalidateTag } = await import("next/cache");
|
|
222
|
+
revalidateTag(tag);
|
|
223
|
+
} catch {
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
async function strontiumRevalidateTags(tags) {
|
|
227
|
+
await Promise.all(tags.map(strontiumRevalidateTag));
|
|
228
|
+
}
|
|
229
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
230
|
+
0 && (module.exports = {
|
|
231
|
+
STRONTIUM_IDEMPOTENCY_HEADER,
|
|
232
|
+
STRONTIUM_REQUEST_ID_HEADER,
|
|
233
|
+
STRONTIUM_TRACE_HEADER,
|
|
234
|
+
createBrowserStrontiumClient,
|
|
235
|
+
createEdgeStrontiumClient,
|
|
236
|
+
createServerStrontiumClient,
|
|
237
|
+
createStrontiumMiddleware,
|
|
238
|
+
extractStrontiumContext,
|
|
239
|
+
getClientStrontiumClient,
|
|
240
|
+
resetClientInstance,
|
|
241
|
+
strontiumRevalidateTag,
|
|
242
|
+
strontiumRevalidateTags,
|
|
243
|
+
taggedRequest,
|
|
244
|
+
withCacheTags,
|
|
245
|
+
wrapRouteHandler
|
|
246
|
+
});
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import {
|
|
2
|
+
STRONTIUM_IDEMPOTENCY_HEADER,
|
|
3
|
+
STRONTIUM_REQUEST_ID_HEADER,
|
|
4
|
+
STRONTIUM_TRACE_HEADER,
|
|
5
|
+
createServerStrontiumClient,
|
|
6
|
+
createStrontiumMiddleware,
|
|
7
|
+
extractStrontiumContext,
|
|
8
|
+
wrapRouteHandler
|
|
9
|
+
} from "./chunk-5YPMJUNO.mjs";
|
|
10
|
+
|
|
11
|
+
// src/client.ts
|
|
12
|
+
import { createStrontiumClient } from "@periodic/strontium";
|
|
13
|
+
var _clientInstance = null;
|
|
14
|
+
function getClientStrontiumClient(config) {
|
|
15
|
+
if (typeof window === "undefined") {
|
|
16
|
+
return null;
|
|
17
|
+
}
|
|
18
|
+
if (!_clientInstance) {
|
|
19
|
+
_clientInstance = createStrontiumClient({
|
|
20
|
+
dedupe: true,
|
|
21
|
+
mode: "performance",
|
|
22
|
+
...config
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
return _clientInstance;
|
|
26
|
+
}
|
|
27
|
+
function resetClientInstance() {
|
|
28
|
+
_clientInstance = null;
|
|
29
|
+
}
|
|
30
|
+
function createBrowserStrontiumClient(config) {
|
|
31
|
+
if (typeof window === "undefined") return null;
|
|
32
|
+
return createStrontiumClient({ dedupe: true, mode: "performance", ...config });
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// src/cache.ts
|
|
36
|
+
function withCacheTags(headers, tags) {
|
|
37
|
+
return {
|
|
38
|
+
...headers,
|
|
39
|
+
// Next.js reads cache tags from this header in fetch()
|
|
40
|
+
"x-next-cache-tags": tags.join(",")
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
function taggedRequest(request, tags) {
|
|
44
|
+
return {
|
|
45
|
+
...request,
|
|
46
|
+
headers: withCacheTags(request.headers ?? {}, tags)
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
async function strontiumRevalidateTag(tag) {
|
|
50
|
+
try {
|
|
51
|
+
const { revalidateTag } = await import("next/cache");
|
|
52
|
+
revalidateTag(tag);
|
|
53
|
+
} catch {
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
async function strontiumRevalidateTags(tags) {
|
|
57
|
+
await Promise.all(tags.map(strontiumRevalidateTag));
|
|
58
|
+
}
|
|
59
|
+
export {
|
|
60
|
+
STRONTIUM_IDEMPOTENCY_HEADER,
|
|
61
|
+
STRONTIUM_REQUEST_ID_HEADER,
|
|
62
|
+
STRONTIUM_TRACE_HEADER,
|
|
63
|
+
createBrowserStrontiumClient,
|
|
64
|
+
createServerStrontiumClient as createEdgeStrontiumClient,
|
|
65
|
+
createServerStrontiumClient,
|
|
66
|
+
createStrontiumMiddleware,
|
|
67
|
+
extractStrontiumContext,
|
|
68
|
+
getClientStrontiumClient,
|
|
69
|
+
resetClientInstance,
|
|
70
|
+
strontiumRevalidateTag,
|
|
71
|
+
strontiumRevalidateTags,
|
|
72
|
+
taggedRequest,
|
|
73
|
+
withCacheTags,
|
|
74
|
+
wrapRouteHandler
|
|
75
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@periodic/strontium-next",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Production-grade Next.js integration for @periodic/strontium — App Router, Pages Router, Edge, and Middleware all covered",
|
|
5
|
+
"main": "./dist/index.js",
|
|
6
|
+
"module": "./dist/index.mjs",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.mjs",
|
|
12
|
+
"require": "./dist/index.js"
|
|
13
|
+
},
|
|
14
|
+
"./edge": {
|
|
15
|
+
"types": "./dist/edge.d.ts",
|
|
16
|
+
"import": "./dist/edge.mjs",
|
|
17
|
+
"require": "./dist/edge.js"
|
|
18
|
+
}
|
|
19
|
+
},
|
|
20
|
+
"files": [
|
|
21
|
+
"dist",
|
|
22
|
+
"README.md",
|
|
23
|
+
"CHANGELOG.md",
|
|
24
|
+
"LICENSE"
|
|
25
|
+
],
|
|
26
|
+
"scripts": {
|
|
27
|
+
"build": "tsup src/index.ts src/edge.ts --format esm,cjs --dts --clean --external next @periodic/strontium",
|
|
28
|
+
"test": "jest",
|
|
29
|
+
"test:watch": "jest --watch",
|
|
30
|
+
"test:coverage": "jest --coverage",
|
|
31
|
+
"lint": "eslint src tests --ext .ts",
|
|
32
|
+
"lint:fix": "eslint src tests --ext .ts --fix",
|
|
33
|
+
"format": "prettier --write \"src/**/*.ts\"",
|
|
34
|
+
"format:check": "prettier --check \"src/**/*.ts\"",
|
|
35
|
+
"typecheck": "tsc --noEmit",
|
|
36
|
+
"clean": "rm -rf dist",
|
|
37
|
+
"prepublishOnly": "npm run format && npm run build && npm run test"
|
|
38
|
+
},
|
|
39
|
+
"keywords": [
|
|
40
|
+
"next",
|
|
41
|
+
"nextjs",
|
|
42
|
+
"http",
|
|
43
|
+
"client",
|
|
44
|
+
"middleware",
|
|
45
|
+
"app-router",
|
|
46
|
+
"pages-router",
|
|
47
|
+
"edge",
|
|
48
|
+
"typescript",
|
|
49
|
+
"strontium"
|
|
50
|
+
],
|
|
51
|
+
"author": "Uday Thakur <udaythakurwork@gmail.com>",
|
|
52
|
+
"license": "MIT",
|
|
53
|
+
"repository": {
|
|
54
|
+
"type": "git",
|
|
55
|
+
"url": "git+https://github.com/udaythakur7469/periodic-strontium-next.git"
|
|
56
|
+
},
|
|
57
|
+
"bugs": {
|
|
58
|
+
"url": "https://github.com/udaythakur7469/periodic-strontium-next/issues"
|
|
59
|
+
},
|
|
60
|
+
"homepage": "https://github.com/udaythakur7469/periodic-strontium-next#readme",
|
|
61
|
+
"peerDependencies": {
|
|
62
|
+
"@periodic/strontium": ">=1.0.0",
|
|
63
|
+
"next": ">=13.0.0"
|
|
64
|
+
},
|
|
65
|
+
"devDependencies": {
|
|
66
|
+
"@periodic/strontium": "^1.0.0",
|
|
67
|
+
"@types/jest": "^29.5.12",
|
|
68
|
+
"@types/node": "^20.12.7",
|
|
69
|
+
"@typescript-eslint/eslint-plugin": "^7.0.0",
|
|
70
|
+
"@typescript-eslint/parser": "^7.0.0",
|
|
71
|
+
"eslint": "^8.57.0",
|
|
72
|
+
"eslint-config-prettier": "^9.1.0",
|
|
73
|
+
"eslint-plugin-prettier": "^5.1.2",
|
|
74
|
+
"jest": "^29.7.0",
|
|
75
|
+
"jest-environment-node": "^29.7.0",
|
|
76
|
+
"next": "^14.2.3",
|
|
77
|
+
"prettier": "^3.2.5",
|
|
78
|
+
"ts-jest": "^29.1.2",
|
|
79
|
+
"tsup": "^8.0.2",
|
|
80
|
+
"typescript": "^5.4.5"
|
|
81
|
+
},
|
|
82
|
+
"engines": {
|
|
83
|
+
"node": ">=16.0.0"
|
|
84
|
+
},
|
|
85
|
+
"sideEffects": false
|
|
86
|
+
}
|