@kubohiroya/turbowarp-plus-editor-git 0.0.0-stage → 0.7.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.
Files changed (59) hide show
  1. package/LICENSE +373 -0
  2. package/README.ja.md +105 -0
  3. package/README.md +35 -2
  4. package/THIRD_PARTY_NOTICES.md +5 -0
  5. package/dist/THIRD_PARTY_NOTICES.md +1186 -0
  6. package/dist/block-diff-location.d.ts +22 -0
  7. package/dist/block-diff-preview.d.ts +17 -0
  8. package/dist/browser.d.ts +4 -0
  9. package/dist/connection-settings.d.ts +35 -0
  10. package/dist/file-diff-view.d.ts +16 -0
  11. package/dist/filesystem.d.ts +89 -0
  12. package/dist/github-pull-requests.d.ts +38 -0
  13. package/dist/index.d.ts +29 -0
  14. package/dist/index.js +3556 -0
  15. package/dist/libesm-OWRWzmu_.js +739 -0
  16. package/dist/messages.d.ts +112 -0
  17. package/dist/mutation-fs.d.ts +58 -0
  18. package/dist/objectSpread2-Dkd68nJH.js +101 -0
  19. package/dist/project-source-diff.d.ts +22 -0
  20. package/dist/project-source-diff.js +169 -0
  21. package/dist/project-source-patch.d.ts +39 -0
  22. package/dist/project-source-review-model.d.ts +20 -0
  23. package/dist/project-source-review.d.ts +34 -0
  24. package/dist/pull-request-panel.d.ts +8 -0
  25. package/dist/remote-operations.d.ts +5 -0
  26. package/dist/repository-DplMQiuo.js +17212 -0
  27. package/dist/repository.d.ts +61 -0
  28. package/dist/repository.js +1 -0
  29. package/dist/scratch-block-diff-renderer-CLYoOIB2.js +1464 -0
  30. package/dist/scratch-block-diff-renderer.d.ts +65 -0
  31. package/dist/smart-http-BmHEkE7K.js +303 -0
  32. package/dist/smart-http.d.ts +60 -0
  33. package/dist/smart-http.js +1 -0
  34. package/dist/style.css +109 -0
  35. package/package.json +60 -4
  36. package/src/block-diff-location.ts +63 -0
  37. package/src/block-diff-preview.tsx +36 -0
  38. package/src/browser.ts +5 -0
  39. package/src/connection-settings.tsx +103 -0
  40. package/src/file-diff-view.tsx +43 -0
  41. package/src/filesystem.ts +125 -0
  42. package/src/github-pull-requests.ts +162 -0
  43. package/src/index.tsx +349 -0
  44. package/src/messages.ts +64 -0
  45. package/src/mutation-fs.ts +204 -0
  46. package/src/project-source-diff.css +79 -0
  47. package/src/project-source-diff.tsx +132 -0
  48. package/src/project-source-patch.ts +126 -0
  49. package/src/project-source-review-model.ts +113 -0
  50. package/src/project-source-review.tsx +217 -0
  51. package/src/pull-request-panel.tsx +134 -0
  52. package/src/remote-operations.ts +90 -0
  53. package/src/repository.ts +308 -0
  54. package/src/scratch-block-diff-renderer.ts +213 -0
  55. package/src/smart-http.ts +190 -0
  56. package/src/style.css +29 -0
  57. package/src/styles.d.ts +1 -0
  58. package/tsconfig.json +18 -0
  59. package/vite.config.ts +26 -0
@@ -0,0 +1,190 @@
1
+ import type {HttpClient} from 'isomorphic-git';
2
+
3
+ export const MAX_GIT_REQUEST_BYTES = 32 * 1024 * 1024;
4
+
5
+ type Connection = Readonly<{mode: 'desktop' | 'cloudflare'; proxyOrigin: string}>;
6
+ type Session = Readonly<{connection: Connection; bearer: string; expiresAt: number}>;
7
+
8
+ export interface GitSmartHttpOptions {
9
+ readonly remote: string;
10
+ readonly connection: Connection;
11
+ readonly getSession: () => Promise<Session | null>;
12
+ readonly maxRequestBytes?: number;
13
+ readonly signal?: AbortSignal;
14
+ readonly fetch?: typeof fetch;
15
+ }
16
+
17
+ type Request = {
18
+ url: string;
19
+ method?: string;
20
+ body?: AsyncIterable<Uint8Array>;
21
+ signal?: AbortSignal;
22
+ };
23
+
24
+ const proxyCodes = {
25
+ 400: ['BAD_REQUEST'],
26
+ 401: ['SESSION_REQUIRED', 'SESSION_EXPIRED', 'REAUTH_REQUIRED'],
27
+ 403: ['FORBIDDEN'],
28
+ 409: ['REDIRECT_REJECTED'],
29
+ 411: ['LENGTH_REQUIRED'],
30
+ 413: ['PAYLOAD_TOO_LARGE'],
31
+ 429: ['RATE_LIMITED'],
32
+ 502: ['UPSTREAM_FAILURE'],
33
+ 504: ['UPSTREAM_TIMEOUT']
34
+ } as const;
35
+
36
+ type ProxyErrorCode = (typeof proxyCodes)[keyof typeof proxyCodes][number];
37
+ export type GitSmartHttpErrorCode = ProxyErrorCode | 'INVALID_REMOTE' | 'INVALID_REQUEST' |
38
+ 'INVALID_CONNECTION' | 'INVALID_SESSION' | 'SESSION_UNAVAILABLE' | 'ABORTED' | 'NETWORK_ERROR' |
39
+ 'INVALID_RESPONSE' | 'PUSH_OUTCOME_UNKNOWN';
40
+
41
+ export class GitSmartHttpError extends Error {
42
+ constructor(readonly code: GitSmartHttpErrorCode, readonly statusCode?: number, readonly requestId?: string) {
43
+ super(code);
44
+ this.name = 'GitSmartHttpError';
45
+ }
46
+ }
47
+
48
+ function remoteUrl(remote: string) {
49
+ // Validate the raw URL before a URL parser can discard dot segments, whitespace, or a default port.
50
+ const match = /^https:\/\/github\.com\/([A-Za-z0-9][A-Za-z0-9-]*)\/([A-Za-z0-9_.-]+?)(?:\.git)?$/u.exec(remote);
51
+ if (!match || match[0] !== remote || ['.', '..', '.git'].includes(match[2].toLowerCase())) {
52
+ throw new GitSmartHttpError('INVALID_REMOTE');
53
+ }
54
+ return `https://github.com/${match[1]}/${match[2]}.git`;
55
+ }
56
+
57
+ function checkConnection({mode, proxyOrigin}: Connection) {
58
+ const url = URL.parse(proxyOrigin);
59
+ if (!url || url.origin !== proxyOrigin ||
60
+ (mode === 'desktop' ? !/^http:\/\/127\.0\.0\.1:[1-9][0-9]{0,4}$/u.test(proxyOrigin) :
61
+ mode !== 'cloudflare' || url.protocol !== 'https:')) {
62
+ throw new GitSmartHttpError('INVALID_CONNECTION');
63
+ }
64
+ }
65
+
66
+ function checkAbort(signal?: AbortSignal) {
67
+ if (signal?.aborted) throw new GitSmartHttpError('ABORTED');
68
+ }
69
+
70
+ async function boundedBody(source: AsyncIterable<Uint8Array>, limit: number, signal?: AbortSignal) {
71
+ const parts: Uint8Array<ArrayBuffer>[] = [];
72
+ let size = 0;
73
+ for await (const bytes of source) {
74
+ checkAbort(signal);
75
+ size += bytes.byteLength;
76
+ if (size > limit) throw new GitSmartHttpError('PAYLOAD_TOO_LARGE');
77
+ parts.push(bytes.slice());
78
+ }
79
+ return new Blob(parts);
80
+ }
81
+
82
+ async function* responseBytes(body: ReadableStream<Uint8Array>, code: GitSmartHttpErrorCode, signal?: AbortSignal) {
83
+ const reader = body.getReader();
84
+ try {
85
+ for (;;) {
86
+ const {done, value} = await reader.read();
87
+ if (done) return;
88
+ yield value;
89
+ }
90
+ } catch {
91
+ if (code !== 'PUSH_OUTCOME_UNKNOWN') checkAbort(signal);
92
+ throw new GitSmartHttpError(code);
93
+ } finally {
94
+ await reader.cancel().catch(() => {});
95
+ reader.releaseLock();
96
+ }
97
+ }
98
+
99
+ async function proxyError(response: Response, writing: boolean) {
100
+ const status = response.status;
101
+ const allowed: readonly ProxyErrorCode[] | undefined = proxyCodes[status as keyof typeof proxyCodes];
102
+ let code: GitSmartHttpErrorCode = allowed?.[0] ?? 'INVALID_RESPONSE';
103
+ let requestId: string | undefined;
104
+ if (response.body && response.headers.get('content-type')?.split(';')[0] === 'application/json') {
105
+ try {
106
+ const body = await boundedBody(responseBytes(response.body, 'INVALID_RESPONSE'), 4096);
107
+ const parsed = JSON.parse(await body.text());
108
+ if (writing && status === 409 && parsed?.code === 'PUSH_DUPLICATE') code = 'PUSH_OUTCOME_UNKNOWN';
109
+ else if (allowed?.includes(parsed?.code)) code = parsed.code;
110
+ if (typeof parsed?.requestId === 'string' &&
111
+ /^[0-9a-f]{8}-(?:[0-9a-f]{4}-){3}[0-9a-f]{12}$/iu.test(parsed.requestId)) {
112
+ requestId = parsed.requestId;
113
+ }
114
+ } catch {
115
+ // Untrusted error bodies never become part of a client error.
116
+ }
117
+ } else {
118
+ await response.body?.cancel().catch(() => {});
119
+ }
120
+ if (writing && (!allowed || status >= 500)) code = 'PUSH_OUTCOME_UNKNOWN';
121
+ return new GitSmartHttpError(code, status, requestId);
122
+ }
123
+
124
+ export function createGitSmartHttp(options: GitSmartHttpOptions) {
125
+ const url = remoteUrl(options.remote);
126
+ const connection = {...options.connection};
127
+ checkConnection(connection);
128
+ const {getSession, maxRequestBytes = MAX_GIT_REQUEST_BYTES, signal: operationSignal, fetch: send = fetch} = options;
129
+ if (!Number.isSafeInteger(maxRequestBytes) || maxRequestBytes <= 0 || maxRequestBytes > MAX_GIT_REQUEST_BYTES) {
130
+ throw new GitSmartHttpError('INVALID_CONNECTION');
131
+ }
132
+ const proxyPath = connection.proxyOrigin + '/v1/git' + url.slice('https://github.com'.length);
133
+ const request = async ({url: requested, method = 'GET', body, signal}: Request) => {
134
+ const path = requested.startsWith(url + '/') ? requested.slice(url.length) : '';
135
+ const discovery = method === 'GET' && !body;
136
+ const rpc = method === 'POST' && Boolean(body);
137
+ const service = ['git-upload-pack', 'git-receive-pack'].find(name =>
138
+ (discovery && path === `/info/refs?service=${name}`) || (rpc && path === `/${name}`));
139
+ if (!service) throw new GitSmartHttpError('INVALID_REQUEST');
140
+
141
+ const abort = signal && operationSignal ? AbortSignal.any([signal, operationSignal]) : signal ?? operationSignal;
142
+ checkAbort(abort);
143
+ const payload = body ? await boundedBody(body, maxRequestBytes, abort) : undefined;
144
+ checkAbort(abort);
145
+ let session: Session | null;
146
+ try {
147
+ session = await getSession();
148
+ } catch {
149
+ checkAbort(abort);
150
+ throw new GitSmartHttpError('SESSION_UNAVAILABLE');
151
+ }
152
+ checkAbort(abort);
153
+ if (!session) throw new GitSmartHttpError('SESSION_REQUIRED');
154
+ if (!Number.isFinite(session.expiresAt) || session.expiresAt <= Date.now()) {
155
+ throw new GitSmartHttpError('SESSION_EXPIRED');
156
+ }
157
+ if (session.connection?.mode !== connection.mode || session.connection.proxyOrigin !== connection.proxyOrigin ||
158
+ typeof session.bearer !== 'string' || !/^[A-Za-z0-9._~+\/-]+=*$/u.test(session.bearer)) {
159
+ throw new GitSmartHttpError('INVALID_SESSION');
160
+ }
161
+ const contentType = `application/x-${service}-${discovery ? 'advertisement' : 'result'}`;
162
+ const headers = new Headers({Authorization: `Bearer ${session.bearer}`, Accept: contentType});
163
+ if (payload) headers.set('Content-Type', `application/x-${service}-request`);
164
+ const writing = rpc && service === 'git-receive-pack';
165
+ if (writing) headers.set('Git-Push-Id', crypto.randomUUID());
166
+
167
+ let response: Response;
168
+ try {
169
+ response = await send(proxyPath + path, {
170
+ method, headers, body: payload, signal: abort, credentials: 'omit', redirect: 'error', cache: 'no-store',
171
+ referrerPolicy: 'no-referrer'
172
+ });
173
+ } catch {
174
+ if (writing) throw new GitSmartHttpError('PUSH_OUTCOME_UNKNOWN');
175
+ checkAbort(abort);
176
+ throw new GitSmartHttpError('NETWORK_ERROR');
177
+ }
178
+ if (response.status !== 200) throw await proxyError(response, writing);
179
+ if (!response.body || response.headers.get('content-type') !== contentType) {
180
+ await response.body?.cancel().catch(() => {});
181
+ throw new GitSmartHttpError(writing ? 'PUSH_OUTCOME_UNKNOWN' : 'INVALID_RESPONSE');
182
+ }
183
+ return {
184
+ url: requested, method, headers: {'content-type': contentType},
185
+ statusCode: 200, statusMessage: 'OK',
186
+ body: responseBytes(response.body, writing ? 'PUSH_OUTCOME_UNKNOWN' : 'NETWORK_ERROR', abort)
187
+ };
188
+ };
189
+ return {url, corsProxy: '' as const, http: {request} satisfies HttpClient};
190
+ }
package/src/style.css ADDED
@@ -0,0 +1,29 @@
1
+ .twgit-backdrop { position: fixed; inset: 0; z-index: 65000; background: #0006; display: flex; align-items: center; justify-content: center; }
2
+ .twgit-panel { width: 94vw; height: 90vh; display: flex; flex-direction: column; background: Canvas; color: CanvasText; border: 1px solid GrayText; border-radius: 8px; box-shadow: 0 12px 48px #0005; font: 14px/1.5 system-ui, sans-serif; overflow: hidden; }
3
+ .twgit-panel > header { display: flex; gap: 8px; align-items: center; padding: 10px 16px; border-bottom: 1px solid GrayText; }
4
+ .twgit-panel > header h2 { margin: 0 auto 0 0; font-size: 18px; }
5
+ .twgit-panel h3 { font-size: 14px; margin: 12px 0 5px; }
6
+ .twgit-panel button { color: inherit; background: Canvas; border: 1px solid GrayText; border-radius: 4px; padding: 4px 8px; cursor: pointer; font: inherit; text-align: start; }
7
+ .twgit-panel button:disabled { opacity: .5; cursor: default; }
8
+ .twgit-panel button:focus-visible, .twgit-panel summary:focus-visible { outline: 2px solid Highlight; outline-offset: 2px; }
9
+ .twgit-panel button[aria-pressed="true"] { color: HighlightText; background: Highlight; }
10
+ .twgit-context { display: flex; gap: 8px 20px; flex-wrap: wrap; padding: 8px 16px; }
11
+ .twgit-connection { margin: 0 16px 8px; max-height: 45%; overflow: auto; }
12
+ .twgit-content { display: grid; grid-template-columns: minmax(230px, 30%) minmax(0, 1fr); flex: 1; min-height: 0; }
13
+ .twgit-panel aside { padding: 0 14px 14px; overflow: auto; border-right: 1px solid GrayText; }
14
+ .twgit-panel aside ul { list-style: none; padding: 0; margin: 4px 0; }
15
+ .twgit-panel aside li { overflow-wrap: anywhere; }
16
+ .twgit-panel summary { cursor: pointer; }
17
+ .twgit-panel .twgit-file { width: 100%; border: 0; margin: 1px 0; }
18
+ .twgit-file span { display: inline-block; width: 1.4em; font-family: monospace; }
19
+ .twgit-diff { padding: 0 16px 16px; min-width: 0; overflow: auto; }
20
+ .twgit-diff .diff { font-size: 12px; }
21
+ .twgit-history { list-style: none; margin: 0; padding: 0; }
22
+ .twgit-history li { padding: 5px 0; }
23
+ .twgit-history small { display: block; font-size: 11px; }
24
+ .twgit-panel > footer { font-size: 11px; padding: 8px 16px; border-top: 1px solid GrayText; }
25
+ .twgit-error { margin: 6px 16px; color: #a11919; padding: 8px; background: #fff0f0; }
26
+ @media (max-width: 700px) { .twgit-content { grid-template-columns: minmax(180px, 42%) minmax(0, 1fr); } .twgit-panel > header { flex-wrap: wrap; } }
27
+ .twgit-view-switch { display: flex; flex-wrap: wrap; gap: 4px; margin: 0 0 10px; }
28
+ .twgit-review-unreadable { margin: 6px 0; padding: 8px; background: color-mix(in srgb, #d9822b 16%, Canvas); }
29
+ .twgit-review-comparison .twgit-review-overview dl { margin: 4px 0 8px; }
@@ -0,0 +1 @@
1
+ declare module '*.css';
package/tsconfig.json ADDED
@@ -0,0 +1,18 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ES2017",
4
+ "lib": ["ES2023", "DOM", "DOM.Iterable"],
5
+ "module": "NodeNext",
6
+ "moduleResolution": "NodeNext",
7
+ "jsx": "react",
8
+ "strict": true,
9
+ "esModuleInterop": true,
10
+ "verbatimModuleSyntax": true,
11
+ "declaration": true,
12
+ "emitDeclarationOnly": true,
13
+ "rootDir": "src",
14
+ "outDir": "dist",
15
+ "skipLibCheck": true
16
+ },
17
+ "include": ["src/**/*.ts", "src/**/*.tsx"]
18
+ }
package/vite.config.ts ADDED
@@ -0,0 +1,26 @@
1
+ import {defineConfig, esmExternalRequirePlugin} from 'vite';
2
+
3
+ export default defineConfig({
4
+ resolve: {alias: {buffer: 'buffer/'}},
5
+ build: {
6
+ target: 'es2017',
7
+ // The Editor host minifies the final bundle. Preserve class bindings for webpack 4.
8
+ minify: false,
9
+ license: {fileName: 'THIRD_PARTY_NOTICES.md'},
10
+ lib: {
11
+ entry: {
12
+ index: 'src/browser.ts',
13
+ repository: 'src/repository.ts',
14
+ 'smart-http': 'src/smart-http.ts',
15
+ 'project-source-diff': 'src/project-source-diff.tsx'
16
+ },
17
+ formats: ['es'],
18
+ fileName: (_format, name) => `${name}.js`,
19
+ cssFileName: 'style'
20
+ },
21
+ rolldownOptions: {
22
+ // The bundled JSX runtime must resolve React to the host's copy in ESM builds.
23
+ plugins: [esmExternalRequirePlugin({external: ['react', 'react-dom']})]
24
+ }
25
+ }
26
+ });