next-ws 0.0.0-beta-20250824054249 → 0.0.0-beta-20250826080310
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/cli.cjs +68 -4
- package/dist/server/index.cjs +49 -3
- package/package.json +1 -1
package/dist/cli.cjs
CHANGED
|
@@ -27,7 +27,7 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
|
|
|
27
27
|
var import_minimist = __toESM(require("minimist"));
|
|
28
28
|
|
|
29
29
|
// package.json
|
|
30
|
-
var version = "0.0.0-beta-
|
|
30
|
+
var version = "0.0.0-beta-20250826080310";
|
|
31
31
|
|
|
32
32
|
// src/commands/helpers/define.ts
|
|
33
33
|
function defineCommandGroup(definition) {
|
|
@@ -757,14 +757,78 @@ var patchRouterServer = definePatchStep({
|
|
|
757
757
|
}).toSource();
|
|
758
758
|
}
|
|
759
759
|
});
|
|
760
|
+
var patchHeaders = definePatchStep({
|
|
761
|
+
title: "Add WebSocket contextual headers resolution to request headers",
|
|
762
|
+
path: "next:dist/client/components/headers.js",
|
|
763
|
+
async transform(code) {
|
|
764
|
+
const marker = "@patch headers";
|
|
765
|
+
const snippet = (0, import_jscodeshift.default)(`
|
|
766
|
+
// ${marker}
|
|
767
|
+
const kRequestStorage = Symbol.for('next-ws.request-store');
|
|
768
|
+
const requestStorage = Reflect.get(globalThis, kRequestStorage);
|
|
769
|
+
const contextualHeaders = requestStorage?.getStore()?.headers;
|
|
770
|
+
if (contextualHeaders) return contextualHeaders;
|
|
771
|
+
`);
|
|
772
|
+
const block = import_jscodeshift.default.blockStatement(snippet.nodes()[0].program.body);
|
|
773
|
+
return (0, import_jscodeshift.default)(code).find(import_jscodeshift.default.FunctionDeclaration, { id: { name: "headers" } }).forEach(({ node }) => {
|
|
774
|
+
const body = node.body.body;
|
|
775
|
+
const existing = (0, import_jscodeshift.default)(body).find(CommentLine, { value: ` ${marker}` }).paths()[0];
|
|
776
|
+
const idx = body.indexOf(existing?.parent.node);
|
|
777
|
+
if (existing && idx > -1) body[idx] = block;
|
|
778
|
+
else body.unshift(block);
|
|
779
|
+
}).toSource();
|
|
780
|
+
}
|
|
781
|
+
});
|
|
782
|
+
var patchCookies = definePatchStep({
|
|
783
|
+
title: "Add WebSocket contextual cookies resolution to request cookies",
|
|
784
|
+
path: "next:dist/client/components/headers.js",
|
|
785
|
+
async transform(code) {
|
|
786
|
+
const marker = "@patch cookies";
|
|
787
|
+
const snippet = (0, import_jscodeshift.default)(`
|
|
788
|
+
// ${marker}
|
|
789
|
+
const kRequestStorage = Symbol.for('next-ws.request-store');
|
|
790
|
+
const requestStorage = Reflect.get(globalThis, kRequestStorage);
|
|
791
|
+
const contextualCookies = requestStorage?.getStore()?.cookies;
|
|
792
|
+
if (contextualCookies) return contextualCookies;
|
|
793
|
+
`);
|
|
794
|
+
const block = import_jscodeshift.default.blockStatement(snippet.nodes()[0].program.body);
|
|
795
|
+
return (0, import_jscodeshift.default)(code).find(import_jscodeshift.default.FunctionDeclaration, { id: { name: "cookies" } }).forEach(({ node }) => {
|
|
796
|
+
const body = node.body.body;
|
|
797
|
+
const existing = (0, import_jscodeshift.default)(body).find(CommentLine, { value: ` ${marker}` }).paths()[0];
|
|
798
|
+
const idx = body.indexOf(existing?.parent.node);
|
|
799
|
+
if (existing && idx > -1) body[idx] = block;
|
|
800
|
+
else body.unshift(block);
|
|
801
|
+
}).toSource();
|
|
802
|
+
}
|
|
803
|
+
});
|
|
760
804
|
var patch_1_default = definePatch({
|
|
761
805
|
name: "patch-1",
|
|
762
|
-
versions: ">=13.5.1 <=
|
|
763
|
-
steps: [patchNextNodeServer, patchRouterServer]
|
|
806
|
+
versions: ">=13.5.1 <=14.2.32",
|
|
807
|
+
steps: [patchNextNodeServer, patchRouterServer, patchHeaders, patchCookies]
|
|
808
|
+
});
|
|
809
|
+
|
|
810
|
+
// src/patches/patch-2.ts
|
|
811
|
+
var patchHeaders2 = definePatchStep({
|
|
812
|
+
...patchHeaders,
|
|
813
|
+
path: "next:dist/server/request/headers.js"
|
|
814
|
+
});
|
|
815
|
+
var patchCookies2 = definePatchStep({
|
|
816
|
+
...patchCookies,
|
|
817
|
+
path: "next:dist/server/request/cookies.js"
|
|
818
|
+
});
|
|
819
|
+
var patch_2_default = definePatch({
|
|
820
|
+
name: "patch-2",
|
|
821
|
+
versions: ">=15.0.0 <=15.5.0",
|
|
822
|
+
steps: [
|
|
823
|
+
patchNextNodeServer,
|
|
824
|
+
patchRouterServer,
|
|
825
|
+
patchHeaders2,
|
|
826
|
+
patchCookies2
|
|
827
|
+
]
|
|
764
828
|
});
|
|
765
829
|
|
|
766
830
|
// src/patches/index.ts
|
|
767
|
-
var patches_default = [patch_1_default];
|
|
831
|
+
var patches_default = [patch_1_default, patch_2_default];
|
|
768
832
|
|
|
769
833
|
// src/commands/helpers/semver.ts
|
|
770
834
|
var import_range = __toESM(require("semver/classes/range.js"));
|
package/dist/server/index.cjs
CHANGED
|
@@ -65,8 +65,16 @@ var [getHttpServer, setHttpServer, useHttpServer] = (
|
|
|
65
65
|
var [getWebSocketServer, setWebSocketServer, useWebSocketServer] = useGlobal(
|
|
66
66
|
Symbol.for("next-ws.websocket-server")
|
|
67
67
|
);
|
|
68
|
+
var [getRequestStorage, setRequestStorage, useRequestStorage] = (
|
|
69
|
+
//
|
|
70
|
+
useGlobal(
|
|
71
|
+
Symbol.for("next-ws.request-store")
|
|
72
|
+
//
|
|
73
|
+
)
|
|
74
|
+
);
|
|
68
75
|
|
|
69
76
|
// src/server/setup.ts
|
|
77
|
+
var import_node_async_hooks2 = require("async_hooks");
|
|
70
78
|
var logger2 = __toESM(require("next/dist/build/output/log.js"));
|
|
71
79
|
var import_ws = require("ws");
|
|
72
80
|
|
|
@@ -130,7 +138,7 @@ async function importRouteModule(nextServer, filePath) {
|
|
|
130
138
|
}
|
|
131
139
|
try {
|
|
132
140
|
const buildPagePath = nextServer.getPagePath(filePath);
|
|
133
|
-
return require(buildPagePath).routeModule;
|
|
141
|
+
return (await require(buildPagePath)).routeModule;
|
|
134
142
|
} catch (cause) {
|
|
135
143
|
console.error(cause);
|
|
136
144
|
return void 0;
|
|
@@ -154,6 +162,36 @@ function toNextRequest(message) {
|
|
|
154
162
|
});
|
|
155
163
|
}
|
|
156
164
|
|
|
165
|
+
// src/server/helpers/store.ts
|
|
166
|
+
var import_node_async_hooks = require("async_hooks");
|
|
167
|
+
var import_cookies = require("next/dist/compiled/@edge-runtime/cookies");
|
|
168
|
+
var ReadonlyHeaders = class extends Headers {
|
|
169
|
+
append() {
|
|
170
|
+
throw new Error("Headers are read-only");
|
|
171
|
+
}
|
|
172
|
+
set() {
|
|
173
|
+
throw new Error("Headers are read-only");
|
|
174
|
+
}
|
|
175
|
+
delete() {
|
|
176
|
+
throw new Error("Headers are read-only");
|
|
177
|
+
}
|
|
178
|
+
};
|
|
179
|
+
var ReadonlyRequestsCookies = class extends import_cookies.RequestCookies {
|
|
180
|
+
set() {
|
|
181
|
+
throw new Error("Cookies are read-only");
|
|
182
|
+
}
|
|
183
|
+
delete() {
|
|
184
|
+
throw new Error("Cookies are read-only");
|
|
185
|
+
}
|
|
186
|
+
};
|
|
187
|
+
function createRequestStore(request) {
|
|
188
|
+
return {
|
|
189
|
+
headers: new ReadonlyHeaders(request.headers),
|
|
190
|
+
cookies: new ReadonlyRequestsCookies(request.headers)
|
|
191
|
+
};
|
|
192
|
+
}
|
|
193
|
+
var requestStorage = new import_node_async_hooks.AsyncLocalStorage();
|
|
194
|
+
|
|
157
195
|
// src/server/setup.ts
|
|
158
196
|
function setupWebSocketServer(nextServer) {
|
|
159
197
|
const httpServer = (
|
|
@@ -167,6 +205,10 @@ function setupWebSocketServer(nextServer) {
|
|
|
167
205
|
//
|
|
168
206
|
useWebSocketServer(() => new import_ws.WebSocketServer({ noServer: true }))
|
|
169
207
|
);
|
|
208
|
+
const requestStorage2 = (
|
|
209
|
+
//
|
|
210
|
+
useRequestStorage(() => new import_node_async_hooks2.AsyncLocalStorage())
|
|
211
|
+
);
|
|
170
212
|
logger2.ready("[next-ws] has started the WebSocket server");
|
|
171
213
|
const kInstalled = Symbol.for("next-ws.http-server.attached");
|
|
172
214
|
if (Reflect.has(httpServer, kInstalled)) return;
|
|
@@ -193,14 +235,18 @@ function setupWebSocketServer(nextServer) {
|
|
|
193
235
|
}
|
|
194
236
|
if (handleSocket)
|
|
195
237
|
logger2.warnOnce(
|
|
196
|
-
"DeprecationWarning: [next-ws] SOCKET is deprecated,
|
|
238
|
+
"DeprecationWarning: [next-ws] SOCKET is deprecated, prefer UPGRADE instead, see https://github.com/apteryxxyz/next-ws#-usage"
|
|
197
239
|
);
|
|
198
240
|
wsServer.handleUpgrade(message, socket, head, async (client) => {
|
|
199
241
|
wsServer.emit("connection", client, message);
|
|
200
242
|
try {
|
|
201
243
|
const context = { params: route.params };
|
|
202
244
|
if (handleUpgrade) {
|
|
203
|
-
await
|
|
245
|
+
await requestStorage2.run(
|
|
246
|
+
createRequestStore(request),
|
|
247
|
+
//
|
|
248
|
+
() => handleUpgrade(client, wsServer, request, context)
|
|
249
|
+
);
|
|
204
250
|
} else if (handleSocket) {
|
|
205
251
|
const handleClose = (
|
|
206
252
|
//
|