Skip to content
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

removed buffer-alloc #166

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions examples/sine.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
* https://web.archive.org/web/20110816002016/http://blogs.msdn.com/b/dawate/archive/2009/06/24/intro-to-audio-programming-part-3-synthesizing-simple-wave-audio-using-c.aspx
*/

const Readable = require('stream').Readable
const bufferAlloc = require('buffer-alloc')
const { Readable } = require('stream')
const { Buffer } = require('buffer')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Buffer is a global in Node.js, I haven't seen much code in the wild that imports it? Is there any upside to that 🤔

Suggested change
const { Buffer } = require('buffer')

Copy link
Author

@jimmywarting jimmywarting Sep 22, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know...

ever since xo started doing it i thought "ok, why not do it?" what could possible be the reason for it?

  • If you don't require/import Buffer then you have the rule that no-undefined or "no global" will throw an error for in many cases/projectsd.
  • it makes it harder for other compiles to "walk" the AST and figure out if something is using some global stuff and then polyfill it.
    • it just makes something as simple as Ryan dhal stated: just include that little extra extension for imports, it makes it much easier for the resolver to figure out what to include
  • You also got the Blob at the buffer module import { Blob, } from 'buffer' so why not use require('buffer').Buffer for the same reason? It's such a small thing that you can include that otherwise makes some task so simple for compilers solve in complex ways. you don't see things like global windows stuff being included into node (cuz it's always node first and DOM second)
    • make it explicitly and you will know what you depend upon
    • i don't know if it can help make some stuff friendlier for Deno also
  • another reason could possible be to use sourcemaps to override what is imported

on 2nd note i think it's better to use Uint8Array + DataView, TextEncoder/Decoder instead :P
better cross comp

const Speaker = require('../')

// the frequency to play
Expand All @@ -33,7 +33,7 @@ function read (n) {
const sampleSize = this.bitDepth / 8
const blockAlign = sampleSize * this.channels
const numSamples = n / blockAlign | 0
const buf = bufferAlloc(numSamples * blockAlign)
const buf = Buffer.alloc(numSamples * blockAlign)
const amplitude = 32760 // Max amplitude for 16-bit audio

// the "angle" used in the function, adjusted for the number of
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
},
"dependencies": {
"bindings": "^1.3.0",
"buffer-alloc": "^1.1.0",
"debug": "^4.0.0"
},
"devDependencies": {
Expand Down
5 changes: 2 additions & 3 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ describe('Speaker', function () {
this.slow(1000)
const s = new Speaker()
let called = false
s.on('close', function () {
s.once('close', function () {
called = true
done()
})
Expand All @@ -96,10 +96,9 @@ describe('Speaker', function () {

it('should accept a device option', function (done) {
const s = new Speaker({ device: 'test' })

assert.strictEqual(s.device, 'test')

s.on('close', done)
s.once('close', done)
s.end(Buffer.alloc(0))
})

Expand Down