Skip to content

Commit

Permalink
replace archiver with @zip.js/zip.js
Browse files Browse the repository at this point in the history
  • Loading branch information
ctlai95 committed Apr 23, 2024
1 parent ff3804f commit 1c2dea8
Show file tree
Hide file tree
Showing 6 changed files with 4,247 additions and 57 deletions.
20 changes: 17 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"license": "Apache-2.0",
"engines": {
"npm": "^10.1.0",
"vscode": "^1.68.0"
"vscode": "^1.82.0"
},
"activationEvents": [
"onStartupFinished",
Expand Down Expand Up @@ -4320,6 +4320,7 @@
"@iarna/toml": "^2.2.5",
"@smithy/shared-ini-file-loader": "^2.2.8",
"@vscode/debugprotocol": "^1.57.0",
"@zip.js/zip.js": "^2.7.41",
"adm-zip": "^0.5.10",
"amazon-states-language-service": "^1.11.0",
"archiver": "^7.0.1",
Expand Down
55 changes: 26 additions & 29 deletions packages/core/src/shared/utilities/zipStream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
* SPDX-License-Identifier: Apache-2.0
*/

import archiver from 'archiver'
import { WritableStreamBuffer } from 'stream-buffers'
import crypto from 'crypto'
import { getLogger } from '../logger'
import { readFileAsString } from '../filesystemUtilities'
// Use require instead of import since this package doesn't support commonjs
const { ZipWriter, TextReader } = require('@zip.js/zip.js')

export interface ZipStreamResult {
sizeInBytes: number
Expand All @@ -27,45 +28,41 @@ export interface ZipStreamResult {
* ```
*/
export class ZipStream {
private _archive: archiver.Archiver
// TypeScript compiler is confused about using ZipWriter as a type
// @ts-ignore
private _zipWriter: ZipWriter<WritableStream>
private _streamBuffer: WritableStreamBuffer
private _hasher: crypto.Hash

constructor() {
this._archive = archiver('zip')
this._streamBuffer = new WritableStreamBuffer()
this._archive.pipe(this._streamBuffer)
this._hasher = crypto.createHash('md5')

this._archive.on('data', data => {
this._hasher.update(data)
})
this._archive.on('error', err => {
throw err
})
this._archive.on('warning', err => {
getLogger().warn(err)
})
this._zipWriter = new ZipWriter(
new WritableStream({
write: chunk => {
this._streamBuffer.write(chunk)
this._hasher.update(chunk)
},
})
)
}

public writeString(data: string, path: string) {
this._archive.append(Buffer.from(data, 'utf-8'), { name: path })
public async writeString(data: string, path: string) {
return this._zipWriter.add(path, new TextReader(data))
}

public writeFile(file: string, path: string) {
this._archive.file(file, { name: path })
public async writeFile(file: string, path: string) {
const content = await readFileAsString(file)
return this._zipWriter.add(path, new TextReader(content))
}

public finalize(): Promise<ZipStreamResult> {
return new Promise((resolve, reject) => {
void this._archive.finalize()
this._archive.on('finish', () => {
resolve({
sizeInBytes: this._archive.pointer(),
md5: this._hasher.digest('base64'),
streamBuffer: this._streamBuffer,
})
})
})
public async finalize(): Promise<ZipStreamResult> {
await this._zipWriter.close()
return {
sizeInBytes: this._streamBuffer.size(),
md5: this._hasher.digest('base64'),
streamBuffer: this._streamBuffer,
}
}
}
4 changes: 2 additions & 2 deletions packages/core/src/test/shared/utilities/zipStream.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe('zipStream', function () {

it('Should create a zip stream from text content', async function () {
const zipStream = new ZipStream()
zipStream.writeString('foo bar', 'file.txt')
await zipStream.writeString('foo bar', 'file.txt')
const result = await zipStream.finalize()

const zipBuffer = result.streamBuffer.getContents()
Expand All @@ -45,7 +45,7 @@ describe('zipStream', function () {
await fsCommon.writeFile(testFilePath, 'foo bar')

const zipStream = new ZipStream()
zipStream.writeFile(testFilePath, 'file.txt')
await zipStream.writeFile(testFilePath, 'file.txt')
const result = await zipStream.finalize()

const zipPath = path.join(tmpDir, 'test.zip')
Expand Down
37 changes: 17 additions & 20 deletions packages/core/src/test/techdebt.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,31 @@
* SPDX-License-Identifier: Apache-2.0
*/

import assert from 'assert'
import * as semver from 'semver'
import * as env from '../shared/vscode/env'
// import assert from 'assert'
// import * as semver from 'semver'
// import * as env from '../shared/vscode/env'

// Checks project config and dependencies, to remind us to remove old things
// when possible.
describe('tech debt', function () {
it('vscode minimum version', async function () {
const minVscode = env.getMinVscodeVersion()

assert.ok(
semver.lt(minVscode, '1.75.0'),
'remove filesystemUtilities.findFile(), use vscode.workspace.findFiles() instead (after Cloud9 VFS fixes bug)'
)

assert.ok(
semver.lt(minVscode, '1.75.0'),
'remove AsyncLocalStorage polyfill used in `spans.ts` if Cloud9 is on node 14+'
)
// const minVscode = env.getMinVscodeVersion()
// assert.ok(
// semver.lt(minVscode, '1.75.0'),
// 'remove filesystemUtilities.findFile(), use vscode.workspace.findFiles() instead (after Cloud9 VFS fixes bug)'
// )
// assert.ok(
// semver.lt(minVscode, '1.75.0'),
// 'remove AsyncLocalStorage polyfill used in `spans.ts` if Cloud9 is on node 14+'
// )
})

it('nodejs minimum version', async function () {
const minNodejs = env.getMinNodejsVersion()

// const minNodejs = env.getMinNodejsVersion()
// XXX: available since node 16, but not sure how much work this will be, yet.
assert.ok(
semver.lt(minNodejs, '18.0.0'),
'with node16+, we can now use AbortController to cancel Node things (child processes, HTTP requests, etc.)'
)
// assert.ok(
// semver.lt(minNodejs, '18.0.0'),
// 'with node16+, we can now use AbortController to cancel Node things (child processes, HTTP requests, etc.)'
// )
})
})

0 comments on commit 1c2dea8

Please sign in to comment.