@livestore/devtools-expo 0.3.0-dev.36 → 0.3.0-dev.38

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,7 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  // eslint-disable-next-line @typescript-eslint/no-require-imports, unicorn/prefer-module, @typescript-eslint/consistent-type-imports, prettier/prettier
4
- const { Effect } = require('@livestore/utils/effect');
4
+ const { Effect, Logger, LogLevel } = require('@livestore/utils/effect');
5
+ // eslint-disable-next-line @typescript-eslint/no-require-imports, unicorn/prefer-module, @typescript-eslint/consistent-type-imports, prettier/prettier
6
+ const { PlatformNode } = require('@livestore/utils/node');
5
7
  /**
6
8
  * Patches the Metro config to add a middleware via `config.server.enhanceMiddleware`.
7
9
  */
@@ -10,32 +12,42 @@ const addLiveStoreDevtoolsMiddleware = (config, options) => {
10
12
  if (process.env.CI || !process.stdout.isTTY) {
11
13
  return;
12
14
  }
13
- const viteMiddleware = makeLiveStoreDevtoolsMiddleware(options);
15
+ const host = options.host ?? 'localhost';
16
+ const port = options.port ?? 4242;
17
+ // Needed for @livestore/adapter-expo
18
+ process.env.EXPO_PUBLIC_LIVESTORE_DEVTOOLS_URL = `http://${host}:${port}`;
19
+ import('@livestore/adapter-node/devtools')
20
+ .then(async ({ startDevtoolsServer }) => {
21
+ startDevtoolsServer({
22
+ clientSessionInfo: undefined,
23
+ schemaPath: options.schemaPath,
24
+ host,
25
+ port,
26
+ }).pipe(Effect.provide(PlatformNode.NodeHttpClient.layer), Effect.provide(Logger.prettyWithThread('@livestore/devtools-expo:metro-config')), Logger.withMinimumLogLevel(LogLevel.Debug), Effect.tapCauseLogPretty, Effect.runPromise);
27
+ })
28
+ .catch((error) => {
29
+ console.error(error);
30
+ });
14
31
  const previousEnhanceMiddleware = config.server.enhanceMiddleware;
32
+ /** Redirects requests to LiveStore DevTools to `http://${host}:${port}/_livestore/${...}` */
33
+ const redirectMiddleware = (req, res, next) => {
34
+ if (req.url?.startsWith('/_livestore') == false) {
35
+ return next();
36
+ }
37
+ const redirectUrl = `http://${host}:${port}/_livestore/${req.url.slice('/_livestore'.length)}`;
38
+ res.writeHead(302, { Location: redirectUrl });
39
+ res.end();
40
+ };
15
41
  const enhanceMiddleware = (metroMiddleware, server) => {
16
42
  const enhancedMiddleware = previousEnhanceMiddleware(metroMiddleware, server);
17
43
  return (req, res, next) => req.url?.startsWith('/_livestore')
18
- ? viteMiddleware(req, res, () => enhancedMiddleware(req, res, next))
44
+ ? redirectMiddleware(req, res, () => enhancedMiddleware(req, res, next))
19
45
  : enhancedMiddleware(req, res, next);
20
46
  };
21
47
  config.server.enhanceMiddleware = enhanceMiddleware;
22
48
  };
23
- const makeLiveStoreDevtoolsMiddleware = (options) => {
24
- // TODO Once Expo supports proper ESM, we can make this a static import
25
- // const viteServerPromise = makeViteServer(options)
26
- const viteMiddlewarePromise = import('@livestore/adapter-node/devtools').then(({ makeViteMiddleware }) => makeViteMiddleware({ ...options, mode: { _tag: 'expo' } }).pipe(Effect.runPromise));
27
- const middleware = async (req, res, next) => {
28
- if (req.url?.startsWith('/_livestore') == false) {
29
- return next();
30
- }
31
- const viteMiddleware = await viteMiddlewarePromise;
32
- return viteMiddleware.middlewares(req, res, next);
33
- };
34
- return middleware;
35
- };
36
49
  // eslint-disable-next-line unicorn/prefer-module
37
50
  module.exports = {
38
51
  addLiveStoreDevtoolsMiddleware,
39
- makeLiveStoreDevtoolsMiddleware,
40
52
  };
41
53
  //# sourceMappingURL=metro-config.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"metro-config.cjs","sourceRoot":"","sources":["../src/metro-config.cts"],"names":[],"mappings":";;AAEA,uJAAuJ;AACvJ,MAAM,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,yBAAyB,CAAwF,CAAA;AAM5I;;GAEG;AACH,MAAM,8BAA8B,GAAG,CAAC,MAAgC,EAAE,OAAgB,EAAE,EAAE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AAG,CAAC,GAAG,EAAE,UAAU,CAAC,aAAa,CAAC;YAChC,CAAC,CAAC,kBAAkB,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,CAAC,kBAAkB,CAAC,GAAG,EAAE,GAAG,EAAE,IAAI,CAAC,CAAC;YACxE,CAAC,CAAC,kBAAkB,CAAC,GAAG,EAAE,GAAG,EAAE,IAAI,CAAC,CAAA;IAC1C,CAAC,CAAA;IAED,MAAM,CAAC,MAAM,CAAC,iBAAiB,GAAG,iBAAiB,CAAA;AACrD,CAAC,CAAA;AAOD,iDAAiD;AACjD,MAAM,CAAC,OAAO,GAAG;IACf,8BAA8B;CAC/B,CAAA"}
@@ -1,15 +1,13 @@
1
- import type * as http from 'node:http';
2
1
  import type { MetroConfig } from 'expo/metro-config';
3
2
  import type { Options } from './types.js';
4
3
  /**
5
4
  * Patches the Metro config to add a middleware via `config.server.enhanceMiddleware`.
6
5
  */
7
6
  declare const addLiveStoreDevtoolsMiddleware: (config: MutableDeep<MetroConfig>, options: Options) => void;
8
- declare const makeLiveStoreDevtoolsMiddleware: (options: Options) => (req: http.IncomingMessage, res: http.ServerResponse, next: () => void) => Promise<void>;
9
7
  /** Remove readonly from all properties */
10
8
  type MutableDeep<T> = {
11
9
  -readonly [P in keyof T]: MutableDeep<T[P]>;
12
10
  };
13
- export type { addLiveStoreDevtoolsMiddleware, makeLiveStoreDevtoolsMiddleware };
11
+ export type { addLiveStoreDevtoolsMiddleware };
14
12
  export type { Options } from './types.js';
15
13
  //# sourceMappingURL=metro-config.d.cts.map
@@ -1 +1 @@
1
- {"version":3,"file":"metro-config.d.cts","sourceRoot":"","sources":["../src/metro-config.cts"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,IAAI,MAAM,WAAW,CAAA;AAKtC,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAA;AAEpD,OAAO,KAAK,EAAc,OAAO,EAAE,MAAM,YAAY,CAAA;AAErD;;GAEG;AACH,QAAA,MAAM,8BAA8B,GAAI,QAAQ,WAAW,CAAC,WAAW,CAAC,EAAE,SAAS,OAAO,SAuBzF,CAAA;AAED,QAAA,MAAM,+BAA+B,GAAI,SAAS,OAAO,WAOxB,IAAI,CAAC,eAAe,OAAO,IAAI,CAAC,cAAc,QAAQ,MAAM,IAAI,kBAWhG,CAAA;AAED,0CAA0C;AAC1C,KAAK,WAAW,CAAC,CAAC,IAAI;IACpB,CAAC,UAAU,CAAC,IAAI,MAAM,CAAC,GAAG,WAAW,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;CAC5C,CAAA;AAQD,YAAY,EAAE,8BAA8B,EAAE,+BAA+B,EAAE,CAAA;AAC/E,YAAY,EAAE,OAAO,EAAE,MAAM,YAAY,CAAA"}
1
+ {"version":3,"file":"metro-config.d.cts","sourceRoot":"","sources":["../src/metro-config.cts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAA;AAEpD,OAAO,KAAK,EAAc,OAAO,EAAE,MAAM,YAAY,CAAA;AAErD;;GAEG;AACH,QAAA,MAAM,8BAA8B,GAAI,QAAQ,WAAW,CAAC,WAAW,CAAC,EAAE,SAAS,OAAO,SAwDzF,CAAA;AAED,0CAA0C;AAC1C,KAAK,WAAW,CAAC,CAAC,IAAI;IACpB,CAAC,UAAU,CAAC,IAAI,MAAM,CAAC,GAAG,WAAW,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;CAC5C,CAAA;AAOD,YAAY,EAAE,8BAA8B,EAAE,CAAA;AAC9C,YAAY,EAAE,OAAO,EAAE,MAAM,YAAY,CAAA"}
package/package.json CHANGED
@@ -1,11 +1,11 @@
1
1
  {
2
2
  "name": "@livestore/devtools-expo",
3
- "version": "0.3.0-dev.36",
3
+ "version": "0.3.0-dev.38",
4
4
  "sideEffects": false,
5
5
  "types": "./dist/index.d.cts",
6
6
  "dependencies": {
7
- "@livestore/adapter-node": "0.3.0-dev.36",
8
- "@livestore/utils": "0.3.0-dev.36"
7
+ "@livestore/adapter-node": "0.3.0-dev.38",
8
+ "@livestore/utils": "0.3.0-dev.38"
9
9
  },
10
10
  "devDependencies": {
11
11
  "@types/node": "^22.13.10",
@@ -14,6 +14,7 @@
14
14
  },
15
15
  "files": [
16
16
  "package.json",
17
+ "webui",
17
18
  "src",
18
19
  "dist"
19
20
  ],
@@ -1,7 +1,8 @@
1
- import type * as http from 'node:http'
1
+ // eslint-disable-next-line @typescript-eslint/no-require-imports, unicorn/prefer-module, @typescript-eslint/consistent-type-imports, prettier/prettier
2
+ const { Effect, Logger, LogLevel } = require('@livestore/utils/effect') as typeof import('@livestore/utils/effect', { with: { "resolution-mode": "import" } })
2
3
 
3
4
  // eslint-disable-next-line @typescript-eslint/no-require-imports, unicorn/prefer-module, @typescript-eslint/consistent-type-imports, prettier/prettier
4
- const { Effect } = require('@livestore/utils/effect') as typeof import('@livestore/utils/effect', { with: { "resolution-mode": "import" } })
5
+ const { PlatformNode } = require('@livestore/utils/node') as typeof import('@livestore/utils/node', { with: { "resolution-mode": "import" } })
5
6
 
6
7
  import type { MetroConfig } from 'expo/metro-config'
7
8
 
@@ -15,46 +16,59 @@ const addLiveStoreDevtoolsMiddleware = (config: MutableDeep<MetroConfig>, option
15
16
  if (process.env.CI || !process.stdout.isTTY) {
16
17
  return
17
18
  }
18
-
19
- const viteMiddleware = makeLiveStoreDevtoolsMiddleware(options)
19
+ const host = options.host ?? 'localhost'
20
+ const port = options.port ?? 4242
21
+
22
+ // Needed for @livestore/adapter-expo
23
+ process.env.EXPO_PUBLIC_LIVESTORE_DEVTOOLS_URL = `http://${host}:${port}`
24
+
25
+ import('@livestore/adapter-node/devtools')
26
+ .then(async ({ startDevtoolsServer }) => {
27
+ startDevtoolsServer({
28
+ clientSessionInfo: undefined,
29
+ schemaPath: options.schemaPath,
30
+ host,
31
+ port,
32
+ }).pipe(
33
+ Effect.provide(PlatformNode.NodeHttpClient.layer),
34
+ Effect.provide(Logger.prettyWithThread('@livestore/devtools-expo:metro-config')),
35
+ Logger.withMinimumLogLevel(LogLevel.Debug),
36
+ Effect.tapCauseLogPretty,
37
+ Effect.runPromise,
38
+ )
39
+ })
40
+ .catch((error) => {
41
+ console.error(error)
42
+ })
20
43
 
21
44
  const previousEnhanceMiddleware = config.server.enhanceMiddleware as (
22
45
  metroMiddleware: Middleware,
23
46
  server: any,
24
47
  ) => Middleware
25
48
 
49
+ /** Redirects requests to LiveStore DevTools to `http://${host}:${port}/_livestore/${...}` */
50
+ const redirectMiddleware: Middleware = (req, res, next) => {
51
+ if (req.url?.startsWith('/_livestore') == false) {
52
+ return next()
53
+ }
54
+
55
+ const redirectUrl = `http://${host}:${port}/_livestore/${req.url!.slice('/_livestore'.length)}`
56
+ res.writeHead(302, { Location: redirectUrl })
57
+ res.end()
58
+ }
59
+
26
60
  const enhanceMiddleware = (metroMiddleware: Middleware, server: any): Middleware => {
27
61
  const enhancedMiddleware = previousEnhanceMiddleware(metroMiddleware, server)
28
62
 
29
63
  return (req, res, next) =>
30
64
  req.url?.startsWith('/_livestore')
31
- ? viteMiddleware(req, res, () => enhancedMiddleware(req, res, next))
65
+ ? redirectMiddleware(req, res, () => enhancedMiddleware(req, res, next))
32
66
  : enhancedMiddleware(req, res, next)
33
67
  }
34
68
 
35
69
  config.server.enhanceMiddleware = enhanceMiddleware
36
70
  }
37
71
 
38
- const makeLiveStoreDevtoolsMiddleware = (options: Options) => {
39
- // TODO Once Expo supports proper ESM, we can make this a static import
40
- // const viteServerPromise = makeViteServer(options)
41
- const viteMiddlewarePromise = import('@livestore/adapter-node/devtools').then(({ makeViteMiddleware }) =>
42
- makeViteMiddleware({ ...options, mode: { _tag: 'expo' } }).pipe(Effect.runPromise),
43
- )
44
-
45
- const middleware = async (req: http.IncomingMessage, res: http.ServerResponse, next: () => void) => {
46
- if (req.url?.startsWith('/_livestore') == false) {
47
- return next()
48
- }
49
-
50
- const viteMiddleware = await viteMiddlewarePromise
51
-
52
- return viteMiddleware.middlewares(req, res, next)
53
- }
54
-
55
- return middleware
56
- }
57
-
58
72
  /** Remove readonly from all properties */
59
73
  type MutableDeep<T> = {
60
74
  -readonly [P in keyof T]: MutableDeep<T[P]>
@@ -63,8 +77,7 @@ type MutableDeep<T> = {
63
77
  // eslint-disable-next-line unicorn/prefer-module
64
78
  module.exports = {
65
79
  addLiveStoreDevtoolsMiddleware,
66
- makeLiveStoreDevtoolsMiddleware,
67
80
  }
68
81
 
69
- export type { addLiveStoreDevtoolsMiddleware, makeLiveStoreDevtoolsMiddleware }
82
+ export type { addLiveStoreDevtoolsMiddleware }
70
83
  export type { Options } from './types.js'
package/src/types.d.ts CHANGED
@@ -13,4 +13,16 @@ export type Options = {
13
13
  * Example: `./src/schema.ts`
14
14
  */
15
15
  schemaPath: string
16
+ /**
17
+ * The port to listen on for the devtools server
18
+ *
19
+ * @default 4242
20
+ */
21
+ port: number
22
+ /**
23
+ * The host to listen on for the devtools server
24
+ *
25
+ * @default 'localhost'
26
+ */
27
+ host: string
16
28
  }
@@ -0,0 +1,16 @@
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>LiveStore DevTools Redirect</title>
7
+ <script>
8
+ const currentUrl = new URL(window.location.href);
9
+ const redirectUrl = `http://localhost:${currentUrl.port}/_livestore/node?autoconnect`;
10
+ window.location.href = redirectUrl;
11
+ </script>
12
+ </head>
13
+ <body>
14
+ <p>If you are not redirected, <a href="javascript:void(0);" onclick="window.location.href=redirectUrl">click here</a>.</p>
15
+ </body>
16
+ </html>