+++ /dev/null
-import '../../common/init';
-import { isMainThread, workerData } from 'worker_threads';
-import { wrt } from '../../browser/wrt';
-import { URL } from 'url';
-import * as fs from 'fs';
-import * as https from 'https';
-import * as XWalkExtension from '../../common/wrt_xwalk_extension';
-
-function getManifestFile(manifestUrl: string) {
- console.debug('manifestUrl : '+manifestUrl);
- return new Promise((resolve, reject) => {
- const req = https.request(manifestUrl, (res) => {
- res.setEncoding('utf8');
- let responseBody = '';
- res.on('data', (data) => {
- responseBody += data;
- });
- res.on('end', () => {
- resolve(JSON.parse(responseBody));
- });
- }).on('error', (err) => {
- console.debug(`error : ${err}`);
- reject(err);
- });
- req.end();
- });
-}
-
-async function downloadIcon(iconSrc: string, iconFile: string) {
- console.debug('iconSrc : ' + iconSrc);
- return new Promise((resolve, reject) => {
- const req = https.request(iconSrc, (res) => {
- const Stream = require('stream').Transform;
- let data = new Stream();
- res.on('data', (chunk) => {
- data.push(chunk);
- });
- res.on('end', () => {
- fs.writeFileSync(iconFile, data.read());
- resolve('done');
- });
- }).on('error', (err) => {
- console.debug(`error : ${err}`);
- reject(err);
- });
- req.end();
- });
-}
-
-function makeFileSync(file: string, data: string) {
- fs.writeFileSync(file, data);
-}
-
-function concatWithBaseUrl(path: string, baseUrl: string) {
- if (path.substr(0, 1) === '/') {
- const url = new URL(path, baseUrl);
- return url.toString();
- } else if (path.indexOf('http:') == -1 && path.indexOf('https:') == -1) {
- return baseUrl.substr(0, baseUrl.lastIndexOf('/') + 1) + path;
- }
- return path;
-}
-
-let baseWorkingDir = '/home/owner/content/Downloads/AppManifest';
-let downloadVirtualDir = 'downloads/AppManifest';
-let iconFile: string = '';
-let iconName: string = '';
-let manifestFile: string = '';
-let convertedConfigXml: string = '';
-let refCount: number = 0;
-
-function getAppName(appName: string) {
- appName = appName.replace(/ /g, '');
- console.debug('appName : ' + appName);
- return appName;
-}
-
-function makeWorkingFolder(appName: string) {
- let workingDir = `${baseWorkingDir}/${appName}`;
- fs.rmdirSync(workingDir, { recursive: true });
- fs.mkdir(workingDir, { recursive: true }, (err) => {
- if (err)
- console.debug(`mkdir error : ${err}`)
- });
-}
-
-async function handleIcon(appName: string, manifestUrl: string, manifest: any) {
- let lengthOfIcons = manifest['icons'].length;
- let lastIcon = manifest['icons'][lengthOfIcons - 1];
- let iconSrc = concatWithBaseUrl(lastIcon['src'], manifestUrl);
- iconName = iconSrc.substr(iconSrc.lastIndexOf('/') + 1);
- iconFile = `${baseWorkingDir}/${appName}/${iconName}`;
- await downloadIcon(iconSrc, iconFile);
- iconFile = `${downloadVirtualDir}/${appName}/${iconName}`;
- refCount++;
-}
-
-function makeManifestFile(appName: string, manifest: any) {
- manifestFile = `${baseWorkingDir}/${appName}/appmanifest.json`;
- makeFileSync(manifestFile, JSON.stringify(manifest));
- manifestFile = `${downloadVirtualDir}/${appName}/appmanifest.json`;
- refCount++;
-}
-
-function makePkgId(startUrl: string) {
- let id = Buffer.from(startUrl).toString('base64');
- id = id.replace(/=/gi, '');
- console.debug(`id : ${id}`);
- return id.substr(-10);
-}
-
-function convertConfigXml(appName: string, startUrl: string) {
- convertedConfigXml = `${baseWorkingDir}/${appName}/config.xml`;
- let id = makePkgId(startUrl);
- let configXml = `<?xml version='1.0' encoding='UTF-8'?>`;
- configXml += `<widget xmlns='http://www.w3.org/ns/widgets' xmlns:tizen='http://tizen.org/ns/widgets' id='http://yourdomain/AppManifest' version='1.0.0' viewmodes='maximized'>`;
- configXml += `<tizen:application id='${id}.${appName}' package='${id}' required_version='3.0' />`;
- configXml += `<content src='${startUrl}' />`
- configXml += `<icon src='${iconName}' />`;
- configXml += `<name>${appName}</name>`;
- configXml += `<access origin='*' subdomains='true' />`;
- configXml += `</widget>`;
- makeFileSync(convertedConfigXml, configXml);
- convertedConfigXml = `${downloadVirtualDir}/${appName}/config.xml`;
- refCount++;
-}
-
-function cleanUpAndQuit(appName: string) {
- let workingDir = `${baseWorkingDir}/${appName}`;
- fs.rmdirSync(workingDir, { recursive: true });
- process.exit();
-}
-
-function installWgt(appName: string) {
- let wgtPath = `${baseWorkingDir}/${appName}/${appName}.wgt`;
- let installinfo = "{\"app_id\":\"" + appName + "\",\"pkg_path\":\"" + wgtPath + "\"}";
- console.debug(`installWgt info: ${installinfo}`);
- (wrt.tv as any).notifyInstall(installinfo);
- process.exit();
-}
-
-function makeWgt(appName: string) {
- let wgtPath = `${downloadVirtualDir}/${appName}/${appName}.wgt`;
- let onArchive = (archive: any) => {
- function progressCallback(opId: string, val: number, name: any) {
- console.debug('opId: ' + opId + ' with progress val: ' + (val * 100).toFixed(0) + '%');
- }
- function successCallback() {
- console.debug(`File added : ${refCount}`);
- refCount--;
- if (!refCount) {
- installWgt(appName);
- }
- }
- console.debug(`convertedConfigXml : ${convertedConfigXml}`);
- console.debug(`manifestFile : ${manifestFile}`);
- console.debug(`iconFile : ${iconFile}`);
- let defaultArchiveFileEntryOption = { destination:'', stripSourceDirectory: true};
- archive.add(convertedConfigXml, successCallback, null, progressCallback, defaultArchiveFileEntryOption);
- archive.add(manifestFile, successCallback, null, progressCallback, defaultArchiveFileEntryOption);
- if (iconFile)
- archive.add(iconFile, successCallback, null, progressCallback, defaultArchiveFileEntryOption);
- }
- global.tizen.archive.open(wgtPath, 'w', onArchive, () => { }, { overwrite: true });
-}
-
-async function parseAndHandleManifest(manifestUrl: string) {
- let manifest: any = await getManifestFile(manifestUrl);
- let appName = getAppName(manifest['name']);
- try {
- makeWorkingFolder(appName);
- if (manifest['icons']) {
- await handleIcon(appName, manifestUrl, manifest);
- }
- if (manifest['start_url']) {
- let startUrl = concatWithBaseUrl(manifest['start_url'], manifestUrl);
- manifest['start_url'] = startUrl;
-
- makeManifestFile(appName, manifest);
- convertConfigXml(appName, startUrl);
- }
- makeWgt(appName);
- } catch (e) {
- console.debug(`Exception: ${e}`);
- cleanUpAndQuit(appName);
- }
-}
-
-export function run(manifestUrl: string) {
- console.debug(`Appmanifest parser starts for ${manifestUrl}`);
- setInterval(() => { }, 500);
- wrt.tv?.delayShutdown();
- XWalkExtension.initialize();
- parseAndHandleManifest(manifestUrl);
-}
-
-if (!isMainThread) {
- run(decodeURIComponent(workerData.id));
-}