@remotion/gif 4.1.0-alpha10 β 4.1.0-alpha12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.rollup.cache/Users/jonathanburger/remotion/packages/gif/dist/esm/Gif.d.ts +7 -0
- package/.rollup.cache/Users/jonathanburger/remotion/packages/gif/dist/esm/Gif.js +16 -0
- package/.rollup.cache/Users/jonathanburger/remotion/packages/gif/dist/esm/GifForDevelopment.d.ts +3 -0
- package/.rollup.cache/Users/jonathanburger/remotion/packages/gif/dist/esm/GifForDevelopment.js +78 -0
- package/.rollup.cache/Users/jonathanburger/remotion/packages/gif/dist/esm/GifForRendering.d.ts +3 -0
- package/.rollup.cache/Users/jonathanburger/remotion/packages/gif/dist/esm/GifForRendering.js +82 -0
- package/.rollup.cache/Users/jonathanburger/remotion/packages/gif/dist/esm/canvas.d.ts +13 -0
- package/.rollup.cache/Users/jonathanburger/remotion/packages/gif/dist/esm/canvas.js +93 -0
- package/.rollup.cache/Users/jonathanburger/remotion/packages/gif/dist/esm/get-gif-duration-in-seconds.d.ts +5 -0
- package/.rollup.cache/Users/jonathanburger/remotion/packages/gif/dist/esm/get-gif-duration-in-seconds.js +30 -0
- package/.rollup.cache/Users/jonathanburger/remotion/packages/gif/dist/esm/gif-cache.d.ts +4 -0
- package/.rollup.cache/Users/jonathanburger/remotion/packages/gif/dist/esm/gif-cache.js +3 -0
- package/.rollup.cache/Users/jonathanburger/remotion/packages/gif/dist/esm/gifuct/deinterlace.d.ts +4 -0
- package/.rollup.cache/Users/jonathanburger/remotion/packages/gif/dist/esm/gifuct/deinterlace.js +22 -0
- package/.rollup.cache/Users/jonathanburger/remotion/packages/gif/dist/esm/gifuct/index.d.ts +4 -0
- package/.rollup.cache/Users/jonathanburger/remotion/packages/gif/dist/esm/gifuct/index.js +48 -0
- package/.rollup.cache/Users/jonathanburger/remotion/packages/gif/dist/esm/gifuct/lzw.d.ts +5 -0
- package/.rollup.cache/Users/jonathanburger/remotion/packages/gif/dist/esm/gifuct/lzw.js +115 -0
- package/.rollup.cache/Users/jonathanburger/remotion/packages/gif/dist/esm/gifuct/types.d.ts +96 -0
- package/.rollup.cache/Users/jonathanburger/remotion/packages/gif/dist/esm/gifuct/types.js +1 -0
- package/.rollup.cache/Users/jonathanburger/remotion/packages/gif/dist/esm/index.d.ts +4 -0
- package/.rollup.cache/Users/jonathanburger/remotion/packages/gif/dist/esm/index.js +3 -0
- package/.rollup.cache/Users/jonathanburger/remotion/packages/gif/dist/esm/is-cors-error.d.ts +1 -0
- package/.rollup.cache/Users/jonathanburger/remotion/packages/gif/dist/esm/is-cors-error.js +9 -0
- package/.rollup.cache/Users/jonathanburger/remotion/packages/gif/dist/esm/js-binary-schema-parser/gif.d.ts +51 -0
- package/.rollup.cache/Users/jonathanburger/remotion/packages/gif/dist/esm/js-binary-schema-parser/gif.js +155 -0
- package/.rollup.cache/Users/jonathanburger/remotion/packages/gif/dist/esm/js-binary-schema-parser/parser.d.ts +9 -0
- package/.rollup.cache/Users/jonathanburger/remotion/packages/gif/dist/esm/js-binary-schema-parser/parser.js +48 -0
- package/.rollup.cache/Users/jonathanburger/remotion/packages/gif/dist/esm/js-binary-schema-parser/uint8-parser.d.ts +17 -0
- package/.rollup.cache/Users/jonathanburger/remotion/packages/gif/dist/esm/js-binary-schema-parser/uint8-parser.js +67 -0
- package/.rollup.cache/Users/jonathanburger/remotion/packages/gif/dist/esm/lru/index.d.ts +101 -0
- package/.rollup.cache/Users/jonathanburger/remotion/packages/gif/dist/esm/lru/index.js +254 -0
- package/.rollup.cache/Users/jonathanburger/remotion/packages/gif/dist/esm/parse-generate.d.ts +18 -0
- package/.rollup.cache/Users/jonathanburger/remotion/packages/gif/dist/esm/parse-generate.js +91 -0
- package/.rollup.cache/Users/jonathanburger/remotion/packages/gif/dist/esm/parser/decompress-frames.d.ts +2 -0
- package/.rollup.cache/Users/jonathanburger/remotion/packages/gif/dist/esm/parser/decompress-frames.js +15 -0
- package/.rollup.cache/Users/jonathanburger/remotion/packages/gif/dist/esm/preload-gif.d.ts +8 -0
- package/.rollup.cache/Users/jonathanburger/remotion/packages/gif/dist/esm/preload-gif.js +37 -0
- package/.rollup.cache/Users/jonathanburger/remotion/packages/gif/dist/esm/props.d.ts +25 -0
- package/.rollup.cache/Users/jonathanburger/remotion/packages/gif/dist/esm/props.js +1 -0
- package/.rollup.cache/Users/jonathanburger/remotion/packages/gif/dist/esm/react-tools.d.ts +9 -0
- package/.rollup.cache/Users/jonathanburger/remotion/packages/gif/dist/esm/react-tools.js +35 -0
- package/.rollup.cache/Users/jonathanburger/remotion/packages/gif/dist/esm/resolve-gif-source.d.ts +1 -0
- package/.rollup.cache/Users/jonathanburger/remotion/packages/gif/dist/esm/resolve-gif-source.js +3 -0
- package/.rollup.cache/Users/jonathanburger/remotion/packages/gif/dist/esm/use-element-size.d.ts +6 -0
- package/.rollup.cache/Users/jonathanburger/remotion/packages/gif/dist/esm/use-element-size.js +68 -0
- package/.rollup.cache/Users/jonathanburger/remotion/packages/gif/dist/esm/useCurrentGifIndex.d.ts +2 -0
- package/.rollup.cache/Users/jonathanburger/remotion/packages/gif/dist/esm/useCurrentGifIndex.js +31 -0
- package/.rollup.cache/Users/jonathanburger/remotion/packages/gif/dist/esm/worker/index.d.ts +1 -0
- package/.rollup.cache/Users/jonathanburger/remotion/packages/gif/dist/esm/worker/index.js +8 -0
- package/.rollup.cache/Users/jonathanburger/remotion/packages/gif/dist/esm/worker/source.d.ts +1 -0
- package/.rollup.cache/Users/jonathanburger/remotion/packages/gif/dist/esm/worker/source.js +4 -0
- package/.rollup.cache/Users/jonathanburger/remotion/packages/gif/dist/esm/worker/worker.d.ts +1 -0
- package/.rollup.cache/Users/jonathanburger/remotion/packages/gif/dist/esm/worker/worker.js +37 -0
- package/.rollup.cache/Users/jonathanburger/remotion/packages/gif/dist/tsconfig-esm.tsbuildinfo +1 -0
- package/.turbo/turbo-build.log +7 -0
- package/package.json +2 -5
- package/dist/tsconfig-esm.tsbuildinfo +0 -1
- package/dist/tsconfig.tsbuildinfo +0 -1
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import type { ParsedGif } from '../gifuct/types';
|
|
2
|
+
import type { Stream } from './uint8-parser';
|
|
3
|
+
export declare const GIF: (((stream: Stream, result: ParsedGif, parent: ParsedGif, parseFn: (st: Stream, schema: unknown, result: ParsedGif, parent: ParsedGif) => void) => void) | {
|
|
4
|
+
header: ({
|
|
5
|
+
signature: (stream: Stream) => string;
|
|
6
|
+
version?: undefined;
|
|
7
|
+
} | {
|
|
8
|
+
version: (stream: Stream) => string;
|
|
9
|
+
signature?: undefined;
|
|
10
|
+
})[];
|
|
11
|
+
lsd?: undefined;
|
|
12
|
+
frames?: undefined;
|
|
13
|
+
} | {
|
|
14
|
+
lsd: ({
|
|
15
|
+
width: (stream: Stream) => number;
|
|
16
|
+
height?: undefined;
|
|
17
|
+
gct?: undefined;
|
|
18
|
+
backgroundColorIndex?: undefined;
|
|
19
|
+
pixelAspectRatio?: undefined;
|
|
20
|
+
} | {
|
|
21
|
+
height: (stream: Stream) => number;
|
|
22
|
+
width?: undefined;
|
|
23
|
+
gct?: undefined;
|
|
24
|
+
backgroundColorIndex?: undefined;
|
|
25
|
+
pixelAspectRatio?: undefined;
|
|
26
|
+
} | {
|
|
27
|
+
gct: (stream: Stream) => Record<string, number | boolean>;
|
|
28
|
+
width?: undefined;
|
|
29
|
+
height?: undefined;
|
|
30
|
+
backgroundColorIndex?: undefined;
|
|
31
|
+
pixelAspectRatio?: undefined;
|
|
32
|
+
} | {
|
|
33
|
+
backgroundColorIndex: (stream: Stream) => number;
|
|
34
|
+
width?: undefined;
|
|
35
|
+
height?: undefined;
|
|
36
|
+
gct?: undefined;
|
|
37
|
+
pixelAspectRatio?: undefined;
|
|
38
|
+
} | {
|
|
39
|
+
pixelAspectRatio: (stream: Stream) => number;
|
|
40
|
+
width?: undefined;
|
|
41
|
+
height?: undefined;
|
|
42
|
+
gct?: undefined;
|
|
43
|
+
backgroundColorIndex?: undefined;
|
|
44
|
+
})[];
|
|
45
|
+
header?: undefined;
|
|
46
|
+
frames?: undefined;
|
|
47
|
+
} | {
|
|
48
|
+
frames: (stream: Stream, result: unknown, parent: unknown, _parse: (st: Stream, schema: unknown, result: unknown, parent: unknown) => void) => unknown[];
|
|
49
|
+
header?: undefined;
|
|
50
|
+
lsd?: undefined;
|
|
51
|
+
})[];
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import { conditional, loop } from './parser';
|
|
2
|
+
import { peekByte, peekBytes, readArray, readBits, readByte, readBytes, readString, readUnsigned, } from './uint8-parser';
|
|
3
|
+
// a set of 0x00 terminated subblocks
|
|
4
|
+
const subBlocksSchema = {
|
|
5
|
+
blocks: (stream) => {
|
|
6
|
+
const terminator = 0x00;
|
|
7
|
+
const chunks = [];
|
|
8
|
+
const streamSize = stream.data.length;
|
|
9
|
+
let total = 0;
|
|
10
|
+
for (let size = readByte()(stream); size !== terminator; size = readByte()(stream)) {
|
|
11
|
+
// size becomes undefined for some case when file is corrupted and terminator is not proper
|
|
12
|
+
// null check to avoid recursion
|
|
13
|
+
if (!size)
|
|
14
|
+
break;
|
|
15
|
+
// catch corrupted files with no terminator
|
|
16
|
+
if (stream.pos + size >= streamSize) {
|
|
17
|
+
const availableSize = streamSize - stream.pos;
|
|
18
|
+
chunks.push(readBytes(availableSize)(stream));
|
|
19
|
+
total += availableSize;
|
|
20
|
+
break;
|
|
21
|
+
}
|
|
22
|
+
chunks.push(readBytes(size)(stream));
|
|
23
|
+
total += size;
|
|
24
|
+
}
|
|
25
|
+
const result = new Uint8Array(total);
|
|
26
|
+
let offset = 0;
|
|
27
|
+
for (let i = 0; i < chunks.length; i++) {
|
|
28
|
+
result.set(chunks[i], offset);
|
|
29
|
+
offset += chunks[i].length;
|
|
30
|
+
}
|
|
31
|
+
return result;
|
|
32
|
+
},
|
|
33
|
+
};
|
|
34
|
+
// global control extension
|
|
35
|
+
const gceSchema = conditional({
|
|
36
|
+
gce: [
|
|
37
|
+
{ codes: readBytes(2) },
|
|
38
|
+
{ byteSize: readByte() },
|
|
39
|
+
{
|
|
40
|
+
extras: readBits({
|
|
41
|
+
future: { index: 0, length: 3 },
|
|
42
|
+
disposal: { index: 3, length: 3 },
|
|
43
|
+
userInput: { index: 6 },
|
|
44
|
+
transparentColorGiven: { index: 7 },
|
|
45
|
+
}),
|
|
46
|
+
},
|
|
47
|
+
{ delay: readUnsigned(true) },
|
|
48
|
+
{ transparentColorIndex: readByte() },
|
|
49
|
+
{ terminator: readByte() },
|
|
50
|
+
],
|
|
51
|
+
}, (stream) => {
|
|
52
|
+
const codes = peekBytes(2)(stream);
|
|
53
|
+
return codes[0] === 0x21 && codes[1] === 0xf9;
|
|
54
|
+
});
|
|
55
|
+
// image pipeline block
|
|
56
|
+
const imageSchema = conditional({
|
|
57
|
+
image: [
|
|
58
|
+
{ code: readByte() },
|
|
59
|
+
{
|
|
60
|
+
descriptor: [
|
|
61
|
+
{ left: readUnsigned(true) },
|
|
62
|
+
{ top: readUnsigned(true) },
|
|
63
|
+
{ width: readUnsigned(true) },
|
|
64
|
+
{ height: readUnsigned(true) },
|
|
65
|
+
{
|
|
66
|
+
lct: readBits({
|
|
67
|
+
exists: { index: 0 },
|
|
68
|
+
interlaced: { index: 1 },
|
|
69
|
+
sort: { index: 2 },
|
|
70
|
+
future: { index: 3, length: 2 },
|
|
71
|
+
size: { index: 5, length: 3 },
|
|
72
|
+
}),
|
|
73
|
+
},
|
|
74
|
+
],
|
|
75
|
+
},
|
|
76
|
+
conditional({
|
|
77
|
+
lct: readArray(3, (_stream, _result, parent) => {
|
|
78
|
+
return 2 ** (parent.descriptor.lct.size + 1);
|
|
79
|
+
}),
|
|
80
|
+
}, (_stream, _result, parent) => {
|
|
81
|
+
return parent.descriptor.lct.exists;
|
|
82
|
+
}),
|
|
83
|
+
{ data: [{ minCodeSize: readByte() }, subBlocksSchema] },
|
|
84
|
+
],
|
|
85
|
+
}, (stream) => {
|
|
86
|
+
return peekByte()(stream) === 0x2c;
|
|
87
|
+
});
|
|
88
|
+
// plain text block
|
|
89
|
+
const textSchema = conditional({
|
|
90
|
+
text: [
|
|
91
|
+
{ codes: readBytes(2) },
|
|
92
|
+
{ blockSize: readByte() },
|
|
93
|
+
{
|
|
94
|
+
preData: (stream, _result, parent) => readBytes(parent.text.blockSize)(stream),
|
|
95
|
+
},
|
|
96
|
+
subBlocksSchema,
|
|
97
|
+
],
|
|
98
|
+
}, (stream) => {
|
|
99
|
+
const codes = peekBytes(2)(stream);
|
|
100
|
+
return codes[0] === 0x21 && codes[1] === 0x01;
|
|
101
|
+
});
|
|
102
|
+
// application block
|
|
103
|
+
const applicationSchema = conditional({
|
|
104
|
+
application: [
|
|
105
|
+
{ codes: readBytes(2) },
|
|
106
|
+
{ blockSize: readByte() },
|
|
107
|
+
{
|
|
108
|
+
id: (stream, _result, parent) => readString(parent.blockSize)(stream),
|
|
109
|
+
},
|
|
110
|
+
subBlocksSchema,
|
|
111
|
+
],
|
|
112
|
+
}, (stream) => {
|
|
113
|
+
const codes = peekBytes(2)(stream);
|
|
114
|
+
return codes[0] === 0x21 && codes[1] === 0xff;
|
|
115
|
+
});
|
|
116
|
+
// comment block
|
|
117
|
+
const commentSchema = conditional({
|
|
118
|
+
comment: [{ codes: readBytes(2) }, subBlocksSchema],
|
|
119
|
+
}, (stream) => {
|
|
120
|
+
const codes = peekBytes(2)(stream);
|
|
121
|
+
return codes[0] === 0x21 && codes[1] === 0xfe;
|
|
122
|
+
});
|
|
123
|
+
export const GIF = [
|
|
124
|
+
{ header: [{ signature: readString(3) }, { version: readString(3) }] },
|
|
125
|
+
{
|
|
126
|
+
lsd: [
|
|
127
|
+
{ width: readUnsigned(true) },
|
|
128
|
+
{ height: readUnsigned(true) },
|
|
129
|
+
{
|
|
130
|
+
gct: readBits({
|
|
131
|
+
exists: { index: 0 },
|
|
132
|
+
resolution: { index: 1, length: 3 },
|
|
133
|
+
sort: { index: 4 },
|
|
134
|
+
size: { index: 5, length: 3 },
|
|
135
|
+
}),
|
|
136
|
+
},
|
|
137
|
+
{ backgroundColorIndex: readByte() },
|
|
138
|
+
{ pixelAspectRatio: readByte() },
|
|
139
|
+
],
|
|
140
|
+
},
|
|
141
|
+
conditional({
|
|
142
|
+
gct: readArray(3, (_stream, result) => 2 ** (result.lsd.gct.size + 1)),
|
|
143
|
+
}, (_stream, result) => result.lsd.gct.exists),
|
|
144
|
+
// content frames
|
|
145
|
+
{
|
|
146
|
+
frames: loop([gceSchema, applicationSchema, commentSchema, imageSchema, textSchema], (stream) => {
|
|
147
|
+
const nextCode = peekByte()(stream);
|
|
148
|
+
// rather than check for a terminator, we should check for the existence
|
|
149
|
+
// of an ext or image block to avoid infinite loops
|
|
150
|
+
// var terminator = 0x3B;
|
|
151
|
+
// return nextCode !== terminator;
|
|
152
|
+
return nextCode === 0x21 || nextCode === 0x2c;
|
|
153
|
+
}),
|
|
154
|
+
},
|
|
155
|
+
];
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { ParsedGif } from '../gifuct/types';
|
|
2
|
+
import type { Stream } from './uint8-parser';
|
|
3
|
+
export declare const parse: <T extends Record<string, T>>(stream: Stream, schema: GifSchema, result?: T, parent?: T) => GifSchema;
|
|
4
|
+
export declare const loop: <R>(schema: GifSchema, continueFunc: (st: Stream, r: R, p: R) => boolean) => (stream: Stream, result: R, parent: R, _parse: ParseFn<R>) => R[];
|
|
5
|
+
type ConditionalFunction<T> = (st: Stream, result: T, parent: T) => boolean;
|
|
6
|
+
type ParseFn<T> = (st: Stream, schema: GifSchema, result: T, parent: T) => void;
|
|
7
|
+
export type GifSchema = unknown | ParsedGif;
|
|
8
|
+
export declare const conditional: <T>(schema: GifSchema, conditionFunc: ConditionalFunction<T>) => (stream: Stream, result: T, parent: T, parseFn: ParseFn<T>) => void;
|
|
9
|
+
export {};
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
export const parse = (stream, schema, result = {}, parent = result) => {
|
|
2
|
+
if (Array.isArray(schema)) {
|
|
3
|
+
schema.forEach((partSchema) => {
|
|
4
|
+
return parse(stream, partSchema, result, parent);
|
|
5
|
+
});
|
|
6
|
+
}
|
|
7
|
+
else if (typeof schema === 'function') {
|
|
8
|
+
schema(stream, result, parent, parse);
|
|
9
|
+
}
|
|
10
|
+
else {
|
|
11
|
+
// @ts-expect-error
|
|
12
|
+
const key = Object.keys(schema)[0];
|
|
13
|
+
// @ts-expect-error
|
|
14
|
+
if (Array.isArray(schema[key])) {
|
|
15
|
+
// @ts-expect-error
|
|
16
|
+
parent[key] = {};
|
|
17
|
+
// @ts-expect-error
|
|
18
|
+
parse(stream, schema[key], result, parent[key]);
|
|
19
|
+
}
|
|
20
|
+
else {
|
|
21
|
+
// @ts-expect-error
|
|
22
|
+
parent[key] = schema[key](stream, result, parent, parse);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
return result;
|
|
26
|
+
};
|
|
27
|
+
export const loop = (schema, continueFunc) => {
|
|
28
|
+
return function (stream, result, parent, _parse) {
|
|
29
|
+
const arr = [];
|
|
30
|
+
let lastStreamPos = stream.pos;
|
|
31
|
+
while (continueFunc(stream, result, parent)) {
|
|
32
|
+
const newParent = {};
|
|
33
|
+
_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
|
|
34
|
+
// it falls into infinite recursion, null check to avoid the same
|
|
35
|
+
if (stream.pos === lastStreamPos) {
|
|
36
|
+
break;
|
|
37
|
+
}
|
|
38
|
+
lastStreamPos = stream.pos;
|
|
39
|
+
arr.push(newParent);
|
|
40
|
+
}
|
|
41
|
+
return arr;
|
|
42
|
+
};
|
|
43
|
+
};
|
|
44
|
+
export const conditional = (schema, conditionFunc) => (stream, result, parent, parseFn) => {
|
|
45
|
+
if (conditionFunc(stream, result, parent)) {
|
|
46
|
+
parseFn(stream, schema, result, parent);
|
|
47
|
+
}
|
|
48
|
+
};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { GifSchema } from './parser';
|
|
2
|
+
export type Stream = {
|
|
3
|
+
data: Uint8Array;
|
|
4
|
+
pos: number;
|
|
5
|
+
};
|
|
6
|
+
export declare const buildStream: (uint8Data: Uint8Array) => {
|
|
7
|
+
data: Uint8Array;
|
|
8
|
+
pos: number;
|
|
9
|
+
};
|
|
10
|
+
export declare const readByte: () => (stream: Stream) => number;
|
|
11
|
+
export declare const peekByte: (offset?: number) => (stream: Stream) => number;
|
|
12
|
+
export declare const readBytes: (length: number) => (stream: Stream) => Uint8Array;
|
|
13
|
+
export declare const peekBytes: (length: number) => (stream: Stream) => Uint8Array;
|
|
14
|
+
export declare const readString: (length: number) => (stream: Stream) => string;
|
|
15
|
+
export declare const readUnsigned: (littleEndian: boolean) => (stream: Stream) => number;
|
|
16
|
+
export declare const readArray: <T>(byteSize: number, totalOrFunc: number | ((st: Stream, r: T, p: T) => number)) => (stream: Stream, result: T, parent: T) => any[];
|
|
17
|
+
export declare const readBits: (schema: GifSchema) => (stream: Stream) => Record<string, number | boolean>;
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/* eslint-disable no-bitwise */
|
|
2
|
+
// Default stream and parsers for Uint8TypedArray data type
|
|
3
|
+
export const buildStream = (uint8Data) => ({
|
|
4
|
+
data: uint8Data,
|
|
5
|
+
pos: 0,
|
|
6
|
+
});
|
|
7
|
+
export const readByte = () => (stream) => {
|
|
8
|
+
return stream.data[stream.pos++];
|
|
9
|
+
};
|
|
10
|
+
export const peekByte = (offset = 0) => (stream) => {
|
|
11
|
+
return stream.data[stream.pos + offset];
|
|
12
|
+
};
|
|
13
|
+
export const readBytes = (length) => (stream) => {
|
|
14
|
+
// eslint-disable-next-line no-return-assign
|
|
15
|
+
return stream.data.subarray(stream.pos, (stream.pos += length));
|
|
16
|
+
};
|
|
17
|
+
export const peekBytes = (length) => (stream) => {
|
|
18
|
+
return stream.data.subarray(stream.pos, stream.pos + length);
|
|
19
|
+
};
|
|
20
|
+
export const readString = (length) => (stream) => {
|
|
21
|
+
return Array.from(readBytes(length)(stream))
|
|
22
|
+
.map((value) => String.fromCharCode(value))
|
|
23
|
+
.join('');
|
|
24
|
+
};
|
|
25
|
+
export const readUnsigned = (littleEndian) => (stream) => {
|
|
26
|
+
const bytes = readBytes(2)(stream);
|
|
27
|
+
return littleEndian ? (bytes[1] << 8) + bytes[0] : (bytes[0] << 8) + bytes[1];
|
|
28
|
+
};
|
|
29
|
+
export const readArray = (byteSize, totalOrFunc) => (stream, result, parent) => {
|
|
30
|
+
const total = typeof totalOrFunc === 'function'
|
|
31
|
+
? totalOrFunc(stream, result, parent)
|
|
32
|
+
: totalOrFunc;
|
|
33
|
+
const parser = readBytes(byteSize);
|
|
34
|
+
const arr = new Array(total);
|
|
35
|
+
for (let i = 0; i < total; i++) {
|
|
36
|
+
arr[i] = parser(stream);
|
|
37
|
+
}
|
|
38
|
+
return arr;
|
|
39
|
+
};
|
|
40
|
+
const subBitsTotal = (bits, startIndex, length) => {
|
|
41
|
+
let result = 0;
|
|
42
|
+
for (let i = 0; i < length; i++) {
|
|
43
|
+
result += Number(bits[startIndex + i] && 2 ** (length - i - 1));
|
|
44
|
+
}
|
|
45
|
+
return result;
|
|
46
|
+
};
|
|
47
|
+
export const readBits = (schema) => (stream) => {
|
|
48
|
+
const byte = readByte()(stream);
|
|
49
|
+
// convert the byte to bit array
|
|
50
|
+
const bits = new Array(8);
|
|
51
|
+
for (let i = 0; i < 8; i++) {
|
|
52
|
+
bits[7 - i] = Boolean(byte & (1 << i));
|
|
53
|
+
}
|
|
54
|
+
// convert the bit array to values based on the schema
|
|
55
|
+
// @ts-expect-error
|
|
56
|
+
return Object.keys(schema).reduce((res, key) => {
|
|
57
|
+
// @ts-expect-error
|
|
58
|
+
const def = schema[key];
|
|
59
|
+
if (def.length) {
|
|
60
|
+
res[key] = subBitsTotal(bits, def.index, def.length);
|
|
61
|
+
}
|
|
62
|
+
else {
|
|
63
|
+
res[key] = bits[def.index];
|
|
64
|
+
}
|
|
65
|
+
return res;
|
|
66
|
+
}, {});
|
|
67
|
+
};
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
export interface Options<KeyType, ValueType> {
|
|
2
|
+
/**
|
|
3
|
+
The maximum number of milliseconds an item should remain in the cache.
|
|
4
|
+
|
|
5
|
+
@default Infinity
|
|
6
|
+
|
|
7
|
+
By default, `maxAge` will be `Infinity`, which means that items will never expire.
|
|
8
|
+
Lazy expiration upon the next write or read call.
|
|
9
|
+
|
|
10
|
+
Individual expiration of an item can be specified by the `set(key, value, maxAge)` method.
|
|
11
|
+
*/
|
|
12
|
+
readonly maxAge?: number;
|
|
13
|
+
/**
|
|
14
|
+
The maximum number of items before evicting the least recently used items.
|
|
15
|
+
*/
|
|
16
|
+
readonly maxSize: number;
|
|
17
|
+
/**
|
|
18
|
+
Called right before an item is evicted from the cache.
|
|
19
|
+
|
|
20
|
+
Useful for side effects or for items like object URLs that need explicit cleanup (`revokeObjectURL`).
|
|
21
|
+
*/
|
|
22
|
+
onEviction?: (key: KeyType, value: ValueType) => void;
|
|
23
|
+
}
|
|
24
|
+
export declare class QuickLRU<KeyType, ValueType> implements Iterable<[KeyType, ValueType]> {
|
|
25
|
+
/**
|
|
26
|
+
The maximum number of milliseconds an item should remain in the cache.
|
|
27
|
+
|
|
28
|
+
@default Infinity
|
|
29
|
+
|
|
30
|
+
By default, `maxAge` will be `Infinity`, which means that items will never expire.
|
|
31
|
+
Lazy expiration upon the next write or read call.
|
|
32
|
+
|
|
33
|
+
Individual expiration of an item can be specified by the `set(key, value, maxAge)` method.
|
|
34
|
+
*/
|
|
35
|
+
maxAge: number;
|
|
36
|
+
/**
|
|
37
|
+
The maximum number of items before evicting the least recently used items.
|
|
38
|
+
*/
|
|
39
|
+
maxSize: number;
|
|
40
|
+
/**
|
|
41
|
+
Called right before an item is evicted from the cache.
|
|
42
|
+
|
|
43
|
+
Useful for side effects or for items like object URLs that need explicit cleanup (`revokeObjectURL`).
|
|
44
|
+
*/
|
|
45
|
+
onEviction?: (key: KeyType, value: ValueType) => void;
|
|
46
|
+
private _size;
|
|
47
|
+
private cache;
|
|
48
|
+
private oldCache;
|
|
49
|
+
/**
|
|
50
|
+
Simple ["Least Recently Used" (LRU) cache](https://en.m.wikipedia.org/wiki/Cache_replacement_policies#Least_Recently_Used_.28LRU.29).
|
|
51
|
+
|
|
52
|
+
The instance is an [`Iterable`](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Iteration_protocols) of `[key, value]` pairs so you can use it directly in a [`forβ¦of`](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Statements/for...of) loop.
|
|
53
|
+
|
|
54
|
+
@example
|
|
55
|
+
```
|
|
56
|
+
import { QuickLRU } from 'quick-lru-ts';
|
|
57
|
+
|
|
58
|
+
const lru = new QuickLRU({maxSize: 1000});
|
|
59
|
+
|
|
60
|
+
lru.set('π¦', 'π');
|
|
61
|
+
|
|
62
|
+
lru.has('π¦');
|
|
63
|
+
//=> true
|
|
64
|
+
|
|
65
|
+
lru.get('π¦');
|
|
66
|
+
//=> 'π'
|
|
67
|
+
```
|
|
68
|
+
*/
|
|
69
|
+
constructor(options: Options<KeyType, ValueType>);
|
|
70
|
+
private _emitEvictions;
|
|
71
|
+
private _deleteIfExpired;
|
|
72
|
+
private _getOrDeleteIfExpired;
|
|
73
|
+
private _getItemValue;
|
|
74
|
+
private _peek;
|
|
75
|
+
private _set;
|
|
76
|
+
private _moveToRecent;
|
|
77
|
+
private _entriesAscending;
|
|
78
|
+
/**
|
|
79
|
+
Get an item.
|
|
80
|
+
|
|
81
|
+
@returns The stored item or `undefined`.
|
|
82
|
+
*/
|
|
83
|
+
get(key: KeyType): ValueType | undefined;
|
|
84
|
+
set(key: KeyType, value: ValueType, { maxAge }?: {
|
|
85
|
+
maxAge?: number;
|
|
86
|
+
}): void;
|
|
87
|
+
has(key: KeyType): boolean;
|
|
88
|
+
peek(key: KeyType): ValueType | undefined;
|
|
89
|
+
delete(key: KeyType): boolean;
|
|
90
|
+
clear(): void;
|
|
91
|
+
resize(maxSize: number): void;
|
|
92
|
+
keys(): Generator<KeyType, void, unknown>;
|
|
93
|
+
values(): Generator<ValueType, void, unknown>;
|
|
94
|
+
[Symbol.iterator](): IterableIterator<[KeyType, ValueType]>;
|
|
95
|
+
/**
|
|
96
|
+
Iterable for all entries, starting with the newest (descending in recency).
|
|
97
|
+
*/
|
|
98
|
+
entriesDescending(): IterableIterator<[KeyType, ValueType]>;
|
|
99
|
+
entriesAscending(): Generator<(KeyType | ValueType)[], void, unknown>;
|
|
100
|
+
get size(): number;
|
|
101
|
+
}
|