@instantdb/react 0.22.96-experimental.add-posthog-frontend.20386914944.1 → 0.22.96-experimental.add-posthog-frontend.20387039167.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.
Files changed (58) hide show
  1. package/dist/commonjs/next-ssr/HydrationStreamProvider.d.ts +66 -0
  2. package/dist/commonjs/next-ssr/HydrationStreamProvider.d.ts.map +1 -0
  3. package/dist/commonjs/next-ssr/HydrationStreamProvider.js +140 -0
  4. package/dist/commonjs/next-ssr/HydrationStreamProvider.js.map +1 -0
  5. package/dist/commonjs/next-ssr/InstantNextDatabase.d.ts +12 -0
  6. package/dist/commonjs/next-ssr/InstantNextDatabase.d.ts.map +1 -0
  7. package/dist/commonjs/next-ssr/InstantNextDatabase.js +42 -0
  8. package/dist/commonjs/next-ssr/InstantNextDatabase.js.map +1 -0
  9. package/dist/commonjs/next-ssr/InstantSuspenseProvider.d.ts +28 -0
  10. package/dist/commonjs/next-ssr/InstantSuspenseProvider.d.ts.map +1 -0
  11. package/dist/commonjs/next-ssr/InstantSuspenseProvider.js +99 -0
  12. package/dist/commonjs/next-ssr/InstantSuspenseProvider.js.map +1 -0
  13. package/dist/commonjs/next-ssr/getUserFromInstantCookie.d.ts +3 -0
  14. package/dist/commonjs/next-ssr/getUserFromInstantCookie.d.ts.map +1 -0
  15. package/dist/commonjs/next-ssr/getUserFromInstantCookie.js +21 -0
  16. package/dist/commonjs/next-ssr/getUserFromInstantCookie.js.map +1 -0
  17. package/dist/commonjs/next-ssr/htmlescape.d.ts +3 -0
  18. package/dist/commonjs/next-ssr/htmlescape.d.ts.map +1 -0
  19. package/dist/commonjs/next-ssr/htmlescape.js +25 -0
  20. package/dist/commonjs/next-ssr/htmlescape.js.map +1 -0
  21. package/dist/commonjs/next-ssr/index.d.ts +28 -0
  22. package/dist/commonjs/next-ssr/index.d.ts.map +1 -0
  23. package/dist/commonjs/next-ssr/index.js +43 -0
  24. package/dist/commonjs/next-ssr/index.js.map +1 -0
  25. package/dist/esm/next-ssr/HydrationStreamProvider.d.ts +66 -0
  26. package/dist/esm/next-ssr/HydrationStreamProvider.d.ts.map +1 -0
  27. package/dist/esm/next-ssr/HydrationStreamProvider.js +103 -0
  28. package/dist/esm/next-ssr/HydrationStreamProvider.js.map +1 -0
  29. package/dist/esm/next-ssr/InstantNextDatabase.d.ts +12 -0
  30. package/dist/esm/next-ssr/InstantNextDatabase.d.ts.map +1 -0
  31. package/dist/esm/next-ssr/InstantNextDatabase.js +35 -0
  32. package/dist/esm/next-ssr/InstantNextDatabase.js.map +1 -0
  33. package/dist/esm/next-ssr/InstantSuspenseProvider.d.ts +28 -0
  34. package/dist/esm/next-ssr/InstantSuspenseProvider.d.ts.map +1 -0
  35. package/dist/esm/next-ssr/InstantSuspenseProvider.js +94 -0
  36. package/dist/esm/next-ssr/InstantSuspenseProvider.js.map +1 -0
  37. package/dist/esm/next-ssr/getUserFromInstantCookie.d.ts +3 -0
  38. package/dist/esm/next-ssr/getUserFromInstantCookie.d.ts.map +1 -0
  39. package/dist/esm/next-ssr/getUserFromInstantCookie.js +17 -0
  40. package/dist/esm/next-ssr/getUserFromInstantCookie.js.map +1 -0
  41. package/dist/esm/next-ssr/htmlescape.d.ts +3 -0
  42. package/dist/esm/next-ssr/htmlescape.d.ts.map +1 -0
  43. package/dist/esm/next-ssr/htmlescape.js +21 -0
  44. package/dist/esm/next-ssr/htmlescape.js.map +1 -0
  45. package/dist/esm/next-ssr/index.d.ts +28 -0
  46. package/dist/esm/next-ssr/index.d.ts.map +1 -0
  47. package/dist/esm/next-ssr/index.js +32 -0
  48. package/dist/esm/next-ssr/index.js.map +1 -0
  49. package/dist/standalone/index.js +1563 -1507
  50. package/dist/standalone/index.umd.cjs +14 -14
  51. package/package.json +18 -6
  52. package/src/next-ssr/HydrationStreamProvider.tsx +200 -0
  53. package/src/next-ssr/InstantNextDatabase.tsx +56 -0
  54. package/src/next-ssr/InstantSuspenseProvider.tsx +189 -0
  55. package/src/next-ssr/getUserFromInstantCookie.ts +11 -0
  56. package/src/next-ssr/htmlescape.ts +24 -0
  57. package/src/next-ssr/index.tsx +47 -0
  58. package/tsconfig.json +1 -0
@@ -0,0 +1,66 @@
1
+ export declare const isServer: boolean;
2
+ import * as React from 'react';
3
+ interface DataTransformer {
4
+ serialize: (object: any) => any;
5
+ deserialize: (object: any) => any;
6
+ }
7
+ interface HydrationStreamContext<TShape> {
8
+ id: string;
9
+ stream: {
10
+ /**
11
+ * **Server method**
12
+ * Push a new entry to the stream
13
+ * Will be ignored on the client
14
+ */
15
+ push: (...shape: Array<TShape>) => void;
16
+ };
17
+ }
18
+ export interface HydrationStreamProviderProps<TShape> {
19
+ children: React.ReactNode;
20
+ /**
21
+ * Optional transformer to serialize/deserialize the data
22
+ * Example devalue, superjson et al
23
+ */
24
+ transformer?: DataTransformer;
25
+ /**
26
+ * **Client method**
27
+ * Called in the browser when new entries are received
28
+ */
29
+ onEntries: (entries: Array<TShape>) => void;
30
+ /**
31
+ * **Server method**
32
+ * onFlush is called on the server when the cache is flushed
33
+ */
34
+ onFlush?: () => Array<TShape>;
35
+ /**
36
+ * A nonce that'll allow the inline script to be executed when Content Security Policy is enforced
37
+ */
38
+ nonce?: string;
39
+ }
40
+ export declare function createHydrationStreamProvider<TShape>(): {
41
+ Provider: (props: {
42
+ children: React.ReactNode;
43
+ /**
44
+ * Optional transformer to serialize/deserialize the data
45
+ * Example devalue, superjson et al
46
+ */
47
+ transformer?: DataTransformer;
48
+ /**
49
+ * **Client method**
50
+ * Called in the browser when new entries are received
51
+ */
52
+ onEntries: (entries: Array<TShape>) => void;
53
+ /**
54
+ * **Server method**
55
+ * onFlush is called on the server when the cache is flushed
56
+ */
57
+ onFlush?: () => Array<TShape>;
58
+ /**
59
+ * A nonce that'll allow the inline script to be executed when Content Security Policy is enforced
60
+ */
61
+ nonce?: string;
62
+ }) => import("react/jsx-runtime").JSX.Element;
63
+ context: React.Context<HydrationStreamContext<TShape>>;
64
+ };
65
+ export {};
66
+ //# sourceMappingURL=HydrationStreamProvider.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"HydrationStreamProvider.d.ts","sourceRoot":"","sources":["../../../src/next-ssr/HydrationStreamProvider.tsx"],"names":[],"mappings":"AAQA,eAAO,MAAM,QAAQ,SAAwD,CAAC;AAI9E,OAAO,KAAK,KAAK,MAAM,OAAO,CAAC;AAK/B,UAAU,eAAe;IACvB,SAAS,EAAE,CAAC,MAAM,EAAE,GAAG,KAAK,GAAG,CAAC;IAChC,WAAW,EAAE,CAAC,MAAM,EAAE,GAAG,KAAK,GAAG,CAAC;CACnC;AAWD,UAAU,sBAAsB,CAAC,MAAM;IACrC,EAAE,EAAE,MAAM,CAAC;IACX,MAAM,EAAE;QACN;;;;WAIG;QACH,IAAI,EAAE,CAAC,GAAG,KAAK,EAAE,KAAK,CAAC,MAAM,CAAC,KAAK,IAAI,CAAC;KACzC,CAAC;CACH;AAED,MAAM,WAAW,4BAA4B,CAAC,MAAM;IAClD,QAAQ,EAAE,KAAK,CAAC,SAAS,CAAC;IAC1B;;;OAGG;IACH,WAAW,CAAC,EAAE,eAAe,CAAC;IAC9B;;;OAGG;IACH,SAAS,EAAE,CAAC,OAAO,EAAE,KAAK,CAAC,MAAM,CAAC,KAAK,IAAI,CAAC;IAC5C;;;OAGG;IACH,OAAO,CAAC,EAAE,MAAM,KAAK,CAAC,MAAM,CAAC,CAAC;IAC9B;;OAEG;IACH,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB;AAED,wBAAgB,6BAA6B,CAAC,MAAM;sBAaD;QAC/C,QAAQ,EAAE,KAAK,CAAC,SAAS,CAAC;QAC1B;;;WAGG;QACH,WAAW,CAAC,EAAE,eAAe,CAAC;QAC9B;;;WAGG;QACH,SAAS,EAAE,CAAC,OAAO,EAAE,KAAK,CAAC,MAAM,CAAC,KAAK,IAAI,CAAC;QAC5C;;;WAGG;QACH,OAAO,CAAC,EAAE,MAAM,KAAK,CAAC,MAAM,CAAC,CAAC;QAC9B;;WAEG;QACH,KAAK,CAAC,EAAE,MAAM,CAAC;KAChB;;EAmGF"}
@@ -0,0 +1,103 @@
1
+ 'use client';
2
+ import { jsx as _jsx } from "react/jsx-runtime";
3
+ // This allows us to send serialized json from the server to the client using 'useServerInsertedHTML'
4
+ // We are allowed to send whatever json we want, however it must be triggered at the same time as the client is recieving new html to display
5
+ // after a suspsense boundary resolves
6
+ // We do this to send a list of triples and attrs for each query that the server made so they can be pre-filled on the client render.
7
+ // useServerInsertedHTML is called whenever a suspense boundary resolves, resulting in new html being streamed down to the client.
8
+ export const isServer = typeof window === 'undefined' || 'Deno' in globalThis;
9
+ import { useServerInsertedHTML } from 'next/navigation.js';
10
+ import * as React from 'react';
11
+ import { htmlEscapeJsonString } from './htmlescape.js';
12
+ const serializedSymbol = Symbol('serialized');
13
+ export function createHydrationStreamProvider() {
14
+ const context = React.createContext(null);
15
+ /**
16
+
17
+ * 1. (Happens on server): `useServerInsertedHTML()` is called **on the server** whenever a `Suspense`-boundary completes
18
+ * - This means that we might have some new entries in the cache that needs to be flushed
19
+ * - We pass these to the client by inserting a `<script>`-tag where we do `window[id].push(serializedVersionOfCache)`
20
+ * 2. (Happens in browser) In `useEffect()`:
21
+ * - We check if `window[id]` is set to an array and call `push()` on all the entries which will call `onEntries()` with the new entries
22
+ * - We replace `window[id]` with a `push()`-method that will be called whenever new entries are received
23
+ **/
24
+ function UseClientHydrationStreamProvider(props) {
25
+ var _a, _b;
26
+ // unique id for the cache provider
27
+ const id = `__RQ${React.useId()}`;
28
+ const idJSON = htmlEscapeJsonString(JSON.stringify(id));
29
+ const [transformer] = React.useState(() => {
30
+ var _a;
31
+ return ((_a = props.transformer) !== null && _a !== void 0 ? _a : {
32
+ // noop
33
+ serialize: (obj) => obj,
34
+ deserialize: (obj) => obj,
35
+ });
36
+ });
37
+ // <server stuff>
38
+ const [stream] = React.useState(() => {
39
+ if (!isServer) {
40
+ return {
41
+ push() {
42
+ // no-op on the client
43
+ },
44
+ };
45
+ }
46
+ return [];
47
+ });
48
+ const count = React.useRef(0);
49
+ useServerInsertedHTML(() => {
50
+ var _a, _b;
51
+ // This only happens on the server
52
+ stream.push(...((_b = (_a = props.onFlush) === null || _a === void 0 ? void 0 : _a.call(props)) !== null && _b !== void 0 ? _b : []));
53
+ if (!stream.length) {
54
+ return null;
55
+ }
56
+ // console.log(`pushing ${stream.length} entries`)
57
+ const serializedCacheArgs = stream
58
+ .map((entry) => transformer.serialize(entry))
59
+ .map((entry) => JSON.stringify(entry))
60
+ .join(',');
61
+ // Flush stream
62
+ // eslint-disable-next-line react-hooks/immutability
63
+ stream.length = 0;
64
+ const html = [
65
+ `window[${idJSON}] = window[${idJSON}] || [];`,
66
+ `window[${idJSON}].push(${htmlEscapeJsonString(serializedCacheArgs)});`,
67
+ ];
68
+ return (_jsx("script", { nonce: props.nonce, dangerouslySetInnerHTML: {
69
+ __html: html.join(''),
70
+ } }, count.current++));
71
+ });
72
+ // </server stuff>
73
+ // <client stuff>
74
+ // Setup and run the onEntries handler on the client only, but do it during
75
+ // the initial render so children have access to the data immediately
76
+ // This is important to avoid the client suspending during the initial render
77
+ // if the data has not yet been hydrated.
78
+ if (!isServer) {
79
+ const win = window;
80
+ if (!((_a = win[id]) === null || _a === void 0 ? void 0 : _a.initialized)) {
81
+ // Client: consume cache:
82
+ const onEntries = (...serializedEntries) => {
83
+ const entries = serializedEntries.map((serialized) => transformer.deserialize(serialized));
84
+ props.onEntries(entries);
85
+ };
86
+ const winStream = (_b = win[id]) !== null && _b !== void 0 ? _b : [];
87
+ onEntries(...winStream);
88
+ // eslint-disable-next-line react-hooks/immutability
89
+ win[id] = {
90
+ initialized: true,
91
+ push: onEntries,
92
+ };
93
+ }
94
+ }
95
+ // </client stuff>
96
+ return (_jsx(context.Provider, { value: { stream, id }, children: props.children }));
97
+ }
98
+ return {
99
+ Provider: UseClientHydrationStreamProvider,
100
+ context,
101
+ };
102
+ }
103
+ //# sourceMappingURL=HydrationStreamProvider.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"HydrationStreamProvider.js","sourceRoot":"","sources":["../../../src/next-ssr/HydrationStreamProvider.tsx"],"names":[],"mappings":"AAAA,YAAY,CAAC;;AAEb,qGAAqG;AACrG,6IAA6I;AAC7I,sCAAsC;AACtC,qIAAqI;AACrI,kIAAkI;AAElI,MAAM,CAAC,MAAM,QAAQ,GAAG,OAAO,MAAM,KAAK,WAAW,IAAI,MAAM,IAAI,UAAU,CAAC;AAE9E,OAAO,EAAE,qBAAqB,EAAE,MAAM,oBAAoB,CAAC;AAE3D,OAAO,KAAK,KAAK,MAAM,OAAO,CAAC;AAC/B,OAAO,EAAE,oBAAoB,EAAE,MAAM,iBAAiB,CAAC;AAEvD,MAAM,gBAAgB,GAAG,MAAM,CAAC,YAAY,CAAC,CAAC;AAmD9C,MAAM,UAAU,6BAA6B;IAC3C,MAAM,OAAO,GAAG,KAAK,CAAC,aAAa,CACjC,IAAW,CACZ,CAAC;IACF;;;;;;;;QAQI;IACJ,SAAS,gCAAgC,CAAC,KAqBzC;;QACC,mCAAmC;QACnC,MAAM,EAAE,GAAG,OAAO,KAAK,CAAC,KAAK,EAAE,EAAE,CAAC;QAClC,MAAM,MAAM,GAAG,oBAAoB,CAAC,IAAI,CAAC,SAAS,CAAC,EAAE,CAAC,CAAC,CAAC;QAExD,MAAM,CAAC,WAAW,CAAC,GAAG,KAAK,CAAC,QAAQ,CAClC,GAAG,EAAE;;YACH,OAAA,CAAC,MAAA,KAAK,CAAC,WAAW,mCAAI;gBACpB,OAAO;gBACP,SAAS,EAAE,CAAC,GAAQ,EAAE,EAAE,CAAC,GAAG;gBAC5B,WAAW,EAAE,CAAC,GAAQ,EAAE,EAAE,CAAC,GAAG;aAC/B,CAA4C,CAAA;SAAA,CAChD,CAAC;QAEF,iBAAiB;QACjB,MAAM,CAAC,MAAM,CAAC,GAAG,KAAK,CAAC,QAAQ,CAAgB,GAAG,EAAE;YAClD,IAAI,CAAC,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AI,CAAC,QAAQ,EAAE,CAAC;YACd,MAAM,GAAG,GAAG,MAAa,CAAC;YAC1B,IAAI,CAAC,CAAA,MAAA,GAAG,CAAC,EAAE,CAAC,0CAAE,WAAW,CAAA,EAAE,CAAC;gBAC1B,yBAAyB;gBACzB,MAAM,SAAS,GAAG,CAAC,GAAG,iBAA4C,EAAE,EAAE;oBACpE,MAAM,OAAO,GAAG,iBAAiB,CAAC,GAAG,CAAC,CAAC,UAAU,EAAE,EAAE,CACnD,WAAW,CAAC,WAAW,CAAC,UAAU,CAAC,CACpC,CAAC;oBACF,KAAK,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC;gBAC3B,CAAC,CAAC;gBAEF,MAAM,SAAS,GAA8B,MAAA,GAAG,CAAC,EAAE,CAAC,mCAAI,EAAE,CAAC;gBAE3D,SAAS,CAAC,GAAG,SAAS,CAAC,CAAC;gBAExB,oDAAoD;gBACpD,GAAG,CAAC,EAAE,CAAC,GAAG;oBACR,WAAW,EAAE,IAAI;oBACjB,IAAI,EAAE,SAAS;iBAChB,CAAC;YACJ,CAAC;QACH,CAAC;QACD,kBAAkB;QAElB,OAAO,CACL,KAAC,OAAO,CAAC,QAAQ,IAAC,KAAK,EAAE,EAAE,MAAM,EAAE,EAAE,EAAE,YACpC,KAAK,CAAC,QAAQ,GACE,CACpB,CAAC;IACJ,CAAC;IAED,OAAO;QACL,QAAQ,EAAE,gCAAgC;QAC1C,OAAO;KACR,CAAC;AACJ,CAAC","sourcesContent":["'use client';\n\n// This allows us to send serialized json from the server to the client using 'useServerInsertedHTML'\n// We are allowed to send whatever json we want, however it must be triggered at the same time as the client is recieving new html to display\n// after a suspsense boundary resolves\n// We do this to send a list of triples and attrs for each query that the server made so they can be pre-filled on the client render.\n// useServerInsertedHTML is called whenever a suspense boundary resolves, resulting in new html being streamed down to the client.\n\nexport const isServer = typeof window === 'undefined' || 'Deno' in globalThis;\n\nimport { useServerInsertedHTML } from 'next/navigation.js';\n\nimport * as React from 'react';\nimport { htmlEscapeJsonString } from './htmlescape.js';\n\nconst serializedSymbol = Symbol('serialized');\n\ninterface DataTransformer {\n serialize: (object: any) => any;\n deserialize: (object: any) => any;\n}\n\ntype Serialized<TData> = unknown & {\n [serializedSymbol]: TData;\n};\n\ninterface TypedDataTransformer<TData> {\n serialize: (obj: TData) => Serialized<TData>;\n deserialize: (obj: Serialized<TData>) => TData;\n}\n\ninterface HydrationStreamContext<TShape> {\n id: string;\n stream: {\n /**\n * **Server method**\n * Push a new entry to the stream\n * Will be ignored on the client\n */\n push: (...shape: Array<TShape>) => void;\n };\n}\n\nexport interface HydrationStreamProviderProps<TShape> {\n children: React.ReactNode;\n /**\n * Optional transformer to serialize/deserialize the data\n * Example devalue, superjson et al\n */\n transformer?: DataTransformer;\n /**\n * **Client method**\n * Called in the browser when new entries are received\n */\n onEntries: (entries: Array<TShape>) => void;\n /**\n * **Server method**\n * onFlush is called on the server when the cache is flushed\n */\n onFlush?: () => Array<TShape>;\n /**\n * A nonce that'll allow the inline script to be executed when Content Security Policy is enforced\n */\n nonce?: string;\n}\n\nexport function createHydrationStreamProvider<TShape>() {\n const context = React.createContext<HydrationStreamContext<TShape>>(\n null as any,\n );\n /**\n\n * 1. (Happens on server): `useServerInsertedHTML()` is called **on the server** whenever a `Suspense`-boundary completes\n * - This means that we might have some new entries in the cache that needs to be flushed\n * - We pass these to the client by inserting a `<script>`-tag where we do `window[id].push(serializedVersionOfCache)`\n * 2. (Happens in browser) In `useEffect()`:\n * - We check if `window[id]` is set to an array and call `push()` on all the entries which will call `onEntries()` with the new entries\n * - We replace `window[id]` with a `push()`-method that will be called whenever new entries are received\n **/\n function UseClientHydrationStreamProvider(props: {\n children: React.ReactNode;\n /**\n * Optional transformer to serialize/deserialize the data\n * Example devalue, superjson et al\n */\n transformer?: DataTransformer;\n /**\n * **Client method**\n * Called in the browser when new entries are received\n */\n onEntries: (entries: Array<TShape>) => void;\n /**\n * **Server method**\n * onFlush is called on the server when the cache is flushed\n */\n onFlush?: () => Array<TShape>;\n /**\n * A nonce that'll allow the inline script to be executed when Content Security Policy is enforced\n */\n nonce?: string;\n }) {\n // unique id for the cache provider\n const id = `__RQ${React.useId()}`;\n const idJSON = htmlEscapeJsonString(JSON.stringify(id));\n\n const [transformer] = React.useState(\n () =>\n (props.transformer ?? {\n // noop\n serialize: (obj: any) => obj,\n deserialize: (obj: any) => obj,\n }) as unknown as TypedDataTransformer<TShape>,\n );\n\n // <server stuff>\n const [stream] = React.useState<Array<TShape>>(() => {\n if (!isServer) {\n return {\n push() {\n // no-op on the client\n },\n } as unknown as Array<TShape>;\n }\n return [];\n });\n const count = React.useRef(0);\n useServerInsertedHTML(() => {\n // This only happens on the server\n stream.push(...(props.onFlush?.() ?? []));\n\n if (!stream.length) {\n return null;\n }\n // console.log(`pushing ${stream.length} entries`)\n const serializedCacheArgs = stream\n .map((entry) => transformer.serialize(entry))\n .map((entry) => JSON.stringify(entry))\n .join(',');\n\n // Flush stream\n // eslint-disable-next-line react-hooks/immutability\n stream.length = 0;\n\n const html: Array<string> = [\n `window[${idJSON}] = window[${idJSON}] || [];`,\n `window[${idJSON}].push(${htmlEscapeJsonString(serializedCacheArgs)});`,\n ];\n return (\n <script\n key={count.current++}\n nonce={props.nonce}\n dangerouslySetInnerHTML={{\n __html: html.join(''),\n }}\n />\n );\n });\n // </server stuff>\n\n // <client stuff>\n // Setup and run the onEntries handler on the client only, but do it during\n // the initial render so children have access to the data immediately\n // This is important to avoid the client suspending during the initial render\n // if the data has not yet been hydrated.\n if (!isServer) {\n const win = window as any;\n if (!win[id]?.initialized) {\n // Client: consume cache:\n const onEntries = (...serializedEntries: Array<Serialized<TShape>>) => {\n const entries = serializedEntries.map((serialized) =>\n transformer.deserialize(serialized),\n );\n props.onEntries(entries);\n };\n\n const winStream: Array<Serialized<TShape>> = win[id] ?? [];\n\n onEntries(...winStream);\n\n // eslint-disable-next-line react-hooks/immutability\n win[id] = {\n initialized: true,\n push: onEntries,\n };\n }\n }\n // </client stuff>\n\n return (\n <context.Provider value={{ stream, id }}>\n {props.children}\n </context.Provider>\n );\n }\n\n return {\n Provider: UseClientHydrationStreamProvider,\n context,\n };\n}\n"]}
@@ -0,0 +1,12 @@
1
+ import { AuthState, InstantSchemaDef, InstaQLResponse, PageInfoResponse, RuleParams, ValidQuery } from '@instantdb/core';
2
+ import InstantReactWebDatabase from '../InstantReactWebDatabase.ts';
3
+ export declare class InstantNextDatabase<Schema extends InstantSchemaDef<any, any, any>, UseDates extends boolean> extends InstantReactWebDatabase<Schema, UseDates> {
4
+ useSuspenseQuery: <Q extends ValidQuery<Q, Schema>>(q: Q, opts?: {
5
+ ruleParams: RuleParams;
6
+ }) => {
7
+ data: InstaQLResponse<Schema, Q, NonNullable<UseDates>>;
8
+ pageInfo?: PageInfoResponse<Q>;
9
+ };
10
+ useAuth: () => AuthState;
11
+ }
12
+ //# sourceMappingURL=InstantNextDatabase.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"InstantNextDatabase.d.ts","sourceRoot":"","sources":["../../../src/next-ssr/InstantNextDatabase.tsx"],"names":[],"mappings":"AAAA,OAAO,EACL,SAAS,EACT,gBAAgB,EAChB,eAAe,EACf,gBAAgB,EAChB,UAAU,EACV,UAAU,EACX,MAAM,iBAAiB,CAAC;AACzB,OAAO,uBAAuB,MAAM,+BAA+B,CAAC;AAIpE,qBAAa,mBAAmB,CAC9B,MAAM,SAAS,gBAAgB,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,CAAC,EAC9C,QAAQ,SAAS,OAAO,CACxB,SAAQ,uBAAuB,CAAC,MAAM,EAAE,QAAQ,CAAC;IAC1C,gBAAgB,GAAI,CAAC,SAAS,UAAU,CAAC,CAAC,EAAE,MAAM,CAAC,EACxD,GAAG,CAAC,EACJ,OAAO;QACL,UAAU,EAAE,UAAU,CAAC;KACxB,KACA;QACD,IAAI,EAAE,eAAe,CAAC,MAAM,EAAE,CAAC,EAAE,WAAW,CAAC,QAAQ,CAAC,CAAC,CAAC;QACxD,QAAQ,CAAC,EAAE,gBAAgB,CAAC,CAAC,CAAC,CAAC;KAChC,CAQC;IAEF,OAAO,QAAO,SAAS,CAoBrB;CACH"}
@@ -0,0 +1,35 @@
1
+ import InstantReactWebDatabase from "../InstantReactWebDatabase.js";
2
+ import { useContext } from 'react';
3
+ import { SuspsenseQueryContext } from "./InstantSuspenseProvider.js";
4
+ export class InstantNextDatabase extends InstantReactWebDatabase {
5
+ constructor() {
6
+ super(...arguments);
7
+ this.useSuspenseQuery = (q, opts) => {
8
+ const ctx = useContext(SuspsenseQueryContext);
9
+ if (!ctx) {
10
+ throw new Error('useSuspenseQuery must be used within a SuspenseQueryProvider');
11
+ }
12
+ return ctx.useSuspenseQuery(q, opts);
13
+ };
14
+ this.useAuth = () => {
15
+ const ctx = useContext(SuspsenseQueryContext);
16
+ const realAuthResult = this._useAuth();
17
+ if (!ctx) {
18
+ return realAuthResult;
19
+ }
20
+ const { ssrUser } = ctx;
21
+ if (ssrUser === undefined) {
22
+ return realAuthResult;
23
+ }
24
+ if (realAuthResult.isLoading) {
25
+ return {
26
+ error: undefined,
27
+ isLoading: false,
28
+ user: ssrUser !== null && ssrUser !== void 0 ? ssrUser : undefined, // null -> undefined for the response
29
+ };
30
+ }
31
+ return realAuthResult;
32
+ };
33
+ }
34
+ }
35
+ //# sourceMappingURL=InstantNextDatabase.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"InstantNextDatabase.js","sourceRoot":"","sources":["../../../src/next-ssr/InstantNextDatabase.tsx"],"names":[],"mappings":"AAQA,OAAO,uBAAuB,MAAM,+BAA+B,CAAC;AACpE,OAAO,EAAE,UAAU,EAAE,MAAM,OAAO,CAAC;AACnC,OAAO,EAAE,qBAAqB,EAAE,MAAM,8BAA+B,CAAC;AAEtE,MAAM,OAAO,mBAGX,SAAQ,uBAAyC;IAHnD;;QAIS,qBAAgB,GAAG,CACxB,CAAI,EACJ,IAEC,EAID,EAAE;YACF,MAAM,GAAG,GAAG,UAAU,CAAC,qBAAqB,CAAC,CAAC;YAC9C,IAAI,CAAC,GAAG,EAAE,CAAC;gBACT,MAAM,IAAI,KAAK,CACb,8DAA8D,CAC/D,CAAC;YACJ,CAAC;YACD,OAAO,GAAG,CAAC,gBAAgB,CAAC,CAAC,EAAE,IAAI,CAAQ,CAAC;QAC9C,CAAC,CAAC;QAEF,YAAO,GAAG,GAAc,EAAE;YACxB,MAAM,GAAG,GAAG,UAAU,CAAC,qBAAqB,CAAC,CAAC;YAC9C,MAAM,cAAc,GAAG,IAAI,CAAC,QAAQ,EAAE,CAAC;YACvC,IAAI,CAAC,GAAG,EAAE,CAAC;gBACT,OAAO,cAAc,CAAC;YACxB,CAAC;YAED,MAAM,EAAE,OAAO,EAAE,GAAG,GAAG,CAAC;YACxB,IAAI,OAAO,KAAK,SAAS,EAAE,CAAC;gBAC1B,OAAO,cAAc,CAAC;YACxB,CAAC;YACD,IAAI,cAAc,CAAC,SAAS,EAAE,CAAC;gBAC7B,OAAO;oBACL,KAAK,EAAE,SAAS;oBAChB,SAAS,EAAE,KAAK;oBAChB,IAAI,EAAE,OAAO,aAAP,OAAO,cAAP,OAAO,GAAI,SAAS,EAAE,qCAAqC;iBAClE,CAAC;YACJ,CAAC;YAED,OAAO,cAAc,CAAC;QACxB,CAAC,CAAC;IACJ,CAAC;CAAA","sourcesContent":["import {\n AuthState,\n InstantSchemaDef,\n InstaQLResponse,\n PageInfoResponse,\n RuleParams,\n ValidQuery,\n} from '@instantdb/core';\nimport InstantReactWebDatabase from '../InstantReactWebDatabase.ts';\nimport { useContext } from 'react';\nimport { SuspsenseQueryContext } from './InstantSuspenseProvider.tsx';\n\nexport class InstantNextDatabase<\n Schema extends InstantSchemaDef<any, any, any>,\n UseDates extends boolean,\n> extends InstantReactWebDatabase<Schema, UseDates> {\n public useSuspenseQuery = <Q extends ValidQuery<Q, Schema>>(\n q: Q,\n opts?: {\n ruleParams: RuleParams;\n },\n ): {\n data: InstaQLResponse<Schema, Q, NonNullable<UseDates>>;\n pageInfo?: PageInfoResponse<Q>;\n } => {\n const ctx = useContext(SuspsenseQueryContext);\n if (!ctx) {\n throw new Error(\n 'useSuspenseQuery must be used within a SuspenseQueryProvider',\n );\n }\n return ctx.useSuspenseQuery(q, opts) as any;\n };\n\n useAuth = (): AuthState => {\n const ctx = useContext(SuspsenseQueryContext);\n const realAuthResult = this._useAuth();\n if (!ctx) {\n return realAuthResult;\n }\n\n const { ssrUser } = ctx;\n if (ssrUser === undefined) {\n return realAuthResult;\n }\n if (realAuthResult.isLoading) {\n return {\n error: undefined,\n isLoading: false,\n user: ssrUser ?? undefined, // null -> undefined for the response\n };\n }\n\n return realAuthResult;\n };\n}\n"]}
@@ -0,0 +1,28 @@
1
+ import { InstantConfig, InstantSchemaDef, InstaQLResponse, PageInfoResponse, RuleParams, User, ValidQuery } from '@instantdb/core';
2
+ import InstantReactWebDatabase from '../InstantReactWebDatabase.ts';
3
+ type InstantSuspenseProviderProps<Schema extends InstantSchemaDef<any, any, any>> = {
4
+ nonce?: string;
5
+ children: React.ReactNode;
6
+ db?: InstantReactWebDatabase<Schema, any>;
7
+ config?: Omit<InstantConfig<any, any>, 'schema'> & {
8
+ schema: string;
9
+ };
10
+ user?: User | null;
11
+ };
12
+ type SuspenseQueryContextValue = {
13
+ useSuspenseQuery: (query: any, opts?: SuspenseQueryOpts) => any;
14
+ ssrUser: User | null | undefined;
15
+ };
16
+ export declare const SuspsenseQueryContext: import("react").Context<SuspenseQueryContextValue | null>;
17
+ export declare const createUseSuspenseQuery: <Schema extends InstantSchemaDef<any, any, any>, UseDates extends boolean>(_db: InstantReactWebDatabase<Schema, UseDates>) => (<Q extends ValidQuery<Q, Schema>>(q: Q, opts?: {
18
+ ruleParams: RuleParams;
19
+ }) => {
20
+ data: InstaQLResponse<Schema, Q, NonNullable<UseDates>>;
21
+ pageInfo?: PageInfoResponse<Q>;
22
+ });
23
+ type SuspenseQueryOpts = {
24
+ ruleParams: RuleParams;
25
+ };
26
+ export declare const InstantSuspenseProvider: (props: InstantSuspenseProviderProps<any>) => import("react/jsx-runtime").JSX.Element;
27
+ export {};
28
+ //# sourceMappingURL=InstantSuspenseProvider.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"InstantSuspenseProvider.d.ts","sourceRoot":"","sources":["../../../src/next-ssr/InstantSuspenseProvider.tsx"],"names":[],"mappings":"AAGA,OAAO,EAEL,aAAa,EACb,gBAAgB,EAChB,eAAe,EACf,gBAAgB,EAChB,UAAU,EACV,IAAI,EACJ,UAAU,EACX,MAAM,iBAAiB,CAAC;AACzB,OAAO,uBAAuB,MAAM,+BAA+B,CAAC;AAQpE,KAAK,4BAA4B,CAC/B,MAAM,SAAS,gBAAgB,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,CAAC,IAC5C;IACF,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,QAAQ,EAAE,KAAK,CAAC,SAAS,CAAC;IAC1B,EAAE,CAAC,EAAE,uBAAuB,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IAC1C,MAAM,CAAC,EAAE,IAAI,CAAC,aAAa,CAAC,GAAG,EAAE,GAAG,CAAC,EAAE,QAAQ,CAAC,GAAG;QACjD,MAAM,EAAE,MAAM,CAAC;KAChB,CAAC;IACF,IAAI,CAAC,EAAE,IAAI,GAAG,IAAI,CAAC;CACpB,CAAC;AAIF,KAAK,yBAAyB,GAAG;IAC/B,gBAAgB,EAAE,CAAC,KAAK,EAAE,GAAG,EAAE,IAAI,CAAC,EAAE,iBAAiB,KAAK,GAAG,CAAC;IAChE,OAAO,EAAE,IAAI,GAAG,IAAI,GAAG,SAAS,CAAC;CAClC,CAAC;AAEF,eAAO,MAAM,qBAAqB,2DACqB,CAAC;AAGxD,eAAO,MAAM,sBAAsB,GACjC,MAAM,SAAS,gBAAgB,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,CAAC,EAC9C,QAAQ,SAAS,OAAO,EAExB,KAAK,uBAAuB,CAAC,MAAM,EAAE,QAAQ,CAAC,KAC7C,CAAC,CAAC,CAAC,SAAS,UAAU,CAAC,CAAC,EAAE,MAAM,CAAC,EAClC,CAAC,EAAE,CAAC,EACJ,IAAI,CAAC,EAAE;IACL,UAAU,EAAE,UAAU,CAAC;CACxB,KACE;IACH,IAAI,EAAE,eAAe,CAAC,MAAM,EAAE,CAAC,EAAE,WAAW,CAAC,QAAQ,CAAC,CAAC,CAAC;IACxD,QAAQ,CAAC,EAAE,gBAAgB,CAAC,CAAC,CAAC,CAAC;CAChC,CAUA,CAAC;AAEF,KAAK,iBAAiB,GAAG;IACvB,UAAU,EAAE,UAAU,CAAC;CACxB,CAAC;AAEF,eAAO,MAAM,uBAAuB,GAClC,OAAO,4BAA4B,CAAC,GAAG,CAAC,4CAkHzC,CAAC"}
@@ -0,0 +1,94 @@
1
+ 'use client';
2
+ import { jsx as _jsx } from "react/jsx-runtime";
3
+ // InstantSuspenseProvider can only be used in a client context so this prevents errors from trying to use it in a server component.
4
+ import { FrameworkClient, } from '@instantdb/core';
5
+ import { createHydrationStreamProvider, isServer, } from "./HydrationStreamProvider.js";
6
+ import { createContext, useContext, useRef, useState } from 'react';
7
+ const stream = createHydrationStreamProvider();
8
+ export const SuspsenseQueryContext = createContext(null);
9
+ // Creates a typed useSuspense hook
10
+ export const createUseSuspenseQuery = (_db) => {
11
+ return (q, opts) => {
12
+ const ctx = useContext(SuspsenseQueryContext);
13
+ if (!ctx) {
14
+ throw new Error('useSuspenseQuery must be used within a SuspenseQueryProvider');
15
+ }
16
+ return ctx.useSuspenseQuery(q, opts);
17
+ };
18
+ };
19
+ export const InstantSuspenseProvider = (props) => {
20
+ var _a;
21
+ const clientRef = useRef(null);
22
+ if (!props.db) {
23
+ throw new Error('Must provide either a db or config to InstantSuspenseProvider');
24
+ }
25
+ const db = useRef(props.db);
26
+ const [trackedKeys] = useState(() => new Set());
27
+ if (!clientRef.current) {
28
+ if (props.user && !props.user.refresh_token) {
29
+ throw new Error('User must have a refresh_token field. Recieved: ' +
30
+ JSON.stringify(props.user, null, 2));
31
+ }
32
+ clientRef.current = new FrameworkClient({
33
+ token: (_a = props.user) === null || _a === void 0 ? void 0 : _a.refresh_token,
34
+ db: db.current.core,
35
+ });
36
+ }
37
+ if (isServer) {
38
+ clientRef.current.subscribe((result) => {
39
+ const { queryHash } = result;
40
+ trackedKeys.add(queryHash);
41
+ });
42
+ }
43
+ const useSuspenseQuery = (query, opts) => {
44
+ const nonSuspenseResult = db.current.useQuery(query, Object.assign({}, opts));
45
+ if (nonSuspenseResult.data) {
46
+ return {
47
+ data: nonSuspenseResult.data,
48
+ pageInfo: nonSuspenseResult.pageInfo,
49
+ };
50
+ }
51
+ // should never happen (typeguard)
52
+ if (!clientRef.current) {
53
+ throw new Error('Client ref not set up');
54
+ }
55
+ let entry = clientRef.current.getExistingResultForQuery(query, {
56
+ ruleParams: opts === null || opts === void 0 ? void 0 : opts.ruleParams,
57
+ });
58
+ if (!entry) {
59
+ entry = clientRef.current.query(query, opts);
60
+ }
61
+ if (entry.status === 'pending') {
62
+ throw entry.promise;
63
+ }
64
+ if (entry.status === 'error') {
65
+ throw entry.error;
66
+ }
67
+ if (entry.status === 'success' && entry.type === 'session') {
68
+ return entry.data;
69
+ }
70
+ if (entry.status === 'success') {
71
+ const data = entry.data;
72
+ const result = clientRef.current.completeIsomorphic(query, data.triples, data.attrs, data.pageInfo);
73
+ return result;
74
+ }
75
+ };
76
+ return (_jsx(SuspsenseQueryContext.Provider, { value: { useSuspenseQuery, ssrUser: props.user }, children: _jsx(stream.Provider, { nonce: props.nonce, onFlush: () => {
77
+ const toSend = [];
78
+ for (const [key, value] of clientRef.current.resultMap.entries()) {
79
+ if (trackedKeys.has(key) && value.status === 'success') {
80
+ toSend.push({
81
+ queryKey: key,
82
+ value: value.data,
83
+ });
84
+ }
85
+ }
86
+ trackedKeys.clear();
87
+ return toSend;
88
+ }, onEntries: (entries) => {
89
+ entries.forEach((entry) => {
90
+ clientRef.current.addQueryResult(entry.queryKey, entry.value);
91
+ });
92
+ }, children: props.children }) }));
93
+ };
94
+ //# sourceMappingURL=InstantSuspenseProvider.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"InstantSuspenseProvider.js","sourceRoot":"","sources":["../../../src/next-ssr/InstantSuspenseProvider.tsx"],"names":[],"mappings":"AAAA,YAAY,CAAC;;AACb,oIAAoI;AAEpI,OAAO,EACL,eAAe,GAQhB,MAAM,iBAAiB,CAAC;AAEzB,OAAO,EACL,6BAA6B,EAC7B,QAAQ,GACT,MAAM,8BAA+B,CAAC;AACvC,OAAO,EAAE,aAAa,EAAE,UAAU,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AAepE,MAAM,MAAM,GAAG,6BAA6B,EAAO,CAAC;AAOpD,MAAM,CAAC,MAAM,qBAAqB,GAChC,aAAa,CAAmC,IAAI,CAAC,CAAC;AAExD,mCAAmC;AACnC,MAAM,CAAC,MAAM,sBAAsB,GAAG,CAIpC,GAA8C,EAS7C,EAAE;IACH,OAAO,CAAkC,CAAM,EAAE,IAAS,EAAE,EAAE;QAC5D,MAAM,GAAG,GAAG,UAAU,CAAC,qBAAqB,CAAC,CAAC;QAC9C,IAAI,CAAC,GAAG,EAAE,CAAC;YACT,MAAM,IAAI,KAAK,CACb,8DAA8D,CAC/D,CAAC;QACJ,CAAC;QACD,OAAO,GAAG,CAAC,gBAAgB,CAAC,CAAC,EAAE,IAAI,CAAQ,CAAC;IAC9C,CAAC,CAAC;AACJ,CAAC,CAAC;AAMF,MAAM,CAAC,MAAM,uBAAuB,GAAG,CACrC,KAAwC,EACxC,EAAE;;IACF,MAAM,SAAS,GAAG,MAAM,CAAyB,IAAI,CAAC,CAAC;IAEvD,IAAI,CAAC,KAAK,CAAC,EAAE,EAAE,CAAC;QACd,MAAM,IAAI,KAAK,CACb,+DAA+D,CAChE,CAAC;IACJ,CAAC;IAED,MAAM,EAAE,GAAG,MAAM,CAAyC,KAAK,CAAC,EAAE,CAAC,CAAC;IAEpE,MAA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AI,SAAS,CAAC,OAAQ,CAAC,SAAS,CAAC,OAAO,EAAE,EAAE,CAAC;oBAClE,IAAI,WAAW,CAAC,GAAG,CAAC,GAAG,CAAC,IAAI,KAAK,CAAC,MAAM,KAAK,SAAS,EAAE,CAAC;wBACvD,MAAM,CAAC,IAAI,CAAC;4BACV,QAAQ,EAAE,GAAG;4BACb,KAAK,EAAE,KAAK,CAAC,IAAI;yBAClB,CAAC,CAAC;oBACL,CAAC;gBACH,CAAC;gBAED,WAAW,CAAC,KAAK,EAAE,CAAC;gBACpB,OAAO,MAAM,CAAC;YAChB,CAAC,EACD,SAAS,EAAE,CAAC,OAAO,EAAE,EAAE;gBACrB,OAAO,CAAC,OAAO,CAAC,CAAC,KAAK,EAAE,EAAE;oBACxB,SAAS,CAAC,OAAQ,CAAC,cAAc,CAAC,KAAK,CAAC,QAAQ,EAAE,KAAK,CAAC,KAAK,CAAC,CAAC;gBACjE,CAAC,CAAC,CAAC;YACL,CAAC,YAEA,KAAK,CAAC,QAAQ,GACC,GACa,CAClC,CAAC;AACJ,CAAC,CAAC","sourcesContent":["'use client';\n// InstantSuspenseProvider can only be used in a client context so this prevents errors from trying to use it in a server component.\n\nimport {\n FrameworkClient,\n InstantConfig,\n InstantSchemaDef,\n InstaQLResponse,\n PageInfoResponse,\n RuleParams,\n User,\n ValidQuery,\n} from '@instantdb/core';\nimport InstantReactWebDatabase from '../InstantReactWebDatabase.ts';\nimport {\n createHydrationStreamProvider,\n isServer,\n} from './HydrationStreamProvider.tsx';\nimport { createContext, useContext, useRef, useState } from 'react';\nimport { InstantReactAbstractDatabase } from '@instantdb/react-common';\n\ntype InstantSuspenseProviderProps<\n Schema extends InstantSchemaDef<any, any, any>,\n> = {\n nonce?: string;\n children: React.ReactNode;\n db?: InstantReactWebDatabase<Schema, any>;\n config?: Omit<InstantConfig<any, any>, 'schema'> & {\n schema: string;\n };\n user?: User | null;\n};\n\nconst stream = createHydrationStreamProvider<any>();\n\ntype SuspenseQueryContextValue = {\n useSuspenseQuery: (query: any, opts?: SuspenseQueryOpts) => any;\n ssrUser: User | null | undefined;\n};\n\nexport const SuspsenseQueryContext =\n createContext<SuspenseQueryContextValue | null>(null);\n\n// Creates a typed useSuspense hook\nexport const createUseSuspenseQuery = <\n Schema extends InstantSchemaDef<any, any, any>,\n UseDates extends boolean,\n>(\n _db: InstantReactWebDatabase<Schema, UseDates>,\n): (<Q extends ValidQuery<Q, Schema>>(\n q: Q,\n opts?: {\n ruleParams: RuleParams;\n },\n) => {\n data: InstaQLResponse<Schema, Q, NonNullable<UseDates>>;\n pageInfo?: PageInfoResponse<Q>;\n}) => {\n return <Q extends ValidQuery<Q, Schema>>(q: any, opts: any) => {\n const ctx = useContext(SuspsenseQueryContext);\n if (!ctx) {\n throw new Error(\n 'useSuspenseQuery must be used within a SuspenseQueryProvider',\n );\n }\n return ctx.useSuspenseQuery(q, opts) as any;\n };\n};\n\ntype SuspenseQueryOpts = {\n ruleParams: RuleParams;\n};\n\nexport const InstantSuspenseProvider = (\n props: InstantSuspenseProviderProps<any>,\n) => {\n const clientRef = useRef<FrameworkClient | null>(null);\n\n if (!props.db) {\n throw new Error(\n 'Must provide either a db or config to InstantSuspenseProvider',\n );\n }\n\n const db = useRef<InstantReactAbstractDatabase<any, any>>(props.db);\n\n const [trackedKeys] = useState(() => new Set<string>());\n\n if (!clientRef.current) {\n if (props.user && !props.user.refresh_token) {\n throw new Error(\n 'User must have a refresh_token field. Recieved: ' +\n JSON.stringify(props.user, null, 2),\n );\n }\n clientRef.current = new FrameworkClient({\n token: props.user?.refresh_token,\n db: db.current.core,\n });\n }\n\n if (isServer) {\n clientRef.current.subscribe((result) => {\n const { queryHash } = result;\n trackedKeys.add(queryHash);\n });\n }\n\n const useSuspenseQuery = (query: any, opts: SuspenseQueryOpts) => {\n const nonSuspenseResult = db.current.useQuery(query, {\n ...opts,\n });\n\n if (nonSuspenseResult.data) {\n return {\n data: nonSuspenseResult.data,\n pageInfo: nonSuspenseResult.pageInfo,\n };\n }\n\n // should never happen (typeguard)\n if (!clientRef.current) {\n throw new Error('Client ref not set up');\n }\n\n let entry = clientRef.current.getExistingResultForQuery(query, {\n ruleParams: opts?.ruleParams,\n });\n\n if (!entry) {\n entry = clientRef.current!.query(query, opts);\n }\n\n if (entry.status === 'pending') {\n throw entry.promise;\n }\n\n if (entry.status === 'error') {\n throw entry.error;\n }\n\n if (entry.status === 'success' && entry.type === 'session') {\n return entry.data;\n }\n\n if (entry.status === 'success') {\n const data = entry.data;\n const result = clientRef.current.completeIsomorphic(\n query,\n data.triples,\n data.attrs,\n data.pageInfo,\n );\n\n return result;\n }\n };\n\n return (\n <SuspsenseQueryContext.Provider\n value={{ useSuspenseQuery, ssrUser: props.user }}\n >\n <stream.Provider\n nonce={props.nonce}\n onFlush={() => {\n const toSend: { queryKey: string; value: any }[] = [];\n for (const [key, value] of clientRef.current!.resultMap.entries()) {\n if (trackedKeys.has(key) && value.status === 'success') {\n toSend.push({\n queryKey: key,\n value: value.data,\n });\n }\n }\n\n trackedKeys.clear();\n return toSend;\n }}\n onEntries={(entries) => {\n entries.forEach((entry) => {\n clientRef.current!.addQueryResult(entry.queryKey, entry.value);\n });\n }}\n >\n {props.children}\n </stream.Provider>\n </SuspsenseQueryContext.Provider>\n );\n};\n"]}
@@ -0,0 +1,3 @@
1
+ import { User } from '@instantdb/core';
2
+ export declare const getUserFromInstantCookie: (appId: string) => Promise<User | null>;
3
+ //# sourceMappingURL=getUserFromInstantCookie.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"getUserFromInstantCookie.d.ts","sourceRoot":"","sources":["../../../src/next-ssr/getUserFromInstantCookie.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,iBAAiB,CAAC;AAGvC,eAAO,MAAM,wBAAwB,GACnC,OAAO,MAAM,KACZ,OAAO,CAAC,IAAI,GAAG,IAAI,CAKrB,CAAC"}
@@ -0,0 +1,17 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ import { cookies } from 'next/headers.js';
11
+ export const getUserFromInstantCookie = (appId) => __awaiter(void 0, void 0, void 0, function* () {
12
+ const cookieStore = yield cookies();
13
+ const userJSON = cookieStore.get('instant_user_' + appId);
14
+ const user = userJSON ? JSON.parse(userJSON.value) : null;
15
+ return user;
16
+ });
17
+ //# sourceMappingURL=getUserFromInstantCookie.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"getUserFromInstantCookie.js","sourceRoot":"","sources":["../../../src/next-ssr/getUserFromInstantCookie.ts"],"names":[],"mappings":";;;;;;;;;AACA,OAAO,EAAE,OAAO,EAAE,MAAM,iBAAiB,CAAC;AAE1C,MAAM,CAAC,MAAM,wBAAwB,GAAG,CACtC,KAAa,EACS,EAAE;IACxB,MAAM,WAAW,GAAG,MAAM,OAAO,EAAE,CAAC;IACpC,MAAM,QAAQ,GAAG,WAAW,CAAC,GAAG,CAAC,eAAe,GAAG,KAAK,CAAC,CAAC;IAC1D,MAAM,IAAI,GAAG,QAAQ,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC;IAC1D,OAAO,IAAI,CAAC;AACd,CAAC,CAAA,CAAC","sourcesContent":["import { User } from '@instantdb/core';\nimport { cookies } from 'next/headers.js';\n\nexport const getUserFromInstantCookie = async (\n appId: string,\n): Promise<User | null> => {\n const cookieStore = await cookies();\n const userJSON = cookieStore.get('instant_user_' + appId);\n const user = userJSON ? JSON.parse(userJSON.value) : null;\n return user;\n};\n"]}
@@ -0,0 +1,3 @@
1
+ export declare const ESCAPE_REGEX: RegExp;
2
+ export declare function htmlEscapeJsonString(str: string): string;
3
+ //# sourceMappingURL=htmlescape.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"htmlescape.d.ts","sourceRoot":"","sources":["../../../src/next-ssr/htmlescape.ts"],"names":[],"mappings":"AAmBA,eAAO,MAAM,YAAY,QAAuB,CAAC;AAEjD,wBAAgB,oBAAoB,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAExD"}
@@ -0,0 +1,21 @@
1
+ // --------------------------------------------------------------------------------
2
+ //
3
+ // copied from
4
+ // https://github.com/vercel/next.js/blob/6bc07792a4462a4bf921a72ab30dc4ab2c4e1bda/packages/next/src/server/htmlescape.ts
5
+ // License: https://github.com/vercel/next.js/blob/6bc07792a4462a4bf921a72ab30dc4ab2c4e1bda/packages/next/license.md
6
+ //
7
+ // --------------------------------------------------------------------------------
8
+ // This utility is based on https://github.com/zertosh/htmlescape
9
+ // License: https://github.com/zertosh/htmlescape/blob/0527ca7156a524d256101bb310a9f970f63078ad/LICENSE
10
+ const ESCAPE_LOOKUP = {
11
+ '&': '\\u0026',
12
+ '>': '\\u003e',
13
+ '<': '\\u003c',
14
+ '\u2028': '\\u2028',
15
+ '\u2029': '\\u2029',
16
+ };
17
+ export const ESCAPE_REGEX = /[&><\u2028\u2029]/g;
18
+ export function htmlEscapeJsonString(str) {
19
+ return str.replace(ESCAPE_REGEX, (match) => ESCAPE_LOOKUP[match]);
20
+ }
21
+ //# sourceMappingURL=htmlescape.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"htmlescape.js","sourceRoot":"","sources":["../../../src/next-ssr/htmlescape.ts"],"names":[],"mappings":"AAAA,mFAAmF;AACnF,EAAE;AACF,cAAc;AACd,yHAAyH;AACzH,oHAAoH;AACpH,EAAE;AACF,mFAAmF;AAEnF,iEAAiE;AACjE,uGAAuG;AAEvG,MAAM,aAAa,GAA2B;IAC5C,GAAG,EAAE,SAAS;IACd,GAAG,EAAE,SAAS;IACd,GAAG,EAAE,SAAS;IACd,QAAQ,EAAE,SAAS;IACnB,QAAQ,EAAE,SAAS;CACpB,CAAC;AAEF,MAAM,CAAC,MAAM,YAAY,GAAG,oBAAoB,CAAC;AAEjD,MAAM,UAAU,oBAAoB,CAAC,GAAW;IAC9C,OAAO,GAAG,CAAC,OAAO,CAAC,YAAY,EAAE,CAAC,KAAK,EAAE,EAAE,CAAC,aAAa,CAAC,KAAK,CAAE,CAAC,CAAC;AACrE,CAAC","sourcesContent":["// --------------------------------------------------------------------------------\n//\n// copied from\n// https://github.com/vercel/next.js/blob/6bc07792a4462a4bf921a72ab30dc4ab2c4e1bda/packages/next/src/server/htmlescape.ts\n// License: https://github.com/vercel/next.js/blob/6bc07792a4462a4bf921a72ab30dc4ab2c4e1bda/packages/next/license.md\n//\n// --------------------------------------------------------------------------------\n\n// This utility is based on https://github.com/zertosh/htmlescape\n// License: https://github.com/zertosh/htmlescape/blob/0527ca7156a524d256101bb310a9f970f63078ad/LICENSE\n\nconst ESCAPE_LOOKUP: Record<string, string> = {\n '&': '\\\\u0026',\n '>': '\\\\u003e',\n '<': '\\\\u003c',\n '\\u2028': '\\\\u2028',\n '\\u2029': '\\\\u2029',\n};\n\nexport const ESCAPE_REGEX = /[&><\\u2028\\u2029]/g;\n\nexport function htmlEscapeJsonString(str: string): string {\n return str.replace(ESCAPE_REGEX, (match) => ESCAPE_LOOKUP[match]!);\n}\n"]}
@@ -0,0 +1,28 @@
1
+ import { InstantConfig, InstantSchemaDef, InstantUnknownSchema } from '@instantdb/core';
2
+ import { InstantNextDatabase } from './InstantNextDatabase.tsx';
3
+ export { getUserFromInstantCookie } from './getUserFromInstantCookie.ts';
4
+ export { InstantNextDatabase } from './InstantNextDatabase.tsx';
5
+ export { InstantSuspenseProvider } from './InstantSuspenseProvider.tsx';
6
+ export { createInstantRouteHandler } from '@instantdb/core';
7
+ /**
8
+ *
9
+ * The first step: init your application!
10
+ *
11
+ * Visit https://instantdb.com/dash to get your `appId` :)
12
+ *
13
+ * @example
14
+ * import { init } from "@instantdb/react"
15
+ *
16
+ * const db = init({ appId: "my-app-id" })
17
+ *
18
+ * // You can also provide a schema for type safety and editor autocomplete!
19
+ *
20
+ * import { init } from "@instantdb/react"
21
+ * import schema from ""../instant.schema.ts";
22
+ *
23
+ * const db = init({ appId: "my-app-id", schema })
24
+ *
25
+ * // To learn more: https://instantdb.com/docs/modeling-data
26
+ */
27
+ export declare function init<Schema extends InstantSchemaDef<any, any, any> = InstantUnknownSchema, UseDates extends boolean = false>(config: InstantConfig<Schema, UseDates>): InstantNextDatabase<Schema, UseDates>;
28
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/next-ssr/index.tsx"],"names":[],"mappings":"AAAA,OAAO,EACL,aAAa,EACb,gBAAgB,EAChB,oBAAoB,EACrB,MAAM,iBAAiB,CAAC;AAIzB,OAAO,EAAE,mBAAmB,EAAE,MAAM,2BAA2B,CAAC;AAEhE,OAAO,EAAE,wBAAwB,EAAE,MAAM,+BAA+B,CAAC;AAEzE,OAAO,EAAE,mBAAmB,EAAE,MAAM,2BAA2B,CAAC;AAChE,OAAO,EAAE,uBAAuB,EAAE,MAAM,+BAA+B,CAAC;AAExE,OAAO,EAAE,yBAAyB,EAAE,MAAM,iBAAiB,CAAC;AAE5D;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,IAAI,CAClB,MAAM,SAAS,gBAAgB,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,CAAC,GAAG,oBAAoB,EACrE,QAAQ,SAAS,OAAO,GAAG,KAAK,EAEhC,MAAM,EAAE,aAAa,CAAC,MAAM,EAAE,QAAQ,CAAC,GACtC,mBAAmB,CAAC,MAAM,EAAE,QAAQ,CAAC,CAIvC"}
@@ -0,0 +1,32 @@
1
+ import version from "../version.js";
2
+ import { InstantNextDatabase } from "./InstantNextDatabase.js";
3
+ export { getUserFromInstantCookie } from "./getUserFromInstantCookie.js";
4
+ export { InstantNextDatabase } from "./InstantNextDatabase.js";
5
+ export { InstantSuspenseProvider } from "./InstantSuspenseProvider.js";
6
+ export { createInstantRouteHandler } from '@instantdb/core';
7
+ /**
8
+ *
9
+ * The first step: init your application!
10
+ *
11
+ * Visit https://instantdb.com/dash to get your `appId` :)
12
+ *
13
+ * @example
14
+ * import { init } from "@instantdb/react"
15
+ *
16
+ * const db = init({ appId: "my-app-id" })
17
+ *
18
+ * // You can also provide a schema for type safety and editor autocomplete!
19
+ *
20
+ * import { init } from "@instantdb/react"
21
+ * import schema from ""../instant.schema.ts";
22
+ *
23
+ * const db = init({ appId: "my-app-id", schema })
24
+ *
25
+ * // To learn more: https://instantdb.com/docs/modeling-data
26
+ */
27
+ export function init(config) {
28
+ return new InstantNextDatabase(config, {
29
+ '@instantdb/react': version,
30
+ });
31
+ }
32
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/next-ssr/index.tsx"],"names":[],"mappings":"AAMA,OAAO,OAAO,MAAM,eAAe,CAAC;AAEpC,OAAO,EAAE,mBAAmB,EAAE,MAAM,0BAA2B,CAAC;AAEhE,OAAO,EAAE,wBAAwB,EAAE,MAAM,+BAA+B,CAAC;AAEzE,OAAO,EAAE,mBAAmB,EAAE,MAAM,0BAA2B,CAAC;AAChE,OAAO,EAAE,uBAAuB,EAAE,MAAM,8BAA+B,CAAC;AAExE,OAAO,EAAE,yBAAyB,EAAE,MAAM,iBAAiB,CAAC;AAE5D;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAM,UAAU,IAAI,CAIlB,MAAuC;IAEvC,OAAO,IAAI,mBAAmB,CAAmB,MAAM,EAAE;QACvD,kBAAkB,EAAE,OAAO;KAC5B,CAAC,CAAC;AACL,CAAC","sourcesContent":["import {\n InstantConfig,\n InstantSchemaDef,\n InstantUnknownSchema,\n} from '@instantdb/core';\n\nimport version from '../version.ts';\n\nimport { InstantNextDatabase } from './InstantNextDatabase.tsx';\n\nexport { getUserFromInstantCookie } from './getUserFromInstantCookie.ts';\n\nexport { InstantNextDatabase } from './InstantNextDatabase.tsx';\nexport { InstantSuspenseProvider } from './InstantSuspenseProvider.tsx';\n\nexport { createInstantRouteHandler } from '@instantdb/core';\n\n/**\n *\n * The first step: init your application!\n *\n * Visit https://instantdb.com/dash to get your `appId` :)\n *\n * @example\n * import { init } from \"@instantdb/react\"\n *\n * const db = init({ appId: \"my-app-id\" })\n *\n * // You can also provide a schema for type safety and editor autocomplete!\n *\n * import { init } from \"@instantdb/react\"\n * import schema from \"\"../instant.schema.ts\";\n *\n * const db = init({ appId: \"my-app-id\", schema })\n *\n * // To learn more: https://instantdb.com/docs/modeling-data\n */\nexport function init<\n Schema extends InstantSchemaDef<any, any, any> = InstantUnknownSchema,\n UseDates extends boolean = false,\n>(\n config: InstantConfig<Schema, UseDates>,\n): InstantNextDatabase<Schema, UseDates> {\n return new InstantNextDatabase<Schema, UseDates>(config, {\n '@instantdb/react': version,\n });\n}\n"]}