Skip to content

Commit

Permalink
feat: allow buffer as input (#31)
Browse files Browse the repository at this point in the history
* feature: allow buffer as input

* Test case for buffer

* Removed tab

* Quotes

* I'm newb

* Woops
  • Loading branch information
Stannnnn authored and PBug90 committed Nov 30, 2019
1 parent 01bcf0b commit edef518
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/ReplayParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ class ReplayParser extends EventEmitter {
this.decompressed = Buffer.from('')
}

parse ($buffer: string) {
parse ($buffer: string | Buffer) {
this.msElapsed = 0
this.buffer = readFileSync($buffer)
this.buffer = Buffer.isBuffer($buffer) ? $buffer : readFileSync($buffer)
this.buffer = this.buffer.slice(this.buffer.indexOf('Warcraft III recorded game'))
this.filename = $buffer
this.filename = Buffer.isBuffer($buffer) ? 'buffer' : $buffer
const decompressed: Buffer[] = []

this._parseHeader()
Expand Down
2 changes: 1 addition & 1 deletion src/W3GReplay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class W3GReplay extends ReplayParser {
}

// gamedatablock timeslotblock commandblock actionblock
parse ($buffer: string): ParserOutput {
parse ($buffer: string | Buffer): ParserOutput {
this.parseStartTime = performance.now()
this.buffer = Buffer.from('')
this.filename = ''
Expand Down
8 changes: 8 additions & 0 deletions test/replays.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import W3GReplay from '../src/W3GReplay'
import { Validator } from 'jsonschema'
import { readFileSync } from 'fs'

const Parser = new W3GReplay()

Expand Down Expand Up @@ -181,6 +182,13 @@ describe('Replay parsing tests', () => {
expect(test.players.length).toBe(2)
})

it('parses a standard 1.30.4 replay properly as buffer', () => {
const buffer: Buffer = readFileSync('./replays/standard_1304.w3g')
const test = Parser.parse(buffer)
expect(test.version).toBe('1.30.2+')
expect(test.players.length).toBe(2)
})

it('parses a standard 1.30.4 2on2 replay properly', () => {
const test = Parser.parse('./replays/standard_1304.2on2.w3g')
expect(test.version).toBe('1.30.2+')
Expand Down

0 comments on commit edef518

Please sign in to comment.