-
Notifications
You must be signed in to change notification settings - Fork 199
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(zip): cd header and zip64 info generation implemented #2792
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have added suggestions for aligning coding style with loaders.gl norms. I can see that you prefer a different, arguably more modern style. While there is no "right" style, there is some value in being consistent across a big code base.
@@ -0,0 +1,96 @@ | |||
export const signature = new Uint8Array([0x01, 0x00]); | |||
|
|||
/** info that can be placed into zip64 field */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a link to some specification in the comment?
* @param options info that can be placed into zip64 field | ||
* @returns buffer with field | ||
*/ | ||
export const createZip64Info = (options: Zip64Options): ArrayBuffer => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: Prefer function syntax for longer functions. IIMHO, the => arrow notation is best for callbacks and one liners.,
export const createZip64Info = (options: Zip64Options): ArrayBuffer => { | |
export function createZip64Info(options: Zip64Options): ArrayBuffer { |
|
||
let result = new ArrayBuffer(0); | ||
|
||
zip64Fields.forEach((field) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: Prefer for loops:
zip64Fields.forEach((field) => { | |
for (field of zip64Fields) { |
zip64Length: (options.offset ? 1 : 0) * 8 + (options.size ? 1 : 0) * 16 | ||
}; | ||
|
||
let result = new ArrayBuffer(0); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Repeated concatenation is expensive (you end up copying the same data over and over again). Better to save an array of chunks and concatenate at the end.
let result = new ArrayBuffer(0); | |
const chunks: ArrayBuffer[] = []; | |
... | |
return concatenateArrayBuffers(chunks); |
} | ||
const newValue = new DataView(new ArrayBuffer(field.size)); | ||
setNumbers[field.size](newValue, 0, optionsToUse[field.name ?? ''] ?? field.default); | ||
result = concatArrays(result, newValue.buffer); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agan, avoid repeated concatenation...
|
||
const encodedName = new TextEncoder().encode(optionsToUse.fileName); | ||
|
||
const resHeader = concatArrays(concatArrays(header.buffer, encodedName), zip64header); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Import from loaders.gl/loader-utils, see comment below.
} | ||
const header = new DataView(new ArrayBuffer(46)); | ||
|
||
fields.forEach((field) => |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use for loop, see comment below
if (Object.keys(optionsToZip64).length) { | ||
zip64header = createZip64Info(optionsToZip64); | ||
optionsToUse.extraLength = zip64header.byteLength; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps we can have this function return null if no keys, rather than check length of keys here?
if (Object.keys(optionsToZip64).length) { | |
zip64header = createZip64Info(optionsToZip64); | |
optionsToUse.extraLength = zip64header.byteLength; | |
} | |
const zip64header = createZip64Info(optionsToZip64); | |
if (zip64Header) { | |
optionsToUse.extraLength = zip64header.byteLength; | |
} |
* @param options info that can be placed into cd header | ||
* @returns buffer with header | ||
*/ | ||
export const generateCdHeader = (options: CDGenOptions): ArrayBuffer => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- Use
function
keywors - I recommend preserving capitalization rather than decapitalizing to fit the camelCase.
- Also be consistent with function names and option types
export const generateCdHeader = (options: CDGenOptions): ArrayBuffer => { | |
export function generateCDHeader(options: GenerateCDOptions): ArrayBuffer { |
@@ -188,3 +189,167 @@ const findExpectedData = (zip64data: Zip64Data): {length: number; name: string}[ | |||
|
|||
return zip64dataList; | |||
}; | |||
|
|||
/** info that can be placed into cd header */ | |||
type CDGenOptions = { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
GenerateCDOptions?
Part of changes I'll need in order to create slpk with hash file inside