nuxt-skew-protection 0.5.0 → 0.7.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/module.d.mts +13 -0
- package/dist/module.json +6 -1
- package/dist/module.mjs +79 -12
- package/dist/runtime/adapters/ably/node.js +3 -2
- package/dist/runtime/adapters/ably/web.js +3 -2
- package/dist/runtime/adapters/pusher/node.js +3 -2
- package/dist/runtime/adapters/pusher/web.js +3 -2
- package/dist/runtime/app/components/SkewNotification.d.vue.ts +13 -7
- package/dist/runtime/app/components/SkewNotification.vue.d.ts +13 -7
- package/dist/runtime/app/composables/useActiveConnections.d.ts +39 -0
- package/dist/runtime/app/composables/useActiveConnections.js +45 -10
- package/dist/runtime/app/composables/useRuntimeConfigSkewProtection.d.ts +0 -1
- package/dist/runtime/app/composables/useRuntimeConfigSkewProtection.js +1 -5
- package/dist/runtime/app/composables/useSkewProtection.d.ts +13 -1
- package/dist/runtime/app/composables/useSkewProtection.js +43 -8
- package/dist/runtime/app/plugins/check-updates-adapter.client.d.ts +7 -1
- package/dist/runtime/app/plugins/check-updates-adapter.client.js +19 -36
- package/dist/runtime/app/plugins/check-updates-sse.client.d.ts +7 -1
- package/dist/runtime/app/plugins/check-updates-sse.client.js +30 -37
- package/dist/runtime/app/plugins/check-updates-websocket.client.d.ts +7 -1
- package/dist/runtime/app/plugins/check-updates-websocket.client.js +42 -62
- package/dist/runtime/app/types.d.ts +28 -0
- package/dist/runtime/app/types.js +0 -0
- package/dist/runtime/app/utils/create-skew-connection.d.ts +24 -0
- package/dist/runtime/app/utils/create-skew-connection.js +92 -0
- package/dist/runtime/const.d.ts +9 -0
- package/dist/runtime/const.js +8 -0
- package/dist/runtime/server/middleware/set-skew-protection-cookie.js +4 -7
- package/dist/runtime/server/plugins/connection-tracking-cloudflare-durable.d.ts +2 -0
- package/dist/runtime/server/plugins/connection-tracking-cloudflare-durable.js +78 -0
- package/dist/runtime/server/plugins/connection-tracking.js +47 -11
- package/dist/runtime/server/routes/_skew/route.post.d.ts +4 -0
- package/dist/runtime/server/routes/_skew/route.post.js +13 -0
- package/dist/runtime/server/routes/_skew/sse.js +21 -10
- package/dist/runtime/server/routes/_skew/subscribe-stats.post.d.ts +8 -0
- package/dist/runtime/server/routes/_skew/subscribe-stats.post.js +13 -0
- package/dist/runtime/server/routes/_skew/ws.js +37 -8
- package/dist/runtime/types.d.ts +4 -1
- package/package.json +24 -20
- package/dist/runtime/app/plugins/init-build-id.d.ts +0 -12
- package/dist/runtime/app/plugins/init-build-id.js +0 -29
package/dist/module.d.mts
CHANGED
|
@@ -62,6 +62,19 @@ interface ModuleOptions {
|
|
|
62
62
|
* @default false
|
|
63
63
|
*/
|
|
64
64
|
connectionTracking?: boolean;
|
|
65
|
+
/**
|
|
66
|
+
* Track which routes users are currently viewing
|
|
67
|
+
* Requires connectionTracking to be enabled
|
|
68
|
+
* @default false
|
|
69
|
+
*/
|
|
70
|
+
routeTracking?: boolean;
|
|
71
|
+
/**
|
|
72
|
+
* Track IP addresses of connected users
|
|
73
|
+
* Requires connectionTracking to be enabled
|
|
74
|
+
* Privacy note: IPs are only stored in memory and exposed via stats
|
|
75
|
+
* @default false
|
|
76
|
+
*/
|
|
77
|
+
ipTracking?: boolean;
|
|
65
78
|
}
|
|
66
79
|
interface ModulePublicRuntimeConfig {
|
|
67
80
|
skewProtection: NuxtSkewProtectionRuntimeConfig;
|
package/dist/module.json
CHANGED
|
@@ -4,7 +4,12 @@
|
|
|
4
4
|
"nuxt": ">=4.0.0"
|
|
5
5
|
},
|
|
6
6
|
"configKey": "skewProtection",
|
|
7
|
-
"
|
|
7
|
+
"moduleDependencies": {
|
|
8
|
+
"@nuxtjs/robots": {
|
|
9
|
+
"version": ">=5.6.7"
|
|
10
|
+
}
|
|
11
|
+
},
|
|
12
|
+
"version": "0.7.0",
|
|
8
13
|
"builder": {
|
|
9
14
|
"@nuxt/module-builder": "1.0.2",
|
|
10
15
|
"unbuild": "3.6.1"
|
package/dist/module.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { writeFileSync, unlinkSync, existsSync, readFileSync, promises } from 'node:fs';
|
|
2
|
-
import { useLogger, useNuxt, tryUseNuxt, defineNuxtModule, createResolver, hasNuxtModule, addTypeTemplate, addComponent, addImports,
|
|
2
|
+
import { useLogger, useNuxt, tryUseNuxt, defineNuxtModule, createResolver, hasNuxtModule, addTypeTemplate, addComponent, addImports, addServerHandler, addPlugin, tryResolveModule, addTemplate } from '@nuxt/kit';
|
|
3
3
|
import { colors } from 'consola/utils';
|
|
4
4
|
import { useSiteConfig, installNuxtSiteConfig } from 'nuxt-site-config/kit';
|
|
5
5
|
import { readPackageJSON } from 'pkg-types';
|
|
@@ -725,7 +725,12 @@ const module$1 = defineNuxtModule({
|
|
|
725
725
|
compatibility: {
|
|
726
726
|
nuxt: ">=4.0.0"
|
|
727
727
|
},
|
|
728
|
-
configKey: "skewProtection"
|
|
728
|
+
configKey: "skewProtection",
|
|
729
|
+
moduleDependencies: {
|
|
730
|
+
"@nuxtjs/robots": {
|
|
731
|
+
version: ">=5.6.7"
|
|
732
|
+
}
|
|
733
|
+
}
|
|
729
734
|
},
|
|
730
735
|
defaults: {
|
|
731
736
|
retentionDays: 30,
|
|
@@ -755,10 +760,18 @@ const module$1 = defineNuxtModule({
|
|
|
755
760
|
await installNuxtSiteConfig();
|
|
756
761
|
hookNuxtSeoProLicense();
|
|
757
762
|
nuxt.options.runtimeConfig.public = nuxt.options.runtimeConfig.public || {};
|
|
763
|
+
if (options.routeTracking && !options.connectionTracking) {
|
|
764
|
+
logger.warn("`routeTracking` requires `connectionTracking: true`. Route tracking will be disabled.");
|
|
765
|
+
}
|
|
766
|
+
if (options.ipTracking && !options.connectionTracking) {
|
|
767
|
+
logger.warn("`ipTracking` requires `connectionTracking: true`. IP tracking will be disabled.");
|
|
768
|
+
}
|
|
758
769
|
nuxt.options.runtimeConfig.public.skewProtection = {
|
|
759
770
|
cookie: options.cookie,
|
|
760
771
|
debug: options.debug,
|
|
761
772
|
connectionTracking: options.connectionTracking,
|
|
773
|
+
routeTracking: options.connectionTracking && options.routeTracking,
|
|
774
|
+
ipTracking: options.connectionTracking && options.ipTracking,
|
|
762
775
|
version
|
|
763
776
|
};
|
|
764
777
|
const nitroPreset = resolveNitroPreset(nuxt.options.nitro);
|
|
@@ -771,14 +784,19 @@ const module$1 = defineNuxtModule({
|
|
|
771
784
|
addTypeTemplate({
|
|
772
785
|
filename: "types/nuxt-skew-protection.d.ts",
|
|
773
786
|
getContents: () => `// Generated by nuxt-skew-protection
|
|
787
|
+
import type { ChunksOutdatedPayload, SkewAdapterConfig, SkewConnection, SkewSSEConfig, SkewWebSocketConfig } from '#skew-protection/app/types'
|
|
788
|
+
|
|
774
789
|
declare module '#app' {
|
|
775
790
|
interface RuntimeNuxtHooks {
|
|
776
791
|
'skew-protection:chunks-outdated': (payload: ChunksOutdatedPayload) => void | Promise<void>
|
|
777
792
|
'skew:message': (message: { type: string, [key: string]: unknown }) => void | Promise<void>
|
|
793
|
+
'skew:ws:config': (config: SkewWebSocketConfig) => void | Promise<void>
|
|
794
|
+
'skew:sse:config': (config: SkewSSEConfig) => void | Promise<void>
|
|
795
|
+
'skew:adapter:config': (config: SkewAdapterConfig) => void | Promise<void>
|
|
778
796
|
}
|
|
779
797
|
interface NuxtApp {
|
|
780
|
-
$
|
|
781
|
-
|
|
798
|
+
$skewConnection?: SkewConnection
|
|
799
|
+
_skewStatsSubscribed?: boolean
|
|
782
800
|
}
|
|
783
801
|
interface NuxtAppManifestMeta {
|
|
784
802
|
skewProtection?: {
|
|
@@ -796,10 +814,13 @@ declare module 'nuxt/app' {
|
|
|
796
814
|
interface RuntimeNuxtHooks {
|
|
797
815
|
'skew-protection:chunks-outdated': (payload: ChunksOutdatedPayload) => void | Promise<void>
|
|
798
816
|
'skew:message': (message: { type: string, [key: string]: unknown }) => void | Promise<void>
|
|
817
|
+
'skew:ws:config': (config: SkewWebSocketConfig) => void | Promise<void>
|
|
818
|
+
'skew:sse:config': (config: SkewSSEConfig) => void | Promise<void>
|
|
819
|
+
'skew:adapter:config': (config: SkewAdapterConfig) => void | Promise<void>
|
|
799
820
|
}
|
|
800
821
|
interface NuxtApp {
|
|
801
|
-
$
|
|
802
|
-
|
|
822
|
+
$skewConnection?: SkewConnection
|
|
823
|
+
_skewStatsSubscribed?: boolean
|
|
803
824
|
}
|
|
804
825
|
interface NuxtAppManifestMeta {
|
|
805
826
|
skewProtection?: {
|
|
@@ -813,20 +834,30 @@ declare module 'nuxt/app' {
|
|
|
813
834
|
}
|
|
814
835
|
}
|
|
815
836
|
|
|
816
|
-
export {}
|
|
837
|
+
export type { ChunksOutdatedPayload, SkewAdapterConfig, SkewConnection, SkewSSEConfig, SkewWebSocketConfig }
|
|
817
838
|
`
|
|
818
839
|
}, { nuxt: true });
|
|
819
840
|
addTypeTemplate({
|
|
820
841
|
filename: "types/nuxt-skew-protection-nitro.d.ts",
|
|
821
842
|
getContents: () => `// Generated by nuxt-skew-protection
|
|
843
|
+
import type { H3Event } from 'h3'
|
|
844
|
+
|
|
822
845
|
declare module 'nitropack/types' {
|
|
823
846
|
interface NitroRuntimeHooks {
|
|
824
847
|
'skew:connection:open': (payload: {
|
|
825
848
|
id: string
|
|
826
849
|
version: string
|
|
827
|
-
|
|
850
|
+
route?: string
|
|
851
|
+
ip?: string
|
|
852
|
+
send: (data: unknown) => void
|
|
828
853
|
}) => void
|
|
854
|
+
'skew:connection:route-update': (payload: { id: string, route: string }) => void
|
|
829
855
|
'skew:connection:close': (payload: { id: string }) => void
|
|
856
|
+
// event is H3Event for SSE (via POST), { headers } for WebSocket (no H3Event available)
|
|
857
|
+
'skew:subscribe-stats': (payload: { id: string, event?: H3Event | { headers?: Headers } }) => void
|
|
858
|
+
// event is H3Event for SSE (via POST), { headers } for WebSocket (no H3Event available)
|
|
859
|
+
'skew:authorize-stats': (payload: { event?: H3Event | { headers?: Headers }, authorize: () => void }) => void
|
|
860
|
+
'skew:stats': (callback: (stats: { total: number, versions: Record<string, number>, routes: Record<string, number> }) => void) => void
|
|
830
861
|
}
|
|
831
862
|
}
|
|
832
863
|
|
|
@@ -848,17 +879,39 @@ export {}
|
|
|
848
879
|
});
|
|
849
880
|
nuxt.options.nitro = nuxt.options.nitro || {};
|
|
850
881
|
nuxt.options.nitro.plugins = nuxt.options.nitro.plugins || [];
|
|
851
|
-
|
|
882
|
+
const connectionTrackingPlugin = nitroPreset === "cloudflare-durable" ? "./runtime/server/plugins/connection-tracking-cloudflare-durable" : "./runtime/server/plugins/connection-tracking";
|
|
883
|
+
nuxt.options.nitro.plugins.push(resolver.resolve(connectionTrackingPlugin));
|
|
852
884
|
}
|
|
853
|
-
|
|
854
|
-
src: resolver.resolve("./runtime/app/plugins/init-build-id")
|
|
855
|
-
});
|
|
885
|
+
nuxt.options.alias["#skew-protection"] = resolver.resolve("./runtime");
|
|
856
886
|
nuxt.options.alias["nuxt-skew-protection/server"] = resolver.resolve("./runtime/server");
|
|
857
887
|
if (options.storage?.driver) {
|
|
858
888
|
nuxt.options.nitro = nuxt.options.nitro || {};
|
|
859
889
|
nuxt.options.nitro.storage = nuxt.options.nitro.storage || {};
|
|
860
890
|
nuxt.options.nitro.storage["skew-protection"] = options.storage;
|
|
861
891
|
}
|
|
892
|
+
if (nuxt.options.dev && options.connectionTracking) {
|
|
893
|
+
if (nuxt.options.nitro?.experimental?.websocket) {
|
|
894
|
+
addServerHandler({
|
|
895
|
+
route: "/_skew/ws",
|
|
896
|
+
handler: resolver.resolve("./runtime/server/routes/_skew/ws")
|
|
897
|
+
});
|
|
898
|
+
addServerHandler({
|
|
899
|
+
route: "/_skew/subscribe-stats",
|
|
900
|
+
method: "post",
|
|
901
|
+
handler: resolver.resolve("./runtime/server/routes/_skew/subscribe-stats.post")
|
|
902
|
+
});
|
|
903
|
+
if (options.routeTracking) {
|
|
904
|
+
addServerHandler({
|
|
905
|
+
route: "/_skew/route",
|
|
906
|
+
method: "post",
|
|
907
|
+
handler: resolver.resolve("./runtime/server/routes/_skew/route.post")
|
|
908
|
+
});
|
|
909
|
+
}
|
|
910
|
+
addPlugin(resolver.resolve("./runtime/app/plugins/check-updates-websocket.client"));
|
|
911
|
+
} else {
|
|
912
|
+
logger.warn("connectionTracking in dev mode requires `nitro.experimental.websocket: true`");
|
|
913
|
+
}
|
|
914
|
+
}
|
|
862
915
|
if (!nuxt.options.dev) {
|
|
863
916
|
const isCloudflareRuntime = nitroPreset?.includes("cloudflare");
|
|
864
917
|
const isVercel = nitroPreset?.includes("vercel") || process.env.VERCEL_SKEW_PROTECTION_ENABLED === "1";
|
|
@@ -1042,6 +1095,20 @@ export { subscribe }`
|
|
|
1042
1095
|
route: "/_skew/sse",
|
|
1043
1096
|
handler: resolver.resolve("./runtime/server/routes/_skew/sse")
|
|
1044
1097
|
});
|
|
1098
|
+
if (options.connectionTracking) {
|
|
1099
|
+
addServerHandler({
|
|
1100
|
+
route: "/_skew/subscribe-stats",
|
|
1101
|
+
method: "post",
|
|
1102
|
+
handler: resolver.resolve("./runtime/server/routes/_skew/subscribe-stats.post")
|
|
1103
|
+
});
|
|
1104
|
+
if (options.routeTracking) {
|
|
1105
|
+
addServerHandler({
|
|
1106
|
+
route: "/_skew/route",
|
|
1107
|
+
method: "post",
|
|
1108
|
+
handler: resolver.resolve("./runtime/server/routes/_skew/route.post")
|
|
1109
|
+
});
|
|
1110
|
+
}
|
|
1111
|
+
}
|
|
1045
1112
|
addPlugin({
|
|
1046
1113
|
src: resolver.resolve("./runtime/app/plugins/check-updates-sse.client"),
|
|
1047
1114
|
mode: "client"
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { Rest } from "ably";
|
|
2
2
|
import { defineNodeBroadcast } from "../../../utils";
|
|
3
|
+
import { SKEW_DEFAULT_CHANNEL, SKEW_MESSAGE_TYPE } from "../../const.js";
|
|
3
4
|
export const broadcast = defineNodeBroadcast(async (config, version) => {
|
|
4
5
|
const ably = new Rest({ key: config.key });
|
|
5
|
-
const channelName = config.channel ||
|
|
6
|
-
const eventName = config.event ||
|
|
6
|
+
const channelName = config.channel || SKEW_DEFAULT_CHANNEL;
|
|
7
|
+
const eventName = config.event || SKEW_MESSAGE_TYPE.VERSION;
|
|
7
8
|
const channel = ably.channels.get(channelName);
|
|
8
9
|
await channel.publish(eventName, { version });
|
|
9
10
|
});
|
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
import { onNuxtReady } from "nuxt/app";
|
|
2
2
|
import { defineWebSubscribe } from "../../../utils";
|
|
3
|
+
import { SKEW_DEFAULT_CHANNEL, SKEW_MESSAGE_TYPE } from "../../const.js";
|
|
3
4
|
export const subscribe = defineWebSubscribe((config, onMessage) => {
|
|
4
5
|
let cleanup;
|
|
5
6
|
onNuxtReady(async () => {
|
|
6
7
|
const { Realtime } = await import("ably");
|
|
7
8
|
const ably = new Realtime({ key: config.key });
|
|
8
|
-
const channelName = config.channel ||
|
|
9
|
-
const eventName = config.event ||
|
|
9
|
+
const channelName = config.channel || SKEW_DEFAULT_CHANNEL;
|
|
10
|
+
const eventName = config.event || SKEW_MESSAGE_TYPE.VERSION;
|
|
10
11
|
const channel = ably.channels.get(channelName);
|
|
11
12
|
await channel.subscribe(eventName, (message) => {
|
|
12
13
|
const data = typeof message.data === "string" ? JSON.parse(message.data) : message.data;
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { createHash, createHmac } from "node:crypto";
|
|
2
2
|
import { defineNodeBroadcast } from "../../../utils";
|
|
3
|
+
import { SKEW_DEFAULT_CHANNEL, SKEW_MESSAGE_TYPE } from "../../const.js";
|
|
3
4
|
export const broadcast = defineNodeBroadcast(async (config, version) => {
|
|
4
|
-
const channelName = config.channel ||
|
|
5
|
-
const eventName = config.event ||
|
|
5
|
+
const channelName = config.channel || SKEW_DEFAULT_CHANNEL;
|
|
6
|
+
const eventName = config.event || SKEW_MESSAGE_TYPE.VERSION;
|
|
6
7
|
const body = JSON.stringify({
|
|
7
8
|
name: eventName,
|
|
8
9
|
channel: channelName,
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { onNuxtReady } from "nuxt/app";
|
|
2
2
|
import { defineWebSubscribe } from "../../../utils";
|
|
3
|
+
import { SKEW_DEFAULT_CHANNEL, SKEW_MESSAGE_TYPE } from "../../const.js";
|
|
3
4
|
export const subscribe = defineWebSubscribe((config, onMessage) => {
|
|
4
5
|
let cleanup;
|
|
5
6
|
onNuxtReady(async () => {
|
|
@@ -7,8 +8,8 @@ export const subscribe = defineWebSubscribe((config, onMessage) => {
|
|
|
7
8
|
const pusher = new Pusher(config.key, {
|
|
8
9
|
cluster: config.cluster
|
|
9
10
|
});
|
|
10
|
-
const channelName = config.channel ||
|
|
11
|
-
const eventName = config.event ||
|
|
11
|
+
const channelName = config.channel || SKEW_DEFAULT_CHANNEL;
|
|
12
|
+
const eventName = config.event || SKEW_MESSAGE_TYPE.VERSION;
|
|
12
13
|
const channel = pusher.subscribe(channelName);
|
|
13
14
|
channel.bind(eventName, (data) => {
|
|
14
15
|
onMessage({ version: data.version });
|
|
@@ -5,15 +5,21 @@ interface Props {
|
|
|
5
5
|
*/
|
|
6
6
|
forceOpen?: boolean;
|
|
7
7
|
}
|
|
8
|
+
declare function handleDismiss(): void;
|
|
9
|
+
declare function handleReload(): Promise<void>;
|
|
8
10
|
declare var __VLS_8: {
|
|
9
11
|
version: any;
|
|
10
|
-
isCurrentChunksOutdated:
|
|
11
|
-
dismiss:
|
|
12
|
-
reload:
|
|
13
|
-
timeAgo:
|
|
14
|
-
releaseDate:
|
|
15
|
-
payload:
|
|
16
|
-
|
|
12
|
+
isCurrentChunksOutdated: boolean;
|
|
13
|
+
dismiss: typeof handleDismiss;
|
|
14
|
+
reload: typeof handleReload;
|
|
15
|
+
timeAgo: string;
|
|
16
|
+
releaseDate: Date;
|
|
17
|
+
payload: {
|
|
18
|
+
deletedChunks: string[];
|
|
19
|
+
invalidatedModules: string[];
|
|
20
|
+
passedReleases: string[];
|
|
21
|
+
} | null;
|
|
22
|
+
isAppOutdated: boolean;
|
|
17
23
|
};
|
|
18
24
|
type __VLS_Slots = {} & {
|
|
19
25
|
default?: (props: typeof __VLS_8) => any;
|
|
@@ -5,15 +5,21 @@ interface Props {
|
|
|
5
5
|
*/
|
|
6
6
|
forceOpen?: boolean;
|
|
7
7
|
}
|
|
8
|
+
declare function handleDismiss(): void;
|
|
9
|
+
declare function handleReload(): Promise<void>;
|
|
8
10
|
declare var __VLS_8: {
|
|
9
11
|
version: any;
|
|
10
|
-
isCurrentChunksOutdated:
|
|
11
|
-
dismiss:
|
|
12
|
-
reload:
|
|
13
|
-
timeAgo:
|
|
14
|
-
releaseDate:
|
|
15
|
-
payload:
|
|
16
|
-
|
|
12
|
+
isCurrentChunksOutdated: boolean;
|
|
13
|
+
dismiss: typeof handleDismiss;
|
|
14
|
+
reload: typeof handleReload;
|
|
15
|
+
timeAgo: string;
|
|
16
|
+
releaseDate: Date;
|
|
17
|
+
payload: {
|
|
18
|
+
deletedChunks: string[];
|
|
19
|
+
invalidatedModules: string[];
|
|
20
|
+
passedReleases: string[];
|
|
21
|
+
} | null;
|
|
22
|
+
isAppOutdated: boolean;
|
|
17
23
|
};
|
|
18
24
|
type __VLS_Slots = {} & {
|
|
19
25
|
default?: (props: typeof __VLS_8) => any;
|
|
@@ -1,8 +1,47 @@
|
|
|
1
|
+
export interface ConnectionInfo {
|
|
2
|
+
id: string;
|
|
3
|
+
version: string;
|
|
4
|
+
route: string;
|
|
5
|
+
ip?: string;
|
|
6
|
+
}
|
|
1
7
|
export interface ConnectionStats {
|
|
2
8
|
total: number;
|
|
3
9
|
versions: Record<string, number>;
|
|
10
|
+
routes: Record<string, number>;
|
|
11
|
+
connections: ConnectionInfo[];
|
|
12
|
+
yourId?: string;
|
|
4
13
|
}
|
|
14
|
+
/**
|
|
15
|
+
* Subscribe to real-time connection statistics.
|
|
16
|
+
*
|
|
17
|
+
* Requires:
|
|
18
|
+
* - `connectionTracking: true` in module config
|
|
19
|
+
* - Server-side `skew:authorize-stats` hook to authorize the subscription
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* ```ts
|
|
23
|
+
* // server/plugins/stats-auth.ts
|
|
24
|
+
* export default defineNitroPlugin((nitroApp) => {
|
|
25
|
+
* nitroApp.hooks.hook('skew:authorize-stats', async ({ event, authorize }) => {
|
|
26
|
+
* const session = await getUserSession(event)
|
|
27
|
+
* if (session.user?.role === 'admin') {
|
|
28
|
+
* authorize()
|
|
29
|
+
* }
|
|
30
|
+
* })
|
|
31
|
+
* })
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
5
34
|
export declare function useActiveConnections(): {
|
|
35
|
+
/** Whether the subscription is authorized (null while pending) */
|
|
36
|
+
authorized: import("vue").ComputedRef<boolean | null>;
|
|
37
|
+
/** Total active connections */
|
|
6
38
|
total: import("vue").ComputedRef<number>;
|
|
39
|
+
/** Users per build version */
|
|
7
40
|
versions: import("vue").ComputedRef<Record<string, number>>;
|
|
41
|
+
/** Users per route (requires routeTracking: true) */
|
|
42
|
+
routes: import("vue").ComputedRef<Record<string, number>>;
|
|
43
|
+
/** Individual connections with id, version, route */
|
|
44
|
+
connections: import("vue").ComputedRef<ConnectionInfo[]>;
|
|
45
|
+
/** Your connection ID (to identify yourself in the list) */
|
|
46
|
+
yourId: import("vue").ComputedRef<string | undefined>;
|
|
8
47
|
};
|
|
@@ -1,21 +1,56 @@
|
|
|
1
|
-
import { useNuxtApp, useRuntimeConfig } from "nuxt/app";
|
|
2
|
-
import { computed
|
|
3
|
-
const stats = ref({ total: 0, versions: {} });
|
|
4
|
-
let hooked = false;
|
|
1
|
+
import { useNuxtApp, useRuntimeConfig, useState } from "nuxt/app";
|
|
2
|
+
import { computed } from "vue";
|
|
5
3
|
export function useActiveConnections() {
|
|
4
|
+
const nuxtApp = useNuxtApp();
|
|
6
5
|
const config = useRuntimeConfig().public.skewProtection;
|
|
7
6
|
if (!config?.connectionTracking) {
|
|
8
7
|
throw new Error("[nuxt-skew-protection] useActiveConnections() requires `connectionTracking: true` in your module config.");
|
|
9
8
|
}
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
9
|
+
const stats = useState("skew-stats", () => ({ total: 0, versions: {}, routes: {}, connections: [] }));
|
|
10
|
+
const authorized = useState("skew-authorized", () => null);
|
|
11
|
+
const subscribed = nuxtApp._skewStatsSubscribed;
|
|
12
|
+
if (!subscribed) {
|
|
13
|
+
nuxtApp._skewStatsSubscribed = true;
|
|
14
|
+
nuxtApp.hooks.hook("skew:message", (message) => {
|
|
15
|
+
if (message.type === "connected") {
|
|
16
|
+
nuxtApp.$skewConnection?.subscribeStats();
|
|
17
|
+
} else if (message.type === "stats") {
|
|
18
|
+
authorized.value = true;
|
|
19
|
+
stats.value = {
|
|
20
|
+
total: message.total,
|
|
21
|
+
versions: message.versions,
|
|
22
|
+
routes: message.routes || {},
|
|
23
|
+
connections: message.connections || [],
|
|
24
|
+
yourId: message.yourId
|
|
25
|
+
};
|
|
26
|
+
} else if (message.type === "stats-unauthorized") {
|
|
27
|
+
authorized.value = false;
|
|
28
|
+
}
|
|
15
29
|
});
|
|
30
|
+
const startConnection = () => {
|
|
31
|
+
nuxtApp.$skewConnection?.connect();
|
|
32
|
+
nuxtApp.$skewConnection?.subscribeStats();
|
|
33
|
+
};
|
|
34
|
+
if (nuxtApp.$skewConnection) {
|
|
35
|
+
startConnection();
|
|
36
|
+
} else {
|
|
37
|
+
nuxtApp.hook("app:suspense:resolve", startConnection);
|
|
38
|
+
}
|
|
39
|
+
} else {
|
|
40
|
+
nuxtApp.$skewConnection?.subscribeStats();
|
|
16
41
|
}
|
|
17
42
|
return {
|
|
43
|
+
/** Whether the subscription is authorized (null while pending) */
|
|
44
|
+
authorized: computed(() => authorized.value),
|
|
45
|
+
/** Total active connections */
|
|
18
46
|
total: computed(() => stats.value.total),
|
|
19
|
-
|
|
47
|
+
/** Users per build version */
|
|
48
|
+
versions: computed(() => stats.value.versions),
|
|
49
|
+
/** Users per route (requires routeTracking: true) */
|
|
50
|
+
routes: computed(() => stats.value.routes),
|
|
51
|
+
/** Individual connections with id, version, route */
|
|
52
|
+
connections: computed(() => stats.value.connections),
|
|
53
|
+
/** Your connection ID (to identify yourself in the list) */
|
|
54
|
+
yourId: computed(() => stats.value.yourId)
|
|
20
55
|
};
|
|
21
56
|
}
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import type { SkewProtectionRuntimeConfig } from '../../types.js';
|
|
2
2
|
/**
|
|
3
3
|
* Get skew protection runtime config with proper types
|
|
4
|
-
* Ensures cookie config is always defined with required properties
|
|
5
4
|
*/
|
|
6
5
|
export declare function useRuntimeConfigSkewProtection(): SkewProtectionRuntimeConfig;
|
|
@@ -1,8 +1,4 @@
|
|
|
1
1
|
import { useRuntimeConfig } from "nuxt/app";
|
|
2
2
|
export function useRuntimeConfigSkewProtection() {
|
|
3
|
-
|
|
4
|
-
return {
|
|
5
|
-
...config.public.skewProtection,
|
|
6
|
-
buildId: config.app.buildId
|
|
7
|
-
};
|
|
3
|
+
return useRuntimeConfig().public.skewProtection;
|
|
8
4
|
}
|
|
@@ -1,11 +1,23 @@
|
|
|
1
1
|
import type { NuxtAppManifestMeta } from 'nuxt/app';
|
|
2
2
|
import type { ChunksOutdatedPayload } from '../../types.js';
|
|
3
|
+
export interface UseSkewProtectionOptions {
|
|
4
|
+
/**
|
|
5
|
+
* Lazy connection mode - don't auto-connect on mount
|
|
6
|
+
* When false (default), connections are established automatically on mount
|
|
7
|
+
* When true, users must call connect() manually
|
|
8
|
+
* @default false
|
|
9
|
+
*/
|
|
10
|
+
lazy?: boolean;
|
|
11
|
+
}
|
|
3
12
|
export declare function checkForUpdates(): Promise<void>;
|
|
4
|
-
export declare function useSkewProtection(): {
|
|
13
|
+
export declare function useSkewProtection(options?: UseSkewProtectionOptions): {
|
|
5
14
|
manifest: import("vue").Ref<NuxtAppManifestMeta | undefined, NuxtAppManifestMeta | undefined>;
|
|
6
15
|
clientVersion: any;
|
|
16
|
+
isConnected: import("vue").ComputedRef<boolean>;
|
|
7
17
|
isOnline: Readonly<import("vue").ShallowRef<boolean>>;
|
|
8
18
|
isOutdated: import("vue").ComputedRef<boolean | undefined>;
|
|
19
|
+
connect: () => void;
|
|
20
|
+
disconnect: () => void;
|
|
9
21
|
onCurrentChunksOutdated: (callback: (payload: ChunksOutdatedPayload) => void | Promise<void>) => () => void;
|
|
10
22
|
onAppOutdated: (callback: (manifest?: NuxtAppManifestMeta) => void | Promise<void>) => () => void;
|
|
11
23
|
checkForUpdates: typeof checkForUpdates;
|
|
@@ -1,14 +1,16 @@
|
|
|
1
1
|
import { buildAssetsURL } from "#internal/nuxt/paths";
|
|
2
2
|
import { useOnline } from "@vueuse/core";
|
|
3
|
-
import { useNuxtApp } from "nuxt/app";
|
|
4
|
-
import { computed, onUnmounted, ref } from "vue";
|
|
5
|
-
import {
|
|
3
|
+
import { useNuxtApp, useRuntimeConfig } from "nuxt/app";
|
|
4
|
+
import { computed, onMounted, onUnmounted, ref } from "vue";
|
|
5
|
+
import { SKEW_MESSAGE_TYPE } from "../../const.js";
|
|
6
|
+
import { logger } from "../../shared/logger.js";
|
|
7
|
+
import { createBackoffQueue } from "../utils/backoff-queue.js";
|
|
8
|
+
const isConnected = ref(false);
|
|
6
9
|
export async function checkForUpdates() {
|
|
7
10
|
if (import.meta.client && !useOnline().value)
|
|
8
11
|
return;
|
|
9
12
|
const nuxtApp = useNuxtApp();
|
|
10
|
-
const
|
|
11
|
-
const clientVersion = nuxtApp.$skewProtectionBuildId?.value || runtimeConfig.buildId;
|
|
13
|
+
const clientVersion = nuxtApp.$skewConnection?.buildId || useRuntimeConfig().app.buildId;
|
|
12
14
|
const meta = await $fetch(`${buildAssetsURL("builds/latest.json")}?${Date.now()}`).catch(() => {
|
|
13
15
|
return null;
|
|
14
16
|
});
|
|
@@ -16,11 +18,41 @@ export async function checkForUpdates() {
|
|
|
16
18
|
await nuxtApp.hooks.callHook("app:manifest:update", meta);
|
|
17
19
|
}
|
|
18
20
|
}
|
|
19
|
-
export function useSkewProtection() {
|
|
21
|
+
export function useSkewProtection(options = {}) {
|
|
22
|
+
const { lazy = false } = options;
|
|
20
23
|
const nuxtApp = useNuxtApp();
|
|
21
|
-
const
|
|
22
|
-
const clientVersion = nuxtApp.$skewProtectionBuildId?.value || nuxtApp.$skewProtectionCookie?.value || runtimeConfig.buildId;
|
|
24
|
+
const clientVersion = nuxtApp.$skewConnection?.buildId || nuxtApp.$skewConnection?.cookie?.value || useRuntimeConfig().app.buildId;
|
|
23
25
|
const manifest = ref();
|
|
26
|
+
const queue = createBackoffQueue({
|
|
27
|
+
delays: [0, 5e3, 3e4, 3e5],
|
|
28
|
+
onTick: () => nuxtApp.runWithContext(checkForUpdates)
|
|
29
|
+
});
|
|
30
|
+
if (!lazy) {
|
|
31
|
+
onMounted(() => {
|
|
32
|
+
nuxtApp.$skewConnection?.connect();
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
nuxtApp.hooks.hook("skew:message", (msg) => {
|
|
36
|
+
if (msg.type !== SKEW_MESSAGE_TYPE.VERSION && msg.type !== SKEW_MESSAGE_TYPE.CONNECTED)
|
|
37
|
+
return;
|
|
38
|
+
if (!msg.version || msg.version === clientVersion)
|
|
39
|
+
return;
|
|
40
|
+
logger.debug(`[SkewProtection] Version mismatch (${msg.version} !== ${clientVersion}), starting backoff checks`);
|
|
41
|
+
queue.start();
|
|
42
|
+
});
|
|
43
|
+
function connect() {
|
|
44
|
+
if (!import.meta.client || isConnected.value)
|
|
45
|
+
return;
|
|
46
|
+
isConnected.value = true;
|
|
47
|
+
nuxtApp.$skewConnection?.connect();
|
|
48
|
+
}
|
|
49
|
+
function disconnect() {
|
|
50
|
+
if (!import.meta.client || !isConnected.value)
|
|
51
|
+
return;
|
|
52
|
+
isConnected.value = false;
|
|
53
|
+
queue.clear();
|
|
54
|
+
nuxtApp.$skewConnection?.disconnect();
|
|
55
|
+
}
|
|
24
56
|
function onCurrentChunksOutdated(callback) {
|
|
25
57
|
const hook = nuxtApp.hooks.hook("skew-protection:chunks-outdated", callback);
|
|
26
58
|
onUnmounted(() => {
|
|
@@ -45,8 +77,11 @@ export function useSkewProtection() {
|
|
|
45
77
|
return {
|
|
46
78
|
manifest,
|
|
47
79
|
clientVersion,
|
|
80
|
+
isConnected: computed(() => isConnected.value),
|
|
48
81
|
isOnline: useOnline(),
|
|
49
82
|
isOutdated: computed(() => manifest.value && clientVersion !== manifest.value.id),
|
|
83
|
+
connect,
|
|
84
|
+
disconnect,
|
|
50
85
|
onCurrentChunksOutdated,
|
|
51
86
|
onAppOutdated,
|
|
52
87
|
checkForUpdates,
|
|
@@ -1,2 +1,8 @@
|
|
|
1
|
-
|
|
1
|
+
import type { SkewAdapterConfig } from '../types.js';
|
|
2
|
+
export type { SkewAdapterConfig };
|
|
3
|
+
declare const _default: import("nuxt/app").Plugin<{
|
|
4
|
+
skewConnection: import("../utils/create-skew-connection.js").SkewConnection;
|
|
5
|
+
}> & import("nuxt/app").ObjectPlugin<{
|
|
6
|
+
skewConnection: import("../utils/create-skew-connection.js").SkewConnection;
|
|
7
|
+
}>;
|
|
2
8
|
export default _default;
|
|
@@ -1,42 +1,25 @@
|
|
|
1
|
-
import { config, subscribe } from "#skew-adapter";
|
|
2
|
-
import { defineNuxtPlugin, useRuntimeConfig } from "nuxt/app";
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import { createBackoffQueue } from "../utils/backoff-queue.js";
|
|
1
|
+
import { config as adapterConfig, subscribe } from "#skew-adapter";
|
|
2
|
+
import { defineNuxtPlugin, useNuxtApp, useRuntimeConfig } from "nuxt/app";
|
|
3
|
+
import { SKEW_MESSAGE_TYPE } from "../../const.js";
|
|
4
|
+
import { createSkewConnection } from "../utils/create-skew-connection.js";
|
|
6
5
|
export default defineNuxtPlugin({
|
|
7
6
|
name: "skew-protection:adapter-updates",
|
|
8
|
-
setup(
|
|
9
|
-
const
|
|
7
|
+
async setup() {
|
|
8
|
+
const nuxtApp = useNuxtApp();
|
|
10
9
|
const adapterName = useRuntimeConfig().public.skewProtection.adapterName;
|
|
11
|
-
const
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
10
|
+
const config = {
|
|
11
|
+
channel: adapterConfig.channel || "skew-protection",
|
|
12
|
+
adapterConfig
|
|
13
|
+
};
|
|
14
|
+
await nuxtApp.callHook("skew:adapter:config", config);
|
|
15
|
+
const skewConnection = createSkewConnection({
|
|
16
|
+
name: `Adapter:${adapterName}`,
|
|
17
|
+
setup(onMessage) {
|
|
18
|
+
return subscribe(config.adapterConfig, (msg) => {
|
|
19
|
+
onMessage({ type: SKEW_MESSAGE_TYPE.VERSION, channel: config.channel, ...msg });
|
|
20
|
+
});
|
|
21
|
+
}
|
|
17
22
|
});
|
|
18
|
-
|
|
19
|
-
cleanup = subscribe(config, (msg) => {
|
|
20
|
-
if (!msg.version) {
|
|
21
|
-
logger.debug(`[${adapterName}] Received message without version, ignoring`);
|
|
22
|
-
return;
|
|
23
|
-
}
|
|
24
|
-
logger.debug(`[${adapterName}] Received version: ${msg.version}`);
|
|
25
|
-
if (msg.version !== clientVersion) {
|
|
26
|
-
logger.debug(`[${adapterName}] Version mismatch (${msg.version} !== ${clientVersion}), starting backoff checks`);
|
|
27
|
-
queue.start();
|
|
28
|
-
}
|
|
29
|
-
});
|
|
30
|
-
});
|
|
31
|
-
nuxtApp.hook("app:error", () => {
|
|
32
|
-
queue.clear();
|
|
33
|
-
cleanup?.();
|
|
34
|
-
});
|
|
35
|
-
if (import.meta.client) {
|
|
36
|
-
window.addEventListener("beforeunload", () => {
|
|
37
|
-
queue.clear();
|
|
38
|
-
cleanup?.();
|
|
39
|
-
});
|
|
40
|
-
}
|
|
23
|
+
return { provide: { skewConnection } };
|
|
41
24
|
}
|
|
42
25
|
});
|
|
@@ -1,2 +1,8 @@
|
|
|
1
|
-
|
|
1
|
+
import type { SkewSSEConfig } from '../types.js';
|
|
2
|
+
export type { SkewSSEConfig };
|
|
3
|
+
declare const _default: import("nuxt/app").Plugin<{
|
|
4
|
+
skewConnection: import("../utils/create-skew-connection.js").SkewConnection;
|
|
5
|
+
}> & import("nuxt/app").ObjectPlugin<{
|
|
6
|
+
skewConnection: import("../utils/create-skew-connection.js").SkewConnection;
|
|
7
|
+
}>;
|
|
2
8
|
export default _default;
|