Skip to content
This repository has been archived by the owner on Jan 17, 2025. It is now read-only.

Commit

Permalink
feat(dat): implement a dat handler
Browse files Browse the repository at this point in the history
refs #48
  • Loading branch information
enko committed Jul 24, 2018
1 parent 466419f commit 6d5eca0
Show file tree
Hide file tree
Showing 5 changed files with 1,094 additions and 21 deletions.
77 changes: 77 additions & 0 deletions electron/dat-handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/**
* @license MIT
*/

import {
MimeTypedBuffer,
RegisterBufferProtocolRequest,
} from 'electron';
import * as path from 'path';

// import {
// Magic,
// MAGIC_MIME_TYPE,
// } from 'mmmagic';

const Dat = require('dat-node');

import * as url from 'url';

const signale = require('signale');

type requestCallback = (buffer?: Buffer | MimeTypedBuffer) => void;

export function createDatHandler() {
return async function (request: RegisterBufferProtocolRequest, cb: requestCallback) {
// const magic = new Magic(MAGIC_MIME_TYPE);

try {
const datUrl = url.parse(request.url);
if (!(typeof datUrl.host === 'string')) {
throw new Error('Invalid url');
}

let filepath = '/tmp';
if (typeof process.env.HOME === 'string') {
filepath = path.join(process.env.HOME, '.ssb', 'datfiles', datUrl.host);
} else if (typeof process.env.USERPROFILE === 'string') {
filepath = path.join(filepath, '.ssb', 'datfiles', datUrl.host);
}

const buffer = await new Promise<any>((resolve: any, reject: any) => {
Dat(filepath, {
temp: true,
sparse: true,
key: datUrl.host,
}, (err: any, dat: any) => {
if (err) {
reject(err);
return;
}
dat.joinNetwork();

dat.archive.readFile(datUrl.path, function (downloadError: any, content: any) {
if (downloadError) {
reject(downloadError);
return;
}
resolve(content);
});
});
});

console.log(buffer);

cb({
mimeType: 'application/octet-stream',
data: buffer,
});

} catch (error) {
signale.error('failed to fetch blob');
signale.error(error);
signale.error(JSON.stringify(request, undefined, ' '));
cb();
}
};
}
18 changes: 11 additions & 7 deletions electron/start-electron.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

const signale = require('signale');

process.on('uncaughtException', function(error) {
process.on('uncaughtException', function (error) {
signale.error(error);
process.exit();
});

process.on('unhandledRejection', function(error) {
process.on('unhandledRejection', function (error) {
signale.error(error);
process.exit();
});
Expand All @@ -29,11 +29,12 @@ import {
openWindow,
setupContext,
} from '.';
import { createDatHandler } from './dat-handler';

const debug = require('debug')('ngx:ssb:init');

// tslint:disable-next-line:no-floating-promises
(async function() {
(async function () {
let win: BrowserWindow | undefined | null;
const args = process.argv.slice(1);
const serve = args.includes('--serve');
Expand Down Expand Up @@ -80,18 +81,21 @@ const debug = require('debug')('ngx:ssb:init');
});

const handleRedirect = (event: any, redirectUrl: string) => {
event.preventDefault();
require('electron').shell.openExternal(redirectUrl);
return false;
if (!redirectUrl.startsWith('http://localhost:4200')) {
event.preventDefault();
require('electron').shell.openExternal(redirectUrl);
return false;
}
};

win.webContents.on('will-navigate', handleRedirect);
win.webContents.on('new-window', handleRedirect);

protocol.registerBufferProtocol('ssb', createBlobHandler());
protocol.registerBufferProtocol('dat', createDatHandler());
}

protocol.registerStandardSchemes(['ssb']);
protocol.registerStandardSchemes(['ssb', 'dat']);
app.on('ready', createWindow);

app.on('window-all-closed', () => {
Expand Down
Loading

0 comments on commit 6d5eca0

Please sign in to comment.