🎥 Make videos programmatically with React
work towards GIF ES Module support
Jonny Burger1226d1284ce9
diff --git a/packages/gif/package.json b/packages/gif/package.jsonindex e7b995945f..2aa53a1492 100644--- a/packages/gif/package.json+++ b/packages/gif/package.json@@ -10,16 +10,27 @@"url": "https://github.com/remotion-dev/remotion/issues"},"license": "SEE LICENSE IN LICENSE.md",- "author": "",- "main": "dist/index.js",+ "author": "Dmitry Ivakhnenko, Jonny Burger",+ "main": "dist/cjs/index.js",+ "types": "dist/cjs/index.d.ts",+ "module": "dist/esm/index.mjs","files": ["dist"],"scripts": {"lint": "eslint src --ext ts,tsx",- "build": "node build.mjs && tsc -d",+ "build": "node build.mjs && rollup --config rollup.config.js && tsc -d","watch": "tsc -w"},+ "exports": {+ "./package.json": "./package.json",+ ".": {+ "types": "./dist/cjs/index.d.ts",+ "module": "./dist/esm/index.mjs",+ "import": "./dist/esm/index.mjs",+ "require": "./dist/cjs/index.js"+ }+ },"dependencies": {"js-binary-schema-parser": "^2.0.3","lru_map": "0.4.1",@@ -27,6 +38,7 @@},"devDependencies": {"@jonny/eslint-config": "3.0.266",+ "@rollup/plugin-typescript": "^8.2.0","@types/node": "^16.7.5","@types/react": "18.0.26","@types/react-dom": "18.0.10",@@ -36,6 +48,7 @@"prettier-plugin-organize-imports": "^2.3.4","react": "18.0.0","react-dom": "18.0.0",+ "rollup": "^2.70.1","typescript": "^4.7.0","webpack": "5.74.0"},diff --git a/packages/gif/rollup.config.js b/packages/gif/rollup.config.jsnew file mode 100644index 0000000000..145571d05a--- /dev/null+++ b/packages/gif/rollup.config.js@@ -0,0 +1,23 @@+// rollup.config.js+import typescript from '@rollup/plugin-typescript';++export default [+ {+ input: 'src/index.ts',+ output: [+ {+ file: 'dist/esm/index.mjs',+ format: 'es',+ sourcemap: false,+ },+ ],+ external: ['react', 'remotion', 'react/jsx-runtime'],+ plugins: [+ typescript({+ tsconfig: 'tsconfig-esm.json',+ sourceMap: false,+ outputToFilesystem: true,+ }),+ ],+ },+];diff --git a/packages/gif/src/gifuct/index.ts b/packages/gif/src/gifuct/index.tsindex 02e4f0c701..10e16b439d 100644--- a/packages/gif/src/gifuct/index.ts+++ b/packages/gif/src/gifuct/index.ts@@ -1,9 +1,6 @@-// @ts-expect-error-import {parse} from 'js-binary-schema-parser';-// @ts-expect-error-import {buildStream} from 'js-binary-schema-parser/lib/parsers/uint8';-// @ts-expect-error-import GIF from 'js-binary-schema-parser/lib/schemas/gif';+import {GIF} from '../js-binary-schema-parser/gif';+import {parse} from '../js-binary-schema-parser/parser';+import {buildStream} from '../js-binary-schema-parser/uint8-parser';import {deinterlace} from './deinterlace';import {lzw} from './lzw';import type {Frame, ParsedFrameWithoutPatch, ParsedGif} from './types';diff --git a/packages/gif/src/js-binary-schema-parser/gif.ts b/packages/gif/src/js-binary-schema-parser/gif.tsnew file mode 100644index 0000000000..985b23544a--- /dev/null+++ b/packages/gif/src/js-binary-schema-parser/gif.ts@@ -0,0 +1,200 @@+import {conditional} from './parser';+import {+ peekByte,+ peekBytes,+ readArray,+ readBits,+ readByte,+ readBytes,+ readString,+ readUnsigned,+} from './uint8-parser';++// a set of 0x00 terminated subblocks+const subBlocksSchema = {+ blocks: (stream) => {+ const terminator = 0x00;+ const chunks = [];+ const streamSize = stream.data.length;+ let total = 0;+ for (+ let size = readByte()(stream);+ size !== terminator;+ size = readByte()(stream)+ ) {+ // size becomes undefined for some case when file is corrupted and terminator is not proper+ // null check to avoid recursion+ if (!size) break;+ // catch corrupted files with no terminator+ if (stream.pos + size >= streamSize) {+ const availableSize = streamSize - stream.pos;+ chunks.push(readBytes(availableSize)(stream));+ total += availableSize;+ break;+ }++ chunks.push(readBytes(size)(stream));+ total += size;+ }++ const result = new Uint8Array(total);+ let offset = 0;+ for (let i = 0; i < chunks.length; i++) {+ result.set(chunks[i], offset);+ offset += chunks[i].length;+ }++ return result;+ },+};++// global control extension+const gceSchema = conditional(+ {+ gce: [+ {codes: readBytes(2)},+ {byteSize: readByte()},+ {+ extras: readBits({+ future: {index: 0, length: 3},+ disposal: {index: 3, length: 3},+ userInput: {index: 6},+ transparentColorGiven: {index: 7},+ }),+ },+ {delay: readUnsigned(true)},+ {transparentColorIndex: readByte()},+ {terminator: readByte()},+ ],+ },+ (stream) => {+ const codes = peekBytes(2)(stream);+ return codes[0] === 0x21 && codes[1] === 0xf9;+ }+);++// image pipeline block+const imageSchema = conditional(+ {+ image: [+ {code: readByte()},+ {+ descriptor: [+ {left: readUnsigned(true)},+ {top: readUnsigned(true)},+ {width: readUnsigned(true)},+ {height: readUnsigned(true)},+ {+ lct: readBits({+ exists: {index: 0},+ interlaced: {index: 1},+ sort: {index: 2},+ future: {index: 3, length: 2},+ size: {index: 5, length: 3},+ }),+ },+ ],+ },+ conditional(+ {+ lct: readArray(3, (stream, result, parent) => {+ return 2 ** (parent.descriptor.lct.size + 1);+ }),+ },+ (stream, result, parent) => {+ return parent.descriptor.lct.exists;+ }+ ),+ {data: [{minCodeSize: readByte()}, subBlocksSchema]},+ ],+ },+ (stream) => {+ return peekByte()(stream) === 0x2c;+ }+);++// plain text block+const textSchema = conditional(+ {+ text: [+ {codes: readBytes(2)},+ {blockSize: readByte()},+ {+ preData: (stream, result, parent) =>+ readBytes(parent.text.blockSize)(stream),+ },+ subBlocksSchema,+ ],+ },+ (stream) => {+ const codes = peekBytes(2)(stream);+ return codes[0] === 0x21 && codes[1] === 0x01;+ }+);++// application block+const applicationSchema = conditional(+ {+ application: [+ {codes: readBytes(2)},+ {blockSize: readByte()},+ {id: (stream, result, parent) => readString(parent.blockSize)(stream)},+ subBlocksSchema,+ ],+ },+ (stream) => {+ const codes = peekBytes(2)(stream);+ return codes[0] === 0x21 && codes[1] === 0xff;+ }+);++// comment block+const commentSchema = conditional(+ {+ comment: [{codes: readBytes(2)}, subBlocksSchema],+ },+ (stream) => {+ const codes = peekBytes(2)(stream);+ return codes[0] === 0x21 && codes[1] === 0xfe;+ }+);++export const GIF = [+ {header: [{signature: readString(3)}, {version: readString(3)}]},+ {+ lsd: [+ {width: readUnsigned(true)},+ {height: readUnsigned(true)},+ {+ gct: readBits({+ exists: {index: 0},+ resolution: {index: 1, length: 3},+ sort: {index: 4},+ size: {index: 5, length: 3},+ }),+ },+ {backgroundColorIndex: readByte()},+ {pixelAspectRatio: readByte()},+ ],+ },+ conditional(+ {+ gct: readArray(3, (stream, result) => 2 ** (result.lsd.gct.size + 1)),+ },+ (stream, result) => result.lsd.gct.exists+ ),+ // content frames+ {+ frames: loop(+ [gceSchema, applicationSchema, commentSchema, imageSchema, textSchema],+ (stream) => {+ const nextCode = peekByte()(stream);+ // rather than check for a terminator, we should check for the existence+ // of an ext or image block to avoid infinite loops+ // var terminator = 0x3B;+ // return nextCode !== terminator;+ return nextCode === 0x21 || nextCode === 0x2c;+ }+ ),+ },+];diff --git a/packages/gif/src/js-binary-schema-parser/parser.ts b/packages/gif/src/js-binary-schema-parser/parser.tsnew file mode 100644index 0000000000..3ad316c8e4--- /dev/null+++ b/packages/gif/src/js-binary-schema-parser/parser.ts@@ -0,0 +1,55 @@+export const parse = function parse(stream, schema) {+ const result =+ arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};+ const parent =+ arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : result;++ if (Array.isArray(schema)) {+ schema.forEach((partSchema) => {+ return parse(stream, partSchema, result, parent);+ });+ } else if (typeof schema === 'function') {+ schema(stream, result, parent, parse);+ } else {+ const key = Object.keys(schema)[0];++ if (Array.isArray(schema[key])) {+ parent[key] = {};+ parse(stream, schema[key], result, parent[key]);+ } else {+ parent[key] = schema[key](stream, result, parent, parse);+ }+ }++ return result;+};++export const conditional = (schema, conditionFunc) => {+ return function (stream, result, parent, parse) {+ if (conditionFunc(stream, result, parent)) {+ parse(stream, schema, result, parent);+ }+ };+};++export const loop = (schema, continueFunc) => {+ return function (stream, result, parent, parse) {+ const arr = [];+ let lastStreamPos = stream.pos;++ while (continueFunc(stream, result, parent)) {+ const newParent = {};+ parse(stream, schema, result, newParent); // cases when whole file is parsed but no termination is there and stream position is not getting updated as well+ // it falls into infinite recursion, null check to avoid the same++ if (stream.pos === lastStreamPos) {+ break;+ }++ lastStreamPos = stream.pos;+ arr.push(newParent);+ }++ return arr;+ };+};diff --git a/packages/gif/src/js-binary-schema-parser/uint8-parser.ts b/packages/gif/src/js-binary-schema-parser/uint8-parser.tsnew file mode 100644index 0000000000..0af292d87c--- /dev/null+++ b/packages/gif/src/js-binary-schema-parser/uint8-parser.ts@@ -0,0 +1,103 @@+// Default stream and parsers for Uint8TypedArray data type+export const buildStream = (uint8Data: Uint8Array) => {+ return {+ data: uint8Data,+ pos: 0,+ };+};++export const readByte = () => {+ return function (stream) {+ return stream.data[stream.pos++];+ };+};++export const peekByte = (...arguments) => {+ const offset =+ arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;+ return function (stream) {+ return stream.data[stream.pos + offset];+ };+};++export const readBytes = (length) => {+ return function (stream) {+ return stream.data.subarray(stream.pos, (stream.pos += length));+ };+};++export const peekBytes = (length) => {+ return function (stream) {+ return stream.data.subarray(stream.pos, stream.pos + length);+ };+};++export const readString = (length) => {+ return function (stream) {+ return Array.from(readBytes(length)(stream))+ .map((value) => {+ return String.fromCharCode(value);+ })+ .join('');+ };+};++export const readUnsigned = (littleEndian) => {+ return function (stream) {+ const bytes = readBytes(2)(stream);+ return littleEndian+ ? (bytes[1] << 8) + bytes[0]+ : (bytes[0] << 8) + bytes[1];+ };+};++export const readArray = (byteSize, totalOrFunc) => {+ return function (stream, result, parent) {+ const total =+ typeof totalOrFunc === 'function'+ ? totalOrFunc(stream, result, parent)+ : totalOrFunc;+ const parser = readBytes(byteSize);+ const arr = new Array(total);++ for (let i = 0; i < total; i++) {+ arr[i] = parser(stream);+ }++ return arr;+ };+};++const subBitsTotal = function subBitsTotal(bits, startIndex, length) {+ let result = 0;++ for (let i = 0; i < length; i++) {+ result += bits[startIndex + i] && 2 ** (length - i - 1);+ }++ return result;+};++export const readBits = (schema) => {+ return function (stream) {+ const _byte = readByte()(stream); // convert the byte to bit array++ const bits = new Array(8);++ for (let i = 0; i < 8; i++) {+ bits[7 - i] = Boolean(_byte & (1 << i));+ } // convert the bit array to values based on the schema++ return Object.keys(schema).reduce((res, key) => {+ const def = schema[key];++ if (def.length) {+ res[key] = subBitsTotal(bits, def.index, def.length);+ } else {+ res[key] = bits[def.index];+ }++ return res;+ }, {});+ };+};diff --git a/packages/gif/src/worker/source.ts b/packages/gif/src/worker/source.tsindex 53c665d814..e7941de45d 100644--- a/packages/gif/src/worker/source.ts+++ b/packages/gif/src/worker/source.ts@@ -1,4 +1,4 @@// Auto-generated by build.mjsexport const src =// eslint-disable-next-line no-template-curly-in-string- "\"use strict\";(()=>{var N=Object.create;var j=Object.defineProperty;var Q=Object.getOwnPropertyDescriptor;var V=Object.getOwnPropertyNames;var Y=Object.getPrototypeOf,ee=Object.prototype.hasOwnProperty;var T=(t,e)=>()=>(e||t((e={exports:{}}).exports,e),e.exports);var re=(t,e,r,n)=>{if(e&&typeof e==\"object\"||typeof e==\"function\")for(let o of V(e))!ee.call(t,o)&&o!==r&&j(t,o,{get:()=>e[o],enumerable:!(n=Q(e,o))||n.enumerable});return t};var G=(t,e,r)=>(r=t!=null?N(Y(t)):{},re(e||!t||!t.__esModule?j(r,\"default\",{value:t,enumerable:!0}):r,t));var z=T(y=>{\"use strict\";Object.defineProperty(y,\"__esModule\",{value:!0});y.loop=y.conditional=y.parse=void 0;var te=function t(e,r){var n=arguments.length>2&&arguments[2]!==void 0?arguments[2]:{},o=arguments.length>3&&arguments[3]!==void 0?arguments[3]:n;if(Array.isArray(r))r.forEach(function(a){return t(e,a,n,o)});else if(typeof r==\"function\")r(e,n,o,t);else{var i=Object.keys(r)[0];Array.isArray(r[i])?(o[i]={},t(e,r[i],n,o[i])):o[i]=r[i](e,n,o,t)}return n};y.parse=te;var ne=function(e,r){return function(n,o,i,a){r(n,o,i)&&a(n,e,o,i)}};y.conditional=ne;var ae=function(e,r){return function(n,o,i,a){for(var c=[],d=n.pos;r(n,o,i);){var u={};if(a(n,e,o,u),n.pos===d)break;d=n.pos,c.push(u)}return c}};y.loop=ae});var E=T(p=>{\"use strict\";Object.defineProperty(p,\"__esModule\",{value:!0});p.readBits=p.readArray=p.readUnsigned=p.readString=p.peekBytes=p.readBytes=p.peekByte=p.readByte=p.buildStream=void 0;var oe=function(e){return{data:e,pos:0}};p.buildStream=oe;var W=function(){return function(e){return e.data[e.pos++]}};p.readByte=W;var ie=function(){var e=arguments.length>0&&arguments[0]!==void 0?arguments[0]:0;return function(r){return r.data[r.pos+e]}};p.peekByte=ie;var _=function(e){return function(r){return r.data.subarray(r.pos,r.pos+=e)}};p.readBytes=_;var se=function(e){return function(r){return r.data.subarray(r.pos,r.pos+e)}};p.peekBytes=se;var de=function(e){return function(r){return Array.from(_(e)(r)).map(function(n){return String.fromCharCode(n)}).join(\"\")}};p.readString=de;var ce=function(e){return function(r){var n=_(2)(r);return e?(n[1]<<8)+n[0]:(n[0]<<8)+n[1]}};p.readUnsigned=ce;var ue=function(e,r){return function(n,o,i){for(var a=typeof r==\"function\"?r(n,o,i):r,c=_(e),d=new Array(a),u=0;u<a;u++)d[u]=c(n);return d}};p.readArray=ue;var le=function(e,r,n){for(var o=0,i=0;i<n;i++)o+=e[r+i]&&Math.pow(2,n-i-1);return o},pe=function(e){return function(r){for(var n=W()(r),o=new Array(8),i=0;i<8;i++)o[7-i]=!!(n&1<<i);return Object.keys(e).reduce(function(a,c){var d=e[c];return d.length?a[c]=le(o,d.index,d.length):a[c]=o[d.index],a},{})}};p.readBits=pe});var K=T(I=>{\"use strict\";Object.defineProperty(I,\"__esModule\",{value:!0});I.default=void 0;var h=z(),s=E(),F={blocks:function(e){for(var r=0,n=[],o=e.data.length,i=0,a=(0,s.readByte)()(e);a!==r&&a;a=(0,s.readByte)()(e)){if(e.pos+a>=o){var c=o-e.pos;n.push((0,s.readBytes)(c)(e)),i+=c;break}n.push((0,s.readBytes)(a)(e)),i+=a}for(var d=new Uint8Array(i),u=0,f=0;f<n.length;f++)d.set(n[f],u),u+=n[f].length;return d}},fe=(0,h.conditional)({gce:[{codes:(0,s.readBytes)(2)},{byteSize:(0,s.readByte)()},{extras:(0,s.readBits)({future:{index:0,length:3},disposal:{index:3,length:3},userInput:{index:6},transparentColorGiven:{index:7}})},{delay:(0,s.readUnsigned)(!0)},{transparentColorIndex:(0,s.readByte)()},{terminator:(0,s.readByte)()}]},function(t){var e=(0,s.peekBytes)(2)(t);return e[0]===33&&e[1]===249}),me=(0,h.conditional)({image:[{code:(0,s.readByte)()},{descriptor:[{left:(0,s.readUnsigned)(!0)},{top:(0,s.readUnsigned)(!0)},{width:(0,s.readUnsigned)(!0)},{height:(0,s.readUnsigned)(!0)},{lct:(0,s.readBits)({exists:{index:0},interlaced:{index:1},sort:{index:2},future:{index:3,length:2},size:{index:5,length:3}})}]},(0,h.conditional)({lct:(0,s.readArray)(3,function(t,e,r){return Math.pow(2,r.descriptor.lct.size+1)})},function(t,e,r){return r.descriptor.lct.exists}),{data:[{minCodeSize:(0,s.readByte)()},F]}]},function(t){return(0,s.peekByte)()(t)===44}),ge=(0,h.conditional)({text:[{codes:(0,s.readBytes)(2)},{blockSize:(0,s.readByte)()},{preData:function(e,r,n){return(0,s.readBytes)(n.text.blockSize)(e)}},F]},function(t){var e=(0,s.peekBytes)(2)(t);return e[0]===33&&e[1]===1}),ye=(0,h.conditional)({application:[{codes:(0,s.readBytes)(2)},{blockSize:(0,s.readByte)()},{id:function(e,r,n){return(0,s.readString)(n.blockSize)(e)}},F]},function(t){var e=(0,s.peekBytes)(2)(t);return e[0]===33&&e[1]===255}),he=(0,h.conditional)({comment:[{codes:(0,s.readBytes)(2)},F]},function(t){var e=(0,s.peekBytes)(2)(t);return e[0]===33&&e[1]===254}),ve=[{header:[{signature:(0,s.readString)(3)},{version:(0,s.readString)(3)}]},{lsd:[{width:(0,s.readUnsigned)(!0)},{height:(0,s.readUnsigned)(!0)},{gct:(0,s.readBits)({exists:{index:0},resolution:{index:1,length:3},sort:{index:4},size:{index:5,length:3}})},{backgroundColorIndex:(0,s.readByte)()},{pixelAspectRatio:(0,s.readByte)()}]},(0,h.conditional)({gct:(0,s.readArray)(3,function(t,e){return Math.pow(2,e.lsd.gct.size+1)})},function(t,e){return e.lsd.gct.exists}),{frames:(0,h.loop)([fe,ye,he,me,ge],function(t){var e=(0,s.peekByte)()(t);return e===33||e===44})}],be=ve;I.default=be});var D=G(z()),O=G(E()),R=G(K());var X=(t,e)=>{let r=new Array(t.length),n=t.length/e,o=function(d,u){let f=t.slice(u*e,(u+1)*e);r.splice(...[d*e,e].concat(f))},i=[0,4,2,1],a=[8,8,4,2],c=0;for(let d=0;d<4;d++)for(let u=i[d];u<n;u+=a[d])o(u,c),c++;return r};var Z=(t,e,r)=>{let i=r,a,c,d,u,f;var B;let l,m;var C,b,g,k,P;let v=new Array(r),U=new Array(4096),A=new Array(4096),S=new Array(4096+1),M=t,x=1<<M,J=x+1;for(a=x+2,f=-1,d=M+1,c=(1<<d)-1,l=0;l<x;l++)U[l]=0,A[l]=l;var C,B,b,g,P,k;for(C=B=b=g=P=k=0,m=0;m<i;){if(g===0){if(B<d){C+=e[k]<<B,B+=8,k++;continue}if(l=C&c,C>>=d,B-=d,l>a||l===J)break;if(l===x){d=M+1,c=(1<<d)-1,a=x+2,f=-1;continue}if(f===-1){S[g++]=A[l],f=l,b=l;continue}for(u=l,l===a&&(S[g++]=b,l=f);l>x;)S[g++]=A[l],l=U[l];b=A[l]&255,S[g++]=b,a<4096&&(U[a]=f,A[a]=b,a++,!(a&c)&&a<4096&&(d++,c+=a)),f=u}g--,v[P++]=S[g],m++}for(m=P;m<i;m++)v[m]=0;return v};var q=t=>{let e=new Uint8Array(t);return(0,D.parse)((0,O.buildStream)(e),R.default)},L=(t,e)=>{var a,c;if(!t.image){console.warn(\"gif frame does not have associated image.\");return}let{image:r}=t,n=r.descriptor.width*r.descriptor.height,o=Z(r.data.minCodeSize,r.data.blocks,n);return(a=r.descriptor.lct)!=null&&a.interlaced&&(o=X(o,r.descriptor.width)),{pixels:o,dims:{top:t.image.descriptor.top,left:t.image.descriptor.left,width:t.image.descriptor.width,height:t.image.descriptor.height},colorTable:(c=r.descriptor.lct)!=null&&c.exists?r.lct:e,delay:(t.gce.delay||10)*10,disposalType:t.gce.extras.disposal,transparentIndex:t.gce.extras.transparentColorGiven?t.gce.transparentColorIndex:-1}};var $=t=>t.frames.filter(e=>!(\"application\"in e)).map(e=>e.image?L(e,t.gct):null).filter(Boolean).map(e=>e);var xe=t=>{let e=null;for(let r of t.frames)e=r.gce?r.gce:e,\"image\"in r&&!(\"gce\"in r)&&e!==null&&(r.gce=e)},H=(t,{signal:e})=>fetch(t,{signal:e}).then(r=>{var n;if(!((n=r.headers.get(\"Content-Type\"))!=null&&n.includes(\"image/gif\")))throw Error(`Wrong content type: \"${r.headers.get(\"Content-Type\")}\"`);return r.arrayBuffer()}).then(r=>q(r)).then(r=>(xe(r),r)).then(r=>Promise.all([$(r),{width:r.lsd.width,height:r.lsd.height}])).then(([r,n])=>{let o=[],i=n.width*n.height*4,a=new Uint8ClampedArray(i);for(let c=0;c<r.length;++c){let d=r[c],u=r[c].disposalType===3?a.slice():null;if(o.push(Be(a,d,n)),r[c].disposalType===2)a=new Uint8ClampedArray(i);else if(r[c].disposalType===3){if(!u)throw Error(\"Disposal type 3 without previous frame\");a=u}else a=o[c].slice()}return{...n,loaded:!0,delays:r.map(c=>c.delay),frames:o}}),Be=(t,e,r)=>{let{width:n,height:o,top:i,left:a}=e.dims,c=i*r.width+a;for(let d=0;d<o;d++)for(let u=0;u<n;u++){let f=d*n+u,l=e.pixels[f];if(l!==e.transparentIndex){let m=c+d*r.width+u,v=e.colorTable[l];t[m*4]=v[0],t[m*4+1]=v[1],t[m*4+2]=v[2],t[m*4+3]=l===e.transparentIndex?0:255}}return t};var w=new Map;self.addEventListener(\"message\",t=>{let{type:e,src:r}=t.data||t;switch(e){case\"parse\":{if(!w.has(r)){let n=new AbortController,o={signal:n.signal};w.set(r,n),H(r,o).then(i=>{self.postMessage(Object.assign(i,{src:r}),i.frames.map(a=>a.buffer))}).catch(i=>{self.postMessage({src:r,error:i,loaded:!0})}).finally(()=>{w.delete(r)})}break}case\"cancel\":{w.has(r)&&(w.get(r).abort(),w.delete(r));break}default:break}});})();\n";\ No newline at end of file+ "\"use strict\";(()=>{var N=Object.create;var K=Object.defineProperty;var Q=Object.getOwnPropertyDescriptor;var V=Object.getOwnPropertyNames;var Y=Object.getPrototypeOf,ee=Object.prototype.hasOwnProperty;var T=(t,e)=>()=>(e||t((e={exports:{}}).exports,e),e.exports);var re=(t,e,r,n)=>{if(e&&typeof e==\"object\"||typeof e==\"function\")for(let o of V(e))!ee.call(t,o)&&o!==r&&K(t,o,{get:()=>e[o],enumerable:!(n=Q(e,o))||n.enumerable});return t};var G=(t,e,r)=>(r=t!=null?N(Y(t)):{},re(e||!t||!t.__esModule?K(r,\"default\",{value:t,enumerable:!0}):r,t));var z=T(y=>{\"use strict\";Object.defineProperty(y,\"__esModule\",{value:!0});y.loop=y.conditional=y.parse=void 0;var te=function t(e,r){var n=arguments.length>2&&arguments[2]!==void 0?arguments[2]:{},o=arguments.length>3&&arguments[3]!==void 0?arguments[3]:n;if(Array.isArray(r))r.forEach(function(a){return t(e,a,n,o)});else if(typeof r==\"function\")r(e,n,o,t);else{var i=Object.keys(r)[0];Array.isArray(r[i])?(o[i]={},t(e,r[i],n,o[i])):o[i]=r[i](e,n,o,t)}return n};y.parse=te;var ne=function(e,r){return function(n,o,i,a){r(n,o,i)&&a(n,e,o,i)}};y.conditional=ne;var ae=function(e,r){return function(n,o,i,a){for(var c=[],d=n.pos;r(n,o,i);){var l={};if(a(n,e,o,l),n.pos===d)break;d=n.pos,c.push(l)}return c}};y.loop=ae});var E=T(p=>{\"use strict\";Object.defineProperty(p,\"__esModule\",{value:!0});p.readBits=p.readArray=p.readUnsigned=p.readString=p.peekBytes=p.readBytes=p.peekByte=p.readByte=p.buildStream=void 0;var oe=function(e){return{data:e,pos:0}};p.buildStream=oe;var X=function(){return function(e){return e.data[e.pos++]}};p.readByte=X;var ie=function(){var e=arguments.length>0&&arguments[0]!==void 0?arguments[0]:0;return function(r){return r.data[r.pos+e]}};p.peekByte=ie;var _=function(e){return function(r){return r.data.subarray(r.pos,r.pos+=e)}};p.readBytes=_;var se=function(e){return function(r){return r.data.subarray(r.pos,r.pos+e)}};p.peekBytes=se;var de=function(e){return function(r){return Array.from(_(e)(r)).map(function(n){return String.fromCharCode(n)}).join(\"\")}};p.readString=de;var ce=function(e){return function(r){var n=_(2)(r);return e?(n[1]<<8)+n[0]:(n[0]<<8)+n[1]}};p.readUnsigned=ce;var le=function(e,r){return function(n,o,i){for(var a=typeof r==\"function\"?r(n,o,i):r,c=_(e),d=new Array(a),l=0;l<a;l++)d[l]=c(n);return d}};p.readArray=le;var ue=function(e,r,n){for(var o=0,i=0;i<n;i++)o+=e[r+i]&&Math.pow(2,n-i-1);return o},pe=function(e){return function(r){for(var n=X()(r),o=new Array(8),i=0;i<8;i++)o[7-i]=!!(n&1<<i);return Object.keys(e).reduce(function(a,c){var d=e[c];return d.length?a[c]=ue(o,d.index,d.length):a[c]=o[d.index],a},{})}};p.readBits=pe});var Z=T(I=>{\"use strict\";Object.defineProperty(I,\"__esModule\",{value:!0});I.default=void 0;var h=z(),s=E(),F={blocks:function(e){for(var r=0,n=[],o=e.data.length,i=0,a=(0,s.readByte)()(e);a!==r&&a;a=(0,s.readByte)()(e)){if(e.pos+a>=o){var c=o-e.pos;n.push((0,s.readBytes)(c)(e)),i+=c;break}n.push((0,s.readBytes)(a)(e)),i+=a}for(var d=new Uint8Array(i),l=0,f=0;f<n.length;f++)d.set(n[f],l),l+=n[f].length;return d}},fe=(0,h.conditional)({gce:[{codes:(0,s.readBytes)(2)},{byteSize:(0,s.readByte)()},{extras:(0,s.readBits)({future:{index:0,length:3},disposal:{index:3,length:3},userInput:{index:6},transparentColorGiven:{index:7}})},{delay:(0,s.readUnsigned)(!0)},{transparentColorIndex:(0,s.readByte)()},{terminator:(0,s.readByte)()}]},function(t){var e=(0,s.peekBytes)(2)(t);return e[0]===33&&e[1]===249}),me=(0,h.conditional)({image:[{code:(0,s.readByte)()},{descriptor:[{left:(0,s.readUnsigned)(!0)},{top:(0,s.readUnsigned)(!0)},{width:(0,s.readUnsigned)(!0)},{height:(0,s.readUnsigned)(!0)},{lct:(0,s.readBits)({exists:{index:0},interlaced:{index:1},sort:{index:2},future:{index:3,length:2},size:{index:5,length:3}})}]},(0,h.conditional)({lct:(0,s.readArray)(3,function(t,e,r){return Math.pow(2,r.descriptor.lct.size+1)})},function(t,e,r){return r.descriptor.lct.exists}),{data:[{minCodeSize:(0,s.readByte)()},F]}]},function(t){return(0,s.peekByte)()(t)===44}),ge=(0,h.conditional)({text:[{codes:(0,s.readBytes)(2)},{blockSize:(0,s.readByte)()},{preData:function(e,r,n){return(0,s.readBytes)(n.text.blockSize)(e)}},F]},function(t){var e=(0,s.peekBytes)(2)(t);return e[0]===33&&e[1]===1}),ye=(0,h.conditional)({application:[{codes:(0,s.readBytes)(2)},{blockSize:(0,s.readByte)()},{id:function(e,r,n){return(0,s.readString)(n.blockSize)(e)}},F]},function(t){var e=(0,s.peekBytes)(2)(t);return e[0]===33&&e[1]===255}),he=(0,h.conditional)({comment:[{codes:(0,s.readBytes)(2)},F]},function(t){var e=(0,s.peekBytes)(2)(t);return e[0]===33&&e[1]===254}),ve=[{header:[{signature:(0,s.readString)(3)},{version:(0,s.readString)(3)}]},{lsd:[{width:(0,s.readUnsigned)(!0)},{height:(0,s.readUnsigned)(!0)},{gct:(0,s.readBits)({exists:{index:0},resolution:{index:1,length:3},sort:{index:4},size:{index:5,length:3}})},{backgroundColorIndex:(0,s.readByte)()},{pixelAspectRatio:(0,s.readByte)()}]},(0,h.conditional)({gct:(0,s.readArray)(3,function(t,e){return Math.pow(2,e.lsd.gct.size+1)})},function(t,e){return e.lsd.gct.exists}),{frames:(0,h.loop)([fe,ye,he,me,ge],function(t){var e=(0,s.peekByte)()(t);return e===33||e===44})}],be=ve;I.default=be});var j=G(z()),W=G(E()),R=G(Z());var D=(t,e)=>{let r=new Array(t.length),n=t.length/e,o=function(d,l){let f=t.slice(l*e,(l+1)*e);r.splice(...[d*e,e].concat(f))},i=[0,4,2,1],a=[8,8,4,2],c=0;for(let d=0;d<4;d++)for(let l=i[d];l<n;l+=a[d])o(l,c),c++;return r};var O=(t,e,r)=>{let i=r,a,c,d,l,f;var B;let u,m;var C,b,g,k,P;let v=new Array(r),U=new Array(4096),A=new Array(4096),S=new Array(4096+1),M=t,x=1<<M,J=x+1;for(a=x+2,f=-1,d=M+1,c=(1<<d)-1,u=0;u<x;u++)U[u]=0,A[u]=u;var C,B,b,g,P,k;for(C=B=b=g=P=k=0,m=0;m<i;){if(g===0){if(B<d){C+=e[k]<<B,B+=8,k++;continue}if(u=C&c,C>>=d,B-=d,u>a||u===J)break;if(u===x){d=M+1,c=(1<<d)-1,a=x+2,f=-1;continue}if(f===-1){S[g++]=A[u],f=u,b=u;continue}for(l=u,u===a&&(S[g++]=b,u=f);u>x;)S[g++]=A[u],u=U[u];b=A[u]&255,S[g++]=b,a<4096&&(U[a]=f,A[a]=b,a++,!(a&c)&&a<4096&&(d++,c+=a)),f=l}g--,v[P++]=S[g],m++}for(m=P;m<i;m++)v[m]=0;return v};console.log({parser:j.default,builder:W.default});var q=t=>{let e=new Uint8Array(t);return j.default.parse(W.default.buildStream(e),R.default)},L=(t,e)=>{var a,c;if(!t.image){console.warn(\"gif frame does not have associated image.\");return}let{image:r}=t,n=r.descriptor.width*r.descriptor.height,o=O(r.data.minCodeSize,r.data.blocks,n);return(a=r.descriptor.lct)!=null&&a.interlaced&&(o=D(o,r.descriptor.width)),{pixels:o,dims:{top:t.image.descriptor.top,left:t.image.descriptor.left,width:t.image.descriptor.width,height:t.image.descriptor.height},colorTable:(c=r.descriptor.lct)!=null&&c.exists?r.lct:e,delay:(t.gce.delay||10)*10,disposalType:t.gce.extras.disposal,transparentIndex:t.gce.extras.transparentColorGiven?t.gce.transparentColorIndex:-1}};var $=t=>t.frames.filter(e=>!(\"application\"in e)).map(e=>e.image?L(e,t.gct):null).filter(Boolean).map(e=>e);var xe=t=>{let e=null;for(let r of t.frames)e=r.gce?r.gce:e,\"image\"in r&&!(\"gce\"in r)&&e!==null&&(r.gce=e)},H=(t,{signal:e})=>fetch(t,{signal:e}).then(r=>{var n;if(!((n=r.headers.get(\"Content-Type\"))!=null&&n.includes(\"image/gif\")))throw Error(`Wrong content type: \"${r.headers.get(\"Content-Type\")}\"`);return r.arrayBuffer()}).then(r=>q(r)).then(r=>(xe(r),r)).then(r=>Promise.all([$(r),{width:r.lsd.width,height:r.lsd.height}])).then(([r,n])=>{let o=[],i=n.width*n.height*4,a=new Uint8ClampedArray(i);for(let c=0;c<r.length;++c){let d=r[c],l=r[c].disposalType===3?a.slice():null;if(o.push(Be(a,d,n)),r[c].disposalType===2)a=new Uint8ClampedArray(i);else if(r[c].disposalType===3){if(!l)throw Error(\"Disposal type 3 without previous frame\");a=l}else a=o[c].slice()}return{...n,loaded:!0,delays:r.map(c=>c.delay),frames:o}}),Be=(t,e,r)=>{let{width:n,height:o,top:i,left:a}=e.dims,c=i*r.width+a;for(let d=0;d<o;d++)for(let l=0;l<n;l++){let f=d*n+l,u=e.pixels[f];if(u!==e.transparentIndex){let m=c+d*r.width+l,v=e.colorTable[u];t[m*4]=v[0],t[m*4+1]=v[1],t[m*4+2]=v[2],t[m*4+3]=u===e.transparentIndex?0:255}}return t};var w=new Map;self.addEventListener(\"message\",t=>{let{type:e,src:r}=t.data||t;switch(e){case\"parse\":{if(!w.has(r)){let n=new AbortController,o={signal:n.signal};w.set(r,n),H(r,o).then(i=>{self.postMessage(Object.assign(i,{src:r}),i.frames.map(a=>a.buffer))}).catch(i=>{self.postMessage({src:r,error:i,loaded:!0})}).finally(()=>{w.delete(r)})}break}case\"cancel\":{w.has(r)&&(w.get(r).abort(),w.delete(r));break}default:break}});})();\n";\ No newline at end of filediff --git a/packages/gif/tsconfig-esm.json b/packages/gif/tsconfig-esm.jsonnew file mode 100644index 0000000000..93ac160761--- /dev/null+++ b/packages/gif/tsconfig-esm.json@@ -0,0 +1,13 @@+{+ "extends": "../tsconfig-esm.settings.json",+ "compilerOptions": {+ "composite": true,+ "rootDir": "./src",+ "outDir": "./dist/esm",+ "skipLibCheck": true,+ "jsx": "react-jsx",+ "moduleResolution": "node"+ },+ "references": [{"path": "../core"}],+ "include": ["./src"]+}diff --git a/packages/gif/tsconfig.json b/packages/gif/tsconfig.jsonindex 5fbc2a7348..c30b747fec 100644--- a/packages/gif/tsconfig.json+++ b/packages/gif/tsconfig.json@@ -3,10 +3,10 @@"compilerOptions": {"composite": true,"rootDir": "./src",- "outDir": "./dist",+ "outDir": "./dist/cjs","skipLibCheck": true,"jsx": "react-jsx"},- "include": ["./src"],- "references": [{"path": "../core"}]+ "references": [{"path": "../core"}],+ "include": ["./src"]}diff --git a/pnpm-lock.yaml b/pnpm-lock.yamlindex 607e1d5121..1ebffc40e7 100644--- a/pnpm-lock.yaml+++ b/pnpm-lock.yaml@@ -563,6 +563,7 @@ importers:packages/gif:specifiers:'@jonny/eslint-config': 3.0.266+ '@rollup/plugin-typescript': ^8.2.0'@types/node': ^16.7.5'@types/react': 18.0.26'@types/react-dom': 18.0.10@@ -575,6 +576,7 @@ importers:react: 18.0.0react-dom: 18.0.0remotion: 3.3.62+ rollup: ^2.70.1typescript: ^4.7.0webpack: 5.74.0dependencies:@@ -583,6 +585,7 @@ importers:remotion: link:../coredevDependencies:'@jonny/eslint-config': 3.0.266_t54va6jeo5lf3vtqih5uiyrm5e+ '@rollup/plugin-typescript': 8.5.0_2s5nfqwcf62746iqzotp3mpuy4'@types/node': 16.10.2'@types/react': 18.0.26'@types/react-dom': 18.0.10@@ -592,6 +595,7 @@ importers:prettier-plugin-organize-imports: 2.3.4_lbsuufegg2dnc4wh6qgf26kgdyreact: 18.0.0react-dom: [email protected]+ rollup: 2.76.0typescript: 4.7.2webpack: [email protected]@@ -24844,8 +24848,8 @@ packages:'@webassemblyjs/ast': 1.11.1'@webassemblyjs/wasm-edit': 1.11.1'@webassemblyjs/wasm-parser': 1.11.1- acorn: 8.8.1- acorn-import-assertions: [email protected]+ acorn: 8.8.0+ acorn-import-assertions: [email protected]browserslist: 4.21.4chrome-trace-event: 1.0.3enhanced-resolve: 5.10.0