Skip to content

Commit

Permalink
fix: 🐛 deleting a global variable is diabolical
Browse files Browse the repository at this point in the history
  • Loading branch information
touv committed Jun 19, 2024
1 parent 7428915 commit 8936dc0
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 36 deletions.
1 change: 1 addition & 0 deletions packages/core/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ ezs.toBuffer = (options) => new Output(options);
ezs.use = (plugin) => Statement.set(ezs, plugin);
ezs.addPath = (p) => ezsPath.push(p);
ezs.getPath = () => ezsPath;
ezs.getCache = () => ezsCache;
ezs.loadScript = (file) => ezs.memoize(`ezs.loadScript>${file}`, () => File(ezs, file));
ezs.compileScript = (script) => new Commands(ezs.parseString(script));
ezs.parseCommand = (command) => ezs.memoize(`ezs.parseCommand>${command}`, () => parseCommand(command));
Expand Down
64 changes: 28 additions & 36 deletions packages/core/src/statements/combine.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,16 @@ import cacache from 'cacache';
const hashCoerce = hasher({ sort: false, coerce: true });
const core = (id, value) => ({ id, value });

export const database = {};

async function saveIn(data, feed) {
if (this.isLast()) {
return feed.close();
}
const databaseID = this.getEnv();
const database = this.getEnv();
const { id, value } = data;
const isKey = Boolean(id);
if (isKey) {
if (!database[databaseID]) {
database[databaseID] = {};
}
if (!database[databaseID][id]) {
database[databaseID][id] = value;
if (!database[id]) {
database[id] = value;
}
}
return feed.send(data);
Expand Down Expand Up @@ -129,36 +124,33 @@ export default async function combine(data, feed) {
});
this.databaseID = hashCoerce.hash({ primer, commands });
const input = ezs.createStream(ezs.objectMode());
if (!database[this.databaseID]) {
database[this.databaseID] = {};
let stream;
if (cacheName) {
const cacheObject = await cacache.get.info(this.cachePath, this.databaseID);
if (cacheObject) {
stream = cacache.get.stream.byDigest(this.cachePath, cacheObject.integrity).pipe(ezs('unpack'));
}
this.database = {};
let stream;
if (cacheName) {
const cacheObject = await cacache.get.info(this.cachePath, this.databaseID);
if (cacheObject) {
stream = cacache.get.stream.byDigest(this.cachePath, cacheObject.integrity).pipe(ezs('unpack'));
}
if (!stream) {
const statements = ezs.compileCommands(commands, this.getEnv());
const logger = ezs.createTrap(this.getParam('logger'), this.getEnv());
stream = ezs.createPipeline(input, statements, logger)
.pipe(ezs(cacheSave, {
cachePath: this.cachePath,
cacheKey: this.databaseID,
}));
}
const output = stream
.pipe(ezs(saveIn, null, this.databaseID))
.pipe(ezs.catch())
.on('data', (d) => assert(d)) // WARNING: The data must be consumed, otherwise the "end" event has not been triggered
.on('error', (e) => feed.stop(e));
whenReady = new Promise((resolve) => output.on('end', resolve));
input.write(primer);
input.end();
}
if (!stream) {
const statements = ezs.compileCommands(commands, this.getEnv());
const logger = ezs.createTrap(this.getParam('logger'), this.getEnv());
stream = ezs.createPipeline(input, statements, logger)
.pipe(ezs(cacheSave, {
cachePath: this.cachePath,
cacheKey: this.databaseID,
}));
}
const output = stream
.pipe(ezs(saveIn, null, this.database))
.pipe(ezs.catch())
.on('data', (d) => assert(d)) // WARNING: The data must be consumed, otherwise the "end" event has not been triggered
.on('error', (e) => feed.stop(e));
whenReady = new Promise((resolve) => output.on('end', resolve));
input.write(primer);
input.end();
}
if (this.isLast()) {
delete database[this.databaseID];
return feed.close();
}
await whenReady;
Expand All @@ -170,10 +162,10 @@ export default async function combine(data, feed) {
return feed.send(data);
}
const values = keys.map((key) => {
if (!database[this.databaseID][key]) {
if (!this.database[key]) {
return null;
}
return core(key, database[this.databaseID][key]);
return core(key, this.database[key]);
});
// length of the values is always equal to the length of the keys.
if (Array.isArray(pathVal)) {
Expand Down

0 comments on commit 8936dc0

Please sign in to comment.