Skip to content

🎥 Make videos programmatically with React

application

Jonny Burgerf791adb79c7c
diff --git a/packages/gif/src/gifuct/types.ts b/packages/gif/src/gifuct/types.ts
index ffe22a9eb9..9920e3397a 100644
--- a/packages/gif/src/gifuct/types.ts
+++ b/packages/gif/src/gifuct/types.ts
@@ -5,7 +5,7 @@ type Dimensions = {
height: number;
};
-type Application = {
+export type Application = {
application: {
blockSize: number;
blocks: number[];
diff --git a/packages/gif/src/js-binary-schema-parser/gif.ts b/packages/gif/src/js-binary-schema-parser/gif.ts
index 08fee704b0..5a5570e27b 100644
--- a/packages/gif/src/js-binary-schema-parser/gif.ts
+++ b/packages/gif/src/js-binary-schema-parser/gif.ts
@@ -1,4 +1,5 @@
-import {conditional} from './parser';
+import type {Application, Frame, ParsedGif} from '../gifuct/types';
+import {conditional, loop} from './parser';
import type {Stream} from './uint8-parser';
import {
peekByte,
@@ -68,7 +69,7 @@ const gceSchema = conditional(
{terminator: readByte()},
],
},
- (stream) => {
+ (stream: Stream) => {
const codes = peekBytes(2)(stream);
return codes[0] === 0x21 && codes[1] === 0xf9;
}
@@ -98,18 +99,21 @@ const imageSchema = conditional(
},
conditional(
{
- lct: readArray(3, (stream, result, parent) => {
- return 2 ** (parent.descriptor.lct.size + 1);
- }),
+ lct: readArray(
+ 3,
+ (_stream: Stream, _result: unknown, parent: Frame['image']) => {
+ return 2 ** (parent.descriptor.lct.size + 1);
+ }
+ ),
},
- (stream, result, parent) => {
+ (_stream: Stream, _result: unknown, parent: Frame['image']) => {
return parent.descriptor.lct.exists;
}
),
{data: [{minCodeSize: readByte()}, subBlocksSchema]},
],
},
- (stream) => {
+ (stream: Stream) => {
return peekByte()(stream) === 0x2c;
}
);
@@ -121,13 +125,13 @@ const textSchema = conditional(
{codes: readBytes(2)},
{blockSize: readByte()},
{
- preData: (stream, result, parent) =>
+ preData: (stream: Stream, _result: unknown, parent: Frame['image']) =>
readBytes(parent.text.blockSize)(stream),
},
subBlocksSchema,
],
},
- (stream) => {
+ (stream: Stream) => {
const codes = peekBytes(2)(stream);
return codes[0] === 0x21 && codes[1] === 0x01;
}
@@ -139,11 +143,17 @@ const applicationSchema = conditional(
application: [
{codes: readBytes(2)},
{blockSize: readByte()},
- {id: (stream, result, parent) => readString(parent.blockSize)(stream)},
+ {
+ id: (
+ stream: Stream,
+ _result: unknown,
+ parent: Application['application']
+ ) => readString(parent.blockSize)(stream),
+ },
subBlocksSchema,
],
},
- (stream) => {
+ (stream: Stream) => {
const codes = peekBytes(2)(stream);
return codes[0] === 0x21 && codes[1] === 0xff;
}
@@ -154,7 +164,7 @@ const commentSchema = conditional(
{
comment: [{codes: readBytes(2)}, subBlocksSchema],
},
- (stream) => {
+ (stream: Stream) => {
const codes = peekBytes(2)(stream);
return codes[0] === 0x21 && codes[1] === 0xfe;
}
@@ -180,15 +190,18 @@ export const GIF = [
},
conditional(
{
- gct: readArray(3, (stream, result) => 2 ** (result.lsd.gct.size + 1)),
+ gct: readArray(
+ 3,
+ (_stream, result: ParsedGif) => 2 ** (result.lsd.gct.size + 1)
+ ),
},
- (stream, result) => result.lsd.gct.exists
+ (_stream: Stream, result: ParsedGif) => result.lsd.gct.exists
),
// content frames
{
frames: loop(
[gceSchema, applicationSchema, commentSchema, imageSchema, textSchema],
- (stream) => {
+ (stream: 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
diff --git a/packages/gif/src/js-binary-schema-parser/parser.ts b/packages/gif/src/js-binary-schema-parser/parser.ts
index 3ad316c8e4..6261afe534 100644
--- a/packages/gif/src/js-binary-schema-parser/parser.ts
+++ b/packages/gif/src/js-binary-schema-parser/parser.ts
@@ -1,4 +1,7 @@
-export const parse = function parse(stream, schema) {
+import type {GIF} from './gif';
+import type {Stream} from './uint8-parser';
+
+export const parse = function parse(stream: Stream, schema: typeof GIF) {
const result =
arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
const parent =
@@ -24,16 +27,16 @@ export const parse = function parse(stream, schema) {
return result;
};
-export const conditional = (schema, conditionFunc) => {
- return function (stream, result, parent, parse) {
+export const conditional = (schema: typeof GIF, conditionFunc) => {
+ return function (stream: 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) {
+export const loop = (schema: typeof GIF, continueFunc) => {
+ return function (stream: Stream, result, parent, parse) {
const arr = [];
let lastStreamPos = stream.pos;
diff --git a/packages/gif/src/js-binary-schema-parser/uint8-parser.ts b/packages/gif/src/js-binary-schema-parser/uint8-parser.ts
index 38c2f8c940..438f990530 100644
--- a/packages/gif/src/js-binary-schema-parser/uint8-parser.ts
+++ b/packages/gif/src/js-binary-schema-parser/uint8-parser.ts
@@ -1,6 +1,7 @@
/* eslint-disable no-bitwise */
// Default stream and parsers for Uint8TypedArray data type
+import type {Frame} from '../gifuct/types';
import type {GIF} from './gif';
export type Stream = {
@@ -44,7 +45,11 @@ export const readUnsigned = (littleEndian: boolean) => (stream: Stream) => {
};
export const readArray =
- (byteSize: number, totalOrFunc) => (stream: Stream, result, parent) => {
+ <T>(
+ byteSize: number,
+ totalOrFunc: number | ((st: Stream, r: T, p: unknown) => number)
+ ) =>
+ (stream: Stream, result: T, parent: Frame['image']) => {
const total =
typeof totalOrFunc === 'function'
? totalOrFunc(stream, result, parent)
@@ -72,23 +77,28 @@ const subBitsTotal = (
return result;
};
-export const readBits = (schema: typeof GIF) => (stream: Stream) => {
- const byte = readByte()(stream);
- // convert the byte to bit array
- const bits = new Array<boolean>(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];
+export const readBits =
+ (schema: typeof GIF) =>
+ (stream: Stream): Record<string, number | boolean> => {
+ const byte = readByte()(stream);
+ // convert the byte to bit array
+ const bits = new Array<boolean>(8);
+ for (let i = 0; i < 8; i++) {
+ bits[7 - i] = Boolean(byte & (1 << i));
}
- return res;
- }, {} as Record<string, number>);
-};
+ // 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;
+ }, {} as Record<string, number | boolean>) as Record<
+ string,
+ number | boolean
+ >;
+ };