mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-06 00:27:24 +00:00
Add nsite deployments
This commit is contained in:
10 files changed
+503
-16
No files matched your search
@@ -4,14 +4,14 @@
|
||||
*/
|
||||
export interface JSRuntimeFS {
|
||||
// Core file operations with proper overloads for type safety
|
||||
readFile(path: string): Promise<Uint8Array>;
|
||||
readFile(path: string): Promise<Uint8Array<ArrayBuffer>>;
|
||||
readFile(path: string, options: 'utf8'): Promise<string>;
|
||||
readFile(path: string, options: string): Promise<string>;
|
||||
readFile(path: string, options: { encoding: 'utf8' }): Promise<string>;
|
||||
readFile(path: string, options: { encoding: string }): Promise<string>;
|
||||
readFile(path: string, options?: string | { encoding?: string }): Promise<string | Uint8Array>;
|
||||
readFile(path: string, options?: string | { encoding?: string }): Promise<string | Uint8Array<ArrayBuffer>>;
|
||||
|
||||
writeFile(path: string, data: string | Uint8Array, options?: string | { encoding?: string }): Promise<void>;
|
||||
writeFile(path: string, data: string | Uint8Array<ArrayBuffer>, options?: string | { encoding?: string }): Promise<void>;
|
||||
|
||||
// readdir overloads for different return types
|
||||
readdir(path: string): Promise<string[]>;
|
||||
|
||||
@@ -0,0 +1,178 @@
|
||||
import { BlossomUploader } from '@nostrify/nostrify/uploaders';
|
||||
import { NSecSigner, NPool } from '@nostrify/nostrify';
|
||||
import type { NostrEvent } from '@nostrify/nostrify';
|
||||
import { nip19 } from 'nostr-tools';
|
||||
import type { JSRuntimeFS } from '../JSRuntime';
|
||||
import type { DeployAdapter, DeployOptions, DeployResult, NsiteDeployConfig } from './types';
|
||||
|
||||
/**
|
||||
* Nsite Deploy Adapter
|
||||
* Deploys static sites to Nostr using kind 34128 events and Blossom file storage
|
||||
*/
|
||||
export class NsiteAdapter implements DeployAdapter {
|
||||
private fs: JSRuntimeFS;
|
||||
private nostr: NsiteDeployConfig['nostr'];
|
||||
private nsec: string;
|
||||
private gateway: string;
|
||||
private relayUrls: string[];
|
||||
private blossomServers: string[];
|
||||
|
||||
constructor(config: NsiteDeployConfig) {
|
||||
this.fs = config.fs;
|
||||
this.nostr = config.nostr;
|
||||
this.nsec = config.nsec;
|
||||
this.gateway = config.gateway;
|
||||
this.relayUrls = config.relayUrls;
|
||||
this.blossomServers = config.blossomServers;
|
||||
}
|
||||
|
||||
async deploy(options: DeployOptions): Promise<DeployResult> {
|
||||
const { projectPath } = options;
|
||||
|
||||
// Check if dist directory exists and contains index.html
|
||||
const distPath = `${projectPath}/dist`;
|
||||
try {
|
||||
await this.fs.readFile(`${distPath}/index.html`, 'utf8');
|
||||
} catch {
|
||||
throw new Error('No index.html found in dist directory. Please build the project first.');
|
||||
}
|
||||
|
||||
// Decode nsec to get the private key and create signer
|
||||
const decoded = nip19.decode(this.nsec);
|
||||
if (decoded.type !== 'nsec') {
|
||||
throw new Error('Invalid nsec format');
|
||||
}
|
||||
const signer = new NSecSigner(decoded.data);
|
||||
const pubkey = await signer.getPublicKey();
|
||||
|
||||
// Create a grouped Nostr client for the configured relays
|
||||
const relayClient = this.nostr.group(this.relayUrls);
|
||||
|
||||
// Initialize Blossom uploader
|
||||
const uploader = new BlossomUploader({
|
||||
servers: this.blossomServers,
|
||||
signer,
|
||||
});
|
||||
|
||||
// Collect all files and upload them
|
||||
const fileEvents: NostrEvent[] = [];
|
||||
await this.uploadDirectory(distPath, '', uploader, signer, fileEvents);
|
||||
|
||||
// Publish all file events to the relays
|
||||
for (const event of fileEvents) {
|
||||
await relayClient.event(event, { signal: AbortSignal.timeout(5000) });
|
||||
}
|
||||
|
||||
// Publish kind 10063 server list if not already published
|
||||
await this.publishServerList(pubkey, signer, relayClient);
|
||||
|
||||
// Generate npub subdomain
|
||||
const npub = this.pubkeyToNpub(pubkey);
|
||||
const siteUrl = `https://${npub}.${this.gateway}`;
|
||||
|
||||
return {
|
||||
url: siteUrl,
|
||||
metadata: {
|
||||
pubkey,
|
||||
npub,
|
||||
filesPublished: fileEvents.length,
|
||||
provider: 'nsite',
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
private async uploadDirectory(
|
||||
dirPath: string,
|
||||
relativePath: string,
|
||||
uploader: BlossomUploader,
|
||||
signer: NSecSigner,
|
||||
fileEvents: NostrEvent[]
|
||||
): Promise<void> {
|
||||
const entries = await this.fs.readdir(dirPath, { withFileTypes: true });
|
||||
|
||||
for (const entry of entries) {
|
||||
const fullPath = `${dirPath}/${entry.name}`;
|
||||
const relPath = relativePath ? `${relativePath}/${entry.name}` : entry.name;
|
||||
|
||||
if (entry.isDirectory()) {
|
||||
await this.uploadDirectory(fullPath, relPath, uploader, signer, fileEvents);
|
||||
} else if (entry.isFile()) {
|
||||
await this.uploadFile(fullPath, relPath, uploader, signer, fileEvents);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private async uploadFile(
|
||||
filePath: string,
|
||||
relativePath: string,
|
||||
uploader: BlossomUploader,
|
||||
signer: NSecSigner,
|
||||
fileEvents: NostrEvent[]
|
||||
): Promise<void> {
|
||||
try {
|
||||
// Read file content
|
||||
const content = await this.fs.readFile(filePath);
|
||||
|
||||
// Convert to File object for upload
|
||||
const file = new File([content], relativePath.split('/').pop() || 'file');
|
||||
|
||||
// Upload to Blossom and get tags
|
||||
const tags = await uploader.upload(file);
|
||||
|
||||
// Extract sha256 hash from tags
|
||||
// Tags format: [['url', 'https://...'], ['x', 'sha256hash'], ['size', '1234'], ...]
|
||||
const xTag = tags.find(([name]) => name === 'x');
|
||||
if (!xTag || !xTag[1]) {
|
||||
throw new Error(`Failed to get sha256 hash for ${relativePath}`);
|
||||
}
|
||||
const sha256 = xTag[1];
|
||||
|
||||
// Create absolute path (must start with /)
|
||||
const absolutePath = relativePath.startsWith('/') ? relativePath : `/${relativePath}`;
|
||||
|
||||
// Create kind 34128 event
|
||||
const event = await signer.signEvent({
|
||||
kind: 34128,
|
||||
content: '',
|
||||
created_at: Math.floor(Date.now() / 1000),
|
||||
tags: [
|
||||
['d', absolutePath],
|
||||
['x', sha256],
|
||||
],
|
||||
});
|
||||
|
||||
fileEvents.push(event);
|
||||
} catch (error) {
|
||||
console.warn(`Failed to upload file ${filePath}:`, error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
private async publishServerList(pubkey: string, signer: NSecSigner, relayClient: NPool): Promise<void> {
|
||||
// Check if server list already exists
|
||||
const events = await relayClient.query([{
|
||||
kinds: [10063],
|
||||
authors: [pubkey],
|
||||
limit: 1,
|
||||
}], { signal: AbortSignal.timeout(5000) });
|
||||
|
||||
// If server list exists, don't publish a new one
|
||||
if (events.length > 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Create kind 10063 server list event
|
||||
const serverListEvent = await signer.signEvent({
|
||||
kind: 10063,
|
||||
content: '',
|
||||
created_at: Math.floor(Date.now() / 1000),
|
||||
tags: this.blossomServers.map(server => ['server', server]),
|
||||
});
|
||||
|
||||
await relayClient.event(serverListEvent, { signal: AbortSignal.timeout(5000) });
|
||||
}
|
||||
|
||||
private pubkeyToNpub(pubkey: string): string {
|
||||
return nip19.npubEncode(pubkey);
|
||||
}
|
||||
}
|
||||
@@ -2,3 +2,4 @@ export * from './types';
|
||||
export * from './ShakespeareAdapter';
|
||||
export * from './NetlifyAdapter';
|
||||
export * from './VercelAdapter';
|
||||
export * from './NsiteAdapter';
|
||||
+10
-1
@@ -1,5 +1,5 @@
|
||||
import type { JSRuntimeFS } from '../JSRuntime';
|
||||
import type { NostrSigner } from '@nostrify/nostrify';
|
||||
import type { NostrSigner, NPool } from '@nostrify/nostrify';
|
||||
|
||||
export interface DeployOptions {
|
||||
/** Project ID */
|
||||
@@ -45,3 +45,12 @@ export interface VercelDeployConfig {
|
||||
projectName?: string;
|
||||
corsProxy?: string;
|
||||
}
|
||||
|
||||
export interface NsiteDeployConfig {
|
||||
fs: JSRuntimeFS;
|
||||
nostr: NPool;
|
||||
nsec: string;
|
||||
gateway: string;
|
||||
relayUrls: string[];
|
||||
blossomServers: string[];
|
||||
}
|
||||
Reference in new issue
Block a user