@rojaebl/supa-store 0.0.0-stage → 0.7.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/LICENSE +21 -0
- package/README.md +707 -3
- package/dist/derived.d.ts +9 -0
- package/dist/derived.js +103 -0
- package/dist/index.d.ts +123 -0
- package/dist/index.js +271 -0
- package/dist/label.d.ts +5 -0
- package/dist/label.js +38 -0
- package/dist/mutation.d.ts +46 -0
- package/dist/mutation.js +131 -0
- package/dist/query.d.ts +184 -0
- package/dist/query.js +301 -0
- package/dist/react.d.ts +29 -0
- package/dist/react.js +36 -0
- package/dist/refetch.d.ts +25 -0
- package/dist/refetch.js +117 -0
- package/dist/resourceHook.d.ts +57 -0
- package/dist/resourceHook.js +292 -0
- package/dist/rpc/client.d.ts +65 -0
- package/dist/rpc/client.js +226 -0
- package/dist/rpc/error.d.ts +7 -0
- package/dist/rpc/error.js +13 -0
- package/dist/rpc/framing.d.ts +4 -0
- package/dist/rpc/framing.js +64 -0
- package/dist/rpc/index.d.ts +4 -0
- package/dist/rpc/index.js +4 -0
- package/dist/rpc/session.d.ts +18 -0
- package/dist/rpc/session.js +120 -0
- package/dist/rpc/transport.d.ts +26 -0
- package/dist/rpc/transport.js +122 -0
- package/dist/server/core.d.ts +72 -0
- package/dist/server/core.js +367 -0
- package/dist/server/index.d.ts +2 -0
- package/dist/server/index.js +2 -0
- package/dist/server/node.d.ts +12 -0
- package/dist/server/node.js +240 -0
- package/dist/server/sse.d.ts +10 -0
- package/dist/server/sse.js +25 -0
- package/dist/server/web.d.ts +8 -0
- package/dist/server/web.js +147 -0
- package/dist/stream/client.d.ts +43 -0
- package/dist/stream/client.js +397 -0
- package/dist/stream/hook.d.ts +9 -0
- package/dist/stream/hook.js +70 -0
- package/dist/stream/index.d.ts +53 -0
- package/dist/stream/index.js +43 -0
- package/dist/stream/protocol.d.ts +105 -0
- package/dist/stream/protocol.js +32 -0
- package/dist/track.d.ts +19 -0
- package/dist/track.js +25 -0
- package/package.json +104 -3
- package/tools/decline-loader.cjs +8 -0
- package/tools/definition.cjs +14 -0
- package/tools/next.d.mts +2 -0
- package/tools/next.mjs +30 -0
- package/tools/vite.d.mts +3 -0
- package/tools/vite.mjs +43 -0
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
// 연결의 약속(스펙 §7) — 메시지 갈래 · 닫기 코드 · 문구 · 기본 값. 서버의 홉과 화면의 클라이언트가 함께 쓴다
|
|
2
|
+
// 화면 쪽에서도 가져오므로 node · React 의 어느 것도 들이지 않는다
|
|
3
|
+
/** 4001 은 「다시 오지 마라」, 4000 은 「곧 다시 와라」 */
|
|
4
|
+
export const CLOSE = { GO_AWAY: 4001, COME_BACK: 4000 };
|
|
5
|
+
export const TEXT = {
|
|
6
|
+
reconnecting: "연결 끊김. 재시도 중..",
|
|
7
|
+
commandFailed: "연결 끊김. 명령 실패",
|
|
8
|
+
unknown: "연결 끊김. 알 수 없음",
|
|
9
|
+
timeout: "응답 지연",
|
|
10
|
+
};
|
|
11
|
+
export const DEFAULTS = {
|
|
12
|
+
historyCount: 1000,
|
|
13
|
+
historyMs: 300_000,
|
|
14
|
+
pingMs: 25_000,
|
|
15
|
+
idleMs: 60_000,
|
|
16
|
+
helloMs: 10_000,
|
|
17
|
+
timeoutMs: 30_000,
|
|
18
|
+
backoffStartMs: 1_000,
|
|
19
|
+
backoffMaxMs: 30_000,
|
|
20
|
+
jitter: 0.2,
|
|
21
|
+
};
|
|
22
|
+
/** SSE 와 POST 가 싣는 헤더 — 이름은 소문자로 읽는다. 토큰은 Authorization 에만 싣는다(주소에 싣지 않는다) */
|
|
23
|
+
export const HEADER = {
|
|
24
|
+
/** POST 가 어느 스트림 연결의 것인지 — welcome.connection */
|
|
25
|
+
connection: "x-supa-connection",
|
|
26
|
+
/** SSE 의 hello.channel */
|
|
27
|
+
channel: "x-supa-channel",
|
|
28
|
+
/** SSE 의 hello.epoch — 버전은 Last-Event-ID 가 싣는다 */
|
|
29
|
+
epoch: "x-supa-epoch",
|
|
30
|
+
lastEventId: "last-event-id",
|
|
31
|
+
authorization: "authorization",
|
|
32
|
+
};
|
package/dist/track.d.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/** 읽은 것 하나 — store · 바꾸기 칸 · 자원 칸이 저마다 낸다 */
|
|
2
|
+
export interface Channel {
|
|
3
|
+
subscribe(listener: () => void): () => void;
|
|
4
|
+
/** 이 값이 앞과 같으면(Object.is) 읽은 것이 그대로다 */
|
|
5
|
+
snapshot(): unknown;
|
|
6
|
+
/** 계산 공간이 이 채널을 구독하는 동안 부른다. 돌려준 함수가 보기를 푼다 — 자원 칸만 둔다 */
|
|
7
|
+
watch?(): () => void;
|
|
8
|
+
}
|
|
9
|
+
/** 파생 store 의 상태가 이 표식으로 자기 채널을 둔다 — ctx 가 파생을 알아보고 값을 꺼낸다 */
|
|
10
|
+
export declare const DERIVED: unique symbol;
|
|
11
|
+
/** 지금 track 이 돌고 있는가 — 적을 것을 지을 값이 들 때 먼저 묻는다 */
|
|
12
|
+
export declare function isTracking(): boolean;
|
|
13
|
+
/** 읽었다고 적는다 — track 이 도는 동안에만 */
|
|
14
|
+
export declare function noteRead(channel: Channel): void;
|
|
15
|
+
/** run 이 도는 동안 읽힌 채널을 모은다. 겹쳐 부르면 안쪽 것만 모은다 */
|
|
16
|
+
export declare function track<T>(run: () => T): {
|
|
17
|
+
value: T;
|
|
18
|
+
reads: Channel[];
|
|
19
|
+
};
|
package/dist/track.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
// 계산 공간 — 도는 동안 읽힌 것을 적는다. 자원 select 와 파생 store 가 이 안에서 돈다. React 를 가져오지 않는다
|
|
2
|
+
/** 파생 store 의 상태가 이 표식으로 자기 채널을 둔다 — ctx 가 파생을 알아보고 값을 꺼낸다 */
|
|
3
|
+
export const DERIVED = Symbol.for("supa-store.derived");
|
|
4
|
+
/** 지금 도는 track 이 모으는 채널 — track 바깥이면 null 이라 읽어도 적지 않는다 */
|
|
5
|
+
let recording = null;
|
|
6
|
+
/** 지금 track 이 돌고 있는가 — 적을 것을 지을 값이 들 때 먼저 묻는다 */
|
|
7
|
+
export function isTracking() {
|
|
8
|
+
return recording !== null;
|
|
9
|
+
}
|
|
10
|
+
/** 읽었다고 적는다 — track 이 도는 동안에만 */
|
|
11
|
+
export function noteRead(channel) {
|
|
12
|
+
recording?.add(channel);
|
|
13
|
+
}
|
|
14
|
+
/** run 이 도는 동안 읽힌 채널을 모은다. 겹쳐 부르면 안쪽 것만 모은다 */
|
|
15
|
+
export function track(run) {
|
|
16
|
+
const outer = recording;
|
|
17
|
+
const mine = new Set();
|
|
18
|
+
recording = mine;
|
|
19
|
+
try {
|
|
20
|
+
return { value: run(), reads: [...mine] };
|
|
21
|
+
}
|
|
22
|
+
finally {
|
|
23
|
+
recording = outer;
|
|
24
|
+
}
|
|
25
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,107 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rojaebl/supa-store",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.7.2",
|
|
4
|
+
"license": "MIT",
|
|
5
|
+
"publishConfig": {
|
|
6
|
+
"access": "public"
|
|
7
|
+
},
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/RoJaebl/supa-store.git"
|
|
11
|
+
},
|
|
12
|
+
"type": "module",
|
|
13
|
+
"engines": {
|
|
14
|
+
"node": ">=20"
|
|
15
|
+
},
|
|
16
|
+
"types": "./dist/index.d.ts",
|
|
17
|
+
"files": [
|
|
18
|
+
"dist",
|
|
19
|
+
"tools/*.cjs",
|
|
20
|
+
"tools/*.mjs",
|
|
21
|
+
"tools/*.d.mts",
|
|
22
|
+
"!tools/*.test.mjs"
|
|
23
|
+
],
|
|
24
|
+
"exports": {
|
|
25
|
+
".": {
|
|
26
|
+
"types": "./dist/index.d.ts",
|
|
27
|
+
"default": "./dist/index.js"
|
|
28
|
+
},
|
|
29
|
+
"./react": {
|
|
30
|
+
"types": "./dist/react.d.ts",
|
|
31
|
+
"default": "./dist/react.js"
|
|
32
|
+
},
|
|
33
|
+
"./query": {
|
|
34
|
+
"types": "./dist/query.d.ts",
|
|
35
|
+
"default": "./dist/query.js"
|
|
36
|
+
},
|
|
37
|
+
"./stream": {
|
|
38
|
+
"types": "./dist/stream/index.d.ts",
|
|
39
|
+
"default": "./dist/stream/index.js"
|
|
40
|
+
},
|
|
41
|
+
"./rpc": {
|
|
42
|
+
"types": "./dist/rpc/index.d.ts",
|
|
43
|
+
"default": "./dist/rpc/index.js"
|
|
44
|
+
},
|
|
45
|
+
"./server": {
|
|
46
|
+
"types": "./dist/server/index.d.ts",
|
|
47
|
+
"default": "./dist/server/index.js"
|
|
48
|
+
},
|
|
49
|
+
"./server/node": {
|
|
50
|
+
"types": "./dist/server/node.d.ts",
|
|
51
|
+
"default": "./dist/server/node.js"
|
|
52
|
+
},
|
|
53
|
+
"./server/web": {
|
|
54
|
+
"types": "./dist/server/web.d.ts",
|
|
55
|
+
"default": "./dist/server/web.js"
|
|
56
|
+
},
|
|
57
|
+
"./vite": {
|
|
58
|
+
"types": "./tools/vite.d.mts",
|
|
59
|
+
"default": "./tools/vite.mjs"
|
|
60
|
+
},
|
|
61
|
+
"./next": {
|
|
62
|
+
"types": "./tools/next.d.mts",
|
|
63
|
+
"default": "./tools/next.mjs"
|
|
64
|
+
}
|
|
65
|
+
},
|
|
66
|
+
"peerDependencies": {
|
|
67
|
+
"next": ">=14",
|
|
68
|
+
"react": ">=18",
|
|
69
|
+
"vite": ">=6",
|
|
70
|
+
"ws": ">=8"
|
|
71
|
+
},
|
|
72
|
+
"peerDependenciesMeta": {
|
|
73
|
+
"next": {
|
|
74
|
+
"optional": true
|
|
75
|
+
},
|
|
76
|
+
"react": {
|
|
77
|
+
"optional": true
|
|
78
|
+
},
|
|
79
|
+
"vite": {
|
|
80
|
+
"optional": true
|
|
81
|
+
},
|
|
82
|
+
"ws": {
|
|
83
|
+
"optional": true
|
|
84
|
+
}
|
|
85
|
+
},
|
|
86
|
+
"devDependencies": {
|
|
87
|
+
"@testing-library/dom": "^10.4.2",
|
|
88
|
+
"@testing-library/react": "^16.3.3",
|
|
89
|
+
"@types/node": "^24.19.2",
|
|
90
|
+
"@types/react": "^19.1.0",
|
|
91
|
+
"@types/react-dom": "^19.1.0",
|
|
92
|
+
"@types/ws": "^8.18.2",
|
|
93
|
+
"jsdom": "^30.1.1",
|
|
94
|
+
"react": "^19.1.0",
|
|
95
|
+
"react-dom": "^19.1.0",
|
|
96
|
+
"typescript": "^5.8.0",
|
|
97
|
+
"vite": "^8.3.5",
|
|
98
|
+
"vitest": "^3.1.0",
|
|
99
|
+
"ws": "^8.22.0"
|
|
100
|
+
},
|
|
101
|
+
"scripts": {
|
|
102
|
+
"build": "tsc -p tsconfig.json && tsc -p tsconfig.server.json",
|
|
103
|
+
"test": "pnpm build && vitest run && node --test \"tools/!(pack).test.mjs\"",
|
|
104
|
+
"test:pack": "node --test tools/pack.test.mjs",
|
|
105
|
+
"typecheck": "tsc -p tsconfig.json --noEmit && tsc -p tsconfig.server.json --noEmit && tsc -p tsconfig.test.json"
|
|
106
|
+
}
|
|
6
107
|
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
// webpack loader — 정의 파일(createStore 를 가져오는 파일)이 HMR 갱신을 받지 않게 한다.
|
|
2
|
+
// 정의 모듈이 다시 실행되면 store 가 하나 더 생기므로, 갱신은 여기서 멈추고 페이지를 통째로 다시 읽는다
|
|
3
|
+
const { isDefinition } = require("./definition.cjs");
|
|
4
|
+
|
|
5
|
+
module.exports = function declineLoader(source) {
|
|
6
|
+
if (!isDefinition(source)) return source;
|
|
7
|
+
return `${source}\nif (import.meta.webpackHot) import.meta.webpackHot.decline();\n`;
|
|
8
|
+
};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
// isDefinition — 파일이 @rojaebl/supa-store(또는 npm 별칭 supa-store)에서 createStore 를 가져오면 정의 파일이다.
|
|
2
|
+
// 정규식으로 가린다. 넘치는 쪽(정의가 아닌데 참)은 다시 읽기가 한 번 늘 뿐이라 덜 위험하다.
|
|
3
|
+
// 이름 공간으로 가져오면(`import * as s`) createStore 를 쓰는지 보지 않고 정의로 친다.
|
|
4
|
+
// 다시 내보내기(`export { createStore } from`)와 동적 import 는 알아보지 못한다
|
|
5
|
+
const IMPORT = /import\s*\{([^}]*)\}\s*from\s*["'](?:@rojaebl\/)?supa-store["']/g;
|
|
6
|
+
const NAMESPACE = /import\s*\*\s*as\s+[\w$]+\s+from\s*["'](?:@rojaebl\/)?supa-store["']/;
|
|
7
|
+
|
|
8
|
+
function isDefinition(code) {
|
|
9
|
+
if (NAMESPACE.test(code)) return true;
|
|
10
|
+
for (const [, names] of code.matchAll(IMPORT)) if (/\bcreateStore\b/.test(names)) return true;
|
|
11
|
+
return false;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
module.exports = { isDefinition };
|
package/tools/next.d.mts
ADDED
package/tools/next.mjs
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
// withStore — 개발 모드에서 정의 파일이 HMR 갱신을 받지 않게 한다(spec §3.6).
|
|
2
|
+
// 정의를 고치면(또는 그것이 기대는 모듈을 고치면) webpack 이 받지 않는 모듈에서 멈추고 전체를 다시 읽는다
|
|
3
|
+
import { fileURLToPath } from "node:url";
|
|
4
|
+
|
|
5
|
+
const LOADER = fileURLToPath(new URL("./decline-loader.cjs", import.meta.url));
|
|
6
|
+
|
|
7
|
+
function wrap(nextConfig) {
|
|
8
|
+
return {
|
|
9
|
+
...nextConfig,
|
|
10
|
+
webpack(config, options) {
|
|
11
|
+
const base = nextConfig?.webpack ? nextConfig.webpack(config, options) : config;
|
|
12
|
+
if (!options.dev || options.isServer) return base;
|
|
13
|
+
base.module ??= {};
|
|
14
|
+
base.module.rules = [
|
|
15
|
+
...(base.module.rules ?? []),
|
|
16
|
+
{ test: /\.[cm]?[jt]sx?$/, exclude: /[\\/]node_modules[\\/]/, loader: LOADER },
|
|
17
|
+
];
|
|
18
|
+
return base;
|
|
19
|
+
},
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function withStore(nextConfig) {
|
|
24
|
+
return (phase, ctx) => {
|
|
25
|
+
// 함수형 설정은 먼저 불러 결과(Promise 일 수 있다)에 같은 감싸기를 건다
|
|
26
|
+
const resolved = typeof nextConfig === "function" ? nextConfig(phase, ctx) : nextConfig;
|
|
27
|
+
if (phase !== "phase-development-server") return resolved;
|
|
28
|
+
return typeof resolved?.then === "function" ? resolved.then(wrap) : wrap(resolved);
|
|
29
|
+
};
|
|
30
|
+
}
|
package/tools/vite.d.mts
ADDED
package/tools/vite.mjs
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
// supaStore — 개발 서버에서 정의 파일이 다시 돌 갱신이면 Fast Refresh 대신 페이지 전체를 다시 읽게 한다.
|
|
2
|
+
// 정의 모듈이 다시 돌면 새 store 가 생기고 옛 store 와 그 듣기는 버려지지 않은 채 남기 때문이다
|
|
3
|
+
import { readFileSync } from "node:fs";
|
|
4
|
+
import { createRequire } from "node:module";
|
|
5
|
+
|
|
6
|
+
const { isDefinition } = createRequire(import.meta.url)("./definition.cjs");
|
|
7
|
+
|
|
8
|
+
export function supaStore() {
|
|
9
|
+
let off = false;
|
|
10
|
+
return {
|
|
11
|
+
name: "supa-store",
|
|
12
|
+
apply: "serve",
|
|
13
|
+
configResolved() {
|
|
14
|
+
off = !!process.env.VITEST;
|
|
15
|
+
},
|
|
16
|
+
hotUpdate({ modules }) {
|
|
17
|
+
if (off || this.environment.name !== "client") return;
|
|
18
|
+
// 바뀐 모듈에서 import 한 쪽으로 거슬러 넓이 우선으로 돈다 — 그 길의 모듈은 모두 다시 실행된다
|
|
19
|
+
const seen = new Set(modules);
|
|
20
|
+
const queue = [...modules];
|
|
21
|
+
const read = new Set(); // 한 번의 갱신 안에서 같은 파일을 두 번 읽지 않는다
|
|
22
|
+
for (let i = 0; i < queue.length; i++) {
|
|
23
|
+
const { file, importers } = queue[i];
|
|
24
|
+
if (file && !read.has(file)) {
|
|
25
|
+
read.add(file);
|
|
26
|
+
let code = "";
|
|
27
|
+
try {
|
|
28
|
+
code = readFileSync(file, "utf8");
|
|
29
|
+
} catch {} // 지워진 파일은 정의가 아니다
|
|
30
|
+
if (isDefinition(code)) {
|
|
31
|
+
this.environment.hot.send({ type: "full-reload" });
|
|
32
|
+
return [];
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
for (const importer of importers) {
|
|
36
|
+
if (seen.has(importer)) continue;
|
|
37
|
+
seen.add(importer);
|
|
38
|
+
queue.push(importer);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
},
|
|
42
|
+
};
|
|
43
|
+
}
|