mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-03 16:03:34 +00:00
Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
92017036d9 |
No files matched your search
@@ -33,6 +33,7 @@ import { Terminal as TerminalComponent } from '@/components/Terminal';
|
||||
import { useSearchParams } from 'react-router-dom';
|
||||
import { useAppContext } from '@/hooks/useAppContext';
|
||||
import { isMediaFile } from '@/lib/fileUtils';
|
||||
import { injectNostrShim, getNostrShimCode, NOSTR_SHIM_PATH } from '@/lib/nostrShim';
|
||||
|
||||
interface PreviewPaneProps {
|
||||
projectId: string;
|
||||
@@ -319,6 +320,104 @@ export function PreviewPane({ projectId, activeTab, onToggleView, projectName, o
|
||||
}
|
||||
}, [historyIndex, navigationHistory, sendNavigationCommand]);
|
||||
|
||||
// Handle nostr.* requests from iframe (NIP-07 proxy)
|
||||
const handleNostrRequest = useCallback(async (message: {
|
||||
jsonrpc: '2.0';
|
||||
method: string;
|
||||
params: Record<string, unknown>;
|
||||
id: number;
|
||||
}) => {
|
||||
const { method, params, id } = message;
|
||||
const nostrMethod = method.replace('nostr.', '');
|
||||
|
||||
try {
|
||||
// Check if window.nostr is available in the parent
|
||||
if (!window.nostr) {
|
||||
throw new Error('Nostr extension not found. Please install a NIP-07 browser extension.');
|
||||
}
|
||||
|
||||
let result: unknown;
|
||||
|
||||
switch (nostrMethod) {
|
||||
case 'getPublicKey':
|
||||
result = await window.nostr.getPublicKey();
|
||||
break;
|
||||
|
||||
case 'signEvent':
|
||||
result = await window.nostr.signEvent(params.event as Parameters<typeof window.nostr.signEvent>[0]);
|
||||
break;
|
||||
|
||||
case 'getRelays':
|
||||
if (window.nostr.getRelays) {
|
||||
result = await window.nostr.getRelays();
|
||||
} else {
|
||||
result = {};
|
||||
}
|
||||
break;
|
||||
|
||||
case 'nip04.encrypt':
|
||||
if (!window.nostr.nip04) {
|
||||
throw new Error('NIP-04 encryption not supported by this extension');
|
||||
}
|
||||
result = await window.nostr.nip04.encrypt(
|
||||
params.pubkey as string,
|
||||
params.plaintext as string
|
||||
);
|
||||
break;
|
||||
|
||||
case 'nip04.decrypt':
|
||||
if (!window.nostr.nip04) {
|
||||
throw new Error('NIP-04 decryption not supported by this extension');
|
||||
}
|
||||
result = await window.nostr.nip04.decrypt(
|
||||
params.pubkey as string,
|
||||
params.ciphertext as string
|
||||
);
|
||||
break;
|
||||
|
||||
case 'nip44.encrypt':
|
||||
if (!window.nostr.nip44) {
|
||||
throw new Error('NIP-44 encryption not supported by this extension');
|
||||
}
|
||||
result = await window.nostr.nip44.encrypt(
|
||||
params.pubkey as string,
|
||||
params.plaintext as string
|
||||
);
|
||||
break;
|
||||
|
||||
case 'nip44.decrypt':
|
||||
if (!window.nostr.nip44) {
|
||||
throw new Error('NIP-44 decryption not supported by this extension');
|
||||
}
|
||||
result = await window.nostr.nip44.decrypt(
|
||||
params.pubkey as string,
|
||||
params.ciphertext as string
|
||||
);
|
||||
break;
|
||||
|
||||
default:
|
||||
throw new Error(`Unknown nostr method: ${nostrMethod}`);
|
||||
}
|
||||
|
||||
// Send success response
|
||||
sendResponse({
|
||||
jsonrpc: '2.0',
|
||||
result: result as JSONRPCResponse['result'],
|
||||
id
|
||||
});
|
||||
} catch (error) {
|
||||
console.error(`[Preview] Nostr ${nostrMethod} error:`, error);
|
||||
sendError({
|
||||
jsonrpc: '2.0',
|
||||
error: {
|
||||
code: -32000,
|
||||
message: error instanceof Error ? error.message : 'Unknown error',
|
||||
},
|
||||
id
|
||||
});
|
||||
}
|
||||
}, [sendResponse, sendError]);
|
||||
|
||||
const handleFetch = useCallback(async (request: JSONRPCRequest) => {
|
||||
const { params, id } = request;
|
||||
const { request: fetchRequest } = params;
|
||||
@@ -348,11 +447,41 @@ export function PreviewPane({ projectId, activeTab, onToggleView, projectName, o
|
||||
// Skip SPA fallback for static assets (files with extensions)
|
||||
const isStaticAsset = /\.[a-zA-Z0-9]+$/.test(path);
|
||||
|
||||
// Serve the nostr shim script at the special path
|
||||
if (path === NOSTR_SHIM_PATH) {
|
||||
console.log('[Preview] Serving nostr shim script');
|
||||
const shimCode = getNostrShimCode();
|
||||
sendResponse({
|
||||
jsonrpc: '2.0',
|
||||
result: {
|
||||
status: 200,
|
||||
statusText: 'OK',
|
||||
headers: {
|
||||
'Content-Type': 'application/javascript',
|
||||
'Cache-Control': 'no-cache',
|
||||
},
|
||||
body: encodeBase64(new TextEncoder().encode(shimCode)),
|
||||
},
|
||||
id
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
// SPA routing: try to serve the exact file first
|
||||
try {
|
||||
const bytes = await projectsManager.readFileBytes(projectId, 'dist' + filePath);
|
||||
console.log(`Serving file: ${filePath}`);
|
||||
|
||||
// Inject nostr shim into index.html
|
||||
let responseBody: string;
|
||||
if (filePath === '/index.html') {
|
||||
const html = new TextDecoder().decode(bytes);
|
||||
const injectedHtml = injectNostrShim(html);
|
||||
responseBody = encodeBase64(new TextEncoder().encode(injectedHtml));
|
||||
} else {
|
||||
responseBody = encodeBase64(bytes);
|
||||
}
|
||||
|
||||
sendResponse({
|
||||
jsonrpc: '2.0',
|
||||
result: {
|
||||
@@ -362,7 +491,7 @@ export function PreviewPane({ projectId, activeTab, onToggleView, projectName, o
|
||||
'Content-Type': getContentType(filePath),
|
||||
'Cache-Control': 'no-cache',
|
||||
},
|
||||
body: encodeBase64(bytes),
|
||||
body: responseBody,
|
||||
},
|
||||
id
|
||||
});
|
||||
@@ -393,6 +522,10 @@ export function PreviewPane({ projectId, activeTab, onToggleView, projectName, o
|
||||
const bytes = await projectsManager.readFileBytes(projectId, 'dist/index.html');
|
||||
console.log(`Serving index.html fallback for: ${path}`);
|
||||
|
||||
// Inject nostr shim into index.html for SPA fallback
|
||||
const html = new TextDecoder().decode(bytes);
|
||||
const injectedHtml = injectNostrShim(html);
|
||||
|
||||
sendResponse({
|
||||
jsonrpc: '2.0',
|
||||
result: {
|
||||
@@ -402,7 +535,7 @@ export function PreviewPane({ projectId, activeTab, onToggleView, projectName, o
|
||||
'Content-Type': 'text/html',
|
||||
'Cache-Control': 'no-cache',
|
||||
},
|
||||
body: encodeBase64(bytes),
|
||||
body: encodeBase64(new TextEncoder().encode(injectedHtml)),
|
||||
},
|
||||
id
|
||||
});
|
||||
@@ -453,12 +586,14 @@ export function PreviewPane({ projectId, activeTab, onToggleView, projectName, o
|
||||
handleConsoleMessage(message);
|
||||
} else if (message.jsonrpc === '2.0' && message.method === 'updateNavigationState') {
|
||||
handleUpdateNavigationState(message);
|
||||
} else if (message.jsonrpc === '2.0' && message.method?.startsWith('nostr.')) {
|
||||
handleNostrRequest(message);
|
||||
}
|
||||
};
|
||||
|
||||
window.addEventListener('message', handleMessage);
|
||||
return () => window.removeEventListener('message', handleMessage);
|
||||
}, [handleFetch, handleConsoleMessage, handleUpdateNavigationState, projectId, previewDomain]);
|
||||
}, [handleFetch, handleConsoleMessage, handleUpdateNavigationState, handleNostrRequest, projectId, previewDomain]);
|
||||
|
||||
useEffect(() => {
|
||||
if (selectedFile) {
|
||||
|
||||
@@ -0,0 +1,127 @@
|
||||
/**
|
||||
* The path where the nostr shim script is served from.
|
||||
* This special path is intercepted by PreviewPane and serves the shim JavaScript.
|
||||
*
|
||||
* NOTE: Cannot start with /_ because the iframe-fetch-client's Service Worker
|
||||
* ignores paths starting with /_ (reserved for its own system files).
|
||||
*/
|
||||
export const NOSTR_SHIM_PATH = '/.nostr/shim.js';
|
||||
|
||||
/**
|
||||
* Returns the JavaScript code for the nostr shim (without script tags).
|
||||
* This is served as an external script to work with the iframe-fetch-client's
|
||||
* document replacement which only re-executes external scripts.
|
||||
*/
|
||||
export function getNostrShimCode(): string {
|
||||
return `(function() {
|
||||
// Map to track pending requests
|
||||
var pendingRequests = new Map();
|
||||
var requestId = 0;
|
||||
|
||||
// Send a request to the parent and wait for response
|
||||
function sendNostrRequest(method, params) {
|
||||
params = params || {};
|
||||
return new Promise(function(resolve, reject) {
|
||||
var id = ++requestId;
|
||||
|
||||
pendingRequests.set(id, { resolve: resolve, reject: reject });
|
||||
|
||||
window.parent.postMessage({
|
||||
jsonrpc: '2.0',
|
||||
method: 'nostr.' + method,
|
||||
params: params,
|
||||
id: id
|
||||
}, '*');
|
||||
|
||||
// Timeout after 60 seconds (signing can take time with hardware wallets)
|
||||
setTimeout(function() {
|
||||
if (pendingRequests.has(id)) {
|
||||
pendingRequests.delete(id);
|
||||
reject(new Error('Nostr request timed out'));
|
||||
}
|
||||
}, 60000);
|
||||
});
|
||||
}
|
||||
|
||||
// Handle responses from parent
|
||||
window.addEventListener('message', function(event) {
|
||||
var message = event.data;
|
||||
|
||||
// Check if this is a nostr response
|
||||
if (message && message.jsonrpc === '2.0' && message.id && pendingRequests.has(message.id)) {
|
||||
var pending = pendingRequests.get(message.id);
|
||||
pendingRequests.delete(message.id);
|
||||
|
||||
if (message.error) {
|
||||
pending.reject(new Error(message.error.message || 'Unknown error'));
|
||||
} else {
|
||||
pending.resolve(message.result);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// Create the window.nostr shim
|
||||
window.nostr = {
|
||||
getPublicKey: function() {
|
||||
return sendNostrRequest('getPublicKey');
|
||||
},
|
||||
|
||||
signEvent: function(event) {
|
||||
return sendNostrRequest('signEvent', { event: event });
|
||||
},
|
||||
|
||||
getRelays: function() {
|
||||
return sendNostrRequest('getRelays');
|
||||
},
|
||||
|
||||
nip04: {
|
||||
encrypt: function(pubkey, plaintext) {
|
||||
return sendNostrRequest('nip04.encrypt', { pubkey: pubkey, plaintext: plaintext });
|
||||
},
|
||||
decrypt: function(pubkey, ciphertext) {
|
||||
return sendNostrRequest('nip04.decrypt', { pubkey: pubkey, ciphertext: ciphertext });
|
||||
}
|
||||
},
|
||||
|
||||
nip44: {
|
||||
encrypt: function(pubkey, plaintext) {
|
||||
return sendNostrRequest('nip44.encrypt', { pubkey: pubkey, plaintext: plaintext });
|
||||
},
|
||||
decrypt: function(pubkey, ciphertext) {
|
||||
return sendNostrRequest('nip44.decrypt', { pubkey: pubkey, ciphertext: ciphertext });
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
console.log('[Preview] window.nostr shim installed - NIP-07 calls will be proxied to parent');
|
||||
})();`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Injects a reference to the nostr shim external script into an HTML string.
|
||||
* Inserts the script at the start of <head> to ensure it runs before any app code.
|
||||
*
|
||||
* We use an external script instead of inline because the iframe-fetch-client's
|
||||
* replaceDocument() only re-executes external scripts (those with src attribute).
|
||||
*/
|
||||
export function injectNostrShim(html: string): string {
|
||||
// Use external script reference - the script will be served by PreviewPane
|
||||
const shimScript = `<script src="${NOSTR_SHIM_PATH}"></script>`;
|
||||
|
||||
// Try to inject after <head> tag
|
||||
const headMatch = html.match(/<head[^>]*>/i);
|
||||
if (headMatch) {
|
||||
const insertPos = headMatch.index! + headMatch[0].length;
|
||||
return html.slice(0, insertPos) + '\n' + shimScript + '\n' + html.slice(insertPos);
|
||||
}
|
||||
|
||||
// Fallback: inject after <!DOCTYPE html> or at the very start
|
||||
const doctypeMatch = html.match(/<!DOCTYPE[^>]*>/i);
|
||||
if (doctypeMatch) {
|
||||
const insertPos = doctypeMatch.index! + doctypeMatch[0].length;
|
||||
return html.slice(0, insertPos) + '\n' + shimScript + '\n' + html.slice(insertPos);
|
||||
}
|
||||
|
||||
// Last resort: prepend
|
||||
return shimScript + '\n' + html;
|
||||
}
|
||||
Reference in new issue
Block a user