Skip to content
This repository has been archived by the owner on Dec 6, 2022. It is now read-only.

Commit

Permalink
feat: add typedef generation
Browse files Browse the repository at this point in the history
  • Loading branch information
Gozala committed Oct 14, 2020
1 parent 787cff3 commit e31d46b
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 28 deletions.
3 changes: 3 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "ipfs"
}
27 changes: 27 additions & 0 deletions .github/workflows/typecheck.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
on:
push:
branches:
- master
- main
- default
pull_request:
branches:
- '**'

name: Typecheck
jobs:
check:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [12.x]
steps:
- uses: actions/checkout@v1
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- name: Install dependencies
run: npm install
- name: Typecheck
uses: gozala/[email protected]
20 changes: 15 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
"main": "src/index.js",
"scripts": {
"lint": "aegir lint",
"build": "aegir build",
"check": "tsc --noEmit --noErrorTruncation",
"build": "npm run build:js && npm run build:types",
"build:js": "aegir build",
"build:types": "tsc --emitDeclarationOnly --declarationDir dist",
"test": "aegir test",
"test:node": "aegir test --target node",
"test:browser": "aegir test --target browser",
Expand Down Expand Up @@ -34,17 +37,24 @@
},
"homepage": "https://github.com/ipld/js-ipld-block#readme",
"devDependencies": {
"aegir": "^25.0.0",
"uint8arrays": "^1.0.0"
"aegir": "^27.0.0",
"uint8arrays": "^1.0.0",
"typescript": "^4.0.3"
},
"dependencies": {
"cids": "^1.0.0",
"class-is": "^1.1.0"
"cids": "^1.0.0"
},
"engines": {
"node": ">=6.0.0",
"npm": ">=3.0.0"
},
"typesVersions": {
"*": {
"*": [
"dist/types/*"
]
}
},
"contributors": [
"David Dias <[email protected]>",
"Volker Mische <[email protected]>",
Expand Down
75 changes: 54 additions & 21 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
'use strict'

const CID = require('cids')
const withIs = require('class-is')

const { version } = require('../package.json')
const blockSymbol = Symbol.for('@ipld/js-ipld-block/block')
const readonly = { writable: false, configurable: false, enumerable: true }

/**
* Represents an immutable block of data that is uniquely referenced with a cid.
*
* @example
* const block = new Block(Uint8Array.from([0, 1, 2, 3]), new CID('...'))
*/
module.exports = class Block {
class Block {
/**
* @constructor
* @param {Uint8Array} data - The data to be stored in the block as a Uint8Array.
* @param {CID} cid - The cid of the data
*/
Expand All @@ -24,46 +26,77 @@ module.exports = class Block {
throw new Error('second argument must be a CID')
}

this._data = data
this._cid = cid
this.data = data
this.cid = cid

Object.defineProperties(this, {
data: readonly,
cid: readonly
})
}

/**
* The data of this block.
*
* @deprecated
* @type {Uint8Array}
*/
get data () {
return this._data
}

set data (val) {
throw new Error('Tried to change an immutable block')
get _data () {
deprecateData()
return this.data
}

/**
* The cid of the data this block represents.
*
* @deprecated
* @type {CID}
*/
get cid () {
return this._cid
get _cid () {
deprecateCID()
return this.cid
}

set cid (val) {
throw new Error('Tried to change an immutable block')
get [Symbol.toStringTag] () {
return 'Block'
}

get [blockSymbol] () {
return true
}

// eslint-disable-next-line valid-jsdoc
/**
* Check if the given value is a Block.
*
* @param {any} other
* @returns {other is Block}
*/
static isBlock (other) { // eslint-disable-line no-unused-vars
// implemented by class-is module
static isBlock (other) {
return Boolean(other && other[blockSymbol])
}
}

// to trick the typings engine
// https://github.com/ipld/js-ipld-block/pull/55#discussion_r478845002
module.exports = withIs(module.exports, { className: 'Block', symbolName: '@ipld/js-ipld-block/block' })
/**
* @param {RegExp} range
* @param {string} message
* @returns {() => void}
*/
const deprecate = (range, message) => {
let warned = false
return () => {
if (range.test(version)) {
if (!warned) {
warned = true
// eslint-disable-next-line no-console
console.warn(message)
}
} else {
throw new Error(message)
}
}
}

const deprecateCID = deprecate(/^0\.10/, 'block._cid is deprecated and will be removed in the next major release. Use block.cid instead')
const deprecateData = deprecate(/^0\.10/, 'block._data is deprecated and will be removed in the next major release. Use block.data instead')

module.exports = Block
4 changes: 2 additions & 2 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ describe('block', () => {
expect(
() => { b.data = 'fail' }
).to.throw(
/immutable/
/read only/
)

expect(
() => { b.cid = 'fail' }
).to.throw(
/immutable/
/read only/
)
})
})
42 changes: 42 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"compilerOptions": {
"allowJs": true,
"checkJs": true,
"forceConsistentCasingInFileNames": true,
"noImplicitReturns": false,
"noImplicitAny": true,
"noImplicitThis": true,
"noFallthroughCasesInSwitch": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"strictFunctionTypes": false,
"strictNullChecks": true,
"strictPropertyInitialization": true,
"strictBindCallApply": true,
"strict": true,
"alwaysStrict": true,
"esModuleInterop": true,
"target": "ES2018",
"moduleResolution": "node",
"declaration": true,
"declarationMap": true,
"outDir": "dist",
"skipLibCheck": true,
"stripInternal": true,
"resolveJsonModule": true,
"paths": {
"multiformats": [
"src"
]
},
"baseUrl": "."
},
"include": [
"src"
],
"exclude": [
"vendor",
"node_modules"
],
"compileOnSave": false
}

0 comments on commit e31d46b

Please sign in to comment.