Skip to content

Commit

Permalink
feat: update steno to support multiple content types (#32)
Browse files Browse the repository at this point in the history
* feat: update steno to support multiple file types

* chore: revert other changes

* chore: remove lint changes to reduce PR complexity

* test: re-add tests for new supported types
  • Loading branch information
SgtPooki authored Dec 26, 2023
1 parent a6c4921 commit 587bf1d
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 9 deletions.
9 changes: 5 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ function getTempFilename(file: PathLike): string {

type Resolve = () => void
type Reject = (error: Error) => void
export type writeableData = Parameters<typeof writeFile>[1]

export class Writer {
#filename: PathLike
Expand All @@ -20,10 +21,10 @@ export class Writer {
#prev: [Resolve, Reject] | null = null
#next: [Resolve, Reject] | null = null
#nextPromise: Promise<void> | null = null
#nextData: string | null = null
#nextData: writeableData | null = null

// File is locked, add data for later
#add(data: string): Promise<void> {
#add(data: writeableData): Promise<void> {
// Only keep most recent data
this.#nextData = data

Expand All @@ -39,7 +40,7 @@ export class Writer {
}

// File isn't locked, write data
async #write(data: string): Promise<void> {
async #write(data: writeableData): Promise<void> {
// Lock file
this.#locked = true
try {
Expand Down Expand Up @@ -75,7 +76,7 @@ export class Writer {
this.#tempFilename = getTempFilename(filename)
}

async write(data: string): Promise<void> {
async write(data: writeableData): Promise<void> {
return this.#locked ? this.#add(data) : this.#write(data)
}
}
55 changes: 50 additions & 5 deletions src/test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { strictEqual as equal } from 'node:assert'
import fs from 'node:fs'
import os from 'node:os'
import path from 'node:path'
import url from 'node:url'
import * as fs from 'node:fs'
import * as os from 'node:os'
import * as path from 'node:path'
import * as url from 'node:url'

import { Writer } from './index.js'

export async function testSteno(): Promise<void> {
export async function testStenoStrings(): Promise<void> {
const max = 1000

const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'steno-test-'))
Expand All @@ -27,3 +28,47 @@ export async function testSteno(): Promise<void> {
equal(parseInt(fs.readFileSync(file, 'utf-8')), max)
}
}

export async function testStenoUintArrays(): Promise<void> {
const max = 1000
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'steno-test-2'))

const file = path.join(dir, 'file.txt')
const fileURL = url.pathToFileURL(path.join(dir, 'fileURL.txt'))
for (const f of [file, fileURL]) {
const textEncoder = new TextEncoder()
const textDecoder = new TextDecoder()
const writer = new Writer(f)
const promises = []

// Test race condition
for (let i = 1; i <= max; ++i) {
promises.push(writer.write(textEncoder.encode(String(i))))
}

// All promises should resolve
await Promise.all(promises)
equal(parseInt(textDecoder.decode(fs.readFileSync(file))), max)
}
}

export async function testStenoBuffers(): Promise<void> {
const max = 1000
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'steno-test-2'))

const file = path.join(dir, 'file.txt')
const fileURL = url.pathToFileURL(path.join(dir, 'fileURL.txt'))
for (const f of [file, fileURL]) {
const writer = new Writer(f)
const promises = []

// Test race condition
for (let i = 1; i <= max; ++i) {
promises.push(writer.write(Buffer.from(String(i), 'utf-8')))
}

// All promises should resolve
await Promise.all(promises)
equal(parseInt(fs.readFileSync(file, 'utf-8')), max)
}
}

0 comments on commit 587bf1d

Please sign in to comment.