Skip to content

Commit db27e1f

Browse files
committed
feat: initial version
0 parents  commit db27e1f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+3372
-0
lines changed

.editorconfig

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
root = true
2+
3+
[*]
4+
end_of_line = lf
5+
insert_final_newline = true
6+
indent_style = tab
7+
charset = utf-8
8+
9+
[{package.json,*.yml}]
10+
indent_style = space
11+
indent_size = 2

.eslintignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
coverage

.eslintrc.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"env": {
3+
"node": true,
4+
"es6": true
5+
},
6+
"parserOptions": {
7+
"ecmaVersion": 2018,
8+
"sourceType": "script"
9+
},
10+
"extends": [
11+
"@csimi/eslint-config"
12+
]
13+
}

.github/workflows/nodejs.yml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
name: build
2+
3+
on:
4+
pull_request:
5+
types:
6+
- opened
7+
- synchronize
8+
push:
9+
branches:
10+
- master
11+
12+
jobs:
13+
build:
14+
15+
strategy:
16+
matrix:
17+
node-version: [10.x, 12.x]
18+
19+
runs-on: ubuntu-latest
20+
21+
steps:
22+
- uses: actions/checkout@v1
23+
- name: Use Node.js ${{ matrix.node-version }}
24+
uses: actions/setup-node@v1
25+
with:
26+
node-version: ${{ matrix.node-version }}
27+
- name: npm install, build, and test
28+
run: |
29+
npm ci
30+
npm run build --if-present
31+
npm test
32+
env:
33+
CI: true
34+
- name: Upload coverage to Codecov
35+
uses: codecov/codecov-action@v1
36+
if: matrix.node-version == '12.x'
37+
with:
38+
token: ${{ secrets.CODECOV_TOKEN }}

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
node_modules
2+
npm-debug.log*
3+
coverage
4+
.nyc_output

.mocharc.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"recursive": true,
3+
"file": ["test/setup.js"]
4+
}

.nycrc.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"all": true,
3+
"produce-source-map": true,
4+
"include": [
5+
"lib/**"
6+
]
7+
}

lib/const.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
const MARKER_LENGTH = 1;
2+
const NUMBER = 0x00;
3+
const BOOLEAN = 0x01;
4+
const STRING = 0x02;
5+
const OBJECT = 0x03;
6+
const MOVIECLIP = 0x04;
7+
const NULL = 0x05;
8+
const UNDEFINED = 0x06;
9+
const REFERENCE = 0x07;
10+
const ECMA_ARRAY = 0x08;
11+
const OBJECT_END = 0x09;
12+
const STRICT_ARRAY = 0x0A;
13+
const DATE = 0x0B;
14+
const LONG_STRING = 0x0C;
15+
const UNSUPPORTED = 0x0D;
16+
const RECORDSET = 0x0E;
17+
const XML_DOCUMENT = 0x0F;
18+
const TYPED_OBJECT = 0x10;
19+
const AVMPLUS_OBJECT = 0x11;
20+
21+
module.exports = {
22+
MARKER_LENGTH,
23+
NUMBER,
24+
BOOLEAN,
25+
STRING,
26+
OBJECT,
27+
MOVIECLIP,
28+
NULL,
29+
UNDEFINED,
30+
REFERENCE,
31+
ECMA_ARRAY,
32+
OBJECT_END,
33+
STRICT_ARRAY,
34+
DATE,
35+
LONG_STRING,
36+
UNSUPPORTED,
37+
RECORDSET,
38+
XML_DOCUMENT,
39+
TYPED_OBJECT,
40+
AVMPLUS_OBJECT,
41+
};

lib/index.js

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
const {
2+
MARKER_LENGTH,
3+
NUMBER,
4+
BOOLEAN,
5+
STRING,
6+
OBJECT,
7+
NULL,
8+
LONG_STRING,
9+
UNDEFINED,
10+
STRICT_ARRAY,
11+
DATE,
12+
} = require('./const');
13+
const {
14+
encodeNumber,
15+
decodeNumber,
16+
} = require('./types/number');
17+
const {
18+
encodeBoolean,
19+
decodeBoolean,
20+
} = require('./types/boolean');
21+
const {
22+
encodeString,
23+
decodeString,
24+
} = require('./types/string');
25+
const {
26+
encodeObject,
27+
decodeObject,
28+
} = require('./types/object');
29+
const {
30+
encodeNull,
31+
decodeNull,
32+
} = require('./types/null');
33+
const {
34+
encodeUndefined,
35+
decodeUndefined,
36+
} = require('./types/undefined');
37+
const {
38+
encodeArray,
39+
decodeArray,
40+
} = require('./types/array');
41+
const {
42+
encodeDate,
43+
decodeDate,
44+
} = require('./types/date');
45+
46+
const toAMF = (value) => {
47+
const type = typeof value;
48+
switch (type) {
49+
case 'number':
50+
return encodeNumber(value);
51+
case 'boolean':
52+
return encodeBoolean(value);
53+
case 'string':
54+
return encodeString(value);
55+
case 'object':
56+
if (value === null) {
57+
return encodeNull(value);
58+
}
59+
if (value instanceof Array) {
60+
return encodeArray(value, toAMF);
61+
}
62+
if (value instanceof Date) {
63+
return encodeDate(value);
64+
}
65+
return encodeObject(value, toAMF);
66+
case 'undefined':
67+
return encodeUndefined(value);
68+
default:
69+
throw new Error(`Cannot encode unknown type of ${type}`);
70+
}
71+
};
72+
73+
const decodeAMF = (buffer, memo) => {
74+
const type = buffer.readUInt8(memo.pos);
75+
memo.pos += MARKER_LENGTH;
76+
switch (type) {
77+
case NUMBER:
78+
return decodeNumber(buffer, memo);
79+
case BOOLEAN:
80+
return decodeBoolean(buffer, memo);
81+
case STRING:
82+
case LONG_STRING:
83+
return decodeString(buffer, memo, type);
84+
case OBJECT:
85+
return decodeObject(buffer, memo, decodeAMF);
86+
case NULL:
87+
return decodeNull(buffer, memo);
88+
case UNDEFINED:
89+
return decodeUndefined(buffer, memo);
90+
case STRICT_ARRAY:
91+
return decodeArray(buffer, memo, decodeAMF);
92+
case DATE:
93+
return decodeDate(buffer, memo);
94+
default:
95+
throw new Error(`Cannot decode unknown type of ${type}`);
96+
}
97+
};
98+
99+
const fromAMF = (buffer) => {
100+
const memo = {
101+
'pos': 0,
102+
};
103+
return decodeAMF(buffer, memo);
104+
};
105+
106+
module.exports = {
107+
toAMF,
108+
fromAMF,
109+
};

lib/types/array.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
const {
2+
MARKER_LENGTH,
3+
STRICT_ARRAY,
4+
} = require('../const');
5+
6+
const ARRAY_LENGTH = 4;
7+
8+
const encodeArray = (value, toAMF) => {
9+
const buf = Buffer.allocUnsafe(MARKER_LENGTH + ARRAY_LENGTH);
10+
11+
buf.writeUInt8(STRICT_ARRAY, 0);
12+
buf.writeUInt32BE(value.length, MARKER_LENGTH);
13+
14+
return Buffer.concat([
15+
buf,
16+
...value.map(toAMF),
17+
]);
18+
};
19+
20+
const decodeArray = (buf, memo, decodeAMF) => {
21+
const data = [];
22+
23+
const length = buf.readUInt32BE(memo.pos);
24+
memo.pos += ARRAY_LENGTH;
25+
26+
while (data.length < length) {
27+
data.push(decodeAMF(buf, memo));
28+
}
29+
30+
return data;
31+
};
32+
33+
module.exports = {
34+
encodeArray,
35+
decodeArray,
36+
};

lib/types/boolean.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const {
2+
BOOLEAN,
3+
} = require('../const');
4+
5+
const BOOLEAN_LENGTH = 1;
6+
7+
const encodeBoolean = (value) => {
8+
return Buffer.from([BOOLEAN, Number(value)]);
9+
};
10+
11+
const decodeBoolean = (buf, memo) => {
12+
const value = Boolean(buf.readUInt8(memo.pos));
13+
memo.pos += BOOLEAN_LENGTH;
14+
return value;
15+
};
16+
17+
module.exports = {
18+
encodeBoolean,
19+
decodeBoolean,
20+
};

lib/types/date.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
const {
2+
MARKER_LENGTH,
3+
DATE,
4+
} = require('../const');
5+
6+
const DATE_LENGTH = 8;
7+
const DATE_TIMEZONE_LENGTH = 2;
8+
9+
const encodeDate = (date) => {
10+
const buf = Buffer.allocUnsafe(MARKER_LENGTH + DATE_LENGTH + DATE_TIMEZONE_LENGTH);
11+
12+
buf.writeUInt8(DATE, 0);
13+
buf.writeDoubleBE(date.valueOf(), MARKER_LENGTH);
14+
buf.writeUInt16BE(0, MARKER_LENGTH + DATE_LENGTH);
15+
16+
return buf;
17+
};
18+
19+
const decodeDate = (buf, memo) => {
20+
const value = new Date(buf.readDoubleBE(memo.pos));
21+
memo.pos += DATE_LENGTH + DATE_TIMEZONE_LENGTH;
22+
return value;
23+
};
24+
25+
module.exports = {
26+
encodeDate,
27+
decodeDate,
28+
};

lib/types/null.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const {
2+
NULL,
3+
} = require('../const');
4+
5+
const encodeNull = () => {
6+
return Buffer.from([NULL]);
7+
};
8+
9+
const decodeNull = () => {
10+
return null;
11+
};
12+
13+
module.exports = {
14+
encodeNull,
15+
decodeNull,
16+
};

lib/types/number.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const {
2+
MARKER_LENGTH,
3+
NUMBER,
4+
} = require('../const');
5+
6+
const NUMBER_LENGTH = 8;
7+
8+
const encodeNumber = (value) => {
9+
const buf = Buffer.allocUnsafe(MARKER_LENGTH + NUMBER_LENGTH);
10+
11+
buf.writeUInt8(NUMBER, 0);
12+
buf.writeDoubleBE(value, MARKER_LENGTH);
13+
14+
return buf;
15+
};
16+
17+
const decodeNumber = (buf, memo) => {
18+
const value = buf.readDoubleBE(memo.pos);
19+
memo.pos += NUMBER_LENGTH;
20+
return value;
21+
};
22+
23+
module.exports = {
24+
encodeNumber,
25+
decodeNumber,
26+
};

0 commit comments

Comments
 (0)