Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix in order to make images compatible with web workers #1354

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions src/gen-media.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,13 @@ import { PresSlide, SlideLayout, ISlideRelMedia } from './core-interfaces'
* @return {Promise} promise
*/
export function encodeSlideMediaRels (layout: PresSlide | SlideLayout): Array<Promise<string>> {
let isWorker = false
if (typeof WorkerGlobalScope !== 'undefined' && self instanceof WorkerGlobalScope) {
isWorker = true
}
const fs = typeof require !== 'undefined' && typeof window === 'undefined' ? require('fs') : null // NodeJS
const https = typeof require !== 'undefined' && typeof window === 'undefined' ? require('https') : null // NodeJS

const imageProms: Array<Promise<string>> = []

// A: Capture all audio/image/video candidates for encoding (filtering online/pre-encoded)
Expand All @@ -35,7 +40,7 @@ export function encodeSlideMediaRels (layout: PresSlide | SlideLayout): Array<Pr
.forEach(rel => {
imageProms.push(
new Promise((resolve, reject) => {
if (fs && rel.path.indexOf('http') !== 0) {
if (!isWorker && fs && rel.path.indexOf('http') !== 0) {
// DESIGN: Node local-file encoding is syncronous, so we can load all images here, then call export with a callback (if any)
try {
const bitmap = fs.readFileSync(rel.path)
Expand All @@ -47,7 +52,7 @@ export function encodeSlideMediaRels (layout: PresSlide | SlideLayout): Array<Pr
candidateRels.filter(dupe => dupe.isDuplicate && dupe.path === rel.path).forEach(dupe => (dupe.data = rel.data))
reject(new Error(`ERROR: Unable to read media: "${rel.path}"\n${String(ex)}`))
}
} else if (fs && https && rel.path.indexOf('http') === 0) {
} else if (!isWorker && fs && https && rel.path.indexOf('http') === 0) {
https.get(rel.path, (res) => {
let rawData = ''
res.setEncoding('binary') // IMPORTANT: Only binary encoding works
Expand Down Expand Up @@ -105,7 +110,7 @@ export function encodeSlideMediaRels (layout: PresSlide | SlideLayout): Array<Pr
layout._relsMedia
.filter(rel => rel.isSvgPng && rel.data)
.forEach(rel => {
if (fs) {
if (fs || isWorker) {
// console.log('Sorry, SVG is not supported in Node (more info: https://github.com/gitbrent/PptxGenJS/issues/401)')
rel.data = IMG_BROKEN
imageProms.push(Promise.resolve().then(() => 'done'))
Expand Down
8 changes: 6 additions & 2 deletions src/pptxgen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,10 @@ export default class PptxGenJS implements IPresentationProps {
* @returns {Promise<string>} the presentation name
*/
async writeFile (props?: WriteFileProps | string): Promise<string> {
let isWorker = false
if (typeof WorkerGlobalScope !== 'undefined' && self instanceof WorkerGlobalScope) {
isWorker = true
}
const fs = typeof require !== 'undefined' && typeof window === 'undefined' ? require('fs') : null // NodeJS
// DEPRECATED: @deprecated v3.5.0 - fileName - [[remove in v4.0.0]]
if (typeof props === 'string') console.log('Warning: `writeFile(filename)` is deprecated - please use `WriteFileProps` argument (v3.5.0)')
Expand All @@ -608,9 +612,9 @@ export default class PptxGenJS implements IPresentationProps {

return await this.exportPresentation({
compression: propsCompress,
outputType: fs ? 'nodebuffer' : null,
outputType: (fs && !isWorker) ? 'nodebuffer' : null,
}).then(async content => {
if (fs) {
if (fs && !isWorker) {
// Node: Output
return await new Promise<string>((resolve, reject) => {
fs.writeFile(fileName, content, err => {
Expand Down
4 changes: 2 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
"baseUrl": "./node_modules",
"declaration": true,
"declarationDir": "./out/defs",
"lib": ["dom", "es2015", "es2017.object"],
"lib": ["webworker", "dom", "es2015", "es2017.object"],
"module": "es6",
"moduleResolution": "node",
"noImplicitAny": false,
"outDir": "./out",
"sourceMap": true,
"target": "es5"
"target": "es6"
},
"include": ["src/**/*"],
"exclude": ["node_modules"]
Expand Down