Skip to content

Commit

Permalink
perf: avoid redundant buffers init
Browse files Browse the repository at this point in the history
  • Loading branch information
antongolub committed Nov 10, 2022
1 parent f50f3e4 commit c1ce6c2
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 11 deletions.
11 changes: 7 additions & 4 deletions src/main/js/firewall/middleware.js
@@ -1,7 +1,8 @@
import {Buffer} from 'node:buffer'
import {httpError, NOT_FOUND, ACCESS_DENIED, METHOD_NOT_ALLOWED, NOT_MODIFIED, OK, FOUND} from '../http/index.js'
import {getPolicy, getPipeline} from './engine.js'
import {getPackument} from './packument.js'
import {normalizePath, gunzip, dropNullEntries, time} from '../util.js'
import {normalizePath, gzip, dropNullEntries, time, jsonBuffer} from '../util.js'
import {hasHit, hasKey, isNoCache} from '../cache.js'
import {getCtx} from '../als.js'
import {checkTarball} from './tarball.js'
Expand Down Expand Up @@ -60,14 +61,14 @@ export const firewall = ({registry, rules, entrypoint: _entrypoint, token}) => a

warmupPipeline(pipeline, boundContext)
const [
{ packumentBuffer, headers, etag, deps, directives },
{ packument, packumentBufferZip, headers, etag, deps, directives },
tarball
] = await Promise.all([
getPackument({ boundContext, rules }),
version ? checkTarball({registry, url: req.url}) : Promise.resolve(false)
])

if (!packumentBuffer) {
if (!packument) {
return next(httpError(NOT_FOUND))
}

Expand Down Expand Up @@ -95,7 +96,9 @@ export const firewall = ({registry, rules, entrypoint: _entrypoint, token}) => a

// Packument request
const isGzip = req.headers['accept-encoding']?.includes('gzip')
const buffer = isGzip ? packumentBuffer : await time(gunzip, `gunzip packument ${name}`)(packumentBuffer)
const buffer = isGzip
? packumentBufferZip || await time(gzip, `gzip packument ${name}`)(jsonBuffer(packument))
: jsonBuffer(packument)
const cl = '' + buffer.length
const extra = isGzip
? {'content-length': cl, 'transfer-encoding': null, 'content-encoding': 'gzip', etag}
Expand Down
12 changes: 5 additions & 7 deletions src/main/js/firewall/packument.js
@@ -1,4 +1,3 @@
import {Buffer} from 'node:buffer'
import crypto from 'node:crypto'

import {getDirectives, getPolicy} from './engine.js'
Expand All @@ -25,22 +24,21 @@ export const getPackument = async ({boundContext, rules}) => {
const deps = getDeps(packument)
const directives = await getDirectives({ packument, rules, boundContext})
const _packument = patchPackument({ packument, directives, entrypoint, registry })
const vkeys = Object.keys(_packument.versions)

if (Object.keys(_packument.versions).length === 0) {
if (vkeys.length === 0) {
return {}
}
const packumentBuffer = Object.keys(_packument.versions).length === Object.keys(packument.versions).length
? buffer
: Buffer.from(JSON.stringify(_packument))
const etag = 'W/' + JSON.stringify(crypto.createHash('sha256').update(packumentBuffer.slice(0, 65_536)).digest('hex'))
const packumentBufferZip = vkeys.length === Object.keys(packument.versions).length && buffer
const etag = 'W/' + JSON.stringify(crypto.createHash('sha256').update(`${packument.name}${vkeys.join(',')}`).digest('hex'))

return {
etag,
deps,
directives,
headers,
packument: _packument,
packumentBuffer,
packumentBufferZip,
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/main/js/util.js
Expand Up @@ -198,3 +198,5 @@ export const time = (fn, label = fn.name) => async (...args) => {
}

export const setFnName = (fn, name) => Object.defineProperty(fn, 'name', { value: name })

export const jsonBuffer = (v) => Buffer.from(JSON.stringify(v))

0 comments on commit c1ce6c2

Please sign in to comment.