1
+ import { DB } from '../Data' ;
2
+ import { DomainNotFound , EmptyDirectory , FileNotFound } from '../presets/RejectMessages' ;
3
+ import { NextHandler } from './NextHandler' ;
4
+ import { request as httpRequest } from 'node:http' ;
5
+ import { join } from 'node:path' ;
6
+
7
+ export const handleRequest = NextHandler ( async ( request , response ) => {
8
+ console . log ( 'Incomming request at ' + request . hostname + ' ' + request . path ) ;
9
+
10
+ // Lookup the site by hostname from the database
11
+ const a = await DB . selectOneFrom ( 'sitelookup' , [ 'site_id' ] , { host : request . hostname } ) ;
12
+
13
+ // Reject if the host does not exist
14
+ if ( ! a ) return DomainNotFound ( request . hostname + request . path ) ;
15
+
16
+ const b = await DB . selectOneFrom ( 'edgenames' , [ 'cid' ] , { site_id : a . site_id } ) ;
17
+
18
+ // Verify if file exists on IPFS node
19
+ const fileData = await new Promise < Object > ( ( accept , reject ) => {
20
+ const preparedURL = ( process . env . IPFS_API || 'http://127.0.0.1:5001' ) + '/api/v0/file/ls?arg=' + ( join ( b . cid , request . path ) ) ;
21
+ var existsRequest = httpRequest ( {
22
+ method : 'post' ,
23
+ host : '127.0.0.1' ,
24
+ port : 5001 ,
25
+ path : '/api/v0/file/ls?arg=' + ( join ( b . cid , request . path ) )
26
+ } , ( incomming ) => {
27
+ let data = '' ;
28
+ incomming . on ( 'data' , ( chunk ) => {
29
+ data += chunk ;
30
+ } ) ;
31
+ incomming . on ( 'end' , ( ) => {
32
+ accept ( JSON . parse ( data ) ) ;
33
+ } ) ;
34
+ } ) ;
35
+ existsRequest . end ( ) ;
36
+ } ) ;
37
+
38
+ // If not exists return
39
+ if ( fileData [ 'Type' ] == 'error' ) return FileNotFound ( request . hostname + request . path ) ;
40
+
41
+ // If directory
42
+ let optionalSuffix = '' ;
43
+ if ( fileData [ 'Objects' ] ) {
44
+ const localCID = Object . keys ( fileData [ 'Objects' ] ) [ 0 ] ;
45
+ if ( ! fileData [ 'Objects' ] [ localCID ] ) {
46
+ return { status : 500 , text : 'file not there...' } ;
47
+ }
48
+
49
+ if ( fileData [ 'Objects' ] [ localCID ] [ 'Type' ] == 'Directory' ) {
50
+
51
+ // Find the index.html
52
+ let fileFound = false ;
53
+ for ( let item of fileData [ 'Objects' ] [ localCID ] [ 'Links' ] ) {
54
+
55
+ // If name is empty, assume its a spread file
56
+ if ( item [ 'Name' ] . length === 0 ) {
57
+ break ;
58
+ }
59
+
60
+ // Check if file is index.html
61
+ if ( item [ 'Name' ] == 'index.html' && item [ 'Type' ] == 'File' ) {
62
+ optionalSuffix = 'index.html' ;
63
+ fileFound = true ;
64
+ break ;
65
+ }
66
+ }
67
+
68
+ // If no index.html found throw
69
+ if ( ! fileFound ) return EmptyDirectory ( request . hostname + request . path ) ;
70
+ }
71
+ }
72
+
73
+ // Fetch file from IPFS Endpoint
74
+ var contentRequest = httpRequest ( join ( process . env . IPFS_IP || 'http://127.0.0.1:8080' , 'ipfs' , b . cid , request . path , optionalSuffix ) , ( incomming ) => {
75
+ for ( const a of Object . keys ( incomming . headers ) ) {
76
+ response . setHeader ( a , incomming . headers [ a ] ) ;
77
+ }
78
+ incomming . on ( 'data' , ( chunk ) => {
79
+ response . write ( chunk ) ;
80
+ } ) ;
81
+ incomming . on ( 'end' , ( ) => {
82
+ response . send ( ) ;
83
+ } ) ;
84
+ } ) ;
85
+ contentRequest . on ( 'error' , ( error ) => {
86
+ console . log ( error ) ;
87
+ } ) ;
88
+ contentRequest . end ( ) ;
89
+ } ) ;
0 commit comments