Skip to content

Instantly share code, notes, and snippets.

@deviationist
Last active April 12, 2023 14:09
Show Gist options
  • Save deviationist/2e0e4e8bb4f312e80f93066da12bc85f to your computer and use it in GitHub Desktop.
Save deviationist/2e0e4e8bb4f312e80f93066da12bc85f to your computer and use it in GitHub Desktop.
A Node class used for storing cookies in a JSON-file.
import { readFileSync, writeFileSync, existsSync, statSync } from 'fs';
export default class Cookies {
static filePath = './cookies.json';
static get() {
const dataStream = readFileSync(Cookies.filePath, 'utf-8');
return JSON.parse(Buffer.from(dataStream));
}
static has() {
if (existsSync(Cookies.filePath)) {
const fileState = statSync(Cookies.filePath);
return true;
}
return false;
}
static set(cookies) {
return writeFileSync(Cookies.filePath, JSON.stringify(cookies, null, 4));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment