mokup 0.1.0 → 0.2.2
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-bin.cjs +110 -0
- package/dist/cli-bin.d.cts +5 -0
- package/dist/cli-bin.d.mts +5 -0
- package/dist/cli-bin.d.ts +5 -0
- package/dist/cli-bin.mjs +108 -0
- package/dist/cli.cjs +16 -0
- package/dist/cli.d.cts +1 -0
- package/dist/cli.d.mts +1 -0
- package/dist/cli.d.ts +1 -0
- package/dist/cli.mjs +1 -0
- package/dist/index.d.cts +12 -1
- package/dist/index.d.mts +12 -1
- package/dist/index.d.ts +12 -1
- package/dist/runtime.cjs +16 -0
- package/dist/runtime.d.cts +1 -0
- package/dist/runtime.d.mts +1 -0
- package/dist/runtime.d.ts +1 -0
- package/dist/runtime.mjs +1 -0
- package/dist/server/worker.cjs +16 -0
- package/dist/server/worker.d.cts +1 -0
- package/dist/server/worker.d.mts +1 -0
- package/dist/server/worker.d.ts +1 -0
- package/dist/server/worker.mjs +1 -0
- package/dist/server.cjs +16 -0
- package/dist/server.d.cts +1 -0
- package/dist/server.d.mts +1 -0
- package/dist/server.d.ts +1 -0
- package/dist/server.mjs +1 -0
- package/dist/sw.cjs +90 -0
- package/dist/sw.d.cts +14 -0
- package/dist/sw.d.mts +14 -0
- package/dist/sw.d.ts +14 -0
- package/dist/sw.mjs +87 -0
- package/dist/vite.cjs +702 -21
- package/dist/vite.d.cts +1 -1
- package/dist/vite.d.mts +1 -1
- package/dist/vite.d.ts +1 -1
- package/dist/vite.mjs +702 -21
- package/package.json +32 -2
package/dist/cli-bin.cjs
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
const node_process = require('node:process');
|
|
5
|
+
const cli = require('@mokup/cli');
|
|
6
|
+
|
|
7
|
+
function parseBuildOptions(argv2) {
|
|
8
|
+
const dirs = [];
|
|
9
|
+
const includes = [];
|
|
10
|
+
const excludes = [];
|
|
11
|
+
let outDir;
|
|
12
|
+
let prefix;
|
|
13
|
+
let handlers = true;
|
|
14
|
+
for (let i = 0; i < argv2.length; i += 1) {
|
|
15
|
+
const arg = argv2[i];
|
|
16
|
+
if (arg === "--dir" || arg === "-d") {
|
|
17
|
+
const value = argv2[i + 1];
|
|
18
|
+
if (value) {
|
|
19
|
+
dirs.push(value);
|
|
20
|
+
i += 1;
|
|
21
|
+
}
|
|
22
|
+
continue;
|
|
23
|
+
}
|
|
24
|
+
if (arg === "--out" || arg === "-o") {
|
|
25
|
+
const value = argv2[i + 1];
|
|
26
|
+
if (value) {
|
|
27
|
+
outDir = value;
|
|
28
|
+
i += 1;
|
|
29
|
+
}
|
|
30
|
+
continue;
|
|
31
|
+
}
|
|
32
|
+
if (arg === "--prefix") {
|
|
33
|
+
const value = argv2[i + 1];
|
|
34
|
+
if (value) {
|
|
35
|
+
prefix = value;
|
|
36
|
+
i += 1;
|
|
37
|
+
}
|
|
38
|
+
continue;
|
|
39
|
+
}
|
|
40
|
+
if (arg === "--include") {
|
|
41
|
+
const value = argv2[i + 1];
|
|
42
|
+
if (value) {
|
|
43
|
+
includes.push(new RegExp(value));
|
|
44
|
+
i += 1;
|
|
45
|
+
}
|
|
46
|
+
continue;
|
|
47
|
+
}
|
|
48
|
+
if (arg === "--exclude") {
|
|
49
|
+
const value = argv2[i + 1];
|
|
50
|
+
if (value) {
|
|
51
|
+
excludes.push(new RegExp(value));
|
|
52
|
+
i += 1;
|
|
53
|
+
}
|
|
54
|
+
continue;
|
|
55
|
+
}
|
|
56
|
+
if (arg === "--no-handlers") {
|
|
57
|
+
handlers = false;
|
|
58
|
+
continue;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
const options2 = {
|
|
62
|
+
handlers,
|
|
63
|
+
log: (message) => console.log(message)
|
|
64
|
+
};
|
|
65
|
+
if (dirs.length > 0) {
|
|
66
|
+
options2.dir = dirs;
|
|
67
|
+
}
|
|
68
|
+
if (outDir) {
|
|
69
|
+
options2.outDir = outDir;
|
|
70
|
+
}
|
|
71
|
+
if (prefix) {
|
|
72
|
+
options2.prefix = prefix;
|
|
73
|
+
}
|
|
74
|
+
if (includes.length > 0) {
|
|
75
|
+
options2.include = includes;
|
|
76
|
+
}
|
|
77
|
+
if (excludes.length > 0) {
|
|
78
|
+
options2.exclude = excludes;
|
|
79
|
+
}
|
|
80
|
+
return options2;
|
|
81
|
+
}
|
|
82
|
+
function printHelp() {
|
|
83
|
+
console.log(
|
|
84
|
+
`mokup build [options]
|
|
85
|
+
|
|
86
|
+
Options:
|
|
87
|
+
--dir, -d Mock directory (repeatable)
|
|
88
|
+
--out, -o Output directory (default: .mokup)
|
|
89
|
+
--prefix URL prefix
|
|
90
|
+
--include Include regex (repeatable)
|
|
91
|
+
--exclude Exclude regex (repeatable)
|
|
92
|
+
--no-handlers Skip function handler output`
|
|
93
|
+
);
|
|
94
|
+
}
|
|
95
|
+
const args = node_process.argv.slice(2);
|
|
96
|
+
const command = args[0];
|
|
97
|
+
if (!command || command === "help" || command === "--help" || command === "-h") {
|
|
98
|
+
printHelp();
|
|
99
|
+
node_process.exit(0);
|
|
100
|
+
}
|
|
101
|
+
if (command !== "build") {
|
|
102
|
+
console.error(`Unknown command: ${command}`);
|
|
103
|
+
printHelp();
|
|
104
|
+
node_process.exit(1);
|
|
105
|
+
}
|
|
106
|
+
const options = parseBuildOptions(args.slice(1));
|
|
107
|
+
cli.buildManifest(options).catch((error) => {
|
|
108
|
+
console.error(error instanceof Error ? error.message : String(error));
|
|
109
|
+
node_process.exit(1);
|
|
110
|
+
});
|
package/dist/cli-bin.mjs
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { argv, exit } from 'node:process';
|
|
3
|
+
import { buildManifest } from '@mokup/cli';
|
|
4
|
+
|
|
5
|
+
function parseBuildOptions(argv2) {
|
|
6
|
+
const dirs = [];
|
|
7
|
+
const includes = [];
|
|
8
|
+
const excludes = [];
|
|
9
|
+
let outDir;
|
|
10
|
+
let prefix;
|
|
11
|
+
let handlers = true;
|
|
12
|
+
for (let i = 0; i < argv2.length; i += 1) {
|
|
13
|
+
const arg = argv2[i];
|
|
14
|
+
if (arg === "--dir" || arg === "-d") {
|
|
15
|
+
const value = argv2[i + 1];
|
|
16
|
+
if (value) {
|
|
17
|
+
dirs.push(value);
|
|
18
|
+
i += 1;
|
|
19
|
+
}
|
|
20
|
+
continue;
|
|
21
|
+
}
|
|
22
|
+
if (arg === "--out" || arg === "-o") {
|
|
23
|
+
const value = argv2[i + 1];
|
|
24
|
+
if (value) {
|
|
25
|
+
outDir = value;
|
|
26
|
+
i += 1;
|
|
27
|
+
}
|
|
28
|
+
continue;
|
|
29
|
+
}
|
|
30
|
+
if (arg === "--prefix") {
|
|
31
|
+
const value = argv2[i + 1];
|
|
32
|
+
if (value) {
|
|
33
|
+
prefix = value;
|
|
34
|
+
i += 1;
|
|
35
|
+
}
|
|
36
|
+
continue;
|
|
37
|
+
}
|
|
38
|
+
if (arg === "--include") {
|
|
39
|
+
const value = argv2[i + 1];
|
|
40
|
+
if (value) {
|
|
41
|
+
includes.push(new RegExp(value));
|
|
42
|
+
i += 1;
|
|
43
|
+
}
|
|
44
|
+
continue;
|
|
45
|
+
}
|
|
46
|
+
if (arg === "--exclude") {
|
|
47
|
+
const value = argv2[i + 1];
|
|
48
|
+
if (value) {
|
|
49
|
+
excludes.push(new RegExp(value));
|
|
50
|
+
i += 1;
|
|
51
|
+
}
|
|
52
|
+
continue;
|
|
53
|
+
}
|
|
54
|
+
if (arg === "--no-handlers") {
|
|
55
|
+
handlers = false;
|
|
56
|
+
continue;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
const options2 = {
|
|
60
|
+
handlers,
|
|
61
|
+
log: (message) => console.log(message)
|
|
62
|
+
};
|
|
63
|
+
if (dirs.length > 0) {
|
|
64
|
+
options2.dir = dirs;
|
|
65
|
+
}
|
|
66
|
+
if (outDir) {
|
|
67
|
+
options2.outDir = outDir;
|
|
68
|
+
}
|
|
69
|
+
if (prefix) {
|
|
70
|
+
options2.prefix = prefix;
|
|
71
|
+
}
|
|
72
|
+
if (includes.length > 0) {
|
|
73
|
+
options2.include = includes;
|
|
74
|
+
}
|
|
75
|
+
if (excludes.length > 0) {
|
|
76
|
+
options2.exclude = excludes;
|
|
77
|
+
}
|
|
78
|
+
return options2;
|
|
79
|
+
}
|
|
80
|
+
function printHelp() {
|
|
81
|
+
console.log(
|
|
82
|
+
`mokup build [options]
|
|
83
|
+
|
|
84
|
+
Options:
|
|
85
|
+
--dir, -d Mock directory (repeatable)
|
|
86
|
+
--out, -o Output directory (default: .mokup)
|
|
87
|
+
--prefix URL prefix
|
|
88
|
+
--include Include regex (repeatable)
|
|
89
|
+
--exclude Exclude regex (repeatable)
|
|
90
|
+
--no-handlers Skip function handler output`
|
|
91
|
+
);
|
|
92
|
+
}
|
|
93
|
+
const args = argv.slice(2);
|
|
94
|
+
const command = args[0];
|
|
95
|
+
if (!command || command === "help" || command === "--help" || command === "-h") {
|
|
96
|
+
printHelp();
|
|
97
|
+
exit(0);
|
|
98
|
+
}
|
|
99
|
+
if (command !== "build") {
|
|
100
|
+
console.error(`Unknown command: ${command}`);
|
|
101
|
+
printHelp();
|
|
102
|
+
exit(1);
|
|
103
|
+
}
|
|
104
|
+
const options = parseBuildOptions(args.slice(1));
|
|
105
|
+
buildManifest(options).catch((error) => {
|
|
106
|
+
console.error(error instanceof Error ? error.message : String(error));
|
|
107
|
+
exit(1);
|
|
108
|
+
});
|
package/dist/cli.cjs
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const cli = require('@mokup/cli');
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
Object.prototype.hasOwnProperty.call(cli, '__proto__') &&
|
|
8
|
+
!Object.prototype.hasOwnProperty.call(exports, '__proto__') &&
|
|
9
|
+
Object.defineProperty(exports, '__proto__', {
|
|
10
|
+
enumerable: true,
|
|
11
|
+
value: cli['__proto__']
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
Object.keys(cli).forEach(function (k) {
|
|
15
|
+
if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) exports[k] = cli[k];
|
|
16
|
+
});
|
package/dist/cli.d.cts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from '@mokup/cli';
|
package/dist/cli.d.mts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from '@mokup/cli';
|
package/dist/cli.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from '@mokup/cli';
|
package/dist/cli.mjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from '@mokup/cli';
|
package/dist/index.d.cts
CHANGED
|
@@ -11,6 +11,15 @@ interface MockRule {
|
|
|
11
11
|
headers?: Record<string, string>;
|
|
12
12
|
delay?: number;
|
|
13
13
|
}
|
|
14
|
+
type MokupMockMode = 'server' | 'sw';
|
|
15
|
+
interface MokupSwOptions {
|
|
16
|
+
path?: string;
|
|
17
|
+
scope?: string;
|
|
18
|
+
register?: boolean;
|
|
19
|
+
unregister?: boolean;
|
|
20
|
+
fallback?: boolean;
|
|
21
|
+
basePath?: string | string[];
|
|
22
|
+
}
|
|
14
23
|
interface DirectoryConfig {
|
|
15
24
|
headers?: Record<string, string>;
|
|
16
25
|
status?: number;
|
|
@@ -25,6 +34,8 @@ interface MokupViteOptions {
|
|
|
25
34
|
exclude?: RegExp | RegExp[];
|
|
26
35
|
watch?: boolean;
|
|
27
36
|
log?: boolean;
|
|
37
|
+
mode?: MokupMockMode;
|
|
38
|
+
sw?: MokupSwOptions;
|
|
28
39
|
playground?: boolean | {
|
|
29
40
|
path?: string;
|
|
30
41
|
enabled?: boolean;
|
|
@@ -32,4 +43,4 @@ interface MokupViteOptions {
|
|
|
32
43
|
}
|
|
33
44
|
type MokupViteOptionsInput = MokupViteOptions | MokupViteOptions[];
|
|
34
45
|
|
|
35
|
-
export type { DirectoryConfig, HttpMethod, MockContext, MockMiddleware, MockResponse, MockResponseHandler, MockRule, MokupViteOptions, MokupViteOptionsInput };
|
|
46
|
+
export type { DirectoryConfig, HttpMethod, MockContext, MockMiddleware, MockResponse, MockResponseHandler, MockRule, MokupMockMode, MokupSwOptions, MokupViteOptions, MokupViteOptionsInput };
|
package/dist/index.d.mts
CHANGED
|
@@ -11,6 +11,15 @@ interface MockRule {
|
|
|
11
11
|
headers?: Record<string, string>;
|
|
12
12
|
delay?: number;
|
|
13
13
|
}
|
|
14
|
+
type MokupMockMode = 'server' | 'sw';
|
|
15
|
+
interface MokupSwOptions {
|
|
16
|
+
path?: string;
|
|
17
|
+
scope?: string;
|
|
18
|
+
register?: boolean;
|
|
19
|
+
unregister?: boolean;
|
|
20
|
+
fallback?: boolean;
|
|
21
|
+
basePath?: string | string[];
|
|
22
|
+
}
|
|
14
23
|
interface DirectoryConfig {
|
|
15
24
|
headers?: Record<string, string>;
|
|
16
25
|
status?: number;
|
|
@@ -25,6 +34,8 @@ interface MokupViteOptions {
|
|
|
25
34
|
exclude?: RegExp | RegExp[];
|
|
26
35
|
watch?: boolean;
|
|
27
36
|
log?: boolean;
|
|
37
|
+
mode?: MokupMockMode;
|
|
38
|
+
sw?: MokupSwOptions;
|
|
28
39
|
playground?: boolean | {
|
|
29
40
|
path?: string;
|
|
30
41
|
enabled?: boolean;
|
|
@@ -32,4 +43,4 @@ interface MokupViteOptions {
|
|
|
32
43
|
}
|
|
33
44
|
type MokupViteOptionsInput = MokupViteOptions | MokupViteOptions[];
|
|
34
45
|
|
|
35
|
-
export type { DirectoryConfig, HttpMethod, MockContext, MockMiddleware, MockResponse, MockResponseHandler, MockRule, MokupViteOptions, MokupViteOptionsInput };
|
|
46
|
+
export type { DirectoryConfig, HttpMethod, MockContext, MockMiddleware, MockResponse, MockResponseHandler, MockRule, MokupMockMode, MokupSwOptions, MokupViteOptions, MokupViteOptionsInput };
|
package/dist/index.d.ts
CHANGED
|
@@ -11,6 +11,15 @@ interface MockRule {
|
|
|
11
11
|
headers?: Record<string, string>;
|
|
12
12
|
delay?: number;
|
|
13
13
|
}
|
|
14
|
+
type MokupMockMode = 'server' | 'sw';
|
|
15
|
+
interface MokupSwOptions {
|
|
16
|
+
path?: string;
|
|
17
|
+
scope?: string;
|
|
18
|
+
register?: boolean;
|
|
19
|
+
unregister?: boolean;
|
|
20
|
+
fallback?: boolean;
|
|
21
|
+
basePath?: string | string[];
|
|
22
|
+
}
|
|
14
23
|
interface DirectoryConfig {
|
|
15
24
|
headers?: Record<string, string>;
|
|
16
25
|
status?: number;
|
|
@@ -25,6 +34,8 @@ interface MokupViteOptions {
|
|
|
25
34
|
exclude?: RegExp | RegExp[];
|
|
26
35
|
watch?: boolean;
|
|
27
36
|
log?: boolean;
|
|
37
|
+
mode?: MokupMockMode;
|
|
38
|
+
sw?: MokupSwOptions;
|
|
28
39
|
playground?: boolean | {
|
|
29
40
|
path?: string;
|
|
30
41
|
enabled?: boolean;
|
|
@@ -32,4 +43,4 @@ interface MokupViteOptions {
|
|
|
32
43
|
}
|
|
33
44
|
type MokupViteOptionsInput = MokupViteOptions | MokupViteOptions[];
|
|
34
45
|
|
|
35
|
-
export type { DirectoryConfig, HttpMethod, MockContext, MockMiddleware, MockResponse, MockResponseHandler, MockRule, MokupViteOptions, MokupViteOptionsInput };
|
|
46
|
+
export type { DirectoryConfig, HttpMethod, MockContext, MockMiddleware, MockResponse, MockResponseHandler, MockRule, MokupMockMode, MokupSwOptions, MokupViteOptions, MokupViteOptionsInput };
|
package/dist/runtime.cjs
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const runtime = require('@mokup/runtime');
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
Object.prototype.hasOwnProperty.call(runtime, '__proto__') &&
|
|
8
|
+
!Object.prototype.hasOwnProperty.call(exports, '__proto__') &&
|
|
9
|
+
Object.defineProperty(exports, '__proto__', {
|
|
10
|
+
enumerable: true,
|
|
11
|
+
value: runtime['__proto__']
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
Object.keys(runtime).forEach(function (k) {
|
|
15
|
+
if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) exports[k] = runtime[k];
|
|
16
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from '@mokup/runtime';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from '@mokup/runtime';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from '@mokup/runtime';
|
package/dist/runtime.mjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from '@mokup/runtime';
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const worker = require('@mokup/server/worker');
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
Object.prototype.hasOwnProperty.call(worker, '__proto__') &&
|
|
8
|
+
!Object.prototype.hasOwnProperty.call(exports, '__proto__') &&
|
|
9
|
+
Object.defineProperty(exports, '__proto__', {
|
|
10
|
+
enumerable: true,
|
|
11
|
+
value: worker['__proto__']
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
Object.keys(worker).forEach(function (k) {
|
|
15
|
+
if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) exports[k] = worker[k];
|
|
16
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from '@mokup/server/worker';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from '@mokup/server/worker';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from '@mokup/server/worker';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from '@mokup/server/worker';
|
package/dist/server.cjs
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const server = require('@mokup/server');
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
Object.prototype.hasOwnProperty.call(server, '__proto__') &&
|
|
8
|
+
!Object.prototype.hasOwnProperty.call(exports, '__proto__') &&
|
|
9
|
+
Object.defineProperty(exports, '__proto__', {
|
|
10
|
+
enumerable: true,
|
|
11
|
+
value: server['__proto__']
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
Object.keys(server).forEach(function (k) {
|
|
15
|
+
if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) exports[k] = server[k];
|
|
16
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from '@mokup/server';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from '@mokup/server';
|
package/dist/server.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from '@mokup/server';
|
package/dist/server.mjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from '@mokup/server';
|
package/dist/sw.cjs
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const defaultSwPath = "/mokup-sw.js";
|
|
4
|
+
const defaultSwScope = "/";
|
|
5
|
+
function normalizeSwPath(path) {
|
|
6
|
+
if (!path) {
|
|
7
|
+
return defaultSwPath;
|
|
8
|
+
}
|
|
9
|
+
return path.startsWith("/") ? path : `/${path}`;
|
|
10
|
+
}
|
|
11
|
+
function normalizeSwScope(scope) {
|
|
12
|
+
if (!scope) {
|
|
13
|
+
return defaultSwScope;
|
|
14
|
+
}
|
|
15
|
+
return scope.startsWith("/") ? scope : `/${scope}`;
|
|
16
|
+
}
|
|
17
|
+
function resolveScopeUrl(scope, origin) {
|
|
18
|
+
const url = new URL(scope, origin);
|
|
19
|
+
if (!url.pathname.endsWith("/")) {
|
|
20
|
+
url.pathname = `${url.pathname}/`;
|
|
21
|
+
}
|
|
22
|
+
return url.href;
|
|
23
|
+
}
|
|
24
|
+
function matchesScriptPath(scriptUrl, origin, pathname) {
|
|
25
|
+
try {
|
|
26
|
+
const parsed = new URL(scriptUrl);
|
|
27
|
+
return parsed.origin === origin && parsed.pathname === pathname;
|
|
28
|
+
} catch {
|
|
29
|
+
return false;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
async function registerMokupServiceWorker(options = {}) {
|
|
33
|
+
if (options.enabled === false) {
|
|
34
|
+
return null;
|
|
35
|
+
}
|
|
36
|
+
if (typeof window === "undefined" || !("serviceWorker" in navigator)) {
|
|
37
|
+
return null;
|
|
38
|
+
}
|
|
39
|
+
const path = normalizeSwPath(options.path);
|
|
40
|
+
const scope = normalizeSwScope(options.scope);
|
|
41
|
+
try {
|
|
42
|
+
return await navigator.serviceWorker.register(path, {
|
|
43
|
+
type: "module",
|
|
44
|
+
scope
|
|
45
|
+
});
|
|
46
|
+
} catch (error) {
|
|
47
|
+
console.warn("[mokup] Failed to register service worker:", error);
|
|
48
|
+
return null;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
async function unregisterMokupServiceWorker(options = {}) {
|
|
52
|
+
if (typeof window === "undefined" || !("serviceWorker" in navigator)) {
|
|
53
|
+
return [];
|
|
54
|
+
}
|
|
55
|
+
const origin = window.location.origin;
|
|
56
|
+
const path = normalizeSwPath(options.path);
|
|
57
|
+
const scope = normalizeSwScope(options.scope);
|
|
58
|
+
const pathUrl = new URL(path, origin);
|
|
59
|
+
const scopeUrl = resolveScopeUrl(scope, origin);
|
|
60
|
+
try {
|
|
61
|
+
const registrations = await navigator.serviceWorker.getRegistrations();
|
|
62
|
+
const matched = registrations.filter((registration) => {
|
|
63
|
+
if (registration.scope !== scopeUrl) {
|
|
64
|
+
return false;
|
|
65
|
+
}
|
|
66
|
+
const scriptUrls = [
|
|
67
|
+
registration.active?.scriptURL,
|
|
68
|
+
registration.waiting?.scriptURL,
|
|
69
|
+
registration.installing?.scriptURL
|
|
70
|
+
].filter((entry) => typeof entry === "string");
|
|
71
|
+
return scriptUrls.some(
|
|
72
|
+
(entry) => matchesScriptPath(entry, origin, pathUrl.pathname)
|
|
73
|
+
);
|
|
74
|
+
});
|
|
75
|
+
const removed = [];
|
|
76
|
+
for (const registration of matched) {
|
|
77
|
+
const success = await registration.unregister();
|
|
78
|
+
if (success) {
|
|
79
|
+
removed.push(registration);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
return removed;
|
|
83
|
+
} catch (error) {
|
|
84
|
+
console.warn("[mokup] Failed to unregister service worker:", error);
|
|
85
|
+
return [];
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
exports.registerMokupServiceWorker = registerMokupServiceWorker;
|
|
90
|
+
exports.unregisterMokupServiceWorker = unregisterMokupServiceWorker;
|
package/dist/sw.d.cts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
interface MokupServiceWorkerOptions {
|
|
2
|
+
path?: string;
|
|
3
|
+
scope?: string;
|
|
4
|
+
enabled?: boolean;
|
|
5
|
+
}
|
|
6
|
+
interface MokupServiceWorkerUnregisterOptions {
|
|
7
|
+
path?: string;
|
|
8
|
+
scope?: string;
|
|
9
|
+
}
|
|
10
|
+
declare function registerMokupServiceWorker(options?: MokupServiceWorkerOptions): Promise<ServiceWorkerRegistration | null>;
|
|
11
|
+
declare function unregisterMokupServiceWorker(options?: MokupServiceWorkerUnregisterOptions): Promise<ServiceWorkerRegistration[]>;
|
|
12
|
+
|
|
13
|
+
export { registerMokupServiceWorker, unregisterMokupServiceWorker };
|
|
14
|
+
export type { MokupServiceWorkerOptions, MokupServiceWorkerUnregisterOptions };
|
package/dist/sw.d.mts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
interface MokupServiceWorkerOptions {
|
|
2
|
+
path?: string;
|
|
3
|
+
scope?: string;
|
|
4
|
+
enabled?: boolean;
|
|
5
|
+
}
|
|
6
|
+
interface MokupServiceWorkerUnregisterOptions {
|
|
7
|
+
path?: string;
|
|
8
|
+
scope?: string;
|
|
9
|
+
}
|
|
10
|
+
declare function registerMokupServiceWorker(options?: MokupServiceWorkerOptions): Promise<ServiceWorkerRegistration | null>;
|
|
11
|
+
declare function unregisterMokupServiceWorker(options?: MokupServiceWorkerUnregisterOptions): Promise<ServiceWorkerRegistration[]>;
|
|
12
|
+
|
|
13
|
+
export { registerMokupServiceWorker, unregisterMokupServiceWorker };
|
|
14
|
+
export type { MokupServiceWorkerOptions, MokupServiceWorkerUnregisterOptions };
|
package/dist/sw.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
interface MokupServiceWorkerOptions {
|
|
2
|
+
path?: string;
|
|
3
|
+
scope?: string;
|
|
4
|
+
enabled?: boolean;
|
|
5
|
+
}
|
|
6
|
+
interface MokupServiceWorkerUnregisterOptions {
|
|
7
|
+
path?: string;
|
|
8
|
+
scope?: string;
|
|
9
|
+
}
|
|
10
|
+
declare function registerMokupServiceWorker(options?: MokupServiceWorkerOptions): Promise<ServiceWorkerRegistration | null>;
|
|
11
|
+
declare function unregisterMokupServiceWorker(options?: MokupServiceWorkerUnregisterOptions): Promise<ServiceWorkerRegistration[]>;
|
|
12
|
+
|
|
13
|
+
export { registerMokupServiceWorker, unregisterMokupServiceWorker };
|
|
14
|
+
export type { MokupServiceWorkerOptions, MokupServiceWorkerUnregisterOptions };
|
package/dist/sw.mjs
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
const defaultSwPath = "/mokup-sw.js";
|
|
2
|
+
const defaultSwScope = "/";
|
|
3
|
+
function normalizeSwPath(path) {
|
|
4
|
+
if (!path) {
|
|
5
|
+
return defaultSwPath;
|
|
6
|
+
}
|
|
7
|
+
return path.startsWith("/") ? path : `/${path}`;
|
|
8
|
+
}
|
|
9
|
+
function normalizeSwScope(scope) {
|
|
10
|
+
if (!scope) {
|
|
11
|
+
return defaultSwScope;
|
|
12
|
+
}
|
|
13
|
+
return scope.startsWith("/") ? scope : `/${scope}`;
|
|
14
|
+
}
|
|
15
|
+
function resolveScopeUrl(scope, origin) {
|
|
16
|
+
const url = new URL(scope, origin);
|
|
17
|
+
if (!url.pathname.endsWith("/")) {
|
|
18
|
+
url.pathname = `${url.pathname}/`;
|
|
19
|
+
}
|
|
20
|
+
return url.href;
|
|
21
|
+
}
|
|
22
|
+
function matchesScriptPath(scriptUrl, origin, pathname) {
|
|
23
|
+
try {
|
|
24
|
+
const parsed = new URL(scriptUrl);
|
|
25
|
+
return parsed.origin === origin && parsed.pathname === pathname;
|
|
26
|
+
} catch {
|
|
27
|
+
return false;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
async function registerMokupServiceWorker(options = {}) {
|
|
31
|
+
if (options.enabled === false) {
|
|
32
|
+
return null;
|
|
33
|
+
}
|
|
34
|
+
if (typeof window === "undefined" || !("serviceWorker" in navigator)) {
|
|
35
|
+
return null;
|
|
36
|
+
}
|
|
37
|
+
const path = normalizeSwPath(options.path);
|
|
38
|
+
const scope = normalizeSwScope(options.scope);
|
|
39
|
+
try {
|
|
40
|
+
return await navigator.serviceWorker.register(path, {
|
|
41
|
+
type: "module",
|
|
42
|
+
scope
|
|
43
|
+
});
|
|
44
|
+
} catch (error) {
|
|
45
|
+
console.warn("[mokup] Failed to register service worker:", error);
|
|
46
|
+
return null;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
async function unregisterMokupServiceWorker(options = {}) {
|
|
50
|
+
if (typeof window === "undefined" || !("serviceWorker" in navigator)) {
|
|
51
|
+
return [];
|
|
52
|
+
}
|
|
53
|
+
const origin = window.location.origin;
|
|
54
|
+
const path = normalizeSwPath(options.path);
|
|
55
|
+
const scope = normalizeSwScope(options.scope);
|
|
56
|
+
const pathUrl = new URL(path, origin);
|
|
57
|
+
const scopeUrl = resolveScopeUrl(scope, origin);
|
|
58
|
+
try {
|
|
59
|
+
const registrations = await navigator.serviceWorker.getRegistrations();
|
|
60
|
+
const matched = registrations.filter((registration) => {
|
|
61
|
+
if (registration.scope !== scopeUrl) {
|
|
62
|
+
return false;
|
|
63
|
+
}
|
|
64
|
+
const scriptUrls = [
|
|
65
|
+
registration.active?.scriptURL,
|
|
66
|
+
registration.waiting?.scriptURL,
|
|
67
|
+
registration.installing?.scriptURL
|
|
68
|
+
].filter((entry) => typeof entry === "string");
|
|
69
|
+
return scriptUrls.some(
|
|
70
|
+
(entry) => matchesScriptPath(entry, origin, pathUrl.pathname)
|
|
71
|
+
);
|
|
72
|
+
});
|
|
73
|
+
const removed = [];
|
|
74
|
+
for (const registration of matched) {
|
|
75
|
+
const success = await registration.unregister();
|
|
76
|
+
if (success) {
|
|
77
|
+
removed.push(registration);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
return removed;
|
|
81
|
+
} catch (error) {
|
|
82
|
+
console.warn("[mokup] Failed to unregister service worker:", error);
|
|
83
|
+
return [];
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export { registerMokupServiceWorker, unregisterMokupServiceWorker };
|