@rpcbase/server 0.431.0 → 0.432.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.
@@ -1 +1 @@
1
- {"version":3,"file":"getDerivedKey.d.ts","sourceRoot":"","sources":["../../../../pkg/server/src/getDerivedKey.ts"],"names":[],"mappings":"AAIA,eAAO,MAAM,aAAa,GACxB,WAAW,MAAM,EACjB,MAAM,MAAM,EACZ,SAAQ,MAAW,EAAE,0BAA0B;AAC/C,OAAM,MAAW,KAChB,MAUF,CAAA"}
1
+ {"version":3,"file":"getDerivedKey.d.ts","sourceRoot":"","sources":["../src/getDerivedKey.ts"],"names":[],"mappings":"AAIA,eAAO,MAAM,aAAa,GACxB,WAAW,MAAM,EACjB,MAAM,MAAM,EACZ,SAAQ,MAAW,EAAE,0BAA0B;AAC/C,OAAM,MAAW,KAChB,MAUF,CAAA"}
@@ -1 +1 @@
1
- {"version":3,"file":"hashPassword.d.ts","sourceRoot":"","sources":["../../../../pkg/server/src/hashPassword.ts"],"names":[],"mappings":"AAGA,wBAAsB,YAAY,CAAC,QAAQ,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAoBlF"}
1
+ {"version":3,"file":"hashPassword.d.ts","sourceRoot":"","sources":["../src/hashPassword.ts"],"names":[],"mappings":"AAGA,wBAAsB,YAAY,CAAC,QAAQ,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAoBlF"}
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- export * from "./initServer";
2
- export * from "./getDerivedKey";
3
- export * from "./hashPassword";
1
+ export * from './initServer';
2
+ export * from './getDerivedKey';
3
+ export * from './hashPassword';
4
4
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../pkg/server/src/index.ts"],"names":[],"mappings":"AAAA,cAAc,cAAc,CAAA;AAC5B,cAAc,iBAAiB,CAAA;AAC/B,cAAc,gBAAgB,CAAA"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,cAAc,CAAA;AAC5B,cAAc,iBAAiB,CAAA;AAC/B,cAAc,gBAAgB,CAAA"}
package/dist/index.js CHANGED
@@ -1,3 +1,129 @@
1
- export * from "./initServer";
2
- export * from "./getDerivedKey";
3
- export * from "./hashPassword";
1
+ import session from "express-session";
2
+ import { RedisStore } from "connect-redis";
3
+ import { createClient } from "redis";
4
+ import requestIp from "request-ip";
5
+ import env from "@rpcbase/env";
6
+ import { initApiClient } from "@rpcbase/client";
7
+ import assert from "assert";
8
+ import { hkdfSync, scrypt } from "crypto";
9
+ import { createProxyMiddleware } from "http-proxy-middleware";
10
+ const getDerivedKey = (masterKey, info, length = 32, salt = "") => {
11
+ assert(masterKey?.length >= 32, "MASTER_KEY must be 32 chars or longer.");
12
+ return Buffer.from(hkdfSync(
13
+ "sha256",
14
+ masterKey,
15
+ Buffer.from(salt),
16
+ Buffer.from(info),
17
+ length
18
+ )).toString("hex");
19
+ };
20
+ const handleProxyError = (err) => {
21
+ console.error(`Proxy error: ${err.message}`);
22
+ };
23
+ const metricsIngestProxyMiddleware = (app) => {
24
+ app.use(
25
+ "/ingest",
26
+ createProxyMiddleware({
27
+ target: "https://eu.i.posthog.com",
28
+ changeOrigin: true,
29
+ pathRewrite: { "^/ingest": "" },
30
+ // Remove '/ingest' from the proxied request
31
+ on: {
32
+ error: handleProxyError
33
+ }
34
+ })
35
+ );
36
+ };
37
+ process.env = {
38
+ ...__vite_env__,
39
+ ...process.env,
40
+ ...env.config().parsed
41
+ };
42
+ const isProduction = process.env.NODE_ENV === "production";
43
+ const initServer = async (app, serverEnv) => {
44
+ await initApiClient({ app });
45
+ app.disable("x-powered-by");
46
+ app.set("trust proxy", true);
47
+ app.use(requestIp.mw());
48
+ app.use((req, res, next) => {
49
+ if (req.headers.host?.startsWith("www.")) {
50
+ const newHost = req.headers.host.replace("www.", "");
51
+ return res.redirect(301, `${req.protocol}://${newHost}${req.originalUrl}`);
52
+ }
53
+ next();
54
+ });
55
+ metricsIngestProxyMiddleware(app);
56
+ if (!serverEnv.REDIS_URL) {
57
+ console.log("WARNING", "missing REDIS_URL, will skip session storage middleware");
58
+ return;
59
+ } else {
60
+ console.log("REDIS_URL:", serverEnv.REDIS_URL);
61
+ }
62
+ const sessionSecret = getDerivedKey(serverEnv.MASTER_KEY, "express_session_key");
63
+ const reconnectStrategy = (retries) => {
64
+ console.log("redis_client::rb/server reconnectStrategy::retrying with arg", retries);
65
+ if (retries < 5) {
66
+ console.log("retry count:", retries, "retrying in 1s");
67
+ return 4e3;
68
+ } else {
69
+ return new Error("max retries expired");
70
+ }
71
+ };
72
+ const redisClient = createClient({
73
+ url: serverEnv.REDIS_URL,
74
+ socket: {
75
+ reconnectStrategy,
76
+ connectTimeout: 1e4,
77
+ keepAlive: false
78
+ }
79
+ });
80
+ redisClient.on("ready", () => {
81
+ console.log("session-storage::redis_client connected");
82
+ });
83
+ redisClient.on("error", (error) => {
84
+ console.log("session-storage::redis_client ERROR", error);
85
+ });
86
+ redisClient.connect();
87
+ const sessionConfig = {
88
+ name: "session",
89
+ store: new RedisStore({ client: redisClient }),
90
+ proxy: true,
91
+ resave: false,
92
+ saveUninitialized: false,
93
+ secret: sessionSecret,
94
+ cookie: {
95
+ maxAge: 1e3 * 3600 * 24 * 60
96
+ // 60 days
97
+ }
98
+ };
99
+ if (isProduction) {
100
+ sessionConfig.cookie.secure = true;
101
+ }
102
+ app.use(session(sessionConfig));
103
+ };
104
+ async function hashPassword(password, salt) {
105
+ const keyLength = 64;
106
+ const options = {
107
+ N: 8192,
108
+ // CPU/memory cost parameter
109
+ r: 8,
110
+ // Block size
111
+ p: 1
112
+ // Parallelization factor
113
+ };
114
+ const derivedKey = await new Promise((resolve, reject) => {
115
+ scrypt(password, salt, keyLength, options, (err, derivedKey2) => {
116
+ if (err) {
117
+ reject(err);
118
+ } else {
119
+ resolve(derivedKey2);
120
+ }
121
+ });
122
+ });
123
+ return derivedKey;
124
+ }
125
+ export {
126
+ getDerivedKey,
127
+ hashPassword,
128
+ initServer
129
+ };
@@ -1,4 +1,4 @@
1
- import { Application } from "express";
1
+ import { Application } from 'express';
2
2
  export declare const initServer: (app: Application, serverEnv: {
3
3
  [key: string]: string | undefined;
4
4
  }) => Promise<void>;
@@ -1 +1 @@
1
- {"version":3,"file":"initServer.d.ts","sourceRoot":"","sources":["../../../../pkg/server/src/initServer.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,SAAS,CAAA;AAsBrC,eAAO,MAAM,UAAU,GAAU,KAAK,WAAW,EAAE,WAAW;IAAE,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAAA;CAAE,kBA6ElG,CAAA"}
1
+ {"version":3,"file":"initServer.d.ts","sourceRoot":"","sources":["../src/initServer.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,SAAS,CAAA;AAsBrC,eAAO,MAAM,UAAU,GAAU,KAAK,WAAW,EAAE,WAAW;IAAE,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAAA;CAAE,kBA6ElG,CAAA"}
@@ -1,3 +1,3 @@
1
- import { Application } from "express";
1
+ import { Application } from 'express';
2
2
  export declare const metricsIngestProxyMiddleware: (app: Application) => void;
3
3
  //# sourceMappingURL=metricsIngestProxyMiddleware.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"metricsIngestProxyMiddleware.d.ts","sourceRoot":"","sources":["../../../../pkg/server/src/metricsIngestProxyMiddleware.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,WAAW,EAAC,MAAM,SAAS,CAAA;AAUnC,eAAO,MAAM,4BAA4B,GAAI,KAAK,WAAW,SAY5D,CAAA"}
1
+ {"version":3,"file":"metricsIngestProxyMiddleware.d.ts","sourceRoot":"","sources":["../src/metricsIngestProxyMiddleware.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,WAAW,EAAC,MAAM,SAAS,CAAA;AAUnC,eAAO,MAAM,4BAA4B,GAAI,KAAK,WAAW,SAY5D,CAAA"}
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../../pkg/server/src/types/index.ts"],"names":[],"mappings":"AAEA,MAAM,MAAM,WAAW,GAAG;IACxB,EAAE,EAAE,MAAM,CAAC;IACX,iBAAiB,EAAE,MAAM,CAAC;IAC1B,iBAAiB,EAAE,MAAM,EAAE,CAAC;CAC7B,CAAA"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/types/index.ts"],"names":[],"mappings":"AAEA,MAAM,MAAM,WAAW,GAAG;IACxB,EAAE,EAAE,MAAM,CAAC;IACX,iBAAiB,EAAE,MAAM,CAAC;IAC1B,iBAAiB,EAAE,MAAM,EAAE,CAAC;CAC7B,CAAA"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rpcbase/server",
3
- "version": "0.431.0",
3
+ "version": "0.432.0",
4
4
  "type": "module",
5
5
  "files": [
6
6
  "dist"
@@ -13,12 +13,14 @@
13
13
  },
14
14
  "wireit": {
15
15
  "build": {
16
- "command": "node ../../scripts/build-package.js server",
16
+ "command": "../../node_modules/.bin/vite build",
17
17
  "files": [
18
18
  "src/**/*",
19
19
  "../../tsconfig.json",
20
- "../../tsconfig.base.json",
21
- "../../scripts/build-package.js"
20
+ "../../scripts/tsconfig.pkg.json",
21
+ "./tsconfig.json",
22
+ "./vite.config.js",
23
+ "../../scripts/vite.config-pkg.js"
22
24
  ],
23
25
  "output": [
24
26
  "dist/"
@@ -1,7 +0,0 @@
1
- import assert from "assert";
2
- import { hkdfSync } from "crypto";
3
- export const getDerivedKey = (masterKey, info, length = 32, // Default to 256-bit keys
4
- salt = "") => {
5
- assert(masterKey?.length >= 32, "MASTER_KEY must be 32 chars or longer.");
6
- return Buffer.from(hkdfSync("sha256", masterKey, Buffer.from(salt), Buffer.from(info), length)).toString("hex");
7
- };
@@ -1,21 +0,0 @@
1
- import { scrypt } from "crypto";
2
- export async function hashPassword(password, salt) {
3
- const keyLength = 64; // Length of the derived key
4
- const options = {
5
- N: 8192, // CPU/memory cost parameter
6
- r: 8, // Block size
7
- p: 1 // Parallelization factor
8
- };
9
- // Return a Promise-wrapped scrypt call
10
- const derivedKey = await new Promise((resolve, reject) => {
11
- scrypt(password, salt, keyLength, options, (err, derivedKey) => {
12
- if (err) {
13
- reject(err);
14
- }
15
- else {
16
- resolve(derivedKey);
17
- }
18
- });
19
- });
20
- return derivedKey;
21
- }
@@ -1,79 +0,0 @@
1
- import session from "express-session";
2
- import { RedisStore } from "connect-redis";
3
- import { createClient } from "redis";
4
- import requestIp from "request-ip";
5
- import env from "@rpcbase/env";
6
- import { initApiClient } from "@rpcbase/client";
7
- // dotenv setup, merge process env with vite process.env file
8
- process.env = {
9
- ...__vite_env__,
10
- ...process.env,
11
- ...env.config().parsed
12
- };
13
- import { getDerivedKey } from "./getDerivedKey";
14
- import { metricsIngestProxyMiddleware } from "./metricsIngestProxyMiddleware";
15
- const isProduction = process.env.NODE_ENV === "production";
16
- export const initServer = async (app, serverEnv) => {
17
- await initApiClient({ app });
18
- app.disable("x-powered-by");
19
- app.set("trust proxy", true);
20
- app.use(requestIp.mw());
21
- // redirect www to non-www middleware
22
- app.use((req, res, next) => {
23
- if (req.headers.host?.startsWith("www.")) {
24
- const newHost = req.headers.host.replace("www.", "");
25
- return res.redirect(301, `${req.protocol}://${newHost}${req.originalUrl}`);
26
- }
27
- next();
28
- });
29
- metricsIngestProxyMiddleware(app);
30
- if (!serverEnv.REDIS_URL) {
31
- console.log("WARNING", "missing REDIS_URL, will skip session storage middleware");
32
- return;
33
- }
34
- else {
35
- console.log("REDIS_URL:", serverEnv.REDIS_URL);
36
- }
37
- const sessionSecret = getDerivedKey(serverEnv.MASTER_KEY, "express_session_key");
38
- const reconnectStrategy = (retries) => {
39
- console.log("redis_client::rb/server reconnectStrategy::retrying with arg", retries);
40
- if (retries < 5) {
41
- console.log("retry count:", retries, "retrying in 1s");
42
- return 4000;
43
- }
44
- else {
45
- return new Error("max retries expired");
46
- }
47
- };
48
- const redisClient = createClient({
49
- url: serverEnv.REDIS_URL,
50
- socket: {
51
- reconnectStrategy,
52
- connectTimeout: 10000,
53
- keepAlive: false,
54
- },
55
- });
56
- redisClient.on("ready", () => {
57
- console.log("session-storage::redis_client connected");
58
- });
59
- redisClient.on("error", (error) => {
60
- console.log("session-storage::redis_client ERROR", error);
61
- });
62
- redisClient.connect();
63
- const sessionConfig = {
64
- name: "session",
65
- store: new RedisStore({ client: redisClient }),
66
- proxy: true,
67
- resave: false,
68
- saveUninitialized: false,
69
- secret: sessionSecret,
70
- cookie: {
71
- maxAge: 1000 * 3600 * 24 * 60 // 60 days
72
- },
73
- };
74
- if (isProduction) {
75
- sessionConfig.cookie.secure = true;
76
- // sessionConfig.cookie.domain = ""
77
- }
78
- app.use(session(sessionConfig));
79
- };
@@ -1,14 +0,0 @@
1
- import { createProxyMiddleware } from "http-proxy-middleware";
2
- const handleProxyError = (err) => {
3
- console.error(`Proxy error: ${err.message}`);
4
- };
5
- export const metricsIngestProxyMiddleware = (app) => {
6
- app.use("/ingest", createProxyMiddleware({
7
- target: "https://eu.i.posthog.com",
8
- changeOrigin: true,
9
- pathRewrite: { "^/ingest": "" }, // Remove '/ingest' from the proxied request
10
- on: {
11
- error: handleProxyError
12
- }
13
- }));
14
- };
@@ -1 +0,0 @@
1
- export {};