GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — [node] make crypto.randomFill[Sync] return type equal to first arg type by kjin · Pull Request #26886 · DefinitelyTyped/DefinitelyTyped

Conversation

@kjin

@kjin kjin commented Jun 27, 2018 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor
  • Use a meaningful title for the pull request. Include the name of the package modified.
  • Test the change in your own code. (Compile and run.)
  • Add or edit tests to reflect the change. (Run with npm test.)
  • Follow the advice from the readme.
  • Avoid common mistakes.
  • Run npm run lint package-name (or tsc if no tslint.json is present).
  • Provide a URL to documentation or source code which provides context for the suggested changes: doc: correct crypto.randomFill() and randomFillSync() nodejs/node#21550

Also see: #26655 (comment)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@kjin kjin changed the title [node] imake crypto.randomFill[Sync] return type equal to first arg type [node] make crypto.randomFill[Sync] return type equal to first arg type Jun 27, 2018
@kjin kjin mentioned this pull request Jun 27, 2018
Update crypto in @types/node to use ArrayBufferView, add stream options to crypto stream types #26655
Merged
2 of 9 tasks
ikokostya
ikokostya reviewed Jun 27, 2018
View reviewed changes
Comment thread types/node/index.d.ts Outdated
export function randomFill(buffer: ArrayBufferView, callback: (err: Error | null, buf: ArrayBufferView) => void): void;
export function randomFill(buffer: ArrayBufferView, offset: number, callback: (err: Error | null, buf: ArrayBufferView) => void): void;
export function randomFill(buffer: ArrayBufferView, offset: number, size: number, callback: (err: Error | null, buf: ArrayBufferView) => void): void;
export function randomFillSync<T extends Buffer|ArrayBufferView>(buffer: T, offset?: number, size?: number): T;

@ikokostya ikokostya Jun 27, 2018

Copy link
Copy Markdown
Contributor

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.

style note: can you add spaces around | for consistency with other code.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@Flarna Flarna Jun 27, 2018

Copy link
Copy Markdown
Contributor

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 don't think that Buffer| is needed at all as ArrayBufferView includes Buffer - if not the definition of quite some APIs in crypto would be wrong.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@typescript-bot typescript-bot added Popular package This PR affects a popular package (as counted by NPM download counts). Awaiting reviewer feedback Author is Owner The author of this PR is a listed owner of the package. labels Jun 27, 2018
@typescript-bot

typescript-bot commented Jun 27, 2018 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

@kjin Thank you for submitting this PR!

🔔 @parambirs @tellnes @WilcoBakker @octo-sniffle @smac89 @Flarna @mwiktorczyk @wwwy3y3 @DeividasBakanas @alvis @eps1lon @Hannes-Magnusson-CK @jkomyno @ajafff @hoo29 @n-e @BrunoScheufler @mohsen1 @KSXGitHub @a-tarasyuk @islishude - please review this PR in the next few days. Be sure to explicitly select Approve or Request Changes in the GitHub UI so I know what's going on.

If no reviewer appears after a week, a DefinitelyTyped maintainer will review the PR instead.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@typescript-bot

typescript-bot commented Jun 27, 2018 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

A definition owner has approved this PR ⭐️. A maintainer will merge this PR shortly. If it shouldn't be merged yet, please leave a comment saying so and we'll wait. Thank you for your contribution to DefinitelyTyped!

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@typescript-bot typescript-bot added The Travis CI build failed and removed Awaiting reviewer feedback labels Jun 27, 2018
@typescript-bot

typescript-bot commented Jun 27, 2018 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

@kjin The Travis CI build failed! Please review the logs for more information.

Once you've pushed the fixes, the build will automatically re-run. Thanks!

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ZaneHannanAU

ZaneHannanAU commented Jun 27, 2018

Copy link
Copy Markdown
Contributor

Should just add realistically. I get that this was technically caused by my change but just add a signature rather than changing the signature entirely.

Also because of how ArrayBufferView is defined it's fine with Buffer so there's no need for it specifically.

export function randomFill<T extends ArrayBufferView>(buf: T, offset?: number, size?: number, callback: (err: Error | null, buf: T) => void): void;
export function randomFill(buf: Buffer, offset?: number, size?: number, callback: (err: Error | null, buf: Buffer) => void): void;
export function randomFillSync<T extends ArrayBufferView>(buf: T, offset?: number, size?: number): T;
export function randomFillSync(buf: Buffer, offset?: number, size?: number): Buffer;

Realistically you could revert then add those where appropriate.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Flarna
Flarna approved these changes Jun 27, 2018
View reviewed changes
@typescript-bot typescript-bot added the Owner Approved A listed owner of this package signed off on the pull request. label Jun 27, 2018
@kjin

kjin commented Jun 27, 2018

Copy link
Copy Markdown
Contributor Author

@ZaneHannanAU @Flarna Buffer doesn't appear to have an API similar to ArrayBufferView at all... unless I'm mistaken? In lib.es5.d.ts:

interface ArrayBufferView {
    /**
      * The ArrayBuffer instance referenced by the array.
      */
    buffer: ArrayBufferLike;

    /**
      * The length in bytes of the array.
      */
    byteLength: number;

    /**
      * The offset in bytes of the array.
      */
    byteOffset: number;
}
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@kjin
[node] make crypto.randomFill[Sync] return type equal to first arg type
feadd37
@kjin
kjin force-pushed the buf-2-buf branch 2 times, most recently from 2a79716 to feadd37 Compare June 27, 2018 22:46
@Flarna

Flarna commented Jun 27, 2018

Copy link
Copy Markdown
Contributor

I'm a little bit confused here. I expected ArrayBufferView is the combination of TypedArrays, Buffer and DataView but actually this is not true.
But typescript allows to pass a Buffer to an interface expecting an ArrayBufferView.
Maybe we should replace ArrayBufferView by something like this which would map to API doc:

type TypedArray = Int8Array | Uint8Array | Uint8ClampedArray | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array | Float64Array;
and then use TypedArray | DataView | Buffer
ikokostya reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ZaneHannanAU

ZaneHannanAU commented Jun 27, 2018

Copy link
Copy Markdown
Contributor

@kjin quite completely mistaken.

ArrayBufferView is just an interface. Anything implementing those three (buffer, byteOffset, byteLength) as those types (ArrayBuffer/ArrayBufferLike, number, number) is taken as such.

… basically yeah ArrayBufferView is fine with anything implementing those three specifically. Buffer, Uint8Array, Uint32Array, DataView, BigInt64Array, Float64Array etc.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@typescript-bot typescript-bot added Merge:Express and removed The Travis CI build failed labels Jun 27, 2018
@Flarna

Flarna commented Jun 28, 2018

Copy link
Copy Markdown
Contributor

In crypto APIs no duck typing is used. If you pass an object fitting the ArrayBufferView interface by setting the 3 properties makes typescript happy but node throws with something like this:

TypeError [ERR_INVALID_ARG_TYPE]: The "buf" argument must be of type ArrayBufferView. Received type object
    at Object.randomFillSync (internal/crypto/random.js:71:11)
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ZaneHannanAU

ZaneHannanAU commented Jun 28, 2018

Copy link
Copy Markdown
Contributor

... realistically if it passes the ArrayBuffer.isView(obj) then yeah. Hrmm.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ZaneHannanAU

ZaneHannanAU commented Jun 28, 2018

Copy link
Copy Markdown
Contributor

I'm guessing it's because they must be readonly?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ikokostya

ikokostya commented Jun 28, 2018 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

I agree with @Flarna for replace Buffer | ArrayBufferView type to Buffer | TypedArray | DataView like in Node.js documentation.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@Flarna

Flarna commented Jun 28, 2018

Copy link
Copy Markdown
Contributor

I think we should do the ArrayBufferView => Buffer | TypedArray | DataView change in a separate PR as it is not directly related to this one.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@paulvanbrenk
paulvanbrenk merged commit f4b16db into DefinitelyTyped:master Jun 28, 2018
@kjin kjin mentioned this pull request Jul 17, 2018
chore(deps): update dependency @types/node to v10.5.2 - autoclosed googleapis/cloud-trace-nodejs#803
Closed
@Flarna Flarna mentioned this pull request Jul 18, 2018
[node] introduce TypedArray #27387
Merged
7 of 9 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

2 more reviewers
@ikokostya ikokostya ikokostya left review comments
@Flarna Flarna Flarna approved these changes
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

Author is Owner The author of this PR is a listed owner of the package. Owner Approved A listed owner of this package signed off on the pull request. Popular package This PR affects a popular package (as counted by NPM download counts).

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

6 participants

@kjin @typescript-bot @ZaneHannanAU @Flarna @ikokostya @paulvanbrenk
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.