insite-common 2.0.1 → 2.1.1
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/lib/backend/index.ts +2 -0
- package/lib/backend/server/createServer.ts +23 -0
- package/lib/backend/server/index.ts +3 -0
- package/lib/backend/server/resolveSSL.ts +26 -0
- package/lib/backend/server/showServerListeningMessage.ts +29 -0
- package/lib/common/index.ts +1 -0
- package/lib/common/types/abilities.ts +99 -0
- package/lib/common/types/index.ts +1 -0
- package/lib/frontend/index.ts +1 -0
- package/lib/index.ts +3 -0
- package/package.json +13 -11
- package/bun.lockb +0 -0
- package/dist/index.d.ts +0 -2
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js +0 -1
- package/dist/types.d.ts +0 -41
- package/dist/types.d.ts.map +0 -1
- package/dist/types.js +0 -1
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import http from "node:http";
|
|
2
|
+
import https from "node:https";
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
export function createServer(options: http.ServerOptions): http.Server;
|
|
6
|
+
export function createServer(options: https.ServerOptions): https.Server;
|
|
7
|
+
export function createServer(options: http.ServerOptions, port: number | string): Promise<http.Server>;
|
|
8
|
+
export function createServer(options: https.ServerOptions, port: number | string): Promise<https.Server>;
|
|
9
|
+
export function createServer(options: http.ServerOptions | https.ServerOptions, port?: number | string) {
|
|
10
|
+
|
|
11
|
+
const server =
|
|
12
|
+
("key" in options && "cert" in options) ?
|
|
13
|
+
https.createServer(options) :
|
|
14
|
+
http.createServer(options);
|
|
15
|
+
|
|
16
|
+
return port ?
|
|
17
|
+
new Promise(resolve => {
|
|
18
|
+
server.on("listening", () => resolve(server));
|
|
19
|
+
server.listen(port);
|
|
20
|
+
|
|
21
|
+
}) :
|
|
22
|
+
server;
|
|
23
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { readFileSync } from "node:fs";
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
export function resolveSSL(ssl: {
|
|
5
|
+
cert: Buffer<ArrayBufferLike> | string;
|
|
6
|
+
key: Buffer<ArrayBufferLike> | string;
|
|
7
|
+
} = {
|
|
8
|
+
cert: process.env.INSITE_SSL_CERT!,
|
|
9
|
+
key: process.env.INSITE_SSL_KEY!
|
|
10
|
+
}) {
|
|
11
|
+
if (ssl.cert && ssl.key)
|
|
12
|
+
try {
|
|
13
|
+
if (typeof ssl.cert == "string" && !/^-{3,}BEGIN/.test(ssl.cert))
|
|
14
|
+
ssl.cert = readFileSync(ssl.cert);
|
|
15
|
+
|
|
16
|
+
if (typeof ssl.key == "string" && !/^-{3,}BEGIN/.test(ssl.key))
|
|
17
|
+
ssl.key = readFileSync(ssl.key);
|
|
18
|
+
|
|
19
|
+
return ssl;
|
|
20
|
+
} catch (error) {
|
|
21
|
+
console.error("SSL resolve error:", error);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
return undefined;// eslint-disable-line unicorn/no-useless-undefined
|
|
25
|
+
}
|
|
26
|
+
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type http from "node:http";
|
|
2
|
+
import type https from "node:https";
|
|
3
|
+
import type { AddressInfo } from "node:net";
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
type Server = {
|
|
7
|
+
icon: string;
|
|
8
|
+
name: string;
|
|
9
|
+
protocol: string;
|
|
10
|
+
server: http.Server | https.Server;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
export function showServerListeningMessage(server: Server) {
|
|
15
|
+
|
|
16
|
+
const { address, family, port } = server.server.address() as AddressInfo;
|
|
17
|
+
|
|
18
|
+
console.info(
|
|
19
|
+
`${server.icon} inSite ${server.name} Server is listening on`,
|
|
20
|
+
`${server.protocol}://${
|
|
21
|
+
family === "IPv4" ?
|
|
22
|
+
address : (
|
|
23
|
+
address.replace(/^::(?:ffff:)?/, "") ||
|
|
24
|
+
process.env.NODE_ENV === "development" ? "localhost" : "127.0.0.1"
|
|
25
|
+
)
|
|
26
|
+
}:${port}`
|
|
27
|
+
);
|
|
28
|
+
|
|
29
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export type * from "./types";
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
type CommonSchema = {
|
|
5
|
+
_id: string;
|
|
6
|
+
title?: string;
|
|
7
|
+
description?: string;
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
export type AbilityParamItemSchema = CommonSchema;
|
|
11
|
+
|
|
12
|
+
type AbilityParamItemsSchema = CommonSchema & {
|
|
13
|
+
type: "items";
|
|
14
|
+
items: readonly AbilityParamItemSchema[];
|
|
15
|
+
isInheritable?: boolean;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
type AbilityParamNumberSchema = CommonSchema & {
|
|
19
|
+
type: "number";
|
|
20
|
+
min?: number;
|
|
21
|
+
max?: number;
|
|
22
|
+
isInheritable?: boolean;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
export type AbilityParamSchema = AbilityParamItemsSchema | AbilityParamNumberSchema;
|
|
26
|
+
|
|
27
|
+
export type AbilitySchema = CommonSchema & {
|
|
28
|
+
params?: readonly AbilityParamSchema[];
|
|
29
|
+
abilities?: AbilitiesSchema;
|
|
30
|
+
isInheritable?: boolean;
|
|
31
|
+
top?: boolean;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
export type AbilitiesSchema = readonly AbilitySchema[];
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
type SettleParam<PS extends AbilityParamSchema> =
|
|
38
|
+
PS extends AbilityParamNumberSchema ?
|
|
39
|
+
number :
|
|
40
|
+
PS extends AbilityParamItemsSchema ?
|
|
41
|
+
PS["items"][number]["_id"][] :
|
|
42
|
+
never;
|
|
43
|
+
|
|
44
|
+
type SettleParams<A extends AbilitySchema> =
|
|
45
|
+
A["params"] extends readonly AbilityParamSchema[] ?
|
|
46
|
+
{ [P in A["params"][number] as P["_id"]]: SettleParam<P> } :
|
|
47
|
+
never;
|
|
48
|
+
|
|
49
|
+
type SettleSubAbilities<A extends AbilitySchema> =
|
|
50
|
+
A["abilities"] extends AbilitiesSchema ?
|
|
51
|
+
Partial<{ [SA in A["abilities"][number] as SA["top"] extends true ? never : SA["_id"]]: SettleAbility<SA> }> :
|
|
52
|
+
never;
|
|
53
|
+
|
|
54
|
+
type SettleAbility<A extends AbilitySchema> =
|
|
55
|
+
A extends { params: any } ?
|
|
56
|
+
A extends { abilities: any } ?
|
|
57
|
+
SettleParams<A> & SettleSubAbilities<A> :
|
|
58
|
+
SettleParams<A> :
|
|
59
|
+
A extends { abilities: any } ?
|
|
60
|
+
SettleSubAbilities<A> :
|
|
61
|
+
true;
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
type SettleTopLevelAbility<A extends AbilitySchema> = { [K in A["_id"]]: SettleAbility<A> };
|
|
65
|
+
|
|
66
|
+
type TakeAbilitySchema<A extends AbilitySchema, ForceTopLevel = false> =
|
|
67
|
+
(
|
|
68
|
+
A["abilities"] extends AbilitiesSchema ?
|
|
69
|
+
ScanAbilitySchemas<A["abilities"]> :
|
|
70
|
+
object
|
|
71
|
+
) & (
|
|
72
|
+
ForceTopLevel extends true ?
|
|
73
|
+
SettleTopLevelAbility<A> :
|
|
74
|
+
A["top"] extends true ?
|
|
75
|
+
SettleTopLevelAbility<A> :
|
|
76
|
+
object
|
|
77
|
+
);
|
|
78
|
+
|
|
79
|
+
export type ScanAbilitySchemas<AS extends AbilitiesSchema, ForceTopLevel = false> =
|
|
80
|
+
Partial<
|
|
81
|
+
AS extends readonly [ infer Head extends AbilitySchema, ...infer Tail extends AbilitiesSchema ] ?
|
|
82
|
+
ScanAbilitySchemas<Tail, ForceTopLevel> & TakeAbilitySchema<Head, ForceTopLevel> :
|
|
83
|
+
object
|
|
84
|
+
>;
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
type BasisAbilities = {
|
|
88
|
+
login?: {
|
|
89
|
+
sessionsLimit: number;
|
|
90
|
+
};
|
|
91
|
+
inSite?: {
|
|
92
|
+
users?: {
|
|
93
|
+
roles?: true;
|
|
94
|
+
};
|
|
95
|
+
};
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
export type Abilities<AS extends AbilitiesSchema> = BasisAbilities & ScanAbilitySchemas<AS, true>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export type * from "./abilities";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "../common";
|
package/lib/index.ts
ADDED
package/package.json
CHANGED
|
@@ -1,18 +1,18 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "insite-common",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.1.1",
|
|
4
4
|
"description": "inSite common things",
|
|
5
5
|
"type": "module",
|
|
6
|
-
"main": "./
|
|
7
|
-
"module": "./
|
|
6
|
+
"main": "./lib/index.ts",
|
|
7
|
+
"module": "./lib/index.ts",
|
|
8
8
|
"exports": {
|
|
9
|
-
".": "./
|
|
10
|
-
"./
|
|
9
|
+
".": "./lib/common/index.ts",
|
|
10
|
+
"./frontend": "./lib/frontend/index.ts",
|
|
11
|
+
"./backend": "./lib/backend/index.ts"
|
|
11
12
|
},
|
|
12
|
-
"packageManager": "bun@1.
|
|
13
|
+
"packageManager": "bun@1.2.2",
|
|
13
14
|
"scripts": {
|
|
14
|
-
"test": "echo \"Error: no test specified\" && exit 1"
|
|
15
|
-
"build": "tsc -p ."
|
|
15
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
16
16
|
},
|
|
17
17
|
"repository": {
|
|
18
18
|
"type": "git",
|
|
@@ -24,10 +24,12 @@
|
|
|
24
24
|
],
|
|
25
25
|
"author": "Eugene Nesvetaev <dev@nesvet.dev> (https://nesvet.dev/)",
|
|
26
26
|
"license": "MIT",
|
|
27
|
-
"dependencies": {},
|
|
28
27
|
"devDependencies": {
|
|
29
|
-
"@nesvet/
|
|
30
|
-
"@nesvet/eslint-config": "^2.0.34",
|
|
28
|
+
"@nesvet/eslint-config": "^2.0.47",
|
|
31
29
|
"typescript": "^5.7.3"
|
|
30
|
+
},
|
|
31
|
+
"peerDependencies": {
|
|
32
|
+
"insite-http": "^2.1.1",
|
|
33
|
+
"insite-ws": "^2.1.1"
|
|
32
34
|
}
|
|
33
35
|
}
|
package/bun.lockb
DELETED
|
Binary file
|
package/dist/index.d.ts
DELETED
package/dist/index.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC"}
|
package/dist/index.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from "./types.js";
|
package/dist/types.d.ts
DELETED
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
type CommonSchema = {
|
|
2
|
-
_id: string;
|
|
3
|
-
title?: string;
|
|
4
|
-
};
|
|
5
|
-
export type AbilityParamItemSchema = CommonSchema;
|
|
6
|
-
type AbilityParamItemsSchema = {
|
|
7
|
-
type: "items";
|
|
8
|
-
items: readonly AbilityParamItemSchema[];
|
|
9
|
-
isInheritable?: boolean;
|
|
10
|
-
} & CommonSchema;
|
|
11
|
-
type AbilityParamNumberSchema = {
|
|
12
|
-
type: "number";
|
|
13
|
-
min?: number;
|
|
14
|
-
max?: number;
|
|
15
|
-
isInheritable?: boolean;
|
|
16
|
-
} & CommonSchema;
|
|
17
|
-
type AbilityParamSchema = AbilityParamItemsSchema | AbilityParamNumberSchema;
|
|
18
|
-
export type AbilitySchema = {
|
|
19
|
-
params?: readonly AbilityParamSchema[];
|
|
20
|
-
subAbilities?: AbilitiesSchema;
|
|
21
|
-
isInheritable?: boolean;
|
|
22
|
-
} & CommonSchema;
|
|
23
|
-
export type AbilitiesSchema = readonly AbilitySchema[];
|
|
24
|
-
type ExtractParamSchemaType<PS extends AbilityParamSchema> = PS extends AbilityParamNumberSchema ? number : PS extends AbilityParamItemsSchema ? PS["items"][number]["_id"][] : never;
|
|
25
|
-
type ExtractAbilitySchemaType<AS extends AbilitySchema> = AS["params"] extends readonly AbilityParamSchema[] ? {
|
|
26
|
-
[K in AS["params"][number] as K["_id"]]: ExtractParamSchemaType<K>;
|
|
27
|
-
} : true;
|
|
28
|
-
type FlattenAbilitySchema<A extends AbilitySchema> = {
|
|
29
|
-
[K in A["_id"]]: ExtractAbilitySchemaType<A>;
|
|
30
|
-
} & (A["subAbilities"] extends AbilitiesSchema ? FlattenAbilitySchemas<A["subAbilities"]> : object);
|
|
31
|
-
export type FlattenAbilitySchemas<A extends AbilitiesSchema> = Partial<A extends readonly [infer Head extends AbilitySchema, ...infer Tail extends AbilitiesSchema] ? FlattenAbilitySchema<Head> & FlattenAbilitySchemas<Tail> : object>;
|
|
32
|
-
export type Abilities<A extends AbilitiesSchema> = {
|
|
33
|
-
login?: {
|
|
34
|
-
sessionsLimit: number;
|
|
35
|
-
};
|
|
36
|
-
inSite?: {
|
|
37
|
-
sections: ("users")[];
|
|
38
|
-
};
|
|
39
|
-
} & FlattenAbilitySchemas<A>;
|
|
40
|
-
export {};
|
|
41
|
-
//# sourceMappingURL=types.d.ts.map
|
package/dist/types.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,KAAK,YAAY,GAAG;IACnB,GAAG,EAAE,MAAM,CAAC;IACZ,KAAK,CAAC,EAAE,MAAM,CAAC;CACf,CAAC;AAEF,MAAM,MAAM,sBAAsB,GAAG,YAAY,CAAC;AAElD,KAAK,uBAAuB,GAAG;IAC9B,IAAI,EAAE,OAAO,CAAC;IACd,KAAK,EAAE,SAAS,sBAAsB,EAAE,CAAC;IACzC,aAAa,CAAC,EAAE,OAAO,CAAC;CACxB,GAAG,YAAY,CAAC;AAEjB,KAAK,wBAAwB,GAAG;IA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