Skip to content

Commit

Permalink
Added build.
Browse files Browse the repository at this point in the history
  • Loading branch information
SamTV12345 committed Oct 17, 2023
1 parent 79b10c6 commit 3377139
Show file tree
Hide file tree
Showing 11 changed files with 1,400 additions and 704 deletions.
32 changes: 1 addition & 31 deletions .github/workflows/test-and-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,46 +8,16 @@ on:
- master

jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
node_version:
- 12
- 14
- 16
name: Test Node.js v${{matrix.node_version}}
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: ${{matrix.node_version}}
- run: npm ci
- run: npm test

lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: ${{matrix.node_version}}
- run: npm ci
- run: npm run lint

publish-npm:
if: github.event_name == 'push'
needs:
- test
- lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
with:
fetch-depth: 0
- uses: actions/setup-node@v2
with:
node-version: 12
node-version: 20
registry-url: https://registry.npmjs.org/
- name: Bump version (patch)
run: |
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
*.dirty
node_modules
.idea
dist
8 changes: 8 additions & 0 deletions build.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Bun.build(
{
entrypoints: ['./index.ts'],
outdir: './dist',
minify: false,
splitting: true
}
)
Binary file added bun.lockb
Binary file not shown.
3 changes: 0 additions & 3 deletions index.js

This file was deleted.

3 changes: 3 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import Dirty from './lib/dirty'

export default Dirty
48 changes: 32 additions & 16 deletions lib/dirty/dirty.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,28 @@
'use strict';

const fs = require('fs');
const EventEmitter = require('events').EventEmitter;
import {createReadStream, createWriteStream, ReadStream, WriteStream} from 'fs'
import {EventEmitter} from 'events'


type Error = {
code: string
}

type Row = {
key: string,
val: string
}

class Dirty extends EventEmitter {
constructor(path) {
private readonly path: PathLike;
private readonly _data: Map<any, any>;
private readonly _queue: Map<any, any>;
private _readStream: ReadStream;
private _writeStream: WriteStream;
private _waitForDrain: boolean;
private _inFlightWrites: number;

constructor(path: PathLike) {
super();

this.path = path;
Expand All @@ -24,7 +42,7 @@ class Dirty extends EventEmitter {
* cb is fired when the data is persisted.
* In memory, this is immediate - on disk, it will take some time.
*/
set(key, val, cb) {
set(key: string, val: string, cb:Function) {
if (val === undefined) {
this._data.delete(key);
} else {
Expand All @@ -44,7 +62,7 @@ class Dirty extends EventEmitter {
* Get the value stored at a key in the database
* This is synchronous since a cache is maintained in-memory
*/
get(key) {
get(key: string) {
return this._data.get(key);
}

Expand All @@ -58,14 +76,14 @@ class Dirty extends EventEmitter {
/**
* Remove a key and the value stored there
*/
rm(key, cb) {
rm(key: string, cb:Function) {
this.set(key, undefined, cb);
}

/**
* Iterate over keys, applying match function
*/
forEach(fn) {
forEach(fn: Function) {
for (const [key, val] of this._data) {
if (fn(key, val) === false) break;
}
Expand All @@ -76,7 +94,7 @@ class Dirty extends EventEmitter {
* This is synchronous since a cache is maintained in-memory
* cb is passed as per Dirty.prototype.set
*/
update(key, updater, cb) {
update(key: string, updater:Function, cb:Function) {
this.set(key, updater(this.get(key)), cb);
}

Expand All @@ -103,13 +121,13 @@ class Dirty extends EventEmitter {
return;
}

this._readStream = fs.createReadStream(this.path, {
this._readStream = createReadStream(this.path, {
encoding: 'utf-8',
flags: 'r',
});

this._readStream
.on('error', (err) => {
.on('error', (err: Error) => {
if (err.code === 'ENOENT') {
this.emit('load', 0);
return;
Expand All @@ -128,11 +146,11 @@ class Dirty extends EventEmitter {
return;
}

let row;
let row: Row;
try {
row = JSON.parse(rowStr);
if (!('key' in row)) {
throw new Error();
return '';
}
} catch (e) {
this.emit('error', new Error(`Could not load corrupted row: ${rowStr}`));
Expand All @@ -158,7 +176,7 @@ class Dirty extends EventEmitter {
this.emit('read_close');
});

this._writeStream = fs.createWriteStream(this.path, {
this._writeStream = createWriteStream(this.path, {
encoding: 'utf-8',
flags: 'a',
});
Expand Down Expand Up @@ -196,6 +214,4 @@ class Dirty extends EventEmitter {
}
}

Dirty.Dirty = Dirty;
// Trap `apply` for backwards compatibility with callers that don't use `new`.
module.exports = exports = new Proxy(Dirty, {apply: (target, thisArg, args) => new Dirty(...args)});
export default Dirty
4 changes: 2 additions & 2 deletions lib/dirty/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
'use strict';
import Dirty from './dirty'

module.exports = require('./dirty');
export default Dirty
Loading

0 comments on commit 3377139

Please sign in to comment.